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_jvp_graph.py 5.1 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 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 Jvp
  22. context.set_context(mode=context.GRAPH_MODE)
  23. class SingleInputSingleOutputNet(nn.Cell):
  24. def construct(self, x):
  25. return x**3
  26. class SingleInputMultipleOutputNet(nn.Cell):
  27. def construct(self, x):
  28. return x**3, 2*x
  29. class MultipleInputSingleOutputNet(nn.Cell):
  30. def construct(self, x, y):
  31. return 2*x + 3*y
  32. class MultipleInputMultipleOutputNet(nn.Cell):
  33. def construct(self, x, y):
  34. return 2*x, y**3
  35. def test_jvp_single_input_single_output_default_v_graph():
  36. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  37. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  38. net = SingleInputSingleOutputNet()
  39. Jvp(net)(x, v)
  40. def test_jvp_single_input_single_output_custom_v_graph():
  41. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  42. v = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  43. net = SingleInputSingleOutputNet()
  44. Jvp(net)(x, v)
  45. def test_jvp_single_input_multiple_outputs_default_v_graph():
  46. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  47. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  48. net = SingleInputMultipleOutputNet()
  49. Jvp(net)(x, v)
  50. def test_jvp_single_input_multiple_outputs_custom_v_graph():
  51. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  52. v = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  53. net = SingleInputMultipleOutputNet()
  54. Jvp(net)(x, v)
  55. def test_jvp_multiple_inputs_single_output_default_v_graph():
  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 = MultipleInputSingleOutputNet()
  60. Jvp(net)(x, y, (v, v))
  61. def test_jvp_multiple_inputs_single_output_custom_v_graph():
  62. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  63. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  64. v1 = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  65. v2 = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  66. net = MultipleInputSingleOutputNet()
  67. Jvp(net)(x, y, (v1, v2))
  68. def test_jvp_multiple_inputs_multiple_outputs_default_v_graph():
  69. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  70. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  71. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  72. net = MultipleInputMultipleOutputNet()
  73. Jvp(net)(x, y, (v, v))
  74. def test_jvp_multiple_inputs_multiple_outputs_custom_v_graph():
  75. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  76. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  77. v1 = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  78. v2 = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  79. net = MultipleInputMultipleOutputNet()
  80. Jvp(net)(x, y, (v1, v2))
  81. def test_jvp_wrong_input_v_graph():
  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 = SingleInputSingleOutputNet()
  85. with pytest.raises(TypeError):
  86. Jvp(net)(x, (v, v))
  87. def test_jvp_wrong_input_v_2_graph():
  88. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  89. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  90. net = SingleInputSingleOutputNet()
  91. with pytest.raises(TypeError):
  92. Jvp(net)(x, (v,))
  93. def test_jvp_wrong_input_graph():
  94. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  95. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  96. net = SingleInputSingleOutputNet()
  97. with pytest.raises(TypeError):
  98. Jvp(net)(x, x, v)
  99. def test_jvp_wrong_input_2_graph():
  100. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  101. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  102. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  103. net = MultipleInputSingleOutputNet()
  104. with pytest.raises(TypeError):
  105. Jvp(net)((x, y), (v, v))
  106. def test_jvp_wrong_input_3_graph():
  107. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  108. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  109. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  110. net = MultipleInputSingleOutputNet()
  111. with pytest.raises(TypeError):
  112. Jvp(net)(x, y, v)