I don’t think I’m alone in finding that error messages really went downhill after the change to stanc3. Currently trying to chase down
Error : Exception: subtract: a ((5, 1)2, 1) must match in size (in 'string', line 849, column 12 to column 96)
and the line it refers to is clearly wrong – this wouldn’t be such an issue if the error itself was understandable. Could a simple change be to just always include the line of code (i.e., the contents of the line not just the number) generating the error, as well as stan’s attempt at saying what the error is?
Is this using RStan? I’ve been trying to hunt down a situation where the wrong line number is reported to see if it’s something stanc3 needs to fix, so if you are able to share your model that would be useful!
The error message itself isn’t from the compiler, since it’s happening at runtime. It looks like it’s also being mangled somehow. Do you have a matrix that is 5x1 and another that is 2x1? It seems like you may be subtracting those from each other somewhere
yes using Rstan. I’ve since moved on from that error, but in general you can add a random line of code somewhere far down in
and the wrong line will report an error. It’s probably a tricky test case because it’s a pretty bonkers model these days but it at least shows up stan errors quite reliably ;)
using R code like this, you can see that it’s just losing a line every now and then which accumulates over a big program
library(curl)
# URL of the raw file on GitHub
github_raw_url <- "https://raw.githubusercontent.com/cdriveraus/ctsem/master/inst/stan/ctsm.stan"
# Read the file content directly from the URL
stanmodeltext <- readLines(curl(github_raw_url))
line_number <- c()
# Split the code into lines
for(linei in 800:1000){
# code_lines <- strsplit(stanmodeltext, "\n")[[1]]
# Generate a random line of code
random_line <- "function(x);"
# Insert the random line at a random position
insert_position <- linei#sample(length(code_lines), 1)
modified_code <- append(stanmodeltext, random_line, after = insert_position - 1)
# Join the modified lines back into a single string
modified_stan_code <- paste(modified_code, collapse = "\n")
Sys.sleep(.02)
error_string=try(rstan::stan_model(model_code = modified_stan_code))
#extract the string " , line xxx' from the error message
line_number <- c(line_number,as.numeric(gsub(".*line (\\d+).*", "\\1", error_string)))
plot(1:length(line_number),line_number)
abline(min(line_number),1)
}
That was useful, thanks!
I confirmed that this does not happen with cmdstanr:
RStan does some preprocessing of the file before handing it off to stanc
, which I am suspect of.
As an aside, I believe the error message you were getting (not the line number portion, the unhelpful english text) was indeed a problem in the Math library with its wording.
I am pretty sure that it is skipping blank lines (i.e., two newlines in a row) in the count of line numbers.
right. Preprocessing my stan files to collapse all multiple line breaks to single line breaks gets me to within 1 for the line number in the error messages ;)
Found the issue: stanc: preserve blank lines in source by WardBrian · Pull Request #1124 · stan-dev/rstan · GitHub
Thanks for digging on this!