Исходный код СУБД 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
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
package onnxruntime
 
/*
#include "cshim.h"
#include <stdlib.h>
*/
import "C"
import (
    "fmt"
    "math"
    "sort"
    "sync"
    "unsafe"
)
 
// SessionOptions configures how a Session is created.
type SessionOptions struct {
    handle *C.OrtSessionOptions
    mu     sync.Mutex
}
 
// NewSessionOptions creates a new SessionOptions with default values.
func NewSessionOptions() (*SessionOptions, error) {
    if err := checkInit(); err != nil {
        return nil, err
    }
    var opts *C.OrtSessionOptions
    if err := checkStatus(C.ort_CreateSessionOptions(&opts)); err != nil {
        return nil, wrapErr("create session options", err)
    }
    return &SessionOptions{handle: opts}, nil
}
 
// SetIntraOpNumThreads sets the number of threads used within individual ops.
// 0 means use the default.
func (o *SessionOptions) SetIntraOpNumThreads(n int) error {
    if n < 0 {
        return fmt.Errorf("ort: set intra op threads: thread count must be non-negative, got %d", n)
    }
    if n > math.MaxInt32 {
        return fmt.Errorf("ort: set intra op threads: %d does not fit in C int", n)
    }
    if err := o.lockUsable("set intra op threads"); err != nil {
        return err
    }
    defer o.unlock()
    return wrapErr("set intra op threads", checkStatus(C.ort_SetIntraOpNumThreads(o.handle, C.int(n))))
}
 
// SetInterOpNumThreads sets the number of threads used to run independent ops in parallel.
// 0 means use the default.
func (o *SessionOptions) SetInterOpNumThreads(n int) error {
    if n < 0 {
        return fmt.Errorf("ort: set inter op threads: thread count must be non-negative, got %d", n)
    }
    if n > math.MaxInt32 {
        return fmt.Errorf("ort: set inter op threads: %d does not fit in C int", n)
    }
    if err := o.lockUsable("set inter op threads"); err != nil {
        return err
    }
    defer o.unlock()
    return wrapErr("set inter op threads", checkStatus(C.ort_SetInterOpNumThreads(o.handle, C.int(n))))
}
 
// SetGraphOptimizationLevel sets the level of graph optimizations applied.
func (o *SessionOptions) SetGraphOptimizationLevel(level GraphOptimizationLevel) error {
    if level < GraphOptimizationLevel(math.MinInt32) || level > GraphOptimizationLevel(math.MaxInt32) {
        return fmt.Errorf("ort: set graph optimization level: %d does not fit in C enum", level)
    }
    if err := o.lockUsable("set graph optimization level"); err != nil {
        return err
    }
    defer o.unlock()
    return wrapErr("set graph optimization level",
        checkStatus(C.ort_SetSessionGraphOptimizationLevel(o.handle, C.GraphOptimizationLevel(level))))
}
 
// AddConfigEntry sets an arbitrary session configuration key-value pair.
func (o *SessionOptions) AddConfigEntry(key, value string) error {
    if err := rejectNUL(key, "session config key"); err != nil {
        return err
    }
    if err := rejectNUL(value, "session config value"); err != nil {
        return err
    }
    if err := o.lockUsable("add config entry"); err != nil {
        return err
    }
    defer o.unlock()
    cKey := C.CString(key)
    defer C.free(unsafe.Pointer(cKey))
    cVal := C.CString(value)
    defer C.free(unsafe.Pointer(cVal))
    return wrapErr("add config entry", checkStatus(C.ort_AddSessionConfigEntry(o.handle, cKey, cVal)))
}
 
// AppendExecutionProvider adds an execution provider. CUDA and TensorRT are
// routed through their dedicated V2 APIs; all others use the generic string
// key-value API.
func (o *SessionOptions) AppendExecutionProvider(name string, options map[string]string) error {
    if err := rejectNUL(name, "execution provider name"); err != nil {
        return err
    }
    if err := validateStringMap(options, "execution provider option"); err != nil {
        return err
    }
    if err := o.lockUsable("append execution provider"); err != nil {
        return err
    }
    defer o.unlock()
    switch name {
    case "CUDA", "CUDAExecutionProvider":
        return o.appendCUDA(options)
    case "TensorRT", "TensorrtExecutionProvider":
        return o.appendTensorRT(options)
    default:
        return o.appendGenericProvider(name, options)
    }
}
 
