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

Text-generation, model set-up: torch.compile for attributes instead of models' types #1452

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions examples/text-generation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,23 @@ def patch_scoped_linear_all_reduce(model):
patch_scoped_linear_all_reduce(module)


def get_torch_compiled_model(model):
if model.config.model_type in ["gpt_bigcode", "mpt", "bloom", "gpt2"]:
model.transformer = torch.compile(model.transformer, backend="hpu_backend")
elif model.config.model_type in ["gpt_neox"]:
model.gpt_neox = torch.compile(model.gpt_neox, backend="hpu_backend")
def get_torch_compiled_model(model, logger):
# for gpt_bigcode, mpt, bloom, gpt2 model_type
if hasattr(model, "transformer"):
model.transformer = torch.compile(
model.transformer, backend="hpu_backend", options={"keep_input_mutations": True}
)
# for gpt_neox
elif hasattr(model, "gpt_neox"):
model.gpt_neox = torch.compile(model.gpt_neox, backend="hpu_backend", options={"keep_input_mutations": True})
# for llama, mistral, mixtral, qwen2
elif hasattr(model, "model"):
model.model = torch.compile(model.model, backend="hpu_backend", options={"keep_input_mutations": True})
else:
model.model = torch.compile(model.model, backend="hpu_backend")
logger.warning(
"In low performance case, please explicitly specify a module you want to wrap with `torch.compile`"
)
model = torch.compile(model, backend="hpu_backend", options={"keep_input_mutations": True})
return model


Expand Down Expand Up @@ -305,9 +315,9 @@ def setup_model(args, model_dtype, model_kwargs, logger):
model.base_model.model = wrap_in_hpu_graph(model.base_model.model)

if args.torch_compile:
model = get_torch_compiled_model(model)
model = get_torch_compiled_model(model, logger)
# if args.assistant_model is not None:
# assistant_model = get_torch_compiled_model(assistant_model)
# assistant_model = get_torch_compiled_model(assistant_model, logger)
return model, assistant_model


Expand Down Expand Up @@ -372,7 +382,7 @@ def setup_distributed_model_tp(args, model_dtype, model_kwargs, logger, cache_di
model = wrap_in_hpu_graph(model)

if args.torch_compile:
model = get_torch_compiled_model(model)
model = get_torch_compiled_model(model, logger)

return model, args.assistant_model

Expand Down Expand Up @@ -446,9 +456,9 @@ def setup_distributed_model(args, model_dtype, model_kwargs, logger):
model = setup_quantization(model, args)

if args.torch_compile:
model = get_torch_compiled_model(model)
model = get_torch_compiled_model(model, logger)
# if args.assistant_model is not None:
# assistant_model = get_torch_compiled_model(assistant_model)
# assistant_model = get_torch_compiled_model(assistant_model, logger)
return model, assistant_model


Expand Down