Skip to content

Commit

Permalink
add resolve_children arg
Browse files Browse the repository at this point in the history
  • Loading branch information
carolineechen committed Apr 9, 2024
1 parent 5e0124b commit 33860f2
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 33 deletions.
4 changes: 0 additions & 4 deletions runhouse/resources/envs/conda_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ def __init__(
dryrun=dryrun,
)

@staticmethod
def from_config(config: dict, dryrun: bool = True):
return CondaEnv(**config, dryrun=dryrun)

def config(self, condensed=True):
config = super().config(condensed)
config.update({"conda_yaml": self.conda_yaml})
Expand Down
10 changes: 7 additions & 3 deletions runhouse/resources/envs/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ def env_name(self):
return self.name or "base"

@staticmethod
def from_config(config: dict, dryrun: bool = False):
def from_config(config: dict, dryrun: bool = False, _resolve_children: bool = True):
"""Create an Env object from a config dict"""
config["reqs"] = [
Package.from_config(req, dryrun=True) if isinstance(req, dict) else req
Package.from_config(req, dryrun=True, _resolve_children=_resolve_children)
if isinstance(req, dict)
else req
for req in config.get("reqs", [])
]
config["working_dir"] = (
Package.from_config(config["working_dir"], dryrun=True)
Package.from_config(
config["working_dir"], dryrun=True, _resolve_children=_resolve_children
)
if isinstance(config["working_dir"], dict)
else config["working_dir"]
)
Expand Down
4 changes: 3 additions & 1 deletion runhouse/resources/folders/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def from_config(config: dict, dryrun=False, _resolve_children=True):

return AzureFolder.from_config(config, dryrun=dryrun)
elif isinstance(config["system"], dict):
config["system"] = Cluster.from_config(config["system"], dryrun=dryrun)
config["system"] = Cluster.from_config(
config["system"], dryrun=dryrun, _resolve_children=_resolve_children
)
return Folder(**config, dryrun=dryrun)

@classmethod
Expand Down
8 changes: 6 additions & 2 deletions runhouse/resources/functions/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def __init__(
# --------------------------------------

@classmethod
def from_config(cls, config: dict, dryrun: bool = False):
def from_config(
cls, config: dict, dryrun: bool = False, _resolve_children: bool = True
):
"""Create an AWS Lambda object from a config dictionary."""

if "resource_subtype" in config.keys():
Expand All @@ -160,7 +162,9 @@ def from_config(cls, config: dict, dryrun: bool = False):
name=Env.DEFAULT_NAME,
)
else:
config["env"] = Env.from_config(config["env"])
config["env"] = Env.from_config(
config["env"], _resolve_children=_resolve_children
)

return LambdaFunction(**config, dryrun=dryrun).deploy()