// Clone creates a copy of the session options.
func (o *SessionOptions) Clone() (*SessionOptions, error) {
    if err := o.lockUsable("clone session options"); err != nil {
        return nil, err
    }
    defer o.unlock()
    var out *C.OrtSessionOptions
    if err := checkStatus(C.ort_CloneSessionOptions(o.handle, &out)); err != nil {
        return nil, wrapErr("clone session options", err)
    }
    return &SessionOptions{handle: out}, nil
}
 
// DisableMemPattern disables memory pattern optimization.
func (o *SessionOptions) DisableMemPattern() error {
    if err := o.lockUsable("disable mem pattern"); err != nil {
        return err
    }
    defer o.unlock()
    return wrapErr("disable mem pattern", checkStatus(C.ort_DisableMemPattern(o.handle)))
}
 
// EnableMemPattern enables memory pattern optimization (default).
func (o *SessionOptions) EnableMemPattern() error {
    if err := o.lockUsable("enable mem pattern"); err != nil {
        return err
    }
    defer o.unlock()
    return wrapErr("enable mem pattern", checkStatus(C.ort_EnableMemPattern(o.handle)))
}
 
// EnableCpuMemArena enables the CPU memory arena.
func (o *SessionOptions) EnableCpuMemArena() error {
    if err := o.lockUsable("enable cpu mem arena"); err != nil {
        return err
    }
    defer o.unlock()
    return wrapErr("enable cpu mem arena", checkStatus(C.ort_EnableCpuMemArena(o.handle)))
}
 
// DisableCpuMemArena disables the CPU memory arena.
func (o *SessionOptions) DisableCpuMemArena() error {
    if err := o.lockUsable("disable cpu mem arena"); err != nil {
        return err
    }
    defer o.unlock()
    return wrapErr("disable cpu mem arena", checkStatus(C.ort_DisableCpuMemArena(o.handle)))
}
 
// EnableProfiling enables profiling with the given file prefix.
func (o *SessionOptions) EnableProfiling(profileFilePrefix string) error {
    if err := o.lockUsable("enable profiling"); err != nil {
        return err
    }
    defer o.unlock()
    cPrefix, freePrefix, err := ortPath(profileFilePrefix)
    if err != nil {
        return err
    }
    defer freePrefix()
    return wrapErr("enable profiling", checkStatus(C.ort_EnableProfiling(o.handle, cPrefix)))
}
 
// DisableProfiling disables profiling.
func (o *SessionOptions) DisableProfiling() error {
    if err := o.lockUsable("disable profiling"); err != nil {
        return err
    }
    defer o.unlock()
    return wrapErr("disable profiling", checkStatus(C.ort_DisableProfiling(o.handle)))
}
 
// AddFreeDimensionOverride fixes a dynamic dimension by its denotation string.
func (o *SessionOptions) AddFreeDimensionOverride(dimDenotation string, dimValue int64) error {
    if err := rejectNUL(dimDenotation, "dimension denotation"); err != nil {
        return err
    }
    if err := o.lockUsable("add free dimension override"); err != nil {
        return err
    }
    defer o.unlock()
    cDim := C.CString(dimDenotation)
    defer C.free(unsafe.Pointer(cDim))
    return wrapErr("add free dimension override",
        checkStatus(C.ort_AddFreeDimensionOverride(o.handle, cDim, C.int64_t(dimValue))))
}
 
// AddFreeDimensionOverrideByName fixes a dynamic dimension by its name.
func (o *SessionOptions) AddFreeDimensionOverrideByName(dimName string, dimValue int64) error {
    if err := rejectNUL(dimName, "dimension name"); err != nil {
        return err
    }
    if err := o.lockUsable("add free dimension override by name"); err != nil {
        return err
    }
    defer o.unlock()
    cDim := C.CString(dimName)
    defer C.free(unsafe.Pointer(cDim))
    return wrapErr("add free dimension override by name",
        checkStatus(C.ort_AddFreeDimensionOverrideByName(o.handle, cDim, C.int64_t(dimValue))))
}
 
// SetExecutionMode sets sequential or parallel execution mode.
func (o *SessionOptions) SetExecutionMode(mode ExecutionMode) error {
    if mode < ExecutionMode(math.MinInt32) || mode > ExecutionMode(math.MaxInt32) {
        return fmt.Errorf("ort: set execution mode: %d does not fit in C enum", mode)
    }
    if err := o.lockUsable("set execution mode"); err != nil {
        return err
    }
    defer o.unlock()
    return wrapErr("set execution mode",
        checkStatus(C.ort_SetSessionExecutionMode(o.handle, C.ExecutionMode(mode))))
}
 
