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
17 #include "ops/constant_of_shape.h"
18 #include "ops/op_utils.h"
19 #include "utils/check_convert_utils.h"
20 #include "abstract/primitive_infer_map.h"
21
22 namespace mindspore {
23 namespace ops {
24 namespace {
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)25 abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
26 MS_EXCEPTION_IF_NULL(primitive);
27 (void)CheckAndConvertUtils::CheckInteger("input args size", SizeToLong(input_args.size()), kEqual, 1,
28 "ConstantOfShape");
29 auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
30 return std::make_shared<abstract::Shape>(input_shape);
31 }
32
InferType(const PrimitivePtr & primitive)33 TypePtr InferType(const PrimitivePtr &primitive) {
34 MS_EXCEPTION_IF_NULL(primitive);
35 auto data_type = TypeId(GetValue<int64_t>(primitive->GetAttr(kDataType)));
36 return TypeIdToType(data_type);
37 }
38 } // namespace
39
Init(int64_t data_type,const std::vector<float> & value)40 void ConstantOfShape::Init(int64_t data_type, const std::vector<float> &value) {
41 this->set_data_type(data_type);
42 this->set_value(value);
43 }
44
set_data_type(int64_t data_type)45 void ConstantOfShape::set_data_type(int64_t data_type) { (void)this->AddAttr(kDataType, MakeValue(data_type)); }
46
get_data_type() const47 int64_t ConstantOfShape::get_data_type() const {
48 auto value_ptr = this->GetAttr(kDataType);
49 return GetValue<int64_t>(value_ptr);
50 }
51
set_value(const std::vector<float> & value)52 void ConstantOfShape::set_value(const std::vector<float> &value) { (void)this->AddAttr(kValue, MakeValue(value)); }
53
get_value() const54 std::vector<float> ConstantOfShape::get_value() const {
55 auto value_ptr = this->GetAttr(kValue);
56 return GetValue<std::vector<float>>(value_ptr);
57 }
ConstantOfShapeInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)58 AbstractBasePtr ConstantOfShapeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
59 const std::vector<AbstractBasePtr> &input_args) {
60 return std::make_shared<abstract::AbstractTensor>(InferType(primitive), InferShape(primitive, input_args)->shape());
61 }
62 REGISTER_PRIMITIVE_C(kNameConstantOfShape, ConstantOfShape);
63 } // namespace ops
64 } // namespace mindspore
65