import torchfrom kernels import get_kernelfrom pylate import models
device = "cuda" if torch.cuda.is_available() else "mps"model = models.ColBERT( model_name_or_path="perplexity-ai/pplx-embed-v1-late-0.6b", trust_remote_code=True, device=device,)maxsim = get_kernel("erikkaum/maxsim", version=1, trust_remote_code=True)
q_emb = model.encode(["What motivates scientific discovery?"], is_query=True, convert_to_tensor=True)d_emb = model.encode([ "Scientists explore the universe driven by curiosity.", "Children learn through curious exploration.", "Historical discoveries began with curious questions.",], is_query=False, convert_to_tensor=True)
# Pad to [B=1, n_candidates, Ld_max, dim] for score_candidates_padded.Lq, dim = q_emb[0].shapen, Ld_max = len(d_emb), max(d.shape[0] for d in d_emb)queries_pad = q_emb[0].unsqueeze(0).to(device, torch.float16)documents_pad = torch.zeros(1, n, Ld_max, dim, device=device, dtype=torch.float16)for i, d in enumerate(d_emb): documents_pad[0, i, : d.shape[0]] = d.to(device, torch.float16)query_lengths = torch.tensor([Lq], dtype=torch.int32, device=device)doc_lengths = torch.tensor([[d.shape[0] for d in d_emb]], dtype=torch.int32, device=device)
scores = maxsim.score_candidates_padded(queries_pad, documents_pad, query_lengths, doc_lengths)print(scores[0].tolist()) # fp32 scores per candidate