Debian Codegraph @

clients/samples/25_hipblaslt_weight_swizzle_padding/sample_hipblaslt_weight_swizzle_padding.cpp

raw
1 /*******************************************************************************
2 *
3 * MIT License
4 *
5 * Copyright (C) 2024-2025 Advanced Micro Devices, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 *******************************************************************************/
26 #include "TensorDataManipulation.hpp"
27 #include <array>
28 #include <cstddef>
29 #include <iostream>
30
31 int main(int argc, char** argv)
32 {
33 constexpr size_t m{18};
34 constexpr size_t k{34};
35 auto weight = Tensor::Manipulation::Tensor::create<int>({m, k});
36
37 for(size_t i = 0; i < m; ++i)
38 {
39 for(size_t j = 0; j < k; ++j)
40 {
41 weight.setValue<int>({i, j}, i * k + j);
42 }
43 }
44
45 std::cout << "Original weight:\n";
46 Tensor::Manipulation::printTensorDataMultiDims<int>(std::cout, weight);
47 constexpr size_t MiM = 16;
48 constexpr size_t MiK = 16;
49 constexpr size_t MiKv = 4;
50 constexpr size_t PackK = 2;
51 constexpr auto MultipleM = MiM;
52 constexpr auto MultipleK = MiK * PackK;
53 const auto paddedM = (m / MultipleM + !!(m % MultipleM)) * MultipleM;
54 const auto paddedK = (k / MultipleK + !!(k % MultipleK)) * MultipleK;
55 Tensor::Manipulation::Shape paddedShape{paddedM, paddedK};
56 auto paddedWeight = ::Tensor::Manipulation::pad(weight, paddedShape, 0);
57 std::cout << "Padded weight:\n";
58 Tensor::Manipulation::printTensorDataMultiDims<int>(std::cout, paddedWeight);
59 paddedWeight.reshape({paddedM / MiM, MiM, paddedK / (MiK * PackK), MiK / MiKv, MiKv * PackK});
60 Tensor::Manipulation::Tensor permuted = permute(paddedWeight, {0, 2, 3, 1, 4});
61 std::cout << "Swizzle weight:\n";
62 Tensor::Manipulation::printTensorDataMultiDims<int>(std::cout, permuted);
63 return 0;
64 }