summaryrefslogtreecommitdiff
path: root/compiler/enco/frontend/caffe/src/Frontend.cpp
blob: 7d2b3d36cb1c1b0cb5c32268aa31f9cb77944724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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.
 */

#include "Frontend.h"
#include "Context.h"
#include "GraphBuilderRegistry.h"

#include <nncc/core/ADT/tensor/Shape.h>
#include <nncc/core/ADT/tensor/LexicalLayout.h>

#include <map>
#include <set>
#include <string>

#include <cassert>
#include <stdexcept>

using namespace nncc::core::ADT;

using tensor::LexicalLayout;

Frontend::Frontend() : _prototxt{new ::caffe::NetParameter}, _caffemodel{new ::caffe::NetParameter}
{
  // DO NOTHING
}

enco::Bundle Frontend::load(void) const
{
  auto module = coco::Module::create();
  auto blk = module->entity()->block()->create();
  module->block()->append(blk);

  auto data = coco::Data::create();

  // For weight access
  caffeimport::WeightContext weight_ctx(_caffemodel.get());

  // For inter-layer communication
  std::map<std::string, tensor::Shape> shape_ctx;
  std::map<std::string, coco::Bag *> bag_ctx;

  std::set<std::string> bags;
  std::map<std::string, uint32_t> def_count;
  std::map<std::string, uint32_t> use_count;

  auto def = [&bags, &def_count, &use_count](const std::string &name) {
    if (bags.find(name) == bags.end())
    {
      bags.insert(name);
      def_count[name] = 0;
      use_count[name] = 0;
    }

    def_count.at(name) += 1;
  };

  auto use = [&use_count](const std::string &name) { use_count.at(name) += 1; };

  auto outputs = [&bags, &def_count, &use_count](void) {
    std::set<std::string> res;

    for (const auto &bag : bags)
    {
      if (def_count.at(bag) > use_count.at(bag))
      {
        res.insert(bag);
      }
    }

    return res;
  };

  caffeimport::GraphBuilderContext opbuilder_context(module.get(), data.get(), blk, shape_ctx,
                                                     bag_ctx, weight_ctx);

  for (const auto &layer : _prototxt->layer())
  {
    assert(layer.has_name());
    assert(layer.has_type());

    for (uint32_t n = 0; n < layer.top().size(); ++n)
    {
      def(layer.top(n));
    }

    for (uint32_t n = 0; n < layer.bottom().size(); ++n)
    {
      use(layer.bottom(n));
    }

    if (const auto *graph_builder = caffeimport::GraphBuilderRegistry::get().lookup(layer.type()))
    {
      graph_builder->build(layer, &opbuilder_context);
    }
    else
    {
      throw std::runtime_error{"Not supported: " + layer.type()};
    }
  }

  // Finalize: Create output for each top blob
  for (const auto &name : outputs())
  {
    const auto &shape = shape_ctx.at(name);
    auto bag = bag_ctx.at(name);

    auto output = module->entity()->output()->create(shape);

    output->bag(bag);
    output->name(name);
    output->reorder<LexicalLayout>();

    module->output()->insert(output);
  }

  enco::Bundle bundle;

  bundle.module(std::move(module));
  bundle.data(std::move(data));

  return std::move(bundle);
}