Last active
October 22, 2020 19:59
-
-
Save edoakes/753f5989efdf290fc7498004f705d756 to your computer and use it in GitHub Desktop.
Ray Serve by reference vs. pass by value experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import numpy as np | |
import ray | |
ray.init(log_to_driver=False) | |
@ray.remote | |
class Actor: | |
def __init__(self, next_actor, by_ref): | |
self.next_actor = next_actor | |
self.by_ref = by_ref | |
async def f(self, data): | |
if self.next_actor is None: | |
if self.by_ref: | |
ray.get(data[0]) | |
return | |
return await self.next_actor.f.remote(data) | |
@ray.remote | |
class Driver: | |
def __init__(self, by_ref, data_size): | |
self.by_ref = by_ref | |
self.data = ray.put(np.zeros(data_size, dtype=np.uint8)) | |
self.last = Actor.remote(None, by_ref) | |
self.middle = Actor.remote(self.last, by_ref) | |
self.first = Actor.remote(self.middle, by_ref) | |
async def run(self): | |
if self.by_ref: | |
await self.first.f.remote([self.data]) | |
else: | |
await self.first.f.remote(self.data) | |
def run_condition(by_ref, data_size, batch_size): | |
d = Driver.remote(by_ref, data_size) | |
stats = [] | |
for i in range(15): | |
start = time.time() | |
ray.get([d.run.remote() for _ in range(batch_size)]) | |
if i >= 5: | |
stats.append(batch_size / (time.time() - start)) | |
return f"{np.mean(stats)} tasks/s +- {round(np.std(stats), 2)}" | |
by_ref_small_single = run_condition(True, 1, 1) | |
print(f"by_ref_small_single: {by_ref_small_single}") | |
by_val_small_single = run_condition(False, 1, 1) | |
print(f"by_val_small_single: {by_val_small_single}") | |
by_ref_large_single = run_condition(True, 1*1024*1024, 1) | |
print(f"by_ref_large_single: {by_ref_large_single}") | |
by_val_large_single = run_condition(False, 1*1024*1024, 1) | |
print(f"by_val_large_single: {by_val_large_single}") | |
by_ref_small_batch = run_condition(True, 1, 1000) | |
print(f"by_ref_small_batch: {by_ref_small_batch}") | |
by_val_small_batch = run_condition(False, 1, 1000) | |
print(f"by_val_small_batch: {by_val_small_batch}") | |
by_ref_large_batch = run_condition(True, 1*1024*1024, 1000) | |
print(f"by_ref_large_batch: {by_ref_large_batch}") | |
by_val_large_batch = run_condition(False, 1*1024*1024, 1000) | |
print(f"by_val_large_batch: {by_val_large_batch}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import numpy as np | |
import ray | |
ray.init(log_to_driver=False) | |
@ray.remote | |
class Actor: | |
def __init__(self, next_actor, by_ref, data_size): | |
self.next_actor = next_actor | |
self.by_ref = by_ref | |
self.data_size = data_size | |
async def f(self): | |
if self.next_actor is None: | |
return np.zeros(self.data_size, dtype=np.uint8) | |
elif self.by_ref: | |
return self.next_actor.f.remote() | |
else: | |
return await self.next_actor.f.remote() | |
@ray.remote | |
class Driver: | |
def __init__(self, by_ref, data_size): | |
self.last = Actor.remote(None, by_ref, data_size) | |
self.middle = Actor.remote(self.last, by_ref, data_size) | |
self.first = Actor.remote(self.middle, by_ref, data_size) | |
async def run(self): | |
await self.first.f.remote() | |
def run_condition(by_ref, data_size, batch_size): | |
d = Driver.remote(by_ref, data_size) | |
stats = [] | |
for i in range(15): | |
start = time.time() | |
ray.get([d.run.remote() for _ in range(batch_size)]) | |
if i >= 5: | |
stats.append(batch_size / (time.time() - start)) | |
return f"{np.mean(stats)} tasks/s +- {round(np.std(stats), 2)}" | |
by_ref_small_single = run_condition(True, 1, 1) | |
print(f"by_ref_small_single: {by_ref_small_single}") | |
by_val_small_single = run_condition(False, 1, 1) | |
print(f"by_val_small_single: {by_val_small_single}") | |
by_ref_large_single = run_condition(True, 1*1024*1024, 1) | |
print(f"by_ref_large_single: {by_ref_large_single}") | |
by_val_large_single = run_condition(False, 1*1024*1024, 1) | |
print(f"by_val_large_single: {by_val_large_single}") | |
by_ref_small_batch = run_condition(True, 1, 1000) | |
print(f"by_ref_small_batch: {by_ref_small_batch}") | |
by_val_small_batch = run_condition(False, 1, 1000) | |
print(f"by_val_small_batch: {by_val_small_batch}") | |
by_ref_large_batch = run_condition(True, 1*1024*1024, 1000) | |
print(f"by_ref_large_batch: {by_ref_large_batch}") | |
by_val_large_batch = run_condition(False, 1*1024*1024, 1000) | |
print(f"by_val_large_batch: {by_val_large_batch}") |
Author
edoakes
commented
Oct 22, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment