-
Notifications
You must be signed in to change notification settings - Fork 19
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
✈️ Introduce Jetstream/Pytorch in TGI #88
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
68c77df
feat(tgi): add functions to load Jetstream Pytorch engine for Llama2
tengomucho b28ef47
chore(TokenSelector): remove XLA xm rng seed set
tengomucho c74900e
fix(version): remove warning on deprecated API
tengomucho be56089
fix(generator): use pad_token_id for padding
tengomucho 6db3c2c
fix(decode): clear unrequested slots
tengomucho 02ffeea
feat(imports): add function to check if Jetstream Pytorch can be used
tengomucho 8e98023
feat(Jetstream): improved support for engine load
tengomucho e3840e1
feat(TGI): Added Jetstream/Pytorch generator
tengomucho 3ff7197
chore(fsdp v2): avoid importing PretrainedModel
tengomucho 6c9348c
feat(tgi): introduce AutoGenerator
tengomucho 42ebaef
feat(Jetstream PT): Enable support only if env var is set
tengomucho 0af77a4
feat(TGI): use AutoGenerator in model server
tengomucho 3d782ab
feat(package): add optional dependency on Jetstream/Pytorch
tengomucho 33bb7d4
test(Jetstream Pytorch): added a simple decode test
tengomucho 3cc2ff8
test(decode): added a variant with do_sample=True with Jetstream PT
tengomucho e5e2fd4
fix(README): correct link
tengomucho aac4237
doc(README): add mention on how to install and enable Pytorch/Jetstream
tengomucho 07a71db
feat(build): make clean removes old TGI builds too
tengomucho b77a352
review: comply to comments requests
tengomucho 76fbf94
review(AutoGenerator): log if using Jetstream/PT or torch xla
tengomucho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,26 @@ | ||
import os | ||
import sys | ||
|
||
from loguru import logger | ||
|
||
|
||
def jetstream_pt_available() -> bool: | ||
"""Check if the necessary imports to use jetstream_pt are available. | ||
""" | ||
try: | ||
# For now Jetstream Pytorch is opt-in, it can be enabled with an ENV variable. | ||
jetstream_pt_enabled = os.environ.get("JETSTREAM_PT", False) == "1" | ||
if not jetstream_pt_enabled: | ||
return False | ||
# Torch XLA should not be imported before torch_xla2 to avoid conflicts. | ||
if 'torch_xla2' not in sys.modules and 'torch_xla.core' in sys.modules: | ||
logger.warning("torch_xla2 cannot be imported after torch_xla, disabling Jetstream PyTorch support.") | ||
return False | ||
# Import torch_xla2 first! | ||
import torch_xla2 # noqa: F401, isort:skip | ||
|
||
import jetstream_pt # noqa: F401 | ||
|
||
return True | ||
except ImportError: | ||
return False |
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
39 changes: 39 additions & 0 deletions
39
text-generation-inference/server/text_generation_server/auto_generator.py
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,39 @@ | ||
from loguru import logger | ||
|
||
from .generator_base import Generator | ||
from .jetstream_pt_support import model_can_use_jetstream_pt | ||
|
||
|
||
class AutoGenerator: | ||
|
||
@staticmethod | ||
def from_pretrained( | ||
model_path: str, revision: str, max_batch_size: int, max_sequence_length: int | ||
) -> Generator: | ||
"""Instantiate a Generator for TPU using Jetstream Pytorch or Pytorch/XLA. | ||
|
||
Args: | ||
model_path (`str`): | ||
The path to a local model. This path must also contain a Tokenizer. | ||
revision (`str`): | ||
The revision of the model. | ||
max_batch_size (`int`): | ||
The maximum batch size. | ||
max_sequence_length (`int`): | ||
The maximum sequence length. | ||
|
||
Returns: | ||
A TpuGenerator. | ||
""" | ||
if model_can_use_jetstream_pt(model_path): | ||
logger.debug("Using Jetstream PyTorch generator.") | ||
from .jetstream_pt_support.generator import TpuGeneratorJetStream | ||
return TpuGeneratorJetStream.from_pretrained( | ||
model_path, revision=revision, max_batch_size=max_batch_size, max_sequence_length=max_sequence_length | ||
) | ||
else: | ||
logger.debug("Using PyTorch/XLA generator.") | ||
from .generator import TpuGenerator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would useful to a user to log 1) when we have successfully loaded jetstream and 2) when we're falling back to the base generator |
||
return TpuGenerator.from_pretrained( | ||
model_path, revision=revision, max_batch_size=max_batch_size, max_sequence_length=max_sequence_length | ||
) |
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
15 changes: 15 additions & 0 deletions
15
text-generation-inference/server/text_generation_server/jetstream_pt_support/__init__.py
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,15 @@ | ||
# Copyright 2024 The HuggingFace Team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .compatibility import create_engine, model_can_use_jetstream_pt |
53 changes: 53 additions & 0 deletions
53
...-generation-inference/server/text_generation_server/jetstream_pt_support/compatibility.py
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,53 @@ | ||
# Copyright 2024 The HuggingFace Team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
from typing import Any | ||
|
||
from transformers import AutoConfig | ||
|
||
from optimum.tpu import jetstream_pt_available | ||
|
||
|
||
def model_can_use_jetstream_pt(model_path: str) -> bool: | ||
"""Checks if the model is supported by Jetstream Pytorch on Optimum TPU and if the required dependencies to provide | ||
the engine are installed. | ||
""" | ||
config = AutoConfig.from_pretrained(model_path) | ||
# For now only Llama 2 with tokenizer.model is supported | ||
if config.model_type != "llama" or not os.path.exists( | ||
os.path.join(model_path, "tokenizer.model") | ||
): | ||
return False | ||
if jetstream_pt_available(): | ||
return True | ||
return False | ||
|
||
|
||
def create_engine( | ||
model_path: str, | ||
batch_size: int, | ||
sequence_length: int, | ||
max_input_tokens: int, | ||
max_output_tokens: int, | ||
) -> Any: | ||
if not model_can_use_jetstream_pt(model_path): | ||
# The model is not compatible with Jetstream PyTorch, just exit | ||
return None | ||
|
||
# Now import engine_loader to prevent importing it at the top when not supported | ||
from .engine_loader import create_engine | ||
return create_engine( | ||
model_path, batch_size, sequence_length, max_input_tokens, max_output_tokens | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would it make sense to emit a warning here? Like "
JETSTREAM_PT
is enabled, but torch_xla2 is not installed. Falling back to torch_xla".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's actually a little trickier than that:
torch_xla
cannot be imported aftertorch_xla
has been imported. I will add a warning.