Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
Change-Id: Ic6698970f31cb4ddfc01de618b4e6471e6c444ca
  • Loading branch information
BestAnHongjun committed Aug 15, 2024
1 parent 41eb9d1 commit e6b824d
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 4 deletions.
188 changes: 184 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ Current large language models (LLMs) primarily utilize next-token prediction met
By integrating SentenceVAE into the input and output layers of LLMs, we develop Sentence-level LLMs (SLLMs) that employ a sentence-by-sentence inference method.

<div align="center">
<img src="assets/sllm.svg">
<img src="assets/sllm.svg"><br>
<span><small>Fig. 2. (a) The schematic form of published LLMs. (b) The schematic form of SLLMs, which embedded with SentenceVAEs.</small></span>
</div>

The SLLMs can maintain the integrity of the original semantic content by segmenting the context into sentences, thereby improving accuracy while boosting inference speed. Moreover, compared to previous LLMs, SLLMs process fewer tokens over equivalent context length, significantly reducing memory demands for self-attention computation and facilitating the handling of longer context. Extensive experiments on [Wanjuan dataset](https://github.com/opendatalab/WanJuan1.0/) have revealed that the proposed method can accelerate inference speed by 204~365%, reduce perplexity (PPL) to 46~75% of its original metric, and decrease memory overhead by 86~91% for the equivalent context length, compared to previous token-by-token methods.
The SLLMs can maintain the integrity of the original semantic content by segmenting the context into sentences, thereby improving accuracy while boosting inference speed. Moreover, compared to previous LLMs, SLLMs process fewer tokens over equivalent context length, significantly reducing memory demands for self-attention computation and facilitating the handling of longer context. Extensive experiments on [Wanjuan dataset](https://github.com/opendatalab/WanJuan1.0/) have revealed that the proposed method can accelerate inference speed by 204 ~ 365%, reduce perplexity (PPL) to 46 ~ 75% of its original metric, and decrease memory overhead by 86 ~ 91% for the equivalent context length, compared to previous token-by-token methods.

<div align="center">
<table border="1" cellspacing="0" cellpadding="5">
<table cellspacing="0" cellpadding="5">
<tr>
<th rowspan="2">Model</th>
<th rowspan="2">Total Params</th>
Expand Down Expand Up @@ -142,6 +142,186 @@ The SLLMs can maintain the integrity of the original semantic content by segment
In addition, by corroborating the Scaling Law, we extrapolated the feasibility of our methodologies to larger-scale models.

<div align="center">
<img src="assets/scaling_law.svg">
<img src="assets/scaling_law.svg"><br>
<span><small>Fig. 3. Scaling Law of (a) SLLMs and (b) SVAEs.</small></span>
</div>

# 2.Quick Start

<details>
<summary>Installation</summary>

Step1. Install SentenceVAE from source.

```sh
git clone https://github.com/BestAnHongjun/SentenceVAE.git
cd SentenceVAE
pip3 install -e . # or python3 setup.py develop
```

</details>

<details>
<summary>Prepare OPT models</summary>

Step1. Create a folder named `model_repo` under `SentenceVAE` to save OPT series models.

```sh
cd SentenceVAE
mkdir -p model_repo
```

Step2. Navigate to the `model_repo` directory with `cd` and initialize [`git-lfs`](https://git-lfs.com).

```sh
cd model_repo
git lfs install
```

Step3. Download [OPT-125M](https://huggingface.co/facebook/opt-125m) model for SentenceVAE-768 series and SLLM-125M series.

```sh
git clone https://huggingface.co/facebook/opt-125m
```

Step4. Download [OPT-350M](https://huggingface.co/facebook/opt-350m) model for SentenceVAE-1024 series and SLLM-350M series.

```sh
git clone https://huggingface.co/facebook/opt-350m
```

Step5. Download [OPT-1.3B](https://huggingface.co/facebook/opt-1.3b) model for Sentence-2048 series and SLLM-1.3B series.

```sh
git clone https://huggingface.co/facebook/opt-1.3b
```

</details>

<details>
<summary>SentenceVAE Demo</summary>

Step1. Download a pretrained model from table below.

<div align="center">

|Model|Hidden Size|Hidden Layers|Loss↓|PPL↓|Download Link|
|:-:|:-:|:-:|:-:|:-:|:-:|
|SVAE-768-H1|768|1|1.339|3.605|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceVAE/resolve/master/SVAE-768-H1.pth)|
|SVAE-768-H2|768|2|1.019|2.588|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceVAE/resolve/master/SVAE-768-H2.pth)|
|SVAE-768-H4|768|4|**0.5598**|**1.649**|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceVAE/resolve/master/SVAE-768-H4.pth)|
|SVAE-1024-H1|1024|1|0.9266|2.406|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceVAE/resolve/master/SVAE-1024-H1.pth)|
|SVAE-1024-H2|1024|2|0.6610|1.845|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceVAE/resolve/master/SVAE-1024-H2.pth)|
|SVAE-1024-H4|1024|4|**0.3704**|**1.384**|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceVAE/resolve/master/SVAE-1024-H4.pth)|
|SVAE-2048-H1|2048|1|0.5165|1.622|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceVAE/resolve/master/SVAE-2048-H1.pth)|
|SVAE-2048-H2|2048|2|0.2845|1.292|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceVAE/resolve/master/SVAE-2048-H2.pth)|
|SVAE-2048-H4|2048|4|**0.1270**|**1.115**|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceVAE/resolve/master/SVAE-2048-H4.pth)|

