convolution_host_tensor_descriptor_helper.hpp Source File

convolution_host_tensor_descriptor_helper.hpp Source File#

Composable Kernel: convolution_host_tensor_descriptor_helper.hpp Source File
library/utility/convolution_host_tensor_descriptor_helper.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5
6#include "ck/ck.hpp"
8
11
12namespace ck {
13namespace utils {
14namespace conv {
15
16namespace detail {
17
18template <typename OldLayout>
19std::vector<std::size_t> get_layout_transpose_gnchw_to_old()
20{
21 // HACK: NHWC/KYXC/NHWK, which is treated as GNHWC/GKYXC/GNHWK by this function,
22 // is used by some legacy kernel. New kernel should use GNHWK/GKYXC/GNHWK
23 // TODO: remove this branch after removing legacy kernel
27 {
28 return {0, 1, 3, 2};
29 }
33 {
34 return {0, 1, 4, 2, 3};
35 }
39 {
40 return {0, 1, 5, 2, 3, 4};
41 }
42 // separate from legacy code above
46 {
47 return {0, 1, 2, 3};
48 }
51 {
52 return {1, 0, 2, 3};
53 }
56 {
57 return {1, 0, 2, 3, 4};
58 }
61 {
62 return {1, 0, 2, 3, 4, 5};
63 }
67 {
68 return {0, 1, 2, 3, 4};
69 }
73 {
74 return {0, 1, 2, 3, 4, 5};
75 }
79 {
80 return {0, 1, 3, 2};
81 }
85 {
86 return {0, 1, 4, 2, 3};
87 }
91 {
92 return {0, 1, 5, 2, 3, 4};
93 }
97 {
98 return {2, 0, 3, 1};
99 }
103 {
104 return {3, 0, 4, 1, 2};
105 }
109 {
110 return {4, 0, 5, 1, 2, 3};
111 }
112 else
113 {
114 printf("%s\n", __func__);
115 throw std::runtime_error("wrong! unsupported layout");
116 }
117}
118
119} // namespace detail
120
121// make tensor descriptor for packed input tensor, and order the dimension in the order of GNCHW
122// regardless of physical layout
123template <typename InLayout>
126{
127 std::vector<std::size_t> physical_lengths;
128
129 // HACK: NHWC/KYXC/NHWK, which is treated as GNHWC/GKYXC/GNHWK by this function,
130 // is used by some legacy kernel. New kernel should use GNHWK/GKYXC/GNHWK
131 // TODO: remove this branch after removing legacy kernel
135 {
136 if(param.G_ != 1)
137 {
138 throw std::runtime_error("wrong! G != 1");
139 }
140
141 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.G_),
142 static_cast<std::size_t>(param.N_),
143 static_cast<std::size_t>(param.C_)};
144
145 physical_lengths.insert(physical_lengths.begin() + 2,
146 param.input_spatial_lengths_.begin(),
147 param.input_spatial_lengths_.begin() + param.num_dim_spatial_);
148 }
149 // separate from legacy code above
153 {
154 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.N_),
155 static_cast<std::size_t>(param.G_),
156 static_cast<std::size_t>(param.C_)};
157
158 physical_lengths.insert(physical_lengths.end(),
159 param.input_spatial_lengths_.begin(),
160 param.input_spatial_lengths_.begin() + param.num_dim_spatial_);
161 }
165 {
166 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.G_),
167 static_cast<std::size_t>(param.N_),
168 static_cast<std::size_t>(param.C_)};
169
170 physical_lengths.insert(physical_lengths.end(),
171 param.input_spatial_lengths_.begin(),
172 param.input_spatial_lengths_.begin() + param.num_dim_spatial_);
173 }
177 {
178 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.G_),
179 static_cast<std::size_t>(param.N_),
180 static_cast<std::size_t>(param.C_)};
181
182 physical_lengths.insert(physical_lengths.begin() + 2,
183 param.input_spatial_lengths_.begin(),
184 param.input_spatial_lengths_.begin() + param.num_dim_spatial_);
185 }
189 {
190 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.N_),
191 static_cast<std::size_t>(param.G_),
192 static_cast<std::size_t>(param.C_)};
193
194 physical_lengths.insert(physical_lengths.begin() + 1,
195 param.input_spatial_lengths_.begin(),
196 param.input_spatial_lengths_.begin() + param.num_dim_spatial_);
197 }
198 else
199 {
200 printf("%s\n", __func__);
201 printf("%s\n", InLayout::name);
202 throw std::runtime_error("wrong! unsupported layout");
203 }
204
206 // TBD: specify explicit conv layout rather than base one
207 HostTensorDescriptor(physical_lengths,
210 InLayout{});
211}
212
213// make tensor descriptor for packed weight tensor, and order the dimension in the order of GKCYX
214// regardless of physical layout
215template <typename WeiLayout>
218{
219 std::vector<std::size_t> physical_lengths;
220
221 // HACK: NHWC/KYXC/NHWK, which is treated as GNHWC/GKYXC/GNHWK by this function,
222 // is used by some legacy kernel. New kernel should use GNHWK/GKYXC/GNHWK
223 // TODO: remove this branch after removing legacy kernel
227 {
228 if(param.G_ != 1)
229 {
230 throw std::runtime_error("wrong! G != 1");
231 }
232
233 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.G_),
234 static_cast<std::size_t>(param.K_),
235 static_cast<std::size_t>(param.C_)};
236
237 physical_lengths.insert(physical_lengths.begin() + 2,
238 param.filter_spatial_lengths_.begin(),
239 param.filter_spatial_lengths_.begin() + param.num_dim_spatial_);
240 }
241 // separate from legacy code above
245 {
246 if(param.G_ != 1)
247 {
248 throw std::runtime_error("wrong! G != 1");
249 }
250
251 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.K_),
252 static_cast<std::size_t>(param.C_)};
253
254 physical_lengths.insert(physical_lengths.end(),
255 param.filter_spatial_lengths_.begin(),
256 param.filter_spatial_lengths_.begin() + param.num_dim_spatial_);
257 }
261 {
262 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.G_),
263 static_cast<std::size_t>(param.K_),
264 static_cast<std::size_t>(param.C_)};
265
266 physical_lengths.insert(physical_lengths.end(),
267 param.filter_spatial_lengths_.begin(),
268 param.filter_spatial_lengths_.begin() + param.num_dim_spatial_);
269 }
273 {
274 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.G_),
275 static_cast<std::size_t>(param.K_),
276 static_cast<std::size_t>(param.C_)};
277
278 physical_lengths.insert(physical_lengths.begin() + 2,
279 param.filter_spatial_lengths_.begin(),
280 param.filter_spatial_lengths_.begin() + param.num_dim_spatial_);
281 }
285 {
286 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.K_),
287 static_cast<std::size_t>(param.G_),
288 static_cast<std::size_t>(param.C_)};
289
290 physical_lengths.insert(physical_lengths.begin() + 1,
291 param.filter_spatial_lengths_.begin(),
292 param.filter_spatial_lengths_.begin() + param.num_dim_spatial_);
293 }
294 else
295 {
296 printf("%s\n", __func__);
297 printf("%s\n", WeiLayout::name);
298 throw std::runtime_error("wrong! unsupported layout");
299 }
300
302 HostTensorDescriptor(physical_lengths,
305 WeiLayout{});
306}
307
308// make tensor descriptor for packed output tensor, and order the dimension in the order of GNKHW
309// regardless of physical layout
310template <typename OutLayout>
313{
314 std::vector<std::size_t> physical_lengths;
315
316 // HACK: NHWC/KYXC/NHWK, which is treated as GNHWC/GKYXC/GNHWK by this function,
317 // is used by some legacy kernel. New kernel should use GNHWK/GKYXC/GNHWK
318 // TODO: remove this branch after removing legacy kernel
322 {
323 if(param.G_ != 1)
324 {
325 throw std::runtime_error("wrong! G != 1");
326 }
327
328 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.G_),
329 static_cast<std::size_t>(param.N_),
330 static_cast<std::size_t>(param.K_)};
331
332 physical_lengths.insert(physical_lengths.begin() + 2,
333 param.output_spatial_lengths_.begin(),
334 param.output_spatial_lengths_.begin() + param.num_dim_spatial_);
335 }
336 // separate from legacy code above
340 {
341 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.G_),
342 static_cast<std::size_t>(param.N_),
343 static_cast<std::size_t>(param.K_)};
344
345 physical_lengths.insert(physical_lengths.end(),
346 param.output_spatial_lengths_.begin(),
347 param.output_spatial_lengths_.begin() + param.num_dim_spatial_);
348 }
349 // separate from legacy code above
353 {
354 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.N_),
355 static_cast<std::size_t>(param.G_),
356 static_cast<std::size_t>(param.K_)};
357
358 physical_lengths.insert(physical_lengths.end(),
359 param.output_spatial_lengths_.begin(),
360 param.output_spatial_lengths_.begin() + param.num_dim_spatial_);
361 }
365 {
366 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.G_),
367 static_cast<std::size_t>(param.N_),
368 static_cast<std::size_t>(param.K_)};
369
370 physical_lengths.insert(physical_lengths.begin() + 2,
371 param.output_spatial_lengths_.begin(),
372 param.output_spatial_lengths_.begin() + param.num_dim_spatial_);
373 }
377 {
378 physical_lengths = std::vector<std::size_t>{static_cast<std::size_t>(param.N_),
379 static_cast<std::size_t>(param.G_),
380 static_cast<std::size_t>(param.K_)};
381
382 physical_lengths.insert(physical_lengths.begin() + 1,
383 param.output_spatial_lengths_.begin(),
384 param.output_spatial_lengths_.begin() + param.num_dim_spatial_);
385 }
386 else
387 {
388 printf("%s\n", __func__);
389 printf("%s\n", OutLayout::name);
390 throw std::runtime_error("wrong! unsupported layout");
391 }
392
394 HostTensorDescriptor(physical_lengths,
397 OutLayout{});
398}
399
400} // namespace conv
401} // namespace utils
402} // namespace ck
HostTensorDescriptor transpose_host_tensor_descriptor_given_new2old(const HostTensorDescriptor &a, const New2Old &new2old, const NewLayout &new_layout=NewLayout())
Definition library/utility/host_tensor.hpp:599
Definition library/utility/convolution_host_tensor_descriptor_helper.hpp:16
std::vector< std::size_t > get_layout_transpose_gnchw_to_old()
Definition library/utility/convolution_host_tensor_descriptor_helper.hpp:19
Definition library/utility/convolution_host_tensor_descriptor_helper.hpp:14
HostTensorDescriptor make_weight_host_tensor_descriptor_g_k_c_xs_packed(const ck::utils::conv::ConvParam &param)
Definition library/utility/convolution_host_tensor_descriptor_helper.hpp:217
HostTensorDescriptor make_output_host_tensor_descriptor_g_n_k_wos_packed(const ck::utils::conv::ConvParam &param)
Definition library/utility/convolution_host_tensor_descriptor_helper.hpp:312
HostTensorDescriptor make_input_host_tensor_descriptor_g_n_c_wis_packed(const ck::utils::conv::ConvParam &param)
Definition library/utility/convolution_host_tensor_descriptor_helper.hpp:125
Definition library/utility/check_err.hpp:24
Definition ck.hpp:268
constexpr bool is_same_v
Definition type.hpp:283
A descriptor class for host tensors that manages tensor dimensions, strides, and layout.
Definition library/utility/host_tensor.hpp:171
Definition tensor_operation/gpu/device/tensor_layout.hpp:45
Definition library/utility/convolution_parameter.hpp:20
ck::long_index_t C_
Definition library/utility/convolution_parameter.hpp:50
ck::long_index_t num_dim_spatial_
Definition library/utility/convolution_parameter.hpp:46
std::vector< ck::long_index_t > input_spatial_lengths_
Definition library/utility/convolution_parameter.hpp:53
std::vector< ck::long_index_t > output_spatial_lengths_
Definition library/utility/convolution_parameter.hpp:54
ck::long_index_t N_
Definition library/utility/convolution_parameter.hpp:48
ck::long_index_t G_
Definition library/utility/convolution_parameter.hpp:47
ck::long_index_t K_
Definition library/utility/convolution_parameter.hpp:49
std::vector< ck::long_index_t > filter_spatial_lengths_
Definition library/utility/convolution_parameter.hpp:52