Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8dd050f

Browse filesBrowse files
authored
fix: detach the torch tensors (#1526)
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
1 parent 9705431 commit 8dd050f
Copy full SHA for 8dd050f

2 files changed

+60-2Lines changed: 60 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docarray/typing/tensor/torch_tensor.py‎

Copy file name to clipboardExpand all lines: docarray/typing/tensor/torch_tensor.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _docarray_to_json_compatible(self) -> np.ndarray:
137137
Convert `TorchTensor` into a json compatible object
138138
:return: a representation of the tensor compatible with orjson
139139
"""
140-
return self.numpy() ## might need to check device later
140+
return self.detach().numpy() # might need to check device later
141141

142142
def unwrap(self) -> torch.Tensor:
143143
"""
Collapse file

‎tests/units/typing/tensor/test_torch_tensor.py‎

Copy file name to clipboardExpand all lines: tests/units/typing/tensor/test_torch_tensor.py
+59-1Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
import json
2+
13
import pytest
24
import torch
35
from pydantic.tools import parse_obj_as, schema_json_of
46

7+
from docarray import BaseDoc
58
from docarray.base_doc.io.json import orjson_dumps
9+
from docarray.proto import DocProto
610
from docarray.typing import TorchEmbedding, TorchTensor
711

812

13+
class MyDoc(BaseDoc):
14+
tens: TorchTensor
15+
16+
917
@pytest.mark.proto
1018
def test_proto_tensor():
1119
tensor = parse_obj_as(TorchTensor, torch.zeros(3, 224, 224))
@@ -63,7 +71,7 @@ def test_wrong_but_reshapable():
6371
parse_obj_as(TorchTensor[3, 224, 224], torch.zeros(224, 224))
6472

6573

66-
def test_inependent_variable_dim():
74+
def test_independent_variable_dim():
6775
# test independent variable dimensions
6876
tensor = parse_obj_as(TorchTensor[3, 'x', 'y'], torch.zeros(3, 224, 224))
6977
assert isinstance(tensor, TorchTensor)
@@ -177,3 +185,53 @@ class MMdoc(BaseDoc):
177185

178186
doc_copy.embedding = torch.randn(32)
179187
assert not (doc.embedding == doc_copy.embedding).all()
188+
189+
190+
@pytest.mark.parametrize('requires_grad', [True, False])
191+
def test_json_serialization(requires_grad):
192+
orig_doc = MyDoc(tens=torch.rand(10, requires_grad=requires_grad))
193+
serialized_doc = orig_doc.to_json()
194+
assert serialized_doc
195+
assert isinstance(serialized_doc, str)
196+
197+
json_doc = json.loads(serialized_doc)
198+
assert json_doc['tens']
199+
assert len(json_doc['tens']) == 10
200+
201+
202+
@pytest.mark.parametrize('protocol', ['pickle', 'protobuf'])
203+
@pytest.mark.parametrize('requires_grad', [True, False])
204+
def test_bytes_serialization(requires_grad, protocol):
205+
orig_doc = MyDoc(tens=torch.rand(10, requires_grad=requires_grad))
206+
serialized_doc = orig_doc.to_bytes(protocol=protocol)
207+
assert serialized_doc
208+
assert isinstance(serialized_doc, bytes)
209+
210+
conv_doc = MyDoc.from_bytes(serialized_doc, protocol=protocol)
211+
assert isinstance(conv_doc.tens, TorchTensor)
212+
assert conv_doc.tens.shape == (10,)
213+
214+
215+
@pytest.mark.parametrize('protocol', ['pickle', 'protobuf'])
216+
@pytest.mark.parametrize('requires_grad', [True, False])
217+
def test_base64_serialization(requires_grad, protocol):
218+
orig_doc = MyDoc(tens=torch.rand(10, requires_grad=requires_grad))
219+
serialized_doc = orig_doc.to_base64(protocol=protocol)
220+
assert serialized_doc
221+
assert isinstance(serialized_doc, str)
222+
223+
conv_doc = MyDoc.from_base64(serialized_doc, protocol=protocol)
224+
assert isinstance(conv_doc.tens, TorchTensor)
225+
assert conv_doc.tens.shape == (10,)
226+
227+
228+
@pytest.mark.parametrize('requires_grad', [True, False])
229+
def test_protobuf_serialization(requires_grad):
230+
orig_doc = MyDoc(tens=torch.rand(10, requires_grad=requires_grad))
231+
serialized_doc = orig_doc.to_protobuf()
232+
assert serialized_doc
233+
assert isinstance(serialized_doc, DocProto)
234+
235+
conv_doc = MyDoc.from_protobuf(serialized_doc)
236+
assert isinstance(conv_doc.tens, TorchTensor)
237+
assert conv_doc.tens.shape == (10,)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.