Add nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData
Browse filesUpload fine-tuned rerankers for BioASQ 14B
Co-authored-by: André Ribeiro <andrepedro2004@hotmail.com>
Co-authored-by: Rúben Garrido <rubengarrido@ua.pt>
- .gitattributes +1 -0
- nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/config.json +48 -0
- nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/llama_bidirectional_model.py +362 -0
- nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/model.safetensors +3 -0
- nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/ranx_results.json +49 -0
- nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/tokenizer.json +3 -0
- nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/tokenizer_config.json +14 -0
- nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/trainer_state.json +126 -0
- nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/training_args.bin +3 -0
.gitattributes
CHANGED
|
@@ -35,3 +35,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
BAAI-bge-reranker-base-E2-S1-Mpairwise-FullDataTrue/tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
| 37 |
BAAI-bge-reranker-v2-m3-E2-S1-Mpairwise-FullDataTrue/tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
BAAI-bge-reranker-base-E2-S1-Mpairwise-FullDataTrue/tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
| 37 |
BAAI-bge-reranker-v2-m3-E2-S1-Mpairwise-FullDataTrue/tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/config.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"LlamaBidirectionalForSequenceClassification"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"auto_map": {
|
| 8 |
+
"AutoConfig": "llama_bidirectional_model.LlamaBidirectionalConfig",
|
| 9 |
+
"AutoModelForSequenceClassification": "llama_bidirectional_model.LlamaBidirectionalForSequenceClassification"
|
| 10 |
+
},
|
| 11 |
+
"bos_token_id": 128000,
|
| 12 |
+
"dtype": "bfloat16",
|
| 13 |
+
"eos_token_id": 128001,
|
| 14 |
+
"head_dim": 64,
|
| 15 |
+
"hidden_act": "silu",
|
| 16 |
+
"hidden_size": 2048,
|
| 17 |
+
"id2label": {
|
| 18 |
+
"0": "LABEL_0"
|
| 19 |
+
},
|
| 20 |
+
"initializer_range": 0.02,
|
| 21 |
+
"intermediate_size": 8192,
|
| 22 |
+
"label2id": {
|
| 23 |
+
"LABEL_0": 0
|
| 24 |
+
},
|
| 25 |
+
"max_position_embeddings": 131072,
|
| 26 |
+
"mlp_bias": false,
|
| 27 |
+
"model_type": "llama_bidirec",
|
| 28 |
+
"num_attention_heads": 32,
|
| 29 |
+
"num_hidden_layers": 16,
|
| 30 |
+
"num_key_value_heads": 8,
|
| 31 |
+
"pad_token_id": 128001,
|
| 32 |
+
"pooling": "avg",
|
| 33 |
+
"pretraining_tp": 1,
|
| 34 |
+
"rms_norm_eps": 1e-05,
|
| 35 |
+
"rope_parameters": {
|
| 36 |
+
"factor": 32.0,
|
| 37 |
+
"high_freq_factor": 4.0,
|
| 38 |
+
"low_freq_factor": 1.0,
|
| 39 |
+
"original_max_position_embeddings": 8192,
|
| 40 |
+
"rope_theta": 500000.0,
|
| 41 |
+
"rope_type": "llama3"
|
| 42 |
+
},
|
| 43 |
+
"temperature": 1.0,
|
| 44 |
+
"tie_word_embeddings": true,
|
| 45 |
+
"transformers_version": "5.2.0",
|
| 46 |
+
"use_cache": false,
|
| 47 |
+
"vocab_size": 128256
|
| 48 |
+
}
|
nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/llama_bidirectional_model.py
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
| 2 |
+
# SPDX-License-Identifier: Apache-2.0.
|
| 3 |
+
"""
|
| 4 |
+
Bidirectional Llama model for cross-encoder reranking.
|
| 5 |
+
|
| 6 |
+
Modifies LlamaModel to use bidirectional (non-causal) attention so each token
|
| 7 |
+
attends to all others — required for cross-encoder scoring of query-document pairs.
|
| 8 |
+
|
| 9 |
+
Provides three classes:
|
| 10 |
+
- LlamaBidirectionalConfig: Adds pooling and temperature to LlamaConfig.
|
| 11 |
+
- LlamaBidirectionalModel: LlamaModel with causal masking replaced by
|
| 12 |
+
bidirectional masking. Overrides forward() to support transformers >=4.44.
|
| 13 |
+
- LlamaBidirectionalForSequenceClassification: Pools hidden states and
|
| 14 |
+
projects to a relevance score via a linear head.
|
| 15 |
+
|
| 16 |
+
Transformers version compatibility (>=4.44 including 5.0+):
|
| 17 |
+
The forward() implementation handles these API changes at import time via
|
| 18 |
+
inspect.signature() on LlamaDecoderLayer and DynamicCache:
|
| 19 |
+
|
| 20 |
+
< 4.53: _update_causal_mask exists on LlamaModel (not used here).
|
| 21 |
+
4.53+: Masking moved to masking_utils; requires full forward() override.
|
| 22 |
+
< 4.54: Decoder layer returns a tuple.
|
| 23 |
+
4.54+: Decoder layer returns a tensor.
|
| 24 |
+
< 4.56: Cache kwarg is ``past_key_value`` (singular).
|
| 25 |
+
4.56+: Cache kwarg is ``past_key_values`` (plural); DynamicCache accepts config.
|
| 26 |
+
5.0+: Native ``create_bidirectional_mask`` in masking_utils.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
import inspect
|
| 30 |
+
from typing import Optional, Union, Tuple, List
|
| 31 |
+
|
| 32 |
+
import torch
|
| 33 |
+
import torch.nn as nn
|
| 34 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
| 35 |
+
from transformers.modeling_outputs import SequenceClassifierOutputWithPast
|
| 36 |
+
from transformers.cache_utils import Cache, DynamicCache
|
| 37 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast
|
| 38 |
+
from transformers.models.llama.configuration_llama import LlamaConfig
|
| 39 |
+
from transformers.models.llama.modeling_llama import (
|
| 40 |
+
LlamaDecoderLayer,
|
| 41 |
+
LlamaModel,
|
| 42 |
+
LlamaPreTrainedModel,
|
| 43 |
+
)
|
| 44 |
+
from transformers.utils import logging
|
| 45 |
+
|
| 46 |
+
logger = logging.get_logger(__name__)
|
| 47 |
+
|
| 48 |
+
# Check if native create_bidirectional_mask exists (transformers >= 5.0)
|
| 49 |
+
try:
|
| 50 |
+
from transformers.masking_utils import create_bidirectional_mask
|
| 51 |
+
|
| 52 |
+
_HAS_NATIVE_BIDIRECTIONAL_MASK = True
|
| 53 |
+
except ImportError:
|
| 54 |
+
from transformers.modeling_attn_mask_utils import _prepare_4d_attention_mask
|
| 55 |
+
|
| 56 |
+
_HAS_NATIVE_BIDIRECTIONAL_MASK = False
|
| 57 |
+
|
| 58 |
+
# Detect API differences via introspection
|
| 59 |
+
_decoder_forward_params = inspect.signature(LlamaDecoderLayer.forward).parameters
|
| 60 |
+
_dynamic_cache_init_params = inspect.signature(DynamicCache.__init__).parameters
|
| 61 |
+
|
| 62 |
+
# past_key_value (singular) in < 4.56, past_key_values (plural) in >= 4.56
|
| 63 |
+
_USE_PLURAL_CACHE_PARAM = "past_key_values" in _decoder_forward_params
|
| 64 |
+
# DynamicCache accepts config parameter in >= 4.56
|
| 65 |
+
_DYNAMIC_CACHE_ACCEPTS_CONFIG = "config" in _dynamic_cache_init_params
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class LlamaBidirectionalConfig(LlamaConfig):
|
| 69 |
+
"""Configuration for LlamaBidirectionalModel with pooling and temperature settings."""
|
| 70 |
+
|
| 71 |
+
model_type = "llama_bidirec"
|
| 72 |
+
|
| 73 |
+
def __init__(
|
| 74 |
+
self, pooling: str = "avg", temperature: float = 1.0, **kwargs
|
| 75 |
+
) -> None:
|
| 76 |
+
"""
|
| 77 |
+
Initialize bidirectional Llama configuration.
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
pooling: Pooling strategy for embeddings ("avg", "cls", "last", etc.)
|
| 81 |
+
temperature: Temperature scaling for embeddings
|
| 82 |
+
**kwargs: Additional arguments passed to LlamaConfig
|
| 83 |
+
"""
|
| 84 |
+
self.pooling = pooling
|
| 85 |
+
self.temperature = temperature
|
| 86 |
+
super().__init__(**kwargs)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class LlamaBidirectionalModel(LlamaModel):
|
| 90 |
+
"""
|
| 91 |
+
LlamaModel modified to use bidirectional (non-causal) attention.
|
| 92 |
+
|
| 93 |
+
In standard Llama, each token can only attend to previous tokens (causal attention).
|
| 94 |
+
This model removes that restriction, allowing each token to attend to all tokens
|
| 95 |
+
in the sequence, which is useful for embedding tasks.
|
| 96 |
+
|
| 97 |
+
The key modifications are:
|
| 98 |
+
1. Setting is_causal=False on all attention layers
|
| 99 |
+
2. Using a bidirectional attention mask instead of causal mask
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
config_class = LlamaBidirectionalConfig
|
| 103 |
+
|
| 104 |
+
def __init__(self, config: LlamaConfig) -> None:
|
| 105 |
+
super().__init__(config)
|
| 106 |
+
for layer in self.layers:
|
| 107 |
+
layer.self_attn.is_causal = False
|
| 108 |
+
|
| 109 |
+
def _create_bidirectional_mask(
|
| 110 |
+
self,
|
| 111 |
+
input_embeds: torch.Tensor,
|
| 112 |
+
attention_mask: torch.Tensor | None,
|
| 113 |
+
) -> torch.Tensor | None:
|
| 114 |
+
"""
|
| 115 |
+
Create bidirectional attention mask.
|
| 116 |
+
|
| 117 |
+
Args:
|
| 118 |
+
input_embeds: Input embeddings tensor of shape (batch_size, seq_len, hidden_size)
|
| 119 |
+
attention_mask: Optional 2D attention mask of shape (batch_size, seq_len)
|
| 120 |
+
where 1 indicates tokens to attend to and 0 indicates masked tokens
|
| 121 |
+
|
| 122 |
+
Returns:
|
| 123 |
+
4D attention mask suitable for the attention implementation, or None
|
| 124 |
+
if no masking is needed
|
| 125 |
+
"""
|
| 126 |
+
if attention_mask is None:
|
| 127 |
+
return None
|
| 128 |
+
|
| 129 |
+
if _HAS_NATIVE_BIDIRECTIONAL_MASK:
|
| 130 |
+
return create_bidirectional_mask(
|
| 131 |
+
config=self.config,
|
| 132 |
+
input_embeds=input_embeds,
|
| 133 |
+
attention_mask=attention_mask,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Fallback for transformers < 5.0 without create_bidirectional_mask
|
| 137 |
+
|
| 138 |
+
# Flash attention handles 2D masks internally; only pass mask if there
|
| 139 |
+
# are actually masked tokens (zeros), otherwise return None for efficiency
|
| 140 |
+
if getattr(self.config, "_attn_implementation", None) == "flash_attention_2":
|
| 141 |
+
has_masked_tokens = (attention_mask == 0).any()
|
| 142 |
+
return attention_mask if has_masked_tokens else None
|
| 143 |
+
|
| 144 |
+
return _prepare_4d_attention_mask(attention_mask, input_embeds.dtype)
|
| 145 |
+
|
| 146 |
+
def forward(
|
| 147 |
+
self,
|
| 148 |
+
input_ids: torch.LongTensor | None = None,
|
| 149 |
+
attention_mask: torch.Tensor | None = None,
|
| 150 |
+
position_ids: torch.LongTensor | None = None,
|
| 151 |
+
past_key_values: Cache | None = None,
|
| 152 |
+
inputs_embeds: torch.FloatTensor | None = None,
|
| 153 |
+
cache_position: torch.LongTensor | None = None,
|
| 154 |
+
use_cache: bool | None = None,
|
| 155 |
+
**kwargs,
|
| 156 |
+
) -> BaseModelOutputWithPast:
|
| 157 |
+
"""
|
| 158 |
+
Forward pass with bidirectional attention.
|
| 159 |
+
|
| 160 |
+
Args:
|
| 161 |
+
input_ids: Input token IDs of shape (batch_size, seq_len)
|
| 162 |
+
attention_mask: Attention mask of shape (batch_size, seq_len)
|
| 163 |
+
position_ids: Position IDs for rotary embeddings
|
| 164 |
+
past_key_values: Cached key/value states for incremental decoding
|
| 165 |
+
inputs_embeds: Pre-computed input embeddings (alternative to input_ids)
|
| 166 |
+
cache_position: Position indices for cache updates
|
| 167 |
+
use_cache: Whether to return cached key/value states
|
| 168 |
+
**kwargs: Additional arguments passed to decoder layers
|
| 169 |
+
|
| 170 |
+
Returns:
|
| 171 |
+
BaseModelOutputWithPast containing last_hidden_state and past_key_values
|
| 172 |
+
"""
|
| 173 |
+
if (input_ids is None) ^ (inputs_embeds is not None):
|
| 174 |
+
raise ValueError(
|
| 175 |
+
"You must specify exactly one of input_ids or inputs_embeds"
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
if inputs_embeds is None:
|
| 179 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
| 180 |
+
|
| 181 |
+
# Initialize cache if needed
|
| 182 |
+
if use_cache and past_key_values is None:
|
| 183 |
+
if _DYNAMIC_CACHE_ACCEPTS_CONFIG:
|
| 184 |
+
past_key_values = DynamicCache(config=self.config)
|
| 185 |
+
else:
|
| 186 |
+
past_key_values = DynamicCache()
|
| 187 |
+
|
| 188 |
+
if cache_position is None:
|
| 189 |
+
past_seen_tokens = (
|
| 190 |
+
past_key_values.get_seq_length() if past_key_values is not None else 0
|
| 191 |
+
)
|
| 192 |
+
cache_position = torch.arange(
|
| 193 |
+
past_seen_tokens,
|
| 194 |
+
past_seen_tokens + inputs_embeds.shape[1],
|
| 195 |
+
device=inputs_embeds.device,
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
if position_ids is None:
|
| 199 |
+
position_ids = cache_position.unsqueeze(0)
|
| 200 |
+
|
| 201 |
+
bidirectional_mask = self._create_bidirectional_mask(
|
| 202 |
+
inputs_embeds, attention_mask
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
hidden_states = inputs_embeds
|
| 206 |
+
position_embeddings = self.rotary_emb(hidden_states, position_ids)
|
| 207 |
+
|
| 208 |
+
# Build decoder layer kwargs with correct cache parameter name
|
| 209 |
+
# (past_key_value in < 4.56, past_key_values in >= 4.56)
|
| 210 |
+
layer_kwargs = {
|
| 211 |
+
"attention_mask": bidirectional_mask,
|
| 212 |
+
"position_ids": position_ids,
|
| 213 |
+
"use_cache": use_cache,
|
| 214 |
+
"cache_position": cache_position,
|
| 215 |
+
"position_embeddings": position_embeddings,
|
| 216 |
+
}
|
| 217 |
+
if _USE_PLURAL_CACHE_PARAM:
|
| 218 |
+
layer_kwargs["past_key_values"] = past_key_values
|
| 219 |
+
else:
|
| 220 |
+
layer_kwargs["past_key_value"] = past_key_values
|
| 221 |
+
|
| 222 |
+
for decoder_layer in self.layers[: self.config.num_hidden_layers]:
|
| 223 |
+
layer_outputs = decoder_layer(hidden_states, **layer_kwargs)
|
| 224 |
+
|
| 225 |
+
# Decoder returns tuple in < 4.54, tensor in >= 4.54
|
| 226 |
+
if isinstance(layer_outputs, tuple):
|
| 227 |
+
hidden_states = layer_outputs[0]
|
| 228 |
+
else:
|
| 229 |
+
hidden_states = layer_outputs
|
| 230 |
+
|
| 231 |
+
hidden_states = self.norm(hidden_states)
|
| 232 |
+
|
| 233 |
+
return BaseModelOutputWithPast(
|
| 234 |
+
last_hidden_state=hidden_states,
|
| 235 |
+
past_key_values=past_key_values,
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def pool(
|
| 240 |
+
last_hidden_states: torch.Tensor, attention_mask: torch.Tensor, pool_type: str
|
| 241 |
+
) -> torch.Tensor:
|
| 242 |
+
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
|
| 243 |
+
|
| 244 |
+
if pool_type == "avg":
|
| 245 |
+
emb = last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
|
| 246 |
+
elif pool_type == "weighted_avg":
|
| 247 |
+
emb = last_hidden.sum(dim=1)
|
| 248 |
+
elif pool_type == "cls":
|
| 249 |
+
emb = last_hidden[:, 0]
|
| 250 |
+
elif pool_type == "last":
|
| 251 |
+
left_padding = attention_mask[:, -1].sum() == attention_mask.shape[0]
|
| 252 |
+
if left_padding:
|
| 253 |
+
emb = last_hidden[:, -1]
|
| 254 |
+
else:
|
| 255 |
+
sequence_lengths = attention_mask.sum(dim=1) - 1
|
| 256 |
+
batch_size = last_hidden.shape[0]
|
| 257 |
+
emb = last_hidden[
|
| 258 |
+
torch.arange(batch_size, device=last_hidden.device), sequence_lengths
|
| 259 |
+
]
|
| 260 |
+
else:
|
| 261 |
+
raise ValueError(f"pool_type {pool_type} not supported")
|
| 262 |
+
|
| 263 |
+
return emb
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
class LlamaBidirectionalForSequenceClassification(LlamaPreTrainedModel):
|
| 267 |
+
config_class = LlamaBidirectionalConfig
|
| 268 |
+
|
| 269 |
+
def __init__(self, config):
|
| 270 |
+
super().__init__(config)
|
| 271 |
+
self.num_labels = config.num_labels
|
| 272 |
+
self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
|
| 273 |
+
self.model = LlamaBidirectionalModel(config)
|
| 274 |
+
|
| 275 |
+
# Initialize weights and apply final processing
|
| 276 |
+
self.post_init()
|
| 277 |
+
|
| 278 |
+
def forward(
|
| 279 |
+
self,
|
| 280 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 281 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 282 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 283 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 284 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 285 |
+
labels: Optional[torch.LongTensor] = None,
|
| 286 |
+
use_cache: Optional[bool] = None,
|
| 287 |
+
output_attentions: Optional[bool] = None,
|
| 288 |
+
output_hidden_states: Optional[bool] = None,
|
| 289 |
+
return_dict: Optional[bool] = None,
|
| 290 |
+
**kwargs,
|
| 291 |
+
) -> Union[Tuple, SequenceClassifierOutputWithPast]:
|
| 292 |
+
r"""
|
| 293 |
+
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
| 294 |
+
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
| 295 |
+
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
| 296 |
+
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
| 297 |
+
"""
|
| 298 |
+
return_dict = (
|
| 299 |
+
return_dict if return_dict is not None else self.config.use_return_dict
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
transformer_outputs = self.model(
|
| 303 |
+
input_ids,
|
| 304 |
+
attention_mask=attention_mask,
|
| 305 |
+
position_ids=position_ids,
|
| 306 |
+
past_key_values=past_key_values,
|
| 307 |
+
inputs_embeds=inputs_embeds,
|
| 308 |
+
use_cache=use_cache,
|
| 309 |
+
output_attentions=output_attentions,
|
| 310 |
+
output_hidden_states=output_hidden_states,
|
| 311 |
+
return_dict=return_dict,
|
| 312 |
+
**kwargs,
|
| 313 |
+
)
|
| 314 |
+
hidden_states = transformer_outputs[0]
|
| 315 |
+
|
| 316 |
+
pooled_hidden_states = pool(
|
| 317 |
+
last_hidden_states=hidden_states,
|
| 318 |
+
attention_mask=attention_mask,
|
| 319 |
+
pool_type=self.config.pooling,
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
pooled_logits = self.score(pooled_hidden_states)
|
| 323 |
+
pooled_logits = pooled_logits / self.config.temperature
|
| 324 |
+
|
| 325 |
+
loss = None
|
| 326 |
+
if labels is not None:
|
| 327 |
+
labels = labels.to(pooled_logits.device)
|
| 328 |
+
if self.config.problem_type is None:
|
| 329 |
+
if self.num_labels == 1:
|
| 330 |
+
self.config.problem_type = "regression"
|
| 331 |
+
elif self.num_labels > 1 and (
|
| 332 |
+
labels.dtype == torch.long or labels.dtype == torch.int
|
| 333 |
+
):
|
| 334 |
+
self.config.problem_type = "single_label_classification"
|
| 335 |
+
else:
|
| 336 |
+
self.config.problem_type = "multi_label_classification"
|
| 337 |
+
|
| 338 |
+
if self.config.problem_type == "regression":
|
| 339 |
+
loss_fct = MSELoss()
|
| 340 |
+
if self.num_labels == 1:
|
| 341 |
+
loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
|
| 342 |
+
else:
|
| 343 |
+
loss = loss_fct(pooled_logits, labels)
|
| 344 |
+
elif self.config.problem_type == "single_label_classification":
|
| 345 |
+
loss_fct = CrossEntropyLoss()
|
| 346 |
+
loss = loss_fct(
|
| 347 |
+
pooled_logits.view(-1, self.num_labels), labels.view(-1)
|
| 348 |
+
)
|
| 349 |
+
elif self.config.problem_type == "multi_label_classification":
|
| 350 |
+
loss_fct = BCEWithLogitsLoss()
|
| 351 |
+
loss = loss_fct(pooled_logits, labels)
|
| 352 |
+
if not return_dict:
|
| 353 |
+
output = (pooled_logits,) + transformer_outputs[1:]
|
| 354 |
+
return ((loss,) + output) if loss is not None else output
|
| 355 |
+
|
| 356 |
+
return SequenceClassifierOutputWithPast(
|
| 357 |
+
loss=loss,
|
| 358 |
+
logits=pooled_logits,
|
| 359 |
+
past_key_values=transformer_outputs.past_key_values,
|
| 360 |
+
hidden_states=transformer_outputs.hidden_states,
|
| 361 |
+
attentions=transformer_outputs.attentions,
|
| 362 |
+
)
|
nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f87836a17731e8fb71b35529a9768b148bd47800f0e667a3618ff6190dddf4aa
|
| 3 |
+
size 2471649792
|
nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/ranx_results.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model": "nvidia-llama-nemotron-rerank-1b-v2",
|
| 3 |
+
"val_files": [
|
| 4 |
+
"../../data/val_data/13B3_golden.json",
|
| 5 |
+
"../../data/val_data/13B1_golden.json",
|
| 6 |
+
"../../data/val_data/13B2_golden.json",
|
| 7 |
+
"../../data/val_data/13B4_golden.json"
|
| 8 |
+
],
|
| 9 |
+
"total": {
|
| 10 |
+
"ndcg@5": 0.9996141027131991,
|
| 11 |
+
"mrr": 1.0,
|
| 12 |
+
"recall@10": 0.5881809405568562,
|
| 13 |
+
"recall@100": 0.9990907464893759,
|
| 14 |
+
"map@10": 0.5879983675276444,
|
| 15 |
+
"map-bioasq@10": 0.9994906295851674
|
| 16 |
+
},
|
| 17 |
+
"13B3_golden.json": {
|
| 18 |
+
"ndcg@5": 1.0,
|
| 19 |
+
"mrr": 1.0,
|
| 20 |
+
"recall@10": 0.6106086949554449,
|
| 21 |
+
"recall@100": 0.9996573386636207,
|
| 22 |
+
"map@10": 0.6106086949554449,
|
| 23 |
+
"map-bioasq@10": 1.0
|
| 24 |
+
},
|
| 25 |
+
"13B1_golden.json": {
|
| 26 |
+
"ndcg@5": 0.998456410852796,
|
| 27 |
+
"mrr": 1.0,
|
| 28 |
+
"recall@10": 0.7087873938986045,
|
| 29 |
+
"recall@100": 1.0,
|
| 30 |
+
"map@10": 0.7080571017817577,
|
| 31 |
+
"map-bioasq@10": 0.9979625183406695
|
| 32 |
+
},
|
| 33 |
+
"13B2_golden.json": {
|
| 34 |
+
"ndcg@5": 1.0,
|
| 35 |
+
"mrr": 1.0,
|
| 36 |
+
"recall@10": 0.6088870534178833,
|
| 37 |
+
"recall@100": 0.9996638655462186,
|
| 38 |
+
"map@10": 0.6088870534178833,
|
| 39 |
+
"map-bioasq@10": 1.0
|
| 40 |
+
},
|
| 41 |
+
"13B4_golden.json": {
|
| 42 |
+
"ndcg@5": 1.0,
|
| 43 |
+
"mrr": 1.0,
|
| 44 |
+
"recall@10": 0.4244406199554923,
|
| 45 |
+
"recall@100": 0.997041781747664,
|
| 46 |
+
"map@10": 0.4244406199554923,
|
| 47 |
+
"map-bioasq@10": 1.0
|
| 48 |
+
}
|
| 49 |
+
}
|
nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9c85066e7642934ed09b44155e6566b0b5dab2637fb9433439ba5c9c7f8b50d3
|
| 3 |
+
size 17210018
|
nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/tokenizer_config.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"bos_token": "<|begin_of_text|>",
|
| 4 |
+
"clean_up_tokenization_spaces": true,
|
| 5 |
+
"eos_token": "<|end_of_text|>",
|
| 6 |
+
"is_local": false,
|
| 7 |
+
"model_input_names": [
|
| 8 |
+
"input_ids",
|
| 9 |
+
"attention_mask"
|
| 10 |
+
],
|
| 11 |
+
"model_max_length": 4096,
|
| 12 |
+
"pad_token": "<|end_of_text|>",
|
| 13 |
+
"tokenizer_class": "TokenizersBackend"
|
| 14 |
+
}
|
nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/trainer_state.json
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"best_global_step": null,
|
| 3 |
+
"best_metric": null,
|
| 4 |
+
"best_model_checkpoint": null,
|
| 5 |
+
"epoch": 0.06740815638692282,
|
| 6 |
+
"eval_steps": 500,
|
| 7 |
+
"global_step": 600,
|
| 8 |
+
"is_hyper_param_search": false,
|
| 9 |
+
"is_local_process_zero": true,
|
| 10 |
+
"is_world_process_zero": true,
|
| 11 |
+
"log_history": [
|
| 12 |
+
{
|
| 13 |
+
"epoch": 0.005617346365576902,
|
| 14 |
+
"grad_norm": 16.5,
|
| 15 |
+
"learning_rate": 9.972475002808674e-05,
|
| 16 |
+
"loss": 2.824787292480469,
|
| 17 |
+
"step": 50
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"epoch": 0.011234692731153803,
|
| 21 |
+
"grad_norm": 0.0022125244140625,
|
| 22 |
+
"learning_rate": 9.944388270980789e-05,
|
| 23 |
+
"loss": 0.038616840839385984,
|
| 24 |
+
"step": 100
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"epoch": 0.016852039096730706,
|
| 28 |
+
"grad_norm": 4.867217739956686e-13,
|
| 29 |
+
"learning_rate": 9.916301539152905e-05,
|
| 30 |
+
"loss": 0.0010739766061306,
|
| 31 |
+
"step": 150
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"epoch": 0.022469385462307607,
|
| 35 |
+
"grad_norm": 6.344635039567947e-09,
|
| 36 |
+
"learning_rate": 9.88821480732502e-05,
|
| 37 |
+
"loss": 2.2708853066433222e-07,
|
| 38 |
+
"step": 200
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"epoch": 0.028086731827884508,
|
| 42 |
+
"grad_norm": 20.5,
|
| 43 |
+
"learning_rate": 9.860128075497135e-05,
|
| 44 |
+
"loss": 0.2818438529968262,
|
| 45 |
+
"step": 250
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"epoch": 0.03370407819346141,
|
| 49 |
+
"grad_norm": 0.0,
|
| 50 |
+
"learning_rate": 9.832041343669252e-05,
|
| 51 |
+
"loss": 0.17607328414916992,
|
| 52 |
+
"step": 300
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"epoch": 0.03932142455903831,
|
| 56 |
+
"grad_norm": 0.0004119873046875,
|
| 57 |
+
"learning_rate": 9.803954611841366e-05,
|
| 58 |
+
"loss": 9.271328735351563,
|
| 59 |
+
"step": 350
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"epoch": 0.044938770924615214,
|
| 63 |
+
"grad_norm": 0.0,
|
| 64 |
+
"learning_rate": 9.775867880013482e-05,
|
| 65 |
+
"loss": 0.04490839958190918,
|
| 66 |
+
"step": 400
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"epoch": 0.05055611729019211,
|
| 70 |
+
"grad_norm": 0.0,
|
| 71 |
+
"learning_rate": 9.747781148185597e-05,
|
| 72 |
+
"loss": 0.0,
|
| 73 |
+
"step": 450
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"epoch": 0.056173463655769015,
|
| 77 |
+
"grad_norm": 0.0,
|
| 78 |
+
"learning_rate": 9.719694416357712e-05,
|
| 79 |
+
"loss": 0.24927730560302735,
|
| 80 |
+
"step": 500
|
| 81 |
+
},
|
| 82 |
+
{
|
| 83 |
+
"epoch": 0.06179081002134592,
|
| 84 |
+
"grad_norm": 0.0,
|
| 85 |
+
"learning_rate": 9.691607684529829e-05,
|
| 86 |
+
"loss": 0.0,
|
| 87 |
+
"step": 550
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"epoch": 0.06740815638692282,
|
| 91 |
+
"grad_norm": 1.0244548320770264e-08,
|
| 92 |
+
"learning_rate": 9.663520952701945e-05,
|
| 93 |
+
"loss": 0.13165409088134766,
|
| 94 |
+
"step": 600
|
| 95 |
+
},
|
| 96 |
+
{
|
| 97 |
+
"epoch": 0.06740815638692282,
|
| 98 |
+
"eval_loss": 1.0580704090656923e-09,
|
| 99 |
+
"eval_runtime": 21.5468,
|
| 100 |
+
"eval_samples_per_second": 15.687,
|
| 101 |
+
"eval_steps_per_second": 1.996,
|
| 102 |
+
"step": 600
|
| 103 |
+
}
|
| 104 |
+
],
|
| 105 |
+
"logging_steps": 50,
|
| 106 |
+
"max_steps": 17802,
|
| 107 |
+
"num_input_tokens_seen": 0,
|
| 108 |
+
"num_train_epochs": 2,
|
| 109 |
+
"save_steps": 500,
|
| 110 |
+
"stateful_callbacks": {
|
| 111 |
+
"TrainerControl": {
|
| 112 |
+
"args": {
|
| 113 |
+
"should_epoch_stop": false,
|
| 114 |
+
"should_evaluate": false,
|
| 115 |
+
"should_log": false,
|
| 116 |
+
"should_save": true,
|
| 117 |
+
"should_training_stop": true
|
| 118 |
+
},
|
| 119 |
+
"attributes": {}
|
| 120 |
+
}
|
| 121 |
+
},
|
| 122 |
+
"total_flos": 0.0,
|
| 123 |
+
"train_batch_size": 4,
|
| 124 |
+
"trial_name": null,
|
| 125 |
+
"trial_params": null
|
| 126 |
+
}
|
nvidia-llama-nemotron-rerank-1b-v2-E2-S4-Mmulti_neg_pairwise-Linfonce-FullData/training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:19a8807969141e6e7d9cfd4e3c81f521debc00e53513ee5111e2b2d927655343
|
| 3 |
+
size 5393
|