Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#ifdef DEBUG_NODE_INPUTS_OUTPUTS
 
#include "core/framework/debug_node_inputs_outputs_utils.h"
#include "core/framework/print_tensor_utils.h"
#include "core/framework/print_tensor_statistics_utils.h"
#include <iomanip>
#include <cctype>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
 
#ifdef DEBUG_NODE_INPUTS_OUTPUTS_ENABLE_DUMP_TO_SQLDB
#include <sqlite3.h>
#endif
 
#include "core/common/path_utils.h"
#include "core/framework/tensorprotoutils.h"
#include "core/platform/env.h"
#include "core/platform/env_var_utils.h"
 
namespace onnxruntime {
namespace utils {
 
void NodeDumpAnalysis::Add(const std::string& node_name, const std::string& op_type, bool is_half_overflow) {
  std::lock_guard<std::mutex> lock(set_mutex);
  if (is_half_overflow) {
    auto p = half_overflow_nodes.insert(node_name);
    if (p.second) {  // insert succeeded
      ++half_overflow_ops[op_type];
    }
  }
 
  counter++;
}
 
void NodeDumpAnalysis::PrintToStdOut(const std::string& model_path) {
  std::lock_guard<std::mutex> lock(set_mutex);
  if (counter == 0) {
    return;
  }
 
  // We added counter twice per node (once for node inputs, once for node outputs), so we need to divide it by 2.
  counter /= 2;
 
  std::cout << "Total counter in node dumping: " << counter << std::endl;
 
  if (!half_overflow_nodes.empty()) {
    std::cout << "Found " << half_overflow_nodes.size() << " nodes cannot be converted to half precision due to potential input/output overflow." << std::endl;
 
    if (half_overflow_nodes.count("") > 0) {
      std::cout << "Warning: some node name is empty and node_block_list is not completed. "
                << "Please update the model to make sure each node has name then run this tool again!" << std::endl;
    }
 
    // Sort and display the op frequency in the descending order
    std::cout << "Operator frequencies for these nodes:" << std::endl;
    std::vector<std::pair<std::string, int>> op_freq(half_overflow_ops.begin(), half_overflow_ops.end());
    std::sort(op_freq.begin(), op_freq.end(),
              [](const std::pair<std::string, int>& a, const std::pair<std::string, int>& b) {
                return b.second < a.second;
              });
    for (const auto& pair : op_freq) {
      std::cout << pair.first << " : " << pair.second << std::endl;
    }
  } else {
    std::cout << "No node has potential overflow during half conversion so node_block_list is empty." << std::endl;
  }
 
  std::cout << "# -------" << std::endl;
  std::cout << "# Example python script for float16 conversion" << std::endl;
  std::cout << "# For details, search `node_block_list` in https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/python/tools/transformers/float16.py" << std::endl;
  std::cout << "# -------" << std::endl;
  std::cout << "from onnxruntime.transformers.onnx_model import OnnxModel" << std::endl;
  std::cout << "m = OnnxModel(onnx.load('" << model_path << "'))" << std::endl;
  if (!half_overflow_nodes.empty()) {
    std::cout << "node_block_list = [" << std::endl;
    for (const auto& node : half_overflow_nodes) {
      if (!node.empty()) {
        std::cout << "  '" << node << "'," << std::endl;
      }
    }
    std::cout << "]" << std::endl;
    std::cout << "m.convert_float_to_float16(keep_io_types=False, node_block_list=node_block_list)" << std::endl;
  } else {
    std::cout << "m.convert_float_to_float16(keep_io_types=False)" << std::endl;
  }
 
  std::cout << "m.save_model_to_file('fp16/optimized.onnx', use_external_data_format=False)" << std::endl;
}
 
namespace {
 
struct TensorMetadata {
  std::string name;
  std::string producer;
  std::string consumer;
  std::string device_type;
  std::string ep_type;
  size_t step;
};
 
std::string GetCleanedEpType(const Node& node) {
  std::string ep_type = node.GetExecutionProviderType();
 
  // Remove "ExecutionProvider" suffix from ep type to reduce length.
  const std::string suffix_to_remove = "ExecutionProvider";
  size_t pos = ep_type.find(suffix_to_remove);
  if (pos != std::string::npos) {
    ep_type.erase(pos, suffix_to_remove.length());
  }
 
  return ep_type;
}
 
bool FilterNode(const NodeDumpOptions& dump_options, const Node& node) {
  auto match_pattern =
      [](const std::string& value, const std::string& delimited_patterns) {
        // match all if empty
        if (delimited_patterns.empty()) return true;
 
        // search for exact match against delimited patterns
        auto pattern_begin = delimited_patterns.begin();
        while (true) {
          const auto pattern_end = std::find(
              pattern_begin, delimited_patterns.end(), kFilterPatternDelimiter);
 
          if (std::equal(value.begin(), value.end(), pattern_begin, pattern_end)) return true;
 
          if (pattern_end == delimited_patterns.end()) break;
 
          pattern_begin = pattern_end + 1;
        }
 
        return false;
      };
 
  return match_pattern(node.Name(), dump_options.filter.name_pattern) &&
         match_pattern(node.OpType(), dump_options.filter.op_type_pattern);
}
 
template <typename T>
void DumpTensorToStdOut(const Tensor& tensor, const NodeDumpOptions& dump_options, TensorStatisticsData& tensor_statistics) {
  if ((dump_options.dump_flags & NodeDumpOptions::DumpFlags::InputData) != 0) {
    onnxruntime::utils::PrintCpuTensor<T>(tensor, dump_options.snippet_threshold, dump_options.snippet_edge_items);
  }
 
  if ((dump_options.dump_flags & NodeDumpOptions::DumpFlags::StatisticsData) != 0) {
    onnxruntime::utils::PrintCpuTensorStats<T>(tensor, tensor_statistics);
  }
}
 
PathString MakeTensorFileName(const TensorMetadata& tensor_metadata, const NodeDumpOptions& dump_options) {
  auto make_valid_name = [](std::string name) {
    std::replace_if(
        name.begin(), name.end(), [](char c) { return !std::isalnum(c); }, '_');
    return name;
  };
 
  if (dump_options.prepend_ep_to_file_name) {
    return path_utils::MakePathString(make_valid_name(tensor_metadata.ep_type + "_" + tensor_metadata.name), dump_options.file_suffix, ".tensorproto");
  } else {
    return path_utils::MakePathString(make_valid_name(tensor_metadata.name), dump_options.file_suffix, ".tensorproto");
  }
}
 
void DumpTensorToFile(const Tensor& tensor, const std::string& tensor_name, const std::filesystem::path& file_path) {
  auto tensor_proto = utils::TensorToTensorProto(tensor, tensor_name);
  const PathString file_path_str = file_path.native();
  int output_fd;
  ORT_THROW_IF_ERROR(Env::Default().FileOpenWr(file_path_str, output_fd));
  try {
    ORT_ENFORCE(
        tensor_proto.SerializeToFileDescriptor(output_fd),
        "Failed to write tensor to file - tensor: ", tensor_name, ", file: ", ToUTF8String(file_path_str));
  } catch (...) {
    ORT_IGNORE_RETURN_VALUE(Env::Default().FileClose(output_fd));
    throw;
  }
  ORT_THROW_IF_ERROR(Env::Default().FileClose(output_fd));
}
 
#ifdef DEBUG_NODE_INPUTS_OUTPUTS_ENABLE_DUMP_TO_SQLDB
sqlite3* SqliteConnection() {
  static thread_local std::unique_ptr<sqlite3, decltype(&sqlite3_close)> sqlite_db(
      []() {
        std::stringstream ss;
        ss << "-pid" << Env::Default().GetSelfPid() << ".db";
        const auto& opt = NodeDumpOptionsFromEnvironmentVariables();
        auto sqlite_db_prefix = opt.sqlite_db_prefix;
        auto sqlite_db_path = sqlite_db_prefix.Concat(ss.str()).ToPathString();
 
        sqlite3* db;
        int rc = sqlite3_open(sqlite_db_path.c_str(), &db);
        ORT_ENFORCE(rc == SQLITE_OK, "Failed to connect to sqlite3 db ", sqlite_db_path.c_str());
 
        const char* sql_create_tensor_table =
            "Create table if not exists Tensors ( "
            "  Step int not null, "
            "  Name text not null, "
            "  Value TensorProto, "
            "  DeviceType text, "
            "  TracedProducer NodeArg, "
            "  TracedConsumers NodeArgList, "
            "  primary key (Step, Name) "
            ");";
 
        const char* error_message = nullptr;
        rc = sqlite3_exec(db, sql_create_tensor_table, nullptr, 0, (char**)&error_message);
        ORT_ENFORCE(rc == SQLITE_OK,
                    "Failed to create Tensors table in sqlite3 db ", sqlite_db_path.c_str(),
                    " on ", error_message);
 
        const char* sql_create_node_table =
            "Create table if not exists Nodes ( "
            "  ExecutionCounter int, "
            "  Name text primary key not null, "
            "  OpType text not null, "
            "  ExecutionProvider text "
            ");";
 
        rc = sqlite3_exec(db, sql_create_node_table, nullptr, 0, (char**)&error_message);
        ORT_ENFORCE(rc == SQLITE_OK,
                    "Failed to create Nodes table in sqlite3 db ", sqlite_db_path.c_str(),
                    " on ", error_message);
 
        return db;
      }(),
      &sqlite3_close);
 
  return sqlite_db.get();
}
 
#define SQL_OK(command) \
  ORT_ENFORCE((command) == SQLITE_OK, "Failed sql operation on ", sqlite3_errmsg(SqliteConnection()))
 
void SqlStepWithRetry(sqlite3_stmt* stmt, int sql_expected) {
  int attempt = 0;
  while (true) {
    int rc = sqlite3_step(stmt);
    if (rc == sql_expected) {
      return;
    }
 
    if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
      if (attempt % 10000 == 0) {
        std::cerr << "Warning: Pid " << Env::Default().GetSelfPid()
                  << " gently spinning on sql db busy or locked\n";
      }
      Env::Default().SleepForMicroseconds(100);
      attempt++;
      continue;
    }
 
    ORT_THROW("Failed sql step for ", sqlite3_expanded_sql(stmt), " on ", sqlite3_errmsg(SqliteConnection()));
  }
}
 
bool TensorExistsInSqlDb(const TensorMetadata& tensor_metadata) {
  static thread_local std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)> stmt_uptr(
      []() {
        sqlite3* db = SqliteConnection();
 
        const char* sql_tensor_exists =
            "select count(name) from Tensors where Name == ? and Step == ?;";
 
        sqlite3_stmt* stmt = nullptr;
        SQL_OK(sqlite3_prepare_v2(db, sql_tensor_exists, -1, &stmt, nullptr));
 
        return stmt;
      }(),
      &sqlite3_finalize);
 
