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.

broadcast.cc 2.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <set>
  17. #include <vector>
  18. #include <memory>
  19. #include "ops/broadcast.h"
  20. #include "ops/op_utils.h"
  21. #include "utils/check_convert_utils.h"
  22. namespace mindspore {
  23. namespace ops {
  24. void Broadcast::Init(const int64_t root_rank, const std::string &group) {
  25. this->set_root_rank(root_rank);
  26. this->set_group(group);
  27. }
  28. void Broadcast::set_root_rank(const int64_t root_rank) { this->AddAttr(kKeepProb, MakeValue(root_rank)); }
  29. void Broadcast::set_group(const std::string &group) {
  30. CheckAndConvertUtils::CheckString(kGroup, group, {"hccl_world_group", "hccl_world_group"}, this->name());
  31. this->AddAttr(kGroup, MakeValue(group));
  32. }
  33. int64_t Broadcast::get_root_rank() const {
  34. auto value_ptr = this->GetAttr(kRootRank);
  35. return GetValue<float>(value_ptr);
  36. }
  37. std::string Broadcast::get_group() const {
  38. auto value_ptr = this->GetAttr(kGroup);
  39. return GetValue<std::string>(value_ptr);
  40. }
  41. AbstractBasePtr BroadcastInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
  42. const std::vector<AbstractBasePtr> &input_args) {
  43. MS_EXCEPTION_IF_NULL(primitive);
  44. auto prim_name = primitive->name();
  45. for (const auto &item : input_args) {
  46. MS_EXCEPTION_IF_NULL(item);
  47. }
  48. // infer shape
  49. auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
  50. // infer type
  51. auto x_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
  52. std::vector<TypePtr> output_types;
  53. const std::set<TypePtr> valid_types = {kInt8, kInt32, kFloat16, kFloat32};
  54. for (size_t i = 0; i < input_args.size(); i++) {
  55. auto out_type = input_args[i]->BuildType()->cast<TensorTypePtr>()->element();
  56. output_types.push_back(out_type);
  57. CheckAndConvertUtils::CheckTensorTypeValid("index_type", out_type, valid_types, prim_name);
  58. }
  59. return std::make_shared<abstract::AbstractTensor>(x_type, in_shape);
  60. }
  61. REGISTER_PRIMITIVE_C(kNameBroadcast, Broadcast);
  62. } // namespace ops
  63. } // namespace mindspore