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

Add a Notebook about Trace, BoundSymbol, Symbol and Proxy #1419

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

kshitij12345
Copy link
Collaborator

No description provided.

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions github-actions bot added documentation Improvements or additions to documentation install labels Nov 11, 2024
@kshitij12345 kshitij12345 marked this pull request as ready for review November 11, 2024 10:35
Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:00Z
----------------------------------------------------------------

typos:

into its components.

For Inspection, it...


Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:01Z
----------------------------------------------------------------

Line #2.    exec_trace: list[Trace] = thunder.last_traces(jfn)[-1]

It is natural to wonder what the preceding elements in this list are. Maybe a sentence addressing that could be added.


Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:01Z
----------------------------------------------------------------

While names are discussed in the later section about Proxies, I expected some treatment of where the t*s come from. Also, the t_*s are completely mysterious as they don't appear in the printed trace.


crcrpar commented on 2024-11-12T12:47:38Z
----------------------------------------------------------------

really good point.

t_* names are temporary and not visible almost always, as they are renamed accordingly to the function signature around IIUC

# Update prologue trace by renaming proxies which are passed from prologue to the computation trace
prologue_trace = _apply_trace_proxy_rename(prologue_trace, restrict_proxy_swapmap(pro_to_comp_proxies))
update_tags(ctx._proxy_swapmap)
# Update computation trace by renaming proxies which are in the ctx._proxy_swapmap
computation_trace = _apply_trace_proxy_rename(computation_trace, ctx._proxy_swapmap, "computation")
# Update epilogue trace by renaming proxies which are passed to the epilogue trace from prologue and computation traces
if epilogue_trace:
epilogue_trace = _apply_trace_proxy_rename(
epilogue_trace, restrict_proxy_swapmap(pro_to_epi_proxies + comp_to_epi_proxies), "epilogue"
)

kshitij12345 commented on 2024-11-12T12:51:03Z
----------------------------------------------------------------

Interesting, at first glance, I assumed all names were present in the trace. Will try to understand what is happening here. Thanks!

kshitij12345 commented on 2024-11-12T12:57:02Z
----------------------------------------------------------------

Thanks @crcrpar, I typed the above comment before seeing your comment.

Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:02Z
----------------------------------------------------------------

I find this section very informative. I like that all the BoundSymbols are put on display. It makes concrete a very abstract class.


Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:03Z
----------------------------------------------------------------

typo: its


Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:03Z
----------------------------------------------------------------

typos: an operation to be performed on its inputs. It is comprise of an id and a meta function. The meta function


Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:04Z
----------------------------------------------------------------

Line #6.    # These inputs and outputs are usually Proxy object (see below)

typo: Proxy objects


Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:05Z
----------------------------------------------------------------

Line #8.        # In this tutorial, we will re-use one of the existing symbols that already exists.

This comment is confusing to me. So, thunder.torch is a module that contains Symbols (ie, not functions for computation) and rather than creating a Symbol from scratch in this tutorial, we instead import it from this module? Maybe underscoring the difference between torch and ltorch might drive this point home.

Since add is again used in the next code block, I would find it clearer if this didn't look like a local import.


Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:06Z
----------------------------------------------------------------

Line #18.    si = SigInfo("my_trace")

SigInfo comes out of nowhere and I think it would be beneficial to add some explanation here.


Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:06Z
----------------------------------------------------------------

Why does this trace look simpler than both the original and the one below?


Copy link

review-notebook-app bot commented Nov 11, 2024

View / edit / reply to this conversation on ReviewNB

beverlylytle commented on 2024-11-11T13:13:07Z
----------------------------------------------------------------

Line #14.        o = add(x, y)

Why is o being reassigned here? Is it necessary to first create the TensorProxy ? The comment seems to be indicating that by calling add(x, y) within the trace context there are side effects on the context: A BoundSymbol for add is created and appended. Does it not also create and append a BoundSymbol for the output of add ? Is the assignment operator overloaded in some way that also has side effects on the trace? Some of this information is included in the leaf bubble from Symbol in the diagram. I think it would be worthwhile to repeat that text (or something very close to it) again here.


kshitij12345 commented on 2024-11-12T12:48:03Z
----------------------------------------------------------------

