MCPcopy
hub / github.com/tinygrad/tinygrad / as_memoryview

Method as_memoryview

tinygrad/device.py:195–200  ·  view source on GitHub ↗
(self, allow_zero_copy=False, force_zero_copy=False)

Source from the content-addressed store, hash-verified

193 return f"<buf real:{self.is_allocated()} device:{self.device} size:{self.size} dtype:{self.dtype}" + \
194 (f" offset:{self.offset}" if self._base is not None else "") + (f" {self.options=}" if self.options is not None else "") + ">"
195 def as_memoryview(self, allow_zero_copy=False, force_zero_copy=False) -> memoryview:
196 # zero copy with as_memoryview (disabled by default due to use after free)
197 if (force_zero_copy or allow_zero_copy) and hasattr(self.allocator, '_as_buffer') and self.options is None:
198 return self.allocator._as_buffer(self._buf)
199 assert not force_zero_copy, "force zero copy was passed, but copy is required"
200 return self.copyout(memoryview(bytearray(self.nbytes)))
201 def numpy(self) -> 'np.ndarray': # type: ignore [name-defined] # noqa: F821
202 import numpy as np
203 assert _to_np_dtype(self.dtype.base) is not None, f"no np dtype for {self.dtype.base}"

Callers 15

numpyMethod · 0.95
timeline_valueMethod · 0.95
_dataMethod · 0.80
dataMethod · 0.80
exec_copyFunction · 0.80
to_npFunction · 0.80
intel_xmx.pyFile · 0.80
synchronizeMethod · 0.80
ensure_accessibleFunction · 0.80
_collect_interruptsMethod · 0.80
__call__Method · 0.80

Calls 2

copyoutMethod · 0.95
_as_bufferMethod · 0.45

Tested by 15

test_subbufferMethod · 0.64
test_subbuffer_castMethod · 0.64
test_subbuffer_doubleMethod · 0.64
test_subbuffer_lenMethod · 0.64
test_subbuffer_allocMethod · 0.64
test_subbuffer_uafMethod · 0.64
make_bufferFunction · 0.64