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 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import unittest
  4. from ctypes import *
  5. import numpy as np
  6. from megenginelite import *
  7. set_log_level(2)
  8. class TestShuffleNet(unittest.TestCase):
  9. source_dir = os.getenv("LITE_TEST_RESOURCE")
  10. input_data_path = os.path.join(source_dir, "input_data.npy")
  11. correct_data_path = os.path.join(source_dir, "output_data.npy")
  12. correct_data = np.load(correct_data_path).flatten()
  13. input_data = np.load(input_data_path)
  14. def check_correct(self, out_data, error=1e-4):
  15. out_data = out_data.flatten()
  16. assert np.isfinite(out_data.sum())
  17. assert self.correct_data.size == out_data.size
  18. for i in range(out_data.size):
  19. assert abs(out_data[i] - self.correct_data[i]) < error
  20. def do_forward(self, network, times=3):
  21. input_name = network.get_input_name(0)
  22. input_tensor = network.get_io_tensor(input_name)
  23. output_name = network.get_output_name(0)
  24. output_tensor = network.get_io_tensor(output_name)
  25. input_tensor.set_data_by_copy(self.input_data)
  26. for i in range(times):
  27. network.forward()
  28. network.wait()
  29. output_data = output_tensor.to_numpy()
  30. self.check_correct(output_data)
  31. class TestGlobal(TestShuffleNet):
  32. def test_device_count(self):
  33. LiteGlobal.try_coalesce_all_free_memory()
  34. count = LiteGlobal.get_device_count(LiteDeviceType.LITE_CPU)
  35. assert count > 0
  36. def test_register_decryption_method(self):
  37. @decryption_func
  38. def function(in_arr, key_arr, out_arr):
  39. if not out_arr:
  40. return in_arr.size
  41. else:
  42. for i in range(in_arr.size):
  43. out_arr[i] = in_arr[i] ^ key_arr[0] ^ key_arr[0]
  44. return out_arr.size
  45. LiteGlobal.register_decryption_and_key("just_for_test", function, [15])
  46. config = LiteConfig()
  47. config.bare_model_cryption_name = "just_for_test".encode("utf-8")
  48. network = LiteNetwork()
  49. model_path = os.path.join(self.source_dir, "shufflenet.mge")
  50. network.load(model_path)
  51. self.do_forward(network)
  52. def test_set_get_memory_pair(self):
  53. if LiteGlobal.get_device_count(LiteDeviceType.LITE_AX) > 0:
  54. arr1 = np.ones([2, 3])
  55. arr2 = np.ones([2, 3])
  56. vir_ptr = arr1.ctypes.data_as(c_void_p)
  57. phy_ptr = arr2.ctypes.data_as(c_void_p)
  58. LiteGlobal.register_memory_pair(
  59. vir_ptr, phy_ptr, 10, LiteDeviceType.LITE_AX
  60. )
  61. phy_ptr2 = LiteGlobal.lookup_physic_ptr(vir_ptr, LiteDeviceType.LITE_AX)
  62. assert phy_ptr.value == phy_ptr2.value
  63. LiteGlobal.clear_memory_pair(vir_ptr, phy_ptr, LiteDeviceType.LITE_AX)