|
| 1 | +import os |
| 2 | +import json |
| 3 | +import pickle |
| 4 | +import numpy as np |
| 5 | +from copy import deepcopy |
| 6 | +from tqdm import tqdm |
| 7 | +from itertools import product |
| 8 | +from argparse import ArgumentParser |
| 9 | + |
| 10 | +import torch |
| 11 | +import torch.nn as nn |
| 12 | +import torch.optim as optim |
| 13 | +from torch import Tensor |
| 14 | +from utils.loss import loss_dict |
| 15 | + |
| 16 | +class QueryImpModel(nn.Module): |
| 17 | + def __init__(self, query_rep, scaler): |
| 18 | + super().__init__() |
| 19 | + self.query_rep = nn.Parameter(torch.FloatTensor(query_rep), requires_grad=True) |
| 20 | + self.scaler = scaler |
| 21 | + |
| 22 | + def forward(self, psg_embs: Tensor, attn_mask: Tensor = None): |
| 23 | + pred_scores = (self.scaler / 2) * torch.matmul(self.query_rep, psg_embs.transpose(0, 1)) |
| 24 | + if attn_mask is not None: |
| 25 | + extended_attention_mask = (1.0 - attn_mask) * torch.finfo(pred_scores.dtype).min |
| 26 | + pred_scores += extended_attention_mask |
| 27 | + pred_probs = nn.functional.log_softmax(pred_scores, dim=-1) |
| 28 | + return pred_probs |
| 29 | + |
| 30 | + |
| 31 | +class QueryScoreModel(nn.Module): |
| 32 | + def __init__(self, query_rep, scaler=2.0): |
| 33 | + super().__init__() |
| 34 | + self.query_rep = nn.Parameter(torch.FloatTensor(query_rep), requires_grad=True) |
| 35 | + self.scaler = scaler |
| 36 | + |
| 37 | + def forward(self, psg_embs: Tensor, attn_mask: Tensor = None): |
| 38 | + pred_scores = (self.scaler / 2) * torch.matmul(self.query_rep, psg_embs.transpose(0, 1)) |
| 39 | + if attn_mask is not None: |
| 40 | + extended_attention_mask = (1.0 - attn_mask) * torch.finfo(pred_scores.dtype).min |
| 41 | + pred_scores += extended_attention_mask |
| 42 | + return pred_scores.unsqueeze(0) |
| 43 | + |
| 44 | + |
| 45 | +def load_results(inp_path, rerank_path, ce_top_k, llm_top_k, use_logits, use_alpha): |
| 46 | + llm_rerank = None |
| 47 | + ce_rerank = None |
| 48 | + |
| 49 | + if llm_top_k > 0: |
| 50 | + suffix = "_llm" |
| 51 | + suffix += "_FIRST" if use_logits else "_gen" |
| 52 | + suffix += "_alpha" if use_alpha else "_num" |
| 53 | + llm_rerank = json.load(open(os.path.join(rerank_path, f"rerank_{llm_top_k}{suffix}.json"))) |
| 54 | + |
| 55 | + if ce_top_k > 0: |
| 56 | + ce_rerank = json.load(open(os.path.join(rerank_path, f"rerank_{ce_top_k}_ce.json"))) |
| 57 | + |
| 58 | + examples = pickle.load(open(inp_path, "rb")) |
| 59 | + return examples, ce_rerank, llm_rerank |
| 60 | + |
| 61 | + |
| 62 | +def prepare_distill_ce(data, ce_rerank, ce_top_k): |
| 63 | + qid = data["query_id"] |
| 64 | + pids = data["passage_ids"][:ce_top_k] |
| 65 | + |
| 66 | + data_passage_mapping = {pid: deepcopy(emb) for pid, emb in zip(data["passage_ids"], data["passage_embs"])} |
| 67 | + target_scores = [ce_rerank[qid][pid] for pid in pids] |
| 68 | + psg_embs = [data_passage_mapping[pid] for pid in pids] |
| 69 | + |
| 70 | + target_scores = torch.FloatTensor(target_scores) |
| 71 | + target_probs = nn.functional.log_softmax(target_scores, dim=-1) |
| 72 | + |
| 73 | + baseline_rep = torch.FloatTensor(data["query_rep"]) |
| 74 | + passage_reps = torch.FloatTensor(np.array(psg_embs)) |
| 75 | + |
| 76 | + init_scores = torch.matmul(baseline_rep, passage_reps.transpose(0, 1)) |
| 77 | + scaler = (target_scores.max() - target_scores.min()) / (init_scores.max().item() - init_scores.min().item()) |
| 78 | + |
| 79 | + return passage_reps, target_probs, scaler |
| 80 | + |
| 81 | + |
| 82 | +def prepare_distill_llm(data, llm_rerank, query_rep, llm_top_k): |
| 83 | + qid = data["query_id"] |
| 84 | + pids = data["passage_ids"][:llm_top_k] |
| 85 | + |
| 86 | + data_passage_mapping = {pid: deepcopy(emb) for pid, emb in zip(data["passage_ids"], data["passage_embs"])} |
| 87 | + reranked_target_scores = [llm_rerank[qid][pid] for pid in pids] |
| 88 | + reranked_psg_embs = [data_passage_mapping[pid] for pid in pids] |
| 89 | + |
| 90 | + reranked_target_scores = torch.FloatTensor(reranked_target_scores) |
| 91 | + reranked_passage_reps = torch.FloatTensor(np.array(reranked_psg_embs)) |
| 92 | + |
| 93 | + init_scores = torch.matmul(query_rep, reranked_passage_reps.transpose(0, 1)) |
| 94 | + scaler = (reranked_target_scores.max() - reranked_target_scores.min()) / \ |
| 95 | + (init_scores.max().item() - init_scores.min().item()) |
| 96 | + |
| 97 | + return reranked_passage_reps, reranked_target_scores.unsqueeze(0), scaler |
| 98 | + |
| 99 | + |
| 100 | +def run_query_teacher_importance_learner(inp_path, rerank_path, output_path, loss_path, ce_top_k, llm_top_k, learning_rate, |
| 101 | + num_updates, use_logits, use_alpha, llm_loss): |
| 102 | + assert llm_loss in loss_dict |
| 103 | + examples, ce_rerank, llm_rerank = load_results(inp_path, rerank_path, ce_top_k, llm_top_k, use_logits, use_alpha) |
| 104 | + |
| 105 | + reps = [] |
| 106 | + ids = [] |
| 107 | + |
| 108 | + for data in tqdm(examples): |
| 109 | + baseline_rep = torch.FloatTensor(data["query_rep"]) |
| 110 | + |
| 111 | + try: |
| 112 | + learned_rep = baseline_rep |
| 113 | + if ce_top_k > 0: |
| 114 | + passage_reps, target_probs, scaler = prepare_distill_ce(data, ce_rerank, ce_top_k) |
| 115 | + ce_dstl_model = QueryImpModel(query_rep=baseline_rep.numpy(), scaler=scaler) |
| 116 | + loss_function = nn.KLDivLoss(reduction="batchmean", log_target=True) |
| 117 | + optimizer = optim.Adam(ce_dstl_model.parameters(), lr=learning_rate) |
| 118 | + |
| 119 | + for _ in range(num_updates): |
| 120 | + optimizer.zero_grad() |
| 121 | + pred_probs = ce_dstl_model(psg_embs=passage_reps) |
| 122 | + loss = loss_function(pred_probs.unsqueeze(0), target_probs.unsqueeze(0)) |
| 123 | + loss.backward() |
| 124 | + optimizer.step() |
| 125 | + |
| 126 | + learned_rep = ce_dstl_model.query_rep.data.cpu().detach() |
| 127 | + |
| 128 | + reranked_passage_reps, reranked_target_scores, scaler = prepare_distill_llm(data, llm_rerank, learned_rep, |
| 129 | + llm_top_k) |
| 130 | + llm_dstl_model = QueryScoreModel(query_rep=learned_rep.numpy(), scaler=scaler) |
| 131 | + optimizer = optim.Adam(llm_dstl_model.parameters(), lr=learning_rate / 5) |
| 132 | + |
| 133 | + for _ in range(num_updates // 5): |
| 134 | + optimizer.zero_grad() |
| 135 | + pred_scores = llm_dstl_model(psg_embs=reranked_passage_reps) |
| 136 | + loss = loss_dict[llm_loss](pred_scores, reranked_target_scores, weighted=True if llm_loss == "ranknet" else False) |
| 137 | + loss.backward() |
| 138 | + optimizer.step() |
| 139 | + |
| 140 | + rep = llm_dstl_model.query_rep.data.cpu().detach() |
| 141 | + reps.append(rep.numpy()) |
| 142 | + ids.append(data["query_id"]) |
| 143 | + except Exception as e: |
| 144 | + print(f"Error for query ID {data['query_id']}: {e}") |
| 145 | + |
| 146 | + pickle.dump((np.array(reps), ids), open(output_path, "wb")) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + |
| 151 | + parser = ArgumentParser() |
| 152 | + parser.add_argument('--inp_path', required=True) |
| 153 | + parser.add_argument('--rerank_path', required=True) |
| 154 | + parser.add_argument('--output_path', required=True) |
| 155 | + parser.add_argument('--loss_path', required=True) |
| 156 | + parser.add_argument('--ce_top_k', type=int, default=100) |
| 157 | + parser.add_argument('--llm_top_k', type=int, default=9) |
| 158 | + parser.add_argument('--learning_rate', type=float, default=0.005) |
| 159 | + parser.add_argument('--num_updates', type=int, default=100) |
| 160 | + parser.add_argument('--use_logits', type=int, default=0) |
| 161 | + parser.add_argument('--use_alpha', type=int, default=0) |
| 162 | + parser.add_argument('--llm_loss', type=str, default="lambdarank") |
| 163 | + |
| 164 | + args = parser.parse_args() |
| 165 | + |
| 166 | + run_query_teacher_importance_learner(args.inp_path, args.rerank_path, args.output_path, args.loss_path, args.ce_top_k, args.llm_top_k, args.learning_rate, args.num_updates, args.use_logits, args.use_alpha, args.llm_loss) |
0 commit comments