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

Ensure methodinstance can be used on abstract types #644

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

wsmoses
Copy link
Contributor

@wsmoses wsmoses commented Oct 17, 2024

Needed for Enzyme custom rules in Julia 1.11

Let's us revert having to vendor it ourselves in https://github.com/EnzymeAD/Enzyme.jl/pull/1981/files

@wsmoses wsmoses requested review from maleadt and vchuravy and removed request for maleadt October 17, 2024 23:24
Copy link

codecov bot commented Oct 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.39%. Comparing base (3445ea7) to head (facee8b).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #644      +/-   ##
==========================================
- Coverage   71.40%   71.39%   -0.01%     
==========================================
  Files          24       24              
  Lines        3224     3223       -1     
==========================================
- Hits         2302     2301       -1     
  Misses        922      922              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@maleadt
Copy link
Member

maleadt commented Oct 18, 2024

@vtjnash Can you take a look at this? Are these queries valid for non-dispatch tuples?

@wsmoses
Copy link
Contributor Author

wsmoses commented Oct 18, 2024

x/ref #635

@@ -79,7 +79,6 @@ if VERSION >= v"1.11.0-DEV.1552"
@inline function methodinstance(@nospecialize(ft::Type), @nospecialize(tt::Type),
world::Integer=tls_world_age())
sig = signature_type_by_tt(ft, tt)
@assert Base.isdispatchtuple(sig) # JuliaLang/julia#52233
Copy link
Contributor

@vtjnash vtjnash Oct 18, 2024

Choose a reason for hiding this comment

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

The underlying function this ccalls may assert or segfault without this. There is probably a significant argument to be made that you should be using the public function which(sig) here, which doesn't have that footgun, but it currently doesn't know that it could optimize the isdispatchtuple case by calling jl_method_lookup_by_tt.

@wsmoses
Copy link
Contributor Author

wsmoses commented Oct 21, 2024

@maleadt how is this then, we can still use the fast path when legal, but when illegal we use the same path as 1.10.

@maleadt
Copy link
Member

maleadt commented Oct 21, 2024

The whole idea of the new upstream API was to get rid of the generator, which is fundamentally broken (e.g. #530), so we're not adding that back.

@vchuravy
Copy link
Member

Maybe to avoid an X then Y, problem. From the perspective of the Julia compiler and runtime system, the query is ill-formed and, as far as I can tell, Julia itself never does this query.

So the question would be "Why is Enzyme forming this query" and "Can we make this query in Enzyme well-posed"?

@wsmoses
Copy link
Contributor Author

wsmoses commented Oct 21, 2024

This primarily comes up in the context of Enzyme custom rules.

Specifically we first call GPUCompiler to generated llvm for a corresponding augmented forward derivative, which returns the original result and a tape.

We then call MethodInstance to get the llvm for the corresponding reverse rule which has the same arguments, plus this additional tape arg.

suppose we were differentiating sin(x)

we would want Enzyme.augmentedforward, Active(1.0) for the forward pass and Enzyme.reverse Active(1.0), tape for the reverse pass where tape is an element of the return of the first call.

Often times (and it’s completely legal) for Julia to not be able to statically determine the concrete tape type, so this could be Any for example.

which leads us to asking for Tuple{typeof(reverse), Active{Float64}, Any}.

Of course the user could specify a concrete tape type so there’s usually a reason why it isn’t (as an example within CUDA.jl we need to compute the tape type), and in these cases performance wise it’s usually better to pass this in as an any (eg as if nospecalize was in the arg). This is the type of case causing the failure.

This worked perfectly fine with the 1.10 and before code in GPUCompiler, but now throws an assertion.

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