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.

fill.cc 2.8 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/fill.h"
  17. #include <memory>
  18. #include "ops/op_utils.h"
  19. #include "utils/check_convert_utils.h"
  20. #include "utils/tensor_construct_utils.h"
  21. namespace mindspore {
  22. namespace ops {
  23. AbstractBasePtr FillInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
  24. const std::vector<AbstractBasePtr> &input_args) {
  25. MS_EXCEPTION_IF_NULL(primitive);
  26. auto prim_name = primitive->name();
  27. const int64_t input_num = 3;
  28. (void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
  29. for (const auto &item : input_args) {
  30. MS_EXCEPTION_IF_NULL(item);
  31. }
  32. auto input_dtype = input_args[kInputIndex0]->cast<abstract::AbstractTypePtr>();
  33. MS_EXCEPTION_IF_NULL(input_dtype);
  34. auto dtype_value = input_dtype->BuildValue();
  35. MS_EXCEPTION_IF_NULL(dtype_value);
  36. auto dtype = dtype_value->cast<TypePtr>();
  37. MS_EXCEPTION_IF_NULL(dtype);
  38. auto valid_types = common_valid_types;
  39. valid_types.insert(kBool);
  40. (void)CheckAndConvertUtils::CheckTypeValid("output datatype", dtype, valid_types, prim_name);
  41. auto out_shape = GetValue<std::vector<int64_t>>(input_args[kInputIndex1]->BuildValue());
  42. auto x_type = input_args[kInputIndex2]->BuildType();
  43. auto x_type_id = x_type->type_id();
  44. auto x_value = input_args[kInputIndex2]->BuildValue();
  45. auto abs = std::make_shared<abstract::AbstractTensor>(dtype, std::make_shared<abstract::Shape>(out_shape));
  46. tensor::TensorPtr tensor = std::make_shared<tensor::Tensor>(x_type_id, out_shape);
  47. MS_EXCEPTION_IF_NULL(tensor);
  48. auto mem_size = IntToSize(tensor->ElementsNum());
  49. if (x_type_id == kNumberTypeInt) {
  50. auto int_value = GetValue<int>(x_value);
  51. SetTensorData(tensor->data_c(), int_value, mem_size);
  52. } else if (x_type_id == kNumberTypeFloat || x_type_id == kNumberTypeFloat32) {
  53. auto float_value = GetValue<float>(x_value);
  54. SetTensorData(tensor->data_c(), float_value, mem_size);
  55. } else {
  56. MS_LOG(ERROR) << " Fill not supported to flod the constant type " << input_args[kInputIndex2]->ToString();
  57. }
  58. abs->set_value(tensor);
  59. return abs;
  60. }
  61. REGISTER_PRIMITIVE_C(kNameFill, Fill);
  62. } // namespace ops
  63. } // namespace mindspore