  sqlite3_stmt* stmt = stmt_uptr.get();
 
  SQL_OK(sqlite3_reset(stmt));
  SQL_OK(sqlite3_bind_text(stmt, 1, tensor_metadata.name.c_str(), -1, SQLITE_TRANSIENT));
  SQL_OK(sqlite3_bind_int(stmt, 2, (int)tensor_metadata.step));
  SqlStepWithRetry(stmt, SQLITE_ROW);
  bool exists = sqlite3_column_int(stmt, 0) > 0;
  SqlStepWithRetry(stmt, SQLITE_DONE);
 
  return exists;
}
 
void InsertTensorInSqlDb(const Tensor& tensor, const TensorMetadata& tensor_metadata) {
  static thread_local std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)> stmt_uptr(
      []() {
        sqlite3* db = SqliteConnection();
 
        const char* sql_insert_tensor =
            "Insert into Tensors (Step, Name, Value, DeviceType, TracedProducer, TracedConsumers) "
            " values (?, ?, ?, ?, \"\", \"\"); ";
 
        sqlite3_stmt* stmt = nullptr;
        SQL_OK(sqlite3_prepare_v2(db, sql_insert_tensor, -1, &stmt, nullptr));
 
        return stmt;
      }(),
      &sqlite3_finalize);
 
