yangulei commented on a change in pull request #10112:
URL: https://github.com/apache/tvm/pull/10112#discussion_r803256748
##########
File path: tests/python/relay/test_cpp_build_module.py
##########
@@ -93,6 +93,35 @@ def test_fp16_build():
np.testing.assert_allclose(out.numpy(), X.numpy() + Y.numpy(), atol=1e-5,
rtol=1e-5)
[email protected]_llvm
+def test_bf16_build():
+ data = relay.var("data", shape=(1, 3, 224, 224), dtype='float32')
+ weight = relay.var("weight", shape=(64, 3, 7, 7), dtype='float32')
+ bn_gamma = relay.var("gamma", shape=(64,), dtype='float32')
+ bn_beta = relay.var("beta", shape=(64,), dtype='float32')
+ bn_mean = relay.var("mean", shape=(64,), dtype='float32')
+ bn_var = relay.var("var", shape=(64,), dtype='float32')
+ params = {
+ "weight": np.random.uniform(-1, 1, size=(64, 3, 7,
7)).astype('float32'),
+ "gamma": np.random.uniform(-1, 1, size=(64, )).astype('float32'),
+ "beta": np.random.uniform(-1, 1, size=(64, )).astype('float32'),
+ "mean": np.random.uniform(-1, 1, size=(64, )).astype('float32'),
+ "var": np.random.uniform(-1, 1, size=(64, )).astype('float32'),
+ }
+ conv_bf16 = relay.nn.conv2d(relay.cast(data, 'bfloat16'),
relay.cast(weight, 'bfloat16'),
+ strides=(2, 2), padding=(3, 3, 3, 3),
channels=64, kernel_size=(7, 7), out_dtype='bfloat16')
+ bn_bf16 = relay.nn.batch_norm(conv_bf16, relay.cast(bn_gamma, 'bfloat16'),
+ relay.cast(bn_beta, 'bfloat16'),
relay.cast(bn_mean, 'bfloat16'), relay.cast(bn_var, 'bfloat16'))
+ relu_bf16 = relay.nn.relu(bn_bf16[0])
+ maxpool_bf16 = relay.nn.max_pool2d(
+ relu_bf16, pool_size=(2, 2), strides=(2, 2))
+ avgpool_bf16 = relay.nn.avg_pool2d(
+ maxpool_bf16, pool_size=(2, 2), strides=(2, 2))
+ mod_bf16 = tvm.IRModule.from_expr(avgpool_bf16)
+ with tvm.transform.PassContext(opt_level=3):
+ relay.build(mod_bf16, target="llvm", params=params)
Review comment:
Good point.
The correctness of simple bfloat16 `adding` has been checked at
https://github.com/apache/tvm/blob/14d0187ce9cefc41e33aa30b55c08a75a6711732/tests/python/unittest/test_target_codegen_llvm.py#L739
While, verifying the correctness of bfloat16 inference is much more complex
than fist thought.
We usually use relative error and absolute error to evaluate the accuracy,
but both of them could be large for a simple `multiply` with random inputs.
On the other hand, the MSE of the outputs (array with len=1000) of native
bfloat16 inference for ResNet\<18/34/50/101/152\>_v1b and VGG\<11/13/16/19\>
are about 0.5% to 1%, and less than 0.2% for BYOC-oneDNN inference. Also the
**_envelope curve_** of the bfloat16 and float32 outputs are close.
I couldn't find any metrics good enough to estimate the accuracy of bfloat16
inference. Any suggestions for this?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]