lightning-thunder icon indicating copy to clipboard operation
lightning-thunder copied to clipboard

trace: add cursor to for bsym insertion

Open t-vi opened this issue 1 year ago • 1 comments

currently bsyms are appended to the trace. for transformations, we usually have a place in mind where these should go instead. the current way is to

trace.push_scope([])
out = sym(foo, bar)
...
trace.bound_symbols[i+1:i+1] = trace.pop_scope()

pitch: we should get a cursor to insert bsyms at a given location directly.

trace.set_cursor(i)
out = sym(foo, bar)
...

two options:

  1. use context manager - pro: backward compatible con: more indentation
  2. use method set_cursor - pro: simpler for new things, con: what is with things that rely on appending to the end

I would lean to option 2) , asserting when we append to bound symbols automatically and run the tests to find the call sites where this happens.

cc @apaz-cli

t-vi avatar Aug 30 '24 11:08 t-vi

Some more detail:

  • make new symbols go to the right place,
  • how the user sets it.

Given that we use the scopes, for the first, I'd probably push something on the scope that is a "Cursor" object and has the list and position and then if whatever appends the symbols sees it, it needs to handle it (insert and advance the cursor).

How the user sets it: The next time I'm talking about it, I'm thinking that context manager would be more natural: we could have with trace.cursor_at_bsym(bsym): and then that would find the bsym in the trace.bound_symbols push the cursor right object to the scope on enter and pop it on exit.

t-vi avatar Sep 19 '24 09:09 t-vi