Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ignore =
E741
per-file-ignores =
# These files need tabs for testing.
test/dygraph_to_static/test_legacy_error.py:E101
test/dygraph_to_static/test_error.py:E101

# Ignore compare with True in sot unittest
test/sot/test_dup_top.py:E712
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ repos:
# Exclude some unit test files that require tabs.
exclude: |
(?x)^(
test/dygraph_to_static/test_legacy_error.py
test/dygraph_to_static/test_error.py
)$
- repo: local
hooks:
Expand Down
37 changes: 34 additions & 3 deletions paddle/fluid/pybind/pir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ void BindOperation(py::module *m) {
.def("as_while_op",
[](Operation &self) { return PyWhileOp(self.dyn_cast<WhileOp>()); })
.def("__repr__",

[](Operation &self) {
std::ostringstream print_stream;
print_stream << "Operation(";
Expand All @@ -644,9 +645,39 @@ void BindOperation(py::module *m) {
return ApiBuilder::Instance().GetBuilder()->Insert(op);
},
return_value_policy::reference)
.def("move_before", [](Operation &self, Operation &other) {
self.MoveTo(other.GetParent(), Block::Iterator{other});
});
.def("move_before",
[](Operation &self, Operation &other) {
self.MoveTo(other.GetParent(), Block::Iterator{other});
})
.def_property(
"callstack",
[](Operation &self) -> py::list {
py::list callstack_list;
pir::Attribute op_callstack = self.attribute<pir::Attribute>(
paddle::framework::OpProtoAndCheckerMaker::
OpCreationCallstackAttrName());
auto op_callstack_infos = PADDLE_GET_CONST(
std::vector<std::string>,
paddle::dialect::GetAttributeData(op_callstack));
for (auto &op_callstack_info : op_callstack_infos) {
callstack_list.append(op_callstack_info);
}
return callstack_list;
},
[](Operation &self,
const std::vector<std::string> &callstack) -> void {
std::vector<pir::Attribute> op_callstack_infos;
for (auto str : callstack) {
op_callstack_infos.push_back(
pir::StrAttribute::get(pir::IrContext::Instance(), str));
}

self.set_attribute(
paddle::framework::OpProtoAndCheckerMaker::
OpCreationCallstackAttrName(),
pir::ArrayAttribute::get(pir::IrContext::Instance(),
op_callstack_infos));
});
py::class_<Operation::BlockContainer> block_container(
*m, "Operation_BlockContainer", R"DOC(
The Operation_BlockContainer only use to walk all blocks in the operation.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ known-first-party = ["paddle"]

[tool.ruff.lint.per-file-ignores]
# These files need tabs for testing.
"test/dygraph_to_static/test_legacy_error.py" = ["E101", "W191"]
Copy link
Member

Choose a reason for hiding this comment

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

我试了一下 ruff 似乎两个都不需要 ignore

Copy link
Member

Choose a reason for hiding this comment

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

那可能是 ruff 和 flake8 实现不一致了

"test/dygraph_to_static/test_error.py" = ["E101", "W191"]
# Ignore compare with True in sot unittest
"test/sot/test_dup_top.py" = ["E712"]
# Ignore undefined variables in CMake config and some dygraph_to_static tests
Expand Down
44 changes: 28 additions & 16 deletions python/paddle/jit/dy2static/origin_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from collections.abc import Sequence

from paddle.base import core
from paddle.base.framework import Program
from paddle.framework import use_pir_api
from paddle.utils import gast

from .utils import ORIGIN_INFO
Expand Down Expand Up @@ -269,8 +269,6 @@ def update_op_callstack_with_origin_info(program):
Replaces op callstack information about transformed static code with original dygraph code.
"""

assert isinstance(program, Program)

def get_new_op_callstack(callstack):
"""
An example of callstack:
Expand Down Expand Up @@ -306,21 +304,35 @@ def get_new_op_callstack(callstack):

return callstack

def get_all_pir_block_ops(block):
ops = []
for op in block.ops:
ops.append(op)
for sub_block in op.blocks():
ops += get_all_pir_block_ops(sub_block)
return ops

op_maker = core.op_proto_and_checker_maker
callstack_var_name = op_maker.kOpCreationCallstackAttrName()

for block in program.blocks:
for i, op in enumerate(block.ops):
if use_pir_api():
global_block = program.global_block()
ops = get_all_pir_block_ops(global_block)
for op in ops:
if op.has_attr(callstack_var_name):
callstack = op.attr(callstack_var_name)

callstack = get_new_op_callstack(callstack)

try:
# (@xiongkun) In 2-order derivative for paddle science, there may exists `pow_grad`
# which has op_proto == nullptr and causes _set_attr failed. so we add a try...except.
op._set_attr(callstack_var_name, callstack)
except:
pass

op.callstack = get_new_op_callstack(op.callstack)
else:
for block in program.blocks:
for i, op in enumerate(block.ops):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for i, op in enumerate(block.ops):
for op in block.ops:

可以不需要使用enumerate

Copy link
Member

Choose a reason for hiding this comment

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

老代码加缩进而已,反正这个分支以后也会删

if op.has_attr(callstack_var_name):
callstack = op.attr(callstack_var_name)

callstack = get_new_op_callstack(callstack)

try:
# (@xiongkun) In 2-order derivative for paddle science, there may exists `pow_grad`
# which has op_proto == nullptr and causes _set_attr failed. so we add a try...except.
op._set_attr(callstack_var_name, callstack)
except:
pass
return program
3 changes: 1 addition & 2 deletions python/paddle/jit/dy2static/program_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,8 +1258,7 @@ def pir_from_func_spec(
if need_wrap_into_list:
outputs = [outputs]

# TODO(@xiongkun): support op call stack in new ir?
# main_program = update_op_callstack_with_origin_info(main_program)
main_program = update_op_callstack_with_origin_info(main_program)

return ConcreteProgram(
inputs=static_inputs,
Expand Down
9 changes: 9 additions & 0 deletions test/dygraph_to_static/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ foreach(ITEST ${LEGACY_ONLY_TEST_FILES})
STATUS "PT Disabled OpTest: not found ${ITEST} in dygraph_to_static")
endif()
endforeach()

# PIR only tests for dygraph_to_static
set(PIR_ONLY_TEST_FILES test_error)
foreach(ITEST ${PIR_ONLY_TEST_FILES})
if(TEST ${ITEST})
set_tests_properties(${ITEST} PROPERTIES ENVIRONMENT
"FLAGS_enable_pir_api=True")
endif()
endforeach()
2 changes: 2 additions & 0 deletions test/dygraph_to_static/check_approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def __init__(self, start: Location, end: Location):
UseToStaticAsDecoratorDiagnostic: [
"test_rollback.py",
"test_legacy_error.py",
"test_error.py",
"test_op_attr.py",
"test_se_resnet.py",
"test_lac.py",
Expand All @@ -142,6 +143,7 @@ def __init__(self, start: Location, end: Location):
"test_eval_frame.py",
"test_ignore_module.py",
"test_legacy_error.py",
"test_error.py",
"test_local_cast.py",
"test_ordered_set.py",
"test_origin_info.py",
Expand Down
Loading