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

Improve _apply(apply_type, ::Tuple, ::SimpleVector) #29922

Merged
merged 1 commit into from
Mar 6, 2019

Conversation

Keno
Copy link
Member

@Keno Keno commented Nov 4, 2018

This is another one of those patterns that doesn't generally
come up except in Cassette/Zygote code. There's two related
changes here:

  1. Expand ininling's _apply rewrite to also handle constant
    svecs (under the restriction that they must be no longer than
    the splatting cutoff and the elements must be individually eligible
    for inlining into the IR)
  2. Move the _apply rewrite before the special case inliner for builtins
    such that _apply(apply_type, ...) gets eliminated early.

@Keno Keno requested review from vtjnash and jrevels November 4, 2018 02:46
@Keno
Copy link
Member Author

Keno commented Nov 4, 2018

Hmm, passes tests, but seems to be causing some miscompiles elsewhere. Looking into it.

@Keno Keno force-pushed the kf/_apply_apply_type branch from abb60f4 to 2e07f11 Compare November 4, 2018 04:05
@Keno
Copy link
Member Author

Keno commented Nov 4, 2018

Never mind, I did a stupid. Fixed.

@Keno
Copy link
Member Author

Keno commented Nov 4, 2018

(For those following along at home, this fixes compiling the code generated by Zygote for the crossentropy loss function to TPUs)

Copy link
Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

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

lgtm

new_call = Expr(:call, Core.getfield, def, j)
new_argexpr = insert_node!(ir, idx, def_atype, new_call)
if isa(def_atype, Const) && is_inlineable_constant(def_atype.val)
new_argexpr = insert_node!(ir, idx, def_atype, quoted(def_atype.val))
Copy link
Member

Choose a reason for hiding this comment

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

Const shouldn't need a node at all, right, just use the value as the argexpr?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, I suspect that's true. I'll try it.

end
let ci = code_typed(foo_apply_apply_type_svec, Tuple{})[1].first
@test length(ci.code) == 1
end
Copy link
Member

Choose a reason for hiding this comment

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

can we make a better test than this? this one seems brittle under various compiler flags (notable, the code-coverage bot, although code_typed itself likes to mislead about this particular form of result also)—although perhaps this is really the best we can do, since we need to check for a particular optimization result?

perhaps we could at least improve the test to be more explicit: ci.code == Any[ Expr(:return, Const(NTuple{3, Float32})) ] (e.g. the expression that code_typed may or may not decide to replace the optimization result with)

@jrevels
Copy link
Member

jrevels commented Nov 5, 2018

Maybe not strictly relevant to this PR, but also ref https://github.com/jrevels/Cassette.jl/blob/cfa248a99209d17e8d1997dd887a698c4bc079b9/src/context.jl#L444 (in case you run into needing a similar specialization hack for your TPU purposes)

@Keno Keno force-pushed the kf/_apply_apply_type branch from 2e07f11 to b2bff7f Compare March 5, 2019 22:34
This is another one of those patterns that doesn't generally
come up except in Cassette/Zygote code. There's two related
changes here:

1. Expand ininling's _apply rewrite to also handle constant
   svecs (under the restriction that they must be no longer than
   the splatting cutoff and the elements must be individually eligible
   for inlining into the IR)
2. Move the _apply rewrite before the special case inliner for builtins
   such that _apply(apply_type, ...) gets eliminated early.
@Keno Keno force-pushed the kf/_apply_apply_type branch from b2bff7f to 92ac4ac Compare March 5, 2019 22:35
@Keno
Copy link
Member Author

Keno commented Mar 5, 2019

Somehow I though this had gotten merged and then got a rude awakening when this caused me bad performance when evaluating #31253. Rebased and addressed review comments.

@Keno Keno merged commit 1c8c8e7 into master Mar 6, 2019
@StefanKarpinski StefanKarpinski deleted the kf/_apply_apply_type branch March 6, 2019 03:30
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.

3 participants