如何在不每次加载模型的情况下使用 ray 并行获得模型推理?

问题描述

我是 ray 的新手,正在试验它。 我有一个带有 tensorflow 模型的预测器类,想使用该类的 predict 方法在另一个函数中并行获取推理。

class Predictor:
    def__init__(...):

    def load_models(...):

    def load_data(...):
    
    def predict(...):

在另一个脚本中,我初始化了类并加载了所需的数据和模型。然后我尝试调用一个函数

@ray.remote
def newfunc(predictor,inputs):
   # creating dataset
   # preprocessing
   outputs = predictor.predict(inputs)

   return outputs

if __name__ == __main__:
    predictor = Predictor()
    predictor.load_models()
    predictor.load_data()



    print("testing ray")
    refs = [newfunc.remote(predictor,x) for x in np.array_split(bigdata,24)]
    print(ray.get(refs[0]))

我收到一个错误,说我的对象没有某个属性 AttributeError: 'Predictor' object has no attribute 'dataManager'

但是我可以在没有 ray 的情况下很好地使用类的实例,并且不会出现这个问题。 Ray 是否为每个工人创建了一个新对象?似乎每次都没有加载数据管理器,所以也许这就是我收到属性错误的原因。我希望只需创建一次对象并将所有内容加载一次并将其用作参数。我试过创建对象的演员,但这种方法没有运气。有人对如何并行化使用对象的函数有任何建议吗?

我也试过这个:

@ray.remote
def newfunc(predictor,inputs):
   # creating dataset
   # preprocessing
   outputs = predictor.predict(inputs)

   return outputs

if __name__ == __main__:
    predictor = Predictor()
    predictor.load_models()
    predictor.load_data()
    predictor_id = ray.put(predictor)


    print("testing ray")
    refs = [newfunc.remote(predictor_id,24)]
    print(ray.get(refs[0]))

但这也没有用。事实上,我尝试从原始对象访问属性并使用射线 objectref

>>> predictor.dataManager
<prophetball.StreamLearner.utils.generators.DataManager object at 0x7fac0c00c290>
>>> ray.get(predictor_id).dataManager
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
AttributeError: 'Predictor' object has no attribute 'dataManager'

完整回溯:

Traceback (most recent call last):
  File "<stdin>",in <module>
  File "/root/anaconda3/envs/LarusTF/lib/python3.7/site-packages/ray/_private/client_mode_hook.py",line 47,in wrapper
    return func(*args,**kwargs)
  File "/root/anaconda3/envs/LarusTF/lib/python3.7/site-packages/ray/worker.py",line 1431,in get
    raise value.as_instanceof_cause()
ray.exceptions.RayTaskError(AttributeError): ray::promos_in_chromosome() (pid=7750,ip=172.16.69.99)
  File "python/ray/_raylet.pyx",line 502,in ray._raylet.execute_task
  File "<stdin>",line 133,in promos_in_chromosome
  File "/run/media/root/prophet/DevOps/bnlwe-da-p-80200-prophetball/prophetball/StreamLearner/utils/predictor.py",line 1336,in units_from_predictor
  File "/run/media/root/prophet/DevOps/bnlwe-da-p-80200-prophetball/prophetball/StreamLearner/utils/predictor.py",line 1194,in add_missing_features_to_forecast
    df = self.dataManager.add_non_POS_data(df,lock_date = lock_date)
AttributeError: 'Predictor' object has no attribute 'dataManager'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)