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.

param_pack.cpp 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "hcc_detail/hcc_defs_prologue.h"
  2. #include "test/rocm/fixture.h"
  3. #include "test/common/checker.h"
  4. #include "test/common/utils.h"
  5. using namespace megdnn;
  6. using namespace test;
  7. namespace {
  8. template <class T>
  9. std::vector<int32_t> create_offsets(const TensorShapeArray& shapes, size_t alignment) {
  10. size_t dtype_size = sizeof(T);
  11. if (alignment < dtype_size)
  12. alignment = dtype_size;
  13. alignment /= dtype_size;
  14. auto get_aligned = [alignment](size_t v) {
  15. auto mod = v & (alignment - 1);
  16. return v + ((alignment - mod) & (alignment - 1));
  17. };
  18. std::vector<dt_int32> offsets(shapes.size() << 1);
  19. size_t offset = 0;
  20. for (size_t i = 0; i < shapes.size(); i++) {
  21. offset = get_aligned(offset);
  22. offsets[i << 1] = offset;
  23. offset += shapes[i].total_nr_elems();
  24. offsets[(i << 1) + 1] = offset;
  25. }
  26. return offsets;
  27. }
  28. template <class T>
  29. std::vector<T> create_pack(
  30. size_t pack_size, const std::vector<int32_t>& offsets,
  31. const std::vector<std::vector<T>>& ptr) {
  32. megdnn_assert(pack_size == static_cast<size_t>(offsets.back()));
  33. std::vector<T> data(pack_size, 0);
  34. for (size_t i = 0; i * 2 < offsets.size(); ++i) {
  35. size_t begin = offsets[i * 2], end = offsets[i * 2 + 1];
  36. for (size_t j = 0; j < end - begin; j++)
  37. data[begin + j] = ptr[i][j];
  38. }
  39. return data;
  40. }
  41. template <class T>
  42. std::vector<std::vector<T>> create_params(
  43. size_t nr_params, const TensorShapeArray& shapes) {
  44. std::vector<std::vector<T>> params;
  45. for (size_t i = 0; i < nr_params; ++i) {
  46. std::vector<T> expected_data;
  47. for (size_t x = 0; x < shapes[i].total_nr_elems(); ++x) {
  48. expected_data.push_back(rand());
  49. }
  50. params.push_back(std::move(expected_data));
  51. }
  52. return params;
  53. }
  54. template <class T>
  55. T* create_device_data(Handle* handle, const T* data, size_t size) {
  56. T* data_device = static_cast<T*>(test::megdnn_malloc(handle, size * sizeof(T)));
  57. if (data)
  58. test::megdnn_memcpy_H2D(handle, data_device, data, size * sizeof(T));
  59. return data_device;
  60. }
  61. template <class T>
  62. void test_param_pack_concat(
  63. Handle* handle, const TensorShapeArray& shapes, DType type) {
  64. auto concat = handle->create_operator<ParamPackConcat>();
  65. size_t nr_params = shapes.size();
  66. std::vector<T*> param_ptrs;
  67. std::vector<std::vector<T>> params = create_params<T>(nr_params, shapes);
  68. for (size_t i = 0; i < nr_params; ++i) {
  69. param_ptrs.push_back(create_device_data<T>(
  70. handle, params[i].data(), shapes[i].total_nr_elems()));
  71. }
  72. std::vector<int32_t> offsets =
  73. create_offsets<T>(shapes, handle->alignment_requirement());
  74. size_t pack_size = offsets.back();
  75. int32_t* offsets_gpu =
  76. create_device_data<int32_t>(handle, offsets.data(), offsets.size());
  77. std::vector<T> expected_pack = create_pack<T>(pack_size, offsets, params);
  78. T* pack_gpu = create_device_data<T>(handle, nullptr, expected_pack.size());
  79. TensorLayout dst_layout({pack_size}, type);
  80. TensorND dst_tensor(pack_gpu, dst_layout);
  81. TensorLayout offsets_layout({offsets.size()}, dtype::Int32());
  82. TensorND offsets_tensor(offsets_gpu, offsets_layout);
  83. test::WorkspaceWrapper workspace(
  84. handle,
  85. concat->get_workspace_in_bytes({nr_params}, offsets_layout, {pack_size}));
  86. TensorND src_tensor(param_ptrs.data(), TensorLayout({nr_params}, dtype::Int32()));
  87. concat->exec(src_tensor, offsets_tensor, dst_tensor, workspace.workspace());
  88. // check
  89. T* actual_pack = static_cast<T*>(malloc(pack_size * sizeof(T)));
  90. test::megdnn_memcpy_D2H(handle, actual_pack, pack_gpu, sizeof(T) * pack_size);
  91. for (size_t i = 0; i < pack_size; ++i) {
  92. ASSERT_EQ(actual_pack[i], expected_pack[i]);
  93. }
  94. free(actual_pack);
  95. test::megdnn_free(handle, pack_gpu);
  96. test::megdnn_free(handle, offsets_gpu);
  97. for (auto ptr : param_ptrs) {
  98. test::megdnn_free(handle, ptr);
  99. }
  100. }
  101. } // namespace
  102. TEST_F(ROCM, PARAM_PACK) {
  103. SmallVector<TensorShapeArray> shapes_vec;
  104. shapes_vec.push_back({{1}});
  105. shapes_vec.push_back({{129}, {21}});
  106. shapes_vec.push_back({{15}, {21}, {34}});
  107. shapes_vec.push_back({{1, 2}, {3, 5}, {5, 8}, {7, 11}, {9, 14}});
  108. shapes_vec.push_back(
  109. {{1, 2},
  110. {3, 5},
  111. {1},
  112. {3, 3, 3, 4},
  113. {71},
  114. {9, 14},
  115. {111, 111, 111},
  116. {128, 128, 128}});
  117. for (auto shapes : shapes_vec) {
  118. test_param_pack_concat<int32_t>(handle_rocm(), shapes, dtype::Int32());
  119. test_param_pack_concat<int16_t>(handle_rocm(), shapes, dtype::Int16());
  120. test_param_pack_concat<float>(handle_rocm(), shapes, dtype::Float32());
  121. }
  122. }
  123. // vim: syntax=cpp.doxygen