padding.cpp 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * \file dnn/test/cuda/padding.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 */

#include "test/common/padding.h"
#include "megdnn/oprs.h"
#include "test/common/checker.h"
#include "test/common/rng.h"
M
Megvii Engine Team 已提交
17
#include "test/cuda/benchmark.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include "test/cuda/fixture.h"

using namespace megdnn;
using namespace test;

TEST_F(CUDA, PADDING) {
    std::vector<padding::TestArg> args = padding::get_args();
    Checker<Padding> checker(handle_cuda());
    UniformIntNonZeroRNG rng(1, 9);
    for (auto&& arg : args) {
        checker.set_param(arg.param)
                .set_rng(0, &rng)
                .set_dtype(0, dtype::Int8())
                .set_dtype(1, dtype::Int8())
                .execs({arg.src, arg.dst});
    }
}

TEST_F(CUDA, PADDING_BACKWARD) {
    std::vector<padding::TestArg> args = padding::get_args_backward();
    Checker<PaddingBackward> checker(handle_cuda());
    UniformFloatRNG rng(1, 9);
    for (auto&& arg : args) {
        checker.set_param(arg.param)
                .set_rng(0, &rng)
                .set_dtype(0, dtype::Float32())
                .set_dtype(1, dtype::Float32())
                .execs({arg.src, arg.dst});
    }
}

TEST_F(CUDA, PADDING_REFLECT) {
    Checker<Padding> checker(handle_cuda(), false);
    param::Padding param;
    param.padding_val = 10;
    param.padding_mode = param::Padding::PaddingMode::REFLECT;
    param.front_offset_dim0 = 2;
    param.front_offset_dim1 = 0;
    param.front_offset_dim2 = 0;
    param.front_offset_dim3 = 0;
    param.front_offset_dim4 = 0;
    param.front_offset_dim5 = 0;
    param.front_offset_dim6 = 0;
    param.back_offset_dim0 = 3;
    param.back_offset_dim1 = 0;
    param.back_offset_dim2 = 0;
    param.back_offset_dim3 = 0;
    param.back_offset_dim4 = 0;
    param.back_offset_dim5 = 0;
    param.back_offset_dim6 = 0;
    checker.set_param(param).exect(
            Testcase{TensorValue({5}, dtype::Int8(), {1, 2, 3, 4, 5}), {}},
M
Megvii Engine Team 已提交
70 71 72
            Testcase{
                    {},
                    TensorValue({10}, dtype::Int8(), {3, 2, 1, 2, 3, 4, 5, 4, 3, 2})});
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
}

TEST_F(CUDA, PADDING_REFLECT2) {
    Checker<Padding> checker(handle_cuda(), false);
    param::Padding param;
    param.padding_val = 10;
    param.padding_mode = param::Padding::PaddingMode::REFLECT;
    param.front_offset_dim0 = 1;
    param.front_offset_dim1 = 2;
    param.front_offset_dim2 = 0;
    param.front_offset_dim3 = 0;
    param.front_offset_dim4 = 0;
    param.front_offset_dim5 = 0;
    param.front_offset_dim6 = 0;
    param.back_offset_dim0 = 1;
    param.back_offset_dim1 = 2;
    param.back_offset_dim2 = 0;
    param.back_offset_dim3 = 0;
    param.back_offset_dim4 = 0;
    param.back_offset_dim5 = 0;
    param.back_offset_dim6 = 0;
    checker.set_param(param).exect(
M
Megvii Engine Team 已提交
95 96 97 98 99 100 101
            Testcase{
                    TensorValue({3, 3}, dtype::Int8(), {3, 5, 1, 3, 6, 1, 4, 7, 9}),
                    {}},
            Testcase{{}, TensorValue({5, 7}, dtype::Int8(), {1, 6, 3, 6, 1, 6, 3, 1, 5,
                                                             3, 5, 1, 5, 3, 1, 6, 3, 6,
                                                             1, 6, 3, 9, 7, 4, 7, 9, 7,
                                                             4, 1, 6, 3, 6, 1, 6, 3})});
102 103
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
TEST_F(CUDA, PADDING_REFLECT2_QUANTIZED) {
    Checker<Padding> checker(handle_cuda(), false);
    param::Padding param;
    param.padding_mode = param::Padding::PaddingMode::REFLECT;
    param.front_offset_dim0 = 2;
    param.front_offset_dim1 = 1;
    param.front_offset_dim2 = 0;
    param.front_offset_dim3 = 0;
    param.front_offset_dim4 = 0;
    param.front_offset_dim5 = 0;
    param.front_offset_dim6 = 0;
    param.back_offset_dim0 = 0;
    param.back_offset_dim1 = 2;
    param.back_offset_dim2 = 0;
    param.back_offset_dim3 = 0;
    param.back_offset_dim4 = 0;
    param.back_offset_dim5 = 0;
    param.back_offset_dim6 = 0;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {3, 3}, dtype::QuantizedS8(), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
                    {}},
            Testcase{{}, TensorValue({5, 6}, dtype::QuantizedS8(), {8, 7, 8, 9, 8, 7, 5,
                                                                    4, 5, 6, 5, 4, 2, 1,
                                                                    2, 3, 2, 1, 5, 4, 5,
                                                                    6, 5, 4, 8, 7, 8, 9,
                                                                    8, 7})});
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
TEST_F(CUDA, PADDING_REPLICATE) {
    Checker<Padding> checker(handle_cuda(), false);
    param::Padding param;
    param.padding_val = 10;
    param.padding_mode = param::Padding::PaddingMode::REPLICATE;
    param.front_offset_dim0 = 1;
    param.front_offset_dim1 = 0;
    param.front_offset_dim2 = 0;
    param.front_offset_dim3 = 0;
    param.front_offset_dim4 = 0;
    param.front_offset_dim5 = 0;
    param.front_offset_dim6 = 0;
    param.back_offset_dim0 = 2;
    param.back_offset_dim1 = 0;
    param.back_offset_dim2 = 0;
    param.back_offset_dim3 = 0;
    param.back_offset_dim4 = 0;
    param.back_offset_dim5 = 0;
    param.back_offset_dim6 = 0;
    checker.set_param(param).exect(
M
Megvii Engine Team 已提交
154 155 156 157 158 159
            Testcase{TensorValue({9}, dtype::Int8(), {1, 2, 3, 4, 5, 6, 7, 8, 9}), {}},
            Testcase{
                    {},
                    TensorValue(
                            {12}, dtype::Int8(),
                            {1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9})});
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}

