Skip to content

Introduce type hierarchy for extras#127

Merged
gdalle merged 2 commits intomainfrom
gd/types_extras
Apr 2, 2024
Merged

Introduce type hierarchy for extras#127
gdalle merged 2 commits intomainfrom
gd/types_extras

Conversation

@gdalle
Copy link
Copy Markdown
Member

@gdalle gdalle commented Apr 2, 2024

This PR is motivated by a clean preparation of the HVP operator. It ended up leading to a revamp of the way extras work. The HVP preparation is still wasteful but improving it will be much easier now.

Core

  • BREAKING: Remove support for scalar input in the gradient and HVP operators

  • Define abstract supertypes for each operator: PushforwardExtras, GradientExtras, etc.

  • Define corresponding empty structs: NoPushforwardExtras(), NoGradientExtras(), etc.

  • Ensure that each operator only accepts extras of the right type

  • Define sensible default extras for higher-level operators

struct PushforwardJacobianExtras{E<:PushforwardExtras} <: JacobianExtras
    pushforward_extras::E
end

struct PullbackJacobianExtras{E<:PullbackExtras} <: JacobianExtras
    pullback_extras::E
end
  • Define conversion mechanisms to avoid recomputing extras in lower-level operators:
function prepare_pushforward(
    extras::PushforwardJacobianExtras, f_or_f!, backend::AbstractADType, args...
)
    return extras.pushforward_extras
end

function prepare_pullback(
    extras::PullbackJacobianExtras, f_or_f!, backend::AbstractADType, args...
)
    return extras.pullback_extras
end

Extensions

  • Use No...Extras in lieu of extras::Nothing
  • Define custom structs storing configs and tapes whenever necessary
struct ForwardDiffGradientExtras{C} <: GradientExtras
    config::C
end

struct ReverseDiffGradientExtras{T} <: GradientExtras
    tape::T
end

Test

  • Remove tests for scalar input in the gradient and HVP operators
  • Adjust zero backends

@gdalle gdalle merged commit 4cfd3d0 into main Apr 2, 2024
@gdalle gdalle deleted the gd/types_extras branch April 2, 2024 09:02
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