常见问题与已知问题#
何时应该使用cudf.pandas而不是直接使用cuDF库?#
cudf.pandas 是在 GPU 上运行 pandas 代码的最快和最简单的方法。然而,在某些情况下,应考虑直接使用 cuDF 库。
cuDF 实现了 pandas API 的一个子集,而
cudf.pandas会根据需要自动回退到 pandas。如果你能编写仅使用 cuDF 支持的操作的代码,直接使用 cuDF 将带来性能提升。cuDF 确实提供了一些 pandas 没有的函数和方法。例如,cuDF 有一个
.list访问器 用于处理类似列表的数据。如果你需要访问 cuDF 中的额外功能,你需要直接使用 cuDF 包。
这与pandas的匹配程度如何?#
你可以使用100%的pandas API,大多数功能将与pandas完全相同。
cudf.pandas 已经通过了整个 pandas 单元测试套件的测试。
目前,我们已经通过了超过 187,000 个单元测试中的 93%,目标是达到 100%。测试失败通常发生在边缘情况下,并且由于 cuDF 和 pandas 之间的少量行为差异。您可以在 已知限制 中了解更多关于这些边缘情况的信息。
我们还运行夜间测试,以跟踪cudf.pandas与其他第三方库之间的交互。请参阅第三方库兼容性。
如何判断cudf.pandas是否处于激活状态?#
无论是否使用cudf.pandas,您都不需要以不同的方式编写任何代码。您应该使用pandas,一切应该都能正常工作。
然而,在测试和开发过程中的某些情况下,您可能希望明确验证cudf.pandas是否处于活动状态。为此,请在代码中打印pandas模块并查看输出;它应该看起来像这样:
%load_ext cudf.pandas
import pandas as pd
print(pd)
<module 'pandas' (ModuleAccelerator(fast=cudf, slow=pandas))>
哪些函数将在GPU上运行?#
通常,cudf.pandas 会加速 cuDF API 中的所有功能在 GPU 上的运行。但也有一些例外。例如,某些函数由 cuDF 在 GPU 上加速,但不支持所有关键字参数的组合。在不支持的关键字参数的情况下,cuDF 无法提供 GPU 加速,cudf.pandas 将回退到 CPU。
评估哪些函数在GPU上运行的最准确方法是尝试在运行代码时使用cudf.pandas的分析功能。分析器将指示哪些函数在GPU / CPU上运行。为了提高性能,尝试仅使用可以完全在GPU上运行的功能。这有助于减少需要回退到CPU的内存传输次数。
如何通过cudf.pandas提高我的工作流程性能?#
大多数工作流程在使用cudf.pandas时会看到显著的性能提升。然而,有时事情可能会比预期的慢。首先,需要注意的是,GPU擅长并行处理大量数据。对于小数据量,由于数据传输的成本,GPU可能比CPU慢。cuDF在处理大量数据行时表现最佳。作为一个非常粗略的经验法则,cudf.pandas在处理超过10,000到100,000行数据的工作流程中表现出色,具体取决于算法、数据类型和其他因素。大小为几GB和/或有数百万行的数据集非常适合cudf.pandas。
以下是一些提高工作流性能的更多技巧:
cudf.pandas 是否适用于第三方库?#
cudf.pandas 已经与许多流行的第三方库进行了测试。
cudf.pandas 不仅能够工作,而且会加速这些库中的 pandas 操作。
作为我们 CI/CD 系统的一部分,我们目前测试了与以下 Python 库的常见交互:
库 |
状态 |
|---|---|
cuGraph |
✅ |
cuML |
✅ |
Hvplot |
✅ |
Holoview |
✅ |
Ibis |
✅ |
Joblib |
❌ |
NumPy |
✅ |
Matplotlib |
✅ |
Plotly |
✅ |
PyTorch |
✅ |
Seaborn |
✅ |
Scikit-Learn |
✅ |
SciPy |
✅ |
Tensorflow |
✅ |
XGBoost |
✅ |
请查看已知限制部分,了解哪些功能预计无法正常工作(以及原因)。
我可以在Dask或PySpark中使用cudf.pandas吗?#
cudf.pandas 目前并未设计用于分布式或外核计算(OOC)工作流。如果您正在寻找加速的OOC和分布式数据处理解决方案,我们推荐使用Dask和Apache Spark。
Dask 和 Apache Spark 都支持通过基于配置的接口进行加速计算。Dask 允许您配置数据框后端以使用 cuDF(了解更多在这篇博客中),而RAPIDS Accelerator for Apache Spark为 Spark 提供了类似的基于配置的插件。
是否有任何已知的限制?#
有一些已知的限制你应该注意:
因为回退涉及将数据从GPU复制到CPU再返回, 值的可变性 在Pandas对象中并不总是得到保证。你应该遵循 pandas的建议,优先使用不可变操作。
出于性能考虑,目前未实现连接和基于连接的操作以保持与标准 pandas 相同的行顺序
cudf.pandas与直接使用import cudf不兼容,并且旨在用于基于 pandas 的工作流程。使用“常规”pandas序列化的对象无法反序列化:你必须使用启用了
cudf.pandas的对象进行序列化,才能在启用cudf.pandas时进行反序列化。Global variables can be accessed but can’t be modified during CPU-fallback
%load_ext cudf.pandas import pandas as pd lst = [10] def udf(x): lst.append(x) return x + lst[0] s = pd.Series(range(2)).apply(udf) print(s) # we can access the value in lst 0 10 1 11 dtype: int64 print(lst) # lst is unchanged, as this specific UDF could not run on the GPU [10]
cudf.pandas(以及一般的cuDF)仅与pandas 2兼容。cudf的24.02版本是最后一个支持pandas 1.5.x的版本。In order for
cudf.pandasto produce a proxy array that ducktypes as a NumPy array, we create a proxy type that actually subclassesnumpy.ndarray. We can verify this with an isinstance check.%load_ext cudf.pandas import pandas as pd import numpy as np arr = pd.Series([1, 1, 2]).unique() # returns a proxy array isinstance(arr, np.ndarray) # returns True, where arr is a proxy array
Because the proxy type ducktypes as a NumPy array, NumPy functions may attempt to access internal members, such as the data buffer, via the NumPy C API. However, our proxy mechanism is designed to proxy function calls at the Python level, which is incompatible with these types of accesses. To handle these situations, we perform an eager device-to-host (DtoH) copy, which sets the data buffer correctly but incurs the cost of extra time when creating the proxy array. In the previous example, creating
arrperformed this kind of implicit DtoH transfer.With this approach, we also get compatibility with third party libraries like
torch.import torch x = torch.from_numpy(arr)
我可以强制在CPU上运行吗?#
要在CPU上运行你的代码,只需在不激活cudf.pandas的情况下运行,将使用“常规的pandas”。
如果需要,在使用cudf.pandas进行测试或基准测试时,可以禁用GPU加速。为此,请设置CUDF_PANDAS_FALLBACK_MODE环境变量,例如。
CUDF_PANDAS_FALLBACK_MODE=1 python -m cudf.pandas some_script.py