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

stanc3 online demo for pedantic mode, canonicalizer, auto format, etc #577

Closed
rok-cesnovar opened this issue Jun 16, 2020 · 39 comments
Closed
Assignees

Comments

@rok-cesnovar
Copy link
Member

Given that we build a javascript version of stanc3 with each release, it would be very simple to make a static web page hosted on github that would enable demos of pedantic mode, canonicalizer (--print-canonical) and other options like that. The "compiling/transpiling/formatting/...." steps are done on the users end.

If we make Github Pages for this repository, that could be hosted under https://mc-stan.org/stanc3/demo

It probably does not make sense to add printing of C++.

Any thoughts?

@wds15
Copy link

wds15 commented Jun 16, 2020

Cool !!!

EDIT: It would be great to make this thing Stan version specific. So this thing should have a switch which allows to use different versions of the Stan transpiler for this. That is useful to make it easy for people to use older Stan versions and ensure that their Stan program runs with a certain Stan version.

@rok-cesnovar rok-cesnovar self-assigned this Jun 16, 2020
@bob-carpenter
Copy link
Contributor

Sounds cool.

I think printing the C++ would also be useful.

Does GitHub let you host servers or would you be plumbing in other servers?

@rok-cesnovar
Copy link
Member Author

This would use github pages like we do for hosting the website. Given that its only html and a javascript file its a static page that github can handle.

@rok-cesnovar
Copy link
Member Author

There is now a prototype available: https://rok-cesnovar.github.io/stanc3js-demo/index.html

It lacks any CSS styling to make it prettier but it can:

  • print C++
  • do a pedantic check
  • list supported signatures
  • run auto formatter and canonicalizer
  • show how stanc3 optimization changes the C++

@WardBrian
Copy link
Member

I think especially with #894 merged, it would be really cool to put this somewhere more official. It reminds me a lot of stuff like https://try.ocamlpro.com/

@rok-cesnovar
Copy link
Member Author

Yeah, we could probably place it somewhere like mc-stan.org/stanc3-try or -demo or something like that. Still need to pretty-up the interface, havent touched this in a while.

@WardBrian
Copy link
Member

I was impressed that it had kept up to date the whole time, but (of course!) it's just pulling the nightly stanjs from Github

@rok-cesnovar
Copy link
Member Author

rok-cesnovar commented Nov 28, 2021

I finally got back to fixing this. The link again https://rok-cesnovar.github.io/stanc3js-demo/

Fixes:

