• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-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 "ir/cell.h"
18 
19 #include "abstract/abstract_value.h"
20 #include "utils/ms_utils.h"
21 
22 namespace mindspore {
MakeId()23 static std::string MakeId() {
24   // Use atomic to make id generator thread safe.
25   static std::atomic<uint64_t> last_id{1};
26   return "C" + std::to_string(last_id.fetch_add(1, std::memory_order_relaxed));
27 }
28 
29 using mindspore::abstract::AbstractFunction;
30 
ToAbstract()31 abstract::AbstractBasePtr Cell::ToAbstract() { return nullptr; }
32 
Cell(const std::string & name)33 Cell::Cell(const std::string &name) : Named(name), id_(MakeId()) {}
34 
operator ==(const Value & other) const35 bool Cell::operator==(const Value &other) const {
36   if (other.isa<Cell>()) {
37     auto &other_prim = static_cast<const Cell &>(other);
38     return *this == other_prim;
39   } else {
40     return false;
41   }
42 }
43 
operator ==(const Cell & other) const44 bool Cell::operator==(const Cell &other) const {
45   if (name() != other.name()) {
46     return false;
47   }
48   return common::IsAttrsEqual(attrs_, other.attrs_);
49 }
50 
GetAttrString() const51 std::string Cell::GetAttrString() const {
52   std::ostringstream buffer;
53   bool begin = true;
54   buffer << "{" << std::endl;
55   for (auto &attr : attrs_) {
56     if (!begin) {
57       buffer << ", " << std::endl;
58     } else {
59       begin = false;
60     }
61     buffer << attr.first << ":" << attr.second->ToString();
62   }
63   buffer << "}";
64   return buffer.str();
65 }
66 
ToString() const67 std::string Cell::ToString() const {
68   std::ostringstream buffer;
69   buffer << "Cell " << name();
70   return buffer.str();
71 }
72 
DelAttr(const std::string & name)73 void Cell::DelAttr(const std::string &name) { attrs_.erase(name); }
74 }  // namespace mindspore
75