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.

global_setting.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 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. from ctypes import *
  10. import numpy as np
  11. from .base import _Ctensor, _lib, _LiteCObjBase
  12. from .network import *
  13. from .struct import LiteDataType, LiteDeviceType, LiteIOType, Structure
  14. from .tensor import *
  15. LiteDecryptionFunc = CFUNCTYPE(
  16. c_size_t, c_void_p, c_size_t, POINTER(c_uint8), c_size_t, c_void_p
  17. )
  18. class _GlobalAPI(_LiteCObjBase):
  19. """
  20. get the api from the lib
  21. """
  22. _api_ = [
  23. ("LITE_get_device_count", [c_int, POINTER(c_size_t)]),
  24. ("LITE_try_coalesce_all_free_memory", []),
  25. (
  26. "LITE_register_decryption_and_key",
  27. [c_char_p, LiteDecryptionFunc, POINTER(c_uint8), c_size_t],
  28. ),
  29. (
  30. "LITE_update_decryption_or_key",
  31. [c_char_p, c_void_p, POINTER(c_uint8), c_size_t],
  32. ),
  33. ("LITE_set_loader_lib_path", [c_char_p]),
  34. ("LITE_set_persistent_cache", [c_char_p, c_int]),
  35. # ('LITE_set_tensor_rt_cache', [c_char_p]),
  36. ("LITE_dump_persistent_cache", [c_char_p]),
  37. ("LITE_dump_tensor_rt_cache", [c_char_p]),
  38. ]
  39. def decryption_func(func):
  40. """the decryption function decorator
  41. :type func: a function accept three array, in_arr, key_arr and out_arr, if out_arr is None, just query the out array lenght in byte
  42. """
  43. @CFUNCTYPE(c_size_t, c_void_p, c_size_t, POINTER(c_uint8), c_size_t, c_void_p)
  44. def wrapper(c_in_data, in_length, c_key_data, key_length, c_out_data):
  45. in_arr = np.frombuffer(c_in_data, dtype=np.uint8, count=in_length)
  46. key_arr = np.frombuffer(c_key_data, dtype=np.uint8, count=key_length)
  47. if c_out_data:
  48. out_length = func(in_arr, None)
  49. out_arr = np.frombuffer(c_out_data, dtype=np.uint8, count=out_length)
  50. return func(in_arr, key_arr, out_arr)
  51. # just query the output length
  52. else:
  53. return func(in_arr, key_arr, None)
  54. return wrapper
  55. class LiteGlobal(object):
  56. """
  57. some global config in lite
  58. """
  59. _api = _GlobalAPI()._lib
  60. @staticmethod
  61. def register_decryption_and_key(decryption_name, decryption_func, key):
  62. c_name = c_char_p(decryption_name.encode("utf-8"))
  63. key_length = len(key)
  64. c_key = (c_uint8 * key_length)(*key)
  65. LiteGlobal._api.LITE_register_decryption_and_key(
  66. c_name, decryption_func, c_key, key_length
  67. )
  68. @staticmethod
  69. def update_decryption_key(decryption_name, key):
  70. c_name = c_char_p(decryption_name.encode("utf-8"))
  71. key_length = len(key)
  72. c_key = (c_uint8 * key_length)(*key)
  73. LiteGlobal._api.LITE_update_decryption_or_key(c_name, None, c_key, key_length)
  74. @staticmethod
  75. def set_loader_lib_path(path):
  76. c_path = c_char_p(path.encode("utf-8"))
  77. LiteGlobal._api.LITE_set_loader_lib_path(c_path)
  78. @staticmethod
  79. def set_persistent_cache(path, always_sync=False):
  80. c_path = c_char_p(path.encode("utf-8"))
  81. LiteGlobal._api.LITE_set_persistent_cache(c_path, always_sync)
  82. @staticmethod
  83. def set_tensorrt_cache(path):
  84. c_path = c_char_p(path.encode("utf-8"))
  85. LiteGlobal._api.LITE_set_tensorrt_cache(c_path)
  86. @staticmethod
  87. def dump_persistent_cache(path):
  88. c_path = c_char_p(path.encode("utf-8"))
  89. LiteGlobal._api.LITE_dump_persistent_cache(c_path)
  90. @staticmethod
  91. def dump_tensorrt_cache():
  92. LiteGlobal._api.LITE_dump_tensorrt_cache()
  93. @staticmethod
  94. def get_device_count(device_type):
  95. count = c_size_t()
  96. LiteGlobal._api.LITE_get_device_count(device_type, byref(count))
  97. return count.value
  98. @staticmethod
  99. def try_coalesce_all_free_memory():
  100. LiteGlobal._api.LITE_try_coalesce_all_free_memory()

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