File size: 20,222 Bytes
b05bf4f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# Copyright 2024 Bytedance Ltd. and/or its affiliates
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
from __future__ import annotations
import torch
from torch import nn
from torch.nn import functional as F
from transformers import (
PreTrainedModel,
BertModel,
AutoTokenizer,
)
import os
from transformers.modeling_outputs import SequenceClassifierOutput
from typing import Union, List, Optional
from collections import defaultdict
import numpy as np
import math
from huggingface_hub import hf_hub_download
from .configuration_listconranker import ListConRankerConfig
class QueryEmbedding(nn.Module):
def __init__(self, config) -> None:
super().__init__()
self.query_embedding = nn.Embedding(2, config.list_con_hidden_size)
self.layerNorm = nn.LayerNorm(config.list_con_hidden_size)
def forward(self, x, tags):
query_embeddings = self.query_embedding(tags)
x += query_embeddings
x = self.layerNorm(x)
return x
class ListTransformer(nn.Module):
def __init__(self, num_layer, config) -> None:
super().__init__()
self.config = config
self.list_transformer_layer = nn.TransformerEncoderLayer(
config.list_con_hidden_size,
self.config.num_attention_heads,
batch_first=True,
activation=F.gelu,
norm_first=False,
)
self.list_transformer = nn.TransformerEncoder(
self.list_transformer_layer, num_layer
)
self.relu = nn.ReLU()
self.query_embedding = QueryEmbedding(config)
self.linear_score3 = nn.Linear(
config.list_con_hidden_size * 2, config.list_con_hidden_size
)
self.linear_score2 = nn.Linear(
config.list_con_hidden_size * 2, config.list_con_hidden_size
)
self.linear_score1 = nn.Linear(config.list_con_hidden_size * 2, 1)
def forward(
self, pair_features: torch.Tensor, pair_nums: List[int]
) -> torch.Tensor:
batch_pair_features = pair_features.split(pair_nums)
pair_feature_query_passage_concat_list = []
for i in range(len(batch_pair_features)):
pair_feature_query = (
batch_pair_features[i][0].unsqueeze(0).repeat(pair_nums[i] - 1, 1)
)
pair_feature_passage = batch_pair_features[i][1:]
pair_feature_query_passage_concat_list.append(
torch.cat([pair_feature_query, pair_feature_passage], dim=1)
)
pair_feature_query_passage_concat = torch.cat(
pair_feature_query_passage_concat_list, dim=0
)
batch_pair_features = nn.utils.rnn.pad_sequence(
batch_pair_features, batch_first=True
)
query_embedding_tags = torch.zeros(
batch_pair_features.size(0),
batch_pair_features.size(1),
dtype=torch.long,
device=self.device,
)
query_embedding_tags[:, 0] = 1
batch_pair_features = self.query_embedding(
batch_pair_features, query_embedding_tags
)
mask = self.generate_attention_mask(pair_nums)
query_mask = self.generate_attention_mask_custom(pair_nums)
pair_list_features = self.list_transformer(
batch_pair_features, src_key_padding_mask=mask, mask=query_mask
)
output_pair_list_features = []
output_query_list_features = []
pair_features_after_transformer_list = []
for idx, pair_num in enumerate(pair_nums):
output_pair_list_features.append(pair_list_features[idx, 1:pair_num, :])
output_query_list_features.append(pair_list_features[idx, 0, :])
pair_features_after_transformer_list.append(
pair_list_features[idx, :pair_num, :]
)
pair_features_after_transformer_cat_query_list = []
for idx, pair_num in enumerate(pair_nums):
query_ft = (
output_query_list_features[idx].unsqueeze(0).repeat(pair_num - 1, 1)
)
pair_features_after_transformer_cat_query = torch.cat(
[query_ft, output_pair_list_features[idx]], dim=1
)
pair_features_after_transformer_cat_query_list.append(
pair_features_after_transformer_cat_query
)
pair_features_after_transformer_cat_query = torch.cat(
pair_features_after_transformer_cat_query_list, dim=0
)
pair_feature_query_passage_concat = self.relu(
self.linear_score2(pair_feature_query_passage_concat)
)
pair_features_after_transformer_cat_query = self.relu(
self.linear_score3(pair_features_after_transformer_cat_query)
)
final_ft = torch.cat(
[
pair_feature_query_passage_concat,
pair_features_after_transformer_cat_query,
],
dim=1,
)
logits = self.linear_score1(final_ft).squeeze()
return logits, torch.cat(pair_features_after_transformer_list, dim=0)
def generate_attention_mask(self, pair_num):
max_len = max(pair_num)
batch_size = len(pair_num)
mask = torch.zeros(batch_size, max_len, dtype=torch.bool, device=self.device)
for i, length in enumerate(pair_num):
mask[i, length:] = True
return mask
def generate_attention_mask_custom(self, pair_num):
max_len = max(pair_num)
mask = torch.zeros(max_len, max_len, dtype=torch.bool, device=self.device)
mask[0, 1:] = True
return mask
class ListConRankerModel(PreTrainedModel):
"""
ListConRanker model for sequence classification that's compatible with AutoModelForSequenceClassification.
"""
config_class = ListConRankerConfig
base_model_prefix = "listconranker"
def __init__(self, config: ListConRankerConfig):
super().__init__(config)
self.config = config
self.num_labels = config.num_labels
self.hf_model = BertModel(config.bert_config)
self.sigmoid = nn.Sigmoid()
self.linear_in_embedding = nn.Linear(
config.hidden_size, config.list_con_hidden_size
)
self.list_transformer = ListTransformer(
config.list_transformer_layers,
config,
)
def forward(
self,
input_ids: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
token_type_ids: Optional[torch.Tensor] = None,
position_ids: Optional[torch.Tensor] = None,
head_mask: Optional[torch.Tensor] = None,
inputs_embeds: Optional[torch.Tensor] = None,
labels: Optional[torch.Tensor] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
**kwargs,
) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]:
if self.training:
raise NotImplementedError("Training not supported; use eval mode.")
device = input_ids.device
self.list_transformer.device = device
# Reorganize by unique queries and their passages
(
reorganized_input_ids,
reorganized_attention_mask,
reorganized_token_type_ids,
pair_nums,
group_indices,
) = self._reorganize_inputs(input_ids, attention_mask, token_type_ids)
out = self.hf_model(
input_ids=reorganized_input_ids,
attention_mask=reorganized_attention_mask,
token_type_ids=reorganized_token_type_ids,
return_dict=True,
)
feats = out.last_hidden_state
pooled = self.average_pooling(feats, reorganized_attention_mask)
embedded = self.linear_in_embedding(pooled)
logits, _ = self.list_transformer(embedded, pair_nums)
probs = self.sigmoid(logits)
# Restore original order
sorted_probs = self._restore_original_order(probs, group_indices)
sorted_logits = self._restore_original_order(logits, group_indices)
if not return_dict:
return (sorted_probs, sorted_logits)
return SequenceClassifierOutput(
loss=None,
logits=sorted_logits,
hidden_states=out.hidden_states,
attentions=out.attentions,
)
def _reorganize_inputs(
self,
input_ids: torch.Tensor,
attention_mask: torch.Tensor,
token_type_ids: Optional[torch.Tensor],
) -> tuple[
torch.Tensor, torch.Tensor, Optional[torch.Tensor], List[int], List[List[int]]
]:
"""
Group inputs by unique queries: for each query, produce [query] + its passages,
then flatten, pad, and return pair sizes and original indices mapping.
"""
batch_size = input_ids.size(0)
# Structure: query_key -> {
# 'query': (seq, mask, tt),
# 'passages': [(seq, mask, tt), ...],
# 'indices': [original_index, ...]
# }
grouped = {}
for idx in range(batch_size):
seq = input_ids[idx]
mask = attention_mask[idx]
token_type_ids[idx] if token_type_ids is not None else torch.zeros_like(seq)
sep_idxs = (seq == self.config.sep_token_id).nonzero(as_tuple=True)[0]
if sep_idxs.numel() == 0:
raise ValueError(f"No SEP in sequence {idx}")
first_sep = sep_idxs[0].item()
second_sep = sep_idxs[1].item()
# Extract query and passage
q_seq = seq[: first_sep + 1]
q_mask = mask[: first_sep + 1]
q_tt = torch.zeros_like(q_seq)
p_seq = seq[first_sep : second_sep + 1]
p_mask = mask[first_sep : second_sep + 1]
p_seq = p_seq.clone()
p_seq[0] = self.config.cls_token_id
p_tt = torch.zeros_like(p_seq)
# Build key excluding CLS/SEP
key = tuple(
q_seq[
(q_seq != self.config.cls_token_id)
& (q_seq != self.config.sep_token_id)
].tolist()
)
# truncation
q_seq = q_seq[: self.config.max_position_embeddings]
q_seq[-1] = self.config.sep_token_id
p_seq = p_seq[: self.config.max_position_embeddings]
p_seq[-1] = self.config.sep_token_id
q_mask = q_mask[: self.config.max_position_embeddings]
p_mask = p_mask[: self.config.max_position_embeddings]
q_tt = q_tt[: self.config.max_position_embeddings]
p_tt = p_tt[: self.config.max_position_embeddings]
if key not in grouped:
grouped[key] = {
"query": (q_seq, q_mask, q_tt),
"passages": [],
"indices": [],
}
grouped[key]["passages"].append((p_seq, p_mask, p_tt))
grouped[key]["indices"].append(idx)
# Flatten according to group insertion order
seqs, masks, tts, pair_nums, group_indices = [], [], [], [], []
for key, data in grouped.items():
q_seq, q_mask, q_tt = data["query"]
passages = data["passages"]
indices = data["indices"]
# record sizes and original positions
pair_nums.append(len(passages) + 1) # +1 for the query
group_indices.append(indices)
# append query then its passages
seqs.append(q_seq)
masks.append(q_mask)
tts.append(q_tt)
for p_seq, p_mask, p_tt in passages:
seqs.append(p_seq)
masks.append(p_mask)
tts.append(p_tt)
# Pad to uniform length
max_len = max(s.size(0) for s in seqs)
padded_seqs, padded_masks, padded_tts = [], [], []
for s, m, t in zip(seqs, masks, tts):
ps = torch.zeros(max_len, dtype=s.dtype, device=s.device)
pm = torch.zeros(max_len, dtype=m.dtype, device=m.device)
pt = torch.zeros(max_len, dtype=t.dtype, device=t.device)
ps[: s.size(0)] = s
pm[: m.size(0)] = m
pt[: t.size(0)] = t
padded_seqs.append(ps)
padded_masks.append(pm)
padded_tts.append(pt)
rid = torch.stack(padded_seqs)
ram = torch.stack(padded_masks)
rtt = torch.stack(padded_tts) if token_type_ids is not None else None
return rid, ram, rtt, pair_nums, group_indices
def _restore_original_order(
self,
logits: torch.Tensor,
group_indices: List[List[int]],
) -> torch.Tensor:
"""
Map flattened logits back so each original index gets its passage score.
"""
out = torch.zeros(logits.size(0), dtype=logits.dtype, device=logits.device)
i = 0
for indices in group_indices:
for idx in indices:
out[idx] = logits[i]
i += 1
return out.reshape(-1, 1)
def average_pooling(self, hidden_state, attention_mask):
extended_attention_mask = (
attention_mask.unsqueeze(-1)
.expand(hidden_state.size())
.to(dtype=hidden_state.dtype)
)
masked_hidden_state = hidden_state * extended_attention_mask
sum_embeddings = torch.sum(masked_hidden_state, dim=1)
sum_mask = extended_attention_mask.sum(dim=1)
return sum_embeddings / sum_mask
@classmethod
def from_pretrained(
cls, model_name_or_path, config: Optional[ListConRankerConfig] = None, **kwargs
):
model = super().from_pretrained(model_name_or_path, config=config, **kwargs)
model.hf_model = BertModel.from_pretrained(
model_name_or_path, config=model.config.bert_config, **kwargs
)
linear_path = hf_hub_download(
repo_id = model_name_or_path,
filename = "linear_in_embedding.pt",
revision = "main",
cache_dir = kwargs['cache_dir'] if 'cache_dir' in kwargs else None
)
list_transformer_path = hf_hub_download(
repo_id = "ByteDance/ListConRanker",
filename = "list_transformer.pt",
revision = "main",
cache_dir = kwargs['cache_dir'] if 'cache_dir' in kwargs else None
)
try:
model.linear_in_embedding.load_state_dict(torch.load(linear_path))
model.list_transformer.load_state_dict(torch.load(list_transformer_path))
except FileNotFoundError as e:
raise e
return model
def multi_passage(
self,
sentences: List[List[str]],
batch_size: int = 32,
tokenizer: AutoTokenizer = AutoTokenizer.from_pretrained(
"ByteDance/ListConRanker"
),
):
"""
Process multiple passages for each query.
:param sentences: List of lists, where each inner list contains sentences for a query.
:return: Tensor of logits for each passage.
"""
pairs = []
for batch in sentences:
if len(batch) < 2:
raise ValueError("Each query must have at least one passage.")
query = batch[0]
passages = batch[1:]
for passage in passages:
pairs.append((query, passage))
total_batches = (len(pairs) + batch_size - 1) // batch_size
total_logits = torch.zeros(len(pairs), dtype=torch.float, device=self.device)
for batch in range(total_batches):
batch_pairs = pairs[batch * batch_size : (batch + 1) * batch_size]
inputs = tokenizer(
batch_pairs,
padding=True,
truncation=False,
return_tensors="pt",
)
for k, v in inputs.items():
inputs[k] = v.to(self.device)
logits = self(**inputs)[0]
total_logits[batch * batch_size : (batch + 1) * batch_size] = (
logits.squeeze(1)
)
return total_logits.tolist()
def multi_passage_in_iterative_inference(
self,
sentences: List[str],
stop_num: int = 20,
decrement_rate: float = 0.2,
min_filter_num: int = 10,
tokenizer: AutoTokenizer = AutoTokenizer.from_pretrained(
"ByteDance/ListConRanker"
),
):
"""
Process multiple passages for one query in iterative inference.
:param sentences: List contains sentences for a query.
:return: Tensor of logits for each passage.
"""
if stop_num < 1:
raise ValueError("stop_num must be greater than 0")
if decrement_rate <= 0 or decrement_rate >= 1:
raise ValueError("decrement_rate must be in (0, 1)")
if min_filter_num < 1:
raise ValueError("min_filter_num must be greater than 0")
query = sentences[0]
passage = sentences[1:]
filter_times = 0
passage2score = defaultdict(list)
while len(passage) > stop_num:
batch = [[query] + passage]
pred_scores = self.multi_passage(
batch, batch_size=len(batch[0]) - 1, tokenizer=tokenizer
)
pred_scores_argsort = np.argsort(
pred_scores
).tolist() # Sort in increasing order
passage_len = len(passage)
to_filter_num = math.ceil(passage_len * decrement_rate)
if to_filter_num < min_filter_num:
to_filter_num = min_filter_num
have_filter_num = 0
while have_filter_num < to_filter_num:
idx = pred_scores_argsort[have_filter_num]
passage2score[passage[idx]].append(pred_scores[idx] + filter_times)
have_filter_num += 1
while (
pred_scores[pred_scores_argsort[have_filter_num - 1]]
== pred_scores[pred_scores_argsort[have_filter_num]]
):
idx = pred_scores_argsort[have_filter_num]
passage2score[passage[idx]].append(pred_scores[idx] + filter_times)
have_filter_num += 1
next_passage = []
next_passage_idx = have_filter_num
while next_passage_idx < len(passage):
idx = pred_scores_argsort[next_passage_idx]
next_passage.append(passage[idx])
next_passage_idx += 1
passage = next_passage
filter_times += 1
batch = [[query] + passage]
pred_scores = self.multi_passage(
batch, batch_size=len(batch[0]) - 1, tokenizer=tokenizer
)
cnt = 0
while cnt < len(passage):
passage2score[passage[cnt]].append(pred_scores[cnt] + filter_times)
cnt += 1
passage = sentences[1:]
final_score = []
for i in range(len(passage)):
p = passage[i]
final_score.append(passage2score[p][0])
return final_score |