  sqlite3_stmt* stmt = stmt_uptr.get();
 
  SQL_OK(sqlite3_reset(stmt));
  SQL_OK(sqlite3_bind_int(stmt, 1, tensor_metadata.step));
  SQL_OK(sqlite3_bind_text(stmt, 2, tensor_metadata.name.c_str(), -1, SQLITE_TRANSIENT));
 
  auto tensor_proto = utils::TensorToTensorProto(tensor, tensor_metadata.name);
  std::string bytes = tensor_proto.SerializeAsString();
  const char* data = bytes.data();
  int size = bytes.size();
 
  SQL_OK(sqlite3_bind_blob(stmt, 3, data, size, SQLITE_TRANSIENT));
  SQL_OK(sqlite3_bind_text(stmt, 4, tensor_metadata.device_type.c_str(), -1, SQLITE_TRANSIENT));
 
  SqlStepWithRetry(stmt, SQLITE_DONE);
}
 
void UpdateTensorUsageInSqlDb(const TensorMetadata& tensor_metadata) {
  static thread_local std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)> stmt_uptr(
      []() {
        sqlite3* db = SqliteConnection();
 
        const char* sql_update_tensor =
            "Update Tensors set "
            "  TracedProducer = TracedProducer || ?, "
            "  TracedConsumers = TracedConsumers || ? "
            "where Name = ? and Step = ?;";
 
        sqlite3_stmt* stmt = nullptr;
        SQL_OK(sqlite3_prepare_v2(db, sql_update_tensor, -1, &stmt, nullptr));
 
        return stmt;
      }(),
      &sqlite3_finalize);
 
  sqlite3_stmt* stmt = stmt_uptr.get();
 
  SQL_OK(sqlite3_reset(stmt));
  SQL_OK(sqlite3_bind_text(stmt, 1, tensor_metadata.producer.c_str(), -1, SQLITE_TRANSIENT));
  SQL_OK(sqlite3_bind_text(stmt, 2, tensor_metadata.consumer.c_str(), -1, SQLITE_TRANSIENT));
  SQL_OK(sqlite3_bind_text(stmt, 3, tensor_metadata.name.c_str(), -1, SQLITE_TRANSIENT));
  SQL_OK(sqlite3_bind_int(stmt, 4, tensor_metadata.step));
 
  SqlStepWithRetry(stmt, SQLITE_DONE);
}
 
