mario-rc commited on
Commit
898bb4f
·
verified ·
1 Parent(s): b9a37c8

Release final shared-prompt-gating checkpoint

Browse files

Replace the previous checkpoint with the selected final model and a common model-card format across the six-model family.

README.md CHANGED
@@ -1,130 +1,121 @@
1
  ---
2
  library_name: transformers
3
- base_model: Skywork/Skywork-Reward-V2-Llama-3.1-8B
4
  pipeline_tag: text-classification
5
- language:
6
- - en
7
- datasets:
8
- - RLHFlow/UltraFeedback-preference-standard
9
- - allenai/reward-bench
10
  tags:
11
  - reward-model
12
- - preference-modeling
13
- - rlhf
14
- - multi-domain
15
- - coherence
16
- - commonsense
17
- - empathy
18
- - multicultural
19
  ---
20
 
21
- # Multi-Domain Reward Model Skywork Llama-3.1-8B-Instruct
22
-
23
- This is a multi-domain reward model built from
24
- [`Skywork/Skywork-Reward-V2-Llama-3.1-8B`](https://huggingface.co/Skywork/Skywork-Reward-V2-Llama-3.1-8B).
25
- It combines 23 fine-grained regression objectives across coherence, commonsense,
26
- empathy, and multicultural response quality with a prompt-conditioned gating
27
- network that produces a single preference score.
28
-
29
- The checkpoint was packaged with the custom `RewardModelWithGating`
30
- architecture used in the Multi-Domain Reward Model project.
31
-
32
- Project repository: [`Mario-RC/multi-domain-reward-model`](https://github.com/Mario-RC/multi-domain-reward-model).
33
-
34
- ## Intended Use
35
-
36
- Use this model to score and compare assistant responses when the evaluation
37
- should account for multiple quality dimensions rather than a single generic
38
- helpfulness score. The primary use case is reward modeling or offline response
39
- ranking for chat-style data.
40
-
41
- ## Training Data
42
 
43
- The model uses multi-objective scoring and preference data from:
 
 
 
44
 
45
- - [`multidomain_data_scoring`](https://github.com/mestecha/multidomain_data_scoring)
46
- - [`RLHFlow/UltraFeedback-preference-standard`](https://huggingface.co/datasets/RLHFlow/UltraFeedback-preference-standard)
47
- - [`allenai/reward-bench`](https://huggingface.co/datasets/allenai/reward-bench) for evaluation
48
 
49
- ## Evaluation
 
50
 
51
- Preference accuracy by domain:
 
 
 
 
 
 
 
52
 
53
- | Domain | Accuracy (%) |
54
- | :--- | ---: |
55
- | Coherence | 87.5270 |
56
- | Commonsense | 98.0365 |
57
- | Empathy | 96.2669 |
58
- | Multicultural | 85.2487 |
59
 
60
- ## Hugging Face Models
 
 
 
 
 
 
 
61
 
62
- The packaged multi-domain reward models are available on Hugging Face under the `mario-rc` namespace:
63
 
64
- | Model | Base reward model | Preference accuracy (%) | Scoring Spearman |
65
- | --- | --- | :---: | :---: |
66
- | [`multi-domain-rm-fsfairx-gemma-2-9b-it`](https://huggingface.co/mario-rc/multi-domain-rm-fsfairx-gemma-2-9b-it) | [`sfairXC/FsfairX-Gemma2-RM-v0.1`](https://huggingface.co/sfairXC/FsfairX-Gemma2-RM-v0.1) | 92.56 | 0.7346 |
67
- | [`multi-domain-rm-fsfairx-llama-3-8b-it`](https://huggingface.co/mario-rc/multi-domain-rm-fsfairx-llama-3-8b-it) | [`sfairXC/FsfairX-LLaMA3-RM-v0.1`](https://huggingface.co/sfairXC/FsfairX-LLaMA3-RM-v0.1) | 89.87 | 0.7108 |
68
- | [`multi-domain-rm-mistral-7b-it`](https://huggingface.co/mario-rc/multi-domain-rm-mistral-7b-it) | [`weqweasdas/RM-Mistral-7B`](https://huggingface.co/weqweasdas/RM-Mistral-7B) | 91.69 | 0.6710 |
69
- | [`multi-domain-rm-qwen-3-nemotron-8b-it`](https://huggingface.co/mario-rc/multi-domain-rm-qwen-3-nemotron-8b-it) | [`nvidia/Qwen3-Nemotron-8B-BRRM`](https://huggingface.co/nvidia/Qwen3-Nemotron-8B-BRRM) | 93.36 | 0.6704 |
70
- | [`multi-domain-rm-skywork-llama-3.1-8b-it`](https://huggingface.co/mario-rc/multi-domain-rm-skywork-llama-3.1-8b-it) | [`Skywork/Skywork-Reward-V2-Llama-3.1-8B`](https://huggingface.co/Skywork/Skywork-Reward-V2-Llama-3.1-8B) | 92.69 | 0.7264 |
71
- | [`multi-domain-rm-skywork-qwen-3-8b-it`](https://huggingface.co/mario-rc/multi-domain-rm-skywork-qwen-3-8b-it) | [`Skywork/Skywork-Reward-V2-Qwen3-8B`](https://huggingface.co/Skywork/Skywork-Reward-V2-Qwen3-8B) | 95.72 | 0.7156 |
72
-
73
- ## Usage Example
74
-
75
- This checkpoint uses the project's custom `RewardModelWithGating` class. Run the
76
- example from an environment where `multidomain_model/modeling_custom.py` is
77
- importable.
78
 
79
  ```python
80
  import torch
81
- from transformers import AutoTokenizer
82
- from modeling_custom import RewardModelWithGating
83
-
84
- model_id = "mario-rc/multi-domain-rm-skywork-llama-3.1-8b-it"
85
- dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
86
- device_map = {"": 0} if torch.cuda.is_available() else None
87
-
88
- tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
89
- model = RewardModelWithGating.from_pretrained(
90
- model_id,
91
- device_map=device_map,
92
- dtype=dtype,
93
  ).eval()
94
- device = next(model.parameters()).device
95
 
96
- messages = [
97
- {"role": "user", "content": "I failed an important exam and feel awful."},
98
- {"role": "assistant", "content": "I'm sorry. That is a hard setback, but it does not define your ability. Take a little time to recover, then we can make a concrete study plan for the next attempt."},
99
- ]
100
-
101
- encoded = tokenizer.apply_chat_template(
102
- messages,
 
 
 
 
 
 
 
 
 
 
103
  return_tensors="pt",
104
- padding=True,
105
- truncation=True,
106
- max_length=4096,
107
- )
108
- inputs = {"input_ids": encoded.to(device)} if isinstance(encoded, torch.Tensor) else {
109
- key: value.to(device) for key, value in encoded.items()
110
- }
111
-
112
- with torch.no_grad():
113
- score = model(**inputs).score.float().item()
114
-
115
- print(score)
 
 
 
 
 
 
 
 
116
  ```
117
 
118
- ## Limitations
 
 
 
 
 
 
 
 
 
119
 
120
- This is a reward model, not a standalone chat assistant. Scores are intended for
121
- relative comparison and should be calibrated for each downstream use case. The
122
- model inherits limitations from its base model and from the annotation coverage
123
- of the multi-domain datasets, especially for cultural contexts not represented
124
- in the evaluation data.
125
 
126
- ## Credits
127
 
128
- This model is based on the ArmoRM/RLHFlow reward-modeling approach and adapts it
129
- to custom multi-domain attributes for coherence, commonsense, empathy, and
130
- multicultural response quality.
 
1
  ---
2
  library_name: transformers
 
3
  pipeline_tag: text-classification
4
+ base_model: Skywork/Skywork-Reward-V2-Llama-3.1-8B
 
 
 
 
5
  tags:
6
  - reward-model
7
+ - preference-model
8
+ - multidomain
9
+ - shared-prompt-gating
 
 
 
 
10
  ---
11
 
12
+ # Multi-Domain Reward Model Skywork Llama 3.1 8B
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ This is the final Multi-Domain Reward Model checkpoint built on
15
+ [`Skywork/Skywork-Reward-V2-Llama-3.1-8B`](https://huggingface.co/Skywork/Skywork-Reward-V2-Llama-3.1-8B). It scores responses across
16
+ coherence, commonsense, empathy, and multicultural understanding using 23 fine-grained attributes
17
+ and one prompt-conditioned gate shared by both responses in a preference pair.
18
 
19
+ ## Released model family
 
 
20
 
21
+ All checkpoints use the same held-out set of 15,489 preference pairs. Confidence intervals are 95%
22
+ prompt-cluster bootstrap intervals.
23
 
24
+ | Model | Base reward model | Test accuracy | 95% CI |
25
+ |---|---|---:|---:|
26
+ | [FsfairX Gemma 2 9B](https://huggingface.co/mario-rc/multi-domain-rm-fsfairx-gemma-2-9b-it) | [sfairXC/FsfairX-Gemma2-RM-v0.1](https://huggingface.co/sfairXC/FsfairX-Gemma2-RM-v0.1) | **88.01%** | [87.43, 88.59] |
27
+ | [Skywork Qwen 3 8B](https://huggingface.co/mario-rc/multi-domain-rm-skywork-qwen-3-8b-it) | [Skywork/Skywork-Reward-V2-Qwen3-8B](https://huggingface.co/Skywork/Skywork-Reward-V2-Qwen3-8B) | **87.82%** | [87.24, 88.40] |
28
+ | [FsfairX Llama 3 8B](https://huggingface.co/mario-rc/multi-domain-rm-fsfairx-llama-3-8b-it) | [sfairXC/FsfairX-LLaMA3-RM-v0.1](https://huggingface.co/sfairXC/FsfairX-LLaMA3-RM-v0.1) | **86.86%** | [86.24, 87.48] |
29
+ | [Skywork Llama 3.1 8B](https://huggingface.co/mario-rc/multi-domain-rm-skywork-llama-3.1-8b-it) | [Skywork/Skywork-Reward-V2-Llama-3.1-8B](https://huggingface.co/Skywork/Skywork-Reward-V2-Llama-3.1-8B) | **86.82%** | [86.20, 87.43] |
30
+ | [Mistral 7B](https://huggingface.co/mario-rc/multi-domain-rm-mistral-7b-it) | [weqweasdas/RM-Mistral-7B](https://huggingface.co/weqweasdas/RM-Mistral-7B) | **84.41%** | [83.75, 85.06] |
31
+ | [Qwen 3 Nemotron 8B](https://huggingface.co/mario-rc/multi-domain-rm-qwen-3-nemotron-8b-it) | [nvidia/Qwen3-Nemotron-8B-BRRM](https://huggingface.co/nvidia/Qwen3-Nemotron-8B-BRRM) | **83.65%** | [82.96, 84.30] |
32
 
33
+ ## This checkpoint
 
 
 
 
 
34
 
35
+ | Metric | Result |
36
+ |---|---:|
37
+ | Held-out preference accuracy | **86.82%** |
38
+ | 95% confidence interval | [86.20, 87.43] |
39
+ | Coherence accuracy | 75.59% |
40
+ | Commonsense accuracy | 97.39% |
41
+ | Empathy accuracy | 92.72% |
42
+ | Multicultural accuracy | 74.99% |
43
 
44
+ ## Usage
45
 
46
+ The repository includes custom Transformers code, so `trust_remote_code=True` is required. Compute
47
+ the gate once from the prompt and reuse that tensor when scoring both complete candidates.
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  ```python
50
  import torch
51
+ from transformers import AutoModel, AutoTokenizer
52
+
53
+ repo_id = "mario-rc/multi-domain-rm-skywork-llama-3.1-8b-it"
54
+ tokenizer = AutoTokenizer.from_pretrained(repo_id, trust_remote_code=True)
55
+ model = AutoModel.from_pretrained(
56
+ repo_id,
57
+ torch_dtype=torch.bfloat16,
58
+ device_map="auto",
59
+ trust_remote_code=True,
 
 
 
60
  ).eval()
 
61
 
62
+ prompt = [{"role": "user", "content": "How can I support a friend who feels excluded?"}]
63
+ chosen = prompt + [{
64
+ "role": "assistant",
65
+ "content": "Listen without judging, validate how they feel, and ask what support would help.",
66
+ }]
67
+ rejected = prompt + [{"role": "assistant", "content": "Tell them to ignore it."}]
68
+
69
+ prompt_ids = tokenizer.apply_chat_template(
70
+ prompt,
71
+ tokenize=True,
72
+ add_generation_prompt=True,
73
+ return_tensors="pt",
74
+ ).to(model.device)
75
+ chosen_ids = tokenizer.apply_chat_template(
76
+ chosen,
77
+ tokenize=True,
78
+ add_generation_prompt=False,
79
  return_tensors="pt",
80
+ ).to(model.device)
81
+ rejected_ids = tokenizer.apply_chat_template(
82
+ rejected,
83
+ tokenize=True,
84
+ add_generation_prompt=False,
85
+ return_tensors="pt",
86
+ ).to(model.device)
87
+
88
+ with torch.inference_mode():
89
+ gate = model.compute_gating(input_ids=prompt_ids)
90
+ chosen_score = model(
91
+ input_ids=chosen_ids,
92
+ gating_output_override=gate,
93
+ ).score
94
+ rejected_score = model(
95
+ input_ids=rejected_ids,
96
+ gating_output_override=gate,
97
+ ).score
98
+
99
+ print({"chosen": chosen_score.item(), "rejected": rejected_score.item()})
100
  ```
101
 
102
+ For padded batches, also pass the matching `attention_mask`. Scores are intended for comparison
103
+ within a prompt; they are not calibrated probabilities or universal utility values.
104
+
105
+ ## Training data
106
+
107
+ The model was trained with the
108
+ [Multi-Domain-Data-Scoring and Multi-Domain-Data-Preference-Pairs](https://github.com/mestecha/multidomain_data_scoring)
109
+ datasets. Users are responsible for reviewing the source data and applicable terms.
110
+
111
+ ## Intended use and limitations
112
 
113
+ This checkpoint is intended for research on preference ranking, reward modeling, reranking, and
114
+ alignment evaluation. Performance differs by domain, language, topic, and distribution. It should
115
+ not be used as the sole decision-maker in high-impact settings, and outputs should be audited for
116
+ biases inherited from the base model and training data.
 
117
 
118
+ ## License
119
 
120
+ The project code is released under Apache-2.0. Use of this checkpoint is also subject to the license
121
+ and usage conditions of the base model and training datasets.
 
config.json CHANGED
@@ -4,6 +4,9 @@
4
  ],
5
  "attention_bias": false,
6
  "attention_dropout": 0.0,
 
 
 
7
  "bos_token_id": 128000,
8
  "dtype": "bfloat16",
9
  "eos_token_id": [
@@ -11,7 +14,35 @@
11
  128008,
12
  128009
13
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  "gating_hidden_dim": 64,
 
 
15
  "gating_n_hidden": 1,
16
  "gating_temperature": 2.0,
17
  "head_dim": 128,
@@ -43,6 +74,7 @@
43
  "rope_theta": 500000.0,
44
  "rope_type": "llama3"
45
  },
 
46
  "tie_word_embeddings": false,
47
  "transformers_version": "5.3.0",
48
  "use_cache": false,
 
4
  ],
5
  "attention_bias": false,
6
  "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoModel": "modeling_custom.RewardModelWithGating"
9
+ },
10
  "bos_token_id": 128000,
11
  "dtype": "bfloat16",
12
  "eos_token_id": [
 
14
  128008,
15
  128009
16
  ],
17
+ "gating_active_attribute_indices": [
18
+ 0,
19
+ 1,
20
+ 2,
21
+ 3,
22
+ 4,
23
+ 5,
24
+ 6,
25
+ 7,
26
+ 8,
27
+ 9,
28
+ 10,
29
+ 11,
30
+ 12,
31
+ 13,
32
+ 14,
33
+ 15,
34
+ 16,
35
+ 17,
36
+ 18,
37
+ 19,
38
+ 20,
39
+ 21,
40
+ 22
41
+ ],
42
+ "gating_dropout": 0.05,
43
  "gating_hidden_dim": 64,
44
+ "gating_learnable_logit_scale": false,
45
+ "gating_logit_scale": 2.0,
46
  "gating_n_hidden": 1,
47
  "gating_temperature": 2.0,
48
  "head_dim": 128,
 
74
  "rope_theta": 500000.0,
75
  "rope_type": "llama3"
76
  },
77
+ "shared_prompt_gating": true,
78
  "tie_word_embeddings": false,
79
  "transformers_version": "5.3.0",
80
  "use_cache": false,
results/plots/spearman_by_attribute.png → model-00001-of-00004.safetensors RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:c2a36a5270f9be69eed9ce5bb404f24747367b9e1865fa820859dffc6c88f152
3
- size 119865
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:86ca401765f707e08ad005924b409d4db9d332a95001c903a56bd1df40368657
3
+ size 3903440936
results/plots/spearman_by_attribute_baseline.png → model-00002-of-00004.safetensors RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:3070c0c9916a1d9d93ba3ecfe7ad627516fdb81d18644d46e204fae60aabb01e
3
- size 120059
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e7f5a022674451e32d3ff897b44ed31b8beee226a53bd9b6dae31ca3e73805ad
3
+ size 3926025408
model.safetensors → model-00003-of-00004.safetensors RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:1ce4805f114045ff00f23a13f9d7e8ac46b33ff7a7f4f345182304f7f2e3b8d4
3
- size 15010600834
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c5d03ca2426551235ee495d39c50fd0208e1d987df22ecd9617f5a36fcce68e9
3
+ size 3926025448
model-00004-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1129c15e9ea55604869030314294940920965e497103974ce620e67c0477e9eb
3
+ size 3255108794
model.safetensors.index.json ADDED
@@ -0,0 +1,305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_parameters": 7505283113,
4
+ "total_size": 15010566226
5
+ },
6
+ "weight_map": {
7
+ "gating.layers.0.bias": "model-00001-of-00004.safetensors",
8
+ "gating.layers.0.weight": "model-00001-of-00004.safetensors",
9
+ "gating.layers.1.bias": "model-00001-of-00004.safetensors",
10
+ "gating.layers.1.weight": "model-00001-of-00004.safetensors",
11
+ "gating.logit_scale": "model-00001-of-00004.safetensors",
12
+ "model.embed_tokens.weight": "model-00001-of-00004.safetensors",
13
+ "model.layers.0.input_layernorm.weight": "model-00001-of-00004.safetensors",
14
+ "model.layers.0.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
15
+ "model.layers.0.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
16
+ "model.layers.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
17
+ "model.layers.0.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
18
+ "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
19
+ "model.layers.0.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
20
+ "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
21
+ "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
22
+ "model.layers.1.input_layernorm.weight": "model-00001-of-00004.safetensors",
23
+ "model.layers.1.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
24
+ "model.layers.1.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
25
+ "model.layers.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
26
+ "model.layers.1.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
27
+ "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
28
+ "model.layers.1.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
29
+ "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
30
+ "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
31
+ "model.layers.10.input_layernorm.weight": "model-00002-of-00004.safetensors",
32
+ "model.layers.10.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
33
+ "model.layers.10.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
34
+ "model.layers.10.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
35
+ "model.layers.10.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
36
+ "model.layers.10.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
37
+ "model.layers.10.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
38
+ "model.layers.10.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
39
+ "model.layers.10.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
40
+ "model.layers.11.input_layernorm.weight": "model-00002-of-00004.safetensors",
41
+ "model.layers.11.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
42
+ "model.layers.11.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
43
+ "model.layers.11.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
44
+ "model.layers.11.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
45
+ "model.layers.11.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
46
+ "model.layers.11.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
47
+ "model.layers.11.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
48
+ "model.layers.11.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
49
+ "model.layers.12.input_layernorm.weight": "model-00002-of-00004.safetensors",
50
+ "model.layers.12.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
51
+ "model.layers.12.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
52
+ "model.layers.12.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
53
+ "model.layers.12.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
54
+ "model.layers.12.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
55
+ "model.layers.12.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
56
+ "model.layers.12.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
57
+ "model.layers.12.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
58
+ "model.layers.13.input_layernorm.weight": "model-00002-of-00004.safetensors",
59
+ "model.layers.13.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
60
+ "model.layers.13.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
61
+ "model.layers.13.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
62
+ "model.layers.13.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
63
+ "model.layers.13.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
64
+ "model.layers.13.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
65
+ "model.layers.13.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
66
+ "model.layers.13.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
67
+ "model.layers.14.input_layernorm.weight": "model-00002-of-00004.safetensors",
68
+ "model.layers.14.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
69
+ "model.layers.14.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
70
+ "model.layers.14.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
71
+ "model.layers.14.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
72
+ "model.layers.14.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
73
+ "model.layers.14.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
74
+ "model.layers.14.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
75
+ "model.layers.14.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
76
+ "model.layers.15.input_layernorm.weight": "model-00002-of-00004.safetensors",
77
+ "model.layers.15.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
78
+ "model.layers.15.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
79
+ "model.layers.15.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
80
+ "model.layers.15.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
81
+ "model.layers.15.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
82
+ "model.layers.15.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
83
+ "model.layers.15.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
84
+ "model.layers.15.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
85
+ "model.layers.16.input_layernorm.weight": "model-00003-of-00004.safetensors",
86
+ "model.layers.16.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
87
+ "model.layers.16.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
88
+ "model.layers.16.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
89
+ "model.layers.16.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
90
+ "model.layers.16.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
91
+ "model.layers.16.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
92
+ "model.layers.16.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
93
+ "model.layers.16.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
94
+ "model.layers.17.input_layernorm.weight": "model-00003-of-00004.safetensors",
95
+ "model.layers.17.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
96
+ "model.layers.17.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
97
+ "model.layers.17.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
98
+ "model.layers.17.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
99
+ "model.layers.17.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
100
+ "model.layers.17.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
101
+ "model.layers.17.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
102
+ "model.layers.17.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
103
+ "model.layers.18.input_layernorm.weight": "model-00003-of-00004.safetensors",
104
+ "model.layers.18.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
105
+ "model.layers.18.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
106
+ "model.layers.18.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
107
+ "model.layers.18.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
108
+ "model.layers.18.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
109
+ "model.layers.18.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
110
+ "model.layers.18.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
111
+ "model.layers.18.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
112
+ "model.layers.19.input_layernorm.weight": "model-00003-of-00004.safetensors",
113
+ "model.layers.19.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
114
+ "model.layers.19.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
115
+ "model.layers.19.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
116
+ "model.layers.19.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
117
+ "model.layers.19.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
118
+ "model.layers.19.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
119
+ "model.layers.19.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
120
+ "model.layers.19.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
121
+ "model.layers.2.input_layernorm.weight": "model-00001-of-00004.safetensors",
122
+ "model.layers.2.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
123
+ "model.layers.2.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
124
+ "model.layers.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
125
+ "model.layers.2.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
126
+ "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
127
+ "model.layers.2.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
128
+ "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
129
+ "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
130
+ "model.layers.20.input_layernorm.weight": "model-00003-of-00004.safetensors",
131
+ "model.layers.20.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
132
+ "model.layers.20.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
133
+ "model.layers.20.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
134
+ "model.layers.20.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
135
+ "model.layers.20.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
136
+ "model.layers.20.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
137
+ "model.layers.20.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
138
+ "model.layers.20.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
139
+ "model.layers.21.input_layernorm.weight": "model-00003-of-00004.safetensors",
140
+ "model.layers.21.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
141
+ "model.layers.21.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
142
+ "model.layers.21.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
143
+ "model.layers.21.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
144
+ "model.layers.21.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
145
+ "model.layers.21.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
146
+ "model.layers.21.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
147
+ "model.layers.21.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
148
+ "model.layers.22.input_layernorm.weight": "model-00003-of-00004.safetensors",
149
+ "model.layers.22.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
150
+ "model.layers.22.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
151
+ "model.layers.22.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
152
+ "model.layers.22.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
153
+ "model.layers.22.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
154
+ "model.layers.22.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
155
+ "model.layers.22.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
156
+ "model.layers.22.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
157
+ "model.layers.23.input_layernorm.weight": "model-00003-of-00004.safetensors",
158
+ "model.layers.23.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
159
+ "model.layers.23.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
160
+ "model.layers.23.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
161
+ "model.layers.23.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
162
+ "model.layers.23.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
163
+ "model.layers.23.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
164
+ "model.layers.23.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
165
+ "model.layers.23.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
166
+ "model.layers.24.input_layernorm.weight": "model-00003-of-00004.safetensors",
167
+ "model.layers.24.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
168
+ "model.layers.24.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
169
+ "model.layers.24.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
170
+ "model.layers.24.post_attention_layernorm.weight": "model-00004-of-00004.safetensors",
171
+ "model.layers.24.self_attn.k_proj.weight": "model-00004-of-00004.safetensors",
172
+ "model.layers.24.self_attn.o_proj.weight": "model-00004-of-00004.safetensors",
173
+ "model.layers.24.self_attn.q_proj.weight": "model-00004-of-00004.safetensors",
174
+ "model.layers.24.self_attn.v_proj.weight": "model-00004-of-00004.safetensors",
175
+ "model.layers.25.input_layernorm.weight": "model-00004-of-00004.safetensors",
176
+ "model.layers.25.mlp.down_proj.weight": "model-00004-of-00004.safetensors",
177
+ "model.layers.25.mlp.gate_proj.weight": "model-00004-of-00004.safetensors",
178
+ "model.layers.25.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
179
+ "model.layers.25.post_attention_layernorm.weight": "model-00004-of-00004.safetensors",
180
+ "model.layers.25.self_attn.k_proj.weight": "model-00004-of-00004.safetensors",
181
+ "model.layers.25.self_attn.o_proj.weight": "model-00004-of-00004.safetensors",
182
+ "model.layers.25.self_attn.q_proj.weight": "model-00004-of-00004.safetensors",
183
+ "model.layers.25.self_attn.v_proj.weight": "model-00004-of-00004.safetensors",
184
+ "model.layers.26.input_layernorm.weight": "model-00004-of-00004.safetensors",
185
+ "model.layers.26.mlp.down_proj.weight": "model-00004-of-00004.safetensors",
186
+ "model.layers.26.mlp.gate_proj.weight": "model-00004-of-00004.safetensors",
187
+ "model.layers.26.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
188
+ "model.layers.26.post_attention_layernorm.weight": "model-00004-of-00004.safetensors",
189
+ "model.layers.26.self_attn.k_proj.weight": "model-00004-of-00004.safetensors",
190
+ "model.layers.26.self_attn.o_proj.weight": "model-00004-of-00004.safetensors",
191
+ "model.layers.26.self_attn.q_proj.weight": "model-00004-of-00004.safetensors",
192
+ "model.layers.26.self_attn.v_proj.weight": "model-00004-of-00004.safetensors",
193
+ "model.layers.27.input_layernorm.weight": "model-00004-of-00004.safetensors",
194
+ "model.layers.27.mlp.down_proj.weight": "model-00004-of-00004.safetensors",
195
+ "model.layers.27.mlp.gate_proj.weight": "model-00004-of-00004.safetensors",
196
+ "model.layers.27.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
197
+ "model.layers.27.post_attention_layernorm.weight": "model-00004-of-00004.safetensors",
198
+ "model.layers.27.self_attn.k_proj.weight": "model-00004-of-00004.safetensors",
199
+ "model.layers.27.self_attn.o_proj.weight": "model-00004-of-00004.safetensors",
200
+ "model.layers.27.self_attn.q_proj.weight": "model-00004-of-00004.safetensors",
201
+ "model.layers.27.self_attn.v_proj.weight": "model-00004-of-00004.safetensors",
202
+ "model.layers.28.input_layernorm.weight": "model-00004-of-00004.safetensors",
203
+ "model.layers.28.mlp.down_proj.weight": "model-00004-of-00004.safetensors",
204
+ "model.layers.28.mlp.gate_proj.weight": "model-00004-of-00004.safetensors",
205
+ "model.layers.28.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
206
+ "model.layers.28.post_attention_layernorm.weight": "model-00004-of-00004.safetensors",
207
+ "model.layers.28.self_attn.k_proj.weight": "model-00004-of-00004.safetensors",
208
+ "model.layers.28.self_attn.o_proj.weight": "model-00004-of-00004.safetensors",
209
+ "model.layers.28.self_attn.q_proj.weight": "model-00004-of-00004.safetensors",
210
+ "model.layers.28.self_attn.v_proj.weight": "model-00004-of-00004.safetensors",
211
+ "model.layers.29.input_layernorm.weight": "model-00004-of-00004.safetensors",
212
+ "model.layers.29.mlp.down_proj.weight": "model-00004-of-00004.safetensors",
213
+ "model.layers.29.mlp.gate_proj.weight": "model-00004-of-00004.safetensors",
214
+ "model.layers.29.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
215
+ "model.layers.29.post_attention_layernorm.weight": "model-00004-of-00004.safetensors",
216
+ "model.layers.29.self_attn.k_proj.weight": "model-00004-of-00004.safetensors",
217
+ "model.layers.29.self_attn.o_proj.weight": "model-00004-of-00004.safetensors",
218
+ "model.layers.29.self_attn.q_proj.weight": "model-00004-of-00004.safetensors",
219
+ "model.layers.29.self_attn.v_proj.weight": "model-00004-of-00004.safetensors",
220
+ "model.layers.3.input_layernorm.weight": "model-00001-of-00004.safetensors",
221
+ "model.layers.3.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
222
+ "model.layers.3.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
223
+ "model.layers.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
224
+ "model.layers.3.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
225
+ "model.layers.3.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
226
+ "model.layers.3.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
227
+ "model.layers.3.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
228
+ "model.layers.3.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
229
+ "model.layers.30.input_layernorm.weight": "model-00004-of-00004.safetensors",
230
+ "model.layers.30.mlp.down_proj.weight": "model-00004-of-00004.safetensors",
231
+ "model.layers.30.mlp.gate_proj.weight": "model-00004-of-00004.safetensors",
232
+ "model.layers.30.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
233
+ "model.layers.30.post_attention_layernorm.weight": "model-00004-of-00004.safetensors",
234
+ "model.layers.30.self_attn.k_proj.weight": "model-00004-of-00004.safetensors",
235
+ "model.layers.30.self_attn.o_proj.weight": "model-00004-of-00004.safetensors",
236
+ "model.layers.30.self_attn.q_proj.weight": "model-00004-of-00004.safetensors",
237
+ "model.layers.30.self_attn.v_proj.weight": "model-00004-of-00004.safetensors",
238
+ "model.layers.31.input_layernorm.weight": "model-00004-of-00004.safetensors",
239
+ "model.layers.31.mlp.down_proj.weight": "model-00004-of-00004.safetensors",
240
+ "model.layers.31.mlp.gate_proj.weight": "model-00004-of-00004.safetensors",
241
+ "model.layers.31.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
242
+ "model.layers.31.post_attention_layernorm.weight": "model-00004-of-00004.safetensors",
243
+ "model.layers.31.self_attn.k_proj.weight": "model-00004-of-00004.safetensors",
244
+ "model.layers.31.self_attn.o_proj.weight": "model-00004-of-00004.safetensors",
245
+ "model.layers.31.self_attn.q_proj.weight": "model-00004-of-00004.safetensors",
246
+ "model.layers.31.self_attn.v_proj.weight": "model-00004-of-00004.safetensors",
247
+ "model.layers.4.input_layernorm.weight": "model-00001-of-00004.safetensors",
248
+ "model.layers.4.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
249
+ "model.layers.4.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
250
+ "model.layers.4.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
251
+ "model.layers.4.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
252
+ "model.layers.4.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
253
+ "model.layers.4.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
254
+ "model.layers.4.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
255
+ "model.layers.4.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
256
+ "model.layers.5.input_layernorm.weight": "model-00001-of-00004.safetensors",
257
+ "model.layers.5.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
258
+ "model.layers.5.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
259
+ "model.layers.5.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
260
+ "model.layers.5.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
261
+ "model.layers.5.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
262
+ "model.layers.5.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
263
+ "model.layers.5.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
264
+ "model.layers.5.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
265
+ "model.layers.6.input_layernorm.weight": "model-00001-of-00004.safetensors",
266
+ "model.layers.6.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
267
+ "model.layers.6.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
268
+ "model.layers.6.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
269
+ "model.layers.6.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
270
+ "model.layers.6.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
271
+ "model.layers.6.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
272
+ "model.layers.6.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
273
+ "model.layers.6.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
274
+ "model.layers.7.input_layernorm.weight": "model-00002-of-00004.safetensors",
275
+ "model.layers.7.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
276
+ "model.layers.7.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
277
+ "model.layers.7.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
278
+ "model.layers.7.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
279
+ "model.layers.7.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
280
+ "model.layers.7.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
281
+ "model.layers.7.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
282
+ "model.layers.7.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
283
+ "model.layers.8.input_layernorm.weight": "model-00002-of-00004.safetensors",
284
+ "model.layers.8.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
285
+ "model.layers.8.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
286
+ "model.layers.8.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
287
+ "model.layers.8.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
288
+ "model.layers.8.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
289
+ "model.layers.8.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
290
+ "model.layers.8.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
291
+ "model.layers.8.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
292
+ "model.layers.9.input_layernorm.weight": "model-00002-of-00004.safetensors",
293
+ "model.layers.9.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
294
+ "model.layers.9.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
295
+ "model.layers.9.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
296
+ "model.layers.9.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
297
+ "model.layers.9.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
298
+ "model.layers.9.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
299
+ "model.layers.9.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
300
+ "model.layers.9.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
301
+ "model.norm.weight": "model-00004-of-00004.safetensors",
302
+ "regression_layer.weight": "model-00004-of-00004.safetensors",
303
+ "reward_transform_matrix": "model-00004-of-00004.safetensors"
304
+ }
305
+ }
modeling_custom.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # modeling_custom.py
2
+
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+ from dataclasses import dataclass
7
+ from typing import List, Optional
8
+ from transformers import AutoConfig, AutoModel, PreTrainedModel
9
+ from transformers.utils import ModelOutput
10
+ try:
11
+ from .utils import TOKEN_PATTERNS_BY_MODEL_TYPE, find_token_for_gating
12
+ except ImportError: # Local source-tree execution.
13
+ from utils import TOKEN_PATTERNS_BY_MODEL_TYPE, find_token_for_gating
14
+
15
+ class GatingNetwork(nn.Module):
16
+ def __init__(self, in_features: int, out_features: int, bias: bool = True, temperature: float = 10,
17
+ logit_scale: float = 1., hidden_dim: int = 1024, n_hidden: int = 3, dropout: float = 0.0,
18
+ learnable_logit_scale: bool = False,
19
+ active_attribute_indices: Optional[List[int]] = None):
20
+ super().__init__()
21
+ self.temperature = temperature
22
+ self.logit_scale = nn.Parameter(
23
+ torch.ones(1) * logit_scale, requires_grad=learnable_logit_scale
24
+ )
25
+ self.dropout_prob = dropout
26
+ active_mask = torch.ones(out_features, dtype=torch.bool)
27
+ if active_attribute_indices is not None:
28
+ if not active_attribute_indices:
29
+ raise ValueError("At least one attribute must be active.")
30
+ if min(active_attribute_indices) < 0 or max(active_attribute_indices) >= out_features:
31
+ raise ValueError("active_attribute_indices contains an out-of-range index.")
32
+ active_mask.zero_()
33
+ active_mask[list(active_attribute_indices)] = True
34
+ # Derived from packaged config; omit from state_dict for legacy compatibility.
35
+ self.register_buffer("active_attribute_mask", active_mask, persistent=False)
36
+ layers = []
37
+ for _ in range(n_hidden):
38
+ layers.append(nn.Linear(in_features, hidden_dim))
39
+ in_features = hidden_dim
40
+ layers.append(nn.Linear(in_features, out_features, bias=bias))
41
+ self.layers = nn.ModuleList(layers)
42
+
43
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
44
+ for i, layer in enumerate(self.layers):
45
+ x = layer(x)
46
+ if i < len(self.layers) - 1:
47
+ x = F.relu(x)
48
+ if self.dropout_prob > 0 and self.training:
49
+ x = F.dropout(x, p=self.dropout_prob)
50
+ logits = x / self.temperature
51
+ mask = self.active_attribute_mask.to(device=logits.device)
52
+ logits = logits.masked_fill(~mask, torch.finfo(logits.dtype).min)
53
+ x = F.softmax(logits, dim=-1)
54
+ return x * self.logit_scale
55
+
56
+ @dataclass
57
+ class CustomOutput(ModelOutput):
58
+ rewards: Optional[torch.Tensor] = None
59
+ hidden_state: Optional[torch.Tensor] = None
60
+ prompt_embedding: Optional[torch.Tensor] = None
61
+ gating_output: Optional[torch.Tensor] = None
62
+ score: Optional[torch.Tensor] = None
63
+ logits: Optional[torch.Tensor] = None
64
+
65
+ class RewardModelWithGating(PreTrainedModel):
66
+ """Backbone-agnostic reward model with a prompt-conditioned gating network."""
67
+
68
+ config_class = AutoConfig
69
+ base_model_prefix = "model"
70
+
71
+ def __init__(self, config):
72
+ super().__init__(config)
73
+ self.num_labels = config.num_labels
74
+ self.model = AutoModel.from_config(config)
75
+ config_dict = config.to_dict()
76
+
77
+ # Default objective count for this project.
78
+ self.num_objectives = config_dict.get("num_objectives", 23)
79
+
80
+ self.regression_layer = nn.Linear(config.hidden_size, self.num_objectives, bias=False)
81
+ self.post_init()
82
+
83
+ # Avoid torch.eye to keep compatibility with BF16 training setups.
84
+ I = torch.zeros(self.num_objectives, self.num_objectives)
85
+ I[range(self.num_objectives), range(self.num_objectives)] = 1.
86
+ self.reward_transform_matrix = nn.Parameter(I)
87
+ self.reward_transform_matrix.requires_grad = False
88
+
89
+ self.gating = GatingNetwork(config.hidden_size, self.num_objectives,
90
+ temperature=config_dict.get("gating_temperature", 10),
91
+ logit_scale=config_dict.get("gating_logit_scale", 1.0),
92
+ hidden_dim=config_dict.get("gating_hidden_dim", 1024),
93
+ n_hidden=config_dict.get("gating_n_hidden", 3),
94
+ dropout=config_dict.get("gating_dropout", 0.0),
95
+ learnable_logit_scale=config_dict.get("gating_learnable_logit_scale", False),
96
+ active_attribute_indices=config_dict.get(
97
+ "gating_active_attribute_indices"))
98
+
99
+ def compute_gating(
100
+ self,
101
+ input_ids: torch.LongTensor,
102
+ attention_mask: Optional[torch.Tensor] = None,
103
+ ) -> torch.Tensor:
104
+ """Compute routing weights from a prompt-only token sequence.
105
+
106
+ Callers should render the prompt with ``add_generation_prompt=True``.
107
+ The final non-padding prompt token is causal and therefore cannot see
108
+ either candidate response.
109
+ """
110
+ outputs = self.model(
111
+ input_ids=input_ids,
112
+ attention_mask=attention_mask,
113
+ return_dict=True,
114
+ )
115
+ hidden = outputs[0]
116
+ if attention_mask is None:
117
+ positions = torch.full(
118
+ (hidden.shape[0],), hidden.shape[1] - 1,
119
+ dtype=torch.long, device=hidden.device,
120
+ )
121
+ else:
122
+ token_positions = torch.arange(
123
+ hidden.shape[1], device=hidden.device
124
+ ).unsqueeze(0)
125
+ positions = (attention_mask.long() * token_positions).argmax(dim=-1)
126
+ rows = torch.arange(hidden.shape[0], device=hidden.device)
127
+ return self.gating(hidden[rows, positions])
128
+
129
+ def forward(
130
+ self,
131
+ input_ids: Optional[torch.LongTensor] = None,
132
+ attention_mask: Optional[torch.Tensor] = None,
133
+ position_ids: Optional[torch.LongTensor] = None,
134
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
135
+ inputs_embeds: Optional[torch.FloatTensor] = None,
136
+ labels: Optional[torch.FloatTensor] = None,
137
+ use_cache: Optional[bool] = None,
138
+ output_attentions: Optional[bool] = None,
139
+ output_hidden_states: Optional[bool] = None,
140
+ return_dict: Optional[bool] = None,
141
+ gating_output_override: Optional[torch.Tensor] = None,
142
+ ) -> CustomOutput:
143
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
144
+ transformer_outputs = self.model(
145
+ input_ids,
146
+ attention_mask=attention_mask,
147
+ position_ids=position_ids,
148
+ past_key_values=past_key_values,
149
+ inputs_embeds=inputs_embeds,
150
+ use_cache=use_cache,
151
+ output_attentions=output_attentions,
152
+ output_hidden_states=output_hidden_states,
153
+ return_dict=return_dict,
154
+ )
155
+ tokens_hidden_states = transformer_outputs[0]
156
+ if input_ids is not None:
157
+ batch_size = input_ids.shape[0]
158
+ elif inputs_embeds is not None:
159
+ batch_size = inputs_embeds.shape[0]
160
+ else:
161
+ raise ValueError("Either input_ids or inputs_embeds must be provided.")
162
+
163
+ if self.config.pad_token_id is None and batch_size != 1:
164
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
165
+
166
+ if self.config.pad_token_id is None:
167
+ sequence_lengths = -1
168
+ else:
169
+ if input_ids is not None:
170
+ # If no pad token is found, modulo keeps ONNX-compatible indexing.
171
+ sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
172
+ sequence_lengths = sequence_lengths % input_ids.shape[-1]
173
+ sequence_lengths = sequence_lengths.to(tokens_hidden_states.device)
174
+ else:
175
+ sequence_lengths = -1
176
+
177
+ dummy_iterator = torch.arange(batch_size, device=tokens_hidden_states.device)
178
+ hidden_states = tokens_hidden_states[dummy_iterator, sequence_lengths]
179
+ assert hidden_states.shape == (batch_size, self.config.hidden_size)
180
+
181
+ rewards = self.regression_layer(hidden_states)
182
+ prompt_embedding = None
183
+ if gating_output_override is not None:
184
+ gating_output = gating_output_override.to(device=rewards.device, dtype=rewards.dtype)
185
+ if gating_output.ndim == 1:
186
+ gating_output = gating_output.unsqueeze(0)
187
+ if gating_output.shape != rewards.shape:
188
+ raise ValueError(
189
+ f"gating_output_override shape {tuple(gating_output.shape)} does not "
190
+ f"match rewards shape {tuple(rewards.shape)}"
191
+ )
192
+ else:
193
+ if input_ids is None:
194
+ raise ValueError("input_ids is required to compute gating token positions.")
195
+ model_type = getattr(self.config, "model_type", None)
196
+ gating_token_positions = [
197
+ find_token_for_gating(ids.detach().cpu().tolist(), model_type) for ids in input_ids
198
+ ]
199
+ prompt_embedding = tokens_hidden_states[dummy_iterator, gating_token_positions, :]
200
+ gating_output = self.gating(prompt_embedding)
201
+ rewards_adjusted = rewards @ self.reward_transform_matrix
202
+ score = torch.sum(gating_output * rewards_adjusted, dim=1)
203
+
204
+ return CustomOutput(
205
+ rewards=rewards,
206
+ hidden_state=hidden_states,
207
+ prompt_embedding=prompt_embedding,
208
+ gating_output=gating_output,
209
+ score=score,
210
+ logits=score,
211
+ )
212
+
213
+
214
+ # Backward compatibility alias for existing imports/checkpoints.
215
+ LlamaForRewardModelWithGating = RewardModelWithGating
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch>=2.6
2
+ transformers>=4.51
3
+ safetensors>=0.5
4
+ accelerate>=1.0
results/eval.json DELETED
@@ -1,604 +0,0 @@
1
- {
2
- "model": "model/multi-domain-rm-skywork-llama-3.1-8b-it-test-196",
3
- "training_metadata": {
4
- "base_model_path": "Skywork/Skywork-Reward-V2-Llama-3.1-8B",
5
- "stage_1_weights_path": "model/regression_weights/Skywork-Reward-V2-Llama-3.1-8B_Multi-Domain-Data-Scoring_100pct.pt",
6
- "stage_2_weights_path": "model/gating_network/gating_network_Skywork-Reward-V2-Llama-3.1-8B_mo_Multi-Domain-Data-Scoring_pref_Multi-Domain-Data-Preference-Pairs_ref_null_t2.0_n30000_seed0_le0.0005_we0.0_n_1_hi64_dr0.05_ba2048_co0.03_lo2.0.pt"
7
- },
8
- "scoring_80pct": {
9
- "evaluated": 5122,
10
- "skipped": 0,
11
- "attributes": {
12
- "co_discourse_structure": {
13
- "n": 1280,
14
- "mse": 0.033118,
15
- "pearson": 0.766439,
16
- "spearman": 0.777125
17
- },
18
- "co_logical_consistency": {
19
- "n": 1280,
20
- "mse": 0.040838,
21
- "pearson": 0.709531,
22
- "spearman": 0.723998
23
- },
24
- "co_mutual_grounding": {
25
- "n": 1280,
26
- "mse": 0.036121,
27
- "pearson": 0.764542,
28
- "spearman": 0.775175
29
- },
30
- "co_overall_coherence_score": {
31
- "n": 1280,
32
- "mse": 0.033191,
33
- "pearson": 0.759132,
34
- "spearman": 0.769916
35
- },
36
- "co_temporal_causal_coherence": {
37
- "n": 1280,
38
- "mse": 0.032116,
39
- "pearson": 0.739292,
40
- "spearman": 0.74636
41
- },
42
- "co_topic_coherence": {
43
- "n": 1280,
44
- "mse": 0.032,
45
- "pearson": 0.767388,
46
- "spearman": 0.769546
47
- },
48
- "cs_causality": {
49
- "n": 1286,
50
- "mse": 0.026153,
51
- "pearson": 0.629517,
52
- "spearman": 0.598503
53
- },
54
- "cs_coherence": {
55
- "n": 1286,
56
- "mse": 0.009247,
57
- "pearson": 0.74146,
58
- "spearman": 0.753485
59
- },
60
- "cs_consistency": {
61
- "n": 1286,
62
- "mse": 0.031536,
63
- "pearson": 0.572519,
64
- "spearman": 0.571924
65
- },
66
- "cs_desire": {
67
- "n": 1286,
68
- "mse": 0.032596,
69
- "pearson": 0.660779,
70
- "spearman": 0.646299
71
- },
72
- "cs_empathy": {
73
- "n": 1286,
74
- "mse": 0.017902,
75
- "pearson": 0.770497,
76
- "spearman": 0.762808
77
- },
78
- "cs_reaction": {
79
- "n": 1286,
80
- "mse": 0.028105,
81
- "pearson": 0.765706,
82
- "spearman": 0.770195
83
- },
84
- "em_emotional_awareness": {
85
- "n": 1274,
86
- "mse": 0.022979,
87
- "pearson": 0.799919,
88
- "spearman": 0.810258
89
- },
90
- "em_emotional_validation": {
91
- "n": 1274,
92
- "mse": 0.020944,
93
- "pearson": 0.819489,
94
- "spearman": 0.836191
95
- },
96
- "em_helpful_response": {
97
- "n": 1274,
98
- "mse": 0.022296,
99
- "pearson": 0.765768,
100
- "spearman": 0.751327
101
- },
102
- "em_overall_empathy_score": {
103
- "n": 1274,
104
- "mse": 0.017749,
105
- "pearson": 0.834078,
106
- "spearman": 0.847137
107
- },
108
- "em_perspective_taking": {
109
- "n": 1274,
110
- "mse": 0.018077,
111
- "pearson": 0.814856,
112
- "spearman": 0.830864
113
- },
114
- "em_supportive_engagement": {
115
- "n": 1274,
116
- "mse": 0.022242,
117
- "pearson": 0.811304,
118
- "spearman": 0.798736
119
- },
120
- "mu_coherence": {
121
- "n": 1282,
122
- "mse": 0.003183,
123
- "pearson": 0.53157,
124
- "spearman": 0.595166
125
- },
126
- "mu_cultural_specificity": {
127
- "n": 1282,
128
- "mse": 0.017801,
129
- "pearson": 0.805912,
130
- "spearman": 0.837055
131
- },
132
- "mu_cultural_value": {
133
- "n": 1282,
134
- "mse": 0.065155,
135
- "pearson": 0.226475,
136
- "spearman": 0.2136
137
- },
138
- "mu_empathy": {
139
- "n": 1282,
140
- "mse": 0.01003,
141
- "pearson": 0.9126,
142
- "spearman": 0.83685
143
- },
144
- "mu_naturalness": {
145
- "n": 1282,
146
- "mse": 0.003088,
147
- "pearson": 0.583882,
148
- "spearman": 0.603499
149
- }
150
- },
151
- "domains": {
152
- "coherence": {
153
- "mse": 0.034564,
154
- "pearson": 0.751054,
155
- "spearman": 0.760353
156
- },
157
- "commonsense": {
158
- "mse": 0.024257,
159
- "pearson": 0.69008,
160
- "spearman": 0.683869
161
- },
162
- "empathy": {
163
- "mse": 0.020715,
164
- "pearson": 0.807569,
165
- "spearman": 0.812419
166
- },
167
- "multicultural": {
168
- "mse": 0.019851,
169
- "pearson": 0.612088,
170
- "spearman": 0.617234
171
- }
172
- },
173
- "average": {
174
- "mse": 0.025064,
175
- "pearson": 0.719681,
176
- "spearman": 0.72287
177
- },
178
- "global_score": {
179
- "mean": -0.084258,
180
- "std": 1.821894,
181
- "min": -6.84375,
182
- "max": 6.9375
183
- }
184
- },
185
- "scoring_100pct": {
186
- "evaluated": 5122,
187
- "skipped": 0,
188
- "attributes": {
189
- "co_discourse_structure": {
190
- "n": 1280,
191
- "mse": 0.032518,
192
- "pearson": 0.770777,
193
- "spearman": 0.782585
194
- },
195
- "co_logical_consistency": {
196
- "n": 1280,
197
- "mse": 0.040172,
198
- "pearson": 0.715056,
199
- "spearman": 0.730289
200
- },
201
- "co_mutual_grounding": {
202
- "n": 1280,
203
- "mse": 0.035946,
204
- "pearson": 0.765847,
205
- "spearman": 0.776893
206
- },
207
- "co_overall_coherence_score": {
208
- "n": 1280,
209
- "mse": 0.032674,
210
- "pearson": 0.763504,
211
- "spearman": 0.774096
212
- },
213
- "co_temporal_causal_coherence": {
214
- "n": 1280,
215
- "mse": 0.031754,
216
- "pearson": 0.743045,
217
- "spearman": 0.748906
218
- },
219
- "co_topic_coherence": {
220
- "n": 1280,
221
- "mse": 0.031385,
222
- "pearson": 0.772437,
223
- "spearman": 0.773025
224
- },
225
- "cs_causality": {
226
- "n": 1286,
227
- "mse": 0.025816,
228
- "pearson": 0.635689,
229
- "spearman": 0.599198
230
- },
231
- "cs_coherence": {
232
- "n": 1286,
233
- "mse": 0.00913,
234
- "pearson": 0.745643,
235
- "spearman": 0.753881
236
- },
237
- "cs_consistency": {
238
- "n": 1286,
239
- "mse": 0.031209,
240
- "pearson": 0.578453,
241
- "spearman": 0.577192
242
- },
243
- "cs_desire": {
244
- "n": 1286,
245
- "mse": 0.032062,
246
- "pearson": 0.666989,
247
- "spearman": 0.652277
248
- },
249
- "cs_empathy": {
250
- "n": 1286,
251
- "mse": 0.017647,
252
- "pearson": 0.774784,
253
- "spearman": 0.767929
254
- },
255
- "cs_reaction": {
256
- "n": 1286,
257
- "mse": 0.027656,
258
- "pearson": 0.770099,
259
- "spearman": 0.773719
260
- },
261
- "em_emotional_awareness": {
262
- "n": 1274,
263
- "mse": 0.022634,
264
- "pearson": 0.803611,
265
- "spearman": 0.812949
266
- },
267
- "em_emotional_validation": {
268
- "n": 1274,
269
- "mse": 0.020593,
270
- "pearson": 0.822651,
271
- "spearman": 0.838193
272
- },
273
- "em_helpful_response": {
274
- "n": 1274,
275
- "mse": 0.022002,
276
- "pearson": 0.76928,
277
- "spearman": 0.752839
278
- },
279
- "em_overall_empathy_score": {
280
- "n": 1274,
281
- "mse": 0.017547,
282
- "pearson": 0.835748,
283
- "spearman": 0.847668
284
- },
285
- "em_perspective_taking": {
286
- "n": 1274,
287
- "mse": 0.017851,
288
- "pearson": 0.817472,
289
- "spearman": 0.832984
290
- },
291
- "em_supportive_engagement": {
292
- "n": 1274,
293
- "mse": 0.021954,
294
- "pearson": 0.814144,
295
- "spearman": 0.800278
296
- },
297
- "mu_coherence": {
298
- "n": 1282,
299
- "mse": 0.003149,
300
- "pearson": 0.53863,
301
- "spearman": 0.598112
302
- },
303
- "mu_cultural_specificity": {
304
- "n": 1282,
305
- "mse": 0.017417,
306
- "pearson": 0.811084,
307
- "spearman": 0.841164
308
- },
309
- "mu_cultural_value": {
310
- "n": 1282,
311
- "mse": 0.064614,
312
- "pearson": 0.240758,
313
- "spearman": 0.225872
314
- },
315
- "mu_empathy": {
316
- "n": 1282,
317
- "mse": 0.009984,
318
- "pearson": 0.912989,
319
- "spearman": 0.837596
320
- },
321
- "mu_naturalness": {
322
- "n": 1282,
323
- "mse": 0.003063,
324
- "pearson": 0.587859,
325
- "spearman": 0.60883
326
- }
327
- },
328
- "domains": {
329
- "coherence": {
330
- "mse": 0.034075,
331
- "pearson": 0.755111,
332
- "spearman": 0.764299
333
- },
334
- "commonsense": {
335
- "mse": 0.02392,
336
- "pearson": 0.695276,
337
- "spearman": 0.687366
338
- },
339
- "empathy": {
340
- "mse": 0.02043,
341
- "pearson": 0.810484,
342
- "spearman": 0.814152
343
- },
344
- "multicultural": {
345
- "mse": 0.019646,
346
- "pearson": 0.618264,
347
- "spearman": 0.622315
348
- }
349
- },
350
- "average": {
351
- "mse": 0.024729,
352
- "pearson": 0.724198,
353
- "spearman": 0.726368
354
- },
355
- "global_score": {
356
- "mean": -0.115085,
357
- "std": 1.837174,
358
- "min": -7.0,
359
- "max": 7.0
360
- }
361
- },
362
- "preference": {
363
- "total": 15489,
364
- "correct": 14356,
365
- "ties": 6,
366
- "skipped": 0,
367
- "accuracy": 92.6851,
368
- "margin_mean": 3.905499,
369
- "margin_std": 3.03703,
370
- "domains": {
371
- "coherence": {
372
- "accuracy": 87.527,
373
- "correct": 3242,
374
- "total": 3704,
375
- "ties": 2
376
- },
377
- "commonsense": {
378
- "accuracy": 98.0365,
379
- "correct": 4993,
380
- "total": 5093,
381
- "ties": 1
382
- },
383
- "empathy": {
384
- "accuracy": 96.2669,
385
- "correct": 3636,
386
- "total": 3777,
387
- "ties": 1
388
- },
389
- "multicultural": {
390
- "accuracy": 85.2487,
391
- "correct": 2485,
392
- "total": 2915,
393
- "ties": 2
394
- }
395
- },
396
- "difficulty": {
397
- "easy": {
398
- "accuracy": 95.8952,
399
- "correct": 11891,
400
- "total": 12400,
401
- "ties": 4
402
- },
403
- "hard": {
404
- "accuracy": 73.5965,
405
- "correct": 839,
406
- "total": 1140,
407
- "ties": 0
408
- },
409
- "medium": {
410
- "accuracy": 83.4274,
411
- "correct": 1626,
412
- "total": 1949,
413
- "ties": 2
414
- }
415
- }
416
- },
417
- "cultural": {
418
- "evaluated": 180,
419
- "skipped": 0,
420
- "global_score": {
421
- "mean": -0.9639,
422
- "std": 1.4414,
423
- "min": -6.3438,
424
- "max": 3.4531
425
- },
426
- "countries": {
427
- "Australia": {
428
- "n": 15,
429
- "score_mean": -0.949,
430
- "score_std": 0.7752,
431
- "mu_attributes": {
432
- "mu_coherence": 0.875,
433
- "mu_cultural_specificity": 0.5217,
434
- "mu_cultural_value": 0.638,
435
- "mu_empathy": 0.1304,
436
- "mu_naturalness": 0.8076
437
- }
438
- },
439
- "Chile": {
440
- "n": 15,
441
- "score_mean": -1.6327,
442
- "score_std": 1.3299,
443
- "mu_attributes": {
444
- "mu_coherence": 0.876,
445
- "mu_cultural_specificity": 0.5439,
446
- "mu_cultural_value": 0.6354,
447
- "mu_empathy": 0.1497,
448
- "mu_naturalness": 0.8112
449
- }
450
- },
451
- "China": {
452
- "n": 15,
453
- "score_mean": -0.7811,
454
- "score_std": 1.2349,
455
- "mu_attributes": {
456
- "mu_coherence": 0.8802,
457
- "mu_cultural_specificity": 0.5197,
458
- "mu_cultural_value": 0.6831,
459
- "mu_empathy": 0.1485,
460
- "mu_naturalness": 0.8031
461
- }
462
- },
463
- "Germany": {
464
- "n": 15,
465
- "score_mean": -1.2286,
466
- "score_std": 1.4947,
467
- "mu_attributes": {
468
- "mu_coherence": 0.876,
469
- "mu_cultural_specificity": 0.5379,
470
- "mu_cultural_value": 0.6578,
471
- "mu_empathy": 0.1596,
472
- "mu_naturalness": 0.8005
473
- }
474
- },
475
- "India": {
476
- "n": 15,
477
- "score_mean": -0.8579,
478
- "score_std": 0.9938,
479
- "mu_attributes": {
480
- "mu_coherence": 0.8753,
481
- "mu_cultural_specificity": 0.5374,
482
- "mu_cultural_value": 0.6479,
483
- "mu_empathy": 0.1505,
484
- "mu_naturalness": 0.8042
485
- }
486
- },
487
- "Japan": {
488
- "n": 15,
489
- "score_mean": -0.7905,
490
- "score_std": 1.2135,
491
- "mu_attributes": {
492
- "mu_coherence": 0.8711,
493
- "mu_cultural_specificity": 0.5289,
494
- "mu_cultural_value": 0.6315,
495
- "mu_empathy": 0.1584,
496
- "mu_naturalness": 0.8073
497
- }
498
- },
499
- "Kenya": {
500
- "n": 15,
501
- "score_mean": -0.3351,
502
- "score_std": 0.9712,
503
- "mu_attributes": {
504
- "mu_coherence": 0.8747,
505
- "mu_cultural_specificity": 0.5799,
506
- "mu_cultural_value": 0.6654,
507
- "mu_empathy": 0.1466,
508
- "mu_naturalness": 0.8076
509
- }
510
- },
511
- "Mexico": {
512
- "n": 15,
513
- "score_mean": -0.4658,
514
- "score_std": 1.5211,
515
- "mu_attributes": {
516
- "mu_coherence": 0.8953,
517
- "mu_cultural_specificity": 0.5355,
518
- "mu_cultural_value": 0.6893,
519
- "mu_empathy": 0.1547,
520
- "mu_naturalness": 0.8031
521
- }
522
- },
523
- "Poland": {
524
- "n": 15,
525
- "score_mean": -1.5578,
526
- "score_std": 2.4427,
527
- "mu_attributes": {
528
- "mu_coherence": 0.8745,
529
- "mu_cultural_specificity": 0.5493,
530
- "mu_cultural_value": 0.6445,
531
- "mu_empathy": 0.1511,
532
- "mu_naturalness": 0.8065
533
- }
534
- },
535
- "South Africa": {
536
- "n": 15,
537
- "score_mean": -0.7571,
538
- "score_std": 1.4231,
539
- "mu_attributes": {
540
- "mu_coherence": 0.8753,
541
- "mu_cultural_specificity": 0.5734,
542
- "mu_cultural_value": 0.6654,
543
- "mu_empathy": 0.184,
544
- "mu_naturalness": 0.8112
545
- }
546
- },
547
- "Spain": {
548
- "n": 15,
549
- "score_mean": -1.1197,
550
- "score_std": 1.1839,
551
- "mu_attributes": {
552
- "mu_coherence": 0.8758,
553
- "mu_cultural_specificity": 0.5292,
554
- "mu_cultural_value": 0.6508,
555
- "mu_empathy": 0.1525,
556
- "mu_naturalness": 0.8068
557
- }
558
- },
559
- "United States of America": {
560
- "n": 15,
561
- "score_mean": -1.0909,
562
- "score_std": 1.4211,
563
- "mu_attributes": {
564
- "mu_coherence": 0.8852,
565
- "mu_cultural_specificity": 0.5185,
566
- "mu_cultural_value": 0.6552,
567
- "mu_empathy": 0.147,
568
- "mu_naturalness": 0.8185
569
- }
570
- }
571
- },
572
- "arousal": {
573
- "1": {
574
- "n": 12,
575
- "mean": -1.1308,
576
- "std": 1.2953
577
- },
578
- "2": {
579
- "n": 32,
580
- "mean": -1.301,
581
- "std": 1.2291
582
- },
583
- "3": {
584
- "n": 53,
585
- "mean": -0.6617,
586
- "std": 1.5538
587
- },
588
- "4": {
589
- "n": 51,
590
- "mean": -1.022,
591
- "std": 1.4584
592
- },
593
- "5": {
594
- "n": 32,
595
- "mean": -0.9719,
596
- "std": 1.37
597
- }
598
- },
599
- "score_vs_arousal": {
600
- "pearson": 0.0378,
601
- "spearman": 0.0356
602
- }
603
- }
604
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
results/eval_baseline.json DELETED
@@ -1,333 +0,0 @@
1
- {
2
- "model": "Skywork/Skywork-Reward-V2-Llama-3.1-8B",
3
- "type": "baseline",
4
- "scoring": {
5
- "evaluated": 5122,
6
- "skipped": 0,
7
- "attributes": {
8
- "co_discourse_structure": {
9
- "n": 1280,
10
- "mse": 267.768484,
11
- "pearson": 0.399503,
12
- "spearman": 0.402577
13
- },
14
- "co_logical_consistency": {
15
- "n": 1280,
16
- "mse": 269.287788,
17
- "pearson": 0.334027,
18
- "spearman": 0.345432
19
- },
20
- "co_mutual_grounding": {
21
- "n": 1280,
22
- "mse": 267.451739,
23
- "pearson": 0.415589,
24
- "spearman": 0.419526
25
- },
26
- "co_overall_coherence_score": {
27
- "n": 1280,
28
- "mse": 268.872669,
29
- "pearson": 0.389374,
30
- "spearman": 0.396514
31
- },
32
- "co_temporal_causal_coherence": {
33
- "n": 1280,
34
- "mse": 270.468984,
35
- "pearson": 0.366464,
36
- "spearman": 0.383941
37
- },
38
- "co_topic_coherence": {
39
- "n": 1280,
40
- "mse": 270.248653,
41
- "pearson": 0.373761,
42
- "spearman": 0.38069
43
- },
44
- "cs_causality": {
45
- "n": 1286,
46
- "mse": 368.376983,
47
- "pearson": 0.119173,
48
- "spearman": 0.107379
49
- },
50
- "cs_coherence": {
51
- "n": 1286,
52
- "mse": 396.305863,
53
- "pearson": 0.403845,
54
- "spearman": 0.400605
55
- },
56
- "cs_consistency": {
57
- "n": 1286,
58
- "mse": 375.106672,
59
- "pearson": 0.083197,
60
- "spearman": 0.077648
61
- },
62
- "cs_desire": {
63
- "n": 1286,
64
- "mse": 369.624386,
65
- "pearson": 0.127067,
66
- "spearman": 0.120142
67
- },
68
- "cs_empathy": {
69
- "n": 1286,
70
- "mse": 385.477148,
71
- "pearson": 0.222875,
72
- "spearman": 0.208758
73
- },
74
- "cs_reaction": {
75
- "n": 1286,
76
- "mse": 375.427491,
77
- "pearson": 0.199355,
78
- "spearman": 0.188804
79
- },
80
- "em_emotional_awareness": {
81
- "n": 1274,
82
- "mse": 428.055558,
83
- "pearson": 0.587765,
84
- "spearman": 0.609782
85
- },
86
- "em_emotional_validation": {
87
- "n": 1274,
88
- "mse": 425.000385,
89
- "pearson": 0.576845,
90
- "spearman": 0.646894
91
- },
92
- "em_helpful_response": {
93
- "n": 1274,
94
- "mse": 425.850433,
95
- "pearson": 0.463525,
96
- "spearman": 0.452144
97
- },
98
- "em_overall_empathy_score": {
99
- "n": 1274,
100
- "mse": 426.318754,
101
- "pearson": 0.618128,
102
- "spearman": 0.652989
103
- },
104
- "em_perspective_taking": {
105
- "n": 1274,
106
- "mse": 426.829288,
107
- "pearson": 0.620675,
108
- "spearman": 0.639341
109
- },
110
- "em_supportive_engagement": {
111
- "n": 1274,
112
- "mse": 425.657313,
113
- "pearson": 0.548979,
114
- "spearman": 0.57158
115
- },
116
- "mu_coherence": {
117
- "n": 1282,
118
- "mse": 68.11454,
119
- "pearson": 0.301372,
120
- "spearman": 0.327568
121
- },
122
- "mu_cultural_specificity": {
123
- "n": 1282,
124
- "mse": 66.841459,
125
- "pearson": 0.224928,
126
- "spearman": 0.243449
127
- },
128
- "mu_cultural_value": {
129
- "n": 1282,
130
- "mse": 66.84707,
131
- "pearson": 0.014693,
132
- "spearman": 0.017508
133
- },
134
- "mu_empathy": {
135
- "n": 1282,
136
- "mse": 63.197375,
137
- "pearson": 0.447345,
138
- "spearman": 0.398579
139
- },
140
- "mu_naturalness": {
141
- "n": 1282,
142
- "mse": 67.880863,
143
- "pearson": 0.22854,
144
- "spearman": 0.227955
145
- }
146
- },
147
- "domains": {
148
- "coherence": {
149
- "mse": 269.016386,
150
- "pearson": 0.379786,
151
- "spearman": 0.388113
152
- },
153
- "commonsense": {
154
- "mse": 378.386424,
155
- "pearson": 0.192585,
156
- "spearman": 0.183889
157
- },
158
- "empathy": {
159
- "mse": 426.285289,
160
- "pearson": 0.569319,
161
- "spearman": 0.595455
162
- },
163
- "multicultural": {
164
- "mse": 66.576262,
165
- "pearson": 0.243376,
166
- "spearman": 0.243012
167
- }
168
- },
169
- "average": {
170
- "mse": 294.565648,
171
- "pearson": 0.35074,
172
- "spearman": 0.357383
173
- }
174
- },
175
- "preference": {
176
- "total": 16091,
177
- "correct": 12528,
178
- "ties": 46,
179
- "skipped": 0,
180
- "accuracy": 77.8572,
181
- "margin_mean": 14.479973,
182
- "margin_std": 16.697453,
183
- "domains": {
184
- "coherence": {
185
- "accuracy": 68.363,
186
- "correct": 2539,
187
- "total": 3714,
188
- "ties": 13
189
- },
190
- "commonsense": {
191
- "accuracy": 98.2532,
192
- "correct": 5006,
193
- "total": 5095,
194
- "ties": 2
195
- },
196
- "empathy": {
197
- "accuracy": 92.0572,
198
- "correct": 3477,
199
- "total": 3777,
200
- "ties": 0
201
- },
202
- "multicultural": {
203
- "accuracy": 42.9672,
204
- "correct": 1506,
205
- "total": 3505,
206
- "ties": 31
207
- }
208
- },
209
- "difficulty": {
210
- "easy": {
211
- "accuracy": 86.5554,
212
- "correct": 10790,
213
- "total": 12466,
214
- "ties": 14
215
- },
216
- "hard": {
217
- "accuracy": 44.069,
218
- "correct": 639,
219
- "total": 1450,
220
- "ties": 8
221
- },
222
- "medium": {
223
- "accuracy": 50.5287,
224
- "correct": 1099,
225
- "total": 2175,
226
- "ties": 24
227
- }
228
- }
229
- },
230
- "cultural": {
231
- "evaluated": 180,
232
- "skipped": 0,
233
- "global_score": {
234
- "mean": -4.8502,
235
- "std": 6.9709,
236
- "min": -24.25,
237
- "max": 11.8125
238
- },
239
- "countries": {
240
- "Australia": {
241
- "n": 15,
242
- "score_mean": -3.379,
243
- "score_std": 7.5048
244
- },
245
- "Chile": {
246
- "n": 15,
247
- "score_mean": -2.7193,
248
- "score_std": 7.3955
249
- },
250
- "China": {
251
- "n": 15,
252
- "score_mean": -8.7948,
253
- "score_std": 7.0204
254
- },
255
- "Germany": {
256
- "n": 15,
257
- "score_mean": -2.6734,
258
- "score_std": 7.6305
259
- },
260
- "India": {
261
- "n": 15,
262
- "score_mean": -3.3963,
263
- "score_std": 6.6906
264
- },
265
- "Japan": {
266
- "n": 15,
267
- "score_mean": -6.0005,
268
- "score_std": 5.6984
269
- },
270
- "Kenya": {
271
- "n": 15,
272
- "score_mean": -5.2865,
273
- "score_std": 6.4503
274
- },
275
- "Mexico": {
276
- "n": 15,
277
- "score_mean": -4.4237,
278
- "score_std": 6.8976
279
- },
280
- "Poland": {
281
- "n": 15,
282
- "score_mean": -4.7628,
283
- "score_std": 5.3618
284
- },
285
- "South Africa": {
286
- "n": 15,
287
- "score_mean": -5.309,
288
- "score_std": 6.4878
289
- },
290
- "Spain": {
291
- "n": 15,
292
- "score_mean": -7.5237,
293
- "score_std": 6.7529
294
- },
295
- "United States of America": {
296
- "n": 15,
297
- "score_mean": -3.9339,
298
- "score_std": 6.5357
299
- }
300
- },
301
- "arousal": {
302
- "1": {
303
- "n": 12,
304
- "mean": -7.4241,
305
- "std": 6.301
306
- },
307
- "2": {
308
- "n": 32,
309
- "mean": -6.8282,
310
- "std": 6.8316
311
- },
312
- "3": {
313
- "n": 53,
314
- "mean": -5.1853,
315
- "std": 7.1044
316
- },
317
- "4": {
318
- "n": 51,
319
- "mean": -4.427,
320
- "std": 6.6602
321
- },
322
- "5": {
323
- "n": 32,
324
- "mean": -2.0265,
325
- "std": 6.5304
326
- }
327
- },
328
- "score_vs_arousal": {
329
- "pearson": 0.2261,
330
- "spearman": 0.2092
331
- }
332
- }
333
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
results/plots/cultural_attributes_by_country.png DELETED
Binary file (75.6 kB)
 
results/plots/preference_by_domain.png DELETED
Binary file (55.3 kB)
 
results/plots/preference_by_domain_baseline.png DELETED
Binary file (57.5 kB)
 
training_metadata.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "base_model_path": "Skywork/Skywork-Reward-V2-Llama-3.1-8B",
3
- "stage_1_weights_path": "model/regression_weights/Skywork-Reward-V2-Llama-3.1-8B_Multi-Domain-Data-Scoring_100pct.pt",
4
- "stage_2_weights_path": "model/gating_network/gating_network_Skywork-Reward-V2-Llama-3.1-8B_mo_Multi-Domain-Data-Scoring_pref_Multi-Domain-Data-Preference-Pairs_ref_null_t2.0_n30000_seed0_le0.0005_we0.0_n_1_hi64_dr0.05_ba2048_co0.03_lo2.0.pt"
5
- }
 
 
 
 
 
 
utils.py ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # utils.py — Shared utility functions for the multidomain_model pipeline.
2
+
3
+ import json
4
+ import os
5
+ from typing import Optional, Sequence
6
+
7
+ import torch
8
+ from transformers import AutoTokenizer
9
+
10
+
11
+ # ---------------------------------------------------------------------------
12
+ # Remote-code detection
13
+ # ---------------------------------------------------------------------------
14
+
15
+ def _requires_remote_code(model_path: str) -> bool:
16
+ """Return True when the model needs trust_remote_code=True."""
17
+ model_path_l = str(model_path).lower()
18
+ return "qwen3" in model_path_l
19
+
20
+
21
+ # ---------------------------------------------------------------------------
22
+ # Tokenizer loading
23
+ # ---------------------------------------------------------------------------
24
+
25
+ def _load_tokenizer_robust(model_path: str):
26
+ """Load tokenizer with fallback to slow tokenizer when fast conversion deps are missing."""
27
+ trust_remote_code = _requires_remote_code(model_path)
28
+ try:
29
+ return AutoTokenizer.from_pretrained(model_path, trust_remote_code=trust_remote_code)
30
+ except (ValueError, ImportError) as e:
31
+ print(f"Warning: Fast tokenizer load failed ({e}). Retrying with use_fast=False...")
32
+ return AutoTokenizer.from_pretrained(model_path, use_fast=False, trust_remote_code=trust_remote_code)
33
+
34
+
35
+ # ---------------------------------------------------------------------------
36
+ # Dataset / file resolution
37
+ # ---------------------------------------------------------------------------
38
+
39
+ def _resolve_local_dataset_file(dataset_path: str):
40
+ """Resolve local JSON/JSONL path, accepting optional missing extension."""
41
+ candidate_paths = [dataset_path]
42
+ if not dataset_path.endswith(".jsonl") and not dataset_path.endswith(".json"):
43
+ candidate_paths.extend([f"{dataset_path}.jsonl", f"{dataset_path}.json"])
44
+
45
+ for candidate in candidate_paths:
46
+ if os.path.isfile(candidate):
47
+ return candidate
48
+ return None
49
+
50
+
51
+ def _resolve_jsonl_path(path: str) -> str:
52
+ """Return *path* if it exists, otherwise try appending .jsonl."""
53
+ if os.path.isfile(path):
54
+ return path
55
+ candidate = path + ".jsonl"
56
+ if os.path.isfile(candidate):
57
+ return candidate
58
+ raise FileNotFoundError(f"Dataset not found: {path} (also tried {candidate})")
59
+
60
+
61
+ def load_cultural_test(data_dir: str) -> list[dict]:
62
+ """Load all JSON/JSONL cultural test files from *data_dir* and return a flat list of records."""
63
+ records: list[dict] = []
64
+ if not os.path.isdir(data_dir):
65
+ return records
66
+ for fname in sorted(os.listdir(data_dir)):
67
+ fpath = os.path.join(data_dir, fname)
68
+ if fname.endswith(".jsonl"):
69
+ with open(fpath, "r", encoding="utf-8") as f:
70
+ for line in f:
71
+ line = line.strip()
72
+ if line:
73
+ records.append(json.loads(line))
74
+ elif fname.endswith(".json"):
75
+ with open(fpath, "r", encoding="utf-8") as f:
76
+ rows = json.load(f)
77
+ if isinstance(rows, list):
78
+ records.extend(rows)
79
+ else:
80
+ records.append(rows)
81
+ return records
82
+
83
+
84
+ def parse_cultural_conversation(record: dict) -> list[dict]:
85
+ """Parse a cultural test record's conversation field into chat messages.
86
+
87
+ Maps the first speaker to 'user', the second to 'assistant', and merges
88
+ consecutive turns from the same speaker.
89
+ """
90
+ conv = record.get("conversation", "")
91
+ lines = conv.split("\n")
92
+ messages: list[dict] = []
93
+ speakers: dict[str, str] = {}
94
+
95
+ for line in lines:
96
+ line = line.strip()
97
+ if not line:
98
+ continue
99
+ idx = line.find(": ")
100
+ if idx <= 0:
101
+ continue
102
+ speaker_id = line[:idx]
103
+ text = line[idx + 2:]
104
+ if speaker_id not in speakers:
105
+ speakers[speaker_id] = "user" if len(speakers) == 0 else "assistant"
106
+ role = speakers[speaker_id]
107
+ if messages and messages[-1]["role"] == role:
108
+ messages[-1]["content"] += "\n" + text
109
+ else:
110
+ messages.append({"role": role, "content": text})
111
+ return messages
112
+
113
+
114
+ def load_jsonl_test(path: str) -> list[dict]:
115
+ """Load all records whose split == 'test' from a JSONL file."""
116
+ path = _resolve_jsonl_path(path)
117
+ records: list[dict] = []
118
+ with open(path, "r", encoding="utf-8") as f:
119
+ for line in f:
120
+ line = line.strip()
121
+ if not line:
122
+ continue
123
+ record = json.loads(line)
124
+ split = record.get("split") or record.get("metadata", {}).get("split")
125
+ if split == "test":
126
+ records.append(record)
127
+ return records
128
+
129
+
130
+ # ---------------------------------------------------------------------------
131
+ # Save-path construction (stages 1 & 2)
132
+ # ---------------------------------------------------------------------------
133
+
134
+ def _build_save_paths(base_data_dir: str, model_name: str, dataset_folder: str, base_file_stem: str, n_shards: int, shard_idx: int):
135
+ """Construct output dir and filename consistently across stages."""
136
+ final_dir = os.path.join(base_data_dir, "embeddings", model_name, dataset_folder)
137
+ os.makedirs(final_dir, exist_ok=True)
138
+ if n_shards > 1:
139
+ file_name = f"{base_file_stem}-{shard_idx:05d}-of-{n_shards:05d}.safetensors"
140
+ else:
141
+ file_name = f"{base_file_stem}.safetensors"
142
+ return final_dir, os.path.join(final_dir, file_name)
143
+
144
+
145
+ # ---------------------------------------------------------------------------
146
+ # Inference model path resolution
147
+ # ---------------------------------------------------------------------------
148
+
149
+ def _resolve_inference_model_path(
150
+ config: dict,
151
+ cli_model_path: str | None,
152
+ cli_model_parent_dir: str | None,
153
+ cli_model_name: str | None,
154
+ ) -> str:
155
+ if cli_model_path:
156
+ return cli_model_path
157
+
158
+ inference_cfg = config.get("inference", {}) if isinstance(config, dict) else {}
159
+ if not isinstance(inference_cfg, dict):
160
+ inference_cfg = {}
161
+
162
+ explicit_model_path = inference_cfg.get("model_path")
163
+ if explicit_model_path:
164
+ return str(explicit_model_path)
165
+
166
+ if cli_model_parent_dir or cli_model_name:
167
+ model_parent_dir = str(cli_model_parent_dir or inference_cfg.get("model_parent_dir", "model"))
168
+ model_name = cli_model_name or inference_cfg.get("model_name")
169
+ if not model_name:
170
+ raise ValueError("model_name must be provided via --model_name or config.yaml inference.model_name")
171
+ return os.path.join(model_parent_dir, str(model_name))
172
+
173
+ model_name = inference_cfg.get("model_name")
174
+ if not model_name:
175
+ raise ValueError("model_name must be provided via --model_name or config.yaml inference.model_name")
176
+ model_parent_dir = str(inference_cfg.get("model_parent_dir", "model"))
177
+ return os.path.join(model_parent_dir, str(model_name))
178
+
179
+
180
+ # ---------------------------------------------------------------------------
181
+ # Token patterns and gating-position lookup
182
+ # ---------------------------------------------------------------------------
183
+
184
+ # Canonical mapping uses "llama3" (stage-2 convention); "llama" is an alias
185
+ # so that modeling_custom / stage-3 lookups also resolve correctly.
186
+ TOKEN_PATTERNS_BY_MODEL_TYPE = {
187
+ # Llama3: "<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
188
+ "llama3": [128009, 128006, 78191, 128007, 271],
189
+ "llama": [128009, 128006, 78191, 128007, 271],
190
+ # Gemma2: "<end_of_turn>\n<start_of_turn>model\n"
191
+ "gemma2": [107, 108, 106, 2516, 108],
192
+ # Mistral Instruct: "[/INST]" marks the start of the assistant response.
193
+ "mistral": [733, 28748, 16289, 28793],
194
+ }
195
+
196
+
197
+ def find_token_for_gating(tokens: Sequence[int], model_type: Optional[str]) -> int:
198
+ """Return the start index of the last model-specific token pattern.
199
+
200
+ For Qwen3/auto (and any model_type without an explicit pattern), falls back
201
+ to the last token position.
202
+ """
203
+ if model_type == "qwen3":
204
+ return max(len(tokens) - 1, 0)
205
+
206
+ token_pattern = TOKEN_PATTERNS_BY_MODEL_TYPE.get(model_type)
207
+ if not token_pattern:
208
+ return max(len(tokens) - 1, 0)
209
+
210
+ token_pattern_len = len(token_pattern)
211
+ search_end = len(tokens)
212
+ for j in range(search_end - token_pattern_len, -1, -1):
213
+ if list(tokens[j:j + token_pattern_len]) == token_pattern:
214
+ return j
215
+ # Fallback if exact marker pattern is not present in rendered prompt.
216
+ return max(len(tokens) - 1, 0)
217
+
218
+
219
+ # ---------------------------------------------------------------------------
220
+ # Inference scoring helper
221
+ # ---------------------------------------------------------------------------
222
+
223
+ def _tokenize_chat(tokenizer, messages, device, max_length, *, add_generation_prompt=False):
224
+ """Render then tokenize a chat consistently across preparation and inference."""
225
+ text = tokenizer.apply_chat_template(
226
+ messages, tokenize=False, add_generation_prompt=add_generation_prompt,
227
+ )
228
+ encoding = tokenizer(
229
+ text, return_tensors="pt", padding=True, truncation=True, max_length=max_length,
230
+ )
231
+ return {
232
+ key: value.to(device) if isinstance(value, torch.Tensor) else value
233
+ for key, value in encoding.items()
234
+ }
235
+
236
+
237
+ @torch.no_grad()
238
+ def _score_messages(model, tokenizer, messages, device, max_length, gating_output_override=None):
239
+ """Tokenize chat messages and run one model forward pass."""
240
+ if (
241
+ gating_output_override is None
242
+ and getattr(model.config, "shared_prompt_gating", False)
243
+ and messages
244
+ and messages[-1].get("role") == "assistant"
245
+ and len(messages) > 1
246
+ ):
247
+ prompt_encoding = _tokenize_chat(
248
+ tokenizer, messages[:-1], device, max_length,
249
+ add_generation_prompt=True,
250
+ )
251
+ gating_output_override = model.compute_gating(
252
+ input_ids=prompt_encoding["input_ids"],
253
+ attention_mask=prompt_encoding.get("attention_mask"),
254
+ )
255
+ encoding = _tokenize_chat(tokenizer, messages, device, max_length)
256
+ return model(
257
+ input_ids=encoding["input_ids"],
258
+ attention_mask=encoding.get("attention_mask"),
259
+ gating_output_override=gating_output_override,
260
+ )
261
+
262
+
263
+ @torch.no_grad()
264
+ def _score_pair_shared_gate(
265
+ model, tokenizer, prompt_messages, chosen_messages, rejected_messages,
266
+ device, max_length,
267
+ ):
268
+ """Score a preference pair with one prompt-only gate shared by both candidates."""
269
+ prompt_encoding = _tokenize_chat(
270
+ tokenizer,
271
+ prompt_messages,
272
+ device,
273
+ max_length,
274
+ add_generation_prompt=True,
275
+ )
276
+ gating_output = model.compute_gating(
277
+ input_ids=prompt_encoding["input_ids"],
278
+ attention_mask=prompt_encoding.get("attention_mask"),
279
+ )
280
+ chosen = _score_messages(
281
+ model, tokenizer, chosen_messages, device, max_length, gating_output,
282
+ )
283
+ rejected = _score_messages(
284
+ model, tokenizer, rejected_messages, device, max_length, gating_output,
285
+ )
286
+ return chosen, rejected, gating_output