attribute.h 4.4 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/

#pragma once

#include "common/variant.h"
#include "framework.pb.h"

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
25
namespace framework {
朔-望's avatar
朔-望 已提交
26

朔-望's avatar
朔-望 已提交
27
class BlockDesc;
朔-望's avatar
朔-望 已提交
28

朔-望's avatar
朔-望 已提交
29
class Attribute {
朔-望's avatar
朔-望 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  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<bool>(attr_desc.b());
            break;
        }
        case proto::AttrType::INT: {
            attr.Set<int>(attr_desc.i());
            break;
        }
        case proto::AttrType::FLOAT: {
            attr.Set<float>(attr_desc.f());
            break;
        }
        case proto::AttrType::STRING: {
            attr.Set<std::string>(attr_desc.s());
            break;
        }
        case proto::AttrType::BOOLEANS: {
            std::vector<bool> val(attr_desc.bools_size());
            for (int i = 0; i < attr_desc.bools_size(); ++i) {
                val[i] = attr_desc.bools(i);
            }
            attr.Set<std::vector<bool>>(val);
            break;
        }
        case proto::AttrType::INTS: {
            std::vector<int> val(attr_desc.ints_size());
            for (int i = 0; i < attr_desc.ints_size(); ++i) {
                val[i] = attr_desc.ints(i);
            }
            attr.Set<std::vector<int>>(val);
            break;
        }
        case proto::AttrType::FLOATS: {
            std::vector<float> val(attr_desc.floats_size());
            for (int i = 0; i < attr_desc.floats_size(); ++i) {
                val[i] = attr_desc.floats(i);
            }
            attr.Set<std::vector<float>>(val);
            break;
        }
        case proto::AttrType::STRINGS: {
            std::vector<std::string> val(attr_desc.strings_size());
            for (int i = 0; i < attr_desc.strings_size(); ++i) {
                val[i] = attr_desc.strings(i);
            }
            attr.Set<std::vector<std::string>>(val);
            break;
        }
        case proto::AttrType::LONG: {
            attr.Set<int64_t>(attr_desc.l());
            break;
        }
        default:
            //        std::cout << " not support " << std::endl;
            break;
        }
        //    std::cout << "end get attr value" << std::endl;
        return attr;
朔-望's avatar
朔-望 已提交
93
    }
朔-望's avatar
朔-望 已提交
94

朔-望's avatar
朔-望 已提交
95 96 97 98 99
    Attribute() {}
    template <typename T, typename... Args> Attribute &Set(Args &&... args) {
        variant_.Set<T>(args...);
        return *this;
    }
朔-望's avatar
朔-望 已提交
100

朔-望's avatar
朔-望 已提交
101
    template <typename T> T &Get() const { return variant_.Get<T>(); }
朔-望's avatar
朔-望 已提交
102

朔-望's avatar
朔-望 已提交
103 104 105 106 107
  private:
    Variant<int, float, std::string, std::vector<int>, std::vector<float>,
            std::vector<std::string>, bool, std::vector<bool>, BlockDesc *,
            int64_t>
        variant_;
朔-望's avatar
朔-望 已提交
108
};
朔-望's avatar
朔-望 已提交
109

朔-望's avatar
朔-望 已提交
110
using AttributeMap = std::unordered_map<std::string, Attribute>;
朔-望's avatar
朔-望 已提交
111

朔-望's avatar
朔-望 已提交
112
class AttrReader {
朔-望's avatar
朔-望 已提交
113 114
  public:
    explicit AttrReader(const AttributeMap &attrs) : attrs_(attrs) {}
朔-望's avatar
朔-望 已提交
115

朔-望's avatar
朔-望 已提交
116 117 118 119 120 121 122
    template <typename T> 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<T>();
    }
朔-望's avatar
朔-望 已提交
123

朔-望's avatar
朔-望 已提交
124 125
  private:
    const AttributeMap &attrs_;
朔-望's avatar
朔-望 已提交
126
};
朔-望's avatar
朔-望 已提交
127

朔-望's avatar
朔-望 已提交
128
} // namespace framework
L
liuruilong 已提交
129
} // namespace paddle_mobile