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

YOLO-NAS generate float64 constants after export to ONNX #1392

Closed
tomwang221812 opened this issue Aug 18, 2023 · 4 comments
Closed

YOLO-NAS generate float64 constants after export to ONNX #1392

tomwang221812 opened this issue Aug 18, 2023 · 4 comments
Labels
🐛 Bug Something isn't working Fixed-In-3.2.1 Fixed in 3.2.1 release YoloNAS

Comments

@tomwang221812
Copy link

🐛 Describe the bug

Hi,

Export Code:

model = models.get(Models.YOLO_NAS_S, pretrained_weights="coco")
export_result = model.export(output = "yolo_nas_s.onnx", 
                             engine = 'onnxruntime',
                             device = 'cuda',
                             onnx_simplify = True,
                             preprocessing = False, 
                             postprocessing = False, 
                             output_predictions_format = DetectionOutputFormatMode.BATCH_FORMAT,
                             onnx_export_kwargs = {'opset_version': 11, 'verbose': False},
                             batch_size = 1,
                             input_image_shape = (640, 640)
                            )

I found that the exported onnx model will create some Expand OPs with data type float64 and maybe we don't need to cast the data to float32 before Concat if torch.meshgrid takes the same data type as input tensor?

image

The related code is in:

shift_x = torch.arange(end=w) + self.grid_cell_offset
shift_y = torch.arange(end=h) + self.grid_cell_offset
if torch_version_is_greater_or_equal(1, 10):
shift_y, shift_x = torch.meshgrid(shift_y, shift_x, indexing="ij")
else:
shift_y, shift_x = torch.meshgrid(shift_y, shift_x)
anchor_point = torch.stack([shift_x, shift_y], dim=-1).to(dtype=dtype)

After some experiment I found that the data type casting must be earlier than torch.meshgrid to resolve this issue.

            shift_x = torch.arange(end=w, dtype=dtype) + self.grid_cell_offset
            shift_y = torch.arange(end=h, dtype=dtype) + self.grid_cell_offset
            if torch_version_is_greater_or_equal(1, 10):
                shift_y, shift_x = torch.meshgrid(shift_y, shift_x, indexing="ij")
            else:
                shift_y, shift_x = torch.meshgrid(shift_y, shift_x)

            anchor_point = torch.stack([shift_x, shift_y], dim=-1)

After that the Expand OP data type should be float32:

image

I think most of the time this is not an issue but some compiler of AI accelerator seems lack of float64 data type support that makes the compiler stop compiling.

Versions

Docker image: nvcr.io/nvidia/pytorch:23.07-py3

PyTorch version: 2.1.0a0+b5021ba
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.26.4
Libc version: glibc-2.35

Python version: 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-76-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.1.105
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: Quadro RTX 4000
Nvidia driver version: 525.125.06
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.3
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   39 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          8
On-line CPU(s) list:             0-7
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
CPU family:                      6
Model:                           158
Thread(s) per core:              1
Core(s) per socket:              8
Socket(s):                       1
Stepping:                        13
CPU max MHz:                     4700.0000
CPU min MHz:                     800.0000
BogoMIPS:                        6000.00
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       256 KiB (8 instances)
L1i cache:                       256 KiB (8 instances)
L2 cache:                        2 MiB (8 instances)
L3 cache:                        12 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-7
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT disabled
Vulnerability Retbleed:          Mitigation; Enhanced IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Mitigation; Microcode
Vulnerability Tsx async abort:   Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] numpy==1.22.2
[pip3] pytorch-quantization==2.1.2
[pip3] torch==2.1.0a0+b5021ba
[pip3] torch-tensorrt==1.5.0.dev0
[pip3] torchdata==0.7.0a0
[pip3] torchmetrics==0.8.0
[pip3] torchtext==0.16.0a0
[pip3] torchvision==0.16.0a0
[pip3] triton==2.1.0
[conda] Could not collect
@bit-scientist bit-scientist added the 🐛 Bug Something isn't working label Aug 19, 2023
@BloodAxe BloodAxe added the TODO Probably should be done, but not planned yet label Aug 23, 2023
@BloodAxe
Copy link
Contributor

Thanks for your bugreport @tomwang221812
We will fix that!

@BloodAxe BloodAxe added the Planned The fix is on the roadmap label Aug 24, 2023
BloodAxe added a commit that referenced this issue Aug 24, 2023
BloodAxe added a commit that referenced this issue Aug 24, 2023
* Fix wrong type annotation for quantization_mode

* Fix initialization of example_input_image

* Added provides to stay compatible with newer onnxruntime that require provides to be present

* Fixed edge case of exporting detection models of small size and num_pre_nms_predictions that exceeds this value

* Change initilization of grid in yolox/yolonas/ppyoloe to ensure that constants in ONNX file is fp32 and not fp64.
#1392
@tomwang221812
Copy link
Author

Hi,

The device for torch.arange may need to be initialized as well

shift_x = torch.arange(end=w, dtype=dtype, device=device) + self.grid_cell_offset
...

Thanks.

@BloodAxe
Copy link
Contributor

The PR with a fix was already merged and will be released as 3.2.1 probably this week.

@BloodAxe
Copy link
Contributor

BloodAxe commented Sep 7, 2023

Fixed in 3.2.1

@BloodAxe BloodAxe closed this as completed Sep 7, 2023
@BloodAxe BloodAxe added YoloNAS Fixed-In-3.2.1 Fixed in 3.2.1 release and removed Planned The fix is on the roadmap TODO Probably should be done, but not planned yet labels Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Something isn't working Fixed-In-3.2.1 Fixed in 3.2.1 release YoloNAS
Projects
None yet
Development

No branches or pull requests

3 participants