You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_vjp_graph.py 2.8 kB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 graph mode"""
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. import mindspore.context as context
  20. from mindspore import Tensor
  21. from mindspore.nn.grad import Vjp
  22. context.set_context(mode=context.GRAPH_MODE)
  23. class SingleInputNet(nn.Cell):
  24. def construct(self, x):
  25. return x**3
  26. class MultipleInputsOutputNet(nn.Cell):
  27. def construct(self, x, y):
  28. return 2*x, y**3
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_cpu
  31. @pytest.mark.env_onecard
  32. def test_vjp_single_input_graph():
  33. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  34. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  35. net = SingleInputNet()
  36. expect_primal = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32))
  37. expect_grad = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32))
  38. primal, grad = Vjp(net)(x, v)
  39. assert np.allclose(primal.asnumpy(), expect_primal.asnumpy())
  40. assert np.allclose(grad.asnumpy(), expect_grad.asnumpy())
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_cpu
  43. @pytest.mark.env_onecard
  44. def test_vjp_multiple_inputs_default_v_graph():
  45. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  46. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  47. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  48. net = MultipleInputsOutputNet()
  49. expect_primal_0 = Tensor(np.array([[2, 4], [6, 8]]).astype(np.float32))
  50. expect_primal_1 = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32))
  51. expect_grad_0 = Tensor(np.array([[2, 2], [2, 2]]).astype(np.float32))
  52. expect_grad_1 = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32))
  53. primal, grad = Vjp(net)(x, y, (v, v))
  54. assert isinstance(primal, tuple)
  55. assert len(primal) == 2
  56. assert np.allclose(primal[0].asnumpy(), expect_primal_0.asnumpy())
  57. assert np.allclose(primal[1].asnumpy(), expect_primal_1.asnumpy())
  58. assert isinstance(grad, tuple)
  59. assert len(grad) == 2
  60. assert np.allclose(grad[0].asnumpy(), expect_grad_0.asnumpy())
  61. assert np.allclose(grad[1].asnumpy(), expect_grad_1.asnumpy())