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_fuse_composite.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. import logging
  16. import numpy as np
  17. import mindspore.context as context
  18. import mindspore.ops.composite as C
  19. from mindspore import Tensor
  20. from mindspore.nn import Cell
  21. from mindspore.ops import operations as P
  22. from mindspore.nn.composite_ops import ReLU
  23. log = logging.getLogger("ME")
  24. log.setLevel(level=logging.DEBUG)
  25. context.set_context(mode=context.GRAPH_MODE, save_graphs=True, device_target="Ascend")
  26. class NetBackwordFuse1(Cell):
  27. def __init__(self):
  28. super(NetBackwordFuse1, self).__init__()
  29. self.relu = ReLU()
  30. self.reduce_sum = P.ReduceSum(keep_dims=True)
  31. def construct(self, x):
  32. relu = self.relu(x)
  33. mul = P.Mul()(relu, 2.0)
  34. add = relu + mul
  35. out = self.reduce_sum(add, (0, ))
  36. return out
  37. class NetBackwordFuse2(Cell):
  38. def __init__(self):
  39. super(NetBackwordFuse2, self).__init__()
  40. self.relu = ReLU()
  41. self.reduce_sum = P.ReduceSum(keep_dims=True)
  42. def construct(self, x):
  43. relu = self.relu(x)
  44. mul = P.Mul()(relu, 2.0)
  45. reduce = self.reduce_sum(relu, (0, ))
  46. div = 1.0 / reduce
  47. add1 = reduce + div
  48. out = relu + add1
  49. return out
  50. def test_composite_fuse1():
  51. x = np.random.normal(0, 1, [2, 3, 1, 3]).astype(np.float32)
  52. net = NetBackwordFuse1()
  53. result = net(Tensor(x))
  54. print("================relu result=======================")
  55. print("x: {}".format(x))
  56. print("result: {}".format(result))
  57. print("=======================================")
  58. def test_composite_fuse2():
  59. x = np.random.normal(0, 1, [2, 3, 1, 3]).astype(np.float32)
  60. net = NetBackwordFuse2()
  61. result = net(Tensor(x))
  62. print("================relu result=======================")
  63. print("x: {}".format(x))
  64. print("result: {}".format(result))
  65. print("=======================================")
  66. test_composite_fuse1()