Cross-language invocation Part 1: Java calling Python functions and actors (#4166)

This commit is contained in:
Hao Chen
2019-03-21 13:34:21 +08:00
committed by GitHub
parent 828dc08ac8
commit d03999d01e
28 changed files with 872 additions and 228 deletions
+139 -2
View File
@@ -2,7 +2,6 @@
package org.ray.api;
import org.ray.api.function.RayFunc;
import org.ray.api.function.RayFunc0;
import org.ray.api.function.RayFunc1;
import org.ray.api.function.RayFunc2;
@@ -11,7 +10,6 @@ import org.ray.api.function.RayFunc4;
import org.ray.api.function.RayFunc5;
import org.ray.api.function.RayFunc6;
import org.ray.api.options.ActorCreationOptions;
import org.ray.api.options.BaseTaskOptions;
import org.ray.api.options.CallOptions;
/**
@@ -2312,4 +2310,143 @@ class RayCall {
Object[] args = new Object[]{t0, t1, t2, t3, t4, t5};
return Ray.internal().createActor(f, args, options);
}
// ===========================
// Cross-language methods.
// ===========================
public static RayObject callPy(String moduleName, String functionName) {
Object[] args = new Object[]{};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, CallOptions options) {
Object[] args = new Object[]{};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0) {
Object[] args = new Object[]{obj0};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, CallOptions options) {
Object[] args = new Object[]{obj0};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, CallOptions options) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, CallOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, CallOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, CallOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4, obj5};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5, CallOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4, obj5};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(RayPyActor pyActor, String functionName) {
Object[] args = new Object[]{};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0) {
Object[] args = new Object[]{obj0};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0, Object obj1) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0, Object obj1, Object obj2) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0, Object obj1, Object obj2, Object obj3) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayPyActor createPyActor(String moduleName, String className) {
Object[] args = new Object[]{};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, ActorCreationOptions options) {
Object[] args = new Object[]{};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0) {
Object[] args = new Object[]{obj0};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, ActorCreationOptions options) {
Object[] args = new Object[]{obj0};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4, obj5};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4, obj5};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
}
@@ -0,0 +1,18 @@
package org.ray.api;
/**
* Handle of a Python actor.
*/
public interface RayPyActor extends RayActor {
/**
* @return Module name of the Python actor class.
*/
String getModuleName();
/**
* @return Name of the Python actor class.
*/
String getClassName();
}
@@ -3,6 +3,7 @@ package org.ray.api.runtime;
import java.util.List;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.RayPyActor;
import org.ray.api.RuntimeContext;
import org.ray.api.WaitResult;
import org.ray.api.function.RayFunc;
@@ -45,8 +46,8 @@ public interface RayRuntime {
<T> List<T> get(List<UniqueId> objectIds);
/**
* Wait for a list of RayObjects to be locally available,
* until specified number of objects are ready, or specified timeout has passed.
* Wait for a list of RayObjects to be locally available, until specified number of objects are
* ready, or specified timeout has passed.
*
* @param waitList A list of RayObject to wait for.
* @param numReturns The number of objects that should be returned.
@@ -96,4 +97,37 @@ public interface RayRuntime {
ActorCreationOptions options);
RuntimeContext getRuntimeContext();
/**
* Invoke a remote Python function.
*
* @param moduleName Module name of the Python function.
* @param functionName Name of the Python function.
* @param args Arguments of the function.
* @param options The options for this call.
* @return The result object.
*/
RayObject callPy(String moduleName, String functionName, Object[] args, CallOptions options);
/**
* Invoke a remote Python function on an actor.
*
* @param pyActor A handle to the actor.
* @param functionName Name of the actor method.
* @param args Arguments of the function.
* @return The result object.
*/
RayObject callPy(RayPyActor pyActor, String functionName, Object[] args);
/**
* Create a Python actor on a remote node.
*
* @param moduleName Module name of the Python actor class.
* @param className Name of the Python actor class.
* @param args Arguments of the actor constructor.
* @param options The options for creating actor.
* @return A handle to the actor.
*/
RayPyActor createPyActor(String moduleName, String className, Object[] args,
ActorCreationOptions options);
}