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

Add flatten composite rule #50672

Merged
merged 20 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ba39d24
Add flatten composite rule
xysheng-baidu Feb 20, 2023
a80f705
get the right xshape and pass func test
xysheng-baidu Feb 21, 2023
4943544
add cinn unit test
xysheng-baidu Feb 21, 2023
d9ffe5e
Remove cinn test, wait for it to be added after repair
xysheng-baidu Feb 21, 2023
d3f8af7
add comp test to test_flatten_contiguous_range_op.py
xysheng-baidu Feb 22, 2023
83deca7
Merge branch 'PaddlePaddle:develop' into composite_rule_flatten
xysheng-baidu Feb 22, 2023
4e43a73
remove func test on composite_ops
xysheng-baidu Feb 22, 2023
3c906bb
Add comments to maybe_wrap_dim func
xysheng-baidu Feb 22, 2023
c569f59
remove commented code
xysheng-baidu Feb 22, 2023
48547ab
fix the problem with 0D tensor case
xysheng-baidu Feb 22, 2023
d384698
add flatten split rule comment
xysheng-baidu Feb 22, 2023
c11d340
fix conflicts
xysheng-baidu Feb 22, 2023
28af93d
Merge branch 'PaddlePaddle:develop' into composite_rule_flatten
xysheng-baidu Feb 23, 2023
e09e5f1
fix syntax issues
xysheng-baidu Feb 23, 2023
bb4c836
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
xysheng-baidu Feb 24, 2023
70d7453
block flatten on resnet_prim_cinn
xysheng-baidu Feb 24, 2023
aebfa82
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
xysheng-baidu Feb 24, 2023
95f333b
remove maybe_wrap_dim func
xysheng-baidu Feb 24, 2023
4522f26
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
xysheng-baidu Feb 27, 2023
fe21b3a
Use none instead od xshape
xysheng-baidu Feb 27, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def test_cinn(self):
not paddle.is_compiled_with_cinn(), "padle is not compiled with CINN"
)
def test_prim_cinn(self):
core._set_prim_forward_blacklist("flatten_contiguous_range")
dy2st_prim_cinn = train(
to_static=True, enable_prim=True, enable_cinn=True
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ def setUp(self):
self.python_api = paddle.flatten
self.python_out_sig = ["Out"]
self.op_type = "flatten_contiguous_range"
self.prim_op_type = "comp"
self.start_axis = 0
self.stop_axis = -1
self.init_test_case()
self.inputs = {"X": np.random.random(self.in_shape).astype("float64")}
self.init_attrs()
self.enable_cinn = False
Copy link
Contributor

Choose a reason for hiding this comment

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

子类的CINN测试都过不了吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

是的,没有能过的。

self.outputs = {
"Out": self.inputs["X"].reshape(self.new_shape),
"XShape": np.random.random(self.in_shape).astype("float32"),
}

def test_check_output(self):
self.check_output(no_check_set=["XShape"], check_eager=True)
self.check_output(
no_check_set=["XShape"], check_eager=True, check_prim=True
)

def test_check_grad(self):
self.check_grad(["X"], "Out", check_eager=True)
self.check_grad(["X"], "Out", check_eager=True, check_prim=True)

def init_test_case(self):
self.in_shape = (3, 2, 5, 4)
Expand Down
26 changes: 26 additions & 0 deletions python/paddle/incubate/autograd/composite_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,32 @@ def mean_composite(x, axis, keepdim):
return divide(sum_x, norm)


@REGISTER_COMPOSITE('flatten_contiguous_range')
def flatten_contiguous_range_composite(x, start_axis, stop_axis):
"""
define composite rule of op flatten, flatten_contiguous_range -> flatten.
CINN doesn't need xshape for backward pass, return none instead of xshape.
shape_out is the parameter of reshape, get from start_axis and stop_axis.
out = reshape(x, shape=shape_out), xshape
"""
shape_in = x.shape
start_dim = start_axis if len(shape_in) != 0 else 0
end_dim = stop_axis if len(shape_in) != 0 else 0
assert start_dim <= end_dim
if len(shape_in) == 0 or start_dim == end_dim:
return reshape(x, shape=shape_in), None
slice_numel = 1
for i in range(start_dim, end_dim + 1):
slice_numel *= shape_in[i]
shape_out = []
for i in range(start_dim):
shape_out.append(shape_in[i])
shape_out.append(slice_numel)
for i in range(end_dim + 1, len(shape_in)):
shape_out.append(shape_in[i])
return reshape(x, shape=shape_out), None


@REGISTER_COMPOSITE('dropout')
def dropout_composite(x, seed_tensor, p, is_test, mode, seed, fix_seed):
"""define composite rule of op dropout.
Expand Down