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_graph_parallel.py 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 graph parallel case"""
  16. import model
  17. def injective_graph(shape):
  18. gb = model.GraphBuilder()
  19. with gb.graph_scope('injective') as _:
  20. a1 = gb.tensor(shape, 'float32')
  21. a2 = gb.emit('Abs', a1)
  22. a3 = gb.emit('Abs', a2)
  23. gb.emit('Abs', a3)
  24. return gb.get()[0]
  25. def reduce_graph(shape, reduce_axis):
  26. gb = model.GraphBuilder()
  27. with gb.graph_scope('reduce') as _:
  28. a1 = gb.tensor(shape, 'float32')
  29. a2 = gb.emit('Abs', a1)
  30. a3 = gb.emit('Abs', a2)
  31. gb.emit('ReduceSum', a3, 'C', attrs={'reduce_axis': reduce_axis})
  32. return gb.get()[0]
  33. def control_graph(shape):
  34. gb = model.GraphBuilder()
  35. with gb.graph_scope('control') as _:
  36. a1 = gb.tensor(shape, 'float32')
  37. a2 = gb.emit('Abs', a1)
  38. gb.emit('ControlDepend', a2)
  39. return gb.get()[0]
  40. def block_fusion(graphs):
  41. gain = model.parallel_estimate(graphs)
  42. print("fusion = {}, bottleneck = {}, gain = {}".format(gain.fusion_type, gain.bottleneck, gain.gain))
  43. return gain.fusion_type == "block_fusion" and gain.gain > 0
  44. if __name__ == "__main__":
  45. assert block_fusion([injective_graph([40, 1024]), injective_graph([40, 1024])])
  46. assert block_fusion([reduce_graph([1024, 1024], [1]), injective_graph([24, 1024])])
  47. assert not block_fusion([reduce_graph([1024, 1024], [1]), injective_graph([50, 1024])])
  48. assert not block_fusion([reduce_graph([1024, 1024], [0, 1]), injective_graph([1024, 1024])])
  49. assert block_fusion([control_graph([20, 128]), injective_graph([40, 1024])])