mindspore2022/tests/ut/python/fallback/test_graph_fallback.py

258 lines
6.1 KiB
Python

# Copyright 2021 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 """
import pytest
import numpy as np
import mindspore.nn as nn
from mindspore import Tensor, ms_function, context
from mindspore.ops import operations as P
from mindspore.ops import functional as F
import mindspore.common.dtype as mstype
import mindspore.common._monad as monad
context.set_context(mode=context.GRAPH_MODE)
# `add_func` is defined in current file.
def add_func(x, y):
return x + y
@ms_function
def do_increment(i):
add_1 = F.partial(add_func, 1)
return add_1(i)
def test_increment():
a = do_increment(9)
assert a == 10
@ms_function
def use_monad(x, y):
res = P.Mul()(x, y)
res = F.depend(res, monad.U)
return res
def test_use_monad():
x = Tensor(1.0, mstype.float32)
y = Tensor(1.0, mstype.float32)
print(use_monad(x, y))
@ms_function
def use_tensor_with_mstype():
me_x = Tensor(1, mstype.int32)
return me_x
def test_tensor_with_mstype():
"""
Feature: JIT Fallback
Description: Test tensor with mstype in graph mode.
Expectation: No exception.
"""
print(use_tensor_with_mstype())
@ms_function
def use_tuple_of_tensor():
me_x = (Tensor(1), Tensor(1))
return me_x
@pytest.mark.skip(reason='Not support graph fallback feature yet')
def test_tuple_of_tensor():
"""
Feature: JIT Fallback
Description: Test tuple of tensor in graph mode.
Expectation: No exception.
"""
print(use_tuple_of_tensor())
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.x = Tensor([2, 3, 4])
def construct(self):
x_len = len(self.x)
for i in range(x_len):
print(i)
return x_len
def test_builtins_len():
net = Net()
net()
@ms_function
def np_fallback_func():
array_x = tuple([2, 3, 4, 5])
np_x = np.array(array_x).astype(np.float32)
me_x = Tensor(np_x)
me_x = me_x + me_x
return me_x
def test_np_fallback_func():
print(np_fallback_func())
# Test `return` interpret node.
@ms_function
def div_mod_func1():
x = 8
y = 3
a = divmod(x, y)
return Tensor(a)
def test_div_mod_func1():
print(div_mod_func1()) # (2, 2)
# Test interpret node with parameters as input.
@ms_function
def div_mod_func2(x, y):
a = divmod(x, y)
return Tensor(a)
def test_div_mod_func2_scalar():
"""
Feature: JIT Fallback
Description: Test divmod in graph.
Expectation: No exception.
"""
print(div_mod_func2(8, 3)) # (2, 2)
@pytest.mark.skip(reason='Not support in graph jit fallback feature yet')
def test_div_mod_func2_tensor():
"""
Feature: JIT Fallback
Description: Test divmod with Tensor input in graph. We'll support it in Tensor Input Fallback solution.
Expectation: Not supported exception.
"""
with pytest.raises(RuntimeError) as err:
print(div_mod_func2(Tensor(8), Tensor(3)))
assert "Not support Tensor or variable type as input during running JIT Fallback, but got" in str(err.value)
# NameError: name 'Tensor' is not defined.
@ms_function
def select_func(cond, x, y):
if isinstance(cond, (tuple, list)):
output = y
elif isinstance(cond, Tensor):
output = F.select(cond, x, y)
else:
output = x
return output
def test_select_func():
cond = Tensor([True, False])
x = Tensor([2, 3], mstype.float32)
y = Tensor([1, 2], mstype.float32)
print(select_func(cond, x, y))
# Not interpret 'Tensor'.
@ms_function
def select_func2(cond, x, y):
if isinstance(cond, (tuple, list)):
output = y
if isinstance(cond, Tensor):
output = F.select(cond, x, y)
else:
output = x
return output
def test_select_func2():
cond = Tensor([True, False])
x = Tensor([2, 3], mstype.float32)
y = Tensor([1, 2], mstype.float32)
print(select_func2(cond, x, y))
# NameError: name 'Tensor' is not defined.
@ms_function
def slice_func(a, b):
a[1:3, ::] = b
return a
def test_slice_func():
a = Tensor(np.arange(60).reshape(3, 4, 5), dtype=mstype.float32)
b = Tensor([1], dtype=mstype.float32)
print(slice_func(a, b))
@ms_function
def np_fallback_func_tensor_index(x):
array_x = tuple([2, 3, 4, 5])
np_x = np.array(array_x).astype(np.float32)
me_x = Tensor(np_x)
me_x = me_x + me_x
return me_x[x]
# NameError: name 'array_x' is not defined.
@pytest.mark.skip(reason='Not support graph fallback feature yet')
def test_np_fallback_func_tensor_index():
"""
Feature: Fallback feature: support Tensor index.
Description: Fallback feature: support Tensor index.
Expectation: Fallback feature: support Tensor index.
"""
x = Tensor(1, mstype.int32)
output = np_fallback_func_tensor_index(x)
output_expect = Tensor(6, mstype.float32)
assert output == output_expect
# EvalCNode: This may be not defined, or it can't be a operator.
@pytest.mark.skip(reason='Not support graph fallback feature yet')
def test_np_tensor_add():
"""
Feature: Fallback feature
Description: support Tensor add.
Expectation: No exception.
"""
@ms_function
def np_tensor_add():
a = Tensor(np.array(4))
b = Tensor(np.array(5))
tensor_list = [a, b]
for tensor in tensor_list:
print(tensor)
x = 6
np_x = np.array(x)
c = Tensor(np_x)
d = tensor_list[-1] + c
tensor_list.append(d)
return tensor_list
tensor_list = np_tensor_add()
print("tensor_list:", tensor_list)
assert tensor_list[-1] == 11