onnxruntime

Форк
0
/
backend-webgl.ts 
97 строк · 2.8 Кб
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
3

4
import { env } from 'onnxruntime-common';
5

6
import { Backend, SessionHandler } from '../backend';
7
import { Logger } from '../instrument';
8
import { Session } from '../session';
9

10
import { WebGLSessionHandler } from './webgl/session-handler';
11
import { WebGLContext } from './webgl/webgl-context';
12
import { createWebGLContext } from './webgl/webgl-context-factory';
13

14
/**
15
 * WebGLBackend is the entry point for all WebGL opeartions
16
 * When it starts it created the WebGLRenderingContext
17
 * and other main framework components such as Program and Texture Managers
18
 */
19
export class WebGLBackend implements Backend {
20
  glContext: WebGLContext;
21

22
  get contextId(): 'webgl' | 'webgl2' | undefined {
23
    return env.webgl.contextId;
24
  }
25
  set contextId(value: 'webgl' | 'webgl2' | undefined) {
26
    env.webgl.contextId = value;
27
  }
28

29
  get matmulMaxBatchSize(): number | undefined {
30
    return env.webgl.matmulMaxBatchSize;
31
  }
32
  set matmulMaxBatchSize(value: number | undefined) {
33
    env.webgl.matmulMaxBatchSize = value;
34
  }
35

36
  get textureCacheMode(): 'initializerOnly' | 'full' | undefined {
37
    return env.webgl.textureCacheMode;
38
  }
39
  set textureCacheMode(value: 'initializerOnly' | 'full' | undefined) {
40
    env.webgl.textureCacheMode = value;
41
  }
42

43
  get pack(): boolean | undefined {
44
    return env.webgl.pack;
45
  }
46
  set pack(value: boolean | undefined) {
47
    env.webgl.pack = value;
48
  }
49

50
  get async(): boolean | undefined {
51
    return env.webgl.async;
52
  }
53
  set async(value: boolean | undefined) {
54
    env.webgl.async = value;
55
  }
56

57
  initialize(): boolean {
58
    try {
59
      this.glContext = createWebGLContext(this.contextId);
60
      if (typeof this.matmulMaxBatchSize !== 'number') {
61
        this.matmulMaxBatchSize = 16;
62
      }
63
      if (typeof this.textureCacheMode !== 'string') {
64
        this.textureCacheMode = 'full';
65
      }
66
      if (typeof this.pack !== 'boolean') {
67
        this.pack = false;
68
      }
69
      if (typeof this.async !== 'boolean') {
70
        this.async = false;
71
      }
72

73
      Logger.setWithEnv(env);
74

75
      if (!env.webgl.context) {
76
        Object.defineProperty(env.webgl, 'context', { value: this.glContext.gl });
77
      }
78

79
      Logger.verbose(
80
        'WebGLBackend',
81
        `Created WebGLContext: ${typeof this.glContext} with matmulMaxBatchSize: ${
82
          this.matmulMaxBatchSize
83
        }; textureCacheMode: ${this.textureCacheMode}; pack: ${this.pack}; async: ${this.async}.`,
84
      );
85
      return true;
86
    } catch (e) {
87
      Logger.warning('WebGLBackend', `Unable to initialize WebGLBackend. ${e}`);
88
      return false;
89
    }
90
  }
91
  createSessionHandler(context: Session.Context): SessionHandler {
92
    return new WebGLSessionHandler(this, context);
93
  }
94
  dispose(): void {
95
    this.glContext.dispose();
96
  }
97
}
98

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.