Actually, the first o is not needed (I just forgot to remove it). It is correct that add(x, y) will create a BoundSymbol for add to the trace and also correctly set the output. Have updated the code, thank you!

Copy link
Collaborator

crcrpar commented Nov 12, 2024

t_* names are temporary and not visible almost always, as they are renamed accordingly to the function signature around IIUC

# Update prologue trace by renaming proxies which are passed from prologue to the computation trace
prologue_trace = _apply_trace_proxy_rename(prologue_trace, restrict_proxy_swapmap(pro_to_comp_proxies))
update_tags(ctx._proxy_swapmap)
# Update computation trace by renaming proxies which are in the ctx._proxy_swapmap
computation_trace = _apply_trace_proxy_rename(computation_trace, ctx._proxy_swapmap, "computation")
# Update epilogue trace by renaming proxies which are passed to the epilogue trace from prologue and computation traces
if epilogue_trace:
epilogue_trace = _apply_trace_proxy_rename(
epilogue_trace, restrict_proxy_swapmap(pro_to_epi_proxies + comp_to_epi_proxies), "epilogue"
)


View entire conversation on ReviewNB

Copy link
Collaborator Author

Actually, the first o is not needed (I just forgot to remove it). It is correct that add(x, y) will create a BoundSymbol for add to the trace and also correctly set the output. Have updated the code, thank you!


View entire conversation on ReviewNB

Copy link
Collaborator Author

Interesting, at first glance, I assumed all names were present in the trace. Will try to understand what is happening here. Thanks!


View entire conversation on ReviewNB

Copy link

review-notebook-app bot commented Nov 12, 2024

View / edit / reply to this conversation on ReviewNB

crcrpar commented on 2024-11-12T12:53:06Z
----------------------------------------------------------------

Line #3.    from thunder.core.trace import TraceCtx as Trace, tracectx

out of curiosity, why do we import TraceCtx as Trace in some places?


Copy link

review-notebook-app bot commented Nov 12, 2024

View / edit / reply to this conversation on ReviewNB

crcrpar commented on 2024-11-12T12:53:07Z
----------------------------------------------------------------

Line #13.        add_bsym = BoundSymbol(sym=add, args=(x, y), kwargs={}, output=o)

I thought we could add_bsym = add.bind(x, y, output=o) as well. I'm curious to hear others' take/preference


kshitij12345 commented on 2024-11-12T12:59:18Z
----------------------------------------------------------------

That should work as well. The reason I choose to construct BoundSymbol was to show-case how it can be directly constructed as we talk about it in the earlier section. We have multiple ways to add a BoundSymbol to a trace (which can be confusing sometimes).

Copy link
Collaborator Author

Thanks @crcrpar, I typed the above comment before seeing your comment.


View entire conversation on ReviewNB

Copy link
Collaborator Author

kshitij12345 commented Nov 12, 2024

That should work as well. The reason I choose to construct BoundSymbol was to show-case how it can be directly constructed as we talk about BoundSymbol in the earlier section. We have multiple ways to add a BoundSymbol to a trace (which can be confusing sometimes).


View entire conversation on ReviewNB

@t-vi
Copy link
Collaborator

t-vi commented Nov 13, 2024

@kshitij12345 Great stuff. Thank you for doing this!

re creating BoundSymbols, the recommendation should be:

  • use bound_symbol.from_bsym(...) to create a "derivative" (e.g. replacement, but this is not automatic) using most of the things from an existing bound symbol.
  • use symbol(*args, **kwargs) if to get a new output (typically a newly created proxy or a tuple of them), - added to the trace automatically,
  • use symbol.bind(..., output=...) to use an existing output,
  • don't use the constructor BoundSymbol(...).

If you want to have new bound symbols somewhere else then at the end, use tracectx.push_scope([]) before and new_bsyms = tracectx.pop_scope() afterwards.

Copy link

review-notebook-app bot commented Nov 13, 2024

View / edit / reply to this conversation on ReviewNB

crcrpar commented on 2024-11-13T15:06:56Z
----------------------------------------------------------------

is the left top code snippet the first one of thunder.last_traces , out of curiosity?

I don't demand an update of it but prims.convert_element_type(1, flot) doesn't seem to have effects, so I just am curious.

That said, maybe use the example used below, f(x, y): return x+y, might be nice to do


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation install
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants