def testRecursiveObjects(self): ray.init(start_ray_local=True, num_workers=0) class ClassA(object): pass ray.register_class(ClassA) # Make a list that contains itself. l = [] l.append(l) # Make an object that contains itself as a field. a1 = ClassA() a1.field = a1 # Make two objects that contain each other as fields. a2 = ClassA() a3 = ClassA() a2.field = a3 a3.field = a2 # Make a dictionary that contains itself. d1 = {} d1["key"] = d1 # Create a list of recursive objects. recursive_objects = [l, a1, a2, a3, d1] # Check that exceptions are thrown when we serialize the recursive objects. for obj in recursive_objects: self.assertRaises(Exception, lambda : ray.put(obj)) ray.worker.cleanup()
def testRecursiveObjects(self): ray.init(start_ray_local=True, num_workers=0) class ClassA(object): pass ray.register_class(ClassA) # Make a list that contains itself. l = [] l.append(l) # Make an object that contains itself as a field. a1 = ClassA() a1.field = a1 # Make two objects that contain each other as fields. a2 = ClassA() a3 = ClassA() a2.field = a3 a3.field = a2 # Make a dictionary that contains itself. d1 = {} d1["key"] = d1 # Create a list of recursive objects. recursive_objects = [l, a1, a2, a3, d1] # Check that exceptions are thrown when we serialize the recursive objects. for obj in recursive_objects: self.assertRaises(Exception, lambda: ray.put(obj)) ray.worker.cleanup()
def testObjStore(self): node_ip_address = "127.0.0.1" scheduler_address = ray.services.start_ray_local(num_objstores=2, num_workers=0, worker_path=None) ray.connect(node_ip_address, scheduler_address, mode=ray.SCRIPT_MODE) objstore_addresses = [objstore_info["address"] for objstore_info in ray.scheduler_info()["objstores"]] w1 = ray.worker.Worker() w2 = ray.worker.Worker() ray.reusables._cached_reusables = [] # This is a hack to make the test run. ray.connect(node_ip_address, scheduler_address, objstore_address=objstore_addresses[0], mode=ray.SCRIPT_MODE, worker=w1) ray.reusables._cached_reusables = [] # This is a hack to make the test run. ray.connect(node_ip_address, scheduler_address, objstore_address=objstore_addresses[1], mode=ray.SCRIPT_MODE, worker=w2) for cls in [Foo, Bar, Baz, Qux, SubQux, Exception, CustomError, Point, NamedTupleExample]: ray.register_class(cls) # putting and getting an object shouldn't change it for data in RAY_TEST_OBJECTS: objectid = ray.put(data, w1) result = ray.get(objectid, w1) assert_equal(result, data) # putting an object, shipping it to another worker, and getting it shouldn't change it for data in RAY_TEST_OBJECTS: objectid = ray.put(data, w1) result = ray.get(objectid, w2) assert_equal(result, data) # putting an object, shipping it to another worker, and getting it shouldn't change it for data in RAY_TEST_OBJECTS: objectid = ray.put(data, w2) result = ray.get(objectid, w1) assert_equal(result, data) # This test fails. See https://github.com/ray-project/ray/issues/159. # getting multiple times shouldn't matter # for data in [np.zeros([10, 20]), np.random.normal(size=[45, 25]), np.zeros([10, 20], dtype=np.dtype("float64")), np.zeros([10, 20], dtype=np.dtype("float32")), np.zeros([10, 20], dtype=np.dtype("int64")), np.zeros([10, 20], dtype=np.dtype("int32"))]: # objectid = worker.put(data, w1) # result = worker.get(objectid, w2) # result = worker.get(objectid, w2) # result = worker.get(objectid, w2) # assert_equal(result, data) # Getting a buffer after modifying it before it finishes should return updated buffer objectid = ray.libraylib.get_objectid(w1.handle) buf = ray.libraylib.allocate_buffer(w1.handle, objectid, 100) buf[0][0] = 1 ray.libraylib.finish_buffer(w1.handle, objectid, buf[1], 0) completedbuffer = ray.libraylib.get_buffer(w1.handle, objectid) self.assertEqual(completedbuffer[0][0], 1) # We started multiple drivers manually, so we will disconnect them manually. ray.disconnect(worker=w1) ray.disconnect(worker=w2) ray.worker.cleanup()
def testDeallocation(self): reload(test_functions) for module in [ ra.core, ra.random, ra.linalg, da.core, da.random, da.linalg ]: reload(module) ray.init(start_ray_local=True, num_workers=1) ray.register_class(da.DistArray) def check_not_deallocated(object_ids): reference_counts = ray.scheduler_info()["reference_counts"] for object_id in object_ids: self.assertGreater(reference_counts[object_id.id], 0) def check_everything_deallocated(): reference_counts = ray.scheduler_info()["reference_counts"] self.assertEqual(reference_counts, len(reference_counts) * [-1]) z = da.zeros.remote([da.BLOCK_SIZE, 2 * da.BLOCK_SIZE]) time.sleep(0.1) objectid_val = z.id time.sleep(0.1) check_not_deallocated([z]) del z time.sleep(0.1) check_everything_deallocated() x = ra.zeros.remote([10, 10]) y = ra.zeros.remote([10, 10]) z = ra.dot.remote(x, y) objectid_val = x.id time.sleep(0.1) check_not_deallocated([x, y, z]) del x time.sleep(0.1) check_not_deallocated([y, z]) del y time.sleep(0.1) check_not_deallocated([z]) del z time.sleep(0.1) check_everything_deallocated() z = da.zeros.remote([4 * da.BLOCK_SIZE]) time.sleep(0.1) check_not_deallocated(ray.get(z).objectids.tolist()) del z time.sleep(0.1) check_everything_deallocated() ray.worker.cleanup()
def testRegisterClass(self): ray.init(start_ray_local=True, num_workers=0) # Check that putting an object of a class that has not been registered # throws an exception. class TempClass(object): pass self.assertRaises(Exception, lambda : ray.put(Foo)) # Check that registering a class that Ray cannot serialize efficiently # raises an exception. self.assertRaises(Exception, lambda : ray.register_class(type(True))) # Check that registering the same class with pickle works. ray.register_class(type(float), pickle=True) self.assertEqual(ray.get(ray.put(float)), float) ray.worker.cleanup()
def mat_mul(size, num_workers, block_size): # Start Ray. ray.init(start_ray_local=True, num_workers=num_workers) ray.register_class(da.DistArray) # Allocate two size x size arrays of all one's, and multiply them. Block on # the result. print "Matrix multiply with size {}, block size {}".format( size, block_size) start = time.time() a = da.ones.remote([size, size], block_size=block_size) b = da.ones.remote([size, size], block_size=block_size) c = da.dot.remote(a, b) c = da.assemble.remote(c) print c.id c_get = ray.get(c) end = time.time() print c_get print "{}".format(end - start) return ray
def testAssemble(self): for module in [ ra.core, ra.random, ra.linalg, da.core, da.random, da.linalg ]: reload(module) ray.init(start_ray_local=True, num_workers=1) ray.register_class(da.DistArray) a = ra.ones.remote([da.BLOCK_SIZE, da.BLOCK_SIZE]) b = ra.zeros.remote([da.BLOCK_SIZE, da.BLOCK_SIZE]) x = da.DistArray([2 * da.BLOCK_SIZE, da.BLOCK_SIZE], np.array([[a], [b]])) assert_equal( x.assemble(), np.vstack([ np.ones([da.BLOCK_SIZE, da.BLOCK_SIZE]), np.zeros([da.BLOCK_SIZE, da.BLOCK_SIZE]) ])) ray.worker.cleanup()
def testGet(self): ray.init(start_ray_local=True, num_workers=3) for cls in [Foo, Bar, Baz, Qux, SubQux, Exception, CustomError, Point, NamedTupleExample]: ray.register_class(cls) # Remote objects should be deallocated when the corresponding ObjectID goes # out of scope, and all results of ray.get called on the ID go out of scope. for val in RAY_TEST_OBJECTS: x = ray.put(val) objectid = x.id xval = ray.get(x) del x, xval self.assertEqual(ray.scheduler_info()["reference_counts"][objectid], -1) # Remote objects that do not contain numpy arrays should be deallocated when # the corresponding ObjectID goes out of scope, even if ray.get has been # called on the ObjectID. for val in [True, False, None, 1, 1.0, 1L, "hi", u"hi", [1, 2, 3], (1, 2, 3), [(), {(): ()}]]: x = ray.put(val) objectid = x.id xval = ray.get(x) del x self.assertEqual(ray.scheduler_info()["reference_counts"][objectid], -1)
def testPassingArgumentsByValue(self): ray.init(num_workers=1) @ray.remote def f(x): return x ray.register_class(Exception) ray.register_class(CustomError) ray.register_class(Point) ray.register_class(Foo) ray.register_class(Bar) ray.register_class(Baz) ray.register_class(NamedTupleExample) # Check that we can pass arguments by value to remote functions and that # they are uncorrupted. for obj in RAY_TEST_OBJECTS: assert_equal(obj, ray.get(f.remote(obj))) ray.worker.cleanup()
# find most common word most_popular_word = None most_popular_ct = 0 for word, ct in ray.get(res).items(): if ct > most_popular_ct: most_popular_word = word most_popular_ct = ct print "most popular word is '{}' with count {}".format( most_popular_word, most_popular_ct) def chunks(l, n): chunk_size = (len(l) - 1) / n + 1 for i in xrange(0, len(l), chunk_size): yield l[i:i + chunk_size] if __name__ == '__main__': bench_env = raybench.Env() bench_env.ray_init() ray.register_class(type(dict_merge.remote), pickle=True) num_splits = bench_env.num_workers input_splits = init_wc(num_splits, 100000) print "number of splits", len(input_splits) do_wc(input_splits) ray.flush_log()
first_block = ray.get(self.objectids[(0,) * self.ndim]) dtype = first_block.dtype result = np.zeros(self.shape, dtype=dtype) for index in np.ndindex(*self.num_blocks): lower = DistArray.compute_block_lower(index, self.shape) upper = DistArray.compute_block_upper(index, self.shape) result[[slice(l, u) for (l, u) in zip(lower, upper)]] = ray.get(self.objectids[index]) return result def __getitem__(self, sliced): # TODO(rkn): fix this, this is just a placeholder that should work but is inefficient a = self.assemble() return a[sliced] # Register the DistArray class with Ray so that it knows how to serialize it. ray.register_class(DistArray) @ray.remote def assemble(a): return a.assemble() # TODO(rkn): what should we call this method @ray.remote def numpy_to_dist(a): result = DistArray(a.shape) for index in np.ndindex(*result.num_blocks): lower = DistArray.compute_block_lower(index, a.shape) upper = DistArray.compute_block_upper(index, a.shape) result.objectids[index] = ray.put(a[[slice(l, u) for (l, u) in zip(lower, upper)]]) return result
for index in np.ndindex(*self.num_blocks): lower = DistArray.compute_block_lower(index, self.shape) upper = DistArray.compute_block_upper(index, self.shape) result[[slice(l, u) for (l, u) in zip(lower, upper)]] = ray.get( self.objectids[index]) return result def __getitem__(self, sliced): # TODO(rkn): Fix this, this is just a placeholder that should work but is # inefficient. a = self.assemble() return a[sliced] # Register the DistArray class with Ray so that it knows how to serialize it. ray.register_class(DistArray) @ray.remote def assemble(a): return a.assemble() # TODO(rkn): What should we call this method? @ray.remote def numpy_to_dist(a): result = DistArray(a.shape) for index in np.ndindex(*result.num_blocks): lower = DistArray.compute_block_lower(index, a.shape) upper = DistArray.compute_block_upper(index, a.shape) result.objectids[index] = ray.put(a[[slice(l, u) for (l, u)
def testMethods(self): for module in [ ra.core, ra.random, ra.linalg, da.core, da.random, da.linalg ]: reload(module) ray.init(start_ray_local=True, num_objstores=2, num_workers=10) ray.register_class(da.DistArray) x = da.zeros.remote([9, 25, 51], "float") assert_equal(ray.get(da.assemble.remote(x)), np.zeros([9, 25, 51])) x = da.ones.remote([11, 25, 49], dtype_name="float") assert_equal(ray.get(da.assemble.remote(x)), np.ones([11, 25, 49])) x = da.random.normal.remote([11, 25, 49]) y = da.copy.remote(x) assert_equal(ray.get(da.assemble.remote(x)), ray.get(da.assemble.remote(y))) x = da.eye.remote(25, dtype_name="float") assert_equal(ray.get(da.assemble.remote(x)), np.eye(25)) x = da.random.normal.remote([25, 49]) y = da.triu.remote(x) assert_equal(ray.get(da.assemble.remote(y)), np.triu(ray.get(da.assemble.remote(x)))) x = da.random.normal.remote([25, 49]) y = da.tril.remote(x) assert_equal(ray.get(da.assemble.remote(y)), np.tril(ray.get(da.assemble.remote(x)))) x = da.random.normal.remote([25, 49]) y = da.random.normal.remote([49, 18]) z = da.dot.remote(x, y) w = da.assemble.remote(z) u = da.assemble.remote(x) v = da.assemble.remote(y) assert_almost_equal(ray.get(w), np.dot(ray.get(u), ray.get(v))) assert_almost_equal(ray.get(w), np.dot(ray.get(u), ray.get(v))) # test add x = da.random.normal.remote([23, 42]) y = da.random.normal.remote([23, 42]) z = da.add.remote(x, y) assert_almost_equal( ray.get(da.assemble.remote(z)), ray.get(da.assemble.remote(x)) + ray.get(da.assemble.remote(y))) # test subtract x = da.random.normal.remote([33, 40]) y = da.random.normal.remote([33, 40]) z = da.subtract.remote(x, y) assert_almost_equal( ray.get(da.assemble.remote(z)), ray.get(da.assemble.remote(x)) - ray.get(da.assemble.remote(y))) # test transpose x = da.random.normal.remote([234, 432]) y = da.transpose.remote(x) assert_equal( ray.get(da.assemble.remote(x)).T, ray.get(da.assemble.remote(y))) # test numpy_to_dist x = da.random.normal.remote([23, 45]) y = da.assemble.remote(x) z = da.numpy_to_dist.remote(y) w = da.assemble.remote(z) assert_equal(ray.get(da.assemble.remote(x)), ray.get(da.assemble.remote(z))) assert_equal(ray.get(y), ray.get(w)) # test da.tsqr for shape in [[123, da.BLOCK_SIZE], [7, da.BLOCK_SIZE], [da.BLOCK_SIZE, da.BLOCK_SIZE], [da.BLOCK_SIZE, 7], [10 * da.BLOCK_SIZE, da.BLOCK_SIZE]]: x = da.random.normal.remote(shape) K = min(shape) q, r = da.linalg.tsqr.remote(x) x_val = ray.get(da.assemble.remote(x)) q_val = ray.get(da.assemble.remote(q)) r_val = ray.get(r) self.assertTrue(r_val.shape == (K, shape[1])) assert_equal(r_val, np.triu(r_val)) assert_almost_equal(x_val, np.dot(q_val, r_val)) assert_almost_equal(np.dot(q_val.T, q_val), np.eye(K)) # test da.linalg.modified_lu def test_modified_lu(d1, d2): print "testing dist_modified_lu with d1 = " + str( d1) + ", d2 = " + str(d2) assert d1 >= d2 k = min(d1, d2) m = ra.random.normal.remote([d1, d2]) q, r = ra.linalg.qr.remote(m) l, u, s = da.linalg.modified_lu.remote(da.numpy_to_dist.remote(q)) q_val = ray.get(q) r_val = ray.get(r) l_val = ray.get(da.assemble.remote(l)) u_val = ray.get(u) s_val = ray.get(s) s_mat = np.zeros((d1, d2)) for i in range(len(s_val)): s_mat[i, i] = s_val[i] assert_almost_equal(q_val - s_mat, np.dot(l_val, u_val)) # check that q - s = l * u assert_equal(np.triu(u_val), u_val) # check that u is upper triangular assert_equal(np.tril(l_val), l_val) # check that l is lower triangular for d1, d2 in [(100, 100), (99, 98), (7, 5), (7, 7), (20, 7), (20, 10)]: test_modified_lu(d1, d2) # test dist_tsqr_hr def test_dist_tsqr_hr(d1, d2): print "testing dist_tsqr_hr with d1 = " + str( d1) + ", d2 = " + str(d2) a = da.random.normal.remote([d1, d2]) y, t, y_top, r = da.linalg.tsqr_hr.remote(a) a_val = ray.get(da.assemble.remote(a)) y_val = ray.get(da.assemble.remote(y)) t_val = ray.get(t) y_top_val = ray.get(y_top) r_val = ray.get(r) tall_eye = np.zeros((d1, min(d1, d2))) np.fill_diagonal(tall_eye, 1) q = tall_eye - np.dot(y_val, np.dot(t_val, y_top_val.T)) assert_almost_equal(np.dot(q.T, q), np.eye(min(d1, d2))) # check that q.T * q = I assert_almost_equal(np.dot( q, r_val), a_val) # check that a = (I - y * t * y_top.T) * r for d1, d2 in [(123, da.BLOCK_SIZE), (7, da.BLOCK_SIZE), (da.BLOCK_SIZE, da.BLOCK_SIZE), (da.BLOCK_SIZE, 7), (10 * da.BLOCK_SIZE, da.BLOCK_SIZE)]: test_dist_tsqr_hr(d1, d2) def test_dist_qr(d1, d2): print "testing qr with d1 = {}, and d2 = {}.".format(d1, d2) a = da.random.normal.remote([d1, d2]) K = min(d1, d2) q, r = da.linalg.qr.remote(a) a_val = ray.get(da.assemble.remote(a)) q_val = ray.get(da.assemble.remote(q)) r_val = ray.get(da.assemble.remote(r)) self.assertEqual(q_val.shape, (d1, K)) self.assertEqual(r_val.shape, (K, d2)) assert_almost_equal(np.dot(q_val.T, q_val), np.eye(K)) assert_equal(r_val, np.triu(r_val)) assert_almost_equal(a_val, np.dot(q_val, r_val)) for d1, d2 in [(123, da.BLOCK_SIZE), (7, da.BLOCK_SIZE), (da.BLOCK_SIZE, da.BLOCK_SIZE), (da.BLOCK_SIZE, 7), (13, 21), (34, 35), (8, 7)]: test_dist_qr(d1, d2) test_dist_qr(d2, d1) for _ in range(20): d1 = np.random.randint(1, 35) d2 = np.random.randint(1, 35) test_dist_qr(d1, d2) ray.worker.cleanup()
class BlockMatrix(object): def __init__(self, shape, block_size=10): assert len(shape) == 2 self.shape = shape self.block_size = block_size self.num_blocks = [ int(np.ceil(a / self.block_size)) for a in self.shape ] # The field "block_ids" will be a numpy array of object IDs, where each # object ID refers to a numpy array. self.block_ids = np.empty(self.num_blocks, dtype=object) # Note that we need to call ray.register_class so that we can pass BlockMatrix # objects to remote functions and return them from remote functions. ray.register_class(BlockMatrix) # This is a helper function which creates an array of zeros. @ray.remote def zeros_helper(shape): return np.zeros(shape) # EXERCISE: Define a remote function which returns a BlockMatrix of zeros. You # can assume that len(shape) == 2. You can do this as follows. # - First call the BlockMatrix constructor with the appropriate shape. # - Then call "zeros_helper" to create an object ID for each entry of # the "block_ids" field of the BlockMatrix. # # NOTE: You could call "zeros_helper" once for every block in the BlockMatrix,
parser = argparse.ArgumentParser( description="Run the policy gradient algorithm.") parser.add_argument("--environment", default="Pong-v0", type=str, help="The gym environment to use.") parser.add_argument("--redis-address", default=None, type=str, help="The Redis address of the cluster.") args = parser.parse_args() ray.init(redis_address=args.redis_address) ray.register_class(AtariRamPreprocessor) ray.register_class(AtariPixelPreprocessor) ray.register_class(NoPreprocessor) mdp_name = args.environment if args.environment == "Pong-v0": preprocessor = AtariPixelPreprocessor() elif mdp_name == "Pong-ram-v3": preprocessor = AtariRamPreprocessor() elif mdp_name == "CartPole-v0": preprocessor = NoPreprocessor() else: print("No environment was chosen, so defaulting to Pong-v0.") mdp_name = "Pong-v0" preprocessor = AtariPixelPreprocessor()