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_highgrad_param.py 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright 2020 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 high order grad with respect to parameter first, then input."""
  16. import pytest
  17. import numpy as np
  18. import mindspore.nn as nn
  19. import mindspore.ops as ops
  20. from mindspore import Tensor, context
  21. from mindspore import ParameterTuple, Parameter
  22. class Net(nn.Cell):
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. self.mul = ops.Mul()
  26. weight_np = np.array([2, 2]).astype(np.float32)
  27. self.weight = Parameter(Tensor(weight_np), name="weight", requires_grad=True)
  28. def construct(self, x):
  29. x_square = self.mul(x, x)
  30. x_square_z = self.mul(x_square, self.weight)
  31. output = self.mul(x_square_z, self.weight)
  32. return output
  33. class Grad(nn.Cell):
  34. def __init__(self, network):
  35. super(Grad, self).__init__()
  36. self.grad = ops.GradOperation(get_by_list=True, sens_param=False)
  37. self.network = network
  38. self.params = ParameterTuple(network.trainable_params())
  39. def construct(self, x):
  40. output = self.grad(self.network, self.params)(x)
  41. return output
  42. class GradSec(nn.Cell):
  43. def __init__(self, network):
  44. super(GradSec, self).__init__()
  45. self.grad = ops.GradOperation(get_all=True, sens_param=False)
  46. self.network = network
  47. def construct(self, x):
  48. output = self.grad(self.network)(x)
  49. return output
  50. @pytest.mark.level0
  51. @pytest.mark.platform_arm_ascend_training
  52. @pytest.mark.platform_x86_ascend_training
  53. @pytest.mark.platform_x86_gpu_training
  54. @pytest.mark.platform_x86_cpu_training
  55. @pytest.mark.env_onecard
  56. def test_sit_high_order_grad_params():
  57. context.set_context(mode=context.GRAPH_MODE)
  58. x = Tensor(np.array([1, 1]).astype(np.float32))
  59. net = Net()
  60. first_grad = Grad(net)
  61. second_grad = GradSec(first_grad)
  62. grad = second_grad(x)
  63. assert (grad[0].asnumpy() == np.array([8, 8])).all()