ChatGPT最令人兴奋的开源聊天机器人替代品之一——由Graphcore(拟未) IPU驱动的OpenAssistant的OASST1微调版Pythia-12B现在可以在Paperspace上免费运行。作为真正的开源模型,可以不受限制地商用。
http://ipu.dev/y86FIU
oasst-sft-4-pythia12b是EleutherAI的Pythia模型系列的一个变体,使用开放助理对话(OASST1)数据集进行微调(fine tune),这是一个众包的“人类生成的、人类注释的助理式对话语料”。
OASST1数据集由35种不同语言的161,443条信息组成,注释了461,292个质量等级,形成了超过10,000个完全注释的对话树。
Paperspace上运行OASST1微调版Pythia-12B推理
OpenAssistant的OASST1微调版Pythia可以通过Paperspace Gradient notebook在Graphcore IPU上轻松运行。新用户可以通过Paperspace的六小时免费试用,在IPU-POD
4
上尝试Pythia。对于更高的性能实现,你可以扩展到IPU-POD
16


该notebook指导你创建和配置一个推理传递途径,并运行该传递途径以建立一个逐次的对话。


因为OpenAssistant模型使用与Dolly相同的Pythia-12B底层模型,所以我们使用Dolly传递途径来运行它。
http://ipu.dev/y86FIU
让我们从加载推理配置开始。我们使用与Dolly相同的配置文件,并手动修改训练集的词库大小,这是模型图之间的唯一区别。一个适合你的实例的配置将被自动选择。
from utils.setup import dolly_config_setup
config_name = "dolly_pod4" if number_of_ipus == 4 else "dolly_pod16"
config, *_ = dolly_config_setup("config/inference.yml", "release", config_name)
# Update vocab size for oasst-sft-4-pythia-12b-epoch-3.5 - 50288 rather than 50280
config.model.embedding.vocab_size = 50288
config
接下来,我们要创建我们的推理途径。在这里,我们定义了最大的序列长度和最大的微批尺寸。在IPU上执行一个模型之前,需要通过编译将其变成可执行的格式。这将在流水线创建时发生。所有的输入形状在编译前必须是已知的,所以如果最大序列长度或微批尺寸改变了,途径将需要被重新编译。


选择一个较长的序列长度或较大的批处理量将使用更多的IPU内存。这意味着,增加一个可能需要你减少另一个。


这个单元大约需要18分钟来完成,其中包括下载模型权重。
import api
# changing these parameters will trigger a recompile.
sequence_length = 512  # max 2048
micro_batch_size = 1
# The pipeline is set to load the OpenAssistant checkpoint rather than the default Dolly one
# We override the Dolly prompt templating by specifying a prompt_format. Setting the format to
# just echo the instruction means that the pipeline does no formatting and it is up to the
# application to provide correctly templated prompts
# We set the text string that OpenAssistant uses to mark that it has finished generation, which
# is different from the Dolly one
oasst_pipeline = api.DollyPipeline(
    config,
    sequence_length=sequence_length,
    micro_batch_size=micro_batch_size,
    hf_dolly_checkpoint="OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
    prompt_format="{instruction}",
    end_key="<|endoftext|>",
)
调用你刚刚创建的oasst_pipeline对象,从提示中生成文本。
为了制作一个聊天机器人,我们将接受用户的输入并将其反馈给模型。为了使notebook能够自动测试,我们创建一个类似于Python内置函数input()的函数,收集用户的输入并返回,但如果设置了测试环境变量EXAMPLE_PROMPTS,它将从该变量返回罐装输入。该变量应该被设置为一个字符串的JSON列表,例如:
export EXAMPLE_PROMPTS='["Are you related to Dolly?", "Ah OK. How many islands are there in Scotland?"]'
import os
import json
example_prompts = os.getenv("EXAMPLE_PROMPTS")
if example_prompts is not None:
    example_prompts = json.loads(example_prompts)
