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_bprop_disorder.py 3.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright 2019 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 bprop disorder """
  16. import functools
  17. import numpy as np
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Parameter
  20. from mindspore.ops import composite as C
  21. from mindspore.ops import operations as P
  22. from mindspore.common.parameter import ParameterTuple
  23. from ..ut_filter import non_graph_engine
  24. from ....mindspore_test_framework.mindspore_test import mindspore_test
  25. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  26. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  27. class DisOrderTest1(nn.Cell):
  28. """ DisOrderTest1 definition """
  29. def __init__(self):
  30. super(DisOrderTest1, self).__init__()
  31. weight = Tensor(np.ones([1], np.float32))
  32. self.s1 = Parameter(weight, name="s1")
  33. self.s2 = Parameter(weight, name="s2")
  34. self.s3 = Parameter(weight, name="s3")
  35. self.s4 = Parameter(weight, name="s4")
  36. self.mul = P.Mul()
  37. self.add = P.TensorAdd()
  38. def construct(self, x):
  39. return x * (self.s1 * self.s2 + self.s2 * self.s3 + self.s3 * self.s4 + self.s4 * self.s1)
  40. class DisOrderTest2(nn.Cell):
  41. """ DisOrderTest2 definition """
  42. def __init__(self):
  43. super(DisOrderTest2, self).__init__()
  44. weight = Tensor(np.ones([1], np.float32))
  45. self.s1 = Parameter(weight, name="s1")
  46. self.s2 = Parameter(weight, name="s2")
  47. self.s3 = Parameter(weight, name="s3")
  48. self.s4 = Parameter(weight, name="s4")
  49. self.mul = P.Mul()
  50. self.add = P.TensorAdd()
  51. def construct(self, x):
  52. return self.mul(x, (self.add(self.add(self.add(self.mul(self.s1, self.s2), self.mul(self.s2, self.s3)),
  53. self.mul(self.s3, self.s4)), self.mul(self.s4, self.s1))))
  54. class GradNetWrap(nn.Cell):
  55. """ GradNetWrap definition """
  56. def __init__(self, net):
  57. super(GradNetWrap, self).__init__()
  58. self.net = net
  59. self.weights = ParameterTuple(net.get_parameters())
  60. def construct(self, x, sens):
  61. return C.grad_by_list_with_sens(self.net, self.weights)(x, sens)
  62. test_case_ops = [
  63. ('DisOrderTest1', {
  64. 'block': GradNetWrap(DisOrderTest1()),
  65. 'desc_inputs': [Tensor(np.ones([1], np.float32)), Tensor(np.ones([1], np.float32))]}),
  66. ('DisOrderTest2', {
  67. 'block': GradNetWrap(DisOrderTest2()),
  68. 'desc_inputs': [Tensor(np.ones([1], np.float32)), Tensor(np.ones([1], np.float32))]}),
  69. ]
  70. test_case_lists = [test_case_ops]
  71. test_exec_case = functools.reduce(lambda x, y: x + y, test_case_lists)
  72. # use -k to select certain testcast
  73. # pytest tests/python/ops/test_ops.py::test_backward -k LayerNorm
  74. import mindspore.context as context
  75. @non_graph_engine
  76. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  77. def test_exec():
  78. context.set_context(mode=context.GRAPH_MODE)
  79. return test_exec_case