Skip to content

Commit

Permalink
add accelerate and device_map to speed up loads
Browse files Browse the repository at this point in the history
  • Loading branch information
bluecoconut committed May 19, 2023
1 parent 5fcf51d commit 2029ef6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lambdaprompt/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,27 +221,32 @@ class Parameters(Backend.Parameters):

def __init__(self, model_name, torch_dtype=None, trust_remote_code=True, use_auth_token=None, **param_override):
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
torch_dtype = torch_dtype or torch.bfloat16
super().__init__(**param_override)
config = AutoConfig.from_pretrained(
"HuggingFaceH4/starchat-alpha",
trust_remote_code=True
)
self.model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch_dtype,
trust_remote_code=trust_remote_code,
use_auth_token=use_auth_token,
config=config,
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(
model_name,
trust_remote_code=trust_remote_code,
use_auth_token=use_auth_token,
device_map="auto"
)
if tokenizer.pad_token_id is None:
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "left"
self.tokenizer = tokenizer
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model.eval()
self.model.to(device=device, dtype=torch_dtype)

def preprocess(self, prompt):
return prompt
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dynamic = ["version"]

[project.optional-dependencies]
server = ["fastapi", "uvicorn", "aiosqlite"]
local = ["transformers"]
local = ["transformers", "accelerate"]
all = ["lambdaprompt[server,local]"]

[tool.setuptools_scm]
Expand Down

0 comments on commit 2029ef6

Please sign in to comment.