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_common.h 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #pragma once
  2. #include "lite_build_config.h"
  3. #if LITE_BUILD_WITH_MGE
  4. #include "../src/mge/common.h"
  5. #include "../src/mge/network_impl.h"
  6. #include "../src/misc.h"
  7. #include "lite/network.h"
  8. #include "lite/tensor.h"
  9. #include "megbrain/comp_node.h"
  10. #include "megbrain/graph/bases.h"
  11. #include "megbrain/plugin/opr_io_dump.h"
  12. #include "megbrain/plugin/profiler.h"
  13. #include "megbrain/serialization/extern_c_opr.h"
  14. #include "megbrain/serialization/file.h"
  15. #include "megbrain/serialization/load_dump_config.h"
  16. #include "megbrain/serialization/serializer.h"
  17. #include "megbrain/tensor.h"
  18. #include "megbrain/utils/thin/hash_table.h"
  19. #include "npy.h"
  20. #include <gtest/gtest.h>
  21. #include <string.h>
  22. #include <chrono>
  23. #include <memory>
  24. #include <random>
  25. namespace lite {
  26. template <typename T>
  27. static ::testing::AssertionResult compare_memory(
  28. const void* memory0, const void* memory1, size_t length, float maxerr = 1e-3) {
  29. const T* data_ptr0 = static_cast<const T*>(memory0);
  30. const T* data_ptr1 = static_cast<const T*>(memory1);
  31. for (size_t i = 0; i < length; i++) {
  32. auto diff = std::abs(data_ptr0[i] - data_ptr1[i]);
  33. if (diff > maxerr) {
  34. return ::testing::AssertionFailure() << "Unequal value:\n"
  35. << "value 0 = " << data_ptr0[i] << "\n"
  36. << "value 1 = " << data_ptr1[i] << "\n"
  37. << "At index: " << i << "\n";
  38. }
  39. }
  40. return ::testing::AssertionSuccess();
  41. }
  42. template <typename T>
  43. void compare_lite_tensor(
  44. std::shared_ptr<Tensor> tensor0, std::shared_ptr<Tensor> tensor1,
  45. float maxerr = 1e-3) {
  46. size_t elemsize = tensor0->get_layout().get_elem_size();
  47. T* data_ptr0 = static_cast<T*>(tensor0->get_memory_ptr());
  48. T* data_ptr1 = static_cast<T*>(tensor1->get_memory_ptr());
  49. size_t length = tensor0->get_tensor_total_size_in_byte() / elemsize;
  50. EXPECT_TRUE(compare_memory<T>(data_ptr0, data_ptr1, length, maxerr));
  51. }
  52. __attribute__((unused)) static std::shared_ptr<Tensor> get_input_data(
  53. std::string path) {
  54. std::string type_str;
  55. std::vector<npy::ndarray_len_t> stl_shape;
  56. std::vector<int8_t> raw;
  57. npy::LoadArrayFromNumpy(path, type_str, stl_shape, raw);
  58. auto lite_tensor = std::make_shared<Tensor>(LiteDeviceType::LITE_CPU);
  59. Layout layout;
  60. layout.ndim = stl_shape.size();
  61. const std::map<std::string, LiteDataType> type_map = {
  62. {"f4", LiteDataType::LITE_FLOAT}, {"f2", LiteDataType::LITE_HALF},
  63. {"i8", LiteDataType::LITE_INT64}, {"i4", LiteDataType::LITE_INT},
  64. {"u4", LiteDataType::LITE_UINT}, {"i2", LiteDataType::LITE_INT16},
  65. {"u2", LiteDataType::LITE_UINT16}, {"i1", LiteDataType::LITE_INT8},
  66. {"u1", LiteDataType::LITE_UINT8}};
  67. layout.shapes[0] = 1;
  68. for (size_t i = 0; i < stl_shape.size(); i++) {
  69. layout.shapes[i] = static_cast<size_t>(stl_shape[i]);
  70. }
  71. for (auto& item : type_map) {
  72. if (type_str.find(item.first) != std::string::npos) {
  73. layout.data_type = item.second;
  74. break;
  75. }
  76. }
  77. lite_tensor->set_layout(layout);
  78. size_t length = lite_tensor->get_tensor_total_size_in_byte();
  79. void* dest = lite_tensor->get_memory_ptr();
  80. memcpy(dest, raw.data(), length);
  81. return lite_tensor;
  82. }
  83. __attribute__((unused)) static std::shared_ptr<Tensor> mgelite_lar(
  84. std::string model_path, const Config& config, std::string,
  85. std::shared_ptr<Tensor> input) {
  86. std::unique_ptr<Network> network = std::make_unique<Network>(config);
  87. network->load_model(model_path);
  88. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  89. auto src_ptr = input->get_memory_ptr();
  90. auto src_layout = input->get_layout();
  91. input_tensor->reset(src_ptr, src_layout);
  92. network->forward();
  93. network->wait();
  94. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  95. Layout out_layout = output_tensor->get_layout();
  96. auto ret = std::make_shared<Tensor>(LiteDeviceType::LITE_CPU, out_layout);
  97. void* out_data = output_tensor->get_memory_ptr();
  98. void* dst_data = ret->get_memory_ptr();
  99. memcpy(dst_data, out_data, ret->get_tensor_total_size_in_byte());
  100. return ret;
  101. }
  102. __attribute__((unused)) static std::shared_ptr<Tensor> mgb_lar(
  103. std::string model_path, const Config& config, std::string input_name,
  104. std::shared_ptr<Tensor> input) {
  105. LITE_ASSERT(config.bare_model_cryption_name.size() == 0);
  106. using namespace mgb;
  107. serialization::GraphLoader::LoadConfig mgb_config;
  108. mgb_config.comp_node_mapper = [config](CompNode::Locator& loc) {
  109. loc = to_compnode_locator(config.device_type);
  110. };
  111. mgb_config.comp_graph = ComputingGraph::make();
  112. auto&& graph_opt = mgb_config.comp_graph->options();
  113. if (config.options.weight_preprocess) {
  114. graph_opt.graph_opt.enable_weight_preprocess();
  115. }
  116. graph_opt.comp_node_seq_record_level = config.options.comp_node_seq_record_level;
  117. auto inp_file = mgb::serialization::InputFile::make_fs(model_path.c_str());
  118. auto format = serialization::GraphLoader::identify_graph_dump_format(*inp_file);
  119. mgb_assert(
  120. format.valid(),
  121. "invalid model: unknown model format, please make sure input "
  122. "file is generated by GraphDumper");
  123. auto loader = serialization::GraphLoader::make(std::move(inp_file), format.val());
  124. auto load_ret = loader->load(mgb_config, false);
  125. ComputingGraph::OutputSpec out_spec;
  126. std::vector<HostTensorND> output_tensors(load_ret.output_var_list.size());
  127. for (size_t i = 0; i < load_ret.output_var_list.size(); i++) {
  128. auto cb = [&output_tensors, i](const DeviceTensorND& dv) mutable {
  129. output_tensors[i].copy_from(dv);
  130. };
  131. out_spec.emplace_back(load_ret.output_var_list[i], std::move(cb));
  132. }
  133. auto func = load_ret.graph_compile(out_spec);
  134. auto& in = load_ret.tensor_map.find(input_name)->second;
  135. in->copy_from(*TensorHelper::implement(input)
  136. ->cast_final_safe<TensorImplDft>()
  137. .host_tensor());
  138. func->execute();
  139. func->wait();
  140. std::shared_ptr<Tensor> ret = std::make_shared<Tensor>(
  141. LiteDeviceType::LITE_CPU, to_lite_layout(output_tensors[0].layout()));
  142. auto mge_tensor = TensorHelper::implement(ret)
  143. ->cast_final_safe<TensorImplDft>()
  144. .host_tensor();
  145. mge_tensor->copy_from(output_tensors[0]);
  146. return ret;
  147. }
  148. } // namespace lite
  149. #endif
  150. static inline bool check_gpu_available(size_t num) {
  151. if (mgb::CompNode::get_device_count(mgb::CompNode::DeviceType::CUDA) < num) {
  152. mgb_log_warn("skip test case that requires %zu GPU(s)", num);
  153. return false;
  154. }
  155. return true;
  156. }
  157. #define REQUIRE_CUDA() \
  158. { \
  159. if (!check_gpu_available(1)) { \
  160. return; \
  161. } \
  162. } \
  163. while (0)
  164. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}