elementwise_add_4d.cpp 4.38 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

rocking5566's avatar
rocking5566 committed
4
5
6
#include <iostream>
#include <cstdlib>

Chao Liu's avatar
Chao Liu committed
7
8
#include "ck/ck.hpp"
#include "ck/tensor_operation/gpu/element/binary_element_wise_operation.hpp"
9
#include "ck/tensor_operation/gpu/device/impl/device_elementwise_impl.hpp"
Chao Liu's avatar
Chao Liu committed
10

11
#include "ck/library/utility/algorithm.hpp"
Chao Liu's avatar
Chao Liu committed
12
#include "ck/library/utility/check_err.hpp"
13
14
15
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_tensor.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
rocking5566's avatar
rocking5566 committed
16
17
18
19

using F16 = ck::half_t;
using F32 = float;

20
21
using ABDataType = F16;
using CDataType  = F16;
rocking5566's avatar
rocking5566 committed
22

23
using Add = ck::tensor_operation::element_wise::Add;
rocking5566's avatar
rocking5566 committed
24

25
using DeviceElementwiseAddInstance =
26
27
28
29
30
31
32
    ck::tensor_operation::device::DeviceElementwiseImpl<ck::Tuple<ABDataType, ABDataType>,
                                                        ck::Tuple<CDataType>,
                                                        Add,
                                                        4,
                                                        8,
                                                        ck::Sequence<8, 8>,
                                                        ck::Sequence<8>>;
33
34

template <typename HostTensorA, typename HostTensorB, typename HostTensorC, typename Functor>
rocking5566's avatar
rocking5566 committed
35
36
37
38
39
40
41
42
43
44
45
46
47
void host_elementwise4D(HostTensorC& C,
                        const HostTensorA& A,
                        const HostTensorB& B,
                        const std::vector<std::size_t>& shape,
                        Functor functor)
{
    using ctype = ck::remove_reference_t<decltype(C(0, 0, 0, 0))>;

    for(std::size_t n = 0; n < shape[0]; ++n)
        for(std::size_t c = 0; c < shape[1]; ++c)
            for(std::size_t h = 0; h < shape[2]; ++h)
                for(std::size_t w = 0; w < shape[3]; ++w)
                {
48
49
50
                    auto a_val  = A(n, c, h, w);
                    auto b_val  = B(n, c, h, w);
                    ctype c_val = 0;
rocking5566's avatar
rocking5566 committed
51
                    functor(c_val, a_val, b_val);
52
                    C(n, c, h, w) = c_val;
rocking5566's avatar
rocking5566 committed
53
54
55
56
57
58
59
60
61
62
                }
}

int main()
{
    bool do_verification = true;
    bool time_kernel     = false;

    std::vector<std::size_t> nchw = {4, 16, 32, 32};

rocking5566's avatar
rocking5566 committed
63
64
65
    Tensor<ABDataType> a(nchw);
    Tensor<ABDataType> b(nchw);
    Tensor<CDataType> c(nchw);
rocking5566's avatar
rocking5566 committed
66

rocking5566's avatar
rocking5566 committed
67
68
    a.GenerateTensorValue(GeneratorTensor_3<ABDataType>{0.0, 1.0});
    b.GenerateTensorValue(GeneratorTensor_3<ABDataType>{0.0, 1.0});
rocking5566's avatar
rocking5566 committed
69

70
71
72
    DeviceMem a_device_buf(sizeof(ABDataType) * a.mDesc.GetElementSpaceSize());
    DeviceMem b_device_buf(sizeof(ABDataType) * b.mDesc.GetElementSpaceSize());
    DeviceMem c_device_buf(sizeof(CDataType) * c.mDesc.GetElementSpaceSize());
rocking5566's avatar
rocking5566 committed
73

rocking5566's avatar
rocking5566 committed
74
75
    a_device_buf.ToDevice(a.mData.data());
    b_device_buf.ToDevice(b.mData.data());
rocking5566's avatar
rocking5566 committed
76

77
78
79
80
    std::array<const void*, 2> input = {a_device_buf.GetDeviceBuffer(),
                                        b_device_buf.GetDeviceBuffer()};
    std::array<void*, 1> output      = {c_device_buf.GetDeviceBuffer()};

81
82
83
84
85
    std::array<ck::index_t, 4> abc_lengths;
    std::array<ck::index_t, 4> a_strides;
    std::array<ck::index_t, 4> b_strides;
    std::array<ck::index_t, 4> c_strides;

86
87
88
89
    ck::ranges::copy(nchw, abc_lengths.begin());
    ck::ranges::copy(a.mDesc.GetStrides(), a_strides.begin());
    ck::ranges::copy(b.mDesc.GetStrides(), b_strides.begin());
    ck::ranges::copy(c.mDesc.GetStrides(), c_strides.begin());
90

rocking5566's avatar
rocking5566 committed
91
    auto broadcastAdd = DeviceElementwiseAddInstance{};
92
93
    auto argument     = broadcastAdd.MakeArgumentPointer(
        abc_lengths, {a_strides, b_strides}, {c_strides}, input, output, Add{});
rocking5566's avatar
rocking5566 committed
94
95
96

    if(!broadcastAdd.IsSupportedArgument(argument.get()))
    {
97
98
        throw std::runtime_error(
            "The runtime parameters seems not supported by the device instance, exiting!");
rocking5566's avatar
rocking5566 committed
99
100
101
102
103
104
105
106
107
108
109
    };

    auto broadcastAdd_invoker_ptr = broadcastAdd.MakeInvokerPointer();
    float ave_time =
        broadcastAdd_invoker_ptr->Run(argument.get(), StreamConfig{nullptr, time_kernel});

    std::cout << "Perf: " << ave_time << " ms" << std::endl;

    bool pass = true;
    if(do_verification)
    {
rocking5566's avatar
rocking5566 committed
110
111
        c_device_buf.FromDevice(c.mData.data());
        Tensor<CDataType> host_c(nchw);
rocking5566's avatar
rocking5566 committed
112

113
114
        host_elementwise4D<Tensor<ABDataType>, Tensor<ABDataType>, Tensor<CDataType>, Add>(
            host_c, a, b, nchw, Add{});
rocking5566's avatar
rocking5566 committed
115

116
        pass &= ck::utils::check_err(c, host_c, "Error: Incorrect results c", 1e-3, 1e-3);
rocking5566's avatar
rocking5566 committed
117
118
119
120
    }

    return pass ? 0 : 1;
}