File tree Expand file tree Collapse file tree 3 files changed +14
-16
lines changed Expand file tree Collapse file tree 3 files changed +14
-16
lines changed Original file line number Diff line number Diff line change @@ -1041,13 +1041,7 @@ def tobytes(self) -> bytes:
10411041 return self ._evaluate ().tobytes ()
10421042
10431043 def tofile (self , file ) -> None :
1044- tensor = self ._evaluate ()
1045- if hasattr (tensor , "tofile" ):
1046- # Some existing implementation (e.g. PyTorch <2.10) of TensorProtocol
1047- # may not have tofile() as it was introduced in v0.1.11
1048- tensor .tofile (file )
1049- else :
1050- super ().tofile (file )
1044+ self ._evaluate ().tofile (file )
10511045
10521046
10531047class PackedTensor (TensorBase , _protocols .TensorProtocol , Generic [TArrayCompatible ]): # pylint: disable=too-many-ancestors
Original file line number Diff line number Diff line change @@ -147,6 +147,18 @@ def tobytes(self) -> bytes:
147147 """Return the tensor as a byte string conformed to the ONNX specification, in little endian."""
148148 ...
149149
150+ def tofile (self , file ) -> None :
151+ """Write the tensor as a byte string conformed to the ONNX specification to the given file-like object.
152+
153+ The file-like object must support ``file.write(bytes)``.
154+ If the file-like object also supports ``file.fileno()``, it will be used
155+ to write the data directly to the underlying file descriptor. This is
156+ more efficient for large tensors.
157+
158+ .. versionadded:: 0.1.11
159+ """
160+ ...
161+
150162
151163@typing .runtime_checkable
152164class ValueProtocol (Protocol ):
Original file line number Diff line number Diff line change @@ -210,15 +210,7 @@ def _write_external_data(
210210 if current_offset > file_size :
211211 data_file .write (b"\0 " * (current_offset - file_size ))
212212
213- if hasattr (tensor , "tofile" ):
214- # Some existing implementation (e.g. PyTorch <2.10) of TensorProtocol
215- # may not have tofile() as it was introduced in v0.1.11
216- tensor .tofile (data_file )
217- else :
218- raw_data = tensor .tobytes ()
219- if isinstance (tensor , _core .ExternalTensor ):
220- tensor .release ()
221- data_file .write (raw_data )
213+ tensor .tofile (data_file )
222214
223215
224216def _create_external_tensor (
You can’t perform that action at this time.
0 commit comments