hive.utils.registry module

class hive.utils.registry.Registrable[source]

Bases: object

Class used to denote which types of objects can be registered in the RLHive Registry. These objects can also be configured directly from the command line, and recursively built from the config, assuming type annotations are present.

classmethod type_name()[source]

This should represent a string that denotes the which type of class you are creating. For example, “logger”, “agent”, or “env”.

class hive.utils.registry.CallableType(fn)[source]

Bases: Registrable

A wrapper that allows any callable to be registered in the RLHive Registry. Specifically, it maps the arguments and annotations of the wrapped function to the resulting callable, allowing any argument names and type annotations of the underlying function to be present for outer wrapper. When called with some arguments, this object returns a partial function with those arguments assigned.

By default, the type_name is “callable”, but if you want to create specific types of callables, you can simply create a subclass and override the type_name method. See hive.utils.utils.OptimizerFn.

Parameters

fn – callable to be wrapped.

classmethod type_name()[source]

This should represent a string that denotes the which type of class you are creating. For example, “logger”, “agent”, or “env”.

class hive.utils.registry.Registry[source]

Bases: object

This is the Registry class for RLHive. It allows you to register different types of Registrable classes and objects and generates constructors for those classes in the form of get_{type_name}.

These constructors allow you to construct objects from dictionary configs. These configs should have two fields: name, which corresponds to the name used when registering a class in the registry, and kwargs, which corresponds to the keyword arguments that will be passed to the constructor of the object. These constructors can also build objects recursively, i.e. if a config contains the config for another Registrable object, this will be automatically created before being passed to the constructor of the original object. These constructors also allow you to directly specify/override arguments for object constructors directly from the command line. These parameters are specified in dot notation. They also are able to handle lists and dictionaries of Registrable objects.

For example, let’s consider the following scenario: Your agent class has an argument arg1 which is annotated to be List[Class1], Class1 is Registrable, and the Class1 constructor takes an argument arg2. In the passed yml config, there are two different Class1 object configs listed. the constructor will check to see if both –agent.arg1.0.arg2 and –agent.arg1.1.arg2 have been passed.

The parameters passed in the command line will be parsed according to the type annotation of the corresponding low level constructor. If it is not one of int, float, str, or bool, it simply loads the string into python using a yaml loader.

Each constructor returns the object, as well a dictionary config with all the parameters used to create the object and any Registrable objects created in the process of creating this object.

register(name, constructor, type)[source]

Register a Registrable class/object with RLHive.

Parameters
  • name (str) – Name of the class/object being registered.

  • constructor (callable) – Callable that will be passed all kwargs from configs and be analyzed to get type annotations.

  • type (type) – Type of class/object being registered. Should be subclass of Registrable.

register_all(base_class, class_dict)[source]

Bulk register function.

Parameters
  • base_class (type) – Corresponds to the type of the register function

  • class_dict (dict[str, callable]) – A dictionary mapping from name to constructor.

get_agent(object_or_config, prefix=None)
get_env(object_or_config, prefix=None)
get_function(object_or_config, prefix=None)
get_init_fn(object_or_config, prefix=None)
get_logger(object_or_config, prefix=None)
get_loss_fn(object_or_config, prefix=None)
get_optimizer_fn(object_or_config, prefix=None)
get_replay(object_or_config, prefix=None)
get_schedule(object_or_config, prefix=None)
hive.utils.registry.construct_objects(object_constructor, config, prefix=None)[source]

Helper function that constructs any objects specified in the config that are registrable.

Returns the object, as well a dictionary config with all the parameters used to create the object and any Registrable objects created in the process of creating this object.

Parameters
  • object_constructor (callable) – constructor of object that corresponds to config. The signature of this function will be analyzed to see if there are any Registrable objects that might be specified in the config.

  • config (dict) – The kwargs for the object being created. May contain configs for other Registrable objects that need to be recursively created.

  • prefix (str) – Prefix that is attached to the argument names when looking for command line arguments.

hive.utils.registry.get_callable_parsed_args(callable, prefix=None)[source]

Helper function that extracts the command line arguments for a given function.

Parameters
  • callable (callable) – function whose arguments will be inspected to extract arguments from the command line.

  • prefix (str) – Prefix that is attached to the argument names when looking for command line arguments.

hive.utils.registry.get_parsed_args(arguments, prefix=None)[source]

Helper function that takes a dictionary mapping argument names to types, and extracts command line arguments for those arguments. If the dictionary contains a key-value pair “bar”: int, and the prefix passed is “foo”, this function will look for a command line argument “--foo.bar”. If present, it will cast it to an int.

If the type for a given argument is not one of int, float, str, or bool, it simply loads the string into python using a yaml loader.

Parameters
  • arguments (dict[str, type]) – dictionary mapping argument names to types

  • prefix (str) – prefix that is attached to each argument name before searching for command line arguments.