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

mps support (wip) #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
59 changes: 27 additions & 32 deletions kurtis/inference.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import torch

from .utils import get_device


Expand All @@ -22,36 +20,33 @@ def inference_model(
"""

response = None
try:
device = get_device()
# Ensure the model is on the correct device
model.eval()
messages = [
{
"role": "system",
"content": config.QA_INSTRUCTION,
},
{"role": "user", "content": input_text},
]
input_text = tokenizer.apply_chat_template(messages, tokenize=False)
inputs = tokenizer.encode(f"{input_text}assistant\n", return_tensors="pt").to(
device
)
with torch.no_grad():
outputs = model.generate(
inputs,
max_new_tokens=512,
temperature=0.4,
top_p=0.9,
top_k=50,
repetition_penalty=1.0,
do_sample=True,
eos_token_id=tokenizer.eos_token_id,
)
new_tokens = outputs[0][inputs.shape[-1] :]
response = tokenizer.decode(new_tokens, skip_special_tokens=True)
except Exception as e:
response = f"An error occurred during inference: {str(e)}"
device = get_device()
# Ensure the model is on the correct device
model.to(device)
model.eval()
messages = [
{
"role": "system",
"content": config.QA_INSTRUCTION,
},
{"role": "user", "content": input_text},
]
input_text = tokenizer.apply_chat_template(messages, tokenize=False)
inputs = tokenizer.encode(f"{input_text}assistant\n", return_tensors="pt").to(
device
)
outputs = model.generate(
inputs,
max_new_tokens=512,
temperature=0.4,
top_p=0.9,
top_k=50,
repetition_penalty=1.0,
do_sample=True,
eos_token_id=tokenizer.eos_token_id,
)
new_tokens = outputs[0][inputs.shape[-1] :]
response = tokenizer.decode(new_tokens, skip_special_tokens=True)

fallback_response = "I'm sorry, I don't have an answer for that."
return response.strip() if response else fallback_response
2 changes: 1 addition & 1 deletion kurtis/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_compute_dtype=torch_dtype,
)


Expand Down
2 changes: 0 additions & 2 deletions kurtis/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ def formatting_prompts_func(example):
optim="paged_adamw_8bit",
run_name=model_output,
save_strategy="epoch",
use_mps_device=False,
no_cuda=False,
),
peft_config=config.LORA_CONFIG,
formatting_func=formatting_prompts_func,
Expand Down
16 changes: 7 additions & 9 deletions kurtis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@ def load_config(config_module="kurtis.config.default"):


# https://github.com/pytorch/pytorch/issues/83015
# def get_device():
# return (
# "mps"
# if torch.backends.mps.is_available()
# else "cuda" if torch.cuda.is_available() else "cpu"
# )


def get_device():
return "cuda" if torch.cuda.is_available() else "cpu"
return (
"mps"
if torch.backends.mps.is_available()
else "cuda"
if torch.cuda.is_available()
else "cpu"
)


def free_unused_memory():
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ dependencies = [
"tqdm>=4.67.0",
"bitsandbytes>=0.44.1 ; platform_machine == 'x86_64'",
"setuptools>=75.3.0",
"bitsandbytes ; sys_platform == 'darwin' and platform_machine == 'arm64'",
]

[tool.uv.sources]
torch = { index = "pytorch-cu124", marker = "platform_machine == 'x86_64'"}
bitsandbytes = { git = "https://github.com/bitsandbytes-foundation/bitsandbytes.git", branch = "multi-backend-refactor", marker = "sys_platform == 'darwin' and platform_machine == 'arm64'" }

[[tool.uv.index]]
name = "pytorch-cu124"
Expand Down
41 changes: 34 additions & 7 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.