• PaddleNLP使用Vicuna


    LLaMA 模型

    LLaMa 是一个大型语言模型,由 Meta 开源。
    
    它的全称是 Large Language Model Meta AI,参数量从 70 亿到 650 亿不等。
    
    例如,130 亿参数的 LLaMA 模型在大多数基准上可以胜过参数量达 1750 亿的 GPT-3,而且可以在单块 V100 GPU 上运行。
    
    而最大的 650 亿参数的 LLaMA 模型可以媲美谷歌的 Chinchilla-70B 和 PaLM-540B。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Vicuna 模型

    Vicuna 是一个由 UC 伯克利、CMU、斯坦福等机构的学者联手发布的最新开源大模型。
    
    基于 Meta 开源的 LLaMA 大模型,使用 ShareGPT 平台上的用户共享对话数据微调而来。
    
    包含 7B 和 13B 两个型号的开源预训练模型。
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    下载模型

    # 下载 Vicuna 7B
    # !git lfs clone http://git.aistudio.baidu.com/180581/vicuna-7b-v1.1.git
    
    # 下载 Vicuna 13B
    !git lfs clone http://git.aistudio.baidu.com/180581/vicuna-13b-v1.1.git
    
    • 1
    • 2
    • 3
    • 4
    • 5

    开发环境

    !pip install --pre --upgrade paddlenlp -f https://www.paddlepaddle.org.cn/whl/paddlenlp.html --user
    !pip install paddlepaddle-gpu==0.0.0.post112 -f https://www.paddlepaddle.org.cn/whl/linux/gpu/develop.html --user
    
    • 1
    • 2

    代码

    import os
    import glob
    import paddle
    
    from tqdm import tqdm
    from paddlenlp.transformers import LlamaForCausalLM, LlamaConfig, LlamaTokenizer
    
    pattern = 'paddle-model-?????-of-?????.pdparams'
    
    # Vicuna 7B
    # ckpt_dir = 'vicuna-7b-v1.1'
    # config_dict =  {
    #     "hidden_size": 4096,
    #     "initializer_range": 0.02,
    #     "intermediate_size": 11008,
    #     "max_position_embeddings": 2048,
    #     "model_type": "llama",
    #     "num_attention_heads": 32,
    #     "num_hidden_layers": 32,
    #     "rms_norm_eps": 1e-06,
    #     "vocab_size": 32000,
    #     "bos_token_id": 1,
    #     "eos_token_id": 2,
    #     "pad_token_id": 0,
    #     "use_cache": True,
    #     "use_recompute": False,
    #     "use_flash_attention": False,
    # }
    
    # Vicuna 13B
    ckpt_dir = 'vicuna-13b-v1.1'
    config_dict =  {
        "hidden_size": 5120,
        "initializer_range": 0.02,
        "intermediate_size": 13824,
        "max_position_embeddings": 2048,
        "model_type": "llama",
        "num_attention_heads": 40,
        "num_hidden_layers": 40,
        "rms_norm_eps": 1e-06,
        "vocab_size": 32000,
        "bos_token_id": 1,
        "eos_token_id": 2,
        "pad_token_id": 0,
        "use_cache": True,
        "use_recompute": False,
        "use_flash_attention": False,
    }
    
    paddle.set_default_dtype('float16')
    
    tokenizer = LlamaTokenizer.from_pretrained(ckpt_dir)
    
    config = LlamaConfig(**config_dict)
    
    model = LlamaForCausalLM(config)
    model.eval()
    
    for name, layer in model.named_sublayers():
        if 'rotary_emb' in name:
            layer.inv_freq = layer.inv_freq.cast(paddle.float32)
    
    paddle.device.cuda.empty_cache()
    
    
    for file_path in tqdm(glob.glob(os.path.join(ckpt_dir, pattern))):
        params = paddle.load(file_path)
        assert model.set_dict(params)[1] == [], 'Load error.'
        del params
        paddle.device.cuda.empty_cache()
    
    input_text = input('USER: ')
    prompt = f'''USER: {input_text}\n\nASSISTANT: '''
    with paddle.no_grad():
        with paddle.amp.auto_cast(False, level='O2', dtype='float16'):
            while True:
                if input_text == 'exit':
                    break
                inputs = tokenizer(
                    prompt, 
                    return_tensors="pd", 
                    return_attention_mask=True,
                    return_position_ids=True
                )
                outputs = model.generate(
                    input_ids=inputs.input_ids, 
                    attention_mask=inputs.attention_mask, 
                    position_ids=inputs.position_ids, 
                    max_length=2048-inputs.input_ids.shape[1], 
                    min_length=0, 
                    decode_strategy="sampling",
                    temperature=0.8, 
                    top_k=40, 
                    top_p=0.95, 
                    repetition_penalty=1.1,
                    bos_token_id=tokenizer.bos_token_id,
                    eos_token_id=tokenizer.eos_token_id,
                    pad_token_id=tokenizer.pad_token_id,
                    use_cache=True, 
                    use_fast=True, 
                    use_fp16_decoding=True)
                response = tokenizer.decode(outputs[0][0], skip_special_tokens=True)
                print('ASSISTANT: ' + response)
                input_text = input('USER: ')
                prompt += f'''{response}\n\nUSER: {input_text}\n\nASSISTANT: '''
                del inputs
                del outputs
                del response
                paddle.device.cuda.empty_cache()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
  • 相关阅读:
    药品和药企信息接口
    高考志愿选专业,如何分析自己的兴趣爱好?
    (附源码)APP+springboot个人健康管理 毕业设计 202031
    Apache和Tomcat的区别
    Java核心知识体系3:异常机制详解
    docker容器访问宿主机mysql数据库
    WPF MVVM
    Postgresql 模块插件之pg_stat_statements
    Word控件Spire.Doc 【段落处理】教程(二):C#/VB.NET:在 Word 中设置段落缩进
    抖音wss弹幕RPC
  • 原文地址:https://blog.csdn.net/qq236237606/article/details/132652307