问题描述
import ray
import numpy as np
ray.init()
@ray.remote
def f():
return np.zeros(10000000)
results = []
for i in range(100):
print(i)
results += ray.get([f.remote() for _ in range(50)])
通常,当对象存储填满时,它会开始驱逐未使用的对象(以最近最少使用的方式)。但是,由于所有对象都是保存在结果列表中的 numpy 数组,它们都还在使用中,而这些 numpy 数组所在的内存实际上在对象存储中,因此它们在对象存储。在这些对象超出范围之前,对象存储无法驱逐它们。
问题:如何在不超出单机内存的情况下指定像redis这样的外部对象存储?我不想使用 /dev/shm 或 /tmp 作为对象存储,因为只有有限的内存可用并且它很快就会填满
解决方法
从 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 中,它将在共享内存中,它由一个名为 Plasma store 的 ray 分布式对象存储管理,如果您的对象大小 >= 100KB)。所以,
obj = ray.get(ref) # Now,obj is pointing to the shared memory directly.
目前,对象溢出功能支持第 1 种情况的磁盘溢出,但不支持第 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)