rahul003 closed pull request #10538: [MXNET-318] Allow dot for fp16 on GPU URL: https://github.com/apache/incubator-mxnet/pull/10538
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/src/operator/tensor/dot-inl.h b/src/operator/tensor/dot-inl.h index c5f278e78a4..a6833c288cc 100644 --- a/src/operator/tensor/dot-inl.h +++ b/src/operator/tensor/dot-inl.h @@ -69,9 +69,10 @@ void DotForward_(const nnvm::NodeAttrs& attrs, << "Binary function only support input/output with the same type"; CHECK_EQ(outputs[0].type_flag_, inputs[1].type_flag_) << "Binary function only support input/output with the same type"; - CHECK(outputs[0].type_flag_ == kFloat32 || outputs[0].type_flag_ == kFloat64) - << "dot only supports float32 and float64"; - MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, DType, { + CHECK(outputs[0].type_flag_ == kFloat32 || outputs[0].type_flag_ == kFloat64 || + ctx.run_ctx.ctx.dev_mask() == mshadow::gpu::kDevMask) + << "dot only supports float32/float64 for CPU, and float16/float32/float64 for GPU"; + MSHADOW_REAL_TYPE_SWITCH(outputs[0].type_flag_, DType, { if (inputs[0].ndim() == 1 && inputs[1].ndim() == 1) { CHECK_NE(req[0], kAddTo) << "AddTo not yet supported"; Tensor<xpu, 1, DType> out = outputs[0].get<xpu, 1, DType>(s); @@ -129,7 +130,7 @@ void DotBackward_(const nnvm::NodeAttrs& attrs, Stream<xpu> *s = ctx.get_stream<xpu>(); CHECK_NE(req[0], kWriteInplace); CHECK_NE(req[1], kWriteInplace); - MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, DType, { + MSHADOW_REAL_TYPE_SWITCH(outputs[0].type_flag_, DType, { if (inputs[1].ndim() == 1 && inputs[2].ndim() == 1) { Tensor<xpu, 1, DType> mout_grad = inputs[0].get<xpu, 1, DType>(s); Tensor<xpu, 1, DType> mlhs_data = inputs[1].get<xpu, 1, DType>(s); diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index 5d382220a7a..78fd84145ec 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -2070,7 +2070,7 @@ def test_stn(): @with_seed(1234) def test_dot(): ctx=default_context() - dtypes = ['float32', 'float64'] + dtypes = ['float16', 'float32', 'float64'] # Test normal dot. for data_type in dtypes: @@ -2094,10 +2094,16 @@ def test_dot(): c = mx.sym.dot(a, b) exe = c.simple_bind(ctx=ctx, a=a_npy.shape, b=b_npy.shape) outputs = exe.forward(is_train=True, a=a_npy, b=b_npy) - assert_almost_equal(outputs[0].asnumpy(), c_npy, rtol=1e-3) - exe.backward(out_grads=[mx.nd.array(ograd_npy, mx.cpu())]) - assert_almost_equal(exe.grad_dict['a'].asnumpy(), agrad_npy, rtol=1e-3) - assert_almost_equal(exe.grad_dict['b'].asnumpy(), bgrad_npy, rtol=1e-3) + assert_almost_equal(outputs[0].asnumpy(), c_npy, + rtol=1e-2 if data_type == 'float16' else 1e-3, + atol=1e-2 if data_type == 'float16' else 1e-3) + exe.backward(out_grads=[mx.nd.array(ograd_npy, mx.cpu()).astype(data_type)]) + assert_almost_equal(exe.grad_dict['a'].asnumpy(), agrad_npy, + rtol=1e-2 if data_type == 'float16' else 1e-3, + atol=1e-2 if data_type == 'float16' else 1e-3) + assert_almost_equal(exe.grad_dict['b'].asnumpy(), bgrad_npy, + rtol=1e-2 if data_type == 'float16' else 1e-3, + atol=1e-2 if data_type == 'float16' else 1e-3) # Test dot with transpose flag using gradient checker. def dot_sym(data_type): ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services