-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhugot_ort.go
183 lines (167 loc) · 4.53 KB
/
hugot_ort.go
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
//go:build !NOORT || ALL
package hugot
import (
"context"
"errors"
"fmt"
ort "github.com/yalue/onnxruntime_go"
"github.com/knights-analytics/hugot/options"
"github.com/knights-analytics/hugot/util"
)
func NewORTSession(opts ...options.WithOption) (*Session, error) {
if ort.IsInitialized() {
return nil, errors.New("another session is currently active, and only one session can be active at one time")
}
session, err := newSession("ORT", opts...)
if err != nil {
return nil, err
}
// set session options and initialise
if initialised, ortErr := session.initialiseORT(); ortErr != nil {
if initialised {
destroyErr := session.Destroy()
envErr := ort.DestroyEnvironment()
return nil, errors.Join(ortErr, destroyErr, envErr)
}
return nil, ortErr
}
session.environmentDestroy = func() error {
return ort.DestroyEnvironment()
}
return session, err
}
func (s *Session) initialiseORT() (bool, error) {
o := s.options.ORTOptions
// Set pre-initialisation options
if o.LibraryPath != nil {
ortPathExists, err := util.FileSystem.Exists(context.Background(), *o.LibraryPath)
if err != nil {
return false, err
}
if !ortPathExists {
return false, fmt.Errorf("cannot find the ort library at: %s", *o.LibraryPath)
}
ort.SetSharedLibraryPath(*o.LibraryPath)
}
// Start OnnxRuntime
if err := ort.InitializeEnvironment(); err != nil {
return false, err
}
if o.Telemetry != nil {
if err := ort.EnableTelemetry(); err != nil {
return true, err
}
} else {
if err := ort.DisableTelemetry(); err != nil {
return true, err
}
}
// Create session options for use in all pipelines
sessionOptions, optionsError := ort.NewSessionOptions()
if optionsError != nil {
return true, optionsError
}
s.options.RuntimeOptions = sessionOptions
s.options.Destroy = func() error {
return sessionOptions.Destroy()
}
if o.IntraOpNumThreads != nil {
if err := sessionOptions.SetIntraOpNumThreads(*o.IntraOpNumThreads); err != nil {
return true, err
}
}
if o.InterOpNumThreads != nil {
if err := sessionOptions.SetInterOpNumThreads(*o.InterOpNumThreads); err != nil {
return true, err
}
}
if o.CPUMemArena != nil {
if err := sessionOptions.SetCpuMemArena(*o.CPUMemArena); err != nil {
return true, err
}
}
if o.MemPattern != nil {
if err := sessionOptions.SetMemPattern(*o.MemPattern); err != nil {
return true, err
}
}
if o.ParallelExecutionMode != nil {
if *o.ParallelExecutionMode {
if err := sessionOptions.SetExecutionMode(ort.ExecutionModeParallel); err != nil {
return true, err
}
} else {
if err := sessionOptions.SetExecutionMode(ort.ExecutionModeSequential); err != nil {
return true, err
}
}
}
if o.IntraOpSpinning != nil {
if *o.IntraOpSpinning {
if err := sessionOptions.AddSessionConfigEntry("session.intra_op.allow_spinning", "1"); err != nil {
return true, err
}
} else {
if err := sessionOptions.AddSessionConfigEntry("session.intra_op.allow_spinning", "0"); err != nil {
return true, err
}
}
}
if o.InterOpSpinning != nil {
if *o.InterOpSpinning {
if err := sessionOptions.AddSessionConfigEntry("session.inter_op.allow_spinning", "1"); err != nil {
return true, err
}
} else {
if err := sessionOptions.AddSessionConfigEntry("session.inter_op.allow_spinning", "0"); err != nil {
return true, err
}
}
}
if o.CudaOptions != nil {
cudaOptions, optErr := ort.NewCUDAProviderOptions()
if optErr != nil {
return true, optErr
}
if len(o.CudaOptions) > 0 {
optErr = cudaOptions.Update(o.CudaOptions)
if optErr != nil {
return true, optErr
}
}
if err := sessionOptions.AppendExecutionProviderCUDA(cudaOptions); err != nil {
return true, err
}
}
if o.CoreMLOptions != nil {
if err := sessionOptions.AppendExecutionProviderCoreML(*o.CoreMLOptions); err != nil {
return true, err
}
}
if o.DirectMLOptions != nil {
if err := sessionOptions.AppendExecutionProviderDirectML(*o.DirectMLOptions); err != nil {
return true, err
}
}
if o.OpenVINOOptions != nil {
if err := sessionOptions.AppendExecutionProviderOpenVINO(o.OpenVINOOptions); err != nil {
return true, err
}
}
if o.TensorRTOptions != nil {
tensorRTOptions, optErr := ort.NewTensorRTProviderOptions()
if optErr != nil {
return true, optErr
}
if len(o.TensorRTOptions) > 0 {
optErr = tensorRTOptions.Update(o.TensorRTOptions)
if optErr != nil {
return true, optErr
}
}
if err := sessionOptions.AppendExecutionProviderTensorRT(tensorRTOptions); err != nil {
return true, err
}
}
return true, nil
}