You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/01/21 18:35:14 UTC
[GitHub] [tvm] comaniac commented on a change in pull request #9986: [CUDNN] Support gradient kernels
comaniac commented on a change in pull request #9986:
URL: https://github.com/apache/tvm/pull/9986#discussion_r789902697
##########
File path: python/tvm/topi/cuda/conv2d.py
##########
@@ -123,3 +123,22 @@ def conv2d_cudnn(
def schedule_conv2d_cudnn(cfg, outs):
"""Create the schedule for conv2d_cudnn"""
return generic.schedule_extern(outs)
+
+
+def conv2d_backward_weight(
+ dy, x, kernel_size, padding, stride, dilation, groups, layout, output_dtype
+):
+ """Compute conv2d wgrad using CuDNN library"""
+ assert layout in ["NCHW", "NHWC"]
+ return cudnn.conv_backward_filter(
+ dy,
+ x,
+ kernel_size,
+ padding,
+ stride,
+ dilation,
+ conv_mode=1,
+ tensor_format=0 if layout == "NCHW" else 1,
+ conv_dtype=output_dtype,
+ groups=groups,
+ )
Review comment:
This might confuse people IMHO. Maybe name this function `conv2d_backward_weight_cudnn` as other conv2ds?
##########
File path: tests/python/contrib/test_cudnn.py
##########
@@ -236,6 +236,148 @@ def test_softmax():
verify_softmax_4d((1, 16, 256, 256), "float64", log_softmax=True)
+def verify_conv2d_backward_data(data_dtype, conv_dtype, tensor_format=0, tol=1e-5):
+ batch = 3
+ in_channel = 4
+ out_channel = 16
+ filter_h, filter_w = 3, 3
+ pad_h, pad_w = 1, 1
+ stride_h, stride_w = 1, 1
+ height, width = 32, 32
+
+ if tensor_format == 0:
+ xshape = [batch, in_channel, height, width]
+ wshape = [out_channel, in_channel, filter_h, filter_w]
+ oshape = xshape
+ oshape[1] = out_channel
+ ref_func = tvm.topi.testing.conv2d_transpose_nchw_python
+ else:
+ xshape = [batch, height, width, in_channel]
+ wshape = [out_channel, filter_h, filter_w, in_channel]
+ oshape = xshape
+ oshape[3] = out_channel
+ ref_func = lambda dy_np, w_np, strides, padding, out_pad: tvm.topi.testing.conv2d_transpose_nhwc_python(
+ dy_np, np.transpose(w_np, [1, 2, 3, 0]), "HWOI", strides, padding, out_pad
+ )
+
+ dy_np = np.random.uniform(-1, 1, oshape).astype(data_dtype)
+ w_np = np.random.uniform(-1, 1, wshape).astype(data_dtype)
+
+ if data_dtype == "float16":
+ dx_np = ref_func(
+ dy_np.astype("float32"),
+ w_np.astype("float32"),
+ (stride_h, stride_w),
+ (pad_h, pad_w),
+ (0, 0),
+ )
+ dx_np = dx_np.astype("float16")
+ else:
+ dx_np = ref_func(dy_np, w_np, (stride_h, stride_w), (pad_h, pad_w), (0, 0))
+
+ dy = te.placeholder(oshape, name="dy", dtype=data_dtype)
+ w = te.placeholder(wshape, name="dw", dtype=data_dtype)
+ dx = cudnn.conv_backward_data(
+ dy,
+ w,
+ [pad_h, pad_w],
+ [stride_h, stride_w],
+ [1, 1],
+ conv_mode=1,
+ tensor_format=tensor_format,
+ conv_dtype=conv_dtype,
+ groups=1,
+ )
+
+ s = te.create_schedule(dx.op)
+
+ dev = tvm.cuda(0)
+ f = tvm.build(s, [dy, w, dx], "cuda --host=llvm", name="conv2d_backward_data")
+
+ dy = tvm.nd.array(dy_np, dev)
+ w = tvm.nd.array(w_np, dev)
+ dx = tvm.nd.array(dx_np, dev)
+
+ f(dy, w, dx)
+ print(np.max(np.abs(dx.numpy() - dx_np)))
+ print(np.mean(np.abs(dx.numpy() - dx_np)))
Review comment:
Remove?
##########
File path: tests/python/contrib/test_cudnn.py
##########
@@ -236,6 +236,148 @@ def test_softmax():
verify_softmax_4d((1, 16, 256, 256), "float64", log_softmax=True)
+def verify_conv2d_backward_data(data_dtype, conv_dtype, tensor_format=0, tol=1e-5):
+ batch = 3
+ in_channel = 4
+ out_channel = 16
+ filter_h, filter_w = 3, 3
+ pad_h, pad_w = 1, 1
+ stride_h, stride_w = 1, 1
+ height, width = 32, 32
+
+ if tensor_format == 0:
+ xshape = [batch, in_channel, height, width]
+ wshape = [out_channel, in_channel, filter_h, filter_w]
+ oshape = xshape
+ oshape[1] = out_channel
+ ref_func = tvm.topi.testing.conv2d_transpose_nchw_python
+ else:
+ xshape = [batch, height, width, in_channel]
+ wshape = [out_channel, filter_h, filter_w, in_channel]
+ oshape = xshape
+ oshape[3] = out_channel
+ ref_func = lambda dy_np, w_np, strides, padding, out_pad: tvm.topi.testing.conv2d_transpose_nhwc_python(
+ dy_np, np.transpose(w_np, [1, 2, 3, 0]), "HWOI", strides, padding, out_pad
+ )
+
+ dy_np = np.random.uniform(-1, 1, oshape).astype(data_dtype)
+ w_np = np.random.uniform(-1, 1, wshape).astype(data_dtype)
+
+ if data_dtype == "float16":
+ dx_np = ref_func(
+ dy_np.astype("float32"),
+ w_np.astype("float32"),
+ (stride_h, stride_w),
+ (pad_h, pad_w),
+ (0, 0),
+ )
+ dx_np = dx_np.astype("float16")
+ else:
+ dx_np = ref_func(dy_np, w_np, (stride_h, stride_w), (pad_h, pad_w), (0, 0))
+
+ dy = te.placeholder(oshape, name="dy", dtype=data_dtype)
+ w = te.placeholder(wshape, name="dw", dtype=data_dtype)
+ dx = cudnn.conv_backward_data(
+ dy,
+ w,
+ [pad_h, pad_w],
+ [stride_h, stride_w],
+ [1, 1],
+ conv_mode=1,
+ tensor_format=tensor_format,
+ conv_dtype=conv_dtype,
+ groups=1,
+ )
+
+ s = te.create_schedule(dx.op)
+
+ dev = tvm.cuda(0)
+ f = tvm.build(s, [dy, w, dx], "cuda --host=llvm", name="conv2d_backward_data")
+
+ dy = tvm.nd.array(dy_np, dev)
+ w = tvm.nd.array(w_np, dev)
+ dx = tvm.nd.array(dx_np, dev)
+
+ f(dy, w, dx)
+ print(np.max(np.abs(dx.numpy() - dx_np)))
+ print(np.mean(np.abs(dx.numpy() - dx_np)))
+ tvm.testing.assert_allclose(dx.numpy(), dx_np, atol=tol, rtol=tol)
+
+
+@tvm.testing.requires_gpu
+@requires_cudnn
+def test_conv2d_backward_data():
+ verify_conv2d_backward_data("float32", "float32", tensor_format=0, tol=1e-5)
+ verify_conv2d_backward_data("float32", "float32", tensor_format=1, tol=1e-2)
+ # The scipy convolve function does not support fp16, so the reference will be computed with
+ # fp32. Use larger tolerance to be on the safe side (1e-2 also seems mostly ok).
+ verify_conv2d_backward_data("float16", "float16", tensor_format=1, tol=1e-1)
+
+
+def verify_conv2d_backward_filter(data_dtype, conv_dtype, tensor_format=0, tol=1e-5):
+ batch = 3
+ in_channel = 4
+ out_channel = 16
+ filter_h, filter_w = 3, 3
+ pad_h, pad_w = 1, 1
+ stride_h, stride_w = 1, 1
+ height, width = 32, 32
+
+ if tensor_format == 0:
+ x_shape = [batch, in_channel, height, width]
+ dy_shape = [batch, out_channel, height, width]
+ else:
+ x_shape = [batch, height, width, in_channel]
+ dy_shape = [batch, height, width, out_channel]
+
+ x_np = np.random.uniform(-1, 1, x_shape).astype(data_dtype)
+ dy_np = np.random.uniform(-1, 1, dy_shape).astype(data_dtype)
+
+ dw_np = tvm.topi.testing.conv2d_backward_weight_python(
+ dy_np,
+ x_np,
+ (filter_h, filter_w),
+ (stride_h, stride_w),
+ (pad_h, pad_w),
+ "NCHW" if tensor_format == 0 else "NHWC",
+ )
+
+ x = te.placeholder(x_shape, name="x", dtype=data_dtype)
+ dy = te.placeholder(dy_shape, name="dy", dtype=data_dtype)
+ dw = cudnn.conv_backward_filter(
+ dy,
+ x,
+ (filter_h, filter_w),
+ [pad_h, pad_w],
+ [stride_h, stride_w],
+ [1, 1],
+ conv_mode=1,
+ tensor_format=tensor_format,
+ conv_dtype=conv_dtype,
+ )
+
+ s = te.create_schedule(dw.op)
+
+ dev = tvm.cuda(0)
+ f = tvm.build(s, [dy, x, dw], "cuda --host=llvm", name="conv2d_backward_filter")
+
+ x = tvm.nd.array(x_np, dev)
+ dy = tvm.nd.array(dy_np, dev)
+ dw = tvm.nd.array(dw_np, dev)
+
+ f(dy, x, dw)
+ print(np.max(np.abs(dw.numpy() - dw_np)))
+ print(np.mean(np.abs(dw.numpy() - dw_np)))
Review comment:
ditto
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org