|
| 1 | +/******************************************************************************* |
| 2 | + * |
| 3 | + * MIT License |
| 4 | + * |
| 5 | + * Copyright (c) 2023 Advanced Micro Devices, Inc. |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all |
| 15 | + * copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + * SOFTWARE. |
| 24 | + * |
| 25 | + *******************************************************************************/ |
| 26 | +#ifndef GUARD_MIOPEN_ARGMAX_DRIVER_HPP |
| 27 | +#define GUARD_MIOPEN_ARGMAX_DRIVER_HPP |
| 28 | + |
| 29 | +#include "InputFlags.hpp" |
| 30 | +#include "driver.hpp" |
| 31 | +#include "tensor_driver.hpp" |
| 32 | +#include "timer.hpp" |
| 33 | +#include "random.hpp" |
| 34 | +#include <algorithm> |
| 35 | +#include <cfloat> |
| 36 | +#include <cstdlib> |
| 37 | +#include <memory> |
| 38 | +#include <miopen/miopen.h> |
| 39 | +#include <miopen/tensor.hpp> |
| 40 | +#include <numeric> |
| 41 | +#include <vector> |
| 42 | +#include <../test/tensor_holder.hpp> |
| 43 | +#include <../test/verify.hpp> |
| 44 | + |
| 45 | +template <typename Tgpu, typename Tcheck> |
| 46 | +int32_t mloArgmaxForwardRunHost(miopenTensorDescriptor_t inputDesc, |
| 47 | + miopenTensorDescriptor_t outputDesc, |
| 48 | + Tgpu* input, |
| 49 | + int32_t* outputhost, |
| 50 | + int32_t dim) |
| 51 | +{ |
| 52 | + auto input_dims = miopen::deref(inputDesc).GetLengths(); |
| 53 | + auto output_dims = miopen::deref(outputDesc).GetLengths(); |
| 54 | + |
| 55 | + int32_t reduce_size = static_cast<int32_t>(input_dims[dim]); |
| 56 | + auto output_numel = |
| 57 | + std::accumulate(output_dims.begin(), output_dims.end(), 1L, std::multiplies<int64_t>()); |
| 58 | + |
| 59 | + auto inner_size = std::accumulate( |
| 60 | + input_dims.begin() + dim + 1, input_dims.end(), 1ULL, std::multiplies<uint64_t>()); |
| 61 | + |
| 62 | + int32_t ret = 0; |
| 63 | + |
| 64 | + for(size_t o = 0; o < output_numel; o++) |
| 65 | + { |
| 66 | + size_t input_idx = (o / inner_size) * inner_size * reduce_size + o % inner_size; |
| 67 | + |
| 68 | + int32_t max_idx = 0; |
| 69 | + Tcheck max = static_cast<Tcheck>(input[input_idx]); |
| 70 | + |
| 71 | + for(int32_t i = 1; i < reduce_size; i++) |
| 72 | + { |
| 73 | + input_idx += inner_size; |
| 74 | + Tcheck val = static_cast<Tcheck>(input[input_idx]); |
| 75 | + if(max < val) |
| 76 | + { |
| 77 | + max = val; |
| 78 | + max_idx = i; |
| 79 | + } |
| 80 | + } |
| 81 | + outputhost[o] = max_idx; |
| 82 | + } |
| 83 | + return ret; |
| 84 | +} |
| 85 | + |
| 86 | +template <typename Tgpu, typename Tref> |
| 87 | +class ArgmaxDriver : public Driver |
| 88 | +{ |
| 89 | +public: |
| 90 | + ArgmaxDriver() : Driver() |
| 91 | + { |
| 92 | + miopenCreateTensorDescriptor(&inputDesc); |
| 93 | + miopenCreateTensorDescriptor(&outputDesc); |
| 94 | + |
| 95 | + data_type = miopen_type<Tgpu>{}; |
| 96 | + } |
| 97 | + |
| 98 | + int AddCmdLineArgs() override; |
| 99 | + int ParseCmdLineArgs(int argc, char* argv[]) override; |
| 100 | + InputFlags& GetInputFlags() override { return inflags; } |
| 101 | + |
| 102 | + int GetandSetData() override; |
| 103 | + std::vector<int> GetInputTensorLengthsFromCmdLine(); |
| 104 | + |
| 105 | + int AllocateBuffersAndCopy() override; |
| 106 | + |
| 107 | + int RunForwardGPU() override; |
| 108 | + int RunForwardCPU(); |
| 109 | + |
| 110 | + int RunBackwardGPU() override; |
| 111 | + |
| 112 | + int VerifyBackward() override; |
| 113 | + int VerifyForward() override; |
| 114 | + ~ArgmaxDriver() override |
| 115 | + { |
| 116 | + miopenDestroyTensorDescriptor(inputDesc); |
| 117 | + miopenDestroyTensorDescriptor(outputDesc); |
| 118 | + } |
| 119 | + |
| 120 | +private: |
| 121 | + InputFlags inflags; |
| 122 | + |
| 123 | + int forw; |
| 124 | + |
| 125 | + miopenTensorDescriptor_t inputDesc; |
| 126 | + miopenTensorDescriptor_t outputDesc; |
| 127 | + |
| 128 | + std::unique_ptr<GPUMem> in_dev; |
| 129 | + std::unique_ptr<GPUMem> out_dev; |
| 130 | + |
| 131 | + std::vector<Tgpu> in; |
| 132 | + std::vector<int> out; |
| 133 | + std::vector<int> outhost; |
| 134 | + |
| 135 | + int dim; |
| 136 | +}; |
| 137 | + |
| 138 | +template <typename Tgpu, typename Tref> |
| 139 | +int ArgmaxDriver<Tgpu, Tref>::ParseCmdLineArgs(int argc, char* argv[]) |
| 140 | +{ |
| 141 | + inflags.Parse(argc, argv); |
| 142 | + |
| 143 | + if(inflags.GetValueInt("time") == 1) |
| 144 | + { |
| 145 | + miopenEnableProfiling(GetHandle(), true); |
| 146 | + } |
| 147 | + return miopenStatusSuccess; |
| 148 | +} |
| 149 | + |
| 150 | +template <typename Tgpu, typename Tref> |
| 151 | +int ArgmaxDriver<Tgpu, Tref>::GetandSetData() |
| 152 | +{ |
| 153 | + std::vector<int> in_len = GetInputTensorLengthsFromCmdLine(); |
| 154 | + dim = inflags.GetValueInt("DimToReduce"); |
| 155 | + |
| 156 | + SetTensorNd(inputDesc, in_len, data_type); |
| 157 | + |
| 158 | + std::vector<int> out_len; |
| 159 | + |
| 160 | + for(int i = 0; i < in_len.size(); i++) |
| 161 | + { |
| 162 | + if(i != dim) |
| 163 | + { |
| 164 | + out_len.push_back(in_len[i]); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + if(out_len.empty()) |
| 169 | + out_len.push_back(1); |
| 170 | + |
| 171 | + SetTensorNd(outputDesc, out_len, miopenInt32); |
| 172 | + |
| 173 | + return 0; |
| 174 | +} |
| 175 | + |
| 176 | +template <typename Tgpu, typename Tref> |
| 177 | +int ArgmaxDriver<Tgpu, Tref>::AddCmdLineArgs() |
| 178 | +{ |
| 179 | + inflags.AddInputFlag("forw", 'F', "1", "Run only Forward Argmax (Default=1)", "int"); |
| 180 | + inflags.AddInputFlag("batchsize", 'n', "21", "Mini-batch size (Default=100)", "int"); |
| 181 | + inflags.AddInputFlag("in_channels", 'c', "500", "Number of Input Channels (Default=3)", "int"); |
| 182 | + inflags.AddInputFlag("in_d", 'D', "0", "Input Depth (Default=0)", "int"); |
| 183 | + inflags.AddInputFlag("in_h", 'H', "0", "Input Height (Default=32)", "int"); |
| 184 | + inflags.AddInputFlag("in_w", 'W', "375", "Input Width (Default=32)", "int"); |
| 185 | + inflags.AddInputFlag( |
| 186 | + "DimToReduce", 'R', "0", "The indice of the dimensions to be reduced(Default=1)", "int"); |
| 187 | + inflags.AddInputFlag("iter", 'i', "10", "Number of Iterations (Default=10)", "int"); |
| 188 | + inflags.AddInputFlag("verify", 'V', "1", "Verify Each Layer (Default=1)", "int"); |
| 189 | + inflags.AddInputFlag("time", 't', "0", "Time Each Layer (Default=0)", "int"); |
| 190 | + inflags.AddInputFlag( |
| 191 | + "wall", 'w', "0", "Wall-clock Time Each Layer, Requires time == 1 (Default=0)", "int"); |
| 192 | + |
| 193 | + return miopenStatusSuccess; |
| 194 | +} |
| 195 | + |
| 196 | +template <typename Tgpu, typename Tref> |
| 197 | +std::vector<int> ArgmaxDriver<Tgpu, Tref>::GetInputTensorLengthsFromCmdLine() |
| 198 | +{ |
| 199 | + int in_n = inflags.GetValueInt("batchsize"); |
| 200 | + int in_c = inflags.GetValueInt("in_channels"); |
| 201 | + int in_w = inflags.GetValueInt("in_w"); |
| 202 | + int in_h = inflags.GetValueInt("in_h"); |
| 203 | + int in_d = inflags.GetValueInt("in_d"); |
| 204 | + |
| 205 | + if((in_n != 0) && (in_c != 0) && (in_d != 0) && (in_h != 0) && (in_w != 0)) |
| 206 | + { |
| 207 | + return std::vector<int>({in_n, in_c, in_d, in_h, in_w}); |
| 208 | + } |
| 209 | + else if((in_n != 0) && (in_c != 0) && (in_h != 0) && (in_w != 0)) |
| 210 | + { |
| 211 | + return std::vector<int>({in_n, in_c, in_h, in_w}); |
| 212 | + } |
| 213 | + else if((in_n != 0) && (in_c != 0) && (in_w != 0)) |
| 214 | + { |
| 215 | + return std::vector<int>({in_n, in_c, in_w}); |
| 216 | + } |
| 217 | + else if((in_n != 0) && (in_w != 0)) |
| 218 | + { |
| 219 | + return std::vector<int>({in_n, in_w}); |
| 220 | + } |
| 221 | + else if(in_n != 0) |
| 222 | + { |
| 223 | + return std::vector<int>({in_n}); |
| 224 | + } |
| 225 | + else |
| 226 | + { |
| 227 | + std::cerr << "Error Input Tensor Lengths\n" << std::endl; |
| 228 | + return std::vector<int>({0}); |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +template <typename Tgpu, typename Tref> |
| 233 | +int ArgmaxDriver<Tgpu, Tref>::AllocateBuffersAndCopy() |
| 234 | +{ |
| 235 | + size_t in_sz = GetTensorSize(inputDesc); |
| 236 | + size_t out_sz = GetTensorSize(outputDesc); |
| 237 | + |
| 238 | + uint32_t ctx = 0; |
| 239 | + |
| 240 | + in_dev = std::unique_ptr<GPUMem>(new GPUMem(ctx, in_sz, sizeof(Tgpu))); |
| 241 | + out_dev = std::unique_ptr<GPUMem>(new GPUMem(ctx, out_sz, sizeof(int))); |
| 242 | + |
| 243 | + in = std::vector<Tgpu>(in_sz, static_cast<Tgpu>(0)); |
| 244 | + out = std::vector<int>(out_sz, static_cast<int>(0)); |
| 245 | + outhost = std::vector<int>(out_sz, static_cast<int>(0)); |
| 246 | + |
| 247 | + for(int i = 0; i < in_sz; i++) |
| 248 | + { |
| 249 | + in[i] = prng::gen_A_to_B<Tgpu>(static_cast<Tgpu>(0.0), static_cast<Tgpu>(1.0)); |
| 250 | + } |
| 251 | + |
| 252 | + if(in_dev->ToGPU(GetStream(), in.data()) != 0) |
| 253 | + std::cerr << "Error copying (in) to GPU, size: " << in_dev->GetSize() << std::endl; |
| 254 | + |
| 255 | + if(out_dev->ToGPU(GetStream(), out.data()) != 0) |
| 256 | + std::cerr << "Error copying (out) to GPU, size: " << out_dev->GetSize() << std::endl; |
| 257 | + |
| 258 | + return miopenStatusSuccess; |
| 259 | +} |
| 260 | + |
| 261 | +template <typename Tgpu, typename Tref> |
| 262 | +int ArgmaxDriver<Tgpu, Tref>::RunForwardGPU() |
| 263 | +{ |
| 264 | + float kernel_total_time = 0; |
| 265 | + float kernel_first_time = 0; |
| 266 | + |
| 267 | + Timer t; |
| 268 | + START_TIME |
| 269 | + |
| 270 | + for(int i = 0; i < inflags.GetValueInt("iter"); i++) |
| 271 | + { |
| 272 | + miopenArgmaxForward( |
| 273 | + GetHandle(), inputDesc, in_dev->GetMem(), dim, outputDesc, out_dev->GetMem()); |
| 274 | + |
| 275 | + float time = 0; |
| 276 | + miopenGetKernelTime(GetHandle(), &time); |
| 277 | + kernel_total_time += time; |
| 278 | + if(i == 0) |
| 279 | + kernel_first_time = time; |
| 280 | + } |
| 281 | + |
| 282 | + if(inflags.GetValueInt("time") == 1) |
| 283 | + { |
| 284 | + STOP_TIME |
| 285 | + int iter = inflags.GetValueInt("iter"); |
| 286 | + if(WALL_CLOCK) |
| 287 | + std::cout << "Wall-clock Time Forward Argmax Elapsed: " << t.gettime_ms() / iter |
| 288 | + << " ms\n"; |
| 289 | + |
| 290 | + float kernel_average_time = |
| 291 | + iter > 1 ? (kernel_total_time - kernel_first_time) / (iter - 1) : kernel_first_time; |
| 292 | + std::cout << "GPU Kernel Time Forward Argmax Elapsed: " << kernel_average_time << " ms\n"; |
| 293 | + } |
| 294 | + |
| 295 | + if(out_dev->FromGPU(GetStream(), out.data()) != 0) |
| 296 | + std::cerr << "Error copying (out_dev) from GPU, size: " << out_dev->GetSize() << std::endl; |
| 297 | + |
| 298 | + return miopenStatusSuccess; |
| 299 | +} |
| 300 | + |
| 301 | +template <typename Tgpu, typename Tref> |
| 302 | +int ArgmaxDriver<Tgpu, Tref>::RunForwardCPU() |
| 303 | +{ |
| 304 | + mloArgmaxForwardRunHost<Tgpu, Tref>(inputDesc, outputDesc, in.data(), outhost.data(), dim); |
| 305 | + |
| 306 | + return miopenStatusSuccess; |
| 307 | +} |
| 308 | + |
| 309 | +template <typename Tgpu, typename Tref> |
| 310 | +int ArgmaxDriver<Tgpu, Tref>::RunBackwardGPU() |
| 311 | +{ |
| 312 | + return miopenStatusSuccess; |
| 313 | +} |
| 314 | + |
| 315 | +template <typename Tgpu, typename Tref> |
| 316 | +int ArgmaxDriver<Tgpu, Tref>::VerifyForward() |
| 317 | +{ |
| 318 | + RunForwardCPU(); |
| 319 | + auto error = miopen::rms_range(outhost, out); |
| 320 | + |
| 321 | + if(!std::isfinite(error) || std::abs(static_cast<float>(error)) != 0.0f) |
| 322 | + { |
| 323 | + std::cout << "Forward Argmax FAILED: Result does not equal" << std::endl; |
| 324 | + return EC_VerifyFwd; |
| 325 | + } |
| 326 | + else |
| 327 | + { |
| 328 | + std::cout << "Forward Argmax Verifies on CPU and GPU (err=" << error << ")\n"; |
| 329 | + } |
| 330 | + |
| 331 | + return miopenStatusSuccess; |
| 332 | +} |
| 333 | + |
| 334 | +template <typename Tgpu, typename Tref> |
| 335 | +int ArgmaxDriver<Tgpu, Tref>::VerifyBackward() |
| 336 | +{ |
| 337 | + return miopenStatusSuccess; |
| 338 | +} |
| 339 | + |
| 340 | +#endif // GUARD_MIOPEN_ARGMAX_DRIVER_HPP |
0 commit comments