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

Matrix::MUL operators using and test Daoyuan's Paddle Function, SparseMatrixArg and Function Test #1147

Merged
merged 13 commits into from
Jan 30, 2017

Conversation

tianbingsz
Copy link
Contributor

@tianbingsz tianbingsz commented Jan 13, 2017

Add the following Matrix::Mul operators: (#977)
Use and test Daoyuan's SparseMatrixArg, Function Test.

  1. CpuMatrix::mul(CpuMatrix, CpuMatrix)
  2. GpuMatrix::mul(GpuMatrix, GpuMatrix)
  3. CpuSparseMatrix::mul(CpuMatrix, CpuMatrix)
  4. GpuSparseMatrix::mul(GpuMatrix, GpuMatrix)
  5. CpuMatrix::mul(CpuSparseMatrix, CpuMatrix)
  6. GpuMatrix::mul(GpuSparseMatrix, GpuMatrix)
  7. CpuMatrix::mul(CpuMatrix, CpuSparseMatrix)
  8. GpuMatrix::mul(GpuMatrix, GpuSparseMatrix)

Write new sparse/dense matrix unit test using Function test.

@tianbingsz tianbingsz self-assigned this Jan 15, 2017
@tianbingsz tianbingsz force-pushed the paddle_func_sparse branch 4 times, most recently from 90a0f3f to 3f09139 Compare January 18, 2017 23:54
@tianbingsz tianbingsz changed the title Use and test Daoyuan's SparseMatrixArg. Use and test Daoyuan's SparseMatrixArg for Matrix::MUL operators. Jan 19, 2017
@tianbingsz tianbingsz requested a review from hedaoyuan January 19, 2017 06:23
@tianbingsz tianbingsz changed the title Use and test Daoyuan's SparseMatrixArg for Matrix::MUL operators. Matrix::MUL operators using and test Daoyuan's Paddle Function, SparseMatrixArg and Function Test Jan 19, 2017
Copy link
Contributor

@hedaoyuan hedaoyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要先看了一下MulFunc的实现。


/**
* mul operator
* out = scaleT * out + scaleAB*(in1 * in2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释还得在加强,要写明白MulFunc支持哪些参数类型,比如Dense,CSR/CSC Sparse等。
另外,calc实现中的几个if逻辑也需要在这里写清楚。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

public:
void init(const FuncConfig& config) override {
alpha_ = config.get<real>("scaleAB");
beta_ = config.get<real>("scaleT");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alpha_, beta_这两个不一定需要。目前Paddle中对于Matrix::mul()的调用只有C = A * B和C = A * B + C两种,分别可以通过设置C参数的ASSIGN_TO/ADD_TO属性来实现。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* mul operator
* out = scaleT * out + scaleAB*(in1 * in2)
*
* \param outputs[0] output matrix, M * N
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output matrix, M * N这样的注释也并不好,并没有哪个地方解释M和N分别是什么。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

alpha_,
beta_);
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还需要增加一个CHECK检查上面几个if都没有走到的逻辑。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -269,8 +265,8 @@ class SparseMatrixArg : public BufferArg {
const BufferArg& row,
const BufferArg& col,
size_t nnz,
SparseDataFormat format,
SparseDataType type,
SparseFormat format,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SparseFormat和SparseValueType放在Matrix.h里面并不合适;Matrix.h和SparseMatrix.h里面的数据结构最终是不准备用的,所以可以考虑把这两个TensorType.h里面去。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -179,6 +174,7 @@ class BufferArg {
TensorShape shape_;
BufferType bufferType_{TENSOR_UNKNOWN};
ArgType argType_{UNSPECIFIED};
bool trans_{false};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BufferArg可以表示一个高维的data,trans_属性对3维或者4维的BufferArg无法很好的解释。
Mul Function里面的transa/transb实际是Function的属性,而不是BufferArg的属性(cblas_sgemm/cublasSgemm也一样)。

Copy link
Contributor Author

@tianbingsz tianbingsz Jan 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trans_ 是数据的属性,matrix, sparse matrix里面是转置矩阵,对于更高order的Tensor,其实是有对应于矩阵转置的generalization的。https://arxiv.org/pdf/1411.1503v1.pdf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const CpuMatrix& b,
real scaleAB,
real scaleT) {
CHECK(!out.isTransposed()) << "Not supported";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里,包括后面几个API中的一些参数CHECK,实际应该是在Function里面检查。如果运行时参数错误,也需要在Function里面就能报出。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@hedaoyuan hedaoyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve FunctionTest

void run() {
// prepare cpu/gpu arguments
initInputs();

initOutputs();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FunctionCompare可能得加个钩子,uniform(0.001, 1)可能支持不了所有Function的参数初始化。

Copy link
Contributor Author

@tianbingsz tianbingsz Jan 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Will do it later for the future unit test cases.

Copy link
Contributor

@hedaoyuan hedaoyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外,我在#1216 (comment)
里面FunctionBase增加ops和check两个接口,在乘法接口里面也可以实现一下。

@hedaoyuan , 好的,我看一下你的PR,然后再开个PR,在MUL里面添加一下这两个接口。
我这个PR比较大一点,已经address了一下你的comments。如果你觉得没什么问题的话,要不先Approve一下?

real beta_;
bool aTrans_;
bool bTrans_;
bool cTrans_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cTrans_不需要吧。cblas_sgemm/cublasSgemm里面也有没有cTrans这个参数的。
另外,alpha_/beta_我觉得还是去掉吧,从目前Paddle的实现来看NeuralNetwork Computation里面并不会需要这样两个参数,去掉也可以稍微简化一点接口的用法。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
CHECK(!cTrans_) << "output matrix should not be transposed";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以后估计也不会支持cTrans_ == ture,所以直接去掉cTrans_更好。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@tianbingsz tianbingsz merged commit ecbff68 into PaddlePaddle:develop Jan 30, 2017
wangxicoding pushed a commit to wangxicoding/Paddle that referenced this pull request Dec 9, 2021
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

Successfully merging this pull request may close these issues.

2 participants