pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
82.41k stars 22.17k forks source link

Tolerance setting for test_modules.py -- TestModuleCUDA.test_cpu_cuda_parity #122287

Open ClowDragon opened 6 months ago

ClowDragon commented 6 months ago

šŸ› Describe the bug

@toleranceOverride({torch.float32: tol(5e-2, 0), torch.float64: tol(4e-4, 0)}) test/test_modules.py +529 This testcase test_cpu_gpu_parity only override tolerance of float32/64, however the torch.float16 precision still using the default 1e-5 atol and 0.001 rtol The setting of tol for float16 seems unreasonable for float16, it requires even higher precision than float32. Also, I found # Using @toleranceOverride specific to your test is the recommended way of doing this. These are just some values that worked for test_nn. dtype2prec_DONTUSE = {torch.float: 1e-5, torch.double: 1e-5, torch.half: 1e-2, torch.bfloat16: 1e-1} Should torch.half: 1e-2 be added to this decorator for testcase test_cpu_cuda_parity?

Versions

Collecting environment information... PyTorch version: 2.2.0a0+81ea7a4 Is debug build: False CUDA used to build PyTorch: 12.3 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.28.1 Libc version: glibc-2.35

Python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (64-bit runtime) Python platform: Linux-5.11.0-27-generic-x86_64-with-glibc2.35 Is CUDA available: True CUDA runtime version: 12.3.107 CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: Tesla V100-SXM2-16GB GPU 1: Tesla V100-SXM2-16GB GPU 2: Tesla V100-SXM2-16GB GPU 3: Tesla V100-SXM2-16GB

Nvidia driver version: 535.104.05 cuDNN version: Probably one of the following: /usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.7 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: 46 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 64 On-line CPU(s) list: 0-63 Vendor ID: GenuineIntel Model name: Intel(R) Xeon(R) Gold 6130 CPU @ 2.10GHz CPU family: 6 Model: 85 Thread(s) per core: 2 Core(s) per socket: 16 Socket(s): 2 Stepping: 4 CPU max MHz: 3700.0000 CPU min MHz: 1000.0000 BogoMIPS: 4200.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 dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke md_clear flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 1 MiB (32 instances) L1i cache: 1 MiB (32 instances) L2 cache: 32 MiB (32 instances) L3 cache: 44 MiB (2 instances) NUMA node(s): 2 NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62 NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63 Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Meltdown: Mitigation; PTI 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; Full generic retpoline, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable

Versions of relevant libraries: [pip3] numpy==1.24.4 [pip3] onnx==1.15.0rc2 [pip3] optree==0.10.0 [pip3] pytorch-quantization==2.1.2 [pip3] torch==2.2.0a0+81ea7a4 [pip3] torch-tensorrt==2.2.0a0 [pip3] torchdata==0.7.0a0 [pip3] torchtext==0.17.0a0 [pip3] torchvision==0.17.0a0 [pip3] triton==2.1.0+6e4932c [conda] Could not collect

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki

ClowDragon commented 6 months ago

`import torch from torch.testing._internal.common_nn import cross_entropy_loss_reference

input1 = torch.load('/root/input_ce.pt', map_location=torch.device('cpu')).cuda() target = torch.load('/root/target_ce.pt', map_location=torch.device('cpu')).cuda() weight = torch.load('/root/weight_ce.pt', map_location=torch.device('cpu')).cuda()

result = cross_entropy_loss_reference(input1, target, weight, -100, 'mean', 0) print("reference result is: ", result)

loss = torch.nn.CrossEntropyLoss(weight=weight, ignore_index=-100, reduction='mean', label_smoothing=0) torch_result = loss(input1, target) print("torch result is: ", torch_result) `

Also, for test case, test_modules_py_TestModuleCUDA_test_forward_nn_CrossEntropyLoss_cuda_float16 I generate a sample input that gives Absolute difference: 0.001708984375 (up to 1e-05 allowed) The script I use is provided above, which shows the result: reference result is: tensor(-0.3103, device='cuda:0', dtype=torch.float16) torch result is: tensor(-0.3120, device='cuda:0', dtype=torch.float16)

I guess this may be the same case about float16 Tolerance setting.

pt files are provided here: tensor_files.zip

mikaylagawarecki commented 6 months ago

This seems reasonable indeed, I think the reason we have not caught this thus far is that most modules tested in test_modules.py are only tested for torch.float32 and torch.float64, which is on my list to fix https://github.com/pytorch/pytorch/issues/116626#issuecomment-1874399452

So far I have only enabled float16 testing for MPS. Let me know if you are keen to help with this :)

ClowDragon commented 6 months ago

@mikaylagawarecki I have created the pull request, https://github.com/pytorch/pytorch/pull/122596 Do you mind helping me review that pr?