MulOpTest.cpp 7.3 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 28 29 30 31 32 33 34
/**
 *  C = alpha * C + beta * (A * B)
 */
void testMatrixMul(bool transa, bool transb, int dimM, int dimN, int dimK) {
  real alpha = 1.5;
  real beta = 2.0;

  const auto cpuFunc = FunctionBase::funcRegistrar_.createByType("MulOp-CPU");
  cpuFunc->init(FuncConfig().set("scaleAB", alpha).set("scaleT", beta));
35
  const auto gpuFunc = FunctionBase::funcRegistrar_.createByType("MulOp-GPU");
36
  gpuFunc->init(FuncConfig().set("scaleAB", alpha).set("scaleT", beta));
37

38 39 40 41 42 43
  int heightA = (transa == false) ? dimM : dimK;
  int widthA = (transa == false) ? dimK : dimM;
  int heightB = (transb == false) ? dimK : dimN;
  int widthB = (transb == false) ? dimN : dimK;
  int heightC = dimM;
  int widthC = dimN;
44

45 46 47 48 49 50
  auto cpuA = std::make_shared<CpuMatrix>(heightA, widthA, transa);
  auto cpuB = std::make_shared<CpuMatrix>(heightB, widthB, transb);
  auto cpuC = std::make_shared<CpuMatrix>(heightC, widthC);
  auto gpuA = std::make_shared<GpuMatrix>(heightA, widthA, transa);
  auto gpuB = std::make_shared<GpuMatrix>(heightB, widthB, transb);
  auto gpuC = std::make_shared<GpuMatrix>(heightC, widthC);
51 52 53 54

  cpuA->randomizeUniform();
  cpuB->randomizeUniform();
  cpuC->randomizeUniform();
55 56 57
  gpuA->copyFrom(*cpuA);
  gpuB->copyFrom(*cpuB);
  gpuC->copyFrom(*cpuC);
58

59 60 61 62 63 64
  BufferArgs cpuInputs;
  BufferArgs cpuOutputs;
  cpuInputs.addArg(*cpuA);
  cpuInputs.addArg(*cpuB);
  cpuOutputs.addArg(*cpuC, ADD_TO);
  cpuFunc->calc(cpuInputs, cpuOutputs);
65

66 67 68 69 70 71
  BufferArgs gpuInputs;
  BufferArgs gpuOutputs;
  gpuInputs.addArg(*gpuA);
  gpuInputs.addArg(*gpuB);
  gpuOutputs.addArg(*gpuC, ADD_TO);
  gpuFunc->calc(gpuInputs, gpuOutputs);
72

73
  autotest::TensorCheckErr(*cpuC, *gpuC);
74 75
}

