|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- # 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 """
- import pytest
- import numpy as np
-
- import mindspore.nn as nn
- import mindspore.common.dtype as mstype
- from mindspore import Tensor, context, ms_class, ms_function
- from . import test_graph_fallback
-
- context.set_context(mode=context.GRAPH_MODE)
-
-
- def test_fallback_self_attr():
- """
- Feature: JIT Fallback
- Description: Test self.attr in graph.
- Expectation: No exception.
- """
- class Network(nn.Cell):
- def __init__(self):
- super(Network, self).__init__()
- self.dim = 1
-
- def construct(self, x):
- batch = x.shape[0]
- one = Tensor(np.ones([batch, self.dim]), mstype.float32)
- return one * x
-
- net = Network()
- x = Tensor([1, 2], mstype.float32)
- out = net(x)
- expect = np.array([[1., 2.], [1., 2.]])
- assert np.allclose(out.asnumpy(), expect, 1.e-2, 1.e-2)
-
-
- def test_fallback_self_attr_fn():
- """
- Feature: JIT Fallback
- Description: Test self.attr in graph.
- Expectation: No exception.
- """
- class Network(nn.Cell):
- def __init__(self, fn):
- super(Network, self).__init__()
- self.fn = fn
-
- def construct(self):
- x = np.array([1, 2, 3])
- y = np.array([3, 4, 5])
- out = Tensor(self.fn(x, y))
- return out
-
- def fn(x, y):
- return x + y
-
- net = Network(fn)
- out = net()
- expect = np.array([4, 6, 8])
- assert np.all(out.asnumpy() == expect)
-
-
- def test_fallback_self_attr_attr():
- """
- Feature: JIT Fallback
- Description: Test self.attr in graph.
- Expectation: No exception.
- """
- class Network(nn.Cell):
- def __init__(self):
- super(Network, self).__init__()
- self.value = [2, 2, 3]
-
- def construct(self):
- x = np.array(self.value.count(2))
- return Tensor(x)
-
- net = Network()
- out = net()
- assert out == 2
-
-
- def test_fallback_self_method():
- """
- Feature: JIT Fallback
- Description: Test self.method in graph.
- Expectation: No exception.
- """
- class Network(nn.Cell):
- def construct(self):
- x = np.array([1, 2, 3])
- y = np.array([3, 4, 5])
- out = Tensor(self.fn(x, y))
- return out
-
- def fn(self, x, y):
- return x + y
-
- net = Network()
- out = net()
- expect = np.array([4, 6, 8])
- assert np.all(out.asnumpy() == expect)
-
-
- @pytest.mark.skip(reason='Not support in graph jit fallback feature yet')
- def test_fallback_self_method_tensor():
- """
- Feature: JIT Fallback
- Description: Test self.method in graph.
- Expectation: No exception.
- """
- class Network(nn.Cell):
- def construct(self):
- x = np.array([1, 2, 3])
- y = np.array([3, 4, 5])
- z = self.fn(x, y)
- out = Tensor(z)
- return out
-
- def fn(self, x, y):
- return x + y
-
- net = Network()
- out = net()
- print(out)
-
-
- def test_fallback_import_modules():
- """
- Feature: JIT Fallback
- Description: add_func is defined in test_graph_fallback.py
- Expectation: No exception.
- """
- @ms_function
- def use_imported_module(x, y):
- out = test_graph_fallback.add_func(x, y)
- return out
-
- x = Tensor(2, dtype=mstype.int32)
- y = Tensor(3, dtype=mstype.int32)
- out = use_imported_module(x, y)
- print(out)
-
-
- def test_fallback_class_attr():
- """
- Feature: JIT Fallback
- Description: Test user-defined class attributes in graph.
- Expectation: No exception.
- """
- @ms_class
- class InnerNet:
- def __init__(self):
- self.number = 1
-
- class Net(nn.Cell):
- def __init__(self):
- super(Net, self).__init__()
- self.inner_net = InnerNet()
-
- def construct(self):
- out = self.inner_net.number
- return out
-
- net = Net()
- out = net()
- assert out == 1
-
-
- def test_fallback_class_method():
- """
- Feature: JIT Fallback
- Description: Test user-defined class methods in graph.
- Expectation: No exception.
- """
- @ms_class
- class InnerNet:
- def __init__(self):
- self.val = 2
-
- def act(self, x, y):
- return self.val * (x + y)
-
- class Net(nn.Cell):
- def __init__(self):
- super(Net, self).__init__()
- self.inner_net = InnerNet()
-
- def construct(self):
- out = self.inner_net.act(1, 2)
- return out
-
- net = Net()
- out = net()
- assert out == 6
-
-
- def test_fallback_class_input_attr():
- """
- Feature: JIT Fallback
- Description: Test user-defined class attributes in graph.
- Expectation: No exception.
- """
- @ms_class
- class InnerNet:
- def __init__(self):
- self.number = Tensor(np.array([1, 2, 3]))
-
- class Net(nn.Cell):
- def __init__(self, net):
- super(Net, self).__init__()
- self.inner_net = net()
-
- def construct(self):
- out = self.inner_net.number
- return out
-
- net = Net(InnerNet)
- out = net()
- expect_res = np.array([1, 2, 3])
- assert np.all(out.asnumpy() == expect_res)
-
-
- def test_fallback_class_input_method():
- """
- Feature: JIT Fallback
- Description: Test user-defined class methods in graph.
- Expectation: No exception.
- """
- @ms_class
- class InnerNet:
- def __init__(self):
- self.val = 2
-
- def act(self, x, y):
- return self.val * (x + y)
-
- class Net(nn.Cell):
- def __init__(self, net):
- super(Net, self).__init__()
- self.inner_net = net()
-
- def construct(self):
- out = self.inner_net.act(1, 2)
- return out
-
- net = Net(InnerNet)
- out = net()
- assert out == 6
-
-
- def test_fallback_class_class_nested():
- """
- Feature: JIT Fallback
- Description: Test nested ms_class in graph.
- Expectation: No exception.
- """
- @ms_class
- class Inner:
- def __init__(self):
- self.number = 1
-
- @ms_class
- class InnerNet:
- def __init__(self):
- self.inner = Inner()
-
- class Net(nn.Cell):
- def __init__(self):
- super(Net, self).__init__()
- self.inner_net = InnerNet()
-
- def construct(self):
- out = self.inner_net.inner.number
- return out
-
- net = Net()
- out = net()
- assert out == 1
-
-
- def test_fallback_class_cell_nested():
- """
- Feature: JIT Fallback
- Description: Test nested ms_class and cell in graph.
- Expectation: No exception.
- """
- class Net(nn.Cell):
- def __init__(self, val):
- super().__init__()
- self.val = val
-
- def construct(self, x):
- return x + self.val
-
- @ms_class
- class TrainNet():
- class Loss(nn.Cell):
- def __init__(self, net):
- super().__init__()
- self.net = net
-
- def construct(self, x):
- out = self.net(x)
- return out * 2
-
- def __init__(self, net):
- self.net = net
- loss_net = self.Loss(self.net)
- self.number = loss_net(10)
-
- global_net = Net(1)
- class LearnNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.value = TrainNet(global_net).number
-
- def construct(self, x):
- return x + self.value
-
- leanrn_net = LearnNet()
- out = leanrn_net(3)
- print(out)
- assert out == 25
-
-
- @pytest.mark.skip(reason='Not support in graph yet')
- def test_fallback_class_isinstance():
- """
- Feature: JIT Fallback
- Description: Test ms_class in graph.
- Expectation: No exception.
- """
- @ms_class
- class InnerNet:
- def __init__(self):
- self.number = 1
-
- class Net(nn.Cell):
- def __init__(self):
- super(Net, self).__init__()
- self.inner_net = InnerNet()
-
- def construct(self, x):
- if isinstance(self.inner_net, InnerNet):
- return x + 10
- return x
-
- net = Net()
- out = net(5)
- assert out == 15
-
-
- def test_fallback_raise_error_not_class_type():
- """
- Feature: JIT Fallback
- Description: Test ms_class in graph.
- Expectation: No exception.
- """
- with pytest.raises(TypeError):
- @ms_class
- def func(x, y):
- return x + y
-
- func(1, 2)
-
-
- def test_fallback_raise_error_not_class_instance():
- """
- Feature: JIT Fallback
- Description: Test ms_class in graph.
- Expectation: No exception.
- """
- @ms_class
- class InnerNet:
- def __init__(self):
- self.number = 1
-
- class Net(nn.Cell):
- def construct(self):
- out = InnerNet().number
- return out
-
- with pytest.raises(ValueError):
- net = Net()
- net()
-
-
- def test_fallback_raise_error_decorate_cell():
- """
- Feature: JIT Fallback
- Description: Test ms_class in graph.
- Expectation: No exception.
- """
- @ms_class
- class Net(nn.Cell):
- def construct(self, x):
- return x
-
- with pytest.raises(TypeError):
- x = Tensor(1)
- net = Net()
- net(x)
|