初始化项目,由ModelHub XC社区提供模型
Model: atrost/test_steerable_hf_model_v4 Source: Original Platform
This commit is contained in:
36
.gitattributes
vendored
Normal file
36
.gitattributes
vendored
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
*.7z filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.arrow filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ftz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.gz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.h5 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.joblib filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.model filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npy filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.onnx filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ot filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.parquet filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pb filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pickle filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pkl filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pth filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.rar filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
||||||
|
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tflite filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tgz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.wasm filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.xz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zip filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zst filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
||||||
199
README.md
Normal file
199
README.md
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
---
|
||||||
|
library_name: transformers
|
||||||
|
tags: []
|
||||||
|
---
|
||||||
|
|
||||||
|
# Model Card for Model ID
|
||||||
|
|
||||||
|
<!-- Provide a quick summary of what the model is/does. -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Model Details
|
||||||
|
|
||||||
|
### Model Description
|
||||||
|
|
||||||
|
<!-- Provide a longer summary of what this model is. -->
|
||||||
|
|
||||||
|
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
||||||
|
|
||||||
|
- **Developed by:** [More Information Needed]
|
||||||
|
- **Funded by [optional]:** [More Information Needed]
|
||||||
|
- **Shared by [optional]:** [More Information Needed]
|
||||||
|
- **Model type:** [More Information Needed]
|
||||||
|
- **Language(s) (NLP):** [More Information Needed]
|
||||||
|
- **License:** [More Information Needed]
|
||||||
|
- **Finetuned from model [optional]:** [More Information Needed]
|
||||||
|
|
||||||
|
### Model Sources [optional]
|
||||||
|
|
||||||
|
<!-- Provide the basic links for the model. -->
|
||||||
|
|
||||||
|
- **Repository:** [More Information Needed]
|
||||||
|
- **Paper [optional]:** [More Information Needed]
|
||||||
|
- **Demo [optional]:** [More Information Needed]
|
||||||
|
|
||||||
|
## Uses
|
||||||
|
|
||||||
|
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
||||||
|
|
||||||
|
### Direct Use
|
||||||
|
|
||||||
|
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
### Downstream Use [optional]
|
||||||
|
|
||||||
|
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
### Out-of-Scope Use
|
||||||
|
|
||||||
|
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
## Bias, Risks, and Limitations
|
||||||
|
|
||||||
|
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
### Recommendations
|
||||||
|
|
||||||
|
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
||||||
|
|
||||||
|
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
||||||
|
|
||||||
|
## How to Get Started with the Model
|
||||||
|
|
||||||
|
Use the code below to get started with the model.
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
## Training Details
|
||||||
|
|
||||||
|
### Training Data
|
||||||
|
|
||||||
|
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
### Training Procedure
|
||||||
|
|
||||||
|
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
||||||
|
|
||||||
|
#### Preprocessing [optional]
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
|
||||||
|
#### Training Hyperparameters
|
||||||
|
|
||||||
|
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
||||||
|
|
||||||
|
#### Speeds, Sizes, Times [optional]
|
||||||
|
|
||||||
|
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
## Evaluation
|
||||||
|
|
||||||
|
<!-- This section describes the evaluation protocols and provides the results. -->
|
||||||
|
|
||||||
|
### Testing Data, Factors & Metrics
|
||||||
|
|
||||||
|
#### Testing Data
|
||||||
|
|
||||||
|
<!-- This should link to a Dataset Card if possible. -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
#### Factors
|
||||||
|
|
||||||
|
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
#### Metrics
|
||||||
|
|
||||||
|
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
### Results
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
#### Summary
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Model Examination [optional]
|
||||||
|
|
||||||
|
<!-- Relevant interpretability work for the model goes here -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
## Environmental Impact
|
||||||
|
|
||||||
|
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
||||||
|
|
||||||
|
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
||||||
|
|
||||||
|
- **Hardware Type:** [More Information Needed]
|
||||||
|
- **Hours used:** [More Information Needed]
|
||||||
|
- **Cloud Provider:** [More Information Needed]
|
||||||
|
- **Compute Region:** [More Information Needed]
|
||||||
|
- **Carbon Emitted:** [More Information Needed]
|
||||||
|
|
||||||
|
## Technical Specifications [optional]
|
||||||
|
|
||||||
|
### Model Architecture and Objective
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
### Compute Infrastructure
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
#### Hardware
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
#### Software
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
## Citation [optional]
|
||||||
|
|
||||||
|
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
||||||
|
|
||||||
|
**BibTeX:**
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
**APA:**
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
## Glossary [optional]
|
||||||
|
|
||||||
|
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
## More Information [optional]
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
## Model Card Authors [optional]
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
|
|
||||||
|
## Model Card Contact
|
||||||
|
|
||||||
|
[More Information Needed]
|
||||||
1
chat_template.jinja
Normal file
1
chat_template.jinja
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='') %}{%- for message in messages %}{%- if message['role'] == 'system' %}{% set ns.system_prompt = message['content'] %}{%- endif %}{%- endfor %}{{bos_token}}{{ns.system_prompt}}{%- for message in messages %}{%- if message['role'] == 'user' %}{%- set ns.is_tool = false -%}{{'<|User|>' + message['content']}}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is none %}{%- set ns.is_tool = false -%}{%- for tool in message['tool_calls']%}{%- if not ns.is_first %}{{'<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{%- set ns.is_first = true -%}{%- else %}{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}{%- endif %}{%- endfor %}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is not none %}{%- if ns.is_tool %}{{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}{%- set ns.is_tool = false -%}{%- else %}{% set content = message['content'] %}{% if '</think>' in content %}{% set content = content.split('</think>')[-1] %}{% endif %}{{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_tool = true -%}{%- if ns.is_output_first %}{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- set ns.is_output_first = false %}{%- else %}{{'\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- endif %}{%- endif %}{%- endfor -%}{% if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}{% endif %}{% if add_generation_prompt and not ns.is_tool %}{{'<|Assistant|><think>\n'}}{% endif %}
|
||||||
63
config.json
Normal file
63
config.json
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
{
|
||||||
|
"architectures": [
|
||||||
|
"Qwen2ForCausalLMPostBlockSteeringFixed"
|
||||||
|
],
|
||||||
|
"attention_dropout": 0.0,
|
||||||
|
"auto_map": {
|
||||||
|
"AutoModel": "qwen2_postblock_steering_fixed.Qwen2ModelPostBlockSteering",
|
||||||
|
"AutoModelForCausalLM": "qwen2_postblock_steering_fixed.Qwen2ForCausalLMPostBlockSteeringFixed"
|
||||||
|
},
|
||||||
|
"bos_token_id": 151643,
|
||||||
|
"dtype": "bfloat16",
|
||||||
|
"eos_token_id": 151643,
|
||||||
|
"hidden_act": "silu",
|
||||||
|
"hidden_size": 1536,
|
||||||
|
"initializer_range": 0.02,
|
||||||
|
"intermediate_size": 8960,
|
||||||
|
"layer_types": [
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention",
|
||||||
|
"full_attention"
|
||||||
|
],
|
||||||
|
"max_position_embeddings": 131072,
|
||||||
|
"max_window_layers": 21,
|
||||||
|
"model_type": "qwen2",
|
||||||
|
"num_attention_heads": 12,
|
||||||
|
"num_hidden_layers": 28,
|
||||||
|
"num_key_value_heads": 2,
|
||||||
|
"rms_norm_eps": 1e-06,
|
||||||
|
"rope_scaling": null,
|
||||||
|
"rope_theta": 10000,
|
||||||
|
"sliding_window": null,
|
||||||
|
"tie_word_embeddings": false,
|
||||||
|
"transformers_version": "4.57.3",
|
||||||
|
"use_cache": true,
|
||||||
|
"use_mrope": false,
|
||||||
|
"use_sliding_window": false,
|
||||||
|
"vocab_size": 151936
|
||||||
|
}
|
||||||
9
generation_config.json
Normal file
9
generation_config.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"_from_model_config": true,
|
||||||
|
"bos_token_id": 151646,
|
||||||
|
"do_sample": true,
|
||||||
|
"eos_token_id": 151643,
|
||||||
|
"temperature": 0.6,
|
||||||
|
"top_p": 0.95,
|
||||||
|
"transformers_version": "4.57.3"
|
||||||
|
}
|
||||||
3
model.safetensors
Normal file
3
model.safetensors
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:3a71d392648143015bc5b5d56961e08a6c83c679b8513ffec9ed28303e5f0d3d
|
||||||
|
size 3555597480
|
||||||
354
qwen2_postblock_steering_fixed.py
Normal file
354
qwen2_postblock_steering_fixed.py
Normal file
@@ -0,0 +1,354 @@
|
|||||||
|
import torch
|
||||||
|
import os
|
||||||
|
import torch.nn as nn
|
||||||
|
from typing import Optional, Tuple, Iterable, Union
|
||||||
|
|
||||||
|
from transformers.models.qwen2.modeling_qwen2 import (
|
||||||
|
Qwen2ForCausalLM,
|
||||||
|
Qwen2Model,
|
||||||
|
Qwen2DecoderLayer,
|
||||||
|
)
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Low-rank adapter
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
def _get_activation(name: str):
|
||||||
|
name = name.lower()
|
||||||
|
if name in ("silu", "swish"):
|
||||||
|
return nn.SiLU()
|
||||||
|
if name == "relu":
|
||||||
|
return nn.ReLU()
|
||||||
|
if name == "gelu":
|
||||||
|
return nn.GELU()
|
||||||
|
if name == "tanh":
|
||||||
|
return nn.Tanh()
|
||||||
|
raise ValueError(f"Unknown activation: {name}")
|
||||||
|
|
||||||
|
class LowRankAdapter(nn.Module):
|
||||||
|
"""
|
||||||
|
Δh = α * W_up( act(W_down(h)) )
|
||||||
|
"""
|
||||||
|
def __init__(self, hidden_size: int, rank: int, alpha: float, activation: str):
|
||||||
|
super().__init__()
|
||||||
|
self.alpha = float(alpha)
|
||||||
|
self.act = _get_activation(activation)
|
||||||
|
self.down = nn.Linear(hidden_size, rank, bias=False)
|
||||||
|
self.up = nn.Linear(rank, hidden_size, bias=False)
|
||||||
|
|
||||||
|
# start as no-op => preserves pretrained behavior at init
|
||||||
|
nn.init.zeros_(self.up.weight)
|
||||||
|
|
||||||
|
def forward(self, h: torch.Tensor) -> torch.Tensor:
|
||||||
|
return self.alpha * self.up(self.act(self.down(h)))
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Steered Decoder Layer (post-block only)
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
class Qwen2DecoderLayerPostBlockSteering(Qwen2DecoderLayer):
|
||||||
|
"""
|
||||||
|
Drop-in Qwen2DecoderLayer that adds an adapter AFTER the block output.
|
||||||
|
|
||||||
|
apply_to:
|
||||||
|
- "last": apply only to last token (B,S,H) -> only position -1
|
||||||
|
- "all": apply to all tokens
|
||||||
|
"""
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config,
|
||||||
|
layer_idx: int,
|
||||||
|
# Custom arguments with defaults
|
||||||
|
enable: bool = True,
|
||||||
|
rank: int = 8,
|
||||||
|
alpha: float = 1.0,
|
||||||
|
activation: str = "silu",
|
||||||
|
apply_to: str = "all",
|
||||||
|
**kwargs # <--- Best Practice: Catch any extra args the parent might need in future versions
|
||||||
|
):
|
||||||
|
super().__init__(config, layer_idx, **kwargs)
|
||||||
|
assert apply_to in ("last", "all")
|
||||||
|
self.apply_to = apply_to
|
||||||
|
self._adapter_enabled = True
|
||||||
|
|
||||||
|
self.adapter_block = (
|
||||||
|
LowRankAdapter(
|
||||||
|
hidden_size=config.hidden_size,
|
||||||
|
rank=rank,
|
||||||
|
alpha=alpha,
|
||||||
|
activation=activation,
|
||||||
|
)
|
||||||
|
if enable
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
|
def set_adapter_enabled(self, enabled: bool):
|
||||||
|
self._adapter_enabled = bool(enabled)
|
||||||
|
|
||||||
|
def _apply_last(self, x: torch.Tensor, adapter: nn.Module) -> torch.Tensor:
|
||||||
|
if x.ndim != 3:
|
||||||
|
return x
|
||||||
|
last = x[:, -1, :] # (B,H)
|
||||||
|
new_last = (last + adapter(last)).unsqueeze(1) # (B,1,H)
|
||||||
|
return torch.cat([x[:, :-1, :], new_last], dim=1)
|
||||||
|
|
||||||
|
def _apply_all(self, x: torch.Tensor, adapter: nn.Module) -> torch.Tensor:
|
||||||
|
if x.ndim != 3:
|
||||||
|
return x
|
||||||
|
b, s, h = x.shape
|
||||||
|
flat = x.reshape(b * s, h)
|
||||||
|
delta = adapter(flat).reshape(b, s, h)
|
||||||
|
return x + delta
|
||||||
|
|
||||||
|
def _apply_adapter(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
|
if (self.adapter_block is None) or (not self._adapter_enabled):
|
||||||
|
return x
|
||||||
|
if self.apply_to == "last":
|
||||||
|
return self._apply_last(x, self.adapter_block)
|
||||||
|
return self._apply_all(x, self.adapter_block)
|
||||||
|
|
||||||
|
def forward(
|
||||||
|
self,
|
||||||
|
hidden_states: torch.Tensor,
|
||||||
|
attention_mask: Optional[torch.Tensor] = None,
|
||||||
|
position_ids: Optional[torch.LongTensor] = None,
|
||||||
|
past_key_value: Optional[Tuple[torch.Tensor]] = None, # legacy name
|
||||||
|
output_attentions: Optional[bool] = False,
|
||||||
|
use_cache: Optional[bool] = False,
|
||||||
|
cache_position: Optional[torch.LongTensor] = None,
|
||||||
|
position_embeddings: Optional[Tuple[torch.Tensor, torch.FloatTensor]] = None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
# Standard Qwen2 layer, inject adapter at the very end (post-block).
|
||||||
|
|
||||||
|
# NOTE: transformers 4.57+ Qwen2 expects decoder layers to return a Tensor
|
||||||
|
# (and optionally attn weights), NOT (hidden_states, ..., present_kv).
|
||||||
|
# Cache is carried via `past_key_values` (new API) and/or handled internally.
|
||||||
|
|
||||||
|
residual = hidden_states
|
||||||
|
hidden_states = self.input_layernorm(hidden_states)
|
||||||
|
|
||||||
|
# Avoid passing BOTH past_key_value and past_key_values to attention.
|
||||||
|
past_key_values = kwargs.pop("past_key_values", None)
|
||||||
|
attn_kwargs = dict(kwargs)
|
||||||
|
|
||||||
|
if past_key_values is not None:
|
||||||
|
attn_kwargs["past_key_values"] = past_key_values
|
||||||
|
# do NOT also pass legacy past_key_value
|
||||||
|
pkv_arg = {}
|
||||||
|
else:
|
||||||
|
pkv_arg = {"past_key_value": past_key_value} if past_key_value is not None else {}
|
||||||
|
|
||||||
|
attn_out = self.self_attn(
|
||||||
|
hidden_states=hidden_states,
|
||||||
|
attention_mask=attention_mask,
|
||||||
|
position_ids=position_ids,
|
||||||
|
output_attentions=output_attentions,
|
||||||
|
use_cache=use_cache,
|
||||||
|
cache_position=cache_position,
|
||||||
|
position_embeddings=position_embeddings,
|
||||||
|
**pkv_arg,
|
||||||
|
**attn_kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
# HF attention returns (attn_output,) or (attn_output, attn_weights)
|
||||||
|
if isinstance(attn_out, tuple):
|
||||||
|
attn_output = attn_out[0]
|
||||||
|
attn_weights = attn_out[1] if (output_attentions and len(attn_out) > 1) else None
|
||||||
|
else:
|
||||||
|
attn_output = attn_out
|
||||||
|
attn_weights = None
|
||||||
|
|
||||||
|
hidden_states = residual + attn_output
|
||||||
|
|
||||||
|
residual = hidden_states
|
||||||
|
hidden_states = self.post_attention_layernorm(hidden_states)
|
||||||
|
hidden_states = residual + self.mlp(hidden_states)
|
||||||
|
|
||||||
|
# ✅ post-block steering
|
||||||
|
hidden_states = self._apply_adapter(hidden_states)
|
||||||
|
|
||||||
|
# Return a Tensor (or Tensor + attn weights if requested). Do NOT return cache.
|
||||||
|
if output_attentions:
|
||||||
|
return (hidden_states, attn_weights)
|
||||||
|
return hidden_states
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Qwen2Model + hardcoded steering config
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
class Qwen2ModelPostBlockSteering(Qwen2Model):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config,
|
||||||
|
layers_to_steer: Union[str, Iterable[int]] = "all",
|
||||||
|
rank: int = 8,
|
||||||
|
apply_to: str = "all",
|
||||||
|
alpha: float = 1.0,
|
||||||
|
activation: str = "silu",
|
||||||
|
):
|
||||||
|
super().__init__(config)
|
||||||
|
|
||||||
|
if layers_to_steer == "all":
|
||||||
|
layer_ids = set(range(config.num_hidden_layers))
|
||||||
|
else:
|
||||||
|
layer_ids = set(int(i) for i in layers_to_steer)
|
||||||
|
|
||||||
|
new_layers = nn.ModuleList()
|
||||||
|
for i in range(config.num_hidden_layers):
|
||||||
|
new_layers.append(
|
||||||
|
Qwen2DecoderLayerPostBlockSteering(
|
||||||
|
config=config,
|
||||||
|
layer_idx=i,
|
||||||
|
enable=(i in layer_ids),
|
||||||
|
rank=rank,
|
||||||
|
alpha=alpha,
|
||||||
|
activation=activation,
|
||||||
|
apply_to=apply_to,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.layers = new_layers
|
||||||
|
|
||||||
|
def set_adapter_enabled(self, enabled: bool):
|
||||||
|
for layer in self.layers:
|
||||||
|
if hasattr(layer, "set_adapter_enabled"):
|
||||||
|
layer.set_adapter_enabled(enabled)
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Qwen2ForCausalLM with hardcoded knobs + base frozen by default
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
class Qwen2ForCausalLMPostBlockSteeringFixed(Qwen2ForCausalLM):
|
||||||
|
"""
|
||||||
|
Hardcoded steering config + base frozen by default.
|
||||||
|
|
||||||
|
Change these class constants to match what you want globally.
|
||||||
|
"""
|
||||||
|
STEER_RANK: int = 8
|
||||||
|
STEER_APPLY_TO: str = "last" # "last" or "all"
|
||||||
|
STEER_LAYERS: Union[str, Iterable[int]] = "all" # or e.g. [0, 5, 10]
|
||||||
|
STEER_ALPHA: float = 1.0
|
||||||
|
STEER_ACTIVATION: str = "silu"
|
||||||
|
|
||||||
|
def __init__(self, config):
|
||||||
|
super().__init__(config)
|
||||||
|
|
||||||
|
# Replace base transformer with steered one using hardcoded config
|
||||||
|
self.model = Qwen2ModelPostBlockSteering(
|
||||||
|
config,
|
||||||
|
layers_to_steer=self.STEER_LAYERS,
|
||||||
|
rank=self.STEER_RANK,
|
||||||
|
apply_to=self.STEER_APPLY_TO,
|
||||||
|
alpha=self.STEER_ALPHA,
|
||||||
|
activation=self.STEER_ACTIVATION,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Freeze base by default (only steering trainable)
|
||||||
|
self.freeze_base_keep_steering_trainable()
|
||||||
|
|
||||||
|
# ---- freezing / params ----
|
||||||
|
|
||||||
|
def freeze_base_keep_steering_trainable(self):
|
||||||
|
for n, p in self.named_parameters():
|
||||||
|
p.requires_grad = ("adapter_block" in n)
|
||||||
|
|
||||||
|
def steering_parameters(self):
|
||||||
|
for n, p in self.named_parameters():
|
||||||
|
if "adapter_block" in n:
|
||||||
|
yield p
|
||||||
|
|
||||||
|
# ---- dtype/device correctness for device_map="auto" ----
|
||||||
|
|
||||||
|
def cast_adapters_like_base(self):
|
||||||
|
"""
|
||||||
|
If you load with torch_dtype="auto" and/or device_map="auto",
|
||||||
|
adapters are newly-created modules and need to match each layer’s dtype/device.
|
||||||
|
"""
|
||||||
|
for layer in self.model.layers:
|
||||||
|
ref = layer.input_layernorm.weight
|
||||||
|
if getattr(layer, "adapter_block", None) is not None:
|
||||||
|
layer.adapter_block.to(device=ref.device, dtype=ref.dtype)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_pretrained(cls, *args, **kwargs):
|
||||||
|
model = super().from_pretrained(*args, **kwargs)
|
||||||
|
# Ensure adapters are on the right shards/dtype, then freeze base
|
||||||
|
if hasattr(model, "cast_adapters_like_base"):
|
||||||
|
model.cast_adapters_like_base()
|
||||||
|
if hasattr(model, "freeze_base_keep_steering_trainable"):
|
||||||
|
model.freeze_base_keep_steering_trainable()
|
||||||
|
return model
|
||||||
|
|
||||||
|
def _prepare_for_serialization(self):
|
||||||
|
"""
|
||||||
|
If the model was loaded with device_map/offload, Accelerate attaches hooks that
|
||||||
|
can break save_pretrained for newly-added params (like adapter_block.*).
|
||||||
|
This removes those hooks and consolidates to CPU.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
from accelerate.hooks import remove_hook_from_module
|
||||||
|
remove_hook_from_module(self, recurse=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Clean up common accelerate attributes if present
|
||||||
|
for attr in ("hf_device_map", "_hf_hook"):
|
||||||
|
if hasattr(self, attr):
|
||||||
|
try:
|
||||||
|
delattr(self, attr)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Ensure all params are materialized on CPU for a normal state_dict save
|
||||||
|
self.to("cpu")
|
||||||
|
|
||||||
|
def _strip_accelerate_offload_hooks(self):
|
||||||
|
"""
|
||||||
|
Remove Accelerate's device_map/offload hooks so saving doesn't go through
|
||||||
|
get_state_dict_from_offload (which doesn't know about new adapter params).
|
||||||
|
"""
|
||||||
|
# Best-effort official removers
|
||||||
|
try:
|
||||||
|
from accelerate.hooks import remove_hook_from_module
|
||||||
|
remove_hook_from_module(self, recurse=True) # documented API :contentReference[oaicite:3]{index=3}
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Hard removal: delete _hf_hook from every submodule if still present
|
||||||
|
for m in self.modules():
|
||||||
|
if hasattr(m, "_hf_hook"):
|
||||||
|
# try to detach cleanly if possible
|
||||||
|
try:
|
||||||
|
m._hf_hook.detach_hook(m)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
delattr(m, "_hf_hook")
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# device_map bookkeeping (common on big-model inference)
|
||||||
|
if hasattr(self, "hf_device_map"):
|
||||||
|
try:
|
||||||
|
delattr(self, "hf_device_map")
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def save_pretrained(self, save_directory, **kwargs):
|
||||||
|
os.makedirs(save_directory, exist_ok=True)
|
||||||
|
|
||||||
|
# 1) remove accelerate offload hooks
|
||||||
|
self._strip_accelerate_offload_hooks()
|
||||||
|
|
||||||
|
# 2) consolidate to CPU (you cannot save sharded/offloaded weights “in place”)
|
||||||
|
self.to("cpu")
|
||||||
|
|
||||||
|
# 3) create a normal state_dict and pass it explicitly to bypass accelerate offload-saving
|
||||||
|
# (save_pretrained supports state_dict=...) :contentReference[oaicite:4]{index=4}
|
||||||
|
sd = {k: v.cpu() for k, v in self.state_dict().items()}
|
||||||
|
|
||||||
|
return super().save_pretrained(save_directory, state_dict=sd, **kwargs)
|
||||||
23
special_tokens_map.json
Normal file
23
special_tokens_map.json
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"bos_token": {
|
||||||
|
"content": "<|begin▁of▁sentence|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false
|
||||||
|
},
|
||||||
|
"eos_token": {
|
||||||
|
"content": "<|end▁of▁sentence|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false
|
||||||
|
},
|
||||||
|
"pad_token": {
|
||||||
|
"content": "<|end▁of▁sentence|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false
|
||||||
|
}
|
||||||
|
}
|
||||||
3
tokenizer.json
Normal file
3
tokenizer.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:e20ddafc659ba90242154b55275402edeca0715e5dbb30f56815a4ce081f4893
|
||||||
|
size 11422778
|
||||||
194
tokenizer_config.json
Normal file
194
tokenizer_config.json
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
{
|
||||||
|
"add_bos_token": true,
|
||||||
|
"add_eos_token": false,
|
||||||
|
"add_prefix_space": null,
|
||||||
|
"added_tokens_decoder": {
|
||||||
|
"151643": {
|
||||||
|
"content": "<|end▁of▁sentence|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151644": {
|
||||||
|
"content": "<|User|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151645": {
|
||||||
|
"content": "<|Assistant|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151646": {
|
||||||
|
"content": "<|begin▁of▁sentence|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151647": {
|
||||||
|
"content": "<|EOT|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151648": {
|
||||||
|
"content": "<think>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151649": {
|
||||||
|
"content": "</think>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151650": {
|
||||||
|
"content": "<|quad_start|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151651": {
|
||||||
|
"content": "<|quad_end|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151652": {
|
||||||
|
"content": "<|vision_start|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151653": {
|
||||||
|
"content": "<|vision_end|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151654": {
|
||||||
|
"content": "<|vision_pad|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151655": {
|
||||||
|
"content": "<|image_pad|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151656": {
|
||||||
|
"content": "<|video_pad|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151657": {
|
||||||
|
"content": "<tool_call>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151658": {
|
||||||
|
"content": "</tool_call>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151659": {
|
||||||
|
"content": "<|fim_prefix|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151660": {
|
||||||
|
"content": "<|fim_middle|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151661": {
|
||||||
|
"content": "<|fim_suffix|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151662": {
|
||||||
|
"content": "<|fim_pad|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151663": {
|
||||||
|
"content": "<|repo_name|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151664": {
|
||||||
|
"content": "<|file_sep|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"bos_token": "<|begin▁of▁sentence|>",
|
||||||
|
"clean_up_tokenization_spaces": false,
|
||||||
|
"eos_token": "<|end▁of▁sentence|>",
|
||||||
|
"extra_special_tokens": {},
|
||||||
|
"legacy": true,
|
||||||
|
"model_max_length": 16384,
|
||||||
|
"pad_token": "<|end▁of▁sentence|>",
|
||||||
|
"sp_model_kwargs": {},
|
||||||
|
"tokenizer_class": "LlamaTokenizerFast",
|
||||||
|
"unk_token": null,
|
||||||
|
"use_default_system_prompt": false
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user