TEST_F(CUDA, PADDING_REPLICATE2) {
    Checker<Padding> checker(handle_cuda(), false);
    param::Padding param;
    param.padding_val = 10;
    param.padding_mode = param::Padding::PaddingMode::REPLICATE;
    param.front_offset_dim0 = 2;
    param.front_offset_dim1 = 1;
    param.front_offset_dim2 = 0;
    param.front_offset_dim3 = 0;
    param.front_offset_dim4 = 0;
    param.front_offset_dim5 = 0;
    param.front_offset_dim6 = 0;
    param.back_offset_dim0 = 0;
    param.back_offset_dim1 = 3;
    param.back_offset_dim2 = 0;
    param.back_offset_dim3 = 0;
    param.back_offset_dim4 = 0;
    param.back_offset_dim5 = 0;
    param.back_offset_dim6 = 0;
    checker.set_param(param).exect(
M
Megvii Engine Team 已提交
182 183 184 185 186 187 188
            Testcase{
                    TensorValue({3, 3}, dtype::Int8(), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
                    {}},
            Testcase{{}, TensorValue({5, 7}, dtype::Int8(), {1, 1, 2, 3, 3, 3, 3, 1, 1,
                                                             2, 3, 3, 3, 3, 1, 1, 2, 3,
                                                             3, 3, 3, 4, 4, 5, 6, 6, 6,
                                                             6, 7, 7, 8, 9, 9, 9, 9})});
189 190 191 192 193 194 195 196 197 198
}

// #if MEGDNN_WITH_BENCHMARK

TEST_F(CUDA, BENCHMARK_PADDING_CONSTANT) {
    using Param = Padding::Param;

    auto run = [&](const TensorShapeArray& shapes, Param param) {
        CUBenchmarker<PaddingForward> benchmarker(handle_cuda());
        benchmarker.set_param(param);
M
Megvii Engine Team 已提交
199
        benchmarker.set_dtype(0, dtype::Int8()).set_dtype(1, dtype::Int8());
200 201 202

        for (auto&& shape : shapes) {
            double memaccess =
M
Megvii Engine Team 已提交
203
                    double(TensorLayout(shape, dtype::Int8()).span().dist_byte()) *
204 205
                    2e-6;
            auto time_ms = benchmarker.execs({shape, {}});
M
Megvii Engine Team 已提交
206 207
            printf("execute %s, time %.4f ms, %.4f GB/s\n", shape.to_string().c_str(),
                   time_ms, memaccess / time_ms);
208 209 210 211 212 213 214 215 216 217 218 219 220
        }
    };

    printf("mode -> constant; dtype -> int8\n");
    {
        TensorShapeArray shapes = {
                {16, 3, 736, 1280},
        };
        Param param;
        param.padding_mode = param::Padding::PaddingMode::CONSTANT;
        param.front_offset_dim1 = 1;
        run(shapes, param);
    }
M
Megvii Engine Team 已提交
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    printf("mode -> replicate; dtype -> int8\n");
    {
        TensorShapeArray shapes = {
                {16, 3, 736, 1280},
        };
        Param param;
        param.padding_mode = param::Padding::PaddingMode::REPLICATE;
        param.front_offset_dim1 = 1;
        run(shapes, param);
    }
    printf("mode -> reflect; dtype -> int8\n");
    {
        TensorShapeArray shapes = {
                {16, 3, 736, 1280},
        };
        Param param;
        param.padding_mode = param::Padding::PaddingMode::REFLECT;
        param.front_offset_dim1 = 1;
        run(shapes, param);
    }
}

// #endif