File size: 3,337 Bytes
2b6d729
 
 
 
 
 
 
 
 
8b8d414
 
d2df56b
 
 
0911327
 
d2df56b
 
 
 
0911327
 
8b8d414
d2df56b
 
 
 
 
740eb40
cc8d49b
d2df56b
69d5187
 
0911327
69d5187
 
 
 
 
 
 
 
 
 
 
 
 
 
3c37a9f
69d5187
 
 
 
 
 
d0dd825
740eb40
69d5187
 
 
 
 
 
d2df56b
 
 
 
 
62814d1
 
 
 
 
 
 
 
 
69d5187
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
---
license: apache-2.0
datasets:
- LLM360/AmberDatasets
language:
- en
metrics:
- accuracy
pipeline_tag: text-generation
---

# FBI-LLM-7B
## Model Details

This work presents a Fully BInarized Large Language Model (FBI-LLM), demonstrating for the first time how to train a large-scale binary language model (not the ternary LLM like BitNet b1.58 from scratch to match the performance of its full-precision counterparts (e.g., FP16 or BF16) in transformer-based LLMs. It achieves this by employing an autoregressive distillation (AD) loss with maintaining equivalent model dimensions (130M, 1.3B, 7B) and training data volume as regular LLM pretraining, while delivering competitive results in terms of perplexity and task-specific effectiveness. Intriguingly, by analyzing the training trajectory, we find that the pretrained weight is not necessary for training binarized LLMs from scratch. This research encourages a new computational framework and may facilitate the future design of specialized hardware tailored for fully 1-bit LLMs. We make all models, code, and training dataset fully accessible and transparent to support further research.

**Input**: Models input text only.

**Output**: Models generate text only.

## Tokenizer
We use the same tokenizer as [meta-llama/Llama-2-7b-hf](https://huggingface.co/meta-llama/Llama-2-7b-hf)

## Training Data
We use [AmberDateset](https://huggingface.co/datasets/LLM360/AmberDatasets) to train our models.

## Result

<img src=https://huggingface.co/LiqunMa/FBI-LLM_7B/resolve/main/main_result.jpg width="90%" />
<!-- ![image](https://huggingface.co/LiqunMa/FBI-LLM_7B/resolve/main/main_result.jpg =200x) -->

## How to use
Please download the code from [LiqunMa/FBI-LLM](https://github.com/LiqunMa/FBI-LLM) firstly
```python
from pathlib import Path
from transformers import AutoTokenizer,LlamaConfig,LlamaForCausalLM, AutoModelForCausalLM
from qat.replace_module import replace_with_learnable_binarylinear


def load_model(model_size, model_dir):
    assert model_size in ["130M", "1.3B", "7B"]

    model_dir = Path(model_dir)
    with Path(f'FBI-LLM_configs/FBI-LLM_llama2_{model_size}.json').open('r') as r_f:
        config = json.load(r_f)
    llama_config = LlamaConfig(**config)
    model = LlamaForCausalLM(llama_config).to('cuda')
    tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-hf', padding_side="right", use_fast=False)
    model = replace_with_learnable_binarylinear(model, scaling_pattern = "column", keep_parts = ["lm_head"])

    weight_dict = {}
    ckpt_plist = [p for p in model_dir.iterdir() if p.suffix == '.bin']
    for p in ckpt_plist:
      weight_dict = torch.load(p)
      for k,v in _weight_dict.items():
        if 'self_attn.rotary_emb.inv_freq' not in k:
          weight_dict[k] = v

    model.load_state_dict(weight_dict)
    for param in model.parameters():
        param.data = param.data.to(torch.float16)

    return model, tokenizer
```

## Citation
### BibTeX:
```
@misc{ma2024fbillmscalingfullybinarized,
      title={FBI-LLM: Scaling Up Fully Binarized LLMs from Scratch via Autoregressive Distillation}, 
      author={Liqun Ma and Mingjie Sun and Zhiqiang Shen},
      year={2024},
      eprint={2407.07093},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2407.07093}, 
}
```