Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
 
#include <cmath>
#include "core/framework/print_tensor_utils.h"
#include "core/framework/int2.h"
 
namespace onnxruntime {
namespace utils {
 
// Currently we only store statistics data for float tensors that printed to stdout.
// It can be extended to other types if needed.
struct TensorStatisticsData {
  bool is_float = false;
  float float_min;
  float float_max;
};
 
template <typename T>
int my_fpclassify(const T& val) {
  return std::fpclassify(val);
}
 
template <>
int my_fpclassify(const MLFloat16& val) {
  return std::fpclassify(val.ToFloat());
}
 
template <>
int my_fpclassify(const BFloat16& val) {
  return std::fpclassify(val.ToFloat());
}
 
template <typename T>
void PrintFloatStats(const T* data, size_t count) {
  size_t inf = 0;
  size_t nan = 0;
  size_t zero = 0;
  size_t subnormal = 0;
  for (size_t i = 0; i < count; i++) {
    switch (my_fpclassify(data[i])) {
      case FP_INFINITE:
        inf++;
        break;
      case FP_NAN:
        nan++;
        break;
      case FP_SUBNORMAL:
        subnormal++;
        break;
      case FP_ZERO:
        zero++;
        break;
      default:
        break;
    }
  }
 
  if (inf)
    std::cout << ",Inf=" << inf;
  if (nan)
    std::cout << ",NaN=" << nan;
  if (zero)
    std::cout << ",Zero=" << zero;
  if (subnormal)
    std::cout << ",SubNormal=" << subnormal;
}
 
template <typename T>
void PrintCommonStats(const T* data, size_t count, TensorStatisticsData& tensor_statistics) {
  T min = data[0];
  T max = min;
  for (size_t i = 1; i < count; i++) {
    auto value = data[i];
    if (value > max) {
      max = value;
    }
    if (value < min) {
      min = value;
    }
  }
 
  std::cout << "Min=";
  PrintValue(min);
 
  std::cout << ",Max=";
  PrintValue(max);
 
  // Statistics for float and double only for now.
  if constexpr (std::is_same<T, float>::value) {
    tensor_statistics.is_float = true;
    tensor_statistics.float_min = static_cast<float>(min);
    tensor_statistics.float_max = static_cast<float>(max);
  }
}
 
#define DEF_PRINT_COMMON_STATS_PACKED(PACKED_TYPE)                    \
  template <>                                                         \
  inline void PrintCommonStats<PACKED_TYPE>(                          \
      const PACKED_TYPE* data, size_t count, TensorStatisticsData&) { \
    using UnpackedType = typename PACKED_TYPE::UnpackedType;          \
    UnpackedType min = data[0].GetElem(0);                            \
    UnpackedType max = min;                                           \
    for (size_t i = 1; i < count; i++) {                              \
      auto indices = PACKED_TYPE::GetTensorElemIndices(i);            \
      auto value = data[indices.first].GetElem(indices.second);       \
      if (value > max) {                                              \
        max = value;                                                  \
      }                                                               \
      if (value < min) {                                              \
        min = value;                                                  \
      }                                                               \
    }                                                                 \
                                                                      \
    std::cout << "Min=";                                              \
    PrintValue(min);                                                  \
                                                                      \
    std::cout << ",Max=";                                             \
    PrintValue(max);                                                  \
  }
 
DEF_PRINT_COMMON_STATS_PACKED(Int4x2)
DEF_PRINT_COMMON_STATS_PACKED(UInt4x2)
#if !defined(DISABLE_FLOAT4_TYPES)
DEF_PRINT_COMMON_STATS_PACKED(Float4E2M1x2)
#endif
DEF_PRINT_COMMON_STATS_PACKED(Int2x4)
DEF_PRINT_COMMON_STATS_PACKED(UInt2x4)
 
template <typename T>
void PrintHalfStats(const T* data, size_t count) {
  float min = data[0].ToFloat();
  float max = min;
  for (size_t i = 1; i < count; i++) {
    float value = data[i].ToFloat();
    if (value > max) {
      max = value;
    }
 
    if (value < min) {
      min = value;
    }
  }
 
  std::cout << "Min=";
  PrintValue(min);
 
  std::cout << ",Max=";
  PrintValue(max);
}
 
template <typename T>
void PrintTensorStats(const T* tensor, size_t count, TensorStatisticsData& tensor_statistics) {
  PrintCommonStats<T>(tensor, count, tensor_statistics);
}
 
template <>
void PrintTensorStats<float>(const float* tensor, size_t count, TensorStatisticsData& tensor_statistics) {
  PrintCommonStats<float>(tensor, count, tensor_statistics);
  PrintFloatStats<float>(tensor, count);
}
 
template <>
void PrintTensorStats<double>(const double* tensor, size_t count, TensorStatisticsData& tensor_statistics) {
  PrintCommonStats<double>(tensor, count, tensor_statistics);
  PrintFloatStats<double>(tensor, count);
}
 
template <>
void PrintTensorStats<MLFloat16>(const MLFloat16* tensor, size_t count, TensorStatisticsData&) {
  PrintHalfStats<MLFloat16>(tensor, count);
  PrintFloatStats<MLFloat16>(tensor, count);
}
 
template <>
void PrintTensorStats<BFloat16>(const BFloat16* tensor, size_t count, TensorStatisticsData&) {
  PrintHalfStats<BFloat16>(tensor, count);
  PrintFloatStats<BFloat16>(tensor, count);
}
 
template <typename T>
void PrintCpuTensorStats(const Tensor& tensor, TensorStatisticsData& tensor_statistics) {
  const auto& shape = tensor.Shape();
  auto num_items = shape.Size();
  if (num_items == 0) {
    return;
  }
 
  const T* data = tensor.Data<T>();
  PrintTensorStats<T>(data, num_items, tensor_statistics);
  std::cout << std::endl;
}
 
template <>
void PrintCpuTensorStats<std::string>(const Tensor&, TensorStatisticsData&) {
}
 
}  // namespace utils
}  // namespace onnxruntime