MulOpTest.cpp 8.1 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
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 已提交
42 43 44 45 46 47
                       FuncConfig()
                           .set("scaleAB", alpha)
                           .set("scaleT", beta)
                           .set("aTrans", transa)
                           .set("bTrans", transb)
                           .set("cTrans", false));
48 49
  // prepare input arguments
  /// matrix A : HA * WA
X
xutianbing 已提交
50
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{heightA, widthA}));
51
  /// matrix B: HB * WB
X
xutianbing 已提交
52
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{heightB, widthB}));
53 54 55

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

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

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

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

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

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

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

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

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

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

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

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

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