void DumpTensorToSqliteDb(const Tensor& tensor, const TensorMetadata& tensor_metadata) {
  if (!TensorExistsInSqlDb(tensor_metadata)) {
    InsertTensorInSqlDb(tensor, tensor_metadata);
  }
 
  UpdateTensorUsageInSqlDb(tensor_metadata);
}
 
void InsertNodePlacementToSqliteDb(const NodeDumpContext& dump_context, const Node& node) {
  static thread_local std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)> stmt_uptr(
      []() {
        sqlite3* db = SqliteConnection();
 
        const char* sql_insert_node =
            "Insert or Ignore into Nodes (ExecutionCounter, Name, OpType, ExecutionProvider) "
            " values (?, ?, ?, ?);";
 
        sqlite3_stmt* stmt = nullptr;
        SQL_OK(sqlite3_prepare_v2(db, sql_insert_node, -1, &stmt, nullptr));
 
        return stmt;
      }(),
      &sqlite3_finalize);
 
  sqlite3_stmt* stmt = stmt_uptr.get();
 
  SQL_OK(sqlite3_reset(stmt));
  SQL_OK(sqlite3_bind_int(stmt, 1, dump_context.program_counter));
  SQL_OK(sqlite3_bind_text(stmt, 2, node.Name().c_str(), -1, SQLITE_TRANSIENT));
  SQL_OK(sqlite3_bind_text(stmt, 3, node.OpType().c_str(), -1, SQLITE_TRANSIENT));
  SQL_OK(sqlite3_bind_text(stmt, 4, node.GetExecutionProviderType().c_str(), -1, SQLITE_TRANSIENT));
 
  SqlStepWithRetry(stmt, SQLITE_DONE);
}
#endif  // DEBUG_NODE_INPUTS_OUTPUTS_ENABLE_DUMP_TO_SQLDB
 
