contiguous.cpp 2.57 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 <migraphx/permutation.hpp>
5
#include <hip/hip_fp16.h>
6

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

Shucai Xiao's avatar
Shucai Xiao committed
12
13
__global__ void
cont_kernel(void* in, void* out, int os1, int os2, int os3, int is1, int is2, int is3)
14
15
16
17
18
19
{
    int i1 = blockIdx.x;
    int i2 = blockIdx.y;
    int i3 = blockIdx.z;
    int i4 = threadIdx.x;

Shucai Xiao's avatar
Shucai Xiao committed
20
    __half* in_ptr  = reinterpret_cast<__half*>(in);
21
22
    __half* out_ptr = reinterpret_cast<__half*>(out);

Shucai Xiao's avatar
Shucai Xiao committed
23
24
    int out_idx      = i1 * os1 + i2 * os2 + i3 * os3 + i4;
    int in_idx       = i1 * is1 + i2 * is2 + i3 * is3 + i4;
25
26
27
    out_ptr[out_idx] = in_ptr[in_idx];
}

28
29
30
void contiguous_nonstandard(hipStream_t stream, const argument& result, const argument& arg)
{
    shape s{result.get_shape().type(), result.get_shape().lens()};
31
32
33
34
35
36
37
38
39
40
41
42
    // auto in_s = arg.get_shape();
    // auto perm = find_permutation(in_s);
    // if (in_s.type() == shape::half_type and perm == std::vector<int64_t>({0, 2, 1, 3}))
    // {
    //     auto lens = s.lens();
    //     auto last_dim = s.lens().back();
    //     dim3 grid(lens[0], lens[1], lens[2]);
    //     dim3 block(last_dim);

    //     auto in_stride = in_s.strides();
    //     auto out_stride = s.strides();

Shucai Xiao's avatar
Shucai Xiao committed
43
44
    //     cont_kernel<<<grid, block, 0, stream>>>(arg.data(), result.data(), out_stride[0],
    //     out_stride[1], out_stride[2], in_stride[0], in_stride[1], in_stride[2]);
45
46
47
    // }
    // else
    // {
Shucai Xiao's avatar
Shucai Xiao committed
48
49
50
51
52
53
    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]; });
        });
    });
54
    // }
55
56
57
58
59
}

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
60
61
62
63
64
    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]; });
    });
65
66
}

67
void contiguous(hipStream_t stream, const argument& result, const argument& arg)
68
{
69
70
71
72
    if(result.get_shape() == arg.get_shape() and result.get_shape().packed())
        contiguous_packed(stream, result, arg);
    else
        contiguous_nonstandard(stream, result, arg);
73
74
}

75
} // namespace device
Paul's avatar
Paul committed
76
} // namespace gpu
Paul's avatar
Paul committed
77
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
78
} // namespace migraphx