Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Frontend,TOPI] Improve dynamism for BatchMatmul and Dense #7496

Merged
merged 17 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions python/tvm/relay/frontend/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@
__all__ = ["from_tensorflow"]


def check_symbolic_shape(shape):
return not all([isinstance(dim, (int, tvm.tir.IntImm)) for dim in shape])


def list_shape_of(tensor, ndim):
shape_tensor = _op.shape_of(tensor)
return [
_op.strided_slice(shape_tensor, begin=[i], end=[i + 1], strides=[1]) for i in range(ndim)
]


def _get_pad_pair(input1d, kernel1d, stride1d):
if input1d % stride1d == 0:
pad = max(kernel1d - stride1d, 0)
Expand Down Expand Up @@ -919,13 +930,31 @@ def _impl(inputs, attr, params, mod):
input_y = inputs[1]
orig_shape_x = _infer_shape(input_x, mod)
orig_shape_y = _infer_shape(input_y, mod)
ndim = len(orig_shape_x)

is_static = not check_symbolic_shape(orig_shape_x)

if len(orig_shape_x) > 3 and not is_static:
monklof marked this conversation as resolved.
Show resolved Hide resolved
shape_of_x = list_shape_of(inputs[0], ndim)
shape_of_y = list_shape_of(inputs[1], ndim)

# reshape n-dimensional batch matmul into 3d
if len(orig_shape_x) > 3:
outer_dims = [orig_shape_x[i] for i in range(0, len(orig_shape_x) - 2)]
num_outer_elts = np.prod(outer_dims)
new_shape_x = (num_outer_elts, orig_shape_x[-2], orig_shape_x[-1])
new_shape_y = (num_outer_elts, orig_shape_y[-2], orig_shape_y[-1])
if is_static:
num_outer_elts = np.prod(outer_dims)
new_shape_x = (num_outer_elts, orig_shape_x[-2], orig_shape_x[-1])
new_shape_y = (num_outer_elts, orig_shape_y[-2], orig_shape_y[-1])
else: # handle dynamic shape (dyn.reshape op)
# new shape = [prod(shape[:-2]), -2, -1]
new_shape_x = [_op.const(1), shape_of_x[-2], shape_of_x[-1]]
new_shape_y = [_op.const(1), shape_of_y[-2], shape_of_y[-1]]
for i in range(ndim - 2):
new_shape_x[0] *= shape_of_x[i]
new_shape_y[0] *= shape_of_y[i]
new_shape_x = _op.concatenate(_op.Tuple(new_shape_x), axis=0)
new_shape_y = _op.concatenate(_op.Tuple(new_shape_y), axis=0)

input_x = _op.reshape(input_x, newshape=new_shape_x)
input_y = _op.reshape(input_y, newshape=new_shape_y)

Expand All @@ -937,11 +966,18 @@ def _impl(inputs, attr, params, mod):

# reshape result back to n-dimensional
if len(orig_shape_x) > 3:
final_shape = list(orig_shape_x)
final_shape[-2] = orig_shape_x[-1] if adj_x else orig_shape_x[-2]
final_shape[-1] = orig_shape_y[-2] if adj_y else orig_shape_y[-1]
ret = _op.reshape(ret, newshape=final_shape)
if is_static:
final_shape = list(orig_shape_x)
final_shape[-2] = orig_shape_x[-1] if adj_x else orig_shape_x[-2]
final_shape[-1] = orig_shape_y[-2] if adj_y else orig_shape_y[-1]
else:
# calculate the resulting shape = [shape[:-2], 0, 0]
final_shape = list(shape_of_x)
final_shape[-2] = shape_of_x[-1] if adj_x else shape_of_x[-2]
final_shape[-1] = shape_of_y[-2] if adj_y else shape_of_y[-1]
final_shape = _op.concatenate(_op.Tuple(final_shape), axis=0)

ret = _op.reshape(ret, newshape=final_shape)
return ret

return _impl
Expand Down
3 changes: 2 additions & 1 deletion python/tvm/topi/cuda/batch_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def batch_matmul_cublas(cfg, x, y, out_shape=None):
"""
b, m, k = x.shape
b, n, k = y.shape
cfg.add_flop(b * m * k * n * 2)
if all([isinstance(s, int) for s in [b, m, n, k]]):
monklof marked this conversation as resolved.
Show resolved Hide resolved
cfg.add_flop(b * m * k * n * 2)
return cublas.batch_matmul(x, y, False, True)


Expand Down
11 changes: 4 additions & 7 deletions python/tvm/topi/cuda/dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# pylint: disable=invalid-name, unused-argument
"""Schedule for dense operator"""
import logging
from tvm import te, tir
from tvm import te
import tvm.autotvm as autotvm
from tvm.autotvm.task.space import SplitEntity
from tvm.contrib import cublas
Expand All @@ -39,14 +39,11 @@ def dense_cublas(cfg, data, weight, bias=None, out_dtype=None):
if out_dtype is None:
out_dtype = data.dtype
assert out_dtype == data.dtype, "Mixed precision not supported."
batch, in_dim = data.shape
out_dim, _ = weight.shape
batch, in_dim = get_const_tuple(data.shape)
out_dim, _ = get_const_tuple(weight.shape)
matmul = cublas.matmul(data, weight, False, True)
if isinstance(batch, int):
if all(isinstance(d, int) for d in [batch, in_dim, out_dim]):
monklof marked this conversation as resolved.
Show resolved Hide resolved
cfg.add_flop(batch * in_dim * out_dim * 2)
elif isinstance(batch, tir.IntImm):
cfg.add_flop(batch.value * in_dim * out_dim * 2)
# if we get a te.Var, we cannot add flop counts
if bias is not None:
matmul = te.compute(
(batch, out_dim), lambda i, j: matmul[i, j] + bias[j], tag=tag.BROADCAST
Expand Down