| 166 | } |
| 167 | |
| 168 | export class Engine implements TensorTracker, DataMover { |
| 169 | state: EngineState; |
| 170 | backendName: string; |
| 171 | registry: {[id: string]: KernelBackend} = {}; |
| 172 | registryFactory: { |
| 173 | [id: string]: { |
| 174 | factory: () => KernelBackend | Promise<KernelBackend>, |
| 175 | priority: number |
| 176 | } |
| 177 | } = {}; |
| 178 | |
| 179 | private profiler: Profiler; |
| 180 | private backendInstance: KernelBackend; |
| 181 | private pendingBackendInit: Promise<boolean>; |
| 182 | private pendingBackendInitId = 0; |
| 183 | |
| 184 | constructor(public ENV: Environment) { |
| 185 | this.state = new EngineState(); |
| 186 | } |
| 187 | |
| 188 | async ready(): Promise<void> { |
| 189 | if (this.pendingBackendInit != null) { |
| 190 | return this.pendingBackendInit.then(() => {}); |
| 191 | } |
| 192 | if (this.backendInstance != null) { |
| 193 | return; |
| 194 | } |
| 195 | const sortedBackends = this.getSortedBackends(); |
| 196 | |
| 197 | for (let i = 0; i < sortedBackends.length; i++) { |
| 198 | const backendName = sortedBackends[i]; |
| 199 | const success = await this.initializeBackend(backendName).success; |
| 200 | if (success) { |
| 201 | await this.setBackend(backendName); |
| 202 | return; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | throw new Error( |
| 207 | `Could not initialize any backends, all backend initializations ` + |
| 208 | `failed.`); |
| 209 | } |
| 210 | |
| 211 | get backend(): KernelBackend { |
| 212 | if (this.pendingBackendInit != null) { |
| 213 | throw new Error( |
| 214 | `Backend '${this.backendName}' has not yet been initialized. Make ` + |
| 215 | `sure to await tf.ready() or await tf.setBackend() before calling ` + |
| 216 | `other methods`); |
| 217 | } |
| 218 | if (this.backendInstance == null) { |
| 219 | const {name, asyncInit} = this.initializeBackendsAndReturnBest(); |
| 220 | if (asyncInit) { |
| 221 | throw new Error( |
| 222 | `The highest priority backend '${name}' has not yet been ` + |
| 223 | `initialized. Make sure to await tf.ready() or ` + |
| 224 | `await tf.setBackend() before calling other methods`); |
| 225 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…