首页 > 解决方案 > GPU 上的应用功能 colab

问题描述

我在我的代码上使用了这个应用函数:

def entities_extraction(text):
    doc = nlp(text)
    entities= [ent.text for sentence in doc.sentences for ent in sentence.entities if ent.type in {"PERSON", "ORG", "GPE", "NORP", "FAC", "LOC", "PRODUCT", "EVENT", "WORK_OF_ART", "LAW", "LANGUAGE", "MISC"}]
    return entities

df["entities"] = df['text'].progress_apply(lambda x: entities_extraction(x))

问题是,目前太慢了(需要将近12个小时)

所以我尝试修改它以使用colab gpu:

@cuda.jit
def entities_extraction(text):
    doc = nlp(text)
    entities= [ent.text for sentence in doc.sentences for ent in sentence.entities if ent.type in {"PERSON", "ORG", "GPE", "NORP", "FAC", "LOC", "PRODUCT", "EVENT", "WORK_OF_ART", "LAW", "LANGUAGE", "MISC"}]
    return entities

但我得到这个错误:

ValueError: 
Kernel launch configuration was not specified. Use the syntax:

kernel_function[blockspergrid, threadsperblock](arg0, arg1, ..., argn)

See https://numba.pydata.org/numba-doc/latest/cuda/kernels.html#kernel-invocation for help.

您知道如何解决它,或者您在应用函数上有更好的 gpu 实现吗?

抱歉,如果我犯了很多错误,我对 gpu 的论点不熟悉,以获得更快的代码。谢谢您的帮助!

标签: pythoncudagpugoogle-colaboratorystanford-nlp

解决方案


您应该编写 cuda.jit 装饰器期望的代码,如链接中所示,或者您可以尝试使用cuDF.apply。但是,我不确定 cuDF 是否支持您在 lambda 中的函数。

或者,您可以使用带有参数的Stanza Pipeline API 。use_gpu=True请参阅基本示例部分,您应该注意其中的注释。


推荐阅读