Python ray.init() Examples

The following are 30 code examples of ray.init(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ray , or try the search function .
Example #1
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def testAttachingToCluster(self):
    node_ip_address = "127.0.0.1"
    scheduler_port = np.random.randint(40000, 50000)
    scheduler_address = "{}:{}".format(node_ip_address, scheduler_port)
    ray.services.start_scheduler(scheduler_address, cleanup=True)
    time.sleep(0.1)
    ray.services.start_node(scheduler_address, node_ip_address, num_workers=1, cleanup=True)

    ray.init(node_ip_address=node_ip_address, scheduler_address=scheduler_address)

    @ray.remote
    def f(x):
      return x + 1
    self.assertEqual(ray.get(f.remote(0)), 1)

    ray.worker.cleanup() 
Example #2
Source File: ray_fixtures.py    From garage with MIT License 7 votes vote down vote up
def ray_local_session_fixture():
    """Initializes Ray and shuts down Ray in local mode.

    Yields:
        None: Yield is for purposes of pytest module style.
            All statements before the yield are apart of module setup, and all
            statements after the yield are apart of module teardown.

    """
    if not ray.is_initialized():
        ray.init(local_mode=True,
                 ignore_reinit_error=True,
                 log_to_driver=False,
                 include_webui=False)
    yield
    if ray.is_initialized():
        ray.shutdown() 
Example #3
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testRunningFunctionOnAllWorkers(self):
    ray.init(start_ray_local=True, num_workers=1)

    def f(worker):
      sys.path.append("fake_directory")
    ray.worker.global_worker.run_function_on_all_workers(f)
    @ray.remote
    def get_path():
      return sys.path
    self.assertEqual("fake_directory", ray.get(get_path.remote())[-1])
    def f(worker):
      sys.path.pop(-1)
    ray.worker.global_worker.run_function_on_all_workers(f)
    self.assertTrue("fake_directory" not in ray.get(get_path.remote()))

    ray.worker.cleanup() 
Example #4
Source File: ray_two_machines.py    From ncluster with MIT License 6 votes vote down vote up
def run_driver():
  ray.init(redis_address=args.ip)

  worker = Worker.remote()
  ps = ParameterServer.remote()
  log = util.FileLogger('out')
  log(f"Worker ip {ray.get(worker.ip.remote())}")
  log(f"PS ip {ray.get(ps.ip.remote())}")
  log(f"Driver ip {socket.gethostbyname(socket.gethostname())}")

  time_list = []
  for i in range(args.iters):
    start_time = time.perf_counter()
    grads = worker.compute_gradients.remote()
    result = ps.receive.remote(grads)
    ray.wait([result])
    elapsed_time_ms = (time.perf_counter() - start_time)*1000
    time_list.append(elapsed_time_ms)
    rate = args.size_mb / (elapsed_time_ms/1000)
    log('%03d/%d sent %d MBs in %.1f ms: %.2f MB/second' % (i, args.iters, args.size_mb, elapsed_time_ms, rate))
    
  min = np.min(time_list)
  median = np.median(time_list)
  log(f"min: {min:8.2f}, median: {median:8.2f}, mean: {np.mean(time_list):8.2f}") 
Example #5
Source File: ray_executor.py    From rlgraph with Apache License 2.0 6 votes vote down vote up
def ray_init(self):
        """
        Connects to a Ray cluster or starts one if none exists.
        """
        self.logger.info("Initializing Ray cluster with executor spec:")
        for spec_key, value in self.executor_spec.items():
            self.logger.info("{}: {}".format(spec_key, value))

        # Avoiding accidentally starting local redis clusters.
        if 'redis_address' not in self.executor_spec:
            self.logger.warning("Warning: No redis address provided, starting local redis server.")
        ray.init(
            redis_address=self.executor_spec.get('redis_address', None),
            num_cpus=self.executor_spec.get('num_cpus', None),
            num_gpus=self.executor_spec.get('num_gpus', None)
        ) 
Example #6
Source File: test_apex_agent_long_task_learning.py    From rlgraph with Apache License 2.0 6 votes vote down vote up
def test_worker_update(self):
        """
        Tests if a worker can update from an external batch correct including all
        corrections and postprocessing using the pong spec.

        N.b. this test does not use Ray.
        """
        ray.init()
        agent_config = config_from_path("configs/ray_apex_for_pong.json")
        ray_spec = agent_config["execution_spec"].pop("ray_spec")
        worker_cls = RayValueWorker.as_remote().remote
        ray_spec["worker_spec"]["worker_sample_size"] = 198
        ray_spec["worker_spec"]["worker_executes_exploration"] = True
        ray_spec["worker_spec"]["ray_exploration"] = 0.4

        worker = worker_cls(agent_config, ray_spec["worker_spec"], self.env_spec,)
        time.sleep(5)

        start = time.perf_counter()
        task = worker.execute_and_get_with_count.remote()
        result, count = ray.get(task)
        task_time = time.perf_counter() - start
        print("internal result metrics = {}, external task time = {},"
              "external throughput = {}".format(result.get_metrics(), task_time, 198 / task_time)) 
Example #7
Source File: ray_two_machines_local.py    From ncluster with MIT License 6 votes vote down vote up
def run_driver():
  ray.init(redis_address=args.ip)

  worker = Worker.remote()
  ps = ParameterServer.remote()
  log = util.FileLogger('out')
  log(f"Worker ip {ray.get(worker.ip.remote())}")
  log(f"Driver ip {socket.gethostbyname(socket.gethostname())}")

  time_list = []
  for i in range(args.iters):
    start_time = time.perf_counter()
    grads = worker.compute_gradients.remote()
    result = ps.assign_add.remote(grads)
    result = ray.get(result)[0]
    elapsed_time_ms = (time.perf_counter() - start_time)*1000
    time_list.append(elapsed_time_ms)
    rate = args.size_mb / (elapsed_time_ms/1000)
    log('%03d/%d added %d MBs in %.1f ms: %.2f MB/second' % (i, args.iters, args.size_mb, elapsed_time_ms, rate))
    
  min = np.min(time_list)
  median = np.median(time_list)
  log(f"min: {min:8.2f}, median: {median:8.2f}, mean: {np.mean(time_list):8.2f}") 
Example #8
Source File: services.py    From ray with Apache License 2.0 6 votes vote down vote up
def remaining_processes_alive():
    """See if the remaining processes are alive or not.

    Note that this ignores processes that have been explicitly killed,
    e.g., via a command like node.kill_raylet().

    Returns:
        True if the remaining processes started by ray.init() are alive and
            False otherwise.

    Raises:
        Exception: An exception is raised if the processes were not started by
            ray.init().
    """
    if ray.worker._global_node is None:
        raise RuntimeError("This process is not in a position to determine "
                           "whether all processes are alive or not.")
    return ray.worker._global_node.remaining_processes_alive() 
Example #9
Source File: ray_fixtures.py    From garage with MIT License 6 votes vote down vote up
def ray_session_fixture():
    """Initializes Ray and shuts down Ray.

    Yields:
        None: Yield is for purposes of pytest module style.
            All statements before the yield are apart of module setup, and all
            statements after the yield are apart of module teardown.

    """
    if not ray.is_initialized():
        ray.init(memory=52428800,
                 object_store_memory=78643200,
                 ignore_reinit_error=True,
                 log_to_driver=False,
                 include_webui=False)
    yield
    if ray.is_initialized():
        ray.shutdown() 
Example #10
Source File: failure_test.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testFailedFunctionToRun(self):
    ray.init(start_ray_local=True, num_workers=2, driver_mode=ray.SILENT_MODE)

    def f(worker):
      if ray.worker.global_worker.mode == ray.WORKER_MODE:
        raise Exception("Function to run failed.")
    ray.worker.global_worker.run_function_on_all_workers(f)
    for _ in range(100): # Retry if we need to wait longer.
      if len(ray.task_info()["failed_function_to_runs"]) >= 2:
        break
      time.sleep(0.1)
    # Check that the error message is in the task info.
    self.assertEqual(len(ray.task_info()["failed_function_to_runs"]), 2)
    self.assertTrue("Function to run failed." in ray.task_info()["failed_function_to_runs"][0]["error_message"])
    self.assertTrue("Function to run failed." in ray.task_info()["failed_function_to_runs"][1]["error_message"])

    ray.worker.cleanup() 
Example #11
Source File: failure_test.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testFailImportingReusableVariable(self):
    ray.init(start_ray_local=True, num_workers=2, driver_mode=ray.SILENT_MODE)

    # This will throw an exception when the reusable variable is imported on the
    # workers.
    def initializer():
      if ray.worker.global_worker.mode == ray.WORKER_MODE:
        raise Exception("The initializer failed.")
      return 0
    ray.reusables.foo = ray.Reusable(initializer)
    for _ in range(100): # Retry if we need to wait longer.
      if len(ray.task_info()["failed_reusable_variable_imports"]) >= 1:
        break
      time.sleep(0.1)
    # Check that the error message is in the task info.
    self.assertTrue("The initializer failed." in ray.task_info()["failed_reusable_variable_imports"][0]["error_message"])

    ray.worker.cleanup() 
Example #12
Source File: multi_model_test.py    From dkeras with MIT License 6 votes vote down vote up
def main():
    """
    Test out using multiple models at the same time, this case is running
    ResNet50 and MobileNet
    """
    ray.init()
    n_data = 20

    model1 = dKeras(ResNet50, weights='imagenet', wait_for_workers=True, n_workers=3)
    model2 = dKeras(MobileNet, weights='imagenet', wait_for_workers=True, n_workers=3)

    test_data = np.random.uniform(-1, 1, (n_data, 224, 224, 3))

    model1.predict(test_data)
    model2.predict(test_data)

    model1.close()
    model2.close() 
Example #13
Source File: failure_test.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testFailReinitializingVariable(self):
    ray.init(start_ray_local=True, num_workers=2, driver_mode=ray.SILENT_MODE)

    def initializer():
      return 0
    def reinitializer(foo):
      raise Exception("The reinitializer failed.")
    ray.reusables.foo = ray.Reusable(initializer, reinitializer)
    @ray.remote
    def use_foo():
      ray.reusables.foo
    use_foo.remote()
    for _ in range(100): # Retry if we need to wait longer.
      if len(ray.task_info()["failed_reinitialize_reusable_variables"]) >= 1:
        break
      time.sleep(0.1)
    # Check that the error message is in the task info.
    self.assertTrue("The reinitializer failed." in ray.task_info()["failed_reinitialize_reusable_variables"][0]["error_message"])

    ray.worker.cleanup() 
Example #14
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testComputationGraph(self):
    ray.init(start_ray_local=True, num_workers=1)

    @ray.remote
    def f(x):
      return x
    @ray.remote
    def g(x, y):
      return x, y
    a = f.remote(1)
    b = f.remote(1)
    c = g.remote(a, b)
    c = g.remote(a, 1)
    # Make sure that we can produce a computation_graph visualization.
    ray.visualize_computation_graph(view=False)

    ray.worker.cleanup() 
Example #15
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testPythonMode(self):
    reload(test_functions)
    ray.init(start_ray_local=True, driver_mode=ray.PYTHON_MODE)

    @ray.remote
    def f():
      return np.ones([3, 4, 5])
    xref = f.remote()
    assert_equal(xref, np.ones([3, 4, 5])) # remote functions should return by value
    assert_equal(xref, ray.get(xref)) # ray.get should be the identity
    y = np.random.normal(size=[11, 12])
    assert_equal(y, ray.put(y)) # ray.put should be the identity

    # make sure objects are immutable, this example is why we need to copy
    # arguments before passing them into remote functions in python mode
    aref = test_functions.python_mode_f.remote()
    assert_equal(aref, np.array([0, 0]))
    bref = test_functions.python_mode_g.remote(aref)
    assert_equal(aref, np.array([0, 0])) # python_mode_g should not mutate aref
    assert_equal(bref, np.array([1, 0]))

    ray.worker.cleanup() 
Example #16
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #17
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testPassingArgumentsByValue(self):
    ray.init(start_ray_local=True, num_workers=0)

    # The types that can be passed by value are defined by
    # is_argument_serializable in serialization.py.
    class Foo(object):
      pass
    CAN_PASS_BY_VALUE = [1, 1L, 1.0, True, False, None, [1L, 1.0, True, None],
                         ([1, 2, 3], {False: [1.0, u"hi", ()]}), 100 * ["a"]]
    CANNOT_PASS_BY_VALUE = [int, np.int64(0), np.float64(0), Foo(), [Foo()],
                            (Foo()), {0: Foo()}, [[[int]]], 101 * [1],
                            np.zeros(10)]

    for obj in CAN_PASS_BY_VALUE:
      self.assertTrue(ray.serialization.is_argument_serializable(obj))
      self.assertEqual(obj, ray.serialization.deserialize_argument(ray.serialization.serialize_argument_if_possible(obj)))

    for obj in CANNOT_PASS_BY_VALUE:
      self.assertFalse(ray.serialization.is_argument_serializable(obj))
      self.assertEqual(None, ray.serialization.serialize_argument_if_possible(obj))

    ray.worker.cleanup() 
Example #18
Source File: hello_ray.py    From adeptRL with GNU General Public License v3.0 6 votes vote down vote up
def main_async():
    import asyncio
    from ray.experimental import async_api

    ray.init(num_cpus=4)
    remote_worker = Worker.remote()
    loop = asyncio.get_event_loop()

    t_zero = time.time()

    tasks = [
        async_api.as_future(remote_worker.sleep.remote(i)) for i in range(1, 3)
    ]
    loop.run_until_complete(asyncio.gather(tasks))

    print("delta", time.time() - t_zero) 
Example #19
Source File: basic_test.py    From dkeras with MIT License 6 votes vote down vote up
def main():
    ray.init(object_store_memory=4000000000)
    n_data = 80
    test_data = np.random.uniform(-1, 1, (n_data, 224, 224, 3))
    model = dKeras(ResNet50, weights='imagenet', init_ray=False, wait_for_workers=True, n_workers=4)

    start_time = time.time()
    preds = model.predict(test_data)
    elapsed = time.time() - start_time

    model.close()

    time.sleep(2)
    #
    # model = ResNet50(weights='imagenet')
    # preds2 = model.predict(test_data)
    #
    # # for i in range(n_data):
    # #     for j,n in enumerate(preds[i]):
    # #         print(n, preds2[i][j])
    # #     print('-'*80)

    print("Time elapsed: {}\nFPS: {}".format(elapsed, n_data / elapsed)) 
Example #20
Source File: failure_test.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testReturnAndPassUnknownType(self):
    ray.init(start_ray_local=True, num_workers=1, driver_mode=ray.SILENT_MODE)

    class Foo(object):
      pass
    # Check that returning an unknown type from a remote function raises an
    # exception.
    @ray.remote
    def f():
      return Foo()
    self.assertRaises(Exception, lambda : ray.get(f.remote()))
    # Check that passing an unknown type into a remote function raises an
    # exception.
    @ray.remote
    def g(x):
      return 1
    self.assertRaises(Exception, lambda : g.remote(Foo()))

    ray.worker.cleanup() 
Example #21
Source File: multi_predict_test.py    From dkeras with MIT License 6 votes vote down vote up
def main():
    ray.init()
    n_data = 20
    model = dKeras(ResNet50, weights='imagenet', wait_for_workers=True, n_workers=3)

    for i in range(5):
        test_data = np.random.uniform(-1, 1, (n_data, 224, 224, 3))
        start_time = time.time()
        preds_1 = model.predict(test_data)
        elapsed = time.time() - start_time
        print(np.asarray(preds_1).shape)
        print("Time elapsed: {}\nFPS: {}".format(elapsed, n_data / elapsed))



    model.close() 
Example #22
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testReferenceCountNone(self):
    ray.init(start_ray_local=True, num_workers=1)

    # Make sure that we aren't accidentally messing up Python's reference counts.
    @ray.remote
    def f():
      return sys.getrefcount(None)
    first_count = ray.get(f.remote())
    second_count = ray.get(f.remote())
    self.assertEqual(first_count, second_count)

    ray.worker.cleanup() 
Example #23
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testUsingReusablesOnDriver(self):
    ray.init(start_ray_local=True, num_workers=1)

    # Test that we can add a variable to the key-value store.

    def foo_initializer():
      return []
    def foo_reinitializer(foo):
      return []

    ray.reusables.foo = ray.Reusable(foo_initializer, foo_reinitializer)

    @ray.remote
    def use_foo():
      foo = ray.reusables.foo
      foo.append(1)
      return foo

    # Check that running a remote function does not reset the reusable variable
    # on the driver.
    foo = ray.reusables.foo
    self.assertEqual(foo, [])
    foo.append(2)
    self.assertEqual(foo, [2])
    foo.append(3)
    self.assertEqual(foo, [2, 3])

    self.assertEqual(ray.get(use_foo.remote()), [1])
    self.assertEqual(ray.get(use_foo.remote()), [1])
    self.assertEqual(ray.get(use_foo.remote()), [1])

    # Check that the copy of foo on the driver has not changed.
    self.assertEqual(foo, [2, 3])
    foo = ray.reusables.foo
    self.assertEqual(foo, [2, 3])

    ray.worker.cleanup() 
Example #24
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testReferenceCountFalse(self):
    ray.init(start_ray_local=True, num_workers=1)

    # Make sure that we aren't accidentally messing up Python's reference counts.
    @ray.remote
    def f():
      return sys.getrefcount(False)
    first_count = ray.get(f.remote())
    second_count = ray.get(f.remote())
    self.assertEqual(first_count, second_count)

    ray.worker.cleanup() 
Example #25
Source File: async_api.py    From ray with Apache License 2.0 5 votes vote down vote up
def init():
    """Initialize plasma event handlers for asyncio support."""
    assert ray.is_initialized(), "Please call ray.init before async_api.init"

    global handler
    if handler is None:
        worker = ray.worker.global_worker
        loop = asyncio.get_event_loop()
        handler = PlasmaEventHandler(loop, worker)
        worker.core_worker.set_plasma_added_callback(handler)
        logger.debug("AsyncPlasma Connection Created!") 
Example #26
Source File: async_api.py    From ray with Apache License 2.0 5 votes vote down vote up
def as_future(object_id):
    """Turn an object_id into a Future object.

    Args:
        object_id: A Ray object_id.

    Returns:
        PlasmaObjectFuture: A future object that waits the object_id.
    """
    if handler is None:
        init()
    return handler.as_future(object_id) 
Example #27
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)

    # Remote objects that contain numpy arrays should not be deallocated when
    # the corresponding ObjectID goes out of scope, if ray.get has been called
    # on the ObjectID and the result of that call is still in scope. 
