Skip to content

BUG: .from_dlpack does not set writeable flag for pytorch tensors #29474

@lahwaacz

Description

@lahwaacz

Describe the issue:

All arrays from other libraries imported with from_dlpack come out as non-writable.

#28599 fixed a similar bug, but only for passing a numpy array into from_dlpack. The issue still exists when passing an external writable array, such as torch.tensor. Note that pytorch actually dos not support non-writable tensors.

Reproduce the code example:

import torch
import numpy as np
x = torch.arange(1)
y = np.from_dlpack(x)
y[0] = 1
y.flags.writeable = True

Error message:

Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    y[0] = 1
    ~^^^
ValueError: assignment destination is read-only

Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    y.flags.writeable = True
    ^^^^^^^^^^^^^^^^^
ValueError: cannot set WRITEABLE flag to True of this array

Python and NumPy Versions:

2.3.2
3.13.5 (main, Jun 21 2025, 09:35:00) [GCC 15.1.1 20250425]

Runtime Environment:

[{'numpy_version': '2.3.2',
'python': '3.13.5 (main, Jun 21 2025, 09:35:00) [GCC 15.1.1 20250425]',
'uname': uname_result(system='Linux', node='node', release='6.15.5-arch1-1', version='#1 SMP PREEMPT_DYNAMIC Sun, 06 Jul 2025 11:14:36 +0000', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL',
'AVX512_SPR']}}]

Context for the issue:

This issue prevents the use of numpy for writing into aliased data from other tensor libraries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions