MCPcopy Create free account
hub / github.com/MegEngine/MegCC / load_tensor_binary

Function load_tensor_binary

compiler/script/debug/parse_mgb_bin_dump.py:11–51  ·  view source on GitHub ↗

Load a tensor dumped by the :class:`BinaryOprIODump` plugin; the actual tensor value dump is implemented by ``mgb::debug::dump_tensor``. :param fobj: file object, or a string that contains the file name. :return: tuple ``(tensor_value, tensor_name)``.

(fobj)

Source from the content-addressed store, hash-verified

9
10
11def load_tensor_binary(fobj):
12 """
13 Load a tensor dumped by the :class:`BinaryOprIODump` plugin; the actual
14 tensor value dump is implemented by ``mgb::debug::dump_tensor``.
15
16 :param fobj: file object, or a string that contains the file name.
17 :return: tuple ``(tensor_value, tensor_name)``.
18 """
19 if isinstance(fobj, str):
20 with open(fobj, "rb") as fin:
21 return load_tensor_binary(fin)
22
23 DTYPE_LIST = {
24 0: np.float32,
25 1: np.uint8,
26 2: np.int8,
27 3: np.int16,
28 4: np.int32,
29 # 5: _mgb.intb1,
30 # 6: _mgb.intb2,
31 # 7: _mgb.intb4,
32 8: None,
33 9: np.float16,
34 # quantized dtype start from 100000
35 # see MEGDNN_PARAMETERIZED_DTYPE_ENUM_BASE in
36 # dnn/include/megdnn/dtype.h
37 100000: np.uint8,
38 100001: np.int32,
39 100002: np.int8,
40 }
41
42 header_fmt = struct.Struct("III")
43 name_len, dtype, max_ndim = header_fmt.unpack(fobj.read(header_fmt.size))
44 assert (DTYPE_LIST[dtype]
45 is not None), "Cannot load this tensor: dtype Byte is unsupported."
46
47 shape = list(struct.unpack("I" * max_ndim, fobj.read(max_ndim * 4)))
48 while shape[-1] == 0:
49 shape.pop(-1)
50 name = fobj.read(name_len).decode("ascii")
51 return np.fromfile(fobj, dtype=DTYPE_LIST[dtype]).reshape(shape), name
52
53
54def find_file_name(dir_path, varid):

Callers 2

find_file_nameFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected