import numpy as np
import sys
from _predict import PredictService
from config import InternalConfig
from _backend import MongoORM
from logger import AppLogger
logger = AppLogger(__name__).get_logger()
import time
# num_cpus = psutil.cpu_count(logical=False)
num_cpus = len(psutil.Process().cpu_affinity())
print('Initializing Ray on {} cpus.'.format(num_cpus))
ray.init(num_cpus=num_cpus)
print('Ray initialization {} on {} cores.'.format(
'successful' if ray.is_initialized() else 'failed', num_cpus))
@ray.remote
class MyActor(object):
def __init__(self):
import tensorflow as tf
self._model = tf.keras.models.load_model(
'./assets/sample/models/resnet-50.h5')
def func(self, x):
logging.error(f'calculating {x}')
out = self._model(np.zeros((10, 512, 512, 3)))
return x * 2
actors = [MyActor.remote() for _ in range(2)]
actor_pool = ray.util.ActorPool(actors)
def ray_batch(args_list):
res = actor_pool.map(lambda a, v: a.func.remote(v), args_list)
print(list(res))
import time
st = time.time()
args_list = [1] * 10
ray_batch(args_list)
print(time.time() - st)
The performance gain is 5% here 2 ray actors vs 1 ray actor
im testing on 2 core machine (edited)