def test_duplicate_args(ray_start_regular_shared): @ray.remote def f(arg1, arg2, arg1_duplicate, kwarg1=None, kwarg2=None, kwarg1_duplicate=None): assert arg1 == kwarg1 assert arg1 != arg2 assert arg1 == arg1_duplicate assert kwarg1 != kwarg2 assert kwarg1 == kwarg1_duplicate # Test by-value arguments. arg1 = [1] arg2 = [2] ray.get( f.remote(arg1, arg2, arg1, kwarg1=arg1, kwarg2=arg2, kwarg1_duplicate=arg1)) # Test by-reference arguments. arg1 = ray.put([1]) arg2 = ray.put([2]) ray.get( f.remote(arg1, arg2, arg1, kwarg1=arg1, kwarg2=arg2, kwarg1_duplicate=arg1))
def test_illegal_api_calls(ray_start_regular): # Verify that we cannot call put on an ObjectRef. x = ray.put(1) with pytest.raises(Exception): ray.put(x) # Verify that we cannot call get on a regular value. with pytest.raises(Exception): ray.get(3)
def test_profiling_api(ray_start_2_cpus): @ray.remote def f(delay): with profiling.profile( "custom_event", extra_data={"name": "custom name"}): time.sleep(delay) pass @ray.remote def g(input_list): # The argument input_list should be a list containing one object ref. ray.wait([input_list[0]]) ray.put(1) x = f.remote(1) ray.get([g.remote([x]), g.remote([x])]) # Wait until all of the profiling information appears in the profile # table. timeout_seconds = 20 start_time = time.time() while True: profile_data = ray.timeline() event_types = {event["cat"] for event in profile_data} expected_types = [ "task", "task:deserialize_arguments", "task:execute", "task:store_outputs", "wait_for_function", "ray.get", "ray.put", "ray.wait", "submit_task", "fetch_and_run_function", # TODO (Alex) :https://github.com/ray-project/ray/pull/9346 # "register_remote_function", "custom_event", # This is the custom one from ray.profile. ] if all(expected_type in event_types for expected_type in expected_types): break if time.time() - start_time > timeout_seconds: raise RayTestTimeoutException( "Timed out while waiting for information in " "profile table. Missing events: {}.".format( set(expected_types) - set(event_types))) # The profiling information only flushes once every second. time.sleep(1.1)
def test_future_resolution_skip_plasma(ray_start_cluster): cluster = ray_start_cluster # Disable worker caching so worker leases are not reused; set object # inlining size threshold and enable storing of small objects in in-memory # object store so the borrowed ref is inlined. cluster.add_node( num_cpus=1, resources={"pin_head": 1}, _system_config={ "worker_lease_timeout_milliseconds": 0, "max_direct_call_object_size": 100 * 1024, "put_small_object_in_memory_store": True, }, ) cluster.add_node(num_cpus=1, resources={"pin_worker": 1}) ray.init(address=cluster.address) @ray.remote(resources={"pin_head": 1}) def f(x): return x + 1 @ray.remote(resources={"pin_worker": 1}) def g(x): borrowed_ref = x[0] f_ref = f.remote(borrowed_ref) # borrowed_ref should be inlined on future resolution and shouldn't be # in Plasma. assert ray.worker.global_worker.core_worker.object_exists( borrowed_ref, memory_store_only=True) return ray.get(f_ref) * 2 one = ray.put(1) g_ref = g.remote([one]) assert ray.get(g_ref) == 4
def _ensure_ref(self): if self._ref is None: # As before, set the state of the reference to be an # in-progress self reference value, which # the encoding can detect and handle correctly. self._ref = SelfReferenceSentinel() self._ref = ray.put(self.actor_cls)
def test_wait(ray_start_regular_shared): @ray.remote def f(delay): time.sleep(delay) return object_refs = [f.remote(0), f.remote(0), f.remote(0), f.remote(0)] ready_ids, remaining_ids = ray.wait(object_refs) assert len(ready_ids) == 1 assert len(remaining_ids) == 3 ready_ids, remaining_ids = ray.wait(object_refs, num_returns=4) assert set(ready_ids) == set(object_refs) assert remaining_ids == [] object_refs = [f.remote(0), f.remote(5)] ready_ids, remaining_ids = ray.wait(object_refs, timeout=0.5, num_returns=2) assert len(ready_ids) == 1 assert len(remaining_ids) == 1 # Verify that calling wait with duplicate object refs throws an # exception. x = ray.put(1) with pytest.raises(Exception): ray.wait([x, x]) # Make sure it is possible to call wait with an empty list. ready_ids, remaining_ids = ray.wait([]) assert ready_ids == [] assert remaining_ids == [] # Test semantics of num_returns with no timeout. obj_refs = [ray.put(i) for i in range(10)] (found, rest) = ray.wait(obj_refs, num_returns=2) assert len(found) == 2 assert len(rest) == 8 # Verify that incorrect usage raises a TypeError. x = ray.put(1) with pytest.raises(TypeError): ray.wait(x) with pytest.raises(TypeError): ray.wait(1) with pytest.raises(TypeError): ray.wait([1])
def _ensure_ref(self): with self._lock: if self._ref is None: # As before, set the state of the reference to be an # in-progress self reference value, which # the encoding can detect and handle correctly. self._ref = InProgressSentinel() self._ref = ray.put(self.actor_cls, client_ref_id=self._client_side_ref.id)
def test_get_multiple(ray_start_regular_shared): object_refs = [ray.put(i) for i in range(10)] assert ray.get(object_refs) == list(range(10)) # Get a random choice of object refs with duplicates. indices = list(np.random.choice(range(10), 5)) indices += indices results = ray.get([object_refs[i] for i in indices]) assert results == indices
def test_system_config_when_connecting(ray_start_cluster): config = {"object_timeout_milliseconds": 200} cluster = Cluster() cluster.add_node(_system_config=config, object_store_memory=100 * 1024 * 1024) cluster.wait_for_nodes() # Specifying _system_config when connecting to a cluster is disallowed. with pytest.raises(ValueError): ray.init(address=cluster.address, _system_config=config) # Check that the config was picked up (object pinning is disabled). ray.init(address=cluster.address) obj_ref = ray.put(np.zeros(40 * 1024 * 1024, dtype=np.uint8)) for _ in range(5): put_ref = ray.put(np.zeros(40 * 1024 * 1024, dtype=np.uint8)) del put_ref ray.get(obj_ref)
def _ensure_ref(self): with self._lock: if self._ref is None: # While calling ray.put() on our function, if # our function is recursive, it will attempt to # encode the ClientRemoteFunc -- itself -- and # infinitely recurse on _ensure_ref. # # So we set the state of the reference to be an # in-progress self reference value, which # the encoding can detect and handle correctly. self._ref = SelfReferenceSentinel() self._ref = ray.put(self._func)
def background_thread(self, wait_objects): try: # Test wait ready, _ = ray.wait( wait_objects, num_returns=len(wait_objects), timeout=1000.0, ) assert len(ready) == len(wait_objects) for _ in range(20): num = 10 # Test remote call results = [echo.remote(i) for i in range(num)] assert ray.get(results) == list(range(num)) # Test put and get objects = [ray.put(i) for i in range(num)] assert ray.get(objects) == list(range(num)) time.sleep(random.randint(0, 10) / 1000.0) except Exception as e: with self.lock: self.thread_results.append(e) else: with self.lock: self.thread_results.append("ok")
def test_put_and_get(): value = random.randint(0, 1000000) result = ray.get(ray.put(value)) assert value == result
return 1 # This hits the "nested tasks" issue # https://github.com/ray-project/ray/issues/3644 # So we're on the right track! return ray.get(fact.remote(x - 1)) * x @ray.remote def get_nodes(): return ray.nodes() # Can access the full Ray API in remote methods. print("Cluster nodes", ray.get(get_nodes.remote())) print(ray.nodes()) objectref = ray.put("hello world") # `ClientObjectRef(...)` print(objectref) # `hello world` print(ray.get(objectref)) ref2 = plus2.remote(234) # `ClientObjectRef(...)` print(ref2) # `236` print(ray.get(ref2)) ref3 = fact.remote(20) # `ClientObjectRef(...)`