You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugin.py 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import struct
  10. import numpy as np
  11. def load_tensor_binary(fobj):
  12. """Load a tensor dumped by the :class:`BinaryOprIODump` plugin; the actual
  13. tensor value dump is implemented by ``mgb::debug::dump_tensor``.
  14. Multiple values can be compared by ``tools/compare_binary_iodump.py``.
  15. :param fobj: file object, or a string that contains the file name.
  16. :return: tuple ``(tensor_value, tensor_name)``.
  17. """
  18. if isinstance(fobj, str):
  19. with open(fobj, "rb") as fin:
  20. return load_tensor_binary(fin)
  21. DTYPE_LIST = {
  22. 0: np.float32,
  23. 1: np.uint8,
  24. 2: np.int8,
  25. 3: np.int16,
  26. 4: np.int32,
  27. # 5: _mgb.intb1,
  28. # 6: _mgb.intb2,
  29. # 7: _mgb.intb4,
  30. 8: None,
  31. 9: np.float16,
  32. # quantized dtype start from 100000
  33. # see MEGDNN_PARAMETERIZED_DTYPE_ENUM_BASE in
  34. # dnn/include/megdnn/dtype.h
  35. 100000: np.uint8,
  36. 100001: np.int32,
  37. 100002: np.int8,
  38. }
  39. header_fmt = struct.Struct("III")
  40. name_len, dtype, max_ndim = header_fmt.unpack(fobj.read(header_fmt.size))
  41. assert (
  42. DTYPE_LIST[dtype] is not None
  43. ), "Cannot load this tensor: dtype Byte is unsupported."
  44. shape = list(struct.unpack("I" * max_ndim, fobj.read(max_ndim * 4)))
  45. while shape[-1] == 0:
  46. shape.pop(-1)
  47. name = fobj.read(name_len).decode("ascii")
  48. return np.fromfile(fobj, dtype=DTYPE_LIST[dtype]).reshape(shape), name

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台