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.

TorchEmbedding cannot be deep copied #1131

Closed

Description

@Jackmin801

Describe the bug
TorchEmbedding cannot be deep copied.

To Reproduce
script.py

import torch
from docarray import BaseDocument
from docarray.typing.tensor.embedding.torch import TorchEmbedding

class Meow(BaseDocument):
    embedding : TorchEmbedding

a = Meow(embedding=torch.randn(42))
a.copy(deep=True)

output

Traceback (most recent call last):
  File "/Users/jackmin/Jina/docarray/oink.py", line 16, in <module>
    a.copy(deep=True)
  File "pydantic/main.py", line 657, in pydantic.main.BaseModel.copy
  File "pydantic/main.py", line 612, in pydantic.main.BaseModel._copy_and_set_values
  File "/Users/jackmin/miniconda3/envs/docarray/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/jackmin/miniconda3/envs/docarray/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/jackmin/miniconda3/envs/docarray/lib/python3.10/copy.py", line 153, in deepcopy
    y = copier(memo)
  File "/Users/jackmin/miniconda3/envs/docarray/lib/python3.10/site-packages/torch/_tensor.py", line 100, in __deepcopy__
    return handle_torch_function(Tensor.__deepcopy__, (self,), self, memo)
  File "/Users/jackmin/miniconda3/envs/docarray/lib/python3.10/site-packages/torch/overrides.py", line 1534, in handle_torch_function
    result = torch_func_method(public_api, types, args, kwargs)
  File "/Users/jackmin/Jina/docarray/docarray/typing/tensor/torch_tensor.py", line 225, in __torch_function__
    return super().__torch_function__(func, types_, args, kwargs)
  File "/Users/jackmin/miniconda3/envs/docarray/lib/python3.10/site-packages/torch/_tensor.py", line 1279, in __torch_function__
    ret = func(*args, **kwargs)
  File "/Users/jackmin/miniconda3/envs/docarray/lib/python3.10/site-packages/torch/_tensor.py", line 185, in __deepcopy__
    raise RuntimeError(
RuntimeError: The default implementation of __deepcopy__() for non-wrapper subclasses only works for subclass types that implement new_empty() and for which that function returns another instance of the same subclass. You should either properly implement new_empty() for your subclass or override __deepcopy__() if it is intended behavior for new_empty() to return an instance of a different type.

Expected behavior
No error

Activity

  1. self-assigned this
    on Feb 16, 2023
  2. moved this from Todo to In Progress in DocArray backlogon Feb 16, 2023
  3. moved this from In Progress to Done in DocArray backlogon Feb 21, 2023
  4. gusseppe commented on Jul 20, 2023

    @gusseppe

    Describe the bug
    TorchTensor cannot be deep copied.

    docarray.version
    '0.36.0'

    Related:
    #1143

    To Reproduce
    script.py

    import torch
    from docarray import BaseDoc
    from docarray.typing import TorchTensor
    class Meow(BaseDoc):
        embedding : TorchTensor
    
    a = Meow(embedding=torch.randn(42))
    print(id(a.embedding), id(a.copy(deep=True).embedding))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions