relayout.cpp 6.1 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/**
 * \file dnn/test/common/relayout.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 "megdnn/oprs/general.h"

#include "src/common/relayout_helper.h"
#include "test/common/benchmarker.h"
#include "test/common/relayout.h"
#include "test/common/checker.h"

using namespace megdnn;
using namespace test;
using namespace megdnn::relayout;
using namespace test::relayout;

namespace {
TestArg generate_transpose_args(size_t batch, size_t m, size_t n,
                                       size_t c, DType dtype) {
    TestArg arg;
    arg.src = TensorLayout(
        TensorShape{batch, n, m, c},
        {static_cast<std::ptrdiff_t>(n * m * c), static_cast<std::ptrdiff_t>(c),
         static_cast<std::ptrdiff_t>(n * c), 1},
        dtype);
    arg.dst = TensorLayout(TensorShape{batch, n, m, c}, dtype);
    return arg;
}
} // anonymous namespace

namespace megdnn {
namespace test {
namespace relayout {

#define DEF_TEST(name) \
template<> \
void run_test<name>(Handle *handle)

DEF_TEST(cv) {
    std::vector<TestArg> args;

    for (size_t M = 124; M <= 130; ++M) {
        for (size_t N = 124; N <= 130; ++N) {
            for (size_t CH : {1, 3, 5}) {
                args.push_back(
                    generate_transpose_args(1, M, N, CH, dtype::Uint8()));
                args.push_back(
                    generate_transpose_args(1, M, N, CH, dtype::Int32()));
                args.push_back(
                    generate_transpose_args(1, M, N, CH, dtype::Float32()));
                args.push_back(
                    generate_transpose_args(3, M, N, CH, dtype::Float32()));
            }
        }
    }

    Checker<Relayout> checker(handle);

    for (auto &&arg : args) {
        checker.execl({arg.src, arg.dst});
    }
}


DEF_TEST(broadcast) {
    std::vector<TestArg> args;
    TensorLayout src{{2, 3, 4}, dtype::Float32()},
                 dst{{2, 3, 4}, dtype::Float32()};

    src.stride[0] = 4;
    src.stride[1] = 0;
    args.emplace_back(src, dst);

    // last stride contiguous
    args.emplace_back(TensorLayout({3, 100, 2}, {2, 0, 1}, dtype::Float16()),
                      TensorLayout({3, 100, 2}, {200, 2, 1}, dtype::Float16()));
    Checker<Relayout> checker(handle);

    for (auto &&arg : args) {
        checker.execl({arg.src, arg.dst});
    }
}

DEF_TEST(negative) {
    TensorLayout src{{7, 8, 10}, dtype::Float32()},
                 dst{{7, 8, 10}, dtype::Float32()};

    src.stride[0] *= -1;

    Checker<Relayout> checker(handle);
    checker.execl({src, dst});
}

DEF_TEST(transpose) {
    Checker<Relayout> checker(handle);
    {
        TensorLayout sl({8, 10}, dtype::Int32()), dl({10, 8}, dtype::Int32());
        sl = sl.dimshuffle({1, 0});
        checker.execl({sl, dl});
        checker.execl({dl, sl});
    }
    {
        TensorLayout sl({8, 10, 2}, dtype::Int32()),
                     dl({2, 8, 10}, dtype::Int32());
        sl = sl.dimshuffle({2, 0, 1});
        checker.execl({sl, dl});
        checker.execl({dl, sl});
    }
}

#undef DEF_TEST

} // namespace relayout
} // namespace test
} // namespace megdnn

void test::relayout::run_cv_benchmark(Handle* handle) {
    auto handle_naive = create_cpu_handle(2);
    std::vector<TestArg> args;

    args.push_back(generate_transpose_args(1, 255, 256, 1, dtype::Int32()));
    args.push_back(generate_transpose_args(1, 513, 1025, 3, dtype::Int32()));

    args.push_back(generate_transpose_args(1, 255, 256, 1, dtype::Uint8()));
    args.push_back(generate_transpose_args(1, 513, 1025, 3, dtype::Uint8()));

    args.push_back(generate_transpose_args(1, 255, 256, 3, dtype::Float32()));
    args.push_back(generate_transpose_args(1, 513, 1025, 1, dtype::Float32()));

    args.push_back(generate_transpose_args(2, 987, 573, 6, dtype::Float32()));

    Benchmarker<Relayout> benchmarker(handle);
    Benchmarker<Relayout> benchmarker_naive(handle_naive.get());

    Checker<Relayout> checker(handle);
    benchmarker_naive.set_times(1).set_display(false);
    benchmarker.set_times(1).set_display(false);
    for (auto&& arg : args) {
        checker.execl({arg.src, arg.dst});
        auto t0 = benchmarker.execl({arg.src, arg.dst});
        auto t1 = benchmarker_naive.execl({arg.src, arg.dst});
        double k = arg.dst.span().dist_byte() * 1e3 / (1024 * 1024 * 1024);
        printf("cur=%7.3fms,%5.2fGiB/s naive=%7.3fms,%5.2fGiB/s %s %s\n", t0,
               k / t0, t1, k / t1, arg.dst.TensorShape::to_string().c_str(),
               arg.dst.dtype.name());
    }
}
TEST(RELAYOUT, TRANSPOSE_DET) {
    auto run = [](const TensorShape& shape,
                  const std::vector<size_t>& dimshuffle,
                  bool expect_is_transpose, const TransposeParam& p = {}) {
        TensorLayout src{shape, dtype::Float32{}};
        src = src.dimshuffle(dimshuffle).collapse_contiguous();
        TensorLayout dst{TensorShape{src.total_nr_elems()}, src.dtype};
        TransposeParam p_get;
        bool succ = is_transpose(src, dst, p_get);
        ASSERT_EQ(expect_is_transpose, succ);
        if (succ) {
            ASSERT_EQ(p_get.batch, p.batch);
            ASSERT_EQ(p_get.m, p.m);
            ASSERT_EQ(p_get.n, p.n);
            ASSERT_EQ(p_get.c, p.c);
        }
        // swap m, n
        succ = is_transpose(dst, src, p_get);
        ASSERT_EQ(expect_is_transpose, succ);
        if (succ) {
            ASSERT_EQ(p_get.batch, p.batch);
            ASSERT_EQ(p_get.m, p.n);
            ASSERT_EQ(p_get.n, p.m);
            ASSERT_EQ(p_get.c, p.c);
        }
    };
    run({2, 3}, {1, 0}, true, {1, 2, 3, 1});
    run({2, 3, 5}, {1, 0, 2}, true, {1, 2, 3, 5});
    run({2, 3, 5}, {0, 2, 1}, true, {2, 3, 5, 1});
    run({3, 2, 3, 5}, {0, 2, 1, 3}, true, {3, 2, 3, 5});
    run({3, 2, 3, 5}, {0, 1, 3, 2}, true, {6, 3, 5, 1});
    run({2, 3, 5}, {2, 1, 0}, false);
    run({3, 2, 3, 5}, {3, 2, 1, 0}, false);
}
// vim: syntax=cpp.doxygen