"src/targets/gpu/vscode:/vscode.git/clone" did not exist on "d5bdfed0890eba7764691690a4aba6902e6fa02f"
add.cpp 2.05 KB
Newer Older
Paul's avatar
Paul committed
1
2
#include <migraphx/gpu/device/add.hpp>
#include <migraphx/gpu/device/nary.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
3
4
#include <hip/hip_runtime.h>
#include <hip/hip_fp16.h>
Paul's avatar
Paul committed
5

Paul's avatar
Paul committed
6
namespace migraphx {
Paul's avatar
Paul committed
7
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
8
9
10
namespace gpu {
namespace device {

11
static bool is_bert(const std::vector<shape>& ss)
12
13
14
15
16
17
18
{    
    auto last_dim = ss.front().lens().back();
    if (last_dim % 2 != 0)
    {
        return false;
    }

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    auto n_dim = ss.front().lens().size();
    if(n_dim == 2)
    {
        auto stride = ss.at(1).strides();
        return (stride[0] == 0);
    }

    return false;
}

__global__ void add_kernel(void* a, void* b, int n_dim, void* r, int n)
{
    __half2* ha = reinterpret_cast<__half2*>(a);
    __half2* hb = reinterpret_cast<__half2*>(b);
    __half2* hr = reinterpret_cast<__half2*>(r);
Shucai Xiao's avatar
Shucai Xiao committed
34
    int tid     = blockIdx.x * blockDim.x + threadIdx.x;
Shucai Xiao's avatar
Shucai Xiao committed
35
    if(tid < n)
Shucai Xiao's avatar
Shucai Xiao committed
36
    {
37
38
        int idb = tid % n_dim;
        hr[tid] = __hadd2(ha[tid], hb[idb]);
Shucai Xiao's avatar
Shucai Xiao committed
39
40
41
    }
}

Paul's avatar
Paul committed
42
void add(hipStream_t stream, const argument& result, const argument& arg1, const argument& arg2)
Paul's avatar
Paul committed
43
{
44
45
46
47
48
    auto sr = result.get_shape();
    std::vector<shape> ss;
    ss.push_back(arg1.get_shape());
    ss.push_back(arg2.get_shape());
    if(sr.type() == shape::half_type and is_bert(ss))
Shucai Xiao's avatar
Shucai Xiao committed
49
    {
50
        auto elem_num  = sr.elements() / 2;
Shucai Xiao's avatar
Shucai Xiao committed
51
        auto last_dim  = sr.lens().back() / 2;
Shucai Xiao's avatar
Shucai Xiao committed
52
        int block_size = 1024;
Shucai Xiao's avatar
Shucai Xiao committed
53
        int block_num  = (elem_num + block_size - 1) / block_size;
54
        add_kernel<<<block_num, block_size, 0, stream>>>(
Shucai Xiao's avatar
Shucai Xiao committed
55
            arg1.data(), arg2.data(), last_dim, result.data(), elem_num);
Shucai Xiao's avatar
Shucai Xiao committed
56
57
58
    }
    else
    {
Shucai Xiao's avatar
Shucai Xiao committed
59
        nary(stream, result, arg1, arg2)([](auto x, auto y) __device__ { return x + y; });
Shucai Xiao's avatar
Shucai Xiao committed
60
    }
Paul's avatar
Paul committed
61
62
}

Paul's avatar
Paul committed
63
64
65
66
67
void add(hipStream_t stream,
         const argument& result,
         const argument& arg1,
         const argument& arg2,
         const argument& arg3)
Paul's avatar
Paul committed
68
{
69
70
    nary(stream, result, arg1, arg2, arg3)([](auto x, auto y, auto z)
                                               __device__ { return x + y + z; });
Paul's avatar
Paul committed
71
72
}

Paul's avatar
Paul committed
73
74
} // namespace device
} // namespace gpu
Paul's avatar
Paul committed
75
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
76
} // namespace migraphx