Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/framework/external_data_loader.h"
#if defined(__wasm__)
#include <emscripten.h>
#endif
 
namespace onnxruntime {
 
#if defined(__wasm__)
 
// Error codes returned by the JS loader bodies below. The async and sync
// loaders share this contract - keep them in sync:
//   0 - success
//   1 - Module.MountedFiles is not available
//   2 - file not found in preloaded files
//   3 - out-of-bounds
//   4 - unknown error during memory copy / GPU upload
//   5 - Blob stream returned an unexpected number of bytes
 
#if defined(ORT_WASM_JSPI)
 
// Asynchronous external data loader, available in JSPI builds only.
//
// The offset/length parameters are doubles so the loader itself can address beyond
// 4GB, but the current wasm caller still rejects offset + length >= 4GB before reaching here.
 
// clang-format off
EM_ASYNC_JS(int, OrtLoadWebAssemblyExternalDataAsync,
            (const char* data_file_path, double data_offset, double data_length,
             void* tensor_data, int32_t load_type), {
 
  if (typeof Module == 'undefined' || !Module.MountedFiles) {
    return 1;  // "Module.MountedFiles" is not available.
  }
  let fileName = UTF8ToString(Number(data_file_path >>> 0));
  if (fileName.startsWith('./')) {
    fileName = fileName.substring(2);
  }
  const fileData = Module.MountedFiles.get(fileName);
  if (!fileData) {
    return 2;  // file not found in preloaded files.
  }
  const offset = data_offset;
  const length = data_length;
  const dataIdOrBuffer = Number(tensor_data >>> 0);
 
  if (offset < 0 || length < 0) {
    return 3;
  }
 
  let ownScratchLock = false;
  try {
    let data;
    if (typeof Blob !== 'undefined' && fileData instanceof Blob) {
      if (offset + length > fileData.size) {
        return 3;  // Out of bounds.
      }
      if (length === 0) {
        data = new Uint8Array(0);
      } else {
        // Stream into a reused scratch buffer rather than slice().arrayBuffer(), which
        // creates a large short-lived buffer per initializer and inflates peak memory.
        let scratch;
        if (!Module.ortExtDataScratchBusy) {
          Module.ortExtDataScratchBusy = true;
          ownScratchLock = true;
          scratch = Module.ortExtDataScratch;
          if (!scratch || scratch.length < length) {
            scratch = Module.ortExtDataScratch = new Uint8Array(length);
          }
        } else {
          scratch = new Uint8Array(length);
        }
        const stream = fileData.slice(offset, offset + length).stream();
        // BYOB reader avoids per-chunk allocations; fall back if unavailable.
        let reader;
        let byob = false;
        try {
          reader = stream.getReader({ 'mode': 'byob' });
          byob = true;
        } catch (e) {
          reader = stream.getReader();
        }
        let pos = 0;
        if (byob) {
          let chunk = Module.ortExtDataChunk || new ArrayBuffer(1048576);
          Module.ortExtDataChunk = null;  // BYOB read detaches; one owner per load.
          for (;;) {
            const r = await reader.read(new Uint8Array(chunk, 0, chunk.byteLength));
            if (r.value) {
              if (r.value.byteLength > 0) {
                if (pos + r.value.byteLength > length) {
                  Module.ortExtDataChunk = r.value.buffer;
                  return 5;  // Stream yielded more bytes than requested.
                }
                scratch.set(r.value, pos);
                pos += r.value.byteLength;
              }
              chunk = r.value.buffer;  // reclaim the (detached) chunk buffer
            }
            if (r.done) break;
          }
          Module.ortExtDataChunk = chunk;
        } else {
          for (;;) {
            const r = await reader.read();
            if (r.done) break;
            if (pos + r.value.byteLength > length) {
              return 5;  // Stream yielded more bytes than requested.
            }
            scratch.set(r.value, pos);
            pos += r.value.byteLength;
          }
        }
        if (pos !== length) {
          return 5;  // Reading from the Blob returned an unexpected number of bytes.
        }
        data = scratch.subarray(0, length);
      }
    } else {
      if (offset + length > fileData.byteLength) {
        return 3;  // Out of bounds.
      }
      data = fileData.subarray(offset, offset + length);
    }
 
    switch (load_type) {
      case 0:
        // Load external data to CPU memory.
        // Copy the file data (fileData,offset,length) into WebAssembly memory
        // (HEAPU8,buffer,length).
        HEAPU8.set(data, dataIdOrBuffer);
        break;
      case 1:
        // Load external data to GPU.
        // TODO: use a unified interface for upload external buffer.
        if (Module.webgpuUploadExternalBuffer) {
          Module.webgpuUploadExternalBuffer(dataIdOrBuffer, data);
        } else {
          Module.jsepUploadExternalBuffer(dataIdOrBuffer, data);
        }
        break;
      default:
        return 4;  // Unknown error occurred in memory copy.
    }
    return 0;
  } catch (e) {
    console.error('Failed to load external data "' + fileName + '":', e);
    return 4;
  } finally {
    // `data` may alias the shared scratch; release it only after the copy/upload
    // above has consumed it.
    if (ownScratchLock) {
      Module.ortExtDataScratchBusy = false;
    }
  }
});
// clang-format on
 
#endif
 
common::Status LoadWebAssemblyExternalData(const Env& env,
                                           const std::filesystem::path& data_file_path,
                                           FileOffsetType data_offset,
                                           SafeInt<size_t> data_length,
                                           ExternalDataLoadType load_type,
                                           void* tensor_data) {
#if defined(ORT_WASM_JSPI)
  auto err_code = OrtLoadWebAssemblyExternalDataAsync(data_file_path.c_str(),
                                                      static_cast<double>(data_offset),
                                                      static_cast<double>(static_cast<size_t>(data_length)),
                                                      tensor_data,
                                                      static_cast<int32_t>(load_type));
#else
  auto err_code = EM_ASM_INT(({
                               // If available, "Module.MountedFiles" is a Map for all preloaded files.
                               if (typeof Module == 'undefined' || !Module.MountedFiles) {
                                 return 1;  // "Module.MountedFiles" is not available.
                               }
                               let fileName = UTF8ToString(Number($0 >>> 0));
                               if (fileName.startsWith('./')) {
                                 fileName = fileName.substring(2);
                               }
                               const fileData = Module.MountedFiles.get(fileName);
                               if (!fileData) {
                                 return 2;  // File not found in preloaded files.
                               }
                               const offset = Number($1 >>> 0);
                               const length = Number($2 >>> 0);
                               const dataIdOrBuffer = Number($3 >>> 0);
                               const loadType = $4;
 
                               if (offset + length > fileData.byteLength) {
                                 return 3;  // Out of bounds.
                               }
 
                               try {
                                 const data = fileData.subarray(offset, offset + length);
                                 switch (loadType) {
                                   case 0:
                                     // Load external data to CPU memory.
                                     // Copy the file data (fileData,offset,length) into WebAssembly memory
                                     // (HEAPU8,buffer,length).
                                     HEAPU8.set(data, dataIdOrBuffer);
                                     break;
                                   case 1:
                                     // Load external data to GPU.
                                     // TODO: use a unified interface for upload external buffer.
                                     if (Module.webgpuUploadExternalBuffer) {
                                       Module.webgpuUploadExternalBuffer(dataIdOrBuffer, data);
                                     } else {
                                       Module.jsepUploadExternalBuffer(dataIdOrBuffer, data);
                                     }
                                     break;
                                   default:
                                     return 4;  // Unknown error occurred in memory copy.
                                 }
                                 return 0;
                               } catch {
                                 return 4;
                               }
                             }),
                             data_file_path.c_str(),
                             static_cast<int32_t>(data_offset),
                             static_cast<int32_t>(data_length),
                             tensor_data,
                             static_cast<int32_t>(load_type));
#endif
  const char* err_msg;
  switch (err_code) {
    case 0:
      return Status::OK();
    case 1:
      err_msg = "Module.MountedFiles is not available.";
      break;
    case 2:
      err_msg = "File not found in preloaded files.";
      break;
    case 3:
      err_msg = "Out of bounds.";
      break;
#if defined(ORT_WASM_JSPI)
    case 5:
      err_msg = "Reading from the Blob returned an unexpected number of bytes.";
      break;
#endif
    default:
      err_msg = "Unknown error occurred in memory copy.";
  }
  return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to load external data file \"", data_file_path,
                         "\", error: ", err_msg);
}
 
#endif
 
}  // namespace onnxruntime