norm.cpp 8.3 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#include "test/common/norm.h"
#include "megdnn/dtype.h"
#include "megdnn/oprs.h"
#include "test/common/benchmarker.h"
#include "test/common/checker.h"
#include "test/naive/fixture.h"

namespace megdnn {
namespace test {
TEST_F(NAIVE, L2NORM_FP32_DIM0) {
    Checker<Norm> checker(handle(), false);
    Norm::Param param;
    param.p = 2;
    param.dim = 0;
    checker.set_param(param);
    checker.exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float32(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float32(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
            });
}
TEST_F(NAIVE, L2NORM_FP32_DIM1) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    param.p = 2;
    param.dim = 1;
    checker.set_param(param);
    checker.exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float32(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue(
                            {1, 1, 3, 4}, dtype::Float32(),
                            {12.000, 13.0384, 14.1421, 15.2971, 16.4924, 17.7200,
                             18.9737, 20.2485, 21.5407, 22.8473, 24.1661, 25.4951}),
            });
}
TEST_F(NAIVE, L2NORM_FP32_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    param.p = 2;
    param.dim = 3;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float32(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue(
                            {1, 2, 3, 1}, dtype::Float32(),
                            {3.7417, 11.2250, 19.1311, 27.0924, 35.0714, 43.0581})});
}
// l2, fp16
TEST_F(NAIVE, L2NORM_FP16_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    param.p = 2;
    param.dim = 3;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float16(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue(
                            {1, 2, 3, 1}, dtype::Float16(),
                            {3.7422, 11.2266, 19.1250, 27.0938, 35.0625, 43.0625})});
}
// l1, fp32,fp16
TEST_F(NAIVE, L1NORM_FP32_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    param.p = 1;
    param.dim = 3;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float32(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue(
                            {1, 2, 3, 1}, dtype::Float32(), {6, 22, 38, 54, 70, 86}),
            });
}
TEST_F(NAIVE, L1NORM_FP16_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    param.p = 1;
    param.dim = 3;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float16(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue(
                            {1, 2, 3, 1}, dtype::Float16(), {6, 22, 38, 54, 70, 86}),
            });
}
// l0, fp32,fp16
TEST_F(NAIVE, L0NORM_FP32_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    param.p = 0;
    param.dim = 3;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float32(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue({1, 2, 3, 1}, dtype::Float32(), {3, 4, 4, 4, 4, 4}),
            });
}
TEST_F(NAIVE, L0NORM_FP16_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    param.p = 0;
    param.dim = 3;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float16(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue({1, 2, 3, 1}, dtype::Float16(), {3, 4, 4, 4, 4, 4}),
            });
}
// inf
TEST_F(NAIVE, INF_NORM_FP32_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    using Mode = Norm::Param::Mode;

    param.dim = 3;
    param.mode = Mode::INF_NORM;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float32(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue({1, 2, 3, 1}, dtype::Float32(), {3, 7, 11, 15, 19, 23}),
            });
}
TEST_F(NAIVE, INF_NORM_FP16_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    using Mode = Norm::Param::Mode;

    param.dim = 3;
    param.mode = Mode::INF_NORM;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float16(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue({1, 2, 3, 1}, dtype::Float16(), {3, 7, 11, 15, 19, 23}),
            });
}
// -inf
TEST_F(NAIVE, NEG_INF_NORM_FP32_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    param.mode = Norm::Param::Mode::NEG_INF_NORM;
    param.dim = 3;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float32(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue({1, 2, 3, 1}, dtype::Float32(), {0, 4, 8, 12, 16, 20}),
            });
}
TEST_F(NAIVE, NEG_INF_NORM_FP16_DIM3) {
    Checker<Norm> checker(handle());
    Norm::Param param;
    param.mode = Norm::Param::Mode::NEG_INF_NORM;
    param.dim = 3;
    checker.set_param(param).exect(
            Testcase{
                    TensorValue(
                            {1, 2, 3, 4}, dtype::Float16(),
                            {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11,
                             12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
                    {}},
            Testcase{
                    {},
                    TensorValue({1, 2, 3, 1}, dtype::Float16(), {0, 4, 8, 12, 16, 20}),
            });
}

}  // namespace test
}  // namespace megdnn