mul_add.cpp 2.49 KB
Newer Older
1
#include <migraphx/gpu/device/mul_add.hpp>
Paul's avatar
Paul committed
2
#include <migraphx/gpu/device/nary.hpp>
3
4
#include <hip/hip_runtime.h>
#include <hip/hip_fp16.h>
Paul's avatar
Paul committed
5
6
7
8
9
10

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

11
12
13
14
15
16
17
18
19
20
21
22
23
//__global__ void mul_add_kernel(void* a, void* x, void* b, void* r, int n)
//{
//    int id = blockDim.x * blockIdx.x + threadIdx.x;
//    __half* ha = reinterpret_cast<__half*>(a);
//    __half* hb = reinterpret_cast<__half*>(b);
//    __half* hx = reinterpret_cast<__half*>(x);
//    __half* hr = reinterpret_cast<__half*>(r);
//    if (id < n)
//    {
//        hr[id] = __float2half(__half2float(ha[id]) * __half2float(hx[id]) + __half2float(hb[id]));
//    }
//}

Shucai Xiao's avatar
Shucai Xiao committed
24
__global__ void mul_add_kernel(void* a, int an, void* x, int xn, void* b, int bn, void* r, int n)
25
{
Shucai Xiao's avatar
Shucai Xiao committed
26
    int id      = blockDim.x * blockIdx.x + threadIdx.x;
27
28
29
30
    __half2* ha = reinterpret_cast<__half2*>(a);
    __half2* hb = reinterpret_cast<__half2*>(b);
    __half2* hx = reinterpret_cast<__half2*>(x);
    __half2* hr = reinterpret_cast<__half2*>(r);
Shucai Xiao's avatar
Shucai Xiao committed
31
    if(id < n)
32
    {
Shucai Xiao's avatar
Shucai Xiao committed
33
        hr[id] = __hadd2(__hmul2(ha[id % an], hx[id % xn]), hb[id % bn]);
34
35
36
    }
}

Paul's avatar
Paul committed
37
void mul_add(hipStream_t stream,
Paul's avatar
Paul committed
38
39
40
41
             const argument& result,
             const argument& arg1,
             const argument& arg2,
             const argument& arg3)
Paul's avatar
Paul committed
42
{
43
    auto type = result.get_shape().type();
Shucai Xiao's avatar
Shucai Xiao committed
44
    if(type == shape::half_type)
45
    {
Shucai Xiao's avatar
Shucai Xiao committed
46
        std::cout << "case1" << std::endl;
Shucai Xiao's avatar
Shucai Xiao committed
47
48
49
        int s1e      = arg1.get_shape().element_space() / 2;
        int s2e      = arg2.get_shape().element_space() / 2;
        int s3e      = arg3.get_shape().element_space() / 2;
Shucai Xiao's avatar
Shucai Xiao committed
50
        int elem_num = result.get_shape().elements() / 2;
Shucai Xiao's avatar
Shucai Xiao committed
51
52
53
54
55
        s1e          = (s1e == 0 ? 1 : s1e);
        s2e          = (s2e == 0 ? 1 : s2e);
        s3e          = (s3e == 0 ? 1 : s3e);
        std::cout << "re =" << elem_num << ", s1e = " << s1e << ", s2e = " << s2e
                  << ", s3e = " << s3e << std::endl;
56
        int block_size = 1024;
Shucai Xiao's avatar
Shucai Xiao committed
57
58
59
        int block_num  = (elem_num + block_size - 1) / block_size;
        mul_add_kernel<<<block_num, block_size>>>(
            arg1.data(), s1e, arg2.data(), s2e, arg3.data(), s3e, result.data(), elem_num);
60
61
62
    }
    else
    {
Shucai Xiao's avatar
Shucai Xiao committed
63
        std::cout << "case2" << std::endl;
64
        nary(stream, result, arg1, arg2, arg3)([](auto x, auto a, auto b)
Shucai Xiao's avatar
Shucai Xiao committed
65
                                                   __device__ { return a * x + b; });
66
    }
Paul's avatar
Paul committed
67
68
69
70
71
72
}

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