Hoglet-33 commited on
Commit
abdf962
·
verified ·
1 Parent(s): 6719ea5

Update modeling_pebble.py

Browse files
Files changed (1) hide show
  1. modeling_pebble.py +75 -53
modeling_pebble.py CHANGED
@@ -2,13 +2,14 @@ import torch
2
  import torch.nn as nn
3
  import torch.nn.functional as F
4
  from transformers import PreTrainedModel
5
- from .configuration_pebble import PebbleConfig
6
 
7
  try:
8
  from mamba_ssm import Mamba2
9
  except ImportError:
10
- Mamba2 = None
11
- print("Warning: mamba-ssm not installed. Please install it to use PebbleLM.")
 
12
 
13
  class RMSNorm(nn.Module):
14
  def __init__(self, dim, eps=1e-6):
@@ -23,101 +24,122 @@ class RMSNorm(nn.Module):
23
  return self.weight * xf.to(dt)
24
 
25
  class AttentionBlock(nn.Module):
26
- def __init__(self, dim, n_heads, hidden, rope_theta=10000.0):
27
  super().__init__()
 
 
 
28
  assert dim % n_heads == 0
29
  self.nh, self.hd = n_heads, dim // n_heads
30
- self.rope_theta = rope_theta
31
  self.wqkv = nn.Linear(dim, 3 * dim, bias=False)
32
  self.wo = nn.Linear(dim, dim, bias=False)
33
  self.fc1 = nn.Linear(dim, hidden, bias=False)
34
  self.fc2 = nn.Linear(hidden, dim, bias=False)
35
- self.ln1 = RMSNorm(dim)
36
- self.ln2 = RMSNorm(dim)
 
37
 
38
  def forward(self, x):
39
  B, T, C = x.shape
40
  h = self.ln1(x)
41
- qkv = self.wqkv(h).view(B, T, 3, self.nh, self.hd).permute(2, 0, 3, 1, 4)
 
 
42
  q, k, v = qkv[0], qkv[1], qkv[2]
43
 
44
  half = self.hd // 2
45
  invf = 1.0 / (self.rope_theta ** (
46
- torch.arange(0, half, device=x.device, dtype=torch.float32) * 2.0 / self.hd))
47
- ang = torch.outer(torch.arange(T, device=x.device, dtype=torch.float32), invf)
 
 
48
  cos, sin = ang.cos()[None, None], ang.sin()[None, None]
49
 
50
  q1, q2 = q.float()[..., :half], q.float()[..., half:]
51
  k1, k2 = k.float()[..., :half], k.float()[..., half:]
52
-
53
- q = torch.cat([q1 * cos - q2 * sin, q1 * sin + q2 * cos], dim=-1).to(v.dtype)
54
- k = torch.cat([k1 * cos - k2 * sin, k1 * sin + k2 * cos], dim=-1).to(v.dtype)
 
55
 
56
  y = F.scaled_dot_product_attention(q, k, v, is_causal=True)
57
  y = y.transpose(1, 2).reshape(B, T, C)
 
58
  x = x + self.wo(y)
59
  x = x + self.fc2(F.gelu(self.fc1(self.ln2(x))))
60
  return x
61
 
62
  class MambaBlock(nn.Module):
63
- def __init__(self, dim, d_state=128, d_conv=4, expand=2, headdim=64):
64
  super().__init__()
65
- if Mamba2 is None:
66
- raise ImportError("mamba-ssm is not installed. Please install via `pip install mamba-ssm`")
67
- self.ln = RMSNorm(dim)
68
  self.mixer = Mamba2(
69
- d_model=dim,
70
- d_state=d_state,
71
- d_conv=d_conv,
72
- expand=expand,
73
- headdim=headdim,
74
- use_mem_eff_path=True,
75
  )
76
 
77
  def forward(self, x):
78
  return x + self.mixer(self.ln(x))
79
 
80
- class PebbleLM(PreTrainedModel):
81
  config_class = PebbleConfig
82
- base_model_prefix = "model"
83
- supports_gradient_checkpointing = True
84
 
85
  def __init__(self, config):
86
  super().__init__(config)
87
- self.wte = nn.Embedding(config.vocab_size, config.d_model)
 
 
 
 
88
  self.blocks = nn.ModuleList([
89
- MambaBlock(
90
- config.d_model,
91
- config.mamba_d_state,
92
- config.mamba_d_conv,
93
- config.mamba_expand,
94
- config.mamba_headdim
95
- ) if i % 4 < 3 else AttentionBlock(
96
- config.d_model,
97
- config.n_heads,
98
- config.att_hidden,
99
- config.rope_theta
100
- )
101
- for i in range(config.n_blocks)
102
  ])
103
- self.lnf = RMSNorm(config.d_model)
104
- self.lm_head = nn.Linear(config.d_model, config.vocab_size, bias=False)
105
- self.lm_head.weight = self.wte.weight # weight sharing
 
 
 
 
 
 
 
106
 
107
- def forward(self, input_ids=None, labels=None, targets=None, **kwargs):
108
  x = self.wte(input_ids)
 
109
  for blk in self.blocks:
110
  x = blk(x)
 
111
  logits = self.lm_head(self.lnf(x))
112
-
113
  loss = None
114
  if labels is not None:
115
- loss = F.cross_entropy(logits.view(-1, logits.size(-1)), labels.reshape(-1))
116
- elif targets is not None:
117
- loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.reshape(-1))
118
-
119
- return {"logits": logits, "loss": loss}
 
 
 
 
 
 
 
 
120
 
121
- # Register the model for AutoModel
122
- from transformers import AutoModelForCausalLM
123
- AutoModelForCausalLM.register(PebbleConfig, PebbleLM)
 
 
 
2
  import torch.nn as nn
3
  import torch.nn.functional as F
4
  from transformers import PreTrainedModel
5
+ from transformers.modeling_outputs import CausalLMOutputWithPast
6
 
7
  try:
8
  from mamba_ssm import Mamba2
9
  except ImportError:
10
+ raise ImportError("mamba-ssm is required. pip install mamba-ssm causal-conv1d")
11
+
12
+ from .configuration_pebble import PebbleConfig
13
 
14
  class RMSNorm(nn.Module):
15
  def __init__(self, dim, eps=1e-6):
 
24
  return self.weight * xf.to(dt)
25
 
26
  class AttentionBlock(nn.Module):
27
+ def __init__(self, config):
28
  super().__init__()
29
+ dim = config.hidden_size
30
+ n_heads = config.num_attention_heads
31
+ hidden = config.intermediate_size
32
  assert dim % n_heads == 0
33
  self.nh, self.hd = n_heads, dim // n_heads
 
34
  self.wqkv = nn.Linear(dim, 3 * dim, bias=False)
35
  self.wo = nn.Linear(dim, dim, bias=False)
36
  self.fc1 = nn.Linear(dim, hidden, bias=False)
37
  self.fc2 = nn.Linear(hidden, dim, bias=False)
38
+ self.ln1 = RMSNorm(dim, eps=config.rms_norm_eps)
39
+ self.ln2 = RMSNorm(dim, eps=config.rms_norm_eps)
40
+ self.rope_theta = config.attention.get("rope_theta", 10000.0)
41
 
42
  def forward(self, x):
43
  B, T, C = x.shape
44
  h = self.ln1(x)
45
+
46
+ qkv = self.wqkv(h).view(B, T, 3, self.nh, self.hd) \
47
+ .permute(2, 0, 3, 1, 4)
48
  q, k, v = qkv[0], qkv[1], qkv[2]
49
 
50
  half = self.hd // 2
