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 (llm): fix device issue for eval when not using default device #949

Merged
merged 2 commits into from
May 31, 2024
Merged
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
9 changes: 4 additions & 5 deletions src/brevitas_examples/llm/llm_quant/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,26 @@
from tqdm import tqdm


def create_validation_dataloader(data, seqlen):
def create_validation_dataloader(data, seqlen, device):
nsamples = data['input_ids'].numel() // seqlen
val_dataloader = []
for i in tqdm(range(nsamples)):
batch = data['input_ids'][:, (i * seqlen):((i + 1) * seqlen)].cuda()
batch = data['input_ids'][:, (i * seqlen):((i + 1) * seqlen)].to(device)
attention_mask = torch.ones_like(batch)
val_dataloader.append({'input_ids': batch, 'attention_mask': attention_mask})
return val_dataloader


@torch.no_grad()
def model_eval(model, valenc, seqlen):

nsamples = len(valenc)

dev = next(iter(model.parameters())).device
with torch.no_grad():
nlls = []
for inps in valenc:
lm_logits = model(**inps)['logits']
shift_logits = lm_logits[:, :-1, :].contiguous()
shift_labels = inps['input_ids'][:, 1:].cuda()
shift_labels = inps['input_ids'][:, 1:].to(dev)
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
neg_log_likelihood = loss.float() * seqlen
Expand Down
3 changes: 2 additions & 1 deletion src/brevitas_examples/llm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ def main():
nsamples=args.nsamples, tokenizer=tokenizer, seqlen=args.seqlen, seed=0)
val_data = get_wikitext2(
nsamples=args.nsamples, tokenizer=tokenizer, seqlen=args.seqlen, split='validation', seed=0)
val_data = create_validation_dataloader(val_data, args.seqlen)
fabianandresgrob marked this conversation as resolved.
Show resolved Hide resolved
device = next(iter(model.parameters())).device
val_data = create_validation_dataloader(val_data, args.seqlen, device)
print("Data loaded.")

# Apply LN affine merging before inserting MHA layers
Expand Down
Loading