-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjest.setup.ts
144 lines (138 loc) · 4.32 KB
/
jest.setup.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
import { jest } from '@jest/globals';
import { Rank, ShapeMap, Tensor, KernelBackend } from '@tensorflow/tfjs-core';
const createMockTensorInstance = () => ({
shape: [1],
dtype: 'float32',
size: 1,
data: async () => new Float32Array(1),
dataSync: () => new Float32Array(1),
dispose: () => {},
array: async () => [0],
arraySync: () => [0],
print: () => {},
reshape: function(newShape: number[]) {
const instance = createMockTensorInstance();
instance.shape = newShape;
return instance;
},
cast: function(dtype: string) {
const instance = createMockTensorInstance();
instance.dtype = dtype;
return instance;
},
clone: function() { return createMockTensorInstance(); },
val: async () => new Float32Array(1),
buffer: async () => ({ shape: [1], dtype: 'float32', size: 1, values: new Float32Array(1) }),
expandDims: function() { return createMockTensorInstance(); }
});
// Mock TensorFlow
const tf = {
sequential: jest.fn().mockReturnValue({
add: jest.fn().mockReturnThis(),
compile: jest.fn(),
fit: jest.fn().mockResolvedValue({}),
predict: jest.fn().mockReturnValue({
data: () => Promise.resolve(new Float32Array([0.5])),
dispose: jest.fn()
}),
dispose: jest.fn(),
layers: [],
getWeights: jest.fn().mockReturnValue([]),
setWeights: jest.fn()
}),
layers: {
dense: jest.fn().mockImplementation((config) => ({
apply: jest.fn(),
getWeights: jest.fn().mockReturnValue([]),
setWeights: jest.fn(),
units: config.units,
activation: config.activation,
kernelInitializer: config.kernelInitializer,
getConfig: () => config,
name: 'dense'
}))
},
ready: jest.fn().mockResolvedValue(undefined),
getBackend: jest.fn().mockReturnValue('cpu'),
setBackend: jest.fn().mockResolvedValue(true),
env: jest.fn().mockReturnValue({ flags: {}, platform: 'node' }),
tensor2d: jest.fn().mockReturnValue({
dispose: jest.fn(),
data: () => Promise.resolve(new Float32Array([0.5])),
arraySync: () => [[0.5]],
reshape: jest.fn().mockReturnThis()
}),
train: {
adam: jest.fn().mockReturnValue({
minimize: jest.fn()
})
},
tensor: jest.fn().mockReturnValue({
dispose: jest.fn(),
data: () => Promise.resolve(new Float32Array([0.5])),
arraySync: () => [[0.5]],
reshape: jest.fn().mockReturnThis()
}),
disposeVariables: jest.fn(),
backend: jest.fn().mockReturnValue({
dispose: () => true,
disposeData: () => true,
read: async () => new Float32Array(),
readSync: () => new Float32Array(),
readToGPU: () => ({
texture: undefined,
texShape: [1, 1],
tensorRef: createMockTensorInstance(),
buffer: {
__brand: 'GPUBuffer',
size: 4,
usage: 0,
mapState: 'unmapped',
getMappedRange: () => new ArrayBuffer(4),
unmap: () => {},
destroy: () => {},
label: '',
onSubmittedWorkDone: () => Promise.resolve(undefined),
mapAsync: () => Promise.resolve(undefined),
getBindGroupLayout: () => ({})
}
}),
numDataIds: () => 0,
createTensorFromGPUData: () => createMockTensorInstance(),
write: () => ({}),
memory: () => ({ unreliable: false, reasons: [] }),
refCount: () => 0,
incRef: () => {},
timerAvailable: () => true,
time: () => Promise.resolve({ kernelMs: 0, wallMs: 0 }),
getTexture: () => null,
getDataSubset: () => new Float32Array(),
makeTensorInfo: (shape: number[], dtype: string) => ({ dataId: {}, shape, dtype }),
move: () => {},
fromPixels: () => createMockTensorInstance()
}),
randomNormal: jest.fn().mockImplementation(() => createMockTensorInstance()),
loadLayersModel: jest.fn().mockResolvedValue({
layers: [
{
getClassName: jest.fn().mockReturnValue('Dense'),
inputShape: [10],
outputShape: [10],
apply: jest.fn().mockImplementation(() => createMockTensorInstance())
}
],
predict: jest.fn().mockImplementation(() => createMockTensorInstance()),
dispose: jest.fn()
}),
losses: {
meanSquaredError: jest.fn().mockImplementation(() => createMockTensorInstance())
},
buffer: jest.fn().mockReturnValue({
shape: [1],
dtype: 'float32',
size: 1,
values: new Float32Array(1)
})
};
// Export the mock TensorFlow instance
export default tf;