-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
backend.ts
162 lines (145 loc) · 5.16 KB
/
backend.ts
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
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {Backend, DataToGPUOptions, GPUData, Tensor} from '../tensor';
import {DataId} from '../tensor_info';
import {BackendValues, DataType, WebGLData, WebGPUData} from '../types';
export const EPSILON_FLOAT32 = 1e-7;
export const EPSILON_FLOAT16 = 1e-4;
// Required information for all backends.
export interface BackendTimingInfo {
kernelMs: number|{error: string};
getExtraProfileInfo?(): string; // a field for additional timing information
// e.g. packing / unpacking for WebGL backend
}
export interface TensorStorage {
read(dataId: DataId): Promise<BackendValues>;
readSync(dataId: DataId): BackendValues;
disposeData(dataId: DataId, force?: boolean): boolean;
write(values: BackendValues, shape: number[], dtype: DataType): DataId;
move(
dataId: DataId, values: BackendValues, shape: number[], dtype: DataType,
refCount: number): void;
memory(): {unreliable: boolean;}; // Backend-specific information.
/** Returns number of data ids currently in the storage. */
numDataIds(): number;
refCount(dataId: DataId): number;
}
/** Convenient class for storing tensor-related data. */
export class DataStorage<T> {
private data = new WeakMap<DataId, T>();
private dataIdsCount = 0;
constructor(private backend: KernelBackend, private dataMover: DataMover) {}
get(dataId: DataId) {
if (!this.data.has(dataId)) {
this.dataMover.moveData(this.backend, dataId);
}
return this.data.get(dataId);
}
set(dataId: DataId, value: T): void {
this.dataIdsCount++;
this.data.set(dataId, value);
}
has(dataId: DataId): boolean {
return this.data.has(dataId);
}
delete(dataId: DataId): boolean {
this.dataIdsCount--;
return this.data.delete(dataId);
}
numDataIds(): number {
return this.dataIdsCount;
}
}
export interface DataMover {
/**
* To be called by backends whenever they see a dataId that they don't own.
* Upon calling this method, the mover will fetch the tensor from another
* backend and register it with the current active backend.
*/
moveData(backend: KernelBackend, dataId: DataId): void;
}
export interface BackendTimer {
// check if backend timer is available
timerAvailable(): boolean;
time(f: () => void): Promise<BackendTimingInfo>;
}
/**
* The interface that defines the kernels that should be implemented when
* adding a new backend. New backends don't need to implement every one of the
* methods, this can be done gradually (throw an error for unimplemented
* methods).
*/
export class KernelBackend implements TensorStorage, Backend, BackendTimer {
refCount(dataId: DataId): number {
return notYetImplemented('refCount');
}
incRef(dataId: DataId): void {
return notYetImplemented('incRef');
}
timerAvailable(): boolean {
return true;
}
time(f: () => void): Promise<BackendTimingInfo> {
return notYetImplemented('time');
}
read(dataId: object): Promise<BackendValues> {
return notYetImplemented('read');
}
readSync(dataId: object): BackendValues {
return notYetImplemented('readSync');
}
readToGPU(dataId: object, options?: DataToGPUOptions): GPUData {
return notYetImplemented('readToGPU');
}
numDataIds(): number {
return notYetImplemented('numDataIds');
}
disposeData(dataId: object, force?: boolean): boolean {
return notYetImplemented('disposeData');
}
write(values: BackendValues, shape: number[], dtype: DataType): DataId {
return notYetImplemented('write');
}
move(
dataId: DataId, values: BackendValues, shape: number[], dtype: DataType,
refCount: number): void {
return notYetImplemented('move');
}
createTensorFromGPUData(
values: WebGLData|WebGPUData, shape: number[], dtype: DataType): Tensor {
return notYetImplemented('createTensorFromGPUData');
}
memory(): {unreliable: boolean; reasons?: string[]} {
return notYetImplemented('memory');
}
/** Returns the highest precision for floats in bits (e.g. 16 or 32) */
floatPrecision(): 16|32 {
return notYetImplemented('floatPrecision');
}
/** Returns the smallest representable number. */
epsilon(): number {
return this.floatPrecision() === 32 ? EPSILON_FLOAT32 : EPSILON_FLOAT16;
}
dispose(): void {
return notYetImplemented('dispose');
}
}
function notYetImplemented(kernelName: string): never {
throw new Error(
`'${kernelName}' not yet implemented or not found in the registry. ` +
`This kernel may not be supported by the tfjs backend you have chosen`);
}