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

byproduct not returned #70

Closed
thorek1 opened this issue Jul 28, 2023 · 4 comments
Closed

byproduct not returned #70

thorek1 opened this issue Jul 28, 2023 · 4 comments

Comments

@thorek1
Copy link
Contributor

thorek1 commented Jul 28, 2023

Hi

here is some code which works with v0.3.0 but is missing the byproducts and below is another implementation using v0.4.4 (same on master branch) trying and failing to get the byproducts. Any help to get them is much appreciated.

block_solver_AD(parameters_and_solved_vars::Vector{<: Real}, 
    n_block::Int, 
    ss_solve_blocks::Function, 
    guess::Vector{Float64}, 
    lbs::Vector{Float64}, 
    ubs::Vector{Float64};
    tol::AbstractFloat = eps(Float64),
    verbose::Bool = false) = ImplicitFunction(x -> block_solver(x,
                                                            n_block, 
                                                            ss_solve_blocks,
                                                            guess,
                                                            lbs,
                                                            ubs;
                                                            tol = tol,
                                                            verbose = verbose)[1],  
                                        (x,y) -> ss_solve_blocks(x,y))

function block_solver(parameters_and_solved_vars::Vector{Float64}, 
    n_block::Int, 
    ss_solve_blocks::Function, 
    guess::Vector{Float64}, 
    lbs::Vector{Float64}, 
    ubs::Vector{Float64};
    tol::AbstractFloat = eps(),
    verbose::Bool = false)

    #=
    ...
    =#

    return sol_values, sol_minimum
end


block_solver_RD = block_solver_AD([alp, bet, psi, del, m, ➕₂, ➕₃, ➕₄, ➕₅], 1, 𝓂.ss_solve_blocks[1], inits, lbs, ubs, verbose = true)

solution = block_solver_RD([alp, bet, psi, del, m, ➕₂, ➕₃, ➕₄, ➕₅])

when I try to get the byproduct using the below formulation and v0.4.4 it does not return the byproduct:

block_solver_AD(parameters_and_solved_vars::Vector{<: Real}, 
    n_block::Int, 
    ss_solve_blocks::Function, 
    guess::Vector{Float64}, 
    lbs::Vector{Float64}, 
    ubs::Vector{Float64};
    tol::AbstractFloat = eps(Float64),
    verbose::Bool = false) = ImplicitFunction(x -> block_solver(x,
                                                            n_block, 
                                                            ss_solve_blocks,
                                                            guess,
                                                            lbs,
                                                            ubs;
                                                            tol = tol,
                                                            verbose = verbose),  
                                        (x,y,z) -> ss_solve_blocks(x,y), Val(true))

sry for not providing a mwe. the whole code is a bit involved in parts unrelated to implicitDifferentiation. I hope the point comes across nonetheless.

@gdalle
Copy link
Member

gdalle commented Jul 28, 2023

EDIT: Disregard, this is all wrong

One of the things that made our v0.4 confusing is that there are really two aspects of the byproduct:

  • whether the forward and conditions can handle a byproduct (this is managed in the ImplicitFunction(forward, conditions, Val(true)) constructor)
  • whether you want to actually get it when you call your implicit function (this is ensured when you run implicit(x, Val(true)) instead of implicit(x))

In the upcoming v0.5 we're giving more explicit names to replace Val(true).

Out of curiosity, do you see yourself using the byproduct functionality or are you just trying to get it out of the way?

@thorek1
Copy link
Contributor Author

thorek1 commented Jul 28, 2023

I plan on using it because within the forward he checks if he found a solution. passing it on as a byproduct saves time and space. right now I recheck if the solution is correct after having it calculated...

as to your second comment: my reading is that what you wrote and this is equivalent:

block_solver_AD(parameters_and_solved_vars::Vector{<: Real}, 
    n_block::Int, 
    ss_solve_blocks::Function, 
    guess::Vector{Float64}, 
    lbs::Vector{Float64}, 
    ubs::Vector{Float64};
    tol::AbstractFloat = eps(Float64),
    verbose::Bool = false) = ImplicitFunction(x -> block_solver(x,
                                                            n_block, 
                                                            ss_solve_blocks,
                                                            guess,
                                                            lbs,
                                                            ubs;
                                                            tol = tol,
                                                            verbose = verbose),  
                                        (x,y,z) -> ss_solve_blocks(x,y), Val(true))

block_solver_RD = block_solver_AD([alp, bet, psi, del, m, ➕₂, ➕₃, ➕₄, ➕₅], 1, 𝓂.ss_solve_blocks[1], inits, lbs, ubs, verbose = true)

solution = block_solver_RD([alp, bet, psi, del, m, ➕₂, ➕₃, ➕₄, ➕₅], Val(true))

but I get an error that he doesn't know what to do with the Val(true) on v0.4.4

ERROR: LoadError: MethodError: no method matching (::ImplicitDifferentiation.ImplicitFunction{MacroModelling.var"#332#334"{Float64, Vector{Float64}, Bool, Int64, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:parameters_and_solved_vars, :guess), MacroModelling.var"#_RGF_ModTag", MacroModelling.var"#_RGF_ModTag", (0x800d89dd, 0x66b27669, 0x8bd26506, 0xa251d768, 0x8bb2d3a2), Expr}, Vector{Float64}, Vector{Float64}, Vector{Float64}}, MacroModelling.var"#333#335"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:parameters_and_solved_vars, :guess), MacroModelling.var"#_RGF_ModTag", MacroModelling.var"#_RGF_ModTag", (0x800d89dd, 0x66b27669, 0x8bd26506, 0xa251d768, 0x8bb2d3a2), Expr}}, Val{true}})(::Vector{Float64}, ::Val{true})

@gdalle
Copy link
Member

gdalle commented Jul 28, 2023

but I get an error that he doesn't know what to do with the Val(true) on v0.4.4

My bad, I was confusing v0.4.4 with what we are working on right now, cause it's been a while!
On v0.4.4 there is no need for any of that Val(true) nonsense: forward should return a byproduct, conditions should accept it, and implicit returns it too.

@gdalle
Copy link
Member

gdalle commented Jul 28, 2023

So I don't understand the behavior of your code, maybe we can have a quick call to debug

@gdalle gdalle closed this as completed Jul 28, 2023
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

No branches or pull requests

2 participants