MulOpTest.cpp 12.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
 *  C = alpha * C + beta * (A * B), A, B, C dense matrix
 *  dense = dense * dense
29
 */
30
void testDDDMatrix(bool transa, bool transb, int dimM, int dimN, int dimK) {
31 32 33 34 35
  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));
36
  const auto gpuFunc = FunctionBase::funcRegistrar_.createByType("MulOp-GPU");
37
  gpuFunc->init(FuncConfig().set("scaleAB", alpha).set("scaleT", beta));
38

39 40 41 42 43 44
  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;
45

46 47 48 49 50 51
  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);
52 53 54 55

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

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

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

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

77
TEST(Matrix, DDDMul) {
78
  LOG(INFO) << "test for dense = dense * dense matrix";
79 80 81 82 83 84 85 86 87 88 89 90 91 92
  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;

93
            testDDDMatrix(transa, transb, dimM, dimN, dimK);
94 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
/**
  * C += A * B, B, C dense, A sparse
  * dense = sparse * dense
  */
void testDSparseDMatrix(
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
  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));

  CpuSparseMatrix cpuMatrixA(dimM, dimK, nnz, FLOAT_VALUE, FORMAT, false);
  GpuSparseMatrix gpuMatrixA(dimM, dimK, nnz, FLOAT_VALUE, FORMAT, false);
  CpuMatrix cpuDenseA(dimM, dimK, false);

  auto cpuMatrixB = Matrix::create(dimK, dimN, false, false);
  auto gpuMatrixB = Matrix::create(dimK, dimN, false, true);
  auto cpuDenseB = Matrix::create(dimK, dimN, false, false);

  auto cpuMatrixC = Matrix::create(dimM, dimN, false, false);
  auto gpuMatrixC = Matrix::create(dimM, dimN, false, true);
  auto cpuDenseC = Matrix::create(dimM, dimN, false, 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);

  /*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);

  /*check result*/
  autotest::TensorCheckErr(*cpuMatrixC, *cpuDenseC);
  autotest::TensorCheckErr(*cpuMatrixC, *gpuMatrixC);
}

TEST(Matrix, DSparseDMul) {
  LOG(INFO) << "test for dense = sparse * dense matrix";
  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;
            testDSparseDMatrix(dimM, dimN, dimK, nnz, FORMAT);
          }
        }
      }
    }
  }
}
188 189 190

/**
  * C += A * B, A, C dense, B sparse
191
  * dense = dense * sparse
192
  */
193 194
void testDDSparseMatrix(
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
195 196 197 198 199 200 201
  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));

202 203 204 205 206 207 208 209 210 211 212 213 214
  auto cpuMatrixA = Matrix::create(dimM, dimK, false, false);
  auto gpuMatrixA = Matrix::create(dimM, dimK, false, true);
  auto cpuDenseA = Matrix::create(dimM, dimK, false, false);

  CpuSparseMatrix cpuMatrixB(dimK, dimN, nnz, FLOAT_VALUE, FORMAT, false);

  GpuSparseMatrix gpuMatrixB(dimK, dimN, nnz, FLOAT_VALUE, FORMAT, false);

  auto cpuDenseB = Matrix::create(dimK, dimN, false, false);
  auto cpuMatrixC = Matrix::create(dimM, dimN, false, false);
  auto gpuMatrixC = Matrix::create(dimM, dimN, false, true);
  auto cpuDenseC = Matrix::create(dimM, dimN, false, false);

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  /*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);

  /*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);

  /*check result*/
253 254
  autotest::TensorCheckErr(*cpuMatrixC, *cpuDenseC);
  autotest::TensorCheckErr(*cpuMatrixC, *gpuMatrixC);
255 256
}

257
TEST(Matrix, DDSparseMul) {
258
  LOG(INFO) << "test for dense = dense * sparse matrix";
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  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;
            testDDSparseMatrix(dimM, dimN, dimK, nnz, FORMAT);
          }
        }
      }
    }
  }
}

/**
  * C += A * B, A sparse, B, C dense
  * sparse = dense * dense
  */
void testSparseDDMatrix(
    size_t dimM, size_t dimN, size_t dimK, size_t nnz, SparseFormat FORMAT) {
  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));

  auto cpuMatrixA = Matrix::create(dimM, dimK, false, false);
  auto gpuMatrixA = Matrix::create(dimM, dimK, false, true);
  auto cpuDenseA = Matrix::create(dimM, dimK, false, false);

  auto cpuMatrixB = Matrix::create(dimK, dimN, false, false);
  auto gpuMatrixB = Matrix::create(dimK, dimN, false, true);
  auto cpuDenseB = Matrix::create(dimK, dimN, false, false);

  CpuSparseMatrix cpuMatrixC(dimM, dimN, nnz, FLOAT_VALUE, FORMAT, false);
  CpuSparseMatrix gpuMatrixC_d2h(dimM, dimN, nnz, FLOAT_VALUE, FORMAT, false);
  GpuSparseMatrix gpuMatrixC(dimM, dimN, nnz, FLOAT_VALUE, FORMAT, false);
  CpuMatrix cpuDenseC(dimM, dimN, 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);

  /*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*/
  checkSMatrixEqual(cpuMatrixC, gpuMatrixC_d2h);
  checkSMatrixEqual2Dense(cpuMatrixC, cpuDenseC);
}

TEST(Matrix, SparseDDMul) {
  LOG(INFO) << "test for sparse = dense * dense matrix";
  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;
            testSparseDDMatrix(dimM, dimN, dimK, nnz, FORMAT);
          }
        }
      }
    }
  }
368
}