This class is a wrapper around a list of TensorRepr instances that automatically applies a "broadcasting" mechanism. The broadcasting mechanism allows for a single underlying TensorRepr to be used to represent multiple tensors.
| 807 | |
| 808 | |
| 809 | class TensorReprList: |
| 810 | """ |
| 811 | This class is a wrapper around a list of TensorRepr instances that automatically |
| 812 | applies a "broadcasting" mechanism. The broadcasting mechanism allows for a single |
| 813 | underlying TensorRepr to be used to represent multiple tensors. |
| 814 | """ |
| 815 | |
| 816 | def __init__(self, tensor_reprs: Union[TensorRepr, List[TensorRepr]]): |
| 817 | self.vals: List[TensorRepr] = ( |
| 818 | tensor_reprs if isinstance(tensor_reprs, list) else [tensor_reprs] |
| 819 | ) |
| 820 | |
| 821 | def __len__(self): |
| 822 | return len(self.vals) |
| 823 | |
| 824 | def __getitem__(self, idx: int) -> TensorRepr: |
| 825 | if idx > 0 and len(self) == 1: |
| 826 | return self.vals[0] |
| 827 | else: |
| 828 | return self.vals[idx] |
| 829 | |
| 830 | def __setitem__(self, idx: int, val: TensorRepr) -> None: |
| 831 | if idx > 0 and len(self) == 1: |
| 832 | self.vals[0] = val |
| 833 | else: |
| 834 | self.vals[idx] = val |
| 835 | |
| 836 | def __str__(self) -> str: |
| 837 | return f"[{', '.join(str(ts) for ts in self.vals)}]" |
| 838 | |
| 839 | def __eq__(self, other: object) -> bool: |
| 840 | if not isinstance(other, TensorReprList): |
| 841 | return NotImplemented |
| 842 | |
| 843 | if len(self) == len(other): |
| 844 | for self_val, other_val in zip(self.vals, other.vals): |
| 845 | if self_val != other_val: |
| 846 | return False |
| 847 | |
| 848 | return True |
| 849 | |
| 850 | return False |
| 851 | |
| 852 | def __ne__(self, other: object) -> bool: |
| 853 | return not self.__eq__(other) |
| 854 | |
| 855 | def append(self, val: TensorRepr) -> None: |
| 856 | self.vals.append(val) |
| 857 | |
| 858 | def storage_type(self, idx: int = 0) -> VkStorageType: |
| 859 | return self.vals[idx].storage_type |
| 860 | |
| 861 | def memory_layout(self, idx: int = 0) -> VkMemoryLayout: |
| 862 | return self.vals[idx].memory_layout |
| 863 | |
| 864 | |
| 865 | class TensorRepSet: |
no outgoing calls