I have a tiny Ray pipeline like this:
import ray
import numpy as np
import time
@ray.remote
class PersonDetector:
def __init__(self) -> None:
self.model = self._init_model()
def _init_model(self):
s = np.random.random([100, 4])
return s
def infer(self, img):
b = self.model[0:np.random.randint(100), :]
# random batch boxes
print(b.shape)
time.sleep(4)
return b
@ray.remote
class KptsDetector:
def __init__(self) -> None:
self.model = self._init_model()
def _init_model(self):
s = np.random.random([100, 17])
return s
def infer(self, img, boxes):
sh1 = boxes.shape[0]
kpts = self.model[0: sh1, :]
time.sleep(2)
return kpts
@ray.remote
class HandDetector:
def __init__(self) -> None:
self.model = self._init_model()
def _init_model(self):
s = np.random.random([100, 4])
return s
def infer(self, img):
b = self.model[0:np.random.randint(100), :]
# random batch boxes
data = {}
data['hands'] = b
time.sleep(3)
return data
@ray.remote
def gather_all(hands, kpts):
t0 = time.time()
if isinstance(hands, dict):
print(f'in hands info: {hands.keys()}')
hands = hands['hands']
if hands.shape[0] > kpts.shape[0]:
out = hands.copy()
out[:kpts.shape[0], :] += kpts[..., :4]
else:
out = kpts.copy()[..., :4]
out[:hands.shape[0], :] += hands
print(f'[gather time] {time.time() - t0}')
return out
# how to written DAG in classes?
P = PersonDetector.remote()
K = KptsDetector.remote()
H = HandDetector.remote()
t0 = time.time()
img = []
boxes = P.infer.remote(img)
hands = H.infer.remote(img)
kpts = K.infer.remote(img, boxes)
# out = gather.remote(hands, kpts)
out = gather_all.remote(hands, kpts)
t1 = time.time()
print(t1 - t0)
out = ray.get(out)
t2 = time.time()
print(t2 - t0)
print(t2 - t1)
print(out.shape)
I using time.sleep()
for fake time consuming. As you can see, the HandDetector should running in a sub process, so the whole time should be 6s.
But I got (you can have a try on your computer):
6.45377516746521
6.4489240646362305
Why there are 0.4s time more?