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

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