MCPcopy Create free account
hub / github.com/apache/singa / _batch_norm_helper

Method _batch_norm_helper

test/python/test_onnx.py:294–330  ·  view source on GitHub ↗
(self, dev)

Source from the content-addressed store, hash-verified

292 self._max_pool_helper(gpu_dev)
293
294 def _batch_norm_helper(self, dev):
295 x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
296 s = np.array([1.0, 1.5]).astype(np.float32)
297 bias = np.array([0, 1]).astype(np.float32)
298 mean = np.array([0, 3]).astype(np.float32)
299 var = np.array([1, 1.5]).astype(np.float32)
300
301 x = tensor.from_numpy(x)
302 x.to_device(dev)
303 s = tensor.from_numpy(s)
304 s.to_device(dev)
305
306 bias = tensor.from_numpy(bias)
307 mean = tensor.from_numpy(mean)
308 var = tensor.from_numpy(var)
309
310 bias.to_device(dev)
311 mean.to_device(dev)
312 var.to_device(dev)
313 if dev == cpu_dev:
314 handle = singa.BatchNormHandle(0.9, x.data)
315 else:
316 handle = singa.CudnnBatchNormHandle(0.9, x.data)
317 y = autograd.batchnorm_2d(handle, x, s, bias, mean, var)
318
319 # frontend
320 model = sonnx.to_onnx([x, s, bias, mean, var], [y])
321 # print('The model is:\n{}'.format(model))
322
323 # backend
324 sg_ir = sonnx.prepare(model, device=dev)
325 sg_ir.is_graph = True
326 y_t = sg_ir.run([x, s, bias]) # mean and var has been stored in graph
327
328 np.testing.assert_array_almost_equal(tensor.to_numpy(y),
329 tensor.to_numpy(y_t[0]),
330 decimal=5)
331
332 def test_batch_norm_cpu(self):
333 self._batch_norm_helper(cpu_dev)

Callers 2

test_batch_norm_cpuMethod · 0.95
test_batch_norm_gpuMethod · 0.95

Calls 5

BatchNormHandleMethod · 0.80
CudnnBatchNormHandleMethod · 0.80
prepareMethod · 0.80
to_deviceMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected