MCPcopy Create free account
hub / github.com/pytorch/executorch / from_tensor

Method from_tensor

exir/tensor.py:193–213  ·  view source on GitHub ↗
(cls, tensor: torch.Tensor, const: bool = False)

Source from the content-addressed store, hash-verified

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]

Callers 15

get_attrMethod · 0.45
make_valMethod · 0.45
make_tensor_metaMethod · 0.45
add_named_dataMethod · 0.45
make_specFunction · 0.45
_emit_prim_gettersMethod · 0.45

Calls 3

dim_order_from_strideFunction · 0.85
nbytesMethod · 0.80
cloneMethod · 0.80