Example #28
Source File: train_baseline.py    From sequential_social_dilemma_games with MIT License 5 votes vote down vote up
def main(unused_argv):
    ray.init(num_cpus=FLAGS.num_cpus, redirect_output=True)
    if FLAGS.env == 'harvest':
        hparams = harvest_default_params
    else:
        hparams = cleanup_default_params
    alg_run, env_name, config = setup(FLAGS.env, hparams, FLAGS.algorithm,
                                      FLAGS.train_batch_size,
                                      FLAGS.num_cpus,
                                      FLAGS.num_gpus, FLAGS.num_agents,
                                      FLAGS.use_gpus_for_workers,
                                      FLAGS.use_gpu_for_driver,
                                      FLAGS.num_workers_per_device)

    if FLAGS.exp_name is None:
        exp_name = FLAGS.env + '_' + FLAGS.algorithm
    else:
        exp_name = FLAGS.exp_name
    print('Commencing experiment', exp_name)

    run_experiments({
        exp_name: {
            "run": alg_run,
            "env": env_name,
            "stop": {
                "training_iteration": FLAGS.training_iterations
            },
            'checkpoint_freq': FLAGS.checkpoint_frequency,
            "config": config,
        }
    }) 
Example #29
Source File: async_test.py    From ray with Apache License 2.0 5 votes vote down vote up
def test_simple(init):
    @ray.remote
    def f():
        time.sleep(1)
        return np.zeros(1024 * 1024, dtype=np.uint8)

    future = async_api.as_future(f.remote())
    result = asyncio.get_event_loop().run_until_complete(future)
    assert isinstance(result, np.ndarray) 
Example #30
Source File: runtest.py    From ray-legacy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testReferenceCountTrue(self):
    ray.init(start_ray_local=True, num_workers=1)

    # Make sure that we aren't accidentally messing up Python's reference counts.
    @ray.remote
    def f():
      return sys.getrefcount(True)
    first_count = ray.get(f.remote())
    second_count = ray.get(f.remote())
    self.assertEqual(first_count, second_count)

    ray.worker.cleanup()