MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / __init__

Method __init__

imperative/python/megengine/xla/rules/hlotensor.py:12–45  ·  view source on GitHub ↗
(self, tensor, shape=None, dtype=None)

Source from the content-addressed store, hash-verified

10
11class HLOTensor:
12 def __init__(self, tensor, shape=None, dtype=None) -> None:
13 if isinstance(tensor, Sequence):
14 assert len(tensor) > 0, "cannot create HLOTensor from empty sequence"
15 if isinstance(tensor[0], int):
16 tensor = np.array(tensor)
17 else:
18 assert len(tensor) == 1, f"cannot create HLOTensor from {tensor}"
19 tensor = tensor[0]
20 if isinstance(tensor, ir.OpResultList):
21 assert len(tensor) == 1, f"cannot create HLOTensor from {tensor}"
22 tensor = tensor[0]
23
24 if isinstance(
25 tensor, (int, float, np.int_, np.float16, np.float32, np.float64)
26 ):
27 tensor = tensor if dtype is None else np.array(tensor).astype(dtype)
28 tensor = ir_utils.ir_constant(tensor)
29 elif isinstance(tensor, np.ndarray):
30 tensor = tensor if dtype is None else np.array(tensor).astype(dtype)
31 tensor = ir_utils.ir_constant(tensor)
32 else:
33 assert isinstance(
34 tensor, (ir.RankedTensorType, ir.BlockArgument, ir.OpResult)
35 ), type(tensor)
36
37 infered_shape = get_irnode_shape(tensor)
38 infered_dtype = get_irnode_dtype(tensor)
39
40 _check_shape(infered_shape, shape)
41 _check_dtype(infered_dtype, dtype)
42
43 self._tensor = tensor
44 self._shape = infered_shape
45 self._dtype = infered_dtype
46
47 @property
48 def shape(self):

Callers

nothing calls this directly

Calls 6

get_irnode_shapeFunction · 0.85
get_irnode_dtypeFunction · 0.85
_check_shapeFunction · 0.85
_check_dtypeFunction · 0.85
arrayMethod · 0.80
astypeMethod · 0.45

Tested by

no test coverage detected