Expand Down
10 changes: 7 additions & 3 deletions runhouse/resources/functions/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ def __init__(
# ----------------- Constructor helper methods -----------------

@classmethod
def from_config(cls, config: dict, dryrun: bool = False):
def from_config(cls, config: dict, dryrun: bool = False, _resolve_children=True):
"""Create a Function object from a config dictionary."""
if isinstance(config["system"], dict):
config["system"] = Cluster.from_config(config["system"], dryrun=dryrun)
config["system"] = Cluster.from_config(
config["system"], dryrun=dryrun, _resolve_children=_resolve_children
)
if isinstance(config["env"], dict):
config["env"] = Env.from_config(config["env"], dryrun=dryrun)
config["env"] = Env.from_config(
config["env"], dryrun=dryrun, _resolve_children=_resolve_children
)

config.pop("resource_subtype", None)
return Function(**config, dryrun=dryrun)
Expand Down
5 changes: 3 additions & 2 deletions runhouse/resources/hardware/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ def _save_sub_resources(self):
self._creds.save()

@classmethod
def from_config(cls, config: dict, dryrun=False):
def from_config(cls, config: dict, dryrun=False, _resolve_children=True):
resource_subtype = config.get("resource_subtype")
config = cls._check_for_child_configs(config)
if _resolve_children:
config = cls._check_for_child_configs(config)

if resource_subtype == "Cluster":
return Cluster(**config, dryrun=dryrun)
Expand Down
4 changes: 0 additions & 4 deletions runhouse/resources/hardware/on_demand_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ def autostop_mins(self, mins):
sky.autostop(self.name, mins, down=True)
self._autostop_mins = mins

@staticmethod
def from_config(config: dict, dryrun=False):
return OnDemandCluster(**config, dryrun=dryrun)

def config(self, condensed=True):
config = super().config(condensed)
self.save_attrs_to_config(
Expand Down
5 changes: 3 additions & 2 deletions runhouse/resources/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def config(self, condensed=True):
return config

@classmethod
def from_config(cls, config: dict, dryrun=False):
def from_config(cls, config: dict, dryrun=False, _resolve_children=True):
if config.get("pointers"):
config.pop("resource_subtype", None)
logger.debug(f"Constructing module from pointers {config['pointers']}")
Expand Down Expand Up @@ -177,7 +177,8 @@ def from_config(cls, config: dict, dryrun=False):
# If this resource was put on a cluster with put_resource, the servlet will be populating the rest
# of the class-specific attributes.
new_module = module_cls.__new__(module_cls)
config = module_cls._check_for_child_configs(config)
if _resolve_children:
config = module_cls._check_for_child_configs(config)
new_module.system = config.pop("system", None)
new_module.env = config.pop("env", None)
new_module.name = config.pop("name", None)
Expand Down
10 changes: 7 additions & 3 deletions runhouse/resources/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,19 @@ def to(
return new_package

@staticmethod
def from_config(config: dict, dryrun=False):
def from_config(config: dict, dryrun=False, _resolve_children=True):
if isinstance(config.get("install_target"), dict):
config["install_target"] = Folder.from_config(
config["install_target"], dryrun=dryrun
config["install_target"],
dryrun=dryrun,
_resolve_children=_resolve_children,
)
if config.get("resource_subtype") == "GitPackage":
from runhouse import GitPackage

return GitPackage.from_config(config, dryrun=dryrun)
return GitPackage.from_config(
config, dryrun=dryrun, _resolve_children=_resolve_children
)
return Package(**config, dryrun=dryrun)

@staticmethod
Expand Down
17 changes: 11 additions & 6 deletions runhouse/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def str_dict_or_resource_to_str(val):
return config

@classmethod
def from_name(cls, name, dryrun=False, alt_options=None):
def from_name(cls, name, dryrun=False, alt_options=None, condensed=False):
"""Load existing Resource via its name."""
# TODO is this the right priority order?
from runhouse.resources.hardware.utils import _current_cluster
Expand All @@ -276,16 +276,18 @@ def from_name(cls, name, dryrun=False, alt_options=None):
if not config:
raise ValueError(f"Resource {name} not found.")
config["name"] = name
config = cls._check_for_child_configs(config)

if not condensed:
config = cls._check_for_child_configs(config)

# Add this resource's name to the resource artifact registry if part of a run
rns_client.add_upstream_resource(name)

# Uses child class's from_config
return cls.from_config(config=config, dryrun=dryrun)
return cls.from_config(config=config, dryrun=dryrun, condensed=condensed)

@staticmethod
def from_config(config, dryrun=False):
def from_config(config, dryrun=False, _resolve_children=True):
resource_type = config.pop("resource_type", None)
dryrun = config.pop("dryrun", False) or dryrun

Expand All @@ -297,9 +299,12 @@ def from_config(config, dryrun=False):
)
if not resource_class:
raise TypeError(f"Could not find module associated with {resource_type}")
config = resource_class._check_for_child_configs(config)
if _resolve_children:
config = resource_class._check_for_child_configs(config)

loaded = resource_class.from_config(config=config, dryrun=dryrun)
loaded = resource_class.from_config(
config=config, dryrun=dryrun, _resolve_children=_resolve_children
)
if loaded.name:
rns_client.add_upstream_resource(loaded.name)
return loaded
Expand Down
14 changes: 11 additions & 3 deletions runhouse/resources/tables/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def __init__(
self.metadata = metadata or {}

@staticmethod
def from_config(config: dict, dryrun=False):
if isinstance(config["system"], dict):
config["system"] = _get_cluster_from(config["system"], dryrun=dryrun)
def from_config(config: dict, dryrun=False, _resolve_children=True):
if _resolve_children:
config = Table._check_for_child_configs(config)
return _load_table_subclass(config, dryrun=dryrun)

def config(self, condensed=True):
Expand All @@ -85,6 +85,14 @@ def config(self, condensed=True):

return config

@classmethod
def _check_for_child_configs(cls, config: dict):
"""Overload by child resources to load any resources they hold internally."""
system = config.get("system")
if isinstance(system, str) or isinstance(system, dict):
config["system"] = _get_cluster_from(system)
return config

@property
def data(self) -> "ray.data.Dataset":
"""Get the table data. If data is not already cached, return a Ray dataset.
Expand Down

0 comments on commit 33860f2

Please sign in to comment.