CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[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
[CINN]add CacheGradOpSymbolicShapeInterface #65343
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
…ckward-cache-interface
@@ -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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注释可以删掉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
namespace paddle::dialect { | ||
using CacheGradOpSymbolicShapeInterface = | ||
pir::CacheGradOpSymbolicShapeInterface; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个inferface是否有必要包一层paddle::dialect?
There was a problem hiding this comment.
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中了。
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()); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是不可以直接拆成两个函数?
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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&
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里需要调用非const函数,和其它接口保持一致,所以做此折中
} | ||
} | ||
|
||
void CacheSelfSymbolicShape( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个Self语义不明
There was a problem hiding this comment.
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]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
到这里已经是三层if了。
既然从第二层if到这里的逻辑都只是check,那就专门提一个check函数。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
PR Category
CINN
PR Types
New features
Description
Pcard-67164
This PR adds CacheGradOpSymbolicShapeInterface.