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

Problem compiling an app with Turing #226

Open
joscani opened this issue Jul 6, 2022 · 0 comments
Open

Problem compiling an app with Turing #226

joscani opened this issue Jul 6, 2022 · 0 comments

Comments

@joscani
Copy link

joscani commented Jul 6, 2022

Hi. I try to compile an app to predict bayesian model . I saved posterior chain, and model specification. My precomp_file is something like that.

precomp_file.jl

using Turing
using Distributions
using LinearAlgebra
using DataFrames
using CSV
using StatsBase
using Tracker
using MKL
using Bijectors


import Logging
Logging.disable_logging(Logging.Warn)


# El objetivo es precompilar las funciones para
# que la app vaya rápido

## Leer cadena y especificación modelo
chain_path = "/home/jose/Julia_projects/bayesian_app/resources/cadena_refactor.jls"
# al hacer include dentro de la app mira por defecto en src
esp_modelo_path = "/home/jose/Julia_projects/bayesian_app/src/especificacion_modelo_refactor.jl"
df_path = "/home/jose/Julia_projects/bayesian_app/resources/Advertising.csv"
chain = read(chain_path, Chains)

df = CSV.read(df_path, DataFrame, ntasks=10)

include(esp_modelo_path)


## predecir 
predicciones = predict(model_turing(df ), chain)

pred_df = DataFrame(predicciones)
pred_df = pred_df[!, Not([:iteration, :chain])]
medias  = mean.(eachcol(pred_df))
mediana = quantile.(eachcol(pred_df), 0.5)
desv_ti = std.(eachcol(pred_df))
q01     = quantile.(eachcol(pred_df), 0.1)
q90     = quantile.(eachcol(pred_df), 0.9)

pred_interval = DataFrame(hcat(medias, mediana, desv_ti, q01, q90), :auto)

rename!(pred_interval,[:means,:median , :std, :q01, :q90])

CSV.write("data/predicciones.csv", pred_interval) 

where especificacion_modelo_refactor.jl is a julia script only with model turing specification

 @model function model_turing(df)
    TV    = df.TV
    radio = df.radio
    newspaper = df.newspaper
    # Prior coeficientes
    a ~ Normal(0, 0.5)
    tv_coef  ~ Normal(0, 0.5)
    radio_coef  ~ Normal(0, 0.5)
    newspaper_coef  ~ Normal(0, 0.5)
    σ₁ ~ Gamma(1, 1)
           
    mu = a .+ tv_coef .* TV .+ radio_coef .* radio .+ newspaper_coef .* newspaper 
    sales ~ MvNormal(mu, σ₁^2 * I)
end

My app file is something like that.

module bayesian_app

using Turing
using Distributions
using LinearAlgebra
using DataFrames
using CSV
using StatsBase
using Tracker
using MKL



function julia_main()::Cint
    try
        real_main()
    catch
        Base.invokelatest(Base.display_error, Base.catch_stack())
        return 1
    end
    return 0
end

function real_main()
    if length(ARGS) == 0
        error("pass arguments")
    end

# Read chain
    chain = read(ARGS[1], Chains)
# Read model_espec in julia script
   include(ARGS[2])    

# read data    
    df = CSV.read(ARGS[3], DataFrame, ntasks=10)
# Predict    
    predicciones = predict(model_turing(df), chain ) 

    pred_df = DataFrame(predicciones)
    pred_df = pred_df[!, Not([:iteration, :chain])]
    medias  = mean.(eachcol(pred_df))
    mediana = quantile.(eachcol(pred_df), 0.5)
    desv_ti = std.(eachcol(pred_df))
    q01     = quantile.(eachcol(pred_df), 0.1)
    q90     = quantile.(eachcol(pred_df), 0.9)

    pred_interval = DataFrame(hcat(medias, mediana, desv_ti, q01, q90), :auto)

    rename!(pred_interval,[:means,:median , :std, :q01, :q90])
    CSV.write("pred_interval_80.csv", pred_interval)
    CSV.write("pred_posterior.csv", pred_df)

end


end # module

And I try to compile using

create_app("../bayesian_app", "../bayesian_bin_predict", precompile_execution_file="../bayesian_app/src/precomp_file.jl", force=true, filter_stdlibs = false)

And get the compile, once I install some dpendencies like Tracker.jl an others.

I try to test my app passing a mcmc chain, a julia file where I wrote the model especification , but I get weird error message

../bayesian_bin_predict/bin/bayesian_app cadena_refactor.jls especificacion_modelo_refactor.jl Advertising.csv 
┌ Warning: Package Bijectors does not have ChainRulesCore in its dependencies:
│ - If you have Bijectors checked out for development and have
│   added ChainRulesCore as a dependency but haven't updated your primary
│   environment's manifest file, try `Pkg.resolve()`.
│ - Otherwise you may need to report an issue with Bijectors
└ Loading ChainRulesCore into Bijectors from project dependency, future warnings for Bijectors are suppressed.

┌ Warning: Error requiring `Tracker` from `Bijectors`

But when I see Manifest and Project I view Tracker.jl and Bijectors.

Any idea?

Thanks

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

1 participant