Dereferencing Unspecified Optional Arugments

How severe does this issue affect your experience of using Ray?

  • High: It blocks me to complete my task.

Hi, I was wondering if it is possible to do the following:
Call a remote function, which calls another function (not remote) which has an optional argument that is not specified (meaning the default is automatically selected) – the value of that optional argument is in the object store. Currently, I can only do this in the case wherein the optional argument is specified by the user. This is shown in the following example, wherein the first way of doing things runs (but is not what I want to do), and the second way of doing things is what I want to do (but doesn’t run). The main difference (I think!) is that in the second example, an optional argument is not specified by the user; however also note that the originaly ‘remote’ calling function also no longer recieves as input the reference to the object in the object store which is probably important. As suggested by the error message, what appears to be happening is that proper dereferencing is not occurring. Does anyone know how to do this? Any help would be greatly appreciated!

Code:

H = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
H_ref = ray.put(H)
Data = [1212, 121, 2, 3, 3, 23, 23, 3, 23, 2332, 21, 31, 123312, 31212, 3, 234234, 55334, 4, 2, 32]
def Eval(arg1, arg2=H_ref):
    return arg1 + arg2[0]
Eval_ref = ray.put(Eval)

**First way of doing things:**
@ray.remote
def Main(arg, Eval_ref, H_ref):
    return Eval_ref(arg, arg2=H_ref)

refs = [Main.remote(arg, Eval_ref, H_ref) for arg in Data]
results = ray.get(refs)

**Second way of doing things:**
@ray.remote
def Main(arg, Eval_ref):
    return Eval_ref(arg, arg2=H_ref)

refs = [Main.remote(arg, Eval_ref) for arg in Data]
results = ray.get(refs)

Error message

Traceback (most recent call last):
  File "C:\Users\seanr\AppData\Local\JetBrains\Toolbox\apps\PyCharm-C\ch-0\223.7571.203\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "C:\Users\seanr\Documents\Research Related\Programming\Python\venv\lib\site-packages\ray\_private\auto_init_hook.py", line 18, in auto_init_wrapper
    return fn(*args, **kwargs)
  File "C:\Users\seanr\Documents\Research Related\Programming\Python\venv\lib\site-packages\ray\_private\client_mode_hook.py", line 103, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\seanr\Documents\Research Related\Programming\Python\venv\lib\site-packages\ray\_private\worker.py", line 2540, in get
    raise value.as_instanceof_cause()
ray.exceptions.RayTaskError(TypeError): ray::Main() (pid=18760, ip=127.0.0.1)
  File "python\ray\_raylet.pyx", line 1434, in ray._raylet.execute_task
  File "python\ray\_raylet.pyx", line 1438, in ray._raylet.execute_task
  File "<string>", line 5, in Main
  File "<string>", line 4, in Eval
TypeError: 'ray._raylet.ObjectRef' object is not subscriptable

We don’t recommend the second way because all the object refs in Ray is ref counted, and the second way makes the ref counting complicated (I think if you do this, H_ref will probably never be GC’ed).

Besides, for the answer of your question, when a ref is passed as an argument of a task, ray.get is automatically called. See the below example.

ref = ray.put(1)

@ray.remote
def f(ref):
    print(ref)

f.remote(ref) # ref will be 1, not a ref

However, when the ref is implicitly captured, ray.get is not called. You should probably call ray.get on H_ref in order to run the second case