contiguous.cpp 2.21 KB
Newer Older
1

Paul's avatar
Paul committed
2
3
#include <migraphx/gpu/device/contiguous.hpp>
#include <migraphx/gpu/device/nary.hpp>
4
#include <hip/hip_fp16.h>
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
namespace gpu {
9
namespace device {
10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
void contiguous_nonstandard(hipStream_t stream, const argument& result, const argument& arg)
{
    shape s{result.get_shape().type(), result.get_shape().lens()};
    visit_all(result, arg)([&](auto output_v, auto input_v) {
        hip_visit_views(output_v, input_v, s)([&](auto output, auto input, auto standard_shape) {
            mi_gs_launch(stream,
                         standard_shape)([=](auto idx) __device__ { output[idx] = input[idx]; });
        });
    });
}

void contiguous_packed(hipStream_t stream, const argument& result, const argument& arg)
{
    index_int nelements = result.get_shape().elements();
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    auto type = result.get_shape().type();
    if (type == shape::half_type)
    {
        visit_all(result, arg)([&](auto output_v, auto input_v) {
            const auto* input = device_cast(input_v.data());
            auto* output      = device_cast(output_v.data());
            const __half2* input2 = reinterpret_cast<__half2*>(input_v.data());
            __half2* output2 = reinterpret_cast<__half2*>(output_v.data());
            gs_launch(stream, nelements / 2)([=](auto i) __device__ { 
                output2[i] = input2[i]; 
                if (i == 0 and (nelements % 2) == 1)
                {
                    output[nelements - 1] = input[nelements - 1];
                }
            });
        });
    }
    else
    {
        visit_all(result, arg)([&](auto output_v, auto input_v) {
            const auto* input = device_cast(input_v.data());
            auto* output      = device_cast(output_v.data());
            gs_launch(stream, nelements)([=](auto i) __device__ { output[i] = input[i]; });
        });        
    }
50
51
}

52
void contiguous(hipStream_t stream, const argument& result, const argument& arg)
53
{
54
55
56
57
    if(result.get_shape() == arg.get_shape() and result.get_shape().packed())
        contiguous_packed(stream, result, arg);
    else
        contiguous_nonstandard(stream, result, arg);
58
59
}

60
} // namespace device
Paul's avatar
Paul committed
61
} // namespace gpu
Paul's avatar
Paul committed
62
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
63
} // namespace migraphx