id int32 0 252k | repo stringlengths 7 55 | path stringlengths 4 127 | func_name stringlengths 1 88 | original_string stringlengths 75 19.8k | language stringclasses 1 value | code stringlengths 75 19.8k | code_tokens list | docstring stringlengths 3 17.3k | docstring_tokens list | sha stringlengths 40 40 | url stringlengths 87 242 |
|---|---|---|---|---|---|---|---|---|---|---|---|
24,600 | ray-project/ray | python/ray/experimental/async_plasma.py | PlasmaEventHandler.process_notifications | def process_notifications(self, messages):
"""Process notifications."""
for object_id, object_size, metadata_size in messages:
if object_size > 0 and object_id in self._waiting_dict:
linked_list = self._waiting_dict[object_id]
self._complete_future(linked_list) | python | def process_notifications(self, messages):
"""Process notifications."""
for object_id, object_size, metadata_size in messages:
if object_size > 0 and object_id in self._waiting_dict:
linked_list = self._waiting_dict[object_id]
self._complete_future(linked_list) | [
"def",
"process_notifications",
"(",
"self",
",",
"messages",
")",
":",
"for",
"object_id",
",",
"object_size",
",",
"metadata_size",
"in",
"messages",
":",
"if",
"object_size",
">",
"0",
"and",
"object_id",
"in",
"self",
".",
"_waiting_dict",
":",
"linked_list",
"=",
"self",
".",
"_waiting_dict",
"[",
"object_id",
"]",
"self",
".",
"_complete_future",
"(",
"linked_list",
")"
] | Process notifications. | [
"Process",
"notifications",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_plasma.py#L183-L188 |
24,601 | ray-project/ray | python/ray/experimental/async_plasma.py | PlasmaEventHandler.as_future | def as_future(self, object_id, check_ready=True):
"""Turn an object_id into a Future object.
Args:
object_id: A Ray's object_id.
check_ready (bool): If true, check if the object_id is ready.
Returns:
PlasmaObjectFuture: A future object that waits the object_id.
"""
if not isinstance(object_id, ray.ObjectID):
raise TypeError("Input should be an ObjectID.")
plain_object_id = plasma.ObjectID(object_id.binary())
fut = PlasmaObjectFuture(loop=self._loop, object_id=plain_object_id)
if check_ready:
ready, _ = ray.wait([object_id], timeout=0)
if ready:
if self._loop.get_debug():
logger.debug("%s has been ready.", plain_object_id)
self._complete_future(fut)
return fut
if plain_object_id not in self._waiting_dict:
linked_list = PlasmaObjectLinkedList(self._loop, plain_object_id)
linked_list.add_done_callback(self._unregister_callback)
self._waiting_dict[plain_object_id] = linked_list
self._waiting_dict[plain_object_id].append(fut)
if self._loop.get_debug():
logger.debug("%s added to the waiting list.", fut)
return fut | python | def as_future(self, object_id, check_ready=True):
"""Turn an object_id into a Future object.
Args:
object_id: A Ray's object_id.
check_ready (bool): If true, check if the object_id is ready.
Returns:
PlasmaObjectFuture: A future object that waits the object_id.
"""
if not isinstance(object_id, ray.ObjectID):
raise TypeError("Input should be an ObjectID.")
plain_object_id = plasma.ObjectID(object_id.binary())
fut = PlasmaObjectFuture(loop=self._loop, object_id=plain_object_id)
if check_ready:
ready, _ = ray.wait([object_id], timeout=0)
if ready:
if self._loop.get_debug():
logger.debug("%s has been ready.", plain_object_id)
self._complete_future(fut)
return fut
if plain_object_id not in self._waiting_dict:
linked_list = PlasmaObjectLinkedList(self._loop, plain_object_id)
linked_list.add_done_callback(self._unregister_callback)
self._waiting_dict[plain_object_id] = linked_list
self._waiting_dict[plain_object_id].append(fut)
if self._loop.get_debug():
logger.debug("%s added to the waiting list.", fut)
return fut | [
"def",
"as_future",
"(",
"self",
",",
"object_id",
",",
"check_ready",
"=",
"True",
")",
":",
"if",
"not",
"isinstance",
"(",
"object_id",
",",
"ray",
".",
"ObjectID",
")",
":",
"raise",
"TypeError",
"(",
"\"Input should be an ObjectID.\"",
")",
"plain_object_id",
"=",
"plasma",
".",
"ObjectID",
"(",
"object_id",
".",
"binary",
"(",
")",
")",
"fut",
"=",
"PlasmaObjectFuture",
"(",
"loop",
"=",
"self",
".",
"_loop",
",",
"object_id",
"=",
"plain_object_id",
")",
"if",
"check_ready",
":",
"ready",
",",
"_",
"=",
"ray",
".",
"wait",
"(",
"[",
"object_id",
"]",
",",
"timeout",
"=",
"0",
")",
"if",
"ready",
":",
"if",
"self",
".",
"_loop",
".",
"get_debug",
"(",
")",
":",
"logger",
".",
"debug",
"(",
"\"%s has been ready.\"",
",",
"plain_object_id",
")",
"self",
".",
"_complete_future",
"(",
"fut",
")",
"return",
"fut",
"if",
"plain_object_id",
"not",
"in",
"self",
".",
"_waiting_dict",
":",
"linked_list",
"=",
"PlasmaObjectLinkedList",
"(",
"self",
".",
"_loop",
",",
"plain_object_id",
")",
"linked_list",
".",
"add_done_callback",
"(",
"self",
".",
"_unregister_callback",
")",
"self",
".",
"_waiting_dict",
"[",
"plain_object_id",
"]",
"=",
"linked_list",
"self",
".",
"_waiting_dict",
"[",
"plain_object_id",
"]",
".",
"append",
"(",
"fut",
")",
"if",
"self",
".",
"_loop",
".",
"get_debug",
"(",
")",
":",
"logger",
".",
"debug",
"(",
"\"%s added to the waiting list.\"",
",",
"fut",
")",
"return",
"fut"
] | Turn an object_id into a Future object.
Args:
object_id: A Ray's object_id.
check_ready (bool): If true, check if the object_id is ready.
Returns:
PlasmaObjectFuture: A future object that waits the object_id. | [
"Turn",
"an",
"object_id",
"into",
"a",
"Future",
"object",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_plasma.py#L205-L237 |
24,602 | ray-project/ray | python/ray/tune/web_server.py | TuneClient.get_all_trials | def get_all_trials(self):
"""Returns a list of all trials' information."""
response = requests.get(urljoin(self._path, "trials"))
return self._deserialize(response) | python | def get_all_trials(self):
"""Returns a list of all trials' information."""
response = requests.get(urljoin(self._path, "trials"))
return self._deserialize(response) | [
"def",
"get_all_trials",
"(",
"self",
")",
":",
"response",
"=",
"requests",
".",
"get",
"(",
"urljoin",
"(",
"self",
".",
"_path",
",",
"\"trials\"",
")",
")",
"return",
"self",
".",
"_deserialize",
"(",
"response",
")"
] | Returns a list of all trials' information. | [
"Returns",
"a",
"list",
"of",
"all",
"trials",
"information",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/web_server.py#L48-L51 |
24,603 | ray-project/ray | python/ray/tune/web_server.py | TuneClient.get_trial | def get_trial(self, trial_id):
"""Returns trial information by trial_id."""
response = requests.get(
urljoin(self._path, "trials/{}".format(trial_id)))
return self._deserialize(response) | python | def get_trial(self, trial_id):
"""Returns trial information by trial_id."""
response = requests.get(
urljoin(self._path, "trials/{}".format(trial_id)))
return self._deserialize(response) | [
"def",
"get_trial",
"(",
"self",
",",
"trial_id",
")",
":",
"response",
"=",
"requests",
".",
"get",
"(",
"urljoin",
"(",
"self",
".",
"_path",
",",
"\"trials/{}\"",
".",
"format",
"(",
"trial_id",
")",
")",
")",
"return",
"self",
".",
"_deserialize",
"(",
"response",
")"
] | Returns trial information by trial_id. | [
"Returns",
"trial",
"information",
"by",
"trial_id",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/web_server.py#L53-L57 |
24,604 | ray-project/ray | python/ray/tune/web_server.py | TuneClient.stop_trial | def stop_trial(self, trial_id):
"""Requests to stop trial by trial_id."""
response = requests.put(
urljoin(self._path, "trials/{}".format(trial_id)))
return self._deserialize(response) | python | def stop_trial(self, trial_id):
"""Requests to stop trial by trial_id."""
response = requests.put(
urljoin(self._path, "trials/{}".format(trial_id)))
return self._deserialize(response) | [
"def",
"stop_trial",
"(",
"self",
",",
"trial_id",
")",
":",
"response",
"=",
"requests",
".",
"put",
"(",
"urljoin",
"(",
"self",
".",
"_path",
",",
"\"trials/{}\"",
".",
"format",
"(",
"trial_id",
")",
")",
")",
"return",
"self",
".",
"_deserialize",
"(",
"response",
")"
] | Requests to stop trial by trial_id. | [
"Requests",
"to",
"stop",
"trial",
"by",
"trial_id",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/web_server.py#L65-L69 |
24,605 | ray-project/ray | python/ray/experimental/sgd/sgd.py | DistributedSGD.foreach_worker | def foreach_worker(self, fn):
"""Apply the given function to each remote worker.
Returns:
List of results from applying the function.
"""
results = ray.get([w.foreach_worker.remote(fn) for w in self.workers])
return results | python | def foreach_worker(self, fn):
"""Apply the given function to each remote worker.
Returns:
List of results from applying the function.
"""
results = ray.get([w.foreach_worker.remote(fn) for w in self.workers])
return results | [
"def",
"foreach_worker",
"(",
"self",
",",
"fn",
")",
":",
"results",
"=",
"ray",
".",
"get",
"(",
"[",
"w",
".",
"foreach_worker",
".",
"remote",
"(",
"fn",
")",
"for",
"w",
"in",
"self",
".",
"workers",
"]",
")",
"return",
"results"
] | Apply the given function to each remote worker.
Returns:
List of results from applying the function. | [
"Apply",
"the",
"given",
"function",
"to",
"each",
"remote",
"worker",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/sgd.py#L130-L137 |
24,606 | ray-project/ray | python/ray/experimental/sgd/sgd.py | DistributedSGD.foreach_model | def foreach_model(self, fn):
"""Apply the given function to each model replica in each worker.
Returns:
List of results from applying the function.
"""
results = ray.get([w.foreach_model.remote(fn) for w in self.workers])
out = []
for r in results:
out.extend(r)
return out | python | def foreach_model(self, fn):
"""Apply the given function to each model replica in each worker.
Returns:
List of results from applying the function.
"""
results = ray.get([w.foreach_model.remote(fn) for w in self.workers])
out = []
for r in results:
out.extend(r)
return out | [
"def",
"foreach_model",
"(",
"self",
",",
"fn",
")",
":",
"results",
"=",
"ray",
".",
"get",
"(",
"[",
"w",
".",
"foreach_model",
".",
"remote",
"(",
"fn",
")",
"for",
"w",
"in",
"self",
".",
"workers",
"]",
")",
"out",
"=",
"[",
"]",
"for",
"r",
"in",
"results",
":",
"out",
".",
"extend",
"(",
"r",
")",
"return",
"out"
] | Apply the given function to each model replica in each worker.
Returns:
List of results from applying the function. | [
"Apply",
"the",
"given",
"function",
"to",
"each",
"model",
"replica",
"in",
"each",
"worker",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/sgd.py#L139-L150 |
24,607 | ray-project/ray | python/ray/experimental/sgd/sgd.py | DistributedSGD.for_model | def for_model(self, fn):
"""Apply the given function to a single model replica.
Returns:
Result from applying the function.
"""
return ray.get(self.workers[0].for_model.remote(fn)) | python | def for_model(self, fn):
"""Apply the given function to a single model replica.
Returns:
Result from applying the function.
"""
return ray.get(self.workers[0].for_model.remote(fn)) | [
"def",
"for_model",
"(",
"self",
",",
"fn",
")",
":",
"return",
"ray",
".",
"get",
"(",
"self",
".",
"workers",
"[",
"0",
"]",
".",
"for_model",
".",
"remote",
"(",
"fn",
")",
")"
] | Apply the given function to a single model replica.
Returns:
Result from applying the function. | [
"Apply",
"the",
"given",
"function",
"to",
"a",
"single",
"model",
"replica",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/sgd.py#L152-L158 |
24,608 | ray-project/ray | python/ray/experimental/sgd/sgd.py | DistributedSGD.step | def step(self, fetch_stats=False):
"""Run a single SGD step.
Arguments:
fetch_stats (bool): Whether to return stats from the step. This can
slow down the computation by acting as a global barrier.
"""
if self.strategy == "ps":
return _distributed_sgd_step(
self.workers,
self.ps_list,
write_timeline=False,
fetch_stats=fetch_stats)
else:
return _simple_sgd_step(self.workers) | python | def step(self, fetch_stats=False):
"""Run a single SGD step.
Arguments:
fetch_stats (bool): Whether to return stats from the step. This can
slow down the computation by acting as a global barrier.
"""
if self.strategy == "ps":
return _distributed_sgd_step(
self.workers,
self.ps_list,
write_timeline=False,
fetch_stats=fetch_stats)
else:
return _simple_sgd_step(self.workers) | [
"def",
"step",
"(",
"self",
",",
"fetch_stats",
"=",
"False",
")",
":",
"if",
"self",
".",
"strategy",
"==",
"\"ps\"",
":",
"return",
"_distributed_sgd_step",
"(",
"self",
".",
"workers",
",",
"self",
".",
"ps_list",
",",
"write_timeline",
"=",
"False",
",",
"fetch_stats",
"=",
"fetch_stats",
")",
"else",
":",
"return",
"_simple_sgd_step",
"(",
"self",
".",
"workers",
")"
] | Run a single SGD step.
Arguments:
fetch_stats (bool): Whether to return stats from the step. This can
slow down the computation by acting as a global barrier. | [
"Run",
"a",
"single",
"SGD",
"step",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/sgd.py#L160-L174 |
24,609 | ray-project/ray | python/ray/experimental/serve/router/__init__.py | start_router | def start_router(router_class, router_name):
"""Wrapper for starting a router and register it.
Args:
router_class: The router class to instantiate.
router_name: The name to give to the router.
Returns:
A handle to newly started router actor.
"""
handle = router_class.remote(router_name)
ray.experimental.register_actor(router_name, handle)
handle.start.remote()
return handle | python | def start_router(router_class, router_name):
"""Wrapper for starting a router and register it.
Args:
router_class: The router class to instantiate.
router_name: The name to give to the router.
Returns:
A handle to newly started router actor.
"""
handle = router_class.remote(router_name)
ray.experimental.register_actor(router_name, handle)
handle.start.remote()
return handle | [
"def",
"start_router",
"(",
"router_class",
",",
"router_name",
")",
":",
"handle",
"=",
"router_class",
".",
"remote",
"(",
"router_name",
")",
"ray",
".",
"experimental",
".",
"register_actor",
"(",
"router_name",
",",
"handle",
")",
"handle",
".",
"start",
".",
"remote",
"(",
")",
"return",
"handle"
] | Wrapper for starting a router and register it.
Args:
router_class: The router class to instantiate.
router_name: The name to give to the router.
Returns:
A handle to newly started router actor. | [
"Wrapper",
"for",
"starting",
"a",
"router",
"and",
"register",
"it",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/serve/router/__init__.py#L10-L23 |
24,610 | ray-project/ray | python/ray/tune/automl/search_space.py | SearchSpace.generate_random_one_hot_encoding | def generate_random_one_hot_encoding(self):
"""Returns a list of one-hot encodings for all parameters.
1 one-hot np.array for 1 parameter,
and the 1's place is randomly chosen.
"""
encoding = []
for ps in self.param_list:
one_hot = np.zeros(ps.choices_count())
choice = random.randrange(ps.choices_count())
one_hot[choice] = 1
encoding.append(one_hot)
return encoding | python | def generate_random_one_hot_encoding(self):
"""Returns a list of one-hot encodings for all parameters.
1 one-hot np.array for 1 parameter,
and the 1's place is randomly chosen.
"""
encoding = []
for ps in self.param_list:
one_hot = np.zeros(ps.choices_count())
choice = random.randrange(ps.choices_count())
one_hot[choice] = 1
encoding.append(one_hot)
return encoding | [
"def",
"generate_random_one_hot_encoding",
"(",
"self",
")",
":",
"encoding",
"=",
"[",
"]",
"for",
"ps",
"in",
"self",
".",
"param_list",
":",
"one_hot",
"=",
"np",
".",
"zeros",
"(",
"ps",
".",
"choices_count",
"(",
")",
")",
"choice",
"=",
"random",
".",
"randrange",
"(",
"ps",
".",
"choices_count",
"(",
")",
")",
"one_hot",
"[",
"choice",
"]",
"=",
"1",
"encoding",
".",
"append",
"(",
"one_hot",
")",
"return",
"encoding"
] | Returns a list of one-hot encodings for all parameters.
1 one-hot np.array for 1 parameter,
and the 1's place is randomly chosen. | [
"Returns",
"a",
"list",
"of",
"one",
"-",
"hot",
"encodings",
"for",
"all",
"parameters",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/automl/search_space.py#L153-L165 |
24,611 | ray-project/ray | python/ray/tune/automl/search_space.py | SearchSpace.apply_one_hot_encoding | def apply_one_hot_encoding(self, one_hot_encoding):
"""Apply one hot encoding to generate a specific config.
Arguments:
one_hot_encoding (list): A list of one hot encodings,
1 for each parameter. The shape of each encoding
should match that ``ParameterSpace``
Returns:
A dict config with specific <name, value> pair
"""
config = {}
for ps, one_hot in zip(self.param_list, one_hot_encoding):
index = np.argmax(one_hot)
config[ps.name] = ps.choices[index]
return config | python | def apply_one_hot_encoding(self, one_hot_encoding):
"""Apply one hot encoding to generate a specific config.
Arguments:
one_hot_encoding (list): A list of one hot encodings,
1 for each parameter. The shape of each encoding
should match that ``ParameterSpace``
Returns:
A dict config with specific <name, value> pair
"""
config = {}
for ps, one_hot in zip(self.param_list, one_hot_encoding):
index = np.argmax(one_hot)
config[ps.name] = ps.choices[index]
return config | [
"def",
"apply_one_hot_encoding",
"(",
"self",
",",
"one_hot_encoding",
")",
":",
"config",
"=",
"{",
"}",
"for",
"ps",
",",
"one_hot",
"in",
"zip",
"(",
"self",
".",
"param_list",
",",
"one_hot_encoding",
")",
":",
"index",
"=",
"np",
".",
"argmax",
"(",
"one_hot",
")",
"config",
"[",
"ps",
".",
"name",
"]",
"=",
"ps",
".",
"choices",
"[",
"index",
"]",
"return",
"config"
] | Apply one hot encoding to generate a specific config.
Arguments:
one_hot_encoding (list): A list of one hot encodings,
1 for each parameter. The shape of each encoding
should match that ``ParameterSpace``
Returns:
A dict config with specific <name, value> pair | [
"Apply",
"one",
"hot",
"encoding",
"to",
"generate",
"a",
"specific",
"config",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/automl/search_space.py#L167-L183 |
24,612 | ray-project/ray | python/ray/tune/util.py | pin_in_object_store | def pin_in_object_store(obj):
"""Pin an object in the object store.
It will be available as long as the pinning process is alive. The pinned
object can be retrieved by calling get_pinned_object on the identifier
returned by this call.
"""
obj_id = ray.put(_to_pinnable(obj))
_pinned_objects.append(ray.get(obj_id))
return "{}{}".format(PINNED_OBJECT_PREFIX,
base64.b64encode(obj_id.binary()).decode("utf-8")) | python | def pin_in_object_store(obj):
"""Pin an object in the object store.
It will be available as long as the pinning process is alive. The pinned
object can be retrieved by calling get_pinned_object on the identifier
returned by this call.
"""
obj_id = ray.put(_to_pinnable(obj))
_pinned_objects.append(ray.get(obj_id))
return "{}{}".format(PINNED_OBJECT_PREFIX,
base64.b64encode(obj_id.binary()).decode("utf-8")) | [
"def",
"pin_in_object_store",
"(",
"obj",
")",
":",
"obj_id",
"=",
"ray",
".",
"put",
"(",
"_to_pinnable",
"(",
"obj",
")",
")",
"_pinned_objects",
".",
"append",
"(",
"ray",
".",
"get",
"(",
"obj_id",
")",
")",
"return",
"\"{}{}\"",
".",
"format",
"(",
"PINNED_OBJECT_PREFIX",
",",
"base64",
".",
"b64encode",
"(",
"obj_id",
".",
"binary",
"(",
")",
")",
".",
"decode",
"(",
"\"utf-8\"",
")",
")"
] | Pin an object in the object store.
It will be available as long as the pinning process is alive. The pinned
object can be retrieved by calling get_pinned_object on the identifier
returned by this call. | [
"Pin",
"an",
"object",
"in",
"the",
"object",
"store",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/util.py#L19-L30 |
24,613 | ray-project/ray | python/ray/tune/util.py | get_pinned_object | def get_pinned_object(pinned_id):
"""Retrieve a pinned object from the object store."""
from ray import ObjectID
return _from_pinnable(
ray.get(
ObjectID(base64.b64decode(pinned_id[len(PINNED_OBJECT_PREFIX):])))) | python | def get_pinned_object(pinned_id):
"""Retrieve a pinned object from the object store."""
from ray import ObjectID
return _from_pinnable(
ray.get(
ObjectID(base64.b64decode(pinned_id[len(PINNED_OBJECT_PREFIX):])))) | [
"def",
"get_pinned_object",
"(",
"pinned_id",
")",
":",
"from",
"ray",
"import",
"ObjectID",
"return",
"_from_pinnable",
"(",
"ray",
".",
"get",
"(",
"ObjectID",
"(",
"base64",
".",
"b64decode",
"(",
"pinned_id",
"[",
"len",
"(",
"PINNED_OBJECT_PREFIX",
")",
":",
"]",
")",
")",
")",
")"
] | Retrieve a pinned object from the object store. | [
"Retrieve",
"a",
"pinned",
"object",
"from",
"the",
"object",
"store",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/util.py#L33-L40 |
24,614 | ray-project/ray | python/ray/tune/util.py | merge_dicts | def merge_dicts(d1, d2):
"""Returns a new dict that is d1 and d2 deep merged."""
merged = copy.deepcopy(d1)
deep_update(merged, d2, True, [])
return merged | python | def merge_dicts(d1, d2):
"""Returns a new dict that is d1 and d2 deep merged."""
merged = copy.deepcopy(d1)
deep_update(merged, d2, True, [])
return merged | [
"def",
"merge_dicts",
"(",
"d1",
",",
"d2",
")",
":",
"merged",
"=",
"copy",
".",
"deepcopy",
"(",
"d1",
")",
"deep_update",
"(",
"merged",
",",
"d2",
",",
"True",
",",
"[",
"]",
")",
"return",
"merged"
] | Returns a new dict that is d1 and d2 deep merged. | [
"Returns",
"a",
"new",
"dict",
"that",
"is",
"d1",
"and",
"d2",
"deep",
"merged",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/util.py#L65-L69 |
24,615 | ray-project/ray | python/ray/tune/util.py | deep_update | def deep_update(original, new_dict, new_keys_allowed, whitelist):
"""Updates original dict with values from new_dict recursively.
If new key is introduced in new_dict, then if new_keys_allowed is not
True, an error will be thrown. Further, for sub-dicts, if the key is
in the whitelist, then new subkeys can be introduced.
Args:
original (dict): Dictionary with default values.
new_dict (dict): Dictionary with values to be updated
new_keys_allowed (bool): Whether new keys are allowed.
whitelist (list): List of keys that correspond to dict values
where new subkeys can be introduced. This is only at
the top level.
"""
for k, value in new_dict.items():
if k not in original:
if not new_keys_allowed:
raise Exception("Unknown config parameter `{}` ".format(k))
if isinstance(original.get(k), dict):
if k in whitelist:
deep_update(original[k], value, True, [])
else:
deep_update(original[k], value, new_keys_allowed, [])
else:
original[k] = value
return original | python | def deep_update(original, new_dict, new_keys_allowed, whitelist):
"""Updates original dict with values from new_dict recursively.
If new key is introduced in new_dict, then if new_keys_allowed is not
True, an error will be thrown. Further, for sub-dicts, if the key is
in the whitelist, then new subkeys can be introduced.
Args:
original (dict): Dictionary with default values.
new_dict (dict): Dictionary with values to be updated
new_keys_allowed (bool): Whether new keys are allowed.
whitelist (list): List of keys that correspond to dict values
where new subkeys can be introduced. This is only at
the top level.
"""
for k, value in new_dict.items():
if k not in original:
if not new_keys_allowed:
raise Exception("Unknown config parameter `{}` ".format(k))
if isinstance(original.get(k), dict):
if k in whitelist:
deep_update(original[k], value, True, [])
else:
deep_update(original[k], value, new_keys_allowed, [])
else:
original[k] = value
return original | [
"def",
"deep_update",
"(",
"original",
",",
"new_dict",
",",
"new_keys_allowed",
",",
"whitelist",
")",
":",
"for",
"k",
",",
"value",
"in",
"new_dict",
".",
"items",
"(",
")",
":",
"if",
"k",
"not",
"in",
"original",
":",
"if",
"not",
"new_keys_allowed",
":",
"raise",
"Exception",
"(",
"\"Unknown config parameter `{}` \"",
".",
"format",
"(",
"k",
")",
")",
"if",
"isinstance",
"(",
"original",
".",
"get",
"(",
"k",
")",
",",
"dict",
")",
":",
"if",
"k",
"in",
"whitelist",
":",
"deep_update",
"(",
"original",
"[",
"k",
"]",
",",
"value",
",",
"True",
",",
"[",
"]",
")",
"else",
":",
"deep_update",
"(",
"original",
"[",
"k",
"]",
",",
"value",
",",
"new_keys_allowed",
",",
"[",
"]",
")",
"else",
":",
"original",
"[",
"k",
"]",
"=",
"value",
"return",
"original"
] | Updates original dict with values from new_dict recursively.
If new key is introduced in new_dict, then if new_keys_allowed is not
True, an error will be thrown. Further, for sub-dicts, if the key is
in the whitelist, then new subkeys can be introduced.
Args:
original (dict): Dictionary with default values.
new_dict (dict): Dictionary with values to be updated
new_keys_allowed (bool): Whether new keys are allowed.
whitelist (list): List of keys that correspond to dict values
where new subkeys can be introduced. This is only at
the top level. | [
"Updates",
"original",
"dict",
"with",
"values",
"from",
"new_dict",
"recursively",
".",
"If",
"new",
"key",
"is",
"introduced",
"in",
"new_dict",
"then",
"if",
"new_keys_allowed",
"is",
"not",
"True",
"an",
"error",
"will",
"be",
"thrown",
".",
"Further",
"for",
"sub",
"-",
"dicts",
"if",
"the",
"key",
"is",
"in",
"the",
"whitelist",
"then",
"new",
"subkeys",
"can",
"be",
"introduced",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/util.py#L72-L97 |
24,616 | ray-project/ray | python/ray/rllib/utils/actors.py | TaskPool.completed_prefetch | def completed_prefetch(self, blocking_wait=False, max_yield=999):
"""Similar to completed but only returns once the object is local.
Assumes obj_id only is one id."""
for worker, obj_id in self.completed(blocking_wait=blocking_wait):
plasma_id = ray.pyarrow.plasma.ObjectID(obj_id.binary())
(ray.worker.global_worker.raylet_client.fetch_or_reconstruct(
[obj_id], True))
self._fetching.append((worker, obj_id))
remaining = []
num_yielded = 0
for worker, obj_id in self._fetching:
plasma_id = ray.pyarrow.plasma.ObjectID(obj_id.binary())
if (num_yielded < max_yield
and ray.worker.global_worker.plasma_client.contains(
plasma_id)):
yield (worker, obj_id)
num_yielded += 1
else:
remaining.append((worker, obj_id))
self._fetching = remaining | python | def completed_prefetch(self, blocking_wait=False, max_yield=999):
"""Similar to completed but only returns once the object is local.
Assumes obj_id only is one id."""
for worker, obj_id in self.completed(blocking_wait=blocking_wait):
plasma_id = ray.pyarrow.plasma.ObjectID(obj_id.binary())
(ray.worker.global_worker.raylet_client.fetch_or_reconstruct(
[obj_id], True))
self._fetching.append((worker, obj_id))
remaining = []
num_yielded = 0
for worker, obj_id in self._fetching:
plasma_id = ray.pyarrow.plasma.ObjectID(obj_id.binary())
if (num_yielded < max_yield
and ray.worker.global_worker.plasma_client.contains(
plasma_id)):
yield (worker, obj_id)
num_yielded += 1
else:
remaining.append((worker, obj_id))
self._fetching = remaining | [
"def",
"completed_prefetch",
"(",
"self",
",",
"blocking_wait",
"=",
"False",
",",
"max_yield",
"=",
"999",
")",
":",
"for",
"worker",
",",
"obj_id",
"in",
"self",
".",
"completed",
"(",
"blocking_wait",
"=",
"blocking_wait",
")",
":",
"plasma_id",
"=",
"ray",
".",
"pyarrow",
".",
"plasma",
".",
"ObjectID",
"(",
"obj_id",
".",
"binary",
"(",
")",
")",
"(",
"ray",
".",
"worker",
".",
"global_worker",
".",
"raylet_client",
".",
"fetch_or_reconstruct",
"(",
"[",
"obj_id",
"]",
",",
"True",
")",
")",
"self",
".",
"_fetching",
".",
"append",
"(",
"(",
"worker",
",",
"obj_id",
")",
")",
"remaining",
"=",
"[",
"]",
"num_yielded",
"=",
"0",
"for",
"worker",
",",
"obj_id",
"in",
"self",
".",
"_fetching",
":",
"plasma_id",
"=",
"ray",
".",
"pyarrow",
".",
"plasma",
".",
"ObjectID",
"(",
"obj_id",
".",
"binary",
"(",
")",
")",
"if",
"(",
"num_yielded",
"<",
"max_yield",
"and",
"ray",
".",
"worker",
".",
"global_worker",
".",
"plasma_client",
".",
"contains",
"(",
"plasma_id",
")",
")",
":",
"yield",
"(",
"worker",
",",
"obj_id",
")",
"num_yielded",
"+=",
"1",
"else",
":",
"remaining",
".",
"append",
"(",
"(",
"worker",
",",
"obj_id",
")",
")",
"self",
".",
"_fetching",
"=",
"remaining"
] | Similar to completed but only returns once the object is local.
Assumes obj_id only is one id. | [
"Similar",
"to",
"completed",
"but",
"only",
"returns",
"once",
"the",
"object",
"is",
"local",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/actors.py#L37-L59 |
24,617 | ray-project/ray | python/ray/rllib/utils/actors.py | TaskPool.reset_evaluators | def reset_evaluators(self, evaluators):
"""Notify that some evaluators may be removed."""
for obj_id, ev in self._tasks.copy().items():
if ev not in evaluators:
del self._tasks[obj_id]
del self._objects[obj_id]
ok = []
for ev, obj_id in self._fetching:
if ev in evaluators:
ok.append((ev, obj_id))
self._fetching = ok | python | def reset_evaluators(self, evaluators):
"""Notify that some evaluators may be removed."""
for obj_id, ev in self._tasks.copy().items():
if ev not in evaluators:
del self._tasks[obj_id]
del self._objects[obj_id]
ok = []
for ev, obj_id in self._fetching:
if ev in evaluators:
ok.append((ev, obj_id))
self._fetching = ok | [
"def",
"reset_evaluators",
"(",
"self",
",",
"evaluators",
")",
":",
"for",
"obj_id",
",",
"ev",
"in",
"self",
".",
"_tasks",
".",
"copy",
"(",
")",
".",
"items",
"(",
")",
":",
"if",
"ev",
"not",
"in",
"evaluators",
":",
"del",
"self",
".",
"_tasks",
"[",
"obj_id",
"]",
"del",
"self",
".",
"_objects",
"[",
"obj_id",
"]",
"ok",
"=",
"[",
"]",
"for",
"ev",
",",
"obj_id",
"in",
"self",
".",
"_fetching",
":",
"if",
"ev",
"in",
"evaluators",
":",
"ok",
".",
"append",
"(",
"(",
"ev",
",",
"obj_id",
")",
")",
"self",
".",
"_fetching",
"=",
"ok"
] | Notify that some evaluators may be removed. | [
"Notify",
"that",
"some",
"evaluators",
"may",
"be",
"removed",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/actors.py#L61-L71 |
24,618 | ray-project/ray | python/ray/rllib/optimizers/aso_aggregator.py | AggregationWorkerBase.iter_train_batches | def iter_train_batches(self, max_yield=999):
"""Iterate over train batches.
Arguments:
max_yield (int): Max number of batches to iterate over in this
cycle. Setting this avoids iter_train_batches returning too
much data at once.
"""
for ev, sample_batch in self._augment_with_replay(
self.sample_tasks.completed_prefetch(
blocking_wait=True, max_yield=max_yield)):
sample_batch.decompress_if_needed()
self.batch_buffer.append(sample_batch)
if sum(b.count
for b in self.batch_buffer) >= self.train_batch_size:
train_batch = self.batch_buffer[0].concat_samples(
self.batch_buffer)
yield train_batch
self.batch_buffer = []
# If the batch was replayed, skip the update below.
if ev is None:
continue
# Put in replay buffer if enabled
if self.replay_buffer_num_slots > 0:
if len(self.replay_batches) < self.replay_buffer_num_slots:
self.replay_batches.append(sample_batch)
else:
self.replay_batches[self.replay_index] = sample_batch
self.replay_index += 1
self.replay_index %= self.replay_buffer_num_slots
ev.set_weights.remote(self.broadcasted_weights)
self.num_weight_syncs += 1
self.num_sent_since_broadcast += 1
# Kick off another sample request
self.sample_tasks.add(ev, ev.sample.remote()) | python | def iter_train_batches(self, max_yield=999):
"""Iterate over train batches.
Arguments:
max_yield (int): Max number of batches to iterate over in this
cycle. Setting this avoids iter_train_batches returning too
much data at once.
"""
for ev, sample_batch in self._augment_with_replay(
self.sample_tasks.completed_prefetch(
blocking_wait=True, max_yield=max_yield)):
sample_batch.decompress_if_needed()
self.batch_buffer.append(sample_batch)
if sum(b.count
for b in self.batch_buffer) >= self.train_batch_size:
train_batch = self.batch_buffer[0].concat_samples(
self.batch_buffer)
yield train_batch
self.batch_buffer = []
# If the batch was replayed, skip the update below.
if ev is None:
continue
# Put in replay buffer if enabled
if self.replay_buffer_num_slots > 0:
if len(self.replay_batches) < self.replay_buffer_num_slots:
self.replay_batches.append(sample_batch)
else:
self.replay_batches[self.replay_index] = sample_batch
self.replay_index += 1
self.replay_index %= self.replay_buffer_num_slots
ev.set_weights.remote(self.broadcasted_weights)
self.num_weight_syncs += 1
self.num_sent_since_broadcast += 1
# Kick off another sample request
self.sample_tasks.add(ev, ev.sample.remote()) | [
"def",
"iter_train_batches",
"(",
"self",
",",
"max_yield",
"=",
"999",
")",
":",
"for",
"ev",
",",
"sample_batch",
"in",
"self",
".",
"_augment_with_replay",
"(",
"self",
".",
"sample_tasks",
".",
"completed_prefetch",
"(",
"blocking_wait",
"=",
"True",
",",
"max_yield",
"=",
"max_yield",
")",
")",
":",
"sample_batch",
".",
"decompress_if_needed",
"(",
")",
"self",
".",
"batch_buffer",
".",
"append",
"(",
"sample_batch",
")",
"if",
"sum",
"(",
"b",
".",
"count",
"for",
"b",
"in",
"self",
".",
"batch_buffer",
")",
">=",
"self",
".",
"train_batch_size",
":",
"train_batch",
"=",
"self",
".",
"batch_buffer",
"[",
"0",
"]",
".",
"concat_samples",
"(",
"self",
".",
"batch_buffer",
")",
"yield",
"train_batch",
"self",
".",
"batch_buffer",
"=",
"[",
"]",
"# If the batch was replayed, skip the update below.",
"if",
"ev",
"is",
"None",
":",
"continue",
"# Put in replay buffer if enabled",
"if",
"self",
".",
"replay_buffer_num_slots",
">",
"0",
":",
"if",
"len",
"(",
"self",
".",
"replay_batches",
")",
"<",
"self",
".",
"replay_buffer_num_slots",
":",
"self",
".",
"replay_batches",
".",
"append",
"(",
"sample_batch",
")",
"else",
":",
"self",
".",
"replay_batches",
"[",
"self",
".",
"replay_index",
"]",
"=",
"sample_batch",
"self",
".",
"replay_index",
"+=",
"1",
"self",
".",
"replay_index",
"%=",
"self",
".",
"replay_buffer_num_slots",
"ev",
".",
"set_weights",
".",
"remote",
"(",
"self",
".",
"broadcasted_weights",
")",
"self",
".",
"num_weight_syncs",
"+=",
"1",
"self",
".",
"num_sent_since_broadcast",
"+=",
"1",
"# Kick off another sample request",
"self",
".",
"sample_tasks",
".",
"add",
"(",
"ev",
",",
"ev",
".",
"sample",
".",
"remote",
"(",
")",
")"
] | Iterate over train batches.
Arguments:
max_yield (int): Max number of batches to iterate over in this
cycle. Setting this avoids iter_train_batches returning too
much data at once. | [
"Iterate",
"over",
"train",
"batches",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/optimizers/aso_aggregator.py#L92-L131 |
24,619 | ray-project/ray | python/ray/autoscaler/commands.py | create_or_update_cluster | def create_or_update_cluster(config_file, override_min_workers,
override_max_workers, no_restart, restart_only,
yes, override_cluster_name):
"""Create or updates an autoscaling Ray cluster from a config json."""
config = yaml.load(open(config_file).read())
if override_min_workers is not None:
config["min_workers"] = override_min_workers
if override_max_workers is not None:
config["max_workers"] = override_max_workers
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
config = _bootstrap_config(config)
get_or_create_head_node(config, config_file, no_restart, restart_only, yes,
override_cluster_name) | python | def create_or_update_cluster(config_file, override_min_workers,
override_max_workers, no_restart, restart_only,
yes, override_cluster_name):
"""Create or updates an autoscaling Ray cluster from a config json."""
config = yaml.load(open(config_file).read())
if override_min_workers is not None:
config["min_workers"] = override_min_workers
if override_max_workers is not None:
config["max_workers"] = override_max_workers
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
config = _bootstrap_config(config)
get_or_create_head_node(config, config_file, no_restart, restart_only, yes,
override_cluster_name) | [
"def",
"create_or_update_cluster",
"(",
"config_file",
",",
"override_min_workers",
",",
"override_max_workers",
",",
"no_restart",
",",
"restart_only",
",",
"yes",
",",
"override_cluster_name",
")",
":",
"config",
"=",
"yaml",
".",
"load",
"(",
"open",
"(",
"config_file",
")",
".",
"read",
"(",
")",
")",
"if",
"override_min_workers",
"is",
"not",
"None",
":",
"config",
"[",
"\"min_workers\"",
"]",
"=",
"override_min_workers",
"if",
"override_max_workers",
"is",
"not",
"None",
":",
"config",
"[",
"\"max_workers\"",
"]",
"=",
"override_max_workers",
"if",
"override_cluster_name",
"is",
"not",
"None",
":",
"config",
"[",
"\"cluster_name\"",
"]",
"=",
"override_cluster_name",
"config",
"=",
"_bootstrap_config",
"(",
"config",
")",
"get_or_create_head_node",
"(",
"config",
",",
"config_file",
",",
"no_restart",
",",
"restart_only",
",",
"yes",
",",
"override_cluster_name",
")"
] | Create or updates an autoscaling Ray cluster from a config json. | [
"Create",
"or",
"updates",
"an",
"autoscaling",
"Ray",
"cluster",
"from",
"a",
"config",
"json",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L34-L47 |
24,620 | ray-project/ray | python/ray/autoscaler/commands.py | teardown_cluster | def teardown_cluster(config_file, yes, workers_only, override_cluster_name):
"""Destroys all nodes of a Ray cluster described by a config json."""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
validate_config(config)
config = fillout_defaults(config)
confirm("This will destroy your cluster", yes)
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
def remaining_nodes():
if workers_only:
A = []
else:
A = [
node_id for node_id in provider.non_terminated_nodes({
TAG_RAY_NODE_TYPE: "head"
})
]
A += [
node_id for node_id in provider.non_terminated_nodes({
TAG_RAY_NODE_TYPE: "worker"
})
]
return A
# Loop here to check that both the head and worker nodes are actually
# really gone
A = remaining_nodes()
with LogTimer("teardown_cluster: Termination done."):
while A:
logger.info("teardown_cluster: "
"Terminating {} nodes...".format(len(A)))
provider.terminate_nodes(A)
time.sleep(1)
A = remaining_nodes()
finally:
provider.cleanup() | python | def teardown_cluster(config_file, yes, workers_only, override_cluster_name):
"""Destroys all nodes of a Ray cluster described by a config json."""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
validate_config(config)
config = fillout_defaults(config)
confirm("This will destroy your cluster", yes)
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
def remaining_nodes():
if workers_only:
A = []
else:
A = [
node_id for node_id in provider.non_terminated_nodes({
TAG_RAY_NODE_TYPE: "head"
})
]
A += [
node_id for node_id in provider.non_terminated_nodes({
TAG_RAY_NODE_TYPE: "worker"
})
]
return A
# Loop here to check that both the head and worker nodes are actually
# really gone
A = remaining_nodes()
with LogTimer("teardown_cluster: Termination done."):
while A:
logger.info("teardown_cluster: "
"Terminating {} nodes...".format(len(A)))
provider.terminate_nodes(A)
time.sleep(1)
A = remaining_nodes()
finally:
provider.cleanup() | [
"def",
"teardown_cluster",
"(",
"config_file",
",",
"yes",
",",
"workers_only",
",",
"override_cluster_name",
")",
":",
"config",
"=",
"yaml",
".",
"load",
"(",
"open",
"(",
"config_file",
")",
".",
"read",
"(",
")",
")",
"if",
"override_cluster_name",
"is",
"not",
"None",
":",
"config",
"[",
"\"cluster_name\"",
"]",
"=",
"override_cluster_name",
"validate_config",
"(",
"config",
")",
"config",
"=",
"fillout_defaults",
"(",
"config",
")",
"confirm",
"(",
"\"This will destroy your cluster\"",
",",
"yes",
")",
"provider",
"=",
"get_node_provider",
"(",
"config",
"[",
"\"provider\"",
"]",
",",
"config",
"[",
"\"cluster_name\"",
"]",
")",
"try",
":",
"def",
"remaining_nodes",
"(",
")",
":",
"if",
"workers_only",
":",
"A",
"=",
"[",
"]",
"else",
":",
"A",
"=",
"[",
"node_id",
"for",
"node_id",
"in",
"provider",
".",
"non_terminated_nodes",
"(",
"{",
"TAG_RAY_NODE_TYPE",
":",
"\"head\"",
"}",
")",
"]",
"A",
"+=",
"[",
"node_id",
"for",
"node_id",
"in",
"provider",
".",
"non_terminated_nodes",
"(",
"{",
"TAG_RAY_NODE_TYPE",
":",
"\"worker\"",
"}",
")",
"]",
"return",
"A",
"# Loop here to check that both the head and worker nodes are actually",
"# really gone",
"A",
"=",
"remaining_nodes",
"(",
")",
"with",
"LogTimer",
"(",
"\"teardown_cluster: Termination done.\"",
")",
":",
"while",
"A",
":",
"logger",
".",
"info",
"(",
"\"teardown_cluster: \"",
"\"Terminating {} nodes...\"",
".",
"format",
"(",
"len",
"(",
"A",
")",
")",
")",
"provider",
".",
"terminate_nodes",
"(",
"A",
")",
"time",
".",
"sleep",
"(",
"1",
")",
"A",
"=",
"remaining_nodes",
"(",
")",
"finally",
":",
"provider",
".",
"cleanup",
"(",
")"
] | Destroys all nodes of a Ray cluster described by a config json. | [
"Destroys",
"all",
"nodes",
"of",
"a",
"Ray",
"cluster",
"described",
"by",
"a",
"config",
"json",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L73-L116 |
24,621 | ray-project/ray | python/ray/autoscaler/commands.py | kill_node | def kill_node(config_file, yes, override_cluster_name):
"""Kills a random Raylet worker."""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
config = _bootstrap_config(config)
confirm("This will kill a node in your cluster", yes)
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
nodes = provider.non_terminated_nodes({TAG_RAY_NODE_TYPE: "worker"})
node = random.choice(nodes)
logger.info("kill_node: Terminating worker {}".format(node))
updater = NodeUpdaterThread(
node_id=node,
provider_config=config["provider"],
provider=provider,
auth_config=config["auth"],
cluster_name=config["cluster_name"],
file_mounts=config["file_mounts"],
initialization_commands=[],
setup_commands=[],
runtime_hash="")
_exec(updater, "ray stop", False, False)
time.sleep(5)
if config.get("provider", {}).get("use_internal_ips", False) is True:
node_ip = provider.internal_ip(node)
else:
node_ip = provider.external_ip(node)
finally:
provider.cleanup()
return node_ip | python | def kill_node(config_file, yes, override_cluster_name):
"""Kills a random Raylet worker."""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
config = _bootstrap_config(config)
confirm("This will kill a node in your cluster", yes)
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
nodes = provider.non_terminated_nodes({TAG_RAY_NODE_TYPE: "worker"})
node = random.choice(nodes)
logger.info("kill_node: Terminating worker {}".format(node))
updater = NodeUpdaterThread(
node_id=node,
provider_config=config["provider"],
provider=provider,
auth_config=config["auth"],
cluster_name=config["cluster_name"],
file_mounts=config["file_mounts"],
initialization_commands=[],
setup_commands=[],
runtime_hash="")
_exec(updater, "ray stop", False, False)
time.sleep(5)
if config.get("provider", {}).get("use_internal_ips", False) is True:
node_ip = provider.internal_ip(node)
else:
node_ip = provider.external_ip(node)
finally:
provider.cleanup()
return node_ip | [
"def",
"kill_node",
"(",
"config_file",
",",
"yes",
",",
"override_cluster_name",
")",
":",
"config",
"=",
"yaml",
".",
"load",
"(",
"open",
"(",
"config_file",
")",
".",
"read",
"(",
")",
")",
"if",
"override_cluster_name",
"is",
"not",
"None",
":",
"config",
"[",
"\"cluster_name\"",
"]",
"=",
"override_cluster_name",
"config",
"=",
"_bootstrap_config",
"(",
"config",
")",
"confirm",
"(",
"\"This will kill a node in your cluster\"",
",",
"yes",
")",
"provider",
"=",
"get_node_provider",
"(",
"config",
"[",
"\"provider\"",
"]",
",",
"config",
"[",
"\"cluster_name\"",
"]",
")",
"try",
":",
"nodes",
"=",
"provider",
".",
"non_terminated_nodes",
"(",
"{",
"TAG_RAY_NODE_TYPE",
":",
"\"worker\"",
"}",
")",
"node",
"=",
"random",
".",
"choice",
"(",
"nodes",
")",
"logger",
".",
"info",
"(",
"\"kill_node: Terminating worker {}\"",
".",
"format",
"(",
"node",
")",
")",
"updater",
"=",
"NodeUpdaterThread",
"(",
"node_id",
"=",
"node",
",",
"provider_config",
"=",
"config",
"[",
"\"provider\"",
"]",
",",
"provider",
"=",
"provider",
",",
"auth_config",
"=",
"config",
"[",
"\"auth\"",
"]",
",",
"cluster_name",
"=",
"config",
"[",
"\"cluster_name\"",
"]",
",",
"file_mounts",
"=",
"config",
"[",
"\"file_mounts\"",
"]",
",",
"initialization_commands",
"=",
"[",
"]",
",",
"setup_commands",
"=",
"[",
"]",
",",
"runtime_hash",
"=",
"\"\"",
")",
"_exec",
"(",
"updater",
",",
"\"ray stop\"",
",",
"False",
",",
"False",
")",
"time",
".",
"sleep",
"(",
"5",
")",
"if",
"config",
".",
"get",
"(",
"\"provider\"",
",",
"{",
"}",
")",
".",
"get",
"(",
"\"use_internal_ips\"",
",",
"False",
")",
"is",
"True",
":",
"node_ip",
"=",
"provider",
".",
"internal_ip",
"(",
"node",
")",
"else",
":",
"node_ip",
"=",
"provider",
".",
"external_ip",
"(",
"node",
")",
"finally",
":",
"provider",
".",
"cleanup",
"(",
")",
"return",
"node_ip"
] | Kills a random Raylet worker. | [
"Kills",
"a",
"random",
"Raylet",
"worker",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L119-L157 |
24,622 | ray-project/ray | python/ray/autoscaler/commands.py | attach_cluster | def attach_cluster(config_file, start, use_tmux, override_cluster_name, new):
"""Attaches to a screen for the specified cluster.
Arguments:
config_file: path to the cluster yaml
start: whether to start the cluster if it isn't up
use_tmux: whether to use tmux as multiplexer
override_cluster_name: set the name of the cluster
new: whether to force a new screen
"""
if use_tmux:
if new:
cmd = "tmux new"
else:
cmd = "tmux attach || tmux new"
else:
if new:
cmd = "screen -L"
else:
cmd = "screen -L -xRR"
exec_cluster(config_file, cmd, False, False, False, False, start,
override_cluster_name, None) | python | def attach_cluster(config_file, start, use_tmux, override_cluster_name, new):
"""Attaches to a screen for the specified cluster.
Arguments:
config_file: path to the cluster yaml
start: whether to start the cluster if it isn't up
use_tmux: whether to use tmux as multiplexer
override_cluster_name: set the name of the cluster
new: whether to force a new screen
"""
if use_tmux:
if new:
cmd = "tmux new"
else:
cmd = "tmux attach || tmux new"
else:
if new:
cmd = "screen -L"
else:
cmd = "screen -L -xRR"
exec_cluster(config_file, cmd, False, False, False, False, start,
override_cluster_name, None) | [
"def",
"attach_cluster",
"(",
"config_file",
",",
"start",
",",
"use_tmux",
",",
"override_cluster_name",
",",
"new",
")",
":",
"if",
"use_tmux",
":",
"if",
"new",
":",
"cmd",
"=",
"\"tmux new\"",
"else",
":",
"cmd",
"=",
"\"tmux attach || tmux new\"",
"else",
":",
"if",
"new",
":",
"cmd",
"=",
"\"screen -L\"",
"else",
":",
"cmd",
"=",
"\"screen -L -xRR\"",
"exec_cluster",
"(",
"config_file",
",",
"cmd",
",",
"False",
",",
"False",
",",
"False",
",",
"False",
",",
"start",
",",
"override_cluster_name",
",",
"None",
")"
] | Attaches to a screen for the specified cluster.
Arguments:
config_file: path to the cluster yaml
start: whether to start the cluster if it isn't up
use_tmux: whether to use tmux as multiplexer
override_cluster_name: set the name of the cluster
new: whether to force a new screen | [
"Attaches",
"to",
"a",
"screen",
"for",
"the",
"specified",
"cluster",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L286-L309 |
24,623 | ray-project/ray | python/ray/autoscaler/commands.py | exec_cluster | def exec_cluster(config_file, cmd, docker, screen, tmux, stop, start,
override_cluster_name, port_forward):
"""Runs a command on the specified cluster.
Arguments:
config_file: path to the cluster yaml
cmd: command to run
docker: whether to run command in docker container of config
screen: whether to run in a screen
tmux: whether to run in a tmux session
stop: whether to stop the cluster after command run
start: whether to start the cluster if it isn't up
override_cluster_name: set the name of the cluster
port_forward: port to forward
"""
assert not (screen and tmux), "Can specify only one of `screen` or `tmux`."
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
config = _bootstrap_config(config)
head_node = _get_head_node(
config, config_file, override_cluster_name, create_if_needed=start)
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
updater = NodeUpdaterThread(
node_id=head_node,
provider_config=config["provider"],
provider=provider,
auth_config=config["auth"],
cluster_name=config["cluster_name"],
file_mounts=config["file_mounts"],
initialization_commands=[],
setup_commands=[],
runtime_hash="",
)
def wrap_docker(command):
container_name = config["docker"]["container_name"]
if not container_name:
raise ValueError("Docker container not specified in config.")
return with_docker_exec(
[command], container_name=container_name)[0]
cmd = wrap_docker(cmd) if docker else cmd
if stop:
shutdown_cmd = (
"ray stop; ray teardown ~/ray_bootstrap_config.yaml "
"--yes --workers-only")
if docker:
shutdown_cmd = wrap_docker(shutdown_cmd)
cmd += ("; {}; sudo shutdown -h now".format(shutdown_cmd))
_exec(
updater,
cmd,
screen,
tmux,
expect_error=stop,
port_forward=port_forward)
if tmux or screen:
attach_command_parts = ["ray attach", config_file]
if override_cluster_name is not None:
attach_command_parts.append(
"--cluster-name={}".format(override_cluster_name))
if tmux:
attach_command_parts.append("--tmux")
elif screen:
attach_command_parts.append("--screen")
attach_command = " ".join(attach_command_parts)
attach_info = "Use `{}` to check on command status.".format(
attach_command)
logger.info(attach_info)
finally:
provider.cleanup() | python | def exec_cluster(config_file, cmd, docker, screen, tmux, stop, start,
override_cluster_name, port_forward):
"""Runs a command on the specified cluster.
Arguments:
config_file: path to the cluster yaml
cmd: command to run
docker: whether to run command in docker container of config
screen: whether to run in a screen
tmux: whether to run in a tmux session
stop: whether to stop the cluster after command run
start: whether to start the cluster if it isn't up
override_cluster_name: set the name of the cluster
port_forward: port to forward
"""
assert not (screen and tmux), "Can specify only one of `screen` or `tmux`."
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
config = _bootstrap_config(config)
head_node = _get_head_node(
config, config_file, override_cluster_name, create_if_needed=start)
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
updater = NodeUpdaterThread(
node_id=head_node,
provider_config=config["provider"],
provider=provider,
auth_config=config["auth"],
cluster_name=config["cluster_name"],
file_mounts=config["file_mounts"],
initialization_commands=[],
setup_commands=[],
runtime_hash="",
)
def wrap_docker(command):
container_name = config["docker"]["container_name"]
if not container_name:
raise ValueError("Docker container not specified in config.")
return with_docker_exec(
[command], container_name=container_name)[0]
cmd = wrap_docker(cmd) if docker else cmd
if stop:
shutdown_cmd = (
"ray stop; ray teardown ~/ray_bootstrap_config.yaml "
"--yes --workers-only")
if docker:
shutdown_cmd = wrap_docker(shutdown_cmd)
cmd += ("; {}; sudo shutdown -h now".format(shutdown_cmd))
_exec(
updater,
cmd,
screen,
tmux,
expect_error=stop,
port_forward=port_forward)
if tmux or screen:
attach_command_parts = ["ray attach", config_file]
if override_cluster_name is not None:
attach_command_parts.append(
"--cluster-name={}".format(override_cluster_name))
if tmux:
attach_command_parts.append("--tmux")
elif screen:
attach_command_parts.append("--screen")
attach_command = " ".join(attach_command_parts)
attach_info = "Use `{}` to check on command status.".format(
attach_command)
logger.info(attach_info)
finally:
provider.cleanup() | [
"def",
"exec_cluster",
"(",
"config_file",
",",
"cmd",
",",
"docker",
",",
"screen",
",",
"tmux",
",",
"stop",
",",
"start",
",",
"override_cluster_name",
",",
"port_forward",
")",
":",
"assert",
"not",
"(",
"screen",
"and",
"tmux",
")",
",",
"\"Can specify only one of `screen` or `tmux`.\"",
"config",
"=",
"yaml",
".",
"load",
"(",
"open",
"(",
"config_file",
")",
".",
"read",
"(",
")",
")",
"if",
"override_cluster_name",
"is",
"not",
"None",
":",
"config",
"[",
"\"cluster_name\"",
"]",
"=",
"override_cluster_name",
"config",
"=",
"_bootstrap_config",
"(",
"config",
")",
"head_node",
"=",
"_get_head_node",
"(",
"config",
",",
"config_file",
",",
"override_cluster_name",
",",
"create_if_needed",
"=",
"start",
")",
"provider",
"=",
"get_node_provider",
"(",
"config",
"[",
"\"provider\"",
"]",
",",
"config",
"[",
"\"cluster_name\"",
"]",
")",
"try",
":",
"updater",
"=",
"NodeUpdaterThread",
"(",
"node_id",
"=",
"head_node",
",",
"provider_config",
"=",
"config",
"[",
"\"provider\"",
"]",
",",
"provider",
"=",
"provider",
",",
"auth_config",
"=",
"config",
"[",
"\"auth\"",
"]",
",",
"cluster_name",
"=",
"config",
"[",
"\"cluster_name\"",
"]",
",",
"file_mounts",
"=",
"config",
"[",
"\"file_mounts\"",
"]",
",",
"initialization_commands",
"=",
"[",
"]",
",",
"setup_commands",
"=",
"[",
"]",
",",
"runtime_hash",
"=",
"\"\"",
",",
")",
"def",
"wrap_docker",
"(",
"command",
")",
":",
"container_name",
"=",
"config",
"[",
"\"docker\"",
"]",
"[",
"\"container_name\"",
"]",
"if",
"not",
"container_name",
":",
"raise",
"ValueError",
"(",
"\"Docker container not specified in config.\"",
")",
"return",
"with_docker_exec",
"(",
"[",
"command",
"]",
",",
"container_name",
"=",
"container_name",
")",
"[",
"0",
"]",
"cmd",
"=",
"wrap_docker",
"(",
"cmd",
")",
"if",
"docker",
"else",
"cmd",
"if",
"stop",
":",
"shutdown_cmd",
"=",
"(",
"\"ray stop; ray teardown ~/ray_bootstrap_config.yaml \"",
"\"--yes --workers-only\"",
")",
"if",
"docker",
":",
"shutdown_cmd",
"=",
"wrap_docker",
"(",
"shutdown_cmd",
")",
"cmd",
"+=",
"(",
"\"; {}; sudo shutdown -h now\"",
".",
"format",
"(",
"shutdown_cmd",
")",
")",
"_exec",
"(",
"updater",
",",
"cmd",
",",
"screen",
",",
"tmux",
",",
"expect_error",
"=",
"stop",
",",
"port_forward",
"=",
"port_forward",
")",
"if",
"tmux",
"or",
"screen",
":",
"attach_command_parts",
"=",
"[",
"\"ray attach\"",
",",
"config_file",
"]",
"if",
"override_cluster_name",
"is",
"not",
"None",
":",
"attach_command_parts",
".",
"append",
"(",
"\"--cluster-name={}\"",
".",
"format",
"(",
"override_cluster_name",
")",
")",
"if",
"tmux",
":",
"attach_command_parts",
".",
"append",
"(",
"\"--tmux\"",
")",
"elif",
"screen",
":",
"attach_command_parts",
".",
"append",
"(",
"\"--screen\"",
")",
"attach_command",
"=",
"\" \"",
".",
"join",
"(",
"attach_command_parts",
")",
"attach_info",
"=",
"\"Use `{}` to check on command status.\"",
".",
"format",
"(",
"attach_command",
")",
"logger",
".",
"info",
"(",
"attach_info",
")",
"finally",
":",
"provider",
".",
"cleanup",
"(",
")"
] | Runs a command on the specified cluster.
Arguments:
config_file: path to the cluster yaml
cmd: command to run
docker: whether to run command in docker container of config
screen: whether to run in a screen
tmux: whether to run in a tmux session
stop: whether to stop the cluster after command run
start: whether to start the cluster if it isn't up
override_cluster_name: set the name of the cluster
port_forward: port to forward | [
"Runs",
"a",
"command",
"on",
"the",
"specified",
"cluster",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L312-L391 |
24,624 | ray-project/ray | python/ray/autoscaler/commands.py | rsync | def rsync(config_file, source, target, override_cluster_name, down):
"""Rsyncs files.
Arguments:
config_file: path to the cluster yaml
source: source dir
target: target dir
override_cluster_name: set the name of the cluster
down: whether we're syncing remote -> local
"""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
config = _bootstrap_config(config)
head_node = _get_head_node(
config, config_file, override_cluster_name, create_if_needed=False)
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
updater = NodeUpdaterThread(
node_id=head_node,
provider_config=config["provider"],
provider=provider,
auth_config=config["auth"],
cluster_name=config["cluster_name"],
file_mounts=config["file_mounts"],
initialization_commands=[],
setup_commands=[],
runtime_hash="",
)
if down:
rsync = updater.rsync_down
else:
rsync = updater.rsync_up
rsync(source, target, check_error=False)
finally:
provider.cleanup() | python | def rsync(config_file, source, target, override_cluster_name, down):
"""Rsyncs files.
Arguments:
config_file: path to the cluster yaml
source: source dir
target: target dir
override_cluster_name: set the name of the cluster
down: whether we're syncing remote -> local
"""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
config = _bootstrap_config(config)
head_node = _get_head_node(
config, config_file, override_cluster_name, create_if_needed=False)
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
updater = NodeUpdaterThread(
node_id=head_node,
provider_config=config["provider"],
provider=provider,
auth_config=config["auth"],
cluster_name=config["cluster_name"],
file_mounts=config["file_mounts"],
initialization_commands=[],
setup_commands=[],
runtime_hash="",
)
if down:
rsync = updater.rsync_down
else:
rsync = updater.rsync_up
rsync(source, target, check_error=False)
finally:
provider.cleanup() | [
"def",
"rsync",
"(",
"config_file",
",",
"source",
",",
"target",
",",
"override_cluster_name",
",",
"down",
")",
":",
"config",
"=",
"yaml",
".",
"load",
"(",
"open",
"(",
"config_file",
")",
".",
"read",
"(",
")",
")",
"if",
"override_cluster_name",
"is",
"not",
"None",
":",
"config",
"[",
"\"cluster_name\"",
"]",
"=",
"override_cluster_name",
"config",
"=",
"_bootstrap_config",
"(",
"config",
")",
"head_node",
"=",
"_get_head_node",
"(",
"config",
",",
"config_file",
",",
"override_cluster_name",
",",
"create_if_needed",
"=",
"False",
")",
"provider",
"=",
"get_node_provider",
"(",
"config",
"[",
"\"provider\"",
"]",
",",
"config",
"[",
"\"cluster_name\"",
"]",
")",
"try",
":",
"updater",
"=",
"NodeUpdaterThread",
"(",
"node_id",
"=",
"head_node",
",",
"provider_config",
"=",
"config",
"[",
"\"provider\"",
"]",
",",
"provider",
"=",
"provider",
",",
"auth_config",
"=",
"config",
"[",
"\"auth\"",
"]",
",",
"cluster_name",
"=",
"config",
"[",
"\"cluster_name\"",
"]",
",",
"file_mounts",
"=",
"config",
"[",
"\"file_mounts\"",
"]",
",",
"initialization_commands",
"=",
"[",
"]",
",",
"setup_commands",
"=",
"[",
"]",
",",
"runtime_hash",
"=",
"\"\"",
",",
")",
"if",
"down",
":",
"rsync",
"=",
"updater",
".",
"rsync_down",
"else",
":",
"rsync",
"=",
"updater",
".",
"rsync_up",
"rsync",
"(",
"source",
",",
"target",
",",
"check_error",
"=",
"False",
")",
"finally",
":",
"provider",
".",
"cleanup",
"(",
")"
] | Rsyncs files.
Arguments:
config_file: path to the cluster yaml
source: source dir
target: target dir
override_cluster_name: set the name of the cluster
down: whether we're syncing remote -> local | [
"Rsyncs",
"files",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L416-L453 |
24,625 | ray-project/ray | python/ray/autoscaler/commands.py | get_head_node_ip | def get_head_node_ip(config_file, override_cluster_name):
"""Returns head node IP for given configuration file if exists."""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
head_node = _get_head_node(config, config_file, override_cluster_name)
if config.get("provider", {}).get("use_internal_ips", False) is True:
head_node_ip = provider.internal_ip(head_node)
else:
head_node_ip = provider.external_ip(head_node)
finally:
provider.cleanup()
return head_node_ip | python | def get_head_node_ip(config_file, override_cluster_name):
"""Returns head node IP for given configuration file if exists."""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
head_node = _get_head_node(config, config_file, override_cluster_name)
if config.get("provider", {}).get("use_internal_ips", False) is True:
head_node_ip = provider.internal_ip(head_node)
else:
head_node_ip = provider.external_ip(head_node)
finally:
provider.cleanup()
return head_node_ip | [
"def",
"get_head_node_ip",
"(",
"config_file",
",",
"override_cluster_name",
")",
":",
"config",
"=",
"yaml",
".",
"load",
"(",
"open",
"(",
"config_file",
")",
".",
"read",
"(",
")",
")",
"if",
"override_cluster_name",
"is",
"not",
"None",
":",
"config",
"[",
"\"cluster_name\"",
"]",
"=",
"override_cluster_name",
"provider",
"=",
"get_node_provider",
"(",
"config",
"[",
"\"provider\"",
"]",
",",
"config",
"[",
"\"cluster_name\"",
"]",
")",
"try",
":",
"head_node",
"=",
"_get_head_node",
"(",
"config",
",",
"config_file",
",",
"override_cluster_name",
")",
"if",
"config",
".",
"get",
"(",
"\"provider\"",
",",
"{",
"}",
")",
".",
"get",
"(",
"\"use_internal_ips\"",
",",
"False",
")",
"is",
"True",
":",
"head_node_ip",
"=",
"provider",
".",
"internal_ip",
"(",
"head_node",
")",
"else",
":",
"head_node_ip",
"=",
"provider",
".",
"external_ip",
"(",
"head_node",
")",
"finally",
":",
"provider",
".",
"cleanup",
"(",
")",
"return",
"head_node_ip"
] | Returns head node IP for given configuration file if exists. | [
"Returns",
"head",
"node",
"IP",
"for",
"given",
"configuration",
"file",
"if",
"exists",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L456-L473 |
24,626 | ray-project/ray | python/ray/autoscaler/commands.py | get_worker_node_ips | def get_worker_node_ips(config_file, override_cluster_name):
"""Returns worker node IPs for given configuration file."""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
nodes = provider.non_terminated_nodes({TAG_RAY_NODE_TYPE: "worker"})
if config.get("provider", {}).get("use_internal_ips", False) is True:
return [provider.internal_ip(node) for node in nodes]
else:
return [provider.external_ip(node) for node in nodes]
finally:
provider.cleanup() | python | def get_worker_node_ips(config_file, override_cluster_name):
"""Returns worker node IPs for given configuration file."""
config = yaml.load(open(config_file).read())
if override_cluster_name is not None:
config["cluster_name"] = override_cluster_name
provider = get_node_provider(config["provider"], config["cluster_name"])
try:
nodes = provider.non_terminated_nodes({TAG_RAY_NODE_TYPE: "worker"})
if config.get("provider", {}).get("use_internal_ips", False) is True:
return [provider.internal_ip(node) for node in nodes]
else:
return [provider.external_ip(node) for node in nodes]
finally:
provider.cleanup() | [
"def",
"get_worker_node_ips",
"(",
"config_file",
",",
"override_cluster_name",
")",
":",
"config",
"=",
"yaml",
".",
"load",
"(",
"open",
"(",
"config_file",
")",
".",
"read",
"(",
")",
")",
"if",
"override_cluster_name",
"is",
"not",
"None",
":",
"config",
"[",
"\"cluster_name\"",
"]",
"=",
"override_cluster_name",
"provider",
"=",
"get_node_provider",
"(",
"config",
"[",
"\"provider\"",
"]",
",",
"config",
"[",
"\"cluster_name\"",
"]",
")",
"try",
":",
"nodes",
"=",
"provider",
".",
"non_terminated_nodes",
"(",
"{",
"TAG_RAY_NODE_TYPE",
":",
"\"worker\"",
"}",
")",
"if",
"config",
".",
"get",
"(",
"\"provider\"",
",",
"{",
"}",
")",
".",
"get",
"(",
"\"use_internal_ips\"",
",",
"False",
")",
"is",
"True",
":",
"return",
"[",
"provider",
".",
"internal_ip",
"(",
"node",
")",
"for",
"node",
"in",
"nodes",
"]",
"else",
":",
"return",
"[",
"provider",
".",
"external_ip",
"(",
"node",
")",
"for",
"node",
"in",
"nodes",
"]",
"finally",
":",
"provider",
".",
"cleanup",
"(",
")"
] | Returns worker node IPs for given configuration file. | [
"Returns",
"worker",
"node",
"IPs",
"for",
"given",
"configuration",
"file",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L476-L492 |
24,627 | ray-project/ray | python/ray/experimental/sgd/tfbench/model.py | Model.build_network | def build_network(self,
images,
phase_train=True,
nclass=1001,
image_depth=3,
data_type=tf.float32,
data_format="NCHW",
use_tf_layers=True,
fp16_vars=False):
"""Returns logits and aux_logits from images."""
if data_format == "NCHW":
images = tf.transpose(images, [0, 3, 1, 2])
var_type = tf.float32
if data_type == tf.float16 and fp16_vars:
var_type = tf.float16
network = convnet_builder.ConvNetBuilder(
images, image_depth, phase_train, use_tf_layers, data_format,
data_type, var_type)
with tf.variable_scope(
"cg", custom_getter=network.get_custom_getter()):
self.add_inference(network)
# Add the final fully-connected class layer
logits = (network.affine(nclass, activation="linear")
if not self.skip_final_affine_layer() else
network.top_layer)
aux_logits = None
if network.aux_top_layer is not None:
with network.switch_to_aux_top_layer():
aux_logits = network.affine(
nclass, activation="linear", stddev=0.001)
if data_type == tf.float16:
# TODO(reedwm): Determine if we should do this cast here.
logits = tf.cast(logits, tf.float32)
if aux_logits is not None:
aux_logits = tf.cast(aux_logits, tf.float32)
return logits, aux_logits | python | def build_network(self,
images,
phase_train=True,
nclass=1001,
image_depth=3,
data_type=tf.float32,
data_format="NCHW",
use_tf_layers=True,
fp16_vars=False):
"""Returns logits and aux_logits from images."""
if data_format == "NCHW":
images = tf.transpose(images, [0, 3, 1, 2])
var_type = tf.float32
if data_type == tf.float16 and fp16_vars:
var_type = tf.float16
network = convnet_builder.ConvNetBuilder(
images, image_depth, phase_train, use_tf_layers, data_format,
data_type, var_type)
with tf.variable_scope(
"cg", custom_getter=network.get_custom_getter()):
self.add_inference(network)
# Add the final fully-connected class layer
logits = (network.affine(nclass, activation="linear")
if not self.skip_final_affine_layer() else
network.top_layer)
aux_logits = None
if network.aux_top_layer is not None:
with network.switch_to_aux_top_layer():
aux_logits = network.affine(
nclass, activation="linear", stddev=0.001)
if data_type == tf.float16:
# TODO(reedwm): Determine if we should do this cast here.
logits = tf.cast(logits, tf.float32)
if aux_logits is not None:
aux_logits = tf.cast(aux_logits, tf.float32)
return logits, aux_logits | [
"def",
"build_network",
"(",
"self",
",",
"images",
",",
"phase_train",
"=",
"True",
",",
"nclass",
"=",
"1001",
",",
"image_depth",
"=",
"3",
",",
"data_type",
"=",
"tf",
".",
"float32",
",",
"data_format",
"=",
"\"NCHW\"",
",",
"use_tf_layers",
"=",
"True",
",",
"fp16_vars",
"=",
"False",
")",
":",
"if",
"data_format",
"==",
"\"NCHW\"",
":",
"images",
"=",
"tf",
".",
"transpose",
"(",
"images",
",",
"[",
"0",
",",
"3",
",",
"1",
",",
"2",
"]",
")",
"var_type",
"=",
"tf",
".",
"float32",
"if",
"data_type",
"==",
"tf",
".",
"float16",
"and",
"fp16_vars",
":",
"var_type",
"=",
"tf",
".",
"float16",
"network",
"=",
"convnet_builder",
".",
"ConvNetBuilder",
"(",
"images",
",",
"image_depth",
",",
"phase_train",
",",
"use_tf_layers",
",",
"data_format",
",",
"data_type",
",",
"var_type",
")",
"with",
"tf",
".",
"variable_scope",
"(",
"\"cg\"",
",",
"custom_getter",
"=",
"network",
".",
"get_custom_getter",
"(",
")",
")",
":",
"self",
".",
"add_inference",
"(",
"network",
")",
"# Add the final fully-connected class layer",
"logits",
"=",
"(",
"network",
".",
"affine",
"(",
"nclass",
",",
"activation",
"=",
"\"linear\"",
")",
"if",
"not",
"self",
".",
"skip_final_affine_layer",
"(",
")",
"else",
"network",
".",
"top_layer",
")",
"aux_logits",
"=",
"None",
"if",
"network",
".",
"aux_top_layer",
"is",
"not",
"None",
":",
"with",
"network",
".",
"switch_to_aux_top_layer",
"(",
")",
":",
"aux_logits",
"=",
"network",
".",
"affine",
"(",
"nclass",
",",
"activation",
"=",
"\"linear\"",
",",
"stddev",
"=",
"0.001",
")",
"if",
"data_type",
"==",
"tf",
".",
"float16",
":",
"# TODO(reedwm): Determine if we should do this cast here.",
"logits",
"=",
"tf",
".",
"cast",
"(",
"logits",
",",
"tf",
".",
"float32",
")",
"if",
"aux_logits",
"is",
"not",
"None",
":",
"aux_logits",
"=",
"tf",
".",
"cast",
"(",
"aux_logits",
",",
"tf",
".",
"float32",
")",
"return",
"logits",
",",
"aux_logits"
] | Returns logits and aux_logits from images. | [
"Returns",
"logits",
"and",
"aux_logits",
"from",
"images",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/tfbench/model.py#L79-L114 |
24,628 | ray-project/ray | python/ray/rllib/utils/__init__.py | renamed_class | def renamed_class(cls):
"""Helper class for renaming Agent => Trainer with a warning."""
class DeprecationWrapper(cls):
def __init__(self, config=None, env=None, logger_creator=None):
old_name = cls.__name__.replace("Trainer", "Agent")
new_name = cls.__name__
logger.warn("DeprecationWarning: {} has been renamed to {}. ".
format(old_name, new_name) +
"This will raise an error in the future.")
cls.__init__(self, config, env, logger_creator)
DeprecationWrapper.__name__ = cls.__name__
return DeprecationWrapper | python | def renamed_class(cls):
"""Helper class for renaming Agent => Trainer with a warning."""
class DeprecationWrapper(cls):
def __init__(self, config=None, env=None, logger_creator=None):
old_name = cls.__name__.replace("Trainer", "Agent")
new_name = cls.__name__
logger.warn("DeprecationWarning: {} has been renamed to {}. ".
format(old_name, new_name) +
"This will raise an error in the future.")
cls.__init__(self, config, env, logger_creator)
DeprecationWrapper.__name__ = cls.__name__
return DeprecationWrapper | [
"def",
"renamed_class",
"(",
"cls",
")",
":",
"class",
"DeprecationWrapper",
"(",
"cls",
")",
":",
"def",
"__init__",
"(",
"self",
",",
"config",
"=",
"None",
",",
"env",
"=",
"None",
",",
"logger_creator",
"=",
"None",
")",
":",
"old_name",
"=",
"cls",
".",
"__name__",
".",
"replace",
"(",
"\"Trainer\"",
",",
"\"Agent\"",
")",
"new_name",
"=",
"cls",
".",
"__name__",
"logger",
".",
"warn",
"(",
"\"DeprecationWarning: {} has been renamed to {}. \"",
".",
"format",
"(",
"old_name",
",",
"new_name",
")",
"+",
"\"This will raise an error in the future.\"",
")",
"cls",
".",
"__init__",
"(",
"self",
",",
"config",
",",
"env",
",",
"logger_creator",
")",
"DeprecationWrapper",
".",
"__name__",
"=",
"cls",
".",
"__name__",
"return",
"DeprecationWrapper"
] | Helper class for renaming Agent => Trainer with a warning. | [
"Helper",
"class",
"for",
"renaming",
"Agent",
"=",
">",
"Trainer",
"with",
"a",
"warning",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/__init__.py#L12-L26 |
24,629 | ray-project/ray | python/ray/profiling.py | profile | def profile(event_type, extra_data=None):
"""Profile a span of time so that it appears in the timeline visualization.
Note that this only works in the raylet code path.
This function can be used as follows (both on the driver or within a task).
.. code-block:: python
with ray.profile("custom event", extra_data={'key': 'value'}):
# Do some computation here.
Optionally, a dictionary can be passed as the "extra_data" argument, and
it can have keys "name" and "cname" if you want to override the default
timeline display text and box color. Other values will appear at the bottom
of the chrome tracing GUI when you click on the box corresponding to this
profile span.
Args:
event_type: A string describing the type of the event.
extra_data: This must be a dictionary mapping strings to strings. This
data will be added to the json objects that are used to populate
the timeline, so if you want to set a particular color, you can
simply set the "cname" attribute to an appropriate color.
Similarly, if you set the "name" attribute, then that will set the
text displayed on the box in the timeline.
Returns:
An object that can profile a span of time via a "with" statement.
"""
worker = ray.worker.global_worker
return RayLogSpanRaylet(worker.profiler, event_type, extra_data=extra_data) | python | def profile(event_type, extra_data=None):
"""Profile a span of time so that it appears in the timeline visualization.
Note that this only works in the raylet code path.
This function can be used as follows (both on the driver or within a task).
.. code-block:: python
with ray.profile("custom event", extra_data={'key': 'value'}):
# Do some computation here.
Optionally, a dictionary can be passed as the "extra_data" argument, and
it can have keys "name" and "cname" if you want to override the default
timeline display text and box color. Other values will appear at the bottom
of the chrome tracing GUI when you click on the box corresponding to this
profile span.
Args:
event_type: A string describing the type of the event.
extra_data: This must be a dictionary mapping strings to strings. This
data will be added to the json objects that are used to populate
the timeline, so if you want to set a particular color, you can
simply set the "cname" attribute to an appropriate color.
Similarly, if you set the "name" attribute, then that will set the
text displayed on the box in the timeline.
Returns:
An object that can profile a span of time via a "with" statement.
"""
worker = ray.worker.global_worker
return RayLogSpanRaylet(worker.profiler, event_type, extra_data=extra_data) | [
"def",
"profile",
"(",
"event_type",
",",
"extra_data",
"=",
"None",
")",
":",
"worker",
"=",
"ray",
".",
"worker",
".",
"global_worker",
"return",
"RayLogSpanRaylet",
"(",
"worker",
".",
"profiler",
",",
"event_type",
",",
"extra_data",
"=",
"extra_data",
")"
] | Profile a span of time so that it appears in the timeline visualization.
Note that this only works in the raylet code path.
This function can be used as follows (both on the driver or within a task).
.. code-block:: python
with ray.profile("custom event", extra_data={'key': 'value'}):
# Do some computation here.
Optionally, a dictionary can be passed as the "extra_data" argument, and
it can have keys "name" and "cname" if you want to override the default
timeline display text and box color. Other values will appear at the bottom
of the chrome tracing GUI when you click on the box corresponding to this
profile span.
Args:
event_type: A string describing the type of the event.
extra_data: This must be a dictionary mapping strings to strings. This
data will be added to the json objects that are used to populate
the timeline, so if you want to set a particular color, you can
simply set the "cname" attribute to an appropriate color.
Similarly, if you set the "name" attribute, then that will set the
text displayed on the box in the timeline.
Returns:
An object that can profile a span of time via a "with" statement. | [
"Profile",
"a",
"span",
"of",
"time",
"so",
"that",
"it",
"appears",
"in",
"the",
"timeline",
"visualization",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/profiling.py#L30-L61 |
24,630 | ray-project/ray | python/ray/profiling.py | Profiler._periodically_flush_profile_events | def _periodically_flush_profile_events(self):
"""Drivers run this as a thread to flush profile data in the
background."""
# Note(rkn): This is run on a background thread in the driver. It uses
# the raylet client. This should be ok because it doesn't read
# from the raylet client and we have the GIL here. However,
# if either of those things changes, then we could run into issues.
while True:
# Sleep for 1 second. This will be interrupted if
# self.threads_stopped is set.
self.threads_stopped.wait(timeout=1)
# Exit if we received a signal that we should stop.
if self.threads_stopped.is_set():
return
self.flush_profile_data() | python | def _periodically_flush_profile_events(self):
"""Drivers run this as a thread to flush profile data in the
background."""
# Note(rkn): This is run on a background thread in the driver. It uses
# the raylet client. This should be ok because it doesn't read
# from the raylet client and we have the GIL here. However,
# if either of those things changes, then we could run into issues.
while True:
# Sleep for 1 second. This will be interrupted if
# self.threads_stopped is set.
self.threads_stopped.wait(timeout=1)
# Exit if we received a signal that we should stop.
if self.threads_stopped.is_set():
return
self.flush_profile_data() | [
"def",
"_periodically_flush_profile_events",
"(",
"self",
")",
":",
"# Note(rkn): This is run on a background thread in the driver. It uses",
"# the raylet client. This should be ok because it doesn't read",
"# from the raylet client and we have the GIL here. However,",
"# if either of those things changes, then we could run into issues.",
"while",
"True",
":",
"# Sleep for 1 second. This will be interrupted if",
"# self.threads_stopped is set.",
"self",
".",
"threads_stopped",
".",
"wait",
"(",
"timeout",
"=",
"1",
")",
"# Exit if we received a signal that we should stop.",
"if",
"self",
".",
"threads_stopped",
".",
"is_set",
"(",
")",
":",
"return",
"self",
".",
"flush_profile_data",
"(",
")"
] | Drivers run this as a thread to flush profile data in the
background. | [
"Drivers",
"run",
"this",
"as",
"a",
"thread",
"to",
"flush",
"profile",
"data",
"in",
"the",
"background",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/profiling.py#L94-L110 |
24,631 | ray-project/ray | python/ray/profiling.py | Profiler.flush_profile_data | def flush_profile_data(self):
"""Push the logged profiling data to the global control store."""
with self.lock:
events = self.events
self.events = []
if self.worker.mode == ray.WORKER_MODE:
component_type = "worker"
else:
component_type = "driver"
self.worker.raylet_client.push_profile_events(
component_type, ray.UniqueID(self.worker.worker_id),
self.worker.node_ip_address, events) | python | def flush_profile_data(self):
"""Push the logged profiling data to the global control store."""
with self.lock:
events = self.events
self.events = []
if self.worker.mode == ray.WORKER_MODE:
component_type = "worker"
else:
component_type = "driver"
self.worker.raylet_client.push_profile_events(
component_type, ray.UniqueID(self.worker.worker_id),
self.worker.node_ip_address, events) | [
"def",
"flush_profile_data",
"(",
"self",
")",
":",
"with",
"self",
".",
"lock",
":",
"events",
"=",
"self",
".",
"events",
"self",
".",
"events",
"=",
"[",
"]",
"if",
"self",
".",
"worker",
".",
"mode",
"==",
"ray",
".",
"WORKER_MODE",
":",
"component_type",
"=",
"\"worker\"",
"else",
":",
"component_type",
"=",
"\"driver\"",
"self",
".",
"worker",
".",
"raylet_client",
".",
"push_profile_events",
"(",
"component_type",
",",
"ray",
".",
"UniqueID",
"(",
"self",
".",
"worker",
".",
"worker_id",
")",
",",
"self",
".",
"worker",
".",
"node_ip_address",
",",
"events",
")"
] | Push the logged profiling data to the global control store. | [
"Push",
"the",
"logged",
"profiling",
"data",
"to",
"the",
"global",
"control",
"store",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/profiling.py#L112-L125 |
24,632 | ray-project/ray | python/ray/profiling.py | RayLogSpanRaylet.set_attribute | def set_attribute(self, key, value):
"""Add a key-value pair to the extra_data dict.
This can be used to add attributes that are not available when
ray.profile was called.
Args:
key: The attribute name.
value: The attribute value.
"""
if not isinstance(key, str) or not isinstance(value, str):
raise ValueError("The arguments 'key' and 'value' must both be "
"strings. Instead they are {} and {}.".format(
key, value))
self.extra_data[key] = value | python | def set_attribute(self, key, value):
"""Add a key-value pair to the extra_data dict.
This can be used to add attributes that are not available when
ray.profile was called.
Args:
key: The attribute name.
value: The attribute value.
"""
if not isinstance(key, str) or not isinstance(value, str):
raise ValueError("The arguments 'key' and 'value' must both be "
"strings. Instead they are {} and {}.".format(
key, value))
self.extra_data[key] = value | [
"def",
"set_attribute",
"(",
"self",
",",
"key",
",",
"value",
")",
":",
"if",
"not",
"isinstance",
"(",
"key",
",",
"str",
")",
"or",
"not",
"isinstance",
"(",
"value",
",",
"str",
")",
":",
"raise",
"ValueError",
"(",
"\"The arguments 'key' and 'value' must both be \"",
"\"strings. Instead they are {} and {}.\"",
".",
"format",
"(",
"key",
",",
"value",
")",
")",
"self",
".",
"extra_data",
"[",
"key",
"]",
"=",
"value"
] | Add a key-value pair to the extra_data dict.
This can be used to add attributes that are not available when
ray.profile was called.
Args:
key: The attribute name.
value: The attribute value. | [
"Add",
"a",
"key",
"-",
"value",
"pair",
"to",
"the",
"extra_data",
"dict",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/profiling.py#L146-L160 |
24,633 | ray-project/ray | python/ray/tune/log_sync.py | _LogSyncer.sync_to_worker_if_possible | def sync_to_worker_if_possible(self):
"""Syncs the local logdir on driver to worker if possible.
Requires ray cluster to be started with the autoscaler. Also requires
rsync to be installed.
"""
if self.worker_ip == self.local_ip:
return
ssh_key = get_ssh_key()
ssh_user = get_ssh_user()
global _log_sync_warned
if ssh_key is None or ssh_user is None:
if not _log_sync_warned:
logger.error("Log sync requires cluster to be setup with "
"`ray up`.")
_log_sync_warned = True
return
if not distutils.spawn.find_executable("rsync"):
logger.error("Log sync requires rsync to be installed.")
return
source = "{}/".format(self.local_dir)
target = "{}@{}:{}/".format(ssh_user, self.worker_ip, self.local_dir)
final_cmd = (("""rsync -savz -e "ssh -i {} -o ConnectTimeout=120s """
"""-o StrictHostKeyChecking=no" {} {}""").format(
quote(ssh_key), quote(source), quote(target)))
logger.info("Syncing results to %s", str(self.worker_ip))
sync_process = subprocess.Popen(
final_cmd, shell=True, stdout=self.logfile)
sync_process.wait() | python | def sync_to_worker_if_possible(self):
"""Syncs the local logdir on driver to worker if possible.
Requires ray cluster to be started with the autoscaler. Also requires
rsync to be installed.
"""
if self.worker_ip == self.local_ip:
return
ssh_key = get_ssh_key()
ssh_user = get_ssh_user()
global _log_sync_warned
if ssh_key is None or ssh_user is None:
if not _log_sync_warned:
logger.error("Log sync requires cluster to be setup with "
"`ray up`.")
_log_sync_warned = True
return
if not distutils.spawn.find_executable("rsync"):
logger.error("Log sync requires rsync to be installed.")
return
source = "{}/".format(self.local_dir)
target = "{}@{}:{}/".format(ssh_user, self.worker_ip, self.local_dir)
final_cmd = (("""rsync -savz -e "ssh -i {} -o ConnectTimeout=120s """
"""-o StrictHostKeyChecking=no" {} {}""").format(
quote(ssh_key), quote(source), quote(target)))
logger.info("Syncing results to %s", str(self.worker_ip))
sync_process = subprocess.Popen(
final_cmd, shell=True, stdout=self.logfile)
sync_process.wait() | [
"def",
"sync_to_worker_if_possible",
"(",
"self",
")",
":",
"if",
"self",
".",
"worker_ip",
"==",
"self",
".",
"local_ip",
":",
"return",
"ssh_key",
"=",
"get_ssh_key",
"(",
")",
"ssh_user",
"=",
"get_ssh_user",
"(",
")",
"global",
"_log_sync_warned",
"if",
"ssh_key",
"is",
"None",
"or",
"ssh_user",
"is",
"None",
":",
"if",
"not",
"_log_sync_warned",
":",
"logger",
".",
"error",
"(",
"\"Log sync requires cluster to be setup with \"",
"\"`ray up`.\"",
")",
"_log_sync_warned",
"=",
"True",
"return",
"if",
"not",
"distutils",
".",
"spawn",
".",
"find_executable",
"(",
"\"rsync\"",
")",
":",
"logger",
".",
"error",
"(",
"\"Log sync requires rsync to be installed.\"",
")",
"return",
"source",
"=",
"\"{}/\"",
".",
"format",
"(",
"self",
".",
"local_dir",
")",
"target",
"=",
"\"{}@{}:{}/\"",
".",
"format",
"(",
"ssh_user",
",",
"self",
".",
"worker_ip",
",",
"self",
".",
"local_dir",
")",
"final_cmd",
"=",
"(",
"(",
"\"\"\"rsync -savz -e \"ssh -i {} -o ConnectTimeout=120s \"\"\"",
"\"\"\"-o StrictHostKeyChecking=no\" {} {}\"\"\"",
")",
".",
"format",
"(",
"quote",
"(",
"ssh_key",
")",
",",
"quote",
"(",
"source",
")",
",",
"quote",
"(",
"target",
")",
")",
")",
"logger",
".",
"info",
"(",
"\"Syncing results to %s\"",
",",
"str",
"(",
"self",
".",
"worker_ip",
")",
")",
"sync_process",
"=",
"subprocess",
".",
"Popen",
"(",
"final_cmd",
",",
"shell",
"=",
"True",
",",
"stdout",
"=",
"self",
".",
"logfile",
")",
"sync_process",
".",
"wait",
"(",
")"
] | Syncs the local logdir on driver to worker if possible.
Requires ray cluster to be started with the autoscaler. Also requires
rsync to be installed. | [
"Syncs",
"the",
"local",
"logdir",
"on",
"driver",
"to",
"worker",
"if",
"possible",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/log_sync.py#L131-L159 |
24,634 | ray-project/ray | python/ray/rllib/agents/qmix/mixers.py | QMixer.forward | def forward(self, agent_qs, states):
"""Forward pass for the mixer.
Arguments:
agent_qs: Tensor of shape [B, T, n_agents, n_actions]
states: Tensor of shape [B, T, state_dim]
"""
bs = agent_qs.size(0)
states = states.reshape(-1, self.state_dim)
agent_qs = agent_qs.view(-1, 1, self.n_agents)
# First layer
w1 = th.abs(self.hyper_w_1(states))
b1 = self.hyper_b_1(states)
w1 = w1.view(-1, self.n_agents, self.embed_dim)
b1 = b1.view(-1, 1, self.embed_dim)
hidden = F.elu(th.bmm(agent_qs, w1) + b1)
# Second layer
w_final = th.abs(self.hyper_w_final(states))
w_final = w_final.view(-1, self.embed_dim, 1)
# State-dependent bias
v = self.V(states).view(-1, 1, 1)
# Compute final output
y = th.bmm(hidden, w_final) + v
# Reshape and return
q_tot = y.view(bs, -1, 1)
return q_tot | python | def forward(self, agent_qs, states):
"""Forward pass for the mixer.
Arguments:
agent_qs: Tensor of shape [B, T, n_agents, n_actions]
states: Tensor of shape [B, T, state_dim]
"""
bs = agent_qs.size(0)
states = states.reshape(-1, self.state_dim)
agent_qs = agent_qs.view(-1, 1, self.n_agents)
# First layer
w1 = th.abs(self.hyper_w_1(states))
b1 = self.hyper_b_1(states)
w1 = w1.view(-1, self.n_agents, self.embed_dim)
b1 = b1.view(-1, 1, self.embed_dim)
hidden = F.elu(th.bmm(agent_qs, w1) + b1)
# Second layer
w_final = th.abs(self.hyper_w_final(states))
w_final = w_final.view(-1, self.embed_dim, 1)
# State-dependent bias
v = self.V(states).view(-1, 1, 1)
# Compute final output
y = th.bmm(hidden, w_final) + v
# Reshape and return
q_tot = y.view(bs, -1, 1)
return q_tot | [
"def",
"forward",
"(",
"self",
",",
"agent_qs",
",",
"states",
")",
":",
"bs",
"=",
"agent_qs",
".",
"size",
"(",
"0",
")",
"states",
"=",
"states",
".",
"reshape",
"(",
"-",
"1",
",",
"self",
".",
"state_dim",
")",
"agent_qs",
"=",
"agent_qs",
".",
"view",
"(",
"-",
"1",
",",
"1",
",",
"self",
".",
"n_agents",
")",
"# First layer",
"w1",
"=",
"th",
".",
"abs",
"(",
"self",
".",
"hyper_w_1",
"(",
"states",
")",
")",
"b1",
"=",
"self",
".",
"hyper_b_1",
"(",
"states",
")",
"w1",
"=",
"w1",
".",
"view",
"(",
"-",
"1",
",",
"self",
".",
"n_agents",
",",
"self",
".",
"embed_dim",
")",
"b1",
"=",
"b1",
".",
"view",
"(",
"-",
"1",
",",
"1",
",",
"self",
".",
"embed_dim",
")",
"hidden",
"=",
"F",
".",
"elu",
"(",
"th",
".",
"bmm",
"(",
"agent_qs",
",",
"w1",
")",
"+",
"b1",
")",
"# Second layer",
"w_final",
"=",
"th",
".",
"abs",
"(",
"self",
".",
"hyper_w_final",
"(",
"states",
")",
")",
"w_final",
"=",
"w_final",
".",
"view",
"(",
"-",
"1",
",",
"self",
".",
"embed_dim",
",",
"1",
")",
"# State-dependent bias",
"v",
"=",
"self",
".",
"V",
"(",
"states",
")",
".",
"view",
"(",
"-",
"1",
",",
"1",
",",
"1",
")",
"# Compute final output",
"y",
"=",
"th",
".",
"bmm",
"(",
"hidden",
",",
"w_final",
")",
"+",
"v",
"# Reshape and return",
"q_tot",
"=",
"y",
".",
"view",
"(",
"bs",
",",
"-",
"1",
",",
"1",
")",
"return",
"q_tot"
] | Forward pass for the mixer.
Arguments:
agent_qs: Tensor of shape [B, T, n_agents, n_actions]
states: Tensor of shape [B, T, state_dim] | [
"Forward",
"pass",
"for",
"the",
"mixer",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/agents/qmix/mixers.py#L39-L64 |
24,635 | ray-project/ray | python/ray/tune/suggest/sigopt.py | SigOptSearch.on_trial_complete | def on_trial_complete(self,
trial_id,
result=None,
error=False,
early_terminated=False):
"""Passes the result to SigOpt unless early terminated or errored.
If a trial fails, it will be reported as a failed Observation, telling
the optimizer that the Suggestion led to a metric failure, which
updates the feasible region and improves parameter recommendation.
Creates SigOpt Observation object for trial.
"""
if result:
self.conn.experiments(self.experiment.id).observations().create(
suggestion=self._live_trial_mapping[trial_id].id,
value=result[self._reward_attr],
)
# Update the experiment object
self.experiment = self.conn.experiments(self.experiment.id).fetch()
elif error or early_terminated:
# Reports a failed Observation
self.conn.experiments(self.experiment.id).observations().create(
failed=True, suggestion=self._live_trial_mapping[trial_id].id)
del self._live_trial_mapping[trial_id] | python | def on_trial_complete(self,
trial_id,
result=None,
error=False,
early_terminated=False):
"""Passes the result to SigOpt unless early terminated or errored.
If a trial fails, it will be reported as a failed Observation, telling
the optimizer that the Suggestion led to a metric failure, which
updates the feasible region and improves parameter recommendation.
Creates SigOpt Observation object for trial.
"""
if result:
self.conn.experiments(self.experiment.id).observations().create(
suggestion=self._live_trial_mapping[trial_id].id,
value=result[self._reward_attr],
)
# Update the experiment object
self.experiment = self.conn.experiments(self.experiment.id).fetch()
elif error or early_terminated:
# Reports a failed Observation
self.conn.experiments(self.experiment.id).observations().create(
failed=True, suggestion=self._live_trial_mapping[trial_id].id)
del self._live_trial_mapping[trial_id] | [
"def",
"on_trial_complete",
"(",
"self",
",",
"trial_id",
",",
"result",
"=",
"None",
",",
"error",
"=",
"False",
",",
"early_terminated",
"=",
"False",
")",
":",
"if",
"result",
":",
"self",
".",
"conn",
".",
"experiments",
"(",
"self",
".",
"experiment",
".",
"id",
")",
".",
"observations",
"(",
")",
".",
"create",
"(",
"suggestion",
"=",
"self",
".",
"_live_trial_mapping",
"[",
"trial_id",
"]",
".",
"id",
",",
"value",
"=",
"result",
"[",
"self",
".",
"_reward_attr",
"]",
",",
")",
"# Update the experiment object",
"self",
".",
"experiment",
"=",
"self",
".",
"conn",
".",
"experiments",
"(",
"self",
".",
"experiment",
".",
"id",
")",
".",
"fetch",
"(",
")",
"elif",
"error",
"or",
"early_terminated",
":",
"# Reports a failed Observation",
"self",
".",
"conn",
".",
"experiments",
"(",
"self",
".",
"experiment",
".",
"id",
")",
".",
"observations",
"(",
")",
".",
"create",
"(",
"failed",
"=",
"True",
",",
"suggestion",
"=",
"self",
".",
"_live_trial_mapping",
"[",
"trial_id",
"]",
".",
"id",
")",
"del",
"self",
".",
"_live_trial_mapping",
"[",
"trial_id",
"]"
] | Passes the result to SigOpt unless early terminated or errored.
If a trial fails, it will be reported as a failed Observation, telling
the optimizer that the Suggestion led to a metric failure, which
updates the feasible region and improves parameter recommendation.
Creates SigOpt Observation object for trial. | [
"Passes",
"the",
"result",
"to",
"SigOpt",
"unless",
"early",
"terminated",
"or",
"errored",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/suggest/sigopt.py#L95-L119 |
24,636 | ray-project/ray | python/ray/experimental/sgd/tfbench/resnet_model.py | bottleneck_block_v1 | def bottleneck_block_v1(cnn, depth, depth_bottleneck, stride):
"""Bottleneck block with identity short-cut for ResNet v1.
Args:
cnn: the network to append bottleneck blocks.
depth: the number of output filters for this bottleneck block.
depth_bottleneck: the number of bottleneck filters for this block.
stride: Stride used in the first layer of the bottleneck block.
"""
input_layer = cnn.top_layer
in_size = cnn.top_size
name_key = "resnet_v1"
name = name_key + str(cnn.counts[name_key])
cnn.counts[name_key] += 1
with tf.variable_scope(name):
if depth == in_size:
if stride == 1:
shortcut = input_layer
else:
shortcut = cnn.apool(
1,
1,
stride,
stride,
input_layer=input_layer,
num_channels_in=in_size)
else:
shortcut = cnn.conv(
depth,
1,
1,
stride,
stride,
activation=None,
use_batch_norm=True,
input_layer=input_layer,
num_channels_in=in_size,
bias=None)
cnn.conv(
depth_bottleneck,
1,
1,
stride,
stride,
input_layer=input_layer,
num_channels_in=in_size,
use_batch_norm=True,
bias=None)
cnn.conv(
depth_bottleneck,
3,
3,
1,
1,
mode="SAME_RESNET",
use_batch_norm=True,
bias=None)
res = cnn.conv(
depth, 1, 1, 1, 1, activation=None, use_batch_norm=True, bias=None)
output = tf.nn.relu(shortcut + res)
cnn.top_layer = output
cnn.top_size = depth | python | def bottleneck_block_v1(cnn, depth, depth_bottleneck, stride):
"""Bottleneck block with identity short-cut for ResNet v1.
Args:
cnn: the network to append bottleneck blocks.
depth: the number of output filters for this bottleneck block.
depth_bottleneck: the number of bottleneck filters for this block.
stride: Stride used in the first layer of the bottleneck block.
"""
input_layer = cnn.top_layer
in_size = cnn.top_size
name_key = "resnet_v1"
name = name_key + str(cnn.counts[name_key])
cnn.counts[name_key] += 1
with tf.variable_scope(name):
if depth == in_size:
if stride == 1:
shortcut = input_layer
else:
shortcut = cnn.apool(
1,
1,
stride,
stride,
input_layer=input_layer,
num_channels_in=in_size)
else:
shortcut = cnn.conv(
depth,
1,
1,
stride,
stride,
activation=None,
use_batch_norm=True,
input_layer=input_layer,
num_channels_in=in_size,
bias=None)
cnn.conv(
depth_bottleneck,
1,
1,
stride,
stride,
input_layer=input_layer,
num_channels_in=in_size,
use_batch_norm=True,
bias=None)
cnn.conv(
depth_bottleneck,
3,
3,
1,
1,
mode="SAME_RESNET",
use_batch_norm=True,
bias=None)
res = cnn.conv(
depth, 1, 1, 1, 1, activation=None, use_batch_norm=True, bias=None)
output = tf.nn.relu(shortcut + res)
cnn.top_layer = output
cnn.top_size = depth | [
"def",
"bottleneck_block_v1",
"(",
"cnn",
",",
"depth",
",",
"depth_bottleneck",
",",
"stride",
")",
":",
"input_layer",
"=",
"cnn",
".",
"top_layer",
"in_size",
"=",
"cnn",
".",
"top_size",
"name_key",
"=",
"\"resnet_v1\"",
"name",
"=",
"name_key",
"+",
"str",
"(",
"cnn",
".",
"counts",
"[",
"name_key",
"]",
")",
"cnn",
".",
"counts",
"[",
"name_key",
"]",
"+=",
"1",
"with",
"tf",
".",
"variable_scope",
"(",
"name",
")",
":",
"if",
"depth",
"==",
"in_size",
":",
"if",
"stride",
"==",
"1",
":",
"shortcut",
"=",
"input_layer",
"else",
":",
"shortcut",
"=",
"cnn",
".",
"apool",
"(",
"1",
",",
"1",
",",
"stride",
",",
"stride",
",",
"input_layer",
"=",
"input_layer",
",",
"num_channels_in",
"=",
"in_size",
")",
"else",
":",
"shortcut",
"=",
"cnn",
".",
"conv",
"(",
"depth",
",",
"1",
",",
"1",
",",
"stride",
",",
"stride",
",",
"activation",
"=",
"None",
",",
"use_batch_norm",
"=",
"True",
",",
"input_layer",
"=",
"input_layer",
",",
"num_channels_in",
"=",
"in_size",
",",
"bias",
"=",
"None",
")",
"cnn",
".",
"conv",
"(",
"depth_bottleneck",
",",
"1",
",",
"1",
",",
"stride",
",",
"stride",
",",
"input_layer",
"=",
"input_layer",
",",
"num_channels_in",
"=",
"in_size",
",",
"use_batch_norm",
"=",
"True",
",",
"bias",
"=",
"None",
")",
"cnn",
".",
"conv",
"(",
"depth_bottleneck",
",",
"3",
",",
"3",
",",
"1",
",",
"1",
",",
"mode",
"=",
"\"SAME_RESNET\"",
",",
"use_batch_norm",
"=",
"True",
",",
"bias",
"=",
"None",
")",
"res",
"=",
"cnn",
".",
"conv",
"(",
"depth",
",",
"1",
",",
"1",
",",
"1",
",",
"1",
",",
"activation",
"=",
"None",
",",
"use_batch_norm",
"=",
"True",
",",
"bias",
"=",
"None",
")",
"output",
"=",
"tf",
".",
"nn",
".",
"relu",
"(",
"shortcut",
"+",
"res",
")",
"cnn",
".",
"top_layer",
"=",
"output",
"cnn",
".",
"top_size",
"=",
"depth"
] | Bottleneck block with identity short-cut for ResNet v1.
Args:
cnn: the network to append bottleneck blocks.
depth: the number of output filters for this bottleneck block.
depth_bottleneck: the number of bottleneck filters for this block.
stride: Stride used in the first layer of the bottleneck block. | [
"Bottleneck",
"block",
"with",
"identity",
"short",
"-",
"cut",
"for",
"ResNet",
"v1",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/tfbench/resnet_model.py#L39-L101 |
24,637 | ray-project/ray | python/ray/experimental/sgd/tfbench/resnet_model.py | bottleneck_block | def bottleneck_block(cnn, depth, depth_bottleneck, stride, pre_activation):
"""Bottleneck block with identity short-cut.
Args:
cnn: the network to append bottleneck blocks.
depth: the number of output filters for this bottleneck block.
depth_bottleneck: the number of bottleneck filters for this block.
stride: Stride used in the first layer of the bottleneck block.
pre_activation: use pre_activation structure used in v2 or not.
"""
if pre_activation:
bottleneck_block_v2(cnn, depth, depth_bottleneck, stride)
else:
bottleneck_block_v1(cnn, depth, depth_bottleneck, stride) | python | def bottleneck_block(cnn, depth, depth_bottleneck, stride, pre_activation):
"""Bottleneck block with identity short-cut.
Args:
cnn: the network to append bottleneck blocks.
depth: the number of output filters for this bottleneck block.
depth_bottleneck: the number of bottleneck filters for this block.
stride: Stride used in the first layer of the bottleneck block.
pre_activation: use pre_activation structure used in v2 or not.
"""
if pre_activation:
bottleneck_block_v2(cnn, depth, depth_bottleneck, stride)
else:
bottleneck_block_v1(cnn, depth, depth_bottleneck, stride) | [
"def",
"bottleneck_block",
"(",
"cnn",
",",
"depth",
",",
"depth_bottleneck",
",",
"stride",
",",
"pre_activation",
")",
":",
"if",
"pre_activation",
":",
"bottleneck_block_v2",
"(",
"cnn",
",",
"depth",
",",
"depth_bottleneck",
",",
"stride",
")",
"else",
":",
"bottleneck_block_v1",
"(",
"cnn",
",",
"depth",
",",
"depth_bottleneck",
",",
"stride",
")"
] | Bottleneck block with identity short-cut.
Args:
cnn: the network to append bottleneck blocks.
depth: the number of output filters for this bottleneck block.
depth_bottleneck: the number of bottleneck filters for this block.
stride: Stride used in the first layer of the bottleneck block.
pre_activation: use pre_activation structure used in v2 or not. | [
"Bottleneck",
"block",
"with",
"identity",
"short",
"-",
"cut",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/tfbench/resnet_model.py#L182-L195 |
24,638 | ray-project/ray | python/ray/experimental/sgd/tfbench/resnet_model.py | residual_block | def residual_block(cnn, depth, stride, pre_activation):
"""Residual block with identity short-cut.
Args:
cnn: the network to append residual blocks.
depth: the number of output filters for this residual block.
stride: Stride used in the first layer of the residual block.
pre_activation: use pre_activation structure or not.
"""
input_layer = cnn.top_layer
in_size = cnn.top_size
if in_size != depth:
# Plan A of shortcut.
shortcut = cnn.apool(
1,
1,
stride,
stride,
input_layer=input_layer,
num_channels_in=in_size)
padding = (depth - in_size) // 2
if cnn.channel_pos == "channels_last":
shortcut = tf.pad(shortcut,
[[0, 0], [0, 0], [0, 0], [padding, padding]])
else:
shortcut = tf.pad(shortcut,
[[0, 0], [padding, padding], [0, 0], [0, 0]])
else:
shortcut = input_layer
if pre_activation:
res = cnn.batch_norm(input_layer)
res = tf.nn.relu(res)
else:
res = input_layer
cnn.conv(
depth,
3,
3,
stride,
stride,
input_layer=res,
num_channels_in=in_size,
use_batch_norm=True,
bias=None)
if pre_activation:
res = cnn.conv(
depth,
3,
3,
1,
1,
activation=None,
use_batch_norm=False,
bias=None)
output = shortcut + res
else:
res = cnn.conv(
depth, 3, 3, 1, 1, activation=None, use_batch_norm=True, bias=None)
output = tf.nn.relu(shortcut + res)
cnn.top_layer = output
cnn.top_size = depth | python | def residual_block(cnn, depth, stride, pre_activation):
"""Residual block with identity short-cut.
Args:
cnn: the network to append residual blocks.
depth: the number of output filters for this residual block.
stride: Stride used in the first layer of the residual block.
pre_activation: use pre_activation structure or not.
"""
input_layer = cnn.top_layer
in_size = cnn.top_size
if in_size != depth:
# Plan A of shortcut.
shortcut = cnn.apool(
1,
1,
stride,
stride,
input_layer=input_layer,
num_channels_in=in_size)
padding = (depth - in_size) // 2
if cnn.channel_pos == "channels_last":
shortcut = tf.pad(shortcut,
[[0, 0], [0, 0], [0, 0], [padding, padding]])
else:
shortcut = tf.pad(shortcut,
[[0, 0], [padding, padding], [0, 0], [0, 0]])
else:
shortcut = input_layer
if pre_activation:
res = cnn.batch_norm(input_layer)
res = tf.nn.relu(res)
else:
res = input_layer
cnn.conv(
depth,
3,
3,
stride,
stride,
input_layer=res,
num_channels_in=in_size,
use_batch_norm=True,
bias=None)
if pre_activation:
res = cnn.conv(
depth,
3,
3,
1,
1,
activation=None,
use_batch_norm=False,
bias=None)
output = shortcut + res
else:
res = cnn.conv(
depth, 3, 3, 1, 1, activation=None, use_batch_norm=True, bias=None)
output = tf.nn.relu(shortcut + res)
cnn.top_layer = output
cnn.top_size = depth | [
"def",
"residual_block",
"(",
"cnn",
",",
"depth",
",",
"stride",
",",
"pre_activation",
")",
":",
"input_layer",
"=",
"cnn",
".",
"top_layer",
"in_size",
"=",
"cnn",
".",
"top_size",
"if",
"in_size",
"!=",
"depth",
":",
"# Plan A of shortcut.",
"shortcut",
"=",
"cnn",
".",
"apool",
"(",
"1",
",",
"1",
",",
"stride",
",",
"stride",
",",
"input_layer",
"=",
"input_layer",
",",
"num_channels_in",
"=",
"in_size",
")",
"padding",
"=",
"(",
"depth",
"-",
"in_size",
")",
"//",
"2",
"if",
"cnn",
".",
"channel_pos",
"==",
"\"channels_last\"",
":",
"shortcut",
"=",
"tf",
".",
"pad",
"(",
"shortcut",
",",
"[",
"[",
"0",
",",
"0",
"]",
",",
"[",
"0",
",",
"0",
"]",
",",
"[",
"0",
",",
"0",
"]",
",",
"[",
"padding",
",",
"padding",
"]",
"]",
")",
"else",
":",
"shortcut",
"=",
"tf",
".",
"pad",
"(",
"shortcut",
",",
"[",
"[",
"0",
",",
"0",
"]",
",",
"[",
"padding",
",",
"padding",
"]",
",",
"[",
"0",
",",
"0",
"]",
",",
"[",
"0",
",",
"0",
"]",
"]",
")",
"else",
":",
"shortcut",
"=",
"input_layer",
"if",
"pre_activation",
":",
"res",
"=",
"cnn",
".",
"batch_norm",
"(",
"input_layer",
")",
"res",
"=",
"tf",
".",
"nn",
".",
"relu",
"(",
"res",
")",
"else",
":",
"res",
"=",
"input_layer",
"cnn",
".",
"conv",
"(",
"depth",
",",
"3",
",",
"3",
",",
"stride",
",",
"stride",
",",
"input_layer",
"=",
"res",
",",
"num_channels_in",
"=",
"in_size",
",",
"use_batch_norm",
"=",
"True",
",",
"bias",
"=",
"None",
")",
"if",
"pre_activation",
":",
"res",
"=",
"cnn",
".",
"conv",
"(",
"depth",
",",
"3",
",",
"3",
",",
"1",
",",
"1",
",",
"activation",
"=",
"None",
",",
"use_batch_norm",
"=",
"False",
",",
"bias",
"=",
"None",
")",
"output",
"=",
"shortcut",
"+",
"res",
"else",
":",
"res",
"=",
"cnn",
".",
"conv",
"(",
"depth",
",",
"3",
",",
"3",
",",
"1",
",",
"1",
",",
"activation",
"=",
"None",
",",
"use_batch_norm",
"=",
"True",
",",
"bias",
"=",
"None",
")",
"output",
"=",
"tf",
".",
"nn",
".",
"relu",
"(",
"shortcut",
"+",
"res",
")",
"cnn",
".",
"top_layer",
"=",
"output",
"cnn",
".",
"top_size",
"=",
"depth"
] | Residual block with identity short-cut.
Args:
cnn: the network to append residual blocks.
depth: the number of output filters for this residual block.
stride: Stride used in the first layer of the residual block.
pre_activation: use pre_activation structure or not. | [
"Residual",
"block",
"with",
"identity",
"short",
"-",
"cut",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/tfbench/resnet_model.py#L198-L258 |
24,639 | ray-project/ray | python/ray/rllib/utils/filter.py | MeanStdFilter.apply_changes | def apply_changes(self, other, with_buffer=False):
"""Applies updates from the buffer of another filter.
Params:
other (MeanStdFilter): Other filter to apply info from
with_buffer (bool): Flag for specifying if the buffer should be
copied from other.
Examples:
>>> a = MeanStdFilter(())
>>> a(1)
>>> a(2)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[2, 1.5, 2]
>>> b = MeanStdFilter(())
>>> b(10)
>>> a.apply_changes(b, with_buffer=False)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[3, 4.333333333333333, 2]
>>> a.apply_changes(b, with_buffer=True)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[4, 5.75, 1]
"""
self.rs.update(other.buffer)
if with_buffer:
self.buffer = other.buffer.copy() | python | def apply_changes(self, other, with_buffer=False):
"""Applies updates from the buffer of another filter.
Params:
other (MeanStdFilter): Other filter to apply info from
with_buffer (bool): Flag for specifying if the buffer should be
copied from other.
Examples:
>>> a = MeanStdFilter(())
>>> a(1)
>>> a(2)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[2, 1.5, 2]
>>> b = MeanStdFilter(())
>>> b(10)
>>> a.apply_changes(b, with_buffer=False)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[3, 4.333333333333333, 2]
>>> a.apply_changes(b, with_buffer=True)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[4, 5.75, 1]
"""
self.rs.update(other.buffer)
if with_buffer:
self.buffer = other.buffer.copy() | [
"def",
"apply_changes",
"(",
"self",
",",
"other",
",",
"with_buffer",
"=",
"False",
")",
":",
"self",
".",
"rs",
".",
"update",
"(",
"other",
".",
"buffer",
")",
"if",
"with_buffer",
":",
"self",
".",
"buffer",
"=",
"other",
".",
"buffer",
".",
"copy",
"(",
")"
] | Applies updates from the buffer of another filter.
Params:
other (MeanStdFilter): Other filter to apply info from
with_buffer (bool): Flag for specifying if the buffer should be
copied from other.
Examples:
>>> a = MeanStdFilter(())
>>> a(1)
>>> a(2)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[2, 1.5, 2]
>>> b = MeanStdFilter(())
>>> b(10)
>>> a.apply_changes(b, with_buffer=False)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[3, 4.333333333333333, 2]
>>> a.apply_changes(b, with_buffer=True)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[4, 5.75, 1] | [
"Applies",
"updates",
"from",
"the",
"buffer",
"of",
"another",
"filter",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/filter.py#L156-L181 |
24,640 | ray-project/ray | python/ray/rllib/utils/filter.py | MeanStdFilter.sync | def sync(self, other):
"""Syncs all fields together from other filter.
Examples:
>>> a = MeanStdFilter(())
>>> a(1)
>>> a(2)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[2, array(1.5), 2]
>>> b = MeanStdFilter(())
>>> b(10)
>>> print([b.rs.n, b.rs.mean, b.buffer.n])
[1, array(10.0), 1]
>>> a.sync(b)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[1, array(10.0), 1]
"""
assert other.shape == self.shape, "Shapes don't match!"
self.demean = other.demean
self.destd = other.destd
self.clip = other.clip
self.rs = other.rs.copy()
self.buffer = other.buffer.copy() | python | def sync(self, other):
"""Syncs all fields together from other filter.
Examples:
>>> a = MeanStdFilter(())
>>> a(1)
>>> a(2)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[2, array(1.5), 2]
>>> b = MeanStdFilter(())
>>> b(10)
>>> print([b.rs.n, b.rs.mean, b.buffer.n])
[1, array(10.0), 1]
>>> a.sync(b)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[1, array(10.0), 1]
"""
assert other.shape == self.shape, "Shapes don't match!"
self.demean = other.demean
self.destd = other.destd
self.clip = other.clip
self.rs = other.rs.copy()
self.buffer = other.buffer.copy() | [
"def",
"sync",
"(",
"self",
",",
"other",
")",
":",
"assert",
"other",
".",
"shape",
"==",
"self",
".",
"shape",
",",
"\"Shapes don't match!\"",
"self",
".",
"demean",
"=",
"other",
".",
"demean",
"self",
".",
"destd",
"=",
"other",
".",
"destd",
"self",
".",
"clip",
"=",
"other",
".",
"clip",
"self",
".",
"rs",
"=",
"other",
".",
"rs",
".",
"copy",
"(",
")",
"self",
".",
"buffer",
"=",
"other",
".",
"buffer",
".",
"copy",
"(",
")"
] | Syncs all fields together from other filter.
Examples:
>>> a = MeanStdFilter(())
>>> a(1)
>>> a(2)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[2, array(1.5), 2]
>>> b = MeanStdFilter(())
>>> b(10)
>>> print([b.rs.n, b.rs.mean, b.buffer.n])
[1, array(10.0), 1]
>>> a.sync(b)
>>> print([a.rs.n, a.rs.mean, a.buffer.n])
[1, array(10.0), 1] | [
"Syncs",
"all",
"fields",
"together",
"from",
"other",
"filter",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/filter.py#L192-L214 |
24,641 | ray-project/ray | python/ray/rllib/utils/filter.py | ConcurrentMeanStdFilter.as_serializable | def as_serializable(self):
"""Returns non-concurrent version of current class"""
other = MeanStdFilter(self.shape)
other.sync(self)
return other | python | def as_serializable(self):
"""Returns non-concurrent version of current class"""
other = MeanStdFilter(self.shape)
other.sync(self)
return other | [
"def",
"as_serializable",
"(",
"self",
")",
":",
"other",
"=",
"MeanStdFilter",
"(",
"self",
".",
"shape",
")",
"other",
".",
"sync",
"(",
"self",
")",
"return",
"other"
] | Returns non-concurrent version of current class | [
"Returns",
"non",
"-",
"concurrent",
"version",
"of",
"current",
"class"
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/filter.py#L258-L262 |
24,642 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | parse_general_int | def parse_general_int(s):
"""Parse integer with power-of-2 suffix eg. 32k."""
mo = re.match(r"(\d+)([KkMGT]?)$", s)
if mo:
i, suffix = mo.group(1, 2)
v = int(i)
if suffix:
if suffix == "K" or suffix == "k":
v *= 1024
elif suffix == "M":
v *= (1024 * 1024)
elif suffix == "G":
v *= (1024 * 1024 * 1024)
elif suffix == "T":
v *= (1024 * 1024 * 1024 * 1024)
else:
raise ValueError("invalid integer string %s" % s)
return v
else:
v = int(s)
return v | python | def parse_general_int(s):
"""Parse integer with power-of-2 suffix eg. 32k."""
mo = re.match(r"(\d+)([KkMGT]?)$", s)
if mo:
i, suffix = mo.group(1, 2)
v = int(i)
if suffix:
if suffix == "K" or suffix == "k":
v *= 1024
elif suffix == "M":
v *= (1024 * 1024)
elif suffix == "G":
v *= (1024 * 1024 * 1024)
elif suffix == "T":
v *= (1024 * 1024 * 1024 * 1024)
else:
raise ValueError("invalid integer string %s" % s)
return v
else:
v = int(s)
return v | [
"def",
"parse_general_int",
"(",
"s",
")",
":",
"mo",
"=",
"re",
".",
"match",
"(",
"r\"(\\d+)([KkMGT]?)$\"",
",",
"s",
")",
"if",
"mo",
":",
"i",
",",
"suffix",
"=",
"mo",
".",
"group",
"(",
"1",
",",
"2",
")",
"v",
"=",
"int",
"(",
"i",
")",
"if",
"suffix",
":",
"if",
"suffix",
"==",
"\"K\"",
"or",
"suffix",
"==",
"\"k\"",
":",
"v",
"*=",
"1024",
"elif",
"suffix",
"==",
"\"M\"",
":",
"v",
"*=",
"(",
"1024",
"*",
"1024",
")",
"elif",
"suffix",
"==",
"\"G\"",
":",
"v",
"*=",
"(",
"1024",
"*",
"1024",
"*",
"1024",
")",
"elif",
"suffix",
"==",
"\"T\"",
":",
"v",
"*=",
"(",
"1024",
"*",
"1024",
"*",
"1024",
"*",
"1024",
")",
"else",
":",
"raise",
"ValueError",
"(",
"\"invalid integer string %s\"",
"%",
"s",
")",
"return",
"v",
"else",
":",
"v",
"=",
"int",
"(",
"s",
")",
"return",
"v"
] | Parse integer with power-of-2 suffix eg. 32k. | [
"Parse",
"integer",
"with",
"power",
"-",
"of",
"-",
"2",
"suffix",
"eg",
".",
"32k",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L38-L58 |
24,643 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | parse_all_reduce_spec | def parse_all_reduce_spec(all_reduce_spec):
"""Parse all_reduce_spec.
Args:
all_reduce_spec: a string specifying a combination of all-reduce
algorithms to apply for gradient reduction.
Returns:
a list of AllReduceSpecTuple.
Raises:
ValueError: all_reduce_spec is not well-formed.
An all_reduce_spec has BNF form:
int ::= positive whole number
g_int ::= int[KkMGT]?
alg_spec ::= alg | alg#int
range_spec ::= alg_spec | alg_spec/alg_spec
spec ::= range_spec | range_spec:g_int:range_spec
Not all syntactically correct specifications are supported.
Examples of supported all_reduce_spec strings, with semantics explained:
"xring" == apply ring all-reduce to all tensors
"xring#2" == apply ring all-reduce to all tensors, using two simultaneous
transfer rings, each operating on 1/2 of each tensor.
"nccl" == apply NCCL all-reduce to all tensors (only works within
a single worker process where all devices are GPUs)
"nccl/xring" == apply NCCL all-reduce to all tensors within each worker
to produce at least one full-reduced (locally) value,
then apply ring all-reduce to one such value from each
worker, then apply NCCL broadcast to propagate those globally
reduced values back to every device within each worker.
"pscpu" == Shuffle reduce using worker CPUs as the gather devices: each
distributed tensor is reduced by copying all instances to
one of the worker CPUs, computing the reduction there, then
copying back to each participating device. Tensor reductions
are assigned to specific CPUs round-robin.
"psgpu#4" == Arrange all GPUs across all workers into groups of 4.
Each distributed tensor is shuffle reduced against one
such group of 4 GPUs, selected round-robin. That is, each
tensor is split across 4 shards for the reduction.
"pscpu:2k:pscpu#2:64k:xring" == Apply single-shard pscpu to
tensors of size <= 2048 elements, apply 2-shard pscpu to
tensors up to size 64k elements, apply xring to larger tensors.
"pscpu/pscpu#2" == Use shuffle gather to locally reduce each tensor on
the worker's CPU, then use 2-shard shuffle to reduce those
locally reduced tensors across workers (on the worker CPUs), then
scatter the globally reduced values locally from each worker CPU.
"""
range_parts = all_reduce_spec.split(":") + ["-1"]
if len(range_parts) % 2:
raise ValueError(
"all_reduce_spec not well formed: %s" % all_reduce_spec)
limit = 0
spec = []
alg = None
shards = 1
for i, range_part in enumerate(range_parts):
if i % 2 == 1:
try:
limit = parse_general_int(range_part)
spec.append(
AllReduceSpecTuple(alg=alg, shards=shards, limit=limit))
except ValueError:
raise ValueError(
"all_reduce_spec (%s) contains non-integer range %s" %
(all_reduce_spec, range_part))
else:
alg = range_part
alg_parts = range_part.split("#")
alg = alg_parts[0]
if len(alg_parts) > 1:
try:
shards = int(alg_parts[1])
except ValueError:
raise ValueError(
"all_reduce_spec (%s) contains non-integer "
"shards %s" % all_reduce_spec, alg_parts[1])
else:
shards = 1
if alg not in [
"nccl", "nccl/xring", "nccl/rechd", "nccl/pscpu", "xring",
"pscpu", "psgpu", "pscpu/pscpu"
]:
raise ValueError("all_reduce_spec (%s) contains invalid alg %s"
% (all_reduce_spec, alg))
return spec | python | def parse_all_reduce_spec(all_reduce_spec):
"""Parse all_reduce_spec.
Args:
all_reduce_spec: a string specifying a combination of all-reduce
algorithms to apply for gradient reduction.
Returns:
a list of AllReduceSpecTuple.
Raises:
ValueError: all_reduce_spec is not well-formed.
An all_reduce_spec has BNF form:
int ::= positive whole number
g_int ::= int[KkMGT]?
alg_spec ::= alg | alg#int
range_spec ::= alg_spec | alg_spec/alg_spec
spec ::= range_spec | range_spec:g_int:range_spec
Not all syntactically correct specifications are supported.
Examples of supported all_reduce_spec strings, with semantics explained:
"xring" == apply ring all-reduce to all tensors
"xring#2" == apply ring all-reduce to all tensors, using two simultaneous
transfer rings, each operating on 1/2 of each tensor.
"nccl" == apply NCCL all-reduce to all tensors (only works within
a single worker process where all devices are GPUs)
"nccl/xring" == apply NCCL all-reduce to all tensors within each worker
to produce at least one full-reduced (locally) value,
then apply ring all-reduce to one such value from each
worker, then apply NCCL broadcast to propagate those globally
reduced values back to every device within each worker.
"pscpu" == Shuffle reduce using worker CPUs as the gather devices: each
distributed tensor is reduced by copying all instances to
one of the worker CPUs, computing the reduction there, then
copying back to each participating device. Tensor reductions
are assigned to specific CPUs round-robin.
"psgpu#4" == Arrange all GPUs across all workers into groups of 4.
Each distributed tensor is shuffle reduced against one
such group of 4 GPUs, selected round-robin. That is, each
tensor is split across 4 shards for the reduction.
"pscpu:2k:pscpu#2:64k:xring" == Apply single-shard pscpu to
tensors of size <= 2048 elements, apply 2-shard pscpu to
tensors up to size 64k elements, apply xring to larger tensors.
"pscpu/pscpu#2" == Use shuffle gather to locally reduce each tensor on
the worker's CPU, then use 2-shard shuffle to reduce those
locally reduced tensors across workers (on the worker CPUs), then
scatter the globally reduced values locally from each worker CPU.
"""
range_parts = all_reduce_spec.split(":") + ["-1"]
if len(range_parts) % 2:
raise ValueError(
"all_reduce_spec not well formed: %s" % all_reduce_spec)
limit = 0
spec = []
alg = None
shards = 1
for i, range_part in enumerate(range_parts):
if i % 2 == 1:
try:
limit = parse_general_int(range_part)
spec.append(
AllReduceSpecTuple(alg=alg, shards=shards, limit=limit))
except ValueError:
raise ValueError(
"all_reduce_spec (%s) contains non-integer range %s" %
(all_reduce_spec, range_part))
else:
alg = range_part
alg_parts = range_part.split("#")
alg = alg_parts[0]
if len(alg_parts) > 1:
try:
shards = int(alg_parts[1])
except ValueError:
raise ValueError(
"all_reduce_spec (%s) contains non-integer "
"shards %s" % all_reduce_spec, alg_parts[1])
else:
shards = 1
if alg not in [
"nccl", "nccl/xring", "nccl/rechd", "nccl/pscpu", "xring",
"pscpu", "psgpu", "pscpu/pscpu"
]:
raise ValueError("all_reduce_spec (%s) contains invalid alg %s"
% (all_reduce_spec, alg))
return spec | [
"def",
"parse_all_reduce_spec",
"(",
"all_reduce_spec",
")",
":",
"range_parts",
"=",
"all_reduce_spec",
".",
"split",
"(",
"\":\"",
")",
"+",
"[",
"\"-1\"",
"]",
"if",
"len",
"(",
"range_parts",
")",
"%",
"2",
":",
"raise",
"ValueError",
"(",
"\"all_reduce_spec not well formed: %s\"",
"%",
"all_reduce_spec",
")",
"limit",
"=",
"0",
"spec",
"=",
"[",
"]",
"alg",
"=",
"None",
"shards",
"=",
"1",
"for",
"i",
",",
"range_part",
"in",
"enumerate",
"(",
"range_parts",
")",
":",
"if",
"i",
"%",
"2",
"==",
"1",
":",
"try",
":",
"limit",
"=",
"parse_general_int",
"(",
"range_part",
")",
"spec",
".",
"append",
"(",
"AllReduceSpecTuple",
"(",
"alg",
"=",
"alg",
",",
"shards",
"=",
"shards",
",",
"limit",
"=",
"limit",
")",
")",
"except",
"ValueError",
":",
"raise",
"ValueError",
"(",
"\"all_reduce_spec (%s) contains non-integer range %s\"",
"%",
"(",
"all_reduce_spec",
",",
"range_part",
")",
")",
"else",
":",
"alg",
"=",
"range_part",
"alg_parts",
"=",
"range_part",
".",
"split",
"(",
"\"#\"",
")",
"alg",
"=",
"alg_parts",
"[",
"0",
"]",
"if",
"len",
"(",
"alg_parts",
")",
">",
"1",
":",
"try",
":",
"shards",
"=",
"int",
"(",
"alg_parts",
"[",
"1",
"]",
")",
"except",
"ValueError",
":",
"raise",
"ValueError",
"(",
"\"all_reduce_spec (%s) contains non-integer \"",
"\"shards %s\"",
"%",
"all_reduce_spec",
",",
"alg_parts",
"[",
"1",
"]",
")",
"else",
":",
"shards",
"=",
"1",
"if",
"alg",
"not",
"in",
"[",
"\"nccl\"",
",",
"\"nccl/xring\"",
",",
"\"nccl/rechd\"",
",",
"\"nccl/pscpu\"",
",",
"\"xring\"",
",",
"\"pscpu\"",
",",
"\"psgpu\"",
",",
"\"pscpu/pscpu\"",
"]",
":",
"raise",
"ValueError",
"(",
"\"all_reduce_spec (%s) contains invalid alg %s\"",
"%",
"(",
"all_reduce_spec",
",",
"alg",
")",
")",
"return",
"spec"
] | Parse all_reduce_spec.
Args:
all_reduce_spec: a string specifying a combination of all-reduce
algorithms to apply for gradient reduction.
Returns:
a list of AllReduceSpecTuple.
Raises:
ValueError: all_reduce_spec is not well-formed.
An all_reduce_spec has BNF form:
int ::= positive whole number
g_int ::= int[KkMGT]?
alg_spec ::= alg | alg#int
range_spec ::= alg_spec | alg_spec/alg_spec
spec ::= range_spec | range_spec:g_int:range_spec
Not all syntactically correct specifications are supported.
Examples of supported all_reduce_spec strings, with semantics explained:
"xring" == apply ring all-reduce to all tensors
"xring#2" == apply ring all-reduce to all tensors, using two simultaneous
transfer rings, each operating on 1/2 of each tensor.
"nccl" == apply NCCL all-reduce to all tensors (only works within
a single worker process where all devices are GPUs)
"nccl/xring" == apply NCCL all-reduce to all tensors within each worker
to produce at least one full-reduced (locally) value,
then apply ring all-reduce to one such value from each
worker, then apply NCCL broadcast to propagate those globally
reduced values back to every device within each worker.
"pscpu" == Shuffle reduce using worker CPUs as the gather devices: each
distributed tensor is reduced by copying all instances to
one of the worker CPUs, computing the reduction there, then
copying back to each participating device. Tensor reductions
are assigned to specific CPUs round-robin.
"psgpu#4" == Arrange all GPUs across all workers into groups of 4.
Each distributed tensor is shuffle reduced against one
such group of 4 GPUs, selected round-robin. That is, each
tensor is split across 4 shards for the reduction.
"pscpu:2k:pscpu#2:64k:xring" == Apply single-shard pscpu to
tensors of size <= 2048 elements, apply 2-shard pscpu to
tensors up to size 64k elements, apply xring to larger tensors.
"pscpu/pscpu#2" == Use shuffle gather to locally reduce each tensor on
the worker's CPU, then use 2-shard shuffle to reduce those
locally reduced tensors across workers (on the worker CPUs), then
scatter the globally reduced values locally from each worker CPU. | [
"Parse",
"all_reduce_spec",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L61-L148 |
24,644 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | build_all_reduce_device_prefixes | def build_all_reduce_device_prefixes(job_name, num_tasks):
"""Build list of device prefix names for all_reduce.
Args:
job_name: "worker", "ps" or "localhost".
num_tasks: number of jobs across which device names should be generated.
Returns:
A list of device name prefix strings. Each element spells out the full
host name without adding the device.
e.g. "/job:worker/task:0"
"""
if job_name != "localhost":
return ["/job:%s/task:%d" % (job_name, d) for d in range(0, num_tasks)]
else:
assert num_tasks == 1
return ["/job:%s" % job_name] | python | def build_all_reduce_device_prefixes(job_name, num_tasks):
"""Build list of device prefix names for all_reduce.
Args:
job_name: "worker", "ps" or "localhost".
num_tasks: number of jobs across which device names should be generated.
Returns:
A list of device name prefix strings. Each element spells out the full
host name without adding the device.
e.g. "/job:worker/task:0"
"""
if job_name != "localhost":
return ["/job:%s/task:%d" % (job_name, d) for d in range(0, num_tasks)]
else:
assert num_tasks == 1
return ["/job:%s" % job_name] | [
"def",
"build_all_reduce_device_prefixes",
"(",
"job_name",
",",
"num_tasks",
")",
":",
"if",
"job_name",
"!=",
"\"localhost\"",
":",
"return",
"[",
"\"/job:%s/task:%d\"",
"%",
"(",
"job_name",
",",
"d",
")",
"for",
"d",
"in",
"range",
"(",
"0",
",",
"num_tasks",
")",
"]",
"else",
":",
"assert",
"num_tasks",
"==",
"1",
"return",
"[",
"\"/job:%s\"",
"%",
"job_name",
"]"
] | Build list of device prefix names for all_reduce.
Args:
job_name: "worker", "ps" or "localhost".
num_tasks: number of jobs across which device names should be generated.
Returns:
A list of device name prefix strings. Each element spells out the full
host name without adding the device.
e.g. "/job:worker/task:0" | [
"Build",
"list",
"of",
"device",
"prefix",
"names",
"for",
"all_reduce",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L151-L167 |
24,645 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | group_device_names | def group_device_names(devices, group_size):
"""Group device names into groups of group_size.
Args:
devices: list of strings naming devices.
group_size: int >= 1
Returns:
list of lists of devices, where each inner list is group_size long,
and each device appears at least once in an inner list. If
len(devices) % group_size = 0 then each device will appear
exactly once.
Raises:
ValueError: group_size > len(devices)
"""
num_devices = len(devices)
if group_size > num_devices:
raise ValueError(
"only %d devices, but group_size=%d" % (num_devices, group_size))
num_groups = (
num_devices // group_size + (1 if
(num_devices % group_size != 0) else 0))
groups = [[] for i in range(num_groups)]
for i in range(0, num_groups * group_size):
groups[i % num_groups].append(devices[i % num_devices])
return groups | python | def group_device_names(devices, group_size):
"""Group device names into groups of group_size.
Args:
devices: list of strings naming devices.
group_size: int >= 1
Returns:
list of lists of devices, where each inner list is group_size long,
and each device appears at least once in an inner list. If
len(devices) % group_size = 0 then each device will appear
exactly once.
Raises:
ValueError: group_size > len(devices)
"""
num_devices = len(devices)
if group_size > num_devices:
raise ValueError(
"only %d devices, but group_size=%d" % (num_devices, group_size))
num_groups = (
num_devices // group_size + (1 if
(num_devices % group_size != 0) else 0))
groups = [[] for i in range(num_groups)]
for i in range(0, num_groups * group_size):
groups[i % num_groups].append(devices[i % num_devices])
return groups | [
"def",
"group_device_names",
"(",
"devices",
",",
"group_size",
")",
":",
"num_devices",
"=",
"len",
"(",
"devices",
")",
"if",
"group_size",
">",
"num_devices",
":",
"raise",
"ValueError",
"(",
"\"only %d devices, but group_size=%d\"",
"%",
"(",
"num_devices",
",",
"group_size",
")",
")",
"num_groups",
"=",
"(",
"num_devices",
"//",
"group_size",
"+",
"(",
"1",
"if",
"(",
"num_devices",
"%",
"group_size",
"!=",
"0",
")",
"else",
"0",
")",
")",
"groups",
"=",
"[",
"[",
"]",
"for",
"i",
"in",
"range",
"(",
"num_groups",
")",
"]",
"for",
"i",
"in",
"range",
"(",
"0",
",",
"num_groups",
"*",
"group_size",
")",
":",
"groups",
"[",
"i",
"%",
"num_groups",
"]",
".",
"append",
"(",
"devices",
"[",
"i",
"%",
"num_devices",
"]",
")",
"return",
"groups"
] | Group device names into groups of group_size.
Args:
devices: list of strings naming devices.
group_size: int >= 1
Returns:
list of lists of devices, where each inner list is group_size long,
and each device appears at least once in an inner list. If
len(devices) % group_size = 0 then each device will appear
exactly once.
Raises:
ValueError: group_size > len(devices) | [
"Group",
"device",
"names",
"into",
"groups",
"of",
"group_size",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L170-L196 |
24,646 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | split_grads_by_size | def split_grads_by_size(threshold_size, device_grads):
"""Break gradients into two sets according to tensor size.
Args:
threshold_size: int size cutoff for small vs large tensor.
device_grads: List of lists of (gradient, variable) tuples. The outer
list is over devices. The inner list is over individual gradients.
Returns:
small_grads: Subset of device_grads where shape is <= theshold_size
elements.
large_grads: Subset of device_grads where shape is > threshold_size
elements.
"""
small_grads = []
large_grads = []
for dl in device_grads:
small_dl = []
large_dl = []
for (g, v) in dl:
tensor_size = g.get_shape().num_elements()
if tensor_size <= threshold_size:
small_dl.append([g, v])
else:
large_dl.append([g, v])
if small_dl:
small_grads.append(small_dl)
if large_dl:
large_grads.append(large_dl)
return small_grads, large_grads | python | def split_grads_by_size(threshold_size, device_grads):
"""Break gradients into two sets according to tensor size.
Args:
threshold_size: int size cutoff for small vs large tensor.
device_grads: List of lists of (gradient, variable) tuples. The outer
list is over devices. The inner list is over individual gradients.
Returns:
small_grads: Subset of device_grads where shape is <= theshold_size
elements.
large_grads: Subset of device_grads where shape is > threshold_size
elements.
"""
small_grads = []
large_grads = []
for dl in device_grads:
small_dl = []
large_dl = []
for (g, v) in dl:
tensor_size = g.get_shape().num_elements()
if tensor_size <= threshold_size:
small_dl.append([g, v])
else:
large_dl.append([g, v])
if small_dl:
small_grads.append(small_dl)
if large_dl:
large_grads.append(large_dl)
return small_grads, large_grads | [
"def",
"split_grads_by_size",
"(",
"threshold_size",
",",
"device_grads",
")",
":",
"small_grads",
"=",
"[",
"]",
"large_grads",
"=",
"[",
"]",
"for",
"dl",
"in",
"device_grads",
":",
"small_dl",
"=",
"[",
"]",
"large_dl",
"=",
"[",
"]",
"for",
"(",
"g",
",",
"v",
")",
"in",
"dl",
":",
"tensor_size",
"=",
"g",
".",
"get_shape",
"(",
")",
".",
"num_elements",
"(",
")",
"if",
"tensor_size",
"<=",
"threshold_size",
":",
"small_dl",
".",
"append",
"(",
"[",
"g",
",",
"v",
"]",
")",
"else",
":",
"large_dl",
".",
"append",
"(",
"[",
"g",
",",
"v",
"]",
")",
"if",
"small_dl",
":",
"small_grads",
".",
"append",
"(",
"small_dl",
")",
"if",
"large_dl",
":",
"large_grads",
".",
"append",
"(",
"large_dl",
")",
"return",
"small_grads",
",",
"large_grads"
] | Break gradients into two sets according to tensor size.
Args:
threshold_size: int size cutoff for small vs large tensor.
device_grads: List of lists of (gradient, variable) tuples. The outer
list is over devices. The inner list is over individual gradients.
Returns:
small_grads: Subset of device_grads where shape is <= theshold_size
elements.
large_grads: Subset of device_grads where shape is > threshold_size
elements. | [
"Break",
"gradients",
"into",
"two",
"sets",
"according",
"to",
"tensor",
"size",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L199-L228 |
24,647 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | aggregate_single_gradient | def aggregate_single_gradient(grad_and_vars, use_mean, check_inf_nan):
"""Calculate the average gradient for a shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
grad_and_vars: A list or tuple of (gradient, variable) tuples. Each
(gradient, variable) pair within the outer list represents the gradient
of the variable calculated for a single tower, and the number of pairs
equals the number of towers.
use_mean: if True, mean is taken, else sum of gradients is taken.
check_inf_nan: check grads for nans and infs.
Returns:
The tuple ([(average_gradient, variable),], has_nan_or_inf) where the
gradient has been averaged across all towers. The variable is chosen from
the first tower. The has_nan_or_inf indicates the grads has nan or inf.
"""
grads = [g for g, _ in grad_and_vars]
grad = tf.add_n(grads)
if use_mean and len(grads) > 1:
grad = tf.multiply(grad, 1.0 / len(grads))
v = grad_and_vars[0][1]
if check_inf_nan:
has_nan_or_inf = tf.logical_not(tf.reduce_all(tf.is_finite(grads)))
return (grad, v), has_nan_or_inf
else:
return (grad, v), None | python | def aggregate_single_gradient(grad_and_vars, use_mean, check_inf_nan):
"""Calculate the average gradient for a shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
grad_and_vars: A list or tuple of (gradient, variable) tuples. Each
(gradient, variable) pair within the outer list represents the gradient
of the variable calculated for a single tower, and the number of pairs
equals the number of towers.
use_mean: if True, mean is taken, else sum of gradients is taken.
check_inf_nan: check grads for nans and infs.
Returns:
The tuple ([(average_gradient, variable),], has_nan_or_inf) where the
gradient has been averaged across all towers. The variable is chosen from
the first tower. The has_nan_or_inf indicates the grads has nan or inf.
"""
grads = [g for g, _ in grad_and_vars]
grad = tf.add_n(grads)
if use_mean and len(grads) > 1:
grad = tf.multiply(grad, 1.0 / len(grads))
v = grad_and_vars[0][1]
if check_inf_nan:
has_nan_or_inf = tf.logical_not(tf.reduce_all(tf.is_finite(grads)))
return (grad, v), has_nan_or_inf
else:
return (grad, v), None | [
"def",
"aggregate_single_gradient",
"(",
"grad_and_vars",
",",
"use_mean",
",",
"check_inf_nan",
")",
":",
"grads",
"=",
"[",
"g",
"for",
"g",
",",
"_",
"in",
"grad_and_vars",
"]",
"grad",
"=",
"tf",
".",
"add_n",
"(",
"grads",
")",
"if",
"use_mean",
"and",
"len",
"(",
"grads",
")",
">",
"1",
":",
"grad",
"=",
"tf",
".",
"multiply",
"(",
"grad",
",",
"1.0",
"/",
"len",
"(",
"grads",
")",
")",
"v",
"=",
"grad_and_vars",
"[",
"0",
"]",
"[",
"1",
"]",
"if",
"check_inf_nan",
":",
"has_nan_or_inf",
"=",
"tf",
".",
"logical_not",
"(",
"tf",
".",
"reduce_all",
"(",
"tf",
".",
"is_finite",
"(",
"grads",
")",
")",
")",
"return",
"(",
"grad",
",",
"v",
")",
",",
"has_nan_or_inf",
"else",
":",
"return",
"(",
"grad",
",",
"v",
")",
",",
"None"
] | Calculate the average gradient for a shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
grad_and_vars: A list or tuple of (gradient, variable) tuples. Each
(gradient, variable) pair within the outer list represents the gradient
of the variable calculated for a single tower, and the number of pairs
equals the number of towers.
use_mean: if True, mean is taken, else sum of gradients is taken.
check_inf_nan: check grads for nans and infs.
Returns:
The tuple ([(average_gradient, variable),], has_nan_or_inf) where the
gradient has been averaged across all towers. The variable is chosen from
the first tower. The has_nan_or_inf indicates the grads has nan or inf. | [
"Calculate",
"the",
"average",
"gradient",
"for",
"a",
"shared",
"variable",
"across",
"all",
"towers",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L241-L270 |
24,648 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | aggregate_gradients_using_copy_with_device_selection | def aggregate_gradients_using_copy_with_device_selection(
tower_grads, avail_devices, use_mean=True, check_inf_nan=False):
"""Aggregate gradients, controlling device for the aggregation.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over towers. The inner list is over individual gradients.
use_mean: if True, mean is taken, else sum of gradients is taken.
check_inf_nan: If true, check grads for nans and infs.
Returns:
The tuple ([(average_gradient, variable),], has_nan_or_inf) where the
gradient has been averaged across all towers. The variable is chosen from
the first tower. The has_nan_or_inf indicates the grads has nan or inf.
"""
agg_grads = []
has_nan_or_inf_list = []
for i, single_grads in enumerate(zip(*tower_grads)):
with tf.device(avail_devices[i % len(avail_devices)]):
grad_and_var, has_nan_or_inf = aggregate_single_gradient(
single_grads, use_mean, check_inf_nan)
agg_grads.append(grad_and_var)
has_nan_or_inf_list.append(has_nan_or_inf)
return agg_grads | python | def aggregate_gradients_using_copy_with_device_selection(
tower_grads, avail_devices, use_mean=True, check_inf_nan=False):
"""Aggregate gradients, controlling device for the aggregation.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over towers. The inner list is over individual gradients.
use_mean: if True, mean is taken, else sum of gradients is taken.
check_inf_nan: If true, check grads for nans and infs.
Returns:
The tuple ([(average_gradient, variable),], has_nan_or_inf) where the
gradient has been averaged across all towers. The variable is chosen from
the first tower. The has_nan_or_inf indicates the grads has nan or inf.
"""
agg_grads = []
has_nan_or_inf_list = []
for i, single_grads in enumerate(zip(*tower_grads)):
with tf.device(avail_devices[i % len(avail_devices)]):
grad_and_var, has_nan_or_inf = aggregate_single_gradient(
single_grads, use_mean, check_inf_nan)
agg_grads.append(grad_and_var)
has_nan_or_inf_list.append(has_nan_or_inf)
return agg_grads | [
"def",
"aggregate_gradients_using_copy_with_device_selection",
"(",
"tower_grads",
",",
"avail_devices",
",",
"use_mean",
"=",
"True",
",",
"check_inf_nan",
"=",
"False",
")",
":",
"agg_grads",
"=",
"[",
"]",
"has_nan_or_inf_list",
"=",
"[",
"]",
"for",
"i",
",",
"single_grads",
"in",
"enumerate",
"(",
"zip",
"(",
"*",
"tower_grads",
")",
")",
":",
"with",
"tf",
".",
"device",
"(",
"avail_devices",
"[",
"i",
"%",
"len",
"(",
"avail_devices",
")",
"]",
")",
":",
"grad_and_var",
",",
"has_nan_or_inf",
"=",
"aggregate_single_gradient",
"(",
"single_grads",
",",
"use_mean",
",",
"check_inf_nan",
")",
"agg_grads",
".",
"append",
"(",
"grad_and_var",
")",
"has_nan_or_inf_list",
".",
"append",
"(",
"has_nan_or_inf",
")",
"return",
"agg_grads"
] | Aggregate gradients, controlling device for the aggregation.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over towers. The inner list is over individual gradients.
use_mean: if True, mean is taken, else sum of gradients is taken.
check_inf_nan: If true, check grads for nans and infs.
Returns:
The tuple ([(average_gradient, variable),], has_nan_or_inf) where the
gradient has been averaged across all towers. The variable is chosen from
the first tower. The has_nan_or_inf indicates the grads has nan or inf. | [
"Aggregate",
"gradients",
"controlling",
"device",
"for",
"the",
"aggregation",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L273-L296 |
24,649 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | extract_ranges | def extract_ranges(index_list, range_size_limit=32):
"""Extract consecutive ranges and singles from index_list.
Args:
index_list: List of monotone increasing non-negative integers.
range_size_limit: Largest size range to return. If a larger
consecutive range exists it will be returned as multiple
ranges.
Returns:
ranges, singles where ranges is a list of [first, last] pairs of
consecutive elements in index_list, and singles is all of the
other elements, in original order.
"""
if not index_list:
return [], []
first = index_list[0]
last = first
ranges = []
singles = []
for i in index_list[1:]:
if i == last + 1 and (last - first) <= range_size_limit:
last = i
else:
if last > first:
ranges.append([first, last])
else:
singles.append(first)
first = i
last = i
if last > first:
ranges.append([first, last])
else:
singles.append(first)
return ranges, singles | python | def extract_ranges(index_list, range_size_limit=32):
"""Extract consecutive ranges and singles from index_list.
Args:
index_list: List of monotone increasing non-negative integers.
range_size_limit: Largest size range to return. If a larger
consecutive range exists it will be returned as multiple
ranges.
Returns:
ranges, singles where ranges is a list of [first, last] pairs of
consecutive elements in index_list, and singles is all of the
other elements, in original order.
"""
if not index_list:
return [], []
first = index_list[0]
last = first
ranges = []
singles = []
for i in index_list[1:]:
if i == last + 1 and (last - first) <= range_size_limit:
last = i
else:
if last > first:
ranges.append([first, last])
else:
singles.append(first)
first = i
last = i
if last > first:
ranges.append([first, last])
else:
singles.append(first)
return ranges, singles | [
"def",
"extract_ranges",
"(",
"index_list",
",",
"range_size_limit",
"=",
"32",
")",
":",
"if",
"not",
"index_list",
":",
"return",
"[",
"]",
",",
"[",
"]",
"first",
"=",
"index_list",
"[",
"0",
"]",
"last",
"=",
"first",
"ranges",
"=",
"[",
"]",
"singles",
"=",
"[",
"]",
"for",
"i",
"in",
"index_list",
"[",
"1",
":",
"]",
":",
"if",
"i",
"==",
"last",
"+",
"1",
"and",
"(",
"last",
"-",
"first",
")",
"<=",
"range_size_limit",
":",
"last",
"=",
"i",
"else",
":",
"if",
"last",
">",
"first",
":",
"ranges",
".",
"append",
"(",
"[",
"first",
",",
"last",
"]",
")",
"else",
":",
"singles",
".",
"append",
"(",
"first",
")",
"first",
"=",
"i",
"last",
"=",
"i",
"if",
"last",
">",
"first",
":",
"ranges",
".",
"append",
"(",
"[",
"first",
",",
"last",
"]",
")",
"else",
":",
"singles",
".",
"append",
"(",
"first",
")",
"return",
"ranges",
",",
"singles"
] | Extract consecutive ranges and singles from index_list.
Args:
index_list: List of monotone increasing non-negative integers.
range_size_limit: Largest size range to return. If a larger
consecutive range exists it will be returned as multiple
ranges.
Returns:
ranges, singles where ranges is a list of [first, last] pairs of
consecutive elements in index_list, and singles is all of the
other elements, in original order. | [
"Extract",
"consecutive",
"ranges",
"and",
"singles",
"from",
"index_list",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L448-L482 |
24,650 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | pack_range | def pack_range(key, packing, grad_vars, rng):
"""Form the concatenation of a specified range of gradient tensors.
Args:
key: Value under which to store meta-data in packing that will be used
later to restore the grad_var list structure.
packing: Dict holding data describing packed ranges of small tensors.
grad_vars: List of (grad, var) pairs for one tower.
rng: A pair of integers giving the first, last indices of a consecutive
range of tensors to be packed.
Returns:
A tensor that is the concatenation of all the specified small tensors.
"""
to_pack = grad_vars[rng[0]:rng[1] + 1]
members = []
variables = []
restore_shapes = []
with tf.name_scope("pack"):
for g, v in to_pack:
variables.append(v)
restore_shapes.append(g.shape)
with tf.device(g.device):
members.append(tf.reshape(g, [-1]))
packing[key] = GradPackTuple(
indices=range(rng[0], rng[1] + 1),
vars=variables,
shapes=restore_shapes)
with tf.device(members[0].device):
return tf.concat(members, 0) | python | def pack_range(key, packing, grad_vars, rng):
"""Form the concatenation of a specified range of gradient tensors.
Args:
key: Value under which to store meta-data in packing that will be used
later to restore the grad_var list structure.
packing: Dict holding data describing packed ranges of small tensors.
grad_vars: List of (grad, var) pairs for one tower.
rng: A pair of integers giving the first, last indices of a consecutive
range of tensors to be packed.
Returns:
A tensor that is the concatenation of all the specified small tensors.
"""
to_pack = grad_vars[rng[0]:rng[1] + 1]
members = []
variables = []
restore_shapes = []
with tf.name_scope("pack"):
for g, v in to_pack:
variables.append(v)
restore_shapes.append(g.shape)
with tf.device(g.device):
members.append(tf.reshape(g, [-1]))
packing[key] = GradPackTuple(
indices=range(rng[0], rng[1] + 1),
vars=variables,
shapes=restore_shapes)
with tf.device(members[0].device):
return tf.concat(members, 0) | [
"def",
"pack_range",
"(",
"key",
",",
"packing",
",",
"grad_vars",
",",
"rng",
")",
":",
"to_pack",
"=",
"grad_vars",
"[",
"rng",
"[",
"0",
"]",
":",
"rng",
"[",
"1",
"]",
"+",
"1",
"]",
"members",
"=",
"[",
"]",
"variables",
"=",
"[",
"]",
"restore_shapes",
"=",
"[",
"]",
"with",
"tf",
".",
"name_scope",
"(",
"\"pack\"",
")",
":",
"for",
"g",
",",
"v",
"in",
"to_pack",
":",
"variables",
".",
"append",
"(",
"v",
")",
"restore_shapes",
".",
"append",
"(",
"g",
".",
"shape",
")",
"with",
"tf",
".",
"device",
"(",
"g",
".",
"device",
")",
":",
"members",
".",
"append",
"(",
"tf",
".",
"reshape",
"(",
"g",
",",
"[",
"-",
"1",
"]",
")",
")",
"packing",
"[",
"key",
"]",
"=",
"GradPackTuple",
"(",
"indices",
"=",
"range",
"(",
"rng",
"[",
"0",
"]",
",",
"rng",
"[",
"1",
"]",
"+",
"1",
")",
",",
"vars",
"=",
"variables",
",",
"shapes",
"=",
"restore_shapes",
")",
"with",
"tf",
".",
"device",
"(",
"members",
"[",
"0",
"]",
".",
"device",
")",
":",
"return",
"tf",
".",
"concat",
"(",
"members",
",",
"0",
")"
] | Form the concatenation of a specified range of gradient tensors.
Args:
key: Value under which to store meta-data in packing that will be used
later to restore the grad_var list structure.
packing: Dict holding data describing packed ranges of small tensors.
grad_vars: List of (grad, var) pairs for one tower.
rng: A pair of integers giving the first, last indices of a consecutive
range of tensors to be packed.
Returns:
A tensor that is the concatenation of all the specified small tensors. | [
"Form",
"the",
"concatenation",
"of",
"a",
"specified",
"range",
"of",
"gradient",
"tensors",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L488-L517 |
24,651 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | unpack_grad_tuple | def unpack_grad_tuple(gv, gpt):
"""Unpack a previously packed collection of gradient tensors.
Args:
gv: A (grad, var) pair to be unpacked.
gpt: A GradPackTuple describing the packing operation that produced gv.
Returns:
A list of (grad, var) pairs corresponding to the values that were
originally packed into gv, maybe following subsequent operations like
reduction.
"""
elt_widths = [x.num_elements() for x in gpt.shapes]
with tf.device(gv[0][0].device):
with tf.name_scope("unpack"):
splits = tf.split(gv[0], elt_widths)
unpacked_gv = []
for idx, s in enumerate(splits):
unpacked_gv.append((tf.reshape(s, gpt.shapes[idx]),
gpt.vars[idx]))
return unpacked_gv | python | def unpack_grad_tuple(gv, gpt):
"""Unpack a previously packed collection of gradient tensors.
Args:
gv: A (grad, var) pair to be unpacked.
gpt: A GradPackTuple describing the packing operation that produced gv.
Returns:
A list of (grad, var) pairs corresponding to the values that were
originally packed into gv, maybe following subsequent operations like
reduction.
"""
elt_widths = [x.num_elements() for x in gpt.shapes]
with tf.device(gv[0][0].device):
with tf.name_scope("unpack"):
splits = tf.split(gv[0], elt_widths)
unpacked_gv = []
for idx, s in enumerate(splits):
unpacked_gv.append((tf.reshape(s, gpt.shapes[idx]),
gpt.vars[idx]))
return unpacked_gv | [
"def",
"unpack_grad_tuple",
"(",
"gv",
",",
"gpt",
")",
":",
"elt_widths",
"=",
"[",
"x",
".",
"num_elements",
"(",
")",
"for",
"x",
"in",
"gpt",
".",
"shapes",
"]",
"with",
"tf",
".",
"device",
"(",
"gv",
"[",
"0",
"]",
"[",
"0",
"]",
".",
"device",
")",
":",
"with",
"tf",
".",
"name_scope",
"(",
"\"unpack\"",
")",
":",
"splits",
"=",
"tf",
".",
"split",
"(",
"gv",
"[",
"0",
"]",
",",
"elt_widths",
")",
"unpacked_gv",
"=",
"[",
"]",
"for",
"idx",
",",
"s",
"in",
"enumerate",
"(",
"splits",
")",
":",
"unpacked_gv",
".",
"append",
"(",
"(",
"tf",
".",
"reshape",
"(",
"s",
",",
"gpt",
".",
"shapes",
"[",
"idx",
"]",
")",
",",
"gpt",
".",
"vars",
"[",
"idx",
"]",
")",
")",
"return",
"unpacked_gv"
] | Unpack a previously packed collection of gradient tensors.
Args:
gv: A (grad, var) pair to be unpacked.
gpt: A GradPackTuple describing the packing operation that produced gv.
Returns:
A list of (grad, var) pairs corresponding to the values that were
originally packed into gv, maybe following subsequent operations like
reduction. | [
"Unpack",
"a",
"previously",
"packed",
"collection",
"of",
"gradient",
"tensors",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L520-L540 |
24,652 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | pack_small_tensors | def pack_small_tensors(tower_grads, max_bytes=0):
"""Concatenate gradients together more intelligently.
Does binpacking
Args:
tower_grads: List of lists of (gradient, variable) tuples.
max_bytes: Int giving max number of bytes in a tensor that
may be considered small.
"""
assert max_bytes >= 0
orig_grads = [g for g, _ in tower_grads[0]]
# Check to make sure sizes are accurate; not entirely important
assert all(g.dtype == tf.float32 for g in orig_grads)
sizes = [4 * g.shape.num_elements() for g in orig_grads]
print_stats(sizes)
small_ranges = []
large_indices = []
new_sizes = []
def end_interval(indices, small_ranges, large_indices):
if len(indices) > 1:
small_ranges.insert(0, [indices[0], indices[-1]])
else:
large_indices.insert(0, indices[0])
cur_range = []
cur_size = 0
for i, s in reversed(list(enumerate(sizes))):
if cur_size > max_bytes:
end_interval(cur_range, small_ranges, large_indices)
new_sizes.insert(0, cur_size)
cur_range = []
cur_size = 0
cur_range.insert(0, i)
cur_size += s
end_interval(cur_range, small_ranges, large_indices)
new_sizes.insert(0, cur_size)
print_stats(new_sizes)
num_gv = len(orig_grads)
packing = {}
if len(small_ranges):
new_tower_grads = []
for dev_idx, gv_list in enumerate(tower_grads):
assert len(gv_list) == num_gv, (
"Possible cause: "
"Networks constructed on different workers "
"don't have the same number of variables. "
"If you use tf.GraphKeys or tf.global_variables() "
"with multiple graphs per worker during network "
"construction, you need to use "
"appropriate scopes, see "
"https://github.com/ray-project/ray/issues/3136")
new_gv_list = []
for r in small_ranges:
key = "%d:%d" % (dev_idx, len(new_gv_list))
new_gv_list.append((pack_range(key, packing, gv_list, r),
"packing_var_placeholder"))
for i in large_indices:
new_gv_list.append(gv_list[i])
new_tower_grads.append(new_gv_list)
return new_tower_grads, packing
else:
return tower_grads, None | python | def pack_small_tensors(tower_grads, max_bytes=0):
"""Concatenate gradients together more intelligently.
Does binpacking
Args:
tower_grads: List of lists of (gradient, variable) tuples.
max_bytes: Int giving max number of bytes in a tensor that
may be considered small.
"""
assert max_bytes >= 0
orig_grads = [g for g, _ in tower_grads[0]]
# Check to make sure sizes are accurate; not entirely important
assert all(g.dtype == tf.float32 for g in orig_grads)
sizes = [4 * g.shape.num_elements() for g in orig_grads]
print_stats(sizes)
small_ranges = []
large_indices = []
new_sizes = []
def end_interval(indices, small_ranges, large_indices):
if len(indices) > 1:
small_ranges.insert(0, [indices[0], indices[-1]])
else:
large_indices.insert(0, indices[0])
cur_range = []
cur_size = 0
for i, s in reversed(list(enumerate(sizes))):
if cur_size > max_bytes:
end_interval(cur_range, small_ranges, large_indices)
new_sizes.insert(0, cur_size)
cur_range = []
cur_size = 0
cur_range.insert(0, i)
cur_size += s
end_interval(cur_range, small_ranges, large_indices)
new_sizes.insert(0, cur_size)
print_stats(new_sizes)
num_gv = len(orig_grads)
packing = {}
if len(small_ranges):
new_tower_grads = []
for dev_idx, gv_list in enumerate(tower_grads):
assert len(gv_list) == num_gv, (
"Possible cause: "
"Networks constructed on different workers "
"don't have the same number of variables. "
"If you use tf.GraphKeys or tf.global_variables() "
"with multiple graphs per worker during network "
"construction, you need to use "
"appropriate scopes, see "
"https://github.com/ray-project/ray/issues/3136")
new_gv_list = []
for r in small_ranges:
key = "%d:%d" % (dev_idx, len(new_gv_list))
new_gv_list.append((pack_range(key, packing, gv_list, r),
"packing_var_placeholder"))
for i in large_indices:
new_gv_list.append(gv_list[i])
new_tower_grads.append(new_gv_list)
return new_tower_grads, packing
else:
return tower_grads, None | [
"def",
"pack_small_tensors",
"(",
"tower_grads",
",",
"max_bytes",
"=",
"0",
")",
":",
"assert",
"max_bytes",
">=",
"0",
"orig_grads",
"=",
"[",
"g",
"for",
"g",
",",
"_",
"in",
"tower_grads",
"[",
"0",
"]",
"]",
"# Check to make sure sizes are accurate; not entirely important",
"assert",
"all",
"(",
"g",
".",
"dtype",
"==",
"tf",
".",
"float32",
"for",
"g",
"in",
"orig_grads",
")",
"sizes",
"=",
"[",
"4",
"*",
"g",
".",
"shape",
".",
"num_elements",
"(",
")",
"for",
"g",
"in",
"orig_grads",
"]",
"print_stats",
"(",
"sizes",
")",
"small_ranges",
"=",
"[",
"]",
"large_indices",
"=",
"[",
"]",
"new_sizes",
"=",
"[",
"]",
"def",
"end_interval",
"(",
"indices",
",",
"small_ranges",
",",
"large_indices",
")",
":",
"if",
"len",
"(",
"indices",
")",
">",
"1",
":",
"small_ranges",
".",
"insert",
"(",
"0",
",",
"[",
"indices",
"[",
"0",
"]",
",",
"indices",
"[",
"-",
"1",
"]",
"]",
")",
"else",
":",
"large_indices",
".",
"insert",
"(",
"0",
",",
"indices",
"[",
"0",
"]",
")",
"cur_range",
"=",
"[",
"]",
"cur_size",
"=",
"0",
"for",
"i",
",",
"s",
"in",
"reversed",
"(",
"list",
"(",
"enumerate",
"(",
"sizes",
")",
")",
")",
":",
"if",
"cur_size",
">",
"max_bytes",
":",
"end_interval",
"(",
"cur_range",
",",
"small_ranges",
",",
"large_indices",
")",
"new_sizes",
".",
"insert",
"(",
"0",
",",
"cur_size",
")",
"cur_range",
"=",
"[",
"]",
"cur_size",
"=",
"0",
"cur_range",
".",
"insert",
"(",
"0",
",",
"i",
")",
"cur_size",
"+=",
"s",
"end_interval",
"(",
"cur_range",
",",
"small_ranges",
",",
"large_indices",
")",
"new_sizes",
".",
"insert",
"(",
"0",
",",
"cur_size",
")",
"print_stats",
"(",
"new_sizes",
")",
"num_gv",
"=",
"len",
"(",
"orig_grads",
")",
"packing",
"=",
"{",
"}",
"if",
"len",
"(",
"small_ranges",
")",
":",
"new_tower_grads",
"=",
"[",
"]",
"for",
"dev_idx",
",",
"gv_list",
"in",
"enumerate",
"(",
"tower_grads",
")",
":",
"assert",
"len",
"(",
"gv_list",
")",
"==",
"num_gv",
",",
"(",
"\"Possible cause: \"",
"\"Networks constructed on different workers \"",
"\"don't have the same number of variables. \"",
"\"If you use tf.GraphKeys or tf.global_variables() \"",
"\"with multiple graphs per worker during network \"",
"\"construction, you need to use \"",
"\"appropriate scopes, see \"",
"\"https://github.com/ray-project/ray/issues/3136\"",
")",
"new_gv_list",
"=",
"[",
"]",
"for",
"r",
"in",
"small_ranges",
":",
"key",
"=",
"\"%d:%d\"",
"%",
"(",
"dev_idx",
",",
"len",
"(",
"new_gv_list",
")",
")",
"new_gv_list",
".",
"append",
"(",
"(",
"pack_range",
"(",
"key",
",",
"packing",
",",
"gv_list",
",",
"r",
")",
",",
"\"packing_var_placeholder\"",
")",
")",
"for",
"i",
"in",
"large_indices",
":",
"new_gv_list",
".",
"append",
"(",
"gv_list",
"[",
"i",
"]",
")",
"new_tower_grads",
".",
"append",
"(",
"new_gv_list",
")",
"return",
"new_tower_grads",
",",
"packing",
"else",
":",
"return",
"tower_grads",
",",
"None"
] | Concatenate gradients together more intelligently.
Does binpacking
Args:
tower_grads: List of lists of (gradient, variable) tuples.
max_bytes: Int giving max number of bytes in a tensor that
may be considered small. | [
"Concatenate",
"gradients",
"together",
"more",
"intelligently",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L543-L606 |
24,653 | ray-project/ray | python/ray/experimental/sgd/modified_allreduce.py | unpack_small_tensors | def unpack_small_tensors(tower_grads, packing):
"""Undo the structure alterations to tower_grads done by pack_small_tensors.
Args:
tower_grads: List of List of (grad, var) tuples.
packing: A dict generated by pack_small_tensors describing the changes
it made to tower_grads.
Returns:
new_tower_grads: identical to tower_grads except that concatentations
of small tensors have been split apart and returned to their original
positions, paired with their original variables.
"""
if not packing:
return tower_grads
new_tower_grads = []
num_devices = len(tower_grads)
num_packed = len(packing.keys()) // num_devices
for dev_idx, gv_list in enumerate(tower_grads):
new_gv_list = gv_list[num_packed:]
for i in xrange(0, num_packed):
k = "%d:%d" % (dev_idx, i)
gpt = packing[k]
gv = unpack_grad_tuple(gv_list[i], gpt)
for gi, idx in enumerate(gpt.indices):
assert idx == gpt.indices[gi]
new_gv_list.insert(idx, gv[gi])
new_tower_grads.append(new_gv_list)
return new_tower_grads | python | def unpack_small_tensors(tower_grads, packing):
"""Undo the structure alterations to tower_grads done by pack_small_tensors.
Args:
tower_grads: List of List of (grad, var) tuples.
packing: A dict generated by pack_small_tensors describing the changes
it made to tower_grads.
Returns:
new_tower_grads: identical to tower_grads except that concatentations
of small tensors have been split apart and returned to their original
positions, paired with their original variables.
"""
if not packing:
return tower_grads
new_tower_grads = []
num_devices = len(tower_grads)
num_packed = len(packing.keys()) // num_devices
for dev_idx, gv_list in enumerate(tower_grads):
new_gv_list = gv_list[num_packed:]
for i in xrange(0, num_packed):
k = "%d:%d" % (dev_idx, i)
gpt = packing[k]
gv = unpack_grad_tuple(gv_list[i], gpt)
for gi, idx in enumerate(gpt.indices):
assert idx == gpt.indices[gi]
new_gv_list.insert(idx, gv[gi])
new_tower_grads.append(new_gv_list)
return new_tower_grads | [
"def",
"unpack_small_tensors",
"(",
"tower_grads",
",",
"packing",
")",
":",
"if",
"not",
"packing",
":",
"return",
"tower_grads",
"new_tower_grads",
"=",
"[",
"]",
"num_devices",
"=",
"len",
"(",
"tower_grads",
")",
"num_packed",
"=",
"len",
"(",
"packing",
".",
"keys",
"(",
")",
")",
"//",
"num_devices",
"for",
"dev_idx",
",",
"gv_list",
"in",
"enumerate",
"(",
"tower_grads",
")",
":",
"new_gv_list",
"=",
"gv_list",
"[",
"num_packed",
":",
"]",
"for",
"i",
"in",
"xrange",
"(",
"0",
",",
"num_packed",
")",
":",
"k",
"=",
"\"%d:%d\"",
"%",
"(",
"dev_idx",
",",
"i",
")",
"gpt",
"=",
"packing",
"[",
"k",
"]",
"gv",
"=",
"unpack_grad_tuple",
"(",
"gv_list",
"[",
"i",
"]",
",",
"gpt",
")",
"for",
"gi",
",",
"idx",
"in",
"enumerate",
"(",
"gpt",
".",
"indices",
")",
":",
"assert",
"idx",
"==",
"gpt",
".",
"indices",
"[",
"gi",
"]",
"new_gv_list",
".",
"insert",
"(",
"idx",
",",
"gv",
"[",
"gi",
"]",
")",
"new_tower_grads",
".",
"append",
"(",
"new_gv_list",
")",
"return",
"new_tower_grads"
] | Undo the structure alterations to tower_grads done by pack_small_tensors.
Args:
tower_grads: List of List of (grad, var) tuples.
packing: A dict generated by pack_small_tensors describing the changes
it made to tower_grads.
Returns:
new_tower_grads: identical to tower_grads except that concatentations
of small tensors have been split apart and returned to their original
positions, paired with their original variables. | [
"Undo",
"the",
"structure",
"alterations",
"to",
"tower_grads",
"done",
"by",
"pack_small_tensors",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L609-L637 |
24,654 | ray-project/ray | python/ray/tune/logger.py | CSVLogger._init | def _init(self):
"""CSV outputted with Headers as first set of results."""
# Note that we assume params.json was already created by JsonLogger
progress_file = os.path.join(self.logdir, "progress.csv")
self._continuing = os.path.exists(progress_file)
self._file = open(progress_file, "a")
self._csv_out = None | python | def _init(self):
"""CSV outputted with Headers as first set of results."""
# Note that we assume params.json was already created by JsonLogger
progress_file = os.path.join(self.logdir, "progress.csv")
self._continuing = os.path.exists(progress_file)
self._file = open(progress_file, "a")
self._csv_out = None | [
"def",
"_init",
"(",
"self",
")",
":",
"# Note that we assume params.json was already created by JsonLogger",
"progress_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"logdir",
",",
"\"progress.csv\"",
")",
"self",
".",
"_continuing",
"=",
"os",
".",
"path",
".",
"exists",
"(",
"progress_file",
")",
"self",
".",
"_file",
"=",
"open",
"(",
"progress_file",
",",
"\"a\"",
")",
"self",
".",
"_csv_out",
"=",
"None"
] | CSV outputted with Headers as first set of results. | [
"CSV",
"outputted",
"with",
"Headers",
"as",
"first",
"set",
"of",
"results",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/logger.py#L156-L162 |
24,655 | ray-project/ray | python/ray/tune/logger.py | UnifiedLogger.sync_results_to_new_location | def sync_results_to_new_location(self, worker_ip):
"""Sends the current log directory to the remote node.
Syncing will not occur if the cluster is not started
with the Ray autoscaler.
"""
if worker_ip != self._log_syncer.worker_ip:
self._log_syncer.set_worker_ip(worker_ip)
self._log_syncer.sync_to_worker_if_possible() | python | def sync_results_to_new_location(self, worker_ip):
"""Sends the current log directory to the remote node.
Syncing will not occur if the cluster is not started
with the Ray autoscaler.
"""
if worker_ip != self._log_syncer.worker_ip:
self._log_syncer.set_worker_ip(worker_ip)
self._log_syncer.sync_to_worker_if_possible() | [
"def",
"sync_results_to_new_location",
"(",
"self",
",",
"worker_ip",
")",
":",
"if",
"worker_ip",
"!=",
"self",
".",
"_log_syncer",
".",
"worker_ip",
":",
"self",
".",
"_log_syncer",
".",
"set_worker_ip",
"(",
"worker_ip",
")",
"self",
".",
"_log_syncer",
".",
"sync_to_worker_if_possible",
"(",
")"
] | Sends the current log directory to the remote node.
Syncing will not occur if the cluster is not started
with the Ray autoscaler. | [
"Sends",
"the",
"current",
"log",
"directory",
"to",
"the",
"remote",
"node",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/logger.py#L241-L249 |
24,656 | ray-project/ray | python/ray/tune/automl/search_policy.py | deep_insert | def deep_insert(path_list, value, config):
"""Inserts value into config by path, generating intermediate dictionaries.
Example:
>>> deep_insert(path.split("."), value, {})
"""
if len(path_list) > 1:
inside_config = config.setdefault(path_list[0], {})
deep_insert(path_list[1:], value, inside_config)
else:
config[path_list[0]] = value | python | def deep_insert(path_list, value, config):
"""Inserts value into config by path, generating intermediate dictionaries.
Example:
>>> deep_insert(path.split("."), value, {})
"""
if len(path_list) > 1:
inside_config = config.setdefault(path_list[0], {})
deep_insert(path_list[1:], value, inside_config)
else:
config[path_list[0]] = value | [
"def",
"deep_insert",
"(",
"path_list",
",",
"value",
",",
"config",
")",
":",
"if",
"len",
"(",
"path_list",
")",
">",
"1",
":",
"inside_config",
"=",
"config",
".",
"setdefault",
"(",
"path_list",
"[",
"0",
"]",
",",
"{",
"}",
")",
"deep_insert",
"(",
"path_list",
"[",
"1",
":",
"]",
",",
"value",
",",
"inside_config",
")",
"else",
":",
"config",
"[",
"path_list",
"[",
"0",
"]",
"]",
"=",
"value"
] | Inserts value into config by path, generating intermediate dictionaries.
Example:
>>> deep_insert(path.split("."), value, {}) | [
"Inserts",
"value",
"into",
"config",
"by",
"path",
"generating",
"intermediate",
"dictionaries",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/automl/search_policy.py#L18-L28 |
24,657 | ray-project/ray | python/ray/function_manager.py | FunctionDescriptor.from_bytes_list | def from_bytes_list(cls, function_descriptor_list):
"""Create a FunctionDescriptor instance from list of bytes.
This function is used to create the function descriptor from
backend data.
Args:
cls: Current class which is required argument for classmethod.
function_descriptor_list: list of bytes to represent the
function descriptor.
Returns:
The FunctionDescriptor instance created from the bytes list.
"""
assert isinstance(function_descriptor_list, list)
if len(function_descriptor_list) == 0:
# This is a function descriptor of driver task.
return FunctionDescriptor.for_driver_task()
elif (len(function_descriptor_list) == 3
or len(function_descriptor_list) == 4):
module_name = ensure_str(function_descriptor_list[0])
class_name = ensure_str(function_descriptor_list[1])
function_name = ensure_str(function_descriptor_list[2])
if len(function_descriptor_list) == 4:
return cls(module_name, function_name, class_name,
function_descriptor_list[3])
else:
return cls(module_name, function_name, class_name)
else:
raise Exception(
"Invalid input for FunctionDescriptor.from_bytes_list") | python | def from_bytes_list(cls, function_descriptor_list):
"""Create a FunctionDescriptor instance from list of bytes.
This function is used to create the function descriptor from
backend data.
Args:
cls: Current class which is required argument for classmethod.
function_descriptor_list: list of bytes to represent the
function descriptor.
Returns:
The FunctionDescriptor instance created from the bytes list.
"""
assert isinstance(function_descriptor_list, list)
if len(function_descriptor_list) == 0:
# This is a function descriptor of driver task.
return FunctionDescriptor.for_driver_task()
elif (len(function_descriptor_list) == 3
or len(function_descriptor_list) == 4):
module_name = ensure_str(function_descriptor_list[0])
class_name = ensure_str(function_descriptor_list[1])
function_name = ensure_str(function_descriptor_list[2])
if len(function_descriptor_list) == 4:
return cls(module_name, function_name, class_name,
function_descriptor_list[3])
else:
return cls(module_name, function_name, class_name)
else:
raise Exception(
"Invalid input for FunctionDescriptor.from_bytes_list") | [
"def",
"from_bytes_list",
"(",
"cls",
",",
"function_descriptor_list",
")",
":",
"assert",
"isinstance",
"(",
"function_descriptor_list",
",",
"list",
")",
"if",
"len",
"(",
"function_descriptor_list",
")",
"==",
"0",
":",
"# This is a function descriptor of driver task.",
"return",
"FunctionDescriptor",
".",
"for_driver_task",
"(",
")",
"elif",
"(",
"len",
"(",
"function_descriptor_list",
")",
"==",
"3",
"or",
"len",
"(",
"function_descriptor_list",
")",
"==",
"4",
")",
":",
"module_name",
"=",
"ensure_str",
"(",
"function_descriptor_list",
"[",
"0",
"]",
")",
"class_name",
"=",
"ensure_str",
"(",
"function_descriptor_list",
"[",
"1",
"]",
")",
"function_name",
"=",
"ensure_str",
"(",
"function_descriptor_list",
"[",
"2",
"]",
")",
"if",
"len",
"(",
"function_descriptor_list",
")",
"==",
"4",
":",
"return",
"cls",
"(",
"module_name",
",",
"function_name",
",",
"class_name",
",",
"function_descriptor_list",
"[",
"3",
"]",
")",
"else",
":",
"return",
"cls",
"(",
"module_name",
",",
"function_name",
",",
"class_name",
")",
"else",
":",
"raise",
"Exception",
"(",
"\"Invalid input for FunctionDescriptor.from_bytes_list\"",
")"
] | Create a FunctionDescriptor instance from list of bytes.
This function is used to create the function descriptor from
backend data.
Args:
cls: Current class which is required argument for classmethod.
function_descriptor_list: list of bytes to represent the
function descriptor.
Returns:
The FunctionDescriptor instance created from the bytes list. | [
"Create",
"a",
"FunctionDescriptor",
"instance",
"from",
"list",
"of",
"bytes",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L73-L103 |
24,658 | ray-project/ray | python/ray/function_manager.py | FunctionDescriptor.from_function | def from_function(cls, function):
"""Create a FunctionDescriptor from a function instance.
This function is used to create the function descriptor from
a python function. If a function is a class function, it should
not be used by this function.
Args:
cls: Current class which is required argument for classmethod.
function: the python function used to create the function
descriptor.
Returns:
The FunctionDescriptor instance created according to the function.
"""
module_name = function.__module__
function_name = function.__name__
class_name = ""
function_source_hasher = hashlib.sha1()
try:
# If we are running a script or are in IPython, include the source
# code in the hash.
source = inspect.getsource(function)
if sys.version_info[0] >= 3:
source = source.encode()
function_source_hasher.update(source)
function_source_hash = function_source_hasher.digest()
except (IOError, OSError, TypeError):
# Source code may not be available:
# e.g. Cython or Python interpreter.
function_source_hash = b""
return cls(module_name, function_name, class_name,
function_source_hash) | python | def from_function(cls, function):
"""Create a FunctionDescriptor from a function instance.
This function is used to create the function descriptor from
a python function. If a function is a class function, it should
not be used by this function.
Args:
cls: Current class which is required argument for classmethod.
function: the python function used to create the function
descriptor.
Returns:
The FunctionDescriptor instance created according to the function.
"""
module_name = function.__module__
function_name = function.__name__
class_name = ""
function_source_hasher = hashlib.sha1()
try:
# If we are running a script or are in IPython, include the source
# code in the hash.
source = inspect.getsource(function)
if sys.version_info[0] >= 3:
source = source.encode()
function_source_hasher.update(source)
function_source_hash = function_source_hasher.digest()
except (IOError, OSError, TypeError):
# Source code may not be available:
# e.g. Cython or Python interpreter.
function_source_hash = b""
return cls(module_name, function_name, class_name,
function_source_hash) | [
"def",
"from_function",
"(",
"cls",
",",
"function",
")",
":",
"module_name",
"=",
"function",
".",
"__module__",
"function_name",
"=",
"function",
".",
"__name__",
"class_name",
"=",
"\"\"",
"function_source_hasher",
"=",
"hashlib",
".",
"sha1",
"(",
")",
"try",
":",
"# If we are running a script or are in IPython, include the source",
"# code in the hash.",
"source",
"=",
"inspect",
".",
"getsource",
"(",
"function",
")",
"if",
"sys",
".",
"version_info",
"[",
"0",
"]",
">=",
"3",
":",
"source",
"=",
"source",
".",
"encode",
"(",
")",
"function_source_hasher",
".",
"update",
"(",
"source",
")",
"function_source_hash",
"=",
"function_source_hasher",
".",
"digest",
"(",
")",
"except",
"(",
"IOError",
",",
"OSError",
",",
"TypeError",
")",
":",
"# Source code may not be available:",
"# e.g. Cython or Python interpreter.",
"function_source_hash",
"=",
"b\"\"",
"return",
"cls",
"(",
"module_name",
",",
"function_name",
",",
"class_name",
",",
"function_source_hash",
")"
] | Create a FunctionDescriptor from a function instance.
This function is used to create the function descriptor from
a python function. If a function is a class function, it should
not be used by this function.
Args:
cls: Current class which is required argument for classmethod.
function: the python function used to create the function
descriptor.
Returns:
The FunctionDescriptor instance created according to the function. | [
"Create",
"a",
"FunctionDescriptor",
"from",
"a",
"function",
"instance",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L106-L140 |
24,659 | ray-project/ray | python/ray/function_manager.py | FunctionDescriptor.from_class | def from_class(cls, target_class):
"""Create a FunctionDescriptor from a class.
Args:
cls: Current class which is required argument for classmethod.
target_class: the python class used to create the function
descriptor.
Returns:
The FunctionDescriptor instance created according to the class.
"""
module_name = target_class.__module__
class_name = target_class.__name__
return cls(module_name, "__init__", class_name) | python | def from_class(cls, target_class):
"""Create a FunctionDescriptor from a class.
Args:
cls: Current class which is required argument for classmethod.
target_class: the python class used to create the function
descriptor.
Returns:
The FunctionDescriptor instance created according to the class.
"""
module_name = target_class.__module__
class_name = target_class.__name__
return cls(module_name, "__init__", class_name) | [
"def",
"from_class",
"(",
"cls",
",",
"target_class",
")",
":",
"module_name",
"=",
"target_class",
".",
"__module__",
"class_name",
"=",
"target_class",
".",
"__name__",
"return",
"cls",
"(",
"module_name",
",",
"\"__init__\"",
",",
"class_name",
")"
] | Create a FunctionDescriptor from a class.
Args:
cls: Current class which is required argument for classmethod.
target_class: the python class used to create the function
descriptor.
Returns:
The FunctionDescriptor instance created according to the class. | [
"Create",
"a",
"FunctionDescriptor",
"from",
"a",
"class",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L143-L156 |
24,660 | ray-project/ray | python/ray/function_manager.py | FunctionDescriptor.is_for_driver_task | def is_for_driver_task(self):
"""See whether this function descriptor is for a driver or not.
Returns:
True if this function descriptor is for driver tasks.
"""
return all(
len(x) == 0
for x in [self.module_name, self.class_name, self.function_name]) | python | def is_for_driver_task(self):
"""See whether this function descriptor is for a driver or not.
Returns:
True if this function descriptor is for driver tasks.
"""
return all(
len(x) == 0
for x in [self.module_name, self.class_name, self.function_name]) | [
"def",
"is_for_driver_task",
"(",
"self",
")",
":",
"return",
"all",
"(",
"len",
"(",
"x",
")",
"==",
"0",
"for",
"x",
"in",
"[",
"self",
".",
"module_name",
",",
"self",
".",
"class_name",
",",
"self",
".",
"function_name",
"]",
")"
] | See whether this function descriptor is for a driver or not.
Returns:
True if this function descriptor is for driver tasks. | [
"See",
"whether",
"this",
"function",
"descriptor",
"is",
"for",
"a",
"driver",
"or",
"not",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L164-L172 |
24,661 | ray-project/ray | python/ray/function_manager.py | FunctionDescriptor._get_function_id | def _get_function_id(self):
"""Calculate the function id of current function descriptor.
This function id is calculated from all the fields of function
descriptor.
Returns:
ray.ObjectID to represent the function descriptor.
"""
if self.is_for_driver_task:
return ray.FunctionID.nil()
function_id_hash = hashlib.sha1()
# Include the function module and name in the hash.
function_id_hash.update(self.module_name.encode("ascii"))
function_id_hash.update(self.function_name.encode("ascii"))
function_id_hash.update(self.class_name.encode("ascii"))
function_id_hash.update(self._function_source_hash)
# Compute the function ID.
function_id = function_id_hash.digest()
return ray.FunctionID(function_id) | python | def _get_function_id(self):
"""Calculate the function id of current function descriptor.
This function id is calculated from all the fields of function
descriptor.
Returns:
ray.ObjectID to represent the function descriptor.
"""
if self.is_for_driver_task:
return ray.FunctionID.nil()
function_id_hash = hashlib.sha1()
# Include the function module and name in the hash.
function_id_hash.update(self.module_name.encode("ascii"))
function_id_hash.update(self.function_name.encode("ascii"))
function_id_hash.update(self.class_name.encode("ascii"))
function_id_hash.update(self._function_source_hash)
# Compute the function ID.
function_id = function_id_hash.digest()
return ray.FunctionID(function_id) | [
"def",
"_get_function_id",
"(",
"self",
")",
":",
"if",
"self",
".",
"is_for_driver_task",
":",
"return",
"ray",
".",
"FunctionID",
".",
"nil",
"(",
")",
"function_id_hash",
"=",
"hashlib",
".",
"sha1",
"(",
")",
"# Include the function module and name in the hash.",
"function_id_hash",
".",
"update",
"(",
"self",
".",
"module_name",
".",
"encode",
"(",
"\"ascii\"",
")",
")",
"function_id_hash",
".",
"update",
"(",
"self",
".",
"function_name",
".",
"encode",
"(",
"\"ascii\"",
")",
")",
"function_id_hash",
".",
"update",
"(",
"self",
".",
"class_name",
".",
"encode",
"(",
"\"ascii\"",
")",
")",
"function_id_hash",
".",
"update",
"(",
"self",
".",
"_function_source_hash",
")",
"# Compute the function ID.",
"function_id",
"=",
"function_id_hash",
".",
"digest",
"(",
")",
"return",
"ray",
".",
"FunctionID",
"(",
"function_id",
")"
] | Calculate the function id of current function descriptor.
This function id is calculated from all the fields of function
descriptor.
Returns:
ray.ObjectID to represent the function descriptor. | [
"Calculate",
"the",
"function",
"id",
"of",
"current",
"function",
"descriptor",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L221-L240 |
24,662 | ray-project/ray | python/ray/function_manager.py | FunctionDescriptor.get_function_descriptor_list | def get_function_descriptor_list(self):
"""Return a list of bytes representing the function descriptor.
This function is used to pass this function descriptor to backend.
Returns:
A list of bytes.
"""
descriptor_list = []
if self.is_for_driver_task:
# Driver task returns an empty list.
return descriptor_list
else:
descriptor_list.append(self.module_name.encode("ascii"))
descriptor_list.append(self.class_name.encode("ascii"))
descriptor_list.append(self.function_name.encode("ascii"))
if len(self._function_source_hash) != 0:
descriptor_list.append(self._function_source_hash)
return descriptor_list | python | def get_function_descriptor_list(self):
"""Return a list of bytes representing the function descriptor.
This function is used to pass this function descriptor to backend.
Returns:
A list of bytes.
"""
descriptor_list = []
if self.is_for_driver_task:
# Driver task returns an empty list.
return descriptor_list
else:
descriptor_list.append(self.module_name.encode("ascii"))
descriptor_list.append(self.class_name.encode("ascii"))
descriptor_list.append(self.function_name.encode("ascii"))
if len(self._function_source_hash) != 0:
descriptor_list.append(self._function_source_hash)
return descriptor_list | [
"def",
"get_function_descriptor_list",
"(",
"self",
")",
":",
"descriptor_list",
"=",
"[",
"]",
"if",
"self",
".",
"is_for_driver_task",
":",
"# Driver task returns an empty list.",
"return",
"descriptor_list",
"else",
":",
"descriptor_list",
".",
"append",
"(",
"self",
".",
"module_name",
".",
"encode",
"(",
"\"ascii\"",
")",
")",
"descriptor_list",
".",
"append",
"(",
"self",
".",
"class_name",
".",
"encode",
"(",
"\"ascii\"",
")",
")",
"descriptor_list",
".",
"append",
"(",
"self",
".",
"function_name",
".",
"encode",
"(",
"\"ascii\"",
")",
")",
"if",
"len",
"(",
"self",
".",
"_function_source_hash",
")",
"!=",
"0",
":",
"descriptor_list",
".",
"append",
"(",
"self",
".",
"_function_source_hash",
")",
"return",
"descriptor_list"
] | Return a list of bytes representing the function descriptor.
This function is used to pass this function descriptor to backend.
Returns:
A list of bytes. | [
"Return",
"a",
"list",
"of",
"bytes",
"representing",
"the",
"function",
"descriptor",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L242-L260 |
24,663 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager.export_cached | def export_cached(self):
"""Export cached remote functions
Note: this should be called only once when worker is connected.
"""
for remote_function in self._functions_to_export:
self._do_export(remote_function)
self._functions_to_export = None
for info in self._actors_to_export:
(key, actor_class_info) = info
self._publish_actor_class_to_key(key, actor_class_info) | python | def export_cached(self):
"""Export cached remote functions
Note: this should be called only once when worker is connected.
"""
for remote_function in self._functions_to_export:
self._do_export(remote_function)
self._functions_to_export = None
for info in self._actors_to_export:
(key, actor_class_info) = info
self._publish_actor_class_to_key(key, actor_class_info) | [
"def",
"export_cached",
"(",
"self",
")",
":",
"for",
"remote_function",
"in",
"self",
".",
"_functions_to_export",
":",
"self",
".",
"_do_export",
"(",
"remote_function",
")",
"self",
".",
"_functions_to_export",
"=",
"None",
"for",
"info",
"in",
"self",
".",
"_actors_to_export",
":",
"(",
"key",
",",
"actor_class_info",
")",
"=",
"info",
"self",
".",
"_publish_actor_class_to_key",
"(",
"key",
",",
"actor_class_info",
")"
] | Export cached remote functions
Note: this should be called only once when worker is connected. | [
"Export",
"cached",
"remote",
"functions"
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L318-L328 |
24,664 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager.export | def export(self, remote_function):
"""Export a remote function.
Args:
remote_function: the RemoteFunction object.
"""
if self._worker.mode is None:
# If the worker isn't connected, cache the function
# and export it later.
self._functions_to_export.append(remote_function)
return
if self._worker.mode != ray.worker.SCRIPT_MODE:
# Don't need to export if the worker is not a driver.
return
self._do_export(remote_function) | python | def export(self, remote_function):
"""Export a remote function.
Args:
remote_function: the RemoteFunction object.
"""
if self._worker.mode is None:
# If the worker isn't connected, cache the function
# and export it later.
self._functions_to_export.append(remote_function)
return
if self._worker.mode != ray.worker.SCRIPT_MODE:
# Don't need to export if the worker is not a driver.
return
self._do_export(remote_function) | [
"def",
"export",
"(",
"self",
",",
"remote_function",
")",
":",
"if",
"self",
".",
"_worker",
".",
"mode",
"is",
"None",
":",
"# If the worker isn't connected, cache the function",
"# and export it later.",
"self",
".",
"_functions_to_export",
".",
"append",
"(",
"remote_function",
")",
"return",
"if",
"self",
".",
"_worker",
".",
"mode",
"!=",
"ray",
".",
"worker",
".",
"SCRIPT_MODE",
":",
"# Don't need to export if the worker is not a driver.",
"return",
"self",
".",
"_do_export",
"(",
"remote_function",
")"
] | Export a remote function.
Args:
remote_function: the RemoteFunction object. | [
"Export",
"a",
"remote",
"function",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L334-L348 |
24,665 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager._do_export | def _do_export(self, remote_function):
"""Pickle a remote function and export it to redis.
Args:
remote_function: the RemoteFunction object.
"""
if self._worker.load_code_from_local:
return
# Work around limitations of Python pickling.
function = remote_function._function
function_name_global_valid = function.__name__ in function.__globals__
function_name_global_value = function.__globals__.get(
function.__name__)
# Allow the function to reference itself as a global variable
if not is_cython(function):
function.__globals__[function.__name__] = remote_function
try:
pickled_function = pickle.dumps(function)
finally:
# Undo our changes
if function_name_global_valid:
function.__globals__[function.__name__] = (
function_name_global_value)
else:
del function.__globals__[function.__name__]
check_oversized_pickle(pickled_function,
remote_function._function_name,
"remote function", self._worker)
key = (b"RemoteFunction:" + self._worker.task_driver_id.binary() + b":"
+ remote_function._function_descriptor.function_id.binary())
self._worker.redis_client.hmset(
key, {
"driver_id": self._worker.task_driver_id.binary(),
"function_id": remote_function._function_descriptor.
function_id.binary(),
"name": remote_function._function_name,
"module": function.__module__,
"function": pickled_function,
"max_calls": remote_function._max_calls
})
self._worker.redis_client.rpush("Exports", key) | python | def _do_export(self, remote_function):
"""Pickle a remote function and export it to redis.
Args:
remote_function: the RemoteFunction object.
"""
if self._worker.load_code_from_local:
return
# Work around limitations of Python pickling.
function = remote_function._function
function_name_global_valid = function.__name__ in function.__globals__
function_name_global_value = function.__globals__.get(
function.__name__)
# Allow the function to reference itself as a global variable
if not is_cython(function):
function.__globals__[function.__name__] = remote_function
try:
pickled_function = pickle.dumps(function)
finally:
# Undo our changes
if function_name_global_valid:
function.__globals__[function.__name__] = (
function_name_global_value)
else:
del function.__globals__[function.__name__]
check_oversized_pickle(pickled_function,
remote_function._function_name,
"remote function", self._worker)
key = (b"RemoteFunction:" + self._worker.task_driver_id.binary() + b":"
+ remote_function._function_descriptor.function_id.binary())
self._worker.redis_client.hmset(
key, {
"driver_id": self._worker.task_driver_id.binary(),
"function_id": remote_function._function_descriptor.
function_id.binary(),
"name": remote_function._function_name,
"module": function.__module__,
"function": pickled_function,
"max_calls": remote_function._max_calls
})
self._worker.redis_client.rpush("Exports", key) | [
"def",
"_do_export",
"(",
"self",
",",
"remote_function",
")",
":",
"if",
"self",
".",
"_worker",
".",
"load_code_from_local",
":",
"return",
"# Work around limitations of Python pickling.",
"function",
"=",
"remote_function",
".",
"_function",
"function_name_global_valid",
"=",
"function",
".",
"__name__",
"in",
"function",
".",
"__globals__",
"function_name_global_value",
"=",
"function",
".",
"__globals__",
".",
"get",
"(",
"function",
".",
"__name__",
")",
"# Allow the function to reference itself as a global variable",
"if",
"not",
"is_cython",
"(",
"function",
")",
":",
"function",
".",
"__globals__",
"[",
"function",
".",
"__name__",
"]",
"=",
"remote_function",
"try",
":",
"pickled_function",
"=",
"pickle",
".",
"dumps",
"(",
"function",
")",
"finally",
":",
"# Undo our changes",
"if",
"function_name_global_valid",
":",
"function",
".",
"__globals__",
"[",
"function",
".",
"__name__",
"]",
"=",
"(",
"function_name_global_value",
")",
"else",
":",
"del",
"function",
".",
"__globals__",
"[",
"function",
".",
"__name__",
"]",
"check_oversized_pickle",
"(",
"pickled_function",
",",
"remote_function",
".",
"_function_name",
",",
"\"remote function\"",
",",
"self",
".",
"_worker",
")",
"key",
"=",
"(",
"b\"RemoteFunction:\"",
"+",
"self",
".",
"_worker",
".",
"task_driver_id",
".",
"binary",
"(",
")",
"+",
"b\":\"",
"+",
"remote_function",
".",
"_function_descriptor",
".",
"function_id",
".",
"binary",
"(",
")",
")",
"self",
".",
"_worker",
".",
"redis_client",
".",
"hmset",
"(",
"key",
",",
"{",
"\"driver_id\"",
":",
"self",
".",
"_worker",
".",
"task_driver_id",
".",
"binary",
"(",
")",
",",
"\"function_id\"",
":",
"remote_function",
".",
"_function_descriptor",
".",
"function_id",
".",
"binary",
"(",
")",
",",
"\"name\"",
":",
"remote_function",
".",
"_function_name",
",",
"\"module\"",
":",
"function",
".",
"__module__",
",",
"\"function\"",
":",
"pickled_function",
",",
"\"max_calls\"",
":",
"remote_function",
".",
"_max_calls",
"}",
")",
"self",
".",
"_worker",
".",
"redis_client",
".",
"rpush",
"(",
"\"Exports\"",
",",
"key",
")"
] | Pickle a remote function and export it to redis.
Args:
remote_function: the RemoteFunction object. | [
"Pickle",
"a",
"remote",
"function",
"and",
"export",
"it",
"to",
"redis",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L350-L391 |
24,666 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager.fetch_and_register_remote_function | def fetch_and_register_remote_function(self, key):
"""Import a remote function."""
(driver_id_str, function_id_str, function_name, serialized_function,
num_return_vals, module, resources,
max_calls) = self._worker.redis_client.hmget(key, [
"driver_id", "function_id", "name", "function", "num_return_vals",
"module", "resources", "max_calls"
])
function_id = ray.FunctionID(function_id_str)
driver_id = ray.DriverID(driver_id_str)
function_name = decode(function_name)
max_calls = int(max_calls)
module = decode(module)
# This is a placeholder in case the function can't be unpickled. This
# will be overwritten if the function is successfully registered.
def f():
raise Exception("This function was not imported properly.")
# This function is called by ImportThread. This operation needs to be
# atomic. Otherwise, there is race condition. Another thread may use
# the temporary function above before the real function is ready.
with self.lock:
self._function_execution_info[driver_id][function_id] = (
FunctionExecutionInfo(
function=f,
function_name=function_name,
max_calls=max_calls))
self._num_task_executions[driver_id][function_id] = 0
try:
function = pickle.loads(serialized_function)
except Exception:
# If an exception was thrown when the remote function was
# imported, we record the traceback and notify the scheduler
# of the failure.
traceback_str = format_error_message(traceback.format_exc())
# Log the error message.
push_error_to_driver(
self._worker,
ray_constants.REGISTER_REMOTE_FUNCTION_PUSH_ERROR,
"Failed to unpickle the remote function '{}' with "
"function ID {}. Traceback:\n{}".format(
function_name, function_id.hex(), traceback_str),
driver_id=driver_id)
else:
# The below line is necessary. Because in the driver process,
# if the function is defined in the file where the python
# script was started from, its module is `__main__`.
# However in the worker process, the `__main__` module is a
# different module, which is `default_worker.py`
function.__module__ = module
self._function_execution_info[driver_id][function_id] = (
FunctionExecutionInfo(
function=function,
function_name=function_name,
max_calls=max_calls))
# Add the function to the function table.
self._worker.redis_client.rpush(
b"FunctionTable:" + function_id.binary(),
self._worker.worker_id) | python | def fetch_and_register_remote_function(self, key):
"""Import a remote function."""
(driver_id_str, function_id_str, function_name, serialized_function,
num_return_vals, module, resources,
max_calls) = self._worker.redis_client.hmget(key, [
"driver_id", "function_id", "name", "function", "num_return_vals",
"module", "resources", "max_calls"
])
function_id = ray.FunctionID(function_id_str)
driver_id = ray.DriverID(driver_id_str)
function_name = decode(function_name)
max_calls = int(max_calls)
module = decode(module)
# This is a placeholder in case the function can't be unpickled. This
# will be overwritten if the function is successfully registered.
def f():
raise Exception("This function was not imported properly.")
# This function is called by ImportThread. This operation needs to be
# atomic. Otherwise, there is race condition. Another thread may use
# the temporary function above before the real function is ready.
with self.lock:
self._function_execution_info[driver_id][function_id] = (
FunctionExecutionInfo(
function=f,
function_name=function_name,
max_calls=max_calls))
self._num_task_executions[driver_id][function_id] = 0
try:
function = pickle.loads(serialized_function)
except Exception:
# If an exception was thrown when the remote function was
# imported, we record the traceback and notify the scheduler
# of the failure.
traceback_str = format_error_message(traceback.format_exc())
# Log the error message.
push_error_to_driver(
self._worker,
ray_constants.REGISTER_REMOTE_FUNCTION_PUSH_ERROR,
"Failed to unpickle the remote function '{}' with "
"function ID {}. Traceback:\n{}".format(
function_name, function_id.hex(), traceback_str),
driver_id=driver_id)
else:
# The below line is necessary. Because in the driver process,
# if the function is defined in the file where the python
# script was started from, its module is `__main__`.
# However in the worker process, the `__main__` module is a
# different module, which is `default_worker.py`
function.__module__ = module
self._function_execution_info[driver_id][function_id] = (
FunctionExecutionInfo(
function=function,
function_name=function_name,
max_calls=max_calls))
# Add the function to the function table.
self._worker.redis_client.rpush(
b"FunctionTable:" + function_id.binary(),
self._worker.worker_id) | [
"def",
"fetch_and_register_remote_function",
"(",
"self",
",",
"key",
")",
":",
"(",
"driver_id_str",
",",
"function_id_str",
",",
"function_name",
",",
"serialized_function",
",",
"num_return_vals",
",",
"module",
",",
"resources",
",",
"max_calls",
")",
"=",
"self",
".",
"_worker",
".",
"redis_client",
".",
"hmget",
"(",
"key",
",",
"[",
"\"driver_id\"",
",",
"\"function_id\"",
",",
"\"name\"",
",",
"\"function\"",
",",
"\"num_return_vals\"",
",",
"\"module\"",
",",
"\"resources\"",
",",
"\"max_calls\"",
"]",
")",
"function_id",
"=",
"ray",
".",
"FunctionID",
"(",
"function_id_str",
")",
"driver_id",
"=",
"ray",
".",
"DriverID",
"(",
"driver_id_str",
")",
"function_name",
"=",
"decode",
"(",
"function_name",
")",
"max_calls",
"=",
"int",
"(",
"max_calls",
")",
"module",
"=",
"decode",
"(",
"module",
")",
"# This is a placeholder in case the function can't be unpickled. This",
"# will be overwritten if the function is successfully registered.",
"def",
"f",
"(",
")",
":",
"raise",
"Exception",
"(",
"\"This function was not imported properly.\"",
")",
"# This function is called by ImportThread. This operation needs to be",
"# atomic. Otherwise, there is race condition. Another thread may use",
"# the temporary function above before the real function is ready.",
"with",
"self",
".",
"lock",
":",
"self",
".",
"_function_execution_info",
"[",
"driver_id",
"]",
"[",
"function_id",
"]",
"=",
"(",
"FunctionExecutionInfo",
"(",
"function",
"=",
"f",
",",
"function_name",
"=",
"function_name",
",",
"max_calls",
"=",
"max_calls",
")",
")",
"self",
".",
"_num_task_executions",
"[",
"driver_id",
"]",
"[",
"function_id",
"]",
"=",
"0",
"try",
":",
"function",
"=",
"pickle",
".",
"loads",
"(",
"serialized_function",
")",
"except",
"Exception",
":",
"# If an exception was thrown when the remote function was",
"# imported, we record the traceback and notify the scheduler",
"# of the failure.",
"traceback_str",
"=",
"format_error_message",
"(",
"traceback",
".",
"format_exc",
"(",
")",
")",
"# Log the error message.",
"push_error_to_driver",
"(",
"self",
".",
"_worker",
",",
"ray_constants",
".",
"REGISTER_REMOTE_FUNCTION_PUSH_ERROR",
",",
"\"Failed to unpickle the remote function '{}' with \"",
"\"function ID {}. Traceback:\\n{}\"",
".",
"format",
"(",
"function_name",
",",
"function_id",
".",
"hex",
"(",
")",
",",
"traceback_str",
")",
",",
"driver_id",
"=",
"driver_id",
")",
"else",
":",
"# The below line is necessary. Because in the driver process,",
"# if the function is defined in the file where the python",
"# script was started from, its module is `__main__`.",
"# However in the worker process, the `__main__` module is a",
"# different module, which is `default_worker.py`",
"function",
".",
"__module__",
"=",
"module",
"self",
".",
"_function_execution_info",
"[",
"driver_id",
"]",
"[",
"function_id",
"]",
"=",
"(",
"FunctionExecutionInfo",
"(",
"function",
"=",
"function",
",",
"function_name",
"=",
"function_name",
",",
"max_calls",
"=",
"max_calls",
")",
")",
"# Add the function to the function table.",
"self",
".",
"_worker",
".",
"redis_client",
".",
"rpush",
"(",
"b\"FunctionTable:\"",
"+",
"function_id",
".",
"binary",
"(",
")",
",",
"self",
".",
"_worker",
".",
"worker_id",
")"
] | Import a remote function. | [
"Import",
"a",
"remote",
"function",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L393-L453 |
24,667 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager.get_execution_info | def get_execution_info(self, driver_id, function_descriptor):
"""Get the FunctionExecutionInfo of a remote function.
Args:
driver_id: ID of the driver that the function belongs to.
function_descriptor: The FunctionDescriptor of the function to get.
Returns:
A FunctionExecutionInfo object.
"""
if self._worker.load_code_from_local:
# Load function from local code.
# Currently, we don't support isolating code by drivers,
# thus always set driver ID to NIL here.
driver_id = ray.DriverID.nil()
if not function_descriptor.is_actor_method():
self._load_function_from_local(driver_id, function_descriptor)
else:
# Load function from GCS.
# Wait until the function to be executed has actually been
# registered on this worker. We will push warnings to the user if
# we spend too long in this loop.
# The driver function may not be found in sys.path. Try to load
# the function from GCS.
with profiling.profile("wait_for_function"):
self._wait_for_function(function_descriptor, driver_id)
try:
function_id = function_descriptor.function_id
info = self._function_execution_info[driver_id][function_id]
except KeyError as e:
message = ("Error occurs in get_execution_info: "
"driver_id: %s, function_descriptor: %s. Message: %s" %
(driver_id, function_descriptor, e))
raise KeyError(message)
return info | python | def get_execution_info(self, driver_id, function_descriptor):
"""Get the FunctionExecutionInfo of a remote function.
Args:
driver_id: ID of the driver that the function belongs to.
function_descriptor: The FunctionDescriptor of the function to get.
Returns:
A FunctionExecutionInfo object.
"""
if self._worker.load_code_from_local:
# Load function from local code.
# Currently, we don't support isolating code by drivers,
# thus always set driver ID to NIL here.
driver_id = ray.DriverID.nil()
if not function_descriptor.is_actor_method():
self._load_function_from_local(driver_id, function_descriptor)
else:
# Load function from GCS.
# Wait until the function to be executed has actually been
# registered on this worker. We will push warnings to the user if
# we spend too long in this loop.
# The driver function may not be found in sys.path. Try to load
# the function from GCS.
with profiling.profile("wait_for_function"):
self._wait_for_function(function_descriptor, driver_id)
try:
function_id = function_descriptor.function_id
info = self._function_execution_info[driver_id][function_id]
except KeyError as e:
message = ("Error occurs in get_execution_info: "
"driver_id: %s, function_descriptor: %s. Message: %s" %
(driver_id, function_descriptor, e))
raise KeyError(message)
return info | [
"def",
"get_execution_info",
"(",
"self",
",",
"driver_id",
",",
"function_descriptor",
")",
":",
"if",
"self",
".",
"_worker",
".",
"load_code_from_local",
":",
"# Load function from local code.",
"# Currently, we don't support isolating code by drivers,",
"# thus always set driver ID to NIL here.",
"driver_id",
"=",
"ray",
".",
"DriverID",
".",
"nil",
"(",
")",
"if",
"not",
"function_descriptor",
".",
"is_actor_method",
"(",
")",
":",
"self",
".",
"_load_function_from_local",
"(",
"driver_id",
",",
"function_descriptor",
")",
"else",
":",
"# Load function from GCS.",
"# Wait until the function to be executed has actually been",
"# registered on this worker. We will push warnings to the user if",
"# we spend too long in this loop.",
"# The driver function may not be found in sys.path. Try to load",
"# the function from GCS.",
"with",
"profiling",
".",
"profile",
"(",
"\"wait_for_function\"",
")",
":",
"self",
".",
"_wait_for_function",
"(",
"function_descriptor",
",",
"driver_id",
")",
"try",
":",
"function_id",
"=",
"function_descriptor",
".",
"function_id",
"info",
"=",
"self",
".",
"_function_execution_info",
"[",
"driver_id",
"]",
"[",
"function_id",
"]",
"except",
"KeyError",
"as",
"e",
":",
"message",
"=",
"(",
"\"Error occurs in get_execution_info: \"",
"\"driver_id: %s, function_descriptor: %s. Message: %s\"",
"%",
"(",
"driver_id",
",",
"function_descriptor",
",",
"e",
")",
")",
"raise",
"KeyError",
"(",
"message",
")",
"return",
"info"
] | Get the FunctionExecutionInfo of a remote function.
Args:
driver_id: ID of the driver that the function belongs to.
function_descriptor: The FunctionDescriptor of the function to get.
Returns:
A FunctionExecutionInfo object. | [
"Get",
"the",
"FunctionExecutionInfo",
"of",
"a",
"remote",
"function",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L455-L489 |
24,668 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager._wait_for_function | def _wait_for_function(self, function_descriptor, driver_id, timeout=10):
"""Wait until the function to be executed is present on this worker.
This method will simply loop until the import thread has imported the
relevant function. If we spend too long in this loop, that may indicate
a problem somewhere and we will push an error message to the user.
If this worker is an actor, then this will wait until the actor has
been defined.
Args:
function_descriptor : The FunctionDescriptor of the function that
we want to execute.
driver_id (str): The ID of the driver to push the error message to
if this times out.
"""
start_time = time.time()
# Only send the warning once.
warning_sent = False
while True:
with self.lock:
if (self._worker.actor_id.is_nil()
and (function_descriptor.function_id in
self._function_execution_info[driver_id])):
break
elif not self._worker.actor_id.is_nil() and (
self._worker.actor_id in self._worker.actors):
break
if time.time() - start_time > timeout:
warning_message = ("This worker was asked to execute a "
"function that it does not have "
"registered. You may have to restart "
"Ray.")
if not warning_sent:
ray.utils.push_error_to_driver(
self._worker,
ray_constants.WAIT_FOR_FUNCTION_PUSH_ERROR,
warning_message,
driver_id=driver_id)
warning_sent = True
time.sleep(0.001) | python | def _wait_for_function(self, function_descriptor, driver_id, timeout=10):
"""Wait until the function to be executed is present on this worker.
This method will simply loop until the import thread has imported the
relevant function. If we spend too long in this loop, that may indicate
a problem somewhere and we will push an error message to the user.
If this worker is an actor, then this will wait until the actor has
been defined.
Args:
function_descriptor : The FunctionDescriptor of the function that
we want to execute.
driver_id (str): The ID of the driver to push the error message to
if this times out.
"""
start_time = time.time()
# Only send the warning once.
warning_sent = False
while True:
with self.lock:
if (self._worker.actor_id.is_nil()
and (function_descriptor.function_id in
self._function_execution_info[driver_id])):
break
elif not self._worker.actor_id.is_nil() and (
self._worker.actor_id in self._worker.actors):
break
if time.time() - start_time > timeout:
warning_message = ("This worker was asked to execute a "
"function that it does not have "
"registered. You may have to restart "
"Ray.")
if not warning_sent:
ray.utils.push_error_to_driver(
self._worker,
ray_constants.WAIT_FOR_FUNCTION_PUSH_ERROR,
warning_message,
driver_id=driver_id)
warning_sent = True
time.sleep(0.001) | [
"def",
"_wait_for_function",
"(",
"self",
",",
"function_descriptor",
",",
"driver_id",
",",
"timeout",
"=",
"10",
")",
":",
"start_time",
"=",
"time",
".",
"time",
"(",
")",
"# Only send the warning once.",
"warning_sent",
"=",
"False",
"while",
"True",
":",
"with",
"self",
".",
"lock",
":",
"if",
"(",
"self",
".",
"_worker",
".",
"actor_id",
".",
"is_nil",
"(",
")",
"and",
"(",
"function_descriptor",
".",
"function_id",
"in",
"self",
".",
"_function_execution_info",
"[",
"driver_id",
"]",
")",
")",
":",
"break",
"elif",
"not",
"self",
".",
"_worker",
".",
"actor_id",
".",
"is_nil",
"(",
")",
"and",
"(",
"self",
".",
"_worker",
".",
"actor_id",
"in",
"self",
".",
"_worker",
".",
"actors",
")",
":",
"break",
"if",
"time",
".",
"time",
"(",
")",
"-",
"start_time",
">",
"timeout",
":",
"warning_message",
"=",
"(",
"\"This worker was asked to execute a \"",
"\"function that it does not have \"",
"\"registered. You may have to restart \"",
"\"Ray.\"",
")",
"if",
"not",
"warning_sent",
":",
"ray",
".",
"utils",
".",
"push_error_to_driver",
"(",
"self",
".",
"_worker",
",",
"ray_constants",
".",
"WAIT_FOR_FUNCTION_PUSH_ERROR",
",",
"warning_message",
",",
"driver_id",
"=",
"driver_id",
")",
"warning_sent",
"=",
"True",
"time",
".",
"sleep",
"(",
"0.001",
")"
] | Wait until the function to be executed is present on this worker.
This method will simply loop until the import thread has imported the
relevant function. If we spend too long in this loop, that may indicate
a problem somewhere and we will push an error message to the user.
If this worker is an actor, then this will wait until the actor has
been defined.
Args:
function_descriptor : The FunctionDescriptor of the function that
we want to execute.
driver_id (str): The ID of the driver to push the error message to
if this times out. | [
"Wait",
"until",
"the",
"function",
"to",
"be",
"executed",
"is",
"present",
"on",
"this",
"worker",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L518-L558 |
24,669 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager._publish_actor_class_to_key | def _publish_actor_class_to_key(self, key, actor_class_info):
"""Push an actor class definition to Redis.
The is factored out as a separate function because it is also called
on cached actor class definitions when a worker connects for the first
time.
Args:
key: The key to store the actor class info at.
actor_class_info: Information about the actor class.
"""
# We set the driver ID here because it may not have been available when
# the actor class was defined.
self._worker.redis_client.hmset(key, actor_class_info)
self._worker.redis_client.rpush("Exports", key) | python | def _publish_actor_class_to_key(self, key, actor_class_info):
"""Push an actor class definition to Redis.
The is factored out as a separate function because it is also called
on cached actor class definitions when a worker connects for the first
time.
Args:
key: The key to store the actor class info at.
actor_class_info: Information about the actor class.
"""
# We set the driver ID here because it may not have been available when
# the actor class was defined.
self._worker.redis_client.hmset(key, actor_class_info)
self._worker.redis_client.rpush("Exports", key) | [
"def",
"_publish_actor_class_to_key",
"(",
"self",
",",
"key",
",",
"actor_class_info",
")",
":",
"# We set the driver ID here because it may not have been available when",
"# the actor class was defined.",
"self",
".",
"_worker",
".",
"redis_client",
".",
"hmset",
"(",
"key",
",",
"actor_class_info",
")",
"self",
".",
"_worker",
".",
"redis_client",
".",
"rpush",
"(",
"\"Exports\"",
",",
"key",
")"
] | Push an actor class definition to Redis.
The is factored out as a separate function because it is also called
on cached actor class definitions when a worker connects for the first
time.
Args:
key: The key to store the actor class info at.
actor_class_info: Information about the actor class. | [
"Push",
"an",
"actor",
"class",
"definition",
"to",
"Redis",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L560-L574 |
24,670 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager.load_actor_class | def load_actor_class(self, driver_id, function_descriptor):
"""Load the actor class.
Args:
driver_id: Driver ID of the actor.
function_descriptor: Function descriptor of the actor constructor.
Returns:
The actor class.
"""
function_id = function_descriptor.function_id
# Check if the actor class already exists in the cache.
actor_class = self._loaded_actor_classes.get(function_id, None)
if actor_class is None:
# Load actor class.
if self._worker.load_code_from_local:
driver_id = ray.DriverID.nil()
# Load actor class from local code.
actor_class = self._load_actor_from_local(
driver_id, function_descriptor)
else:
# Load actor class from GCS.
actor_class = self._load_actor_class_from_gcs(
driver_id, function_descriptor)
# Save the loaded actor class in cache.
self._loaded_actor_classes[function_id] = actor_class
# Generate execution info for the methods of this actor class.
module_name = function_descriptor.module_name
actor_class_name = function_descriptor.class_name
actor_methods = inspect.getmembers(
actor_class, predicate=is_function_or_method)
for actor_method_name, actor_method in actor_methods:
method_descriptor = FunctionDescriptor(
module_name, actor_method_name, actor_class_name)
method_id = method_descriptor.function_id
executor = self._make_actor_method_executor(
actor_method_name,
actor_method,
actor_imported=True,
)
self._function_execution_info[driver_id][method_id] = (
FunctionExecutionInfo(
function=executor,
function_name=actor_method_name,
max_calls=0,
))
self._num_task_executions[driver_id][method_id] = 0
self._num_task_executions[driver_id][function_id] = 0
return actor_class | python | def load_actor_class(self, driver_id, function_descriptor):
"""Load the actor class.
Args:
driver_id: Driver ID of the actor.
function_descriptor: Function descriptor of the actor constructor.
Returns:
The actor class.
"""
function_id = function_descriptor.function_id
# Check if the actor class already exists in the cache.
actor_class = self._loaded_actor_classes.get(function_id, None)
if actor_class is None:
# Load actor class.
if self._worker.load_code_from_local:
driver_id = ray.DriverID.nil()
# Load actor class from local code.
actor_class = self._load_actor_from_local(
driver_id, function_descriptor)
else:
# Load actor class from GCS.
actor_class = self._load_actor_class_from_gcs(
driver_id, function_descriptor)
# Save the loaded actor class in cache.
self._loaded_actor_classes[function_id] = actor_class
# Generate execution info for the methods of this actor class.
module_name = function_descriptor.module_name
actor_class_name = function_descriptor.class_name
actor_methods = inspect.getmembers(
actor_class, predicate=is_function_or_method)
for actor_method_name, actor_method in actor_methods:
method_descriptor = FunctionDescriptor(
module_name, actor_method_name, actor_class_name)
method_id = method_descriptor.function_id
executor = self._make_actor_method_executor(
actor_method_name,
actor_method,
actor_imported=True,
)
self._function_execution_info[driver_id][method_id] = (
FunctionExecutionInfo(
function=executor,
function_name=actor_method_name,
max_calls=0,
))
self._num_task_executions[driver_id][method_id] = 0
self._num_task_executions[driver_id][function_id] = 0
return actor_class | [
"def",
"load_actor_class",
"(",
"self",
",",
"driver_id",
",",
"function_descriptor",
")",
":",
"function_id",
"=",
"function_descriptor",
".",
"function_id",
"# Check if the actor class already exists in the cache.",
"actor_class",
"=",
"self",
".",
"_loaded_actor_classes",
".",
"get",
"(",
"function_id",
",",
"None",
")",
"if",
"actor_class",
"is",
"None",
":",
"# Load actor class.",
"if",
"self",
".",
"_worker",
".",
"load_code_from_local",
":",
"driver_id",
"=",
"ray",
".",
"DriverID",
".",
"nil",
"(",
")",
"# Load actor class from local code.",
"actor_class",
"=",
"self",
".",
"_load_actor_from_local",
"(",
"driver_id",
",",
"function_descriptor",
")",
"else",
":",
"# Load actor class from GCS.",
"actor_class",
"=",
"self",
".",
"_load_actor_class_from_gcs",
"(",
"driver_id",
",",
"function_descriptor",
")",
"# Save the loaded actor class in cache.",
"self",
".",
"_loaded_actor_classes",
"[",
"function_id",
"]",
"=",
"actor_class",
"# Generate execution info for the methods of this actor class.",
"module_name",
"=",
"function_descriptor",
".",
"module_name",
"actor_class_name",
"=",
"function_descriptor",
".",
"class_name",
"actor_methods",
"=",
"inspect",
".",
"getmembers",
"(",
"actor_class",
",",
"predicate",
"=",
"is_function_or_method",
")",
"for",
"actor_method_name",
",",
"actor_method",
"in",
"actor_methods",
":",
"method_descriptor",
"=",
"FunctionDescriptor",
"(",
"module_name",
",",
"actor_method_name",
",",
"actor_class_name",
")",
"method_id",
"=",
"method_descriptor",
".",
"function_id",
"executor",
"=",
"self",
".",
"_make_actor_method_executor",
"(",
"actor_method_name",
",",
"actor_method",
",",
"actor_imported",
"=",
"True",
",",
")",
"self",
".",
"_function_execution_info",
"[",
"driver_id",
"]",
"[",
"method_id",
"]",
"=",
"(",
"FunctionExecutionInfo",
"(",
"function",
"=",
"executor",
",",
"function_name",
"=",
"actor_method_name",
",",
"max_calls",
"=",
"0",
",",
")",
")",
"self",
".",
"_num_task_executions",
"[",
"driver_id",
"]",
"[",
"method_id",
"]",
"=",
"0",
"self",
".",
"_num_task_executions",
"[",
"driver_id",
"]",
"[",
"function_id",
"]",
"=",
"0",
"return",
"actor_class"
] | Load the actor class.
Args:
driver_id: Driver ID of the actor.
function_descriptor: Function descriptor of the actor constructor.
Returns:
The actor class. | [
"Load",
"the",
"actor",
"class",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L619-L668 |
24,671 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager._load_actor_from_local | def _load_actor_from_local(self, driver_id, function_descriptor):
"""Load actor class from local code."""
module_name, class_name = (function_descriptor.module_name,
function_descriptor.class_name)
try:
module = importlib.import_module(module_name)
actor_class = getattr(module, class_name)
if isinstance(actor_class, ray.actor.ActorClass):
return actor_class._modified_class
else:
return actor_class
except Exception:
logger.exception(
"Failed to load actor_class %s.".format(class_name))
raise Exception(
"Actor {} failed to be imported from local code.".format(
class_name)) | python | def _load_actor_from_local(self, driver_id, function_descriptor):
"""Load actor class from local code."""
module_name, class_name = (function_descriptor.module_name,
function_descriptor.class_name)
try:
module = importlib.import_module(module_name)
actor_class = getattr(module, class_name)
if isinstance(actor_class, ray.actor.ActorClass):
return actor_class._modified_class
else:
return actor_class
except Exception:
logger.exception(
"Failed to load actor_class %s.".format(class_name))
raise Exception(
"Actor {} failed to be imported from local code.".format(
class_name)) | [
"def",
"_load_actor_from_local",
"(",
"self",
",",
"driver_id",
",",
"function_descriptor",
")",
":",
"module_name",
",",
"class_name",
"=",
"(",
"function_descriptor",
".",
"module_name",
",",
"function_descriptor",
".",
"class_name",
")",
"try",
":",
"module",
"=",
"importlib",
".",
"import_module",
"(",
"module_name",
")",
"actor_class",
"=",
"getattr",
"(",
"module",
",",
"class_name",
")",
"if",
"isinstance",
"(",
"actor_class",
",",
"ray",
".",
"actor",
".",
"ActorClass",
")",
":",
"return",
"actor_class",
".",
"_modified_class",
"else",
":",
"return",
"actor_class",
"except",
"Exception",
":",
"logger",
".",
"exception",
"(",
"\"Failed to load actor_class %s.\"",
".",
"format",
"(",
"class_name",
")",
")",
"raise",
"Exception",
"(",
"\"Actor {} failed to be imported from local code.\"",
".",
"format",
"(",
"class_name",
")",
")"
] | Load actor class from local code. | [
"Load",
"actor",
"class",
"from",
"local",
"code",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L670-L686 |
24,672 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager._load_actor_class_from_gcs | def _load_actor_class_from_gcs(self, driver_id, function_descriptor):
"""Load actor class from GCS."""
key = (b"ActorClass:" + driver_id.binary() + b":" +
function_descriptor.function_id.binary())
# Wait for the actor class key to have been imported by the
# import thread. TODO(rkn): It shouldn't be possible to end
# up in an infinite loop here, but we should push an error to
# the driver if too much time is spent here.
while key not in self.imported_actor_classes:
time.sleep(0.001)
# Fetch raw data from GCS.
(driver_id_str, class_name, module, pickled_class,
actor_method_names) = self._worker.redis_client.hmget(
key, [
"driver_id", "class_name", "module", "class",
"actor_method_names"
])
class_name = ensure_str(class_name)
module_name = ensure_str(module)
driver_id = ray.DriverID(driver_id_str)
actor_method_names = json.loads(ensure_str(actor_method_names))
actor_class = None
try:
with self.lock:
actor_class = pickle.loads(pickled_class)
except Exception:
logger.exception(
"Failed to load actor class %s.".format(class_name))
# The actor class failed to be unpickled, create a fake actor
# class instead (just to produce error messages and to prevent
# the driver from hanging).
actor_class = self._create_fake_actor_class(
class_name, actor_method_names)
# If an exception was thrown when the actor was imported, we record
# the traceback and notify the scheduler of the failure.
traceback_str = ray.utils.format_error_message(
traceback.format_exc())
# Log the error message.
push_error_to_driver(
self._worker, ray_constants.REGISTER_ACTOR_PUSH_ERROR,
"Failed to unpickle actor class '{}' for actor ID {}. "
"Traceback:\n{}".format(class_name,
self._worker.actor_id.hex(),
traceback_str), driver_id)
# TODO(rkn): In the future, it might make sense to have the worker
# exit here. However, currently that would lead to hanging if
# someone calls ray.get on a method invoked on the actor.
# The below line is necessary. Because in the driver process,
# if the function is defined in the file where the python script
# was started from, its module is `__main__`.
# However in the worker process, the `__main__` module is a
# different module, which is `default_worker.py`
actor_class.__module__ = module_name
return actor_class | python | def _load_actor_class_from_gcs(self, driver_id, function_descriptor):
"""Load actor class from GCS."""
key = (b"ActorClass:" + driver_id.binary() + b":" +
function_descriptor.function_id.binary())
# Wait for the actor class key to have been imported by the
# import thread. TODO(rkn): It shouldn't be possible to end
# up in an infinite loop here, but we should push an error to
# the driver if too much time is spent here.
while key not in self.imported_actor_classes:
time.sleep(0.001)
# Fetch raw data from GCS.
(driver_id_str, class_name, module, pickled_class,
actor_method_names) = self._worker.redis_client.hmget(
key, [
"driver_id", "class_name", "module", "class",
"actor_method_names"
])
class_name = ensure_str(class_name)
module_name = ensure_str(module)
driver_id = ray.DriverID(driver_id_str)
actor_method_names = json.loads(ensure_str(actor_method_names))
actor_class = None
try:
with self.lock:
actor_class = pickle.loads(pickled_class)
except Exception:
logger.exception(
"Failed to load actor class %s.".format(class_name))
# The actor class failed to be unpickled, create a fake actor
# class instead (just to produce error messages and to prevent
# the driver from hanging).
actor_class = self._create_fake_actor_class(
class_name, actor_method_names)
# If an exception was thrown when the actor was imported, we record
# the traceback and notify the scheduler of the failure.
traceback_str = ray.utils.format_error_message(
traceback.format_exc())
# Log the error message.
push_error_to_driver(
self._worker, ray_constants.REGISTER_ACTOR_PUSH_ERROR,
"Failed to unpickle actor class '{}' for actor ID {}. "
"Traceback:\n{}".format(class_name,
self._worker.actor_id.hex(),
traceback_str), driver_id)
# TODO(rkn): In the future, it might make sense to have the worker
# exit here. However, currently that would lead to hanging if
# someone calls ray.get on a method invoked on the actor.
# The below line is necessary. Because in the driver process,
# if the function is defined in the file where the python script
# was started from, its module is `__main__`.
# However in the worker process, the `__main__` module is a
# different module, which is `default_worker.py`
actor_class.__module__ = module_name
return actor_class | [
"def",
"_load_actor_class_from_gcs",
"(",
"self",
",",
"driver_id",
",",
"function_descriptor",
")",
":",
"key",
"=",
"(",
"b\"ActorClass:\"",
"+",
"driver_id",
".",
"binary",
"(",
")",
"+",
"b\":\"",
"+",
"function_descriptor",
".",
"function_id",
".",
"binary",
"(",
")",
")",
"# Wait for the actor class key to have been imported by the",
"# import thread. TODO(rkn): It shouldn't be possible to end",
"# up in an infinite loop here, but we should push an error to",
"# the driver if too much time is spent here.",
"while",
"key",
"not",
"in",
"self",
".",
"imported_actor_classes",
":",
"time",
".",
"sleep",
"(",
"0.001",
")",
"# Fetch raw data from GCS.",
"(",
"driver_id_str",
",",
"class_name",
",",
"module",
",",
"pickled_class",
",",
"actor_method_names",
")",
"=",
"self",
".",
"_worker",
".",
"redis_client",
".",
"hmget",
"(",
"key",
",",
"[",
"\"driver_id\"",
",",
"\"class_name\"",
",",
"\"module\"",
",",
"\"class\"",
",",
"\"actor_method_names\"",
"]",
")",
"class_name",
"=",
"ensure_str",
"(",
"class_name",
")",
"module_name",
"=",
"ensure_str",
"(",
"module",
")",
"driver_id",
"=",
"ray",
".",
"DriverID",
"(",
"driver_id_str",
")",
"actor_method_names",
"=",
"json",
".",
"loads",
"(",
"ensure_str",
"(",
"actor_method_names",
")",
")",
"actor_class",
"=",
"None",
"try",
":",
"with",
"self",
".",
"lock",
":",
"actor_class",
"=",
"pickle",
".",
"loads",
"(",
"pickled_class",
")",
"except",
"Exception",
":",
"logger",
".",
"exception",
"(",
"\"Failed to load actor class %s.\"",
".",
"format",
"(",
"class_name",
")",
")",
"# The actor class failed to be unpickled, create a fake actor",
"# class instead (just to produce error messages and to prevent",
"# the driver from hanging).",
"actor_class",
"=",
"self",
".",
"_create_fake_actor_class",
"(",
"class_name",
",",
"actor_method_names",
")",
"# If an exception was thrown when the actor was imported, we record",
"# the traceback and notify the scheduler of the failure.",
"traceback_str",
"=",
"ray",
".",
"utils",
".",
"format_error_message",
"(",
"traceback",
".",
"format_exc",
"(",
")",
")",
"# Log the error message.",
"push_error_to_driver",
"(",
"self",
".",
"_worker",
",",
"ray_constants",
".",
"REGISTER_ACTOR_PUSH_ERROR",
",",
"\"Failed to unpickle actor class '{}' for actor ID {}. \"",
"\"Traceback:\\n{}\"",
".",
"format",
"(",
"class_name",
",",
"self",
".",
"_worker",
".",
"actor_id",
".",
"hex",
"(",
")",
",",
"traceback_str",
")",
",",
"driver_id",
")",
"# TODO(rkn): In the future, it might make sense to have the worker",
"# exit here. However, currently that would lead to hanging if",
"# someone calls ray.get on a method invoked on the actor.",
"# The below line is necessary. Because in the driver process,",
"# if the function is defined in the file where the python script",
"# was started from, its module is `__main__`.",
"# However in the worker process, the `__main__` module is a",
"# different module, which is `default_worker.py`",
"actor_class",
".",
"__module__",
"=",
"module_name",
"return",
"actor_class"
] | Load actor class from GCS. | [
"Load",
"actor",
"class",
"from",
"GCS",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L702-L759 |
24,673 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager._make_actor_method_executor | def _make_actor_method_executor(self, method_name, method, actor_imported):
"""Make an executor that wraps a user-defined actor method.
The wrapped method updates the worker's internal state and performs any
necessary checkpointing operations.
Args:
method_name (str): The name of the actor method.
method (instancemethod): The actor method to wrap. This should be a
method defined on the actor class and should therefore take an
instance of the actor as the first argument.
actor_imported (bool): Whether the actor has been imported.
Checkpointing operations will not be run if this is set to
False.
Returns:
A function that executes the given actor method on the worker's
stored instance of the actor. The function also updates the
worker's internal state to record the executed method.
"""
def actor_method_executor(dummy_return_id, actor, *args):
# Update the actor's task counter to reflect the task we're about
# to execute.
self._worker.actor_task_counter += 1
# Execute the assigned method and save a checkpoint if necessary.
try:
if is_class_method(method):
method_returns = method(*args)
else:
method_returns = method(actor, *args)
except Exception as e:
# Save the checkpoint before allowing the method exception
# to be thrown, but don't save the checkpoint for actor
# creation task.
if (isinstance(actor, ray.actor.Checkpointable)
and self._worker.actor_task_counter != 1):
self._save_and_log_checkpoint(actor)
raise e
else:
# Handle any checkpointing operations before storing the
# method's return values.
# NOTE(swang): If method_returns is a pointer to the actor's
# state and the checkpointing operations can modify the return
# values if they mutate the actor's state. Is this okay?
if isinstance(actor, ray.actor.Checkpointable):
# If this is the first task to execute on the actor, try to
# resume from a checkpoint.
if self._worker.actor_task_counter == 1:
if actor_imported:
self._restore_and_log_checkpoint(actor)
else:
# Save the checkpoint before returning the method's
# return values.
self._save_and_log_checkpoint(actor)
return method_returns
return actor_method_executor | python | def _make_actor_method_executor(self, method_name, method, actor_imported):
"""Make an executor that wraps a user-defined actor method.
The wrapped method updates the worker's internal state and performs any
necessary checkpointing operations.
Args:
method_name (str): The name of the actor method.
method (instancemethod): The actor method to wrap. This should be a
method defined on the actor class and should therefore take an
instance of the actor as the first argument.
actor_imported (bool): Whether the actor has been imported.
Checkpointing operations will not be run if this is set to
False.
Returns:
A function that executes the given actor method on the worker's
stored instance of the actor. The function also updates the
worker's internal state to record the executed method.
"""
def actor_method_executor(dummy_return_id, actor, *args):
# Update the actor's task counter to reflect the task we're about
# to execute.
self._worker.actor_task_counter += 1
# Execute the assigned method and save a checkpoint if necessary.
try:
if is_class_method(method):
method_returns = method(*args)
else:
method_returns = method(actor, *args)
except Exception as e:
# Save the checkpoint before allowing the method exception
# to be thrown, but don't save the checkpoint for actor
# creation task.
if (isinstance(actor, ray.actor.Checkpointable)
and self._worker.actor_task_counter != 1):
self._save_and_log_checkpoint(actor)
raise e
else:
# Handle any checkpointing operations before storing the
# method's return values.
# NOTE(swang): If method_returns is a pointer to the actor's
# state and the checkpointing operations can modify the return
# values if they mutate the actor's state. Is this okay?
if isinstance(actor, ray.actor.Checkpointable):
# If this is the first task to execute on the actor, try to
# resume from a checkpoint.
if self._worker.actor_task_counter == 1:
if actor_imported:
self._restore_and_log_checkpoint(actor)
else:
# Save the checkpoint before returning the method's
# return values.
self._save_and_log_checkpoint(actor)
return method_returns
return actor_method_executor | [
"def",
"_make_actor_method_executor",
"(",
"self",
",",
"method_name",
",",
"method",
",",
"actor_imported",
")",
":",
"def",
"actor_method_executor",
"(",
"dummy_return_id",
",",
"actor",
",",
"*",
"args",
")",
":",
"# Update the actor's task counter to reflect the task we're about",
"# to execute.",
"self",
".",
"_worker",
".",
"actor_task_counter",
"+=",
"1",
"# Execute the assigned method and save a checkpoint if necessary.",
"try",
":",
"if",
"is_class_method",
"(",
"method",
")",
":",
"method_returns",
"=",
"method",
"(",
"*",
"args",
")",
"else",
":",
"method_returns",
"=",
"method",
"(",
"actor",
",",
"*",
"args",
")",
"except",
"Exception",
"as",
"e",
":",
"# Save the checkpoint before allowing the method exception",
"# to be thrown, but don't save the checkpoint for actor",
"# creation task.",
"if",
"(",
"isinstance",
"(",
"actor",
",",
"ray",
".",
"actor",
".",
"Checkpointable",
")",
"and",
"self",
".",
"_worker",
".",
"actor_task_counter",
"!=",
"1",
")",
":",
"self",
".",
"_save_and_log_checkpoint",
"(",
"actor",
")",
"raise",
"e",
"else",
":",
"# Handle any checkpointing operations before storing the",
"# method's return values.",
"# NOTE(swang): If method_returns is a pointer to the actor's",
"# state and the checkpointing operations can modify the return",
"# values if they mutate the actor's state. Is this okay?",
"if",
"isinstance",
"(",
"actor",
",",
"ray",
".",
"actor",
".",
"Checkpointable",
")",
":",
"# If this is the first task to execute on the actor, try to",
"# resume from a checkpoint.",
"if",
"self",
".",
"_worker",
".",
"actor_task_counter",
"==",
"1",
":",
"if",
"actor_imported",
":",
"self",
".",
"_restore_and_log_checkpoint",
"(",
"actor",
")",
"else",
":",
"# Save the checkpoint before returning the method's",
"# return values.",
"self",
".",
"_save_and_log_checkpoint",
"(",
"actor",
")",
"return",
"method_returns",
"return",
"actor_method_executor"
] | Make an executor that wraps a user-defined actor method.
The wrapped method updates the worker's internal state and performs any
necessary checkpointing operations.
Args:
method_name (str): The name of the actor method.
method (instancemethod): The actor method to wrap. This should be a
method defined on the actor class and should therefore take an
instance of the actor as the first argument.
actor_imported (bool): Whether the actor has been imported.
Checkpointing operations will not be run if this is set to
False.
Returns:
A function that executes the given actor method on the worker's
stored instance of the actor. The function also updates the
worker's internal state to record the executed method. | [
"Make",
"an",
"executor",
"that",
"wraps",
"a",
"user",
"-",
"defined",
"actor",
"method",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L761-L819 |
24,674 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager._save_and_log_checkpoint | def _save_and_log_checkpoint(self, actor):
"""Save an actor checkpoint if necessary and log any errors.
Args:
actor: The actor to checkpoint.
Returns:
The result of the actor's user-defined `save_checkpoint` method.
"""
actor_id = self._worker.actor_id
checkpoint_info = self._worker.actor_checkpoint_info[actor_id]
checkpoint_info.num_tasks_since_last_checkpoint += 1
now = int(1000 * time.time())
checkpoint_context = ray.actor.CheckpointContext(
actor_id, checkpoint_info.num_tasks_since_last_checkpoint,
now - checkpoint_info.last_checkpoint_timestamp)
# If we should take a checkpoint, notify raylet to prepare a checkpoint
# and then call `save_checkpoint`.
if actor.should_checkpoint(checkpoint_context):
try:
now = int(1000 * time.time())
checkpoint_id = (self._worker.raylet_client.
prepare_actor_checkpoint(actor_id))
checkpoint_info.checkpoint_ids.append(checkpoint_id)
actor.save_checkpoint(actor_id, checkpoint_id)
if (len(checkpoint_info.checkpoint_ids) >
ray._config.num_actor_checkpoints_to_keep()):
actor.checkpoint_expired(
actor_id,
checkpoint_info.checkpoint_ids.pop(0),
)
checkpoint_info.num_tasks_since_last_checkpoint = 0
checkpoint_info.last_checkpoint_timestamp = now
except Exception:
# Checkpoint save or reload failed. Notify the driver.
traceback_str = ray.utils.format_error_message(
traceback.format_exc())
ray.utils.push_error_to_driver(
self._worker,
ray_constants.CHECKPOINT_PUSH_ERROR,
traceback_str,
driver_id=self._worker.task_driver_id) | python | def _save_and_log_checkpoint(self, actor):
"""Save an actor checkpoint if necessary and log any errors.
Args:
actor: The actor to checkpoint.
Returns:
The result of the actor's user-defined `save_checkpoint` method.
"""
actor_id = self._worker.actor_id
checkpoint_info = self._worker.actor_checkpoint_info[actor_id]
checkpoint_info.num_tasks_since_last_checkpoint += 1
now = int(1000 * time.time())
checkpoint_context = ray.actor.CheckpointContext(
actor_id, checkpoint_info.num_tasks_since_last_checkpoint,
now - checkpoint_info.last_checkpoint_timestamp)
# If we should take a checkpoint, notify raylet to prepare a checkpoint
# and then call `save_checkpoint`.
if actor.should_checkpoint(checkpoint_context):
try:
now = int(1000 * time.time())
checkpoint_id = (self._worker.raylet_client.
prepare_actor_checkpoint(actor_id))
checkpoint_info.checkpoint_ids.append(checkpoint_id)
actor.save_checkpoint(actor_id, checkpoint_id)
if (len(checkpoint_info.checkpoint_ids) >
ray._config.num_actor_checkpoints_to_keep()):
actor.checkpoint_expired(
actor_id,
checkpoint_info.checkpoint_ids.pop(0),
)
checkpoint_info.num_tasks_since_last_checkpoint = 0
checkpoint_info.last_checkpoint_timestamp = now
except Exception:
# Checkpoint save or reload failed. Notify the driver.
traceback_str = ray.utils.format_error_message(
traceback.format_exc())
ray.utils.push_error_to_driver(
self._worker,
ray_constants.CHECKPOINT_PUSH_ERROR,
traceback_str,
driver_id=self._worker.task_driver_id) | [
"def",
"_save_and_log_checkpoint",
"(",
"self",
",",
"actor",
")",
":",
"actor_id",
"=",
"self",
".",
"_worker",
".",
"actor_id",
"checkpoint_info",
"=",
"self",
".",
"_worker",
".",
"actor_checkpoint_info",
"[",
"actor_id",
"]",
"checkpoint_info",
".",
"num_tasks_since_last_checkpoint",
"+=",
"1",
"now",
"=",
"int",
"(",
"1000",
"*",
"time",
".",
"time",
"(",
")",
")",
"checkpoint_context",
"=",
"ray",
".",
"actor",
".",
"CheckpointContext",
"(",
"actor_id",
",",
"checkpoint_info",
".",
"num_tasks_since_last_checkpoint",
",",
"now",
"-",
"checkpoint_info",
".",
"last_checkpoint_timestamp",
")",
"# If we should take a checkpoint, notify raylet to prepare a checkpoint",
"# and then call `save_checkpoint`.",
"if",
"actor",
".",
"should_checkpoint",
"(",
"checkpoint_context",
")",
":",
"try",
":",
"now",
"=",
"int",
"(",
"1000",
"*",
"time",
".",
"time",
"(",
")",
")",
"checkpoint_id",
"=",
"(",
"self",
".",
"_worker",
".",
"raylet_client",
".",
"prepare_actor_checkpoint",
"(",
"actor_id",
")",
")",
"checkpoint_info",
".",
"checkpoint_ids",
".",
"append",
"(",
"checkpoint_id",
")",
"actor",
".",
"save_checkpoint",
"(",
"actor_id",
",",
"checkpoint_id",
")",
"if",
"(",
"len",
"(",
"checkpoint_info",
".",
"checkpoint_ids",
")",
">",
"ray",
".",
"_config",
".",
"num_actor_checkpoints_to_keep",
"(",
")",
")",
":",
"actor",
".",
"checkpoint_expired",
"(",
"actor_id",
",",
"checkpoint_info",
".",
"checkpoint_ids",
".",
"pop",
"(",
"0",
")",
",",
")",
"checkpoint_info",
".",
"num_tasks_since_last_checkpoint",
"=",
"0",
"checkpoint_info",
".",
"last_checkpoint_timestamp",
"=",
"now",
"except",
"Exception",
":",
"# Checkpoint save or reload failed. Notify the driver.",
"traceback_str",
"=",
"ray",
".",
"utils",
".",
"format_error_message",
"(",
"traceback",
".",
"format_exc",
"(",
")",
")",
"ray",
".",
"utils",
".",
"push_error_to_driver",
"(",
"self",
".",
"_worker",
",",
"ray_constants",
".",
"CHECKPOINT_PUSH_ERROR",
",",
"traceback_str",
",",
"driver_id",
"=",
"self",
".",
"_worker",
".",
"task_driver_id",
")"
] | Save an actor checkpoint if necessary and log any errors.
Args:
actor: The actor to checkpoint.
Returns:
The result of the actor's user-defined `save_checkpoint` method. | [
"Save",
"an",
"actor",
"checkpoint",
"if",
"necessary",
"and",
"log",
"any",
"errors",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L821-L862 |
24,675 | ray-project/ray | python/ray/function_manager.py | FunctionActorManager._restore_and_log_checkpoint | def _restore_and_log_checkpoint(self, actor):
"""Restore an actor from a checkpoint if available and log any errors.
This should only be called on workers that have just executed an actor
creation task.
Args:
actor: The actor to restore from a checkpoint.
"""
actor_id = self._worker.actor_id
try:
checkpoints = ray.actor.get_checkpoints_for_actor(actor_id)
if len(checkpoints) > 0:
# If we found previously saved checkpoints for this actor,
# call the `load_checkpoint` callback.
checkpoint_id = actor.load_checkpoint(actor_id, checkpoints)
if checkpoint_id is not None:
# Check that the returned checkpoint id is in the
# `available_checkpoints` list.
msg = (
"`load_checkpoint` must return a checkpoint id that " +
"exists in the `available_checkpoints` list, or eone.")
assert any(checkpoint_id == checkpoint.checkpoint_id
for checkpoint in checkpoints), msg
# Notify raylet that this actor has been resumed from
# a checkpoint.
(self._worker.raylet_client.
notify_actor_resumed_from_checkpoint(
actor_id, checkpoint_id))
except Exception:
# Checkpoint save or reload failed. Notify the driver.
traceback_str = ray.utils.format_error_message(
traceback.format_exc())
ray.utils.push_error_to_driver(
self._worker,
ray_constants.CHECKPOINT_PUSH_ERROR,
traceback_str,
driver_id=self._worker.task_driver_id) | python | def _restore_and_log_checkpoint(self, actor):
"""Restore an actor from a checkpoint if available and log any errors.
This should only be called on workers that have just executed an actor
creation task.
Args:
actor: The actor to restore from a checkpoint.
"""
actor_id = self._worker.actor_id
try:
checkpoints = ray.actor.get_checkpoints_for_actor(actor_id)
if len(checkpoints) > 0:
# If we found previously saved checkpoints for this actor,
# call the `load_checkpoint` callback.
checkpoint_id = actor.load_checkpoint(actor_id, checkpoints)
if checkpoint_id is not None:
# Check that the returned checkpoint id is in the
# `available_checkpoints` list.
msg = (
"`load_checkpoint` must return a checkpoint id that " +
"exists in the `available_checkpoints` list, or eone.")
assert any(checkpoint_id == checkpoint.checkpoint_id
for checkpoint in checkpoints), msg
# Notify raylet that this actor has been resumed from
# a checkpoint.
(self._worker.raylet_client.
notify_actor_resumed_from_checkpoint(
actor_id, checkpoint_id))
except Exception:
# Checkpoint save or reload failed. Notify the driver.
traceback_str = ray.utils.format_error_message(
traceback.format_exc())
ray.utils.push_error_to_driver(
self._worker,
ray_constants.CHECKPOINT_PUSH_ERROR,
traceback_str,
driver_id=self._worker.task_driver_id) | [
"def",
"_restore_and_log_checkpoint",
"(",
"self",
",",
"actor",
")",
":",
"actor_id",
"=",
"self",
".",
"_worker",
".",
"actor_id",
"try",
":",
"checkpoints",
"=",
"ray",
".",
"actor",
".",
"get_checkpoints_for_actor",
"(",
"actor_id",
")",
"if",
"len",
"(",
"checkpoints",
")",
">",
"0",
":",
"# If we found previously saved checkpoints for this actor,",
"# call the `load_checkpoint` callback.",
"checkpoint_id",
"=",
"actor",
".",
"load_checkpoint",
"(",
"actor_id",
",",
"checkpoints",
")",
"if",
"checkpoint_id",
"is",
"not",
"None",
":",
"# Check that the returned checkpoint id is in the",
"# `available_checkpoints` list.",
"msg",
"=",
"(",
"\"`load_checkpoint` must return a checkpoint id that \"",
"+",
"\"exists in the `available_checkpoints` list, or eone.\"",
")",
"assert",
"any",
"(",
"checkpoint_id",
"==",
"checkpoint",
".",
"checkpoint_id",
"for",
"checkpoint",
"in",
"checkpoints",
")",
",",
"msg",
"# Notify raylet that this actor has been resumed from",
"# a checkpoint.",
"(",
"self",
".",
"_worker",
".",
"raylet_client",
".",
"notify_actor_resumed_from_checkpoint",
"(",
"actor_id",
",",
"checkpoint_id",
")",
")",
"except",
"Exception",
":",
"# Checkpoint save or reload failed. Notify the driver.",
"traceback_str",
"=",
"ray",
".",
"utils",
".",
"format_error_message",
"(",
"traceback",
".",
"format_exc",
"(",
")",
")",
"ray",
".",
"utils",
".",
"push_error_to_driver",
"(",
"self",
".",
"_worker",
",",
"ray_constants",
".",
"CHECKPOINT_PUSH_ERROR",
",",
"traceback_str",
",",
"driver_id",
"=",
"self",
".",
"_worker",
".",
"task_driver_id",
")"
] | Restore an actor from a checkpoint if available and log any errors.
This should only be called on workers that have just executed an actor
creation task.
Args:
actor: The actor to restore from a checkpoint. | [
"Restore",
"an",
"actor",
"from",
"a",
"checkpoint",
"if",
"available",
"and",
"log",
"any",
"errors",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/function_manager.py#L864-L901 |
24,676 | ray-project/ray | python/ray/rllib/evaluation/sampler.py | _env_runner | def _env_runner(base_env, extra_batch_callback, policies, policy_mapping_fn,
unroll_length, horizon, preprocessors, obs_filters,
clip_rewards, clip_actions, pack, callbacks, tf_sess,
perf_stats, soft_horizon):
"""This implements the common experience collection logic.
Args:
base_env (BaseEnv): env implementing BaseEnv.
extra_batch_callback (fn): function to send extra batch data to.
policies (dict): Map of policy ids to PolicyGraph instances.
policy_mapping_fn (func): Function that maps agent ids to policy ids.
This is called when an agent first enters the environment. The
agent is then "bound" to the returned policy for the episode.
unroll_length (int): Number of episode steps before `SampleBatch` is
yielded. Set to infinity to yield complete episodes.
horizon (int): Horizon of the episode.
preprocessors (dict): Map of policy id to preprocessor for the
observations prior to filtering.
obs_filters (dict): Map of policy id to filter used to process
observations for the policy.
clip_rewards (bool): Whether to clip rewards before postprocessing.
pack (bool): Whether to pack multiple episodes into each batch. This
guarantees batches will be exactly `unroll_length` in size.
clip_actions (bool): Whether to clip actions to the space range.
callbacks (dict): User callbacks to run on episode events.
tf_sess (Session|None): Optional tensorflow session to use for batching
TF policy evaluations.
perf_stats (PerfStats): Record perf stats into this object.
soft_horizon (bool): Calculate rewards but don't reset the
environment when the horizon is hit.
Yields:
rollout (SampleBatch): Object containing state, action, reward,
terminal condition, and other fields as dictated by `policy`.
"""
try:
if not horizon:
horizon = (base_env.get_unwrapped()[0].spec.max_episode_steps)
except Exception:
logger.debug("no episode horizon specified, assuming inf")
if not horizon:
horizon = float("inf")
# Pool of batch builders, which can be shared across episodes to pack
# trajectory data.
batch_builder_pool = []
def get_batch_builder():
if batch_builder_pool:
return batch_builder_pool.pop()
else:
return MultiAgentSampleBatchBuilder(
policies, clip_rewards, callbacks.get("on_postprocess_traj"))
def new_episode():
episode = MultiAgentEpisode(policies, policy_mapping_fn,
get_batch_builder, extra_batch_callback)
if callbacks.get("on_episode_start"):
callbacks["on_episode_start"]({
"env": base_env,
"policy": policies,
"episode": episode,
})
return episode
active_episodes = defaultdict(new_episode)
while True:
perf_stats.iters += 1
t0 = time.time()
# Get observations from all ready agents
unfiltered_obs, rewards, dones, infos, off_policy_actions = \
base_env.poll()
perf_stats.env_wait_time += time.time() - t0
if log_once("env_returns"):
logger.info("Raw obs from env: {}".format(
summarize(unfiltered_obs)))
logger.info("Info return from env: {}".format(summarize(infos)))
# Process observations and prepare for policy evaluation
t1 = time.time()
active_envs, to_eval, outputs = _process_observations(
base_env, policies, batch_builder_pool, active_episodes,
unfiltered_obs, rewards, dones, infos, off_policy_actions, horizon,
preprocessors, obs_filters, unroll_length, pack, callbacks,
soft_horizon)
perf_stats.processing_time += time.time() - t1
for o in outputs:
yield o
# Do batched policy eval
t2 = time.time()
eval_results = _do_policy_eval(tf_sess, to_eval, policies,
active_episodes)
perf_stats.inference_time += time.time() - t2
# Process results and update episode state
t3 = time.time()
actions_to_send = _process_policy_eval_results(
to_eval, eval_results, active_episodes, active_envs,
off_policy_actions, policies, clip_actions)
perf_stats.processing_time += time.time() - t3
# Return computed actions to ready envs. We also send to envs that have
# taken off-policy actions; those envs are free to ignore the action.
t4 = time.time()
base_env.send_actions(actions_to_send)
perf_stats.env_wait_time += time.time() - t4 | python | def _env_runner(base_env, extra_batch_callback, policies, policy_mapping_fn,
unroll_length, horizon, preprocessors, obs_filters,
clip_rewards, clip_actions, pack, callbacks, tf_sess,
perf_stats, soft_horizon):
"""This implements the common experience collection logic.
Args:
base_env (BaseEnv): env implementing BaseEnv.
extra_batch_callback (fn): function to send extra batch data to.
policies (dict): Map of policy ids to PolicyGraph instances.
policy_mapping_fn (func): Function that maps agent ids to policy ids.
This is called when an agent first enters the environment. The
agent is then "bound" to the returned policy for the episode.
unroll_length (int): Number of episode steps before `SampleBatch` is
yielded. Set to infinity to yield complete episodes.
horizon (int): Horizon of the episode.
preprocessors (dict): Map of policy id to preprocessor for the
observations prior to filtering.
obs_filters (dict): Map of policy id to filter used to process
observations for the policy.
clip_rewards (bool): Whether to clip rewards before postprocessing.
pack (bool): Whether to pack multiple episodes into each batch. This
guarantees batches will be exactly `unroll_length` in size.
clip_actions (bool): Whether to clip actions to the space range.
callbacks (dict): User callbacks to run on episode events.
tf_sess (Session|None): Optional tensorflow session to use for batching
TF policy evaluations.
perf_stats (PerfStats): Record perf stats into this object.
soft_horizon (bool): Calculate rewards but don't reset the
environment when the horizon is hit.
Yields:
rollout (SampleBatch): Object containing state, action, reward,
terminal condition, and other fields as dictated by `policy`.
"""
try:
if not horizon:
horizon = (base_env.get_unwrapped()[0].spec.max_episode_steps)
except Exception:
logger.debug("no episode horizon specified, assuming inf")
if not horizon:
horizon = float("inf")
# Pool of batch builders, which can be shared across episodes to pack
# trajectory data.
batch_builder_pool = []
def get_batch_builder():
if batch_builder_pool:
return batch_builder_pool.pop()
else:
return MultiAgentSampleBatchBuilder(
policies, clip_rewards, callbacks.get("on_postprocess_traj"))
def new_episode():
episode = MultiAgentEpisode(policies, policy_mapping_fn,
get_batch_builder, extra_batch_callback)
if callbacks.get("on_episode_start"):
callbacks["on_episode_start"]({
"env": base_env,
"policy": policies,
"episode": episode,
})
return episode
active_episodes = defaultdict(new_episode)
while True:
perf_stats.iters += 1
t0 = time.time()
# Get observations from all ready agents
unfiltered_obs, rewards, dones, infos, off_policy_actions = \
base_env.poll()
perf_stats.env_wait_time += time.time() - t0
if log_once("env_returns"):
logger.info("Raw obs from env: {}".format(
summarize(unfiltered_obs)))
logger.info("Info return from env: {}".format(summarize(infos)))
# Process observations and prepare for policy evaluation
t1 = time.time()
active_envs, to_eval, outputs = _process_observations(
base_env, policies, batch_builder_pool, active_episodes,
unfiltered_obs, rewards, dones, infos, off_policy_actions, horizon,
preprocessors, obs_filters, unroll_length, pack, callbacks,
soft_horizon)
perf_stats.processing_time += time.time() - t1
for o in outputs:
yield o
# Do batched policy eval
t2 = time.time()
eval_results = _do_policy_eval(tf_sess, to_eval, policies,
active_episodes)
perf_stats.inference_time += time.time() - t2
# Process results and update episode state
t3 = time.time()
actions_to_send = _process_policy_eval_results(
to_eval, eval_results, active_episodes, active_envs,
off_policy_actions, policies, clip_actions)
perf_stats.processing_time += time.time() - t3
# Return computed actions to ready envs. We also send to envs that have
# taken off-policy actions; those envs are free to ignore the action.
t4 = time.time()
base_env.send_actions(actions_to_send)
perf_stats.env_wait_time += time.time() - t4 | [
"def",
"_env_runner",
"(",
"base_env",
",",
"extra_batch_callback",
",",
"policies",
",",
"policy_mapping_fn",
",",
"unroll_length",
",",
"horizon",
",",
"preprocessors",
",",
"obs_filters",
",",
"clip_rewards",
",",
"clip_actions",
",",
"pack",
",",
"callbacks",
",",
"tf_sess",
",",
"perf_stats",
",",
"soft_horizon",
")",
":",
"try",
":",
"if",
"not",
"horizon",
":",
"horizon",
"=",
"(",
"base_env",
".",
"get_unwrapped",
"(",
")",
"[",
"0",
"]",
".",
"spec",
".",
"max_episode_steps",
")",
"except",
"Exception",
":",
"logger",
".",
"debug",
"(",
"\"no episode horizon specified, assuming inf\"",
")",
"if",
"not",
"horizon",
":",
"horizon",
"=",
"float",
"(",
"\"inf\"",
")",
"# Pool of batch builders, which can be shared across episodes to pack",
"# trajectory data.",
"batch_builder_pool",
"=",
"[",
"]",
"def",
"get_batch_builder",
"(",
")",
":",
"if",
"batch_builder_pool",
":",
"return",
"batch_builder_pool",
".",
"pop",
"(",
")",
"else",
":",
"return",
"MultiAgentSampleBatchBuilder",
"(",
"policies",
",",
"clip_rewards",
",",
"callbacks",
".",
"get",
"(",
"\"on_postprocess_traj\"",
")",
")",
"def",
"new_episode",
"(",
")",
":",
"episode",
"=",
"MultiAgentEpisode",
"(",
"policies",
",",
"policy_mapping_fn",
",",
"get_batch_builder",
",",
"extra_batch_callback",
")",
"if",
"callbacks",
".",
"get",
"(",
"\"on_episode_start\"",
")",
":",
"callbacks",
"[",
"\"on_episode_start\"",
"]",
"(",
"{",
"\"env\"",
":",
"base_env",
",",
"\"policy\"",
":",
"policies",
",",
"\"episode\"",
":",
"episode",
",",
"}",
")",
"return",
"episode",
"active_episodes",
"=",
"defaultdict",
"(",
"new_episode",
")",
"while",
"True",
":",
"perf_stats",
".",
"iters",
"+=",
"1",
"t0",
"=",
"time",
".",
"time",
"(",
")",
"# Get observations from all ready agents",
"unfiltered_obs",
",",
"rewards",
",",
"dones",
",",
"infos",
",",
"off_policy_actions",
"=",
"base_env",
".",
"poll",
"(",
")",
"perf_stats",
".",
"env_wait_time",
"+=",
"time",
".",
"time",
"(",
")",
"-",
"t0",
"if",
"log_once",
"(",
"\"env_returns\"",
")",
":",
"logger",
".",
"info",
"(",
"\"Raw obs from env: {}\"",
".",
"format",
"(",
"summarize",
"(",
"unfiltered_obs",
")",
")",
")",
"logger",
".",
"info",
"(",
"\"Info return from env: {}\"",
".",
"format",
"(",
"summarize",
"(",
"infos",
")",
")",
")",
"# Process observations and prepare for policy evaluation",
"t1",
"=",
"time",
".",
"time",
"(",
")",
"active_envs",
",",
"to_eval",
",",
"outputs",
"=",
"_process_observations",
"(",
"base_env",
",",
"policies",
",",
"batch_builder_pool",
",",
"active_episodes",
",",
"unfiltered_obs",
",",
"rewards",
",",
"dones",
",",
"infos",
",",
"off_policy_actions",
",",
"horizon",
",",
"preprocessors",
",",
"obs_filters",
",",
"unroll_length",
",",
"pack",
",",
"callbacks",
",",
"soft_horizon",
")",
"perf_stats",
".",
"processing_time",
"+=",
"time",
".",
"time",
"(",
")",
"-",
"t1",
"for",
"o",
"in",
"outputs",
":",
"yield",
"o",
"# Do batched policy eval",
"t2",
"=",
"time",
".",
"time",
"(",
")",
"eval_results",
"=",
"_do_policy_eval",
"(",
"tf_sess",
",",
"to_eval",
",",
"policies",
",",
"active_episodes",
")",
"perf_stats",
".",
"inference_time",
"+=",
"time",
".",
"time",
"(",
")",
"-",
"t2",
"# Process results and update episode state",
"t3",
"=",
"time",
".",
"time",
"(",
")",
"actions_to_send",
"=",
"_process_policy_eval_results",
"(",
"to_eval",
",",
"eval_results",
",",
"active_episodes",
",",
"active_envs",
",",
"off_policy_actions",
",",
"policies",
",",
"clip_actions",
")",
"perf_stats",
".",
"processing_time",
"+=",
"time",
".",
"time",
"(",
")",
"-",
"t3",
"# Return computed actions to ready envs. We also send to envs that have",
"# taken off-policy actions; those envs are free to ignore the action.",
"t4",
"=",
"time",
".",
"time",
"(",
")",
"base_env",
".",
"send_actions",
"(",
"actions_to_send",
")",
"perf_stats",
".",
"env_wait_time",
"+=",
"time",
".",
"time",
"(",
")",
"-",
"t4"
] | This implements the common experience collection logic.
Args:
base_env (BaseEnv): env implementing BaseEnv.
extra_batch_callback (fn): function to send extra batch data to.
policies (dict): Map of policy ids to PolicyGraph instances.
policy_mapping_fn (func): Function that maps agent ids to policy ids.
This is called when an agent first enters the environment. The
agent is then "bound" to the returned policy for the episode.
unroll_length (int): Number of episode steps before `SampleBatch` is
yielded. Set to infinity to yield complete episodes.
horizon (int): Horizon of the episode.
preprocessors (dict): Map of policy id to preprocessor for the
observations prior to filtering.
obs_filters (dict): Map of policy id to filter used to process
observations for the policy.
clip_rewards (bool): Whether to clip rewards before postprocessing.
pack (bool): Whether to pack multiple episodes into each batch. This
guarantees batches will be exactly `unroll_length` in size.
clip_actions (bool): Whether to clip actions to the space range.
callbacks (dict): User callbacks to run on episode events.
tf_sess (Session|None): Optional tensorflow session to use for batching
TF policy evaluations.
perf_stats (PerfStats): Record perf stats into this object.
soft_horizon (bool): Calculate rewards but don't reset the
environment when the horizon is hit.
Yields:
rollout (SampleBatch): Object containing state, action, reward,
terminal condition, and other fields as dictated by `policy`. | [
"This",
"implements",
"the",
"common",
"experience",
"collection",
"logic",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/evaluation/sampler.py#L230-L339 |
24,677 | ray-project/ray | python/ray/rllib/evaluation/sampler.py | _do_policy_eval | def _do_policy_eval(tf_sess, to_eval, policies, active_episodes):
"""Call compute actions on observation batches to get next actions.
Returns:
eval_results: dict of policy to compute_action() outputs.
"""
eval_results = {}
if tf_sess:
builder = TFRunBuilder(tf_sess, "policy_eval")
pending_fetches = {}
else:
builder = None
if log_once("compute_actions_input"):
logger.info("Inputs to compute_actions():\n\n{}\n".format(
summarize(to_eval)))
for policy_id, eval_data in to_eval.items():
rnn_in_cols = _to_column_format([t.rnn_state for t in eval_data])
policy = _get_or_raise(policies, policy_id)
if builder and (policy.compute_actions.__code__ is
TFPolicyGraph.compute_actions.__code__):
# TODO(ekl): how can we make info batch available to TF code?
pending_fetches[policy_id] = policy._build_compute_actions(
builder, [t.obs for t in eval_data],
rnn_in_cols,
prev_action_batch=[t.prev_action for t in eval_data],
prev_reward_batch=[t.prev_reward for t in eval_data])
else:
eval_results[policy_id] = policy.compute_actions(
[t.obs for t in eval_data],
rnn_in_cols,
prev_action_batch=[t.prev_action for t in eval_data],
prev_reward_batch=[t.prev_reward for t in eval_data],
info_batch=[t.info for t in eval_data],
episodes=[active_episodes[t.env_id] for t in eval_data])
if builder:
for k, v in pending_fetches.items():
eval_results[k] = builder.get(v)
if log_once("compute_actions_result"):
logger.info("Outputs of compute_actions():\n\n{}\n".format(
summarize(eval_results)))
return eval_results | python | def _do_policy_eval(tf_sess, to_eval, policies, active_episodes):
"""Call compute actions on observation batches to get next actions.
Returns:
eval_results: dict of policy to compute_action() outputs.
"""
eval_results = {}
if tf_sess:
builder = TFRunBuilder(tf_sess, "policy_eval")
pending_fetches = {}
else:
builder = None
if log_once("compute_actions_input"):
logger.info("Inputs to compute_actions():\n\n{}\n".format(
summarize(to_eval)))
for policy_id, eval_data in to_eval.items():
rnn_in_cols = _to_column_format([t.rnn_state for t in eval_data])
policy = _get_or_raise(policies, policy_id)
if builder and (policy.compute_actions.__code__ is
TFPolicyGraph.compute_actions.__code__):
# TODO(ekl): how can we make info batch available to TF code?
pending_fetches[policy_id] = policy._build_compute_actions(
builder, [t.obs for t in eval_data],
rnn_in_cols,
prev_action_batch=[t.prev_action for t in eval_data],
prev_reward_batch=[t.prev_reward for t in eval_data])
else:
eval_results[policy_id] = policy.compute_actions(
[t.obs for t in eval_data],
rnn_in_cols,
prev_action_batch=[t.prev_action for t in eval_data],
prev_reward_batch=[t.prev_reward for t in eval_data],
info_batch=[t.info for t in eval_data],
episodes=[active_episodes[t.env_id] for t in eval_data])
if builder:
for k, v in pending_fetches.items():
eval_results[k] = builder.get(v)
if log_once("compute_actions_result"):
logger.info("Outputs of compute_actions():\n\n{}\n".format(
summarize(eval_results)))
return eval_results | [
"def",
"_do_policy_eval",
"(",
"tf_sess",
",",
"to_eval",
",",
"policies",
",",
"active_episodes",
")",
":",
"eval_results",
"=",
"{",
"}",
"if",
"tf_sess",
":",
"builder",
"=",
"TFRunBuilder",
"(",
"tf_sess",
",",
"\"policy_eval\"",
")",
"pending_fetches",
"=",
"{",
"}",
"else",
":",
"builder",
"=",
"None",
"if",
"log_once",
"(",
"\"compute_actions_input\"",
")",
":",
"logger",
".",
"info",
"(",
"\"Inputs to compute_actions():\\n\\n{}\\n\"",
".",
"format",
"(",
"summarize",
"(",
"to_eval",
")",
")",
")",
"for",
"policy_id",
",",
"eval_data",
"in",
"to_eval",
".",
"items",
"(",
")",
":",
"rnn_in_cols",
"=",
"_to_column_format",
"(",
"[",
"t",
".",
"rnn_state",
"for",
"t",
"in",
"eval_data",
"]",
")",
"policy",
"=",
"_get_or_raise",
"(",
"policies",
",",
"policy_id",
")",
"if",
"builder",
"and",
"(",
"policy",
".",
"compute_actions",
".",
"__code__",
"is",
"TFPolicyGraph",
".",
"compute_actions",
".",
"__code__",
")",
":",
"# TODO(ekl): how can we make info batch available to TF code?",
"pending_fetches",
"[",
"policy_id",
"]",
"=",
"policy",
".",
"_build_compute_actions",
"(",
"builder",
",",
"[",
"t",
".",
"obs",
"for",
"t",
"in",
"eval_data",
"]",
",",
"rnn_in_cols",
",",
"prev_action_batch",
"=",
"[",
"t",
".",
"prev_action",
"for",
"t",
"in",
"eval_data",
"]",
",",
"prev_reward_batch",
"=",
"[",
"t",
".",
"prev_reward",
"for",
"t",
"in",
"eval_data",
"]",
")",
"else",
":",
"eval_results",
"[",
"policy_id",
"]",
"=",
"policy",
".",
"compute_actions",
"(",
"[",
"t",
".",
"obs",
"for",
"t",
"in",
"eval_data",
"]",
",",
"rnn_in_cols",
",",
"prev_action_batch",
"=",
"[",
"t",
".",
"prev_action",
"for",
"t",
"in",
"eval_data",
"]",
",",
"prev_reward_batch",
"=",
"[",
"t",
".",
"prev_reward",
"for",
"t",
"in",
"eval_data",
"]",
",",
"info_batch",
"=",
"[",
"t",
".",
"info",
"for",
"t",
"in",
"eval_data",
"]",
",",
"episodes",
"=",
"[",
"active_episodes",
"[",
"t",
".",
"env_id",
"]",
"for",
"t",
"in",
"eval_data",
"]",
")",
"if",
"builder",
":",
"for",
"k",
",",
"v",
"in",
"pending_fetches",
".",
"items",
"(",
")",
":",
"eval_results",
"[",
"k",
"]",
"=",
"builder",
".",
"get",
"(",
"v",
")",
"if",
"log_once",
"(",
"\"compute_actions_result\"",
")",
":",
"logger",
".",
"info",
"(",
"\"Outputs of compute_actions():\\n\\n{}\\n\"",
".",
"format",
"(",
"summarize",
"(",
"eval_results",
")",
")",
")",
"return",
"eval_results"
] | Call compute actions on observation batches to get next actions.
Returns:
eval_results: dict of policy to compute_action() outputs. | [
"Call",
"compute",
"actions",
"on",
"observation",
"batches",
"to",
"get",
"next",
"actions",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/evaluation/sampler.py#L508-L554 |
24,678 | ray-project/ray | python/ray/rllib/evaluation/sampler.py | _process_policy_eval_results | def _process_policy_eval_results(to_eval, eval_results, active_episodes,
active_envs, off_policy_actions, policies,
clip_actions):
"""Process the output of policy neural network evaluation.
Records policy evaluation results into the given episode objects and
returns replies to send back to agents in the env.
Returns:
actions_to_send: nested dict of env id -> agent id -> agent replies.
"""
actions_to_send = defaultdict(dict)
for env_id in active_envs:
actions_to_send[env_id] = {} # at minimum send empty dict
for policy_id, eval_data in to_eval.items():
rnn_in_cols = _to_column_format([t.rnn_state for t in eval_data])
actions, rnn_out_cols, pi_info_cols = eval_results[policy_id]
if len(rnn_in_cols) != len(rnn_out_cols):
raise ValueError("Length of RNN in did not match RNN out, got: "
"{} vs {}".format(rnn_in_cols, rnn_out_cols))
# Add RNN state info
for f_i, column in enumerate(rnn_in_cols):
pi_info_cols["state_in_{}".format(f_i)] = column
for f_i, column in enumerate(rnn_out_cols):
pi_info_cols["state_out_{}".format(f_i)] = column
# Save output rows
actions = _unbatch_tuple_actions(actions)
policy = _get_or_raise(policies, policy_id)
for i, action in enumerate(actions):
env_id = eval_data[i].env_id
agent_id = eval_data[i].agent_id
if clip_actions:
actions_to_send[env_id][agent_id] = clip_action(
action, policy.action_space)
else:
actions_to_send[env_id][agent_id] = action
episode = active_episodes[env_id]
episode._set_rnn_state(agent_id, [c[i] for c in rnn_out_cols])
episode._set_last_pi_info(
agent_id, {k: v[i]
for k, v in pi_info_cols.items()})
if env_id in off_policy_actions and \
agent_id in off_policy_actions[env_id]:
episode._set_last_action(agent_id,
off_policy_actions[env_id][agent_id])
else:
episode._set_last_action(agent_id, action)
return actions_to_send | python | def _process_policy_eval_results(to_eval, eval_results, active_episodes,
active_envs, off_policy_actions, policies,
clip_actions):
"""Process the output of policy neural network evaluation.
Records policy evaluation results into the given episode objects and
returns replies to send back to agents in the env.
Returns:
actions_to_send: nested dict of env id -> agent id -> agent replies.
"""
actions_to_send = defaultdict(dict)
for env_id in active_envs:
actions_to_send[env_id] = {} # at minimum send empty dict
for policy_id, eval_data in to_eval.items():
rnn_in_cols = _to_column_format([t.rnn_state for t in eval_data])
actions, rnn_out_cols, pi_info_cols = eval_results[policy_id]
if len(rnn_in_cols) != len(rnn_out_cols):
raise ValueError("Length of RNN in did not match RNN out, got: "
"{} vs {}".format(rnn_in_cols, rnn_out_cols))
# Add RNN state info
for f_i, column in enumerate(rnn_in_cols):
pi_info_cols["state_in_{}".format(f_i)] = column
for f_i, column in enumerate(rnn_out_cols):
pi_info_cols["state_out_{}".format(f_i)] = column
# Save output rows
actions = _unbatch_tuple_actions(actions)
policy = _get_or_raise(policies, policy_id)
for i, action in enumerate(actions):
env_id = eval_data[i].env_id
agent_id = eval_data[i].agent_id
if clip_actions:
actions_to_send[env_id][agent_id] = clip_action(
action, policy.action_space)
else:
actions_to_send[env_id][agent_id] = action
episode = active_episodes[env_id]
episode._set_rnn_state(agent_id, [c[i] for c in rnn_out_cols])
episode._set_last_pi_info(
agent_id, {k: v[i]
for k, v in pi_info_cols.items()})
if env_id in off_policy_actions and \
agent_id in off_policy_actions[env_id]:
episode._set_last_action(agent_id,
off_policy_actions[env_id][agent_id])
else:
episode._set_last_action(agent_id, action)
return actions_to_send | [
"def",
"_process_policy_eval_results",
"(",
"to_eval",
",",
"eval_results",
",",
"active_episodes",
",",
"active_envs",
",",
"off_policy_actions",
",",
"policies",
",",
"clip_actions",
")",
":",
"actions_to_send",
"=",
"defaultdict",
"(",
"dict",
")",
"for",
"env_id",
"in",
"active_envs",
":",
"actions_to_send",
"[",
"env_id",
"]",
"=",
"{",
"}",
"# at minimum send empty dict",
"for",
"policy_id",
",",
"eval_data",
"in",
"to_eval",
".",
"items",
"(",
")",
":",
"rnn_in_cols",
"=",
"_to_column_format",
"(",
"[",
"t",
".",
"rnn_state",
"for",
"t",
"in",
"eval_data",
"]",
")",
"actions",
",",
"rnn_out_cols",
",",
"pi_info_cols",
"=",
"eval_results",
"[",
"policy_id",
"]",
"if",
"len",
"(",
"rnn_in_cols",
")",
"!=",
"len",
"(",
"rnn_out_cols",
")",
":",
"raise",
"ValueError",
"(",
"\"Length of RNN in did not match RNN out, got: \"",
"\"{} vs {}\"",
".",
"format",
"(",
"rnn_in_cols",
",",
"rnn_out_cols",
")",
")",
"# Add RNN state info",
"for",
"f_i",
",",
"column",
"in",
"enumerate",
"(",
"rnn_in_cols",
")",
":",
"pi_info_cols",
"[",
"\"state_in_{}\"",
".",
"format",
"(",
"f_i",
")",
"]",
"=",
"column",
"for",
"f_i",
",",
"column",
"in",
"enumerate",
"(",
"rnn_out_cols",
")",
":",
"pi_info_cols",
"[",
"\"state_out_{}\"",
".",
"format",
"(",
"f_i",
")",
"]",
"=",
"column",
"# Save output rows",
"actions",
"=",
"_unbatch_tuple_actions",
"(",
"actions",
")",
"policy",
"=",
"_get_or_raise",
"(",
"policies",
",",
"policy_id",
")",
"for",
"i",
",",
"action",
"in",
"enumerate",
"(",
"actions",
")",
":",
"env_id",
"=",
"eval_data",
"[",
"i",
"]",
".",
"env_id",
"agent_id",
"=",
"eval_data",
"[",
"i",
"]",
".",
"agent_id",
"if",
"clip_actions",
":",
"actions_to_send",
"[",
"env_id",
"]",
"[",
"agent_id",
"]",
"=",
"clip_action",
"(",
"action",
",",
"policy",
".",
"action_space",
")",
"else",
":",
"actions_to_send",
"[",
"env_id",
"]",
"[",
"agent_id",
"]",
"=",
"action",
"episode",
"=",
"active_episodes",
"[",
"env_id",
"]",
"episode",
".",
"_set_rnn_state",
"(",
"agent_id",
",",
"[",
"c",
"[",
"i",
"]",
"for",
"c",
"in",
"rnn_out_cols",
"]",
")",
"episode",
".",
"_set_last_pi_info",
"(",
"agent_id",
",",
"{",
"k",
":",
"v",
"[",
"i",
"]",
"for",
"k",
",",
"v",
"in",
"pi_info_cols",
".",
"items",
"(",
")",
"}",
")",
"if",
"env_id",
"in",
"off_policy_actions",
"and",
"agent_id",
"in",
"off_policy_actions",
"[",
"env_id",
"]",
":",
"episode",
".",
"_set_last_action",
"(",
"agent_id",
",",
"off_policy_actions",
"[",
"env_id",
"]",
"[",
"agent_id",
"]",
")",
"else",
":",
"episode",
".",
"_set_last_action",
"(",
"agent_id",
",",
"action",
")",
"return",
"actions_to_send"
] | Process the output of policy neural network evaluation.
Records policy evaluation results into the given episode objects and
returns replies to send back to agents in the env.
Returns:
actions_to_send: nested dict of env id -> agent id -> agent replies. | [
"Process",
"the",
"output",
"of",
"policy",
"neural",
"network",
"evaluation",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/evaluation/sampler.py#L557-L607 |
24,679 | ray-project/ray | python/ray/rllib/evaluation/sampler.py | _fetch_atari_metrics | def _fetch_atari_metrics(base_env):
"""Atari games have multiple logical episodes, one per life.
However for metrics reporting we count full episodes all lives included.
"""
unwrapped = base_env.get_unwrapped()
if not unwrapped:
return None
atari_out = []
for u in unwrapped:
monitor = get_wrapper_by_cls(u, MonitorEnv)
if not monitor:
return None
for eps_rew, eps_len in monitor.next_episode_results():
atari_out.append(RolloutMetrics(eps_len, eps_rew, {}, {}, {}))
return atari_out | python | def _fetch_atari_metrics(base_env):
"""Atari games have multiple logical episodes, one per life.
However for metrics reporting we count full episodes all lives included.
"""
unwrapped = base_env.get_unwrapped()
if not unwrapped:
return None
atari_out = []
for u in unwrapped:
monitor = get_wrapper_by_cls(u, MonitorEnv)
if not monitor:
return None
for eps_rew, eps_len in monitor.next_episode_results():
atari_out.append(RolloutMetrics(eps_len, eps_rew, {}, {}, {}))
return atari_out | [
"def",
"_fetch_atari_metrics",
"(",
"base_env",
")",
":",
"unwrapped",
"=",
"base_env",
".",
"get_unwrapped",
"(",
")",
"if",
"not",
"unwrapped",
":",
"return",
"None",
"atari_out",
"=",
"[",
"]",
"for",
"u",
"in",
"unwrapped",
":",
"monitor",
"=",
"get_wrapper_by_cls",
"(",
"u",
",",
"MonitorEnv",
")",
"if",
"not",
"monitor",
":",
"return",
"None",
"for",
"eps_rew",
",",
"eps_len",
"in",
"monitor",
".",
"next_episode_results",
"(",
")",
":",
"atari_out",
".",
"append",
"(",
"RolloutMetrics",
"(",
"eps_len",
",",
"eps_rew",
",",
"{",
"}",
",",
"{",
"}",
",",
"{",
"}",
")",
")",
"return",
"atari_out"
] | Atari games have multiple logical episodes, one per life.
However for metrics reporting we count full episodes all lives included. | [
"Atari",
"games",
"have",
"multiple",
"logical",
"episodes",
"one",
"per",
"life",
"."
] | 4eade036a0505e244c976f36aaa2d64386b5129b | https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/evaluation/sampler.py#L610-L625 |
24,680 | google/flatbuffers | android/jni/msbuild.py | compare_version | def compare_version(a, b):
"""Compare two version number strings of the form W.X.Y.Z.
The numbers are compared most-significant to least-significant.
For example, 12.345.67.89 > 2.987.88.99.
Args:
a: First version number string to compare
b: Second version number string to compare
Returns:
0 if the numbers are identical, a positive number if 'a' is larger, and
a negative number if 'b' is larger.
"""
aa = string.split(a, ".")
bb = string.split(b, ".")
for i in range(0, 4):
if aa[i] != bb[i]:
return cmp(int(aa[i]), int(bb[i]))
return 0 | python | def compare_version(a, b):
"""Compare two version number strings of the form W.X.Y.Z.
The numbers are compared most-significant to least-significant.
For example, 12.345.67.89 > 2.987.88.99.
Args:
a: First version number string to compare
b: Second version number string to compare
Returns:
0 if the numbers are identical, a positive number if 'a' is larger, and
a negative number if 'b' is larger.
"""
aa = string.split(a, ".")
bb = string.split(b, ".")
for i in range(0, 4):
if aa[i] != bb[i]:
return cmp(int(aa[i]), int(bb[i]))
return 0 | [
"def",
"compare_version",
"(",
"a",
",",
"b",
")",
":",
"aa",
"=",
"string",
".",
"split",
"(",
"a",
",",
"\".\"",
")",
"bb",
"=",
"string",
".",
"split",
"(",
"b",
",",
"\".\"",
")",
"for",
"i",
"in",
"range",
"(",
"0",
",",
"4",
")",
":",
"if",
"aa",
"[",
"i",
"]",
"!=",
"bb",
"[",
"i",
"]",
":",
"return",
"cmp",
"(",
"int",
"(",
"aa",
"[",
"i",
"]",
")",
",",
"int",
"(",
"bb",
"[",
"i",
"]",
")",
")",
"return",
"0"
] | Compare two version number strings of the form W.X.Y.Z.
The numbers are compared most-significant to least-significant.
For example, 12.345.67.89 > 2.987.88.99.
Args:
a: First version number string to compare
b: Second version number string to compare
Returns:
0 if the numbers are identical, a positive number if 'a' is larger, and
a negative number if 'b' is larger. | [
"Compare",
"two",
"version",
"number",
"strings",
"of",
"the",
"form",
"W",
".",
"X",
".",
"Y",
".",
"Z",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/android/jni/msbuild.py#L37-L56 |
24,681 | google/flatbuffers | conanfile.py | FlatbuffersConan.configure_cmake | def configure_cmake(self):
"""Create CMake instance and execute configure step
"""
cmake = CMake(self)
cmake.definitions["FLATBUFFERS_BUILD_TESTS"] = False
cmake.definitions["FLATBUFFERS_BUILD_SHAREDLIB"] = self.options.shared
cmake.definitions["FLATBUFFERS_BUILD_FLATLIB"] = not self.options.shared
cmake.configure()
return cmake | python | def configure_cmake(self):
"""Create CMake instance and execute configure step
"""
cmake = CMake(self)
cmake.definitions["FLATBUFFERS_BUILD_TESTS"] = False
cmake.definitions["FLATBUFFERS_BUILD_SHAREDLIB"] = self.options.shared
cmake.definitions["FLATBUFFERS_BUILD_FLATLIB"] = not self.options.shared
cmake.configure()
return cmake | [
"def",
"configure_cmake",
"(",
"self",
")",
":",
"cmake",
"=",
"CMake",
"(",
"self",
")",
"cmake",
".",
"definitions",
"[",
"\"FLATBUFFERS_BUILD_TESTS\"",
"]",
"=",
"False",
"cmake",
".",
"definitions",
"[",
"\"FLATBUFFERS_BUILD_SHAREDLIB\"",
"]",
"=",
"self",
".",
"options",
".",
"shared",
"cmake",
".",
"definitions",
"[",
"\"FLATBUFFERS_BUILD_FLATLIB\"",
"]",
"=",
"not",
"self",
".",
"options",
".",
"shared",
"cmake",
".",
"configure",
"(",
")",
"return",
"cmake"
] | Create CMake instance and execute configure step | [
"Create",
"CMake",
"instance",
"and",
"execute",
"configure",
"step"
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/conanfile.py#L38-L46 |
24,682 | google/flatbuffers | conanfile.py | FlatbuffersConan.package | def package(self):
"""Copy Flatbuffers' artifacts to package folder
"""
cmake = self.configure_cmake()
cmake.install()
self.copy(pattern="LICENSE.txt", dst="licenses")
self.copy(pattern="FindFlatBuffers.cmake", dst=os.path.join("lib", "cmake", "flatbuffers"), src="CMake")
self.copy(pattern="flathash*", dst="bin", src="bin")
self.copy(pattern="flatc*", dst="bin", src="bin")
if self.settings.os == "Windows" and self.options.shared:
if self.settings.compiler == "Visual Studio":
shutil.move(os.path.join(self.package_folder, "lib", "%s.dll" % self.name),
os.path.join(self.package_folder, "bin", "%s.dll" % self.name))
elif self.settings.compiler == "gcc":
shutil.move(os.path.join(self.package_folder, "lib", "lib%s.dll" % self.name),
os.path.join(self.package_folder, "bin", "lib%s.dll" % self.name)) | python | def package(self):
"""Copy Flatbuffers' artifacts to package folder
"""
cmake = self.configure_cmake()
cmake.install()
self.copy(pattern="LICENSE.txt", dst="licenses")
self.copy(pattern="FindFlatBuffers.cmake", dst=os.path.join("lib", "cmake", "flatbuffers"), src="CMake")
self.copy(pattern="flathash*", dst="bin", src="bin")
self.copy(pattern="flatc*", dst="bin", src="bin")
if self.settings.os == "Windows" and self.options.shared:
if self.settings.compiler == "Visual Studio":
shutil.move(os.path.join(self.package_folder, "lib", "%s.dll" % self.name),
os.path.join(self.package_folder, "bin", "%s.dll" % self.name))
elif self.settings.compiler == "gcc":
shutil.move(os.path.join(self.package_folder, "lib", "lib%s.dll" % self.name),
os.path.join(self.package_folder, "bin", "lib%s.dll" % self.name)) | [
"def",
"package",
"(",
"self",
")",
":",
"cmake",
"=",
"self",
".",
"configure_cmake",
"(",
")",
"cmake",
".",
"install",
"(",
")",
"self",
".",
"copy",
"(",
"pattern",
"=",
"\"LICENSE.txt\"",
",",
"dst",
"=",
"\"licenses\"",
")",
"self",
".",
"copy",
"(",
"pattern",
"=",
"\"FindFlatBuffers.cmake\"",
",",
"dst",
"=",
"os",
".",
"path",
".",
"join",
"(",
"\"lib\"",
",",
"\"cmake\"",
",",
"\"flatbuffers\"",
")",
",",
"src",
"=",
"\"CMake\"",
")",
"self",
".",
"copy",
"(",
"pattern",
"=",
"\"flathash*\"",
",",
"dst",
"=",
"\"bin\"",
",",
"src",
"=",
"\"bin\"",
")",
"self",
".",
"copy",
"(",
"pattern",
"=",
"\"flatc*\"",
",",
"dst",
"=",
"\"bin\"",
",",
"src",
"=",
"\"bin\"",
")",
"if",
"self",
".",
"settings",
".",
"os",
"==",
"\"Windows\"",
"and",
"self",
".",
"options",
".",
"shared",
":",
"if",
"self",
".",
"settings",
".",
"compiler",
"==",
"\"Visual Studio\"",
":",
"shutil",
".",
"move",
"(",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"package_folder",
",",
"\"lib\"",
",",
"\"%s.dll\"",
"%",
"self",
".",
"name",
")",
",",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"package_folder",
",",
"\"bin\"",
",",
"\"%s.dll\"",
"%",
"self",
".",
"name",
")",
")",
"elif",
"self",
".",
"settings",
".",
"compiler",
"==",
"\"gcc\"",
":",
"shutil",
".",
"move",
"(",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"package_folder",
",",
"\"lib\"",
",",
"\"lib%s.dll\"",
"%",
"self",
".",
"name",
")",
",",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"package_folder",
",",
"\"bin\"",
",",
"\"lib%s.dll\"",
"%",
"self",
".",
"name",
")",
")"
] | Copy Flatbuffers' artifacts to package folder | [
"Copy",
"Flatbuffers",
"artifacts",
"to",
"package",
"folder"
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/conanfile.py#L54-L69 |
24,683 | google/flatbuffers | conanfile.py | FlatbuffersConan.package_info | def package_info(self):
"""Collect built libraries names and solve flatc path.
"""
self.cpp_info.libs = tools.collect_libs(self)
self.user_info.flatc = os.path.join(self.package_folder, "bin", "flatc") | python | def package_info(self):
"""Collect built libraries names and solve flatc path.
"""
self.cpp_info.libs = tools.collect_libs(self)
self.user_info.flatc = os.path.join(self.package_folder, "bin", "flatc") | [
"def",
"package_info",
"(",
"self",
")",
":",
"self",
".",
"cpp_info",
".",
"libs",
"=",
"tools",
".",
"collect_libs",
"(",
"self",
")",
"self",
".",
"user_info",
".",
"flatc",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"package_folder",
",",
"\"bin\"",
",",
"\"flatc\"",
")"
] | Collect built libraries names and solve flatc path. | [
"Collect",
"built",
"libraries",
"names",
"and",
"solve",
"flatc",
"path",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/conanfile.py#L71-L75 |
24,684 | google/flatbuffers | python/flatbuffers/table.py | Table.Offset | def Offset(self, vtableOffset):
"""Offset provides access into the Table's vtable.
Deprecated fields are ignored by checking the vtable's length."""
vtable = self.Pos - self.Get(N.SOffsetTFlags, self.Pos)
vtableEnd = self.Get(N.VOffsetTFlags, vtable)
if vtableOffset < vtableEnd:
return self.Get(N.VOffsetTFlags, vtable + vtableOffset)
return 0 | python | def Offset(self, vtableOffset):
"""Offset provides access into the Table's vtable.
Deprecated fields are ignored by checking the vtable's length."""
vtable = self.Pos - self.Get(N.SOffsetTFlags, self.Pos)
vtableEnd = self.Get(N.VOffsetTFlags, vtable)
if vtableOffset < vtableEnd:
return self.Get(N.VOffsetTFlags, vtable + vtableOffset)
return 0 | [
"def",
"Offset",
"(",
"self",
",",
"vtableOffset",
")",
":",
"vtable",
"=",
"self",
".",
"Pos",
"-",
"self",
".",
"Get",
"(",
"N",
".",
"SOffsetTFlags",
",",
"self",
".",
"Pos",
")",
"vtableEnd",
"=",
"self",
".",
"Get",
"(",
"N",
".",
"VOffsetTFlags",
",",
"vtable",
")",
"if",
"vtableOffset",
"<",
"vtableEnd",
":",
"return",
"self",
".",
"Get",
"(",
"N",
".",
"VOffsetTFlags",
",",
"vtable",
"+",
"vtableOffset",
")",
"return",
"0"
] | Offset provides access into the Table's vtable.
Deprecated fields are ignored by checking the vtable's length. | [
"Offset",
"provides",
"access",
"into",
"the",
"Table",
"s",
"vtable",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/table.py#L32-L41 |
24,685 | google/flatbuffers | python/flatbuffers/table.py | Table.Indirect | def Indirect(self, off):
"""Indirect retrieves the relative offset stored at `offset`."""
N.enforce_number(off, N.UOffsetTFlags)
return off + encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off) | python | def Indirect(self, off):
"""Indirect retrieves the relative offset stored at `offset`."""
N.enforce_number(off, N.UOffsetTFlags)
return off + encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off) | [
"def",
"Indirect",
"(",
"self",
",",
"off",
")",
":",
"N",
".",
"enforce_number",
"(",
"off",
",",
"N",
".",
"UOffsetTFlags",
")",
"return",
"off",
"+",
"encode",
".",
"Get",
"(",
"N",
".",
"UOffsetTFlags",
".",
"packer_type",
",",
"self",
".",
"Bytes",
",",
"off",
")"
] | Indirect retrieves the relative offset stored at `offset`. | [
"Indirect",
"retrieves",
"the",
"relative",
"offset",
"stored",
"at",
"offset",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/table.py#L43-L46 |
24,686 | google/flatbuffers | python/flatbuffers/table.py | Table.String | def String(self, off):
"""String gets a string from data stored inside the flatbuffer."""
N.enforce_number(off, N.UOffsetTFlags)
off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
start = off + N.UOffsetTFlags.bytewidth
length = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
return bytes(self.Bytes[start:start+length]) | python | def String(self, off):
"""String gets a string from data stored inside the flatbuffer."""
N.enforce_number(off, N.UOffsetTFlags)
off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
start = off + N.UOffsetTFlags.bytewidth
length = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
return bytes(self.Bytes[start:start+length]) | [
"def",
"String",
"(",
"self",
",",
"off",
")",
":",
"N",
".",
"enforce_number",
"(",
"off",
",",
"N",
".",
"UOffsetTFlags",
")",
"off",
"+=",
"encode",
".",
"Get",
"(",
"N",
".",
"UOffsetTFlags",
".",
"packer_type",
",",
"self",
".",
"Bytes",
",",
"off",
")",
"start",
"=",
"off",
"+",
"N",
".",
"UOffsetTFlags",
".",
"bytewidth",
"length",
"=",
"encode",
".",
"Get",
"(",
"N",
".",
"UOffsetTFlags",
".",
"packer_type",
",",
"self",
".",
"Bytes",
",",
"off",
")",
"return",
"bytes",
"(",
"self",
".",
"Bytes",
"[",
"start",
":",
"start",
"+",
"length",
"]",
")"
] | String gets a string from data stored inside the flatbuffer. | [
"String",
"gets",
"a",
"string",
"from",
"data",
"stored",
"inside",
"the",
"flatbuffer",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/table.py#L48-L54 |
24,687 | google/flatbuffers | python/flatbuffers/table.py | Table.VectorLen | def VectorLen(self, off):
"""VectorLen retrieves the length of the vector whose offset is stored
at "off" in this object."""
N.enforce_number(off, N.UOffsetTFlags)
off += self.Pos
off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
ret = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
return ret | python | def VectorLen(self, off):
"""VectorLen retrieves the length of the vector whose offset is stored
at "off" in this object."""
N.enforce_number(off, N.UOffsetTFlags)
off += self.Pos
off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
ret = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
return ret | [
"def",
"VectorLen",
"(",
"self",
",",
"off",
")",
":",
"N",
".",
"enforce_number",
"(",
"off",
",",
"N",
".",
"UOffsetTFlags",
")",
"off",
"+=",
"self",
".",
"Pos",
"off",
"+=",
"encode",
".",
"Get",
"(",
"N",
".",
"UOffsetTFlags",
".",
"packer_type",
",",
"self",
".",
"Bytes",
",",
"off",
")",
"ret",
"=",
"encode",
".",
"Get",
"(",
"N",
".",
"UOffsetTFlags",
".",
"packer_type",
",",
"self",
".",
"Bytes",
",",
"off",
")",
"return",
"ret"
] | VectorLen retrieves the length of the vector whose offset is stored
at "off" in this object. | [
"VectorLen",
"retrieves",
"the",
"length",
"of",
"the",
"vector",
"whose",
"offset",
"is",
"stored",
"at",
"off",
"in",
"this",
"object",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/table.py#L56-L64 |
24,688 | google/flatbuffers | python/flatbuffers/table.py | Table.Vector | def Vector(self, off):
"""Vector retrieves the start of data of the vector whose offset is
stored at "off" in this object."""
N.enforce_number(off, N.UOffsetTFlags)
off += self.Pos
x = off + self.Get(N.UOffsetTFlags, off)
# data starts after metadata containing the vector length
x += N.UOffsetTFlags.bytewidth
return x | python | def Vector(self, off):
"""Vector retrieves the start of data of the vector whose offset is
stored at "off" in this object."""
N.enforce_number(off, N.UOffsetTFlags)
off += self.Pos
x = off + self.Get(N.UOffsetTFlags, off)
# data starts after metadata containing the vector length
x += N.UOffsetTFlags.bytewidth
return x | [
"def",
"Vector",
"(",
"self",
",",
"off",
")",
":",
"N",
".",
"enforce_number",
"(",
"off",
",",
"N",
".",
"UOffsetTFlags",
")",
"off",
"+=",
"self",
".",
"Pos",
"x",
"=",
"off",
"+",
"self",
".",
"Get",
"(",
"N",
".",
"UOffsetTFlags",
",",
"off",
")",
"# data starts after metadata containing the vector length",
"x",
"+=",
"N",
".",
"UOffsetTFlags",
".",
"bytewidth",
"return",
"x"
] | Vector retrieves the start of data of the vector whose offset is
stored at "off" in this object. | [
"Vector",
"retrieves",
"the",
"start",
"of",
"data",
"of",
"the",
"vector",
"whose",
"offset",
"is",
"stored",
"at",
"off",
"in",
"this",
"object",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/table.py#L66-L75 |
24,689 | google/flatbuffers | python/flatbuffers/table.py | Table.Union | def Union(self, t2, off):
"""Union initializes any Table-derived type to point to the union at
the given offset."""
assert type(t2) is Table
N.enforce_number(off, N.UOffsetTFlags)
off += self.Pos
t2.Pos = off + self.Get(N.UOffsetTFlags, off)
t2.Bytes = self.Bytes | python | def Union(self, t2, off):
"""Union initializes any Table-derived type to point to the union at
the given offset."""
assert type(t2) is Table
N.enforce_number(off, N.UOffsetTFlags)
off += self.Pos
t2.Pos = off + self.Get(N.UOffsetTFlags, off)
t2.Bytes = self.Bytes | [
"def",
"Union",
"(",
"self",
",",
"t2",
",",
"off",
")",
":",
"assert",
"type",
"(",
"t2",
")",
"is",
"Table",
"N",
".",
"enforce_number",
"(",
"off",
",",
"N",
".",
"UOffsetTFlags",
")",
"off",
"+=",
"self",
".",
"Pos",
"t2",
".",
"Pos",
"=",
"off",
"+",
"self",
".",
"Get",
"(",
"N",
".",
"UOffsetTFlags",
",",
"off",
")",
"t2",
".",
"Bytes",
"=",
"self",
".",
"Bytes"
] | Union initializes any Table-derived type to point to the union at
the given offset. | [
"Union",
"initializes",
"any",
"Table",
"-",
"derived",
"type",
"to",
"point",
"to",
"the",
"union",
"at",
"the",
"given",
"offset",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/table.py#L77-L85 |
24,690 | google/flatbuffers | python/flatbuffers/table.py | Table.Get | def Get(self, flags, off):
"""
Get retrieves a value of the type specified by `flags` at the
given offset.
"""
N.enforce_number(off, N.UOffsetTFlags)
return flags.py_type(encode.Get(flags.packer_type, self.Bytes, off)) | python | def Get(self, flags, off):
"""
Get retrieves a value of the type specified by `flags` at the
given offset.
"""
N.enforce_number(off, N.UOffsetTFlags)
return flags.py_type(encode.Get(flags.packer_type, self.Bytes, off)) | [
"def",
"Get",
"(",
"self",
",",
"flags",
",",
"off",
")",
":",
"N",
".",
"enforce_number",
"(",
"off",
",",
"N",
".",
"UOffsetTFlags",
")",
"return",
"flags",
".",
"py_type",
"(",
"encode",
".",
"Get",
"(",
"flags",
".",
"packer_type",
",",
"self",
".",
"Bytes",
",",
"off",
")",
")"
] | Get retrieves a value of the type specified by `flags` at the
given offset. | [
"Get",
"retrieves",
"a",
"value",
"of",
"the",
"type",
"specified",
"by",
"flags",
"at",
"the",
"given",
"offset",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/table.py#L87-L93 |
24,691 | google/flatbuffers | python/flatbuffers/table.py | Table.GetVOffsetTSlot | def GetVOffsetTSlot(self, slot, d):
"""
GetVOffsetTSlot retrieves the VOffsetT that the given vtable location
points to. If the vtable value is zero, the default value `d`
will be returned.
"""
N.enforce_number(slot, N.VOffsetTFlags)
N.enforce_number(d, N.VOffsetTFlags)
off = self.Offset(slot)
if off == 0:
return d
return off | python | def GetVOffsetTSlot(self, slot, d):
"""
GetVOffsetTSlot retrieves the VOffsetT that the given vtable location
points to. If the vtable value is zero, the default value `d`
will be returned.
"""
N.enforce_number(slot, N.VOffsetTFlags)
N.enforce_number(d, N.VOffsetTFlags)
off = self.Offset(slot)
if off == 0:
return d
return off | [
"def",
"GetVOffsetTSlot",
"(",
"self",
",",
"slot",
",",
"d",
")",
":",
"N",
".",
"enforce_number",
"(",
"slot",
",",
"N",
".",
"VOffsetTFlags",
")",
"N",
".",
"enforce_number",
"(",
"d",
",",
"N",
".",
"VOffsetTFlags",
")",
"off",
"=",
"self",
".",
"Offset",
"(",
"slot",
")",
"if",
"off",
"==",
"0",
":",
"return",
"d",
"return",
"off"
] | GetVOffsetTSlot retrieves the VOffsetT that the given vtable location
points to. If the vtable value is zero, the default value `d`
will be returned. | [
"GetVOffsetTSlot",
"retrieves",
"the",
"VOffsetT",
"that",
"the",
"given",
"vtable",
"location",
"points",
"to",
".",
"If",
"the",
"vtable",
"value",
"is",
"zero",
"the",
"default",
"value",
"d",
"will",
"be",
"returned",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/table.py#L116-L129 |
24,692 | google/flatbuffers | android/jni/run_flatc.py | main | def main():
"""Script that finds and runs flatc built from source."""
if len(sys.argv) < 2:
sys.stderr.write('Usage: run_flatc.py flatbuffers_dir [flatc_args]\n')
return 1
cwd = os.getcwd()
flatc = ''
flatbuffers_dir = sys.argv[1]
for path in FLATC_SEARCH_PATHS:
current = os.path.join(flatbuffers_dir, path,
'flatc' + EXECUTABLE_EXTENSION)
if os.path.exists(current):
flatc = current
break
if not flatc:
sys.stderr.write('flatc not found\n')
return 1
command = [flatc] + sys.argv[2:]
return subprocess.call(command) | python | def main():
"""Script that finds and runs flatc built from source."""
if len(sys.argv) < 2:
sys.stderr.write('Usage: run_flatc.py flatbuffers_dir [flatc_args]\n')
return 1
cwd = os.getcwd()
flatc = ''
flatbuffers_dir = sys.argv[1]
for path in FLATC_SEARCH_PATHS:
current = os.path.join(flatbuffers_dir, path,
'flatc' + EXECUTABLE_EXTENSION)
if os.path.exists(current):
flatc = current
break
if not flatc:
sys.stderr.write('flatc not found\n')
return 1
command = [flatc] + sys.argv[2:]
return subprocess.call(command) | [
"def",
"main",
"(",
")",
":",
"if",
"len",
"(",
"sys",
".",
"argv",
")",
"<",
"2",
":",
"sys",
".",
"stderr",
".",
"write",
"(",
"'Usage: run_flatc.py flatbuffers_dir [flatc_args]\\n'",
")",
"return",
"1",
"cwd",
"=",
"os",
".",
"getcwd",
"(",
")",
"flatc",
"=",
"''",
"flatbuffers_dir",
"=",
"sys",
".",
"argv",
"[",
"1",
"]",
"for",
"path",
"in",
"FLATC_SEARCH_PATHS",
":",
"current",
"=",
"os",
".",
"path",
".",
"join",
"(",
"flatbuffers_dir",
",",
"path",
",",
"'flatc'",
"+",
"EXECUTABLE_EXTENSION",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"current",
")",
":",
"flatc",
"=",
"current",
"break",
"if",
"not",
"flatc",
":",
"sys",
".",
"stderr",
".",
"write",
"(",
"'flatc not found\\n'",
")",
"return",
"1",
"command",
"=",
"[",
"flatc",
"]",
"+",
"sys",
".",
"argv",
"[",
"2",
":",
"]",
"return",
"subprocess",
".",
"call",
"(",
"command",
")"
] | Script that finds and runs flatc built from source. | [
"Script",
"that",
"finds",
"and",
"runs",
"flatc",
"built",
"from",
"source",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/android/jni/run_flatc.py#L25-L43 |
24,693 | google/flatbuffers | python/flatbuffers/compat.py | import_numpy | def import_numpy():
"""
Returns the numpy module if it exists on the system,
otherwise returns None.
"""
try:
imp.find_module('numpy')
numpy_exists = True
except ImportError:
numpy_exists = False
if numpy_exists:
# We do this outside of try/except block in case numpy exists
# but is not installed correctly. We do not want to catch an
# incorrect installation which would manifest as an
# ImportError.
import numpy as np
else:
np = None
return np | python | def import_numpy():
"""
Returns the numpy module if it exists on the system,
otherwise returns None.
"""
try:
imp.find_module('numpy')
numpy_exists = True
except ImportError:
numpy_exists = False
if numpy_exists:
# We do this outside of try/except block in case numpy exists
# but is not installed correctly. We do not want to catch an
# incorrect installation which would manifest as an
# ImportError.
import numpy as np
else:
np = None
return np | [
"def",
"import_numpy",
"(",
")",
":",
"try",
":",
"imp",
".",
"find_module",
"(",
"'numpy'",
")",
"numpy_exists",
"=",
"True",
"except",
"ImportError",
":",
"numpy_exists",
"=",
"False",
"if",
"numpy_exists",
":",
"# We do this outside of try/except block in case numpy exists",
"# but is not installed correctly. We do not want to catch an",
"# incorrect installation which would manifest as an",
"# ImportError.",
"import",
"numpy",
"as",
"np",
"else",
":",
"np",
"=",
"None",
"return",
"np"
] | Returns the numpy module if it exists on the system,
otherwise returns None. | [
"Returns",
"the",
"numpy",
"module",
"if",
"it",
"exists",
"on",
"the",
"system",
"otherwise",
"returns",
"None",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/compat.py#L50-L70 |
24,694 | google/flatbuffers | python/flatbuffers/builder.py | vtableEqual | def vtableEqual(a, objectStart, b):
"""vtableEqual compares an unwritten vtable to a written vtable."""
N.enforce_number(objectStart, N.UOffsetTFlags)
if len(a) * N.VOffsetTFlags.bytewidth != len(b):
return False
for i, elem in enumerate(a):
x = encode.Get(packer.voffset, b, i * N.VOffsetTFlags.bytewidth)
# Skip vtable entries that indicate a default value.
if x == 0 and elem == 0:
pass
else:
y = objectStart - elem
if x != y:
return False
return True | python | def vtableEqual(a, objectStart, b):
"""vtableEqual compares an unwritten vtable to a written vtable."""
N.enforce_number(objectStart, N.UOffsetTFlags)
if len(a) * N.VOffsetTFlags.bytewidth != len(b):
return False
for i, elem in enumerate(a):
x = encode.Get(packer.voffset, b, i * N.VOffsetTFlags.bytewidth)
# Skip vtable entries that indicate a default value.
if x == 0 and elem == 0:
pass
else:
y = objectStart - elem
if x != y:
return False
return True | [
"def",
"vtableEqual",
"(",
"a",
",",
"objectStart",
",",
"b",
")",
":",
"N",
".",
"enforce_number",
"(",
"objectStart",
",",
"N",
".",
"UOffsetTFlags",
")",
"if",
"len",
"(",
"a",
")",
"*",
"N",
".",
"VOffsetTFlags",
".",
"bytewidth",
"!=",
"len",
"(",
"b",
")",
":",
"return",
"False",
"for",
"i",
",",
"elem",
"in",
"enumerate",
"(",
"a",
")",
":",
"x",
"=",
"encode",
".",
"Get",
"(",
"packer",
".",
"voffset",
",",
"b",
",",
"i",
"*",
"N",
".",
"VOffsetTFlags",
".",
"bytewidth",
")",
"# Skip vtable entries that indicate a default value.",
"if",
"x",
"==",
"0",
"and",
"elem",
"==",
"0",
":",
"pass",
"else",
":",
"y",
"=",
"objectStart",
"-",
"elem",
"if",
"x",
"!=",
"y",
":",
"return",
"False",
"return",
"True"
] | vtableEqual compares an unwritten vtable to a written vtable. | [
"vtableEqual",
"compares",
"an",
"unwritten",
"vtable",
"to",
"a",
"written",
"vtable",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/builder.py#L735-L753 |
24,695 | google/flatbuffers | python/flatbuffers/builder.py | Builder.StartObject | def StartObject(self, numfields):
"""StartObject initializes bookkeeping for writing a new object."""
self.assertNotNested()
# use 32-bit offsets so that arithmetic doesn't overflow.
self.current_vtable = [0 for _ in range_func(numfields)]
self.objectEnd = self.Offset()
self.nested = True | python | def StartObject(self, numfields):
"""StartObject initializes bookkeeping for writing a new object."""
self.assertNotNested()
# use 32-bit offsets so that arithmetic doesn't overflow.
self.current_vtable = [0 for _ in range_func(numfields)]
self.objectEnd = self.Offset()
self.nested = True | [
"def",
"StartObject",
"(",
"self",
",",
"numfields",
")",
":",
"self",
".",
"assertNotNested",
"(",
")",
"# use 32-bit offsets so that arithmetic doesn't overflow.",
"self",
".",
"current_vtable",
"=",
"[",
"0",
"for",
"_",
"in",
"range_func",
"(",
"numfields",
")",
"]",
"self",
".",
"objectEnd",
"=",
"self",
".",
"Offset",
"(",
")",
"self",
".",
"nested",
"=",
"True"
] | StartObject initializes bookkeeping for writing a new object. | [
"StartObject",
"initializes",
"bookkeeping",
"for",
"writing",
"a",
"new",
"object",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/builder.py#L156-L164 |
24,696 | google/flatbuffers | python/flatbuffers/builder.py | Builder.WriteVtable | def WriteVtable(self):
"""
WriteVtable serializes the vtable for the current object, if needed.
Before writing out the vtable, this checks pre-existing vtables for
equality to this one. If an equal vtable is found, point the object to
the existing vtable and return.
Because vtable values are sensitive to alignment of object data, not
all logically-equal vtables will be deduplicated.
A vtable has the following format:
<VOffsetT: size of the vtable in bytes, including this value>
<VOffsetT: size of the object in bytes, including the vtable offset>
<VOffsetT: offset for a field> * N, where N is the number of fields
in the schema for this type. Includes deprecated fields.
Thus, a vtable is made of 2 + N elements, each VOffsetT bytes wide.
An object has the following format:
<SOffsetT: offset to this object's vtable (may be negative)>
<byte: data>+
"""
# Prepend a zero scalar to the object. Later in this function we'll
# write an offset here that points to the object's vtable:
self.PrependSOffsetTRelative(0)
objectOffset = self.Offset()
existingVtable = None
# Trim trailing 0 offsets.
while self.current_vtable and self.current_vtable[-1] == 0:
self.current_vtable.pop()
# Search backwards through existing vtables, because similar vtables
# are likely to have been recently appended. See
# BenchmarkVtableDeduplication for a case in which this heuristic
# saves about 30% of the time used in writing objects with duplicate
# tables.
i = len(self.vtables) - 1
while i >= 0:
# Find the other vtable, which is associated with `i`:
vt2Offset = self.vtables[i]
vt2Start = len(self.Bytes) - vt2Offset
vt2Len = encode.Get(packer.voffset, self.Bytes, vt2Start)
metadata = VtableMetadataFields * N.VOffsetTFlags.bytewidth
vt2End = vt2Start + vt2Len
vt2 = self.Bytes[vt2Start+metadata:vt2End]
# Compare the other vtable to the one under consideration.
# If they are equal, store the offset and break:
if vtableEqual(self.current_vtable, objectOffset, vt2):
existingVtable = vt2Offset
break
i -= 1
if existingVtable is None:
# Did not find a vtable, so write this one to the buffer.
# Write out the current vtable in reverse , because
# serialization occurs in last-first order:
i = len(self.current_vtable) - 1
while i >= 0:
off = 0
if self.current_vtable[i] != 0:
# Forward reference to field;
# use 32bit number to ensure no overflow:
off = objectOffset - self.current_vtable[i]
self.PrependVOffsetT(off)
i -= 1
# The two metadata fields are written last.
# First, store the object bytesize:
objectSize = UOffsetTFlags.py_type(objectOffset - self.objectEnd)
self.PrependVOffsetT(VOffsetTFlags.py_type(objectSize))
# Second, store the vtable bytesize:
vBytes = len(self.current_vtable) + VtableMetadataFields
vBytes *= N.VOffsetTFlags.bytewidth
self.PrependVOffsetT(VOffsetTFlags.py_type(vBytes))
# Next, write the offset to the new vtable in the
# already-allocated SOffsetT at the beginning of this object:
objectStart = SOffsetTFlags.py_type(len(self.Bytes) - objectOffset)
encode.Write(packer.soffset, self.Bytes, objectStart,
SOffsetTFlags.py_type(self.Offset() - objectOffset))
# Finally, store this vtable in memory for future
# deduplication:
self.vtables.append(self.Offset())
else:
# Found a duplicate vtable.
objectStart = SOffsetTFlags.py_type(len(self.Bytes) - objectOffset)
self.head = UOffsetTFlags.py_type(objectStart)
# Write the offset to the found vtable in the
# already-allocated SOffsetT at the beginning of this object:
encode.Write(packer.soffset, self.Bytes, self.Head(),
SOffsetTFlags.py_type(existingVtable - objectOffset))
self.current_vtable = None
return objectOffset | python | def WriteVtable(self):
"""
WriteVtable serializes the vtable for the current object, if needed.
Before writing out the vtable, this checks pre-existing vtables for
equality to this one. If an equal vtable is found, point the object to
the existing vtable and return.
Because vtable values are sensitive to alignment of object data, not
all logically-equal vtables will be deduplicated.
A vtable has the following format:
<VOffsetT: size of the vtable in bytes, including this value>
<VOffsetT: size of the object in bytes, including the vtable offset>
<VOffsetT: offset for a field> * N, where N is the number of fields
in the schema for this type. Includes deprecated fields.
Thus, a vtable is made of 2 + N elements, each VOffsetT bytes wide.
An object has the following format:
<SOffsetT: offset to this object's vtable (may be negative)>
<byte: data>+
"""
# Prepend a zero scalar to the object. Later in this function we'll
# write an offset here that points to the object's vtable:
self.PrependSOffsetTRelative(0)
objectOffset = self.Offset()
existingVtable = None
# Trim trailing 0 offsets.
while self.current_vtable and self.current_vtable[-1] == 0:
self.current_vtable.pop()
# Search backwards through existing vtables, because similar vtables
# are likely to have been recently appended. See
# BenchmarkVtableDeduplication for a case in which this heuristic
# saves about 30% of the time used in writing objects with duplicate
# tables.
i = len(self.vtables) - 1
while i >= 0:
# Find the other vtable, which is associated with `i`:
vt2Offset = self.vtables[i]
vt2Start = len(self.Bytes) - vt2Offset
vt2Len = encode.Get(packer.voffset, self.Bytes, vt2Start)
metadata = VtableMetadataFields * N.VOffsetTFlags.bytewidth
vt2End = vt2Start + vt2Len
vt2 = self.Bytes[vt2Start+metadata:vt2End]
# Compare the other vtable to the one under consideration.
# If they are equal, store the offset and break:
if vtableEqual(self.current_vtable, objectOffset, vt2):
existingVtable = vt2Offset
break
i -= 1
if existingVtable is None:
# Did not find a vtable, so write this one to the buffer.
# Write out the current vtable in reverse , because
# serialization occurs in last-first order:
i = len(self.current_vtable) - 1
while i >= 0:
off = 0
if self.current_vtable[i] != 0:
# Forward reference to field;
# use 32bit number to ensure no overflow:
off = objectOffset - self.current_vtable[i]
self.PrependVOffsetT(off)
i -= 1
# The two metadata fields are written last.
# First, store the object bytesize:
objectSize = UOffsetTFlags.py_type(objectOffset - self.objectEnd)
self.PrependVOffsetT(VOffsetTFlags.py_type(objectSize))
# Second, store the vtable bytesize:
vBytes = len(self.current_vtable) + VtableMetadataFields
vBytes *= N.VOffsetTFlags.bytewidth
self.PrependVOffsetT(VOffsetTFlags.py_type(vBytes))
# Next, write the offset to the new vtable in the
# already-allocated SOffsetT at the beginning of this object:
objectStart = SOffsetTFlags.py_type(len(self.Bytes) - objectOffset)
encode.Write(packer.soffset, self.Bytes, objectStart,
SOffsetTFlags.py_type(self.Offset() - objectOffset))
# Finally, store this vtable in memory for future
# deduplication:
self.vtables.append(self.Offset())
else:
# Found a duplicate vtable.
objectStart = SOffsetTFlags.py_type(len(self.Bytes) - objectOffset)
self.head = UOffsetTFlags.py_type(objectStart)
# Write the offset to the found vtable in the
# already-allocated SOffsetT at the beginning of this object:
encode.Write(packer.soffset, self.Bytes, self.Head(),
SOffsetTFlags.py_type(existingVtable - objectOffset))
self.current_vtable = None
return objectOffset | [
"def",
"WriteVtable",
"(",
"self",
")",
":",
"# Prepend a zero scalar to the object. Later in this function we'll",
"# write an offset here that points to the object's vtable:",
"self",
".",
"PrependSOffsetTRelative",
"(",
"0",
")",
"objectOffset",
"=",
"self",
".",
"Offset",
"(",
")",
"existingVtable",
"=",
"None",
"# Trim trailing 0 offsets.",
"while",
"self",
".",
"current_vtable",
"and",
"self",
".",
"current_vtable",
"[",
"-",
"1",
"]",
"==",
"0",
":",
"self",
".",
"current_vtable",
".",
"pop",
"(",
")",
"# Search backwards through existing vtables, because similar vtables",
"# are likely to have been recently appended. See",
"# BenchmarkVtableDeduplication for a case in which this heuristic",
"# saves about 30% of the time used in writing objects with duplicate",
"# tables.",
"i",
"=",
"len",
"(",
"self",
".",
"vtables",
")",
"-",
"1",
"while",
"i",
">=",
"0",
":",
"# Find the other vtable, which is associated with `i`:",
"vt2Offset",
"=",
"self",
".",
"vtables",
"[",
"i",
"]",
"vt2Start",
"=",
"len",
"(",
"self",
".",
"Bytes",
")",
"-",
"vt2Offset",
"vt2Len",
"=",
"encode",
".",
"Get",
"(",
"packer",
".",
"voffset",
",",
"self",
".",
"Bytes",
",",
"vt2Start",
")",
"metadata",
"=",
"VtableMetadataFields",
"*",
"N",
".",
"VOffsetTFlags",
".",
"bytewidth",
"vt2End",
"=",
"vt2Start",
"+",
"vt2Len",
"vt2",
"=",
"self",
".",
"Bytes",
"[",
"vt2Start",
"+",
"metadata",
":",
"vt2End",
"]",
"# Compare the other vtable to the one under consideration.",
"# If they are equal, store the offset and break:",
"if",
"vtableEqual",
"(",
"self",
".",
"current_vtable",
",",
"objectOffset",
",",
"vt2",
")",
":",
"existingVtable",
"=",
"vt2Offset",
"break",
"i",
"-=",
"1",
"if",
"existingVtable",
"is",
"None",
":",
"# Did not find a vtable, so write this one to the buffer.",
"# Write out the current vtable in reverse , because",
"# serialization occurs in last-first order:",
"i",
"=",
"len",
"(",
"self",
".",
"current_vtable",
")",
"-",
"1",
"while",
"i",
">=",
"0",
":",
"off",
"=",
"0",
"if",
"self",
".",
"current_vtable",
"[",
"i",
"]",
"!=",
"0",
":",
"# Forward reference to field;",
"# use 32bit number to ensure no overflow:",
"off",
"=",
"objectOffset",
"-",
"self",
".",
"current_vtable",
"[",
"i",
"]",
"self",
".",
"PrependVOffsetT",
"(",
"off",
")",
"i",
"-=",
"1",
"# The two metadata fields are written last.",
"# First, store the object bytesize:",
"objectSize",
"=",
"UOffsetTFlags",
".",
"py_type",
"(",
"objectOffset",
"-",
"self",
".",
"objectEnd",
")",
"self",
".",
"PrependVOffsetT",
"(",
"VOffsetTFlags",
".",
"py_type",
"(",
"objectSize",
")",
")",
"# Second, store the vtable bytesize:",
"vBytes",
"=",
"len",
"(",
"self",
".",
"current_vtable",
")",
"+",
"VtableMetadataFields",
"vBytes",
"*=",
"N",
".",
"VOffsetTFlags",
".",
"bytewidth",
"self",
".",
"PrependVOffsetT",
"(",
"VOffsetTFlags",
".",
"py_type",
"(",
"vBytes",
")",
")",
"# Next, write the offset to the new vtable in the",
"# already-allocated SOffsetT at the beginning of this object:",
"objectStart",
"=",
"SOffsetTFlags",
".",
"py_type",
"(",
"len",
"(",
"self",
".",
"Bytes",
")",
"-",
"objectOffset",
")",
"encode",
".",
"Write",
"(",
"packer",
".",
"soffset",
",",
"self",
".",
"Bytes",
",",
"objectStart",
",",
"SOffsetTFlags",
".",
"py_type",
"(",
"self",
".",
"Offset",
"(",
")",
"-",
"objectOffset",
")",
")",
"# Finally, store this vtable in memory for future",
"# deduplication:",
"self",
".",
"vtables",
".",
"append",
"(",
"self",
".",
"Offset",
"(",
")",
")",
"else",
":",
"# Found a duplicate vtable.",
"objectStart",
"=",
"SOffsetTFlags",
".",
"py_type",
"(",
"len",
"(",
"self",
".",
"Bytes",
")",
"-",
"objectOffset",
")",
"self",
".",
"head",
"=",
"UOffsetTFlags",
".",
"py_type",
"(",
"objectStart",
")",
"# Write the offset to the found vtable in the",
"# already-allocated SOffsetT at the beginning of this object:",
"encode",
".",
"Write",
"(",
"packer",
".",
"soffset",
",",
"self",
".",
"Bytes",
",",
"self",
".",
"Head",
"(",
")",
",",
"SOffsetTFlags",
".",
"py_type",
"(",
"existingVtable",
"-",
"objectOffset",
")",
")",
"self",
".",
"current_vtable",
"=",
"None",
"return",
"objectOffset"
] | WriteVtable serializes the vtable for the current object, if needed.
Before writing out the vtable, this checks pre-existing vtables for
equality to this one. If an equal vtable is found, point the object to
the existing vtable and return.
Because vtable values are sensitive to alignment of object data, not
all logically-equal vtables will be deduplicated.
A vtable has the following format:
<VOffsetT: size of the vtable in bytes, including this value>
<VOffsetT: size of the object in bytes, including the vtable offset>
<VOffsetT: offset for a field> * N, where N is the number of fields
in the schema for this type. Includes deprecated fields.
Thus, a vtable is made of 2 + N elements, each VOffsetT bytes wide.
An object has the following format:
<SOffsetT: offset to this object's vtable (may be negative)>
<byte: data>+ | [
"WriteVtable",
"serializes",
"the",
"vtable",
"for",
"the",
"current",
"object",
"if",
"needed",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/builder.py#L166-L273 |
24,697 | google/flatbuffers | python/flatbuffers/builder.py | Builder.Pad | def Pad(self, n):
"""Pad places zeros at the current offset."""
for i in range_func(n):
self.Place(0, N.Uint8Flags) | python | def Pad(self, n):
"""Pad places zeros at the current offset."""
for i in range_func(n):
self.Place(0, N.Uint8Flags) | [
"def",
"Pad",
"(",
"self",
",",
"n",
")",
":",
"for",
"i",
"in",
"range_func",
"(",
"n",
")",
":",
"self",
".",
"Place",
"(",
"0",
",",
"N",
".",
"Uint8Flags",
")"
] | Pad places zeros at the current offset. | [
"Pad",
"places",
"zeros",
"at",
"the",
"current",
"offset",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/builder.py#L311-L314 |
24,698 | google/flatbuffers | python/flatbuffers/builder.py | Builder.PrependSOffsetTRelative | def PrependSOffsetTRelative(self, off):
"""
PrependSOffsetTRelative prepends an SOffsetT, relative to where it
will be written.
"""
# Ensure alignment is already done:
self.Prep(N.SOffsetTFlags.bytewidth, 0)
if not (off <= self.Offset()):
msg = "flatbuffers: Offset arithmetic error."
raise OffsetArithmeticError(msg)
off2 = self.Offset() - off + N.SOffsetTFlags.bytewidth
self.PlaceSOffsetT(off2) | python | def PrependSOffsetTRelative(self, off):
"""
PrependSOffsetTRelative prepends an SOffsetT, relative to where it
will be written.
"""
# Ensure alignment is already done:
self.Prep(N.SOffsetTFlags.bytewidth, 0)
if not (off <= self.Offset()):
msg = "flatbuffers: Offset arithmetic error."
raise OffsetArithmeticError(msg)
off2 = self.Offset() - off + N.SOffsetTFlags.bytewidth
self.PlaceSOffsetT(off2) | [
"def",
"PrependSOffsetTRelative",
"(",
"self",
",",
"off",
")",
":",
"# Ensure alignment is already done:",
"self",
".",
"Prep",
"(",
"N",
".",
"SOffsetTFlags",
".",
"bytewidth",
",",
"0",
")",
"if",
"not",
"(",
"off",
"<=",
"self",
".",
"Offset",
"(",
")",
")",
":",
"msg",
"=",
"\"flatbuffers: Offset arithmetic error.\"",
"raise",
"OffsetArithmeticError",
"(",
"msg",
")",
"off2",
"=",
"self",
".",
"Offset",
"(",
")",
"-",
"off",
"+",
"N",
".",
"SOffsetTFlags",
".",
"bytewidth",
"self",
".",
"PlaceSOffsetT",
"(",
"off2",
")"
] | PrependSOffsetTRelative prepends an SOffsetT, relative to where it
will be written. | [
"PrependSOffsetTRelative",
"prepends",
"an",
"SOffsetT",
"relative",
"to",
"where",
"it",
"will",
"be",
"written",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/builder.py#L342-L354 |
24,699 | google/flatbuffers | python/flatbuffers/builder.py | Builder.PrependUOffsetTRelative | def PrependUOffsetTRelative(self, off):
"""Prepends an unsigned offset into vector data, relative to where it
will be written.
"""
# Ensure alignment is already done:
self.Prep(N.UOffsetTFlags.bytewidth, 0)
if not (off <= self.Offset()):
msg = "flatbuffers: Offset arithmetic error."
raise OffsetArithmeticError(msg)
off2 = self.Offset() - off + N.UOffsetTFlags.bytewidth
self.PlaceUOffsetT(off2) | python | def PrependUOffsetTRelative(self, off):
"""Prepends an unsigned offset into vector data, relative to where it
will be written.
"""
# Ensure alignment is already done:
self.Prep(N.UOffsetTFlags.bytewidth, 0)
if not (off <= self.Offset()):
msg = "flatbuffers: Offset arithmetic error."
raise OffsetArithmeticError(msg)
off2 = self.Offset() - off + N.UOffsetTFlags.bytewidth
self.PlaceUOffsetT(off2) | [
"def",
"PrependUOffsetTRelative",
"(",
"self",
",",
"off",
")",
":",
"# Ensure alignment is already done:",
"self",
".",
"Prep",
"(",
"N",
".",
"UOffsetTFlags",
".",
"bytewidth",
",",
"0",
")",
"if",
"not",
"(",
"off",
"<=",
"self",
".",
"Offset",
"(",
")",
")",
":",
"msg",
"=",
"\"flatbuffers: Offset arithmetic error.\"",
"raise",
"OffsetArithmeticError",
"(",
"msg",
")",
"off2",
"=",
"self",
".",
"Offset",
"(",
")",
"-",
"off",
"+",
"N",
".",
"UOffsetTFlags",
".",
"bytewidth",
"self",
".",
"PlaceUOffsetT",
"(",
"off2",
")"
] | Prepends an unsigned offset into vector data, relative to where it
will be written. | [
"Prepends",
"an",
"unsigned",
"offset",
"into",
"vector",
"data",
"relative",
"to",
"where",
"it",
"will",
"be",
"written",
"."
] | 6cc30b3272d79c85db7d4871ac0aa69541dc89de | https://github.com/google/flatbuffers/blob/6cc30b3272d79c85db7d4871ac0aa69541dc89de/python/flatbuffers/builder.py#L357-L368 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.