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.

mean.py 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. """mean op compute and schedule"""
  15. import _akg.tvm as tvm
  16. from _akg.ops.math.mean import mean
  17. from .default_schedule import DEFAULT_GPU_THREAD
  18. def Mean(x, axis=None, keepdims=True):
  19. """mean."""
  20. outs = mean(x, axis, keepdims)
  21. # remove useless mean_output
  22. if isinstance(outs, tuple):
  23. outs = outs[0]
  24. if outs.op.name == "mean_output":
  25. outs = outs.op.input_tensors[0]
  26. return outs
  27. def gpu_schedule_Mean(outs):
  28. """
  29. gpu schedule function for mean.
  30. Args:
  31. outs (tvm.tensor.Tensor): outputs of compute.
  32. Returns:
  33. sch (schedule.Schedule): The created schedule.
  34. """
  35. out = outs[0] if isinstance(outs, list) else outs
  36. device = "cuda"
  37. with tvm.target.create(device):
  38. sch = tvm.create_schedule(out.op)
  39. if out.op.name == "T_divide":
  40. tensor_c = out
  41. else: # squeeze
  42. tensor_c = out.op.input_tensors[0]
  43. tensor_b = tensor_c.op.input_tensors[0]
  44. if len(tensor_c.op.axis) >= 2:
  45. sch[tensor_b].compute_at(sch[tensor_c], tensor_c.op.axis[1])
  46. else:
  47. sch[tensor_b].compute_at(sch[tensor_c], tensor_c.op.axis[0])
  48. bx, tx = sch[tensor_c].split(tensor_c.op.axis[0], factor=DEFAULT_GPU_THREAD)
  49. sch[tensor_c].bind(bx, tvm.thread_axis("blockIdx.x"))
  50. sch[tensor_c].bind(tx, tvm.thread_axis("threadIdx.x"))
  51. return sch
  52. def SimpleMean(x):
  53. """
  54. SimpleMean compute the mean of the input 4D Tensor over last two axises and keep reduced dimensions.
  55. Args:
  56. x (tvm.tensor.Tensor): Tensor of type float16, float32.
  57. Returns:
  58. tvm.tensor.Tensor, has the same type as x, output shape will be (a, b, 1, 1) if input Tensor x is (a, b, c, d).
  59. """
  60. axis = (2, 3)
  61. keepdims = True
  62. return Mean(x, axis, keepdims)
  63. def gpu_schedule_SimpleMean(outs):
  64. """gpu schedule function for SimpleMean."""
  65. return gpu_schedule_Mean(outs)