Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <filesystem>
#include <variant>
#include "core/framework/allocator.h"
#include "core/framework/config_options.h"
#include "core/session/onnxruntime_c_api.h"
 
namespace onnxruntime {
namespace epctx {
/// <summary>
/// Holds the buffer that will store the output model and the allocator used to allocate the memory.
/// </summary>
struct BufferHolder {
  void** buffer_ptr = nullptr;
  size_t* buffer_size_ptr = nullptr;
  AllocatorPtr buffer_allocator = nullptr;
};
 
/// <summary>
/// Holds the opaque stream state and the write function that ORT calls to write out the output model.
/// </summary>
struct BufferWriteFuncHolder {
  OrtWriteBufferFunc write_func = nullptr;
  void* stream_state = nullptr;  // Opaque pointer to user's stream state. Passed as first argument to write_func.
};
 
/// <summary>
/// Holds the opaque state and write function that EPs use to write EPContext binary data.
/// </summary>
struct EpContextDataWriteFuncHolder {
  OrtWriteNamedBufferFunc write_func = nullptr;
  void* state = nullptr;
};
 
/// <summary>
/// Non-owning copy of the application callbacks and states used for external EPContext binary data.
/// </summary>
struct EpContextDataCallbacks {
  OrtReadNamedBufferFunc read_func = nullptr;
  void* read_state = nullptr;
  OrtWriteNamedBufferFunc write_func = nullptr;
  void* write_state = nullptr;
};
 
/// <summary>
/// Holds path and size threshold used to write out initializers to an external file.
/// </summary>
struct ExternalInitializerFileInfo {
  std::filesystem::path file_path;
  size_t size_threshold = 0;
};
 
/// <summary>
/// Holds the logical file name and caller-owned output parameters for external initializers saved to a buffer.
/// </summary>
struct ExternalInitializerBufferInfo {
  std::filesystem::path logical_file_name;
  size_t size_threshold = 0;
  AllocatorPtr buffer_allocator = nullptr;
  void** buffer_ptr = nullptr;
  size_t* buffer_size_ptr = nullptr;
};
 
/// <summary>
/// Holds function and state provided by user to handle initializer data (i.e., write to stream or embed in model).
/// </summary>
struct InitializerHandler {
  OrtGetInitializerLocationFunc handle_initializer_func = nullptr;
  void* state = nullptr;
};
 
/// <summary>
/// Stores EPContext model generation options. Used in SessionOptions.
/// </summary>
struct ModelGenOptions {
  // Action to take if the output model does not have compiled (EPContext) nodes.
  enum class ActionIfNoCompiledNodes {
    // Return OK() but don't generate an output model. Compiling via SessionOptions defaults to this behavior
    // to maintain compatibility. The explicit compile API does *not* use this action.
    kDontGenerateModel = 0,
 
    // Generate an output model even if it doesn't have compiled nodes.
    // The explicit Compile API defaults to this value.
    kGenerateModel,
 
    // Return an error if the model does not have compiled nodes.
    // The explicit Compile API can be configured to this value.
    kReturnError,
  };
 
  ModelGenOptions();
 
  // Initializes from string key/value pairs in session config options.
  explicit ModelGenOptions(const ConfigOptions& config_options);
 
  bool enable = false;
  bool error_if_output_file_exists = true;
  bool error_if_no_compiled_nodes = false;
  bool embed_ep_context_in_model = false;
  ActionIfNoCompiledNodes action_if_no_compiled_nodes = ActionIfNoCompiledNodes::kDontGenerateModel;
 
  std::variant<std::monostate,         // Initial state (no output model location)
               std::filesystem::path,  // output model path
               BufferHolder,           // buffer to save output model
               BufferWriteFuncHolder>  // Function to write the output model to a user's stream.
      output_model_location = std::monostate{};
 
  std::variant<std::monostate,                 // Initial state (initializers embedded in ONNX model).
               ExternalInitializerFileInfo,    // Initializers saved to a single external file depending on size.
               ExternalInitializerBufferInfo,  // Initializers saved to a caller-owned buffer.
               InitializerHandler>             // Custom function called for every initializer to determine location.
      initializers_location = std::monostate{};
 
  size_t external_initializers_alignment = 4096;
  size_t external_initializers_alignment_threshold = 1048576;
 
  EpContextDataWriteFuncHolder ep_context_data_write_func = {};
 
  bool HasOutputModelLocation() const;
  const std::filesystem::path* TryGetOutputModelPath() const;
  const BufferHolder* TryGetOutputModelBuffer() const;
  const BufferWriteFuncHolder* TryGetOutputModelWriteFunc() const;
  const EpContextDataWriteFuncHolder* TryGetEpContextDataWriteFunc() const;
 
  bool AreInitializersEmbeddedInOutputModel() const;
  const ExternalInitializerFileInfo* TryGetExternalInitializerFileInfo() const;
  const ExternalInitializerBufferInfo* TryGetExternalInitializerBufferInfo() const;
  const InitializerHandler* TryGetInitializerHandler() const;
};
 
}  // namespace epctx
}  // namespace onnxruntime