void DumpCpuTensor(
    const NodeDumpOptions& dump_options,
    const Tensor& tensor, const TensorMetadata& tensor_metadata, TensorStatisticsData& tensor_statistics) {
  switch (dump_options.data_destination) {
    case NodeDumpOptions::DataDestination::StdOut: {
      DispatchOnTensorType(tensor.DataType(), DumpTensorToStdOut, tensor, dump_options, tensor_statistics);
      break;
    }
    case NodeDumpOptions::DataDestination::TensorProtoFiles: {
      const std::filesystem::path tensor_file = dump_options.output_dir / MakeTensorFileName(tensor_metadata, dump_options);
      DumpTensorToFile(tensor, tensor_metadata.name, tensor_file);
      break;
    }
    case NodeDumpOptions::DataDestination::SqliteDb: {
#ifdef DEBUG_NODE_INPUTS_OUTPUTS_ENABLE_DUMP_TO_SQLDB
      DumpTensorToSqliteDb(tensor, tensor_metadata);
#else
      ORT_THROW("Recompile with --cmake_extra_defines onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS=1 onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS_ENABLE_DUMP_TO_SQLDB=1");
#endif
      break;
    }
    default:
      ORT_THROW("Unsupported data destination type: ", static_cast<int>(dump_options.data_destination));
  }
}
 
void DumpTensor(
    const NodeDumpOptions& dump_options,
    const Tensor& tensor, TensorMetadata& tensor_metadata, TensorStatisticsData& tensor_statistics,
    const SessionState& session_state) {
  // check tensor is on CPU before dumping it
  auto& tensor_location = tensor.Location();
  if (tensor_location.device.UsesCpuMemory()) {
    tensor_metadata.device_type = "CPU";
    DumpCpuTensor(dump_options, tensor, tensor_metadata, tensor_statistics);
  } else {
    std::cout << tensor_location << "\n";
 
#if defined(USE_CUDA) || defined(USE_DML)
    const auto data_type = tensor.DataType();
    // Dumping GPU only when cuda is enabled.
    if (tensor_location.device.Type() == OrtDevice::GPU) {
      const auto& execution_providers = session_state.GetExecutionProviders();
      const auto* cpu_execution_provider = execution_providers.Get(onnxruntime::kCpuExecutionProvider);
      auto cpu_allocator = session_state.GetAllocator(cpu_execution_provider->GetOrtDeviceByMemType(OrtMemTypeDefault));
      Tensor cpu_tensor{data_type, tensor.Shape(), cpu_allocator};
      const auto& data_transfer_mgr = session_state.GetDataTransferMgr();
      auto status = data_transfer_mgr.CopyTensor(tensor, cpu_tensor);
      if (status == common::Status::OK()) {
        tensor_metadata.device_type = "GPU";
        DumpCpuTensor(dump_options, cpu_tensor, tensor_metadata, tensor_statistics);
      } else {
        std::cout << " failed to transfer data to cpu.\n";
      }
    }
#else
    ORT_UNUSED_PARAMETER(session_state);
#endif
  }
}
 
}  // namespace
 
