blob: 37b660e4a6aaab2ad1621e2a57a410c9693f9223 (
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
|
/*
* Copyright (c) 2019 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 "BiasAddCanonicalizer.h"
#include "Dialect/TFDialect.h"
#include "Dialect/TFNodes.h"
#include "Dialect/TFNodeVisitor.h"
#include "Dialect/TFNodeImpl.h"
#include <moco/tf/Names.h>
#include <moco/Log.h>
#include <plier/tf/Convert.h>
namespace
{
using plier::tf::DataLayout;
bool canonicalize_biasadd(loco::Graph *graph, moco::tf::TFBiasAdd *node)
{
LOGGER(l);
/**
* @note This will replace TFBiasAdd node with Canonical BiasEncode + TensorBiasAdd
*
* Before
* A -- TFBiasAdd - C
* B -/
*
* After
* A -- TFBiasAdd -
* B -/
* A --------------- TensorBiasAdd - C
* B - BiasEncode -/
*
* Where
* A : value of TFBiasAdd
* B : bias of TFBiasAdd
* C : a node that uses TFBiasAdd as an input
* TFBiasAdd is disconnected from node C
* A and B are drawn twice to simplify the diagram
*/
INFO(l) << "TFNodeCanonicalize TFBiasAdd begin";
// tensorflow data_format: one of NHWC or NCHW.
auto data_layout = plier::tf::as_data_layout(node->data_layout());
// creating loco nodes
auto bias_enc = graph->nodes()->create<loco::BiasEncode>();
auto bias_add = graph->nodes()->create<loco::TensorBiasAdd>();
{
if (data_layout == DataLayout::NHWC)
{
INFO(l) << "TFNodeCanonicalize TFBiasAdd axis 3";
bias_add->axis(3);
}
else if (data_layout == DataLayout::NCHW)
{
INFO(l) << "TFNodeCanonicalize TFBiasAdd axis 1";
bias_add->axis(1); // Channel
// Note: the following descrition of TF 1.13 at
// https://www.tensorflow.org/api_docs/python/tf/nn/bias_add seems wrong:
// "bias: A 1-D Tensor with size matching the last dimension of value."
// because providing the size of W (last dimension) to bias throws an error with TensorFlow
}
}
auto node_A = node->value();
auto node_B = node->bias();
// update connections
bias_add->value(node_A);
bias_add->bias(bias_enc);
bias_enc->input(node_B);
// replace old with new : about C in above note
replace(node).with(bias_add);
INFO(l) << "TFNodeCanonicalize TFBiasAdd done";
return true;
}
} // namespace
namespace moco
{
namespace tf
{
bool BiasAddCanonicalizer::run(loco::Graph *graph)
{
auto active_nodes = loco::active_nodes(loco::output_nodes(graph));
bool changed = false;
for (auto node : active_nodes)
{
if (node->dialect() == TFDialect::get())
{
auto tf_biasadd = dynamic_cast<moco::tf::TFBiasAdd *>(node);
if (tf_biasadd != nullptr)
{
if (canonicalize_biasadd(graph, tf_biasadd))
changed = true;
}
}
}
return changed;
}
} // namespace tf
} // namespace moco
|