MulOpTest.cpp 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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>
16 17
/// todo(tianbing), delete
#include <iostream>
18 19 20
#include "FunctionTest.h"
#include "paddle/math/Matrix.h"
#include "paddle/math/SparseMatrix.h"
21
#include "paddle/math/tests/test_matrixUtil.h"
22 23 24 25
#include "paddle/testing/TestUtil.h"

using namespace paddle;  // NOLINT

26
/**
27
 *  C += A * B, A, B, C dense matrix
28
 *  dense = dense * dense
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
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",
                       FuncConfig().set("scaleAB", alpha).set("scaleT", beta));
  // prepare input arguments
  /// matrix A : HA * WA
  test.addInputs(BufferArg(
      VALUE_TYPE_FLOAT, TensorShape{heightA, widthA}, UNSPECIFIED, transa));
  /// matrix B: HB * WB
  test.addInputs(BufferArg(
      VALUE_TYPE_FLOAT, TensorShape{heightB, widthB}, UNSPECIFIED, transb));

  /// output matrix C: HC * WC
  test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{heightC, widthC}),
                  ADD_TO);
  // run Function
  test.run();
56 57
}

58 59
TEST(MulOp, DDDMatrixMul) {
  LOG(INFO) << "function test for dense = dense * dense matrix";
X
xutianbing 已提交
60 61 62 63 64 65
  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) {
66 67 68 69 70 71 72
              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;
73
            testFuncDDDMatrix(transa, transb, dimM, dimN, dimK);
74 75
          }
        }
76 77 78 79
      }
    }
  }
}
80

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

  /// output matrix C: M * N
  test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimN}), ADD_TO);
  // run Function
  test.run();
108 109
}

110 111
TEST(MuLOp, DSparseDMul) {
  LOG(INFO) << "function test for dense = sparse * dense matrix";
112 113 114 115 116 117 118 119 120 121 122
  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;
123
            testFuncDSparseDMatrix(dimM, dimN, dimK, nnz, FORMAT);
124 125 126 127 128 129
          }
        }
      }
    }
  }
}
130 131 132

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

  /// matrix B: K * N
  test.addInputs(SparseMatrixArg(VALUE_TYPE_FLOAT,
                                 TensorShape{dimK, dimN},
                                 nnz,
                                 FORMAT,
                                 FLOAT_VALUE,
                                 UNSPECIFIED,
                                 false));

  /// output matrix C: M * N
  test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimN}), ADD_TO);
  // run Function
  test.run();
159 160
}

161 162
TEST(MulOp, DDSparseMul) {
  LOG(INFO) << "function test for dense = dense * sparse matrix";
163 164 165 166 167 168 169 170 171 172 173
  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;
174
            testFuncDDSparseMatrix(dimM, dimN, dimK, nnz, FORMAT);
175 176 177 178 179 180 181 182 183 184 185
          }
        }
      }
    }
  }
}

/**
  * C += A * B, A sparse, B, C dense
  * sparse = dense * dense
  */
186
void testFuncSparseDDMatrix(
187 188 189
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
  real alpha = 1.0;
  real beta = 1.0;
190 191 192 193 194 195
  // init Test object
  FunctionCompare test("MulOp",
                       FuncConfig().set("scaleAB", alpha).set("scaleT", beta));
  // prepare input arguments
  /// matrix A : M * K
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimK}));
196

197 198
  /// matrix B: K * N
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimK, dimN}));
199

200 201 202 203 204 205 206 207 208 209 210
  /// output sparse matrix C: M * N
  test.addOutputs(SparseMatrixArg(VALUE_TYPE_FLOAT,
                                  TensorShape{dimM, dimN},
                                  nnz,
                                  FORMAT,
                                  FLOAT_VALUE,
                                  UNSPECIFIED,
                                  false),
                  ADD_TO);
  // run Function
  test.run();
211 212
}

213 214
TEST(MulOp, SparseDDMul) {
  LOG(INFO) << "function test for sparse = dense * dense matrix";
215 216 217 218 219 220 221 222 223 224 225
  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;
226
            testFuncSparseDDMatrix(dimM, dimN, dimK, nnz, FORMAT);
227 228 229 230 231
          }
        }
      }
    }
  }
232
}