!34973 Add st for fallback constant control flow with 230,231,232

Merge pull request !34973 from huanghui/control-for-fallback
This commit is contained in:
i-robot 2022-05-29 12:38:51 +00:00 committed by Gitee
commit abccd6e0e1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 439 additions and 0 deletions

View File

@ -0,0 +1,147 @@
# Copyright 2022 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
""" test graph fallback control flow."""
import pytest
import numpy as np
from mindspore import Tensor, ms_function, context
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_if_1():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2301():
x = Tensor([1])
y = Tensor([2])
if x > y:
y = y * x
for _ in range(2):
y = y + 1
x = x + Tensor([0])
z = Tensor([7]) + y
while y > x and x < z:
y -= x
z = z + y
return y + z
res = func2301()
assert res == 11
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_if_2():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2302():
x = Tensor([1])
y = Tensor([2])
if x > y:
y = y * x
else:
x = x * y
for _ in range(2):
y = y + Tensor(np.array([0]))
x = x + Tensor([0])
z = Tensor([7]) - y
while x < z:
y += x
z = z - y
return x, y, z
res_x, res_y, res_z = func2302()
assert res_x == 2
assert res_y == 4
assert res_z == 1
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_if_3():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2303():
x = np.array([3, 2])
y = Tensor(np.array([3, 2]))
if x[0] > x[1]:
for i in [1, 1, 1]:
x = x + np.array([i, i])
else:
x -= 4
while (y >= 0).all():
y -= Tensor(x[0])
return y
res = func2303()
assert (res.asnumpy() == [-3, -4]).all()
@pytest.mark.skip(reason='Not support graph fallback feature yet')
def test_while_after_for_in_if_4():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2304():
x = [3, 2]
y = [1, 2, 3, 4]
if x[0] > x[1]:
x[0] += 3
x[1] += 3
for i in y:
if not i == 1:
break
x[1] += i
x = np.array(x)
z = int(x[1])
while len(y) < 5:
y.append(z)
return Tensor(y)
res = func2304()
assert (res.asnumpy() == [1, 2, 3, 4, 6]).all()

View File

@ -0,0 +1,142 @@
# Copyright 2022 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
""" test graph fallback control flow."""
import pytest
import numpy as np
from mindspore import Tensor, ms_function, context
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_while_1():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2311():
x = Tensor([2])
y = Tensor([0])
k = 1
while y < Tensor([3]):
for _ in range(3):
y = y + Tensor([k])
z = y + x
while y > x and x < z:
y -= x
z = z + y
return z
res = func2311()
assert res == 6
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_while_2():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2312():
x = Tensor([2])
y = Tensor([2])
while y < Tensor([5]) and x > Tensor([1]):
x -= 1
for _ in range(3):
y = y + Tensor([1])
while x < y:
y -= x
z = Tensor([-1]) - y
return z
res = func2312()
assert res == -2
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_while_3():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2313():
x = [1, 2, 3, 4]
y = Tensor([8])
z = 2
while Tensor([sum(x)]) > y:
for _ in range(1):
x.append(z)
y = Tensor([18])
while y >= 0:
y -= Tensor(np.array([x[0]]))
return Tensor(np.array(x)), y
res_x, res_y = func2313()
assert (res_x.asnumpy() == [1, 2, 3, 4, 2]).all()
assert res_y == -1
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_while_4():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2314():
x = Tensor([1])
y = Tensor([2])
z = []
while max(x, y) == Tensor([2]):
y = y + min(x, y)
for _ in range(3):
z.append(Tensor([2]))
i = 0
while i < len(z):
x = x * z[i]
i = i + 1
return x
res = func2314()
assert res == 8

View File

@ -0,0 +1,150 @@
# Copyright 2022 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
""" test graph fallback control flow."""
import pytest
import numpy as np
import mindspore
from mindspore import Tensor, ms_function, context
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_for_1():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2321():
x = Tensor([0])
y = np.array([1])
for _ in range(2):
for _ in range(2):
x = x + 1
i = np.array([1])
while i < 3:
y = y + i
i += 1
return x + Tensor(y, dtype=mindspore.int64)
res = func2321()
assert res == 8
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_for_2():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2322():
x = Tensor([0])
for i in range(3):
x = x + Tensor([i + 1])
k = (Tensor(1), Tensor(1), Tensor(1))
for j in zip(k):
x = x + j
y = Tensor([0])
t = Tensor(np.array([1, 2, 3]))
i = 0
while i < 3:
y = y + t[i]
i += 1
return x, y
res_x, res_y = func2322()
assert res_x == 15
assert res_y == 6
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_for_3():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2323():
y = [5, 6, 7]
for _ in (0, 1, 2):
for j in range(3):
y[j] = y[j] - 1
y = np.array(y)
z = Tensor(y)
out = 0
i = 0
while i < len(z):
out = out + z[i]
i += 1
return out
res = func2323()
assert res == 9
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_while_after_for_in_for_4():
"""
Feature: JIT Fallback
Description: Test fallback with control flow.
Expectation: No exception.
"""
@ms_function
def func2324():
x = Tensor([0])
for i in range(2):
for j in range(2):
x = x - Tensor([i + j])
z = [np.array([0]), np.array([2]), np.array([2])]
i = 0
while i < len(z):
x = x + Tensor(z[i])
i += 1
return x
res = func2324()
assert res == 0