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 "ops/base_operator.h"
18 #include "mindapi/src/helper.h"
19 #include "ops/op_name.h"
20 #include "ops/primitive_c.h"
21
22 namespace mindspore {
23 namespace ops {
24 MIND_API_BASE_IMPL(BaseOperator, PrimitiveC, api::Primitive);
BaseOperator(const std::string & name)25 BaseOperator::BaseOperator(const std::string &name) : api::Primitive(std::make_shared<PrimitiveC>(name)) {}
26
GetPrim()27 PrimitiveCPtr BaseOperator::GetPrim() {
28 PrimitiveCPtr res = std::dynamic_pointer_cast<PrimitiveC>(impl_);
29 return res;
30 }
31
set_batch_rank(int64_t batch_rank)32 void BaseOperator::set_batch_rank(int64_t batch_rank) { (void)this->AddAttr(kBatchRank, api::MakeValue(batch_rank)); }
33
get_batch_rank() const34 int64_t BaseOperator::get_batch_rank() const {
35 if (this->HasAttr(kBatchRank)) {
36 auto value_ptr = this->GetAttr(kBatchRank);
37 return GetValue<int64_t>(value_ptr);
38 }
39 return 0;
40 }
41
InitIOName(const std::vector<std::string> & inputs_name,const std::vector<std::string> & outputs_name)42 void BaseOperator::InitIOName(const std::vector<std::string> &inputs_name,
43 const std::vector<std::string> &outputs_name) {
44 (void)AddAttr("input_names", api::MakeValue(inputs_name));
45 (void)AddAttr("output_names", api::MakeValue(outputs_name));
46 }
47
GetInstance()48 OperatorRegister &OperatorRegister::GetInstance() {
49 static OperatorRegister instance;
50 return instance;
51 }
52
GetOperatorMap() const53 const std::map<std::string, OperatorDefineFunc> &OperatorRegister::GetOperatorMap() const { return operator_fns_; }
SetOperatorMap(const std::string & kname,const OperatorDefineFunc & fn)54 void OperatorRegister::SetOperatorMap(const std::string &kname, const OperatorDefineFunc &fn) {
55 operator_fns_[kname] = fn;
56 }
57 } // namespace ops
58 } // namespace mindspore
59