| 522 | |
| 523 | @dataclass |
| 524 | class LazyTensor: |
| 525 | _load: Callable[[], Tensor] |
| 526 | shape: list[int] |
| 527 | data_type: DataType |
| 528 | description: str |
| 529 | |
| 530 | def load(self) -> Tensor: |
| 531 | ret = self._load() |
| 532 | # Should be okay if it maps to the same numpy type? |
| 533 | assert ret.data_type == self.data_type or (self.data_type.dtype == ret.data_type.dtype), \ |
| 534 | (self.data_type, ret.data_type, self.description) |
| 535 | return ret |
| 536 | |
| 537 | def astype(self, data_type: DataType) -> LazyTensor: |
| 538 | self.validate_conversion_to(data_type) |
| 539 | |
| 540 | def load() -> Tensor: |
| 541 | return self.load().astype(data_type) |
| 542 | return LazyTensor(load, self.shape, data_type, f'convert({data_type}) {self.description}') |
| 543 | |
| 544 | def transposed(self) -> LazyTensor: |
| 545 | def load() -> Tensor: |
| 546 | loaded = self.load() |
| 547 | assert isinstance(loaded, UnquantizedTensor), f'Cannot transpose {loaded}' |
| 548 | loaded.ndarray = loaded.ndarray.T |
| 549 | return loaded |
| 550 | return LazyTensor(load, self.shape[::-1], self.data_type, f'transpose {self.description}') |
| 551 | |
| 552 | def validate_conversion_to(self, data_type: DataType) -> None: |
| 553 | if data_type != self.data_type and data_type.name not in self.data_type.valid_conversions: |
| 554 | raise ValueError(f'Cannot validate conversion from {self.data_type} to {data_type}.') |
| 555 | |
| 556 | |
| 557 | LazyModel: TypeAlias = 'dict[str, LazyTensor]' |
no outgoing calls
no test coverage detected