-
Notifications
You must be signed in to change notification settings - Fork 265
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
Model integration for Lit-GPT #1792
Merged
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
217e938
lit-gpt integration
aniketmaurya 7bdceb5
update
aniketmaurya 648248c
update
aniketmaurya a099f0c
remove top_k
aniketmaurya 6e3c968
formatting
aniketmaurya 529a4e7
implement logprob in a future PR
aniketmaurya 4ece813
implement logprob in a future PR
aniketmaurya e5e73b5
LIMITED_FUNCTIONALITY_TEXT_MODEL_TAG
aniketmaurya a7c583b
update logs
aniketmaurya 716f6e4
allow temperature zero
aniketmaurya f6841ec
Merge branch 'main' into lit-gpt
aniketmaurya f35af30
fixes
aniketmaurya 608c0fc
eos ids
aniketmaurya 6a99819
format
aniketmaurya f30b69f
fix devices
aniketmaurya 800e624
fix
aniketmaurya 9aa8568
add stop words
aniketmaurya d7ef12f
fixes
aniketmaurya 0b29328
singleton
aniketmaurya db95353
fix
aniketmaurya 0df756e
merge main
aniketmaurya 4b42ab0
formatting
aniketmaurya f300dd1
formatting
aniketmaurya c3fadb8
fix import
aniketmaurya aed82e8
apply suggestion
aniketmaurya 4705abc
apply suggestion
aniketmaurya 8b88d67
apply suggestion
aniketmaurya 373bd8b
update lit-gpt
aniketmaurya 966b5c6
update lit-gpt
aniketmaurya 3cdc206
apply suggestions
aniketmaurya 7a0756b
update
aniketmaurya c7da5d8
update
aniketmaurya 28b46d7
update
aniketmaurya 46ae293
format
aniketmaurya 1a11550
format
aniketmaurya 3e1ef09
black formatting
aniketmaurya d438939
black formatting
aniketmaurya 8fa6516
fix ci
aniketmaurya 7b3311e
Merge branch 'main' into lit-gpt
aniketmaurya ef080f2
fix typing
aniketmaurya d4d60a3
fix typing
aniketmaurya 6864507
format
aniketmaurya 0a6d561
fix typing
aniketmaurya 399176e
fix
aniketmaurya 678e584
fix type
aniketmaurya 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -356,6 +356,16 @@ models: | |
release_date: 2023-01-23 | ||
todo: true | ||
|
||
# Lightning AI's Lit-GPT | ||
- name: lightningai/lit-gpt | ||
display_name: Lit-GPT | ||
description: Lit-GPT is an optimized collection of open-source LLMs for finetuning and inference. It supports – Falcon, Llama 2, Vicuna, LongChat, and other top-performing open-source large language models. | ||
creator_organization: Lightning AI | ||
access: open | ||
num_parameters: 1 | ||
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. depends on the chosen model from the config |
||
release_date: 2023-04-04 | ||
|
||
|
||
# Meta | ||
- name: together/opt-iml-175b | ||
display_name: OPT-IML (175B) | ||
|
27 changes: 27 additions & 0 deletions
27
src/helm/benchmark/window_services/lit_gpt_window_service.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,27 @@ | ||
from .local_window_service import LocalWindowService | ||
from .tokenizer_service import TokenizerService | ||
|
||
|
||
class LitGPTWindowServce(LocalWindowService): | ||
def __init__(self, service: TokenizerService): | ||
super().__init__(service) | ||
|
||
@property | ||
def max_sequence_length(self) -> int: | ||
yifanmai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 2048 | ||
|
||
@property | ||
def max_request_length(self) -> int: | ||
return self.max_sequence_length | ||
|
||
@property | ||
def end_of_text_token(self) -> str: | ||
return "<|endoftext|>" | ||
|
||
@property | ||
def tokenizer_name(self) -> str: | ||
return "lightningai/lit-gpt" | ||
|
||
@property | ||
def prefix_token(self) -> str: | ||
return self.end_of_text_token |
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,180 @@ | ||
import json | ||
import logging | ||
import time | ||
from pathlib import Path | ||
from threading import Lock | ||
from typing import List, Literal, Optional, Dict, Union | ||
|
||
import torch | ||
|
||
from helm.common.cache import Cache, CacheConfig | ||
from helm.common.optional_dependencies import handle_module_not_found_error | ||
from helm.common.request import Request, RequestResult, Sequence, Token | ||
from helm.common.tokenization_request import ( | ||
DecodeRequest, | ||
DecodeRequestResult, | ||
TokenizationRequest, | ||
TokenizationRequestResult, | ||
TokenizationToken, | ||
) | ||
|
||
from .client import Client | ||
from .lit_gpt_generate import generate # type: ignore | ||
|
||
try: | ||
import lightning as L | ||
from lightning.fabric.strategies import FSDPStrategy | ||
from lit_gpt import GPT, Config, Tokenizer | ||
from lit_gpt.model import Block | ||
from lit_gpt.utils import check_valid_checkpoint_dir, lazy_load, quantization | ||
except ModuleNotFoundError as e: | ||
handle_module_not_found_error(e) | ||
|
||
yifanmai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
QuantizationType = Literal["bnb.nf4", "bnb.nf4-dq", "bnb.fp4", "bnb.fp4-dq", "bnb.int8", "gptq.int4"] | ||
|
||
|
||
class SingletonMeta(type): | ||
_instances: Dict[type, type] = {} | ||
_lock: Lock = Lock() | ||
|
||
def __call__(cls, *args, **kwargs): | ||
with cls._lock: | ||
if cls not in cls._instances: | ||
instance = super().__call__(*args, **kwargs) | ||
cls._instances[cls] = instance | ||
return cls._instances[cls] | ||
|
||
|
||
class LitGPT(metaclass=SingletonMeta): | ||
def __init__( | ||
self, | ||
checkpoint_dir: Path = Path(""), | ||
precision: str = "bf16-true", | ||
device: str = "auto", | ||
devices: int = 1, | ||
strategy: Union[str, FSDPStrategy] = "auto", | ||
quantize: Optional[QuantizationType] = None, | ||
): | ||
torch.set_float32_matmul_precision("high") | ||
|
||
if strategy == "fsdp": | ||
strategy = FSDPStrategy(auto_wrap_policy={Block}, cpu_offload=False) | ||
fabric = L.Fabric(devices=devices, accelerator=device, precision=precision, strategy=strategy) # type: ignore | ||
fabric.launch() | ||
logger.info("Using device: {}".format(fabric.device)) | ||
|
||
check_valid_checkpoint_dir(checkpoint_dir) | ||
|
||
with open(checkpoint_dir / "lit_config.json") as fp: | ||
config = Config(**json.load(fp)) | ||
|
||
checkpoint_path = checkpoint_dir / "lit_model.pth" | ||
logger.info(f"Loading model {str(checkpoint_path)!r} with {config.__dict__}") | ||
with fabric.init_module(empty_init=True), quantization(quantize): | ||
model = GPT(config) | ||
|
||
with lazy_load(checkpoint_path) as checkpoint: | ||
model.load_state_dict(checkpoint, strict=quantize is None) | ||
|
||
model.eval() | ||
self.model = fabric.setup(model) | ||
yifanmai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.tokenizer = Tokenizer(checkpoint_dir) | ||
self.fabric = fabric | ||
|
||
|
||
class LitGPTClient(Client): | ||
"""Client for evaluating Lit-GPT (from Lightning AI) supported LLMs""" | ||
|
||
def __init__( | ||
self, | ||
cache_config: CacheConfig, | ||
checkpoint_dir: Path = Path(""), | ||
precision: str = "bf16-true", | ||
device: str = "auto", | ||
devices: int = 1, | ||
strategy: str = "auto", | ||
quantize: Optional[QuantizationType] = None, | ||
): | ||
self.cache = Cache(cache_config) | ||
lit_gpt = LitGPT(checkpoint_dir, precision, device, devices, strategy, quantize) | ||
self.model = lit_gpt.model | ||
self.tokenizer = lit_gpt.tokenizer | ||
self.fabric = lit_gpt.fabric | ||
|
||
def make_request(self, request: Request) -> RequestResult: | ||
model = self.model | ||
tokenizer = self.tokenizer | ||
fabric = self.fabric | ||
encoded = tokenizer.encode(request.prompt, bos=True, eos=False, device=fabric.device) | ||
prompt_length = encoded.size(0) | ||
max_returned_tokens: int = prompt_length + request.max_tokens | ||
assert max_returned_tokens <= model.config.block_size, ( | ||
max_returned_tokens, | ||
model.config.block_size, | ||
) # maximum rope cache length | ||
|
||
model.clear_kv_cache() | ||
|
||
with fabric.init_tensor(): | ||
# set the max_seq_length to limit the memory usage to what we need | ||
model.max_seq_length = max_returned_tokens | ||
|
||
t0 = time.perf_counter() | ||
# helm doesn't have anything equivalent to top_k at the moment | ||
# TODO: allow temperature=0, pick the top token rather than sampling. | ||
stop_tokens: List[torch.Tensor] = [tokenizer.encode(e, device=fabric.device) for e in request.stop_sequences] | ||
|
||
with fabric.init_tensor(): | ||
# enable the kv cache | ||
model.set_kv_cache(batch_size=1) | ||
tokens = generate( | ||
model, | ||
encoded, | ||
max_returned_tokens, | ||
temperature=max(request.temperature, 1e-11), | ||
stop_tokens=stop_tokens, | ||
) | ||
|
||
t = time.perf_counter() - t0 | ||
model.clear_kv_cache() | ||
if request.echo_prompt is False: | ||
output = tokenizer.decode(tokens[prompt_length:]) | ||
else: | ||
output = tokenizer.decode(tokens) | ||
tokens_generated = tokens.size(0) - prompt_length | ||
|
||
logger.debug(f"Time for inference: {t:.02f} sec total, {tokens_generated / t:.02f} tokens/sec") | ||
logger.debug(f"Memory used: {torch.cuda.max_memory_reserved() / 1e9:.02f} GB") | ||
|
||
generated_tokens = [] | ||
for token in tokens: | ||
generated_tokens.append(Token(text=tokenizer.decode(token), logprob=0, top_logprobs={})) | ||
completions = [Sequence(text=output, logprob=0, tokens=generated_tokens)] | ||
|
||
return RequestResult( | ||
success=True, | ||
cached=False, | ||
error=None, | ||
completions=completions, | ||
embedding=[], | ||
request_time=t, | ||
) | ||
|
||
def tokenize(self, request: TokenizationRequest) -> TokenizationRequestResult: | ||
fabric = self.fabric | ||
logger.debug("Using device: {}".format(fabric.device)) | ||
t0 = time.perf_counter() | ||
encoded = self.tokenizer.encode(request.text, bos=True, eos=False, device=fabric.device) | ||
tokens = encoded.tolist() | ||
tokens = [TokenizationToken(value=token) for token in tokens] | ||
t = time.perf_counter() - t0 | ||
return TokenizationRequestResult(success=True, cached=False, tokens=tokens, text=request.text, request_time=t) | ||
|
||
def decode(self, request: DecodeRequest) -> DecodeRequestResult: | ||
t0 = time.perf_counter() | ||
text = self.tokenizer.decode(torch.as_tensor(request.tokens, dtype=torch.int)) | ||
t = time.perf_counter() - t0 | ||
return DecodeRequestResult(success=True, cached=False, text=text, request_time=t) |
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.
I think PyPI will reject packages with dependencies from GitHub, but we can deal with this in a later PR.
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.
true, no dependency outside pypi like source is accepted, you need to drop them