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.

cpu_kernel.cc 2.7 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright 2019 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/cpu_kernel.h"
  17. namespace mindspore {
  18. namespace kernel {
  19. void CPUKernel::InitInputOutputSize(const CNodePtr &kernel_node) {
  20. MS_EXCEPTION_IF_NULL(kernel_node);
  21. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  22. size_t type_size = sizeof(float);
  23. for (size_t input_index = 0; input_index < input_num; ++input_index) {
  24. std::vector<size_t> shape = AnfAlgo::GetInputDeviceShape(kernel_node, input_index);
  25. size_t tensor_size =
  26. shape.empty() ? type_size : std::accumulate(shape.begin(), shape.end(), type_size, std::multiplies<size_t>());
  27. input_size_list_.emplace_back(tensor_size);
  28. }
  29. size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  30. for (size_t output_index = 0; output_index < output_num; ++output_index) {
  31. std::vector<size_t> shape = AnfAlgo::GetOutputDeviceShape(kernel_node, output_index);
  32. size_t tensor_size =
  33. shape.empty() ? type_size : std::accumulate(shape.begin(), shape.end(), type_size, std::multiplies<size_t>());
  34. output_size_list_.emplace_back(tensor_size);
  35. }
  36. }
  37. void CPUKernel::Init(const CNodePtr &kernel_node) {
  38. InitInputOutputSize(kernel_node);
  39. InitKernel(kernel_node);
  40. }
  41. void CPUKernelUtils::ExpandDimsTo4(std::vector<size_t> *shape) {
  42. auto len = shape->size();
  43. if (len < 4) {
  44. for (size_t i = 0; i < 4 - len; ++i) {
  45. shape->insert(shape->begin(), 1);
  46. }
  47. }
  48. }
  49. size_t CPUKernelUtils::CalcOffset(const std::vector<size_t> &shape, size_t dim0, size_t dim1, size_t dim2,
  50. size_t dim3) {
  51. size_t offset = dim0 * shape[1] * shape[2] * shape[3] + dim1 * shape[2] * shape[3] + dim2 * shape[3] + dim3;
  52. return offset;
  53. }
  54. size_t CPUKernelUtils::GetElementNumOnAxis(const std::vector<size_t> &shape, int axis) {
  55. if (axis < 0) {
  56. axis = axis + SizeToInt(shape.size());
  57. }
  58. size_t result = 1;
  59. for (int j = 3; j > axis; --j) {
  60. result *= shape[j];
  61. }
  62. return result;
  63. }
  64. } // namespace kernel
  65. } // namespace mindspore