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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. namespace mindspore {
  19. namespace kernel {
  20. void AddNCPUKernel::InitKernel(const CNodePtr &kernel_node) {
  21. CheckParam(kernel_node);
  22. input_num_ = AnfAlgo::GetInputTensorNum(kernel_node);
  23. output_shape_ = AnfAlgo::GetOutputInferShape(kernel_node, 0);
  24. CPUKernelUtils::ExpandDimsTo4(&output_shape_);
  25. }
  26. bool AddNCPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  27. const std::vector<kernel::AddressPtr> & /*workspace*/,
  28. const std::vector<kernel::AddressPtr> &outputs) {
  29. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  30. size_t offset = 0;
  31. for (size_t i = 0; i < output_shape_[0]; ++i) {
  32. for (size_t j = 0; j < output_shape_[1]; ++j) {
  33. for (size_t k = 0; k < output_shape_[2]; ++k) {
  34. for (size_t m = 0; m < output_shape_[3]; ++m) {
  35. float sum = 0;
  36. for (size_t index = 0; index < input_num_; ++index) {
  37. auto input_addr = reinterpret_cast<float *>(inputs[index]->addr);
  38. sum += input_addr[offset];
  39. }
  40. output_addr[offset++] = sum;
  41. }
  42. }
  43. }
  44. }
  45. return true;
  46. }
  47. void AddNCPUKernel::CheckParam(const CNodePtr &kernel_node) {
  48. auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  49. if (input_shape.size() > 4) {
  50. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", but AddNCPUKernel olny support 4d or lower.";
  51. }
  52. size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  53. if (output_num != 1) {
  54. MS_LOG(EXCEPTION) << "Output number is " << output_num << ", but AddNCPUKernel needs 1 output.";
  55. }
  56. }
  57. } // namespace kernel
  58. } // namespace mindspore