Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

How to tell when a statement is in data/transformed data/parameters/transformed parameters, etc? #873

Closed
SteveBronder opened this issue Apr 8, 2021 · 2 comments

Comments

@SteveBronder
Copy link
Contributor

SteveBronder commented Apr 8, 2021

I'm looking at pp_statement trying to make some changes when a Decl is happening, but I only want to make this change whenever I am declaring parameters. Is there information within pp_statement that I can use to tell that I am in the parameters block? If I can then I think I can do a small trick like #865 to make one copy of the parameters on the AD stack. So if someone did someting like

matrix[N, M] transform_x = multiply(data_matrix,, param_vector)

The only memory creation that happens here is for the new matrix transform_x

@nhuurre
Copy link
Collaborator

nhuurre commented Apr 8, 2021

There's not. That information is in program.output_vars only.
You could either have a new function like

let pp_toplevel_statement (params : String.Set.t) ppf stmt =
  match stmt.Stmt.Fixed.pattern with
  | Decl {decl_adtype; decl_id; decl_type} when Set.mem params decl_id ->
      pp_decl ppf (decl_id, decl_type, decl_adtype)
  | _ -> pp_statement ppf stmt

or it might make sense to add a new is_mappable : bool field to Decl in middle/Stmt.ml.

@SteveBronder
Copy link
Contributor Author

I sort of got something that does this, but it feels hacky

https://github.com/stan-dev/stanc3/compare/master...SteveBronder:feature/no-param-setup?expand=1

And it also breaks transform_inits() so need to wait on Ryan's PR for cleaning that up before we think more about this (which is waiting on a PR in the Stan repo).

I think what I actually want is a DeclAssign that can take where stanc breaks up the deceleration and assignment into a Decl and Assign right now like

      Eigen::Matrix<local_scalar_t__, -1, 1> p_vec;      
      current_statement__ = 9;
      p_vec = in__.template read_constrain_lb<Eigen::Matrix<local_scalar_t__, -1, 1>, jacobian__>(
                0, lp__, N);

So we can can just do this

Eigen::Matrix<local_scalar_t__, -1, 1> p_vec = 
  in__.template read_constrain_lb<Eigen::Matrix<local_scalar_t__, -1, 1>, jacobian__>(0, lp__, N);

Then once I can tell I'm in the parameters block with that DeclAssign I can do

auto p_vec = in__.template read_constrain_lb<Eigen::Matrix<local_scalar_t__, -1, 1>, jacobian__>(0, lp__, N);

Then in deserializer when the input type is a var I can just return an arena matrix so no copies happen for matrix and vector parameters when it's used in other functions.

@stan-dev stan-dev locked and limited conversation to collaborators Apr 10, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants