Исходный код СУБД 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
#include "qlinear_where.h"
#include "qlinear_lookup_table.h"
 
#include "core/providers/common.h"
#include "core/mlas/inc/mlas.h"
#include "core/platform/threadpool.h"
 
#include "core/providers/cpu/math/element_wise_ops.h"
 
namespace onnxruntime {
namespace {
 
template <typename T, typename R>
using EnableIfEigenScalar = typename std::enable_if<std::is_arithmetic<T>::value, R>::type;
 
template <typename T, typename R>
using EnableIfEigenNotScalar = typename std::enable_if<!std::is_arithmetic<T>::value, R>::type;
 
template <typename T>
ProcessBroadcastSpanFuncs CreateScalarBroadcastFuncs() {
  return ProcessBroadcastSpanFuncs{
      // Scalar Condition + Eigen Input -> Eigen output
      [](BroadcastHelper& per_iter_bh) {
        auto* user_data = static_cast<T*>(per_iter_bh.GetUserData());
        bool target = user_data[0] == 1;
        bool is_copy = user_data[1] == 1;
        bool condition = per_iter_bh.ScalarInput0<bool>();
        auto value = per_iter_bh.EigenInput1<T>();
        auto output = per_iter_bh.OutputEigen<T>();
        if (condition == target) {
          output = value;
        } else {
          output = EigenVectorMap<T>::PlainObject::Constant(value.size(), T{});
        }
        // Transform the output to the correct value from LookupTable
        if (!is_copy) {
          auto* look_up_table = user_data + 2;
          std::transform(value.cbegin(), value.cend(), output.begin(),
                         [condition, target, &look_up_table](const T& value_element) {
                           return condition == target ? look_up_table[value_element] : T{};
                         });
        }
      },
      // Eigen Condition + Scalar Input -> Eigen Output
      [](BroadcastHelper& per_iter_bh) {
        auto* user_data = static_cast<T*>(per_iter_bh.GetUserData());
        bool target = user_data[0] == 1;
        bool is_copy = user_data[1] == 1;
        auto condition = per_iter_bh.EigenInput0<bool>();
        const T& value = per_iter_bh.ScalarInput1<T>();
        auto output = per_iter_bh.OutputEigen<T>();
        output = (condition.array() == target)
                     .select(value, EigenVectorMap<T>::PlainObject::Constant(condition.size(), T{}));
        // Transform the output to the correct value from LookupTable
        if (!is_copy) {
          auto* look_up_table = user_data + 2;
          std::transform(condition.cbegin(), condition.cend(), output.begin(),
                         [target, &look_up_table, &value](bool condition_element) {
                           return condition_element == target ? look_up_table[value] : T{};
                         });
        }
      },
      // Eigen Condition + Eigen Input -> Eigen Output
      [](BroadcastHelper& per_iter_bh) {
        auto* user_data = static_cast<T*>(per_iter_bh.GetUserData());
        bool target = user_data[0] == 1;
        bool is_copy = user_data[1] == 1;
        auto condition = per_iter_bh.EigenInput0<bool>();
        auto value = per_iter_bh.EigenInput1<T>();
        auto output = per_iter_bh.OutputEigen<T>();
        output = (condition.array() == target)
                     .select(value, EigenVectorMap<T>::PlainObject::Constant(condition.size(), T{}));
        // Transform the output to the correct value from LookupTable
        if (!is_copy) {
          auto* look_up_table = user_data + 2;
          std::transform(condition.cbegin(), condition.cend(), value.cbegin(), output.begin(),
                         [target, &look_up_table](bool condition_element, const T& value_element) {
                           return (condition_element == target) ? look_up_table[value_element] : T{};
                         });
        }
      }};
}
 
template <typename T>
ProcessBroadcastSpanFuncs CreateNonScalarBroadcastFuncs() {
  return ProcessBroadcastSpanFuncs{
      [](BroadcastHelper& per_iter_bh) {
        auto* user_data = static_cast<T*>(per_iter_bh.GetUserData());
        bool target = user_data[0] == 1;
        bool is_copy = user_data[1] == 1;
        auto* look_up_table = user_data + 2;
        bool condition = per_iter_bh.ScalarInput0<bool>();
        auto value = per_iter_bh.SpanInput1<T>();
        auto output = per_iter_bh.OutputSpan<T>();
        if (condition == target) {
          // Transform the output to the correct value from LookupTable
          std::transform(value.cbegin(), value.cend(), output.begin(),
                         [condition, target, &look_up_table, is_copy](const T& value_element) {
                           return is_copy ? value_element : look_up_table[value_element];
                         });
        } else {
          std::fill(output.begin(), output.end(), T{});
        }
      },
      [](BroadcastHelper& per_iter_bh) {
        auto* user_data = static_cast<T*>(per_iter_bh.GetUserData());
        bool target = user_data[0] == 1;
        bool is_copy = user_data[1] == 1;
        auto* look_up_table = user_data + 2;
        auto condition = per_iter_bh.SpanInput0<bool>();
        const T& value = per_iter_bh.ScalarInput1<T>();
        auto output = per_iter_bh.OutputSpan<T>();
        // Transform the output to the correct value from LookupTable
        std::transform(condition.begin(), condition.end(), output.begin(),
                       [target, &value, &look_up_table, is_copy](bool condition_element) {
                         return condition_element == target ? is_copy ? value : look_up_table[value] : T{};
                       });
      },
      [](BroadcastHelper& per_iter_bh) {
        auto* user_data = static_cast<T*>(per_iter_bh.GetUserData());
        bool target = user_data[0] == 1;
        bool is_copy = user_data[1] == 1;
        auto* look_up_table = user_data + 2;
        auto condition = per_iter_bh.SpanInput0<bool>();
        auto value = per_iter_bh.SpanInput1<T>();
        auto output = per_iter_bh.OutputSpan<T>();
        // Transform the output to the correct value from LookupTable
        std::transform(condition.begin(), condition.end(), value.cbegin(), output.begin(),
                       [target, &look_up_table, is_copy](bool condition_element, const T& value_element) {
                         return condition_element == target ? is_copy ? value_element : look_up_table[value_element] : T{};
                       });
      }};
}
 
template <typename T>
EnableIfEigenScalar<T, ProcessBroadcastSpanFuncs> SelectBroadcastFuncs() {
  // NOTE: Workaround a VS2017 bug by calling a separate function to create the broadcast funcs.
  // If we create them directly here it doesn't bring in the definitions of the Eigen classes leading to
  // a 'class has no constructors' error
  return CreateScalarBroadcastFuncs<T>();
}
 
template <typename T>
EnableIfEigenNotScalar<T, ProcessBroadcastSpanFuncs> SelectBroadcastFuncs() {
  return CreateNonScalarBroadcastFuncs<T>();
}
// function pointer to create typed tensor from type agnostic code whilst avoiding the overhead of std::function
using AllocTensorFunc = std::unique_ptr<Tensor> (*)(const TensorAllocator& allocator, const TensorShape& shape);
 
static std::unique_ptr<Tensor> UntypedSelect(OpKernelContext& context, std::vector<uint8_t>& user_data, const ProcessBroadcastSpanFuncs& functors, const TensorAllocator& allocator, AllocTensorFunc allocate_tensor) {
  const auto& condition = *context.Input<Tensor>(0);
  // select the X input (input 1) for 'true', and Y input (input 2) for 'false'
  bool target = user_data[0] == 1;
  const auto& values = *context.Input<Tensor>(target ? 1 : 4);
 
  InputBroadcaster input_broadcaster(condition, values);
  std::unique_ptr<Tensor> selection_tensor = allocate_tensor(allocator, input_broadcaster.GetOutputShape());
  OutputBroadcaster output_broadcaster(input_broadcaster.GetSpanSize(), *selection_tensor);
  // store value of 'target' directly in void* for user_data so it's accessible in the state-less functors
  BroadcastHelper broadcast_helper(input_broadcaster, output_broadcaster, reinterpret_cast<void*>(user_data.data()));
  BroadcastLooper(broadcast_helper, functors);
  return selection_tensor;
}
 
// Merging Functions
template <typename T>
void MergeScalarAndVector(EigenVectorMap<T> output, const T& scalar_value, ConstEigenVectorMap<T> vector_value) {
  if (scalar_value != T{}) {
    output = EigenVectorMap<T>::PlainObject::Constant(vector_value.size(), scalar_value);
  } else {
    output = vector_value;
  }
};
 
template <typename T>
EnableIfEigenScalar<T, ProcessBroadcastSpanFuncs> MergeBroadcastFuncs() {
  return ProcessBroadcastSpanFuncs{
      [](BroadcastHelper& per_iter_bh) {
        MergeScalarAndVector(per_iter_bh.OutputEigen<T>(),
                             per_iter_bh.ScalarInput0<T>(),  // X selection
                             per_iter_bh.EigenInput1<T>());  // Y selection
      },
      [](BroadcastHelper& per_iter_bh) {
        MergeScalarAndVector(per_iter_bh.OutputEigen<T>(),
                             per_iter_bh.ScalarInput1<T>(),  // Y selection
                             per_iter_bh.EigenInput0<T>());  // X selection
      },
      [](BroadcastHelper& per_iter_bh) {
        auto X_selection = per_iter_bh.EigenInput0<T>();
        auto Y_selection = per_iter_bh.EigenInput1<T>();
        per_iter_bh.OutputEigen<T>() = X_selection.binaryExpr(Y_selection,
                                                              [](T x, T y) -> T {
                                                                return x != T{} ? x : y;
                                                              });
      }};
}
 
template <typename T>
void MergeScalarAndVector(gsl::span<T> output, const T& scalar_value, gsl::span<const T> vector_value) {
  if (!scalar_value.empty()) {
    std::fill(output.begin(), output.end(), scalar_value);
  } else {
    std::copy(vector_value.cbegin(), vector_value.cend(), output.begin());
  }
};
 
template <typename T>
 
EnableIfEigenNotScalar<T, ProcessBroadcastSpanFuncs> MergeBroadcastFuncs() {
  return ProcessBroadcastSpanFuncs{
      [](BroadcastHelper& per_iter_bh) {
        MergeScalarAndVector(per_iter_bh.OutputSpan<T>(),
                             per_iter_bh.ScalarInput0<T>(),  // X selection
                             per_iter_bh.SpanInput1<T>());   // Y selection
      },
      [](BroadcastHelper& per_iter_bh) {
        MergeScalarAndVector(per_iter_bh.OutputSpan<T>(),
                             per_iter_bh.ScalarInput1<T>(),  // Y selection
                             per_iter_bh.SpanInput0<T>());   // X selection
      },
      [](BroadcastHelper& per_iter_bh) {
        auto X_selection = per_iter_bh.SpanInput0<T>();
        auto Y_selection = per_iter_bh.SpanInput1<T>();
        auto output = per_iter_bh.OutputSpan<T>();
        std::transform(X_selection.cbegin(), X_selection.cend(), Y_selection.cbegin(), output.begin(),
                       [](const T& x, const T& y) { return !x.empty() ? x : y; });
      }};
}
static void UntypedMerge(OpKernelContext& context,
                         const Tensor& X_selection_tensor, const Tensor& Y_selection_tensor,
                         const ProcessBroadcastSpanFuncs& functors) {
  InputBroadcaster merge_broadcaster{X_selection_tensor, Y_selection_tensor};
  Tensor& output = *context.Output(0, merge_broadcaster.GetOutputShape());
 
  OutputBroadcaster output_broadcaster{merge_broadcaster.GetSpanSize(), output};
  BroadcastHelper broadcast_helper(merge_broadcaster, output_broadcaster);
 
  BroadcastLooper(broadcast_helper, functors);
}
}  // namespace
 
namespace contrib {
 
QLinearWhere::QLinearWhere(const OpKernelInfo& info) : OpKernel(info) {
  size_t input_def_count = info.node().InputDefs().size();
  ORT_ENFORCE(input_def_count == kExpected_input_count,
              "There must be ", kExpected_input_count, " inputs! (condition, x, x_scale, x_zero_point, y, y_scale, y_zero_point, z_scale, z_zero_point)");
  const Tensor* tensor_x_scale = nullptr;
  const Tensor* tensor_x_zero_point = nullptr;
  const Tensor* tensor_y_scale = nullptr;
  const Tensor* tensor_y_zero_point = nullptr;
  const Tensor* tensor_z_scale = nullptr;
  const Tensor* tensor_z_zero_point = nullptr;
 
  bool get_x_scale = info.TryGetConstantInput(2, &tensor_x_scale);
  bool get_x_zero_point = info.TryGetConstantInput(3, &tensor_x_zero_point);
  bool get_y_scale = info.TryGetConstantInput(5, &tensor_y_scale);
  bool get_y_zero_point = info.TryGetConstantInput(6, &tensor_y_zero_point);
  bool get_z_scale = info.TryGetConstantInput(7, &tensor_z_scale);
  bool get_z_zero_point = info.TryGetConstantInput(8, &tensor_z_zero_point);
  if (!get_z_scale || !get_z_zero_point) {
    // Can not build fix lookup table
    return;
  }
  ORT_ENFORCE(
      tensor_x_zero_point->GetElementType() == tensor_y_zero_point->GetElementType() &&
          tensor_x_zero_point->GetElementType() == tensor_z_zero_point->GetElementType() &&
          tensor_y_zero_point->GetElementType() == tensor_z_zero_point->GetElementType(),
      "Wrong input type encountered for zero point input def of x, y, z");
  bool is_signed_int8 = tensor_z_zero_point->IsDataType<int8_t>();
  const auto identity_float = [](float v) -> float { return v; };
 
  if (get_x_scale && get_x_zero_point) {
    // Build fix lookup table for x
    is_x_fixed_copy_ = has_same_scale(tensor_x_scale, tensor_z_scale) &&
                       has_same_zero_point(is_signed_int8, tensor_x_zero_point, tensor_z_zero_point);
    if (!is_x_fixed_copy_) {
      x_fixed_lookup_table_.resize(256);
      if (is_signed_int8) {
        QlinearBuildLookupTable<int8_t>(
            x_fixed_lookup_table_.data(), tensor_x_scale, tensor_x_zero_point,
            tensor_z_scale, tensor_z_zero_point, identity_float);
      } else {
        QlinearBuildLookupTable<uint8_t>(
            x_fixed_lookup_table_.data(), tensor_x_scale, tensor_x_zero_point,
            tensor_z_scale, tensor_z_zero_point, identity_float);
      }
    }
    is_x_dynamic_ = false;
  }
 
  if (get_y_scale && get_y_zero_point) {
    // Build fix lookup table for y
    is_y_fixed_copy_ = has_same_scale(tensor_y_scale, tensor_z_scale) &&
                       has_same_zero_point(is_signed_int8, tensor_y_zero_point, tensor_z_zero_point);
    if (!is_y_fixed_copy_) {
      y_fixed_lookup_table_.resize(256);
      if (is_signed_int8) {
        QlinearBuildLookupTable<int8_t>(
            y_fixed_lookup_table_.data(), tensor_y_scale, tensor_y_zero_point,
            tensor_z_scale, tensor_z_zero_point, identity_float);
      } else {
        QlinearBuildLookupTable<uint8_t>(
            y_fixed_lookup_table_.data(), tensor_y_scale, tensor_y_zero_point,
            tensor_z_scale, tensor_z_zero_point, identity_float);
      }
    }
    is_y_dynamic_ = false;
  }
}
 
Status QLinearWhere::Compute(OpKernelContext* ctx) const {
  //  const auto* tensor_condition = ctx->Input<Tensor>(0);
  //  const auto* tensor_x_input = ctx->Input<Tensor>(1);
  const auto* tensor_x_scale = ctx->Input<Tensor>(2);
  const auto* tensor_x_zero_point = ctx->Input<Tensor>(3);
  //  const auto* tensor_y_input = ctx->Input<Tensor>(4);
  const auto* tensor_y_scale = ctx->Input<Tensor>(5);
  const auto* tensor_y_zero_point = ctx->Input<Tensor>(6);
  const auto* tensor_z_scale = ctx->Input<Tensor>(7);
  const auto* tensor_z_zero_point = ctx->Input<Tensor>(8);
  //  auto* tensor_output = ctx->Output(0, tensor_condition->Shape());
  ORT_ENFORCE(tensor_x_scale->IsDataType<float>(), "Input scale is not float for quantized input x @ 2");
  ORT_ENFORCE(tensor_y_scale->IsDataType<float>(), "Input scale is not float for quantized input y @ 5");
  ORT_ENFORCE(tensor_z_scale->IsDataType<float>(), "Input scale is not float for quantized output z @ 7");
  ORT_ENFORCE(tensor_x_zero_point->GetElementType() == tensor_y_zero_point->GetElementType() &&
                  tensor_x_zero_point->GetElementType() == tensor_z_zero_point->GetElementType() &&
                  tensor_y_zero_point->GetElementType() == tensor_z_zero_point->GetElementType(),
              "Wrong input type encountered for zero point of quantized input @", 3, 6, 8);
  bool is_signed_int8 = tensor_z_zero_point->IsDataType<int8_t>();
  const auto identity_float = [](float v) -> float { return v; };
 
  std::vector<uint8_t> x_dynamic_lookup_table;
  bool is_x_copy = !is_x_dynamic_ ? is_x_fixed_copy_ : has_same_scale(tensor_x_scale, tensor_z_scale) && has_same_zero_point(is_signed_int8, tensor_x_zero_point, tensor_z_zero_point);
  if (is_x_dynamic_ && !is_x_copy) {
    x_dynamic_lookup_table.resize(256);
    if (is_signed_int8) {
      QlinearBuildLookupTable<int8_t>(
          x_dynamic_lookup_table.data(), tensor_x_scale, tensor_x_zero_point,
          tensor_z_scale, tensor_z_zero_point, identity_float);
    } else {
      QlinearBuildLookupTable<uint8_t>(
          x_dynamic_lookup_table.data(), tensor_x_scale, tensor_x_zero_point,
          tensor_z_scale, tensor_z_zero_point, identity_float);
    }
  }
 
  // Build dynamic lookup table for y
  std::vector<uint8_t> y_dynamic_lookup_table;
  bool is_y_copy = !is_y_dynamic_ ? is_y_fixed_copy_ : has_same_scale(tensor_y_scale, tensor_z_scale) && has_same_zero_point(is_signed_int8, tensor_y_zero_point, tensor_z_zero_point);
  if (is_y_dynamic_ && !is_y_copy) {
    y_dynamic_lookup_table.resize(256);
    if (is_signed_int8) {
      QlinearBuildLookupTable<int8_t>(
          y_dynamic_lookup_table.data(), tensor_y_scale, tensor_y_zero_point,
          tensor_z_scale, tensor_z_zero_point, identity_float);
    } else {
      QlinearBuildLookupTable<uint8_t>(
          y_dynamic_lookup_table.data(), tensor_y_scale, tensor_y_zero_point,
          tensor_z_scale, tensor_z_zero_point, identity_float);
    }
  }
  // Each lookup table is 256 bytes
  const auto& x_lookup_table = is_x_dynamic_ ? x_dynamic_lookup_table : x_fixed_lookup_table_;
  const auto& y_lookup_table = is_y_dynamic_ ? y_dynamic_lookup_table : y_fixed_lookup_table_;
 
  // Each user_data will sized at 2 + 256 bytes, and contain {is_x/y, is_x/y_copy, x/y_lookup_table} respectively
  std::vector<uint8_t> x_user_data(258);
  std::vector<uint8_t> y_user_data(258);
  x_user_data[0] = 1;
  y_user_data[0] = 0;
  x_user_data[1] = is_x_copy ? 1 : 0;
  y_user_data[1] = is_y_copy ? 1 : 0;
  if (!is_x_copy) {
    std::copy(x_lookup_table.begin(), x_lookup_table.end(), x_user_data.begin() + 2);
  }
  if (!is_y_copy) {
    std::copy(y_lookup_table.begin(), y_lookup_table.end(), y_user_data.begin() + 2);
  }
  // Allocator, Allocation, and SelectBroadcastFuncs are the same implementation from where_op.cc
  const auto typed_tensor_allocation = [](const TensorAllocator& allocator,
                                          const TensorShape& shape) {
    return allocator.Allocate<uint8_t>(shape);
  };
  TensorAllocator tensor_allocator{*ctx};
  ProcessBroadcastSpanFuncs funcs = SelectBroadcastFuncs<uint8_t>();
  // UntypedSelect is MODIFIED from where_op.cc
  auto X_selection_tensor = UntypedSelect(*ctx, x_user_data, funcs, tensor_allocator, typed_tensor_allocation);
  auto Y_selection_tensor = UntypedSelect(*ctx, y_user_data, funcs, tensor_allocator, typed_tensor_allocation);
  // UntypedMerge is the same as from where_op.cc
  UntypedMerge(*ctx, *X_selection_tensor, *Y_selection_tensor, MergeBroadcastFuncs<uint8_t>());
 
  return Status();
}
 
ONNX_CPU_OPERATOR_MS_KERNEL(
    QLinearWhere,
    1,
    KernelDefBuilder().TypeConstraint(
        "T",
        {DataTypeImpl::GetTensorType<uint8_t>(), DataTypeImpl::GetTensorType<int8_t>()}),
    QLinearWhere)
}  // namespace contrib
}  // namespace onnxruntime