contiguous.cpp 2.45 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
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) {
Shucai Xiao's avatar
Shucai Xiao committed
16
17
18
19
20
21
            gs_launch(stream, s.elements())([=](auto i) __device__ {
                auto idx = standard_shape.multi(i);
                output[idx] = input[idx];
            });
            // mi_gs_launch(stream,
            //              standard_shape)([=](auto idx) __device__ { output[idx] = input[idx]; });
22
23
24
25
26
27
28
        });
    });
}

void contiguous_packed(hipStream_t stream, const argument& result, const argument& arg)
{
    index_int nelements = result.get_shape().elements();
Shucai Xiao's avatar
Shucai Xiao committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    // 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
    // {
48
49
50
51
52
        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]; });
        });        
Shucai Xiao's avatar
Shucai Xiao committed
53
    // }
54
55
}

56
void contiguous(hipStream_t stream, const argument& result, const argument& arg)
57
{
58
59
60
61
    if(result.get_shape() == arg.get_shape() and result.get_shape().packed())
        contiguous_packed(stream, result, arg);
    else
        contiguous_nonstandard(stream, result, arg);
62
63
}

64
} // namespace device
Paul's avatar
Paul committed
65
} // namespace gpu
Paul's avatar
Paul committed
66
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
67
} // namespace migraphx