1 /**
2 * Copyright 2019-2022 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 "ir/primitive.h"
18
19 #include <utility>
20 #include "abstract/abstract_function.h"
21 #include "utils/ms_utils.h"
22 #include "ops/op_def.h"
23
24 namespace mindspore {
MakeId()25 static uint64_t MakeId() {
26 // Use atomic to make id generator thread safe.
27 static std::atomic<uint64_t> last_id{1};
28 return last_id.fetch_add(1, std::memory_order_relaxed);
29 }
30
Primitive(const std::string & name,bool is_base,const PrimType prim_type,bool inplace_prim)31 Primitive::Primitive(const std::string &name, bool is_base, const PrimType prim_type, bool inplace_prim)
32 : Named(name),
33 prim_type_(prim_type),
34 is_base_(is_base),
35 has_signature_(false),
36 record_evaluate_add_attr_(false),
37 const_prim_(false),
38 inplace_prim_(inplace_prim),
39 id_(MakeId()) {}
40
Primitive(const std::string & name,const mindspore::HashMap<std::string,ValuePtr> & attrs,bool inplace_prim)41 Primitive::Primitive(const std::string &name, const mindspore::HashMap<std::string, ValuePtr> &attrs, bool inplace_prim)
42 : Named(name),
43 attrs_(attrs),
44 prim_type_(kPrimTypeBuiltIn),
45 is_base_(true),
46 has_signature_(false),
47 record_evaluate_add_attr_(false),
48 const_prim_(false),
49 inplace_prim_(inplace_prim),
50 id_(MakeId()) {}
51
Primitive(const Primitive & prim)52 Primitive::Primitive(const Primitive &prim)
53 : Named(prim),
54 attrs_(prim.attrs_),
55 evaluate_added_attrs_(prim.evaluate_added_attrs_),
56 instance_name_(prim.instance_name_),
57 prim_type_(prim.prim_type_),
58 is_base_(prim.is_base_),
59 has_signature_(prim.has_signature_),
60 signatures_(prim.signatures()),
61 record_evaluate_add_attr_(false),
62 const_prim_(false),
63 inplace_prim_(prim.inplace_prim_),
64 const_input_indexes_(prim.const_input_indexes_),
65 id_(prim.id_) {}
66
operator =(const Primitive & other)67 Primitive &Primitive::operator=(const Primitive &other) {
68 if (this == &other) {
69 return *this;
70 }
71 Named::operator=(other);
72 attrs_ = other.attrs_;
73 evaluate_added_attrs_ = other.evaluate_added_attrs_;
74 instance_name_ = other.instance_name_;
75 is_base_ = other.is_base_;
76 has_signature_ = other.has_signature_;
77 prim_type_ = other.prim_type_;
78 record_evaluate_add_attr_ = false;
79 const_prim_ = false;
80 inplace_prim_ = other.inplace_prim_;
81 id_ = other.id_;
82 const_input_indexes_ = other.const_input_indexes_;
83 return *this;
84 }
85
ToAbstract()86 abstract::AbstractBasePtr Primitive::ToAbstract() {
87 return std::make_shared<abstract::PrimitiveAbstractClosure>(shared_from_base<Primitive>(), nullptr);
88 }
89
operator ==(const Value & other) const90 bool Primitive::operator==(const Value &other) const {
91 if (other.isa<Primitive>()) {
92 auto &other_prim = static_cast<const Primitive &>(other);
93 return *this == other_prim;
94 } else {
95 return false;
96 }
97 }
98
operator ==(const Primitive & other) const99 bool Primitive::operator==(const Primitive &other) const {
100 if (name() != other.name()) {
101 return false;
102 }
103 return common::IsAttrsEqual(attrs_, other.attrs_);
104 }
105
GetAttrsText() const106 std::string Primitive::GetAttrsText() const {
107 if (attrs_.empty()) {
108 return "";
109 }
110
111 std::ostringstream oss;
112 oss << "[";
113 bool is_first = true;
114 for (auto &attr : attrs_) {
115 if (is_first) {
116 is_first = false;
117 } else {
118 oss << ", ";
119 }
120 const std::string value = attr.second == nullptr ? "" : attr.second->DumpText();
121 oss << attr.first << ": " << value;
122 }
123 oss << "]";
124
125 return oss.str();
126 }
127
set_signatures(const std::vector<Signature> & signatures)128 void Primitive::set_signatures(const std::vector<Signature> &signatures) {
129 signatures_ = signatures;
130 set_has_signature(!signatures.empty());
131 }
132
ToString() const133 std::string Primitive::ToString() const {
134 if (mindspore::ops::IsPrimitiveFunction(name())) {
135 return "PrimFunc_" + name();
136 }
137 return name();
138 }
139 } // namespace mindspore
140