roi_align.cpp 3.0 KB
Newer Older
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 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/**
 * \file dnn/test/cuda/roi_align.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 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/cuda/fixture.h"

#include "test/common/checker.h"
#include "test/common/roi_pooling.h"

namespace megdnn {
namespace test {

TEST_F(CUDA, ROI_ALIGN_FORWARD) {
    size_t N = 10, C = 3, IH = 102, IW = 108;
    size_t OH = 12, OW = 13, M = 7;
    ROIPoolingRNG rng(N);
    ConstValue const_0{0};
    ConsecutiveRNG consecutive_rng{0.f, 1.f / (N * C * IH * IW * 1.f)};
    using Param = ROIAlign::Param;
    Param param;
    param.spatial_scale = 100;
    param.offset = 0.0;
    param.pooled_height = OH;
    param.pooled_width = OW;
    param.sample_height = 16;
    param.sample_width = 16;
    Checker<ROIAlignForward> checker(handle_cuda());
    auto run = [&](DType dtype) {
        for (auto mode : {Param::Mode::MAX, Param::Mode::AVERAGE}) {
            param.mode = mode;
            if (mode == Param::Mode::MAX) {
                checker.set_rng(0, &consecutive_rng);
            }
            checker.set_param(param)
                    .set_rng(1, &rng)
                    .set_dtype(0, dtype)
                    .set_dtype(1, dtype)
                    .set_dtype(2, dtype)
                    .set_dtype(3, dtype::Int32())
                    .execs({{N, C, IH, IW}, {M, 5}, {}, {}});
        }
    };
    run(dtype::Float32());
    run(dtype::Float16());
}

TEST_F(CUDA, ROI_ALIGN_BACKWARD) {
    size_t N = 10, C = 3, IH = 102, IW = 108;
    size_t OH = 12, OW = 13, M = 7;
    ROIPoolingRNG rng(N);
    ConstValue const_0{0};
    using Param = ROIAlign::Param;
    Param param;
    param.spatial_scale = 100;
    param.offset = 0.0;
    param.pooled_height = OH;
    param.pooled_width = OW;
    param.sample_height = 7;
    param.sample_width = 7;
    UniformIntRNG index_rng(0, param.sample_height * param.sample_width - 1);
    Checker<ROIAlignBackward> checker(handle_cuda());
    checker.set_epsilon(1e-2);
    auto run = [&](DType dtype) {
        for (auto mode : {Param::Mode::MAX, Param::Mode::AVERAGE}) {
            param.mode = mode;
            checker.set_param(param)
                    .set_dtype(0, dtype)
                    .set_dtype(1, dtype)
                    .set_dtype(3, dtype)
                    .set_dtype(2, dtype::Int32())
                    .set_rng(1, &rng)
                    .set_rng(2, &index_rng)
                    .set_rng(3, &const_0)
                    .execs({{M, C, OH, OW},
                            {M, 5},
                            {M, C, OH, OW},
                            {N, C, IH, IW}});
        }
    };
    run(dtype::Float32());
    run(dtype::Float16());
88 89
    checker.set_epsilon(5e-2);
    run(dtype::BFloat16());
90 91 92 93 94 95 96
}

}  // namespace test
}  // namespace megdnn

// vim: syntax=cpp.doxygen