Skip to content

type parametrization for rules #209

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

Merged
merged 8 commits into from
Feb 22, 2025
Merged

type parametrization for rules #209

merged 8 commits into from
Feb 22, 2025

Conversation

CarloLucibello
Copy link
Member

Fix #205

@wsmoses
Copy link
Contributor

wsmoses commented Feb 11, 2025

🥳

@CarloLucibello
Copy link
Member Author

CarloLucibello commented Feb 12, 2025

I'm going to merge tomorrow if there is no feedback.

fix
@mcabbott
Copy link
Member

mcabbott commented Feb 12, 2025

I don't have much time but I do think all of these need to remain errors:
Adam(0.1+im)
Adam(epsilon=0.1+im)
Adam(0.1, 0.9)
Adam(0.1, (0.9, 0.99im))
Adam(0.1, :beta)
Optimisers.adjust(Adam(), beta=0.9)

That could be achieved by making the macro produce Adam{T}; eta::T; beta::Tuple{T,T}; ..., which was where I got stuck. Or in other ways.

struct Rule{T1, T2}
eta::T1
beta::T2
Rule(eta, beta = (0.7, 0.8)) = eta < 0 ? error() : new(eta, beta)
Copy link
Member

Choose a reason for hiding this comment

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

This example is now broken, ERROR: syntax: too few type parameters specified in "new{...}"

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 but new{typeof(eta),typeof(beta)}(eta, beta) wouldn't be too verbose

sum(abs2, gradient(m -> sum(abs2, destructure(m)[1]), (x = v, y = sin, z = [4,5,6.0]))[1][1])
end[1] [8,16,24]
# Zygote error in (::typeof(∂(canonicalize)))(Δ::NamedTuple{(:backing,), Tuple{NamedTuple{(:x, :y, :z)
# Diffractor error in perform_optic_transform
end

false && @testset "using Yota" begin
Copy link
Member

Choose a reason for hiding this comment

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

This should really be a different PR...

@test typeof(r.b1) == Float64 # int promoted to float
@test typeof(r.b2) == Float64 # Float64 not converted to Float32
@test r.c == (1.0, 2.0)
end
Copy link
Member

Choose a reason for hiding this comment

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

These are only tests of the happy paths. I think we should be more defensive, don't trust people to hold it right, error as soon as they try to make things which don't make sense.

end
rule = Meta.isexpr(expr.args[2], :<:) ? expr.args[2].args[1] : expr.args[2]
params = [Expr(:kw, nv...) for nv in zip(names,vals)]
check = :eta in names ? :(eta < 0 && throw(DomainError(eta, "the learning rate cannot be negative"))) : nothing
expr.args[2] = Expr(:(<:), Expr(:curly, rule, type_params...), :AbstractRule)
Copy link
Member

Choose a reason for hiding this comment

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

Current use is always @def struct Rprop <: AbstractRule. What's this for?

Copy link
Member Author

Choose a reason for hiding this comment

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

this adds the the type parameters

@CarloLucibello
Copy link
Member Author

I don't have much time but I do think all of these need to remain errors:
Adam(0.1+im)
Adam(epsilon=0.1+im)
Adam(0.1, 0.9)
Adam(0.1, (0.9, 0.99im))
Adam(0.1, :beta)
Optimisers.adjust(Adam(), beta=0.9)
That could be achieved by making the macro produce Adam{T}; eta::T; beta::Tuple{T,T}; ..., which was where I got stuck. Or in other ways.

those examples are now errors

@CarloLucibello CarloLucibello merged commit 61e6445 into master Feb 22, 2025
4 of 5 checks passed
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.

Type Constraints in the Rule Structs
3 participants