Ray is a unified framework for scaling AI and Python applications. However, it falls short in offering friendly type hints, particularly when it comes to working with the Actor
.
To address this shortfall, sunray provides enhanced and more robust type hints.
pip install sunray
sunray | ray |
---|---|
- sunray returns
Actor[Demo]
, but ray returnsObjectRef[Demo]
- ray mypy raise error
Type[Demo] has no attribute "remote"
sunray | ray |
---|---|
- sunray list all remote methods
- ray list nothing
sunray | ray |
---|---|
- sunray correctly provided parameter hints.
- ray ...
sunray | ray |
---|---|
- with sunray, just annotate it with
Actor[Demo]
. - with ray, I don't known.
sunray | ray |
---|---|
- sunray correctly identified that
stream
returns a generator. - ray still returns ObjectRef.
sunray | ray |
---|---|
- sunray will auto unpack tuple result if options specify
unpack=True
. - ray need to specify how many return numbers, so you need to count it.
- ray mypy raise error 'RemoteFunctionNoArgs has no attribute "options"'.
sunray | ray |
---|---|
- sunray get_actor will return
ActorHandle
, and returnActor[Demo]
if you specify with generic type. - ray just return
Any
.
sunray | ray |
---|---|
- sunray maintains a consistent calling convention, whether it's from internal or external functions.
- ray, you need to first obtain the current actor from the running context, and then call through the actor.
sunray | ray |
---|---|
- sunray can successfully track the input parameter types and output types.
- ray does not have this capability.
sunray
re-export all apis from ray.core
with friendly type hints. In addition, sunray
provides ActorMixin
which is used to help creating more robust actors.
ActorMixin
is a mixin, and provides a classmethod new_actor
import sunray
class Demo(
# Here to specify default actor options
sunray.ActorMixin, name="DemoActor", num_cpus=1, concurrency_groups={"g1": 1}
):
def __init__(self, init_v: int):
self.init_v = init_v
# annotate `add` is a remote_method
@sunray.remote_method
def add(self, v: int) -> int:
return self.init_v + v
# support directly call remote_method
@sunray.remote_method
def calculate(self, v: int) -> int:
return self.add(v)
# support specify remote method options
@sunray.remote_method(concurrency_group="g1")
async def sleep(self): ...
# construct the actor
actor = Demo.new_actor().remote(1)
# call remote method
ref = actor.methods.add.remote(1)
print(sunray.get(ref))