Исходный код СУБД 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include <limits>
 
#include "core/optimizer/stft_decomposition.h"
#include "core/optimizer/initializer.h"
#include "core/optimizer/utils.h"
#include "core/graph/graph_utils.h"
#include "core/optimizer/optimizer_execution_frame.h"
#include "core/optimizer/utils.h"
#include "core/common/safeint.h"
#include "core/framework/op_kernel.h"
#include "core/framework/tensorprotoutils.h"
#include <numbers>
 
using namespace onnxruntime::common;
 
namespace onnxruntime {
 
STFTDecomposition::STFTDecomposition(const InlinedHashSet<std::string_view>& compatible_execution_providers) noexcept
    : GraphTransformer("STFTDecomposition", compatible_execution_providers) {
}
 
template <typename T>
constexpr static ONNX_NAMESPACE::TensorProto_DataType GetDataType() {
  if constexpr (std::is_same<T, float>::value) {
    return ONNX_NAMESPACE::TensorProto_DataType_FLOAT;
  } else if constexpr (std::is_same<T, MLFloat16>::value) {
    return ONNX_NAMESPACE::TensorProto_DataType_FLOAT16;
  } else if constexpr (std::is_same<T, double>::value) {
    return ONNX_NAMESPACE::TensorProto_DataType_DOUBLE;
  } else if constexpr (std::is_same<T, int64_t>::value) {
    return ONNX_NAMESPACE::TensorProto_DataType_INT64;
  } else {
    ORT_THROW_EX(std::logic_error, "Invalid data type requested for STFT decomposition");
  }
}
 
template <typename TDataType, size_t TDims>
NodeArg* AddInitializer(Graph& graph, const char* name, const int64_t (&shape)[TDims], const TDataType* begin) {
  ONNX_NAMESPACE::TensorProto proto;
  proto.set_name(graph.GenerateNodeArgName(name));
  proto.set_data_type(GetDataType<TDataType>());
  int64_t element_count = 1;
  for (size_t i = 0; i < TDims; i++) {
    element_count *= shape[i];
    proto.add_dims(shape[i]);
  }
  utils::SetRawDataInTensorProto(proto, begin, element_count * sizeof(TDataType));
  return &graph_utils::AddInitializerWithOrtValue(graph, proto);
}
 
template <size_t TDims>
NodeArg* AddShapeInitializer(Graph& graph, const char* name, const int64_t (&shape)[TDims]) {
  int64_t shape_shape[] = {TDims};
  return AddInitializer<int64_t>(graph, name, shape_shape, shape);
}
 
std::pair<Node*, NodeArg*> AddNode(Graph& graph,
                                   const char* op_type,
                                   ProviderType execution_provider_type,
                                   gsl::span<NodeArg*> inputs,
                                   const Node* annotation_source = nullptr) {
  auto def_name = graph.GenerateNodeArgName(op_type);
  auto node_arg = &graph.GetOrCreateNodeArg(def_name, nullptr);
  Node& node = annotation_source
                   ? graph.AddNode(graph.GenerateNodeName(op_type),
                                   op_type,
                                   "",
                                   inputs,
                                   {node_arg},
                                   *annotation_source)
                   : graph.AddNode(graph.GenerateNodeName(op_type),
                                   op_type,
                                   "",
                                   inputs,
                                   {node_arg});
  node.SetExecutionProviderType(execution_provider_type);
  return std::make_pair(&node, node_arg);
}
 
std::pair<Node*, NodeArg*> AddNodeCast(Graph& graph, NodeArg* in,
                                       ONNX_NAMESPACE::TensorProto_DataType data_type,
                                       const Node* annotation_source = nullptr) {
  auto def_name = graph.GenerateNodeArgName("Cast");
  auto node_arg = &graph.GetOrCreateNodeArg(def_name, nullptr);
  Node& node = annotation_source
                   ? graph.AddNode(graph.GenerateNodeName("Cast"),
                                   "Cast",
                                   "",
                                   {in},
                                   {node_arg},
                                   *annotation_source)
                   : graph.AddNode(graph.GenerateNodeName("Cast"),
                                   "Cast",
                                   "",
                                   {in},
                                   {node_arg});
  node.AddAttribute("to", static_cast<int64_t>(data_type));
  node.SetExecutionProviderType(kCpuExecutionProvider);
  return std::make_pair(&node, node_arg);
}
 
#define CONTINUE_IF_NO_DIM_VALUE(dim) \
  if (!dim.has_dim_value()) {         \
    continue;                         \
  }
#define CONTINUE_IF_NULL(x) \
  if (x == nullptr) {       \
    continue;               \
  }
 
/*
    This function decomposes a STFT node into a subgraph.
    The decomposition requires that:
      1) The signal input is real valued and not complex valued!
      2) Both (frame_step) *and* either (window or frame_length) inputs must be constant.
    Otherwise the transform will not be applied.
 
    Subgraph pattern 1: STFT with optional Window parameter set
              [root]--(signal)--------------------+
              [root]--(frame_step)---------------+|
              [root]--(window)------------------+||
              [root]--(frame_length) ----------+|||
                                               ||||
                                               vvvv
                                              [STFT]--(output)-->
    After Fusion:
              [root]--(signal)-------------------------+
              [root]                                   |
              [root]--(window)--+                      |
              [root]            |                      |
                                v                      v
         (only for non-fp32) [Cast]             +--[Reshape]
                                |               |      |
                                v               |      v
                            [Reshape]-->[Mul]---|-->[Conv]-------+
                                |               |                |
                                |               +-----|          |
                                |                     v          v
                                +------>[Mul]------>[Conv]-->[Concat]-->[Reshape]-->[Transpose]--(output)-->
 
 
    Subgraph pattern 2: STFT without optional Window parameter set
              [root]--(signal)-------------------+
              [root]--(frame_step)--------------+|
              [root]                             |
              [root]--(frame_length) ----------+||
                                               |||
                                               vvv
                                              [STFT]--(output)-->
    After Fusion:
              [root]--(signal)-->[Reshape]-->[Conv]
              [root]                 |         |
              [root]                 |         v
              [root]                 +------>[Conv]-->[Concat]-->[Reshape]-->[Transpose]--(output)-->
*/
Status STFTDecomposition::ApplyImpl(Graph& graph, bool& modified, int graph_level, const logging::Logger& logger) const {
  GraphViewer graph_viewer(graph);
  auto& order = graph_viewer.GetNodesInTopologicalOrder();
 
  for (NodeIndex i : order) {
    auto node = graph.GetNode(i);
    CONTINUE_IF_NULL(node);
    ORT_RETURN_IF_ERROR(Recurse(*node, modified, graph_level, logger));
 
    if (node->OpType() != "STFT") {
      continue;
    }
 
    Node& stft = *node;
    auto signal = stft.MutableInputDefs()[0];
    auto frame_step = stft.MutableInputDefs()[1];
    auto window = stft.MutableInputDefs()[2];
    auto frame_length = stft.MutableInputDefs()[3];
 
    // If the signal has free dimensions, do not transform...
    auto batch_size_dim = signal->Shape()->dim(0);
    auto signal_length_dim = signal->Shape()->dim(1);
    auto signal_components_dim = signal->Shape()->dim(2);
    CONTINUE_IF_NO_DIM_VALUE(signal_length_dim);
    CONTINUE_IF_NO_DIM_VALUE(signal_components_dim);
 
    auto batch_size = batch_size_dim.has_dim_value() ? batch_size_dim.dim_value() : static_cast<int64_t>(-1);
    auto signal_length = signal_length_dim.dim_value();
    auto is_real = signal_components_dim.dim_value() == 1;
    auto data_type = static_cast<ONNX_NAMESPACE::TensorProto_DataType>(signal->TypeAsProto()->tensor_type().elem_type());
 
    auto frame_step_initializer = graph_utils::GetConstantInitializer(graph, frame_step->Name());
    auto window_initializer = graph_utils::GetConstantInitializer(graph, window->Name());
    auto frame_length_initializer = graph_utils::GetConstantInitializer(graph, frame_length->Name());
    CONTINUE_IF_NULL(frame_step_initializer);
    if (!frame_length_initializer && !window_initializer) {
      continue;
    }
 
    auto read_int64_initializer = [](Graph& graph, const ONNX_NAMESPACE::TensorProto* initializer) {
      return *Initializer(*initializer, graph.ModelPath()).data<int64_t>();
    };
    auto frame_step_value = read_int64_initializer(graph, frame_step_initializer);
 
    // Get DFT Size
    int64_t dft_size = 0;
    if (frame_length_initializer) {
      dft_size = read_int64_initializer(graph, frame_length_initializer);
    }
    if (dft_size == 0 && window_initializer) {
      auto window_length_dim = window->Shape()->dim(0);
      CONTINUE_IF_NO_DIM_VALUE(window_length_dim);
      dft_size = window_length_dim.dim_value();
    }
 
    // Validate model-provided scalar values before using them in size calculations.
    // These come from untrusted model initializers/shapes and must be positive.
    if (dft_size <= 0 || frame_step_value <= 0) {
      LOGS(logger, WARNING) << "STFT decomposition skipped: invalid dft_size (" << dft_size
                            << ") or frame_step_value (" << frame_step_value << ")";
      continue;
    }
 
    bool is_onesided = true;
    auto& attrs = stft.GetAttributes();
    if (attrs.find("onesided") != attrs.end()) {
      auto& onesided_attr = attrs.at("onesided");
      if (utils::HasInt(onesided_attr)) {
        is_onesided = static_cast<bool>(onesided_attr.i());
      }
    }
 
    auto dft_unique_bins = is_onesided ? ((dft_size >> 1) + 1) : dft_size;
 
    Node* signal_recipient = nullptr;
    Node* window_recipient = nullptr;
    Node* stft_producer = nullptr;
    if (is_real) {
      auto output_num_frames = stft.MutableOutputDefs()[0]->Shape()->dim(1).dim_value();
      auto output_frame_length = stft.MutableOutputDefs()[0]->Shape()->dim(2).dim_value();
 
      size_t dft_size_sz, dft_unique_bins_sz, weight_size;
      if (!SafeCast(dft_unique_bins, dft_unique_bins_sz) ||
          !SafeCast(dft_size, dft_size_sz) ||
          !SafeMultiply(dft_unique_bins_sz, dft_size_sz, weight_size)) {
        LOGS(logger, WARNING) << "STFT decomposition skipped: weight size overflow";
        continue;
      }
 
      auto real_weights_data = std::vector<float>(weight_size);
      auto imag_weights_data = std::vector<float>(weight_size);
 
      // Populate weights
      for (size_t k = 0; k < dft_unique_bins_sz; k++) {
        for (size_t n = 0; n < dft_size_sz; n++) {
          auto index = k * dft_size_sz + n;
          auto theta = -2 * std::numbers::pi_v<float> * k * n / static_cast<float>(dft_size);
          real_weights_data[index] = static_cast<float>(cos(theta));
          imag_weights_data[index] = static_cast<float>(sin(theta));
        }
      }
 
      const int64_t weight_shape[] = {dft_unique_bins, 1, 1, dft_size};
      auto* real_weights = AddInitializer<float>(graph, "stft_real_conv_weights", weight_shape, real_weights_data.data());
      auto* imaginary_weights = AddInitializer<float>(graph, "stft_imaginary_conv_weights", weight_shape, imag_weights_data.data());
 
      const int64_t signal_reshaped[] = {batch_size, 1, 1, signal_length};
      auto signal_shape = AddShapeInitializer(graph, "stft_signal_shape", signal_reshaped);
 
      const int64_t unsqueezed_output_shape[] = {2, batch_size, output_frame_length, output_num_frames};
      auto unsqueezed_shape = AddShapeInitializer(graph, "stft_output_reshaped", unsqueezed_output_shape);
 
      NodeArg* signal_reshaped_inputs[] = {signal, signal_shape};
      Node* reshape_signal_node = nullptr;
      NodeArg* reshape_output = nullptr;
      std::tie(reshape_signal_node, reshape_output) =
          AddNode(graph, "Reshape", stft.GetExecutionProviderType(), signal_reshaped_inputs, &stft);
 
      NodeArg* real_weights_final = real_weights;
      NodeArg* imag_weights_final = imaginary_weights;
      if (!window->Exists()) {
        // When we are missing a window function
        if (real_weights_final->TypeAsProto()->tensor_type().elem_type() != data_type) {
          std::tie(std::ignore, real_weights_final) =
              AddNodeCast(graph, real_weights_final, data_type, &stft);
        }
        if (imag_weights_final->TypeAsProto()->tensor_type().elem_type() != data_type) {
          std::tie(std::ignore, imag_weights_final) =
              AddNodeCast(graph, imag_weights_final, data_type, &stft);
        }
      } else {
        // When we have a window function
        const int64_t window_reshaped_shape[] = {1, 1, 1, dft_size};
        auto window_shape = AddShapeInitializer(graph, "stft_window_shape", window_reshaped_shape);
 
        auto window_final = window;
        if (window->TypeAsProto()->tensor_type().elem_type() != GetDataType<float>()) {
          Node* window_cast_node = nullptr;
          std::tie(window_cast_node, window_final) =
              AddNodeCast(graph, window, GetDataType<float>(), &stft);
          window_recipient = window_cast_node;
        }
 
        NodeArg* window_reshaped_inputs[] = {window_final, window_shape};
        Node* window_reshape_node;
        NodeArg* window_reshaped = nullptr;
        std::tie(window_reshape_node, window_reshaped) =
            AddNode(graph, "Reshape", kCpuExecutionProvider, window_reshaped_inputs, &stft);
        if (!window_recipient) {
          window_recipient = window_reshape_node;
        }
 
        NodeArg* scale_real_weights_inputs[] = {real_weights, window_reshaped};
        NodeArg* windowed_real_weights_output = nullptr;
        std::tie(std::ignore, windowed_real_weights_output) =
            AddNode(graph, "Mul", kCpuExecutionProvider, scale_real_weights_inputs, &stft);
 
        NodeArg* scale_imag_weights_inputs[] = {imaginary_weights, window_reshaped};
        NodeArg* windowed_imag_weights_output = nullptr;
        std::tie(std::ignore, windowed_imag_weights_output) =
            AddNode(graph, "Mul", kCpuExecutionProvider, scale_imag_weights_inputs, &stft);
 
        std::tie(std::ignore, real_weights_final) =
            AddNodeCast(graph, windowed_real_weights_output, data_type, &stft);
        std::tie(std::ignore, imag_weights_final) =
            AddNodeCast(graph, windowed_imag_weights_output, data_type, &stft);
      }
 
      // Add Convolution (reals)
      NodeArg* conv_real_inputs[] = {reshape_output, real_weights_final};
      Node* real_conv_node = nullptr;
      NodeArg* real_conv_output = nullptr;
      std::tie(real_conv_node, real_conv_output) =
          AddNode(graph, "Conv", stft.GetExecutionProviderType(), conv_real_inputs, &stft);
      real_conv_node->AddAttribute("strides", std::vector<int64_t>{1, frame_step_value});
 
      // Add Convolution (imaginary)
      NodeArg* conv_imag_inputs[] = {reshape_output, imag_weights_final};
      Node* imag_conv_node = nullptr;
      NodeArg* imag_conv_output = nullptr;
      std::tie(imag_conv_node, imag_conv_output) =
          AddNode(graph, "Conv", stft.GetExecutionProviderType(), conv_imag_inputs, &stft);
      imag_conv_node->AddAttribute("strides", std::vector<int64_t>{1, frame_step_value});
 
      // Concatenate
      NodeArg* concatenate_inputs[] = {real_conv_output, imag_conv_output};
      Node* concat_node = nullptr;
      NodeArg* concatenated_conv_output = nullptr;
      std::tie(concat_node, concatenated_conv_output) =
          AddNode(graph, "Concat", stft.GetExecutionProviderType(), concatenate_inputs, &stft);
      concat_node->AddAttribute("axis", static_cast<int64_t>(0));
 
      // Unsqueeze Reshape
      NodeArg* unsqueeze_reshape_inputs[] = {concatenated_conv_output, unsqueezed_shape};
      NodeArg* unsqueezed_output = nullptr;
      std::tie(std::ignore, unsqueezed_output) =
          AddNode(graph, "Reshape", stft.GetExecutionProviderType(), unsqueeze_reshape_inputs, &stft);
 
      // Transpose
      NodeArg* transpose_inputs[] = {unsqueezed_output};
      Node* transpose_node = nullptr;
      NodeArg* transpose_output = nullptr;
      std::tie(transpose_node, transpose_output) =
          AddNode(graph, "Transpose", stft.GetExecutionProviderType(), transpose_inputs, &stft);
      transpose_node->AddAttribute("perm", std::vector<int64_t>{1, 3, 2, 0});
 
      signal_recipient = reshape_signal_node;
      stft_producer = transpose_node;
    } else {
      continue;
    }
 
    auto input_edges = graph_utils::GraphEdge::GetNodeInputEdges(stft);
    auto output_edges = graph_utils::GraphEdge::GetNodeOutputEdges(stft);
 
    // Copy inputs
    auto signal_target_idx = signal_recipient->Index();
    for (auto cur = input_edges.cbegin(), end = input_edges.cend(); cur != end; ++cur) {
      const graph_utils::GraphEdge& edge = *cur;
      NodeIndex target_idx = 0;
      Node* recipient = nullptr;
      switch (cur->dst_arg_index) {
        case 0:
          target_idx = signal_target_idx;
          recipient = signal_recipient;
          break;
        case 2:
          if (window_recipient) {
            target_idx = window_recipient->Index();
            recipient = window_recipient;
          }
          break;
      }
 
      if (!recipient) {
        continue;
      }
 
      auto arg_index = graph_utils::GetNodeInputIndexFromInputName(*recipient, edge.arg_name);
      graph.AddEdge(edge.src_node, target_idx, edge.src_arg_index, arg_index);
    }
 
    // Copy STFT outputs to stft_producer
    stft_producer->MutableOutputDefs() = stft.MutableOutputDefs();
    auto stft_producer_target_idx = stft_producer->Index();
    for (auto cur = output_edges.cbegin(), end = output_edges.cend(); cur != end; ++cur) {
      graph.AddEdge(stft_producer_target_idx, cur->dst_node, cur->src_arg_index, cur->dst_arg_index);
    }
 
    graph_utils::GraphEdge::RemoveGraphEdges(graph, input_edges);
    graph_utils::GraphEdge::RemoveGraphEdges(graph, output_edges);
    graph.RemoveNode(stft.Index());
 
    modified = true;
  }
  return Status::OK();
}
}  // namespace onnxruntime