MCPcopy
hub / github.com/tinygrad/tinygrad / data

Method data

tinygrad/tensor.py:295–308  ·  view source on GitHub ↗

Returns the data of this tensor as a memoryview. ```python exec="true" source="above" session="tensor" result="python" t = Tensor([1, 2, 3, 4]) print(np.frombuffer(t.data(), dtype=np.int32)) ```

(self)

Source from the content-addressed store, hash-verified

293 def _data(self) -> memoryview: return self._buffer().as_memoryview()
294
295 def data(self) -> memoryview:
296 """
297 Returns the data of this tensor as a memoryview.
298
299 ```python exec="true" source="above" session="tensor" result="python"
300 t = Tensor([1, 2, 3, 4])
301 print(np.frombuffer(t.data(), dtype=np.int32))
302 ```
303 """
304 if 0 in self.shape: return memoryview(bytearray(0)).cast(self.dtype.base.fmt)
305 assert all_int(self.shape), f"no data if shape is symbolic, {self.shape=}"
306 assert self.dtype.base.fmt is not None, f"no fmt dtype for {self.dtype.base}"
307 assert self.dtype.base.fmt != "e" or sys.version_info >= (3, 12)
308 return self._buffer().as_memoryview().cast(self.dtype.base.fmt, self.shape)
309
310 def item(self) -> PyConst:
311 """

Callers 15

itemMethod · 0.95
tolistMethod · 0.95
test_dataMethod · 0.95
test_data_emptyMethod · 0.95
test_data_uint8Method · 0.95
test_data_nestedMethod · 0.95
test_data_constMethod · 0.95
test_data_float32Method · 0.95
test_data_float16Method · 0.95
readintoMethod · 0.45
safe_load_metadataFunction · 0.45
zip_extractFunction · 0.45

Calls 4

_bufferMethod · 0.95
all_intFunction · 0.90
as_memoryviewMethod · 0.80
castMethod · 0.45

Tested by 7

test_dataMethod · 0.76
test_data_emptyMethod · 0.76
test_data_uint8Method · 0.76
test_data_nestedMethod · 0.76
test_data_constMethod · 0.76
test_data_float32Method · 0.76
test_data_float16Method · 0.76