// GetExecutionMode returns the current execution mode. Requires ORT >= 1.27.
func (o *SessionOptions) GetExecutionMode() (ExecutionMode, error) {
    if apiVersion < 27 {
        return 0, fmt.Errorf("ort: GetExecutionMode requires ORT >= 1.27 (have API version %d)", apiVersion)
    }
    if err := o.lockUsable("get execution mode"); err != nil {
        return 0, err
    }
    defer o.unlock()
    var mode C.ExecutionMode
    if err := checkStatus(C.ort_GetSessionExecutionMode(o.handle, &mode)); err != nil {
        return 0, wrapErr("get execution mode", err)
    }
    return ExecutionMode(mode), nil
}
 
// IsMemPatternEnabled reports whether memory pattern optimization is enabled. Requires ORT >= 1.27.
func (o *SessionOptions) IsMemPatternEnabled() (bool, error) {
    if apiVersion < 27 {
        return false, fmt.Errorf("ort: IsMemPatternEnabled requires ORT >= 1.27 (have API version %d)", apiVersion)
    }
    if err := o.lockUsable("get mem pattern enabled"); err != nil {
        return false, err
    }
    defer o.unlock()
    var out C.int
    if err := checkStatus(C.ort_GetMemPatternEnabled(o.handle, &out)); err != nil {
        return false, wrapErr("get mem pattern enabled", err)
    }
    return out != 0, nil
}
 
// AddInitializer overrides a model initializer with the given tensor value.
// The tensor must outlive every session created from these options.
func (o *SessionOptions) AddInitializer(name string, value *Tensor) error {
    if err := rejectNUL(name, "initializer name"); err != nil {
        return err
    }
    if err := o.lockUsable("add initializer"); err != nil {
        return err
    }
    defer o.unlock()
    if err := value.checkUsable("add initializer"); err != nil {
        return err
    }
    cName := C.CString(name)
    defer C.free(unsafe.Pointer(cName))
    return wrapErr("add initializer", checkStatus(C.ort_AddInitializer(o.handle, cName, value.value)))
}
 
// SetOptimizedModelFilePath sets a path to save the optimized model to.
func (o *SessionOptions) SetOptimizedModelFilePath(path string) error {
    if err := o.lockUsable("set optimized model path"); err != nil {
        return err
    }
    defer o.unlock()
    cPath, freePath, err := ortPath(path)
    if err != nil {
        return err
    }
    defer freePath()
    return wrapErr("set optimized model path",
        checkStatus(C.ort_SetOptimizedModelFilePath(o.handle, cPath)))
}
 
// RegisterCustomOpsLibrary loads a shared library containing custom ops.
func (o *SessionOptions) RegisterCustomOpsLibrary(libraryPath string) error {
    if err := o.lockUsable("register custom ops library"); err != nil {
        return err
    }
    defer o.unlock()
    cPath, freePath, err := ortPath(libraryPath)
    if err != nil {
        return err
    }
    defer freePath()
    return wrapErr("register custom ops library",
        checkStatus(C.ort_RegisterCustomOpsLibrary_V2(o.handle, cPath)))
}
 
// HasSessionConfigEntry reports whether the config key exists.
func (o *SessionOptions) HasSessionConfigEntry(key string) (bool, error) {
    if err := rejectNUL(key, "session config key"); err != nil {
        return false, err
    }
    if err := o.lockUsable("has session config entry"); err != nil {
        return false, err
    }
    defer o.unlock()
    cKey := C.CString(key)
    defer C.free(unsafe.Pointer(cKey))
    var out C.int
    if err := checkStatus(C.ort_HasSessionConfigEntry(o.handle, cKey, &out)); err != nil {
        return false, wrapErr("has session config entry", err)
    }
    return out != 0, nil
}
 
// GetSessionConfigEntry returns the value for the given config key.
func (o *SessionOptions) GetSessionConfigEntry(key string) (string, error) {
    if err := rejectNUL(key, "session config key"); err != nil {
        return "", err
    }
    if err := o.lockUsable("get session config entry"); err != nil {
        return "", err
    }
    defer o.unlock()
    cKey := C.CString(key)
    defer C.free(unsafe.Pointer(cKey))
    var size C.size_t
    if err := checkStatus(C.ort_GetSessionConfigEntry(o.handle, cKey, nil, &size)); err != nil {
        return "", wrapErr("get session config entry size", err)
    }
    if size == 0 {
        return "", nil
    }
    if uint64(size) > uint64(math.MaxInt) {
        return "", fmt.Errorf("ort: get session config entry: value size %d exceeds addressable range", uint64(size))
    }
    buf := make([]C.char, size)
    if err := checkStatus(C.ort_GetSessionConfigEntry(o.handle, cKey, &buf[0], &size)); err != nil {
        return "", wrapErr("get session config entry", err)
    }
    return C.GoString(&buf[0]), nil
}
 
