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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * \file dnn/test/cuda/param_pack.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/common/checker.h"
  12. #include "test/common/utils.h"
  13. #include "test/cuda/fixture.h"
  14. using namespace megdnn;
  15. using namespace test;
  16. namespace {
  17. template <class T>
  18. std::vector<int32_t> create_offsets(const TensorShapeArray& shapes, size_t alignment) {
  19. size_t dtype_size = sizeof(T);
  20. if (alignment < dtype_size)
  21. alignment = dtype_size;
  22. alignment /= dtype_size;
  23. auto get_aligned = [alignment](size_t v) {
  24. auto mod = v & (alignment - 1);
  25. return v + ((alignment - mod) & (alignment - 1));
  26. };
  27. std::vector<dt_int32> offsets(shapes.size() << 1);
  28. size_t offset = 0;
  29. for (size_t i = 0; i < shapes.size(); i++) {
  30. offset = get_aligned(offset);
  31. offsets[i << 1] = offset;
  32. offset += shapes[i].total_nr_elems();
  33. offsets[(i << 1) + 1] = offset;
  34. }
  35. return offsets;
  36. }
  37. template <class T>
  38. std::vector<T> create_pack(
  39. size_t pack_size, const std::vector<int32_t>& offsets,
  40. const std::vector<std::vector<T>>& ptr) {
  41. megdnn_assert(pack_size == static_cast<size_t>(offsets.back()));
  42. std::vector<T> data(pack_size, 0);
  43. for (size_t i = 0; i * 2 < offsets.size(); ++i) {
  44. size_t begin = offsets[i * 2], end = offsets[i * 2 + 1];
  45. for (size_t j = 0; j < end - begin; j++)
  46. data[begin + j] = ptr[i][j];
  47. }
  48. return data;
  49. }
  50. template <class T>
  51. std::vector<std::vector<T>> create_params(
  52. size_t nr_params, const TensorShapeArray& shapes) {
  53. std::vector<std::vector<T>> params;
  54. for (size_t i = 0; i < nr_params; ++i) {
  55. std::vector<T> expected_data;
  56. for (size_t x = 0; x < shapes[i].total_nr_elems(); ++x) {
  57. expected_data.push_back(rand());
  58. }
  59. params.push_back(std::move(expected_data));
  60. }
  61. return params;
  62. }
  63. template <class T>
  64. T* create_device_data(Handle* handle, const T* data, size_t size) {
  65. T* data_device = static_cast<T*>(test::megdnn_malloc(handle, size * sizeof(T)));
  66. if (data)
  67. test::megdnn_memcpy_H2D(handle, data_device, data, size * sizeof(T));
  68. return data_device;
  69. }
  70. template <class T>
  71. void test_param_pack_concat(
  72. Handle* handle, const TensorShapeArray& shapes, DType type) {
  73. auto concat = handle->create_operator<ParamPackConcat>();
  74. size_t nr_params = shapes.size();
  75. std::vector<T*> param_ptrs;
  76. std::vector<std::vector<T>> params = create_params<T>(nr_params, shapes);
  77. for (size_t i = 0; i < nr_params; ++i) {
  78. param_ptrs.push_back(create_device_data<T>(
  79. handle, params[i].data(), shapes[i].total_nr_elems()));
  80. }
  81. std::vector<int32_t> offsets =
  82. create_offsets<T>(shapes, handle->alignment_requirement());
  83. size_t pack_size = offsets.back();
  84. int32_t* offsets_gpu =
  85. create_device_data<int32_t>(handle, offsets.data(), offsets.size());
  86. std::vector<T> expected_pack = create_pack<T>(pack_size, offsets, params);
  87. T* pack_gpu = create_device_data<T>(handle, nullptr, expected_pack.size());
  88. TensorLayout dst_layout({pack_size}, type);
  89. TensorND dst_tensor(pack_gpu, dst_layout);
  90. TensorLayout offsets_layout({offsets.size()}, dtype::Int32());
  91. TensorND offsets_tensor(offsets_gpu, offsets_layout);
  92. test::WorkspaceWrapper workspace(
  93. handle,
  94. concat->get_workspace_in_bytes(shapes, offsets_layout, {pack_size}));
  95. TensorND src_tensor(param_ptrs.data(), TensorLayout({nr_params}, dtype::Int32()));
  96. concat->exec(src_tensor, offsets_tensor, dst_tensor, workspace.workspace());
  97. // check
  98. T* actual_pack = static_cast<T*>(malloc(pack_size * sizeof(T)));
  99. test::megdnn_memcpy_D2H(handle, actual_pack, pack_gpu, sizeof(T) * pack_size);
  100. for (size_t i = 0; i < pack_size; ++i) {
  101. ASSERT_EQ(actual_pack[i], expected_pack[i]);
  102. }
  103. free(actual_pack);
  104. test::megdnn_free(handle, pack_gpu);
  105. test::megdnn_free(handle, offsets_gpu);
  106. for (auto ptr : param_ptrs) {
  107. test::megdnn_free(handle, ptr);
  108. }
  109. }
  110. } // namespace
  111. TEST_F(CUDA, PARAM_PACK) {
  112. SmallVector<TensorShapeArray> shapes_vec;
  113. shapes_vec.push_back({{1}});
  114. shapes_vec.push_back({{129}, {21}});
  115. shapes_vec.push_back({{15}, {21}, {34}});
  116. shapes_vec.push_back({{1, 2}, {3, 5}, {5, 8}, {7, 11}, {9, 14}});
  117. shapes_vec.push_back(
  118. {{1, 2},
  119. {3, 5},
  120. {1},
  121. {3, 3, 3, 4},
  122. {71},
  123. {9, 14},
  124. {111, 111, 111},
  125. {128, 128, 128}});
  126. for (auto shapes : shapes_vec) {
  127. test_param_pack_concat<int32_t>(handle_cuda(), shapes, dtype::Int32());
  128. test_param_pack_concat<int16_t>(handle_cuda(), shapes, dtype::Int16());
  129. test_param_pack_concat<float>(handle_cuda(), shapes, dtype::Float32());
  130. }
  131. }
  132. // vim: syntax=cpp.doxygen