Skip to content
This repository was archived by the owner on Sep 9, 2026. It is now read-only.
This repository was archived by the owner on Sep 9, 2026. It is now read-only.

TorchTensor deepcopy raises error when dtype is not float32 #1831

Closed

Description

@igamenovoer

Initial Checks

  • I have read and followed the docs and still think this is a bug

Description

when calling copy.deepcopy() over TorchTensor, it fails when tensor.dtype != torch.float32.
The problem is that TorchTensor.new_empty() will create a float32 tensor no matter what, regardless of self.dtype
image

Example Code

import torch
from docarray.typing.tensor.torch_tensor import TorchTensor
import copy

x = TorchTensor(torch.randn(2,3))
y = copy.deepcopy(x)    # this works

xx = TorchTensor(torch.randint(0,100,(2,3)))
yy = copy.deepcopy(xx)  # RuntimeError: Expected a Storage of type float or an UntypedStorage, but got type Long for argument 1 'storage'

Python, DocArray & OS Version

0.39.1

Affected Components

Activity

  1. JoanFM commented on Nov 14, 2023

    @JoanFM
    Member

    Hey @igamenovoer,

    Thanms for reporting the issue. Since you spotted the problem, would you like to contribute the fix?

  2. ai-naymul commented on Nov 30, 2023

    @ai-naymul
    Contributor

    I think we need to add a custom implementation of deepcopy for TorchTensor to avoid storage-sharing issues.

    @JoanFM could you please help me to contribute in this?

    Here is my approach:

    1. Create a deepcopy methods for that
    2. create a new tensor with the same data and properties
    3. Set the class to the custom Torchtensor class
    4. return the new tensor
  3. JoanFM commented on Nov 30, 2023

    @JoanFM
    Member

    what do u mean by a deepcopy class for that?

  4. ai-naymul commented on Nov 30, 2023

    @ai-naymul
    Contributor

    what do u mean by a deepcopy class for that?

    Oh, I am sorry I was mentioning the method, It's was a typing mistake.

  5. JoanFM commented on Nov 30, 2023

    @JoanFM
    Member

    the approach seems to make sense indeed

  6. ai-naymul commented on Nov 30, 2023

    @ai-naymul
    Contributor

    the approach seems to make sense indeed

    So should I try the approach?

  7. JoanFM commented on Nov 30, 2023

    @JoanFM
    Member

    yws

  8. ai-naymul commented on Dec 2, 2023

    @ai-naymul
    Contributor

    yws

    @JoanFM would you mind having a look at the PR, please?

  9. linked a pull request that will close this issuefix: fix storage issue in torchtensor class #1833on Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions