"git@developer.sourcefind.cn:sugon_wxj/megatron-lm.git" did not exist on "06fc51cef50fded88e0142f32b40dc615e39672a"
int8_gemm_pack.cpp 4.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
25
#include <migraphx/shape.hpp>
#include <migraphx/argument.hpp>
26
#include <migraphx/gpu/device/int8_gemm_pack.hpp>
27
28
#include <migraphx/gpu/device/launch.hpp>
#include <migraphx/gpu/device/types.hpp>
29
#include <migraphx/gpu/device/tensor.hpp>
30
31
32
33
34
35

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {
namespace device {

36
void int8_gemm_pack_a(hipStream_t stream, const argument& result, const argument& arg)
37
{
Shucai Xiao's avatar
Shucai Xiao committed
38
    auto comp_shape    = arg.get_shape();
Shucai Xiao's avatar
Shucai Xiao committed
39
    auto out_lens      = comp_shape.lens();
Shucai Xiao's avatar
Shucai Xiao committed
40
41
    auto dim_0         = out_lens.size() - 2;
    auto dim_1         = out_lens.size() - 1;
Shucai Xiao's avatar
Shucai Xiao committed
42
    std::size_t lda    = comp_shape.strides()[dim_0];
43
    std::size_t m_size = out_lens[dim_0] * out_lens[dim_1];
Shucai Xiao's avatar
Shucai Xiao committed
44
    visit_all(result, arg)([&](auto output, auto input) {
Shucai Xiao's avatar
Shucai Xiao committed
45
        std::size_t nelements = comp_shape.elements();
Shucai Xiao's avatar
Shucai Xiao committed
46
47
        auto* out_ptr         = device_cast(output.data());
        auto* in_ptr          = device_cast(input.data());
48
        visit_tensor_size(out_lens.size(), [&](auto out_dim) {
Shucai Xiao's avatar
Shucai Xiao committed
49
            hip_tensor_descriptor<out_dim> desc(comp_shape);
50
            gs_launch(stream, nelements, 256)([=](auto ii) __device__ {
Shucai Xiao's avatar
Shucai Xiao committed
51
52
53
54
                const size_t nb    = 4;
                auto idx           = desc.multi(ii);
                std::size_t i_m    = idx[dim_1];
                std::size_t i_k    = idx[dim_0];
55
                std::size_t offset = ii / m_size * m_size;
Shucai Xiao's avatar
Shucai Xiao committed
56
57
                out_ptr[i_k % nb + (i_m + (i_k / nb) * lda) * nb + offset] =
                    in_ptr[i_m + i_k * lda + offset];
58
59
60
61
62
            });
        });
    });
}

63
void int8_gemm_pack_b(hipStream_t stream, const argument& result, const argument& arg)
64
{
Shucai Xiao's avatar
Shucai Xiao committed
65
    auto trans_shape = arg.get_shape();
Shucai Xiao's avatar
Shucai Xiao committed
66
67
68
69
    auto out_lens    = trans_shape.lens();
    auto dim_0       = trans_shape.lens().size() - 2;
    auto dim_1       = trans_shape.lens().size() - 1;
    std::size_t ldb  = trans_shape.strides()[dim_1];
70
71
72

    auto wrap_lens = out_lens;
    std::swap(wrap_lens[dim_0], wrap_lens[dim_1]);
Shucai Xiao's avatar
Shucai Xiao committed
73
    shape comp_shape{trans_shape.type(), wrap_lens};
74
    std::size_t m_size = out_lens[dim_0] * out_lens[dim_1];
Shucai Xiao's avatar
Shucai Xiao committed
75
    visit_all(result, arg)([&](auto output, auto input) {
Shucai Xiao's avatar
Shucai Xiao committed
76
        std::size_t nelements = comp_shape.elements();
Shucai Xiao's avatar
Shucai Xiao committed
77
78
        auto* out_ptr         = device_cast(output.data());
        auto* in_ptr          = device_cast(input.data());
79
        visit_tensor_size(out_lens.size(), [&](auto out_dim) {
Shucai Xiao's avatar
Shucai Xiao committed
80
            hip_tensor_descriptor<out_dim> desc(comp_shape);
81
            gs_launch(stream, nelements, 256)([=](auto ii) __device__ {
Shucai Xiao's avatar
Shucai Xiao committed
82
83
                const size_t nb    = 4;
                auto idx           = desc.multi(ii);
84
85
                std::size_t i_n    = idx[dim_1];
                std::size_t i_k    = idx[dim_0];
86
                std::size_t offset = ii / m_size * m_size;
Shucai Xiao's avatar
Shucai Xiao committed
87
88
                out_ptr[i_k % nb + (i_n + (i_k / nb) * ldb) * nb + offset] =
                    in_ptr[i_n + i_k * ldb + offset];
89
90
91
92
93
94
95
96
97
            });
        });
    });
}

} // namespace device
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx