Skip to content

[CINN]add CacheGradOpSymbolicShapeInterface #65343

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

Merged

Conversation

Hongqing-work
Copy link
Contributor

PR Category

CINN

PR Types

New features

Description

Pcard-67164
This PR adds CacheGradOpSymbolicShapeInterface.

Copy link

paddle-bot bot commented Jun 20, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@@ -113,7 +125,7 @@ void ApplyCinnPreprocessPass(
bool has_dynamic_shape = HasDynamicShape(*program);

if (has_dynamic_shape) {
pass_manager->AddPass(pir::CreateShapeOptimizationPass());
// pass_manager->AddPass(pir::CreateShapeOptimizationPass());
Copy link
Contributor

Choose a reason for hiding this comment

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

注释可以删掉

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

Comment on lines +19 to +21
namespace paddle::dialect {
using CacheGradOpSymbolicShapeInterface =
pir::CacheGradOpSymbolicShapeInterface;
Copy link
Contributor

Choose a reason for hiding this comment

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

这个inferface是否有必要包一层paddle::dialect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CacheGradOpSymbolicShapeInterface属于op接口最好放在paddle::dialect,但是和符号推导接口类似,由于pir目录下的shape_optimization_pass需要使用,而pir不能引用paddle::dialect,所以放在pir中了。

Comment on lines 309 to 353
const auto& CacheSelfSymbolicShape = [&]() {
std::vector<symbol::ShapeOrDataDimExprs> result_shape_or_data;
for (auto& result : op->results()) {
result_shape_or_data.emplace_back(
infer_context->GetShapeOrDataForValue(result));
}
if (infer_context->GetOpInferSymbolicShapeCache(op_infer_cache_key)
.has_value()) {
std::vector<symbol::ShapeOrDataDimExprs> cached_result_shape_or_data =
infer_context->GetOpInferSymbolicShapeCache(op_infer_cache_key)
.value();
// check whether the result_shape_or_data is consistent with the cached
// TODO(Hongqing-work): delete check and only set cache for op without
// InferSymbolicShapeInterface after fixing all warnings.
if (cached_result_shape_or_data.size() != result_shape_or_data.size()) {
LOG(WARNING) << "cached shape is not consistent with real shape";
} else {
for (uint32_t i = 0; i < op->num_results(); ++i) {
if (cached_result_shape_or_data[i] != result_shape_or_data[i]) {
LOG(WARNING) << "cached shape is not consistent with real shape";
VLOG(3) << "InferSymbolicShapeCacheKey is: " << op_infer_cache_key;
VLOG(3) << "cached shape is: " << cached_result_shape_or_data[i];
VLOG(3) << "real shape is: " << result_shape_or_data[i];
}
}
}
} else {
infer_context->SetOpInferSymbolicShapeCache(op_infer_cache_key,
result_shape_or_data);
}
};

const auto& CacheGradOpSymbolicShape = [&]() {
auto cache_grad_op_symbolic_shape_interface =
op->dyn_cast<pir::CacheGradOpSymbolicShapeInterface>();
if (cache_grad_op_symbolic_shape_interface) {
VLOG(3) << "CacheGradOpSymbolicShape for: " << op->name();
PADDLE_ENFORCE_EQ(
cache_grad_op_symbolic_shape_interface.CacheGradOpSymbolicShape(
infer_context),
true,
"CacheGradOpSymbolicShape for %s failed.",
op->name());
}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

是不可以直接拆成两个函数?

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 InferSymExprForOp(Operation* op,
InferSymbolicShapeContext* infer_context,
const InferSymbolicShapeCacheKey& op_infer_cache_key) {
auto infer_symbolic_shape_interface =
Copy link
Contributor

Choose a reason for hiding this comment

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

const auto&

不要养成使用auto的习惯。最多能用auto*和const auto&

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里需要调用非const函数,和其它接口保持一致,所以做此折中

}
}

void CacheSelfSymbolicShape(
Copy link
Contributor

Choose a reason for hiding this comment

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

这个Self语义不明

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改为forward和backward

LOG(WARNING) << "cached shape is not consistent with real shape";
} else {
for (uint32_t i = 0; i < op->num_results(); ++i) {
if (cached_result_shape_or_data[i] != result_shape_or_data[i]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

到这里已经是三层if了。
既然从第二层if到这里的逻辑都只是check,那就专门提一个check函数。

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

@Hongqing-work Hongqing-work merged commit a93c291 into PaddlePaddle:develop Jun 27, 2024
@Hongqing-work Hongqing-work deleted the add-backward-cache-interface branch July 18, 2024 08:56
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.

4 participants