-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[ONNX] Fix rotary_embedding_23 implementation #162865
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
[ONNX] Fix rotary_embedding_23 implementation #162865
Conversation
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/162865
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 3cc0463 with merge base a94ddd9 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
|
@pytorchbot merge |
|
The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 1 mandatory check(s) failed. The first few are: Dig deeper by viewing the failures on hud |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 0 checks: Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
@pytorchbot cherry-pick --onto release/2.9 --fixes "ONNX operator fix for the new dynamo export feature" -c critical |
The implementation of rotary_embedding_23 when input is 3D was incorrect.
## Tested
Locally with
```py
import onnx_ir as ir
import onnx
import torch
import os
import numpy as np
base_path = "/home/justinchu/dev/onnx/onnx/backend/test/data/node"
test_names = [
"test_rotary_embedding",
"test_rotary_embedding_3d_input",
"test_rotary_embedding_interleaved",
"test_rotary_embedding_no_position_ids",
"test_rotary_embedding_no_position_ids_interleaved",
"test_rotary_embedding_no_position_ids_rotary_dim",
"test_rotary_embedding_with_interleaved_rotary_dim",
"test_rotary_embedding_with_rotary_dim",
]
model_paths = [os.path.join(base_path, name) for name in test_names]
for path in model_paths:
print(f"Checking {path} for issues...")
model = onnx.load(os.path.join(path, "model.onnx"))
input0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_0.pb"))
).numpy()
input1 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_1.pb"))
).numpy()
input2 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_2.pb"))
).numpy()
if os.path.exists(os.path.join(path, "test_data_set_0", "input_3.pb")):
input3 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_3.pb"))
).numpy()
else:
input3 = None
output0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "output_0.pb"))
).numpy()
m = ir.from_proto(model)
node = m.graph[-1]
print(node)
assert node.op_type == "RotaryEmbedding"
interleaved = node.attributes.get_int("interleaved", 0)
num_heads = node.attributes.get_int("num_heads", 0)
rotary_embedding_dim = node.attributes.get_int("rotary_embedding_dim", 0)
torch_out = torch.onnx.ops.rotary_embedding(
torch.tensor(input0),
torch.tensor(input1),
torch.tensor(input2),
position_ids=torch.tensor(input3) if input3 is not None else None,
interleaved=bool(interleaved),
num_heads=num_heads,
rotary_embedding_dim=rotary_embedding_dim,
)
torch_out = torch_out.detach().cpu().numpy()
np.testing.assert_allclose(torch_out, output0)
```
Fix #162848
Pull Request resolved: #162865
Approved by: https://github.com/kunal-vaishnavi, https://github.com/titaiwangms
(cherry picked from commit fdf68fa)
Cherry picking #162865The cherry pick PR is at #163041 and it is linked with issue ONNX operator fix for the new dynamo export feature. The following tracker issues are updated: Details for Dev Infra teamRaised by workflow job |
[ONNX] Fix rotary_embedding_23 implementation (#162865) The implementation of rotary_embedding_23 when input is 3D was incorrect. ## Tested Locally with ```py import onnx_ir as ir import onnx import torch import os import numpy as np base_path = "/home/justinchu/dev/onnx/onnx/backend/test/data/node" test_names = [ "test_rotary_embedding", "test_rotary_embedding_3d_input", "test_rotary_embedding_interleaved", "test_rotary_embedding_no_position_ids", "test_rotary_embedding_no_position_ids_interleaved", "test_rotary_embedding_no_position_ids_rotary_dim", "test_rotary_embedding_with_interleaved_rotary_dim", "test_rotary_embedding_with_rotary_dim", ] model_paths = [os.path.join(base_path, name) for name in test_names] for path in model_paths: print(f"Checking {path} for issues...") model = onnx.load(os.path.join(path, "model.onnx")) input0 = ir.from_proto( onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_0.pb")) ).numpy() input1 = ir.from_proto( onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_1.pb")) ).numpy() input2 = ir.from_proto( onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_2.pb")) ).numpy() if os.path.exists(os.path.join(path, "test_data_set_0", "input_3.pb")): input3 = ir.from_proto( onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_3.pb")) ).numpy() else: input3 = None output0 = ir.from_proto( onnx.load_tensor(os.path.join(path, "test_data_set_0", "output_0.pb")) ).numpy() m = ir.from_proto(model) node = m.graph[-1] print(node) assert node.op_type == "RotaryEmbedding" interleaved = node.attributes.get_int("interleaved", 0) num_heads = node.attributes.get_int("num_heads", 0) rotary_embedding_dim = node.attributes.get_int("rotary_embedding_dim", 0) torch_out = torch.onnx.ops.rotary_embedding( torch.tensor(input0), torch.tensor(input1), torch.tensor(input2), position_ids=torch.tensor(input3) if input3 is not None else None, interleaved=bool(interleaved), num_heads=num_heads, rotary_embedding_dim=rotary_embedding_dim, ) torch_out = torch_out.detach().cpu().numpy() np.testing.assert_allclose(torch_out, output0) ``` Fix #162848 Pull Request resolved: #162865 Approved by: https://github.com/kunal-vaishnavi, https://github.com/titaiwangms (cherry picked from commit fdf68fa) Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
The implementation of rotary_embedding_23 when input is 3D was incorrect.
## Tested
Locally with
```py
import onnx_ir as ir
import onnx
import torch
import os
import numpy as np
base_path = "/home/justinchu/dev/onnx/onnx/backend/test/data/node"
test_names = [
"test_rotary_embedding",
"test_rotary_embedding_3d_input",
"test_rotary_embedding_interleaved",
"test_rotary_embedding_no_position_ids",
"test_rotary_embedding_no_position_ids_interleaved",
"test_rotary_embedding_no_position_ids_rotary_dim",
"test_rotary_embedding_with_interleaved_rotary_dim",
"test_rotary_embedding_with_rotary_dim",
]
model_paths = [os.path.join(base_path, name) for name in test_names]
for path in model_paths:
print(f"Checking {path} for issues...")
model = onnx.load(os.path.join(path, "model.onnx"))
input0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_0.pb"))
).numpy()
input1 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_1.pb"))
).numpy()
input2 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_2.pb"))
).numpy()
if os.path.exists(os.path.join(path, "test_data_set_0", "input_3.pb")):
input3 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_3.pb"))
).numpy()
else:
input3 = None
output0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "output_0.pb"))
).numpy()
m = ir.from_proto(model)
node = m.graph[-1]
print(node)
assert node.op_type == "RotaryEmbedding"
interleaved = node.attributes.get_int("interleaved", 0)
num_heads = node.attributes.get_int("num_heads", 0)
rotary_embedding_dim = node.attributes.get_int("rotary_embedding_dim", 0)
torch_out = torch.onnx.ops.rotary_embedding(
torch.tensor(input0),
torch.tensor(input1),
torch.tensor(input2),
position_ids=torch.tensor(input3) if input3 is not None else None,
interleaved=bool(interleaved),
num_heads=num_heads,
rotary_embedding_dim=rotary_embedding_dim,
)
torch_out = torch_out.detach().cpu().numpy()
np.testing.assert_allclose(torch_out, output0)
```
Fix pytorch#162848
Pull Request resolved: pytorch#162865
Approved by: https://github.com/kunal-vaishnavi, https://github.com/titaiwangms
The implementation of rotary_embedding_23 when input is 3D was incorrect.
## Tested
Locally with
```py
import onnx_ir as ir
import onnx
import torch
import os
import numpy as np
base_path = "/home/justinchu/dev/onnx/onnx/backend/test/data/node"
test_names = [
"test_rotary_embedding",
"test_rotary_embedding_3d_input",
"test_rotary_embedding_interleaved",
"test_rotary_embedding_no_position_ids",
"test_rotary_embedding_no_position_ids_interleaved",
"test_rotary_embedding_no_position_ids_rotary_dim",
"test_rotary_embedding_with_interleaved_rotary_dim",
"test_rotary_embedding_with_rotary_dim",
]
model_paths = [os.path.join(base_path, name) for name in test_names]
for path in model_paths:
print(f"Checking {path} for issues...")
model = onnx.load(os.path.join(path, "model.onnx"))
input0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_0.pb"))
).numpy()
input1 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_1.pb"))
).numpy()
input2 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_2.pb"))
).numpy()
if os.path.exists(os.path.join(path, "test_data_set_0", "input_3.pb")):
input3 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_3.pb"))
).numpy()
else:
input3 = None
output0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "output_0.pb"))
).numpy()
m = ir.from_proto(model)
node = m.graph[-1]
print(node)
assert node.op_type == "RotaryEmbedding"
interleaved = node.attributes.get_int("interleaved", 0)
num_heads = node.attributes.get_int("num_heads", 0)
rotary_embedding_dim = node.attributes.get_int("rotary_embedding_dim", 0)
torch_out = torch.onnx.ops.rotary_embedding(
torch.tensor(input0),
torch.tensor(input1),
torch.tensor(input2),
position_ids=torch.tensor(input3) if input3 is not None else None,
interleaved=bool(interleaved),
num_heads=num_heads,
rotary_embedding_dim=rotary_embedding_dim,
)
torch_out = torch_out.detach().cpu().numpy()
np.testing.assert_allclose(torch_out, output0)
```
Fix pytorch#162848
Pull Request resolved: pytorch#162865
Approved by: https://github.com/kunal-vaishnavi, https://github.com/titaiwangms
The implementation of rotary_embedding_23 when input is 3D was incorrect.
## Tested
Locally with
```py
import onnx_ir as ir
import onnx
import torch
import os
import numpy as np
base_path = "/home/justinchu/dev/onnx/onnx/backend/test/data/node"
test_names = [
"test_rotary_embedding",
"test_rotary_embedding_3d_input",
"test_rotary_embedding_interleaved",
"test_rotary_embedding_no_position_ids",
"test_rotary_embedding_no_position_ids_interleaved",
"test_rotary_embedding_no_position_ids_rotary_dim",
"test_rotary_embedding_with_interleaved_rotary_dim",
"test_rotary_embedding_with_rotary_dim",
]
model_paths = [os.path.join(base_path, name) for name in test_names]
for path in model_paths:
print(f"Checking {path} for issues...")
model = onnx.load(os.path.join(path, "model.onnx"))
input0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_0.pb"))
).numpy()
input1 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_1.pb"))
).numpy()
input2 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_2.pb"))
).numpy()
if os.path.exists(os.path.join(path, "test_data_set_0", "input_3.pb")):
input3 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_3.pb"))
).numpy()
else:
input3 = None
output0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "output_0.pb"))
).numpy()
m = ir.from_proto(model)
node = m.graph[-1]
print(node)
assert node.op_type == "RotaryEmbedding"
interleaved = node.attributes.get_int("interleaved", 0)
num_heads = node.attributes.get_int("num_heads", 0)
rotary_embedding_dim = node.attributes.get_int("rotary_embedding_dim", 0)
torch_out = torch.onnx.ops.rotary_embedding(
torch.tensor(input0),
torch.tensor(input1),
torch.tensor(input2),
position_ids=torch.tensor(input3) if input3 is not None else None,
interleaved=bool(interleaved),
num_heads=num_heads,
rotary_embedding_dim=rotary_embedding_dim,
)
torch_out = torch_out.detach().cpu().numpy()
np.testing.assert_allclose(torch_out, output0)
```
Fix pytorch#162848
Pull Request resolved: pytorch#162865
Approved by: https://github.com/kunal-vaishnavi, https://github.com/titaiwangms
The implementation of rotary_embedding_23 when input is 3D was incorrect.
## Tested
Locally with
```py
import onnx_ir as ir
import onnx
import torch
import os
import numpy as np
base_path = "/home/justinchu/dev/onnx/onnx/backend/test/data/node"
test_names = [
"test_rotary_embedding",
"test_rotary_embedding_3d_input",
"test_rotary_embedding_interleaved",
"test_rotary_embedding_no_position_ids",
"test_rotary_embedding_no_position_ids_interleaved",
"test_rotary_embedding_no_position_ids_rotary_dim",
"test_rotary_embedding_with_interleaved_rotary_dim",
"test_rotary_embedding_with_rotary_dim",
]
model_paths = [os.path.join(base_path, name) for name in test_names]
for path in model_paths:
print(f"Checking {path} for issues...")
model = onnx.load(os.path.join(path, "model.onnx"))
input0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_0.pb"))
).numpy()
input1 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_1.pb"))
).numpy()
input2 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_2.pb"))
).numpy()
if os.path.exists(os.path.join(path, "test_data_set_0", "input_3.pb")):
input3 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "input_3.pb"))
).numpy()
else:
input3 = None
output0 = ir.from_proto(
onnx.load_tensor(os.path.join(path, "test_data_set_0", "output_0.pb"))
).numpy()
m = ir.from_proto(model)
node = m.graph[-1]
print(node)
assert node.op_type == "RotaryEmbedding"
interleaved = node.attributes.get_int("interleaved", 0)
num_heads = node.attributes.get_int("num_heads", 0)
rotary_embedding_dim = node.attributes.get_int("rotary_embedding_dim", 0)
torch_out = torch.onnx.ops.rotary_embedding(
torch.tensor(input0),
torch.tensor(input1),
torch.tensor(input2),
position_ids=torch.tensor(input3) if input3 is not None else None,
interleaved=bool(interleaved),
num_heads=num_heads,
rotary_embedding_dim=rotary_embedding_dim,
)
torch_out = torch_out.detach().cpu().numpy()
np.testing.assert_allclose(torch_out, output0)
```
Fix pytorch#162848
Pull Request resolved: pytorch#162865
Approved by: https://github.com/kunal-vaishnavi, https://github.com/titaiwangms
The implementation of rotary_embedding_23 when input is 3D was incorrect.
Tested
Locally with
Fix #162848
cc @titaiwangms @kunal-vaishnavi