python - 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 的论点不熟悉,以获得更快的代码。谢谢您的帮助!
解决方案
您应该编写 cuda.jit 装饰器期望的代码,如链接中所示,或者您可以尝试使用cuDF.apply。但是,我不确定 cuDF 是否支持您在 lambda 中的函数。
或者,您可以使用带有参数的Stanza Pipeline API 。use_gpu=True
请参阅基本示例部分,您应该注意其中的注释。
推荐阅读
- ruby-on-rails - 将 AWS Lambda rails 代码迁移到 AWS Fargate
- sql - SQL WHERE 变量的两个值都存在
- c# - 来自用户的整数输入数组(初学者 C#)
- excel - 如何在 MS Excel 中编写基于多个条件的数组公式?
- javascript - Vuetify - 更改 v-text-field type=date 图标的默认颜色
- python - 上传到板时从 Arduino-Cli 调用附加脚本
- dependency-injection - Azure 函数 V3 无法使用 EF 核心 5.0-rc1 加载文件或程序集 Microsoft.Extensions.DependencyInjection.Abstractions,版本 = 5.0.0.0
- javascript - 反应“无法在'MediaRecorder'上执行'stop':MediaRecorder的状态是'inactive'。”
- javascript - 无法在 Nodejs 中写入 JSON
- c# - 在 C# 控制台应用程序中显示 SQL Server 表内容