mul.cpp 1.54 KB
Newer Older
Paul's avatar
Paul committed
1
2
#include <migraphx/gpu/device/mul.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>
Khalique's avatar
Khalique committed
5

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

Shucai Xiao's avatar
Shucai Xiao committed
11
12
13
14
15
16
17
18
19
20
__global__ void mul_kernel(__half* a, __half* b, __half* r, int n)
{
    int tid = blockIdx.x * blockDim.x + threadIdx.x;
    if (tid < n)
    {
        r[tid] = a[tid] * b[tid%768];
    }
}


Khalique's avatar
Khalique committed
21
void mul(hipStream_t stream, const argument& result, const argument& arg1, const argument& arg2)
Khalique's avatar
Khalique committed
22
{
Shucai Xiao's avatar
Shucai Xiao committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    auto s2 = arg2.get_shape();
    if (s2.element_space() == 768 and s2.type() == shape::half_type)
    {
        auto elem_num = s2.elements();
        int block_size = 1024;
        int block_num = (elem_num + block_size - 1) / block_size;
        mul_kernel<<<block_num, block_size>>>(reinterpret_cast<__half*>(arg1.data()),
                                              reinterpret_cast<__half*>(arg2.data()), 
                                              reinterpret_cast<__half*>(result.data()), elem_num);
    }
    else
    {
        nary(stream, result, arg1, arg2)([](auto x, auto y) __device__ { return x * y; });        
    }
Khalique's avatar
Khalique committed
37
38
39
40
41
42
43
44
}

void mul(hipStream_t stream,
         const argument& result,
         const argument& arg1,
         const argument& arg2,
         const argument& arg3)
{
45
46
    nary(stream, result, arg1, arg2, arg3)([](auto x, auto y, auto z)
                                               __device__ { return x * y * z; });
Khalique's avatar
Khalique committed
47
48
49
50
}

} // namespace device
} // namespace gpu
Paul's avatar
Paul committed
51
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
52
} // namespace migraphx