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_function_vjp_graph.py 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 vjp 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 import ms_function
  22. from mindspore.ops.functional import vjp
  23. context.set_context(mode=context.GRAPH_MODE)
  24. class SingleInputNet(nn.Cell):
  25. def construct(self, x):
  26. return x**3
  27. class MultipleInputsOutputNet(nn.Cell):
  28. def construct(self, x, y):
  29. return 2*x, y**3
  30. @pytest.mark.level0
  31. @pytest.mark.platform_x86_cpu
  32. @pytest.mark.env_onecard
  33. def test_vjp_single_input_graph():
  34. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  35. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  36. net = SingleInputNet()
  37. expect_primal = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32))
  38. expect_grad = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32))
  39. primal, grad = vjp(net, x, v)
  40. assert np.allclose(primal.asnumpy(), expect_primal.asnumpy())
  41. assert np.allclose(grad.asnumpy(), expect_grad.asnumpy())
  42. @pytest.mark.level0
  43. @pytest.mark.platform_x86_cpu
  44. @pytest.mark.env_onecard
  45. def test_vjp_multiple_inputs_default_v_graph():
  46. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  47. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  48. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  49. net = MultipleInputsOutputNet()
  50. expect_primal_0 = Tensor(np.array([[2, 4], [6, 8]]).astype(np.float32))
  51. expect_primal_1 = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32))
  52. expect_grad_0 = Tensor(np.array([[2, 2], [2, 2]]).astype(np.float32))
  53. expect_grad_1 = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32))
  54. primal, grad = vjp(net, (x, y), (v, v))
  55. assert isinstance(primal, tuple)
  56. assert len(primal) == 2
  57. assert np.allclose(primal[0].asnumpy(), expect_primal_0.asnumpy())
  58. assert np.allclose(primal[1].asnumpy(), expect_primal_1.asnumpy())
  59. assert isinstance(grad, tuple)
  60. assert len(grad) == 2
  61. assert np.allclose(grad[0].asnumpy(), expect_grad_0.asnumpy())
  62. assert np.allclose(grad[1].asnumpy(), expect_grad_1.asnumpy())
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_cpu
  65. @pytest.mark.env_onecard
  66. def test_vjp_ms_function_single_input_single_output_default_v_graph():
  67. """
  68. Features: Function vjp
  69. Description: Test vjp with ms_function, single input, single output and default v in graph mode.
  70. Expectation: No exception.
  71. """
  72. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  73. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  74. net = SingleInputNet()
  75. @ms_function
  76. def vjp_with_ms_function(inputs, vectors):
  77. output, vjp_grad = vjp(net, inputs, vectors)
  78. return output, vjp_grad
  79. primal, grad = vjp_with_ms_function(x, v)
  80. expect_primal = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32))
  81. expect_grad = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32))
  82. assert np.allclose(primal.asnumpy(), expect_primal.asnumpy())
  83. assert np.allclose(grad.asnumpy(), expect_grad.asnumpy())
  84. @pytest.mark.level0
  85. @pytest.mark.platform_x86_cpu
  86. @pytest.mark.env_onecard
  87. def test_vjp_input_function_single_input_single_output_default_v_graph():
  88. """
  89. Features: Function vjp
  90. Description: Test vjp with function, single input, single output and default v in graph mode.
  91. Expectation: No exception.
  92. """
  93. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  94. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  95. def test_function(inputs):
  96. return inputs**3
  97. primal, grad = vjp(test_function, x, v)
  98. expect_primal = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32))
  99. expect_grad = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32))
  100. assert np.allclose(primal.asnumpy(), expect_primal.asnumpy())
  101. assert np.allclose(grad.asnumpy(), expect_grad.asnumpy())
  102. @pytest.mark.level0
  103. @pytest.mark.platform_x86_cpu
  104. @pytest.mark.env_onecard
  105. def test_vjp_construct_single_input_single_output_default_v_graph():
  106. """
  107. Features: Function vjp
  108. Description: Test vjp with function, single input, single output and default v in graph mode.
  109. Expectation: No exception.
  110. """
  111. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  112. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  113. class Net(nn.Cell):
  114. def __init__(self, network):
  115. super(Net, self).__init__()
  116. self.net = network
  117. def construct(self, inputs, vectors):
  118. net_out, vjp_out = vjp(self.net, inputs, vectors)
  119. return net_out, vjp_out
  120. test_net = Net(SingleInputNet())
  121. primal, grad = test_net(x, v)
  122. expect_primal = Tensor(np.array([[1, 8], [27, 64]]).astype(np.float32))
  123. expect_grad = Tensor(np.array([[3, 12], [27, 48]]).astype(np.float32))
  124. assert np.allclose(primal.asnumpy(), expect_primal.asnumpy())
  125. assert np.allclose(grad.asnumpy(), expect_grad.asnumpy())