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 6.0 kB

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