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

not create new symbol with immutable shape convolution. #1867

Merged
merged 1 commit into from
May 31, 2023

Conversation

fukatani
Copy link
Contributor

Resolve #1763
If conv uses custom_pad and pad[r] - effective_ks[r] == -1 and strides[r] == 1, output_shape equals input_shape.
In this case we should not create new symbol for shape.

I confirmed flexible MultiHeadAttention works with this change.

import torch  # 1.13.1
import numpy as np  # 1.22.3
import coremltools as ct  # 6.2

from ane_transformers.reference.multihead_attention import MultiHeadAttention

N = 10
x = torch.rand(1, 512, 1, N)
y = torch.rand(1, 512, 1, N)
z = torch.rand(1, 512, 1, N)

with torch.no_grad():
    layer = MultiHeadAttention(512, n_head=8, dropout=0.0).eval()
    jit = torch.jit.trace(layer, (x, y, z))

    # Flexible input shape - fails
    flexible_shape = ct.Shape(shape=(1, 512, 1, ct.RangeDim(1, 448)))
    mlmod_flexible_shape = ct.convert(
        jit,
        inputs=[
            ct.TensorType("q", flexible_shape),
            ct.TensorType("k", flexible_shape),
            ct.TensorType("v", flexible_shape),
        ]
    )

    out = layer(x, x, x)
    out_dict = mlmod_flexible_shape.predict({'q': x.detach().numpy().astype(np.float32),
                                             'k': x.detach().numpy().astype(np.float32),
                                             'v': x.detach().numpy().astype(np.float32)})
    np.allclose(out[0], out_dict['var_195'], rtol=0.001, atol=0.001)  # OK

@YifanShenSZ YifanShenSZ self-requested a review May 30, 2023 06:36
@YifanShenSZ
Copy link
Collaborator

Looks perfect, thanks for the fix!

Kicked off CI, will approve once it turns green.

Copy link
Collaborator

@YifanShenSZ YifanShenSZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI passed, approved, thanks again!

@YifanShenSZ YifanShenSZ merged commit ef18cc0 into apple:main May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Apple's ANE Optimised MultiHeadAttention Export Fails With Flexible Input Shape
2 participants