Lightning-AI / Lightning-AI/lit-llama

`scripts/convert_hf_checkpoint.py --verify true` fails

Open
#175 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
6.1k
Forks
517
PR merge metrics
No merged PRs in 30d

Description

```python
Traceback (most recent call last):
File "scripts/convert_hf_checkpoint.py", line 68, in
CLI(convert_hf_checkpoint)
File "/home/carmocca/.local/lib/python3.8/site-packages/jsonargparse/cli.py", line 82, in CLI
return _run_component(component, cfg_init)
File "/home/carmocca/.local/lib/python3.8/site-packages/jsonargparse/cli.py", line 138, in _run_component
return component(**cfg)
File "/usr/local/lib/python3.8/dist-packages/torch/utils/_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
File "scripts/convert_hf_checkpoint.py", line 62, in convert_hf_checkpoint
assert torch.testing.assert_close(out, out_hf)
File "/usr/local/lib/python3.8/dist-packages/torch/testing/_comparison.py", line 1511, in assert_close
raise error_metas[0].to_error(msg)
AssertionError: Tensor-likes are not close!

Mismatched elements: 131071613 / 131072000 (100.0%)
Greatest absolute difference: 32.40781021118164 at index (0, 2739, 1) (up to 1e-05 allowed)
Greatest relative difference: 1.0 at index (0, 0, 0) (up to 1.3e-06 allowed)
```

Using this slightly modified version of the file

```python
import gc
import json
import shutil
import sys
from pathlib import Path

import torch

# support running without installing as a package
wd = Path(__file__).parent.parent.resolve()
sys.path.append(str(wd))

from lit_llama.model import LLaMA, LLaMAConfig
from lit_llama.utils import EmptyInitOnDevice

@torch.no_grad()
def convert_hf_checkpoint(
*,
output_dir: Path = Path("checkpoints/lit-llama"),
ckpt_dir: Path = Path("checkpoints/hf-llama/"),
model_size: str = "7B",
dtype: str = "float32",
verify: bool = False,
) -> None:
"""
Perform the reverse operation of: https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/convert_llama_weights_to_hf.py
"""
output_dir = output_dir / model_size
ckpt_dir = ckpt_dir / model_size

dt = getattr(torch, dtype, None)
if not isinstance(dt, torch.dtype):
raise ValueError(f"{dtype} is not a valid dtype.")
dtype = dt

config = LLaMAConfig.from_name(model_size)

with EmptyInitOnDevice(device="cpu", dtype=dtype):
model = LLaMA(config)

if verify:
try:
from transformers import LlamaForCausalLM
except ImportError:
raise ImportError("verify=True requires transformers to be installed, please `pip install transformers`")
print("Verifying...")

token_sample = torch.randint(0, config.vocab_size, size=(1, config.block_size), dtype=torch.int64)
out = model(token_sample)
del model
gc.collect()

print("Loading original model for comparison")
model_hf = LlamaForCausalLM.from_pretrained(ckpt_dir)
out_hf = model_hf(token_sample)["logits"]

print("Comparing outputs")
assert out.device.type == out_hf.device.type
assert out.dtype == out_hf.dtype
assert torch.testing.assert_close(out, out_hf)

if __name__ == "__main__":
from jsonargparse import CLI

CLI(convert_hf_checkpoint)
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with scripts/convert_hf_checkpoint.py --verify true and inspect the comparison at line 62, including the outputs from LLaMA and LlamaForCausalLM. Verify the conversion behavior against the traceback; done means the verification comparison completes without the tensor mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.