Skip to content

Commit 4c6a6a3

Browse files
authored
[js/webgpu] Fix NAN caused by un-initialized buffer in instance-norm (#19387)
The added case will be NAN because of the un-initialized buffer.
1 parent 6bb6468 commit 4c6a6a3

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

js/web/lib/wasm/jsep/webgpu/ops/instance-norm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ const computeMean =
207207
let offset = currentImageNumber * uniforms.image_size;
208208
var sum = ${fillVector('f32', components)};
209209
var squaredSum = ${fillVector('f32', components)};
210-
for (var i: u32 = 0; i < ${WG}; i++) {
210+
for (var i: u32 = 0; i < min(${WG}, uniforms.H); i++) {
211211
let value = input[offset + i + currentChannelNumber * ${WG}];
212212
sum += value[0];
213213
squaredSum += value[1];

js/web/test/data/ops/instance-norm.jsonc

+80
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,85 @@
224224
]
225225
}
226226
]
227+
},
228+
{
229+
"name": "Simple test with NHWC, components 1, buffer reuse",
230+
"operator": "InstanceNormalization",
231+
"inputShapeDefinitions": "rankOnly",
232+
"opset": {
233+
"domain": "",
234+
"version": 17
235+
},
236+
"cases": [
237+
{
238+
"name": "Simple test",
239+
"inputs": [
240+
{
241+
"data": [1, 2, 3, 4, 5, 6],
242+
"dims": [2, 3, 1, 1],
243+
"type": "float32"
244+
},
245+
{
246+
"data": [1, 2, 3],
247+
"dims": [3],
248+
"type": "float32"
249+
},
250+
{
251+
"data": [4, 5, 6],
252+
"dims": [3],
253+
"type": "float32"
254+
}
255+
],
256+
"outputs": [
257+
{
258+
"data": [4, 5, 6, 4, 5, 6],
259+
"dims": [2, 3, 1, 1],
260+
"type": "float32"
261+
}
262+
]
263+
}
264+
]
265+
},
266+
{
267+
"name": "Simple test with NHWC, components 2, buffer reuse",
268+
"operator": "InstanceNormalization",
269+
"inputShapeDefinitions": "rankOnly",
270+
"opset": {
271+
"domain": "",
272+
"version": 17
273+
},
274+
"cases": [
275+
{
276+
"name": "Simple test",
277+
"inputs": [
278+
{
279+
"data": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2],
280+
"dims": [1, 6, 1, 3],
281+
"type": "float32"
282+
},
283+
{
284+
"data": [1, 2, 3, 4, 5, 6],
285+
"dims": [6],
286+
"type": "float32"
287+
},
288+
{
289+
"data": [4, 5, 6, 7, 8, 9],
290+
"dims": [6],
291+
"type": "float32"
292+
}
293+
],
294+
"outputs": [
295+
{
296+
"data": [
297+
2.775264263153076, 4, 5.224735260009766, 2.5505285263061523, 5, 7.449470520019531, 2.325794219970703, 6,
298+
9.674205780029297, 11.898944854736328, 7, 2.1010589599609375, 14.123676300048828, 8, 1.876321792602539,
299+
16.348413467407227, 9, 1.6515865325927734
300+
],
301+
"dims": [1, 6, 1, 3],
302+
"type": "float32"
303+
}
304+
]
305+
}
306+
]
227307
}
228308
]

0 commit comments

Comments
 (0)