</div>

Step2. Run demo script under `tools/demo` folder. Here's an example:

```sh
cd SentenceVAE

python3 tools/demo/demo_svae.py \
-c config/SVAE/SVAE-768/svae_768_h4.yaml \
--checkpoint /path/to/pretrained/checkpoint \
--input "What's your name?"
```

**Arguments**:
* `-c`,`--config`: path to the corresponding configuration file, please reference [this folder](config/SVAE/).
* `--checkpoint`: path to the checkpoint file you just downloaded.
* `--input`: A sentence you want to test.
* It must be a separate sentence ending with punctuation marks such as commas, periods, etc. Please refer to the [paper](https://arxiv.org/abs/2408.00655) for specific reasons.
* Currently, only English is supported.

The model will compress this sentence into a single vector, decode and restore it for output. In an ideal state, the output and input should be consistent.

</details>

<details>

<summary>SentenceLLM Demo</summary>

**Notice**: Please be aware that, as SFT datasets are typically commercial secrets and difficult for us to access, all the models listed below are **pre-trained models**, not general-purpose conversation models. Therefore, the **PPL** (Perplexity) metric should be used to assess model quality, not conversational performance. If you treat them as Q&A models, you're likely to get gibberish outputs (***in fact, even our baseline OPT model will output gibberish***). We recommend fine-tuning these models on private SFT datasets to explore their potential as general-purpose conversation models.

Step1. Download a pretrained model from table below.

<div align="center">

|Model|Download Link|
|:-:|:-:|
|SLLM-125M-H1|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceLLM/resolve/master/SLLM-125M-H1.pth)|
|SLLM-125M-H2|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceLLM/resolve/master/SLLM-125M-H2.pth)|
|SLLM-125M-H4|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceLLM/resolve/master/SLLM-125M-H4.pth)|
|SLLM-350M-H1|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceLLM/resolve/master/SLLM-350M-H1.pth)|
|SLLM-350M-H2|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceLLM/resolve/master/SLLM-350M-H2.pth)|
|SLLM-350M-H4|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceLLM/resolve/master/SLLM-350M-H4.pth)|
|SLLM-1.3B-H1|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceLLM/resolve/master/SLLM-1.3B-H1.pth)|
|SLLM-1.3B-H2|[ModelScope](https://modelscope.cn/models/CoderAN/SentenceLLM/resolve/master/SLLM-1.3B-H2.pth)|

</div>

Step2. Run demo script under `tools/demo` folder. Here's an example:

```sh
cd SentenceVAE

python3 tools/demo/demo_sllm.py \
-c config/SLLM/SLLM-125m/sllm_125m_h4_all.yaml \
--checkpoint /path/to/pretrained/checkpoint \
--input "What's your name?"
```

**Arguments**:
* `-c`,`--config`: path to the corresponding configuration file, please reference [this folder](config/SLLM/).
* `--checkpoint`: path to the checkpoint file you just downloaded.
* `--input`: Your input sentence.


</details>

# 3.Tutorials

Under writing...

<details>
<summary>Train Models</summary>

* [Prepare Datasets](#)
* [Train SentenceVAEs](#)
* [Train SentenceLLMs](#)

</details>

<details>
<summary>Eval Models</summary>

* [Eval OPT models (baseline)](#)
* [Eval SentenceVAEs](#)
* [Eval SentenceLLMs](#)

</details>

<details>
<summary>Test Benchmarks</summary>

* [Test benchmarks of SentenceVAEs](#)
* [Test benchmarks of SentenceLLMs](#)

</details>

# 4.Cite SentenceVAE

If you use SentenceVAE in your research, please cite our work by using the following BibTeX entry:

```bibtex
@article{an2024sentencevae,
title={SentenceVAE: Enable Next-sentence Prediction for Large Language Models with Faster Speed, Higher Accuracy and Longer Context},
author={An, Hongjun and Chen, Yifan and Sun, Zhe and Li, Xuelong},
journal={arXiv preprint arXiv:2408.00655},
year={2024}
}
```
1 change: 1 addition & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO.

0 comments on commit e6b824d

Please sign in to comment.