Upload 14 files
Browse files- .gitattributes +1 -0
- AsteriskForCausalLM.py +414 -0
- Gemini_Generated_Image_jvekprjvekprjvek.png +3 -0
- README.md +482 -3
- chat_template.jinja +6 -0
- config.json +50 -0
- generation_config.json +9 -0
- handler.py +126 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- special_tokens_map.json +34 -0
- tokenizer.json +0 -0
- tokenizer_config.json +154 -0
- training_args.bin +3 -0
- vocab.json +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Gemini_Generated_Image_jvekprjvekprjvek.png filter=lfs diff=lfs merge=lfs -text
|
AsteriskForCausalLM.py
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hybrid ASPP-Attention Architecture (Asterisk Model)
|
| 3 |
+
Combines Adjacency-Structured Parallel Propagation (ASPP) with standard attention mechanisms
|
| 4 |
+
to enhance model expressiveness while maintaining efficiency.
|
| 5 |
+
|
| 6 |
+
Architecture Design:
|
| 7 |
+
- Hybrid layers: Standard attention + ASPP operator in parallel
|
| 8 |
+
- Gate mechanism for dynamic fusion
|
| 9 |
+
- Knowledge distillation from SmolLM2-135M base model
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn as nn
|
| 14 |
+
import torch.nn.functional as F
|
| 15 |
+
from transformers import LlamaConfig, LlamaForCausalLM, LlamaModel
|
| 16 |
+
from transformers.models.llama.modeling_llama import (
|
| 17 |
+
LlamaAttention,
|
| 18 |
+
LlamaDecoderLayer,
|
| 19 |
+
LlamaRMSNorm,
|
| 20 |
+
LlamaMLP,
|
| 21 |
+
)
|
| 22 |
+
from transformers import AutoConfig, AutoModelForCausalLM
|
| 23 |
+
from typing import Optional, Tuple, List
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class AsteriskConfig(LlamaConfig):
|
| 27 |
+
"""
|
| 28 |
+
Configuration class for Asterisk model.
|
| 29 |
+
Inherits from LlamaConfig with custom model_type.
|
| 30 |
+
"""
|
| 31 |
+
model_type = "asterisk"
|
| 32 |
+
|
| 33 |
+
def __init__(
|
| 34 |
+
self,
|
| 35 |
+
hybrid_layer_indices: Optional[List[int]] = None,
|
| 36 |
+
aspp_hidden_dim: Optional[int] = None,
|
| 37 |
+
aspp_num_steps: int = 2,
|
| 38 |
+
aspp_dropout: float = 0.1,
|
| 39 |
+
# π-flow parameters
|
| 40 |
+
pi_flow: bool = False,
|
| 41 |
+
pi_flow_steps: int = 1,
|
| 42 |
+
pi_flow_scale: float = 0.2,
|
| 43 |
+
pi_flow_use_gate: bool = True,
|
| 44 |
+
**kwargs
|
| 45 |
+
):
|
| 46 |
+
super().__init__(**kwargs)
|
| 47 |
+
self.hybrid_layer_indices = hybrid_layer_indices
|
| 48 |
+
self.aspp_hidden_dim = aspp_hidden_dim
|
| 49 |
+
self.aspp_num_steps = aspp_num_steps
|
| 50 |
+
self.aspp_dropout = aspp_dropout
|
| 51 |
+
# π-flow config
|
| 52 |
+
self.pi_flow = pi_flow
|
| 53 |
+
self.pi_flow_steps = pi_flow_steps
|
| 54 |
+
self.pi_flow_scale = pi_flow_scale
|
| 55 |
+
self.pi_flow_use_gate = pi_flow_use_gate
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class ASPPOperator(nn.Module):
|
| 59 |
+
"""
|
| 60 |
+
Asterisk Operator (ASPP) - Point-wise Parallel Propagation
|
| 61 |
+
|
| 62 |
+
Simplified version WITHOUT neighbor gathering to reduce overfitting:
|
| 63 |
+
- Optional dimensionality reduction for efficiency
|
| 64 |
+
- Point-wise evolution: h_i^(t+1) = φ(h_i^(t)) [NO neighbors]
|
| 65 |
+
- Multi-step evolution for depth without added complexity
|
| 66 |
+
- Dropout for regularization
|
| 67 |
+
|
| 68 |
+
Args:
|
| 69 |
+
hidden_size: Dimension of hidden states (input/output)
|
| 70 |
+
aspp_hidden_dim: Internal dimension for ASPP (default: None, use hidden_size)
|
| 71 |
+
num_steps: Number of evolution steps K (default: 2)
|
| 72 |
+
dropout: Dropout rate for regularization (default: 0.1)
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
def __init__(self, hidden_size: int, aspp_hidden_dim: Optional[int] = None, num_steps: int = 2, dropout: float = 0.1):
|
| 76 |
+
super().__init__()
|
| 77 |
+
self.hidden_size = hidden_size
|
| 78 |
+
self.aspp_hidden_dim = aspp_hidden_dim or hidden_size
|
| 79 |
+
self.num_steps = num_steps
|
| 80 |
+
|
| 81 |
+
# Projection to lower dimension (if specified)
|
| 82 |
+
self.use_projection = (self.aspp_hidden_dim != hidden_size)
|
| 83 |
+
if self.use_projection:
|
| 84 |
+
self.down_proj = nn.Linear(hidden_size, self.aspp_hidden_dim)
|
| 85 |
+
self.up_proj = nn.Linear(self.aspp_hidden_dim, hidden_size)
|
| 86 |
+
self.proj_dropout = nn.Dropout(dropout)
|
| 87 |
+
|
| 88 |
+
# Point-wise update function φ - NO neighbor gathering
|
| 89 |
+
# Much smaller: only processes current position
|
| 90 |
+
self.update_net = nn.Sequential(
|
| 91 |
+
nn.Linear(self.aspp_hidden_dim, self.aspp_hidden_dim * 2),
|
| 92 |
+
nn.SiLU(),
|
| 93 |
+
nn.Dropout(dropout),
|
| 94 |
+
nn.Linear(self.aspp_hidden_dim * 2, self.aspp_hidden_dim),
|
| 95 |
+
nn.Dropout(dropout),
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Learnable K-step parameter
|
| 99 |
+
# sigmoid(1.0) ≈ 0.73, giving k_steps ≈ 1.5 → 2 steps initially
|
| 100 |
+
self.k_logit = nn.Parameter(torch.tensor(1.0))
|
| 101 |
+
|
| 102 |
+
# Learnable residual scale
|
| 103 |
+
self.residual_scale = nn.Parameter(torch.tensor(0.1))
|
| 104 |
+
|
| 105 |
+
# Layer norm for stability
|
| 106 |
+
self.norm = nn.LayerNorm(self.aspp_hidden_dim, eps=1e-5)
|
| 107 |
+
|
| 108 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 109 |
+
"""
|
| 110 |
+
Args:
|
| 111 |
+
hidden_states: [batch_size, seq_len, hidden_size]
|
| 112 |
+
Returns:
|
| 113 |
+
evolved_states: [batch_size, seq_len, hidden_size]
|
| 114 |
+
"""
|
| 115 |
+
# Project to lower dimension if needed
|
| 116 |
+
if self.use_projection:
|
| 117 |
+
h_t = self.down_proj(hidden_states)
|
| 118 |
+
h_t = self.proj_dropout(h_t)
|
| 119 |
+
else:
|
| 120 |
+
h_t = hidden_states
|
| 121 |
+
|
| 122 |
+
# Learnable number of steps
|
| 123 |
+
k_steps = max(1, int(torch.sigmoid(self.k_logit) * self.num_steps))
|
| 124 |
+
|
| 125 |
+
# K-step point-wise evolution (NO neighbor gathering)
|
| 126 |
+
for t in range(k_steps):
|
| 127 |
+
# Apply point-wise update rule φ
|
| 128 |
+
h_t_next = self.update_net(h_t)
|
| 129 |
+
|
| 130 |
+
# Scaled residual connection for stability
|
| 131 |
+
h_t = h_t + self.residual_scale * h_t_next
|
| 132 |
+
h_t = self.norm(h_t)
|
| 133 |
+
|
| 134 |
+
# Project back to original dimension if needed
|
| 135 |
+
if self.use_projection:
|
| 136 |
+
h_t = self.up_proj(h_t)
|
| 137 |
+
h_t = self.proj_dropout(h_t)
|
| 138 |
+
|
| 139 |
+
return h_t
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
class HybridASPPAttentionLayer(LlamaDecoderLayer):
|
| 143 |
+
"""
|
| 144 |
+
Hybrid layer combining ASPP operator and standard attention
|
| 145 |
+
Inherits from LlamaDecoderLayer to maintain compatibility
|
| 146 |
+
|
| 147 |
+
Architecture:
|
| 148 |
+
1. Parallel branches:
|
| 149 |
+
- ASPP operator for local structured reasoning
|
| 150 |
+
- Standard LlamaAttention for global context
|
| 151 |
+
2. Gated fusion of both outputs
|
| 152 |
+
3. π-flow refinement (optional, per-layer)
|
| 153 |
+
4. Feed-forward network
|
| 154 |
+
"""
|
| 155 |
+
|
| 156 |
+
def __init__(self, config: LlamaConfig, layer_idx: int, aspp_hidden_dim: Optional[int] = None, aspp_num_steps: int = 2, aspp_dropout: float = 0.1):
|
| 157 |
+
# Initialize parent LlamaDecoderLayer
|
| 158 |
+
super().__init__(config, layer_idx)
|
| 159 |
+
|
| 160 |
+
# Add ASPP branch
|
| 161 |
+
self.aspp_operator = ASPPOperator(
|
| 162 |
+
hidden_size=config.hidden_size,
|
| 163 |
+
aspp_hidden_dim=aspp_hidden_dim,
|
| 164 |
+
num_steps=aspp_num_steps,
|
| 165 |
+
dropout=aspp_dropout
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
# Gated fusion mechanism with dropout
|
| 169 |
+
self.fusion_gate = nn.Sequential(
|
| 170 |
+
nn.Linear(config.hidden_size * 2, config.hidden_size),
|
| 171 |
+
nn.Dropout(aspp_dropout),
|
| 172 |
+
nn.Sigmoid()
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
# Initialize gate to be balanced (output 0.5 initially)
|
| 176 |
+
with torch.no_grad():
|
| 177 |
+
self.fusion_gate[0].bias.fill_(0.0) # sigmoid(0) = 0.5
|
| 178 |
+
|
| 179 |
+
# π-flow: Per-layer refinement ASPP
|
| 180 |
+
if getattr(config, 'pi_flow', False):
|
| 181 |
+
self.pi_flow_aspp = ASPPOperator(
|
| 182 |
+
hidden_size=config.hidden_size,
|
| 183 |
+
aspp_hidden_dim=aspp_hidden_dim,
|
| 184 |
+
num_steps=aspp_num_steps,
|
| 185 |
+
dropout=aspp_dropout
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# Learnable flow scale (per-layer)
|
| 189 |
+
self.pi_flow_scale = nn.Parameter(
|
| 190 |
+
torch.tensor(getattr(config, 'pi_flow_scale', 0.2))
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
# Token-wise adaptive gating (optional)
|
| 194 |
+
if getattr(config, 'pi_flow_use_gate', True):
|
| 195 |
+
self.pi_flow_gate = nn.Sequential(
|
| 196 |
+
nn.Linear(config.hidden_size, config.hidden_size // 4),
|
| 197 |
+
nn.SiLU(),
|
| 198 |
+
nn.Dropout(aspp_dropout),
|
| 199 |
+
nn.Linear(config.hidden_size // 4, 1),
|
| 200 |
+
nn.Sigmoid()
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
def forward(
|
| 204 |
+
self,
|
| 205 |
+
hidden_states: torch.Tensor,
|
| 206 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 207 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 208 |
+
past_key_values = None,
|
| 209 |
+
use_cache: Optional[bool] = False,
|
| 210 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 211 |
+
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
|
| 212 |
+
**kwargs,
|
| 213 |
+
) -> torch.Tensor:
|
| 214 |
+
"""
|
| 215 |
+
Override LlamaDecoderLayer.forward to add ASPP branch and π-flow
|
| 216 |
+
Returns single tensor like LlamaDecoderLayer
|
| 217 |
+
"""
|
| 218 |
+
residual = hidden_states
|
| 219 |
+
hidden_states = self.input_layernorm(hidden_states)
|
| 220 |
+
|
| 221 |
+
# ASPP branch
|
| 222 |
+
aspp_output = self.aspp_operator(hidden_states)
|
| 223 |
+
|
| 224 |
+
# Attention branch - use parent's self_attn (returns tuple, discard cache with _)
|
| 225 |
+
attn_output, _ = self.self_attn(
|
| 226 |
+
hidden_states=hidden_states,
|
| 227 |
+
attention_mask=attention_mask,
|
| 228 |
+
position_ids=position_ids,
|
| 229 |
+
past_key_values=past_key_values,
|
| 230 |
+
cache_position=cache_position,
|
| 231 |
+
position_embeddings=position_embeddings,
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
# Gated fusion
|
| 235 |
+
fusion_input = torch.cat([aspp_output, attn_output], dim=-1)
|
| 236 |
+
gate = self.fusion_gate(fusion_input)
|
| 237 |
+
|
| 238 |
+
# Combine with gating: gate * ASPP + (1-gate) * Attention
|
| 239 |
+
fused_output = gate * aspp_output + (1 - gate) * attn_output
|
| 240 |
+
|
| 241 |
+
# Residual connection
|
| 242 |
+
hidden_states = residual + fused_output
|
| 243 |
+
|
| 244 |
+
# π-flow: Multi-step refinement in probability space (per-layer)
|
| 245 |
+
if hasattr(self, 'pi_flow_aspp'):
|
| 246 |
+
pi_flow_steps = getattr(self.config if hasattr(self, 'config') else kwargs.get('config'), 'pi_flow_steps', 1)
|
| 247 |
+
|
| 248 |
+
for step in range(pi_flow_steps):
|
| 249 |
+
# Compute velocity field v(h) using ASPP
|
| 250 |
+
v = self.pi_flow_aspp(hidden_states)
|
| 251 |
+
|
| 252 |
+
# Compute adaptive gate (per-token flow strength)
|
| 253 |
+
if hasattr(self, 'pi_flow_gate'):
|
| 254 |
+
gate = self.pi_flow_gate(hidden_states) # [B, L, 1]
|
| 255 |
+
alpha = self.pi_flow_scale * gate
|
| 256 |
+
else:
|
| 257 |
+
alpha = self.pi_flow_scale
|
| 258 |
+
|
| 259 |
+
# Euler step: h' = h + α * v(h)
|
| 260 |
+
hidden_states = hidden_states + alpha * v
|
| 261 |
+
|
| 262 |
+
# MLP block (use parent's mlp)
|
| 263 |
+
residual = hidden_states
|
| 264 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
| 265 |
+
hidden_states = self.mlp(hidden_states)
|
| 266 |
+
hidden_states = residual + hidden_states
|
| 267 |
+
|
| 268 |
+
# Return only hidden_states tensor, like LlamaDecoderLayer
|
| 269 |
+
return hidden_states
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
class AsteriskLlamaModel(LlamaModel):
|
| 273 |
+
"""
|
| 274 |
+
Asterisk-Llama model with full hybrid ASPP-Attention architecture
|
| 275 |
+
|
| 276 |
+
All layers use hybrid ASPP+Attention by default for maximum expressiveness.
|
| 277 |
+
"""
|
| 278 |
+
|
| 279 |
+
def __init__(self, config: LlamaConfig, hybrid_layer_indices: Optional[List[int]] = None, aspp_hidden_dim: Optional[int] = None, aspp_num_steps: int = 2, aspp_dropout: float = 0.1):
|
| 280 |
+
super().__init__(config)
|
| 281 |
+
|
| 282 |
+
# Determine which layers to make hybrid (default: ALL layers)
|
| 283 |
+
if hybrid_layer_indices is None:
|
| 284 |
+
# Use ALL layers as hybrid (full hybrid architecture)
|
| 285 |
+
num_layers = config.num_hidden_layers
|
| 286 |
+
hybrid_layer_indices = list(range(num_layers))
|
| 287 |
+
|
| 288 |
+
self.hybrid_layer_indices = hybrid_layer_indices
|
| 289 |
+
|
| 290 |
+
# Replace specified layers with hybrid layers (with per-layer π-flow if enabled)
|
| 291 |
+
for idx in hybrid_layer_indices:
|
| 292 |
+
if idx < len(self.layers):
|
| 293 |
+
self.layers[idx] = HybridASPPAttentionLayer(
|
| 294 |
+
config,
|
| 295 |
+
layer_idx=idx,
|
| 296 |
+
aspp_hidden_dim=aspp_hidden_dim,
|
| 297 |
+
aspp_num_steps=aspp_num_steps,
|
| 298 |
+
aspp_dropout=aspp_dropout
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
# Initialize weights
|
| 302 |
+
self.post_init()
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
class AsteriskForCausalLM(LlamaForCausalLM):
|
| 306 |
+
"""
|
| 307 |
+
Asterisk Causal LM with Hybrid ASPP-Attention architecture
|
| 308 |
+
|
| 309 |
+
Registered as: AsteriskForCausalLM
|
| 310 |
+
"""
|
| 311 |
+
|
| 312 |
+
config_class = AsteriskConfig
|
| 313 |
+
|
| 314 |
+
def __init__(self, config: AsteriskConfig, hybrid_layer_indices: Optional[List[int]] = None, aspp_hidden_dim: Optional[int] = None, aspp_num_steps: int = 2, aspp_dropout: float = 0.1):
|
| 315 |
+
# Read all ASPP parameters from config if not explicitly provided
|
| 316 |
+
if hybrid_layer_indices is None and hasattr(config, 'hybrid_layer_indices'):
|
| 317 |
+
hybrid_layer_indices = config.hybrid_layer_indices
|
| 318 |
+
if aspp_hidden_dim is None and hasattr(config, 'aspp_hidden_dim'):
|
| 319 |
+
aspp_hidden_dim = config.aspp_hidden_dim
|
| 320 |
+
if hasattr(config, 'aspp_num_steps'):
|
| 321 |
+
aspp_num_steps = config.aspp_num_steps
|
| 322 |
+
if hasattr(config, 'aspp_dropout'):
|
| 323 |
+
aspp_dropout = config.aspp_dropout
|
| 324 |
+
|
| 325 |
+
super().__init__(config)
|
| 326 |
+
|
| 327 |
+
# Replace model with Asterisk version
|
| 328 |
+
self.model = AsteriskLlamaModel(config, hybrid_layer_indices, aspp_hidden_dim, aspp_num_steps, aspp_dropout)
|
| 329 |
+
|
| 330 |
+
# Store hybrid layer info in config for serialization
|
| 331 |
+
self.config.hybrid_layer_indices = hybrid_layer_indices
|
| 332 |
+
|
| 333 |
+
# Initialize weights
|
| 334 |
+
self.post_init()
|
| 335 |
+
|
| 336 |
+
@classmethod
|
| 337 |
+
def from_pretrained_base(
|
| 338 |
+
cls,
|
| 339 |
+
base_model_path: str,
|
| 340 |
+
hybrid_layer_indices: Optional[List[int]] = None,
|
| 341 |
+
aspp_hidden_dim: Optional[int] = None,
|
| 342 |
+
aspp_num_steps: int = 2,
|
| 343 |
+
aspp_dropout: float = 0.1,
|
| 344 |
+
# π-flow parameters
|
| 345 |
+
pi_flow: bool = False,
|
| 346 |
+
pi_flow_steps: int = 1,
|
| 347 |
+
pi_flow_scale: float = 0.2,
|
| 348 |
+
pi_flow_use_gate: bool = True,
|
| 349 |
+
**kwargs
|
| 350 |
+
):
|
| 351 |
+
"""
|
| 352 |
+
Load base model and convert to Asterisk architecture
|
| 353 |
+
|
| 354 |
+
Args:
|
| 355 |
+
base_model_path: Path to base SmolLM2 model
|
| 356 |
+
hybrid_layer_indices: Which layers to make hybrid (None for all)
|
| 357 |
+
aspp_hidden_dim: Internal dimension for ASPP (None = use model hidden_size)
|
| 358 |
+
aspp_num_steps: Number of evolution steps K for ASPP (default: 2)
|
| 359 |
+
aspp_dropout: Dropout rate for ASPP regularization (default: 0.1)
|
| 360 |
+
pi_flow: Enable π-flow refinement step (default: False)
|
| 361 |
+
pi_flow_steps: Number of flow refinement steps (default: 1)
|
| 362 |
+
pi_flow_scale: Initial flow scale parameter (default: 0.2)
|
| 363 |
+
pi_flow_use_gate: Use token-wise adaptive gating (default: True)
|
| 364 |
+
"""
|
| 365 |
+
# Load base model
|
| 366 |
+
base_model = LlamaForCausalLM.from_pretrained(base_model_path, **kwargs)
|
| 367 |
+
base_config = base_model.config
|
| 368 |
+
|
| 369 |
+
# Create Asterisk config from base config with ASPP + π-flow params
|
| 370 |
+
asterisk_config = AsteriskConfig(
|
| 371 |
+
**base_config.to_dict(),
|
| 372 |
+
hybrid_layer_indices=hybrid_layer_indices,
|
| 373 |
+
aspp_hidden_dim=aspp_hidden_dim,
|
| 374 |
+
aspp_num_steps=aspp_num_steps,
|
| 375 |
+
aspp_dropout=aspp_dropout,
|
| 376 |
+
pi_flow=pi_flow,
|
| 377 |
+
pi_flow_steps=pi_flow_steps,
|
| 378 |
+
pi_flow_scale=pi_flow_scale,
|
| 379 |
+
pi_flow_use_gate=pi_flow_use_gate,
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
# Create Asterisk model
|
| 383 |
+
asterisk_model = cls(asterisk_config, hybrid_layer_indices, aspp_hidden_dim, aspp_num_steps, aspp_dropout)
|
| 384 |
+
|
| 385 |
+
# Transfer weights from base model (non-hybrid layers and embeddings)
|
| 386 |
+
asterisk_model.load_state_dict(base_model.state_dict(), strict=False)
|
| 387 |
+
|
| 388 |
+
print(f"✓ Converted base model to Asterisk architecture")
|
| 389 |
+
print(f" Hybrid layers: {asterisk_model.model.hybrid_layer_indices}")
|
| 390 |
+
aspp_dim_str = f"{aspp_hidden_dim}" if aspp_hidden_dim else f"{base_config.hidden_size} (full)"
|
| 391 |
+
print(f" ASPP config: dim={aspp_dim_str}, steps={aspp_num_steps}, dropout={aspp_dropout}")
|
| 392 |
+
if pi_flow:
|
| 393 |
+
print(f" π-flow enabled: steps={pi_flow_steps}, scale={pi_flow_scale}, gate={pi_flow_use_gate}")
|
| 394 |
+
|
| 395 |
+
return asterisk_model, base_model
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
# Register the model for AutoModel
|
| 399 |
+
AutoConfig.register("asterisk", AsteriskConfig)
|
| 400 |
+
AutoModelForCausalLM.register(AsteriskConfig, AsteriskForCausalLM)
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
def get_model_info(model):
|
| 404 |
+
"""Print model architecture information"""
|
| 405 |
+
total_params = sum(p.numel() for p in model.parameters())
|
| 406 |
+
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 407 |
+
|
| 408 |
+
print(f" • Total parameters: {total_params:,}")
|
| 409 |
+
print(f" • Trainable parameters: {trainable_params:,}")
|
| 410 |
+
print(f" • Model size: {total_params * 4 / 1024**2:.2f} MB (fp32)")
|
| 411 |
+
|
| 412 |
+
if isinstance(model, AsteriskForCausalLM):
|
| 413 |
+
print(f" • Hybrid layer indices: {model.model.hybrid_layer_indices}")
|
| 414 |
+
print(f" • Number of hybrid layers: {len(model.model.hybrid_layer_indices)}")
|
Gemini_Generated_Image_jvekprjvekprjvek.png
ADDED
|
Git LFS Details
|
README.md
CHANGED
|
@@ -1,3 +1,482 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
model_name: Asterisk-Pi
|
| 4 |
+
base_model: NoesisLab/Asterisk
|
| 5 |
+
tags:
|
| 6 |
+
- aspp
|
| 7 |
+
- pi-flow
|
| 8 |
+
- hybrid-architecture
|
| 9 |
+
- graph-reasoning
|
| 10 |
+
- probability-flow
|
| 11 |
+
- sft
|
| 12 |
+
- trl
|
| 13 |
+
license: apache-2.0
|
| 14 |
+
language:
|
| 15 |
+
- en
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# Asterisk-Pi: ASPP-Attention with π-Flow Refinement
|
| 19 |
+
|
| 20 |
+
**Asterisk-Pi** is an enhanced version of the Asterisk model that adds **π-flow (probability flow)** refinement to the hybrid ASPP-Attention architecture. Building on the SmolLM2-135M base, Asterisk-Pi implements per-layer iterative refinement inspired by probability flow ODEs from diffusion models, enabling multi-step reasoning through continuous state evolution.
|
| 21 |
+
|
| 22 |
+
## Model Description
|
| 23 |
+
|
| 24 |
+
- **Base Model**: [Asterisk](https://huggingface.co/NoesisLab/Asterisk) (SmolLM2-135M-Instruct with ASPP)
|
| 25 |
+
- **Architecture**: Hybrid ASPP-Attention + Per-Layer π-Flow (30 hybrid layers)
|
| 26 |
+
- **Parameters**: 173.7M (37.5M ASPP + 2.5M π-flow parameters)
|
| 27 |
+
- **Training**: Supervised Fine-Tuning on Mixed Benchmark Dataset
|
| 28 |
+
- **Framework**: Transformers 4.57.6, TRL 0.27.0
|
| 29 |
+
|
| 30 |
+
## Key Innovation: π-Flow Refinement
|
| 31 |
+
|
| 32 |
+
**π-Flow** (Probability Flow) adds iterative refinement to each hybrid layer, inspired by continuous-time probability flow ODEs:
|
| 33 |
+
|
| 34 |
+
```
|
| 35 |
+
h' = h + α * v(h) [Euler discretization]
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
Where:
|
| 39 |
+
- `v(h)` is the velocity field computed by a dedicated ASPP operator
|
| 40 |
+
- `α` is a learnable per-token scaling factor (adaptive gating)
|
| 41 |
+
- Applied after ASPP-Attention fusion in each layer
|
| 42 |
+
|
| 43 |
+
This enables **60 total refinement steps** (30 layers × 2 steps each) throughout the model, allowing gradual convergence to more refined representations.
|
| 44 |
+
|
| 45 |
+
## Evaluation Results
|
| 46 |
+
|
| 47 |
+
Evaluated on LM-Evaluation-Harness:
|
| 48 |
+
|
| 49 |
+
| Task | Metric | Asterisk-Pi | Asterisk (Base) | Δ |
|
| 50 |
+
|------|--------|-------------|-----------------|---|
|
| 51 |
+
| **ARC-Challenge** | acc_norm | **0.3038** | 0.2884 | +0.0154 |
|
| 52 |
+
| **ARC-Easy** | acc_norm | **0.5412** | 0.5450 | -0.0038 |
|
| 53 |
+
| **HellaSwag** | acc_norm | **0.4207** | 0.4430 | -0.0223 |
|
| 54 |
+
| **PIQA** | acc_norm | **0.6703** | 0.6770 | -0.0067 |
|
| 55 |
+
| **WinoGrande** | acc | **0.5391** | 0.5210 | +0.0181 |
|
| 56 |
+
|
| 57 |
+
### Analysis
|
| 58 |
+
|
| 59 |
+
π-Flow shows improvements on:
|
| 60 |
+
- **ARC-Challenge** (+1.54%): More challenging reasoning benefits from iterative refinement
|
| 61 |
+
- **WinoGrande** (+1.81%): Multi-step resolution helps with pronoun disambiguation
|
| 62 |
+
|
| 63 |
+
Mixed results on simpler tasks suggest π-flow adds reasoning depth that's most beneficial for complex multi-step problems.
|
| 64 |
+
|
| 65 |
+
## Architecture
|
| 66 |
+
|
| 67 |
+
### Overview
|
| 68 |
+
|
| 69 |
+

|
| 70 |
+
|
| 71 |
+
*Figure: Asterisk-Pi architecture showing the hybrid ASPP-Attention structure with π-flow refinement. Each of the 30 layers contains parallel ASPP and Attention branches, gated fusion, and iterative π-flow refinement using probability flow ODE.*
|
| 72 |
+
|
| 73 |
+
```
|
| 74 |
+
Input → [30 Hybrid Layers with π-Flow] → Output
|
| 75 |
+
|
| 76 |
+
Each Hybrid Layer:
|
| 77 |
+
1. ASPP-Attention Fusion (from base Asterisk)
|
| 78 |
+
2. π-Flow Refinement (NEW)
|
| 79 |
+
3. Feed-Forward Network
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
### 1. Hybrid ASPP-Attention Layer (Base Asterisk)
|
| 83 |
+
|
| 84 |
+
```python
|
| 85 |
+
class HybridASPPAttentionLayer:
|
| 86 |
+
"""
|
| 87 |
+
Combines ASPP operator with standard attention
|
| 88 |
+
|
| 89 |
+
Components:
|
| 90 |
+
- ASPP operator: Local structured reasoning
|
| 91 |
+
- Standard attention: Global context
|
| 92 |
+
- Gated fusion: Dynamic balancing
|
| 93 |
+
"""
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
**Fusion mechanism:**
|
| 97 |
+
```
|
| 98 |
+
aspp_out = ASPP(hidden_states)
|
| 99 |
+
attn_out = Attention(hidden_states, mask, ...)
|
| 100 |
+
gate = sigmoid(linear([aspp_out || attn_out]))
|
| 101 |
+
fused = gate * aspp_out + (1 - gate) * attn_out
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
### 2. π-Flow Refinement (Per-Layer)
|
| 105 |
+
|
| 106 |
+
```python
|
| 107 |
+
# Added to each hybrid layer
|
| 108 |
+
self.pi_flow_aspp = ASPPOperator(...) # Velocity field network
|
| 109 |
+
self.pi_flow_scale = Parameter(0.2) # Learnable flow strength
|
| 110 |
+
self.pi_flow_gate = MLP(hidden_size -> 1) # Token-wise adaptive gating
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
**π-Flow forward pass:**
|
| 114 |
+
```
|
| 115 |
+
function π_flow_refinement(hidden_states):
|
| 116 |
+
for step = 1 to π_flow_steps:
|
| 117 |
+
# Compute velocity field using dedicated ASPP
|
| 118 |
+
v = pi_flow_aspp(hidden_states)
|
| 119 |
+
|
| 120 |
+
# Adaptive per-token gating
|
| 121 |
+
gate = sigmoid(pi_flow_gate(hidden_states)) # [B, L, 1]
|
| 122 |
+
alpha = pi_flow_scale * gate
|
| 123 |
+
|
| 124 |
+
# Euler step in probability space
|
| 125 |
+
hidden_states = hidden_states + alpha * v
|
| 126 |
+
|
| 127 |
+
return hidden_states
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
**Key design choices:**
|
| 131 |
+
1. **Per-layer π-flow**: Each of 30 layers has independent π-flow parameters
|
| 132 |
+
2. **Learnable scale**: `pi_flow_scale` adapts flow strength during training
|
| 133 |
+
3. **Token-wise gating**: Different tokens get different flow magnitudes
|
| 134 |
+
4. **ASPP velocity**: Reuses ASPP architecture for computing v(h)
|
| 135 |
+
|
| 136 |
+
### 3. Complete Layer Pseudocode
|
| 137 |
+
|
| 138 |
+
```
|
| 139 |
+
function HybridLayerWithPiFlow(hidden_states, attention_mask, ...):
|
| 140 |
+
residual = hidden_states
|
| 141 |
+
hidden_states = input_layernorm(hidden_states)
|
| 142 |
+
|
| 143 |
+
# === Hybrid ASPP-Attention (Base Asterisk) ===
|
| 144 |
+
aspp_output = aspp_operator(hidden_states)
|
| 145 |
+
attn_output = self_attention(hidden_states, attention_mask, ...)
|
| 146 |
+
|
| 147 |
+
# Gated fusion
|
| 148 |
+
fusion_input = concat([aspp_output, attn_output])
|
| 149 |
+
gate = sigmoid(linear(dropout(fusion_input)))
|
| 150 |
+
fused_output = gate * aspp_output + (1 - gate) * attn_output
|
| 151 |
+
|
| 152 |
+
# Residual connection
|
| 153 |
+
hidden_states = residual + fused_output
|
| 154 |
+
|
| 155 |
+
# === π-Flow Refinement (NEW) ===
|
| 156 |
+
for step in [1..pi_flow_steps]:
|
| 157 |
+
v = pi_flow_aspp(hidden_states)
|
| 158 |
+
alpha = pi_flow_scale * sigmoid(pi_flow_gate(hidden_states))
|
| 159 |
+
hidden_states = hidden_states + alpha * v
|
| 160 |
+
|
| 161 |
+
# === MLP Block ===
|
| 162 |
+
residual = hidden_states
|
| 163 |
+
hidden_states = post_attention_layernorm(hidden_states)
|
| 164 |
+
hidden_states = mlp(hidden_states)
|
| 165 |
+
hidden_states = residual + hidden_states
|
| 166 |
+
|
| 167 |
+
return hidden_states
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
## Parameter Breakdown
|
| 171 |
+
|
| 172 |
+
| Component | Parameters | Notes |
|
| 173 |
+
|-----------|------------|-------|
|
| 174 |
+
| **Base SmolLM2** | 135.6M | Embeddings, attention, MLP |
|
| 175 |
+
| **ASPP Operators** | 35.5M | 30 layers × ~1.2M each |
|
| 176 |
+
| **π-Flow ASPPs** | 2.3M | 30 layers × ~77k each |
|
| 177 |
+
| **π-Flow Gates** | 0.2M | 30 layers × ~7k each |
|
| 178 |
+
| **π-Flow Scales** | 30 | 30 learnable scalars |
|
| 179 |
+
| **Total** | **173.7M** | +28% vs base SmolLM2 |
|
| 180 |
+
|
| 181 |
+
π-Flow adds only **1.4% more parameters** (2.5M) compared to base Asterisk (171.2M) while providing 60 total refinement steps.
|
| 182 |
+
|
| 183 |
+
## Quick Start
|
| 184 |
+
|
| 185 |
+
```python
|
| 186 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 187 |
+
import torch
|
| 188 |
+
|
| 189 |
+
# Load model and tokenizer
|
| 190 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 191 |
+
"path/to/Asterisk-Pi",
|
| 192 |
+
trust_remote_code=True,
|
| 193 |
+
torch_dtype=torch.bfloat16,
|
| 194 |
+
device_map="auto"
|
| 195 |
+
)
|
| 196 |
+
tokenizer = AutoTokenizer.from_pretrained("path/to/Asterisk-Pi")
|
| 197 |
+
|
| 198 |
+
# Generate text
|
| 199 |
+
messages = [{"role": "user", "content": "Explain the waterfall model in software engineering."}]
|
| 200 |
+
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
|
| 201 |
+
|
| 202 |
+
outputs = model.generate(
|
| 203 |
+
inputs,
|
| 204 |
+
max_new_tokens=256,
|
| 205 |
+
temperature=0.7,
|
| 206 |
+
do_sample=True,
|
| 207 |
+
)
|
| 208 |
+
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
## Training Details
|
| 212 |
+
|
| 213 |
+
### Training Dataset
|
| 214 |
+
|
| 215 |
+
Mixed benchmark dataset for testing true capabilities:
|
| 216 |
+
|
| 217 |
+
| Dataset | Ratio | Purpose |
|
| 218 |
+
|---------|-------|---------|
|
| 219 |
+
| **GSM8K** | 25% | Math reasoning benchmark |
|
| 220 |
+
| **HellaSwag** | 30% | Commonsense reasoning benchmark |
|
| 221 |
+
| **ARC** | 20% | Science QA (Easy + Challenge) |
|
| 222 |
+
| **OpenHermes** | 10% | High-quality long-form responses |
|
| 223 |
+
| **Capybara** | 15% | Multi-turn conversations |
|
| 224 |
+
|
| 225 |
+
Total: ~10,148 training samples
|
| 226 |
+
|
| 227 |
+
### Training Configuration
|
| 228 |
+
|
| 229 |
+
- **Starting Point**: Asterisk checkpoint (base ASPP-Attention model)
|
| 230 |
+
- **Optimizer**: AdamW (lr=5e-4, weight_decay=0.1)
|
| 231 |
+
- **Batch Size**: 2 per device, gradient accumulation=4 (effective batch=8)
|
| 232 |
+
- **Epochs**: 2
|
| 233 |
+
- **Scheduler**: Linear warmup (10% of steps)
|
| 234 |
+
- **Mixed Precision**: bfloat16
|
| 235 |
+
- **Gradient Checkpointing**: Enabled
|
| 236 |
+
- **Max Grad Norm**: 1.0
|
| 237 |
+
|
| 238 |
+
### π-Flow Configuration
|
| 239 |
+
|
| 240 |
+
```python
|
| 241 |
+
pi_flow = True
|
| 242 |
+
pi_flow_steps = 2 # 2 refinement steps per layer
|
| 243 |
+
pi_flow_scale = 1.0 # Initial flow strength
|
| 244 |
+
pi_flow_use_gate = True # Token-wise adaptive gating
|
| 245 |
+
```
|
| 246 |
+
|
| 247 |
+
### ASPP Configuration (Inherited from Base)
|
| 248 |
+
|
| 249 |
+
```python
|
| 250 |
+
aspp_hidden_dim = 256 # Internal dimension (vs 576 model hidden_size)
|
| 251 |
+
aspp_num_steps = 4 # Evolution steps for ASPP
|
| 252 |
+
aspp_dropout = 0.2 # Regularization
|
| 253 |
+
hybrid_layer_indices = None # All 30 layers
|
| 254 |
+
```
|
| 255 |
+
|
| 256 |
+
## Model Creation from Base Asterisk
|
| 257 |
+
|
| 258 |
+
```python
|
| 259 |
+
from AsteriskForCausalLM import AsteriskForCausalLM
|
| 260 |
+
from safetensors.torch import load_file
|
| 261 |
+
import torch
|
| 262 |
+
|
| 263 |
+
# Load Asterisk config and inject π-flow parameters
|
| 264 |
+
from AsteriskForCausalLM import AsteriskConfig
|
| 265 |
+
config = AsteriskConfig.from_pretrained("path/to/Asterisk", trust_remote_code=True)
|
| 266 |
+
|
| 267 |
+
# Add π-flow configuration
|
| 268 |
+
config.pi_flow = True
|
| 269 |
+
config.pi_flow_steps = 2
|
| 270 |
+
config.pi_flow_scale = 1.0
|
| 271 |
+
config.pi_flow_use_gate = True
|
| 272 |
+
|
| 273 |
+
# Create model with π-flow
|
| 274 |
+
model = AsteriskForCausalLM(config)
|
| 275 |
+
|
| 276 |
+
# Load pretrained Asterisk weights (strict=False ignores new π-flow params)
|
| 277 |
+
state_dict = load_file("path/to/Asterisk/model.safetensors")
|
| 278 |
+
missing_keys, unexpected_keys = model.load_state_dict(state_dict, strict=False)
|
| 279 |
+
|
| 280 |
+
# π-flow parameters are randomly initialized
|
| 281 |
+
print(f"New π-flow parameters: {len(missing_keys)}")
|
| 282 |
+
|
| 283 |
+
# Move to device
|
| 284 |
+
model = model.to(dtype=torch.bfloat16, device="cuda")
|
| 285 |
+
```
|
| 286 |
+
|
| 287 |
+
## Theoretical Background
|
| 288 |
+
|
| 289 |
+
### π-Flow: Probability Flow ODE
|
| 290 |
+
|
| 291 |
+
Inspired by diffusion model score-based formulations:
|
| 292 |
+
|
| 293 |
+
```
|
| 294 |
+
dx/dt = v(x, t) [Continuous probability flow]
|
| 295 |
+
```
|
| 296 |
+
|
| 297 |
+
Discretized with Euler method:
|
| 298 |
+
```
|
| 299 |
+
x_{t+1} = x_t + Δt * v(x_t)
|
| 300 |
+
```
|
| 301 |
+
|
| 302 |
+
In Asterisk-Pi:
|
| 303 |
+
- `x_t` = hidden states at layer output
|
| 304 |
+
- `v(x_t)` = velocity field from dedicated ASPP
|
| 305 |
+
- `Δt` = learnable `pi_flow_scale * gate(x_t)`
|
| 306 |
+
|
| 307 |
+
### Multi-Scale Refinement
|
| 308 |
+
|
| 309 |
+
- **Layer-level**: 30 hybrid layers with ASPP-Attention fusion
|
| 310 |
+
- **π-Flow level**: 2 steps per layer = 60 total refinement operations
|
| 311 |
+
- **ASPP-level**: 4 evolution steps within each ASPP = 240 micro-updates
|
| 312 |
+
|
| 313 |
+
This creates a **hierarchical refinement cascade** enabling gradual convergence to high-quality representations.
|
| 314 |
+
|
| 315 |
+
### Why π-Flow Helps
|
| 316 |
+
|
| 317 |
+
1. **Iterative refinement**: Multiple passes allow correcting errors
|
| 318 |
+
2. **Adaptive flow**: Token-wise gating focuses computation where needed
|
| 319 |
+
3. **Gradient flow**: More direct paths for gradient propagation
|
| 320 |
+
4. **Expressiveness**: Increases model capacity with minimal parameters
|
| 321 |
+
|
| 322 |
+
## Implementation Details
|
| 323 |
+
|
| 324 |
+
### Return Type Handling
|
| 325 |
+
|
| 326 |
+
Critical for Transformers compatibility:
|
| 327 |
+
|
| 328 |
+
```python
|
| 329 |
+
# HybridASPPAttentionLayer.forward() returns tensor only
|
| 330 |
+
def forward(self, hidden_states, ...) -> torch.Tensor:
|
| 331 |
+
# ... ASPP + Attention + π-flow ...
|
| 332 |
+
return hidden_states # ✅ Tensor, not tuple
|
| 333 |
+
|
| 334 |
+
# This matches LlamaDecoderLayer API: -> torch.Tensor
|
| 335 |
+
```
|
| 336 |
+
|
| 337 |
+
### Gradient Checkpointing Compatibility
|
| 338 |
+
|
| 339 |
+
π-Flow is fully compatible with gradient checkpointing:
|
| 340 |
+
- All operations are standard PyTorch ops
|
| 341 |
+
- No custom CUDA kernels
|
| 342 |
+
- Automatic differentiation through flow steps
|
| 343 |
+
|
| 344 |
+
### Weight Initialization
|
| 345 |
+
|
| 346 |
+
- **ASPP parameters**: Transferred from base Asterisk
|
| 347 |
+
- **π-Flow ASPP**: Randomly initialized (Xavier uniform)
|
| 348 |
+
- **π-Flow scale**: Initialized to 0.2 (conservative)
|
| 349 |
+
- **π-Flow gate**: Initialized to output ~0.5 (balanced)
|
| 350 |
+
|
| 351 |
+
## Files in Checkpoint
|
| 352 |
+
|
| 353 |
+
```
|
| 354 |
+
Asterisk-Pi/
|
| 355 |
+
├── AsteriskForCausalLM.py # Model implementation (with π-flow)
|
| 356 |
+
├── config.json # Model configuration
|
| 357 |
+
├── model.safetensors # Model weights
|
| 358 |
+
├── tokenizer.json # Tokenizer
|
| 359 |
+
├── generation_config.json # Generation settings
|
| 360 |
+
└── README.md # This file
|
| 361 |
+
```
|
| 362 |
+
|
| 363 |
+
## Differences from Base Asterisk
|
| 364 |
+
|
| 365 |
+
| Feature | Asterisk | Asterisk-Pi |
|
| 366 |
+
|---------|----------|-------------|
|
| 367 |
+
| **ASPP-Attention** | ✅ | ✅ |
|
| 368 |
+
| **π-Flow Refinement** | ❌ | ✅ (per-layer) |
|
| 369 |
+
| **Parameters** | 171.2M | 173.7M (+1.4%) |
|
| 370 |
+
| **Refinement Steps** | 30 (layers) | 60 (30 layers × 2) |
|
| 371 |
+
| **Training Dataset** | Capybara | Mixed Benchmarks |
|
| 372 |
+
| **Complexity** | Medium | High |
|
| 373 |
+
|
| 374 |
+
## Known Issues & Solutions
|
| 375 |
+
|
| 376 |
+
### 1. Return Type Errors
|
| 377 |
+
|
| 378 |
+
**Issue**: `AttributeError: 'tuple' object has no attribute 'dtype'`
|
| 379 |
+
|
| 380 |
+
**Solution**: `HybridASPPAttentionLayer.forward()` must return `torch.Tensor` only, not tuple. This matches the `LlamaDecoderLayer` API in transformers 4.57.6.
|
| 381 |
+
|
| 382 |
+
### 2. π-Flow in All Layers vs Final Layer
|
| 383 |
+
|
| 384 |
+
**Initial approach**: π-flow only in final layer (limited expressiveness)
|
| 385 |
+
|
| 386 |
+
**Current approach**: π-flow in all 30 hybrid layers for maximum refinement capability.
|
| 387 |
+
|
| 388 |
+
### 3. Training Stability
|
| 389 |
+
|
| 390 |
+
π-Flow can cause instability with high learning rates. Use:
|
| 391 |
+
- Lower learning rate (5e-4 vs 2e-5 for base)
|
| 392 |
+
- Gradient clipping (max_norm=1.0)
|
| 393 |
+
- Conservative initial flow scale (0.2-1.0)
|
| 394 |
+
|
| 395 |
+
## Dependencies
|
| 396 |
+
|
| 397 |
+
```bash
|
| 398 |
+
pip install torch>=2.0.0
|
| 399 |
+
pip install transformers>=4.40.0
|
| 400 |
+
pip install trl>=0.8.0
|
| 401 |
+
pip install datasets>=2.14.0
|
| 402 |
+
pip install accelerate>=0.25.0
|
| 403 |
+
pip install bitsandbytes
|
| 404 |
+
pip install safetensors
|
| 405 |
+
```
|
| 406 |
+
|
| 407 |
+
## Citations
|
| 408 |
+
|
| 409 |
+
If you use this model, please cite:
|
| 410 |
+
|
| 411 |
+
```bibtex
|
| 412 |
+
@misc{asteriskpi2026,
|
| 413 |
+
title={Asterisk-Pi: Probability Flow Refinement for Hybrid ASPP-Attention Models},
|
| 414 |
+
author={NoesisLab},
|
| 415 |
+
year={2026},
|
| 416 |
+
publisher={Huggingface},
|
| 417 |
+
url={https://huggingface.co/NoesisLab/Asterisk-Pi}
|
| 418 |
+
}
|
| 419 |
+
```
|
| 420 |
+
|
| 421 |
+
```bibtex
|
| 422 |
+
@misc{asterisk2026,
|
| 423 |
+
title={Asterisk: Hybrid ASPP-Attention Architecture for Enhanced Language Modeling},
|
| 424 |
+
author={NoesisLab},
|
| 425 |
+
year={2026},
|
| 426 |
+
publisher={Huggingface},
|
| 427 |
+
url={https://huggingface.co/NoesisLab/Asterisk}
|
| 428 |
+
}
|
| 429 |
+
```
|
| 430 |
+
|
| 431 |
+
```bibtex
|
| 432 |
+
@misc{vonwerra2022trl,
|
| 433 |
+
title={{TRL: Transformer Reinforcement Learning}},
|
| 434 |
+
author={Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallouédec},
|
| 435 |
+
year={2020},
|
| 436 |
+
journal={GitHub repository},
|
| 437 |
+
publisher={GitHub},
|
| 438 |
+
howpublished={\url{https://github.com/huggingface/trl}}
|
| 439 |
+
}
|
| 440 |
+
```
|
| 441 |
+
|
| 442 |
+
```bibtex
|
| 443 |
+
@article{allal2024SmolLM2,
|
| 444 |
+
title={SmolLM2 - with great data, comes great performance},
|
| 445 |
+
author={Allal, Loubna Ben and Lozhkov, Anton and Penedo, Guilherme and Wolf, Thomas and von Werra, Leandro},
|
| 446 |
+
year={2024}
|
| 447 |
+
}
|
| 448 |
+
```
|
| 449 |
+
|
| 450 |
+
## Related Work
|
| 451 |
+
|
| 452 |
+
- **Diffusion Models**: π-flow inspired by probability flow ODEs in score-based diffusion
|
| 453 |
+
- **Neural ODEs**: Continuous-depth models with adaptive computation
|
| 454 |
+
- **Iterative Refinement**: Multi-pass decoding in sequence models
|
| 455 |
+
|
| 456 |
+
## Future Directions
|
| 457 |
+
|
| 458 |
+
1. **Adaptive π-flow steps**: Learn number of refinement steps per layer
|
| 459 |
+
2. **Higher-order ODE solvers**: Replace Euler with RK4 or adaptive schemes
|
| 460 |
+
3. **Stochastic π-flow**: Add noise injection for exploration
|
| 461 |
+
4. **Cross-layer π-flow**: Allow information flow between distant layers
|
| 462 |
+
|
| 463 |
+
## License
|
| 464 |
+
|
| 465 |
+
This model inherits the Apache 2.0 license from SmolLM2-135M-Instruct.
|
| 466 |
+
|
| 467 |
+
## Framework Versions
|
| 468 |
+
|
| 469 |
+
- **TRL**: 0.27.0
|
| 470 |
+
- **Transformers**: 4.57.6
|
| 471 |
+
- **PyTorch**: 2.8.0+cu128
|
| 472 |
+
- **Datasets**: 4.5.0
|
| 473 |
+
- **Tokenizers**: 0.22.2
|
| 474 |
+
|
| 475 |
+
## Acknowledgments
|
| 476 |
+
|
| 477 |
+
Built on top of:
|
| 478 |
+
- [Asterisk](https://huggingface.co/NoesisLab/Asterisk) - Base ASPP-Attention architecture
|
| 479 |
+
- [SmolLM2-135M-Instruct](https://huggingface.co/HuggingFaceTB/SmolLM2-135M-Instruct) - Foundation model
|
| 480 |
+
- [TRL](https://github.com/huggingface/trl) - Training framework
|
| 481 |
+
|
| 482 |
+
Special thanks to the diffusion model community for probability flow ODE insights.
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '<|im_start|>system
|
| 2 |
+
You are a helpful AI assistant named SmolLM, trained by Hugging Face<|im_end|>
|
| 3 |
+
' }}{% endif %}{{'<|im_start|>' + message['role'] + '
|
| 4 |
+
' + message['content'] + '<|im_end|>' + '
|
| 5 |
+
'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant
|
| 6 |
+
' }}{% endif %}
|
config.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"AsteriskForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"aspp_dropout": 0.2,
|
| 6 |
+
"aspp_hidden_dim": 256,
|
| 7 |
+
"aspp_num_steps": 4,
|
| 8 |
+
"attention_bias": false,
|
| 9 |
+
"attention_dropout": 0.0,
|
| 10 |
+
"auto_map": {
|
| 11 |
+
"AutoConfig": "AsteriskForCausalLM.AsteriskConfig",
|
| 12 |
+
"AutoModelForCausalLM": "AsteriskForCausalLM.AsteriskForCausalLM"
|
| 13 |
+
},
|
| 14 |
+
"bos_token_id": 1,
|
| 15 |
+
"dtype": "bfloat16",
|
| 16 |
+
"eos_token_id": 2,
|
| 17 |
+
"head_dim": 64,
|
| 18 |
+
"hidden_act": "silu",
|
| 19 |
+
"hidden_size": 576,
|
| 20 |
+
"hybrid_layer_indices": null,
|
| 21 |
+
"initializer_range": 0.041666666666666664,
|
| 22 |
+
"intermediate_size": 1536,
|
| 23 |
+
"is_llama_config": true,
|
| 24 |
+
"max_position_embeddings": 8192,
|
| 25 |
+
"mlp_bias": false,
|
| 26 |
+
"model_type": "asterisk",
|
| 27 |
+
"num_attention_heads": 9,
|
| 28 |
+
"num_hidden_layers": 30,
|
| 29 |
+
"num_key_value_heads": 3,
|
| 30 |
+
"pad_token_id": 2,
|
| 31 |
+
"pi_flow": true,
|
| 32 |
+
"pi_flow_scale": 1.0,
|
| 33 |
+
"pi_flow_steps": 2,
|
| 34 |
+
"pi_flow_use_gate": true,
|
| 35 |
+
"pretraining_tp": 1,
|
| 36 |
+
"rms_norm_eps": 1e-05,
|
| 37 |
+
"rope_interleaved": false,
|
| 38 |
+
"rope_scaling": null,
|
| 39 |
+
"rope_theta": 100000,
|
| 40 |
+
"tie_word_embeddings": true,
|
| 41 |
+
"transformers.js_config": {
|
| 42 |
+
"kv_cache_dtype": {
|
| 43 |
+
"fp16": "float16",
|
| 44 |
+
"q4f16": "float16"
|
| 45 |
+
}
|
| 46 |
+
},
|
| 47 |
+
"transformers_version": "4.57.6",
|
| 48 |
+
"use_cache": true,
|
| 49 |
+
"vocab_size": 49152
|
| 50 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1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+
# handler.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from typing import Any, Dict, List, Union
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
Json = Dict[str, Any]
|
| 11 |
+
Messages = List[Dict[str, str]] # [{"role":"user|assistant|system", "content":"..."}]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _is_messages(x: Any) -> bool:
|
| 15 |
+
return (
|
| 16 |
+
isinstance(x, list)
|
| 17 |
+
and len(x) > 0
|
| 18 |
+
and all(isinstance(m, dict) and "role" in m and "content" in m for m in x)
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class EndpointHandler:
|
| 23 |
+
"""
|
| 24 |
+
Hugging Face Inference Endpoints custom handler.
|
| 25 |
+
Expects:
|
| 26 |
+
- request body is a dict
|
| 27 |
+
- always contains `inputs`
|
| 28 |
+
- may contain `parameters` for generation
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
def __init__(self, model_dir: str):
|
| 32 |
+
self.model_dir = model_dir
|
| 33 |
+
|
| 34 |
+
# Pick dtype/device
|
| 35 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 36 |
+
if self.device == "cuda":
|
| 37 |
+
# bfloat16 is usually safe on A100/H100; if your instance doesn't support bf16, change to float16
|
| 38 |
+
self.dtype = torch.bfloat16
|
| 39 |
+
else:
|
| 40 |
+
self.dtype = torch.float32
|
| 41 |
+
|
| 42 |
+
# IMPORTANT: trust_remote_code=True because repo contains AsteriskForCausalLM.py + auto_map
|
| 43 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 44 |
+
model_dir,
|
| 45 |
+
trust_remote_code=True,
|
| 46 |
+
use_fast=True,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Make sure pad token exists (your config uses pad_token_id=2 which equals eos_token_id in many llama-like models)
|
| 50 |
+
if self.tokenizer.pad_token_id is None and self.tokenizer.eos_token_id is not None:
|
| 51 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 52 |
+
|
| 53 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 54 |
+
model_dir,
|
| 55 |
+
trust_remote_code=True,
|
| 56 |
+
torch_dtype=self.dtype,
|
| 57 |
+
device_map="auto" if self.device == "cuda" else None,
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if self.device != "cuda":
|
| 61 |
+
self.model.to(self.device)
|
| 62 |
+
|
| 63 |
+
self.model.eval()
|
| 64 |
+
|
| 65 |
+
@torch.inference_mode()
|
| 66 |
+
def __call__(self, data: Json) -> Union[Json, List[Json]]:
|
| 67 |
+
inputs = data.get("inputs", "")
|
| 68 |
+
params = data.get("parameters", {}) or {}
|
| 69 |
+
|
| 70 |
+
# Generation defaults (can be overridden via `parameters`)
|
| 71 |
+
max_new_tokens = int(params.get("max_new_tokens", 256))
|
| 72 |
+
temperature = float(params.get("temperature", 0.7))
|
| 73 |
+
top_p = float(params.get("top_p", 0.95))
|
| 74 |
+
top_k = int(params.get("top_k", 0))
|
| 75 |
+
repetition_penalty = float(params.get("repetition_penalty", 1.0))
|
| 76 |
+
|
| 77 |
+
do_sample = bool(params.get("do_sample", temperature > 0))
|
| 78 |
+
num_beams = int(params.get("num_beams", 1))
|
| 79 |
+
|
| 80 |
+
def _one(item: Any) -> Json:
|
| 81 |
+
# Accept:
|
| 82 |
+
# 1) string prompt
|
| 83 |
+
# 2) messages list: [{"role":"user","content":"..."}]
|
| 84 |
+
# 3) dict {"messages":[...]} (common chat style)
|
| 85 |
+
if isinstance(item, dict) and "messages" in item:
|
| 86 |
+
item = item["messages"]
|
| 87 |
+
|
| 88 |
+
if _is_messages(item):
|
| 89 |
+
# Chat template path exists in repo; tokenizer.apply_chat_template will use it if configured
|
| 90 |
+
input_ids = self.tokenizer.apply_chat_template(
|
| 91 |
+
item,
|
| 92 |
+
return_tensors="pt",
|
| 93 |
+
add_generation_prompt=True,
|
| 94 |
+
)
|
| 95 |
+
else:
|
| 96 |
+
if not isinstance(item, str):
|
| 97 |
+
item = str(item)
|
| 98 |
+
enc = self.tokenizer(item, return_tensors="pt")
|
| 99 |
+
input_ids = enc["input_ids"]
|
| 100 |
+
|
| 101 |
+
input_ids = input_ids.to(self.model.device)
|
| 102 |
+
input_len = input_ids.shape[-1]
|
| 103 |
+
|
| 104 |
+
gen_ids = self.model.generate(
|
| 105 |
+
input_ids=input_ids,
|
| 106 |
+
max_new_tokens=max_new_tokens,
|
| 107 |
+
do_sample=do_sample,
|
| 108 |
+
temperature=temperature if do_sample else None,
|
| 109 |
+
top_p=top_p if do_sample else None,
|
| 110 |
+
top_k=top_k if do_sample and top_k > 0 else None,
|
| 111 |
+
num_beams=num_beams,
|
| 112 |
+
repetition_penalty=repetition_penalty,
|
| 113 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
| 114 |
+
eos_token_id=self.tokenizer.eos_token_id,
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Only return newly generated tokens
|
| 118 |
+
new_tokens = gen_ids[0, input_len:]
|
| 119 |
+
text = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 120 |
+
return {"generated_text": text}
|
| 121 |
+
|
| 122 |
+
# Batch support
|
| 123 |
+
if isinstance(inputs, list) and not _is_messages(inputs):
|
| 124 |
+
return [_one(x) for x in inputs]
|
| 125 |
+
else:
|
| 126 |
+
return _one(inputs)
|
merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cd3411332c19c27ac340b99a92d91e0b93f224b62fa3e0cccf7777b4e126b802
|
| 3 |
+
size 381107624
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"additional_special_tokens": [
|
| 3 |
+
"<|im_start|>",
|
| 4 |
+
"<|im_end|>"
|
| 5 |
+
],
|
| 6 |
+
"bos_token": {
|
| 7 |
+
"content": "<|im_start|>",
|
| 8 |
+
"lstrip": false,
|
| 9 |
+
"normalized": false,
|
| 10 |
+
"rstrip": false,
|
| 11 |
+
"single_word": false
|
| 12 |
+
},
|
| 13 |
+
"eos_token": {
|
| 14 |
+
"content": "<|im_end|>",
|
| 15 |
+
"lstrip": false,
|
| 16 |
+
"normalized": false,
|
| 17 |
+
"rstrip": false,
|
| 18 |
+
"single_word": false
|
| 19 |
+
},
|
| 20 |
+
"pad_token": {
|
| 21 |
+
"content": "<|im_end|>",
|
| 22 |
+
"lstrip": false,
|
| 23 |
+
"normalized": false,
|
| 24 |
+
"rstrip": false,
|
| 25 |
+
"single_word": false
|
| 26 |
+
},
|
| 27 |
+
"unk_token": {
|
| 28 |
+
"content": "<|endoftext|>",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false
|
| 33 |
+
}
|
| 34 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"added_tokens_decoder": {
|
| 4 |
+
"0": {
|
| 5 |
+
"content": "<|endoftext|>",
|
| 6 |
+
"lstrip": false,
|
| 7 |
+
"normalized": false,
|
| 8 |
+
"rstrip": false,
|
| 9 |
+
"single_word": false,
|
| 10 |
+
"special": true
|
| 11 |
+
},
|
| 12 |
+
"1": {
|
| 13 |
+
"content": "<|im_start|>",
|
| 14 |
+
"lstrip": false,
|
| 15 |
+
"normalized": false,
|
| 16 |
+
"rstrip": false,
|
| 17 |
+
"single_word": false,
|
| 18 |
+
"special": true
|
| 19 |
+
},
|
| 20 |
+
"2": {
|
| 21 |
+
"content": "<|im_end|>",
|
| 22 |
+
"lstrip": false,
|
| 23 |
+
"normalized": false,
|
| 24 |
+
"rstrip": false,
|
| 25 |
+
"single_word": false,
|
| 26 |
+
"special": true
|
| 27 |
+
},
|
| 28 |
+
"3": {
|
| 29 |
+
"content": "<repo_name>",
|
| 30 |
+
"lstrip": false,
|
| 31 |
+
"normalized": false,
|
| 32 |
+
"rstrip": false,
|
| 33 |
+
"single_word": false,
|
| 34 |
+
"special": true
|
| 35 |
+
},
|
| 36 |
+
"4": {
|
| 37 |
+
"content": "<reponame>",
|
| 38 |
+
"lstrip": false,
|
| 39 |
+
"normalized": false,
|
| 40 |
+
"rstrip": false,
|
| 41 |
+
"single_word": false,
|
| 42 |
+
"special": true
|
| 43 |
+
},
|
| 44 |
+
"5": {
|
| 45 |
+
"content": "<file_sep>",
|
| 46 |
+
"lstrip": false,
|
| 47 |
+
"normalized": false,
|
| 48 |
+
"rstrip": false,
|
| 49 |
+
"single_word": false,
|
| 50 |
+
"special": true
|
| 51 |
+
},
|
| 52 |
+
"6": {
|
| 53 |
+
"content": "<filename>",
|
| 54 |
+
"lstrip": false,
|
| 55 |
+
"normalized": false,
|
| 56 |
+
"rstrip": false,
|
| 57 |
+
"single_word": false,
|
| 58 |
+
"special": true
|
| 59 |
+
},
|
| 60 |
+
"7": {
|
| 61 |
+
"content": "<gh_stars>",
|
| 62 |
+
"lstrip": false,
|
| 63 |
+
"normalized": false,
|
| 64 |
+
"rstrip": false,
|
| 65 |
+
"single_word": false,
|
| 66 |
+
"special": true
|
| 67 |
+
},
|
| 68 |
+
"8": {
|
| 69 |
+
"content": "<issue_start>",
|
| 70 |
+
"lstrip": false,
|
| 71 |
+
"normalized": false,
|
| 72 |
+
"rstrip": false,
|
| 73 |
+
"single_word": false,
|
| 74 |
+
"special": true
|
| 75 |
+
},
|
| 76 |
+
"9": {
|
| 77 |
+
"content": "<issue_comment>",
|
| 78 |
+
"lstrip": false,
|
| 79 |
+
"normalized": false,
|
| 80 |
+
"rstrip": false,
|
| 81 |
+
"single_word": false,
|
| 82 |
+
"special": true
|
| 83 |
+
},
|
| 84 |
+
"10": {
|
| 85 |
+
"content": "<issue_closed>",
|
| 86 |
+
"lstrip": false,
|
| 87 |
+
"normalized": false,
|
| 88 |
+
"rstrip": false,
|
| 89 |
+
"single_word": false,
|
| 90 |
+
"special": true
|
| 91 |
+
},
|
| 92 |
+
"11": {
|
| 93 |
+
"content": "<jupyter_start>",
|
| 94 |
+
"lstrip": false,
|
| 95 |
+
"normalized": false,
|
| 96 |
+
"rstrip": false,
|
| 97 |
+
"single_word": false,
|
| 98 |
+
"special": true
|
| 99 |
+
},
|
| 100 |
+
"12": {
|
| 101 |
+
"content": "<jupyter_text>",
|
| 102 |
+
"lstrip": false,
|
| 103 |
+
"normalized": false,
|
| 104 |
+
"rstrip": false,
|
| 105 |
+
"single_word": false,
|
| 106 |
+
"special": true
|
| 107 |
+
},
|
| 108 |
+
"13": {
|
| 109 |
+
"content": "<jupyter_code>",
|
| 110 |
+
"lstrip": false,
|
| 111 |
+
"normalized": false,
|
| 112 |
+
"rstrip": false,
|
| 113 |
+
"single_word": false,
|
| 114 |
+
"special": true
|
| 115 |
+
},
|
| 116 |
+
"14": {
|
| 117 |
+
"content": "<jupyter_output>",
|
| 118 |
+
"lstrip": false,
|
| 119 |
+
"normalized": false,
|
| 120 |
+
"rstrip": false,
|
| 121 |
+
"single_word": false,
|
| 122 |
+
"special": true
|
| 123 |
+
},
|
| 124 |
+
"15": {
|
| 125 |
+
"content": "<jupyter_script>",
|
| 126 |
+
"lstrip": false,
|
| 127 |
+
"normalized": false,
|
| 128 |
+
"rstrip": false,
|
| 129 |
+
"single_word": false,
|
| 130 |
+
"special": true
|
| 131 |
+
},
|
| 132 |
+
"16": {
|
| 133 |
+
"content": "<empty_output>",
|
| 134 |
+
"lstrip": false,
|
| 135 |
+
"normalized": false,
|
| 136 |
+
"rstrip": false,
|
| 137 |
+
"single_word": false,
|
| 138 |
+
"special": true
|
| 139 |
+
}
|
| 140 |
+
},
|
| 141 |
+
"additional_special_tokens": [
|
| 142 |
+
"<|im_start|>",
|
| 143 |
+
"<|im_end|>"
|
| 144 |
+
],
|
| 145 |
+
"bos_token": "<|im_start|>",
|
| 146 |
+
"clean_up_tokenization_spaces": false,
|
| 147 |
+
"eos_token": "<|im_end|>",
|
| 148 |
+
"extra_special_tokens": {},
|
| 149 |
+
"model_max_length": 8192,
|
| 150 |
+
"pad_token": "<|im_end|>",
|
| 151 |
+
"tokenizer_class": "GPT2Tokenizer",
|
| 152 |
+
"unk_token": "<|endoftext|>",
|
| 153 |
+
"vocab_size": 49152
|
| 154 |
+
}
|
training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:357a1e8bcbd247f80b9437f6d4dd9e81a29edbafaa6fea075a7380b6927773f4
|
| 3 |
+
size 6353
|
vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|