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_grad_graph.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 function grad in graph mode"""
  16. import numpy as np
  17. import mindspore.nn as nn
  18. import mindspore.context as context
  19. from mindspore import Tensor
  20. from mindspore.ops.functional import grad
  21. context.set_context(mode=context.GRAPH_MODE)
  22. class SingleInputSingleOutputNet(nn.Cell):
  23. def construct(self, x):
  24. return x**3
  25. class MultipleInputsMultipleOutputsNet(nn.Cell):
  26. def construct(self, x, y, z):
  27. return x**2 + y**2 + z**2, x*y*z
  28. def function(x, y, z):
  29. return x**2 + y**2 + z**2, x*y*z
  30. def test_grad_single_input_single_output_cell_graph():
  31. """
  32. Features: Function grad.
  33. Description: Test F.grad with single input and single output net in graph mode.
  34. Expectation: No exception.
  35. """
  36. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  37. net = SingleInputSingleOutputNet()
  38. grad(net)(x)
  39. def test_grad_multiple_inputs_multiple_outputs_cell_graph():
  40. """
  41. Features: Function grad.
  42. Description: Test F.grad with multiple inputs and multiple outputs net in graph mode.
  43. Expectation: No exception.
  44. """
  45. x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
  46. y = Tensor(np.array([[-2, 3], [-1, 2]]).astype(np.float32))
  47. z = Tensor(np.array([[0, 3], [5, -1]]).astype(np.float32))
  48. net = MultipleInputsMultipleOutputsNet()
  49. grad(net, grad_position=(1, 2))(x, y, z)
  50. def test_grad_function_with_sens_graph():
  51. """
  52. Features: Function grad.
  53. Description: Test F.grad with function setting sens_param 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([[-2, 3], [-1, 2]]).astype(np.float32))
  58. z = Tensor(np.array([[0, 3], [5, -1]]).astype(np.float32))
  59. v = Tensor(np.array([[-1, 3], [2, 1]]).astype(np.float32))
  60. grad(function, grad_position=(1, 2), sens_param=True)(x, y, z, (v, v))