Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/graph/graph_utils.h"
#include "core/optimizer/initializer.h"
#include "core/optimizer/conv_mul_fusion.h"
#include "core/optimizer/utils.h"
 
using namespace ONNX_NAMESPACE;
using namespace onnxruntime::common;
namespace onnxruntime {
 
Status ConvMulFusion::Apply(Graph& graph, Node& node, RewriteRuleEffect& rule_effect, const logging::Logger&) const {
  auto& conv_node = node;
  auto& mul_node = *graph.GetNode(conv_node.OutputNodesBegin()->Index());
 
  const auto& conv_inputs = conv_node.InputDefs();
  const auto& mul_inputs = mul_node.InputDefs();
 
  const auto* conv_W_tensor_proto = graph_utils::GetConstantInitializer(graph, conv_inputs[1]->Name());
  ORT_ENFORCE(conv_W_tensor_proto);
 
  const auto* mul_B_tensor_proto = graph_utils::GetConstantInitializer(graph, mul_inputs[1]->Name());
  ORT_ENFORCE(mul_B_tensor_proto);
 
  // Conv only supports floating point data types, so can only fuse with an initializer containing those types
  if (!optimizer_utils::IsFloatingPointDataType(*conv_W_tensor_proto) ||
      conv_W_tensor_proto->data_type() != mul_B_tensor_proto->data_type() ||
      conv_W_tensor_proto->dims_size() <= 2) {
    return Status::OK();
  }
 
  if (mul_B_tensor_proto->dims_size() != 0) {
    int axis;
    if (mul_B_tensor_proto->dims_size() == conv_W_tensor_proto->dims_size()) {
      // Test for broadcast multiply such as 1xCx1x1 for a 2D convolution.
      axis = 1;
    } else if (mul_B_tensor_proto->dims_size() == conv_W_tensor_proto->dims_size() - 1) {
      // Test for broadcast multiply such as Cx1x1 for a 2D convolution.
      axis = 0;
    } else {
      return Status::OK();
    }
    if (mul_B_tensor_proto->dims(axis) != conv_W_tensor_proto->dims(0)) {
      return Status::OK();
    }
    // The dimensions of mul_B should be equal to 1 except axis dimension.
    for (int i = 0; i < mul_B_tensor_proto->dims_size(); i++) {
      if (i != axis && mul_B_tensor_proto->dims(i) != 1) {
        return Status::OK();
      }
    }
  }
 
  Initializer conv_W{graph, *conv_W_tensor_proto, graph.ModelPath()};
  Initializer mul_B{graph, *mul_B_tensor_proto, graph.ModelPath()};
 
  const ONNX_NAMESPACE::TensorProto* conv_B_tensor_proto = nullptr;
  std::optional<Initializer> conv_B;
  const bool is_3d = conv_inputs.size() == 3;
  if (is_3d) {
    conv_B_tensor_proto = graph_utils::GetConstantInitializer(graph, conv_inputs[2]->Name());
    ORT_ENFORCE(conv_B_tensor_proto);
 
    if (conv_B_tensor_proto->data_type() != mul_B_tensor_proto->data_type() ||
        conv_B_tensor_proto->dims_size() != 1 ||
        conv_B_tensor_proto->dims(0) != conv_W_tensor_proto->dims(0)) {
      return Status::OK();
    }
 
    conv_B.emplace(graph, *conv_B_tensor_proto, graph.ModelPath());
  }
 
  // Calculate new value of initializers of conv node
  conv_W.scale_by_axis(mul_B, 1);
 
  if (is_3d) {
    if (mul_B_tensor_proto->dims_size() != 0) {
      conv_B->mul(mul_B);
    } else {
      conv_B->scale_by_axis(mul_B, 0);
    }
  }
 
  // Create new initializers of conv
  ONNX_NAMESPACE::TensorProto new_conv_W_tensor_proto;
  conv_W.ToProto(new_conv_W_tensor_proto);
 
  auto new_W_name = graph.GenerateNodeArgName("ConvMulFusion_W_" + conv_W_tensor_proto->name());
  new_conv_W_tensor_proto.set_name(new_W_name);
 
  // Replace initializers of conv node
  NodeArg& new_conv_W_node_arg = graph_utils::AddInitializerWithOrtValue(graph, new_conv_W_tensor_proto);
  graph_utils::ReplaceNodeInput(conv_node, 1, new_conv_W_node_arg);
 
  if (is_3d) {
    ONNX_NAMESPACE::TensorProto new_conv_B_tensor_proto;
    conv_B->ToProto(new_conv_B_tensor_proto);
 
    auto new_B_name = graph.GenerateNodeArgName("ConvMulFusion_Mul_B_" + mul_B_tensor_proto->name());
    new_conv_B_tensor_proto.set_name(new_B_name);
 
    NodeArg& new_conv_B_node_arg = graph_utils::AddInitializerWithOrtValue(graph, new_conv_B_tensor_proto);
    graph_utils::ReplaceNodeInput(conv_node, 2, new_conv_B_node_arg);
  }
 
  // Move output name and edges from Mul node to Conv node and remove Mul node.
  graph_utils::FinalizeNodeFusion(graph, conv_node, mul_node);
 
  rule_effect = RewriteRuleEffect::kModifiedRestOfGraph;
 
  return Status::OK();
}
 
bool ConvMulFusion::SatisfyCondition(const Graph& graph, const Node& node, const logging::Logger&) const {
  if (!graph_utils::IsSupportedOptypeVersionAndDomain(node, "Conv", {1, 11, 22}) ||
      node.GetOutputEdgesCount() != 1) {
    return false;
  }
 
  const auto& next_node = *node.OutputNodesBegin();
  if (!graph_utils::IsSupportedOptypeVersionAndDomain(next_node, "Mul", {7, 13, 14}) ||
      next_node.GetInputEdgesCount() != 1 ||
      // Make sure the two nodes do not span execution providers.
      next_node.GetExecutionProviderType() != node.GetExecutionProviderType()) {
    return false;
  }
 
  // Check that the appropriate inputs to the Conv and Mul nodels are constants.
  if (!graph_utils::NodeArgIsConstant(graph, *node.InputDefs()[1]) ||
      (node.InputDefs().size() == 3 && !graph_utils::NodeArgIsConstant(graph, *node.InputDefs()[2])) ||
      !graph_utils::NodeArgIsConstant(graph, *next_node.InputDefs()[1])) {
    return false;
  }
 
  if (graph.NodeProducesGraphOutput(node)) {
    return false;
  }
 
  return true;
}
 
}  // namespace onnxruntime