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

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"
X
Xin Pan 已提交
17 18 19
#include "paddle/legacy/math/Matrix.h"
#include "paddle/legacy/math/SparseMatrix.h"
#include "paddle/legacy/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
void testFuncDDDMatrix(
    bool transa, bool transb, size_t dimM, size_t dimN, size_t dimK) {
X
xutianbing 已提交
30
  real scaleT = 1.0;
31 32 33 34 35 36 37
  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
38
  CpuGpuFuncCompare test(
X
xutianbing 已提交
39
      "MulOp", FuncConfig().set("aTrans", transa).set("bTrans", transb));
40 41
  // prepare input arguments
  /// matrix A : HA * WA
X
xutianbing 已提交
42
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{heightA, widthA}));
43
  /// matrix B: HB * WB
X
xutianbing 已提交
44
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{heightB, widthB}));
45 46 47

  /// output matrix C: HC * WC
  test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{heightC, widthC}),
X
xutianbing 已提交
48
                  scaleT == 1.0 ? ADD_TO : ASSIGN_TO);
49 50
  // run Function
  test.run();
51 52
}

53 54
TEST(MulOp, DDDMatrixMul) {
  LOG(INFO) << "function test for dense = dense * dense matrix";
X
xutianbing 已提交
55 56 57 58 59 60
  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) {
61 62
              continue;
            }
L
liaogang 已提交
63
            VLOG(3) << std::setiosflags(std::ios::left) << std::setfill(' ')
64 65 66 67
                    << " transa=" << transa << " transb=" << transb
                    << " dimM=" << std::setw(5) << dimM
                    << " dimN=" << std::setw(5) << dimN
                    << " dimK=" << std::setw(5) << dimK;
68
            testFuncDDDMatrix(transa, transb, dimM, dimN, dimK);
69 70
          }
        }
71 72 73 74
      }
    }
  }
}
75

76
/**
L
liaogang 已提交
77 78 79
 * C += A * B, B, C dense, A sparse
 * dense = sparse * dense
 */
80
void testFuncDSparseDMatrix(
81
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
X
xutianbing 已提交
82
  real scaleT = 1.0;
83
  // init Test object
84 85
  CpuGpuFuncCompare test(
      "MulOp", FuncConfig().set("aTrans", false).set("bTrans", false));
86 87
  // prepare input arguments
  /// sparse matrix A : M * K
X
xutianbing 已提交
88 89
  test.addInputs(SparseMatrixArg(
      VALUE_TYPE_FLOAT, TensorShape{dimM, dimK}, nnz, FORMAT, FLOAT_VALUE));
90 91 92 93
  /// matrix B: K * N
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimK, dimN}));

  /// output matrix C: M * N
X
xutianbing 已提交
94
  test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimN}),
X
xutianbing 已提交
95
                  scaleT == 1.0 ? ADD_TO : ASSIGN_TO);
96 97
  // run Function
  test.run();
98 99
}

100 101
TEST(MuLOp, DSparseDMul) {
  LOG(INFO) << "function test for dense = sparse * dense matrix";
102 103 104 105 106
  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}) {
L
liaogang 已提交
107
            VLOG(3) << std::setiosflags(std::ios::left) << std::setfill(' ')
108 109 110 111 112
                    << " 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;
113
            testFuncDSparseDMatrix(dimM, dimN, dimK, nnz, FORMAT);
114 115 116 117 118 119
          }
        }
      }
    }
  }
}
120 121

/**
L
liaogang 已提交
122 123 124
 * C += A * B, A, C dense, B sparse
 * dense = dense * sparse
 */
125
void testFuncDDSparseMatrix(
126
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
X
xutianbing 已提交
127
  real scaleT = 1.0;
128
  // init Test object
129 130
  CpuGpuFuncCompare test(
      "MulOp", FuncConfig().set("aTrans", false).set("bTrans", false));
131 132 133 134 135
  // prepare input arguments
  /// matrix A : M * K
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimK}));

  /// matrix B: K * N
X
xutianbing 已提交
136 137
  test.addInputs(SparseMatrixArg(
      VALUE_TYPE_FLOAT, TensorShape{dimK, dimN}, nnz, FORMAT, FLOAT_VALUE));
138 139

  /// output matrix C: M * N
X
xutianbing 已提交
140
  test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimN}),
X
xutianbing 已提交
141
                  scaleT == 1.0 ? ADD_TO : ASSIGN_TO);
142 143
  // run Function
  test.run();
144 145
}

146 147
TEST(MulOp, DDSparseMul) {
  LOG(INFO) << "function test for dense = dense * sparse matrix";
148 149 150 151 152
  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}) {
L
liaogang 已提交
153
            VLOG(3) << std::setiosflags(std::ios::left) << std::setfill(' ')
154 155 156 157 158
                    << " 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;
159
            testFuncDDSparseMatrix(dimM, dimN, dimK, nnz, FORMAT);
160 161 162 163 164 165 166 167
          }
        }
      }
    }
  }
}

/**
L
liaogang 已提交
168 169 170
 * C += A * B, A sparse, B, C dense
 * sparse = dense * dense
 */
171
void testFuncSparseDDMatrix(
172
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
X
xutianbing 已提交
173
  real scaleT = 1.0;
174
  // init Test object
175 176
  CpuGpuFuncCompare test(
      "MulOp", FuncConfig().set("aTrans", false).set("bTrans", false));
177 178 179
  // prepare input arguments
  /// matrix A : M * K
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimM, dimK}));
180

181 182
  /// matrix B: K * N
  test.addInputs(BufferArg(VALUE_TYPE_FLOAT, TensorShape{dimK, dimN}));
183

184
  /// output sparse matrix C: M * N
X
xutianbing 已提交
185 186 187
  test.addOutputs(
      SparseMatrixArg(
          VALUE_TYPE_FLOAT, TensorShape{dimM, dimN}, nnz, FORMAT, FLOAT_VALUE),
X
xutianbing 已提交
188
      scaleT == 1.0 ? ADD_TO : ASSIGN_TO);
189 190
  // run Function
  test.run();
191 192
}

193 194
TEST(MulOp, SparseDDMul) {
  LOG(INFO) << "function test for sparse = dense * dense matrix";
195 196 197 198 199
  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}) {
L
liaogang 已提交
200
            VLOG(3) << std::setiosflags(std::ios::left) << std::setfill(' ')
201 202 203 204 205
                    << " 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;
206
            testFuncSparseDDMatrix(dimM, dimN, dimK, nnz, FORMAT);
207 208 209 210 211
          }
        }
      }
    }
  }
212
}