1# Copyright 2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15"""test jvp in pynative mode""" 16import numpy as np 17import pytest 18import mindspore.nn as nn 19import mindspore.context as context 20from mindspore import Tensor 21from mindspore.nn.grad import Vjp 22 23context.set_context(mode=context.PYNATIVE_MODE) 24 25 26class SingleInputNet(nn.Cell): 27 def construct(self, x): 28 return x**3 29 30 31class MultipleInputsOutputNet(nn.Cell): 32 def construct(self, x, y): 33 return 2*x, y**3 34 35 36@pytest.mark.level0 37@pytest.mark.platform_x86_cpu 38@pytest.mark.env_onecard 39def test_vjp_single_input_graph(): 40 x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) 41 v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) 42 net = SingleInputNet() 43 expect_primal = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32)) 44 expect_grad = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32)) 45 primal, grad = Vjp(net)(x, v) 46 assert np.allclose(primal.asnumpy(), expect_primal.asnumpy()) 47 assert np.allclose(grad.asnumpy(), expect_grad.asnumpy()) 48 49 50 51@pytest.mark.level0 52@pytest.mark.platform_x86_cpu 53@pytest.mark.env_onecard 54def test_vjp_multiple_inputs_default_v_graph(): 55 x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) 56 y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32)) 57 v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32)) 58 net = MultipleInputsOutputNet() 59 expect_primal_0 = Tensor(np.array([[2, 4], [6, 8]]).astype(np.float32)) 60 expect_primal_1 = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32)) 61 expect_grad_0 = Tensor(np.array([[2, 2], [2, 2]]).astype(np.float32)) 62 expect_grad_1 = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32)) 63 primal, grad = Vjp(net)(x, y, (v, v)) 64 assert isinstance(primal, tuple) 65 assert len(primal) == 2 66 assert np.allclose(primal[0].asnumpy(), expect_primal_0.asnumpy()) 67 assert np.allclose(primal[1].asnumpy(), expect_primal_1.asnumpy()) 68 assert isinstance(grad, tuple) 69 assert len(grad) == 2 70 assert np.allclose(grad[0].asnumpy(), expect_grad_0.asnumpy()) 71 assert np.allclose(grad[1].asnumpy(), expect_grad_1.asnumpy()) 72