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

fix: propagate precision correctly to enable non-bf16 inference #165

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions fam/llm/fast_inference_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def generate(
device, dtype = prompt.device, prompt.dtype

seq = torch.clone(prompt)
input_pos = torch.arange(0, T, device=device)
input_pos = torch.arange(0, T, device=device, dtype=dtype)

next_token = prefill(model, prompt.view(1, -1).repeat(2, 1), spk_emb, input_pos, **sampling_kwargs)
seq = torch.cat([seq, next_token.view(1)])
Expand Down Expand Up @@ -278,7 +278,7 @@ def _load_model(
k = k.replace(".mlp.c_proj.", ".feed_forward.w2.")

model.load_state_dict(state_dict, assign=True)
model = model.to(device=device, dtype=torch.bfloat16)
model = model.to(device=device, dtype=precision)

if quantisation_mode == "int8":
warnings.warn(
Expand All @@ -291,7 +291,7 @@ def _load_model(
quantized_state_dict = simple_quantizer.create_quantized_state_dict()
model = simple_quantizer.convert_for_runtime()
model.load_state_dict(quantized_state_dict, assign=True)
model = model.to(device=device, dtype=torch.bfloat16)
model = model.to(device=device, dtype=precision)
# TODO: int8/int4 doesn't decrease VRAM usage substantially... fix that (might be linked to kv-cache)
torch.cuda.empty_cache()
elif quantisation_mode == "int4":
Expand All @@ -302,7 +302,7 @@ def _load_model(
quantized_state_dict = simple_quantizer.create_quantized_state_dict()
model = simple_quantizer.convert_for_runtime(use_cuda=True)
model.load_state_dict(quantized_state_dict, assign=True)
model = model.to(device=device, dtype=torch.bfloat16)
model = model.to(device=device, dtype=precision)
torch.cuda.empty_cache()
elif quantisation_mode is not None:
raise Exception(f"Invalid quantisation mode {quantisation_mode}! Must be either 'int4' or 'int8'!")
Expand Down