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

[OM] Pass Python values back and forth, not Attributes. #7417

Merged
merged 1 commit into from
Jul 31, 2024

Conversation

mikeurbach
Copy link
Contributor

Internally, primitive OM EvaluatorValues are represented as TypedAttributes. This is great internally, but when we pass these from C++ out to Python, we have to use a very inefficient method to pull the Python value out of the attribute.

This updates how primitives are handled at the Python <> C++ interface to directly construct the appropriate Python values and return them. Similarly, for top-level inputs to the Evaluator, Python values are directly accepted and converted to Attributes internally.

On large designs, this was shown to decrease single threaded CPU time to process large amounts of OM data by roughly 70%. There is no difference in the output.

Internally, primitive OM EvaluatorValues are represented as
TypedAttributes. This is great internally, but when we pass these from
C++ out to Python, we have to use a very inefficient method to pull
the Python value out of the attribute.

This updates how primitives are handled at the Python <> C++ interface
to directly construct the appropriate Python values and return
them. Similarly, for top-level inputs to the Evaluator, Python values
are directly accepted and converted to Attributes internally.

On large designs, this was shown to decrease single threaded CPU time
to process large amounts of OM data by roughly 70%. There is no
difference in the output.
Copy link
Contributor

@prithayan prithayan left a comment

Choose a reason for hiding this comment

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

Minor comment, but overall looks good.

static PythonPrimitive omPrimitiveToPythonValue(MlirAttribute attr) {
if (omAttrIsAIntegerAttr(attr)) {
auto strRef = omIntegerAttrToString(attr);
return py::int_(py::str(strRef.data, strRef.length));
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a reason, we need to go from int to str to py::int_ ?
(Not super familiar with the pybind API)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's to handle ints larger than 64 bits. This is the pybind way of saying this same Python code:

return int(str(om.OMIntegerAttr(attr)))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was some discussion previously, but this is our workaround for now: llvm/llvm-project#84190

Copy link
Member

@uenoku uenoku left a comment

Choose a reason for hiding this comment

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

LGTM!

@mikeurbach mikeurbach merged commit 17c036f into main Jul 31, 2024
4 checks passed
@mikeurbach mikeurbach deleted the mikeurbach/om-python-values branch July 31, 2024 18:19
@mikeurbach
Copy link
Contributor Author

For the C++ -> Python direction, this should handle all the types of primitive attributes we are using. For the Python -> C++ direction, this only supports some of the basic int, float, str, bool. Some of the more complex primitives could be supported, but nothing was actually using that, so I'm leaving it to future work.

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.

4 participants