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

Method fs_store

tinygrad/tensor.py:442–464  ·  view source on GitHub ↗

Store a tensor to storage.

(self)

Source from the content-addressed store, hash-verified

440 return data[:size]
441
442 def fs_store(self) -> Tensor:
443 """
444 Store a tensor to storage.
445 """
446 # TODO: this should work locally as well
447 data = self.contiguous().flatten().bitcast(dtypes.uint8)
448
449 # pad to a multiple of 1mb
450 if (tsize := data.shape[0]) % Tensor.CHUNK_SIZE != 0: data = data.pad((0, Tensor.CHUNK_SIZE - tsize % Tensor.CHUNK_SIZE))
451 size = data.shape[0]
452
453 base_chunks = math.ceil(size / Tensor.CHUNK_SIZE)
454 tree_depth = math.ceil(math.log(base_chunks, Tensor.CHUNK_SIZE // 16))
455
456 to_device = "CPU" if isinstance(self.device, str) and self.device.startswith("DISK") else self.device
457
458 level_chunks = base_chunks
459 for _ in range(tree_depth + 1):
460 data = data.to("tinyfs:store")[:level_chunks * 16].contiguous().to(to_device)
461 if (tsize := data.shape[0]) % Tensor.CHUNK_SIZE != 0: data = data.pad((0, Tensor.CHUNK_SIZE - tsize % Tensor.CHUNK_SIZE))
462 level_chunks = math.ceil(data.shape[0] / Tensor.CHUNK_SIZE)
463
464 return data[:16].contiguous()
465
466 @staticmethod
467 def from_uop(y:UOp, **kwargs) -> Tensor:

Callers 10

upload_fileFunction · 0.80
upload_raid.pyFile · 0.80
test_store_shapeMethod · 0.80
test_storeMethod · 0.80
test_roundtrip_uint8Method · 0.80

Calls 7

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

Tested by 8

test_store_shapeMethod · 0.64
test_storeMethod · 0.64
test_roundtrip_uint8Method · 0.64