从 ray 1.2.0 开始,支持对象溢出以支持核外数据处理。从 1.3+(将在 3 周后发布),此功能将默认开启。
https://docs.ray.io/en/latest/memory-management.html#object-spilling
但是您的示例不适用于此功能。让我在这里解释一下为什么。
你需要知道两件事。
- 当您调用 ray task (f.remote) 或 ray.put 时,它会返回一个对象引用。尝试
ref = f.remote()
print(ref)
- 当您
ray.get
在此引用上运行时,现在 python 变量直接访问内存(在 Ray 中,它将位于共享内存中,如果您的对象大小> = 100KB,则由称为等离子存储的 ray 分布式对象存储管理) . 所以,
obj = ray.get(ref) # Now, obj is pointing to the shared memory directly.
目前,对象溢出功能支持 1 情况下的磁盘溢出,但不支持 2(如果您想像的话,支持 2 会更棘手)。
所以这里有2个解决方案;
- 为您的等离子存储使用文件目录。例如,开始射线
ray.init(_plasma_directory="/tmp")
这将允许您将tmp
文件夹用作等离子存储(意味着射线对象存储在 tmp 文件系统中)。请注意,当您使用此选项时,您可能会看到性能下降。
- 使用带有背压的溢出物。不要使用 获取所有光线对象
ray.get
,而是使用ray.wait
.
import ray
import numpy as np
# Note: You don't need to specify this if you use the latest master.
ray.init(
_system_config={
"automatic_object_spilling_enabled": True,
"object_spilling_config": json.dumps(
{"type": "filesystem", "params": {"directory_path": "/tmp/spill"}},
)
},
)
@ray.remote
def f():
return np.zeros(10000000)
result_refs = []
for i in range(100):
print(i)
result_refs += [f.remote() for _ in range(50)]
while result_refs:
[ready], result_refs = ray.wait(result_refs)
result = ray.get(ready)