const NodeDumpOptions& NodeDumpOptionsFromEnvironmentVariables() {
  static const NodeDumpOptions node_dump_options = []() {
    namespace env_vars = debug_node_inputs_outputs_env_vars;
 
    NodeDumpOptions opts{};
 
    // Preserve existing behavior of printing the shapes by default. Turn it off only if the user has requested so
    // explicitly by setting the value of the env variable to 0.
    opts.dump_flags = NodeDumpOptions::DumpFlags::None;
    if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpShapeData, true)) {
      opts.dump_flags |= NodeDumpOptions::DumpFlags::Shape;
    }
 
    if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpInputData, false)) {
      opts.dump_flags |= NodeDumpOptions::DumpFlags::InputData;
    }
    if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpOutputData, false)) {
      opts.dump_flags |= NodeDumpOptions::DumpFlags::OutputData;
    }
    if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpNodePlacement, true)) {
      opts.dump_flags |= NodeDumpOptions::DumpFlags::NodePlacement;
    }
    if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpStatisticsData, false)) {
      opts.dump_flags |= NodeDumpOptions::DumpFlags::StatisticsData;
    }
    if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpHalfConversionOverflow, false)) {
      // Statistics data is required for half conversion overflow detection.
      opts.dump_flags |= NodeDumpOptions::DumpFlags::StatisticsData;
      opts.dump_flags |= NodeDumpOptions::DumpFlags::HalfConversionOverflow;
    }
 
    opts.filter.name_pattern = Env::Default().GetEnvironmentVar(env_vars::kNameFilter);
    opts.filter.op_type_pattern = Env::Default().GetEnvironmentVar(env_vars::kOpTypeFilter);
 
    const std::string destination = ParseEnvironmentVariableWithDefault<std::string>(
        env_vars::kDumpDataDestination, "stdout");
 
    if (destination == "files") {
      opts.data_destination = NodeDumpOptions::DataDestination::TensorProtoFiles;
    } else if (destination == "sqlite") {
      opts.data_destination = NodeDumpOptions::DataDestination::SqliteDb;
    } else if (destination != "stdout") {
      ORT_THROW("Unsupported data destination type: ", destination);
    }
 
    // Snippet options for StdOut
    opts.snippet_threshold = ParseEnvironmentVariableWithDefault<int>(env_vars::kSnippetThreshold, kDefaultSnippetThreshold);
    opts.snippet_edge_items = ParseEnvironmentVariableWithDefault<int>(env_vars::kSnippetEdgeItems, kDefaultSnippetEdgeItems);
 
    constexpr int kMaxHalfThreshold = 65504;
    // The default value is set to have reasonable margin for input variance.
    int threshold = ParseEnvironmentVariableWithDefault<int>(env_vars::kHalfOverflowThreshold, 50000);
    ORT_ENFORCE(threshold > 0 && threshold <= kMaxHalfThreshold,
                debug_node_inputs_outputs_env_vars::kHalfOverflowThreshold, " shall be a positive integer <= ", kMaxHalfThreshold);
    opts.half_overflow_threshold = static_cast<float>(threshold);
 
    opts.prepend_ep_to_file_name = ParseEnvironmentVariableWithDefault<bool>(env_vars::kPrependEpToFileName, false);
 
    if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kAppendRankToFileName, false)) {
      std::string rank = Env::Default().GetEnvironmentVar("OMPI_COMM_WORLD_RANK");
      if (rank.empty()) {
        opts.file_suffix = "_default_rank_0";
      } else {
        opts.file_suffix = "_rank_" + rank;
      }
    }
 
    opts.output_dir = ToPathString(Env::Default().GetEnvironmentVar(env_vars::kOutputDir));
 
    std::string sqlite_db_prefix =
        ParseEnvironmentVariableWithDefault<std::string>(env_vars::kSqliteDbPrefix, "execution-trace");
    opts.sqlite_db_prefix = ToPathString(sqlite_db_prefix);
 
    // check for confirmation for dumping data to files for all nodes
    const bool is_input_or_output_requested = ((opts.dump_flags & NodeDumpOptions::DumpFlags::InputData) != 0) ||
                                              ((opts.dump_flags & NodeDumpOptions::DumpFlags::OutputData) != 0);
 
    if (is_input_or_output_requested &&
        opts.data_destination == NodeDumpOptions::DataDestination::TensorProtoFiles &&
        opts.filter.name_pattern.empty() && opts.filter.op_type_pattern.empty()) {
      ORT_ENFORCE(
          ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpingDataToFilesForAllNodesIsOk, false),
          "The current environment variable configuration will dump node input or output data to files for every node. "
          "This may cause a lot of files to be generated. Set the environment variable ",
          env_vars::kDumpingDataToFilesForAllNodesIsOk, " to confirm this is what you want.");
    }
 
    return opts;
  }();
 
  return node_dump_options;
}
 
static bool IsAnyOutputDumped(const NodeDumpOptions& dump_options) {
  return dump_options.dump_flags != NodeDumpOptions::DumpFlags::None;
}
 
