Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/optimizer/pad_fusion.h"
#include "core/graph/graph_utils.h"
#include "core/optimizer/initializer.h"
#include "core/optimizer/utils.h"
 
namespace onnxruntime {
 
bool VerifyNotCastChild(const Node& child_node) {
  if (!graph_utils::IsSupportedOptypeVersionAndDomain(child_node, "Conv", {1, 11, 22}) &&
      !graph_utils::IsSupportedOptypeVersionAndDomain(child_node, "AveragePool", {7, 10, 11, 19, 22}) &&
      !graph_utils::IsSupportedOptypeVersionAndDomain(child_node, "MaxPool", {1, 8, 10, 11, 12, 22})) {
    return false;
  }
 
  // Don't fuse if MaxPool has optional output indices tensor because output indices tensor
  // does not incorporate pad values. Basically if we allow the fusion, then dimension values
  // of input tensor < dimension values of input tensor without fusion.
  // This will cause the range of values for output indices tensor to be less than what it
  // should have been.
 
  if (child_node.OutputDefs().size() > 1) {
    return false;
  }
 
  // conv or maxpool node must use explicit padding to perform this fusion.
  if (child_node.GetAttributes().find("auto_pad") != child_node.GetAttributes().end() &&
      child_node.GetAttributes().at("auto_pad").s() != "NOTSET") {
    return false;
  }
 
  if (child_node.OpType() == "AveragePool") {
    // in case there's already padding and count_include_pad is 0, fusion can't be performed
    auto has_pad = false;
    if (child_node.GetAttributes().find("pads") != child_node.GetAttributes().end()) {
      auto const& pads_values = child_node.GetAttributes().at("pads").ints();
      if (!pads_values.empty()) {
        has_pad = std::any_of(pads_values.begin(), pads_values.end(), [](int64_t value) { return value != 0; });
      }
    }
    if (has_pad && child_node.GetAttributes().find("count_include_pad") != child_node.GetAttributes().end()) {
      if (child_node.GetAttributes().at("count_include_pad").i() == 0) {
        return false;
      }
    }
  }
 
  return true;
}
 
void UpdatePaddingAttribute(Node& child_node, const std::vector<int64_t>& pads_values, const size_t pads_size) {
  auto reset_pads = true;
  if (child_node.GetAttributes().find("pads") != child_node.GetAttributes().end()) {
    /* pads can be empty, overwrite pads attribute in this case */
    reset_pads = child_node.GetAttributes().at("pads").ints().empty();
  }
  if (reset_pads) {
    std::vector<int64_t> pads(pads_size - 4, 0);
    child_node.AddAttribute("pads", pads);
  }
 
  auto child_pads = child_node.GetMutableAttributes()["pads"].mutable_ints();
  const size_t child_pads_size = static_cast<size_t>(child_pads->size());
 
  for (size_t pads_index = 2, child_index = 0; pads_index < pads_size / 2; pads_index++, child_index++) {
    child_pads->Set(static_cast<int>(child_index),
                    child_pads->Get(static_cast<int>(child_index)) + pads_values[pads_index]);
    const size_t mirrored_child_index = child_index + (child_pads_size / 2);
    const size_t mirrored_pad_index = pads_index + (pads_size / 2);
    child_pads->Set(static_cast<int>(mirrored_child_index),
                    child_pads->Get(static_cast<int>(mirrored_child_index)) + pads_values[mirrored_pad_index]);
  }
 
  if (child_node.OpType() == "AveragePool") {
    child_node.AddAttribute("count_include_pad", static_cast<int64_t>(1));
  }
}
/*
 * Before:
 *     Pad
 *      |
 *    Cast (Optional)
 *      |
 *   Conv/MaxPool/AveragePool
 *
 * After:
 *    Cast (Optional)
 *      |
 *   Conv/MaxPool/AveragePool
 */
bool PadFusion::SatisfyCondition(const Graph& graph, const Node& node, const logging::Logger&) const {
  // if Pad has input axis, don't fuse it.
  if (!graph_utils::IsSupportedOptypeVersionAndDomain(node, "Pad", {1, 2, 11, 13, 18, 19, 21, 23, 24, 25}) ||
      node.GetOutputEdgesCount() != 1 ||
      node.InputDefs().size() > 3) {
    return false;
  }
 
  if (graph.NodeProducesGraphOutput(node)) {
    return false;
  }
 
  const NodeAttributes& pad_attributes = node.GetAttributes();
  if (pad_attributes.find("mode") != pad_attributes.end() &&
      pad_attributes.at("mode").s() != "constant") {
    return false;
  }
 
  // Since opset 11, <pads> and <constant_value> moved to inputs.
  // Both of these should be initializer because we have to verify the values.
  if (node.SinceVersion() >= 11) {
    if (!graph_utils::NodeArgIsConstant(graph, *node.InputDefs()[1]) ||
        (node.InputDefs().size() > 2 && !graph_utils::NodeArgIsConstant(graph, *node.InputDefs()[2]))) {
      return false;
    }
 
    // constant_value should be zero because Conv and MaxPool allow only 0 as padding value.
    if (node.InputDefs().size() > 2) {
      const auto* pad_constant_value_proto = graph_utils::GetConstantInitializer(graph, node.InputDefs()[2]->Name());
      Initializer pad_constant_value{graph, *pad_constant_value_proto, graph.ModelPath()};
      if (std::any_of(pad_constant_value.DataAsByteSpan().begin(), pad_constant_value.DataAsByteSpan().end(), [](const uint8_t byte) { return byte != 0; })) {
        return false;
      }
    }
  } else {
    if (pad_attributes.find("value") != pad_attributes.end() &&
        pad_attributes.at("value").f() != 0.0) {
      return false;
    }
  }
 
  const Node& child_node = *node.OutputNodesBegin();
  if (graph_utils::IsSupportedOptypeVersionAndDomain(child_node, "Cast", {1, 6, 9, 13, 19, 21, 23, 24, 25})) {
    if (child_node.GetOutputEdgesCount() != 1) {
      return false;
    }
 
    if (graph.NodeProducesGraphOutput(child_node)) {
      return false;
    }
    return VerifyNotCastChild(*child_node.OutputNodesBegin());
  } else {
    return VerifyNotCastChild(child_node);
  }
}
 
/*
 * - For 1st two dimension Pads array's value should be zero and for rest of them values should >= 0
 */
Status PadFusion::Apply(Graph& graph, Node& pad_node, RewriteRuleEffect& rule_effect, const logging::Logger&) const {
  std::vector<int64_t> pads_values;
 
  if (pad_node.SinceVersion() >= 11) {
    const auto* pads_proto = graph_utils::GetConstantInitializer(graph, pad_node.InputDefs()[1]->Name());
    Initializer pads{graph, *pads_proto, graph.ModelPath()};
    pads_values.assign(pads.DataAsSpan<int64_t>().begin(), pads.DataAsSpan<int64_t>().end());
  } else {
    pads_values.assign(pad_node.GetAttributes().at("pads").ints().begin(), pad_node.GetAttributes().at("pads").ints().end());
  }
 
  const size_t pads_size = pads_values.size();
  // Per ONNX Pad spec, pads has 2*rank elements. This fusion only applies when the leading two
  // dimensions (N, C) have zero padding, so we need at least rank 2 (pads_size >= 4) and an even
  // number of entries.
  if (pads_size < 4 || (pads_size % 2) != 0) {
    return Status::OK();
  }
 
  // check if padding is applied only on feature dims
  if (pads_values[0] != 0 || pads_values[1] != 0 || pads_values[pads_size / 2] != 0 ||
      pads_values[pads_size / 2 + 1] != 0) {
    return Status::OK();
  }
 
  // check if padding is only positive
  if (std::any_of(pads_values.begin(), pads_values.end(), [](int64_t value) { return value < 0; })) {
    return Status::OK();
  }
 
  Node& child_node = *graph.GetNode(pad_node.OutputNodesBegin()->Index());
  // We don't need to cast the pad_constant_value because this fusion requires that constant_pad_value
  // to be zero. See PadFusion::SatisfyCondition for details.
  Node& target_padding_node = (child_node.OpType() == "Cast")
                                  ? *graph.GetNode(child_node.OutputNodesBegin()->Index())
                                  : child_node;
 
  // If the target node already has an explicit pads attribute, its length must match the expected
  // pads length for the target op (2 * spatial_rank), where spatial_rank = pads_size / 2 - 2.
  // Otherwise the fused padding values would be written into mismatched positions.
  const auto& target_attrs = target_padding_node.GetAttributes();
  auto target_pads_iter = target_attrs.find("pads");
  if (target_pads_iter != target_attrs.end() && !target_pads_iter->second.ints().empty()) {
    if (static_cast<size_t>(target_pads_iter->second.ints().size()) != pads_size - 4) {
      return Status::OK();
    }
  }
 
  UpdatePaddingAttribute(target_padding_node, pads_values, pads_size);
 
  graph_utils::RemoveNodeOutputEdges(graph, pad_node);
  graph_utils::ReplaceNodeInput(child_node, 0, *pad_node.MutableInputDefs()[0]);
  // Un-pad the output shape of Cast node
  if (child_node.OpType() == "Cast") {
    auto* cast_output_node_arg = child_node.MutableOutputDefs()[0];
    cast_output_node_arg->SetShape(*pad_node.MutableInputDefs()[0]->Shape());
  }
  graph.RemoveNode(pad_node.Index());
  rule_effect = RewriteRuleEffect::kRemovedCurrentNode;
  return Status::OK();
}
}  // namespace onnxruntime