ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Context.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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#ifndef __CONTEXT_H__
18#define __CONTEXT_H__
19
20#include <caffe/proto/caffe.pb.h>
21
22#include <coco/IR.h>
23#include <coco/IR/Data.h>
24
25#include <cassert>
26#include <map>
27#include <string>
28
29namespace caffeimport
30{
31
32using LayerName = std::string;
33using BlobName = std::string;
34// Note: these two maybe evolved to a class
35using ShapeContext = std::map<BlobName, nncc::core::ADT::tensor::Shape>;
36using StoreContext = std::map<BlobName, coco::Bag *>;
37
39{
40public:
41 WeightContext(::caffe::NetParameter *caffemodel) : _caffemodel(caffemodel)
42 {
43 for (uint32_t n = 0; n < _caffemodel->layer_size(); ++n)
44 {
45 auto layer = _caffemodel->mutable_layer(n);
46
47 if (layer->has_name())
48 {
49 _data[layer->name()] = layer;
50 }
51 }
52 }
53
54public:
55 int blob_count(const LayerName &name)
56 {
57 if (_data.find(name) != _data.end())
58 return _data.at(name)->blobs_size();
59
60 assert(false);
61 return 0;
62 }
63
64 ::caffe::BlobProto *blob_get(const LayerName &name, uint32_t n)
65 {
66 if (_data.find(name) != _data.end())
67 return _data.at(name)->mutable_blobs(n);
68
69 assert(false);
70 return nullptr;
71 };
72
73private:
74 ::caffe::NetParameter *_caffemodel;
75 std::map<LayerName, ::caffe::LayerParameter *> _data;
76};
77
79{
80public:
84 : _module(module), _data(data), _block(block), _shape_ctx(shape_ctx), _bag_ctx(bag_ctx),
85 _weight_ctx(weight_ctx)
86 {
87 // DO NOTHING
88 }
89
92
93public:
94 coco::Module *module() { return _module; }
95 coco::Data *data() { return _data; }
96 coco::Block *block() { return _block; }
97 ShapeContext &shape_ctx() { return _shape_ctx; }
98 StoreContext &bag_ctx() { return _bag_ctx; }
99 WeightContext &weight_ctx() { return _weight_ctx; }
100
101private:
102 coco::Module *_module;
103 coco::Data *_data;
104 coco::Block *_block;
105 ShapeContext &_shape_ctx;
106 StoreContext &_bag_ctx;
107 WeightContext &_weight_ctx;
108};
109
110} // namespace caffeimport
111
112#endif // __CONTEXT_H__
ShapeContext & shape_ctx()
Definition Context.h:97
GraphBuilderContext(coco::Module *module, coco::Data *data, coco::Block *block, ShapeContext &shape_ctx, StoreContext &bag_ctx, WeightContext &weight_ctx)
Definition Context.h:81
GraphBuilderContext(const GraphBuilderContext &)=delete
GraphBuilderContext(GraphBuilderContext &&)=delete
WeightContext & weight_ctx()
Definition Context.h:99
WeightContext(::caffe::NetParameter *caffemodel)
Definition Context.h:41
int blob_count(const LayerName &name)
Definition Context.h:55
::caffe::BlobProto * blob_get(const LayerName &name, uint32_t n)
Definition Context.h:64
A unit of (grouped) instructions.
Definition Block.h:40
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
std::map< BlobName, coco::Bag * > StoreContext
Definition Context.h:36
std::string BlobName
Definition Context.h:33
std::string LayerName
Definition Context.h:32
std::map< BlobName, nncc::core::ADT::tensor::Shape > ShapeContext
Definition Context.h:35
Core coco entity for constant weights.
Definition Data.h:31