static void PrintIf(bool boolean_expression, const std::string& message) {
  if (boolean_expression) {
    std::cout << message;
  }
}
 
void DumpNodeInputs(
    const NodeDumpOptions& dump_options,
    const NodeDumpContext& dump_context,
    const OpKernelContext& context,
    const Node& node,
    const SessionState& session_state,
    NodeDumpAnalysis& dump_analysis) {
  const bool is_any_output_dumped = IsAnyOutputDumped(dump_options);
  if (!is_any_output_dumped) {
    return;
  }
 
  if (!FilterNode(dump_options, node)) return;
 
  if (context.GetComputeStream())
    context.GetComputeStream()->Flush();
 
  bool should_dump_node_placement = (dump_options.dump_flags & NodeDumpOptions::DumpFlags::NodePlacement) != 0;
  if (dump_context.iteration == 1 && should_dump_node_placement) {
    PrintIf(should_dump_node_placement, MakeString(" Placement: ", node.GetExecutionProviderType(), "\n"));
#ifdef DEBUG_NODE_INPUTS_OUTPUTS_ENABLE_DUMP_TO_SQLDB
    InsertNodePlacementToSqliteDb(dump_context, node);
#endif
  }
 
  std::cout << "-----------\n";
  std::cout << node.OpType() << " node: " << node.Name() << "\n";
 
  const auto& input_defs = node.InputDefs();
  TensorMetadata tensor_metadata;
 
  bool check_half_overflow = (dump_options.data_destination == NodeDumpOptions::DataDestination::StdOut) &&
                             (dump_options.dump_flags & NodeDumpOptions::DumpFlags::HalfConversionOverflow) != 0;
  bool potential_half_overflow = false;
  for (auto i = 0, end = context.InputCount(); i < end; ++i) {
    if (input_defs[i]->Exists()) {
      std::cout << "Input " << i << " Name: " << input_defs[i]->Name() << "\n";
 
      const auto* type = context.InputType(i);
 
      if (type) {
        if (type->IsTensorType()) {
          if (const auto* tensor = context.Input<Tensor>(i); tensor != nullptr) {
            const auto& shape = tensor->Shape();
 
            const bool is_shape_set = (dump_options.dump_flags & NodeDumpOptions::DumpFlags::Shape) != 0;
            PrintIf(is_shape_set, MakeString(" Shape: ", shape, "\n"));
 
            if ((dump_options.dump_flags & NodeDumpOptions::DumpFlags::InputData) != 0 || check_half_overflow) {
              tensor_metadata.name = input_defs[i]->Name();
              tensor_metadata.step = dump_context.iteration;
              tensor_metadata.consumer = node.Name() + ":" + std::to_string(i);
              tensor_metadata.ep_type = GetCleanedEpType(node);
 
              TensorStatisticsData tensor_statistics;
              DumpTensor(dump_options, *tensor, tensor_metadata, tensor_statistics, session_state);
 
              if (check_half_overflow && tensor_statistics.is_float) {
                float threshold = dump_options.half_overflow_threshold;
                if (tensor_statistics.float_min < -threshold || tensor_statistics.float_max > threshold) {
                  potential_half_overflow = true;
                }
              }
            }
          } else {
            std::cout << " is empty optional tensor.\n";
          }
        } else {
          std::cout << " is non-tensor type.\n";
        }
      } else {
        // this could happen with an empty Optional input or the tensor is removed after pre-packing.
        std::cout << " was missing data type (maybe pre-packed).\n";
      }
    } else {
      std::cout << "Input " << i << " is optional and was not provided.\n";
    }
  }
 
  if (check_half_overflow) {
    dump_analysis.Add(node.Name(), node.OpType(), potential_half_overflow);
  }
}
 
void DumpNodeInputs(
    const NodeDumpContext& dump_context,
    const OpKernelContext& context,
    const Node& node,
    const SessionState& session_state,
    NodeDumpAnalysis& dump_analysis) {
  DumpNodeInputs(NodeDumpOptionsFromEnvironmentVariables(), dump_context, context, node, session_state, dump_analysis);
}
 
