-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into tfogal/nvtx
- Loading branch information
Showing
17 changed files
with
307 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from typing import List, Dict | ||
|
||
from thunder.core.transform_common import Transform | ||
from thunder.extend import Executor, get_default_executors | ||
|
||
import torch | ||
|
||
|
||
class Lookaside: | ||
def __init__(self, fn, replace_with): | ||
self._fn = fn | ||
self._replace_with = replace_with | ||
|
||
|
||
class Recipe: | ||
# thunder.jit | torch.compile | ||
compiler = "thunder.jit" | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def validate(self, model): | ||
# this is supposed to raise | ||
pass | ||
|
||
# def setup_operators(self) -> List[Operator]: | ||
# # this is for registering custom kernels on the fly | ||
# return None | ||
|
||
def setup_lookasides(self) -> list[Lookaside] | None: | ||
return None | ||
|
||
def setup_transforms(self) -> list[Transform] | None: | ||
return None | ||
|
||
def setup_executors(self): | ||
return get_default_executors() | ||
|
||
def setup_config(self) -> dict: | ||
return {} | ||
|
||
def apply(self, model): | ||
self.validate(model) | ||
|
||
self.config = self.setup_config() | ||
lookasides = self.setup_lookasides() | ||
|
||
from thunder.core import jit_ext, interpreter | ||
|
||
if lookasides is not None: | ||
for lookaside in lookasides: | ||
wrapped_replacement_fn = interpreter.interpreter_needs_wrap(lookaside._replace_with) | ||
jit_ext._general_jit_lookaside_map[lookaside._fn] = wrapped_replacement_fn | ||
|
||
self.lookasides = lookasides | ||
self.executors = self.setup_executors() | ||
self.transforms = self.setup_transforms() | ||
|
||
if self.compiler == "thunder.jit": | ||
from thunder import jit | ||
|
||
thunder_model = jit(model, transforms=self.transforms, executors=self.executors, **self.config) | ||
|
||
elif self.compiler == "torch.compile": | ||
from thunder.dynamo import ThunderCompiler | ||
|
||
thunder_backend = ThunderCompiler(transforms=self.transforms, executors=self.executors, **self.config) | ||
thunder_model = torch.compile(model, backend=thunder_backend) | ||
|
||
else: | ||
raise AttributeError(f"Compiler must be one of 'thunder.jit', 'torch.compile'. Found: {self.compiler}.") | ||
|
||
return thunder_model | ||
|
||
|
||
class DynamoRecipe(Recipe): | ||
compiler = "torch.compile" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import thunder | ||
import transformers | ||
import torch | ||
|
||
|
||
class HFBertBasic(thunder.Recipe): | ||
def validate(self, model): | ||
if not isinstance(model, transformers.BertForSequenceClassification): | ||
raise ValueError("The model must be a BertForSequenceClassification") | ||
|
||
def setup_lookasides(self): | ||
warn_lookaside = thunder.Lookaside( | ||
fn=transformers.modeling_utils.PreTrainedModel.warn_if_padding_and_no_attention_mask, | ||
replace_with=lambda *args: None, | ||
) | ||
|
||
if hasattr(torch, "compiler") and hasattr(torch.compiler, "is_compiling"): | ||
is_compiling = torch.compiler.is_compiling | ||
else: | ||
is_compiling = torch._dynamo.is_compiling | ||
|
||
is_compiling_lookaside = thunder.Lookaside(fn=is_compiling, replace_with=lambda *_: True) | ||
|
||
return [warn_lookaside, is_compiling_lookaside] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.