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.

addn_cpu_kernel.cc 2.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "kernel/cpu/addn_cpu_kernel.h"
  17. #include "device/cpu/cpu_device_address.h"
  18. #include "ir/primitive.h"
  19. namespace mindspore {
  20. namespace kernel {
  21. void AddNCPUKernel::InitKernel(const CNodePtr &kernel_node) {
  22. CheckParam(kernel_node);
  23. input_num_ = AnfAlgo::GetInputTensorNum(kernel_node);
  24. output_shape_ = AnfAlgo::GetOutputInferShape(kernel_node, 0);
  25. CPUKernelUtils::ExpandDimsTo4(&output_shape_);
  26. }
  27. bool AddNCPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  28. const std::vector<kernel::AddressPtr> & /*workspace*/,
  29. const std::vector<kernel::AddressPtr> &outputs) {
  30. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  31. size_t offset = 0;
  32. for (size_t i = 0; i < output_shape_[0]; ++i) {
  33. for (size_t j = 0; j < output_shape_[1]; ++j) {
  34. for (size_t k = 0; k < output_shape_[2]; ++k) {
  35. for (size_t m = 0; m < output_shape_[3]; ++m) {
  36. float sum = 0;
  37. for (size_t index = 0; index < input_num_; ++index) {
  38. auto input_addr = reinterpret_cast<float *>(inputs[index]->addr);
  39. sum += input_addr[offset];
  40. }
  41. output_addr[offset++] = sum;
  42. }
  43. }
  44. }
  45. }
  46. return true;
  47. }
  48. void AddNCPUKernel::CheckParam(const CNodePtr &kernel_node) {
  49. auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  50. if (input_shape.size() > 4) {
  51. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", but AddNCPUKernel olny support 4d or lower.";
  52. }
  53. size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  54. if (output_num != 1) {
  55. MS_LOG(EXCEPTION) << "Output number is " << output_num << ", but AddNCPUKernel needs 1 output.";
  56. }
  57. }
  58. } // namespace kernel
  59. } // namespace mindspore