void DumpNodeOutputs(
    const NodeDumpOptions& dump_options,
    const NodeDumpContext& dump_context,
    OpKernelContext& context,
    const Node& node,
    const SessionState& session_state,
    NodeDumpAnalysis& dump_analysis) {
  const bool is_any_output_dumped = IsAnyOutputDumped(dump_options);
  if (!is_any_output_dumped) {
    return;
  }
 
  if (!FilterNode(dump_options, node)) return;
 
  if (context.GetComputeStream())
    context.GetComputeStream()->Flush();
 
  bool should_dump_node_placement = (dump_options.dump_flags & NodeDumpOptions::DumpFlags::NodePlacement) != 0;
  if (dump_context.iteration == 1 && should_dump_node_placement) {
    PrintIf(should_dump_node_placement, MakeString(" Placement: ", node.GetExecutionProviderType(), "\n"));
#ifdef DEBUG_NODE_INPUTS_OUTPUTS_ENABLE_DUMP_TO_SQLDB
    InsertNodePlacementToSqliteDb(dump_context, node);
#endif
  }
 
  std::cout << "-----------\n";
  const auto& output_defs = node.OutputDefs();
  TensorMetadata tensor_metadata;
 
  bool check_half_overflow = (dump_options.data_destination == NodeDumpOptions::DataDestination::StdOut) &&
                             (dump_options.dump_flags & NodeDumpOptions::DumpFlags::HalfConversionOverflow) != 0;
  bool potential_half_overflow = false;
  for (auto i = 0, end = context.OutputCount(); i < end; ++i) {
    if (output_defs[i]->Exists()) {
      std::cout << "Output " << i << " Name: " << output_defs[i]->Name() << "\n";
 
      const auto* type = context.OutputType(i);
      if (type) {
        if (type->IsTensorType()) {
          if (const auto* tensor = context.Output<Tensor>(i); tensor != nullptr) {
            const auto& shape = tensor->Shape();
 
            const bool is_shape_set = (dump_options.dump_flags & NodeDumpOptions::DumpFlags::Shape) != 0;
            PrintIf(is_shape_set, MakeString(" Shape: ", shape, "\n"));
 
            // For MemcpyToHost, the memory copy has not been syncronized so the data is not ready to read yet.
            // Here we skip it since it is just a copy of input tensor (or output of previous node) which has been dumped.
            if (node.OpType() == "MemcpyToHost") {
              std::cout << " is same as input.\n";
              continue;
            }
 
            if ((dump_options.dump_flags & NodeDumpOptions::DumpFlags::OutputData) != 0 || check_half_overflow) {
              tensor_metadata.name = output_defs[i]->Name();
              tensor_metadata.step = dump_context.iteration;
              tensor_metadata.producer = node.Name() + ":" + std::to_string(i);
              tensor_metadata.ep_type = GetCleanedEpType(node);
 
              TensorStatisticsData tensor_statistics;
              DumpTensor(dump_options, *tensor, tensor_metadata, tensor_statistics, session_state);
 
              if (check_half_overflow && tensor_statistics.is_float) {
                float threshold = dump_options.half_overflow_threshold;
                if (tensor_statistics.float_min < -threshold || tensor_statistics.float_max > threshold) {
                  potential_half_overflow = true;
                }
              }
            }
          } else {
            std::cout << " is empty optional tensor.\n";
          }
        } else {
          std::cout << " is non-tensor type.\n";
        }
      } else {
        // should never happen in a successful run
        std::cout << "missing data type\n";
      }
    } else {
      std::cout << "Output " << i << " is optional and was not produced.\n";
    }
 
    if (check_half_overflow) {
      dump_analysis.Add(node.Name(), node.OpType(), potential_half_overflow);
    }
 
    std::cout << std::endl;
  }
}
 
void DumpNodeOutputs(
    const NodeDumpContext& dump_context,
    OpKernelContext& context,
    const Node& node,
    const SessionState& session_state,
    NodeDumpAnalysis& dump_analysis) {
  DumpNodeOutputs(NodeDumpOptionsFromEnvironmentVariables(), dump_context, context, node, session_state, dump_analysis);
}
 
}  // namespace utils
}  // namespace onnxruntime
 
#endif