\

在IPU上使用BART Large进行文本摘要

作者:

分享  

Share on weixin
Share on weibo
Share on linkedin

订阅

文本摘要是人工智能自然语言处理 (NLP) 在实际应用中的最佳范例之一。

每天都会产生大量的信息,无论是在商业领域还是在科学研究等其他领域,快速理解、评估和处理这些信息的能力都极具价值。

摘要是在保留重要信息的同时生成文档的较短版本。从根本上说,它包括从原始输入中提取文本,然后生成描述原始文本要点的新文本。在某些情况下,这两个部分可能由不同的人工智能模型处理。

我们将在本文中演示如何在Graphcore(拟未)IPU上使用BART-Large运行整个摘要的过程。

什么是BART?为什么它适用于文本摘要?

在2018年,谷歌推出了BERT(基于Transformer的双向编码器表示),它被定义为“语言理解”模型,适用于广泛的应用,例如情感分析、文本分类和问答。当时并没有明确将摘要作为一个用例。

同年,Open-AI也在自然语言理解领域取得了进一步的突破,提出了生成式预训练(GPT)的概念。

到了2019年末,Facebook的人工智能研究人员提出了一种将双向编码器(如BERT)和自回归解码器(如GPT)结合起来的方法,并将其命名为BART,即双向自回归变换器。

根据原论文中的介绍,预训练的创新之处在于一种新的插入方案,即在随机重新排列原始句子顺序时进行插入。作者认为当对文本生成和理解任务进行微调时,BART特别有效——这两者都是文本摘要所需要的。

在Graphcore IPU上使用Hugging Face pipeline进行文本摘要

BART是Optimum Graphcore支持的众多NLP模型之一,它是Hugging Face和Graphcore IPU之间的接口。

我们演示了在Graphcore IPU上运行BART-Large推理的文本摘要任务。

Bart-Large

在IPU上进行文本摘要

对于下面的每个代码块,您只需点击一下就能在 Paperspace中运行该代码块,并对代码/参数进行相关修改。我们将在本博客末尾介绍如何在Paperspace Gradient Notebooks以外的环境中运行该过程。

安装依赖项

%pip install optimum-graphcore==0.7.1 wikipedia graphcore-cloud-tools[logger]@git+https://github.com/graphcore/graphcore-cloud-tools

%load_ext graphcore_cloud_tools.notebook_logging.gc_logger
import os

exec_cache_dir = os.getenv("POPLAR_EXECUTABLE_CACHE_DIR", "/tmp/exe_cache/")

模型准备

我们从准备模型开始。首先,我们要定义在IPU上运行模型所需的配置。IPUConfig是一个指定属性和配置参数的类,用于编译模型并将其放到设备上:

from optimum.graphcore import IPUConfig

ipu_config = IPUConfig(
    layers_per_ipu=[12, 12],
    matmul_proportion=0.15,
    executable_cache_dir=exec_cache_dir,
    inference_parallelize_kwargs={
        "max_length": 150,
        "num_beams": 3,
        "use_encoder_output_buffer": True,
        "on_device_generation_steps": 16,
    }
)

接下来,让我们从optimum.graphcore中导入pipeline,创建我们的摘要pipeline:

from optimum.graphcore import pipeline

summarizer = pipeline(
    "summarization",
    model="facebook/bart-large-cnn",
    tokenizer="facebook/bart-large-cnn",
    ipu_config=ipu_config,
    config="facebook/bart-large-cnn",
    max_input_length=1024,
    truncation=True
)

我们定义了一个输入来测试模型。

input_test = 'In computing, a compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name "compiler" is primarily used for programs that translate source code from a high-level programming language to a low-level programming language (e.g. assembly language, object code, or machine code) to create an executable program.'
input_test

第一次运行的编译时间:~ 2:30

%%time
summarizer(input_test, max_length=150, num_beams=3)

更快的童话故事

第一次调用pipeline的速度有点慢,需要几秒钟才能提供答案。这种情况是由于在第一次调用时对模型进行了编译。在后续的提示中,速度会快得多:

the_princess_and_the_pea = 'Once upon a time there was a prince who wanted to marry a princess; but she would have to be a real princess. He travelled all over the world to find one, but nowhere could he get what he wanted. There were princesses enough, but it was difficult to find out whether they were real ones. There was always something about them that was not as it should be. So he came home again and was sad, for he would have liked very much to have a real princess. One evening a terrible storm came on; there was thunder and lightning, and the rain poured down in torrents. Suddenly a knocking was heard at the city gate, and the old king went to open it. It was a princess standing out there in front of the gate. But, good gracious! what a sight the rain and the wind had made her look. The water ran down from her hair and clothes; it ran down into the toes of her shoes and out again at the heels. And yet she said that she was a real princess. Well, we\'ll soon find that out, thought the old queen. But she said nothing, went into the bed-room, took all the bedding off the bedstead, and laid a pea on the bottom; then she took twenty mattresses and laid them on the pea, and then twenty eider-down beds on top of the mattresses. On this the princess had to lie all night. In the morning she was asked how she had slept. "Oh, very badly!" said she. "I have scarcely closed my eyes all night. Heaven only knows what was in the bed, but I was lying on something hard, so that I am black and blue all over my body. It\'s horrible!" Now they knew that she was a real princess because she had felt the pea right through the twenty mattresses and the twenty eider-down beds. Nobody but a real princess could be as sensitive as that. So the prince took her for his wife, for now he knew that he had a real princess; and the pea was put in the museum, where it may still be seen, if no one has stolen it. There, that is a true story.'
the_princess_and_the_pea
%%time
summarizer(the_princess_and_the_pea, max_length=150, num_beams=3)

维基百科文章摘要

现在让我们使用维基百科API来搜索一些可以总结的长文本:

import wikipedia

# TRY IT YOURSELF BY CHANGING THE PAGE TITLE BELOW
page_title = "Queen (band)"
text = wikipedia.page(page_title).content
text
%%time
summarizer(
    text,  # NOTE: the input text would be truncated to max_input_length=1024
    max_length=150,
    num_beams=3,
)

医疗健康记录摘要

摘要任务也可用于总结医疗健康记录(MHR)。让我们导入一个包含一些医学样本的开源数据集。

from datasets import load_dataset

dataset = load_dataset("rungalileo/medical_transcription_40")
dataset

我们将重点放在标记为“文本”的医疗报告上,并从训练数据集中随机选择一个患者ID。

import random

# RUN THIS CELL AGAIN TO SELECT ANOTHER REPORT
random_patient_id = random.randint(0, len(dataset["train"]))

exemplary_medical_report = dataset["train"][random_patient_id]["text"]
exemplary_medical_report
%%time
summarizer(exemplary_medical_report, max_length=150, num_beams=3)

在非Paperspace环境中的IPU上运行BART-Large

使用其他IPU硬件运行演示,需要启用Poplar SDK并安装相关的PopTorch wheel。有关如何启用Poplar SDK和安装PopTorch wheel的详细信息,请参阅系统的入门指南

More Posts

卢涛:后登纳德时代,IPU架构引领Transformer向高阶版演进

GACS 2023 | IPU:赋能生成式AI,不止生成式AI

Graphcore携手Pienso荣获CogX最佳创新类别的自然语言处理奖

Graphcore加入PyTorch基金会

促进低精度数字格式使用,Graphcore发布全新Unit Scaling库

情人节之“AI”跨山海——拟未“AI”的故事绘画连载(三)

获取最新的GRAPHCORE资讯

在下方注册以获取最新的资讯和更新:




    获取最新的GRAPHCORE资讯

    在下方注册以获取最新的资讯和更新: