pad.cpp 1.52 KB
Newer Older
1
2
3
4
5
6
#include <migraphx/shape.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/gpu/device/nary.hpp>
#include <migraphx/gpu/device/pad.hpp>
#include <migraphx/gpu/device/tensor.hpp>
#include <migraphx/gpu/device/launch.hpp>
Khalique's avatar
Khalique committed
7
#include <migraphx/float_equal.hpp>
8
9
10
11
12
13

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

Khalique's avatar
Khalique committed
14
15
argument
pad(hipStream_t stream, argument result, argument arg1, float value, std::vector<std::int64_t> pads)
16
17
{
    std::size_t nelements = arg1.get_shape().elements();
Paul's avatar
Paul committed
18
    hip_visit_all(result, arg1)([&](auto output, auto input) {
Paul's avatar
Paul committed
19
20
        using type      = typename decltype(output)::value_type;
        using hip_index = typename decltype(output)::hip_index;
Paul's avatar
Paul committed
21
        type device_val = value;
Khalique's avatar
Khalique committed
22
23
24
25
        if(float_equal(value, std::numeric_limits<float>::lowest()))
        {
            device_val = device_cast(std::numeric_limits<type>::lowest());
        }
Paul's avatar
Paul committed
26
27
        gs_launch(stream,
                  result.get_shape().elements())([=](auto i) { output.data()[i] = device_val; });
28

Paul's avatar
Paul committed
29
30
31
32
33
34
35
36
37
        hip_index offsets;
        std::copy(pads.begin(), pads.begin() + offsets.size(), offsets.begin());
        gs_launch(stream, nelements)([=](auto i) {
            auto idx = input.get_shape().multi(i);
            for(std::size_t j = 0; j < offsets.size(); j++)
            {
                idx[j] += offsets[j];
            }
            output[idx] = input.data()[i];
Paul's avatar
Paul committed
38
        });
39
40
41
42
43
44
45
46
    });
    return result;
}

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