// // MatrixBandTest.cpp // MNNTests // // Created by MNN on 2019/09/17. // Copyright © 2018, Alibaba Group Holding Limited // /* Test Case From https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/matrix-band-part */ #include #include "MNNTestSuite.h" #include "MNN_generated.h" using namespace MNN::Express; class MatrixBandTest : public MNNTestCase { public: virtual bool run(int precision) { std::unique_ptr MatrixBandOp(new MNN::OpT); MatrixBandOp->type = MNN::OpType_MatrixBandPart; auto matrix = _Input({4, 4}, NHWC, halide_type_of()); auto lower = _Input({}, NHWC, halide_type_of()); auto upper = _Input({}, NHWC, halide_type_of()); auto y = Variable::create(Expr::create(MatrixBandOp.get(), {matrix, lower, upper})); std::vector values = {0.0f, 1.0f, 2.0f, 3.0f, -1.0f, 0.0f, 1.0f, 2.0f, -2.0f, -1.0f, 0.0f, 1.0f, -3.0f, -2.0f, -1.0f, 0.0f}; ::memcpy(matrix->writeMap(), values.data(), values.size() * sizeof(float)); { lower->writeMap()[0] = 1; upper->writeMap()[0] = -1; { auto yPtr = y->readMap(); for (int h = 0; h < 4; ++h) { for (int w = 0; w < 4; ++w) { auto computed = yPtr[4 * h + w]; auto expected = 0.0f; if (h - w <= 1) { expected = values[4 * h + w]; } if (computed != expected) { FUNC_PRINT(1); return false; } } } } } { lower->writeMap()[0] = 2; upper->writeMap()[0] = 1; { auto yPtr = y->readMap(); for (int h = 0; h < 4; ++h) { for (int w = 0; w < 4; ++w) { auto computed = yPtr[4 * h + w]; auto expected = 0.0f; if ((h - w) <= 2 && (w - h) <= 1) { expected = values[4 * h + w]; } if (computed != expected) { FUNC_PRINT(1); return false; } } } } } { matrix->resize({3, 5, 5}); auto matrixPtr = matrix->writeMap(); for (int i = 0; i < matrix->getInfo()->size; ++i) { matrixPtr[i] = (float)i; } lower->writeMap()[0] = 2; upper->writeMap()[0] = 1; auto yPtr = y->readMap(); for (int z = 0; z < 3; ++z) { for (int h = 0; h < 5; ++h) { for (int w = 0; w < 5; ++w) { auto index = w + 5 * h + 5 * 5 * z; auto computed = yPtr[index]; auto expected = 0.0f; if ((h - w) <= 2 && (w - h) <= 1) { expected = (float)(index); } if (computed != expected) { FUNC_PRINT(1); return false; } } } } } return true; } }; MNNTestSuiteRegister(MatrixBandTest, "expr/MatrixBand");