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

Test Data for Pytorch Attention layer #1188

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
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
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_unflatten.npy
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_unflatten.npy
Binary file not shown.
41 changes: 41 additions & 0 deletions testdata/dnn/onnx/generate_onnx_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,47 @@ def forward(self, x):
save_data_and_model("einsum_transpose", mat, einsum, export_params=True)


class TorchAttentionLayer(nn.Module):
def __init__(self, embed_dim=6, num_heads=1):
super(TorchAttentionLayer, self).__init__()
self.attention = nn.MultiheadAttention(
embed_dim=embed_dim,
num_heads=num_heads,
bias=True,
batch_first=True)
def forward(self, x):
return self.attention(x, x, x)[0]

num_heads = 1
batch_size = 2
num_tokens = 5
emb_dim = 6
model = TorchAttentionLayer(embed_dim=emb_dim, num_heads=num_heads).eval()

x = torch.rand(batch_size, num_tokens, emb_dim)
with torch.no_grad():
output = model(x)

save_data_and_model("torch_attention_single_head", x, model, export_params=True)
class Unflatten(torch.nn.Module):
def __init__(self, E, times):
super(Unflatten, self).__init__()
self.E = E
self.times = times

def forward(self, x):
return x.unflatten(-1, (self.times, self.E))

unflatten_dim = 5
times = 3
model = Unflatten(unflatten_dim, times).eval()

x = torch.rand(10, 3, unflatten_dim * times)
with torch.no_grad():
output = model(x)

save_data_and_model("unflatten", x, model, export_params=True)

def _extract_value_info(x, name, type_proto=None): # type: (Union[List[Any], np.ndarray, None], Text, Optional[TypeProto]) -> onnx.ValueInfoProto
if type_proto is None:
if x is None:
Expand Down
Binary file not shown.
Binary file added testdata/dnn/onnx/models/unflatten.onnx
Binary file not shown.