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.

range.cc 4.2 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "ops/range.h"
  17. #include <string>
  18. #include <algorithm>
  19. #include <memory>
  20. #include <set>
  21. #include <vector>
  22. #include "ops/op_utils.h"
  23. #include "utils/check_convert_utils.h"
  24. #include "abstract/primitive_infer_map.h"
  25. namespace mindspore {
  26. namespace ops {
  27. void Range::set_d_type(const int64_t d_type) { this->AddAttr(kDType, MakeValue(d_type)); }
  28. int64_t Range::get_d_type() const {
  29. auto value_ptr = GetAttr(kDType);
  30. return GetValue<int64_t>(value_ptr);
  31. }
  32. void Range::set_start(const int64_t start) { this->AddAttr(kStart, MakeValue(start)); }
  33. int64_t Range::get_start() const {
  34. auto value_ptr = GetAttr(kStart);
  35. return GetValue<int64_t>(value_ptr);
  36. }
  37. void Range::set_limit(const int64_t limit) { this->AddAttr(kLimit, MakeValue(limit)); }
  38. int64_t Range::get_limit() const {
  39. auto value_ptr = GetAttr(kLimit);
  40. return GetValue<int64_t>(value_ptr);
  41. }
  42. void Range::set_delta(const int64_t delta) { this->AddAttr(kDelta, MakeValue(delta)); }
  43. int64_t Range::get_delta() const {
  44. auto value_ptr = GetAttr(kDelta);
  45. return GetValue<int64_t>(value_ptr);
  46. }
  47. void Range::Init(const int64_t d_type, const int64_t start, const int64_t limit, const int64_t delta) {
  48. this->set_d_type(d_type);
  49. this->set_start(start);
  50. this->set_limit(limit);
  51. this->set_delta(delta);
  52. }
  53. AbstractBasePtr RangeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
  54. const std::vector<AbstractBasePtr> &input_args) {
  55. MS_EXCEPTION_IF_NULL(primitive);
  56. auto prim = primitive->cast<PrimRangePtr>();
  57. MS_EXCEPTION_IF_NULL(prim);
  58. int64_t shape_size;
  59. TypeId dtype;
  60. if (input_args.size() == 3) {
  61. MS_EXCEPTION_IF_NULL(input_args[0]->BuildValue());
  62. MS_EXCEPTION_IF_NULL(input_args[1]->BuildValue());
  63. MS_EXCEPTION_IF_NULL(input_args[2]->BuildValue());
  64. auto start_tensor = input_args[0]->BuildValue()->cast<tensor::TensorPtr>();
  65. auto limit_tensor = input_args[1]->BuildValue()->cast<tensor::TensorPtr>();
  66. auto delta_tensor = input_args[2]->BuildValue()->cast<tensor::TensorPtr>();
  67. dtype = static_cast<TypeId>(start_tensor->data_type_c());
  68. switch (dtype) {
  69. case kNumberTypeInt:
  70. case kNumberTypeInt32: {
  71. auto start = *reinterpret_cast<int *>(start_tensor->data_c());
  72. auto limit = *reinterpret_cast<int *>(limit_tensor->data_c());
  73. auto delta = *reinterpret_cast<int *>(delta_tensor->data_c());
  74. shape_size =
  75. std::max(static_cast<int64_t>(std::ceil(static_cast<float>(limit - start) / delta)), static_cast<int64_t>(0));
  76. } break;
  77. case kNumberTypeFloat32:
  78. case kNumberTypeFloat: {
  79. auto start = *reinterpret_cast<float *>(start_tensor->data_c());
  80. auto limit = *reinterpret_cast<float *>(limit_tensor->data_c());
  81. auto delta = *reinterpret_cast<float *>(delta_tensor->data_c());
  82. shape_size =
  83. std::max(static_cast<int64_t>(std::ceil(static_cast<float>(limit - start) / delta)), static_cast<int64_t>(0));
  84. } break;
  85. default: {
  86. MS_LOG(EXCEPTION) << "Range has unsupported dataType: " << dtype;
  87. }
  88. }
  89. } else {
  90. int64_t start = prim->get_start();
  91. int64_t limit = prim->get_limit();
  92. int64_t delta = prim->get_delta();
  93. dtype = kNumberTypeInt32;
  94. shape_size =
  95. std::max(static_cast<int64_t>(std::ceil(LongToDouble(limit - start) / delta)), static_cast<int64_t>(0));
  96. }
  97. return std::make_shared<abstract::AbstractTensor>(
  98. TypeIdToType(dtype), std::make_shared<abstract::Shape>(std::vector<int64_t>{shape_size}));
  99. }
  100. REGISTER_PRIMITIVE_C(kNameRange, Range);
  101. } // namespace ops
  102. } // namespace mindspore