76
TEST(Matrix, mul) {
77
  LOG(INFO) << "test for dense = dense * dense matrix";
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  for (auto transa : {false, true}) {
    for (auto transb : {false, true}) {
      for (auto dimM : {1, 10, 100}) {
        for (auto dimN : {1, 10}) {
          for (auto dimK : {8}) {
            if (true == transa && true == transb) {
              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;

            testMatrixMul(transa, transb, dimM, dimN, dimK);
          }
        }
95 96 97 98
      }
    }
  }
}
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

struct MatrixPara {
  size_t height;
  size_t width;
  bool trans;
  bool sparse;
  size_t nnz;
  SparseFormat format;
};

/**
  * C += A * B, A, C dense, B sparse
  */
void testDSparseDMatrix() {
  real alpha = 1.0;
  real beta = 1.0;
  const auto cpuFunc = FunctionBase::funcRegistrar_.createByType("MulOp-CPU");
  cpuFunc->init(FuncConfig().set("scaleAB", alpha).set("scaleT", beta));
  const auto gpuFunc = FunctionBase::funcRegistrar_.createByType("MulOp-GPU");
  gpuFunc->init(FuncConfig().set("scaleAB", alpha).set("scaleT", beta));

  constexpr size_t dimM = 2;
  constexpr size_t dimN = 2;
  constexpr size_t dimK = 3;
  constexpr size_t NNZ = 3;
  constexpr SparseFormat FORMAT = SPARSE_CSC;

  MatrixPara paraA{dimM, dimK, /*trans*/ false, /*sparse*/ false, NNZ, FORMAT};
  MatrixPara paraB{dimK, dimN, /*trans*/ false, /*sparse*/ true, NNZ, FORMAT};
  MatrixPara paraC{dimM, dimN, /*trans*/ false, /*sparse*/ false, NNZ, FORMAT};

  auto cpuMatrixA =
      Matrix::create(paraA.height, paraA.width, paraA.trans, false);
  auto gpuMatrixA =
      Matrix::create(paraA.height, paraA.width, paraA.trans, true);
  auto cpuDenseA =
      Matrix::create(paraA.height, paraA.width, paraA.trans, false);
  CpuSparseMatrix cpuMatrixB(paraB.height,
                             paraB.width,
                             paraB.nnz,
                             FLOAT_VALUE,
                             paraB.format,
                             paraB.trans);

  GpuSparseMatrix gpuMatrixB(paraB.height,
                             paraB.width,
                             paraB.nnz,
                             FLOAT_VALUE,
                             paraB.format,
                             paraB.trans);

  auto cpuDenseB =
      Matrix::create(paraB.height, paraB.width, paraB.trans, false);
  auto cpuMatrixC =
      Matrix::create(paraC.height, paraC.width, paraC.trans, false);
  auto gpuMatrixC =
      Matrix::create(paraC.height, paraC.width, paraC.trans, true);
  auto cpuDenseC =
      Matrix::create(paraC.height, paraC.width, paraC.trans, false);
  auto gpuMatrixC_d2h =
      Matrix::create(paraC.height, paraC.width, paraC.trans, false);
  /*matrix init*/
  hl_stream_t stream(HPPL_STREAM_1);
  cpuMatrixA->randomizeUniform();
  cpuMatrixB.randomizeUniform();
  cpuMatrixC->randomizeUniform();

  gpuMatrixA->copyFrom(*cpuMatrixA, stream);
  gpuMatrixB.copyFrom(cpuMatrixB, stream);
  gpuMatrixC->copyFrom(*cpuMatrixC, stream);

  cpuDenseA->copyFrom(*cpuMatrixA);
  cpuDenseB->copyFrom(cpuMatrixB);
  cpuDenseC->copyFrom(*cpuMatrixC);
  hl_stream_synchronize(stream);

  LOG(INFO) << "cpuMatrixA: ";
  cpuMatrixA->print(std::cout);
  LOG(INFO) << "cpuMatrixB: ";
  (&cpuMatrixB)->print(std::cout);
  LOG(INFO) << "cpuMatrixC: ";
  cpuMatrixC->print(std::cout);

  LOG(INFO) << "cpuDenseA: ";
  cpuDenseA->print(std::cout);
  LOG(INFO) << "cpuDenseB: ";
  cpuDenseB->print(std::cout);
  LOG(INFO) << "cpuDenseC: ";
  cpuDenseC->print(std::cout);

  LOG(INFO) << "gpuMatrixA: ";
  gpuMatrixA->print(std::cout);
  LOG(INFO) << "gpuMatrixB: ";
  (&gpuMatrixB)->print(std::cout);
  LOG(INFO) << "gpuMatrixC: ";
  gpuMatrixC->print(std::cout);

  /*matrix mul*/
  BufferArgs cpuInputs;
  BufferArgs cpuOutputs;
  cpuInputs.addArg(*cpuMatrixA);
  cpuInputs.addArg(cpuMatrixB);
  cpuOutputs.addArg(*cpuMatrixC, ADD_TO);
  cpuFunc->calc(cpuInputs, cpuOutputs);

  BufferArgs gpuInputs;
  BufferArgs gpuOutputs;
  gpuInputs.addArg(*gpuMatrixA);
  gpuInputs.addArg(gpuMatrixB);
  gpuOutputs.addArg(*gpuMatrixC, ADD_TO);
  gpuFunc->calc(gpuInputs, gpuOutputs);

  BufferArgs denseInputs;
  BufferArgs denseOutputs;
  denseInputs.addArg(*cpuDenseA);
  denseInputs.addArg(*cpuDenseB);
  denseOutputs.addArg(*cpuDenseC, ADD_TO);
  cpuFunc->calc(denseInputs, denseOutputs);

  gpuMatrixC_d2h->copyFrom(*gpuMatrixC, stream);
  hl_stream_synchronize(stream);
  /*check result*/
  // autotest::TensorCheckErr(*cpuMatrixC, *gpuMatrixC);
  checkMatrixEqual(cpuMatrixC, cpuDenseC);
  checkMatrixEqual(cpuMatrixC, gpuMatrixC_d2h);
}

TEST(Matrix, SparseMatrixMul) {
  LOG(INFO) << "test for dense = dense * sparse matrix";
  testDSparseDMatrix();
}