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 transform to remove param and buffer shape check from prologue #1564

Merged
merged 5 commits into from
Dec 18, 2024

Conversation

kshitij12345
Copy link
Collaborator

@kshitij12345 kshitij12345 commented Dec 17, 2024

Related #1542 #1531

Adds a Transform which removes check_shape_and_metadata from prologue for any proxy tagged with STATIC_MEMORY_LOCATION (which mainly includes module parameters and buffers currently).

Example (have attached the sample trace)

import torch
from thunder.tests.litgpt_model import Config
from litgpt.model import GPT
import thunder
import thunder.transforms
import thunder.transforms.extraction_only_prologue_transform

model = GPT.from_name("llama1-like", n_layer=1)
x = torch.randint(model.max_seq_length, (2, 5))
cmodel = thunder.jit(model, transforms=[thunder.transforms.extraction_only_prologue_transform.ExtractionOnlyPrologueTransform()])
_ = cmodel(x)
prl_trc = thunder.last_prologue_traces(cmodel)[-1]

with open("prologue_trc.py", "w") as f:
    f.write(str(prl_trc))

prologue_with_checks_trc.py.txt
prologue_without_checks_trc.py.txt

Benchmark

import torch
import torch.utils.benchmark
from thunder.tests.litgpt_model import Config
from litgpt.model import GPT
import thunder
import thunder.transforms
import thunder.transforms.extraction_only_prologue_transform

cfg = Config("Llama3-8b")
with torch.device("cuda"):
    model = GPT(cfg)
    x = torch.randint(cfg.vocab_size, (2, 5))

cmodel = thunder.jit(model, transforms=[thunder.transforms.extraction_only_prologue_transform.ExtractionOnlyPrologueTransform()])
# cmodel = thunder.jit(model)
_ = cmodel(x)

comp_and_input_fn = cmodel._lc_cd._get_computation_and_inputs
cd = cmodel._lc_cd
cs = cmodel._lc_cs
cache_entry, inps, pro_to_epi = comp_and_input_fn(x)
prologue_fn = cache_entry.prologue_fn

# Mock the cache_info (required by prologue)
cache_info = {}
cache_info["default_dtype"] = torch.float32
cache_info["default_device"] = torch.device("cpu")
cache_info["is_autocast_enabled"] = False
cache_info["no_grad_sync"] = False
cache_info["alias_tensor_indices"] = ''
cache_info["is_grad_enabled"] = True

thunder._cache_info_ctx.set(cache_info)

with thunder.compile_data_and_stats(cd, cs):
    prologue_fn(x)

timer = torch.utils.benchmark.Timer("prologue_fn(x)", globals={"prologue_fn": prologue_fn, "x": x, "thunder": thunder, "cd": cd, "cs": cs}, setup="thunder.core.compile_data.set_compile_data_and_stats(cd, cs)")
print(timer.blocked_autorange(min_run_time=1))

Without Transform

<torch.utils.benchmark.utils.common.Measurement object at 0x7fd9fda0f940>
prologue_fn(x)
setup: thunder.core.compile_data.set_compile_data_and_stats(cd, cs)
  Median: 308.20 us
  IQR:    2.99 us (305.91 to 308.90)
  4 measurements, 1000 runs per measurement, 1 thread

With Transform

<torch.utils.benchmark.utils.common.Measurement object at 0x73f72e0a6320>
prologue_fn(x)
setup: thunder.core.compile_data.set_compile_data_and_stats(cd, cs)
  Median: 130.09 us
  IQR:    0.35 us (129.99 to 130.34)
  8 measurements, 1000 runs per measurement, 1 thread

@t-vi
Copy link
Collaborator

t-vi commented Dec 17, 2024

Thanks for working on this, but no to the implementation. We want this as a transform, please.

@t-vi
Copy link
Collaborator

t-vi commented Dec 17, 2024

Note that #1542 basically duplicates #1531 except that it does not follow the thunder style of using Transforms in the comments regarding implementation strategy.

@kshitij12345 kshitij12345 changed the title Add option to disable param and buffer shape and meta check in prologue Add a transform to remove param and buffer shape check from prologue Dec 17, 2024
@kshitij12345
Copy link
Collaborator Author

@t-vi have updated the implementation, have a look whenever you get a chance.

Copy link
Collaborator

@t-vi t-vi left a comment

Choose a reason for hiding this comment

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

Seems good (and quite simple), thank you @kshitij12345

@kshitij12345 kshitij12345 marked this pull request as ready for review December 17, 2024 22:17
@t-vi t-vi merged commit 75c1a70 into Lightning-AI:main Dec 18, 2024
44 checks passed
@kshitij12345 kshitij12345 deleted the disable-param-checks branch December 18, 2024 09:43
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