Deci-AI / super-gradients

Easily train or fine-tune SOTA computer vision models with one open source training library. The home of Yolo-NAS.
https://www.supergradients.com
Apache License 2.0
4.54k stars 496 forks source link

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

Closed tomwang221812 closed 1 year ago

tomwang221812 commented 1 year ago

🐛 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: https://github.com/Deci-AI/super-gradients/blob/6f0a66f9e830ca55c8ec57e4ded1b45171226472/src/super_gradients/training/models/detection_models/yolo_nas/dfl_heads.py#L284-L291

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
BloodAxe commented 1 year ago

Thanks for your bugreport @tomwang221812 We will fix that!

tomwang221812 commented 1 year ago

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 commented 1 year ago

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

BloodAxe commented 1 year ago

Fixed in 3.2.1