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

CINN sub graph test - 6 #61276

Merged
merged 3 commits into from
Feb 2, 2024
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
4 changes: 2 additions & 2 deletions test/ir/pir/cinn/sub_graphs/test_sub_graph_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def forward(
var_9 = var_8.reshape((0, 16, 49))
var_10 = paddle.tensor.linalg.transpose(var_1, perm=[0, 1, 3, 2])
var_11 = paddle.tensor.linalg.matmul(var_5, var_10)
var_12 = var_11.__mul__(0.25)
var_13 = var_12.__add__(var_9)
var_12 = var_11 * 0.25
var_13 = var_12 + var_9
var_14 = paddle.nn.functional.activation.softmax(var_13)
var_15 = paddle.tensor.linalg.matmul(var_14, var_2)
var_16 = paddle.tensor.linalg.transpose(var_15, perm=[0, 2, 1, 3])
Expand Down
80 changes: 80 additions & 0 deletions test/ir/pir/cinn/sub_graphs/test_sub_graph_31.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# repo: PaddleClas
# model: ppcls^configs^ImageNet^DPN^DPN98
# api:paddle.tensor.manipulation.split||api:paddle.tensor.math.add||api:paddle.tensor.manipulation.concat
import unittest

import numpy as np

import paddle


class LayerCase(paddle.nn.Layer):
def __init__(self):
super().__init__()

def forward(
self,
var_0, # (shape: [22, 1056, 14, 14], dtype: paddle.float32, stop_gradient: False)
var_1, # (shape: [22, 1024, 14, 14], dtype: paddle.float32, stop_gradient: False)
var_2, # (shape: [22, 288, 14, 14], dtype: paddle.float32, stop_gradient: False)
):
var_3, var_4 = paddle.tensor.manipulation.split(
var_0, num_or_sections=[1024, 32], axis=1
)
var_5 = paddle.tensor.math.add(x=var_1, y=var_3)
var_6 = paddle.tensor.manipulation.concat([var_2, var_4], axis=1)
return var_5, var_6


class TestLayer(unittest.TestCase):
def setUp(self):
self.inputs = (
paddle.rand(shape=[22, 1056, 14, 14], dtype=paddle.float32),
paddle.rand(shape=[22, 1024, 14, 14], dtype=paddle.float32),
paddle.rand(shape=[22, 288, 14, 14], dtype=paddle.float32),
)
self.net = LayerCase()

def train(self, net, to_static, with_prim=False, with_cinn=False):
if to_static:
paddle.set_flags({'FLAGS_prim_all': with_prim})
if with_cinn:
build_strategy = paddle.static.BuildStrategy()
build_strategy.build_cinn_pass = True
net = paddle.jit.to_static(
net, build_strategy=build_strategy, full_graph=True
)
else:
net = paddle.jit.to_static(net, full_graph=True)
paddle.seed(123)
outs = net(*self.inputs)
return outs

# NOTE prim + cinn lead to error
def test_ast_prim_cinn(self):
st_out = self.train(self.net, to_static=True)
cinn_out = self.train(
self.net, to_static=True, with_prim=True, with_cinn=False
)
for st, cinn in zip(
paddle.utils.flatten(st_out), paddle.utils.flatten(cinn_out)
):
np.testing.assert_allclose(st.numpy(), cinn.numpy(), atol=1e-8)


if __name__ == '__main__':
unittest.main()
79 changes: 79 additions & 0 deletions test/ir/pir/cinn/sub_graphs/test_sub_graph_32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# repo: PaddleClas
# model: ppcls^configs^ImageNet^ResNeSt^ResNeSt50
# api:paddle.tensor.manipulation.reshape||api:paddle.tensor.linalg.transpose||api:paddle.nn.functional.activation.softmax||api:paddle.tensor.manipulation.reshape
import unittest

import numpy as np

import paddle


class LayerCase(paddle.nn.Layer):
def __init__(self):
super().__init__()

def forward(
self,
var_0, # (shape: [22, 1024, 1, 1], dtype: paddle.float32, stop_gradient: True)
):
var_1 = paddle.tensor.manipulation.reshape(
x=var_0, shape=[22, 1, 2, 512]
)
var_2 = paddle.tensor.linalg.transpose(x=var_1, perm=[0, 2, 1, 3])
var_3 = paddle.nn.functional.activation.softmax(var_2, axis=1)
var_4 = paddle.tensor.manipulation.reshape(
x=var_3, shape=[22, 1024, 1, 1]
)
return var_4


class TestLayer(unittest.TestCase):
def setUp(self):
self.inputs = (
paddle.rand(shape=[22, 1024, 1, 1], dtype=paddle.float32),
)
self.net = LayerCase()

def train(self, net, to_static, with_prim=False, with_cinn=False):
if to_static:
paddle.set_flags({'FLAGS_prim_all': with_prim})
if with_cinn:
build_strategy = paddle.static.BuildStrategy()
build_strategy.build_cinn_pass = True
net = paddle.jit.to_static(
net, build_strategy=build_strategy, full_graph=True
)
else:
net = paddle.jit.to_static(net, full_graph=True)
paddle.seed(123)
outs = net(*self.inputs)
return outs

# NOTE prim + cinn lead to error
def test_ast_prim_cinn(self):
st_out = self.train(self.net, to_static=True)
cinn_out = self.train(
self.net, to_static=True, with_prim=True, with_cinn=False
)
for st, cinn in zip(
paddle.utils.flatten(st_out), paddle.utils.flatten(cinn_out)
):
np.testing.assert_allclose(st.numpy(), cinn.numpy(), atol=1e-8)


if __name__ == '__main__':
unittest.main()
102 changes: 102 additions & 0 deletions test/ir/pir/cinn/sub_graphs/test_sub_graph_33.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# repo: PaddleClas
# model: ppcls^configs^ImageNet^RedNet^RedNet38
# api:paddle.nn.functional.conv._conv_nd||method:reshape||method:unsqueeze||api:paddle.nn.functional.common.unfold||method:reshape||method:__mul__||method:sum||method:reshape
import unittest

import numpy as np

import paddle


class LayerCase(paddle.nn.Layer):
def __init__(self):
super().__init__()
self.parameter_0 = self.create_parameter(
shape=[784],
dtype=paddle.float32,
)
self.parameter_1 = self.create_parameter(
shape=[784, 64, 1, 1],
dtype=paddle.float32,
)

def forward(
self,
var_0, # (shape: [10, 64, 14, 14], dtype: paddle.float32, stop_gradient: False)
var_1, # (shape: [10, 256, 14, 14], dtype: paddle.float32, stop_gradient: False)
):
var_2 = paddle.nn.functional.conv._conv_nd(
var_0,
self.parameter_1,
bias=self.parameter_0,
stride=[1, 1],
padding=[0, 0],
padding_algorithm='EXPLICIT',
dilation=[1, 1],
groups=1,
data_format='NCHW',
channel_dim=1,
op_type='conv2d',
use_cudnn=True,
)
var_3 = var_2.reshape((10, 16, 49, 14, 14))
var_4 = var_3.unsqueeze(2)
var_5 = paddle.nn.functional.common.unfold(var_1, 7, 1, 3, 1)
var_6 = var_5.reshape((10, 16, 16, 49, 14, 14))
var_7 = var_4 * var_6
var_8 = var_7.sum(axis=3)
var_9 = var_8.reshape((10, 256, 14, 14))
return var_9


class TestLayer(unittest.TestCase):
def setUp(self):
self.inputs = (
paddle.rand(shape=[10, 64, 14, 14], dtype=paddle.float32),
paddle.rand(shape=[10, 256, 14, 14], dtype=paddle.float32),
)
self.net = LayerCase()

def train(self, net, to_static, with_prim=False, with_cinn=False):
if to_static:
paddle.set_flags({'FLAGS_prim_all': with_prim})
if with_cinn:
build_strategy = paddle.static.BuildStrategy()
build_strategy.build_cinn_pass = True
net = paddle.jit.to_static(
net, build_strategy=build_strategy, full_graph=True
)
else:
net = paddle.jit.to_static(net, full_graph=True)
paddle.seed(123)
outs = net(*self.inputs)
return outs

# NOTE prim + cinn lead to error
def test_ast_prim_cinn(self):
st_out = self.train(self.net, to_static=True)
cinn_out = self.train(
self.net, to_static=True, with_prim=True, with_cinn=False
)
for st, cinn in zip(
paddle.utils.flatten(st_out), paddle.utils.flatten(cinn_out)
):
np.testing.assert_allclose(st.numpy(), cinn.numpy(), atol=1e-8)


if __name__ == '__main__':
unittest.main()
76 changes: 76 additions & 0 deletions test/ir/pir/cinn/sub_graphs/test_sub_graph_34.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# repo: PaddleClas
# model: ppcls^configs^ImageNet^HRNet^HRNet_W32_C
# api:paddle.nn.functional.common.upsample||api:paddle.tensor.math.add
import unittest

import numpy as np

import paddle


class LayerCase(paddle.nn.Layer):
def __init__(self):
super().__init__()

def forward(
self,
var_0, # (shape: [10, 32, 14, 14], dtype: paddle.float32, stop_gradient: False)
var_1, # (shape: [10, 32, 56, 56], dtype: paddle.float32, stop_gradient: False)
):
var_2 = paddle.nn.functional.common.upsample(
var_0, scale_factor=4, mode='nearest'
)
var_3 = paddle.tensor.math.add(x=var_1, y=var_2)
return var_2, var_3


class TestLayer(unittest.TestCase):
def setUp(self):
self.inputs = (
paddle.rand(shape=[10, 32, 14, 14], dtype=paddle.float32),
paddle.rand(shape=[10, 32, 56, 56], dtype=paddle.float32),
)
self.net = LayerCase()

def train(self, net, to_static, with_prim=False, with_cinn=False):
if to_static:
paddle.set_flags({'FLAGS_prim_all': with_prim})
if with_cinn:
build_strategy = paddle.static.BuildStrategy()
build_strategy.build_cinn_pass = True
net = paddle.jit.to_static(
net, build_strategy=build_strategy, full_graph=True
)
else:
net = paddle.jit.to_static(net, full_graph=True)
paddle.seed(123)
outs = net(*self.inputs)
return outs

def test_ast_prim_cinn(self):
st_out = self.train(self.net, to_static=True)
cinn_out = self.train(
self.net, to_static=True, with_prim=True, with_cinn=True
)
for st, cinn in zip(
paddle.utils.flatten(st_out), paddle.utils.flatten(cinn_out)
):
np.testing.assert_allclose(st.numpy(), cinn.numpy(), atol=1e-8)


if __name__ == '__main__':
unittest.main()
Loading