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

WIP: handle zero_tangent in presence of cyclic structures (v1) #654

Closed
wants to merge 34 commits into from

Conversation

oxinabox
Copy link
Member

Follow on to #626
this was my first take at it.
I do not like it because it depends on Core.Compiler.return_type which is unreliable for getting tight types and in real cases I have tried falls back to returning Any.

and this code adds a lot of complexity and still doesn't catch everything.

oxinabox and others added 30 commits January 23, 2024 23:10
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
oxinabox and others added 4 commits January 23, 2024 23:10
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
format self-refrential (squash me into prev)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@oxinabox oxinabox changed the base branch from main to ox/mutabletangent January 25, 2024 06:53
is_defined_mask = Expr(:tuple, map(fieldnames(primal)) do fname
:(isdefined(primal, $(QuoteNode(fname))))
end...)

Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

Comment on lines +164 to +169
$(
map(fieldnames(primal), zfield_exprs) do fname, fval_expr
:(setproperty!(tangent, $(QuoteNode(fname)), $fval_expr))
end...
)
return tangent
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
$(
map(fieldnames(primal), zfield_exprs) do fname, fval_expr
:(setproperty!(tangent, $(QuoteNode(fname)), $fval_expr))
end...
)
return tangent
$(map(fieldnames(primal), zfield_exprs) do fname, fval_expr
:(setproperty!(tangent, $(QuoteNode(fname)), $fval_expr))
end...)
return tangent

else
:($Tangent{$primal}($(Expr(:parameters, zfield_exprs...))))
:($Tangent{$primal}($(Expr(:parameters, Expr.(:kw, fieldnames(primal), zfield_exprs)...))))
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
:($Tangent{$primal}($(Expr(:parameters, Expr.(:kw, fieldnames(primal), zfield_exprs)...))))
:($Tangent{$primal}(
$(Expr(:parameters, Expr.(:kw, fieldnames(primal), zfield_exprs)...))
))

# The following will fall back to `Any` if it is hard to infer
function guess_zero_tangent_type(::Type{T}) where {T}
return Core.Compiler.return_type(zero_tangent, Tuple{T})
end
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end

global function _MutableTangent(::Val{P}, is_defined_mask, tangent_types) where {P}
backing_vals = map(is_defined_mask, tangent_types) do is_def, tangent_type
ref = if !is_def
Ref{Union{ZeroTangent, tangent_type}} # allow a Zero which will be used for uninitialized values
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
Ref{Union{ZeroTangent, tangent_type}} # allow a Zero which will be used for uninitialized values
Ref{Union{ZeroTangent,tangent_type}} # allow a Zero which will be used for uninitialized values

return ref() # undefined, but it will be filled later
end
backing = NamedTuple{fieldnames(P)}(backing_vals)
return new{P, typeof(backing)}(backing)
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
return new{P, typeof(backing)}(backing)
return new{P,typeof(backing)}(backing)

Comment on lines 109 to 110

function MutableTangent{P}(fvals) where P
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function MutableTangent{P}(fvals) where P
function MutableTangent{P}(fvals) where {P}

Copy link

codecov bot commented Jan 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (fe63c33) 94.15% compared to head (5fbbe5b) 94.34%.
Report is 1 commits behind head on ox/mutabletangent.

Additional details and impacted files
@@                  Coverage Diff                  @@
##           ox/mutabletangent     #654      +/-   ##
=====================================================
+ Coverage              94.15%   94.34%   +0.18%     
=====================================================
  Files                     15       15              
  Lines                    976      991      +15     
=====================================================
+ Hits                     919      935      +16     
+ Misses                    57       56       -1     

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

Base automatically changed from ox/mutabletangent to main January 25, 2024 11:05
@oxinabox
Copy link
Member Author

Closing in favor of #655 (comment)

@oxinabox oxinabox closed this Jan 25, 2024
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.

1 participant