MulOpTest.cpp 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <gtest/gtest.h>
#include "FunctionTest.h"
#include "paddle/math/Matrix.h"
#include "paddle/math/SparseMatrix.h"
19
#include "paddle/math/tests/test_matrixUtil.h"
20 21 22 23
#include "paddle/testing/TestUtil.h"

using namespace paddle;  // NOLINT

24
/**
25
 *  C += A * B, A, B, C dense matrix
26
 *  dense = dense * dense
27
 */
28 29 30 31 32 33 34 35 36 37 38 39
void testFuncDDDMatrix(
    bool transa, bool transb, size_t dimM, size_t dimN, size_t dimK) {
  real alpha = 1.0;
  real beta = 1.0;
  size_t heightA = (transa == false) ? dimM : dimK;
  size_t widthA = (transa == false) ? dimK : dimM;
  size_t heightB = (transb == false) ? dimK : dimN;
  size_t widthB = (transb == false) ? dimN : dimK;
  size_t heightC = dimM;
  size_t widthC = dimN;
  // init Test object
  FunctionCompare test("MulOp",
X
xutianbing 已提交
40 41 42 43 44 45
                       FuncConfig()
                           .set("scaleAB", alpha)
                           .set("scaleT", beta)
                           .set("aTrans", transa)
                           .set("bTrans", transb)
                           .set("cTrans", false));
46 47
  // prepare input arguments
  /// matrix A : HA * WA
X
xutianbing 已提交
48
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{heightA, widthA}));
49
  /// matrix B: HB * WB
X
xutianbing 已提交
50
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{heightB, widthB}));
51 52 53

  /// output matrix C: HC * WC
  test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{heightC, widthC}),
X
xutianbing 已提交
54
                  beta == 1.0 ? ADD_TO : ASSIGN_TO);
55 56
  // run Function
  test.run();
57 58
}

59 60
TEST(MulOp, DDDMatrixMul) {
  LOG(INFO) << "function test for dense = dense * dense matrix";
X
xutianbing 已提交
61 62 63 64 65 66
  for (const auto transa : {false, true}) {
    for (const auto transb : {false, true}) {
      for (const auto dimM : {1, 10, 100}) {
        for (const auto dimN : {1, 10}) {
          for (const auto dimK : {8}) {
            if (transa && transb) {
67 68 69 70 71 72 73
              continue;
            }
            VLOG(3) << setiosflags(std::ios::left) << std::setfill(' ')
                    << " transa=" << transa << " transb=" << transb
                    << " dimM=" << std::setw(5) << dimM
                    << " dimN=" << std::setw(5) << dimN
                    << " dimK=" << std::setw(5) << dimK;
74
            testFuncDDDMatrix(transa, transb, dimM, dimN, dimK);
75 76
          }
        }
77 78 79 80
      }
    }
  }
}
81

82 83 84 85
/**
  * C += A * B, B, C dense, A sparse
  * dense = sparse * dense
  */
86
void testFuncDSparseDMatrix(
87 88 89
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
  real alpha = 1.0;
  real beta = 1.0;
90 91
  // init Test object
  FunctionCompare test("MulOp",
X
xutianbing 已提交
92 93 94 95 96 97
                       FuncConfig()
                           .set("scaleAB", alpha)
                           .set("scaleT", beta)
                           .set("aTrans", false)
                           .set("bTrans", false)
                           .set("cTrans", false));
98 99
  // prepare input arguments
  /// sparse matrix A : M * K
X
xutianbing 已提交
100 101
  test.addInputs(SparseMatrixArg(
      VALUE_TYPE_FLOAT, TensorShape{dimM, dimK}, nnz, FORMAT, FLOAT_VALUE));
102 103 104 105
  /// matrix B: K * N
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimK, dimN}));

  /// output matrix C: M * N
X
xutianbing 已提交
106 107
  test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimN}),
                  beta == 1.0 ? ADD_TO : ASSIGN_TO);
108 109
  // run Function
  test.run();
110 111
}

112 113
TEST(MuLOp, DSparseDMul) {
  LOG(INFO) << "function test for dense = sparse * dense matrix";
114 115 116 117 118 119 120 121 122 123 124
  for (const auto dimM : {10, 100, 1000}) {
    for (const auto dimN : {10, 100}) {
      for (const auto dimK : {3, 10}) {
        for (const auto nnz : {3, 10}) {
          for (const auto FORMAT : {SPARSE_CSR}) {
            VLOG(3) << setiosflags(std::ios::left) << std::setfill(' ')
                    << " dimM=" << std::setw(5) << dimM
                    << " dimN=" << std::setw(5) << dimN
                    << " dimK=" << std::setw(5) << dimK
                    << " nnz=" << std::setw(5) << nnz
                    << " format=" << std::setw(5) << FORMAT;
125
            testFuncDSparseDMatrix(dimM, dimN, dimK, nnz, FORMAT);
126 127 128 129 130 131
          }
        }
      }
    }
  }
}
132 133 134