// Close releases the session options. It is idempotent.
func (o *SessionOptions) Close() error {
    if o == nil {
        return nil
    }
    o.mu.Lock()
    defer o.mu.Unlock()
    if o.handle != nil {
        C.ort_ReleaseSessionOptions(o.handle)
        o.handle = nil
    }
    return nil
}
 
func (o *SessionOptions) lockUsable(op string) error {
    if o == nil {
        return fmt.Errorf("ort: %s: session options are nil or closed", op)
    }
    o.mu.Lock()
    if o.handle == nil {
        o.mu.Unlock()
        return fmt.Errorf("ort: %s: session options are nil or closed", op)
    }
    return nil
}
 
func (o *SessionOptions) unlock() {
    o.mu.Unlock()
}
 
func (o *SessionOptions) appendCUDA(opts map[string]string) error {
    var cudaOpts *C.OrtCUDAProviderOptionsV2
    if err := checkStatus(C.ort_CreateCUDAProviderOptions(&cudaOpts)); err != nil {
        return wrapErr("create cuda options", err)
    }
    defer C.ort_ReleaseCUDAProviderOptions(cudaOpts)
 
    if len(opts) > 0 {
        keys, values := sortedKV(opts)
        defer freeStringArrays(keys, values)
        if err := checkStatus(C.ort_UpdateCUDAProviderOptions(
            cudaOpts, &keys[0], &values[0], C.size_t(len(opts)))); err != nil {
            return wrapErr("update cuda options", err)
        }
    }
 
    return wrapErr("append cuda provider",
        checkStatus(C.ort_SessionOptionsAppendExecutionProvider_CUDA_V2(o.handle, cudaOpts)))
}
 
func (o *SessionOptions) appendTensorRT(opts map[string]string) error {
    var trtOpts *C.OrtTensorRTProviderOptionsV2
    if err := checkStatus(C.ort_CreateTensorRTProviderOptions(&trtOpts)); err != nil {
        return wrapErr("create tensorrt options", err)
    }
    defer C.ort_ReleaseTensorRTProviderOptions(trtOpts)
 
    if len(opts) > 0 {
        keys, values := sortedKV(opts)
        defer freeStringArrays(keys, values)
        if err := checkStatus(C.ort_UpdateTensorRTProviderOptions(
            trtOpts, &keys[0], &values[0], C.size_t(len(opts)))); err != nil {
            return wrapErr("update tensorrt options", err)
        }
    }
 
    return wrapErr("append tensorrt provider",
        checkStatus(C.ort_SessionOptionsAppendExecutionProvider_TensorRT_V2(o.handle, trtOpts)))
}
 
func (o *SessionOptions) appendGenericProvider(name string, opts map[string]string) error {
    cName := C.CString(name)
    defer C.free(unsafe.Pointer(cName))
 
    if len(opts) == 0 {
        return wrapErr("append provider",
            checkStatus(C.ort_SessionOptionsAppendExecutionProvider(o.handle, cName, nil, nil, 0)))
    }
 
    keys, values := sortedKV(opts)
    defer freeStringArrays(keys, values)
    return wrapErr("append provider",
        checkStatus(C.ort_SessionOptionsAppendExecutionProvider(
            o.handle, cName, &keys[0], &values[0], C.size_t(len(opts)))))
}
 
func sortedKV(m map[string]string) ([]*C.char, []*C.char) {
    sorted := make([]string, 0, len(m))
    for k := range m {
        sorted = append(sorted, k)
    }
    sort.Strings(sorted)
 
    keys := make([]*C.char, len(sorted))
    values := make([]*C.char, len(sorted))
    for i, k := range sorted {
        keys[i] = C.CString(k)
        values[i] = C.CString(m[k])
    }
    return keys, values
}
 
func freeStringArrays(keys, values []*C.char) {
    for i := range keys {
        C.free(unsafe.Pointer(keys[i]))
        C.free(unsafe.Pointer(values[i]))
    }
}
 
func validateStringMap(values map[string]string, context string) error {
    for key, value := range values {
        if err := rejectNUL(key, context+" key"); err != nil {
            return err
        }
        if err := rejectNUL(value, context+" value"); err != nil {
            return err
        }
    }
    return nil
}