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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. def test_vjp_single_input_graph():
  30. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  31. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  32. net = SingleInputNet()
  33. Vjp(net)(x, v)
  34. def test_vjp_multiple_inputs_default_v_graph():
  35. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  36. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  37. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  38. net = MultipleInputsOutputNet()
  39. Vjp(net)(x, y, (v, v))
  40. def test_vjp_wrong_input_v_graph():
  41. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  42. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  43. net = SingleInputNet()
  44. with pytest.raises(TypeError):
  45. Vjp(net)(x, (v, v))
  46. def test_vjp_wrong_input_v_2_graph():
  47. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  48. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  49. net = SingleInputNet()
  50. with pytest.raises(TypeError):
  51. Vjp(net)(x, (v,))
  52. def test_vjp_wrong_input_graph():
  53. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  54. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  55. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  56. net = SingleInputNet()
  57. with pytest.raises(TypeError):
  58. Vjp(net)(x, y, v)
  59. def test_vjp_wrong_input_2_graph():
  60. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  61. y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  62. v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
  63. net = MultipleInputsOutputNet()
  64. with pytest.raises(TypeError):
  65. Vjp(net)((x, y), (v, v))