51
  invf = 1.0 / (self.rope_theta ** (
52
+ torch.arange(0, half, device=x.device, dtype=torch.float32)
53
+ * 2.0 / self.hd))
54
+ ang = torch.outer(
55
+ torch.arange(T, device=x.device, dtype=torch.float32), invf)
56
  cos, sin = ang.cos()[None, None], ang.sin()[None, None]
57
 
58
  q1, q2 = q.float()[..., :half], q.float()[..., half:]
59
  k1, k2 = k.float()[..., :half], k.float()[..., half:]
60
+ q = torch.cat([q1 * cos - q2 * sin,
61
+ q1 * sin + q2 * cos], dim=-1).to(v.dtype)
62
+ k = torch.cat([k1 * cos - k2 * sin,
63
+ k1 * sin + k2 * cos], dim=-1).to(v.dtype)
64
 
65
  y = F.scaled_dot_product_attention(q, k, v, is_causal=True)
66
  y = y.transpose(1, 2).reshape(B, T, C)
67
+
68
  x = x + self.wo(y)
69
  x = x + self.fc2(F.gelu(self.fc1(self.ln2(x))))
70
  return x
71
 
72
  class MambaBlock(nn.Module):
73
+ def __init__(self, config):
74
  super().__init__()
75
+ self.ln = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
76
+ mamba_cfg = config.mamba2
 
77
  self.mixer = Mamba2(
78
+ d_model=config.hidden_size,
79
+ d_state=mamba_cfg.get("d_state", 128),
80
+ d_conv=mamba_cfg.get("d_conv", 4),
81
+ expand=mamba_cfg.get("expand", 2),
82
+ headdim=mamba_cfg.get("headdim", 64),
83
+ use_mem_eff_path=mamba_cfg.get("use_mem_eff_path", True),
84
  )
85
 
86
  def forward(self, x):
87
  return x + self.mixer(self.ln(x))
88
 
89
+ class PebbleForCausalLM(PreTrainedModel):
90
  config_class = PebbleConfig
91
+ supports_gradient_checkpointing = False
92
+ _no_split_modules = ["MambaBlock", "AttentionBlock"]
93
 
94
  def __init__(self, config):
95
  super().__init__(config)
96
+ self.config = config
97
+
98
+ self.wte = nn.Embedding(config.vocab_size, config.hidden_size)
99
+
100
+ # 3:1 Mamba:Attention ratio layout
101
  self.blocks = nn.ModuleList([
102
+ MambaBlock(config) if i % 4 < 3
103
+ else AttentionBlock(config)
104
+ for i in range(config.num_hidden_layers)
 
 
 
 
 
 
 
 
 
 
105
  ])
106
+
107
+ self.lnf = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
108
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
109
+
110
+ # Tie weights
111
+ self.tie_weights()
112
+
113
+ def tie_weights(self):
114
+ if self.config.tie_word_embeddings:
115
+ self.lm_head.weight = self.wte.weight
116
 
117
+ def forward(self, input_ids=None, attention_mask=None, labels=None, past_key_values=None, **kwargs):
118
  x = self.wte(input_ids)
119
+
120
  for blk in self.blocks:
121
  x = blk(x)
122
+
123
  logits = self.lm_head(self.lnf(x))
124
+
125
  loss = None
126
  if labels is not None:
127
+ # Shift so that tokens < n predict n+1
128
+ shift_logits = logits[..., :-1, :].contiguous()
129
+ shift_labels = labels[..., 1:].contiguous()
130
+ loss = F.cross_entropy(
131
+ shift_logits.view(-1, shift_logits.size(-1)),
132
+ shift_labels.view(-1)
133
+ )
134
+
135
+ return CausalLMOutputWithPast(
136
+ loss=loss,
137
+ logits=logits,
138
+ past_key_values=past_key_values,
139
+ )
140
 
141
+ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, **kwargs):
142
+ return {
143
+ "input_ids": input_ids,
144
+ "past_key_values": past_key_values,
145
+ }