CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[PIR] refactor pir GetValueName - part 1 #66363
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
[PIR] refactor pir GetValueName - part 1 #66363
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
paddle/fluid/pybind/eager_utils.cc
Outdated
@@ -1988,7 +1988,7 @@ paddle::Tensor CreateTensorFromValue(const pir::Value& value) { | |||
auto dims = phi::vectorize(GetValueDims(value)); | |||
auto ddims = phi::make_ddim(dims); | |||
if (HasValueName(value)) { | |||
tensor.set_name(GetValueName(value)); | |||
tensor.set_name(GetValueName(value)[0]); |
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.
注意封装,[0]
这个 magic operation 可以封装在 GetValueFirstName
,该函数需要检查 size 是否为空,PADDLE_ENFORCE 下
paddle/fluid/pybind/pir.cc
Outdated
@@ -264,7 +265,8 @@ void SetValueName(Value value, const std::string name) { | |||
|
|||
bool IsUsedByShadowOutput(const pir::Value &value) { |
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.
还有使用处吗?
@@ -296,35 +298,50 @@ std::optional<std::string> GetValueInputName(Value value) { | |||
} else { | |||
name = "arg_" + std::to_string(block_arg.index()); |
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.
BlockArgument 分支放在最前面,后面放一系列 OP
paddle/fluid/pybind/pir.cc
Outdated
if (input_name.has_value()) return input_name.value(); | ||
if (input_name.has_value()) { | ||
names.push_back(input_name.value()); | ||
} | ||
|
||
VLOG(5) << "GetValueInputName(value) return empty, try to " |
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.
这个 log 永远都会走到,和之前的逻辑不一致,之前是有 early return
这个 log 可以删掉
paddle/fluid/pybind/pir.cc
Outdated
if (auto names = GetValueName(input); !names.empty()) { | ||
name = names[0]; | ||
} else { | ||
name = "input_" + std::to_string(idx); | ||
} |
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-else,直接原来的逻辑就可以
if need_update:
x = updated
else:
x = default
# ->
x = default
if need_update:
x = updated
paddle/fluid/pybind/pir.cc
Outdated
if (auto names = GetValueName(self); !names.empty()) { | ||
return names[0]; | ||
} | ||
PADDLE_THROW(phi::errors::InvalidArgument( | ||
"Currently, we can only get name of Value from " | ||
"DataOp/ParameterOp/BlockArgument and ShadowOutputOp.")); |
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.
直接 GetValueFirstName
paddle/fluid/pybind/pir.cc
Outdated
PADDLE_THROW(phi::errors::InvalidArgument( | ||
"Currently, we can only get name of Value from " | ||
"DataOp/ParameterOp/BlockArgument and ShadowOutputOp.")); | ||
}, | ||
[](Value self, const std::string &name) { SetValueName(self, 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.
这里居然还有个 setter,那 SetValueName
需要保证将需要所有 name 都修改
改个名吧,SetValueAllNamesWith
GetValueName
也改成 GetValueAllNames
GetValueOutputName
-> GetValueOutputNames
除去 GetValueFirstName
再加一个 TryGetValueFirstName
,不报错,返回 optional<string>
,用于现在 HasValueName
的场景
if (HasValueName(value)) {
name = GetValueName(value);
}
// ->
if (auto maybe_value_name = TryGetValueFirstName(value)) {
name = maybe_value_name.value();
}
可以考虑现在先将相关代码放到 |
paddle/fluid/pybind/pir.cc
Outdated
if (HasValueName(v)) { | ||
shadow_output_name = GetValueName(v); | ||
if (auto names = name_analysis::TryGetValueFirstName(v); | ||
names.has_value()) { |
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.
这个 names.has_value()
是必要的么?optional 重写了 operator bool()
吧?
paddle/fluid/pybind/pir.cc
Outdated
PADDLE_ENFORCE(name.has_value(), | ||
phi::errors::InvalidArgument( | ||
"Currently, we can only get name of Value from " | ||
"DataOp/ParameterOp/BlockArgument and ShadowOutputOp.")); |
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.
ConstantOp
还有 SetParameterOp
是不是也可以加一下?
paddle/fluid/pybind/pir.cc
Outdated
if (auto names = name_analysis::GetValueAllNames(input); !names.empty()) { | ||
name = names[0]; |
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 (output_name.has_value()) all_names.append(output_name.value()); | ||
return all_names; | ||
}) | ||
.def_property_readonly("_names", |
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.
下一个 PR:
我这边会在 |
PR Category
Performance Optimization
PR Types
Not User Facing
Description
优化 GetValueName 相关签名
IsUsedByShadowOutput
-> xHasValueName
-> x (使用TryGetValueFirstName
能覆盖现有场景)SetValueName
->SetValueAllNamesWith
GetValueName
->GetValueAllNames
GetValueOutputName
->GetValueOutputNames
GetValueFirstName
(获取第一个名字)、TryGetValueFirstName
(optional 用于不需要报错和HasValueName
的场景)