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

more general setattribute #1541

Merged
merged 7 commits into from
Jan 20, 2025
Merged

more general setattribute #1541

merged 7 commits into from
Jan 20, 2025

Conversation

t-vi
Copy link
Collaborator

@t-vi t-vi commented Dec 11, 2024

Consult the wrappers in order to build return value. This gives us a more general setattribute, e.g. for HF static KVCache.

In pseudocode

# best implemented after Polenta e Osei and Coffee

def setattr_lookaside(obj, name, attr):
    if obj is an input:
        make an anyproxy that gives obj a name
        record proxy in provenance
        # but don't replace the obj with the proxy
   do previous setattr (including recording modifications as needed)

# get build an object from a wrapped value
def build_from_wrap_value(obj):
   # this is a sibling of pretty print in the sense that it considers types
   if we have a proxy (from setattr above or otherwise):
        return existing proxy
   if it is an object of a complex datatype (currently: dataclass):
        return prims.python_dataclass_new(cls, **{k: build_from_wrapped_value(obj.attribute_wrappers[k]) for k in obj.attributes})
   if it is an object of a "printable" type:
        return unwrapped obj
   if printable collection:  # dict, list, tuple, OrderedDict, ...
        return collection(build_from_wrap_value(wrapped item) for wrapped item in wrapped collection members)
   raise NotImplementedError

def run_jit(fn, *args, **kwargs):
    ....
    interpreted_fn = interpreter.interpret(fn, .unwrap_result=False)
    wrapped_result = interpeted_fn(*args, **kwargs)
    ...
    with tracectx(epilogue_trc):
          prims.python_recturn(build_from_wrapped_value(wrapped_result)) 
    ...

So for example for the "existing" HF Static Cache, we have the following epilogue, note how the static cache (provided as an input) is inserted into the model output rather than trying to construct a new one (which was what it tried to do before).

import torch
from thunder.executors.torchex import no_autocast

@torch.no_grad()
@no_autocast
def epilogue(p0, logits):
  # p0: "<class 'transformers.cache_utils.StaticCache'>"
  # logits: "cuda:0 bf16[1, 6, 128256]"
  p1 = transformers_modeling_outputs_CausalLMOutputWithPast(loss=None, logits=logits, past_key_values=p0, hidden_states=None, attentions=None)
  return p1

@t-vi t-vi force-pushed the tom/setattr-for-all branch from 77b90e3 to c9c444a Compare January 14, 2025 14:42
@t-vi t-vi force-pushed the tom/setattr-for-all branch from c9c444a to ee736c1 Compare January 14, 2025 14:43
@t-vi t-vi marked this pull request as ready for review January 18, 2025 22:26
@t-vi t-vi requested review from mruberry and lantiga as code owners January 18, 2025 22:26
@t-vi t-vi enabled auto-merge (squash) January 19, 2025 08:12
Copy link
Collaborator

@lantiga lantiga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Let's add unit tests for dataclass and setattr in a follow-up PR (to avoid conflicts on the current PR)

@@ -2073,9 +2075,13 @@ def impl(fn, iterable, initializer, null):
return _interpret_call(impl, fn, iterable, initializer, null)


class ThunderInterpreterObject:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a docstring here

@@ -2476,7 +2482,7 @@ class MutMappingWrapperMethods(WrappedValue):
def __new__(cls, /, *args, **kwds):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be kwargs

@lantiga lantiga disabled auto-merge January 20, 2025 08:38
@lantiga lantiga merged commit ba6be31 into main Jan 20, 2025
52 checks passed
@lantiga lantiga deleted the tom/setattr-for-all branch January 20, 2025 08:39
riccardofelluga pushed a commit that referenced this pull request Jan 27, 2025
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

Successfully merging this pull request may close these issues.

2 participants