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

fix bmm -> mm optimization error #548

Merged
merged 1 commit into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion tests/test_torchinductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ def test_linear2(self):
)
self.common(mod, (torch.randn(2, 8),))

def test_bmm(self):
def test_bmm1(self):
def fn(a, b):
return (
torch.bmm(a, b),
Expand All @@ -803,6 +803,19 @@ def fn(a, b):
check_lowp=False,
)

def test_bmm2(self):
def fn(a, b):
return torch.bmm(a.permute(0, 2, 1), b)

self.common(
fn,
(
torch.randn(1, 8, 8),
torch.randn(1, 8, 8),
),
check_lowp=False,
)

def test_gather(self):
def fn(a, b):
return (torch.gather(a.expand([4, 5, 10, 6]), 3, b + 1),)
Expand Down
24 changes: 18 additions & 6 deletions torchinductor/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,16 +611,28 @@ def load(index):

class SqueezeView(BaseView):
@classmethod
def create(cls, x):
def create(cls, x, *, dim=None):

if is_storage_and_layout(x):
storage, old_layout = as_storage_and_layout(x)
new_size = []
new_stride = []
for size, stride in zip(old_layout.size, old_layout.stride):
if size != 1:
new_size.append(size)
new_stride.append(stride)
if dim is not None:
assert isinstance(dim, int), "expected integer dim argument"
assert 0 <= dim and dim < len(old_layout.size)

for i, (size, stride) in enumerate(zip(old_layout.size, old_layout.stride)):
if dim is None:
if size != 1:
new_size.append(size)
new_stride.append(stride)
else:
if i != dim:
new_size.append(size)
new_stride.append(stride)
else:
assert size == 1, "expected squeezed size to be 1"

new_layout = FixedLayout(
old_layout.device,
old_layout.dtype,
Expand Down Expand Up @@ -1897,7 +1909,7 @@ def create(cls, a, b):
# convert to normal mm
data = MatrixMultiply(
layout=output_layout.as_fixed(),
inputs=[View.create(a, [m, k1]), View.create(b, [k2, n])],
inputs=[SqueezeView.create(a, dim=0), SqueezeView.create(b, dim=0)],
)
data.output_view = ReinterpretView(
data,
Expand Down