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_ad.py 1.5 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. # coding: utf-8
  3. # Copyright 2019 Huawei Technologies Co., Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """operator dsl function: mean_ad"""
  17. import akg.tvm
  18. import akg
  19. from akg.ops.math import mean
  20. from akg.utils import validation_check as vc_util
  21. @vc_util.check_input_type(akg.tvm.tensor.Tensor, (list, tuple), (list, tuple, int), bool)
  22. def mean_ad(head, input_shape, axis, keepdims):
  23. """
  24. Compute gradient of mean operator using automatic differentiate.
  25. Args:
  26. head (tvm.tensor.Tensor): Input tensor.
  27. input_shape (Union[list, tuple]): Shape of input tensor of mean operator.
  28. axis (Union[list, tuple, int]): Specifies which axis to reduce.
  29. keepdims (bool): Keep the reduced axis with length 1 if keepdims is true.
  30. Returns:
  31. tvm.tensor.Tensor.
  32. """
  33. a = akg.tvm.placeholder(input_shape, head.dtype, "A")
  34. b, _ = mean.mean(a, axis, keepdims)
  35. jacs = list(akg.differentiate(b, [a], head))
  36. return jacs[0]