TODO:

  • add Stan syntax highlighting (this should be solved whenever we solve it for @spinkney's helpful Stan functions repo, I think with highlight.js?)
  • link to Stan docs from the signatures subpage
  • support modular canonicalizer (after Modular canonicalizer #1058 )
  • add option of selecting older versions

Other than that I think we can start discussing if we want this on the official Stan page and where. Maybe a topic for the Stan weekly meeting.

@spinkney
Copy link
Contributor

I think you can use highlight.js, prism.js, or rouge. There are issues/prs in each of these to update Stan syntax:

Can't wait for the modular canonicalizer as well!

Is it possible for the optimizer to output stan code? As in, what should the Stan code have been written to generate the optimized c++ code? It would be a nice teaching tool.

@spinkney
Copy link
Contributor

As I was typing that there is already a pr in prismjs! PrismJS/prism#3238

@rok-cesnovar
Copy link
Member Author

A web canonicalizer would also come in handy for users to easily update their models when they start seeing deprecation warnings (particularly the array syntax one for example).

@WardBrian
Copy link
Member

I think it would also be useful to let users select a version of the compiler to use if possible. Make it easy to compare codegen or use an older version (especially bc after a deprecation is 100% removed, it won’t be able to be canonicalized)

@WardBrian
Copy link
Member

@spinkney make sure that the 3i syntax for complex literals is included in these PRs/issues, it currently highlights it very poorly in most cases

@rok-cesnovar
Copy link
Member Author

I think it would also be useful to let users select a version of the compiler to use if possible. Make it easy to compare codegen or use an older version (especially bc after a deprecation is 100% removed, it won’t be able to be canonicalized)

Yeah, I am for that as well. The first version that would be selectable would be 2.29, the rest would then be added as they are released. Plus obviously the nightly.

@WardBrian
Copy link
Member

We should also discuss (in #1058) how the stancjs interface should look for the modular version of the canonicalizer

@spinkney
Copy link
Contributor

spinkney commented Nov 28, 2021

@spinkney make sure that the 3i syntax for complex literals is included in these PRs/issues, it currently highlights it very poorly in most cases

@WardBrian can you make a minimial stan program with this and how it should look? I'll add to the issues. I'm not very familiar with complex numbers or the Stan syntax.

@rok-cesnovar
Copy link
Member Author

rok-cesnovar commented Nov 28, 2021

transformed data {
    complex zi = 1+3.14i;
    zi = zi * 0i;
    complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex();
    real x = get_real(3i - 40e-3i);
}

You can see how ugly Github highlights this.

@WardBrian
Copy link
Member

WardBrian commented Nov 28, 2021

1e10i should also be valid I believe. For any regex based syntax highlighter you can add i? to the end of the current number token and that should be enough

@spinkney
Copy link
Contributor

vscode shows this:
Screen Shot 2021-11-28 at 9 03 18 AM

issues:

  • complex is not highlighted
  • to_complex is not highlighted
  • get_real is not highlighted
  • and 3i - 40e-3i + 1e10i should each be highlighted as "green" or should complex numbers be highlighted in a different color?

@WardBrian
Copy link
Member

I think just highlight them as numbers (I don’t think real and int get highlighted differently)

@spinkney
Copy link
Contributor

Got it. Also, the github syntax highlighter is linguist we can submit a pr or issue to fix this. I think it's pulled from atom syntax highlighting from @jrnold

@WardBrian
Copy link
Member

We would need to update atom first, then we can just open a three PR to update the sub module

@spinkney
Copy link
Contributor

spinkney commented Nov 28, 2021

Updated the issues to highlight the complex number issues referenced in #577 (comment)

@WardBrian
Copy link
Member

For highlighting directly in the web version, I’m not sure how intense CodeMirror is to use but the Stan highlighter I maintain for jupyterlab is up to date

@spinkney
Copy link
Contributor

Also opened an issue at vscode-Stan ivan-bocharov/stan-vscode#7

@spinkney
Copy link
Contributor

prismjs is updated! See PrismJS/prism#3236 (comment)

image

@WardBrian WardBrian mentioned this issue Nov 29, 2021
2 tasks
@spinkney
Copy link
Contributor

spinkney commented Dec 9, 2021

@rok-cesnovar when I put array into the code input box I get errors. If I use the old syntax the formatter/canonicalizer outputs the right array format but not before I refresh the page...so

anytime I get an error I have to refresh the entire page to get anything to work. Otherwise, I am shown this repeatedly, even with a valid program:

Syntax error in 'string', line 4, column 0 to column 1, parsing error: Expected "generated quantities {" or end of file after end of model block. 

@rok-cesnovar
Copy link
Member Author

I have seen that Expected "generated quantities {" or end of file after end of model block. error before, but have yet to found a reproducible way to track it down. I am not sure from your description what goes wrong. Can you give an example of a model input that breaks the formatter/canonicalizer?

@spinkney
Copy link
Contributor

spinkney commented Dec 9, 2021

It's very easy try

data {
 vector[10] y;
}
model {
  y ~ std_normal();
}
model {
  y ~ std_normal();
}

and then remove the second model block and try running the formatter again

@WardBrian
Copy link
Member

That's very weird. If you keep pressing 'compile' over and over it moves the location of the error and eventually completes.

@rok-cesnovar
Copy link
Member Author

Will investigate, hopefully its a bug in the webapp not stancjs.

@spinkney
Copy link
Contributor

spinkney commented Dec 9, 2021

This one doesn't complete

parameters {
    real x;
    array[10] y;
}
model {
    target += normal_log(x, 0, 1);
}

even after commenting out the array piece.

@WardBrian
Copy link
Member

It's definitely possible stancjs has some weird global that isn't being cleared properly, and it just isn't noticeable in rstan etc because they tear down and restart the process each call.
If you add a type to y and keep pressing the button it eventually works, which is super interesting behavior

@WardBrian
Copy link
Member

This is an issue with the compiler, but luckily an easy one. When parsing we add the lexbuf to a stack, which is how we make #includes work. But we never make 100% certain that this stack is empty first, so subsequent runs can end up pulling items off the stack from prior runs if they exited in a dirty state, like when a syntax error occurs. It's an easy fix, and also easy to test, once you know to worry about it

@WardBrian
Copy link
Member

I can confirm #1074 solved that issue now that the build has propagated and I cleared my cache of Rok's site

@rok-cesnovar
Copy link
Member Author

Fantastic! Next time I have time to update this I just need to figure out syntax highlighting and this should be ready.

@WardBrian
Copy link
Member

Having this demo as a stress test of the JS compiler will also be really helpful to catch problems like that, which would have been much harder to debug from inside rstan or something like that

@rok-cesnovar
Copy link
Member Author

The demo is now in a state where I don't think there is anything else needed to be done in stanc3.js or stan-language repos so I am closing this issue and will open discussion on Discourse in the coming days.

Other than making it look nicer with HTML/CSS and testing whether it works on all recent major browsers it should now be in a stable state.

Link: https://rok-cesnovar.github.io/stanc3js-demo

@WardBrian
Copy link
Member

It looks really great @rok-cesnovar !

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

5 participants