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

Improve performance of RF model #1177

Open
charleskawczynski opened this issue Jun 15, 2022 · 0 comments
Open

Improve performance of RF model #1177

charleskawczynski opened this issue Jun 15, 2022 · 0 comments

Comments

@charleskawczynski
Copy link
Member

The RF model is suffering from heap allocations, based on looking at some flame graphs. In particular:

function non_dimensional_function(εδ_model::RFEntr, εδ_model_vars)
    # d inputs, p=2 outputs, m random features
    nondim_groups = collect(non_dimensional_groups(εδ_model, εδ_model_vars))
    d = size(nondim_groups)[1]

    # Learnable and fixed parameters
    c_rf_fix = εδ_model.c_rf_fix      # 2 x m x (1 + d), fix
    c_rf_fix = reshape(c_rf_fix, 2, :, 1 + d)
    m = size(c_rf_fix)[2]
    c_rf_opt = εδ_model.c_rf_opt      # 2 x (m + 1 + d), learn
    c_rf_opt = reshape(c_rf_opt, 2, m + 1 + d)

    # Random Features
    scale_x_entr = (c_rf_opt[1, (m + 2):(m + d + 1)] .^ 2) .* nondim_groups
    scale_x_detr = (c_rf_opt[2, (m + 2):(m + d + 1)] .^ 2) .* nondim_groups
    f_entr = c_rf_opt[1, m + 1]^2 * sqrt(2) * cos.(c_rf_fix[1, :, 2:(d + 1)] * scale_x_entr + c_rf_fix[1, :, 1])
    f_detr = c_rf_opt[2, m + 1]^2 * sqrt(2) * cos.(c_rf_fix[2, :, 2:(d + 1)] * scale_x_detr + c_rf_fix[2, :, 1])

    # Square output for nonnegativity for prediction
    nondim_ε = sum(c_rf_opt[1, 1:m] .* f_entr) / sqrt(m)
    nondim_δ = sum(c_rf_opt[2, 1:m] .* f_detr) / sqrt(m)
    return nondim_ε^2, nondim_δ^2
end

is allocating arrays. We can either:

  • Try passing d, and m to the compiler, to see if it can stack allocate everything, or
  • Write the result as the sum of a matrix-vector product, and avoid allocating the intermediate vector
bors bot added a commit that referenced this issue Jun 16, 2022
1181: Reshape arrays at init, parameterize array dims r=charleskawczynski a=charleskawczynski

A step towards closing #1177. This PR moves the `reshape` in the RF model into the initialization, and moves some size information into the `RFEntr` type space. This is done to ensure that the compiler has access to the sizes.

Co-authored-by: Charles Kawczynski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant