{"id":21164201,"url":"https://github.com/QwenLM/Self-Lengthen","last_synced_at":"2025-07-09T16:33:15.129Z","repository":{"id":260589974,"uuid":"881682631","full_name":"QwenLM/Self-Lengthen","owner":"QwenLM","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-06T07:44:13.000Z","size":102,"stargazers_count":59,"open_issues_count":1,"forks_count":3,"subscribers_count":14,"default_branch":"main","last_synced_at":"2024-11-18T10:33:47.807Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/QwenLM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-01T02:50:43.000Z","updated_at":"2024-11-17T05:42:49.000Z","dependencies_parsed_at":"2024-11-01T08:23:18.235Z","dependency_job_id":"6f3e4b3f-8bb3-4450-89d1-70c37b1ed709","html_url":"https://github.com/QwenLM/Self-Lengthen","commit_stats":null,"previous_names":["qwenlm/self-lengthen"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QwenLM%2FSelf-Lengthen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QwenLM%2FSelf-Lengthen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QwenLM%2FSelf-Lengthen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QwenLM%2FSelf-Lengthen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/QwenLM","download_url":"https://codeload.github.com/QwenLM/Self-Lengthen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225570420,"owners_count":17489885,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T14:01:15.973Z","updated_at":"2024-11-20T14:01:23.516Z","avatar_url":"https://github.com/QwenLM.png","language":"Python","funding_links":[],"categories":["A01_文本生成_文本对话"],"sub_categories":["大语言对话模型及数据"],"readme":"# Language Models Can Self-Lengthen to Generate Long Texts\n\n*Shanghaoran Quan, Tianyi Tang, Bowen Yu, An Yang, Dayiheng Liu, Bofei Gao, Jianhong Tu, Yichang Zhang, Jingren Zhou, Junyang Lin*\n\nQwen Team, Alibaba Inc.\n\n---\n\n## :sparkles: Overview\n\nThis repository contains core implementations of the **Self-Lengthen**, proposed by [Language Models Can Self-Lengthen to Generate Long Texts](https://arxiv.org/abs/2410.23933).\n\n**Self-Lengthen** is a novel and effective data-driven technique for extrapolating **long output**, designed to stimulate long-generation ability from scratch using only the LLM's intrinsic knowledge and skills. This is achieved by iteratively self-lengthening the output and inductively self-aligning to generate increasingly longer texts. By applying **Self-Lengthen**, we successfully increased the maximum output length of Qwen from 1,000 words to 8,000 words.\n\n![image](https://qianwen-res.oss-accelerate.aliyuncs.com/assets/self-lengthen/self-lengthen_approach.png)\n\n- **Low resource**: Self-Lengthen does not require high-quality human-written text; only a set of seed user long output instructions is needed.\n\n- **Intrinsic ability**: Self-Lengthen utilizes only the seed LLM's intrinsic knowledge and skills, without any form of distillation from stronger LLMs.\n\n- **Free form**: Self-Lengthen can generate suitable responses to a wide range of long output instructions and is not confined to strictly structured formats.\n\n---\n\n## :rocket: Data Synthesis of Self-Lengthen\n\n### :wrench: Getting Started\n\n1. Clone this repository.\n1. We have made several crucial changes in `FastChat` project to support vLLM LoRA requests and extra decoding parameters like `repetition_penalty` and `top_k`. Please clone [this repository](https://github.com/quanshr/FastChat/tree/self-lengthen) and run `pip install -e \".[model_worker,webui]\"` to install.\n1. Run `pip install -r requirements.txt` to install other required packages.\n1. The repository contains code for both Qwen and LLaMA models to apply Self-Lengthen. Both codebases share the same main structure with only slight differences (e.g., chat template, prompts, filter conditions) to fit their respective properties. Navigate to either the Qwen or LLaMA directory to proceed with the following steps.\n\n### Running the Code\n\nThe code supports both multi-node and single-node execution. To run:\n\n1. Change the `base_model` variable to your base model *path* at the top of `run.sh`, `run_server.sh`, `run_collect_data.sh`, and `config.py`.\n1. Ensure that `WORLD_SIZE`, `RANK`, `MASTER_ADDR`, and `MASTER_PORT` are set to the appropriate values on your cluster (`WORLD_SIZE` is 1 and `RANK` is 0 for a single node).\n1. On each node, execute:\n   ```\n   bash run.sh \u003e run_$RANK.log \\\n      --base_model=YOUR_MODEL_PATH \\\n      --instruct_count=5000 \\\n      --max_iter=3 2\u003e\u00261\n   ```\n   Larger `max_iter` will conduct more macro-iterations and will get longer responses at the end. During each macro-iteration, we start with serving the vLLM workers managed by FastChat. Then, we execute step0) *self instruct*, step1) *initial responses generation*, and step2) *responses extension* sequentially (Considering user privacy, we only release the minimum number of seed instructions needed to run). Finally, we shut down the vLLM server and utilize LLaMA-Factory to fine-tune the Generator and the Extender.\n1. Wait for the Self-Lengthen process to complete. It may take some time for small clusters.\n\n### Data Collection\n\nAfter the Self-Lengthen process finishes:\n\n1. *(Optional)* Run the following command on each node to generate additional data; set `total_count` as the desired generation count on each node:\n   ```bash\n   bash run_collect_data.sh --base_model=YOUR_MODEL_PATH \\\n      --total_count=5000 \\\n      --instruct_count=500 \\\n      --max_iter=3\n   ```\n1. Run `python colloct_data.py` on the master node to collect all the generated training data.\n\n### Training Process\n\nThe training process is straightforward:\n\n1. Incorporate length demands into the queries using `eval/add_length_control.py`.\n1. Perform supervised fine-tuning on the (query, long response) pairs.\n\nThis process helps align the model's output with desired length requirements while maintaining coherence and relevance.\n\n---\n\n## 🎯 Evaluation: LonGen Benchmark\n\nIn the `eval` directory, we provide `LonGen` benchmark ([HF link](https://huggingface.co/datasets/quanshr/LonGen)) to thoroughly evaluate the generated long responses under various instructions and length constraint types. Specifically, we evaluate tasks including 1) Literature and Creative Writing, 2) Academic Research, 3) Business Communication, 4) Journalism and Media, 5) Miscellaneous Professional Writing, 6) Personal Expression, and 7) Technical Documentation. The required output lengths are distributed equally in the range of 2,000 to 8,000 words, containing `about`, `range`, `above`, and `below` four types of length constraints. Depending on the actual generated responses, each type of constraint will have a different formula to calculate the length following scores.\n\nIt's quite easy to evaluate on our `LonGen` benchmark, to evaluate:\n\n1. First get the outputs of the model on `LonGen` and attach the generated outputs in the `response` column.\n1. Run `python length_following_eval.py OUTPUT_DATA_PATH` to calculate the length following scores.\n1. Call GPT-4o to judge the quality scores using the template in `quality_eval.md`.\n\n---\n\n## :mag_right: Overall Results\n\n![image](https://qianwen-res.oss-accelerate.aliyuncs.com/assets/self-lengthen/self-lengthen_result.png)\n\n## Citation\n\nIf you find this work helpful for your research, please kindly cite it.\n\n```\n@article{quan2024language,\n  title={Language Models Can Self-Lengthen to Generate Long Texts},\n  author={Shanghaoran Quan, Tianyi Tang, Bowen Yu, An Yang, Dayiheng Liu, Bofei Gao, Jianhong Tu, Yichang Zhang, Jingren Zhou, Junyang Lin},\n  journal={arXiv preprint arXiv:2410.23933},\n  year={2024}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FQwenLM%2FSelf-Lengthen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FQwenLM%2FSelf-Lengthen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FQwenLM%2FSelf-Lengthen/lists"}