-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed basic hf server to support quantization and streaming (#2293)
I now also put the multi-worker-image PR in here because it builds heavily upon it --------- Co-authored-by: Andreas Köpf <andreas.koepf@provisio.com> Co-authored-by: Dimitri <dimitrivr@icloud.com> Co-authored-by: IRFN <irfantogluk@gmail.com> Co-authored-by: AbdBarho <ka70911@gmail.com> Co-authored-by: Oliver Stanley <olivergestanley@gmail.com> Co-authored-by: Michael Gartsbein <mikegarts@users.noreply.github.com> Co-authored-by: mishka <gartsocial@gmail.com> Co-authored-by: Theodor Peifer <teddypeifer@gmail.com>
- Loading branch information
1 parent
74cdb54
commit 8a97cd4
Showing
10 changed files
with
308 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import os | ||
import signal | ||
import sys | ||
from pathlib import Path | ||
|
||
import huggingface_hub | ||
|
||
|
||
def terminate(signum, frame): | ||
print("Terminating...") | ||
sys.exit(0) | ||
|
||
|
||
if __name__ == "__main__": | ||
signal.signal(signal.SIGINT, terminate) | ||
model_id = os.getenv("MODEL_ID") | ||
snapshot_dir = Path(huggingface_hub.snapshot_download(model_id)) | ||
for file in snapshot_dir.rglob("*.json"): | ||
text = file.read_text() | ||
text = text.replace("LLaMA", "Llama") | ||
file.write_text(text) |
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,36 @@ | ||
import typing | ||
|
||
import transformers | ||
from loguru import logger | ||
|
||
|
||
class Printer(typing.Protocol): | ||
def __call__(self, value: int) -> None: | ||
... | ||
|
||
|
||
def _unpack(value): | ||
if len(value.shape) > 1 and value.shape[0] > 1: | ||
raise ValueError("HFStreamer only supports batch size 1") | ||
elif len(value.shape) > 1: | ||
value = value[0] | ||
return value.cpu().tolist() | ||
|
||
|
||
# based on HF text streamer | ||
class HFStreamer(transformers.generation.streamers.BaseStreamer): | ||
def __init__(self, input_ids, printer: Printer): | ||
self.input_ids = _unpack(input_ids)[::-1] | ||
self.printer = printer | ||
|
||
def put(self, value): | ||
for token_id in _unpack(value): | ||
if self.input_ids: | ||
input_id = self.input_ids.pop() | ||
if input_id != token_id: | ||
logger.warning(f"Input id {input_id} does not match output id {token_id}") | ||
else: | ||
self.printer(token_id) | ||
|
||
def end(self): | ||
pass |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
accelerate | ||
bitsandbytes | ||
fastapi | ||
huggingface_hub | ||
sse-starlette | ||
torch | ||
git+https://github.com/huggingface/transformers@main#egg=transformers | ||
uvicorn |
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.