• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 "mindapi/ir/value.h"
18 #include "mindapi/ir/type.h"
19 #include "mindapi/ir/abstract.h"
20 #include "mindapi/src/helper.h"
21 #include "abstract/abstract_value.h"
22 #include "ir/anf.h"
23 #include "ir/dtype/type.h"
24 #include "ir/value.h"
25 #include "ir/scalar.h"
26 
27 namespace mindspore::api {
28 using ValueImpl = mindspore::Value;
29 using ValueSequenceImpl = mindspore::ValueSequence;
30 using ValueTupleImpl = mindspore::ValueTuple;
31 using StringImmImpl = mindspore::StringImm;
32 using ScalarImpl = mindspore::Scalar;
33 using BoolImmImpl = mindspore::BoolImm;
34 using IntegerImmImpl = mindspore::IntegerImm;
35 using Int8ImmImpl = mindspore::Int8Imm;
36 using Int16ImmImpl = mindspore::Int16Imm;
37 using Int32ImmImpl = mindspore::Int32Imm;
38 using Int64ImmImpl = mindspore::Int64Imm;
39 using UInt8ImmImpl = mindspore::UInt8Imm;
40 using FloatImmImpl = mindspore::FloatImm;
41 using FP32ImmImpl = mindspore::FP32Imm;
42 using FP64ImmImpl = mindspore::FP64Imm;
43 
44 MIND_API_BASE_IMPL(Value, ValueImpl, Base);
45 
type() const46 TypePtr Value::type() const {
47   auto t = ToRef<ValueImpl>(impl_).type();
48   return ToWrapper<Type>(t);
49 }
50 
ToAbstract() const51 AbstractBasePtr Value::ToAbstract() const {
52   auto abs = ToRef<ValueImpl>(impl_).ToAbstract();
53   return ToWrapper<AbstractBase>(abs);
54 }
55 
56 MIND_API_BASE_IMPL(ValueSequence, ValueSequenceImpl, Value);
57 
size() const58 std::size_t ValueSequence::size() const { return ToRef<ValueSequenceImpl>(impl_).size(); }
59 
value() const60 std::vector<ValuePtr> ValueSequence::value() const {
61   auto &elements = ToRef<ValueSequenceImpl>(impl_).value();
62   return ToWrapperVector<Value>(elements);
63 }
64 
65 MIND_API_BASE_IMPL(ValueTuple, ValueTupleImpl, ValueSequence);
66 
ValueTuple(const std::vector<ValuePtr> & elements)67 ValueTuple::ValueTuple(const std::vector<ValuePtr> &elements)
68     : ValueSequence(std::make_shared<ValueTupleImpl>(ToImplVector<ValueImpl>(elements))) {}
69 
70 MIND_API_BASE_IMPL(StringImm, StringImmImpl, Value);
71 
StringImm(const std::string & str)72 StringImm::StringImm(const std::string &str) : Value(std::make_shared<StringImmImpl>(str)) {}
73 
value() const74 const std::string &StringImm::value() const { return ToRef<StringImmImpl>(impl_).value(); }
75 
76 MIND_API_BASE_IMPL(Scalar, ScalarImpl, Value);
77 
78 MIND_API_BASE_IMPL(BoolImm, BoolImmImpl, Scalar);
79 
BoolImm(bool b)80 BoolImm::BoolImm(bool b) : Scalar(std::make_shared<BoolImmImpl>(b)) {}
81 
value() const82 bool BoolImm::value() const { return ToRef<BoolImmImpl>(impl_).value(); }
83 
84 MIND_API_BASE_IMPL(IntegerImm, IntegerImmImpl, Scalar);
85 
86 MIND_API_BASE_IMPL(Int8Imm, Int8ImmImpl, IntegerImm);
87 
Int8Imm(int8_t value)88 Int8Imm::Int8Imm(int8_t value) : IntegerImm(std::make_shared<Int8ImmImpl>(value)) {}
89 
value() const90 int8_t Int8Imm::value() const { return ToRef<Int8ImmImpl>(impl_).value(); }
91 
92 MIND_API_BASE_IMPL(Int16Imm, Int16ImmImpl, IntegerImm);
93 
Int16Imm(int16_t value)94 Int16Imm::Int16Imm(int16_t value) : IntegerImm(std::make_shared<Int16ImmImpl>(value)) {}
95 
value() const96 int16_t Int16Imm::value() const { return ToRef<Int16ImmImpl>(impl_).value(); }
97 
98 MIND_API_BASE_IMPL(Int32Imm, Int32ImmImpl, IntegerImm);
99 
Int32Imm(int32_t value)100 Int32Imm::Int32Imm(int32_t value) : IntegerImm(std::make_shared<Int32ImmImpl>(value)) {}
101 
value() const102 int32_t Int32Imm::value() const { return ToRef<Int32ImmImpl>(impl_).value(); }
103 
104 MIND_API_BASE_IMPL(Int64Imm, Int64ImmImpl, IntegerImm);
105 
Int64Imm(int64_t value)106 Int64Imm::Int64Imm(int64_t value) : IntegerImm(std::make_shared<Int64ImmImpl>(value)) {}
107 
value() const108 int64_t Int64Imm::value() const { return ToRef<Int64ImmImpl>(impl_).value(); }
109 
110 MIND_API_BASE_IMPL(UInt8Imm, UInt8ImmImpl, IntegerImm);
111 
UInt8Imm(uint8_t value)112 UInt8Imm::UInt8Imm(uint8_t value) : UInt8Imm(std::make_shared<UInt8ImmImpl>(value)) {}
113 
value() const114 uint8_t UInt8Imm::value() const { return ToRef<UInt8ImmImpl>(impl_).value(); }
115 
116 MIND_API_BASE_IMPL(FloatImm, FloatImmImpl, Scalar);
117 
118 MIND_API_BASE_IMPL(FP32Imm, FP32ImmImpl, FloatImm);
119 
FP32Imm(float value)120 FP32Imm::FP32Imm(float value) : FloatImm(std::make_shared<FP32ImmImpl>(value)) {}
121 
value() const122 float FP32Imm::value() const { return ToRef<FP32ImmImpl>(impl_).value(); }
123 
124 MIND_API_BASE_IMPL(FP64Imm, FP64ImmImpl, FloatImm);
125 
FP64Imm(double value)126 FP64Imm::FP64Imm(double value) : FloatImm(std::make_shared<FP64ImmImpl>(value)) {}
127 
value() const128 double FP64Imm::value() const { return ToRef<FP64ImmImpl>(impl_).value(); }
129 }  // namespace mindspore::api
130