"...composable_kernel.git" did not exist on "a1b2441f8d3fc229629d0c6c18ef5836d1548e12"
elementwise_add_4d.cpp 5.73 KB
Newer Older
myamlak's avatar
myamlak committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*******************************************************************************
 *
 * MIT License
 *
 * Copyright (c) 2020 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *******************************************************************************/
rocking5566's avatar
rocking5566 committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cstdlib>
#include "check_err.hpp"
#include "config.hpp"
#include "device.hpp"
#include "host_tensor.hpp"
#include "host_tensor_generator.hpp"

#include "device_tensor.hpp"
#include "binary_element_wise_operation.hpp"
#include "device_binary_elementwise.hpp"

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

using ABDataType             = F16;
using CDataType              = F16;
using EltwiseComputeDataType = F32;

45
using Add = ck::tensor_operation::element_wise::Add;
rocking5566's avatar
rocking5566 committed
46

47
48
49
50
51
52
53
54
55
56
57
using DeviceElementwiseAddInstance =
    ck::tensor_operation::device::DeviceBinaryElementwise<ABDataType,
                                                          ABDataType,
                                                          CDataType,
                                                          EltwiseComputeDataType,
                                                          Add,
                                                          4,
                                                          8,
                                                          8,
                                                          8,
                                                          8>;
rocking5566's avatar
rocking5566 committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

template <typename HostTensorA,
          typename HostTensorB,
          typename HostTensorC,
          typename ComputeDataType,
          typename Functor>
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)
                {
myamlak's avatar
myamlak committed
77
78
                    ComputeDataType a_val = ck::type_convert<ComputeDataType>(A(n, c, h, w));
                    ComputeDataType b_val = ck::type_convert<ComputeDataType>(B(n, c, h, w));
rocking5566's avatar
rocking5566 committed
79
80
                    ComputeDataType c_val = 0;
                    functor(c_val, a_val, b_val);
myamlak's avatar
myamlak committed
81
                    C(n, c, h, w) = ck::type_convert<ctype>(c_val);
rocking5566's avatar
rocking5566 committed
82
83
84
85
86
87
88
89
90
91
                }
}

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
92
93
94
    Tensor<ABDataType> a(nchw);
    Tensor<ABDataType> b(nchw);
    Tensor<CDataType> c(nchw);
rocking5566's avatar
rocking5566 committed
95

rocking5566's avatar
rocking5566 committed
96
97
    a.GenerateTensorValue(GeneratorTensor_3<ABDataType>{0.0, 1.0});
    b.GenerateTensorValue(GeneratorTensor_3<ABDataType>{0.0, 1.0});
rocking5566's avatar
rocking5566 committed
98

rocking5566's avatar
rocking5566 committed
99
100
101
    DeviceMem a_device_buf(sizeof(ABDataType) * a.mDesc.GetElementSpace());
    DeviceMem b_device_buf(sizeof(ABDataType) * b.mDesc.GetElementSpace());
    DeviceMem c_device_buf(sizeof(CDataType) * c.mDesc.GetElementSpace());
rocking5566's avatar
rocking5566 committed
102

rocking5566's avatar
rocking5566 committed
103
104
    a_device_buf.ToDevice(a.mData.data());
    b_device_buf.ToDevice(b.mData.data());
rocking5566's avatar
rocking5566 committed
105
106
107

    auto broadcastAdd = DeviceElementwiseAddInstance{};
    auto argument     = broadcastAdd.MakeArgumentPointer(
rocking5566's avatar
rocking5566 committed
108
109
110
111
112
113
114
        a_device_buf.GetDeviceBuffer(),
        b_device_buf.GetDeviceBuffer(),
        c_device_buf.GetDeviceBuffer(),
        std::vector<ck::index_t>{nchw.begin(), nchw.end()},
        std::vector<ck::index_t>{a.mDesc.GetStrides().begin(), a.mDesc.GetStrides().end()},
        std::vector<ck::index_t>{b.mDesc.GetStrides().begin(), b.mDesc.GetStrides().end()},
        std::vector<ck::index_t>{c.mDesc.GetStrides().begin(), c.mDesc.GetStrides().end()},
rocking5566's avatar
rocking5566 committed
115
116
117
118
119
        Add{});

    if(!broadcastAdd.IsSupportedArgument(argument.get()))
    {
        throw std::runtime_error("The runtime parameters seems not supported by the "
120
                                 "DeviceBinaryElementwise instance, exiting!");
rocking5566's avatar
rocking5566 committed
121
122
123
124
125
126
127
128
129
130
131
    };

    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
132
133
        c_device_buf.FromDevice(c.mData.data());
        Tensor<CDataType> host_c(nchw);
rocking5566's avatar
rocking5566 committed
134
135
136
137
138

        host_elementwise4D<Tensor<ABDataType>,
                           Tensor<ABDataType>,
                           Tensor<CDataType>,
                           EltwiseComputeDataType,
rocking5566's avatar
rocking5566 committed
139
                           Add>(host_c, a, b, nchw, Add{});
rocking5566's avatar
rocking5566 committed
140

rocking5566's avatar
rocking5566 committed
141
        pass &=
142
            ck::utils::check_err(c.mData, host_c.mData, "Error: Incorrect results c", 1e-3, 1e-3);
rocking5566's avatar
rocking5566 committed
143
144
145
146
    }

    return pass ? 0 : 1;
}