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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# FRANKENSTALLM-H 3B Hybrid Model โ ์ ๊ฒ ๊ฒฐ๊ณผ ๋ฐ ์์ ์คํ ๊ฐ์ด๋
|
| 2 |
+
|
| 3 |
+
> **์์ฑ์ผ**: 2026-03-05
|
| 4 |
+
> **๋ชฉ์ **: Phase 2 ๊ฒ์ฆ ์ , ๋ฐ๊ฒฌ๋ ์ด์ 6๊ฑด์ ์์ ํ๊ณ ๋ฐ๋ก ์คํ ๊ฐ๋ฅํ ์ํ๋ก ๋ง๋ ๋ค.
|
| 5 |
+
> **๋ค์ ์ธ์
์์ ์ด ๋ฌธ์๋ฅผ ์ฐธ์กฐํ์ฌ ๋ฐ๋ก ์คํํ ๊ฒ.**
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## ์ด์ ์์ฝ (6๊ฑด)
|
| 10 |
+
|
| 11 |
+
| # | ์ฌ๊ฐ๋ | ์ด์ | ํ์ผ | ์ํฅ |
|
| 12 |
+
|---|--------|------|------|------|
|
| 13 |
+
| 1 | **CRITICAL** | Mamba ๋ธ๋ก์ FFN(channel mixer) ์์ | `model/mamba_block.py` | 37/40 ๋ ์ด์ด capacity ๋ถ์กฑ |
|
| 14 |
+
| 2 | **HIGH** | `n_groups=1` (Nemotron ํ์ค์ 8) | `configs/hybrid_3b.yaml` | B/C projection ํํ๋ ฅ ์ ํ |
|
| 15 |
+
| 3 | **HIGH** | Hybrid ์ํคํ
์ฒ startup ๋ก๊ทธ ์์ | `train/pretrain.py` | ๋๋ฒ๊น
ยท๋ชจ๋ํฐ๋ง ๊ณค๋ |
|
| 16 |
+
| 4 | **MEDIUM** | ์ฒดํฌํฌ์ธํธ resume ์ ์ํคํ
์ฒ ๊ฒ์ฆ ์์ | `train/utils.py` | ์๋ชป๋ ๊ฐ์ค์น ๋ก๋ ๊ฐ๋ฅ |
|
| 17 |
+
| 5 | **MEDIUM** | selective_scan์ NaN/Inf ๊ฐ์ง ์์ | `model/mamba_block.py` | ์์น ๋ถ์์ ์ง๋จ ๋ถ๊ฐ |
|
| 18 |
+
| 6 | **LOW** | selective_scan ์
๋ ฅ shape ๊ฒ์ฆ ์์ | `model/mamba_block.py` | ๋ชจํธํ ์๋ฌ ๋ฉ์์ง |
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## ๊ตฌํ ์์ ๋ฐ ์์กด์ฑ
|
| 23 |
+
|
| 24 |
+
```
|
| 25 |
+
Step 1 (FFN ์ถ๊ฐ) โ ๊ฐ์ฅ ๋จผ์ , ์ํคํ
์ฒ ๋ณ๊ฒฝ
|
| 26 |
+
โโโ 1a. model/config.py: mamba_d_ffn ํ๋ ์ถ๊ฐ
|
| 27 |
+
โโโ 1b. model/mamba_block.py: FFN sublayer ์ถ๊ฐ
|
| 28 |
+
โโโ 1c. model/transformer.py: ์์ฑ์ ์ธ์ ์ ๋ฌ + _init_weights ์์
|
| 29 |
+
โโโ 1d. configs/hybrid_3b.yaml: mamba_d_ffn=4608 ์ถ๊ฐ
|
| 30 |
+
|
| 31 |
+
Step 2 (n_groups) โ Step 1๊ณผ ๋
๋ฆฝ, ๋ณ๋ ฌ ๊ฐ๋ฅ
|
| 32 |
+
โโโ configs/hybrid_3b.yaml: n_groups=8
|
| 33 |
+
|
| 34 |
+
Step 3 (๋ก๊ทธ) โ Step 1 ์๋ฃ ํ (ํ๋ผ๋ฏธํฐ ์ ์ ํํด์ผ)
|
| 35 |
+
โโโ train/pretrain.py: startup ๋ฐฐ๋์ hybrid ์ ๋ณด ์ถ๊ฐ
|
| 36 |
+
|
| 37 |
+
Step 4 (์ฒดํฌํฌ์ธํธ ๊ฒ์ฆ) โ ๋
๋ฆฝ
|
| 38 |
+
โโโ train/utils.py: load_checkpoint์ config ๋น๊ต ๋ก์ง
|
| 39 |
+
|
| 40 |
+
Step 5-6 (NaN ๊ฐ์ง + shape ๊ฒ์ฆ) โ ๋
๋ฆฝ
|
| 41 |
+
โโโ model/mamba_block.py: selective_scan ํจ์
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
**๋ณ๋ ฌ ๊ฐ๋ฅ**: Step 1 + Step 2๋ YAML๋ง ๊ฒน์นจ (๋ง์ง๋ง์ ํฉ์น๋ฉด ๋จ).
|
| 45 |
+
Step 4, Step 5-6๋ ๋
๋ฆฝ์ ์ผ๋ก ๋ณ๋ ฌ ์คํ ๊ฐ๋ฅ.
|
| 46 |
+
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
## Step 1: Mamba2Block์ FFN ์ถ๊ฐ (CRITICAL)
|
| 50 |
+
|
| 51 |
+
### ๋ฐฐ๊ฒฝ
|
| 52 |
+
|
| 53 |
+
- Mamba2Block์ SSM(sequence mixer)๋ง ์๊ณ FFN(channel mixer)์ด ์์
|
| 54 |
+
- Nemotron-H์์๋ ๋ชจ๋ Mamba ๋ ์ด์ด ๋ค์ MLP๊ฐ ๋ฐ๋ผ์ด
|
| 55 |
+
- ํ์ฌ 37/40 ๋ ์ด์ด์ FFN์ด ์์ด feature mixing์ด ๋ถ๊ฐ๋ฅ
|
| 56 |
+
- **ํ์ **: `mamba_d_ffn = 4608` (d_model ร 1.5), ์ด ํ๋ผ๋ฏธํฐ ~4.5B, VRAM ~80GB/GPU
|
| 57 |
+
|
| 58 |
+
### 1a. `model/config.py` ์์
|
| 59 |
+
|
| 60 |
+
**์์น**: LMConfig dataclass ๋ด๋ถ (line 61 ์ดํ)
|
| 61 |
+
|
| 62 |
+
**์ถ๊ฐํ ํ๋** (๊ธฐ์กด `mamba_chunk_size` ๋ค์):
|
| 63 |
+
```python
|
| 64 |
+
mamba_d_ffn: Optional[int] = None # FFN dim for Mamba blocks (None โ d_ffn)
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
**`__post_init__` ์ถ๊ฐ** (line 86, hybrid validation ๋ธ๋ก ๋ค์):
|
| 68 |
+
```python
|
| 69 |
+
# Mamba FFN dimension: default to d_ffn if not specified
|
| 70 |
+
if self.mamba_d_ffn is None:
|
| 71 |
+
self.mamba_d_ffn = self.d_ffn
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
**`to_dict()` ์ถ๊ฐ** (๊ธฐ์กด mamba_chunk_size ๋ค์):
|
| 75 |
+
```python
|
| 76 |
+
"mamba_d_ffn": self.mamba_d_ffn,
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
### 1b. `model/mamba_block.py` ์์
|
| 80 |
+
|
| 81 |
+
**Import ๋ณ๊ฒฝ** (line 19):
|
| 82 |
+
```python
|
| 83 |
+
# ๋ณ๊ฒฝ ์ :
|
| 84 |
+
from .layers import RMSNorm
|
| 85 |
+
|
| 86 |
+
# ๋ณ๊ฒฝ ํ:
|
| 87 |
+
from .layers import RMSNorm, SwiGLU
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
**`Mamba2Block.__init__` ์๊ทธ๋์ฒ ๋ณ๊ฒฝ** (line 128-137):
|
| 91 |
+
```python
|
| 92 |
+
# ๋ณ๊ฒฝ ์ :
|
| 93 |
+
def __init__(
|
| 94 |
+
self,
|
| 95 |
+
d_model: int,
|
| 96 |
+
d_state: int = 128,
|
| 97 |
+
head_dim: int = 64,
|
| 98 |
+
expand: int = 2,
|
| 99 |
+
conv_kernel: int = 4,
|
| 100 |
+
n_groups: int = 1,
|
| 101 |
+
chunk_size: int = 256,
|
| 102 |
+
) -> None:
|
| 103 |
+
|
| 104 |
+
# ๋ณ๊ฒฝ ํ:
|
| 105 |
+
def __init__(
|
| 106 |
+
self,
|
| 107 |
+
d_model: int,
|
| 108 |
+
d_state: int = 128,
|
| 109 |
+
head_dim: int = 64,
|
| 110 |
+
expand: int = 2,
|
| 111 |
+
conv_kernel: int = 4,
|
| 112 |
+
n_groups: int = 1,
|
| 113 |
+
chunk_size: int = 256,
|
| 114 |
+
d_ffn: int = 0,
|
| 115 |
+
bias: bool = False,
|
| 116 |
+
) -> None:
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
**FFN ์๋ธ๋ ์ด์ด ์ถ๊ฐ** (line 192, `self.out_proj` ๋ค์):
|
| 120 |
+
```python
|
| 121 |
+
# --- FFN sublayer (channel mixer) ---
|
| 122 |
+
if d_ffn > 0:
|
| 123 |
+
self.ffn_norm = RMSNorm(d_model)
|
| 124 |
+
self.ffn = SwiGLU(d_model, d_ffn, bias=bias)
|
| 125 |
+
else:
|
| 126 |
+
self.ffn_norm = None
|
| 127 |
+
self.ffn = None
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
**`forward()` ์์ ** (line 280):
|
| 131 |
+
```python
|
| 132 |
+
# ๋ณ๊ฒฝ ์ :
|
| 133 |
+
return residual + self.out_proj(y)
|
| 134 |
+
|
| 135 |
+
# ๋ณ๊ฒฝ ํ:
|
| 136 |
+
x = residual + self.out_proj(y)
|
| 137 |
+
# FFN sublayer (channel mixer)
|
| 138 |
+
if self.ffn is not None:
|
| 139 |
+
x = x + self.ffn(self.ffn_norm(x))
|
| 140 |
+
return x
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
### 1c. `model/transformer.py` ์์
|
| 144 |
+
|
| 145 |
+
**Mamba2Block ์์ฑ์ ํธ์ถ ๋ณ๊ฒฝ** (line 124-132):
|
| 146 |
+
```python
|
| 147 |
+
# ๋ณ๊ฒฝ ์ :
|
| 148 |
+
layers.append(Mamba2Block(
|
| 149 |
+
d_model=config.d_model,
|
| 150 |
+
d_state=config.mamba_d_state,
|
| 151 |
+
head_dim=config.mamba_head_dim,
|
| 152 |
+
expand=config.mamba_expand,
|
| 153 |
+
conv_kernel=config.mamba_conv_kernel,
|
| 154 |
+
n_groups=config.mamba_n_groups,
|
| 155 |
+
chunk_size=config.mamba_chunk_size,
|
| 156 |
+
))
|
| 157 |
+
|
| 158 |
+
# ๋ณ๊ฒฝ ํ:
|
| 159 |
+
layers.append(Mamba2Block(
|
| 160 |
+
d_model=config.d_model,
|
| 161 |
+
d_state=config.mamba_d_state,
|
| 162 |
+
head_dim=config.mamba_head_dim,
|
| 163 |
+
expand=config.mamba_expand,
|
| 164 |
+
conv_kernel=config.mamba_conv_kernel,
|
| 165 |
+
n_groups=config.mamba_n_groups,
|
| 166 |
+
chunk_size=config.mamba_chunk_size,
|
| 167 |
+
d_ffn=config.mamba_d_ffn,
|
| 168 |
+
bias=config.bias,
|
| 169 |
+
))
|
| 170 |
+
```
|
| 171 |
+
|
| 172 |
+
**`_init_weights` ์์ ** (line 180-182):
|
| 173 |
+
```python
|
| 174 |
+
# ๋ณ๊ฒฝ ์ :
|
| 175 |
+
# Mamba2Block handles its own parameter init (A_log, D, dt_bias, etc.)
|
| 176 |
+
if isinstance(module, Mamba2Block):
|
| 177 |
+
return
|
| 178 |
+
|
| 179 |
+
# ๋ณ๊ฒฝ ํ (์ด 3์ค์ ์ญ์ ):
|
| 180 |
+
# ์ญ์ ์ด์ : FFN ์ถ๊ฐ ํ ๋ด๋ถ SwiGLU์ nn.Linear๊ฐ init ํ์.
|
| 181 |
+
# A_log, D, dt_bias๋ nn.Parameter์ด๋ฏ๋ก isinstance(nn.Linear) ์ฒดํฌ์ ๊ฑธ๋ฆฌ์ง ์์
|
| 182 |
+
# ์๋์ผ๋ก ์คํต๋จ (Mamba2Block.__init__์์ ์ง์ ์ด๊ธฐํ๋จ).
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
### 1d. `configs/hybrid_3b.yaml` ์์
|
| 186 |
+
|
| 187 |
+
```yaml
|
| 188 |
+
# mamba_chunk_size: 256 ๋ค์ ์ถ๊ฐ:
|
| 189 |
+
mamba_d_ffn: 4608
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
### Step 1 ๊ฒ์ฆ
|
| 193 |
+
|
| 194 |
+
```bash
|
| 195 |
+
cd /PROJECT/0325120031_A/ghong/taketimes/llm-bang
|
| 196 |
+
CUDA_VISIBLE_DEVICES=0 python -c "
|
| 197 |
+
import torch, sys
|
| 198 |
+
sys.path.insert(0, '.')
|
| 199 |
+
from model import LLM, LMConfig
|
| 200 |
+
|
| 201 |
+
config = LMConfig.from_yaml('configs/hybrid_3b.yaml')
|
| 202 |
+
print(f'mamba_d_ffn = {config.mamba_d_ffn}')
|
| 203 |
+
|
| 204 |
+
model = LLM(config)
|
| 205 |
+
total = sum(p.numel() for p in model.parameters())
|
| 206 |
+
print(f'Total params: {total:,} ({total/1e9:.2f}B)')
|
| 207 |
+
|
| 208 |
+
# Forward test
|
| 209 |
+
x = torch.randint(0, 64000, (1, 128))
|
| 210 |
+
logits, loss = model(x, targets=x)
|
| 211 |
+
print(f'Forward OK: logits shape={logits.shape}, loss={loss.item():.4f}')
|
| 212 |
+
|
| 213 |
+
# Backward test
|
| 214 |
+
loss.backward()
|
| 215 |
+
grads_ok = all(p.grad is not None for p in model.parameters() if p.requires_grad)
|
| 216 |
+
print(f'Backward OK: all grads exist = {grads_ok}')
|
| 217 |
+
"
|
| 218 |
+
# ์์ ์ถ๋ ฅ: Total params ~4.5B, Forward/Backward OK
|
| 219 |
+
```
|
| 220 |
+
|
| 221 |
+
---
|
| 222 |
+
|
| 223 |
+
## Step 2: n_groups ์์
|
| 224 |
+
|
| 225 |
+
### `configs/hybrid_3b.yaml`
|
| 226 |
+
|
| 227 |
+
```yaml
|
| 228 |
+
# ๋ณ๊ฒฝ ์ :
|
| 229 |
+
mamba_n_groups: 1
|
| 230 |
+
|
| 231 |
+
# ๋ณ๊ฒฝ ํ:
|
| 232 |
+
mamba_n_groups: 8
|
| 233 |
+
```
|
| 234 |
+
|
| 235 |
+
### ๊ฒ์ฆ
|
| 236 |
+
|
| 237 |
+
n_heads(= d_inner / head_dim = 6144 / 64 = 96) % 8 == 0 โ
|
| 238 |
+
Step 1 ๊ฒ์ฆ ์คํฌ๋ฆฝํธ์์ ํจ๊ป ํ์ธ๋จ (assertion์ด `__init__`์ ์์).
|
| 239 |
+
|
| 240 |
+
---
|
| 241 |
+
|
| 242 |
+
## Step 3: ํ์ด๋ธ๋ฆฌ๋ ์ํคํ
์ฒ startup ๋ก๊ทธ ์ถ๊ฐ
|
| 243 |
+
|
| 244 |
+
### `train/pretrain.py` ์์
|
| 245 |
+
|
| 246 |
+
**์์น**: line 296-297 (๋ชจ๋ธ ํ๋ผ๋ฏธํฐ ์ถ๋ ฅ ๋ถ๋ถ) ๋ค์ ์ถ๊ฐ
|
| 247 |
+
|
| 248 |
+
```python
|
| 249 |
+
if is_main_process():
|
| 250 |
+
total_params = sum(p.numel() for p in model.parameters())
|
| 251 |
+
print(f"Model parameters: {total_params:,}")
|
| 252 |
+
print(f"LMConfig: {lm_config}")
|
| 253 |
+
|
| 254 |
+
# --- ์ฌ๊ธฐ๋ถํฐ ์ถ๊ฐ ---
|
| 255 |
+
if lm_config.use_hybrid:
|
| 256 |
+
pattern = lm_config.hybrid_pattern.split()
|
| 257 |
+
m_count = sum(1 for p in pattern if p == 'M')
|
| 258 |
+
a_count = sum(1 for p in pattern if p == 'A')
|
| 259 |
+
mamba_params = sum(
|
| 260 |
+
p.numel() for n, p in model.named_parameters()
|
| 261 |
+
if 'layers.' in n and pattern[int(n.split('.')[1])] == 'M'
|
| 262 |
+
)
|
| 263 |
+
attn_params = sum(
|
| 264 |
+
p.numel() for n, p in model.named_parameters()
|
| 265 |
+
if 'layers.' in n and pattern[int(n.split('.')[1])] == 'A'
|
| 266 |
+
)
|
| 267 |
+
other_params = total_params - mamba_params - attn_params
|
| 268 |
+
print(
|
| 269 |
+
f" arch : Hybrid Mamba-Transformer\n"
|
| 270 |
+
f" layers : {m_count} Mamba + {a_count} Attention = {len(pattern)} total\n"
|
| 271 |
+
f" params : Mamba {mamba_params/1e6:.0f}M + "
|
| 272 |
+
f"Attn {attn_params/1e6:.0f}M + Other {other_params/1e6:.0f}M\n"
|
| 273 |
+
f" mamba cfg: d_state={lm_config.mamba_d_state}, "
|
| 274 |
+
f"head_dim={lm_config.mamba_head_dim}, "
|
| 275 |
+
f"expand={lm_config.mamba_expand}, "
|
| 276 |
+
f"n_groups={lm_config.mamba_n_groups}, "
|
| 277 |
+
f"d_ffn={lm_config.mamba_d_ffn}"
|
| 278 |
+
)
|
| 279 |
+
# --- ์ถ๊ฐ ๋ ---
|
| 280 |
+
```
|
| 281 |
+
|
| 282 |
+
### ๊ฒ์ฆ
|
| 283 |
+
|
| 284 |
+
Step 1 ๊ฒ์ฆ ์คํ ์ ๋ก๊ทธ์ hybrid ์ ๋ณด๊ฐ ์ถ๋ ฅ๋๋์ง ํ์ธ.
|
| 285 |
+
|
| 286 |
+
---
|
| 287 |
+
|
| 288 |
+
## Step 4: ์ฒดํฌํฌ์ธํธ resume ์ํคํ
์ฒ ๊ฒ์ฆ
|
| 289 |
+
|
| 290 |
+
### `train/utils.py` โ `load_checkpoint()` ์์
|
| 291 |
+
|
| 292 |
+
**์์น**: line 179 (`raw_model.load_state_dict(...)`) ์ง์ ์ ์ถ๊ฐ
|
| 293 |
+
|
| 294 |
+
```python
|
| 295 |
+
# --- Architecture validation ---
|
| 296 |
+
config_path = ckpt_dir / "config.yaml"
|
| 297 |
+
if config_path.exists() and hasattr(raw_model, "config"):
|
| 298 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 299 |
+
saved_cfg = yaml.safe_load(f)
|
| 300 |
+
current_cfg = raw_model.config.to_dict()
|
| 301 |
+
critical_keys = [
|
| 302 |
+
"d_model", "n_layers", "n_heads", "n_kv_heads", "vocab_size",
|
| 303 |
+
"use_hybrid", "hybrid_pattern",
|
| 304 |
+
]
|
| 305 |
+
mismatches = []
|
| 306 |
+
for key in critical_keys:
|
| 307 |
+
saved_val = saved_cfg.get(key)
|
| 308 |
+
current_val = current_cfg.get(key)
|
| 309 |
+
if saved_val is not None and saved_val != current_val:
|
| 310 |
+
mismatches.append(
|
| 311 |
+
f" {key}: checkpoint={saved_val} vs current={current_val}"
|
| 312 |
+
)
|
| 313 |
+
if mismatches:
|
| 314 |
+
raise ValueError(
|
| 315 |
+
f"Checkpoint architecture mismatch!\n"
|
| 316 |
+
f"Checkpoint dir: {ckpt_dir}\n"
|
| 317 |
+
+ "\n".join(mismatches)
|
| 318 |
+
+ "\nUse --config matching the checkpoint, or start fresh."
|
| 319 |
+
)
|
| 320 |
+
# --- End architecture validation ---
|
| 321 |
+
```
|
| 322 |
+
|
| 323 |
+
**์ฐธ๊ณ **: `yaml`์ ์ด๋ฏธ `train/utils.py` line 23์์ import ๋์ด ์์.
|
| 324 |
+
|
| 325 |
+
### ๊ฒ์ฆ
|
| 326 |
+
|
| 327 |
+
```bash
|
| 328 |
+
# ์๋์ ์ผ๋ก ๋ค๋ฅธ config๋ก resume ์๋
|
| 329 |
+
CUDA_VISIBLE_DEVICES=0 python train/pretrain.py \
|
| 330 |
+
--config configs/small.yaml \
|
| 331 |
+
--train_data data/3b_train.bin \
|
| 332 |
+
--resume checkpoints/hybrid_3b_run1/checkpoint-0001000
|
| 333 |
+
# ์์: ValueError "Checkpoint architecture mismatch!" ์ถ๋ ฅ
|
| 334 |
+
```
|
| 335 |
+
|
| 336 |
+
---
|
| 337 |
+
|
| 338 |
+
## Step 5: selective_scan NaN/Inf ๊ฐ์ง
|
| 339 |
+
|
| 340 |
+
### `model/mamba_block.py` โ `selective_scan()` ์์
|
| 341 |
+
|
| 342 |
+
**์์น**: line 94 (`y[:, t, :, :] = y_t.to(x.dtype)`) ๋ค์ ์ถ๊ฐ
|
| 343 |
+
|
| 344 |
+
```python
|
| 345 |
+
# Periodic NaN/Inf check (every 512 steps, < 1% overhead)
|
| 346 |
+
if t % 512 == 511:
|
| 347 |
+
if not torch.isfinite(h).all():
|
| 348 |
+
raise RuntimeError(
|
| 349 |
+
f"NaN/Inf in Mamba SSM state at timestep {t}/{seq_len}. "
|
| 350 |
+
f"h stats: min={h.min().item():.4e}, max={h.max().item():.4e}, "
|
| 351 |
+
f"A_log range=[{A_log.min().item():.4f}, {A_log.max().item():.4f}]"
|
| 352 |
+
)
|
| 353 |
+
```
|
| 354 |
+
|
| 355 |
+
### ๊ฒ์ฆ
|
| 356 |
+
|
| 357 |
+
```bash
|
| 358 |
+
CUDA_VISIBLE_DEVICES=0 python -c "
|
| 359 |
+
import torch, sys
|
| 360 |
+
sys.path.insert(0, '.')
|
| 361 |
+
from model.mamba_block import Mamba2Block
|
| 362 |
+
|
| 363 |
+
block = Mamba2Block(d_model=256, d_state=64, head_dim=32, d_ffn=384)
|
| 364 |
+
x = torch.randn(1, 1024, 256)
|
| 365 |
+
|
| 366 |
+
# ์ ์ ์ผ์ด์ค
|
| 367 |
+
y = block(x)
|
| 368 |
+
print(f'Normal: output shape={y.shape}, finite={torch.isfinite(y).all()}')
|
| 369 |
+
|
| 370 |
+
# NaN ์ฃผ์
ํ
์คํธ
|
| 371 |
+
block.A_log.data.fill_(100.0) # ๋งค์ฐ ํฐ ๊ฐ โ exp(100) overflow
|
| 372 |
+
try:
|
| 373 |
+
y = block(x)
|
| 374 |
+
print('WARNING: NaN not detected!')
|
| 375 |
+
except RuntimeError as e:
|
| 376 |
+
print(f'NaN correctly detected: {e}')
|
| 377 |
+
"
|
| 378 |
+
```
|
| 379 |
+
|
| 380 |
+
---
|
| 381 |
+
|
| 382 |
+
## Step 6: selective_scan ์
๋ ฅ shape ๊ฒ์ฆ
|
| 383 |
+
|
| 384 |
+
### `model/mamba_block.py` โ `selective_scan()` ์์
|
| 385 |
+
|
| 386 |
+
**์์น**: line 49 (`batch, seq_len, n_heads, head_dim = x.shape`) ์ง์ ์ ์ถ๊ฐ
|
| 387 |
+
|
| 388 |
+
```python
|
| 389 |
+
# Input shape validation
|
| 390 |
+
assert x.ndim == 4, f"x expected 4D (B,L,n_heads,head_dim), got {x.shape}"
|
| 391 |
+
assert dt.ndim == 3, f"dt expected 3D (B,L,n_heads), got {dt.shape}"
|
| 392 |
+
assert B.ndim == 4, f"B expected 4D (B,L,n_groups,d_state), got {B.shape}"
|
| 393 |
+
assert C.ndim == 4, f"C expected 4D (B,L,n_groups,d_state), got {C.shape}"
|
| 394 |
+
```
|
| 395 |
+
|
| 396 |
+
---
|
| 397 |
+
|
| 398 |
+
## ์ต์ข
๊ฒ์ฆ ์ ์ฐจ (๋ชจ๋ Step ์๋ฃ ํ)
|
| 399 |
+
|
| 400 |
+
### 1. ๋ชจ๋ธ ์์ฑ + Forward/Backward (๋จ์ผ GPU)
|
| 401 |
+
|
| 402 |
+
```bash
|
| 403 |
+
cd /PROJECT/0325120031_A/ghong/taketimes/llm-bang
|
| 404 |
+
CUDA_VISIBLE_DEVICES=0 python -c "
|
| 405 |
+
import torch, sys
|
| 406 |
+
sys.path.insert(0, '.')
|
| 407 |
+
from model import LLM, LMConfig
|
| 408 |
+
|
| 409 |
+
config = LMConfig.from_yaml('configs/hybrid_3b.yaml')
|
| 410 |
+
model = LLM(config).cuda()
|
| 411 |
+
|
| 412 |
+
total = sum(p.numel() for p in model.parameters())
|
| 413 |
+
print(f'Total params: {total:,} ({total/1e9:.2f}B)')
|
| 414 |
+
assert 4.0e9 < total < 5.0e9, f'Expected ~4.5B params, got {total/1e9:.2f}B'
|
| 415 |
+
|
| 416 |
+
# Forward
|
| 417 |
+
x = torch.randint(0, 64000, (2, 512)).cuda()
|
| 418 |
+
logits, loss = model(x, targets=x)
|
| 419 |
+
print(f'Forward: logits={logits.shape}, loss={loss.item():.4f}')
|
| 420 |
+
|
| 421 |
+
# Backward
|
| 422 |
+
loss.backward()
|
| 423 |
+
no_grad = [n for n, p in model.named_parameters() if p.requires_grad and p.grad is None]
|
| 424 |
+
assert len(no_grad) == 0, f'Missing gradients: {no_grad}'
|
| 425 |
+
print(f'Backward: all {sum(1 for p in model.parameters() if p.requires_grad)} params have grad')
|
| 426 |
+
|
| 427 |
+
# VRAM
|
| 428 |
+
print(f'VRAM: {torch.cuda.memory_allocated()/1e9:.1f}GB allocated')
|
| 429 |
+
"
|
| 430 |
+
```
|
| 431 |
+
|
| 432 |
+
### 2. DDP 8-GPU ํ
์คํธ (10 steps)
|
| 433 |
+
|
| 434 |
+
```bash
|
| 435 |
+
cd /PROJECT/0325120031_A/ghong/taketimes/llm-bang
|
| 436 |
+
torchrun --nproc_per_node=8 --master_port=29501 train/pretrain.py \
|
| 437 |
+
--config configs/hybrid_3b.yaml \
|
| 438 |
+
--train_data data/3b_train.bin \
|
| 439 |
+
--batch_size 2 \
|
| 440 |
+
--lr 1e-4 \
|
| 441 |
+
--warmup_steps 5 \
|
| 442 |
+
--grad_accum 1 \
|
| 443 |
+
--max_steps 10 \
|
| 444 |
+
--checkpoint_dir /tmp/hybrid_test_ckpt \
|
| 445 |
+
--use_fp8
|
| 446 |
+
# ์์: 10 steps ์๋ฃ, ์ฒดํฌํฌ์ธํธ ์ ์ฅ, startup ๋ฐฐ๋์ hybrid ์ ๋ณด ์ถ๋ ฅ
|
| 447 |
+
```
|
| 448 |
+
|
| 449 |
+
### 3. ์ฒดํฌํฌ์ธํธ Resume ํ
์คํธ
|
| 450 |
+
|
| 451 |
+
```bash
|
| 452 |
+
# Step 2 ์ฒดํฌํฌ์ธํธ์์ resume
|
| 453 |
+
torchrun --nproc_per_node=8 --master_port=29501 train/pretrain.py \
|
| 454 |
+
--config configs/hybrid_3b.yaml \
|
| 455 |
+
--train_data data/3b_train.bin \
|
| 456 |
+
--batch_size 2 \
|
| 457 |
+
--lr 1e-4 \
|
| 458 |
+
--warmup_steps 5 \
|
| 459 |
+
--grad_accum 1 \
|
| 460 |
+
--max_steps 20 \
|
| 461 |
+
--checkpoint_dir /tmp/hybrid_test_ckpt \
|
| 462 |
+
--resume /tmp/hybrid_test_ckpt/checkpoint-0000010 \
|
| 463 |
+
--use_fp8
|
| 464 |
+
# ์์: step 10์์ ์ด์ด์ step 20๊น์ง ํ์ต
|
| 465 |
+
```
|
| 466 |
+
|
| 467 |
+
---
|
| 468 |
+
|
| 469 |
+
## ์์ ํ์ง ์๋ ๊ฒ๋ค (์๋์ ์ ์ธ)
|
| 470 |
+
|
| 471 |
+
- **sequential scan ์ฑ๋ฅ**: Python for-loop๋ ๋๋ฆฌ์ง๋ง ๊ตฌ์กฐ ๋ณ๊ฒฝ์ด ํผ. ๋ณ๋ ํ์คํฌ๋ก chunked SSD ๊ตฌํ
|
| 472 |
+
- **FP8 + Mamba ํผํฉ**: ํ์ฌ ์ค๊ณ(Mamba=bf16, Attention=FP8)๊ฐ ์ฌ๋ฐ๋ฆ. te.fp8_autocast๋ te ๋ชจ๋๋ง ์ํฅ
|
| 473 |
+
- **DDP ์ค์ **: find_unused_parameters=False, gradient_as_bucket_view=True ๋ชจ๋ ์ ์
|
| 474 |
+
- **pure Transformer ๋ชจ๋**: use_hybrid=False๋ฉด ๊ธฐ์กด ๋์ ์ ์ง (ํ์ ํธํ)
|
| 475 |
+
|
| 476 |
+
---
|
| 477 |
+
|
| 478 |
+
## ์์ ๋์ ํ์ผ ์์ฝ
|
| 479 |
+
|
| 480 |
+
| ํ์ผ | Step | ๋ณ๊ฒฝ ๋ด์ฉ |
|
| 481 |
+
|------|------|----------|
|
| 482 |
+
| `model/config.py` | 1a | `mamba_d_ffn` ํ๋ + `__post_init__` + `to_dict()` |
|
| 483 |
+
| `model/mamba_block.py` | 1b, 5, 6 | SwiGLU import, FFN sublayer, NaN ๊ฐ์ง, shape ๊ฒ์ฆ |
|
| 484 |
+
| `model/transformer.py` | 1c | Mamba2Block ์์ฑ์์ d_ffn/bias ์ ๋ฌ, `_init_weights` ์์ |
|
| 485 |
+
| `configs/hybrid_3b.yaml` | 1d, 2 | `mamba_d_ffn: 4608`, `mamba_n_groups: 8` |
|
| 486 |
+
| `train/pretrain.py` | 3 | Hybrid startup ๋ก๊ทธ |
|
| 487 |
+
| `train/utils.py` | 4 | `load_checkpoint()` ์ํคํ
์ฒ ๊ฒ์ฆ |
|
| 488 |
+
|
| 489 |
+
---
|
| 490 |
+
|
| 491 |
+
## ์คํ ์ง์ (๋ค์ ์ธ์
์ฉ)
|
| 492 |
+
|
| 493 |
+
์ด ๋ฌธ์๋ฅผ ์ฐธ์กฐํ์ฌ ๋ค์ ๋ช
๋ น์ ๋ด๋ฆฌ๋ฉด ๋ฉ๋๋ค:
|
| 494 |
+
|
| 495 |
+
> "์ด ๋ฌธ์(hashed-drifting-harp.md)์ Step 1~6์ ์์๋๋ก ์คํํด ์ค.
|
| 496 |
+
> Step 1+2๋ ๋ณ๋ ฌ๋ก, Step 3~6์ ๋
๋ฆฝ์ ์ผ๋ก ์งํ.
|
| 497 |
+
> ๊ฐ Step ์๋ฃ ํ ํด๋น ๊ฒ์ฆ์ ์คํํ๊ณ ,
|
| 498 |
+
> ์ ์ฒด ์๋ฃ ํ ์ต์ข
๊ฒ์ฆ ์ ์ฐจ 3๋จ๊ณ๋ฅผ ๋ชจ๋ ์คํํด ์ค."
|