/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include "common/log.h" #include "common/variant.h" #include "framework/framework.pb.h" namespace paddle_mobile { namespace framework { class BlockDesc; class Attribute { public: static Attribute GetAttrValue(const proto::OpDesc::Attr &attr_desc) { // std::cout << "begin get attr value" << std::endl; Attribute attr; switch (attr_desc.type()) { case proto::AttrType::BOOLEAN: { attr.Set(attr_desc.b()); break; } case proto::AttrType::INT: { attr.Set(attr_desc.i()); break; } case proto::AttrType::FLOAT: { attr.Set(attr_desc.f()); break; } case proto::AttrType::STRING: { attr.Set(attr_desc.s()); break; } case proto::AttrType::BOOLEANS: { std::vector val(attr_desc.bools_size()); for (int i = 0; i < attr_desc.bools_size(); ++i) { val[i] = attr_desc.bools(i); } attr.Set>(val); break; } case proto::AttrType::INTS: { std::vector val(attr_desc.ints_size()); for (int i = 0; i < attr_desc.ints_size(); ++i) { val[i] = attr_desc.ints(i); } attr.Set>(val); break; } case proto::AttrType::FLOATS: { std::vector val(attr_desc.floats_size()); for (int i = 0; i < attr_desc.floats_size(); ++i) { val[i] = attr_desc.floats(i); } attr.Set>(val); break; } case proto::AttrType::STRINGS: { std::vector val(attr_desc.strings_size()); for (int i = 0; i < attr_desc.strings_size(); ++i) { val[i] = attr_desc.strings(i); } attr.Set>(val); break; } case proto::AttrType::LONG: { attr.Set(attr_desc.l()); break; } default: // std::cout << " not support " << std::endl; break; } // std::cout << "end get attr value" << std::endl; return attr; } Attribute() {} template Attribute &Set(Args &&... args) { variant_.Set(args...); return *this; } template T &Get() const { return variant_.Get(); } template static typename Vistor::type_t ApplyVistor(Vistor vistor, Attribute attr) { if (attr.variant_.TypeId() == typeid(int).hash_code()) { return vistor(attr.variant_.Get()); } else if (attr.variant_.TypeId() == typeid(float).hash_code()) { return vistor(attr.variant_.Get()); } else if (attr.variant_.TypeId() == typeid(std::string).hash_code()) { return vistor(attr.variant_.Get()); } else if (attr.variant_.TypeId() == typeid(std::vector).hash_code()) { return vistor(attr.variant_.Get>()); } else if (attr.variant_.TypeId() == typeid(std::vector).hash_code()) { return vistor(attr.variant_.Get>()); } else if (attr.variant_.TypeId() == typeid(std::vector).hash_code()) { return vistor(attr.variant_.Get>()); } else if (attr.variant_.TypeId() == typeid(bool).hash_code()) { return vistor(attr.variant_.Get()); } else if (attr.variant_.TypeId() == typeid(std::vector).hash_code()) { return vistor(attr.variant_.Get>()); } else if (attr.variant_.TypeId() == typeid(int64_t).hash_code()) { return vistor(attr.variant_.Get()); } else { throw std::bad_exception(); } } private: Variant, std::vector, std::vector, bool, std::vector, BlockDesc *, int64_t> variant_; }; using AttributeMap = std::unordered_map; class AttrReader { public: explicit AttrReader(const AttributeMap &attrs) : attrs_(attrs) {} template inline T Get(const std::string &name) const { // PADDLE_ENFORCE(attrs_.count(name) != 0, "%s should // be in // AttributeMap", // name); return ((Attribute)attrs_.at(name)).Get(); } private: const AttributeMap &attrs_; }; Print &operator<<(Print &printer, const Attribute &op_desc); } // namespace framework } // namespace paddle_mobile