ONNX 模型库
返回模型

说明文档

启用键值缓存的 Mistral 7B Instruct v0.2 ONNX fp16 格式模型

<!-- description start -->

简介

本仓库包含由 Esperanto Technologies 转换的 Mistral 7B Instruct v0.2 ONNX 文件。 该模型采用 fp16 格式,并启用了键值缓存(KVC)功能。

<!-- description end -->

如何下载 ONNX 模型和权重文件

获取模型最简单的方法是克隆整个仓库。 另一种下载方式是使用 huggingface-hub Python 库。

pip3 install huggingface-hub>=0.17.1

然后你可以使用如下命令将任意单个模型文件高速下载到当前目录:

huggingface-cli download Esperanto/mistral-7b-Instruct-v0.2-kvc-fp16-onnx --local-dir mistral-7b-Instruct-v0.2-kvc-fp16-onnx --local-dir-use-symlinks False

关于使用 huggingface-cli 下载的更多文档,请参阅:HF -> Hub Python Library -> Download files -> Download from the CLI。

如何使用 ONNXRuntime 从 Python 代码运行

该模型可以轻松地在 CPU 上使用 ONNXRuntime 运行。

首先安装依赖包

pip3 install onnx==1.16.1
pip3 install onnxruntime==1.17.1

示例代码:使用此模型生成文本

我们使用贪婪解码定义循环:

import numpy as np
import onnxruntime
import onnx
from transformers import AutoTokenizer

def generate_text(model_path, prompt, tokenizer, max_gen_tokens, total_sequence, window, context):
    model = onnx.load(model_path)

    #我们为第一次迭代创建输入
    input_tensor = tokenizer(prompt, return_tensors=\"pt\")
    prompt_size = len(input_tensor['input_ids'][0])
    actual_input = input_tensor['input_ids']
    if prompt_size < window:
        actual_input = np.concatenate((tokenizer.bos_token_id*np.ones([1, window - prompt_size], dtype = 'int64'),
                                       actual_input), axis=1)
    if prompt_size + max_gen_tokens > total_sequence:
        print(\"错误:需要更长的总序列长度!\")
        return
    first_attention = np.concatenate((np.zeros([1, total_sequence - window], dtype = 'int64'),
                                      np.ones((1, window), dtype = 'int64')), axis=1)
    max_gen_tokens += prompt_size #我们需要在解析提示词的基础上进行生成
    inputs_names =[node.name for node in model.graph.input]
    output_names =[node.name for node in model.graph.output]
    n_heads = 8 #kvc的gqa头数
    inputs_dict = {}
    inputs_dict['input_ids'] = actual_input[:, :window].reshape(1, window).numpy()
    inputs_dict['attention_mask'] = first_attention
    index_pos = sum(first_attention[0])
    inputs_dict['position_ids'] = np.concatenate((np.zeros([1, total_sequence - index_pos], dtype = 'int64'), np.arange(index_pos, dtype = 'int64').reshape(1, index_pos)), axis=1)
    inputs_dict['tree_attention'] = np.triu(-65504*np.ones(total_sequence), k= 1).astype('float16').reshape(1, 1, total_sequence, total_sequence)
    for name in inputs_names:
        if name == 'input_ids' or name == 'attention_mask' or name == 'position_ids' or name == 'tree_attention': continue
        inputs_dict[name] = np.zeros([1, n_heads, context-window, 128], dtype=\"float16\")
    index = 0
    new_token = np.array([10])
    next_index = window
    old_j = 0
    total_input = actual_input.numpy()

    rt_session = onnxruntime.InferenceSession(model_path)
    ## 我们运行推理
    while next_index < max_gen_tokens:
        if new_token.any() == tokenizer.eos_token_id:
            break
        #推理
        output = rt_session.run(output_names, inputs_dict)
        outs_dictionary = {name: content for (name, content) in zip (output_names, output)}
        #我们为下一次推理准备输入
        for name in inputs_names:
            if name == 'input_ids':
                old_j = next_index
                if next_index < prompt_size:
                    if prompt_size - next_index >= window: next_index += window
                    else: next_index = prompt_size 
                    j = next_index - window
                else:
                    next_index +=1
                    j = next_index - window
                    new_token = outs_dictionary['logits'].argmax(-1).reshape(1, window)
                    total_input = np.concatenate((total_input, new_token[: , -1:]), axis = 1)
                inputs_dict['input_ids']= total_input[:, j:next_index].reshape(1, window)
            elif name == 'attention_mask':
                inputs_dict['attention_mask'] = np.concatenate((np.zeros((1, total_sequence-next_index), dtype = 'int64'), np.ones((1, next_index), dtype = 'int64')), axis=1)
            elif name == 'position_ids':
                    inputs_dict['position_ids'] = np.concatenate((np.zeros([1, total_sequence - next_index], dtype = 'int64'), np.arange(next_index, dtype = 'int64').reshape(1, next_index)), axis=1)
            elif name == 'tree_attention': continue
            else:
                old_name = name.replace(\"past_key_values\", \"present\")
                inputs_dict[name] = outs_dictionary[old_name][:, :, next_index-old_j:context-window+(next_index - old_j), :]

    answer = tokenizer.decode(total_input[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
    return answer

现在我们运行推理:

tokenizer = AutoTokenizer.from_pretrained(\"Esperanto/mistral-7b-Instruct-v0.2-kvc-fp16-onnx\")
model_path = \"mistral-7b-Instruct-v0.2-kvc-fp16-onnx/model.onnx\"

max_gen_tokens = 20    #我们想要生成的token数量
total_sequence = 128   #总序列长度
context = 1024         #扩展kvc的上下文长度
window = 16            #每次想要解析的token数量
messages = [
    {\"role\": \"system\", \"content\": \"You are a pirate chatbot who always responds in pirate speak!\"},
    {\"role\": \"user\", \"content\": \"Who are you?\"},
]

prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)

generated = generate_text(model_path, prompt, tokenizer, max_gen_tokens, total_sequence, window, context)
print(generated)

Esperanto/mistral-7b-Instruct-v0.2-kvc-fp16-onnx

作者 Esperanto

↓ 1 ♥ 0

创建时间: 2024-08-02 08:00:45+00:00

更新时间: 2024-12-11 12:32:14+00:00

在 Hugging Face 上查看

文件 (299)

.gitattributes
README.md
added_tokens.json
config.json
model.embed_tokens.weight
model.layers.0.input_layernorm.weight
model.layers.0.post_attention_layernorm.weight
model.layers.1.input_layernorm.weight
model.layers.1.post_attention_layernorm.weight
model.layers.10.input_layernorm.weight
model.layers.10.post_attention_layernorm.weight
model.layers.11.input_layernorm.weight
model.layers.11.post_attention_layernorm.weight
model.layers.12.input_layernorm.weight
model.layers.12.post_attention_layernorm.weight
model.layers.13.input_layernorm.weight
model.layers.13.post_attention_layernorm.weight
model.layers.14.input_layernorm.weight
model.layers.14.post_attention_layernorm.weight
model.layers.15.input_layernorm.weight
model.layers.15.post_attention_layernorm.weight
model.layers.16.input_layernorm.weight
model.layers.16.post_attention_layernorm.weight
model.layers.17.input_layernorm.weight
model.layers.17.post_attention_layernorm.weight
model.layers.18.input_layernorm.weight
model.layers.18.post_attention_layernorm.weight
model.layers.19.input_layernorm.weight
model.layers.19.post_attention_layernorm.weight
model.layers.2.input_layernorm.weight
model.layers.2.post_attention_layernorm.weight
model.layers.20.input_layernorm.weight
model.layers.20.post_attention_layernorm.weight
model.layers.21.input_layernorm.weight
model.layers.21.post_attention_layernorm.weight
model.layers.22.input_layernorm.weight
model.layers.22.post_attention_layernorm.weight
model.layers.23.input_layernorm.weight
model.layers.23.post_attention_layernorm.weight
model.layers.24.input_layernorm.weight
model.layers.24.post_attention_layernorm.weight
model.layers.25.input_layernorm.weight
model.layers.25.post_attention_layernorm.weight
model.layers.26.input_layernorm.weight
model.layers.26.post_attention_layernorm.weight
model.layers.27.input_layernorm.weight
model.layers.27.post_attention_layernorm.weight
model.layers.28.input_layernorm.weight
model.layers.28.post_attention_layernorm.weight
model.layers.29.input_layernorm.weight
model.layers.29.post_attention_layernorm.weight
model.layers.3.input_layernorm.weight
model.layers.3.post_attention_layernorm.weight
model.layers.30.input_layernorm.weight
model.layers.30.post_attention_layernorm.weight
model.layers.31.input_layernorm.weight
model.layers.31.post_attention_layernorm.weight
model.layers.4.input_layernorm.weight
model.layers.4.post_attention_layernorm.weight
model.layers.5.input_layernorm.weight
model.layers.5.post_attention_layernorm.weight
model.layers.6.input_layernorm.weight
model.layers.6.post_attention_layernorm.weight
model.layers.7.input_layernorm.weight
model.layers.7.post_attention_layernorm.weight
model.layers.8.input_layernorm.weight
model.layers.8.post_attention_layernorm.weight
model.layers.9.input_layernorm.weight
model.layers.9.post_attention_layernorm.weight
model.norm.weight
model.onnx ONNX
onnx__MatMul_10000
onnx__MatMul_10001
onnx__MatMul_10002
onnx__MatMul_10003
onnx__MatMul_10004
onnx__MatMul_10032
onnx__MatMul_10033
onnx__MatMul_10034
onnx__MatMul_10035
onnx__MatMul_10036
onnx__MatMul_10037
onnx__MatMul_10038
onnx__MatMul_10066
onnx__MatMul_10067
onnx__MatMul_10068
onnx__MatMul_10069
onnx__MatMul_10070
onnx__MatMul_10071
onnx__MatMul_10072
onnx__MatMul_10100
onnx__MatMul_10101
onnx__MatMul_10102
onnx__MatMul_10103
onnx__MatMul_10104
onnx__MatMul_10105
onnx__MatMul_10106
onnx__MatMul_10134
onnx__MatMul_10135
onnx__MatMul_10136
onnx__MatMul_10137
onnx__MatMul_10138
onnx__MatMul_10139
onnx__MatMul_10140
onnx__MatMul_10168
onnx__MatMul_10169
onnx__MatMul_10170
onnx__MatMul_10171
onnx__MatMul_10172
onnx__MatMul_10173
onnx__MatMul_10174
onnx__MatMul_10202
onnx__MatMul_10203
onnx__MatMul_10204
onnx__MatMul_10205
onnx__MatMul_10206
onnx__MatMul_10207
onnx__MatMul_10208
onnx__MatMul_10236
onnx__MatMul_10237
onnx__MatMul_10238
onnx__MatMul_10239
onnx__MatMul_10240
onnx__MatMul_10241
onnx__MatMul_10242
onnx__MatMul_10270
onnx__MatMul_10271
onnx__MatMul_10272
onnx__MatMul_10273
onnx__MatMul_10274
onnx__MatMul_10275
onnx__MatMul_10276
onnx__MatMul_10304
onnx__MatMul_10305
onnx__MatMul_10306
onnx__MatMul_10307
onnx__MatMul_10308
onnx__MatMul_10309
onnx__MatMul_10310
onnx__MatMul_10338
onnx__MatMul_10339
onnx__MatMul_10340
onnx__MatMul_10341
onnx__MatMul_10342
onnx__MatMul_10343
onnx__MatMul_10344
onnx__MatMul_10372
onnx__MatMul_10373
onnx__MatMul_10374
onnx__MatMul_10375
onnx__MatMul_10376
onnx__MatMul_10377
onnx__MatMul_10378
onnx__MatMul_10406
onnx__MatMul_10407
onnx__MatMul_10408
onnx__MatMul_10409
onnx__MatMul_10410
onnx__MatMul_10411
onnx__MatMul_10412
onnx__MatMul_10440
onnx__MatMul_10441
onnx__MatMul_10442
onnx__MatMul_10443
onnx__MatMul_10444
onnx__MatMul_10445
onnx__MatMul_10446
onnx__MatMul_10474
onnx__MatMul_10475
onnx__MatMul_10476
onnx__MatMul_10477
onnx__MatMul_10478
onnx__MatMul_10479
onnx__MatMul_10480
onnx__MatMul_10508
onnx__MatMul_10509
onnx__MatMul_10510
onnx__MatMul_10511
onnx__MatMul_10512
onnx__MatMul_10513
onnx__MatMul_10514
onnx__MatMul_10542
onnx__MatMul_10543
onnx__MatMul_10544
onnx__MatMul_10545
onnx__MatMul_10546
onnx__MatMul_10547
onnx__MatMul_10548
onnx__MatMul_10576
onnx__MatMul_10577
onnx__MatMul_10578
onnx__MatMul_10579
onnx__MatMul_10580
onnx__MatMul_10581
onnx__MatMul_10582
onnx__MatMul_10610
onnx__MatMul_10611
onnx__MatMul_10612
onnx__MatMul_10613
onnx__MatMul_10614
onnx__MatMul_10615
onnx__MatMul_10616
onnx__MatMul_10644
onnx__MatMul_10645
onnx__MatMul_10646
onnx__MatMul_10647
onnx__MatMul_10648
onnx__MatMul_10649
onnx__MatMul_10650
onnx__MatMul_10678
onnx__MatMul_10679
onnx__MatMul_10680
onnx__MatMul_10681
onnx__MatMul_10682
onnx__MatMul_10683
onnx__MatMul_10684
onnx__MatMul_10712
onnx__MatMul_10713
onnx__MatMul_10714
onnx__MatMul_10715
onnx__MatMul_10716
onnx__MatMul_10717
onnx__MatMul_10718
onnx__MatMul_10746
onnx__MatMul_10747
onnx__MatMul_10748
onnx__MatMul_10749
onnx__MatMul_10750
onnx__MatMul_9662
onnx__MatMul_9663
onnx__MatMul_9664
onnx__MatMul_9692
onnx__MatMul_9693
onnx__MatMul_9694
onnx__MatMul_9695
onnx__MatMul_9696
onnx__MatMul_9697
onnx__MatMul_9698
onnx__MatMul_9726
onnx__MatMul_9727
onnx__MatMul_9728
onnx__MatMul_9729
onnx__MatMul_9730
onnx__MatMul_9731
onnx__MatMul_9732
onnx__MatMul_9760
onnx__MatMul_9761
onnx__MatMul_9762
onnx__MatMul_9763
onnx__MatMul_9764
onnx__MatMul_9765
onnx__MatMul_9766
onnx__MatMul_9794
onnx__MatMul_9795
onnx__MatMul_9796
onnx__MatMul_9797
onnx__MatMul_9798
onnx__MatMul_9799
onnx__MatMul_9800
onnx__MatMul_9828
onnx__MatMul_9829
onnx__MatMul_9830
onnx__MatMul_9831
onnx__MatMul_9832
onnx__MatMul_9833
onnx__MatMul_9834
onnx__MatMul_9862
onnx__MatMul_9863
onnx__MatMul_9864
onnx__MatMul_9865
onnx__MatMul_9866
onnx__MatMul_9867
onnx__MatMul_9868
onnx__MatMul_9896
onnx__MatMul_9897
onnx__MatMul_9898
onnx__MatMul_9899
onnx__MatMul_9900
onnx__MatMul_9901
onnx__MatMul_9902
onnx__MatMul_9930
onnx__MatMul_9931
onnx__MatMul_9932
onnx__MatMul_9933
onnx__MatMul_9934
onnx__MatMul_9935
onnx__MatMul_9936
onnx__MatMul_9964
onnx__MatMul_9965
onnx__MatMul_9966
onnx__MatMul_9967
onnx__MatMul_9968
onnx__MatMul_9969
onnx__MatMul_9970
onnx__MatMul_9998
onnx__MatMul_9999
special_tokens_map.json
tokenizer.model
tokenizer_config.json