partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
train
GlobalState.error_messages
Get the error messages for all drivers or a specific driver. Args: driver_id: The specific driver to get the errors for. If this is None, then this method retrieves the errors for all drivers. Returns: A dictionary mapping driver ID to a list of the error messages for that driver.
python/ray/experimental/state.py
def error_messages(self, driver_id=None): """Get the error messages for all drivers or a specific driver. Args: driver_id: The specific driver to get the errors for. If this is None, then this method retrieves the errors for all drivers. Returns: A dictionary mapping driver ID to a list of the error messages for that driver. """ if driver_id is not None: assert isinstance(driver_id, ray.DriverID) return self._error_messages(driver_id) error_table_keys = self.redis_client.keys( ray.gcs_utils.TablePrefix_ERROR_INFO_string + "*") driver_ids = [ key[len(ray.gcs_utils.TablePrefix_ERROR_INFO_string):] for key in error_table_keys ] return { binary_to_hex(driver_id): self._error_messages( ray.DriverID(driver_id)) for driver_id in driver_ids }
def error_messages(self, driver_id=None): """Get the error messages for all drivers or a specific driver. Args: driver_id: The specific driver to get the errors for. If this is None, then this method retrieves the errors for all drivers. Returns: A dictionary mapping driver ID to a list of the error messages for that driver. """ if driver_id is not None: assert isinstance(driver_id, ray.DriverID) return self._error_messages(driver_id) error_table_keys = self.redis_client.keys( ray.gcs_utils.TablePrefix_ERROR_INFO_string + "*") driver_ids = [ key[len(ray.gcs_utils.TablePrefix_ERROR_INFO_string):] for key in error_table_keys ] return { binary_to_hex(driver_id): self._error_messages( ray.DriverID(driver_id)) for driver_id in driver_ids }
[ "Get", "the", "error", "messages", "for", "all", "drivers", "or", "a", "specific", "driver", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/state.py#L876-L902
[ "def", "error_messages", "(", "self", ",", "driver_id", "=", "None", ")", ":", "if", "driver_id", "is", "not", "None", ":", "assert", "isinstance", "(", "driver_id", ",", "ray", ".", "DriverID", ")", "return", "self", ".", "_error_messages", "(", "driver_i...
4eade036a0505e244c976f36aaa2d64386b5129b
train
GlobalState.actor_checkpoint_info
Get checkpoint info for the given actor id. Args: actor_id: Actor's ID. Returns: A dictionary with information about the actor's checkpoint IDs and their timestamps.
python/ray/experimental/state.py
def actor_checkpoint_info(self, actor_id): """Get checkpoint info for the given actor id. Args: actor_id: Actor's ID. Returns: A dictionary with information about the actor's checkpoint IDs and their timestamps. """ self._check_connected() message = self._execute_command( actor_id, "RAY.TABLE_LOOKUP", ray.gcs_utils.TablePrefix.ACTOR_CHECKPOINT_ID, "", actor_id.binary(), ) if message is None: return None gcs_entry = ray.gcs_utils.GcsTableEntry.GetRootAsGcsTableEntry( message, 0) entry = ( ray.gcs_utils.ActorCheckpointIdData.GetRootAsActorCheckpointIdData( gcs_entry.Entries(0), 0)) checkpoint_ids_str = entry.CheckpointIds() num_checkpoints = len(checkpoint_ids_str) // ID_SIZE assert len(checkpoint_ids_str) % ID_SIZE == 0 checkpoint_ids = [ ray.ActorCheckpointID( checkpoint_ids_str[(i * ID_SIZE):((i + 1) * ID_SIZE)]) for i in range(num_checkpoints) ] return { "ActorID": ray.utils.binary_to_hex(entry.ActorId()), "CheckpointIds": checkpoint_ids, "Timestamps": [ entry.Timestamps(i) for i in range(num_checkpoints) ], }
def actor_checkpoint_info(self, actor_id): """Get checkpoint info for the given actor id. Args: actor_id: Actor's ID. Returns: A dictionary with information about the actor's checkpoint IDs and their timestamps. """ self._check_connected() message = self._execute_command( actor_id, "RAY.TABLE_LOOKUP", ray.gcs_utils.TablePrefix.ACTOR_CHECKPOINT_ID, "", actor_id.binary(), ) if message is None: return None gcs_entry = ray.gcs_utils.GcsTableEntry.GetRootAsGcsTableEntry( message, 0) entry = ( ray.gcs_utils.ActorCheckpointIdData.GetRootAsActorCheckpointIdData( gcs_entry.Entries(0), 0)) checkpoint_ids_str = entry.CheckpointIds() num_checkpoints = len(checkpoint_ids_str) // ID_SIZE assert len(checkpoint_ids_str) % ID_SIZE == 0 checkpoint_ids = [ ray.ActorCheckpointID( checkpoint_ids_str[(i * ID_SIZE):((i + 1) * ID_SIZE)]) for i in range(num_checkpoints) ] return { "ActorID": ray.utils.binary_to_hex(entry.ActorId()), "CheckpointIds": checkpoint_ids, "Timestamps": [ entry.Timestamps(i) for i in range(num_checkpoints) ], }
[ "Get", "checkpoint", "info", "for", "the", "given", "actor", "id", ".", "Args", ":", "actor_id", ":", "Actor", "s", "ID", ".", "Returns", ":", "A", "dictionary", "with", "information", "about", "the", "actor", "s", "checkpoint", "IDs", "and", "their", "t...
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/state.py#L904-L941
[ "def", "actor_checkpoint_info", "(", "self", ",", "actor_id", ")", ":", "self", ".", "_check_connected", "(", ")", "message", "=", "self", ".", "_execute_command", "(", "actor_id", ",", "\"RAY.TABLE_LOOKUP\"", ",", "ray", ".", "gcs_utils", ".", "TablePrefix", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
TensorFlowVariables.get_flat_size
Returns the total length of all of the flattened variables. Returns: The length of all flattened variables concatenated.
python/ray/experimental/tf_utils.py
def get_flat_size(self): """Returns the total length of all of the flattened variables. Returns: The length of all flattened variables concatenated. """ return sum( np.prod(v.get_shape().as_list()) for v in self.variables.values())
def get_flat_size(self): """Returns the total length of all of the flattened variables. Returns: The length of all flattened variables concatenated. """ return sum( np.prod(v.get_shape().as_list()) for v in self.variables.values())
[ "Returns", "the", "total", "length", "of", "all", "of", "the", "flattened", "variables", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/tf_utils.py#L111-L118
[ "def", "get_flat_size", "(", "self", ")", ":", "return", "sum", "(", "np", ".", "prod", "(", "v", ".", "get_shape", "(", ")", ".", "as_list", "(", ")", ")", "for", "v", "in", "self", ".", "variables", ".", "values", "(", ")", ")" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
TensorFlowVariables.get_flat
Gets the weights and returns them as a flat array. Returns: 1D Array containing the flattened weights.
python/ray/experimental/tf_utils.py
def get_flat(self): """Gets the weights and returns them as a flat array. Returns: 1D Array containing the flattened weights. """ self._check_sess() return np.concatenate([ v.eval(session=self.sess).flatten() for v in self.variables.values() ])
def get_flat(self): """Gets the weights and returns them as a flat array. Returns: 1D Array containing the flattened weights. """ self._check_sess() return np.concatenate([ v.eval(session=self.sess).flatten() for v in self.variables.values() ])
[ "Gets", "the", "weights", "and", "returns", "them", "as", "a", "flat", "array", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/tf_utils.py#L127-L137
[ "def", "get_flat", "(", "self", ")", ":", "self", ".", "_check_sess", "(", ")", "return", "np", ".", "concatenate", "(", "[", "v", ".", "eval", "(", "session", "=", "self", ".", "sess", ")", ".", "flatten", "(", ")", "for", "v", "in", "self", "."...
4eade036a0505e244c976f36aaa2d64386b5129b
train
TensorFlowVariables.set_flat
Sets the weights to new_weights, converting from a flat array. Note: You can only set all weights in the network using this function, i.e., the length of the array must match get_flat_size. Args: new_weights (np.ndarray): Flat array containing weights.
python/ray/experimental/tf_utils.py
def set_flat(self, new_weights): """Sets the weights to new_weights, converting from a flat array. Note: You can only set all weights in the network using this function, i.e., the length of the array must match get_flat_size. Args: new_weights (np.ndarray): Flat array containing weights. """ self._check_sess() shapes = [v.get_shape().as_list() for v in self.variables.values()] arrays = unflatten(new_weights, shapes) placeholders = [ self.placeholders[k] for k, v in self.variables.items() ] self.sess.run( list(self.assignment_nodes.values()), feed_dict=dict(zip(placeholders, arrays)))
def set_flat(self, new_weights): """Sets the weights to new_weights, converting from a flat array. Note: You can only set all weights in the network using this function, i.e., the length of the array must match get_flat_size. Args: new_weights (np.ndarray): Flat array containing weights. """ self._check_sess() shapes = [v.get_shape().as_list() for v in self.variables.values()] arrays = unflatten(new_weights, shapes) placeholders = [ self.placeholders[k] for k, v in self.variables.items() ] self.sess.run( list(self.assignment_nodes.values()), feed_dict=dict(zip(placeholders, arrays)))
[ "Sets", "the", "weights", "to", "new_weights", "converting", "from", "a", "flat", "array", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/tf_utils.py#L139-L157
[ "def", "set_flat", "(", "self", ",", "new_weights", ")", ":", "self", ".", "_check_sess", "(", ")", "shapes", "=", "[", "v", ".", "get_shape", "(", ")", ".", "as_list", "(", ")", "for", "v", "in", "self", ".", "variables", ".", "values", "(", ")", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
TensorFlowVariables.get_weights
Returns a dictionary containing the weights of the network. Returns: Dictionary mapping variable names to their weights.
python/ray/experimental/tf_utils.py
def get_weights(self): """Returns a dictionary containing the weights of the network. Returns: Dictionary mapping variable names to their weights. """ self._check_sess() return { k: v.eval(session=self.sess) for k, v in self.variables.items() }
def get_weights(self): """Returns a dictionary containing the weights of the network. Returns: Dictionary mapping variable names to their weights. """ self._check_sess() return { k: v.eval(session=self.sess) for k, v in self.variables.items() }
[ "Returns", "a", "dictionary", "containing", "the", "weights", "of", "the", "network", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/tf_utils.py#L159-L169
[ "def", "get_weights", "(", "self", ")", ":", "self", ".", "_check_sess", "(", ")", "return", "{", "k", ":", "v", ".", "eval", "(", "session", "=", "self", ".", "sess", ")", "for", "k", ",", "v", "in", "self", ".", "variables", ".", "items", "(", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
TensorFlowVariables.set_weights
Sets the weights to new_weights. Note: Can set subsets of variables as well, by only passing in the variables you want to be set. Args: new_weights (Dict): Dictionary mapping variable names to their weights.
python/ray/experimental/tf_utils.py
def set_weights(self, new_weights): """Sets the weights to new_weights. Note: Can set subsets of variables as well, by only passing in the variables you want to be set. Args: new_weights (Dict): Dictionary mapping variable names to their weights. """ self._check_sess() assign_list = [ self.assignment_nodes[name] for name in new_weights.keys() if name in self.assignment_nodes ] assert assign_list, ("No variables in the input matched those in the " "network. Possible cause: Two networks were " "defined in the same TensorFlow graph. To fix " "this, place each network definition in its own " "tf.Graph.") self.sess.run( assign_list, feed_dict={ self.placeholders[name]: value for (name, value) in new_weights.items() if name in self.placeholders })
def set_weights(self, new_weights): """Sets the weights to new_weights. Note: Can set subsets of variables as well, by only passing in the variables you want to be set. Args: new_weights (Dict): Dictionary mapping variable names to their weights. """ self._check_sess() assign_list = [ self.assignment_nodes[name] for name in new_weights.keys() if name in self.assignment_nodes ] assert assign_list, ("No variables in the input matched those in the " "network. Possible cause: Two networks were " "defined in the same TensorFlow graph. To fix " "this, place each network definition in its own " "tf.Graph.") self.sess.run( assign_list, feed_dict={ self.placeholders[name]: value for (name, value) in new_weights.items() if name in self.placeholders })
[ "Sets", "the", "weights", "to", "new_weights", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/tf_utils.py#L171-L198
[ "def", "set_weights", "(", "self", ",", "new_weights", ")", ":", "self", ".", "_check_sess", "(", ")", "assign_list", "=", "[", "self", ".", "assignment_nodes", "[", "name", "]", "for", "name", "in", "new_weights", ".", "keys", "(", ")", "if", "name", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
construct_error_message
Construct a serialized ErrorTableData object. Args: driver_id: The ID of the driver that the error should go to. If this is nil, then the error will go to all drivers. error_type: The type of the error. message: The error message. timestamp: The time of the error. Returns: The serialized object.
python/ray/gcs_utils.py
def construct_error_message(driver_id, error_type, message, timestamp): """Construct a serialized ErrorTableData object. Args: driver_id: The ID of the driver that the error should go to. If this is nil, then the error will go to all drivers. error_type: The type of the error. message: The error message. timestamp: The time of the error. Returns: The serialized object. """ builder = flatbuffers.Builder(0) driver_offset = builder.CreateString(driver_id.binary()) error_type_offset = builder.CreateString(error_type) message_offset = builder.CreateString(message) ray.core.generated.ErrorTableData.ErrorTableDataStart(builder) ray.core.generated.ErrorTableData.ErrorTableDataAddDriverId( builder, driver_offset) ray.core.generated.ErrorTableData.ErrorTableDataAddType( builder, error_type_offset) ray.core.generated.ErrorTableData.ErrorTableDataAddErrorMessage( builder, message_offset) ray.core.generated.ErrorTableData.ErrorTableDataAddTimestamp( builder, timestamp) error_data_offset = ray.core.generated.ErrorTableData.ErrorTableDataEnd( builder) builder.Finish(error_data_offset) return bytes(builder.Output())
def construct_error_message(driver_id, error_type, message, timestamp): """Construct a serialized ErrorTableData object. Args: driver_id: The ID of the driver that the error should go to. If this is nil, then the error will go to all drivers. error_type: The type of the error. message: The error message. timestamp: The time of the error. Returns: The serialized object. """ builder = flatbuffers.Builder(0) driver_offset = builder.CreateString(driver_id.binary()) error_type_offset = builder.CreateString(error_type) message_offset = builder.CreateString(message) ray.core.generated.ErrorTableData.ErrorTableDataStart(builder) ray.core.generated.ErrorTableData.ErrorTableDataAddDriverId( builder, driver_offset) ray.core.generated.ErrorTableData.ErrorTableDataAddType( builder, error_type_offset) ray.core.generated.ErrorTableData.ErrorTableDataAddErrorMessage( builder, message_offset) ray.core.generated.ErrorTableData.ErrorTableDataAddTimestamp( builder, timestamp) error_data_offset = ray.core.generated.ErrorTableData.ErrorTableDataEnd( builder) builder.Finish(error_data_offset) return bytes(builder.Output())
[ "Construct", "a", "serialized", "ErrorTableData", "object", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/gcs_utils.py#L60-L91
[ "def", "construct_error_message", "(", "driver_id", ",", "error_type", ",", "message", ",", "timestamp", ")", ":", "builder", "=", "flatbuffers", ".", "Builder", "(", "0", ")", "driver_offset", "=", "builder", ".", "CreateString", "(", "driver_id", ".", "binar...
4eade036a0505e244c976f36aaa2d64386b5129b
train
init
Initialize synchronously.
python/ray/experimental/async_api.py
def init(): """ Initialize synchronously. """ loop = asyncio.get_event_loop() if loop.is_running(): raise Exception("You must initialize the Ray async API by calling " "async_api.init() or async_api.as_future(obj) before " "the event loop starts.") else: asyncio.get_event_loop().run_until_complete(_async_init())
def init(): """ Initialize synchronously. """ loop = asyncio.get_event_loop() if loop.is_running(): raise Exception("You must initialize the Ray async API by calling " "async_api.init() or async_api.as_future(obj) before " "the event loop starts.") else: asyncio.get_event_loop().run_until_complete(_async_init())
[ "Initialize", "synchronously", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_api.py#L24-L34
[ "def", "init", "(", ")", ":", "loop", "=", "asyncio", ".", "get_event_loop", "(", ")", "if", "loop", ".", "is_running", "(", ")", ":", "raise", "Exception", "(", "\"You must initialize the Ray async API by calling \"", "\"async_api.init() or async_api.as_future(obj) bef...
4eade036a0505e244c976f36aaa2d64386b5129b
train
shutdown
Manually shutdown the async API. Cancels all related tasks and all the socket transportation.
python/ray/experimental/async_api.py
def shutdown(): """Manually shutdown the async API. Cancels all related tasks and all the socket transportation. """ global handler, transport, protocol if handler is not None: handler.close() transport.close() handler = None transport = None protocol = None
def shutdown(): """Manually shutdown the async API. Cancels all related tasks and all the socket transportation. """ global handler, transport, protocol if handler is not None: handler.close() transport.close() handler = None transport = None protocol = None
[ "Manually", "shutdown", "the", "async", "API", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_api.py#L51-L62
[ "def", "shutdown", "(", ")", ":", "global", "handler", ",", "transport", ",", "protocol", "if", "handler", "is", "not", "None", ":", "handler", ".", "close", "(", ")", "transport", ".", "close", "(", ")", "handler", "=", "None", "transport", "=", "None...
4eade036a0505e244c976f36aaa2d64386b5129b
train
flush_redis_unsafe
This removes some non-critical state from the primary Redis shard. This removes the log files as well as the event log from Redis. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, it will only partially address the issue as much of the data is in the task table (and object table), which are not flushed. Args: redis_client: optional, if not provided then ray.init() must have been called.
python/ray/experimental/features.py
def flush_redis_unsafe(redis_client=None): """This removes some non-critical state from the primary Redis shard. This removes the log files as well as the event log from Redis. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, it will only partially address the issue as much of the data is in the task table (and object table), which are not flushed. Args: redis_client: optional, if not provided then ray.init() must have been called. """ if redis_client is None: ray.worker.global_worker.check_connected() redis_client = ray.worker.global_worker.redis_client # Delete the log files from the primary Redis shard. keys = redis_client.keys("LOGFILE:*") if len(keys) > 0: num_deleted = redis_client.delete(*keys) else: num_deleted = 0 print("Deleted {} log files from Redis.".format(num_deleted)) # Delete the event log from the primary Redis shard. keys = redis_client.keys("event_log:*") if len(keys) > 0: num_deleted = redis_client.delete(*keys) else: num_deleted = 0 print("Deleted {} event logs from Redis.".format(num_deleted))
def flush_redis_unsafe(redis_client=None): """This removes some non-critical state from the primary Redis shard. This removes the log files as well as the event log from Redis. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, it will only partially address the issue as much of the data is in the task table (and object table), which are not flushed. Args: redis_client: optional, if not provided then ray.init() must have been called. """ if redis_client is None: ray.worker.global_worker.check_connected() redis_client = ray.worker.global_worker.redis_client # Delete the log files from the primary Redis shard. keys = redis_client.keys("LOGFILE:*") if len(keys) > 0: num_deleted = redis_client.delete(*keys) else: num_deleted = 0 print("Deleted {} log files from Redis.".format(num_deleted)) # Delete the event log from the primary Redis shard. keys = redis_client.keys("event_log:*") if len(keys) > 0: num_deleted = redis_client.delete(*keys) else: num_deleted = 0 print("Deleted {} event logs from Redis.".format(num_deleted))
[ "This", "removes", "some", "non", "-", "critical", "state", "from", "the", "primary", "Redis", "shard", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/features.py#L13-L44
[ "def", "flush_redis_unsafe", "(", "redis_client", "=", "None", ")", ":", "if", "redis_client", "is", "None", ":", "ray", ".", "worker", ".", "global_worker", ".", "check_connected", "(", ")", "redis_client", "=", "ray", ".", "worker", ".", "global_worker", "...
4eade036a0505e244c976f36aaa2d64386b5129b
train
flush_task_and_object_metadata_unsafe
This removes some critical state from the Redis shards. In a multitenant environment, this will flush metadata for all jobs, which may be undesirable. This removes all of the object and task metadata. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, after running this command, fault tolerance will most likely not work.
python/ray/experimental/features.py
def flush_task_and_object_metadata_unsafe(): """This removes some critical state from the Redis shards. In a multitenant environment, this will flush metadata for all jobs, which may be undesirable. This removes all of the object and task metadata. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, after running this command, fault tolerance will most likely not work. """ ray.worker.global_worker.check_connected() def flush_shard(redis_client): # Flush the task table. Note that this also flushes the driver tasks # which may be undesirable. num_task_keys_deleted = 0 for key in redis_client.scan_iter(match=TASK_PREFIX + b"*"): num_task_keys_deleted += redis_client.delete(key) print("Deleted {} task keys from Redis.".format(num_task_keys_deleted)) # Flush the object information. num_object_keys_deleted = 0 for key in redis_client.scan_iter(match=OBJECT_INFO_PREFIX + b"*"): num_object_keys_deleted += redis_client.delete(key) print("Deleted {} object info keys from Redis.".format( num_object_keys_deleted)) # Flush the object locations. num_object_location_keys_deleted = 0 for key in redis_client.scan_iter(match=OBJECT_LOCATION_PREFIX + b"*"): num_object_location_keys_deleted += redis_client.delete(key) print("Deleted {} object location keys from Redis.".format( num_object_location_keys_deleted)) # Loop over the shards and flush all of them. for redis_client in ray.worker.global_state.redis_clients: flush_shard(redis_client)
def flush_task_and_object_metadata_unsafe(): """This removes some critical state from the Redis shards. In a multitenant environment, this will flush metadata for all jobs, which may be undesirable. This removes all of the object and task metadata. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, after running this command, fault tolerance will most likely not work. """ ray.worker.global_worker.check_connected() def flush_shard(redis_client): # Flush the task table. Note that this also flushes the driver tasks # which may be undesirable. num_task_keys_deleted = 0 for key in redis_client.scan_iter(match=TASK_PREFIX + b"*"): num_task_keys_deleted += redis_client.delete(key) print("Deleted {} task keys from Redis.".format(num_task_keys_deleted)) # Flush the object information. num_object_keys_deleted = 0 for key in redis_client.scan_iter(match=OBJECT_INFO_PREFIX + b"*"): num_object_keys_deleted += redis_client.delete(key) print("Deleted {} object info keys from Redis.".format( num_object_keys_deleted)) # Flush the object locations. num_object_location_keys_deleted = 0 for key in redis_client.scan_iter(match=OBJECT_LOCATION_PREFIX + b"*"): num_object_location_keys_deleted += redis_client.delete(key) print("Deleted {} object location keys from Redis.".format( num_object_location_keys_deleted)) # Loop over the shards and flush all of them. for redis_client in ray.worker.global_state.redis_clients: flush_shard(redis_client)
[ "This", "removes", "some", "critical", "state", "from", "the", "Redis", "shards", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/features.py#L47-L84
[ "def", "flush_task_and_object_metadata_unsafe", "(", ")", ":", "ray", ".", "worker", ".", "global_worker", ".", "check_connected", "(", ")", "def", "flush_shard", "(", "redis_client", ")", ":", "# Flush the task table. Note that this also flushes the driver tasks", "# which...
4eade036a0505e244c976f36aaa2d64386b5129b
train
flush_finished_tasks_unsafe
This removes some critical state from the Redis shards. In a multitenant environment, this will flush metadata for all jobs, which may be undesirable. This removes all of the metadata for finished tasks. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, after running this command, fault tolerance will most likely not work.
python/ray/experimental/features.py
def flush_finished_tasks_unsafe(): """This removes some critical state from the Redis shards. In a multitenant environment, this will flush metadata for all jobs, which may be undesirable. This removes all of the metadata for finished tasks. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, after running this command, fault tolerance will most likely not work. """ ray.worker.global_worker.check_connected() for shard_index in range(len(ray.global_state.redis_clients)): _flush_finished_tasks_unsafe_shard(shard_index)
def flush_finished_tasks_unsafe(): """This removes some critical state from the Redis shards. In a multitenant environment, this will flush metadata for all jobs, which may be undesirable. This removes all of the metadata for finished tasks. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, after running this command, fault tolerance will most likely not work. """ ray.worker.global_worker.check_connected() for shard_index in range(len(ray.global_state.redis_clients)): _flush_finished_tasks_unsafe_shard(shard_index)
[ "This", "removes", "some", "critical", "state", "from", "the", "Redis", "shards", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/features.py#L155-L169
[ "def", "flush_finished_tasks_unsafe", "(", ")", ":", "ray", ".", "worker", ".", "global_worker", ".", "check_connected", "(", ")", "for", "shard_index", "in", "range", "(", "len", "(", "ray", ".", "global_state", ".", "redis_clients", ")", ")", ":", "_flush_...
4eade036a0505e244c976f36aaa2d64386b5129b
train
flush_evicted_objects_unsafe
This removes some critical state from the Redis shards. In a multitenant environment, this will flush metadata for all jobs, which may be undesirable. This removes all of the metadata for objects that have been evicted. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, after running this command, fault tolerance will most likely not work.
python/ray/experimental/features.py
def flush_evicted_objects_unsafe(): """This removes some critical state from the Redis shards. In a multitenant environment, this will flush metadata for all jobs, which may be undesirable. This removes all of the metadata for objects that have been evicted. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, after running this command, fault tolerance will most likely not work. """ ray.worker.global_worker.check_connected() for shard_index in range(len(ray.global_state.redis_clients)): _flush_evicted_objects_unsafe_shard(shard_index)
def flush_evicted_objects_unsafe(): """This removes some critical state from the Redis shards. In a multitenant environment, this will flush metadata for all jobs, which may be undesirable. This removes all of the metadata for objects that have been evicted. This can be used to try to address out-of-memory errors caused by the accumulation of metadata in Redis. However, after running this command, fault tolerance will most likely not work. """ ray.worker.global_worker.check_connected() for shard_index in range(len(ray.global_state.redis_clients)): _flush_evicted_objects_unsafe_shard(shard_index)
[ "This", "removes", "some", "critical", "state", "from", "the", "Redis", "shards", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/features.py#L172-L186
[ "def", "flush_evicted_objects_unsafe", "(", ")", ":", "ray", ".", "worker", ".", "global_worker", ".", "check_connected", "(", ")", "for", "shard_index", "in", "range", "(", "len", "(", "ray", ".", "global_state", ".", "redis_clients", ")", ")", ":", "_flush...
4eade036a0505e244c976f36aaa2d64386b5129b
train
PPOPolicyGraph.copy
Creates a copy of self using existing input placeholders.
python/ray/rllib/agents/ppo/ppo_policy_graph.py
def copy(self, existing_inputs): """Creates a copy of self using existing input placeholders.""" return PPOPolicyGraph( self.observation_space, self.action_space, self.config, existing_inputs=existing_inputs)
def copy(self, existing_inputs): """Creates a copy of self using existing input placeholders.""" return PPOPolicyGraph( self.observation_space, self.action_space, self.config, existing_inputs=existing_inputs)
[ "Creates", "a", "copy", "of", "self", "using", "existing", "input", "placeholders", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/agents/ppo/ppo_policy_graph.py#L318-L324
[ "def", "copy", "(", "self", ",", "existing_inputs", ")", ":", "return", "PPOPolicyGraph", "(", "self", ".", "observation_space", ",", "self", ".", "action_space", ",", "self", ".", "config", ",", "existing_inputs", "=", "existing_inputs", ")" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
deepnn
deepnn builds the graph for a deep net for classifying digits. Args: x: an input tensor with the dimensions (N_examples, 784), where 784 is the number of pixels in a standard MNIST image. Returns: A tuple (y, keep_prob). y is a tensor of shape (N_examples, 10), with values equal to the logits of classifying the digit into one of 10 classes (the digits 0-9). keep_prob is a scalar placeholder for the probability of dropout.
examples/parameter_server/model.py
def deepnn(x): """deepnn builds the graph for a deep net for classifying digits. Args: x: an input tensor with the dimensions (N_examples, 784), where 784 is the number of pixels in a standard MNIST image. Returns: A tuple (y, keep_prob). y is a tensor of shape (N_examples, 10), with values equal to the logits of classifying the digit into one of 10 classes (the digits 0-9). keep_prob is a scalar placeholder for the probability of dropout. """ # Reshape to use within a convolutional neural net. # Last dimension is for "features" - there is only one here, since images # are grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc. with tf.name_scope("reshape"): x_image = tf.reshape(x, [-1, 28, 28, 1]) # First convolutional layer - maps one grayscale image to 32 feature maps. with tf.name_scope("conv1"): W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # Pooling layer - downsamples by 2X. with tf.name_scope("pool1"): h_pool1 = max_pool_2x2(h_conv1) # Second convolutional layer -- maps 32 feature maps to 64. with tf.name_scope("conv2"): W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # Second pooling layer. with tf.name_scope("pool2"): h_pool2 = max_pool_2x2(h_conv2) # Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image # is down to 7x7x64 feature maps -- maps this to 1024 features. with tf.name_scope("fc1"): W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # Dropout - controls the complexity of the model, prevents co-adaptation of # features. with tf.name_scope("dropout"): keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # Map the 1024 features to 10 classes, one for each digit with tf.name_scope("fc2"): W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 return y_conv, keep_prob
def deepnn(x): """deepnn builds the graph for a deep net for classifying digits. Args: x: an input tensor with the dimensions (N_examples, 784), where 784 is the number of pixels in a standard MNIST image. Returns: A tuple (y, keep_prob). y is a tensor of shape (N_examples, 10), with values equal to the logits of classifying the digit into one of 10 classes (the digits 0-9). keep_prob is a scalar placeholder for the probability of dropout. """ # Reshape to use within a convolutional neural net. # Last dimension is for "features" - there is only one here, since images # are grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc. with tf.name_scope("reshape"): x_image = tf.reshape(x, [-1, 28, 28, 1]) # First convolutional layer - maps one grayscale image to 32 feature maps. with tf.name_scope("conv1"): W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # Pooling layer - downsamples by 2X. with tf.name_scope("pool1"): h_pool1 = max_pool_2x2(h_conv1) # Second convolutional layer -- maps 32 feature maps to 64. with tf.name_scope("conv2"): W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # Second pooling layer. with tf.name_scope("pool2"): h_pool2 = max_pool_2x2(h_conv2) # Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image # is down to 7x7x64 feature maps -- maps this to 1024 features. with tf.name_scope("fc1"): W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # Dropout - controls the complexity of the model, prevents co-adaptation of # features. with tf.name_scope("dropout"): keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # Map the 1024 features to 10 classes, one for each digit with tf.name_scope("fc2"): W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 return y_conv, keep_prob
[ "deepnn", "builds", "the", "graph", "for", "a", "deep", "net", "for", "classifying", "digits", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/examples/parameter_server/model.py#L120-L180
[ "def", "deepnn", "(", "x", ")", ":", "# Reshape to use within a convolutional neural net.", "# Last dimension is for \"features\" - there is only one here, since images", "# are grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc.", "with", "tf", ".", "name_scope", "(", "\"resha...
4eade036a0505e244c976f36aaa2d64386b5129b
train
get_signature_params
Get signature parameters Support Cython functions by grabbing relevant attributes from the Cython function and attaching to a no-op function. This is somewhat brittle, since funcsigs may change, but given that funcsigs is written to a PEP, we hope it is relatively stable. Future versions of Python may allow overloading the inspect 'isfunction' and 'ismethod' functions / create ABC for Python functions. Until then, it appears that Cython won't do anything about compatability with the inspect module. Args: func: The function whose signature should be checked. Raises: TypeError: A type error if the signature is not supported
python/ray/signature.py
def get_signature_params(func): """Get signature parameters Support Cython functions by grabbing relevant attributes from the Cython function and attaching to a no-op function. This is somewhat brittle, since funcsigs may change, but given that funcsigs is written to a PEP, we hope it is relatively stable. Future versions of Python may allow overloading the inspect 'isfunction' and 'ismethod' functions / create ABC for Python functions. Until then, it appears that Cython won't do anything about compatability with the inspect module. Args: func: The function whose signature should be checked. Raises: TypeError: A type error if the signature is not supported """ # The first condition for Cython functions, the latter for Cython instance # methods if is_cython(func): attrs = [ "__code__", "__annotations__", "__defaults__", "__kwdefaults__" ] if all(hasattr(func, attr) for attr in attrs): original_func = func def func(): return for attr in attrs: setattr(func, attr, getattr(original_func, attr)) else: raise TypeError("{!r} is not a Python function we can process" .format(func)) return list(funcsigs.signature(func).parameters.items())
def get_signature_params(func): """Get signature parameters Support Cython functions by grabbing relevant attributes from the Cython function and attaching to a no-op function. This is somewhat brittle, since funcsigs may change, but given that funcsigs is written to a PEP, we hope it is relatively stable. Future versions of Python may allow overloading the inspect 'isfunction' and 'ismethod' functions / create ABC for Python functions. Until then, it appears that Cython won't do anything about compatability with the inspect module. Args: func: The function whose signature should be checked. Raises: TypeError: A type error if the signature is not supported """ # The first condition for Cython functions, the latter for Cython instance # methods if is_cython(func): attrs = [ "__code__", "__annotations__", "__defaults__", "__kwdefaults__" ] if all(hasattr(func, attr) for attr in attrs): original_func = func def func(): return for attr in attrs: setattr(func, attr, getattr(original_func, attr)) else: raise TypeError("{!r} is not a Python function we can process" .format(func)) return list(funcsigs.signature(func).parameters.items())
[ "Get", "signature", "parameters" ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/signature.py#L39-L75
[ "def", "get_signature_params", "(", "func", ")", ":", "# The first condition for Cython functions, the latter for Cython instance", "# methods", "if", "is_cython", "(", "func", ")", ":", "attrs", "=", "[", "\"__code__\"", ",", "\"__annotations__\"", ",", "\"__defaults__\"",...
4eade036a0505e244c976f36aaa2d64386b5129b
train
check_signature_supported
Check if we support the signature of this function. We currently do not allow remote functions to have **kwargs. We also do not support keyword arguments in conjunction with a *args argument. Args: func: The function whose signature should be checked. warn: If this is true, a warning will be printed if the signature is not supported. If it is false, an exception will be raised if the signature is not supported. Raises: Exception: An exception is raised if the signature is not supported.
python/ray/signature.py
def check_signature_supported(func, warn=False): """Check if we support the signature of this function. We currently do not allow remote functions to have **kwargs. We also do not support keyword arguments in conjunction with a *args argument. Args: func: The function whose signature should be checked. warn: If this is true, a warning will be printed if the signature is not supported. If it is false, an exception will be raised if the signature is not supported. Raises: Exception: An exception is raised if the signature is not supported. """ function_name = func.__name__ sig_params = get_signature_params(func) has_kwargs_param = False has_kwonly_param = False for keyword_name, parameter in sig_params: if parameter.kind == Parameter.VAR_KEYWORD: has_kwargs_param = True if parameter.kind == Parameter.KEYWORD_ONLY: has_kwonly_param = True if has_kwargs_param: message = ("The function {} has a **kwargs argument, which is " "currently not supported.".format(function_name)) if warn: logger.warning(message) else: raise Exception(message) if has_kwonly_param: message = ("The function {} has a keyword only argument " "(defined after * or *args), which is currently " "not supported.".format(function_name)) if warn: logger.warning(message) else: raise Exception(message)
def check_signature_supported(func, warn=False): """Check if we support the signature of this function. We currently do not allow remote functions to have **kwargs. We also do not support keyword arguments in conjunction with a *args argument. Args: func: The function whose signature should be checked. warn: If this is true, a warning will be printed if the signature is not supported. If it is false, an exception will be raised if the signature is not supported. Raises: Exception: An exception is raised if the signature is not supported. """ function_name = func.__name__ sig_params = get_signature_params(func) has_kwargs_param = False has_kwonly_param = False for keyword_name, parameter in sig_params: if parameter.kind == Parameter.VAR_KEYWORD: has_kwargs_param = True if parameter.kind == Parameter.KEYWORD_ONLY: has_kwonly_param = True if has_kwargs_param: message = ("The function {} has a **kwargs argument, which is " "currently not supported.".format(function_name)) if warn: logger.warning(message) else: raise Exception(message) if has_kwonly_param: message = ("The function {} has a keyword only argument " "(defined after * or *args), which is currently " "not supported.".format(function_name)) if warn: logger.warning(message) else: raise Exception(message)
[ "Check", "if", "we", "support", "the", "signature", "of", "this", "function", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/signature.py#L78-L119
[ "def", "check_signature_supported", "(", "func", ",", "warn", "=", "False", ")", ":", "function_name", "=", "func", ".", "__name__", "sig_params", "=", "get_signature_params", "(", "func", ")", "has_kwargs_param", "=", "False", "has_kwonly_param", "=", "False", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
extract_signature
Extract the function signature from the function. Args: func: The function whose signature should be extracted. ignore_first: True if the first argument should be ignored. This should be used when func is a method of a class. Returns: A function signature object, which includes the names of the keyword arguments as well as their default values.
python/ray/signature.py
def extract_signature(func, ignore_first=False): """Extract the function signature from the function. Args: func: The function whose signature should be extracted. ignore_first: True if the first argument should be ignored. This should be used when func is a method of a class. Returns: A function signature object, which includes the names of the keyword arguments as well as their default values. """ sig_params = get_signature_params(func) if ignore_first: if len(sig_params) == 0: raise Exception("Methods must take a 'self' argument, but the " "method '{}' does not have one.".format( func.__name__)) sig_params = sig_params[1:] # Construct the argument default values and other argument information. arg_names = [] arg_defaults = [] arg_is_positionals = [] keyword_names = set() for arg_name, parameter in sig_params: arg_names.append(arg_name) arg_defaults.append(parameter.default) arg_is_positionals.append(parameter.kind == parameter.VAR_POSITIONAL) if parameter.kind == Parameter.POSITIONAL_OR_KEYWORD: # Note KEYWORD_ONLY arguments currently unsupported. keyword_names.add(arg_name) return FunctionSignature(arg_names, arg_defaults, arg_is_positionals, keyword_names, func.__name__)
def extract_signature(func, ignore_first=False): """Extract the function signature from the function. Args: func: The function whose signature should be extracted. ignore_first: True if the first argument should be ignored. This should be used when func is a method of a class. Returns: A function signature object, which includes the names of the keyword arguments as well as their default values. """ sig_params = get_signature_params(func) if ignore_first: if len(sig_params) == 0: raise Exception("Methods must take a 'self' argument, but the " "method '{}' does not have one.".format( func.__name__)) sig_params = sig_params[1:] # Construct the argument default values and other argument information. arg_names = [] arg_defaults = [] arg_is_positionals = [] keyword_names = set() for arg_name, parameter in sig_params: arg_names.append(arg_name) arg_defaults.append(parameter.default) arg_is_positionals.append(parameter.kind == parameter.VAR_POSITIONAL) if parameter.kind == Parameter.POSITIONAL_OR_KEYWORD: # Note KEYWORD_ONLY arguments currently unsupported. keyword_names.add(arg_name) return FunctionSignature(arg_names, arg_defaults, arg_is_positionals, keyword_names, func.__name__)
[ "Extract", "the", "function", "signature", "from", "the", "function", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/signature.py#L122-L157
[ "def", "extract_signature", "(", "func", ",", "ignore_first", "=", "False", ")", ":", "sig_params", "=", "get_signature_params", "(", "func", ")", "if", "ignore_first", ":", "if", "len", "(", "sig_params", ")", "==", "0", ":", "raise", "Exception", "(", "\...
4eade036a0505e244c976f36aaa2d64386b5129b
train
extend_args
Extend the arguments that were passed into a function. This extends the arguments that were passed into a function with the default arguments provided in the function definition. Args: function_signature: The function signature of the function being called. args: The non-keyword arguments passed into the function. kwargs: The keyword arguments passed into the function. Returns: An extended list of arguments to pass into the function. Raises: Exception: An exception may be raised if the function cannot be called with these arguments.
python/ray/signature.py
def extend_args(function_signature, args, kwargs): """Extend the arguments that were passed into a function. This extends the arguments that were passed into a function with the default arguments provided in the function definition. Args: function_signature: The function signature of the function being called. args: The non-keyword arguments passed into the function. kwargs: The keyword arguments passed into the function. Returns: An extended list of arguments to pass into the function. Raises: Exception: An exception may be raised if the function cannot be called with these arguments. """ arg_names = function_signature.arg_names arg_defaults = function_signature.arg_defaults arg_is_positionals = function_signature.arg_is_positionals keyword_names = function_signature.keyword_names function_name = function_signature.function_name args = list(args) for keyword_name in kwargs: if keyword_name not in keyword_names: raise Exception("The name '{}' is not a valid keyword argument " "for the function '{}'.".format( keyword_name, function_name)) # Fill in the remaining arguments. for skipped_name in arg_names[0:len(args)]: if skipped_name in kwargs: raise Exception("Positional and keyword value provided for the " "argument '{}' for the function '{}'".format( keyword_name, function_name)) zipped_info = zip(arg_names, arg_defaults, arg_is_positionals) zipped_info = list(zipped_info)[len(args):] for keyword_name, default_value, is_positional in zipped_info: if keyword_name in kwargs: args.append(kwargs[keyword_name]) else: if default_value != funcsigs._empty: args.append(default_value) else: # This means that there is a missing argument. Unless this is # the last argument and it is a *args argument in which case it # can be omitted. if not is_positional: raise Exception("No value was provided for the argument " "'{}' for the function '{}'.".format( keyword_name, function_name)) no_positionals = len(arg_is_positionals) == 0 or not arg_is_positionals[-1] too_many_arguments = len(args) > len(arg_names) and no_positionals if too_many_arguments: raise Exception("Too many arguments were passed to the function '{}'" .format(function_name)) return args
def extend_args(function_signature, args, kwargs): """Extend the arguments that were passed into a function. This extends the arguments that were passed into a function with the default arguments provided in the function definition. Args: function_signature: The function signature of the function being called. args: The non-keyword arguments passed into the function. kwargs: The keyword arguments passed into the function. Returns: An extended list of arguments to pass into the function. Raises: Exception: An exception may be raised if the function cannot be called with these arguments. """ arg_names = function_signature.arg_names arg_defaults = function_signature.arg_defaults arg_is_positionals = function_signature.arg_is_positionals keyword_names = function_signature.keyword_names function_name = function_signature.function_name args = list(args) for keyword_name in kwargs: if keyword_name not in keyword_names: raise Exception("The name '{}' is not a valid keyword argument " "for the function '{}'.".format( keyword_name, function_name)) # Fill in the remaining arguments. for skipped_name in arg_names[0:len(args)]: if skipped_name in kwargs: raise Exception("Positional and keyword value provided for the " "argument '{}' for the function '{}'".format( keyword_name, function_name)) zipped_info = zip(arg_names, arg_defaults, arg_is_positionals) zipped_info = list(zipped_info)[len(args):] for keyword_name, default_value, is_positional in zipped_info: if keyword_name in kwargs: args.append(kwargs[keyword_name]) else: if default_value != funcsigs._empty: args.append(default_value) else: # This means that there is a missing argument. Unless this is # the last argument and it is a *args argument in which case it # can be omitted. if not is_positional: raise Exception("No value was provided for the argument " "'{}' for the function '{}'.".format( keyword_name, function_name)) no_positionals = len(arg_is_positionals) == 0 or not arg_is_positionals[-1] too_many_arguments = len(args) > len(arg_names) and no_positionals if too_many_arguments: raise Exception("Too many arguments were passed to the function '{}'" .format(function_name)) return args
[ "Extend", "the", "arguments", "that", "were", "passed", "into", "a", "function", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/signature.py#L160-L222
[ "def", "extend_args", "(", "function_signature", ",", "args", ",", "kwargs", ")", ":", "arg_names", "=", "function_signature", ".", "arg_names", "arg_defaults", "=", "function_signature", ".", "arg_defaults", "arg_is_positionals", "=", "function_signature", ".", "arg_...
4eade036a0505e244c976f36aaa2d64386b5129b
train
wait_for_crm_operation
Poll for cloud resource manager operation until finished.
python/ray/autoscaler/gcp/config.py
def wait_for_crm_operation(operation): """Poll for cloud resource manager operation until finished.""" logger.info("wait_for_crm_operation: " "Waiting for operation {} to finish...".format(operation)) for _ in range(MAX_POLLS): result = crm.operations().get(name=operation["name"]).execute() if "error" in result: raise Exception(result["error"]) if "done" in result and result["done"]: logger.info("wait_for_crm_operation: Operation done.") break time.sleep(POLL_INTERVAL) return result
def wait_for_crm_operation(operation): """Poll for cloud resource manager operation until finished.""" logger.info("wait_for_crm_operation: " "Waiting for operation {} to finish...".format(operation)) for _ in range(MAX_POLLS): result = crm.operations().get(name=operation["name"]).execute() if "error" in result: raise Exception(result["error"]) if "done" in result and result["done"]: logger.info("wait_for_crm_operation: Operation done.") break time.sleep(POLL_INTERVAL) return result
[ "Poll", "for", "cloud", "resource", "manager", "operation", "until", "finished", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L36-L52
[ "def", "wait_for_crm_operation", "(", "operation", ")", ":", "logger", ".", "info", "(", "\"wait_for_crm_operation: \"", "\"Waiting for operation {} to finish...\"", ".", "format", "(", "operation", ")", ")", "for", "_", "in", "range", "(", "MAX_POLLS", ")", ":", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
wait_for_compute_global_operation
Poll for global compute operation until finished.
python/ray/autoscaler/gcp/config.py
def wait_for_compute_global_operation(project_name, operation): """Poll for global compute operation until finished.""" logger.info("wait_for_compute_global_operation: " "Waiting for operation {} to finish...".format( operation["name"])) for _ in range(MAX_POLLS): result = compute.globalOperations().get( project=project_name, operation=operation["name"], ).execute() if "error" in result: raise Exception(result["error"]) if result["status"] == "DONE": logger.info("wait_for_compute_global_operation: " "Operation done.") break time.sleep(POLL_INTERVAL) return result
def wait_for_compute_global_operation(project_name, operation): """Poll for global compute operation until finished.""" logger.info("wait_for_compute_global_operation: " "Waiting for operation {} to finish...".format( operation["name"])) for _ in range(MAX_POLLS): result = compute.globalOperations().get( project=project_name, operation=operation["name"], ).execute() if "error" in result: raise Exception(result["error"]) if result["status"] == "DONE": logger.info("wait_for_compute_global_operation: " "Operation done.") break time.sleep(POLL_INTERVAL) return result
[ "Poll", "for", "global", "compute", "operation", "until", "finished", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L55-L76
[ "def", "wait_for_compute_global_operation", "(", "project_name", ",", "operation", ")", ":", "logger", ".", "info", "(", "\"wait_for_compute_global_operation: \"", "\"Waiting for operation {} to finish...\"", ".", "format", "(", "operation", "[", "\"name\"", "]", ")", ")"...
4eade036a0505e244c976f36aaa2d64386b5129b
train
key_pair_name
Returns the ith default gcp_key_pair_name.
python/ray/autoscaler/gcp/config.py
def key_pair_name(i, region, project_id, ssh_user): """Returns the ith default gcp_key_pair_name.""" key_name = "{}_gcp_{}_{}_{}".format(RAY, region, project_id, ssh_user, i) return key_name
def key_pair_name(i, region, project_id, ssh_user): """Returns the ith default gcp_key_pair_name.""" key_name = "{}_gcp_{}_{}_{}".format(RAY, region, project_id, ssh_user, i) return key_name
[ "Returns", "the", "ith", "default", "gcp_key_pair_name", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L79-L82
[ "def", "key_pair_name", "(", "i", ",", "region", ",", "project_id", ",", "ssh_user", ")", ":", "key_name", "=", "\"{}_gcp_{}_{}_{}\"", ".", "format", "(", "RAY", ",", "region", ",", "project_id", ",", "ssh_user", ",", "i", ")", "return", "key_name" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
key_pair_paths
Returns public and private key paths for a given key_name.
python/ray/autoscaler/gcp/config.py
def key_pair_paths(key_name): """Returns public and private key paths for a given key_name.""" public_key_path = os.path.expanduser("~/.ssh/{}.pub".format(key_name)) private_key_path = os.path.expanduser("~/.ssh/{}.pem".format(key_name)) return public_key_path, private_key_path
def key_pair_paths(key_name): """Returns public and private key paths for a given key_name.""" public_key_path = os.path.expanduser("~/.ssh/{}.pub".format(key_name)) private_key_path = os.path.expanduser("~/.ssh/{}.pem".format(key_name)) return public_key_path, private_key_path
[ "Returns", "public", "and", "private", "key", "paths", "for", "a", "given", "key_name", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L85-L89
[ "def", "key_pair_paths", "(", "key_name", ")", ":", "public_key_path", "=", "os", ".", "path", ".", "expanduser", "(", "\"~/.ssh/{}.pub\"", ".", "format", "(", "key_name", ")", ")", "private_key_path", "=", "os", ".", "path", ".", "expanduser", "(", "\"~/.ss...
4eade036a0505e244c976f36aaa2d64386b5129b
train
generate_rsa_key_pair
Create public and private ssh-keys.
python/ray/autoscaler/gcp/config.py
def generate_rsa_key_pair(): """Create public and private ssh-keys.""" key = rsa.generate_private_key( backend=default_backend(), public_exponent=65537, key_size=2048) public_key = key.public_key().public_bytes( serialization.Encoding.OpenSSH, serialization.PublicFormat.OpenSSH).decode("utf-8") pem = key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption()).decode("utf-8") return public_key, pem
def generate_rsa_key_pair(): """Create public and private ssh-keys.""" key = rsa.generate_private_key( backend=default_backend(), public_exponent=65537, key_size=2048) public_key = key.public_key().public_bytes( serialization.Encoding.OpenSSH, serialization.PublicFormat.OpenSSH).decode("utf-8") pem = key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption()).decode("utf-8") return public_key, pem
[ "Create", "public", "and", "private", "ssh", "-", "keys", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L92-L107
[ "def", "generate_rsa_key_pair", "(", ")", ":", "key", "=", "rsa", ".", "generate_private_key", "(", "backend", "=", "default_backend", "(", ")", ",", "public_exponent", "=", "65537", ",", "key_size", "=", "2048", ")", "public_key", "=", "key", ".", "public_k...
4eade036a0505e244c976f36aaa2d64386b5129b
train
_configure_project
Setup a Google Cloud Platform Project. Google Compute Platform organizes all the resources, such as storage buckets, users, and instances under projects. This is different from aws ec2 where everything is global.
python/ray/autoscaler/gcp/config.py
def _configure_project(config): """Setup a Google Cloud Platform Project. Google Compute Platform organizes all the resources, such as storage buckets, users, and instances under projects. This is different from aws ec2 where everything is global. """ project_id = config["provider"].get("project_id") assert config["provider"]["project_id"] is not None, ( "'project_id' must be set in the 'provider' section of the autoscaler" " config. Notice that the project id must be globally unique.") project = _get_project(project_id) if project is None: # Project not found, try creating it _create_project(project_id) project = _get_project(project_id) assert project is not None, "Failed to create project" assert project["lifecycleState"] == "ACTIVE", ( "Project status needs to be ACTIVE, got {}".format( project["lifecycleState"])) config["provider"]["project_id"] = project["projectId"] return config
def _configure_project(config): """Setup a Google Cloud Platform Project. Google Compute Platform organizes all the resources, such as storage buckets, users, and instances under projects. This is different from aws ec2 where everything is global. """ project_id = config["provider"].get("project_id") assert config["provider"]["project_id"] is not None, ( "'project_id' must be set in the 'provider' section of the autoscaler" " config. Notice that the project id must be globally unique.") project = _get_project(project_id) if project is None: # Project not found, try creating it _create_project(project_id) project = _get_project(project_id) assert project is not None, "Failed to create project" assert project["lifecycleState"] == "ACTIVE", ( "Project status needs to be ACTIVE, got {}".format( project["lifecycleState"])) config["provider"]["project_id"] = project["projectId"] return config
[ "Setup", "a", "Google", "Cloud", "Platform", "Project", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L119-L144
[ "def", "_configure_project", "(", "config", ")", ":", "project_id", "=", "config", "[", "\"provider\"", "]", ".", "get", "(", "\"project_id\"", ")", "assert", "config", "[", "\"provider\"", "]", "[", "\"project_id\"", "]", "is", "not", "None", ",", "(", "\...
4eade036a0505e244c976f36aaa2d64386b5129b
train
_configure_iam_role
Setup a gcp service account with IAM roles. Creates a gcp service acconut and binds IAM roles which allow it to control control storage/compute services. Specifically, the head node needs to have an IAM role that allows it to create further gce instances and store items in google cloud storage. TODO: Allow the name/id of the service account to be configured
python/ray/autoscaler/gcp/config.py
def _configure_iam_role(config): """Setup a gcp service account with IAM roles. Creates a gcp service acconut and binds IAM roles which allow it to control control storage/compute services. Specifically, the head node needs to have an IAM role that allows it to create further gce instances and store items in google cloud storage. TODO: Allow the name/id of the service account to be configured """ email = SERVICE_ACCOUNT_EMAIL_TEMPLATE.format( account_id=DEFAULT_SERVICE_ACCOUNT_ID, project_id=config["provider"]["project_id"]) service_account = _get_service_account(email, config) if service_account is None: logger.info("_configure_iam_role: " "Creating new service account {}".format( DEFAULT_SERVICE_ACCOUNT_ID)) service_account = _create_service_account( DEFAULT_SERVICE_ACCOUNT_ID, DEFAULT_SERVICE_ACCOUNT_CONFIG, config) assert service_account is not None, "Failed to create service account" _add_iam_policy_binding(service_account, DEFAULT_SERVICE_ACCOUNT_ROLES) config["head_node"]["serviceAccounts"] = [{ "email": service_account["email"], # NOTE: The amount of access is determined by the scope + IAM # role of the service account. Even if the cloud-platform scope # gives (scope) access to the whole cloud-platform, the service # account is limited by the IAM rights specified below. "scopes": ["https://www.googleapis.com/auth/cloud-platform"] }] return config
def _configure_iam_role(config): """Setup a gcp service account with IAM roles. Creates a gcp service acconut and binds IAM roles which allow it to control control storage/compute services. Specifically, the head node needs to have an IAM role that allows it to create further gce instances and store items in google cloud storage. TODO: Allow the name/id of the service account to be configured """ email = SERVICE_ACCOUNT_EMAIL_TEMPLATE.format( account_id=DEFAULT_SERVICE_ACCOUNT_ID, project_id=config["provider"]["project_id"]) service_account = _get_service_account(email, config) if service_account is None: logger.info("_configure_iam_role: " "Creating new service account {}".format( DEFAULT_SERVICE_ACCOUNT_ID)) service_account = _create_service_account( DEFAULT_SERVICE_ACCOUNT_ID, DEFAULT_SERVICE_ACCOUNT_CONFIG, config) assert service_account is not None, "Failed to create service account" _add_iam_policy_binding(service_account, DEFAULT_SERVICE_ACCOUNT_ROLES) config["head_node"]["serviceAccounts"] = [{ "email": service_account["email"], # NOTE: The amount of access is determined by the scope + IAM # role of the service account. Even if the cloud-platform scope # gives (scope) access to the whole cloud-platform, the service # account is limited by the IAM rights specified below. "scopes": ["https://www.googleapis.com/auth/cloud-platform"] }] return config
[ "Setup", "a", "gcp", "service", "account", "with", "IAM", "roles", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L147-L183
[ "def", "_configure_iam_role", "(", "config", ")", ":", "email", "=", "SERVICE_ACCOUNT_EMAIL_TEMPLATE", ".", "format", "(", "account_id", "=", "DEFAULT_SERVICE_ACCOUNT_ID", ",", "project_id", "=", "config", "[", "\"provider\"", "]", "[", "\"project_id\"", "]", ")", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
_configure_key_pair
Configure SSH access, using an existing key pair if possible. Creates a project-wide ssh key that can be used to access all the instances unless explicitly prohibited by instance config. The ssh-keys created by ray are of format: [USERNAME]:ssh-rsa [KEY_VALUE] [USERNAME] where: [USERNAME] is the user for the SSH key, specified in the config. [KEY_VALUE] is the public SSH key value.
python/ray/autoscaler/gcp/config.py
def _configure_key_pair(config): """Configure SSH access, using an existing key pair if possible. Creates a project-wide ssh key that can be used to access all the instances unless explicitly prohibited by instance config. The ssh-keys created by ray are of format: [USERNAME]:ssh-rsa [KEY_VALUE] [USERNAME] where: [USERNAME] is the user for the SSH key, specified in the config. [KEY_VALUE] is the public SSH key value. """ if "ssh_private_key" in config["auth"]: return config ssh_user = config["auth"]["ssh_user"] project = compute.projects().get( project=config["provider"]["project_id"]).execute() # Key pairs associated with project meta data. The key pairs are general, # and not just ssh keys. ssh_keys_str = next( (item for item in project["commonInstanceMetadata"].get("items", []) if item["key"] == "ssh-keys"), {}).get("value", "") ssh_keys = ssh_keys_str.split("\n") if ssh_keys_str else [] # Try a few times to get or create a good key pair. key_found = False for i in range(10): key_name = key_pair_name(i, config["provider"]["region"], config["provider"]["project_id"], ssh_user) public_key_path, private_key_path = key_pair_paths(key_name) for ssh_key in ssh_keys: key_parts = ssh_key.split(" ") if len(key_parts) != 3: continue if key_parts[2] == ssh_user and os.path.exists(private_key_path): # Found a key key_found = True break # Create a key since it doesn't exist locally or in GCP if not key_found and not os.path.exists(private_key_path): logger.info("_configure_key_pair: " "Creating new key pair {}".format(key_name)) public_key, private_key = generate_rsa_key_pair() _create_project_ssh_key_pair(project, public_key, ssh_user) with open(private_key_path, "w") as f: f.write(private_key) os.chmod(private_key_path, 0o600) with open(public_key_path, "w") as f: f.write(public_key) key_found = True break if key_found: break assert key_found, "SSH keypair for user {} not found for {}".format( ssh_user, private_key_path) assert os.path.exists(private_key_path), ( "Private key file {} not found for user {}" "".format(private_key_path, ssh_user)) logger.info("_configure_key_pair: " "Private key not specified in config, using" "{}".format(private_key_path)) config["auth"]["ssh_private_key"] = private_key_path return config
def _configure_key_pair(config): """Configure SSH access, using an existing key pair if possible. Creates a project-wide ssh key that can be used to access all the instances unless explicitly prohibited by instance config. The ssh-keys created by ray are of format: [USERNAME]:ssh-rsa [KEY_VALUE] [USERNAME] where: [USERNAME] is the user for the SSH key, specified in the config. [KEY_VALUE] is the public SSH key value. """ if "ssh_private_key" in config["auth"]: return config ssh_user = config["auth"]["ssh_user"] project = compute.projects().get( project=config["provider"]["project_id"]).execute() # Key pairs associated with project meta data. The key pairs are general, # and not just ssh keys. ssh_keys_str = next( (item for item in project["commonInstanceMetadata"].get("items", []) if item["key"] == "ssh-keys"), {}).get("value", "") ssh_keys = ssh_keys_str.split("\n") if ssh_keys_str else [] # Try a few times to get or create a good key pair. key_found = False for i in range(10): key_name = key_pair_name(i, config["provider"]["region"], config["provider"]["project_id"], ssh_user) public_key_path, private_key_path = key_pair_paths(key_name) for ssh_key in ssh_keys: key_parts = ssh_key.split(" ") if len(key_parts) != 3: continue if key_parts[2] == ssh_user and os.path.exists(private_key_path): # Found a key key_found = True break # Create a key since it doesn't exist locally or in GCP if not key_found and not os.path.exists(private_key_path): logger.info("_configure_key_pair: " "Creating new key pair {}".format(key_name)) public_key, private_key = generate_rsa_key_pair() _create_project_ssh_key_pair(project, public_key, ssh_user) with open(private_key_path, "w") as f: f.write(private_key) os.chmod(private_key_path, 0o600) with open(public_key_path, "w") as f: f.write(public_key) key_found = True break if key_found: break assert key_found, "SSH keypair for user {} not found for {}".format( ssh_user, private_key_path) assert os.path.exists(private_key_path), ( "Private key file {} not found for user {}" "".format(private_key_path, ssh_user)) logger.info("_configure_key_pair: " "Private key not specified in config, using" "{}".format(private_key_path)) config["auth"]["ssh_private_key"] = private_key_path return config
[ "Configure", "SSH", "access", "using", "an", "existing", "key", "pair", "if", "possible", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L186-L269
[ "def", "_configure_key_pair", "(", "config", ")", ":", "if", "\"ssh_private_key\"", "in", "config", "[", "\"auth\"", "]", ":", "return", "config", "ssh_user", "=", "config", "[", "\"auth\"", "]", "[", "\"ssh_user\"", "]", "project", "=", "compute", ".", "pro...
4eade036a0505e244c976f36aaa2d64386b5129b
train
_configure_subnet
Pick a reasonable subnet if not specified by the config.
python/ray/autoscaler/gcp/config.py
def _configure_subnet(config): """Pick a reasonable subnet if not specified by the config.""" # Rationale: avoid subnet lookup if the network is already # completely manually configured if ("networkInterfaces" in config["head_node"] and "networkInterfaces" in config["worker_nodes"]): return config subnets = _list_subnets(config) if not subnets: raise NotImplementedError("Should be able to create subnet.") # TODO: make sure that we have usable subnet. Maybe call # compute.subnetworks().listUsable? For some reason it didn't # work out-of-the-box default_subnet = subnets[0] if "networkInterfaces" not in config["head_node"]: config["head_node"]["networkInterfaces"] = [{ "subnetwork": default_subnet["selfLink"], "accessConfigs": [{ "name": "External NAT", "type": "ONE_TO_ONE_NAT", }], }] if "networkInterfaces" not in config["worker_nodes"]: config["worker_nodes"]["networkInterfaces"] = [{ "subnetwork": default_subnet["selfLink"], "accessConfigs": [{ "name": "External NAT", "type": "ONE_TO_ONE_NAT", }], }] return config
def _configure_subnet(config): """Pick a reasonable subnet if not specified by the config.""" # Rationale: avoid subnet lookup if the network is already # completely manually configured if ("networkInterfaces" in config["head_node"] and "networkInterfaces" in config["worker_nodes"]): return config subnets = _list_subnets(config) if not subnets: raise NotImplementedError("Should be able to create subnet.") # TODO: make sure that we have usable subnet. Maybe call # compute.subnetworks().listUsable? For some reason it didn't # work out-of-the-box default_subnet = subnets[0] if "networkInterfaces" not in config["head_node"]: config["head_node"]["networkInterfaces"] = [{ "subnetwork": default_subnet["selfLink"], "accessConfigs": [{ "name": "External NAT", "type": "ONE_TO_ONE_NAT", }], }] if "networkInterfaces" not in config["worker_nodes"]: config["worker_nodes"]["networkInterfaces"] = [{ "subnetwork": default_subnet["selfLink"], "accessConfigs": [{ "name": "External NAT", "type": "ONE_TO_ONE_NAT", }], }] return config
[ "Pick", "a", "reasonable", "subnet", "if", "not", "specified", "by", "the", "config", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L272-L309
[ "def", "_configure_subnet", "(", "config", ")", ":", "# Rationale: avoid subnet lookup if the network is already", "# completely manually configured", "if", "(", "\"networkInterfaces\"", "in", "config", "[", "\"head_node\"", "]", "and", "\"networkInterfaces\"", "in", "config", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
_add_iam_policy_binding
Add new IAM roles for the service account.
python/ray/autoscaler/gcp/config.py
def _add_iam_policy_binding(service_account, roles): """Add new IAM roles for the service account.""" project_id = service_account["projectId"] email = service_account["email"] member_id = "serviceAccount:" + email policy = crm.projects().getIamPolicy(resource=project_id).execute() already_configured = True for role in roles: role_exists = False for binding in policy["bindings"]: if binding["role"] == role: if member_id not in binding["members"]: binding["members"].append(member_id) already_configured = False role_exists = True if not role_exists: already_configured = False policy["bindings"].append({ "members": [member_id], "role": role, }) if already_configured: # In some managed environments, an admin needs to grant the # roles, so only call setIamPolicy if needed. return result = crm.projects().setIamPolicy( resource=project_id, body={ "policy": policy, }).execute() return result
def _add_iam_policy_binding(service_account, roles): """Add new IAM roles for the service account.""" project_id = service_account["projectId"] email = service_account["email"] member_id = "serviceAccount:" + email policy = crm.projects().getIamPolicy(resource=project_id).execute() already_configured = True for role in roles: role_exists = False for binding in policy["bindings"]: if binding["role"] == role: if member_id not in binding["members"]: binding["members"].append(member_id) already_configured = False role_exists = True if not role_exists: already_configured = False policy["bindings"].append({ "members": [member_id], "role": role, }) if already_configured: # In some managed environments, an admin needs to grant the # roles, so only call setIamPolicy if needed. return result = crm.projects().setIamPolicy( resource=project_id, body={ "policy": policy, }).execute() return result
[ "Add", "new", "IAM", "roles", "for", "the", "service", "account", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L380-L416
[ "def", "_add_iam_policy_binding", "(", "service_account", ",", "roles", ")", ":", "project_id", "=", "service_account", "[", "\"projectId\"", "]", "email", "=", "service_account", "[", "\"email\"", "]", "member_id", "=", "\"serviceAccount:\"", "+", "email", "policy"...
4eade036a0505e244c976f36aaa2d64386b5129b
train
_create_project_ssh_key_pair
Inserts an ssh-key into project commonInstanceMetadata
python/ray/autoscaler/gcp/config.py
def _create_project_ssh_key_pair(project, public_key, ssh_user): """Inserts an ssh-key into project commonInstanceMetadata""" key_parts = public_key.split(" ") # Sanity checks to make sure that the generated key matches expectation assert len(key_parts) == 2, key_parts assert key_parts[0] == "ssh-rsa", key_parts new_ssh_meta = "{ssh_user}:ssh-rsa {key_value} {ssh_user}".format( ssh_user=ssh_user, key_value=key_parts[1]) common_instance_metadata = project["commonInstanceMetadata"] items = common_instance_metadata.get("items", []) ssh_keys_i = next( (i for i, item in enumerate(items) if item["key"] == "ssh-keys"), None) if ssh_keys_i is None: items.append({"key": "ssh-keys", "value": new_ssh_meta}) else: ssh_keys = items[ssh_keys_i] ssh_keys["value"] += "\n" + new_ssh_meta items[ssh_keys_i] = ssh_keys common_instance_metadata["items"] = items operation = compute.projects().setCommonInstanceMetadata( project=project["name"], body=common_instance_metadata).execute() response = wait_for_compute_global_operation(project["name"], operation) return response
def _create_project_ssh_key_pair(project, public_key, ssh_user): """Inserts an ssh-key into project commonInstanceMetadata""" key_parts = public_key.split(" ") # Sanity checks to make sure that the generated key matches expectation assert len(key_parts) == 2, key_parts assert key_parts[0] == "ssh-rsa", key_parts new_ssh_meta = "{ssh_user}:ssh-rsa {key_value} {ssh_user}".format( ssh_user=ssh_user, key_value=key_parts[1]) common_instance_metadata = project["commonInstanceMetadata"] items = common_instance_metadata.get("items", []) ssh_keys_i = next( (i for i, item in enumerate(items) if item["key"] == "ssh-keys"), None) if ssh_keys_i is None: items.append({"key": "ssh-keys", "value": new_ssh_meta}) else: ssh_keys = items[ssh_keys_i] ssh_keys["value"] += "\n" + new_ssh_meta items[ssh_keys_i] = ssh_keys common_instance_metadata["items"] = items operation = compute.projects().setCommonInstanceMetadata( project=project["name"], body=common_instance_metadata).execute() response = wait_for_compute_global_operation(project["name"], operation) return response
[ "Inserts", "an", "ssh", "-", "key", "into", "project", "commonInstanceMetadata" ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/gcp/config.py#L419-L451
[ "def", "_create_project_ssh_key_pair", "(", "project", ",", "public_key", ",", "ssh_user", ")", ":", "key_parts", "=", "public_key", ".", "split", "(", "\" \"", ")", "# Sanity checks to make sure that the generated key matches expectation", "assert", "len", "(", "key_part...
4eade036a0505e244c976f36aaa2d64386b5129b
train
RemoteFunction._remote
An experimental alternate way to submit remote functions.
python/ray/remote_function.py
def _remote(self, args=None, kwargs=None, num_return_vals=None, num_cpus=None, num_gpus=None, resources=None): """An experimental alternate way to submit remote functions.""" worker = ray.worker.get_global_worker() worker.check_connected() if self._last_export_session < worker._session_index: # If this function was exported in a previous session, we need to # export this function again, because current GCS doesn't have it. self._last_export_session = worker._session_index worker.function_actor_manager.export(self) kwargs = {} if kwargs is None else kwargs args = [] if args is None else args args = ray.signature.extend_args(self._function_signature, args, kwargs) if num_return_vals is None: num_return_vals = self._num_return_vals resources = ray.utils.resources_from_resource_arguments( self._num_cpus, self._num_gpus, self._resources, num_cpus, num_gpus, resources) if worker.mode == ray.worker.LOCAL_MODE: # In LOCAL_MODE, remote calls simply execute the function. # We copy the arguments to prevent the function call from # mutating them and to match the usual behavior of # immutable remote objects. result = self._function(*copy.deepcopy(args)) return result object_ids = worker.submit_task( self._function_descriptor, args, num_return_vals=num_return_vals, resources=resources) if len(object_ids) == 1: return object_ids[0] elif len(object_ids) > 1: return object_ids
def _remote(self, args=None, kwargs=None, num_return_vals=None, num_cpus=None, num_gpus=None, resources=None): """An experimental alternate way to submit remote functions.""" worker = ray.worker.get_global_worker() worker.check_connected() if self._last_export_session < worker._session_index: # If this function was exported in a previous session, we need to # export this function again, because current GCS doesn't have it. self._last_export_session = worker._session_index worker.function_actor_manager.export(self) kwargs = {} if kwargs is None else kwargs args = [] if args is None else args args = ray.signature.extend_args(self._function_signature, args, kwargs) if num_return_vals is None: num_return_vals = self._num_return_vals resources = ray.utils.resources_from_resource_arguments( self._num_cpus, self._num_gpus, self._resources, num_cpus, num_gpus, resources) if worker.mode == ray.worker.LOCAL_MODE: # In LOCAL_MODE, remote calls simply execute the function. # We copy the arguments to prevent the function call from # mutating them and to match the usual behavior of # immutable remote objects. result = self._function(*copy.deepcopy(args)) return result object_ids = worker.submit_task( self._function_descriptor, args, num_return_vals=num_return_vals, resources=resources) if len(object_ids) == 1: return object_ids[0] elif len(object_ids) > 1: return object_ids
[ "An", "experimental", "alternate", "way", "to", "submit", "remote", "functions", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/remote_function.py#L92-L135
[ "def", "_remote", "(", "self", ",", "args", "=", "None", ",", "kwargs", "=", "None", ",", "num_return_vals", "=", "None", ",", "num_cpus", "=", "None", ",", "num_gpus", "=", "None", ",", "resources", "=", "None", ")", ":", "worker", "=", "ray", ".", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
PlasmaObjectLinkedList.append
Append an object to the linked list. Args: future (PlasmaObjectFuture): A PlasmaObjectFuture instance.
python/ray/experimental/async_plasma.py
def append(self, future): """Append an object to the linked list. Args: future (PlasmaObjectFuture): A PlasmaObjectFuture instance. """ future.prev = self.tail if self.tail is None: assert self.head is None self.head = future else: self.tail.next = future self.tail = future # Once done, it will be removed from the list. future.add_done_callback(self.remove)
def append(self, future): """Append an object to the linked list. Args: future (PlasmaObjectFuture): A PlasmaObjectFuture instance. """ future.prev = self.tail if self.tail is None: assert self.head is None self.head = future else: self.tail.next = future self.tail = future # Once done, it will be removed from the list. future.add_done_callback(self.remove)
[ "Append", "an", "object", "to", "the", "linked", "list", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_plasma.py#L97-L111
[ "def", "append", "(", "self", ",", "future", ")", ":", "future", ".", "prev", "=", "self", ".", "tail", "if", "self", ".", "tail", "is", "None", ":", "assert", "self", ".", "head", "is", "None", "self", ".", "head", "=", "future", "else", ":", "s...
4eade036a0505e244c976f36aaa2d64386b5129b
train
PlasmaObjectLinkedList.remove
Remove an object from the linked list. Args: future (PlasmaObjectFuture): A PlasmaObjectFuture instance.
python/ray/experimental/async_plasma.py
def remove(self, future): """Remove an object from the linked list. Args: future (PlasmaObjectFuture): A PlasmaObjectFuture instance. """ if self._loop.get_debug(): logger.debug("Removing %s from the linked list.", future) if future.prev is None: assert future is self.head self.head = future.next if self.head is None: self.tail = None if not self.cancelled(): self.set_result(None) else: self.head.prev = None elif future.next is None: assert future is self.tail self.tail = future.prev if self.tail is None: self.head = None if not self.cancelled(): self.set_result(None) else: self.tail.prev = None
def remove(self, future): """Remove an object from the linked list. Args: future (PlasmaObjectFuture): A PlasmaObjectFuture instance. """ if self._loop.get_debug(): logger.debug("Removing %s from the linked list.", future) if future.prev is None: assert future is self.head self.head = future.next if self.head is None: self.tail = None if not self.cancelled(): self.set_result(None) else: self.head.prev = None elif future.next is None: assert future is self.tail self.tail = future.prev if self.tail is None: self.head = None if not self.cancelled(): self.set_result(None) else: self.tail.prev = None
[ "Remove", "an", "object", "from", "the", "linked", "list", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_plasma.py#L113-L138
[ "def", "remove", "(", "self", ",", "future", ")", ":", "if", "self", ".", "_loop", ".", "get_debug", "(", ")", ":", "logger", ".", "debug", "(", "\"Removing %s from the linked list.\"", ",", "future", ")", "if", "future", ".", "prev", "is", "None", ":", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
PlasmaObjectLinkedList.cancel
Manually cancel all tasks assigned to this event loop.
python/ray/experimental/async_plasma.py
def cancel(self, *args, **kwargs): """Manually cancel all tasks assigned to this event loop.""" # Because remove all futures will trigger `set_result`, # we cancel itself first. super().cancel() for future in self.traverse(): # All cancelled futures should have callbacks to removed itself # from this linked list. However, these callbacks are scheduled in # an event loop, so we could still find them in our list. if not future.cancelled(): future.cancel()
def cancel(self, *args, **kwargs): """Manually cancel all tasks assigned to this event loop.""" # Because remove all futures will trigger `set_result`, # we cancel itself first. super().cancel() for future in self.traverse(): # All cancelled futures should have callbacks to removed itself # from this linked list. However, these callbacks are scheduled in # an event loop, so we could still find them in our list. if not future.cancelled(): future.cancel()
[ "Manually", "cancel", "all", "tasks", "assigned", "to", "this", "event", "loop", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_plasma.py#L140-L150
[ "def", "cancel", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# Because remove all futures will trigger `set_result`,", "# we cancel itself first.", "super", "(", ")", ".", "cancel", "(", ")", "for", "future", "in", "self", ".", "traverse",...
4eade036a0505e244c976f36aaa2d64386b5129b
train
PlasmaObjectLinkedList.set_result
Complete all tasks.
python/ray/experimental/async_plasma.py
def set_result(self, result): """Complete all tasks. """ for future in self.traverse(): # All cancelled futures should have callbacks to removed itself # from this linked list. However, these callbacks are scheduled in # an event loop, so we could still find them in our list. future.set_result(result) if not self.done(): super().set_result(result)
def set_result(self, result): """Complete all tasks. """ for future in self.traverse(): # All cancelled futures should have callbacks to removed itself # from this linked list. However, these callbacks are scheduled in # an event loop, so we could still find them in our list. future.set_result(result) if not self.done(): super().set_result(result)
[ "Complete", "all", "tasks", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_plasma.py#L152-L160
[ "def", "set_result", "(", "self", ",", "result", ")", ":", "for", "future", "in", "self", ".", "traverse", "(", ")", ":", "# All cancelled futures should have callbacks to removed itself", "# from this linked list. However, these callbacks are scheduled in", "# an event loop, s...
4eade036a0505e244c976f36aaa2d64386b5129b
train
PlasmaObjectLinkedList.traverse
Traverse this linked list. Yields: PlasmaObjectFuture: PlasmaObjectFuture instances.
python/ray/experimental/async_plasma.py
def traverse(self): """Traverse this linked list. Yields: PlasmaObjectFuture: PlasmaObjectFuture instances. """ current = self.head while current is not None: yield current current = current.next
def traverse(self): """Traverse this linked list. Yields: PlasmaObjectFuture: PlasmaObjectFuture instances. """ current = self.head while current is not None: yield current current = current.next
[ "Traverse", "this", "linked", "list", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_plasma.py#L162-L171
[ "def", "traverse", "(", "self", ")", ":", "current", "=", "self", ".", "head", "while", "current", "is", "not", "None", ":", "yield", "current", "current", "=", "current", ".", "next" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
PlasmaEventHandler.process_notifications
Process notifications.
python/ray/experimental/async_plasma.py
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): """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)
[ "Process", "notifications", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_plasma.py#L183-L188
[ "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_lis...
4eade036a0505e244c976f36aaa2d64386b5129b
train
PlasmaEventHandler.as_future
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.
python/ray/experimental/async_plasma.py
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): """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
[ "Turn", "an", "object_id", "into", "a", "Future", "object", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/async_plasma.py#L205-L237
[ "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_...
4eade036a0505e244c976f36aaa2d64386b5129b
train
TuneClient.get_all_trials
Returns a list of all trials' information.
python/ray/tune/web_server.py
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): """Returns a list of all trials' information.""" response = requests.get(urljoin(self._path, "trials")) return self._deserialize(response)
[ "Returns", "a", "list", "of", "all", "trials", "information", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/web_server.py#L48-L51
[ "def", "get_all_trials", "(", "self", ")", ":", "response", "=", "requests", ".", "get", "(", "urljoin", "(", "self", ".", "_path", ",", "\"trials\"", ")", ")", "return", "self", ".", "_deserialize", "(", "response", ")" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
TuneClient.get_trial
Returns trial information by trial_id.
python/ray/tune/web_server.py
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): """Returns trial information by trial_id.""" response = requests.get( urljoin(self._path, "trials/{}".format(trial_id))) return self._deserialize(response)
[ "Returns", "trial", "information", "by", "trial_id", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/web_server.py#L53-L57
[ "def", "get_trial", "(", "self", ",", "trial_id", ")", ":", "response", "=", "requests", ".", "get", "(", "urljoin", "(", "self", ".", "_path", ",", "\"trials/{}\"", ".", "format", "(", "trial_id", ")", ")", ")", "return", "self", ".", "_deserialize", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
TuneClient.add_trial
Adds a trial by name and specification (dict).
python/ray/tune/web_server.py
def add_trial(self, name, specification): """Adds a trial by name and specification (dict).""" payload = {"name": name, "spec": specification} response = requests.post(urljoin(self._path, "trials"), json=payload) return self._deserialize(response)
def add_trial(self, name, specification): """Adds a trial by name and specification (dict).""" payload = {"name": name, "spec": specification} response = requests.post(urljoin(self._path, "trials"), json=payload) return self._deserialize(response)
[ "Adds", "a", "trial", "by", "name", "and", "specification", "(", "dict", ")", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/web_server.py#L59-L63
[ "def", "add_trial", "(", "self", ",", "name", ",", "specification", ")", ":", "payload", "=", "{", "\"name\"", ":", "name", ",", "\"spec\"", ":", "specification", "}", "response", "=", "requests", ".", "post", "(", "urljoin", "(", "self", ".", "_path", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
TuneClient.stop_trial
Requests to stop trial by trial_id.
python/ray/tune/web_server.py
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): """Requests to stop trial by trial_id.""" response = requests.put( urljoin(self._path, "trials/{}".format(trial_id))) return self._deserialize(response)
[ "Requests", "to", "stop", "trial", "by", "trial_id", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/web_server.py#L65-L69
[ "def", "stop_trial", "(", "self", ",", "trial_id", ")", ":", "response", "=", "requests", ".", "put", "(", "urljoin", "(", "self", ".", "_path", ",", "\"trials/{}\"", ".", "format", "(", "trial_id", ")", ")", ")", "return", "self", ".", "_deserialize", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
DistributedSGD.foreach_worker
Apply the given function to each remote worker. Returns: List of results from applying the function.
python/ray/experimental/sgd/sgd.py
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): """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
[ "Apply", "the", "given", "function", "to", "each", "remote", "worker", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/sgd.py#L130-L137
[ "def", "foreach_worker", "(", "self", ",", "fn", ")", ":", "results", "=", "ray", ".", "get", "(", "[", "w", ".", "foreach_worker", ".", "remote", "(", "fn", ")", "for", "w", "in", "self", ".", "workers", "]", ")", "return", "results" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
DistributedSGD.foreach_model
Apply the given function to each model replica in each worker. Returns: List of results from applying the function.
python/ray/experimental/sgd/sgd.py
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): """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
[ "Apply", "the", "given", "function", "to", "each", "model", "replica", "in", "each", "worker", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/sgd.py#L139-L150
[ "def", "foreach_model", "(", "self", ",", "fn", ")", ":", "results", "=", "ray", ".", "get", "(", "[", "w", ".", "foreach_model", ".", "remote", "(", "fn", ")", "for", "w", "in", "self", ".", "workers", "]", ")", "out", "=", "[", "]", "for", "r...
4eade036a0505e244c976f36aaa2d64386b5129b
train
DistributedSGD.for_model
Apply the given function to a single model replica. Returns: Result from applying the function.
python/ray/experimental/sgd/sgd.py
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): """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))
[ "Apply", "the", "given", "function", "to", "a", "single", "model", "replica", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/sgd.py#L152-L158
[ "def", "for_model", "(", "self", ",", "fn", ")", ":", "return", "ray", ".", "get", "(", "self", ".", "workers", "[", "0", "]", ".", "for_model", ".", "remote", "(", "fn", ")", ")" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
DistributedSGD.step
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.
python/ray/experimental/sgd/sgd.py
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): """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)
[ "Run", "a", "single", "SGD", "step", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/sgd.py#L160-L174
[ "def", "step", "(", "self", ",", "fetch_stats", "=", "False", ")", ":", "if", "self", ".", "strategy", "==", "\"ps\"", ":", "return", "_distributed_sgd_step", "(", "self", ".", "workers", ",", "self", ".", "ps_list", ",", "write_timeline", "=", "False", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
start_router
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.
python/ray/experimental/serve/router/__init__.py
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): """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
[ "Wrapper", "for", "starting", "a", "router", "and", "register", "it", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/serve/router/__init__.py#L10-L23
[ "def", "start_router", "(", "router_class", ",", "router_name", ")", ":", "handle", "=", "router_class", ".", "remote", "(", "router_name", ")", "ray", ".", "experimental", ".", "register_actor", "(", "router_name", ",", "handle", ")", "handle", ".", "start", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
SearchSpace.generate_random_one_hot_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.
python/ray/tune/automl/search_space.py
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): """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
[ "Returns", "a", "list", "of", "one", "-", "hot", "encodings", "for", "all", "parameters", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/automl/search_space.py#L153-L165
[ "def", "generate_random_one_hot_encoding", "(", "self", ")", ":", "encoding", "=", "[", "]", "for", "ps", "in", "self", ".", "param_list", ":", "one_hot", "=", "np", ".", "zeros", "(", "ps", ".", "choices_count", "(", ")", ")", "choice", "=", "random", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
SearchSpace.apply_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
python/ray/tune/automl/search_space.py
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): """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
[ "Apply", "one", "hot", "encoding", "to", "generate", "a", "specific", "config", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/automl/search_space.py#L167-L183
[ "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", "(",...
4eade036a0505e244c976f36aaa2d64386b5129b
train
pin_in_object_store
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.
python/ray/tune/util.py
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): """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"))
[ "Pin", "an", "object", "in", "the", "object", "store", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/util.py#L19-L30
[ "def", "pin_in_object_store", "(", "obj", ")", ":", "obj_id", "=", "ray", ".", "put", "(", "_to_pinnable", "(", "obj", ")", ")", "_pinned_objects", ".", "append", "(", "ray", ".", "get", "(", "obj_id", ")", ")", "return", "\"{}{}\"", ".", "format", "("...
4eade036a0505e244c976f36aaa2d64386b5129b
train
get_pinned_object
Retrieve a pinned object from the object store.
python/ray/tune/util.py
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): """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):]))))
[ "Retrieve", "a", "pinned", "object", "from", "the", "object", "store", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/util.py#L33-L40
[ "def", "get_pinned_object", "(", "pinned_id", ")", ":", "from", "ray", "import", "ObjectID", "return", "_from_pinnable", "(", "ray", ".", "get", "(", "ObjectID", "(", "base64", ".", "b64decode", "(", "pinned_id", "[", "len", "(", "PINNED_OBJECT_PREFIX", ")", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
merge_dicts
Returns a new dict that is d1 and d2 deep merged.
python/ray/tune/util.py
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): """Returns a new dict that is d1 and d2 deep merged.""" merged = copy.deepcopy(d1) deep_update(merged, d2, True, []) return merged
[ "Returns", "a", "new", "dict", "that", "is", "d1", "and", "d2", "deep", "merged", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/util.py#L65-L69
[ "def", "merge_dicts", "(", "d1", ",", "d2", ")", ":", "merged", "=", "copy", ".", "deepcopy", "(", "d1", ")", "deep_update", "(", "merged", ",", "d2", ",", "True", ",", "[", "]", ")", "return", "merged" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
deep_update
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.
python/ray/tune/util.py
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): """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
[ "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", "...
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/util.py#L72-L97
[ "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"...
4eade036a0505e244c976f36aaa2d64386b5129b
train
TaskPool.completed_prefetch
Similar to completed but only returns once the object is local. Assumes obj_id only is one id.
python/ray/rllib/utils/actors.py
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): """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
[ "Similar", "to", "completed", "but", "only", "returns", "once", "the", "object", "is", "local", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/actors.py#L37-L59
[ "def", "completed_prefetch", "(", "self", ",", "blocking_wait", "=", "False", ",", "max_yield", "=", "999", ")", ":", "for", "worker", ",", "obj_id", "in", "self", ".", "completed", "(", "blocking_wait", "=", "blocking_wait", ")", ":", "plasma_id", "=", "r...
4eade036a0505e244c976f36aaa2d64386b5129b
train
TaskPool.reset_evaluators
Notify that some evaluators may be removed.
python/ray/rllib/utils/actors.py
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): """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
[ "Notify", "that", "some", "evaluators", "may", "be", "removed", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/actors.py#L61-L71
[ "def", "reset_evaluators", "(", "self", ",", "evaluators", ")", ":", "for", "obj_id", ",", "ev", "in", "self", ".", "_tasks", ".", "copy", "(", ")", ".", "items", "(", ")", ":", "if", "ev", "not", "in", "evaluators", ":", "del", "self", ".", "_task...
4eade036a0505e244c976f36aaa2d64386b5129b
train
AggregationWorkerBase.iter_train_batches
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.
python/ray/rllib/optimizers/aso_aggregator.py
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): """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())
[ "Iterate", "over", "train", "batches", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/optimizers/aso_aggregator.py#L92-L131
[ "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", ",", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
create_or_update_cluster
Create or updates an autoscaling Ray cluster from a config json.
python/ray/autoscaler/commands.py
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): """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)
[ "Create", "or", "updates", "an", "autoscaling", "Ray", "cluster", "from", "a", "config", "json", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L34-L47
[ "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", "(", "conf...
4eade036a0505e244c976f36aaa2d64386b5129b
train
teardown_cluster
Destroys all nodes of a Ray cluster described by a config json.
python/ray/autoscaler/commands.py
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): """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()
[ "Destroys", "all", "nodes", "of", "a", "Ray", "cluster", "described", "by", "a", "config", "json", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L73-L116
[ "def", "teardown_cluster", "(", "config_file", ",", "yes", ",", "workers_only", ",", "override_cluster_name", ")", ":", "config", "=", "yaml", ".", "load", "(", "open", "(", "config_file", ")", ".", "read", "(", ")", ")", "if", "override_cluster_name", "is",...
4eade036a0505e244c976f36aaa2d64386b5129b
train
kill_node
Kills a random Raylet worker.
python/ray/autoscaler/commands.py
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): """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
[ "Kills", "a", "random", "Raylet", "worker", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L119-L157
[ "def", "kill_node", "(", "config_file", ",", "yes", ",", "override_cluster_name", ")", ":", "config", "=", "yaml", ".", "load", "(", "open", "(", "config_file", ")", ".", "read", "(", ")", ")", "if", "override_cluster_name", "is", "not", "None", ":", "co...
4eade036a0505e244c976f36aaa2d64386b5129b
train
get_or_create_head_node
Create the cluster head node, which in turn creates the workers.
python/ray/autoscaler/commands.py
def get_or_create_head_node(config, config_file, no_restart, restart_only, yes, override_cluster_name): """Create the cluster head node, which in turn creates the workers.""" provider = get_node_provider(config["provider"], config["cluster_name"]) try: head_node_tags = { TAG_RAY_NODE_TYPE: "head", } nodes = provider.non_terminated_nodes(head_node_tags) if len(nodes) > 0: head_node = nodes[0] else: head_node = None if not head_node: confirm("This will create a new cluster", yes) elif not no_restart: confirm("This will restart cluster services", yes) launch_hash = hash_launch_conf(config["head_node"], config["auth"]) if head_node is None or provider.node_tags(head_node).get( TAG_RAY_LAUNCH_CONFIG) != launch_hash: if head_node is not None: confirm("Head node config out-of-date. It will be terminated", yes) logger.info( "get_or_create_head_node: " "Terminating outdated head node {}".format(head_node)) provider.terminate_node(head_node) logger.info("get_or_create_head_node: Launching new head node...") head_node_tags[TAG_RAY_LAUNCH_CONFIG] = launch_hash head_node_tags[TAG_RAY_NODE_NAME] = "ray-{}-head".format( config["cluster_name"]) provider.create_node(config["head_node"], head_node_tags, 1) nodes = provider.non_terminated_nodes(head_node_tags) assert len(nodes) == 1, "Failed to create head node." head_node = nodes[0] # TODO(ekl) right now we always update the head node even if the hash # matches. We could prompt the user for what they want to do here. runtime_hash = hash_runtime_conf(config["file_mounts"], config) logger.info("get_or_create_head_node: Updating files on head node...") # Rewrite the auth config so that the head node can update the workers remote_key_path = "~/ray_bootstrap_key.pem" remote_config = copy.deepcopy(config) remote_config["auth"]["ssh_private_key"] = remote_key_path # Adjust for new file locations new_mounts = {} for remote_path in config["file_mounts"]: new_mounts[remote_path] = remote_path remote_config["file_mounts"] = new_mounts remote_config["no_restart"] = no_restart # Now inject the rewritten config and SSH key into the head node remote_config_file = tempfile.NamedTemporaryFile( "w", prefix="ray-bootstrap-") remote_config_file.write(json.dumps(remote_config)) remote_config_file.flush() config["file_mounts"].update({ remote_key_path: config["auth"]["ssh_private_key"], "~/ray_bootstrap_config.yaml": remote_config_file.name }) if restart_only: init_commands = config["head_start_ray_commands"] elif no_restart: init_commands = config["head_setup_commands"] else: init_commands = (config["head_setup_commands"] + config["head_start_ray_commands"]) 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=config["initialization_commands"], setup_commands=init_commands, runtime_hash=runtime_hash, ) updater.start() updater.join() # Refresh the node cache so we see the external ip if available provider.non_terminated_nodes(head_node_tags) 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) if updater.exitcode != 0: logger.error("get_or_create_head_node: " "Updating {} failed".format(head_node_ip)) sys.exit(1) logger.info( "get_or_create_head_node: " "Head node up-to-date, IP address is: {}".format(head_node_ip)) monitor_str = "tail -n 100 -f /tmp/ray/session_*/logs/monitor*" use_docker = bool(config["docker"]["container_name"]) if override_cluster_name: modifiers = " --cluster-name={}".format( quote(override_cluster_name)) else: modifiers = "" print("To monitor auto-scaling activity, you can run:\n\n" " ray exec {} {}{}{}\n".format( config_file, "--docker " if use_docker else " ", quote(monitor_str), modifiers)) print("To open a console on the cluster:\n\n" " ray attach {}{}\n".format(config_file, modifiers)) print("To ssh manually to the cluster, run:\n\n" " ssh -i {} {}@{}\n".format(config["auth"]["ssh_private_key"], config["auth"]["ssh_user"], head_node_ip)) finally: provider.cleanup()
def get_or_create_head_node(config, config_file, no_restart, restart_only, yes, override_cluster_name): """Create the cluster head node, which in turn creates the workers.""" provider = get_node_provider(config["provider"], config["cluster_name"]) try: head_node_tags = { TAG_RAY_NODE_TYPE: "head", } nodes = provider.non_terminated_nodes(head_node_tags) if len(nodes) > 0: head_node = nodes[0] else: head_node = None if not head_node: confirm("This will create a new cluster", yes) elif not no_restart: confirm("This will restart cluster services", yes) launch_hash = hash_launch_conf(config["head_node"], config["auth"]) if head_node is None or provider.node_tags(head_node).get( TAG_RAY_LAUNCH_CONFIG) != launch_hash: if head_node is not None: confirm("Head node config out-of-date. It will be terminated", yes) logger.info( "get_or_create_head_node: " "Terminating outdated head node {}".format(head_node)) provider.terminate_node(head_node) logger.info("get_or_create_head_node: Launching new head node...") head_node_tags[TAG_RAY_LAUNCH_CONFIG] = launch_hash head_node_tags[TAG_RAY_NODE_NAME] = "ray-{}-head".format( config["cluster_name"]) provider.create_node(config["head_node"], head_node_tags, 1) nodes = provider.non_terminated_nodes(head_node_tags) assert len(nodes) == 1, "Failed to create head node." head_node = nodes[0] # TODO(ekl) right now we always update the head node even if the hash # matches. We could prompt the user for what they want to do here. runtime_hash = hash_runtime_conf(config["file_mounts"], config) logger.info("get_or_create_head_node: Updating files on head node...") # Rewrite the auth config so that the head node can update the workers remote_key_path = "~/ray_bootstrap_key.pem" remote_config = copy.deepcopy(config) remote_config["auth"]["ssh_private_key"] = remote_key_path # Adjust for new file locations new_mounts = {} for remote_path in config["file_mounts"]: new_mounts[remote_path] = remote_path remote_config["file_mounts"] = new_mounts remote_config["no_restart"] = no_restart # Now inject the rewritten config and SSH key into the head node remote_config_file = tempfile.NamedTemporaryFile( "w", prefix="ray-bootstrap-") remote_config_file.write(json.dumps(remote_config)) remote_config_file.flush() config["file_mounts"].update({ remote_key_path: config["auth"]["ssh_private_key"], "~/ray_bootstrap_config.yaml": remote_config_file.name }) if restart_only: init_commands = config["head_start_ray_commands"] elif no_restart: init_commands = config["head_setup_commands"] else: init_commands = (config["head_setup_commands"] + config["head_start_ray_commands"]) 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=config["initialization_commands"], setup_commands=init_commands, runtime_hash=runtime_hash, ) updater.start() updater.join() # Refresh the node cache so we see the external ip if available provider.non_terminated_nodes(head_node_tags) 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) if updater.exitcode != 0: logger.error("get_or_create_head_node: " "Updating {} failed".format(head_node_ip)) sys.exit(1) logger.info( "get_or_create_head_node: " "Head node up-to-date, IP address is: {}".format(head_node_ip)) monitor_str = "tail -n 100 -f /tmp/ray/session_*/logs/monitor*" use_docker = bool(config["docker"]["container_name"]) if override_cluster_name: modifiers = " --cluster-name={}".format( quote(override_cluster_name)) else: modifiers = "" print("To monitor auto-scaling activity, you can run:\n\n" " ray exec {} {}{}{}\n".format( config_file, "--docker " if use_docker else " ", quote(monitor_str), modifiers)) print("To open a console on the cluster:\n\n" " ray attach {}{}\n".format(config_file, modifiers)) print("To ssh manually to the cluster, run:\n\n" " ssh -i {} {}@{}\n".format(config["auth"]["ssh_private_key"], config["auth"]["ssh_user"], head_node_ip)) finally: provider.cleanup()
[ "Create", "the", "cluster", "head", "node", "which", "in", "turn", "creates", "the", "workers", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L160-L283
[ "def", "get_or_create_head_node", "(", "config", ",", "config_file", ",", "no_restart", ",", "restart_only", ",", "yes", ",", "override_cluster_name", ")", ":", "provider", "=", "get_node_provider", "(", "config", "[", "\"provider\"", "]", ",", "config", "[", "\...
4eade036a0505e244c976f36aaa2d64386b5129b
train
attach_cluster
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
python/ray/autoscaler/commands.py
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): """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)
[ "Attaches", "to", "a", "screen", "for", "the", "specified", "cluster", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L286-L309
[ "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",...
4eade036a0505e244c976f36aaa2d64386b5129b
train
exec_cluster
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
python/ray/autoscaler/commands.py
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): """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()
[ "Runs", "a", "command", "on", "the", "specified", "cluster", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L312-L391
[ "def", "exec_cluster", "(", "config_file", ",", "cmd", ",", "docker", ",", "screen", ",", "tmux", ",", "stop", ",", "start", ",", "override_cluster_name", ",", "port_forward", ")", ":", "assert", "not", "(", "screen", "and", "tmux", ")", ",", "\"Can specif...
4eade036a0505e244c976f36aaa2d64386b5129b
train
rsync
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
python/ray/autoscaler/commands.py
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): """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()
[ "Rsyncs", "files", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L416-L453
[ "def", "rsync", "(", "config_file", ",", "source", ",", "target", ",", "override_cluster_name", ",", "down", ")", ":", "config", "=", "yaml", ".", "load", "(", "open", "(", "config_file", ")", ".", "read", "(", ")", ")", "if", "override_cluster_name", "i...
4eade036a0505e244c976f36aaa2d64386b5129b
train
get_head_node_ip
Returns head node IP for given configuration file if exists.
python/ray/autoscaler/commands.py
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): """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
[ "Returns", "head", "node", "IP", "for", "given", "configuration", "file", "if", "exists", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L456-L473
[ "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", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
get_worker_node_ips
Returns worker node IPs for given configuration file.
python/ray/autoscaler/commands.py
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): """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()
[ "Returns", "worker", "node", "IPs", "for", "given", "configuration", "file", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/autoscaler/commands.py#L476-L492
[ "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",...
4eade036a0505e244c976f36aaa2d64386b5129b
train
FunctionRunner._train
Implements train() for a Function API. If the RunnerThread finishes without reporting "done", Tune will automatically provide a magic keyword __duplicate__ along with a result with "done=True". The TrialRunner will handle the result accordingly (see tune/trial_runner.py).
python/ray/tune/function_runner.py
def _train(self): """Implements train() for a Function API. If the RunnerThread finishes without reporting "done", Tune will automatically provide a magic keyword __duplicate__ along with a result with "done=True". The TrialRunner will handle the result accordingly (see tune/trial_runner.py). """ if self._runner.is_alive(): # if started and alive, inform the reporter to continue and # generate the next result self._continue_semaphore.release() else: # if not alive, try to start self._status_reporter._start() try: self._runner.start() except RuntimeError: # If this is reached, it means the thread was started and is # now done or has raised an exception. pass result = None while result is None and self._runner.is_alive(): # fetch the next produced result try: result = self._results_queue.get( block=True, timeout=RESULT_FETCH_TIMEOUT) except queue.Empty: pass # if no result were found, then the runner must no longer be alive if result is None: # Try one last time to fetch results in case results were reported # in between the time of the last check and the termination of the # thread runner. try: result = self._results_queue.get(block=False) except queue.Empty: pass # check if error occured inside the thread runner if result is None: # only raise an error from the runner if all results are consumed self._report_thread_runner_error(block=True) # Under normal conditions, this code should never be reached since # this branch should only be visited if the runner thread raised # an exception. If no exception were raised, it means that the # runner thread never reported any results which should not be # possible when wrapping functions with `wrap_function`. raise TuneError( ("Wrapped function ran until completion without reporting " "results or raising an exception.")) else: if not self._error_queue.empty(): logger.warning( ("Runner error waiting to be raised in main thread. " "Logging all available results first.")) # This keyword appears if the train_func using the Function API # finishes without "done=True". This duplicates the last result, but # the TrialRunner will not log this result again. if "__duplicate__" in result: new_result = self._last_result.copy() new_result.update(result) result = new_result self._last_result = result return result
def _train(self): """Implements train() for a Function API. If the RunnerThread finishes without reporting "done", Tune will automatically provide a magic keyword __duplicate__ along with a result with "done=True". The TrialRunner will handle the result accordingly (see tune/trial_runner.py). """ if self._runner.is_alive(): # if started and alive, inform the reporter to continue and # generate the next result self._continue_semaphore.release() else: # if not alive, try to start self._status_reporter._start() try: self._runner.start() except RuntimeError: # If this is reached, it means the thread was started and is # now done or has raised an exception. pass result = None while result is None and self._runner.is_alive(): # fetch the next produced result try: result = self._results_queue.get( block=True, timeout=RESULT_FETCH_TIMEOUT) except queue.Empty: pass # if no result were found, then the runner must no longer be alive if result is None: # Try one last time to fetch results in case results were reported # in between the time of the last check and the termination of the # thread runner. try: result = self._results_queue.get(block=False) except queue.Empty: pass # check if error occured inside the thread runner if result is None: # only raise an error from the runner if all results are consumed self._report_thread_runner_error(block=True) # Under normal conditions, this code should never be reached since # this branch should only be visited if the runner thread raised # an exception. If no exception were raised, it means that the # runner thread never reported any results which should not be # possible when wrapping functions with `wrap_function`. raise TuneError( ("Wrapped function ran until completion without reporting " "results or raising an exception.")) else: if not self._error_queue.empty(): logger.warning( ("Runner error waiting to be raised in main thread. " "Logging all available results first.")) # This keyword appears if the train_func using the Function API # finishes without "done=True". This duplicates the last result, but # the TrialRunner will not log this result again. if "__duplicate__" in result: new_result = self._last_result.copy() new_result.update(result) result = new_result self._last_result = result return result
[ "Implements", "train", "()", "for", "a", "Function", "API", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/function_runner.py#L151-L221
[ "def", "_train", "(", "self", ")", ":", "if", "self", ".", "_runner", ".", "is_alive", "(", ")", ":", "# if started and alive, inform the reporter to continue and", "# generate the next result", "self", ".", "_continue_semaphore", ".", "release", "(", ")", "else", "...
4eade036a0505e244c976f36aaa2d64386b5129b
train
Model.build_network
Returns logits and aux_logits from images.
python/ray/experimental/sgd/tfbench/model.py
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): """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
[ "Returns", "logits", "and", "aux_logits", "from", "images", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/tfbench/model.py#L79-L114
[ "def", "build_network", "(", "self", ",", "images", ",", "phase_train", "=", "True", ",", "nclass", "=", "1001", ",", "image_depth", "=", "3", ",", "data_type", "=", "tf", ".", "float32", ",", "data_format", "=", "\"NCHW\"", ",", "use_tf_layers", "=", "T...
4eade036a0505e244c976f36aaa2d64386b5129b
train
renamed_class
Helper class for renaming Agent => Trainer with a warning.
python/ray/rllib/utils/__init__.py
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): """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
[ "Helper", "class", "for", "renaming", "Agent", "=", ">", "Trainer", "with", "a", "warning", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/__init__.py#L12-L26
[ "def", "renamed_class", "(", "cls", ")", ":", "class", "DeprecationWrapper", "(", "cls", ")", ":", "def", "__init__", "(", "self", ",", "config", "=", "None", ",", "env", "=", "None", ",", "logger_creator", "=", "None", ")", ":", "old_name", "=", "cls"...
4eade036a0505e244c976f36aaa2d64386b5129b
train
profile
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.
python/ray/profiling.py
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): """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)
[ "Profile", "a", "span", "of", "time", "so", "that", "it", "appears", "in", "the", "timeline", "visualization", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/profiling.py#L30-L61
[ "def", "profile", "(", "event_type", ",", "extra_data", "=", "None", ")", ":", "worker", "=", "ray", ".", "worker", ".", "global_worker", "return", "RayLogSpanRaylet", "(", "worker", ".", "profiler", ",", "event_type", ",", "extra_data", "=", "extra_data", "...
4eade036a0505e244c976f36aaa2d64386b5129b
train
Profiler._periodically_flush_profile_events
Drivers run this as a thread to flush profile data in the background.
python/ray/profiling.py
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): """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()
[ "Drivers", "run", "this", "as", "a", "thread", "to", "flush", "profile", "data", "in", "the", "background", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/profiling.py#L94-L110
[ "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 thing...
4eade036a0505e244c976f36aaa2d64386b5129b
train
Profiler.flush_profile_data
Push the logged profiling data to the global control store.
python/ray/profiling.py
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): """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)
[ "Push", "the", "logged", "profiling", "data", "to", "the", "global", "control", "store", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/profiling.py#L112-L125
[ "def", "flush_profile_data", "(", "self", ")", ":", "with", "self", ".", "lock", ":", "events", "=", "self", ".", "events", "self", ".", "events", "=", "[", "]", "if", "self", ".", "worker", ".", "mode", "==", "ray", ".", "WORKER_MODE", ":", "compone...
4eade036a0505e244c976f36aaa2d64386b5129b
train
RayLogSpanRaylet.set_attribute
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.
python/ray/profiling.py
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): """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
[ "Add", "a", "key", "-", "value", "pair", "to", "the", "extra_data", "dict", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/profiling.py#L146-L160
[ "def", "set_attribute", "(", "self", ",", "key", ",", "value", ")", ":", "if", "not", "isinstance", "(", "key", ",", "str", ")", "or", "not", "isinstance", "(", "value", ",", "str", ")", ":", "raise", "ValueError", "(", "\"The arguments 'key' and 'value' m...
4eade036a0505e244c976f36aaa2d64386b5129b
train
_LogSyncer.sync_to_worker_if_possible
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.
python/ray/tune/log_sync.py
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): """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()
[ "Syncs", "the", "local", "logdir", "on", "driver", "to", "worker", "if", "possible", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/log_sync.py#L131-L159
[ "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", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
QMixer.forward
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]
python/ray/rllib/agents/qmix/mixers.py
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): """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
[ "Forward", "pass", "for", "the", "mixer", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/agents/qmix/mixers.py#L39-L64
[ "def", "forward", "(", "self", ",", "agent_qs", ",", "states", ")", ":", "bs", "=", "agent_qs", ".", "size", "(", "0", ")", "states", "=", "states", ".", "reshape", "(", "-", "1", ",", "self", ".", "state_dim", ")", "agent_qs", "=", "agent_qs", "."...
4eade036a0505e244c976f36aaa2d64386b5129b
train
SigOptSearch.on_trial_complete
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.
python/ray/tune/suggest/sigopt.py
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): """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]
[ "Passes", "the", "result", "to", "SigOpt", "unless", "early", "terminated", "or", "errored", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/suggest/sigopt.py#L95-L119
[ "def", "on_trial_complete", "(", "self", ",", "trial_id", ",", "result", "=", "None", ",", "error", "=", "False", ",", "early_terminated", "=", "False", ")", ":", "if", "result", ":", "self", ".", "conn", ".", "experiments", "(", "self", ".", "experiment...
4eade036a0505e244c976f36aaa2d64386b5129b
train
compute_ranks
Returns ranks in [0, len(x)) Note: This is different from scipy.stats.rankdata, which returns ranks in [1, len(x)].
python/ray/rllib/agents/ars/utils.py
def compute_ranks(x): """Returns ranks in [0, len(x)) Note: This is different from scipy.stats.rankdata, which returns ranks in [1, len(x)]. """ assert x.ndim == 1 ranks = np.empty(len(x), dtype=int) ranks[x.argsort()] = np.arange(len(x)) return ranks
def compute_ranks(x): """Returns ranks in [0, len(x)) Note: This is different from scipy.stats.rankdata, which returns ranks in [1, len(x)]. """ assert x.ndim == 1 ranks = np.empty(len(x), dtype=int) ranks[x.argsort()] = np.arange(len(x)) return ranks
[ "Returns", "ranks", "in", "[", "0", "len", "(", "x", "))" ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/agents/ars/utils.py#L12-L21
[ "def", "compute_ranks", "(", "x", ")", ":", "assert", "x", ".", "ndim", "==", "1", "ranks", "=", "np", ".", "empty", "(", "len", "(", "x", ")", ",", "dtype", "=", "int", ")", "ranks", "[", "x", ".", "argsort", "(", ")", "]", "=", "np", ".", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
bottleneck_block_v1
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.
python/ray/experimental/sgd/tfbench/resnet_model.py
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): """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
[ "Bottleneck", "block", "with", "identity", "short", "-", "cut", "for", "ResNet", "v1", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/tfbench/resnet_model.py#L39-L101
[ "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", "+", "st...
4eade036a0505e244c976f36aaa2d64386b5129b
train
bottleneck_block
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.
python/ray/experimental/sgd/tfbench/resnet_model.py
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): """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)
[ "Bottleneck", "block", "with", "identity", "short", "-", "cut", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/tfbench/resnet_model.py#L182-L195
[ "def", "bottleneck_block", "(", "cnn", ",", "depth", ",", "depth_bottleneck", ",", "stride", ",", "pre_activation", ")", ":", "if", "pre_activation", ":", "bottleneck_block_v2", "(", "cnn", ",", "depth", ",", "depth_bottleneck", ",", "stride", ")", "else", ":"...
4eade036a0505e244c976f36aaa2d64386b5129b
train
residual_block
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.
python/ray/experimental/sgd/tfbench/resnet_model.py
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): """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
[ "Residual", "block", "with", "identity", "short", "-", "cut", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/tfbench/resnet_model.py#L198-L258
[ "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", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
MeanStdFilter.apply_changes
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]
python/ray/rllib/utils/filter.py
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): """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()
[ "Applies", "updates", "from", "the", "buffer", "of", "another", "filter", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/filter.py#L156-L181
[ "def", "apply_changes", "(", "self", ",", "other", ",", "with_buffer", "=", "False", ")", ":", "self", ".", "rs", ".", "update", "(", "other", ".", "buffer", ")", "if", "with_buffer", ":", "self", ".", "buffer", "=", "other", ".", "buffer", ".", "cop...
4eade036a0505e244c976f36aaa2d64386b5129b
train
MeanStdFilter.copy
Returns a copy of Filter.
python/ray/rllib/utils/filter.py
def copy(self): """Returns a copy of Filter.""" other = MeanStdFilter(self.shape) other.sync(self) return other
def copy(self): """Returns a copy of Filter.""" other = MeanStdFilter(self.shape) other.sync(self) return other
[ "Returns", "a", "copy", "of", "Filter", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/filter.py#L183-L187
[ "def", "copy", "(", "self", ")", ":", "other", "=", "MeanStdFilter", "(", "self", ".", "shape", ")", "other", ".", "sync", "(", "self", ")", "return", "other" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
MeanStdFilter.sync
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]
python/ray/rllib/utils/filter.py
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): """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()
[ "Syncs", "all", "fields", "together", "from", "other", "filter", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/filter.py#L192-L214
[ "def", "sync", "(", "self", ",", "other", ")", ":", "assert", "other", ".", "shape", "==", "self", ".", "shape", ",", "\"Shapes don't match!\"", "self", ".", "demean", "=", "other", ".", "demean", "self", ".", "destd", "=", "other", ".", "destd", "self...
4eade036a0505e244c976f36aaa2d64386b5129b
train
ConcurrentMeanStdFilter.as_serializable
Returns non-concurrent version of current class
python/ray/rllib/utils/filter.py
def as_serializable(self): """Returns non-concurrent version of current class""" other = MeanStdFilter(self.shape) other.sync(self) return other
def as_serializable(self): """Returns non-concurrent version of current class""" other = MeanStdFilter(self.shape) other.sync(self) return other
[ "Returns", "non", "-", "concurrent", "version", "of", "current", "class" ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/filter.py#L258-L262
[ "def", "as_serializable", "(", "self", ")", ":", "other", "=", "MeanStdFilter", "(", "self", ".", "shape", ")", "other", ".", "sync", "(", "self", ")", "return", "other" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
ConcurrentMeanStdFilter.copy
Returns a copy of Filter.
python/ray/rllib/utils/filter.py
def copy(self): """Returns a copy of Filter.""" other = ConcurrentMeanStdFilter(self.shape) other.sync(self) return other
def copy(self): """Returns a copy of Filter.""" other = ConcurrentMeanStdFilter(self.shape) other.sync(self) return other
[ "Returns", "a", "copy", "of", "Filter", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/rllib/utils/filter.py#L264-L268
[ "def", "copy", "(", "self", ")", ":", "other", "=", "ConcurrentMeanStdFilter", "(", "self", ".", "shape", ")", "other", ".", "sync", "(", "self", ")", "return", "other" ]
4eade036a0505e244c976f36aaa2d64386b5129b
train
michalewicz_function
f(x) = -sum{sin(xi) * [sin(i*xi^2 / pi)]^(2m)}
python/ray/tune/examples/genetic_example.py
def michalewicz_function(config, reporter): """f(x) = -sum{sin(xi) * [sin(i*xi^2 / pi)]^(2m)}""" import numpy as np x = np.array( [config["x1"], config["x2"], config["x3"], config["x4"], config["x5"]]) sin_x = np.sin(x) z = (np.arange(1, 6) / np.pi * (x * x)) sin_z = np.power(np.sin(z), 20) # let m = 20 y = np.dot(sin_x, sin_z) # Negate y since we want to minimize y value reporter(timesteps_total=1, neg_mean_loss=-y)
def michalewicz_function(config, reporter): """f(x) = -sum{sin(xi) * [sin(i*xi^2 / pi)]^(2m)}""" import numpy as np x = np.array( [config["x1"], config["x2"], config["x3"], config["x4"], config["x5"]]) sin_x = np.sin(x) z = (np.arange(1, 6) / np.pi * (x * x)) sin_z = np.power(np.sin(z), 20) # let m = 20 y = np.dot(sin_x, sin_z) # Negate y since we want to minimize y value reporter(timesteps_total=1, neg_mean_loss=-y)
[ "f", "(", "x", ")", "=", "-", "sum", "{", "sin", "(", "xi", ")", "*", "[", "sin", "(", "i", "*", "xi^2", "/", "pi", ")", "]", "^", "(", "2m", ")", "}" ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/tune/examples/genetic_example.py#L16-L27
[ "def", "michalewicz_function", "(", "config", ",", "reporter", ")", ":", "import", "numpy", "as", "np", "x", "=", "np", ".", "array", "(", "[", "config", "[", "\"x1\"", "]", ",", "config", "[", "\"x2\"", "]", ",", "config", "[", "\"x3\"", "]", ",", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
parse_general_int
Parse integer with power-of-2 suffix eg. 32k.
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Parse", "integer", "with", "power", "-", "of", "-", "2", "suffix", "eg", ".", "32k", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L38-L58
[ "def", "parse_general_int", "(", "s", ")", ":", "mo", "=", "re", ".", "match", "(", "r\"(\\d+)([KkMGT]?)$\"", ",", "s", ")", "if", "mo", ":", "i", ",", "suffix", "=", "mo", ".", "group", "(", "1", ",", "2", ")", "v", "=", "int", "(", "i", ")", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
parse_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.
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Parse", "all_reduce_spec", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L61-L148
[ "def", "parse_all_reduce_spec", "(", "all_reduce_spec", ")", ":", "range_parts", "=", "all_reduce_spec", ".", "split", "(", "\":\"", ")", "+", "[", "\"-1\"", "]", "if", "len", "(", "range_parts", ")", "%", "2", ":", "raise", "ValueError", "(", "\"all_reduce_...
4eade036a0505e244c976f36aaa2d64386b5129b
train
build_all_reduce_device_prefixes
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"
python/ray/experimental/sgd/modified_allreduce.py
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): """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]
[ "Build", "list", "of", "device", "prefix", "names", "for", "all_reduce", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L151-L167
[ "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_ta...
4eade036a0505e244c976f36aaa2d64386b5129b
train
group_device_names
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)
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Group", "device", "names", "into", "groups", "of", "group_size", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L170-L196
[ "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", ",...
4eade036a0505e244c976f36aaa2d64386b5129b
train
split_grads_by_size
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.
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Break", "gradients", "into", "two", "sets", "according", "to", "tensor", "size", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L199-L228
[ "def", "split_grads_by_size", "(", "threshold_size", ",", "device_grads", ")", ":", "small_grads", "=", "[", "]", "large_grads", "=", "[", "]", "for", "dl", "in", "device_grads", ":", "small_dl", "=", "[", "]", "large_dl", "=", "[", "]", "for", "(", "g",...
4eade036a0505e244c976f36aaa2d64386b5129b
train
aggregate_single_gradient
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.
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Calculate", "the", "average", "gradient", "for", "a", "shared", "variable", "across", "all", "towers", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L241-L270
[ "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", "an...
4eade036a0505e244c976f36aaa2d64386b5129b
train
aggregate_gradients_using_copy_with_device_selection
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.
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Aggregate", "gradients", "controlling", "device", "for", "the", "aggregation", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L273-L296
[ "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", ",", ...
4eade036a0505e244c976f36aaa2d64386b5129b
train
sum_grad_and_var_all_reduce
Apply all-reduce algorithm over specified gradient tensors.
python/ray/experimental/sgd/modified_allreduce.py
def sum_grad_and_var_all_reduce(grad_and_vars, num_workers, alg, gpu_indices, aux_devices=None, num_shards=1): """Apply all-reduce algorithm over specified gradient tensors.""" with tf.name_scope("allreduce"): # Note that each grad_and_vars looks like the following: # ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN)) scaled_grads = [g for g, _ in grad_and_vars] if alg == "nccl": from tensorflow.python.ops import nccl_ops summed_grads = nccl_ops.all_sum(scaled_grads) elif alg == "simple": summed_grads = build_reduce_sum(scaled_grads) elif alg == "trivial": summed_grads = build_trivial_sum(scaled_grads) elif alg == "xring": summed_grads = all_reduce.build_ring_all_reduce( scaled_grads, num_workers, num_shards, gpu_indices, tf.add) elif alg == "nccl/xring": summed_grads = all_reduce.build_nccl_then_ring( scaled_grads, num_shards, tf.add) elif alg == "nccl/rechd": summed_grads = all_reduce.build_nccl_then_recursive_hd( scaled_grads, tf.add) elif alg == "nccl/pscpu": summed_grads = all_reduce.build_nccl_then_shuffle( scaled_grads, aux_devices, tf.add, tf.add_n) elif alg == "pscpu/pscpu": summed_grads = all_reduce.build_shuffle_then_shuffle( scaled_grads, aux_devices, # TODO(tucker): devise a way of better specifying the device # for the second level. [aux_devices[0]], tf.add_n) elif alg in ["pscpu", "psgpu"]: summed_grads = all_reduce.build_shuffle_all_reduce( scaled_grads, aux_devices, tf.add_n) else: raise ValueError("unsupported all_reduce alg: ", alg) result = [] for (_, v), g in zip(grad_and_vars, summed_grads): result.append([g, v]) return result
def sum_grad_and_var_all_reduce(grad_and_vars, num_workers, alg, gpu_indices, aux_devices=None, num_shards=1): """Apply all-reduce algorithm over specified gradient tensors.""" with tf.name_scope("allreduce"): # Note that each grad_and_vars looks like the following: # ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN)) scaled_grads = [g for g, _ in grad_and_vars] if alg == "nccl": from tensorflow.python.ops import nccl_ops summed_grads = nccl_ops.all_sum(scaled_grads) elif alg == "simple": summed_grads = build_reduce_sum(scaled_grads) elif alg == "trivial": summed_grads = build_trivial_sum(scaled_grads) elif alg == "xring": summed_grads = all_reduce.build_ring_all_reduce( scaled_grads, num_workers, num_shards, gpu_indices, tf.add) elif alg == "nccl/xring": summed_grads = all_reduce.build_nccl_then_ring( scaled_grads, num_shards, tf.add) elif alg == "nccl/rechd": summed_grads = all_reduce.build_nccl_then_recursive_hd( scaled_grads, tf.add) elif alg == "nccl/pscpu": summed_grads = all_reduce.build_nccl_then_shuffle( scaled_grads, aux_devices, tf.add, tf.add_n) elif alg == "pscpu/pscpu": summed_grads = all_reduce.build_shuffle_then_shuffle( scaled_grads, aux_devices, # TODO(tucker): devise a way of better specifying the device # for the second level. [aux_devices[0]], tf.add_n) elif alg in ["pscpu", "psgpu"]: summed_grads = all_reduce.build_shuffle_all_reduce( scaled_grads, aux_devices, tf.add_n) else: raise ValueError("unsupported all_reduce alg: ", alg) result = [] for (_, v), g in zip(grad_and_vars, summed_grads): result.append([g, v]) return result
[ "Apply", "all", "-", "reduce", "algorithm", "over", "specified", "gradient", "tensors", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L299-L346
[ "def", "sum_grad_and_var_all_reduce", "(", "grad_and_vars", ",", "num_workers", ",", "alg", ",", "gpu_indices", ",", "aux_devices", "=", "None", ",", "num_shards", "=", "1", ")", ":", "with", "tf", ".", "name_scope", "(", "\"allreduce\"", ")", ":", "# Note tha...
4eade036a0505e244c976f36aaa2d64386b5129b
train
sum_gradients_all_reduce
Apply all-reduce algorithm over specified gradient tensors. Args: dev_prefixes: list of prefix strings to use to generate PS device names. tower_grads: the gradients to reduce. num_workers: number of worker processes across entire job. alg: the all-reduce algorithm to apply. num_shards: alg-specific sharding factor. gpu_indices: indices of local GPUs in order usable for ring-reduce. agg_small_grads_max_bytes: largest tensor eligible for aggregation, in number of bytes. Returns: list of reduced tensors, packing values
python/ray/experimental/sgd/modified_allreduce.py
def sum_gradients_all_reduce(dev_prefixes, tower_grads, num_workers, alg, num_shards, gpu_indices, agg_small_grads_max_bytes=0): """Apply all-reduce algorithm over specified gradient tensors. Args: dev_prefixes: list of prefix strings to use to generate PS device names. tower_grads: the gradients to reduce. num_workers: number of worker processes across entire job. alg: the all-reduce algorithm to apply. num_shards: alg-specific sharding factor. gpu_indices: indices of local GPUs in order usable for ring-reduce. agg_small_grads_max_bytes: largest tensor eligible for aggregation, in number of bytes. Returns: list of reduced tensors, packing values """ alg_contains_shuffle = contains_any(alg, ["pscpu", "psgpu"]) is_hierarchical = "/" in alg if "pscpu" in alg: aux_devices = [prefix + "/cpu:0" for prefix in dev_prefixes] elif "psgpu" in alg: aux_devices = [ prefix + "/gpu:%d" % i for i in range(len(gpu_indices)) for prefix in dev_prefixes ] else: aux_devices = ["/job:localhost/cpu:0"] aux_device_groups = group_device_names( aux_devices, num_shards if alg_contains_shuffle else 1) group_index = 0 if agg_small_grads_max_bytes > 0: tower_grads, packing = pack_small_tensors( tower_grads, max_bytes=agg_small_grads_max_bytes) else: packing = None new_tower_grads = [] if alg == "better": raw_devices = ["/gpu:%i" % (i) for i in gpu_indices] agg_grads = aggregate_gradients_using_copy_with_device_selection( tower_grads, raw_devices) for arr in tower_grads: new_tower_grads.append( [(g, v) for (_, v), (g, _) in zip(arr, agg_grads)]) else: reduced_gv_list = [] for grad_and_vars in zip(*tower_grads): reduced_gv_list.append( sum_grad_and_var_all_reduce( grad_and_vars, num_workers, alg, gpu_indices, aux_devices if is_hierarchical else aux_device_groups[group_index], num_shards)) group_index = (group_index + 1) % len(aux_device_groups) new_tower_grads = [list(x) for x in zip(*reduced_gv_list)] return new_tower_grads, packing
def sum_gradients_all_reduce(dev_prefixes, tower_grads, num_workers, alg, num_shards, gpu_indices, agg_small_grads_max_bytes=0): """Apply all-reduce algorithm over specified gradient tensors. Args: dev_prefixes: list of prefix strings to use to generate PS device names. tower_grads: the gradients to reduce. num_workers: number of worker processes across entire job. alg: the all-reduce algorithm to apply. num_shards: alg-specific sharding factor. gpu_indices: indices of local GPUs in order usable for ring-reduce. agg_small_grads_max_bytes: largest tensor eligible for aggregation, in number of bytes. Returns: list of reduced tensors, packing values """ alg_contains_shuffle = contains_any(alg, ["pscpu", "psgpu"]) is_hierarchical = "/" in alg if "pscpu" in alg: aux_devices = [prefix + "/cpu:0" for prefix in dev_prefixes] elif "psgpu" in alg: aux_devices = [ prefix + "/gpu:%d" % i for i in range(len(gpu_indices)) for prefix in dev_prefixes ] else: aux_devices = ["/job:localhost/cpu:0"] aux_device_groups = group_device_names( aux_devices, num_shards if alg_contains_shuffle else 1) group_index = 0 if agg_small_grads_max_bytes > 0: tower_grads, packing = pack_small_tensors( tower_grads, max_bytes=agg_small_grads_max_bytes) else: packing = None new_tower_grads = [] if alg == "better": raw_devices = ["/gpu:%i" % (i) for i in gpu_indices] agg_grads = aggregate_gradients_using_copy_with_device_selection( tower_grads, raw_devices) for arr in tower_grads: new_tower_grads.append( [(g, v) for (_, v), (g, _) in zip(arr, agg_grads)]) else: reduced_gv_list = [] for grad_and_vars in zip(*tower_grads): reduced_gv_list.append( sum_grad_and_var_all_reduce( grad_and_vars, num_workers, alg, gpu_indices, aux_devices if is_hierarchical else aux_device_groups[group_index], num_shards)) group_index = (group_index + 1) % len(aux_device_groups) new_tower_grads = [list(x) for x in zip(*reduced_gv_list)] return new_tower_grads, packing
[ "Apply", "all", "-", "reduce", "algorithm", "over", "specified", "gradient", "tensors", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L366-L426
[ "def", "sum_gradients_all_reduce", "(", "dev_prefixes", ",", "tower_grads", ",", "num_workers", ",", "alg", ",", "num_shards", ",", "gpu_indices", ",", "agg_small_grads_max_bytes", "=", "0", ")", ":", "alg_contains_shuffle", "=", "contains_any", "(", "alg", ",", "...
4eade036a0505e244c976f36aaa2d64386b5129b
train
extract_ranges
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.
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Extract", "consecutive", "ranges", "and", "singles", "from", "index_list", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L448-L482
[ "def", "extract_ranges", "(", "index_list", ",", "range_size_limit", "=", "32", ")", ":", "if", "not", "index_list", ":", "return", "[", "]", ",", "[", "]", "first", "=", "index_list", "[", "0", "]", "last", "=", "first", "ranges", "=", "[", "]", "si...
4eade036a0505e244c976f36aaa2d64386b5129b
train
pack_range
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.
python/ray/experimental/sgd/modified_allreduce.py
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): """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)
[ "Form", "the", "concatenation", "of", "a", "specified", "range", "of", "gradient", "tensors", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L488-L517
[ "def", "pack_range", "(", "key", ",", "packing", ",", "grad_vars", ",", "rng", ")", ":", "to_pack", "=", "grad_vars", "[", "rng", "[", "0", "]", ":", "rng", "[", "1", "]", "+", "1", "]", "members", "=", "[", "]", "variables", "=", "[", "]", "re...
4eade036a0505e244c976f36aaa2d64386b5129b
train
unpack_grad_tuple
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.
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Unpack", "a", "previously", "packed", "collection", "of", "gradient", "tensors", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L520-L540
[ "def", "unpack_grad_tuple", "(", "gv", ",", "gpt", ")", ":", "elt_widths", "=", "[", "x", ".", "num_elements", "(", ")", "for", "x", "in", "gpt", ".", "shapes", "]", "with", "tf", ".", "device", "(", "gv", "[", "0", "]", "[", "0", "]", ".", "de...
4eade036a0505e244c976f36aaa2d64386b5129b
train
pack_small_tensors
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.
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Concatenate", "gradients", "together", "more", "intelligently", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L543-L606
[ "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 e...
4eade036a0505e244c976f36aaa2d64386b5129b
train
unpack_small_tensors
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.
python/ray/experimental/sgd/modified_allreduce.py
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): """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
[ "Undo", "the", "structure", "alterations", "to", "tower_grads", "done", "by", "pack_small_tensors", "." ]
ray-project/ray
python
https://github.com/ray-project/ray/blob/4eade036a0505e244c976f36aaa2d64386b5129b/python/ray/experimental/sgd/modified_allreduce.py#L609-L637
[ "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", ...
4eade036a0505e244c976f36aaa2d64386b5129b