emrekuruu commited on
Commit
1f73205
·
verified ·
1 Parent(s): c3ed6f0

Delete files configuration_irouterlm.py modeling_irouterlm.py with huggingface_hub

Browse files
Files changed (2) hide show
  1. configuration_irouterlm.py +0 -26
  2. modeling_irouterlm.py +0 -55
configuration_irouterlm.py DELETED
@@ -1,26 +0,0 @@
1
- """IRouterLM Configuration."""
2
- from transformers import PretrainedConfig
3
-
4
- # Standalone copy of train.config.ARM_NAMES: this module is uploaded to the Hub and loaded
5
- # via trust_remote_code, so it cannot import from the training package.
6
- STRATEGY_NAMES = ["MULTIMODAL_RERANK", "MULTIMODAL-SINGLE", "TEXT_RERANK", "TEXT-SINGLE", "BM25"]
7
-
8
-
9
- class IRouterLMConfig(PretrainedConfig):
10
- """Configuration for IRouterLM - a RAG strategy router model."""
11
- model_type = "irouterlm"
12
-
13
- def __init__(
14
- self,
15
- base_model_name: str = "Qwen/Qwen3-0.6B-Base",
16
- hidden_size: int = 1024,
17
- num_labels: int = 5,
18
- classifier_dropout: float = 0.1,
19
- strategy_names: list = None,
20
- **kwargs,
21
- ):
22
- super().__init__(num_labels=num_labels, **kwargs)
23
- self.base_model_name = base_model_name
24
- self.hidden_size = hidden_size
25
- self.classifier_dropout = classifier_dropout
26
- self.strategy_names = strategy_names or STRATEGY_NAMES
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modeling_irouterlm.py DELETED
@@ -1,55 +0,0 @@
1
- """IRouterLM Model."""
2
- import torch
3
- import torch.nn as nn
4
- from transformers import AutoConfig, PreTrainedModel, Qwen3Model
5
- from .configuration_irouterlm import IRouterLMConfig
6
-
7
-
8
- class IRouterLMModel(PreTrainedModel):
9
- """RAG Strategy Router - classifies queries into optimal retrieval strategies."""
10
- config_class = IRouterLMConfig
11
- _no_split_modules = ["Qwen3DecoderLayer"]
12
-
13
- def __init__(self, config: IRouterLMConfig):
14
- super().__init__(config)
15
- # Build the base architecture only; the merged base weights are loaded from this
16
- # checkpoint's model.safetensors by from_pretrained. Calling Qwen3Model.from_pretrained
17
- # here breaks under the meta-device init that from_pretrained uses.
18
- base_config = AutoConfig.from_pretrained(config.base_model_name)
19
- self.transformer = Qwen3Model(base_config)
20
- self.dropout = nn.Dropout(config.classifier_dropout)
21
- self.classifier = nn.Linear(config.hidden_size, config.num_labels)
22
- self.post_init()
23
-
24
- def _init_weights(self, module):
25
- if isinstance(module, nn.Linear):
26
- nn.init.normal_(module.weight, std=0.02)
27
- if module.bias is not None:
28
- nn.init.zeros_(module.bias)
29
-
30
- def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
31
- outputs = self.transformer(input_ids=input_ids, attention_mask=attention_mask, output_hidden_states=True)
32
- hidden = outputs.last_hidden_state
33
- if attention_mask is not None:
34
- mask = attention_mask.unsqueeze(-1).expand(hidden.size()).float()
35
- pooled = (hidden * mask).sum(1) / mask.sum(1).clamp(min=1e-9)
36
- else:
37
- pooled = hidden.mean(dim=1)
38
- logits = self.classifier(self.dropout(pooled))
39
- loss = self._compute_loss(logits, labels) if labels is not None else None
40
- return {"loss": loss, "logits": logits}
41
-
42
- def _compute_loss(self, logits, labels):
43
- labels_norm = labels / (labels.sum(-1, keepdim=True) + 1e-8)
44
- log_probs = torch.nn.functional.log_softmax(logits, dim=-1)
45
- losses = -(labels_norm * log_probs).sum(-1)
46
- return (losses * labels.max(-1)[0]).mean()
47
-
48
- def predict(self, input_ids, attention_mask=None):
49
- self.eval()
50
- with torch.no_grad():
51
- logits = self.forward(input_ids, attention_mask)["logits"]
52
- probs = torch.softmax(logits, dim=-1)
53
- preds = probs.argmax(dim=-1)
54
- return {"predictions": preds, "probabilities": probs,
55
- "strategy_names": [self.config.strategy_names[p.item()] for p in preds]}