|
| 1 | +// Copyright (c) Advanced Micro Devices, Inc., or its affiliates. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +// |
| 4 | +// Numerical (on-device execute + verify) coverage for large 2D grouped |
| 5 | +// convolutions in NHWC layout. Shapes span three regimes: (a) sub-INT_MAX |
| 6 | +// element counts, (b) tensors above 2 GB whose element count still fits int32, |
| 7 | +// and (c) tensors whose element count exceeds INT_MAX (needs the CK |
| 8 | +// large-tensor instances). All geometries are 3x3 with SAME padding. |
| 9 | +// Complements the API probe in conv_api_solution_count_large2d.cpp; catches any |
| 10 | +// silent int32 offset overflow on the regular (non-large-tensor) CK path. |
| 11 | +// |
| 12 | +// Excluded from the default gtest filter (test/gtest/CMakeLists.txt) as |
| 13 | +// multi-GB; run explicitly. A case skips when the solver is inapplicable or the |
| 14 | +// device lacks memory. |
| 15 | + |
| 16 | +#include <algorithm> |
| 17 | +#include <cstddef> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include "unit_conv_solver.hpp" |
| 21 | +#include "get_handle.hpp" |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +using miopen::unit_tests::ConvolutionDescriptorParams; |
| 26 | +using miopen::unit_tests::ConvTestCase; |
| 27 | +using miopen::unit_tests::TensorDescriptorParams; |
| 28 | + |
| 29 | +std::vector<ConvTestCase> GetTestCases(miopenDataType_t dt) |
| 30 | +{ |
| 31 | + const auto L = miopenTensorNHWC; |
| 32 | + // Lengths are in logical [N, C, H, W] order; L selects NHWC memory layout. |
| 33 | + // Filters are [K, C, 3, 3], SAME padding. Element counts annotated per case. |
| 34 | + return { |
| 35 | + // ---- sub-INT_MAX elements, < 2 GB ---- |
| 36 | + {TensorDescriptorParams{dt, L, {48, 2048, 64, 64}}, |
| 37 | + TensorDescriptorParams{dt, L, {2048, 2048, 3, 3}}, |
| 38 | + dt, |
| 39 | + ConvolutionDescriptorParams{{1, 1}, {1, 1}, {1, 1}, 1}}, // 4.0e8 elem |
| 40 | + // ---- > 2 GB bytes, element count still fits int32 ---- |
| 41 | + {TensorDescriptorParams{dt, L, {96, 1024, 112, 112}}, |
| 42 | + TensorDescriptorParams{dt, L, {1024, 1024, 3, 3}}, |
| 43 | + dt, |
| 44 | + ConvolutionDescriptorParams{{1, 1}, {1, 1}, {1, 1}, 1}}, // 1.23e9 elem |
| 45 | + {TensorDescriptorParams{dt, L, {96, 2048, 64, 64}}, |
| 46 | + TensorDescriptorParams{dt, L, {4096, 2048, 3, 3}}, |
| 47 | + dt, |
| 48 | + ConvolutionDescriptorParams{{1, 1}, {1, 1}, {1, 1}, 1}}, // 1.61e9 elem, K=2C |
| 49 | + // ---- element count > INT_MAX (needs CK large-tensor instances) ---- |
| 50 | + {TensorDescriptorParams{dt, L, {160, 1024, 140, 100}}, |
| 51 | + TensorDescriptorParams{dt, L, {1024, 1024, 3, 3}}, |
| 52 | + dt, |
| 53 | + ConvolutionDescriptorParams{{1, 1}, {1, 1}, {1, 1}, 1}}, // 2.29e9 elem |
| 54 | + {TensorDescriptorParams{dt, L, {160, 1024, 100, 140}}, |
| 55 | + TensorDescriptorParams{dt, L, {1024, 1024, 3, 3}}, |
| 56 | + dt, |
| 57 | + ConvolutionDescriptorParams{{1, 1}, {1, 1}, {1, 1}, 1}}, // 2.29e9 elem, H/W swap |
| 58 | + {TensorDescriptorParams{dt, L, {200, 512, 200, 200}}, |
| 59 | + TensorDescriptorParams{dt, L, {512, 512, 3, 3}}, |
| 60 | + dt, |
| 61 | + ConvolutionDescriptorParams{{1, 1}, {1, 1}, {1, 1}, 1}}, // 4.10e9 elem, large spatial |
| 62 | + {TensorDescriptorParams{dt, L, {160, 1024, 120, 120}}, |
| 63 | + TensorDescriptorParams{dt, L, {2048, 1024, 3, 3}}, |
| 64 | + dt, |
| 65 | + ConvolutionDescriptorParams{{1, 1}, {1, 1}, {1, 1}, 1}}, // 4.72e9 elem, K=2C |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +const miopen::unit_tests::UnitTestConvSolverParams& GetTestParams() |
| 70 | +{ |
| 71 | + static const auto params = [] { |
| 72 | +#if MIOPEN_BACKEND_HIP && MIOPEN_USE_COMPOSABLEKERNEL |
| 73 | + Gpu supportedDevices = Gpu::gfx90A | Gpu::gfx94X | Gpu::gfx950; |
| 74 | +#else |
| 75 | + Gpu supportedDevices = Gpu::None; |
| 76 | +#endif |
| 77 | + auto p = miopen::unit_tests::UnitTestConvSolverParams(supportedDevices); |
| 78 | + p.Tunable(5); |
| 79 | + return p; |
| 80 | + }(); |
| 81 | + return params; |
| 82 | +} |
| 83 | + |
| 84 | +enum class Dir |
| 85 | +{ |
| 86 | + Fwd, |
| 87 | + Bwd, |
| 88 | + Wrw |
| 89 | +}; |
| 90 | + |
| 91 | +struct RunGate |
| 92 | +{ |
| 93 | + std::size_t required; |
| 94 | + std::size_t available; |
| 95 | + bool applicable; |
| 96 | +}; |
| 97 | + |
| 98 | +RunGate GateFor(const ConvTestCase& tc, |
| 99 | + const miopen::solver::conv::ConvSolverInterface& solver, |
| 100 | + Dir dir) |
| 101 | +{ |
| 102 | + auto&& handle = get_handle(); |
| 103 | + |
| 104 | + const auto x_desc = tc.GetXTensorDescriptor(); |
| 105 | + const auto w_desc = tc.GetWTensorDescriptor(); |
| 106 | + const auto conv_desc = tc.GetConv(); |
| 107 | + const auto y_desc = conv_desc.GetForwardOutputTensor(x_desc, w_desc, tc.GetYDataType()); |
| 108 | + |
| 109 | + const auto problem = [&] { |
| 110 | + switch(dir) |
| 111 | + { |
| 112 | + case Dir::Fwd: |
| 113 | + return miopen::conv::ProblemDescription( |
| 114 | + x_desc, w_desc, y_desc, conv_desc, miopen::conv::Direction::Forward); |
| 115 | + case Dir::Bwd: |
| 116 | + return miopen::conv::ProblemDescription( |
| 117 | + y_desc, w_desc, x_desc, conv_desc, miopen::conv::Direction::BackwardData); |
| 118 | + default: |
| 119 | + return miopen::conv::ProblemDescription( |
| 120 | + y_desc, w_desc, x_desc, conv_desc, miopen::conv::Direction::BackwardWeights); |
| 121 | + } |
| 122 | + }(); |
| 123 | + |
| 124 | + auto ctx = miopen::ExecutionContext{&handle}; |
| 125 | + problem.SetupFloats(ctx); |
| 126 | + |
| 127 | + if(!solver.IsApplicable(ctx, problem)) |
| 128 | + return {0, 0, false}; |
| 129 | + |
| 130 | + const std::size_t ws_size = |
| 131 | + solver.MayNeedWorkspace() ? solver.GetWorkspaceSize(ctx, problem) : 0; |
| 132 | + const std::size_t x_bytes = x_desc.GetNumBytes(); |
| 133 | + const std::size_t y_bytes = y_desc.GetNumBytes(); |
| 134 | + const std::size_t w_bytes = w_desc.GetNumBytes(); |
| 135 | + const std::size_t h_bytes = std::max(x_bytes, y_bytes); |
| 136 | + |
| 137 | + const std::size_t raw_mem = ws_size + x_bytes + y_bytes + w_bytes + 4 * h_bytes; |
| 138 | + const std::size_t headroom = std::max<std::size_t>(1ULL << 30, raw_mem / 10); |
| 139 | + const std::size_t required_mem = raw_mem + headroom; |
| 140 | + const std::size_t device_mem = handle.GetGlobalMemorySize(); |
| 141 | + |
| 142 | + return {required_mem, device_mem, true}; |
| 143 | +} |
| 144 | + |
| 145 | +} // namespace |
| 146 | + |
| 147 | +#define SKIP_IF_NOT_RUNNABLE(solver_expr, dir) \ |
| 148 | + do \ |
| 149 | + { \ |
| 150 | + miopen::unit_tests::UnitTestConvSolverParams _params; \ |
| 151 | + miopenConvAlgorithm_t _algo; \ |
| 152 | + ConvTestCase _tc; \ |
| 153 | + std::tie(_params, _algo, _tc) = this->GetParam(); \ |
| 154 | + const auto _g = GateFor(_tc, (solver_expr), (dir)); \ |
| 155 | + if(!_g.applicable) \ |
| 156 | + { \ |
| 157 | + GTEST_SKIP() << "solver not applicable to this shape"; \ |
| 158 | + } \ |
| 159 | + if(_g.available < _g.required) \ |
| 160 | + { \ |
| 161 | + GTEST_SKIP() << "Insufficient device memory: need " << _g.required \ |
| 162 | + << " bytes, device has " << _g.available; \ |
| 163 | + } \ |
| 164 | + } while(0) |
| 165 | + |
| 166 | +using GPU_UnitTestConvSolverImplicitGemmGroupFwdXdlops_Large2D_FP16 = |
| 167 | + GPU_UnitTestConvSolverFwd_FP16; |
| 168 | +using GPU_UnitTestConvSolverImplicitGemmGroupBwdXdlops_Large2D_FP16 = |
| 169 | + GPU_UnitTestConvSolverBwd_FP16; |
| 170 | +using GPU_UnitTestConvSolverImplicitGemmGroupWrwXdlops_Large2D_FP16 = |
| 171 | + GPU_UnitTestConvSolverWrw_FP16; |
| 172 | + |
| 173 | +TEST_P(GPU_UnitTestConvSolverImplicitGemmGroupFwdXdlops_Large2D_FP16, |
| 174 | + ConvHipImplicitGemmGroupFwdXdlops) |
| 175 | +{ |
| 176 | + const auto solver = miopen::solver::conv::ConvHipImplicitGemmGroupFwdXdlops{}; |
| 177 | + SKIP_IF_NOT_RUNNABLE(solver, Dir::Fwd); |
| 178 | + this->RunTest(solver); |
| 179 | +}; |
| 180 | + |
| 181 | +TEST_P(GPU_UnitTestConvSolverImplicitGemmGroupBwdXdlops_Large2D_FP16, |
| 182 | + ConvHipImplicitGemmGroupBwdXdlops) |
| 183 | +{ |
| 184 | + const auto solver = miopen::solver::conv::ConvHipImplicitGemmGroupBwdXdlops{}; |
| 185 | + SKIP_IF_NOT_RUNNABLE(solver, Dir::Bwd); |
| 186 | + this->RunTest(solver); |
| 187 | +}; |
| 188 | + |
| 189 | +TEST_P(GPU_UnitTestConvSolverImplicitGemmGroupWrwXdlops_Large2D_FP16, |
| 190 | + ConvHipImplicitGemmGroupWrwXdlops) |
| 191 | +{ |
| 192 | + const auto solver = miopen::solver::conv::ConvHipImplicitGemmGroupWrwXdlops{}; |
| 193 | + SKIP_IF_NOT_RUNNABLE(solver, Dir::Wrw); |
| 194 | + this->RunTest(solver); |
| 195 | +}; |
| 196 | + |
| 197 | +INSTANTIATE_TEST_SUITE_P(Large2D, |
| 198 | + GPU_UnitTestConvSolverImplicitGemmGroupFwdXdlops_Large2D_FP16, |
| 199 | + testing::Combine(testing::Values(GetTestParams()), |
| 200 | + testing::Values(miopenConvolutionAlgoImplicitGEMM), |
| 201 | + testing::ValuesIn(GetTestCases(miopenHalf)))); |
| 202 | + |
| 203 | +INSTANTIATE_TEST_SUITE_P(Large2D, |
| 204 | + GPU_UnitTestConvSolverImplicitGemmGroupBwdXdlops_Large2D_FP16, |
| 205 | + testing::Combine(testing::Values(GetTestParams()), |
| 206 | + testing::Values(miopenConvolutionAlgoImplicitGEMM), |
| 207 | + testing::ValuesIn(GetTestCases(miopenHalf)))); |
| 208 | + |
| 209 | +INSTANTIATE_TEST_SUITE_P(Large2D, |
| 210 | + GPU_UnitTestConvSolverImplicitGemmGroupWrwXdlops_Large2D_FP16, |
| 211 | + testing::Combine(testing::Values(GetTestParams()), |
| 212 | + testing::Values(miopenConvolutionAlgoImplicitGEMM), |
| 213 | + testing::ValuesIn(GetTestCases(miopenHalf)))); |
0 commit comments