ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Reshape.cpp
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#include "Reshape.h"
18
19#include "IRBuilder.h"
20#include "GraphBuilder.h"
21
22#include <morph/tflite.h>
23#include <coco/IR/Module.h>
25
27#include <schema_generated.h>
28
29#include <cassert>
30
31using namespace nncc::core::ADT;
32using namespace morph::tflite;
33
34namespace tflimport
35{
36
37void ReshapeGraphBuilder::build(const tflite::Operator *op, GraphBuilderContext *context) const
38{
39 assert(context != nullptr); // check if init(..) is called
40
41 coco::Module *m = context->m();
42 coco::Block *blk = context->block();
43 TensorBags &bags = context->bags();
44
45 IndexVector opinputs = as_index_vector(op->inputs());
46 IndexVector opoutputs = as_index_vector(op->outputs());
47
48 // these are fixed in tflite
49 // input index 0 : input feature
50 // input index 1 : output shape (int32_t), (optional or not, is not clear)
51 // output index 0 : output feature
52 assert(opinputs.size() == 1 || opinputs.size() == 2);
53 assert(opoutputs.size() == 1);
54
55 // Note: there are actually 3 places where we can get output shape from
56 // current TF lite implementation. From output operand shape, second input,
57 // and ReshapeOption (new_shape). Here we use output operand shape
58 int ifm_idx = opinputs.at(0);
59 int ofm_idx = opoutputs.at(0);
60
61 auto ifm_bag = bags.bag(ifm_idx);
62 auto ofm_bag = bags.bag(ofm_idx);
63
64 // TODO: move to InstrBuilder as 'shuffle_elements()'
65 // Create a 1:1 shuffle instruction from ifm into ofm
66 // Note: Reshape is change of shape information and there is no value change
67 // in the bag itself. We implement this as just make a element wise copy of
68 // the bag from input to output. So there is no need of 'reshape' operator
69 auto shuffle_ins = m->entity()->instr()->create<coco::Shuffle>();
70 auto num_elem = ifm_bag->size();
71
72 assert(num_elem == ofm_bag->size());
73
74 shuffle_ins->from(ifm_bag);
75 shuffle_ins->into(ofm_bag);
76
77 for (uint32_t n = 0; n < num_elem; ++n)
78 {
79 const auto from = coco::ElemID(n);
80 const auto into = coco::ElemID(n);
81
82 shuffle_ins->insert(from, into);
83 }
84
85 // Append the instruction
86 blk->instr()->append(shuffle_ins);
87}
88
89} // namespace tflimport
A unit of (grouped) instructions.
Definition Block.h:40
InstrList * instr(void)
Definition Block.h:65
void append(Child *child)
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
Generic element transfer.
Definition Instrs.h:116
uint32_t size(void) const
Return the number of Element-wise transfers.
Definition Shuffle.cpp:22
Class to store context to build IR from tflite.
Definition Context.h:133
void build(const tflite::Operator *op, GraphBuilderContext *) const override
Definition Reshape.cpp:37
Pre-creates coco:Bags for each operands(tensors)
Definition TensorBags.h:38
coco::Bag * bag(int32_t tensor_id)
Definition TensorBags.h:52
std::vector< int32_t > IndexVector
Definition Convert.h:29
IndexVector as_index_vector(const flatbuffers::Vector< int32_t > *array)
Converts flatbuffers::Vector to IndexVector.
Definition Convert.cpp:28