diff --git a/tests/st/gradient/test_vjp_pynative.py b/tests/st/gradient/test_vjp_pynative.py new file mode 100644 index 0000000000..6b13a918cb --- /dev/null +++ b/tests/st/gradient/test_vjp_pynative.py @@ -0,0 +1,71 @@ +# 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 jvp in pynative mode""" +import numpy as np +import pytest +import mindspore.nn as nn +import mindspore.context as context +from mindspore import Tensor +from mindspore.nn.grad import Vjp + +context.set_context(mode=context.PYNATIVE_MODE) + + +class SingleInputNet(nn.Cell): + def construct(self, x): + return x**3 + + +class MultipleInputsOutputNet(nn.Cell): + def construct(self, x, y): + return 2*x, y**3 + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.env_onecard +def test_vjp_single_input_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputNet() + expect_primal = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32)) + expect_grad = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32)) + primal, grad = Vjp(net)(x, v) + assert np.allclose(primal.asnumpy(), expect_primal.asnumpy()) + assert np.allclose(grad.asnumpy(), expect_grad.asnumpy()) + + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.env_onecard +def test_vjp_multiple_inputs_default_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputsOutputNet() + expect_primal_0 = Tensor(np.array([[2, 4], [6, 8]]).astype(np.float32)) + expect_primal_1 = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32)) + expect_grad_0 = Tensor(np.array([[2, 2], [2, 2]]).astype(np.float32)) + expect_grad_1 = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32)) + primal, grad = Vjp(net)(x, y, (v, v)) + assert isinstance(primal, tuple) + assert len(primal) == 2 + assert np.allclose(primal[0].asnumpy(), expect_primal_0.asnumpy()) + assert np.allclose(primal[1].asnumpy(), expect_primal_1.asnumpy()) + assert isinstance(grad, tuple) + assert len(grad) == 2 + assert np.allclose(grad[0].asnumpy(), expect_grad_0.asnumpy()) + assert np.allclose(grad[1].asnumpy(), expect_grad_1.asnumpy()) diff --git a/tests/ut/python/nn/gradient/test_jvp_graph.py b/tests/ut/python/nn/gradient/test_jvp_graph.py new file mode 100644 index 0000000000..5ba987a28c --- /dev/null +++ b/tests/ut/python/nn/gradient/test_jvp_graph.py @@ -0,0 +1,148 @@ +# 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 jvp in graph mode""" + +import numpy as np +import pytest +import mindspore.nn as nn +import mindspore.context as context +from mindspore import Tensor +from mindspore.nn.grad import Jvp + +context.set_context(mode=context.GRAPH_MODE) + + +class SingleInputSingleOutputNet(nn.Cell): + def construct(self, x): + return x**3 + + +class SingleInputMultipleOutputNet(nn.Cell): + def construct(self, x): + return x**3, 2*x + + +class MultipleInputSingleOutputNet(nn.Cell): + def construct(self, x, y): + return 2*x + 3*y + + +class MultipleInputMultipleOutputNet(nn.Cell): + def construct(self, x, y): + return 2*x, y**3 + + +def test_jvp_single_input_single_output_default_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + Jvp(net)(x, v) + + +def test_jvp_single_input_single_output_custom_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + Jvp(net)(x, v) + + +def test_jvp_single_input_multiple_outputs_default_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputMultipleOutputNet() + Jvp(net)(x, v) + + +def test_jvp_single_input_multiple_outputs_custom_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + net = SingleInputMultipleOutputNet() + Jvp(net)(x, v) + + +def test_jvp_multiple_inputs_single_output_default_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputSingleOutputNet() + Jvp(net)(x, y, (v, v)) + + +def test_jvp_multiple_inputs_single_output_custom_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v1 = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + v2 = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + net = MultipleInputSingleOutputNet() + Jvp(net)(x, y, (v1, v2)) + + +def test_jvp_multiple_inputs_multiple_outputs_default_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputMultipleOutputNet() + Jvp(net)(x, y, (v, v)) + + +def test_jvp_multiple_inputs_multiple_outputs_custom_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v1 = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + v2 = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + net = MultipleInputMultipleOutputNet() + Jvp(net)(x, y, (v1, v2)) + + +def test_jvp_wrong_input_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)(x, (v, v)) + + +def test_jvp_wrong_input_v_2_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)(x, (v,)) + + +def test_jvp_wrong_input_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)(x, x, v) + + +def test_jvp_wrong_input_2_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)((x, y), (v, v)) + + +def test_jvp_wrong_input_3_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)(x, y, v) diff --git a/tests/ut/python/nn/gradient/test_jvp_pynative.py b/tests/ut/python/nn/gradient/test_jvp_pynative.py new file mode 100644 index 0000000000..b711ac8051 --- /dev/null +++ b/tests/ut/python/nn/gradient/test_jvp_pynative.py @@ -0,0 +1,147 @@ +# 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 jvp in pynative mode """ + +import numpy as np +import pytest +import mindspore.nn as nn +import mindspore.context as context +from mindspore import Tensor +from mindspore.nn.grad import Jvp + +context.set_context(mode=context.PYNATIVE_MODE) + +class SingleInputSingleOutputNet(nn.Cell): + def construct(self, x): + return x**3 + + +class SingleInputMultipleOutputNet(nn.Cell): + def construct(self, x): + return x**3, 2*x + + +class MultipleInputSingleOutputNet(nn.Cell): + def construct(self, x, y): + return 2*x + 3*y + + +class MultipleInputMultipleOutputNet(nn.Cell): + def construct(self, x, y): + return 2*x, y**3 + + +def test_jvp_single_input_single_output_default_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + Jvp(net)(x, v) + + +def test_jvp_single_input_single_output_custom_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + Jvp(net)(x, v) + + +def test_jvp_single_input_multiple_outputs_default_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputMultipleOutputNet() + Jvp(net)(x, v) + + +def test_jvp_single_input_multiple_outputs_custom_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + net = SingleInputMultipleOutputNet() + Jvp(net)(x, v) + + +def test_jvp_multiple_inputs_multiple_outputs_default_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputMultipleOutputNet() + Jvp(net)(x, y, (v, v)) + + +def test_jvp_multiple_inputs_multiple_outputs_custom_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v1 = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + v2 = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + net = MultipleInputMultipleOutputNet() + Jvp(net)(x, y, (v1, v2)) + + +def test_jvp_multiple_inputs_single_output_default_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputSingleOutputNet() + Jvp(net)(x, y, (v, v)) + + +def test_jvp_multiple_inputs_single_output_custom_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v1 = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + v2 = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + net = MultipleInputSingleOutputNet() + Jvp(net)(x, y, (v1, v2)) + + +def test_jvp_wrong_input_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)(x, (v, v)) + + +def test_jvp_wrong_input_v_2_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)(x, (v,)) + + +def test_jvp_wrong_input_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)(x, x, v) + + +def test_jvp_wrong_input_2_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)((x, y), (v, v)) + + +def test_jvp_wrong_input_3_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputSingleOutputNet() + with pytest.raises(TypeError): + Jvp(net)(x, y, v) diff --git a/tests/ut/python/nn/gradient/test_vjp_graph.py b/tests/ut/python/nn/gradient/test_vjp_graph.py new file mode 100644 index 0000000000..aaca4cf27c --- /dev/null +++ b/tests/ut/python/nn/gradient/test_vjp_graph.py @@ -0,0 +1,82 @@ +# 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 jvp in graph mode""" +import numpy as np +import pytest +import mindspore.nn as nn +import mindspore.context as context +from mindspore import Tensor +from mindspore.nn.grad import Vjp + +context.set_context(mode=context.GRAPH_MODE) + + +class SingleInputNet(nn.Cell): + def construct(self, x): + return x**3 + + +class MultipleInputsOutputNet(nn.Cell): + def construct(self, x, y): + return 2*x, y**3 + + +def test_vjp_single_input_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputNet() + Vjp(net)(x, v) + + +def test_vjp_multiple_inputs_default_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputsOutputNet() + Vjp(net)(x, y, (v, v)) + + +def test_vjp_wrong_input_v_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputNet() + with pytest.raises(TypeError): + Vjp(net)(x, (v, v)) + + +def test_vjp_wrong_input_v_2_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputNet() + with pytest.raises(TypeError): + Vjp(net)(x, (v,)) + + +def test_vjp_wrong_input_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputNet() + with pytest.raises(TypeError): + Vjp(net)(x, y, v) + + +def test_vjp_wrong_input_2_graph(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputsOutputNet() + with pytest.raises(TypeError): + Vjp(net)((x, y), (v, v)) diff --git a/tests/ut/python/nn/gradient/test_vjp_pynative.py b/tests/ut/python/nn/gradient/test_vjp_pynative.py new file mode 100644 index 0000000000..d3434040b0 --- /dev/null +++ b/tests/ut/python/nn/gradient/test_vjp_pynative.py @@ -0,0 +1,82 @@ +# 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 jvp in pynative mode""" +import numpy as np +import pytest +import mindspore.nn as nn +import mindspore.context as context +from mindspore import Tensor +from mindspore.nn.grad import Vjp + +context.set_context(mode=context.PYNATIVE_MODE) + + +class SingleInputNet(nn.Cell): + def construct(self, x): + return x**3 + + +class MultipleInputsOutputNet(nn.Cell): + def construct(self, x, y): + return 2*x, y**3 + + +def test_vjp_single_input_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputNet() + Vjp(net)(x, v) + + +def test_vjp_multiple_inputs_default_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputsOutputNet() + Vjp(net)(x, y, (v, v)) + + +def test_vjp_wrong_input_v_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputNet() + with pytest.raises(TypeError): + Vjp(net)(x, (v, v)) + + +def test_vjp_wrong_input_v_2_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputNet() + with pytest.raises(TypeError): + Vjp(net)(x, (v,)) + + +def test_vjp_wrong_input_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = SingleInputNet() + with pytest.raises(TypeError): + Vjp(net)(x, y, v) + + +def test_vjp_wrong_input_2_pynative(): + x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) + v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) + net = MultipleInputsOutputNet() + with pytest.raises(TypeError): + Vjp(net)((x, y), (v, v))