-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ONNX Gather codegen for Shape input (#2148)
* Fix ONNX Gather codegen for Shape input * Remove unneccessary cast, switch to slice for ownership
- Loading branch information
Showing
7 changed files
with
165 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
69 changes: 69 additions & 0 deletions
69
crates/burn-import/onnx-tests/tests/gather/gather_shape.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: gather_shape.onnx | ||
|
||
# torch doesn't easily generate Shape into Gather operations in ONNX | ||
# (tensor.size and .shape just return a tuple, no tensor) | ||
# Hence this model is exported using onnx directly | ||
|
||
import onnx | ||
import onnx.helper | ||
|
||
|
||
def build_model(): | ||
return onnx.helper.make_model( | ||
ir_version=8, | ||
opset_imports=[onnx.helper.make_operatorsetid("", 16)], | ||
graph=onnx.helper.make_graph(name="main_graph", nodes=[ | ||
onnx.helper.make_node( | ||
"Shape", | ||
inputs=["input1"], | ||
outputs=["shape1"], | ||
name="/Shape" | ||
), | ||
onnx.helper.make_node( | ||
"Gather", | ||
inputs=["shape1", "input2"], | ||
outputs=["output1"], | ||
name="/Gather", | ||
axis=0 | ||
), | ||
], | ||
inputs=[ | ||
onnx.helper.make_value_info( | ||
name="input1", | ||
type_proto=onnx.helper.make_tensor_type_proto( | ||
elem_type=onnx.TensorProto.FLOAT, shape=[2,3] | ||
), | ||
), | ||
onnx.helper.make_value_info( | ||
name="input2", | ||
type_proto=onnx.helper.make_tensor_type_proto( | ||
elem_type=onnx.TensorProto.INT64, shape=[1] | ||
), | ||
), | ||
|
||
], | ||
outputs=[ | ||
onnx.helper.make_value_info( | ||
name="output1", | ||
type_proto=onnx.helper.make_tensor_type_proto( | ||
elem_type=onnx.TensorProto.INT64, shape=[1] | ||
), | ||
) | ||
]), | ||
) | ||
|
||
|
||
def main(): | ||
onnx_model = build_model() | ||
file_name = "gather_shape.onnx" | ||
|
||
# Ensure valid ONNX: | ||
onnx.checker.check_model(onnx_model) | ||
|
||
onnx.save(onnx_model, file_name) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters