Load a tensor from storage. self should be a tensor of the hash to load
(self, size:int)
| 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 | """ |