Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making it easier to write unittest for comparing gpu and cpu version of a function #385

Closed
emailweixu opened this issue Nov 7, 2016 · 4 comments
Assignees

Comments

@emailweixu
Copy link
Collaborator

Currently, the unittest for comparison of the gpu and cpu version of a same function is quite tedious. An exmaple is https://github.com/baidu/Paddle/pull/358/files/3404bf1e85377a9f50008c95b74017f96bd28f36..f502b0c33ef770cd14c6aea1e6c429900d18d36e

Should add some utility function so that a developer only need to provide the function name for comparing the two versions of function. The utility function will responsible for generating data, running through different size of matrix and comparing the results.

@hedaoyuan hedaoyuan self-assigned this Nov 8, 2016
@hedaoyuan
Copy link
Contributor

OK, I will refactor the test_matrixCompare.cpp. The test case construct in pr #239 has been relatively simple.

@qingqing01 qingqing01 added this to the 0.10.0 milestone Nov 11, 2016
@hedaoyuan
Copy link
Contributor

@hedaoyuan
Copy link
Contributor

hedaoyuan commented Nov 30, 2016

test_matrixCompare.cpp里面的测试主要是比较CpuMatrix和GpuMatrix成员函数实现上是否一致,整个test文件看起来比较冗余,实际上每个test case主要包含四个步骤。

  1. 初始化一个CPU和一个GPU对象;
  2. 分别初始化CpuMatrix和GpuMatrix相应成员函数所用到的参数;
  3. 用构造的参数分别调用一次CPU和GPU对象的成员函数;
  4. 对CPU和GPU成员函数调用结果进行比较。

PR #643 里面增加了一个AutoCompare来简化test case的构造方法,AutoCompare有两种用法:

  1. 使用cmpWithArg方法来比较CPU和GPU函数,这种方式只需要构造CPU成员函数所用的参数,书写的test case可以节省一半代码,并且能够使test case的代码逻辑更清楚;
    比如,Matrix::selectRows的test case,原先的实现如下:
void testMatrixSelectRows(int numSamples, int tableSize, int inputDim) {
  MatrixPtr cpuTable = std::make_shared<CpuMatrix>(tableSize, inputDim);
  MatrixPtr gpuTable = std::make_shared<GpuMatrix>(tableSize, inputDim);
  cpuTable->randomizeUniform();
  gpuTable->copyFrom(*cpuTable);

  IVectorPtr cpuIds;
  IVectorPtr gpuIds;
  cpuIds = VectorT<int>::create(numSamples, false);
  gpuIds = VectorT<int>::create(numSamples, true);
  cpuIds->rand(tableSize);
  gpuIds->copyFrom(*cpuIds);

  MatrixPtr cpuOutput = std::make_shared<CpuMatrix>(numSamples, inputDim);
  MatrixPtr gpuOutput = std::make_shared<GpuMatrix>(numSamples, inputDim);
  cpuOutput->randomizeUniform();
  gpuOutput->copyFrom(*cpuOutput);

  cpuOutput->selectRows(*cpuTable, *cpuIds);
  gpuOutput->selectRows(*gpuTable, *gpuIds);

  // check
  MatrixPtr outputCheck = std::make_shared<CpuMatrix>(numSamples, inputDim);
  outputCheck->copyFrom(*gpuOutput);
  MatrixCheckEqual(*cpuOutput, *outputCheck);
}

使用AutoCompare的test case如下:

void testMatrixSelectRows(int numSamples, int tableSize, int inputDim) {
  AutoCompare test(numSamples, inputDim);
  CpuMatrix arg1(tableSize, inputDim);
  CpuIVector arg2(numSamples);
  arg1.randomizeUniform();
  arg2.rand(tableSize);
  test.cmpWithArg(&Matrix::selectRows, arg1, arg2);
}
  1. 使用cmpWithoutArg方法来比较CPU和GPU函数,这种方式只需要指定比较的成员函数,不需要构造参数。但是cmpWithoutArg使用上有限制,必须是成员函数参数中用到的Matrix都是相同size的。cmpWithoutArg适合用来比较class BaseMatrix里面的element wise和aggregate函数。
    比如BaseMatrix::add(real p);的test case,原先的实现如下:
void testMatrixAdd(int height, int width) {
  MatrixPtr cpuA = std::make_shared<CpuMatrix>(height, width);
  MatrixPtr gpuA = std::make_shared<GpuMatrix>(height, width);

  cpuA->randomizeUniform();
  gpuA->copyFrom(*cpuA);
  cpuA->add(2.5);
  gpuA->add(2.5);

  MatrixPtr outputCheck = std::make_shared<CpuMatrix>(height, width);
  outputCheck->copyFrom(*gpuA);
  MatrixCheckEqual(*cpuA, *outputCheck);
}

使用AutoCompare的test case如下:

AutoCompare test(height, width);
test.cmpWithoutArg<0>(&BaseMatrix::add, height, width);

如果add在BaseMatrix里面是有重载,则这里可以通过static_cast来显示的指定是哪个add函数。

test.cmpWithoutArg<0>(static_cast<void (BaseMatrix::*)(real)>(&BaseMatrix::add), height, width);

@hedaoyuan
Copy link
Contributor

还有一些实现上比较奇怪的成员函数目前不能使用AutoCompare,比如:

  1. 成员函数参数中有输出值的,AutoCompare不会比较参数的结果。
  2. 成员函数参数类型中有shared_ptr类型的,AutoCompare不支持shared_ptr类型的参数。
  3. Vector,SparseMatrix的成员函数比较,需要扩展AutoCompare支持这些类型。

zhhsplendid pushed a commit to zhhsplendid/Paddle that referenced this issue Sep 25, 2019
AnnaTrainingG pushed a commit to AnnaTrainingG/Paddle that referenced this issue Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants