MCPcopy Index your code
hub / github.com/tinygrad/tinygrad / fs_load

Method fs_load

tinygrad/tensor.py:414–440  ·  view source on GitHub ↗

Load a tensor from storage. self should be a tensor of the hash to load

(self, size:int)

Source from the content-addressed store, hash-verified

412
413 CHUNK_SIZE = 2**20
414 def fs_load(self, size:int) -> Tensor:
415 """
416 Load a tensor from storage.
417
418 self should be a tensor of the hash to load
419 """
420 # TODO: this should work locally as well
421 assert self.dtype == dtypes.uint8, "hash is expected to be uint8"
422 h = self.contiguous().flatten()
423 assert h.shape[0] == 16, "expected hash"
424
425 base_chunks = math.ceil(size / Tensor.CHUNK_SIZE)
426 tree_depth = math.ceil(math.log(base_chunks, Tensor.CHUNK_SIZE // 16))
427 data, level_chunks = h, 0
428 for i in reversed(range(tree_depth + 1)):
429 data = data.to("tinyfs:load")
430
431 # if not last level, its still hashes
432 if i > 0 or tree_depth == 0:
433 level_chunks = max(1, math.ceil(base_chunks / (Tensor.CHUNK_SIZE // 16)**(i-1)))
434 pad_amt = 16 * level_chunks
435 else: pad_amt = Tensor.CHUNK_SIZE * level_chunks
436 if (tsize := data.shape[0]) < pad_amt: data = data.pad((0, pad_amt - tsize))
437 data = data[:pad_amt].contiguous()
438 if i != 0: data = data.to(self.device)
439
440 return data[:size]
441
442 def fs_store(self) -> Tensor:
443 """

Callers 7

fetch_fileFunction · 0.80
fetch_mappingFunction · 0.80
fetch_file.pyFile · 0.80
test_load_shapeMethod · 0.80
test_load_large_shapeMethod · 0.80
test_roundtrip_uint8Method · 0.80

Calls 6

contiguousMethod · 0.95
flattenMethod · 0.80
ceilMethod · 0.80
logMethod · 0.80
toMethod · 0.45
padMethod · 0.45

Tested by 4

test_load_shapeMethod · 0.64
test_load_large_shapeMethod · 0.64
test_roundtrip_uint8Method · 0.64