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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. void AllGatherCPUKernel::InitKernel(const CNodePtr &kernel_node) {
  28. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  29. if (input_num != kAllGatherInputNum) {
  30. MS_LOG(EXCEPTION) << "allgather input num:" << input_num;
  31. }
  32. auto ranks_group = AnfAlgo::GetCNodePrimitive(kernel_node)->GetAttr(kRanksGroup);
  33. if (ranks_group != nullptr) {
  34. ranks_group_ = GetValue<std::vector<int>>(ranks_group);
  35. } else {
  36. MS_LOG(EXCEPTION) << "Miss attribute " << kRanksGroup;
  37. }
  38. }
  39. bool AllGatherCPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  40. const std::vector<kernel::AddressPtr> & /*workspace*/,
  41. const std::vector<kernel::AddressPtr> &outputs) {
  42. auto input_addr = reinterpret_cast<float *>(inputs[0]->addr);
  43. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  44. auto input_data_num = inputs[0]->size / sizeof(float);
  45. auto mpi_instance = device::cpu::MPIAdapter::Instance();
  46. MS_EXCEPTION_IF_NULL(mpi_instance);
  47. return mpi_instance->AllGather(input_addr, output_addr, ranks_group_, input_data_num);
  48. }
  49. } // namespace kernel
  50. } // namespace mindspore