commit 9913be5c80ff20e5037d9570398fb4b58f80fecc Author: ModelHub XC Date: Tue Oct 13 01:02:14 2026 +0800 初始化项目,由ModelHub XC社区提供模型 Model: iic/nlp_polylm_assistant_13b_text_generation Source: Original Platform diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..97c8e80 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,32 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bin.* filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zstandard filter=lfs diff=lfs merge=lfs -text +*.tfevents* filter=lfs diff=lfs merge=lfs -text +*.db* filter=lfs diff=lfs merge=lfs -text +*.ark* filter=lfs diff=lfs merge=lfs -text +**/*ckpt*data* filter=lfs diff=lfs merge=lfs -text +**/*ckpt*.meta filter=lfs diff=lfs merge=lfs -text +**/*ckpt*.index filter=lfs diff=lfs merge=lfs -text diff --git a/README.md b/README.md new file mode 100644 index 0000000..45df17c --- /dev/null +++ b/README.md @@ -0,0 +1,121 @@ +--- +tasks: + - text-generation + - text2text-generation + - sentiment-classification + - sentence-similarity + - zero-shot-classification + - named-entity-recognition + - translation + - nli + - question-answering + - text-error-correction + - text-summarization + - faq-question-answering + - text-classification + +model-type: +- gpt + +domain: +- nlp +frameworks: +- pytorch +backbone: +- transformer +license: Apache License 2.0 +language: +- ch,en,es,fr,pt,ru,ar,ja,ko,it,de,th,vi,id +tags: +- transformer + +studios: +- damo/demo-polylm-multialpaca-13b +--- + +# PolyLM多语言-智能服务-文本生成模型 + +## 模型简介 +PolyLM是一个通晓多种语言的大规模语言模型,涵盖中文、英文、西班牙语、法语、德语、俄语、葡萄牙语、意大利语、阿拉伯语、日语、韩语、泰语、越南语和印尼语等18个语言。该模型可以应用于对话问答、文本生成、机器翻译和情感分析等领域,能够自动生成高质量的多语言文本,从而为跨语言、文化的交流提供便利。 + +## Abstract in English +Large language models (LLMs) demonstrate remarkable ability to comprehend, reason, and generate following nature language instructions. However, the development of LLMs has been primarily focused on high-resource languages, such as English, thereby limiting their applicability and research in other languages. Consequently, we present PolyLM, a multilingual LLM trained on 640 billion (B) tokens, avaliable in two model sizes: 1.7B and 13B. To enhance its multilingual capabilities, we 1) integrate bilingual data into training data; and 2) adopt a curriculum learning strategy that increases the proportion of non-English data from 30% in the first stage to 60% in the final stage during pre-training. Further, we propose a multilingual self-instruct method which automatically generates 132.7K diverse multilingual instructions for model fine-tuning. To assess the model's performance, we collect several existing multilingual tasks, including multilingual understanding, question answering, generation, and translation. Extensive experiments show that PolyLM surpasses other open-source models such as LLaMA and BLOOM on multilingual tasks while maintaining comparable performance in English. + +Our models, alone with the multilingual instruction data, are available at [Github](https://github.com/DAMO-NLP-MT/PolyLM) and [Huggingface](https://huggingface.co/DAMO-NLP-MT). + +## 模型版本 +本项目提供了一系列不同规模和用途的模型,参数规模包括1.7B/13B版本(当前模型为13B版本),同时涵盖了预训练底座模型以及指令精调后的Chat版本(即MultiAlpaca系列)。全部版本如下表所示: + +|Model|Precision|Layers|Heads|Hidden|Max_length|LR|Batch|Type| +|---|---|---|---|---|---|---|---|---| +|[PolyLM-1.7B](https://modelscope.cn/models/damo/nlp_polylm_1b7_text_generation/summary)|bfloat16|24|16|2048|2048|1.0e-4|4M|Pretrain Model| +|[PolyLM-13B](https://modelscope.cn/models/damo/nlp_polylm_13b_text_generation/summary)|bfloat16|40|40|5120|2048|6.0e-5|4M|Pretrain Model| +|[PolyLM-MultiAlpaca-13B](https://modelscope.cn/models/damo/nlp_polylm_multialpaca_13b_text_generation/summary)|bfloat16|40|40|5120|2048|6.0e-5|4M|Chat Model| +|[PolyLM-Assistant-13B](https://www.modelscope.cn/models/damo/nlp_polylm_assistant_13b_text_generation/summary)|bfloat16|40|40|5120|2048|6.0e-5|4M|Chat Model| + +## 训练数据 + +该模型以[PolyLM-13B](https://modelscope.cn/models/damo/nlp_polylm_13b_text_generation/summary)预训练模型为底座,在以下数据上指令微调得到: + +|名称|数量|构建方式|备注| +|---|---|---|---| +|[code_alpaca](https://github.com/sahil280114/codealpaca)|28|GPT 3.5 self-instruct|为了正确展示,对代码做了格式过滤,要求输入、输出中至少有一端可以找出一对```| +|[dolly](https://github.com/databrickslabs/dolly)|15,011|人工编写|| +|[flan_v2](https://huggingface.co/datasets/SirNeural/flan_v2)|100,000|各类NLP任务、CoT任务|从 flan_v2中采样,全量数据非常大| +|[gpt4_alpaca](https://github.com/Instruction-Tuning-with-GPT-4/GPT-4-LLM) (英文)|52,002|GPT-4 self-instruct|| +|[gpt4_alpaca](https://github.com/Instruction-Tuning-with-GPT-4/GPT-4-LLM) (中文)|48,818|GPT-4 self-instruct|| +|[multilingual_alpaca](https://www.modelscope.cn/datasets/damo/nlp_polylm_multialpaca_sft/summary)|132,701|GPT-3.5 self-instruct|| +|[open_assistant](https://github.com/LAION-AI/Open-Assistant)|55,668|人工编写|| +|[share_gpt](https://github.com/domeccleston/sharegpt)|140,591|ChatGPT聊天记录|| +|[gpteacher_codegen](https://huggingface.co/GeorgiaTechResearchInstitute/starcoder-gpteacher-code-instruct)|4,535||| + +## 模型下载 +```bash + git lfs install + git clone https://www.modelscope.cn/damo/nlp_polylm_assistant_13b_text_generation.git +``` + +## 模型使用 +```shell +pip install huggingface-hub==0.25.* transformers==4.48.3 +``` + + +```python +# git clone https://github.com/modelscope/modelscope +# cd modelscope +# pip install . + +from modelscope.pipelines import pipeline +from modelscope.utils.constant import Tasks +from modelscope import snapshot_download + +polylm_13b_model_id = 'damo/nlp_polylm_assistant_13b_text_generation' +revision = 'v1.0.0' + +model_dir = snapshot_download(polylm_13b_model_id, revision) + +input_text = f"Beijing is the capital of China.\nTranslate this sentence from English to Chinese." +input_text = "<|user|>\n" + f"{input_text}\n" + "<|assistant|>\n" + +kwargs = {"do_sample": False, "num_beams": 4, "max_new_tokens": 128, "early_stopping": True, "eos_token_id": 2} +pipeline_ins = pipeline(Tasks.text_generation, model=model_dir, external_engine_for_llm=False) + +result = pipeline_ins(input_text, **kwargs) +print(result['text']) + +``` + + +## 论文引用 +如果你觉得这个该模型对有所帮助,请考虑引用下面的相关的论文: +``` bibtex +@misc{wei2023polylm, + title={PolyLM: An Open Source Polyglot Large Language Model}, + author={Xiangpeng Wei and Haoran Wei and Huan Lin and Tianhao Li and Pei Zhang and Xingzhang Ren and Mei Li and Yu Wan and Zhiwei Cao and Binbin Xie and Tianxiang Hu and Shangjie Li and Binyuan Hui and Bowen Yu and Dayiheng Liu and Baosong Yang and Fei Huang and Jun Xie}, + year={2023}, + eprint={2307.06018}, + archivePrefix={arXiv}, + primaryClass={cs.CL} +} +``` diff --git a/config.json b/config.json new file mode 100644 index 0000000..b184a77 --- /dev/null +++ b/config.json @@ -0,0 +1,31 @@ +{ + "activation_function": "gelu_fast", + "architectures": [ + "GPT2LMHeadModel" + ], + "attn_pdrop": 0.0, + "bos_token_id": 255999, + "embd_pdrop": 0.0, + "eos_token_id": 255999, + "initializer_range": 0.02, + "layer_norm_epsilon": 1e-05, + "model_type": "gpt2", + "n_embd": 5120, + "n_head": 40, + "n_inner": 20480, + "n_layer": 40, + "n_positions": 2048, + "reorder_and_upcast_attn": false, + "resid_pdrop": 0.0, + "scale_attn_by_inverse_layer_idx": false, + "scale_attn_weights": true, + "summary_activation": null, + "summary_first_dropout": 0.0, + "summary_proj_to_labels": true, + "summary_type": "cls_index", + "summary_use_proj": true, + "tokenizer_class": "AutoTokenizer", + "transformers_version": "4.29.2", + "use_cache": true, + "vocab_size": 256000 +} diff --git a/configuration.json b/configuration.json new file mode 100644 index 0000000..3731251 --- /dev/null +++ b/configuration.json @@ -0,0 +1,10 @@ +{ + "task": "text-generation", + "framework": "Pytorch", + "model": { + "type": "polylm" + }, + "pipeline": { + "type": "polylm-text-generation" + } +} \ No newline at end of file diff --git a/polylm_web_demo_gradio.py b/polylm_web_demo_gradio.py new file mode 100644 index 0000000..f339d13 --- /dev/null +++ b/polylm_web_demo_gradio.py @@ -0,0 +1,172 @@ +import argparse +import os +import warnings +import mdtex2html +import gradio as gr + +import re +pattern = re.compile("[\n]+") + +import torch +from accelerate import init_empty_weights, load_checkpoint_and_dispatch +from huggingface_hub import snapshot_download + +from transformers.generation.utils import logger +from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer + +model_base_path = "/mnt/workspace/users/xiangpeng.wxp/workspace/polylm/convert_from_megatron_to_transformers/ckpts" + +parser = argparse.ArgumentParser() +parser.add_argument("--model_name", default=f"{model_base_path}/instruct_data_hub_sft_5200", + choices=["DAMO-NLP-MT/polylm-multialpaca-13b"], type=str) +parser.add_argument("--gpu", default="0", type=str) +args = parser.parse_args() + +os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu +num_gpus = len(args.gpu.split(",")) + +if ('int8' in args.model_name or 'int4' in args.model_name) and num_gpus > 1: + raise ValueError("Quantized models do not support model parallel. Please run on a single GPU (e.g., --gpu 0).") + +logger.setLevel("ERROR") +warnings.filterwarnings("ignore") + +model_path = args.model_name +if not os.path.exists(args.model_name): + model_path = snapshot_download(args.model_name) + +config = AutoConfig.from_pretrained(model_path) +tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False) + +if num_gpus > 1: + print("Waiting for all devices to be ready, it may take a few minutes...") + with init_empty_weights(): + raw_model = AutoModelForCausalLM.from_config(config) + raw_model.tie_weights() + model = load_checkpoint_and_dispatch( + raw_model, model_path, device_map="auto", no_split_module_classes=["GPT2Block"] + ) +else: + print("Loading model files, it may take a few minutes...") + model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True).cuda() + +print(model.dtype) + +def postprocess(self, y): + if y is None: + return [] + for i, (message, response) in enumerate(y): + y[i] = ( + None if message is None else mdtex2html.convert((message)), + None if response is None else mdtex2html.convert(response), + ) + return y + + +gr.Chatbot.postprocess = postprocess + + +def parse_text(text): + """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/""" + lines = text.split("\n") + lines = [line for line in lines if line != ""] + count = 0 + for i, line in enumerate(lines): + if "```" in line: + count += 1 + items = line.split('`') + if count % 2 == 1: + lines[i] = f'
'
+            else:
+                lines[i] = f'
' + else: + line = line.replace("`", "\`") + line = line.replace("<", "<") + line = line.replace(">", ">") + line = line.replace(" ", " ") + line = line.replace("*", "*") + line = line.replace("_", "_") + line = line.replace("-", "-") + line = line.replace(".", ".") + line = line.replace("!", "!") + line = line.replace("(", "(") + line = line.replace(")", ")") + line = line.replace("$", "$") + if i > 0: + lines[i] = "
"+line + text = "".join(lines) + return text + + +def predict(input, chatbot, max_length, top_p, temperature, history): + query = input + query = query.strip() + + chatbot.append((query, "")) + prompt = "" + for i, (old_query, response) in enumerate(history): + response = response.strip() + prompt += "<|user|>\n" + f"{old_query}\n" + "<|assistant|>\n" + f"{response}\n" + prompt += "<|user|>\n" + f"{query}\n" + "<|assistant|>\n" + inputs = tokenizer(prompt, return_tensors="pt") + with torch.no_grad(): + outputs = model.generate( + inputs.input_ids.cuda(), + attention_mask=inputs.attention_mask.cuda(), + max_length=max_length, + do_sample=True, + top_p=top_p, + temperature=temperature, + repetition_penalty=1.02, + num_return_sequences=1, + eos_token_id=2, + early_stopping=True) + response = tokenizer.decode( + outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True) + + chatbot[-1] = (parse_text(query), parse_text(response.replace("\n ", "\n"))) + history = history + [(query, response)] + print("==========================================================================") + print(f"chatbot is {chatbot}") + print(f"history is {history}") + print("==========================================================================") + return chatbot, history + + +def reset_user_input(): + return gr.update(value='') + + +def reset_state(): + return [], [] + + +with gr.Blocks() as demo: + gr.HTML("""

欢迎使用 PolyLM 多语言人工智能助手!

""") + + chatbot = gr.Chatbot() + with gr.Row(): + with gr.Column(scale=4): + with gr.Column(scale=12): + user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style( + container=False) + with gr.Column(min_width=32, scale=1): + submitBtn = gr.Button("Submit", variant="primary") + with gr.Column(scale=1): + emptyBtn = gr.Button("Clear History") + max_length = gr.Slider( + 0, 4096, value=2048, step=1.0, label="Maximum length", interactive=True) + top_p = gr.Slider(0, 1, value=0.8, step=0.01, + label="Top P", interactive=True) + temperature = gr.Slider( + 0, 1, value=0.7, step=0.01, label="Temperature", interactive=True) + + history = gr.State([]) # (message, bot_message) + + submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history], [chatbot, history], + show_progress=True) + submitBtn.click(reset_user_input, [], [user_input]) + + emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True) + +demo.queue().launch(share=False, inbrowser=True) diff --git a/pytorch_model-00001-of-00004.bin b/pytorch_model-00001-of-00004.bin new file mode 100644 index 0000000..25b93d3 --- /dev/null +++ b/pytorch_model-00001-of-00004.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3f783ac6c62c466107986de443a7e543dc18a70ab848cf8a9bbb897a39c8cd97 +size 8809402977 diff --git a/pytorch_model-00002-of-00004.bin b/pytorch_model-00002-of-00004.bin new file mode 100644 index 0000000..1e02ca1 --- /dev/null +++ b/pytorch_model-00002-of-00004.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af6e958d9f17bf1b71c6b434ba0de98d43ca76cfaefe143a9c1377bd08c23323 +size 8927403605 diff --git a/pytorch_model-00003-of-00004.bin b/pytorch_model-00003-of-00004.bin new file mode 100644 index 0000000..0376b1d --- /dev/null +++ b/pytorch_model-00003-of-00004.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fc793bbf117cafdb183affe11ba3927cae91d31fd5e776ef9c7fe635115cbda3 +size 8927403605 diff --git a/pytorch_model-00004-of-00004.bin b/pytorch_model-00004-of-00004.bin new file mode 100644 index 0000000..8304e9b --- /dev/null +++ b/pytorch_model-00004-of-00004.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2d08f10b01ecd6dce32c434f7148dce0873445bddf3e00082fa85ce93d80d377 +size 4106529922 diff --git a/pytorch_model.bin.index.json b/pytorch_model.bin.index.json new file mode 100644 index 0000000..09f0265 --- /dev/null +++ b/pytorch_model.bin.index.json @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b2c9a52301c32230341d0974b5d0b9a175704aef2cea57bbcb48daba5624df40 +size 42138 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5f78c02 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +huggingface-hub==0.25.* +transformers==4.48.3 diff --git a/special_tokens_map.json b/special_tokens_map.json new file mode 100644 index 0000000..d85ba6c --- /dev/null +++ b/special_tokens_map.json @@ -0,0 +1,23 @@ +{ + "bos_token": { + "content": "", + "lstrip": false, + "normalized": true, + "rstrip": false, + "single_word": false + }, + "eos_token": { + "content": "", + "lstrip": false, + "normalized": true, + "rstrip": false, + "single_word": false + }, + "unk_token": { + "content": "", + "lstrip": false, + "normalized": true, + "rstrip": false, + "single_word": false + } +} diff --git a/tokenizer.model b/tokenizer.model new file mode 100644 index 0000000..e22c05b --- /dev/null +++ b/tokenizer.model @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:63639ba62c4af921cb7c373630bee7d45568fc5ff7a98add756e8aa57df59065 +size 4754121 diff --git a/tokenizer_config.json b/tokenizer_config.json new file mode 100644 index 0000000..415ae30 --- /dev/null +++ b/tokenizer_config.json @@ -0,0 +1,33 @@ +{ + "add_bos_token": false, + "add_eos_token": false, + "bos_token": { + "__type": "AddedToken", + "content": "", + "lstrip": false, + "normalized": true, + "rstrip": false, + "single_word": false + }, + "clean_up_tokenization_spaces": false, + "eos_token": { + "__type": "AddedToken", + "content": "", + "lstrip": false, + "normalized": true, + "rstrip": false, + "single_word": false + }, + "model_max_length": 2048, + "pad_token": null, + "sp_model_kwargs": {}, + "tokenizer_class": "LlamaTokenizer", + "unk_token": { + "__type": "AddedToken", + "content": "", + "lstrip": false, + "normalized": true, + "rstrip": false, + "single_word": false + } +}