MCPcopy
hub / github.com/tinygrad/tinygrad / _frompy

Function _frompy

tinygrad/tensor.py:53–62  ·  view source on GitHub ↗
(x:list|tuple|bytes, dtype:DType, device:str|tuple[str,...])

Source from the content-addressed store, hash-verified

51 return (len(subs),) + (subs[0] if subs else ())
52
53def _frompy(x:list|tuple|bytes, dtype:DType, device:str|tuple[str,...]) -> UOp:
54 if isinstance(x, bytes): ret, data = UOp.new_buffer("PYTHON", len(x)//dtype.itemsize, dtype), x
55 else:
56 ret = UOp.empty(shape:=get_shape(x), dtype, "PYTHON")
57 assert dtype.fmt is not None, f"{dtype=} has None fmt"
58 truncate_function = truncate[dtype]
59 data = struct.pack(f"{prod(shape)}{dtype.fmt}", *[truncate_function(dtype.const(xi)) for xi in fully_flatten(x)])
60 # fake realize. if target device is PYTHON it needs bytearray to be writable
61 ret.buffer.allocate(memoryview(data if device != "PYTHON" else bytearray(data)))
62 return ret
63
64def _get_winograd_matcols(mat, dims:int, shp:tuple[sint, ...], device:str|tuple[str, ...]|None, dtype:DType) -> list[list[Tensor]]:
65 return [[Tensor.cat(*[Tensor.full(shp[:dim] + (1,) + shp[dim+1:], float(m[k]), device=device, dtype=dtype, buffer=False) for m in mat], dim=dim)

Callers 1

__init__Method · 0.85

Calls 7

prodFunction · 0.90
fully_flattenFunction · 0.90
get_shapeFunction · 0.85
new_bufferMethod · 0.80
allocateMethod · 0.80
emptyMethod · 0.45
constMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…