forked from mindspore-Ecosystem/mindspore
363 lines
8.6 KiB
Python
363 lines
8.6 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
|
|
|
|
|
|
def test_tuple_of_tensor():
|
|
"""
|
|
Feature: JIT Fallback
|
|
Description: Test tuple of tensor in graph mode.
|
|
Expectation: No exception.
|
|
"""
|
|
print(use_tuple_of_tensor())
|
|
|
|
|
|
@ms_function
|
|
def use_list_of_tensor():
|
|
me_x = [Tensor(1), Tensor(1)]
|
|
return me_x
|
|
|
|
|
|
def test_list_of_tensor():
|
|
"""
|
|
Feature: JIT Fallback
|
|
Description: Test list of tensor in graph mode.
|
|
Expectation: No exception.
|
|
"""
|
|
print(use_list_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)
|
|
|
|
|
|
@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))
|
|
|
|
|
|
@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))
|
|
|
|
|
|
@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))
|
|
|
|
|
|
# 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
|
|
|
|
@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_binop_new_tensor():
|
|
"""
|
|
Feature: Fallback feature
|
|
Description: support binop's interpreted nodes.
|
|
Expectation: No exception.
|
|
"""
|
|
class BinOpNet(nn.Cell):
|
|
def __init__(self):
|
|
super(BinOpNet, self).__init__()
|
|
|
|
def construct(self):
|
|
np_array = np.array(9)
|
|
res = Tensor(np_array) + Tensor(np_array)
|
|
return res
|
|
|
|
net = BinOpNet()
|
|
print(net())
|
|
|
|
|
|
@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_fallback_tensor_compare():
|
|
"""
|
|
Feature: Fallback feature
|
|
Description: support compare op's interpreted nodes.
|
|
Expectation: No exception.
|
|
"""
|
|
class CompareNet(nn.Cell):
|
|
def __init__(self):
|
|
super(CompareNet, self).__init__()
|
|
|
|
def construct(self):
|
|
np_array_1 = np.array(1)
|
|
np_array_2 = np.array(2)
|
|
res = Tensor(np_array_1) < Tensor(np_array_2)
|
|
return res
|
|
|
|
compare_net = CompareNet()
|
|
print(compare_net())
|
|
|
|
|
|
@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_fallback_tensor_not():
|
|
"""
|
|
Feature: Fallback feature
|
|
Description: support bool op's interpreted nodes.
|
|
Expectation: No exception.
|
|
"""
|
|
class NotNet(nn.Cell):
|
|
def __init__(self):
|
|
super(NotNet, self).__init__()
|
|
|
|
def construct(self):
|
|
np_array_1 = np.array(True, dtype=np.bool_)
|
|
res = not Tensor(np_array_1)
|
|
return res
|
|
|
|
net = NotNet()
|
|
res = net()
|
|
print("res:", res)
|
|
|
|
|
|
@pytest.mark.skip(reason='Not support graph fallback feature yet')
|
|
def test_fallback_tensor_and():
|
|
"""
|
|
Feature: Fallback feature
|
|
Description: support bool op's interpreted nodes.
|
|
Expectation: No exception.
|
|
"""
|
|
class AndNet(nn.Cell):
|
|
def __init__(self):
|
|
super(AndNet, self).__init__()
|
|
|
|
def construct(self):
|
|
np_array_1 = np.array(True, dtype=np.bool_)
|
|
np_array_2 = np.array(False, dtype=np.bool_)
|
|
res = Tensor(np_array_1) and Tensor(np_array_2)
|
|
return res
|
|
|
|
net = AndNet()
|
|
res = net()
|
|
print("res:", res)
|
|
|
|
|
|
@pytest.mark.skip(reason='Not support graph fallback feature yet')
|
|
def test_fallback_tensor_or():
|
|
"""
|
|
Feature: Fallback feature
|
|
Description: support bool op's interpreted nodes.
|
|
Expectation: No exception.
|
|
"""
|
|
class OrNet(nn.Cell):
|
|
def __init__(self):
|
|
super(OrNet, self).__init__()
|
|
|
|
def construct(self):
|
|
np_array_1 = np.array(True, dtype=np.bool_)
|
|
np_array_2 = np.array(False, dtype=np.bool_)
|
|
res = Tensor(np_array_1) or Tensor(np_array_2)
|
|
return res
|
|
|
|
net = OrNet()
|
|
res = net()
|
|
print("res:", res)
|