# Get input from user like input() but with possibility of automation via EXAMPLE_PROMPTS environment variable
def auto_input(prompt: str) -> str:
    if example_prompts is None:
        return input(prompt)
    auto_in = example_prompts.pop(0) if example_prompts != [] else ""
    print(prompt, "AUTO", auto_in)
    return auto_in
聊天机器人的对话是由用户输入的若干回合和模型写出的回复建立起来的。随着对话的发展,提示应该被逐次扩展,所以模型可以获得完整的背景。
该模型已经在一个特定的提示模板上进行了训练,以代表对话的建立过程:
<|prompter|>user1<|endoftext|> <|assistant|>reply1<|endoftext|> <|prompter|>user2<|endoftext|> <|assistant|>...
途径调用有一些可选的参数,你可以用来控制生成行为:
  • 温度——表示你是否想要更多或更少的创造性输出。1.0的值对应于模型的默认行为。比这更小的值会强调下一个标记的分布,使模型更有可能挑选一个高度可能的下一个标记。0.0的值意味着模型将总是选择最可能的标记。温度大于1.0会使下一个符号分布变平,使更多不寻常的下一个符号更有可能。温度必须是零或正数。

  • k——表示只对最高的k个可能的标记进行抽样。这被称为 "top k "采样。设置为0可以禁用top k采样,从所有可能的标记中采样。k的阈值必须在0和config.model.embedding.vocab_size最大值之间,即50288。默认值是5。

  • output_length——设置一个最大的输出长度(tokens)。生成通常在模型生成其end_key文本时停止,但可以通过指定这个选项使其在这之前停止。None的值将禁用该限制。默认值是 "无"。
你可以从任何用户输入开始。例如,“还有哪些动物与羊驼相似?”
import logging
# Conduct a complete conversation - with ability to set pipeline optional parameters
def chat(temperature=None, top_k=None, output_length=None):
    options = {}
    if temperature is not None:
        options["temperature"] = temperature
    if top_k is not None:
        options["k"] = top_k
    if output_length is not None:
        options["output_length"] = output_length
    # Suppress INFO logging to make a better interactive chat display
    logging.disable(logging.INFO)
    print("To complete the chat, enter an empty prompt")
    prompt = ""
    while True:
        user_input = auto_input("Prompter:")
        if user_input == "":
            break
        prompt += f"<|prompter|>{user_input}<|endoftext|><|assistant|>"
        chat_output = oasst_pipeline(prompt, **options)[0]
        prompt += chat_output + "<|endoftext|>"
    # Restore logging to what it was before
    logging.disable(logging.NOTSET)
chat(temperature=0.0)
# Note the first time you run this cell and enter a prompt, there will be a delay of ~1 minute where
# nothing appears to happen. This is where the server is attaching to the IPUs
请看下面的图片,这是一个使用不同提示的模型的行动例子。
记住,当你完成后要分离你的途径以释放资源:
oasst_pipeline.detach()
在非Paperspace IPU环境下运行OASST1微调版Pythia 
要在Paperspace以外的地方使用IPU硬件运行演示,你需要启用Poplar SDK。
关于如何启用Poplar SDK的细节,请参考您的系统的入门指南[1]。也请参考Jupyter快速入门指南[2],了解如何设置Jupyter,以便能够在远程IPU机器上运行这个notebook。
[1]https://docs.graphcore.ai/en/latest/getting-started.html#getting-started
[2]https://docs.graphcore.ai/projects/jupyter-notebook-quick-start/en/latest/index.html
获取更多Graphcore资讯,阅读深度技术文章,并与其他创新者们一起交流,请至中国官网graphcore.cn,以及关注Graphcore微信、微博和知乎创新社区。
Graphcore中国官网
Graphcore官方微信
Graphcore微博创新社区
Graphcore知乎创新社区
点击阅读原文,查看英文blog。
继续阅读
阅读原文