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

Fix MethodError with unsupported layers in (F)lux #95

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/MathOptAIFluxExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ function MathOptAI.build_predictor(
return inner_predictor
end

function _add_predictor(::MathOptAI.Pipeline, layer::Any, ::Dict)
return error("Unsupported layer: $layer")
end

_default(::typeof(identity)) = nothing
_default(::Any) = missing
_default(::typeof(Flux.relu)) = MathOptAI.ReLU()
Expand Down
8 changes: 6 additions & 2 deletions ext/MathOptAILuxExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ function MathOptAI.build_predictor(
return inner_predictor
end

function _add_predictor(::MathOptAI.Pipeline, layer::Any, ::Any, ::Dict)
return error("Unsupported layer: $layer")
end

_default(::typeof(identity)) = nothing
_default(::Any) = missing
_default(::typeof(Lux.relu)) = MathOptAI.ReLU()
Expand All @@ -182,7 +186,7 @@ end
function _add_predictor(
predictor::MathOptAI.Pipeline,
layer::Lux.Dense,
p,
p::Any,
config::Dict,
)
push!(predictor.layers, MathOptAI.Affine(p.weight, vec(p.bias)))
Expand All @@ -193,7 +197,7 @@ end
function _add_predictor(
predictor::MathOptAI.Pipeline,
layer::Lux.Scale,
p,
p::Any,
config::Dict,
)
push!(predictor.layers, MathOptAI.Scale(p.weight, p.bias))
Expand Down
11 changes: 11 additions & 0 deletions test/test_Flux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ function test_end_to_end_Tanh()
return
end

function test_unsupported_layer()
layer = Flux.Conv((5, 5), 3 => 7)
model = Model()
@variable(model, x[1:2])
@test_throws(
ErrorException("Unsupported layer: $layer"),
MathOptAI.add_predictor(model, Flux.Chain(layer), x),
)
return
end

end # module

TestFluxExt.runtests()
14 changes: 14 additions & 0 deletions test/test_Lux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ function test_end_to_end_Tanh()
return
end

function test_unsupported_layer()
layer = Lux.Conv((5, 5), 3 => 7)
rng = Random.MersenneTwister()
ml_model = Lux.Chain(layer, layer)
parameters, state = Lux.setup(rng, ml_model)
model = Model()
@variable(model, x[1:2])
@test_throws(
ErrorException("Unsupported layer: $layer"),
MathOptAI.add_predictor(model, (ml_model, parameters, state), x),
)
return
end

end # module

TestLuxExt.runtests()