forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
166 lines (149 loc) · 4.88 KB
/
types.ts
File metadata and controls
166 lines (149 loc) · 4.88 KB
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
/**
* @license
* Copyright 2017 Google Inc. 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.
* =============================================================================
*/
/** @docalias number[] */
export interface ShapeMap {
R0: number[];
R1: [number];
R2: [number, number];
R3: [number, number, number];
R4: [number, number, number, number];
R5: [number, number, number, number, number];
R6: [number, number, number, number, number, number];
}
/** @docalias number[] */
export interface ArrayMap {
R0: number;
R1: number[];
R2: number[][];
R3: number[][][];
R4: number[][][][];
R5: number[][][][][];
R6: number[][][][][][];
}
export interface DataTypeMap {
float32: Float32Array;
int32: Int32Array;
bool: Uint8Array;
complex64: Float32Array;
string: string[];
}
export interface SingleValueMap {
bool: boolean;
int32: number;
float32: number;
complex64: number;
string: string;
}
/** @docalias 'float32'|'int32'|'bool'|'complex64'|'string' */
export type DataType = keyof DataTypeMap;
export type NumericDataType = 'float32'|'int32'|'bool'|'complex64';
export type TypedArray = Float32Array|Int32Array|Uint8Array;
/** Tensor data used in tensor creation and user-facing API. */
export type DataValues = DataTypeMap[DataType];
/** The underlying tensor data that gets stored in a backend. */
export type BackendValues = Float32Array|Int32Array|Uint8Array|Uint8Array[];
export enum Rank {
R0 = 'R0',
R1 = 'R1',
R2 = 'R2',
R3 = 'R3',
R4 = 'R4',
R5 = 'R5',
R6 = 'R6'
}
export type FlatVector = boolean[]|number[]|TypedArray;
export type RegularArray<T> =
T[]|T[][]|T[][][]|T[][][][]|T[][][][][]|T[][][][][][];
// tslint:disable-next-line:no-any
export interface RecursiveArray<T extends any> {
[index: number]: T|RecursiveArray<T>;
}
// Looks for upcasting types. Used, for example, in operations with mixed dtype
// inputs.
enum UpcastInt32AndMap {
'float32' = 'float32',
'int32' = 'int32',
'bool' = 'int32',
'complex64' = 'complex64'
}
enum UpcastBoolAndMap {
'float32' = 'float32',
'int32' = 'int32',
'bool' = 'bool',
'complex64' = 'complex64'
}
enum UpcastFloat32AndMap {
'float32' = 'float32',
'int32' = 'float32',
'bool' = 'float32',
'complex64' = 'complex64'
}
enum UpcastComplex64AndMap {
'float32' = 'complex64',
'int32' = 'complex64',
'bool' = 'complex64',
'complex64' = 'complex64'
}
const upcastTypeMap = {
'float32': UpcastFloat32AndMap,
'int32': UpcastInt32AndMap,
'bool': UpcastBoolAndMap,
'complex64': UpcastComplex64AndMap
};
export function upcastType(typeA: DataType, typeB: DataType): DataType {
if (typeA === 'string' || typeB === 'string') {
if (typeA === 'string' && typeB === 'string') {
return 'string';
}
throw new Error(`Can not upcast ${typeA} with ${typeB}`);
}
return upcastTypeMap[typeA][typeB];
}
/** Returns the output type after summation. */
export function sumOutType(type: DataType): DataType {
return upcastType(type, 'int32');
}
/** @docalias TypedArray|Array */
export type TensorLike =
TypedArray|number|boolean|string|RecursiveArray<number|number[]|TypedArray>|
RecursiveArray<boolean>|RecursiveArray<string>|Uint8Array[];
export type ScalarLike = number|boolean|string|Uint8Array;
/** @docalias TypedArray|Array */
export type TensorLike1D = TypedArray|number[]|boolean[]|string[]|Uint8Array[];
/** @docalias TypedArray|Array */
export type TensorLike2D = TypedArray|number[]|number[][]|boolean[]|boolean[][]|
string[]|string[][]|Uint8Array[]|Uint8Array[][];
/** @docalias TypedArray|Array */
export type TensorLike3D = TypedArray|number[]|number[][][]|boolean[]|
boolean[][][]|string[]|string[][][]|Uint8Array[]|Uint8Array[][][];
/** @docalias TypedArray|Array */
export type TensorLike4D = TypedArray|number[]|number[][][][]|boolean[]|
boolean[][][][]|string[]|string[][][][]|Uint8Array[]|Uint8Array[][][][];
/** @docalias TypedArray|Array */
export type TensorLike5D =
TypedArray|number[]|number[][][][][]|boolean[]|boolean[][][][][]|string[]|
string[][][][][]|Uint8Array[]|Uint8Array[][][][][];
/** @docalias TypedArray|Array */
export type TensorLike6D =
TypedArray|number[]|number[][][][][][]|boolean[]|boolean[][][][][][]|
string[]|string[][][][][][]|Uint8Array[]|Uint8Array[][][][][];
/** Type for representing image dat in Uint8Array type. */
export interface PixelData {
width: number;
height: number;
data: Uint8Array;
}