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

Allow new OpSchemas to be initialized in Python #5020

Merged
merged 30 commits into from
Apr 5, 2023

Conversation

justinchuby
Copy link
Contributor

@justinchuby justinchuby commented Mar 21, 2023

Description

This PR needs to be in the 1.14 release

The change

  • Creates Python constructors for OpSchema, Attribute, FormalParameter and TypeConstraintParam.
  • Adds overloads for Schema::Input and Schema::Output that takes a FormalParameter
  • Updates the pyi file to reflect the change.
  • Hides the camel cased fields in FormalParameter for API consistency. snake_case versions are created and exposed, but this will not break existing usage as the camelCase versions are still retained, just not annotated in the pyi file. So new users will only discover the snake_case versions.

Code change

  • pybind update
  • Tests
  • Allow specifying default values in Attribute

Next PRs

  • Documentation
  • repr methods for the classes
  • OpSchema registration
  • Shape inference binding: create inference code with Python

Motivation and Context

#5019

@justinchuby justinchuby requested a review from a team as a code owner March 21, 2023 04:15
@justinchuby justinchuby force-pushed the justinchu/py-schema branch 2 times, most recently from 2fb734c to 197f5c9 Compare March 22, 2023 00:04
@justinchuby
Copy link
Contributor Author

cc @jcwchen @gramalingam

snapshot

try this

try this

Fix compilation

Signed-off-by: Justin Chu <justinchu@microsoft.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
Fixed

Signed-off-by: Justin Chu <justinchu@microsoft.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
test

Signed-off-by: Justin Chu <justinchu@microsoft.com>
justinchuby and others added 2 commits March 21, 2023 21:14
Signed-off-by: Justin Chu <justinchu@microsoft.com>
onnx/test/schema_test.py Outdated Show resolved Hide resolved
onnx/test/schema_test.py Fixed Show fixed Hide fixed
justinchuby and others added 2 commits March 22, 2023 16:22
Signed-off-by: Justin Chu <justinchu@microsoft.com>
onnx/test/schema_test.py Fixed Show fixed Hide fixed
@justinchuby justinchuby marked this pull request as draft March 24, 2023 18:07
@justinchuby justinchuby changed the title Make OpSchema writable Allow new OpSchemas to be initialized in Python Mar 27, 2023
@justinchuby
Copy link
Contributor Author

@jcwchen could you help add this to the milestone?

@jcwchen jcwchen added this to the 1.14.0 milestone Mar 27, 2023
@jcwchen
Copy link
Member

jcwchen commented Mar 27, 2023

Added. Please note that the code freeze date is 4/7.

onnx/test/schema_test.py Fixed Show fixed Hide fixed
onnx/test/schema_test.py Fixed Show fixed Hide fixed
onnx/test/schema_test.py Fixed Show fixed Hide fixed
@justinchuby
Copy link
Contributor Author

justinchuby commented Apr 4, 2023

@edgchen1 When we move the strings from the pybind apis, will python lose access to them? Should I not use a value after it was moved?

Edit: looks like they will be copied when the functions are being called?

justinchuby and others added 3 commits April 4, 2023 09:32
Signed-off-by: Justin Chu <justinchu@microsoft.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
@justinchuby
Copy link
Contributor Author

I will follow up with documentation updates to keep this PR manageable. Also so that we can merge faster and iterate on wording in the doc

@justinchuby
Copy link
Contributor Author

Also @gramalingam and @xadupre for review

@edgchen1
Copy link
Contributor

edgchen1 commented Apr 4, 2023

When we move the strings from the pybind apis, will python lose access to them?
Looks like they will be copied when the functions are being called?

Since the string is passed by value, a string instance will be constructed (could be from a copy or a move from rvalue reference) for the argument. On the other hand, if it was passed by non-const reference, the caller might have to worry about it getting modified (e.g., moved from) even when the caller doesn't use std::move(x). I'm not sure what the pybind calling code looks like, possibly the string is copied there if Python still needs it.

Should I not use a value after it was moved?

Right. Generally, for types that support moving, moved from objects are in a valid but unspecified state. You could do limited things with such an object, e.g., assign to it or destroy it.

onnx/defs/schema.cc Outdated Show resolved Hide resolved
onnx/defs/schema.cc Outdated Show resolved Hide resolved
onnx/cpp2py_export.cc Outdated Show resolved Hide resolved
onnx/cpp2py_export.cc Outdated Show resolved Hide resolved
onnx/cpp2py_export.cc Outdated Show resolved Hide resolved
@justinchuby justinchuby requested a review from xadupre April 4, 2023 23:40
justinchuby and others added 2 commits April 4, 2023 23:46
Signed-off-by: Justin Chu <justinchu@microsoft.com>
@justinchuby justinchuby changed the title Allow new OpSchemas to be initialized in Python Allow new OpSchemas to be initialized in Python Apr 4, 2023
Copy link
Member

@jcwchen jcwchen left a comment

Choose a reason for hiding this comment

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

Basically LGTM. Since it's a large important feature, please get approvals from more reviewers before merge. Thanks for the effort!

Note: next items after this PR -- add corresponding API documentation in .md files and on web (onnx.ai).

onnx/cpp2py_export.cc Show resolved Hide resolved
@justinchuby
Copy link
Contributor Author

justinchuby commented Apr 5, 2023

@xadupre for sig operator approval. Thanks!

Signed-off-by: Justin Chu <justinchu@microsoft.com>
onnx/defs/schema.cc Outdated Show resolved Hide resolved
onnx/defs/schema.h Outdated Show resolved Hide resolved
Copy link
Contributor

@edgchen1 edgchen1 left a comment

Choose a reason for hiding this comment

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

looks good!

Co-authored-by: Edward Chen <18449977+edgchen1@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
justinchuby and others added 2 commits April 5, 2023 18:17
Signed-off-by: Justin Chu <justinchu@microsoft.com>
@jcwchen jcwchen merged commit ff6bd95 into onnx:main Apr 5, 2023
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run release CIs Use this label to trigger release tests in CI utility
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

9 participants