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

CPU forward calculation replaces Eigen with Lapack;Modify linalg exposure rules #35916

Merged
merged 9 commits into from
Sep 26, 2021

Conversation

Zjq9409
Copy link
Contributor

@Zjq9409 Zjq9409 commented Sep 22, 2021

PR types

Others

PR changes

OPs

Describe

Eigh算子前向计算将eigen库替换为Lapack库实现,修改linalg暴露规则。

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@Zjq9409 Zjq9409 changed the title CPU forward calculation replaces Eigen with Lapack CPU forward calculation replaces Eigen with Lapack;Modify linalg exposure rules Sep 24, 2021
input_tensor = dito.Transpose(input);
math::DeviceIndependenceTensorOperations<platform::CPUDeviceContext, T>(
ctx);
*eigen_vectors = dito.Transpose(input);
Copy link
Contributor

Choose a reason for hiding this comment

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

  • lapack输入是列优先的?最好加一些注释说明,这里为什么要transpose。
  • 看了下svd_helper.h里面实现的Transpose,既然只针对最好2维进行transpose,其实没必要区分2-6 D,eigvals_op.h里面SpiltBatchSquareMatrix这种实现方式比较好。
  • EigvalsKernel里面,math::lapackEig<T, Real<T>>这种调用方式也比较好,能够清楚的体现values、vectors的数据类型的关系。

Copy link
Contributor

Choose a reason for hiding this comment

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

如果has_vectorsfalse,好像是否转置得到的特征值都是一样的,所以这里可能不需要transpose?

@@ -128,37 +50,74 @@ struct MatrixEighFunctor<platform::CPUDeviceContext, ValueType, T> {
void operator()(const framework::ExecutionContext &ctx, const Tensor &input,
Copy link
Contributor

Choose a reason for hiding this comment

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

functor里面不再需要从ctx里面获取运行时信息,最好传DeviceContext

int liwork = -1;
int iwork_opt = -1;
T lwork_opt = static_cast<T>(-1);
ValueType rwork_opt = static_cast<ValueType>(-1);
Copy link
Contributor

Choose a reason for hiding this comment

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

这些参数都是啥意思?-1代表什么?lapack函数中会写这些参数吗?可以加一些注释说明下。

auto *infos_data = info_tensor.mutable_data<int>(
framework::make_ddim({batch_size}), ctx.GetPlace());

math::lapackEvd<T, ValueType>(jobz, uplo, n, out_vector, lda, out_value,
Copy link
Contributor

Choose a reason for hiding this comment

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

加个注释说明单独这一次lapack函数调用的目的是什么。

*info_ptr));
}
if (has_vectors) {
*eigen_vectors = dito.Transpose(*eigen_vectors);
Copy link
Contributor

Choose a reason for hiding this comment

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

确认下,前后2个Transpose是必须的吗?

auto *value_data = out_value + i * values_stride;
auto *vector_data = out_vector + i * vector_stride;
int *info_ptr = &infos_data[i];
math::lapackEvd<T, ValueType>(jobz, uplo, n, vector_data, lda, value_data,
Copy link
Contributor

Choose a reason for hiding this comment

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

Evd是什么意思,会被用到别的计算吗,是否直接叫lapackEigh比较好?

T lwork_opt = static_cast<T>(-1);
ValueType rwork_opt = static_cast<ValueType>(-1);

Tensor info_tensor;
Copy link
Contributor

Choose a reason for hiding this comment

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

如果一次只计算1个矩阵,貌似不用定义一个info_tensor,而是直接用int info就够?

PADDLE_ENFORCE_EQ(
*info_ptr, 0,
platform::errors::PreconditionNotMet(
"For batch [%d]: the [%d] argument had an illegal value", i,
Copy link
Contributor

Choose a reason for hiding this comment

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

这个报错信息不太具有实际含义。

}

template <>
void lapackEvd<paddle::platform::complex<float>, float>(
Copy link
Contributor

Choose a reason for hiding this comment

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

这里可不必加paddle::

// lapack is a column-first storage, transpose make the eigen_vectors to
// have a continuous memory layout
*eigen_vectors = dito.Transpose(input);
auto *out_vector = eigen_vectors->mutable_data<T>(ctx.GetPlace());
Copy link
Contributor

Choose a reason for hiding this comment

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

has_vectorsfalse时,eigen_vectors为nullptr,不应该调mutable_data

input_tensor = dito.Transpose(input);
math::DeviceIndependenceTensorOperations<platform::CPUDeviceContext, T>(
ctx);
// lapack is a column-first storage, transpose make the eigen_vectors to
Copy link
Contributor

Choose a reason for hiding this comment

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

一般是说column-major storge

input_tensor = dito.Transpose(input);
math::DeviceIndependenceTensorOperations<platform::CPUDeviceContext, T>(
ctx);
*eigen_vectors = dito.Transpose(input);
Copy link
Contributor

Choose a reason for hiding this comment

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

如果has_vectorsfalse,好像是否转置得到的特征值都是一样的,所以这里可能不需要transpose?

// Call lapackEigh to get the optimal size of work data
math::lapackEigh<T, math::Real<T>>(jobz, uplo, n, out_vector, lda,
out_value, &lwork_opt, lwork, &rwork_opt,
lrwork, &iwork_opt, liwork, &info);
Copy link
Contributor

Choose a reason for hiding this comment

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

这里不需要检查info吗?

int iwork_opt = -1; // The optimal length of the array liwork
T lwork_opt = static_cast<T>(-1); // The optimal length of the array work
ValueType rwork_opt =
static_cast<ValueType>(-1); // The optimal length of the array rwork
Copy link
Contributor

Choose a reason for hiding this comment

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

可以using ValueType = math::Real<T>;,这样模板里面不用传ValueType

Copy link
Contributor

@Xreki Xreki left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

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

LGTM

@Xreki Xreki merged commit 7ff226f into PaddlePaddle:develop Sep 26, 2021
Zjq9409 added a commit to Zjq9409/Paddle that referenced this pull request Sep 26, 2021
lanxianghit pushed a commit that referenced this pull request Sep 26, 2021
) (#36091)

cherry-pick #35916,CPU前向计算将Eigen替换为Lapack,修改linalg暴露规则
AnnaTrainingG pushed a commit to AnnaTrainingG/Paddle that referenced this pull request Sep 29, 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.

3 participants