Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

❓ Unable to freeze tensor of type Int64/Float64 into constant layer, try to compile model with truncate_long_and_double enabled? #2117

Closed
panahikhas opened this issue Jul 15, 2023 · 2 comments
Labels
question Further information is requested

Comments

@panahikhas
Copy link

panahikhas commented Jul 15, 2023

❓ RuntimeError: [Error thrown at core/conversion/converters/converter_util.cpp:251] Unable to freeze tensor of type Int64/Float64 into constant layer, try to compile model with truncate_long_and_double enabled:

  1. A pre-trained Torch model like Resnet18 was loaded
  2. The model was quantized using pytorch_quantization.quant_modules.initialize()
  3. The quantized model was calibrated
  4. The model was fine-tuned (QAT)
  5. I tried to convert the fine-tuned model to TensorRT using
    trt_mod = torch_tensorrt.compile(qat_model, inputs=[torch_tensorrt.Input([32, 3, 32, 32])], enabled_precisions={torch.int8})
    but I encountered the error below:

File "/home/i2027/anaconda3/envs/p/lib/python3.10/site-packages/torch_tensorrt/_compile.py", line 133, in compile
return torch_tensorrt.ts.compile(
File "/home/i2027/anaconda3/envs/p/lib/python3.10/site-packages/torch_tensorrt/ts/_compiler.py", line 139, in compile
compiled_cpp_mod = _C.compile_graph(module._c, _parse_compile_spec(spec))
RuntimeError: [Error thrown at core/conversion/converters/converter_util.cpp:251] Unable to freeze tensor of type Int64/Float64 into constant layer, try to compile model with truncate_long_and_double enabled

I have checked the model parameters and all of them were of type float32. I don't know why TorchTensorRT complains about Int64/Float64! Please note that I have managed to convert a simple CNN to TensorRT using the method described above successfully. However, I failed to convert an existing torchvision model using the steps above. I will be grateful for any hint.

Environment

  • PyTorch Version: 2.0.1+cu118

  • CPU Architecture: x86

  • OS: Ubuntu 20.04

  • How you installed PyTorch: pip

  • Python version: 3.10.11

  • CUDA version: 12.1

  • GPU models and configuration: GeForce GTX 1080 - 12 GB

  • All pckages versions:

    • torch==2.0.1+cu118
    • torch_tensorrt==1.4.0
    • torchvision==0.15.2+cu118
    • pytorch_quantization==2.1.2
    • torchvision==0.15.2+cu118

Additional context

The code is available at https://github.com/panahikhas/TensorRT-QAT/blob/main/torch-tensorrt-QAT.py to reproduce the results.

@panahikhas panahikhas added the question Further information is requested label Jul 15, 2023
@narendasan
Copy link
Collaborator

narendasan commented Jul 17, 2023

Model parameters and all of them were of type float32.

truncate_long_and_double does not apply just to model weights, it also deals with things such as tensor indexing which in PyTorch/TorchScript happens in int64 but when we convert to tensorrt this needs to be in int32.

What happens if you add truncate_long_and_double=True to your compile settings?

@panahikhas
Copy link
Author

Thank you very much for your response.
If I add the argument truncate_long_and_double=True the accuracy become 10% from 80%.
I don't know the reason but the problem was from PyCharm. I was working with it and it had errors. But when I used the terminal or VSCode there was no problem and no warning/error about int64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants