HuggingFace镜像/Mistral-7B-v0.1-openmind
模型介绍文件和版本分析
下载使用量0

Mistral-7B-v0.1 模型卡片

Mistral-7B-v0.1 大型语言模型(LLM)是一个拥有 70 亿参数的预训练生成式文本模型。 在我们测试的所有基准上,Mistral-7B-v0.1 的性能均优于 Llama 2 13B。

有关此模型的完整详情,请阅读我们的论文和发布博客文章。

模型架构

Mistral-7B-v0.1 是一个Transformer模型,其架构选择如下:

  • 分组查询注意力(Grouped-Query Attention)
  • 滑动窗口注意力(Sliding-Window Attention)
  • 字节回退 BPE 分词器(Byte-fallback BPE tokenizer)

在 Openmind 中的使用

from openmind import AutoModelForCausalLM, AutoTokenizer, pipeline , is_torch_npu_available
from openmind_hub import snapshot_download
import torch.nn.functional as F
from torch import Tensor
import openmind
import torch
import argparse
import time

# Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
    token_embeddings = model_output[0]  # First element of model_output contains all token embeddings
    input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
    return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--model_name_or_path",
        type=str,
        help="Path to model",
        default="jeffding/Mistral-7B-v0.1-openmind",
    )
    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    model_path = args.model_name_or_path

    if is_torch_npu_available():
        device = "npu:0"
    else:
        device = "cpu"
        
    # Load model from HuggingFace Hub
    model = AutoModelForCausalLM.from_pretrained(model_path,
                                             device_map=device,
                                             trust_remote_code=False,
                                             revision="main")

    tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=True)
    
    start_time = time.time()
    prompt = "Tell me about AI"
    prompt_template=f'''<s>[INST] {prompt} [/INST]
    '''

    print("\n\n*** Generate:")

    input_ids = tokenizer(prompt_template, return_tensors='pt').input_ids.to(device)
    output = model.generate(inputs=input_ids, temperature=0.7, do_sample=True, top_p=0.95, top_k=40, max_new_tokens=512)
    print(tokenizer.decode(output[0]))

    # Inference can also be done using transformers' pipeline

    print("*** Pipeline:")
    pipe = pipeline(
        "text-generation",
        model=model,
        tokenizer=tokenizer,
        max_new_tokens=512,
        do_sample=True,
        temperature=0.7,
        top_p=0.95,
        top_k=40,
        repetition_penalty=1.1
    )

    print(pipe(prompt_template)[0]['generated_text'])
    
    end_time = time.time()
    print(f"硬件环境:{device},推理执行时间:{end_time - start_time}秒")
    
if __name__ == "__main__":
    main()

故障排除

  • 如果你遇到以下错误:
KeyError: 'mistral'
  • 或者:
NotImplementedError: Cannot copy out of meta tensor; no data!

请确保您使用的是稳定版本的Transformers,4.34.0或更新版本。

注意事项

Mistral 7B是一个预训练基础模型,因此不具备任何内容审核机制。

Mistral AI团队

Albert Jiang、Alexandre Sablayrolles、Arthur Mensch、Chris Bamford、Devendra Singh Chaplot、Diego de las Casas、Florian Bressand、Gianna Lengyel、Guillaume Lample、Lélio Renard Lavaud、Lucile Saulnier、Marie-Anne Lachaux、Pierre Stock、Teven Le Scao、Thibaut Lavril、Thomas Wang、Timothée Lacroix、William El Sayed。