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.

allgather_cpu_kernel.cc 2.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/allgather_cpu_kernel.h"
  17. #include "device/cpu/cpu_device_address.h"
  18. #include "device/cpu/mpi/mpi_adapter.h"
  19. #include "ir/primitive.h"
  20. #include "utils/log_adapter.h"
  21. namespace mindspore {
  22. namespace kernel {
  23. namespace {
  24. constexpr auto kRanksGroup = "group";
  25. constexpr auto kAllGatherInputNum = 1;
  26. } // namespace
  27. AllGatherCPUKernel::AllGatherCPUKernel() : input_data_number_(0) {}
  28. void AllGatherCPUKernel::InitKernel(const CNodePtr &kernel_node) {
  29. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  30. if (input_num != kAllGatherInputNum) {
  31. MS_LOG(EXCEPTION) << "allgather input num:" << input_num;
  32. }
  33. for (size_t i = 0; i < input_num; ++i) {
  34. auto shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, i);
  35. size_t count = 1;
  36. for (size_t j = 0; j < shape.size(); j++) {
  37. count *= IntToSize(shape[j]);
  38. }
  39. input_data_number_ += count;
  40. }
  41. auto ranks_group = AnfAlgo::GetCNodePrimitive(kernel_node)->GetAttr(kRanksGroup);
  42. if (ranks_group != nullptr) {
  43. ranks_group_ = GetValue<std::vector<int>>(ranks_group);
  44. } else {
  45. MS_LOG(EXCEPTION) << "Miss attribute " << kRanksGroup;
  46. }
  47. }
  48. bool AllGatherCPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  49. const std::vector<kernel::AddressPtr> & /*workspace*/,
  50. const std::vector<kernel::AddressPtr> &outputs) {
  51. auto input_addr = reinterpret_cast<float *>(inputs[0]->addr);
  52. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  53. return device::cpu::MPIAdapter::Instance().AllGather(input_addr, output_addr, ranks_group_, input_data_number_);
  54. }
  55. } // namespace kernel
  56. } // namespace mindspore