mirror of
https://github.com/wassname/ray.git
synced 2026-06-28 00:29:38 +08:00
[Java] Refine python function (#8943)
This commit is contained in:
@@ -8,7 +8,7 @@ import io.ray.api.call.PyTaskCaller;
|
||||
import io.ray.api.call.TaskCaller;
|
||||
import io.ray.api.call.VoidTaskCaller;
|
||||
import io.ray.api.function.PyActorClass;
|
||||
import io.ray.api.function.PyRemoteFunction;
|
||||
import io.ray.api.function.PyFunction;
|
||||
import io.ray.api.function.RayFunc0;
|
||||
import io.ray.api.function.RayFunc1;
|
||||
import io.ray.api.function.RayFunc2;
|
||||
@@ -1942,39 +1942,39 @@ class RayCall {
|
||||
// ===========================
|
||||
// Cross-language methods.
|
||||
// ===========================
|
||||
public static <R> PyTaskCaller<R> task(PyRemoteFunction<R> pyRemoteFunction) {
|
||||
public static <R> PyTaskCaller<R> task(PyFunction<R> pyFunction) {
|
||||
Object[] args = new Object[]{};
|
||||
return new PyTaskCaller<>(pyRemoteFunction, args);
|
||||
return new PyTaskCaller<>(pyFunction, args);
|
||||
}
|
||||
|
||||
public static <R> PyTaskCaller<R> task(PyRemoteFunction<R> pyRemoteFunction, Object obj0) {
|
||||
public static <R> PyTaskCaller<R> task(PyFunction<R> pyFunction, Object obj0) {
|
||||
Object[] args = new Object[]{obj0};
|
||||
return new PyTaskCaller<>(pyRemoteFunction, args);
|
||||
return new PyTaskCaller<>(pyFunction, args);
|
||||
}
|
||||
|
||||
public static <R> PyTaskCaller<R> task(PyRemoteFunction<R> pyRemoteFunction, Object obj0, Object obj1) {
|
||||
public static <R> PyTaskCaller<R> task(PyFunction<R> pyFunction, Object obj0, Object obj1) {
|
||||
Object[] args = new Object[]{obj0, obj1};
|
||||
return new PyTaskCaller<>(pyRemoteFunction, args);
|
||||
return new PyTaskCaller<>(pyFunction, args);
|
||||
}
|
||||
|
||||
public static <R> PyTaskCaller<R> task(PyRemoteFunction<R> pyRemoteFunction, Object obj0, Object obj1, Object obj2) {
|
||||
public static <R> PyTaskCaller<R> task(PyFunction<R> pyFunction, Object obj0, Object obj1, Object obj2) {
|
||||
Object[] args = new Object[]{obj0, obj1, obj2};
|
||||
return new PyTaskCaller<>(pyRemoteFunction, args);
|
||||
return new PyTaskCaller<>(pyFunction, args);
|
||||
}
|
||||
|
||||
public static <R> PyTaskCaller<R> task(PyRemoteFunction<R> pyRemoteFunction, Object obj0, Object obj1, Object obj2, Object obj3) {
|
||||
public static <R> PyTaskCaller<R> task(PyFunction<R> pyFunction, Object obj0, Object obj1, Object obj2, Object obj3) {
|
||||
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
|
||||
return new PyTaskCaller<>(pyRemoteFunction, args);
|
||||
return new PyTaskCaller<>(pyFunction, args);
|
||||
}
|
||||
|
||||
public static <R> PyTaskCaller<R> task(PyRemoteFunction<R> pyRemoteFunction, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4) {
|
||||
public static <R> PyTaskCaller<R> task(PyFunction<R> pyFunction, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4) {
|
||||
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
|
||||
return new PyTaskCaller<>(pyRemoteFunction, args);
|
||||
return new PyTaskCaller<>(pyFunction, args);
|
||||
}
|
||||
|
||||
public static <R> PyTaskCaller<R> task(PyRemoteFunction<R> pyRemoteFunction, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) {
|
||||
public static <R> PyTaskCaller<R> task(PyFunction<R> pyFunction, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) {
|
||||
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4, obj5};
|
||||
return new PyTaskCaller<>(pyRemoteFunction, args);
|
||||
return new PyTaskCaller<>(pyFunction, args);
|
||||
}
|
||||
|
||||
public static PyActorCreator actor(PyActorClass pyActorClass) {
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.ray.api.call;
|
||||
|
||||
import io.ray.api.ObjectRef;
|
||||
import io.ray.api.Ray;
|
||||
import io.ray.api.function.PyRemoteFunction;
|
||||
import io.ray.api.function.PyFunction;
|
||||
|
||||
/**
|
||||
* A helper to call python remote function.
|
||||
@@ -10,10 +10,10 @@ import io.ray.api.function.PyRemoteFunction;
|
||||
* @param <R> The type of the python function return value
|
||||
*/
|
||||
public class PyTaskCaller<R> extends BaseTaskCaller<PyTaskCaller<R>> {
|
||||
private final PyRemoteFunction<R> func;
|
||||
private final PyFunction<R> func;
|
||||
private final Object[] args;
|
||||
|
||||
public PyTaskCaller(PyRemoteFunction<R> func, Object[] args) {
|
||||
public PyTaskCaller(PyFunction<R> func, Object[] args) {
|
||||
this.func = func;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ package io.ray.api.function;
|
||||
* we can create this Python actor from Java:
|
||||
*
|
||||
* {@code
|
||||
* PyActorHandle actor = Ray.createActor(new PyActorClass("example_package.example_module", "A"),
|
||||
* PyActorHandle actor = Ray.createActor(PyActorClass.of("example_package.example_module", "A"),
|
||||
* "the value for x");
|
||||
* }
|
||||
* </pre>
|
||||
@@ -29,8 +29,20 @@ public class PyActorClass {
|
||||
// The name of this actor class
|
||||
public final String className;
|
||||
|
||||
public PyActorClass(String moduleName, String className) {
|
||||
private PyActorClass(String moduleName, String className) {
|
||||
this.moduleName = moduleName;
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a python actor class.
|
||||
*
|
||||
* @param moduleName The full module name of this actor class
|
||||
* @param className The name of this actor class
|
||||
* @return a python actor class
|
||||
*/
|
||||
public static PyActorClass of(String moduleName, String className) {
|
||||
return new PyActorClass(moduleName, className);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.ray.api.function;
|
||||
|
||||
/**
|
||||
* A class that represents a method of a Python actor.
|
||||
*
|
||||
* A class that represents a method of a Python actor.
|
||||
* <p>
|
||||
* Note, information about the actor will be inferred from the actor handle,
|
||||
* so it's not specified in this class.
|
||||
*
|
||||
@@ -24,7 +24,7 @@ package io.ray.api.function;
|
||||
*
|
||||
* {@code
|
||||
* // A.foo returns a string, so we have to set the returnType to String.class
|
||||
* ObjectRef<String> res = actor.call(new PyActorMethod<>("foo", String.class));
|
||||
* ObjectRef<String> res = actor.call(PyActorMethod.of("foo", String.class));
|
||||
* String x = res.get();
|
||||
* }
|
||||
* </pre>
|
||||
@@ -35,8 +35,31 @@ public class PyActorMethod<R> {
|
||||
// Type of the return value of this actor method
|
||||
public final Class<R> returnType;
|
||||
|
||||
public PyActorMethod(String methodName, Class<R> returnType) {
|
||||
private PyActorMethod(String methodName, Class<R> returnType) {
|
||||
this.methodName = methodName;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a python actor method.
|
||||
*
|
||||
* @param methodName The name of this actor method
|
||||
* @return a python actor method.
|
||||
*/
|
||||
public static PyActorMethod<Object> of(String methodName) {
|
||||
return of(methodName, Object.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a python actor method.
|
||||
*
|
||||
* @param methodName The name of this actor method
|
||||
* @param returnType Class of the return value of this actor method
|
||||
* @param <R> The type of the return value of this actor method
|
||||
* @return a python actor method.
|
||||
*/
|
||||
public static <R> PyActorMethod<R> of(String methodName, Class<R> returnType) {
|
||||
return new PyActorMethod<>(methodName, returnType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package io.ray.api.function;
|
||||
|
||||
/**
|
||||
* A class that represents a Python remote function.
|
||||
*
|
||||
* <pre>
|
||||
* example_package/
|
||||
* ├──__init__.py
|
||||
* └──example_module.py
|
||||
*
|
||||
* in example_module.py there is a function.
|
||||
*
|
||||
* \@ray.remote
|
||||
* def bar(v):
|
||||
* return v
|
||||
*
|
||||
* then we can call the Python function bar:
|
||||
*
|
||||
* {@code
|
||||
* // bar returns input, so we have to set the returnType to int.class if bar accepts an int
|
||||
* ObjectRef<Integer> res = actor.call(
|
||||
* PyFunction.of("example_package.example_module", "bar", Integer.class),
|
||||
* 1);
|
||||
* Integer value = res.get();
|
||||
*
|
||||
* // bar returns input, so we have to set the returnType to String.class if bar accepts a string
|
||||
* ObjectRef<String> res = actor.call(
|
||||
* PyFunction.of("example_package.example_module", "bar", String.class),
|
||||
* "Hello world!");
|
||||
* String value = res.get();
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public class PyFunction<R> {
|
||||
// The full module name of this function
|
||||
public final String moduleName;
|
||||
// The name of this function
|
||||
public final String functionName;
|
||||
// Type of the return value of this function
|
||||
public final Class<R> returnType;
|
||||
|
||||
private PyFunction(String moduleName, String functionName, Class<R> returnType) {
|
||||
this.moduleName = moduleName;
|
||||
this.functionName = functionName;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a python function.
|
||||
*
|
||||
* @param moduleName The full module name of this function
|
||||
* @param functionName The name of this function
|
||||
* @return a python function.
|
||||
*/
|
||||
public static PyFunction<Object> of(
|
||||
String moduleName, String functionName) {
|
||||
return of(moduleName, functionName, Object.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a python function.
|
||||
*
|
||||
* @param moduleName The full module name of this function
|
||||
* @param functionName The name of this function
|
||||
* @param returnType Class of the return value of this function
|
||||
* @param <R> Type of the return value of this function
|
||||
* @return a python function.
|
||||
*/
|
||||
public static <R> PyFunction<R> of(
|
||||
String moduleName, String functionName, Class<R> returnType) {
|
||||
return new PyFunction<>(moduleName, functionName, returnType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package io.ray.api.function;
|
||||
|
||||
/**
|
||||
* A class that represents a Python remote function.
|
||||
*
|
||||
* <pre>
|
||||
* example_package/
|
||||
* ├──__init__.py
|
||||
* └──example_module.py
|
||||
*
|
||||
* in example_module.py there is a function.
|
||||
*
|
||||
* \@ray.remote
|
||||
* def bar(v):
|
||||
* return v
|
||||
*
|
||||
* then we can call the Python function bar:
|
||||
*
|
||||
* {@code
|
||||
* // bar returns input, so we have to set the returnType to int.class if bar accepts an int
|
||||
* ObjectRef<Integer> res = actor.call(
|
||||
* new PyRemoteFunction<>("example_package.example_module", "bar", Integer.class),
|
||||
* 1);
|
||||
* Integer value = res.get();
|
||||
*
|
||||
* // bar returns input, so we have to set the returnType to String.class if bar accepts a string
|
||||
* ObjectRef<String> res = actor.call(
|
||||
* new PyRemoteFunction<>("example_package.example_module", "bar", String.class),
|
||||
* "Hello world!");
|
||||
* String value = res.get();
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public class PyRemoteFunction<R> {
|
||||
// The full module name of this function
|
||||
public final String moduleName;
|
||||
// The name of this function
|
||||
public final String functionName;
|
||||
// Type of the return value of this function
|
||||
public final Class<R> returnType;
|
||||
|
||||
public PyRemoteFunction(String moduleName, String functionName, Class<R> returnType) {
|
||||
this.moduleName = moduleName;
|
||||
this.functionName = functionName;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import io.ray.api.PyActorHandle;
|
||||
import io.ray.api.WaitResult;
|
||||
import io.ray.api.function.PyActorClass;
|
||||
import io.ray.api.function.PyActorMethod;
|
||||
import io.ray.api.function.PyRemoteFunction;
|
||||
import io.ray.api.function.PyFunction;
|
||||
import io.ray.api.function.RayFunc;
|
||||
import io.ray.api.id.ObjectId;
|
||||
import io.ray.api.id.UniqueId;
|
||||
@@ -103,12 +103,12 @@ public interface RayRuntime {
|
||||
/**
|
||||
* Invoke a remote Python function.
|
||||
*
|
||||
* @param pyRemoteFunction The Python function.
|
||||
* @param pyFunction The Python function.
|
||||
* @param args Arguments of the function.
|
||||
* @param options The options for this call.
|
||||
* @return The result object.
|
||||
*/
|
||||
ObjectRef call(PyRemoteFunction pyRemoteFunction, Object[] args, CallOptions options);
|
||||
ObjectRef call(PyFunction pyFunction, Object[] args, CallOptions options);
|
||||
|
||||
/**
|
||||
* Invoke a remote function on an actor.
|
||||
|
||||
Reference in New Issue
Block a user