add.cpp 1.94 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)
Shucai Xiao's avatar
Shucai Xiao committed
12
{
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    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
28
    int tid     = blockIdx.x * blockDim.x + threadIdx.x;
Shucai Xiao's avatar
Shucai Xiao committed
29
    if(tid < n)
Shucai Xiao's avatar
Shucai Xiao committed
30
    {
31
32
        int idb = tid % n_dim;
        hr[tid] = __hadd2(ha[tid], hb[idb]);
Shucai Xiao's avatar
Shucai Xiao committed
33
34
35
    }
}

Paul's avatar
Paul committed
36
void add(hipStream_t stream, const argument& result, const argument& arg1, const argument& arg2)
Paul's avatar
Paul committed
37
{
38
39
40
41
42
    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
43
    {
44
        auto elem_num  = sr.elements() / 2;
Shucai Xiao's avatar
Shucai Xiao committed
45
        auto last_dim  = sr.lens().back() / 2;
Shucai Xiao's avatar
Shucai Xiao committed
46
        int block_size = 1024;
Shucai Xiao's avatar
Shucai Xiao committed
47
        int block_num  = (elem_num + block_size - 1) / block_size;
48
        add_kernel<<<block_num, block_size, 0, stream>>>(
Shucai Xiao's avatar
Shucai Xiao committed
49
            arg1.data(), arg2.data(), last_dim, result.data(), elem_num);
Shucai Xiao's avatar
Shucai Xiao committed
50
51
52
    }
    else
    {
Shucai Xiao's avatar
Shucai Xiao committed
53
        nary(stream, result, arg1, arg2)([](auto x, auto y) __device__ { return x + y; });
Shucai Xiao's avatar
Shucai Xiao committed
54
    }
Paul's avatar
Paul committed
55
56
}

Paul's avatar
Paul committed
57
58
59
60
61
void add(hipStream_t stream,
         const argument& result,
         const argument& arg1,
         const argument& arg2,
         const argument& arg3)
Paul's avatar
Paul committed
62
{
63
64
    nary(stream, result, arg1, arg2, arg3)([](auto x, auto y, auto z)
                                               __device__ { return x + y + z; });
Paul's avatar
Paul committed
65
66
}

Paul's avatar
Paul committed
67
68
} // namespace device
} // namespace gpu
Paul's avatar
Paul committed
69
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
70
} // namespace migraphx