| 191 | |
| 192 | @classmethod |
| 193 | def from_tensor(cls, tensor: torch.Tensor, const: bool = False) -> TensorSpec: |
| 194 | if const: |
| 195 | # for non-contigous tensors, convert to a contiguous one |
| 196 | tensor = tensor.contiguous() |
| 197 | # Weights cannot be views during emission or serialization |
| 198 | if tensor.nbytes != tensor.untyped_storage().nbytes(): |
| 199 | tensor = tensor.clone() |
| 200 | |
| 201 | spec = cls( |
| 202 | dtype=tensor.dtype, |
| 203 | shape=tensor.shape, |
| 204 | layout=tensor.layout, |
| 205 | const=const, |
| 206 | is_sparse=tensor.is_sparse, |
| 207 | ) |
| 208 | spec.stride = tensor.stride() |
| 209 | spec.dim_order = dim_order_from_stride(spec.stride) |
| 210 | spec.requires_grad = tensor.requires_grad |
| 211 | spec.storage = tensor.untyped_storage() if const else None |
| 212 | |
| 213 | return spec |
| 214 | |
| 215 | def init_mem_planning_fields(self) -> None: |
| 216 | self.lifetime = [None, None] |