LLMs之Grok-1:run.py文件解读—运行语言模型实现推理—即基于用户的输入文本利用grok_1语言模型来生成文本

LLMs之Grok-1:run.py文件解读---运行语言模型实现推理---即基于用户的输入文本利用grok_1语言模型来生成文本

目录

run.py文件解读---运行语言模型实现推理---即基于用户的输入文本利用grok_1语言模型来生成文本

概述

[1、加载预训练的语言模型 grok_1](#1、加载预训练的语言模型 grok_1)

1.1、定义模型的配置

2、定义并初始化推理运行器

[2.1、创建一个 InferenceRunner 对象(用于运行模型推理)](#2.1、创建一个 InferenceRunner 对象(用于运行模型推理))

[2.2、调用 inference_runner.initialize() 方法初始化推理运行器。](#2.2、调用 inference_runner.initialize() 方法初始化推理运行器。)

[2.3、调用 inference_runner.run() 方法运行模型推理并获取生成器。](#2.3、调用 inference_runner.run() 方法运行模型推理并获取生成器。)

3、模型生成

全部代码


run.py文件解读 ---运行语言模型实现推理---即基于用户的输入文本 利用grok_1语言模型来生成文本

源码地址grok-1/run.py at main · xai-org/grok-1 · GitHub

概述

这段代码使用了一个预训练的语言模型 grok_1_model 来生成文本。代码首先定义了模型的配置,然后创建了一个 InferenceRunner 对象来运行模型推理。最后,代码定义了一个输入字符串,并使用 sample_from_model 函数从模型中获取一个样本,将其打印出来。

1、加载预训练的语言模型 grok_1

1.1、 定义模型的配置

定义一个名为 grok_1_model 的 LanguageModelConfig 对象,该对象包含有关模型配置的详细信息,例如词汇表大小、序列长度、嵌入层初始化比例、输出和嵌入层的乘数比例等。模型的架构是一个 TransformerConfig 对象,其中包括了嵌入大小、扩展因子、键大小、头数量、层数、注意力输出乘数等参数。

2、定义并 初始化推理运行器

2.1、 创建一个 InferenceRunner 对象 ( 用于运行模型推理 )

InferenceRunner 接受一个 ModelRunner 对象作为参数,该对象包含了模型配置、批处理大小、检查点路径等信息。InferenceRunner 还需要指定一些其他参数,如名称、加载路径、分词器路径、本地和跨主机配置等。

2.2、 调用 inference_runner.initialize() 方法初始化推理运行器。

2.3、 调用 inference_runner.run() 方法运行模型推理并获取生成器。

3、模型生成

定义一个输入字符串 inp,然后使用 sample_from_model 函数从生成器中获取一个样本,并将其打印出来。

全部代码

python 复制代码
# Copyright 2024 X.AI Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from model import LanguageModelConfig, TransformerConfig, QuantizedWeight8bit as QW8Bit
from runners import InferenceRunner, ModelRunner, sample_from_model


CKPT_PATH = "./checkpoints/"


def main():
    grok_1_model = LanguageModelConfig(
        vocab_size=128 * 1024,
        pad_token=0,
        eos_token=2,
        sequence_len=8192,
        embedding_init_scale=1.0,
        output_multiplier_scale=0.5773502691896257,
        embedding_multiplier_scale=78.38367176906169,
        model=TransformerConfig(
            emb_size=48 * 128,
            widening_factor=8,
            key_size=128,
            num_q_heads=48,
            num_kv_heads=8,
            num_layers=64,
            attn_output_multiplier=0.08838834764831845,
            shard_activations=True,
            # MoE.
            num_experts=8,
            num_selected_experts=2,
            # Activation sharding.
            data_axis="data",
            model_axis="model",
        ),
    )
    inference_runner = InferenceRunner(
        pad_sizes=(1024,),
        runner=ModelRunner(
            model=grok_1_model,
            bs_per_device=0.125,
            checkpoint_path=CKPT_PATH,
        ),
        name="local",
        load=CKPT_PATH,
        tokenizer_path="./tokenizer.model",
        local_mesh_config=(1, 8),
        between_hosts_config=(1, 1),
    )
    inference_runner.initialize()
    gen = inference_runner.run()

    inp = "The answer to life the universe and everything is of course"
    print(f"Output for prompt: {inp}", sample_from_model(gen, inp, max_len=100, temperature=0.01))


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    main()
相关推荐
Langchain1 小时前
不可错过!CMU最新《生成式人工智能大模型》课程:从文本、图像到多模态大模型
人工智能·自然语言处理·langchain·大模型·llm·大语言模型·多模态大模型
AAI机器之心3 小时前
LLM大模型:开源RAG框架汇总
人工智能·chatgpt·开源·大模型·llm·大语言模型·rag
网安-搬运工9 小时前
RAG再总结之如何使大模型更好使用外部数据:四个不同层级及查询-文档对齐策略
人工智能·自然语言处理·大模型·llm·大语言模型·ai大模型·rag
我爱学Python!1 天前
基于 LangChain 的自动化测试用例的生成与执行
人工智能·自然语言处理·langchain·自动化·llm·测试用例·大语言模型
deephub2 天前
SCoRe: 通过强化学习教导大语言模型进行自我纠错
人工智能·深度学习·自然语言处理·大语言模型
AI_小站3 天前
图解大模型计算加速系列:vLLM源码解析1,整体架构
人工智能·深度学习·架构·llm·大语言模型·ai大模型·vllm
少喝冰美式4 天前
深度学习 Transformer 的标签平滑(Label Smoothing)
人工智能·深度学习·llm·transformer·大语言模型·ai大模型·计算机技术
网安-搬运工5 天前
一文讲明白大模型分布式逻辑(从GPU通信原语到Megatron、Deepspeed)
人工智能·分布式·语言模型·自然语言处理·大模型·大语言模型·ai大模型
叶庭云6 天前
了解针对基座大语言模型(类似 ChatGPT 的架构,Decoder-only)的重头预训练和微调训练
chatgpt·微调·大语言模型·预训练·基座模型
余生H7 天前
前端大模型入门:使用Transformers.js手搓纯网页版RAG(二)- qwen1.5-0.5B - 纯前端不调接口
前端·javascript·人工智能·大语言模型·rag·端侧大模型·webml