8000 `torch.asarray` `copy=True` parameter has no effect if `device=` is not `None` or `cpu` · Issue #108408 · pytorch/pytorch · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

torch.asarray copy=True parameter has no effect if device= is not None or cpu #108408

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

Closed
fcharras opened this issue Sep 1, 2023 · 2 comments
Closed
Assignees
Labels
high priority module: correctness (silent) issue that returns an incorrect result silently module: python frontend For issues relating to PyTorch's Python frontend triage review triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@fcharras
Copy link
fcharras commented Sep 1, 2023

🐛 Describe the bug

array = torch.empty(10, 10, dtype=torch.float32, device="cuda")
print(array.data_ptr())
array_2 = torch.asarray(array, copy=True, device="cuda")
print(array_2.data_ptr())

print the same pointers, for instance:

140345826344960
140345826344960

showing that array and array_2 share the same memory (and indeed mutating one will mutate the other), despite passing copy=True.

(edit: this follow-up snippet

array[0,0] = 0
array_2[0,0] = 10
print(array[0,0])

indeed prints tensor(10., device='cuda:0') but it should print tensor(0., device='cuda:0')
)

Passing device="cpu" or device=None (even if the default device has been set to "cuda" using torch.set_default_device) does not exhibit the bug.

The issue happens not only with device="cuda" but also with device="mps" or device="xpu" (relevant issue) providing the respective backends have been installed. So the issue seems to happen before the call is dispatched to the respective backend.

Quoting another asarray issue that has been recently reported and fixed in #106773 :

The asarray function is quite important, because it allows writing library-agnostic code and asarray is one of the most heavily used functions in test suites when doing so.

Versions: latest installed from mamba using get-started instructions:

mamba create -n my_torch_env pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

Versions

Collecting environment information...
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: linux (x86_64)
GCC version: (GCC) 9.3.0
Clang version: Could not collect
CMake version: version 3.20.2
Libc version: glibc-2.28

Python version: 3.11.5 | packaged by conda-forge | (main, Aug 27 2023, 03:34:09) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-4.18.0-394.el8.x86_64-x86_64-with-glibc2.28
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A100-PCIE-40GB
Nvidia driver version: 515.48.07
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.8.2.0
/usr/lib64/libcudnn_adv_infer.so.8.2.0
/usr/lib64/libcudnn_adv_train.so.8.2.0
/usr/lib64/libcudnn_cnn_infer.so.8.2.0
/usr/lib64/libcudnn_cnn_train.so.8.2.0
/usr/lib64/libcudnn_ops_infer.so.8.2.0
/usr/lib64/libcudnn_ops_train.so.8.2.0
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
Byte Order:          Little Endian
CPU(s):              64
On-line CPU(s) list: 0-63
Thread(s) per core:  2
Core(s) per socket:  16
Socket(s):           2
NUMA node(s):        2
Vendor ID:           AuthenticAMD
CPU family:          23
Model:               49
Model name:          AMD EPYC 7302 16-Core Processor
Stepping:            0
CPU MHz:             3000.000
CPU max MHz:         3000.0000
CPU min MHz:         1500.0000
BogoMIPS:            5988.97
Virtualization:      AMD-V
L1d cache:           32K
L1i cache:           32K
L2 cache:            512K
L3 cache:            16384K
NUMA node0 CPU(s):   0-15,32-47
NUMA node1 CPU(s):   16-31,48-63
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd amd_ppin arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip rdpid overflow_recov succor smca sme sev sev_es

Versions of relevant libraries:
[pip3] numpy==1.25.2
[pip3] torch==2.0.1
[pip3] torchaudio==2.0.2
[pip3] torchvision==0.15.2
[pip3] triton==2.0.0
[conda] blas                      2.116                       mkl    conda-forge
[conda] blas-devel                3.9.0            16_linux64_mkl    conda-forge
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] libblas                   3.9.0            16_linux64_mkl    conda-forge
[conda] libcblas                  3.9.0            16_linux64_mkl    conda-forge
[conda] liblapack                 3.9.0            16_linux64_mkl    conda-forge
[conda] liblapacke                3.9.0            16_linux64_mkl    conda-forge
[conda] mkl                       2022.1.0           h84fe81f_915    conda-forge
[conda] mkl-devel                 2022.1.0           ha770c72_916    conda-forge
[conda] mkl-include               2022.1.0           h84fe81f_915    conda-forge
[conda] numpy                     1.25.2          py311h64a7726_0    conda-forge
[conda] pytorch                   2.0.1           py3.11_cuda11.8_cudnn8.7.0_0    pytorch
[conda] pytorch-cuda              11.8                 h7e8668a_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.0.2               py311_cu118    pytorch
[conda] torchtriton               2.0.0                     py311    pytorch
[conda] torchvision               0.15.2              py311_cu118    pytorch

cc @ezyang @gchanan @zou3519 @kadeng @albanD

@fcharras
Copy link
Author
fcharras commented Sep 1, 2023

cc @rgommers in case you face the same issue with Array API tests

@FFFrog
Copy link
Collaborator
FFFrog commented Sep 4, 2023

@fcharras, the reason is that the index of the device is not specified, and it is judged that it is copied across devices, but in the end, it is found that it belongs to the same device during the copy process, and finally the fast path of function to is triggered and returns directly instead of copying.

bool wrong_device = device.has_value() && device.value() != tensor.device();
bool wrong_dtype =
dtype.has_value() && dtype.value() != tensor.scalar_type();
bool needs_copying = !copy.has_value() && (wrong_device || wrong_dtype);
// Given a defined tensor, we copy it if either we have to (copy=True) or
// if we need to (copy=None) because of mismatched device or dtype.
if (force_copy || needs_copying) {
if (wrong_device || wrong_dtype) {
tensor = tensor.to(
device.value_or(tensor.device()),
dtype.value_or(tensor.scalar_type()));

Tensor to(const Tensor& self, Device device, ScalarType dtype, bool non_blocking, bool copy, c10::optional<c10::MemoryFormat> optional_memory_format) {
return to_impl(
self,
dtype,
nullopt,
ensure_has_index(device),
nullopt,
non_blocking,
copy,
optional_memory_format);
}

static inline Tensor to_impl(
const Tensor& self,
c10::optional<ScalarType> dtype,
c10::optional<Layout> layout,
c10::optional<Device> device,
c10::optional<bool> pin_memory,
bool non_blocking,
bool copy,
c10::optional<c10::MemoryFormat> optional_memory_format) {
// fast path
if (to_will_alias(self, dtype, layout, device, copy, optional_memory_format)) {
return self;
}
return at::_to_copy(
self, dtype, layout, device, pin_memory, non_blocking, optional_memory_format);
}

@FFFrog FFFrog self-assigned this Sep 4, 2023
@soulitzer soulitzer added high priority triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module module: correctness (silent) issue that returns an incorrect result silently module: python frontend For issues relating to PyTorch's Python frontend labels Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
high priority module: correctness (silent) issue that returns an incorrect result silently module: python frontend For issues relating to PyTorch's Python frontend triage review triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0