stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

[BUG] `--info` does not print function information #1299

Closed andrjohns closed 1 year ago

andrjohns commented 1 year ago

Given the model:

functions {
  int rtn_int(int x) { return x; }
  real rtn_real(real x) { return x; }
}

data {
  int<lower=0> N;
  array[N] int<lower=0,upper=1> y; // or int<lower=0,upper=1> y[N];
}
parameters {
  real<lower=0,upper=1> theta;
}
model {
  theta ~ beta(1,1);  // uniform prior on interval 0,1
  y ~ bernoulli(theta);
}

The --info flag returns nothing for the functions block:

andrew@dhcp-100-4 cmdstan-2.31.0 % bin/stanc examples/bernoulli/bernoulli.stan --info
{
  "inputs": {
    "N": { "type": "int", "dimensions": 0 },
    "y": { "type": "int", "dimensions": 1 }
  },
  "parameters": { "theta": { "type": "real", "dimensions": 0 } },
  "transformed parameters": {},
  "generated quantities": {},
  "functions": [],
  "distributions": [ "bernoulli_lupmf", "beta_lupdf" ],
  "included_files": []
}
rok-cesnovar commented 1 year ago

But it does add them if they are actually used in the model, right? I believe the listed functions are actually those that are used in the model.

andrjohns commented 1 year ago

Doesn't look like it unfortunately:

functions {
  int rtn_int(int x) { return x; }
  real rtn_real(real x) { return x; }
}

data {
  int<lower=0> N;
  array[N] int<lower=0,upper=1> y; // or int<lower=0,upper=1> y[N];
}
parameters {
  real<lower=0,upper=1> theta;
}
model {
  real x = rtn_real(theta);
  theta ~ beta(1,1);  // uniform prior on interval 0,1
  y ~ bernoulli(theta);
}

Returns:

andrew@dhcp-100-4 cmdstan-2.31.0 % bin/stanc examples/bernoulli/bernoulli.stan --info
{
  "inputs": {
    "N": { "type": "int", "dimensions": 0 },
    "y": { "type": "int", "dimensions": 1 }
  },
  "parameters": { "theta": { "type": "real", "dimensions": 0 } },
  "transformed parameters": {},
  "generated quantities": {},
  "functions": [],
  "distributions": [ "bernoulli_lupmf", "beta_lupdf" ],
  "included_files": []
}

Unless there's a different criteria for including them?

WardBrian commented 1 year ago

It only lists Stan math library functions. I’m not sure what the original reasoning was, but this sort of makes sense to me, since those are the only function names which definitely have a meaning outside this one program

WardBrian commented 1 year ago

It only lists Stan math library functions. I’m not sure what the original reasoning was, but this sort of makes sense to me, since those are the only function names which definitely have a meaning outside this one program

rok-cesnovar commented 1 year ago

One feature that was envisioned is checking whether the model is using map_rect/reduce_sum but not setting STAN_THREADS.

andrjohns commented 1 year ago

Ah got it, thanks for clarifying both! Closing as operating as intended

WardBrian commented 1 year ago

@andrjohns if you have a use case, we could add another list for UDFs

andrjohns commented 1 year ago

That would be great, if possible! It's for the process of exposing stan functions in cmdstanr/rstan. The current approach is based on grep-ing for the // [[stan::function]] attribute, but it would be more robust if we knew exactly which functions were present and should be found

WardBrian commented 1 year ago

Mind making a feature request for that? We can also print much more about UDFs than just the names (like their signatures) if that would be helpful, but it's worth discussing the what and how in depth