/**
  * C += A * B, A, C dense, B sparse
135
  * dense = dense * sparse
136
  */
137
void testFuncDDSparseMatrix(
138
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
139 140
  real alpha = 1.0;
  real beta = 1.0;
141 142
  // init Test object
  FunctionCompare test("MulOp",
X
xutianbing 已提交
143 144 145 146 147 148
                       FuncConfig()
                           .set("scaleAB", alpha)
                           .set("scaleT", beta)
                           .set("aTrans", false)
                           .set("bTrans", false)
                           .set("cTrans", false));
149 150 151 152 153
  // prepare input arguments
  /// matrix A : M * K
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimK}));

  /// matrix B: K * N
X
xutianbing 已提交
154 155
  test.addInputs(SparseMatrixArg(
      VALUE_TYPE_FLOAT, TensorShape{dimK, dimN}, nnz, FORMAT, FLOAT_VALUE));
156 157

  /// output matrix C: M * N
X
xutianbing 已提交
158 159
  test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimN}),
                  beta == 1.0 ? ADD_TO : ASSIGN_TO);
160 161
  // run Function
  test.run();
162 163
}

164 165
TEST(MulOp, DDSparseMul) {
  LOG(INFO) << "function test for dense = dense * sparse matrix";
166 167 168 169 170 171 172 173 174 175 176
  for (const auto dimM : {10, 100, 1000}) {
    for (const auto dimN : {10, 100}) {
      for (const auto dimK : {3, 10}) {
        for (const auto nnz : {3, 10}) {
          for (const auto FORMAT : {SPARSE_CSR, SPARSE_CSC}) {
            VLOG(3) << setiosflags(std::ios::left) << std::setfill(' ')
                    << " dimM=" << std::setw(5) << dimM
                    << " dimN=" << std::setw(5) << dimN
                    << " dimK=" << std::setw(5) << dimK
                    << " nnz=" << std::setw(5) << nnz
                    << " format=" << std::setw(5) << FORMAT;
177
            testFuncDDSparseMatrix(dimM, dimN, dimK, nnz, FORMAT);
178 179 180 181 182 183 184 185 186 187 188
          }
        }
      }
    }
  }
}

/**
  * C += A * B, A sparse, B, C dense
  * sparse = dense * dense
  */
189
void testFuncSparseDDMatrix(
190 191 192
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
  real alpha = 1.0;
  real beta = 1.0;
193 194
  // init Test object
  FunctionCompare test("MulOp",
X
xutianbing 已提交
195 196 197 198 199 200
                       FuncConfig()
                           .set("scaleAB", alpha)
                           .set("scaleT", beta)
                           .set("aTrans", false)
                           .set("bTrans", false)
                           .set("cTrans", false));
201 202 203
  // prepare input arguments
  /// matrix A : M * K
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimK}));
204

205 206
  /// matrix B: K * N
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimK, dimN}));
207

208
  /// output sparse matrix C: M * N
X
xutianbing 已提交
209 210 211 212
  test.addOutputs(
      SparseMatrixArg(
          VALUE_TYPE_FLOAT, TensorShape{dimM, dimN}, nnz, FORMAT, FLOAT_VALUE),
      beta == 1.0 ? ADD_TO : ASSIGN_TO);
213 214
  // run Function
  test.run();
215 216
}

217 218
TEST(MulOp, SparseDDMul) {
  LOG(INFO) << "function test for sparse = dense * dense matrix";
219 220 221 222 223 224 225 226 227 228 229
  for (const auto dimM : {10, 100, 1000}) {
    for (const auto dimN : {10, 100}) {
      for (const auto dimK : {3, 10}) {
        for (const auto nnz : {3, 10}) {
          for (const auto FORMAT : {SPARSE_CSC, SPARSE_CSR}) {
            VLOG(3) << setiosflags(std::ios::left) << std::setfill(' ')
                    << " dimM=" << std::setw(5) << dimM
                    << " dimN=" << std::setw(5) << dimN
                    << " dimK=" << std::setw(5) << dimK
                    << " nnz=" << std::setw(5) << nnz
                    << " format=" << std::setw(5) << FORMAT;
230
            testFuncSparseDDMatrix(dimM, dimN, dimK, nnz, FORMAT);
231 232 233 234 235
          }
        }
      }
    }
  }
236
}