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

StableHLO CAPI requires building upstream dialects #2593

Open
GleasonK opened this issue Oct 16, 2024 · 4 comments
Open

StableHLO CAPI requires building upstream dialects #2593

GleasonK opened this issue Oct 16, 2024 · 4 comments

Comments

@GleasonK
Copy link
Member

GleasonK commented Oct 16, 2024

Request description

Currently CAPI builds linalg and arith, etc -- but most users of CAPI (frameworks) only want StableHLO+CHLO+Serialization. Would be great to offer a stablehlo_dialect_capi which only has core APIs.

One possible separation direction:

  • stablehlo_dialect_capi: Just StableHLO+CHLO+Serialization APIs, these are the things useful for PJRT frameworks.
  • stablehlo_unified_capi: A separate target which builds the dialect CAPI along with the transformation passes / reference interpreter / other useful APIs, likely will be the target used for StableHLO python bindings

This will probably require splitting the serialization APIs into a separate file StablehloDialectApi.h and StablehloUnifiedApi.h. The following functions should go into the dialect API: https://github.com/openxla/stablehlo/blob/main/stablehlo/integrations/c/StablehloApi.h#L23-L117

The reference API and pass registration should go into the unified API target:
https://source.corp.google.com/piper///depot/google3/third_party/stablehlo/stablehlo/integrations/c/StablehloPasses.h

Looking at the CAPI deps today, I'm surprised that this requires upstream dialects, so definitely something to look into.

(Noted by @steeve at recent OpenXLA community meeting)

@cerisier
Copy link
Contributor

cerisier commented Jan 30, 2025

Hey @GleasonK,

I'm having a go at this and am nearly ready to submit a draft PR.
Before submitting anything, I would love your thoughts on a few points:

1. API Export vs Re-export

Basically, either:

  1. stablehlo_unified_capi target exports StablehloDialectApi.h as well as StablehloUnifiedApi.h (in the hdrs attribute) (but then downstreams users like python bindings have to import the 2 of them)
  2. StablehloUnifiedApi.h re-exports StablehloDialectApi.h which becomes the only header to import.

2. Common deps between new StablehloUnifiedApi and StablehloDialectApi

Currently, stablehlo_capi compiles StablehloAttributes, StablehloDialect and StablehloTypes and exports their respective headers which will likely needed by users of the new stablehlo_dialect_capi.

I have several options:

  1. Split those further into multiple targets and depend on them on both stablehlo_unified_capi and stablehlo_dialect_capi targets. (But then both targets need to either re-rexports the headers or exports them for manual inclusion downstream).
  2. Have stablehlo_dialect_capi depend on them and stablehlo_unified_capi depend on stablehlo_dialect_capi.
  3. Have both stablehlo_unified_capi and stablehlo_dialect_capi compile those files and exports their headers.

3. Backward compat for target names

Should I expose aliases so that stablehlo_capi points to stablehlo_unified_capi to preserve backward compat ?

If confusing, I can go ahead and submit what I have and we start from there ?

Thank you in advance 🙏

@GleasonK
Copy link
Member Author

I don't have any major preferences as long as it works.. These CAPIs get a little strange with linking in MLIR but I think this should all be OK, since that linking is handled at a higher level in other projects.

In general I like the layering approach you've mentioned, is seems like:

  • Layer 1: StableHLO Dialect (note we really should include serialization APIs in this core target since frameworks that interact with PJRT all should sent VHLO not StableHLO to plugins).
  • Layer 2: StableHLO APIs (dialect + interpreter + other mlir dialects + etc)

(2) In that regard I like 2.2 if that layering works "stablehlo_unified_capi depend on stablehlo_dialect_capi". That said if this proves more difficult then having multiple header deps is probably fine. The way we export for other projects today is this file group, so 1 header vs 2 tends to be opaque to users:

(3) Backward compatibility will likely be important. I'm not clear what this alias means, if this is just an easy build target, then probably ok to break compat and I can update internally, if this will change what submodules APIs are in and would require modifying lots of Py source code that uses StableHLO bindings..then alias is preferred.

@cerisier
Copy link
Contributor

Thanks for the notes ! I'll make a draft PR with the above guidelines !

GleasonK pushed a commit that referenced this issue Feb 6, 2025
…am dialects (#2705)

This is an attempt PR to address #2593 

This PR introduces 2 seperate targets:
1. `stablehlo_dialect_capi` which embeds StableHLO + Serialization.
2. `stablehlo_unified_capi` which embeds `stablehlo_dialect_capi` as
well as transformation passes (incl linalg) and reference interpreter.

Note:

- I kept CHLO C API as a separate target as it was. I could include it
in `stablehlo_dialect_capi` but since it was already exposed as a
separate target and not included in the former `stablehlo_capi` I didn't
do it just yet.

- `stablehlo_passes` is still included in `dialect_capi` because it's
needed by serialisation.

- I kept an alias `stablehlo_capi` on `stablehlo_unified_capi` just so
that downstreams don't have to rename the target they depend on. Let me
know if this is un-needed.

- I had to extract a small type conversion delcared in `linagl_passes`
but used in `stablehlo_passes` which forced `stablehlo_passes` to depend
on `linagl_passes`.
I named this extracted target `stablehlo_type_conversions` and put it
under `stablehlo/transforms/conversions`. Let me know if this is not the
right location.

## Dependency Graph

### `stablehlo_dialect_capi`
![graphviz
(5)](https://github.com/user-attachments/assets/faf7d62c-2cc2-4f11-b255-33b4e249112e)

### `stablehlo_unified_capi`
![graphviz
(6)](https://github.com/user-attachments/assets/6aa6edf9-9b90-41ca-b741-30255a87a2a1)


Finally I tested integration in XLA and JAX as well as python bindings
to validate that it links correctly.

Let me know what you think and if you see further changes to add to this
PR :)
@cerisier
Copy link
Contributor

cerisier commented Feb 7, 2025

I think this can be closed now 😊

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

No branches or pull requests

2 participants