| 72 | |
| 73 | template <class T> |
| 74 | void test_param_pack_concat( |
| 75 | Handle* handle, const TensorShapeArray& shapes, DType type, |
| 76 | bool bench = false) { |
| 77 | auto concat = handle->create_operator<ParamPackConcat>(); |
| 78 | size_t nr_params = shapes.size(); |
| 79 | |
| 80 | std::vector<T*> param_ptrs; |
| 81 | std::vector<std::vector<T>> params = create_params<T>(nr_params, shapes); |
| 82 | for (size_t i = 0; i < nr_params; ++i) { |
| 83 | param_ptrs.push_back(create_device_data<T>( |
| 84 | handle, params[i].data(), shapes[i].total_nr_elems())); |
| 85 | } |
| 86 | std::vector<int32_t> offsets = |
| 87 | create_offsets<T>(shapes, handle->alignment_requirement()); |
| 88 | size_t pack_size = offsets.back(); |
| 89 | int32_t* offsets_gpu = |
| 90 | create_device_data<int32_t>(handle, offsets.data(), offsets.size()); |
| 91 | |
| 92 | std::vector<T> expected_pack = create_pack<T>(pack_size, offsets, params); |
| 93 | T* pack_gpu = create_device_data<T>(handle, nullptr, expected_pack.size()); |
| 94 | |
| 95 | TensorLayout dst_layout({pack_size}, type); |
| 96 | TensorND dst_tensor(pack_gpu, dst_layout); |
| 97 | |
| 98 | TensorLayout offsets_layout({offsets.size()}, dtype::Int32()); |
| 99 | TensorND offsets_tensor(offsets_gpu, offsets_layout); |
| 100 | |
| 101 | test::WorkspaceWrapper workspace( |
| 102 | handle, |
| 103 | concat->get_workspace_in_bytes({nr_params}, offsets_layout, {pack_size})); |
| 104 | TensorND src_tensor(param_ptrs.data(), TensorLayout({nr_params}, dtype::Int32())); |
| 105 | int nr_times = 100; |
| 106 | concat->exec(src_tensor, offsets_tensor, dst_tensor, workspace.workspace()); |
| 107 | if (bench) { |
| 108 | Timer timer; |
| 109 | auto* cnrt_handle = static_cast<cambricon::HandleImpl*>(handle); |
| 110 | cnrtQueueSync(cnrt_handle->queue()); |
| 111 | timer.start(); |
| 112 | for (int i = 0; i < nr_times; i++) { |
| 113 | concat->exec(src_tensor, offsets_tensor, dst_tensor, workspace.workspace()); |
| 114 | } |
| 115 | cnrtQueueSync(cnrt_handle->queue()); |
| 116 | timer.stop(); |
| 117 | printf("time used: %f ms\n", timer.get_time_in_us() / 1000.0 / nr_times); |
| 118 | } |
| 119 | // check |
| 120 | T* actual_pack = static_cast<T*>(malloc(pack_size * sizeof(T))); |
| 121 | test::megdnn_memcpy_D2H(handle, actual_pack, pack_gpu, sizeof(T) * pack_size); |
| 122 | for (size_t i = 0; i < pack_size; ++i) { |
| 123 | ASSERT_EQ(actual_pack[i], expected_pack[i]) |
| 124 | << i << " " << actual_pack[i] << " " << expected_pack[i]; |
| 125 | } |
| 126 | free(actual_pack); |
| 127 | test::megdnn_free(handle, pack_gpu); |
| 128 | test::megdnn_free(handle, offsets_gpu); |
| 129 | for (auto ptr : param_ptrs) { |
| 130 | test::megdnn_free(handle, ptr); |
| 131 | } |
nothing calls this directly
no test coverage detected