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

add multigpu inference example #91

Merged
merged 1 commit into from
Jul 26, 2024
Merged
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
41 changes: 41 additions & 0 deletions examples/multigpu_infer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-


from angle_emb import AnglE
from datasets import load_dataset
from multiprocess import set_start_method


# configuration
n_gpus = 4
workers = 8
batch_size = 16

# init angle
angle = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1', pooling_strategy='cls').cuda()

# load dataset
ds = load_dataset('mteb/stsbenchmark-sts', split='test')
ds = ds.select_columns(('sentence1', 'sentence2'))


def encode(examples, rank):
device = f"cuda:{rank}"
angle.to(device)

docs = [f'{s1} {s2}' for s1, s2 in zip(examples['sentence1'], examples['sentence2'])]
examples['emb'] = angle.encode(docs).tolist()
return examples


if __name__ == '__main__':

# it is required to put the inference code in the main function.

set_start_method('spawn')

# map and encode
ds = ds.map(encode, with_rank=True, num_proc=n_gpus, batched=True, batch_size=batch_size)

print(ds)
# ds.push_to_hub(xxx)
Loading