| 2081 | |
| 2082 | namespace { |
| 2083 | void test_param_pack_concat(const TensorShapeArray& shapes, DType type) { |
| 2084 | auto cn = CompNode::load("xpu0"); |
| 2085 | auto graph = ComputingGraph::make(); |
| 2086 | auto align = cn.get_mem_addr_alignment() / type.size(); |
| 2087 | |
| 2088 | size_t size = 0; |
| 2089 | std::vector<size_t> begins; |
| 2090 | for (auto&& shape : shapes) { |
| 2091 | size = get_aligned_power2(size, align); |
| 2092 | begins.push_back(size); |
| 2093 | size += shape.total_nr_elems(); |
| 2094 | } |
| 2095 | |
| 2096 | SmallVector<SymbolVar> srcs; |
| 2097 | for (size_t i = 0; i < shapes.size(); i++) { |
| 2098 | auto data = std::make_shared<HostTensorND>(); |
| 2099 | data->comp_node(cn).dtype(dtype::Int32()).resize(shapes[i]); |
| 2100 | auto ptr = data->ptr<dt_int32>(); |
| 2101 | for (size_t j = 0; j < shapes[i].total_nr_elems(); j++) { |
| 2102 | ptr[j] = j; |
| 2103 | } |
| 2104 | auto nd = opr::Host2DeviceCopy::make(*graph, data); |
| 2105 | srcs.push_back(nd); |
| 2106 | } |
| 2107 | |
| 2108 | auto host_offsets_gen = megdnn::ParamPackConcat::gen_offsets( |
| 2109 | shapes, cn.get_mem_addr_alignment(), 4); |
| 2110 | ASSERT_EQ(host_offsets_gen.back(), size); |
| 2111 | auto host_offsets = std::make_shared<HostTensorND>(); |
| 2112 | host_offsets->comp_node(cn).dtype(dtype::Int32{}).resize({srcs.size() * 2}); |
| 2113 | memcpy(host_offsets->raw_ptr(), host_offsets_gen.data(), srcs.size() * 8); |
| 2114 | auto offsets = opr::Host2DeviceCopy::make(*graph, host_offsets); |
| 2115 | |
| 2116 | auto z = opr::ParamPackConcat::make(srcs, offsets, host_offsets_gen); |
| 2117 | HostTensorND host_z; |
| 2118 | |
| 2119 | auto func = graph->compile({make_callback_copy(z, host_z)}); |
| 2120 | func->execute(); |
| 2121 | |
| 2122 | HostTensorND expected; |
| 2123 | expected.comp_node(cn).dtype(dtype::Int32()).resize({size}); |
| 2124 | { |
| 2125 | auto ptr = expected.ptr<dt_int32>(); |
| 2126 | |
| 2127 | memset(ptr, 0, sizeof(int32_t) * size); |
| 2128 | for (size_t i = 0; i < begins.size(); i++) { |
| 2129 | auto begin = begins[i]; |
| 2130 | auto shape = shapes[i]; |
| 2131 | for (size_t j = 0; j < shape.total_nr_elems(); j++) { |
| 2132 | ptr[begin + j] = j; |
| 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | MGB_ASSERT_TENSOR_EQ(expected, host_z); |
| 2137 | } |
| 2138 | |
| 2139 | template <size_t nr_out> |
| 2140 | void test_param_pack_split(const TensorShapeArray& shapes) { |
no test coverage detected