IntelPython / dpctl

Python SYCL bindings and SYCL-based Python Array API library
https://intelpython.github.io/dpctl/
Apache License 2.0
101 stars 30 forks source link

`dlpack` interface with `pytorch` has unexpected behavior with F-aligned arrays ? #1241

Closed fcharras closed 1 year ago

fcharras commented 1 year ago

Reproducer:

import dpctl.tensor as dpt
import intel_extension_for_pytorch
import torch
array_dpt_gpu = dpt.reshape(dpt.arange(1000, device="gpu", dtype=dpt.float32), (4, 250))
array_dpt_gpu_F_aligned = dpt.asarray(array_dpt_gpu, order="F")
array_torch = dpt.from_dlpack(array_dpt_gpu_F_aligned)

where array_torch first (truncated) row outputs:

tensor([[  0., 250., 500., 750.,   1., 251., 501., 751.,   2., 252., 502., 752.,
           3., 253., 503., 753.,   4., 254., 504., 754.,   5., 255., 505., 755.,
           6., 256., 506., 756.,   7., 257., 507., 757.,   8., 258., 508., 758.,
           9., 259., 509., 759.,  10., 260., 510., 760.,  11., 261., 511., 761.,

which is different from array_dpt_gpu or array_dpt_gpu_F_aligned:

usm_ndarray([[  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,
               10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,
               20.,  21.,  22.,  23.,  24.,  25.,  26.,  27.,  28.,  29.,
               30.,  31.,  32.,  33.,  34.,  35.,  36.,  37.,  38.,  39.,

it seems that from_dlpack in pytorch can't see F-alignments and assumes C-contiguous buffer instead.

fcharras commented 1 year ago

Cross posted with intel extension for pytorch repo at https://github.com/intel/intel-extension-for-pytorch/issues/369

Environment informations:

Collecting environment information...
PyTorch version: 1.13.0a0+git49444c3
PyTorch CXX11 ABI: Yes
IPEX version: 1.13.120+gitb243ae3
IPEX commit: b243ae39
Build type: Release

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Clang version: N/A
IGC version: 2023.1.0 (2023.1.0.20230320)
CMake version: version 3.26.3
Libc version: glibc-2.35

Python version: 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-6.3.2-arch1-1-x86_64-with-glibc2.35
Is XPU available: True
DPCPP runtime version: N/A
MKL version: N/A
GPU models and configuration: 
[0] _DeviceProperties(name='Intel(R) Iris(R) Xe Graphics [0x9a49]', platform_name='Intel(R) Level-Zero', dev_type='gpu, support_fp64=0, total_memory=12544MB, max_compute_units=96, gpu_eu_count=96)
Intel OpenCL ICD version: 22.43.24595.30
Level Zero version: 1.3.24595.30

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:                      11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
CPU family:                      6
Model:                           140
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
Stepping:                        1
CPU max MHz:                     4700.0000
CPU min MHz:                     400.0000
BogoMIPS:                        5608.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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx 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 cat_l2 invpcid_single cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves split_lock_detect dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid movdiri movdir64b fsrm avx512_vp2intersect md_clear ibt flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       192 KiB (4 instances)
L1i cache:                       128 KiB (4 instances)
L2 cache:                        5 MiB (4 instances)
L3 cache:                        12 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-7
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced / Automatic IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] intel-extension-for-pytorch==1.13.120+gitb243ae3
[pip3] numpy==1.24.3
[pip3] torch==1.13.0a0+git49444c3
[pip3] torchvision==0.14.1a0+5e8e2f1
[conda] intel-extension-for-pytorch 1.13.120+gitb243ae3          pypi_0    pypi
[conda] mkl                       2023.1.0            intel_46342    intel
[conda] mkl-dpcpp                 2023.1.0            intel_46342    intel
[conda] numpy                     1.23.5          py310h53a5b5f_0    conda-forge
[conda] torch                     1.13.0a0+git49444c3          pypi_0    pypi
[conda] torchvision               0.14.1a0+5e8e2f1          pypi_0    pypi
fcharras commented 1 year ago

It's probably an issue on dpctl side, I've noticed that dpt.from_dlpack(array_dpt_gpu_F_aligned) has the same issues.

oleksandr-pavlyk commented 1 year ago

Smaller reproducer:

In [9]: array_dpt_gpu = dpt.reshape(dpt.arange(10, device="gpu", dtype=dpt.float32), (2, 5))
   ...: array_dpt_gpu_F_aligned = dpt.asarray(array_dpt_gpu, order="F")

In [10]: y = dpt.from_dlpack(array_dpt_gpu_F_aligned)

In [11]: array_dpt_gpu
Out[11]:
usm_ndarray([[0., 1., 2., 3., 4.],
             [5., 6., 7., 8., 9.]], dtype=float32)

In [12]: array_dpt_gpu_F_aligned
Out[12]:
usm_ndarray([[0., 1., 2., 3., 4.],
             [5., 6., 7., 8., 9.]], dtype=float32)

In [13]: y
Out[13]:
usm_ndarray([[0., 5., 1., 6., 2.],
             [7., 3., 8., 4., 9.]], dtype=float32)
oleksandr-pavlyk commented 1 year ago

When encoding the array into DLPack structure, both 'F'-contiguous and 'C'-contiguous arrays have null strides pointer.

The exporter must populate the strides vector in DLPack when exporting 'F'-contiguous arrays, since null strides pointer is reserved to signal the C-contiguous allocation.