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 "backend/optimizer/common/fusion_id_allocator.h"
17 #include "backend/session/anf_runtime_algorithm.h"
18
19 namespace mindspore {
20 namespace opt {
FusionIdAllocator()21 FusionIdAllocator::FusionIdAllocator() { fusion_id = 0; }
22
~FusionIdAllocator()23 FusionIdAllocator::~FusionIdAllocator() {}
24
Init()25 void FusionIdAllocator::Init() { fusion_id = 0; }
26
AllocateFusionId()27 int64_t FusionIdAllocator::AllocateFusionId() {
28 fusion_id++;
29 return fusion_id;
30 }
31
HasFusionIdAttr(const AnfNodePtr & node) const32 bool FusionIdAllocator::HasFusionIdAttr(const AnfNodePtr &node) const {
33 MS_EXCEPTION_IF_NULL(node);
34 if (!node->isa<CNode>()) {
35 return false;
36 }
37 auto cnode = node->cast<CNodePtr>();
38 return AnfAlgo::HasNodeAttr(kAttrFusionId, cnode);
39 }
40
GetFusionId(const AnfNodePtr & node)41 int64_t FusionIdAllocator::GetFusionId(const AnfNodePtr &node) {
42 if (HasFusionIdAttr(node)) {
43 return AnfAlgo::GetNodeAttr<int64_t>(node, kAttrFusionId);
44 }
45 return -1;
46 }
47
SetFusionId(const AnfNodePtr & node,int64_t id)48 void FusionIdAllocator::SetFusionId(const AnfNodePtr &node, int64_t id) {
49 ValuePtr fusion_id_v = MakeValue(id);
50 AnfAlgo::SetNodeAttr(kAttrFusionId, fusion_id_v, node);
51 }
52 } // namespace opt
53 } // namespace mindspore
54