forked from Lightning-AI/litgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script to convert Meta checkpoints to ours (Lightning-AI#12)
Co-authored-by: Carlos Mocholí <[email protected]>
- Loading branch information
Showing
4 changed files
with
70 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
__pycache__ | ||
.idea | ||
.DS_Store | ||
*.egg-info | ||
|
||
# data | ||
data | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,76 @@ | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
|
||
import torch | ||
from tqdm import tqdm | ||
import os | ||
import shutil | ||
|
||
""" | ||
Sample usage: | ||
```bash | ||
python -m models.llama.convert_checkpoint -h | ||
python -m scripts.convert_checkpoint -h | ||
python -m models.llama.convert_checkpoint meta_weights_for_meta_model converted | ||
python -m scripts.convert_checkpoint converted | ||
``` | ||
""" | ||
|
||
def convert_state_dict(state_dict): | ||
converted = {} | ||
converted["transformer.wte.weight"] = state_dict["tok_embeddings.weight"] | ||
converted["lm_head.weight"] = state_dict["output.weight"] | ||
converted["transformer.ln_f.scale"] = state_dict["norm.weight"] | ||
|
||
for key in [k for k in state_dict if k.startswith("layers")]: | ||
layer_idx = key.split(".")[1] | ||
|
||
@contextmanager | ||
def on_dtype(dtype): | ||
original = torch.get_default_dtype() | ||
torch.set_default_dtype(dtype) | ||
yield | ||
torch.set_default_dtype(original) | ||
# attention | ||
# the wq, wk, wv from the FB model are stacked in our model as c_attn | ||
converted[f"transformer.h.{layer_idx}.attn.c_attn.weight"] = torch.cat(( | ||
state_dict[f"layers.{layer_idx}.attention.wq.weight"], | ||
state_dict[f"layers.{layer_idx}.attention.wk.weight"], | ||
state_dict[f"layers.{layer_idx}.attention.wv.weight"], | ||
)) | ||
converted[f"transformer.h.{layer_idx}.attn.c_proj.weight"] = state_dict[f"layers.{layer_idx}.attention.wo.weight"] | ||
# mlp | ||
converted[f"transformer.h.{layer_idx}.mlp.c_fc1.weight"] = state_dict[f"layers.{layer_idx}.feed_forward.w1.weight"] | ||
converted[f"transformer.h.{layer_idx}.mlp.c_proj.weight"] = state_dict[f"layers.{layer_idx}.feed_forward.w2.weight"] | ||
converted[f"transformer.h.{layer_idx}.mlp.c_fc2.weight"] = state_dict[f"layers.{layer_idx}.feed_forward.w3.weight"] | ||
# rms norm | ||
converted[f"transformer.h.{layer_idx}.rms_1.scale"] = state_dict[f"layers.{layer_idx}.attention_norm.weight"] | ||
converted[f"transformer.h.{layer_idx}.rms_2.scale"] = state_dict[f"layers.{layer_idx}.ffn_norm.weight"] | ||
return converted | ||
|
||
|
||
def meta_weights_for_meta_model( | ||
def meta_weights_for_nano_model( | ||
*, | ||
output_dir: Path, | ||
ckpt_dir: Path = Path("/srv/data/checkpoints/llama/raw"), | ||
tokenizer_path: Path = Path("/srv/data/checkpoints/llama/raw/tokenizer.model"), | ||
model_size: str = "7B", | ||
): | ||
... | ||
|
||
|
||
def meta_weights_for_nano_model(): | ||
... | ||
output_dir = output_dir / model_size | ||
ckpt_dir = ckpt_dir / model_size | ||
os.makedirs(output_dir, exist_ok=True) | ||
|
||
# the tokenizer is the same for all model sizes, so we store it in the parent dir | ||
if "tokenizer.model" not in os.listdir(output_dir.parent): | ||
shutil.copy(tokenizer_path, output_dir.parent) | ||
|
||
def lightning_weights_for_nano_model(): | ||
... | ||
checkpoint_files = sorted(ckpt_dir.glob("*.pth")) | ||
|
||
|
||
# for the bigger models, there are multiple model-parallel checkpoints | ||
# and we combine them into one single file | ||
combined = {} | ||
for file in tqdm(checkpoint_files, total=len(checkpoint_files)): | ||
checkpoint = torch.load(file, map_location="cpu") | ||
converted = convert_state_dict(checkpoint) | ||
combined.update(converted) | ||
|
||
torch.save(combined, Path(output_dir, "state_dict.pth")) | ||
|
||
|
||
if __name__ == "__main__": | ||
from jsonargparse import CLI | ||
|
||
CLI([meta_weights_for_meta_model, meta_weights_for_nano_model, lightning_weights_for_nano_model]) | ||
CLI(meta_weights_for_nano_model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from setuptools import find_packages, setup | ||
|
||
setup( | ||
name="lightning-llama", | ||
version="0.0.1", | ||
description="", | ||
author="Lightning AI", | ||
url="https://lightning.ai", | ||
packages=find_packages(where="."), | ||
python_requires=">=3.8", | ||
) |