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.

test_global.py 3.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. # This file is part of MegEngine, a deep learning framework developed by
  3. # Megvii.
  4. #
  5. # Copyright (c) Copyright (c) 2020-2021 Megvii Inc. All rights reserved.
  6. import os
  7. import unittest
  8. import numpy as np
  9. from megenginelite import *
  10. set_log_level(2)
  11. class TestShuffleNet(unittest.TestCase):
  12. source_dir = os.getenv("LITE_TEST_RESOUCE")
  13. input_data_path = os.path.join(source_dir, "input_data.npy")
  14. correct_data_path = os.path.join(source_dir, "output_data.npy")
  15. correct_data = np.load(correct_data_path).flatten()
  16. input_data = np.load(input_data_path)
  17. def check_correct(self, out_data, error=1e-4):
  18. out_data = out_data.flatten()
  19. assert np.isfinite(out_data.sum())
  20. assert self.correct_data.size == out_data.size
  21. for i in range(out_data.size):
  22. assert abs(out_data[i] - self.correct_data[i]) < error
  23. def do_forward(self, network, times=3):
  24. input_name = network.get_input_name(0)
  25. input_tensor = network.get_io_tensor(input_name)
  26. output_name = network.get_output_name(0)
  27. output_tensor = network.get_io_tensor(output_name)
  28. input_tensor.set_data_by_copy(self.input_data)
  29. for i in range(times):
  30. network.forward()
  31. network.wait()
  32. output_data = output_tensor.to_numpy()
  33. self.check_correct(output_data)
  34. class TestGlobal(TestShuffleNet):
  35. def test_device_count(self):
  36. LiteGlobal.try_coalesce_all_free_memory()
  37. count = LiteGlobal.get_device_count(LiteDeviceType.LITE_CPU)
  38. assert count > 0
  39. def test_register_decryption_method(self):
  40. @decryption_func
  41. def function(in_arr, key_arr, out_arr):
  42. if not out_arr:
  43. return in_arr.size
  44. else:
  45. for i in range(in_arr.size):
  46. out_arr[i] = in_arr[i] ^ key_arr[0] ^ key_arr[0]
  47. return out_arr.size
  48. LiteGlobal.register_decryption_and_key("just_for_test", function, [15])
  49. config = LiteConfig()
  50. config.bare_model_cryption_name = "just_for_test".encode("utf-8")
  51. network = LiteNetwork()
  52. model_path = os.path.join(self.source_dir, "shufflenet.mge")
  53. network.load(model_path)
  54. self.do_forward(network)
  55. def test_update_decryption_key(self):
  56. wrong_key = [0] * 32
  57. LiteGlobal.update_decryption_key("AES_default", wrong_key)
  58. with self.assertRaises(RuntimeError):
  59. config = LiteConfig()
  60. config.bare_model_cryption_name = "AES_default".encode("utf-8")
  61. network = LiteNetwork(config)
  62. model_path = os.path.join(self.source_dir, "shufflenet_crypt_aes.mge")
  63. network.load(model_path)
  64. right_key = [i for i in range(32)]
  65. LiteGlobal.update_decryption_key("AES_default", right_key)
  66. config = LiteConfig()
  67. config.bare_model_cryption_name = "AES_default".encode("utf-8")
  68. network = LiteNetwork(config)
  69. model_path = os.path.join(self.source_dir, "shufflenet_crypt_aes.mge")
  70. network.load(model_path)
  71. self.do_forward(network)

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