Install completions for entrypoints, misc config tweaks (#569)

* Install completions for entrypoints, misc config tweaks

* Bump dcargs
This commit is contained in:
Brent Yi authored and GitHub committed 2022-09-27 22:23:22 -07:00
1 parent 7c1734fe5c
commit 7f38490ec0
8 files changed
+65 -53

No files matched your search

+2 -2
View File
@@ -94,10 +94,10 @@ pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 -f https://download.py
pip install git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch pip install git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch
``` ```
Experimental: install tab completion for nerfactory (bash & zsh) Install tab completion for nerfactory (bash & zsh)
``` ```
python scripts/completions/configure.py install nf-install-cli
``` ```
Run the test cases Run the test cases
+2 -2
View File
@@ -32,11 +32,11 @@ pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 -f https://download.py
pip install git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch pip install git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch
``` ```
Experimental: install tab completion for nerfactory (bash and zsh) Install tab completion for nerfactory (bash and zsh)
```bash ```bash
# If the CLI changes, we can also re-install to synchronize completions # If the CLI changes, we can also re-install to synchronize completions
python scripts/completions/configure.py install nf-install-cli
``` ```
# Downloading data # Downloading data
+5 -1
View File
@@ -240,7 +240,7 @@ class Config(PrintableConfig):
"""Method name. Required to set in python or via cli""" """Method name. Required to set in python or via cli"""
experiment_name: Optional[str] = None experiment_name: Optional[str] = None
"""Experiment name. If None, will automatically be set to dataset name""" """Experiment name. If None, will automatically be set to dataset name"""
timestamp: str = datetime.now().strftime("%Y-%m-%d_%H%M%S") timestamp: str = "{timestamp}"
"""Experiment timestamp.""" """Experiment timestamp."""
machine: MachineConfig = MachineConfig() machine: MachineConfig = MachineConfig()
logging: LoggingConfig = LoggingConfig() logging: LoggingConfig = LoggingConfig()
@@ -256,6 +256,10 @@ class Config(PrintableConfig):
} }
) )
def set_timestamp(self) -> None:
"""Dynamically set the experiment timestamp"""
self.timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")
def set_experiment_name(self) -> None: def set_experiment_name(self) -> None:
"""Dynamically set the experiment name""" """Dynamically set the experiment name"""
if self.experiment_name is None: if self.experiment_name is None:
+2 -25
View File
@@ -18,11 +18,9 @@ Put all the method implementations in one location.
from __future__ import annotations from __future__ import annotations
import copy from typing import Dict
from typing import Dict, Type
import dcargs import dcargs
from typeguard import typeguard_ignore
from nerfactory.configs.base import ( from nerfactory.configs.base import (
AdamOptimizerConfig, AdamOptimizerConfig,
@@ -232,28 +230,7 @@ base_configs["tensorf"] = Config(
) )
@typeguard_ignore # TypeGuard doesn't understand the generic alias that's returned here. AnnotatedBaseConfigUnion = dcargs.extras.subcommand_type_from_defaults(base_configs)
def _make_base_config_subcommand_type() -> Type[Config]:
"""Generate a Union[] type over the possible base config types, with runtime
annotations containing default values. Used to generate CLIs.
Returns:
An annotated Union type, which can be used to pick a base configuration.
"""
# When a base config is used to generate a CLI: replace the auto-generated timestamp
# with {timestamp}. This makes the CLI helptext (and, for zsh, autocomplete
# generation) consistent everytime you run a script with --help.
#
# Note that when a config is instantiated with dcargs.cli(), the __post_init__
# called when the config is instantiated will set the correct timestamp.
base_configs_placeholder_timestamp = {}
for name, config in base_configs.items():
base_configs_placeholder_timestamp[name] = copy.deepcopy(config)
return dcargs.extras.subcommand_type_from_defaults(base_configs_placeholder_timestamp)
AnnotatedBaseConfigUnion = _make_base_config_subcommand_type()
"""Union[] type over config types, annotated with default instances for use with """Union[] type over config types, annotated with default instances for use with
dcargs.cli(). Allows the user to pick between one of several base configurations, and dcargs.cli(). Allows the user to pick between one of several base configurations, and
then override values in it.""" then override values in it."""
+2 -2
View File
@@ -18,7 +18,7 @@ dependencies = [
"aiortc==1.3.2", "aiortc==1.3.2",
"appdirs>=1.4", "appdirs>=1.4",
"av==9.2.0", "av==9.2.0",
"dcargs>=0.3.9", "dcargs>=0.3.11",
"gdown==4.5.1", "gdown==4.5.1",
"ninja==1.10.2.3", "ninja==1.10.2.3",
"functorch==0.2.1", "functorch==0.2.1",
@@ -74,7 +74,7 @@ docs = [
] ]
[project.scripts] [project.scripts]
nf-install-cli = "scripts.completions.configure:entrypoint" nf-install-cli = "scripts.completions.install:entrypoint"
nf-process-data = "scripts.process_data:entrypoint" nf-process-data = "scripts.process_data:entrypoint"
nf-train = "scripts.train:entrypoint" nf-train = "scripts.train:entrypoint"
nf-eval = "scripts.eval:entrypoint" nf-eval = "scripts.eval:entrypoint"
@@ -10,7 +10,7 @@ import shutil
import stat import stat
import subprocess import subprocess
import sys import sys
from typing import List, Literal from typing import List, Literal, Union
import dcargs import dcargs
from rich.console import Console from rich.console import Console
@@ -58,7 +58,9 @@ def _check_dcargs_cli(script_path: pathlib.Path) -> bool:
return False return False
def _generate_completion(script_path: pathlib.Path, shell: ShellType, completions_dir: pathlib.Path) -> pathlib.Path: def _generate_completion(
path_or_entrypoint: Union[pathlib.Path, str], shell: ShellType, completions_dir: pathlib.Path
) -> pathlib.Path:
"""Given a path to a dcargs CLI, write a completion script to a target directory. """Given a path to a dcargs CLI, write a completion script to a target directory.
Args: Args:
@@ -69,12 +71,22 @@ def _generate_completion(script_path: pathlib.Path, shell: ShellType, completion
Returns: Returns:
Success flag. Success flag.
""" """
# Use zsh standard for naming completion scripts. if isinstance(path_or_entrypoint, pathlib.Path):
target_path = completions_dir / shell / ("_" + script_path.name.replace(".", "_")) # Scripts.
target_name = "_" + path_or_entrypoint.name.replace(".", "_")
args = [sys.executable, str(path_or_entrypoint), "--dcargs-print-completion", shell]
elif isinstance(path_or_entrypoint, str):
# Entry points.
target_name = "_" + path_or_entrypoint
args = [path_or_entrypoint, "--dcargs-print-completion", shell]
else:
assert_never(path_or_entrypoint)
target_path = completions_dir / shell / target_name
# Generate and write the new completion. # Generate and write the new completion.
new = subprocess.run( new = subprocess.run(
args=[sys.executable, str(script_path), "--dcargs-print-completion", shell], args=args,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
encoding="utf8", encoding="utf8",
@@ -151,7 +163,7 @@ def _update_rc(
def main( def main(
mode: ConfigureMode, mode: ConfigureMode = "install",
/, /,
) -> None: ) -> None:
"""Main script. """Main script.
@@ -194,8 +206,28 @@ def main(
else: else:
CONSOLE.log(f":heavy_check_mark: No existing completions at: {target_dir}.") CONSOLE.log(f":heavy_check_mark: No existing completions at: {target_dir}.")
elif mode == "install": elif mode == "install":
# Set to True to install completions for scripts as well.
include_scripts = False
# Find dcargs CLIs. # Find dcargs CLIs.
script_paths = list(filter(_check_dcargs_cli, scripts_dir.glob("**/*.py"))) script_paths = list(filter(_check_dcargs_cli, scripts_dir.glob("**/*.py"))) if include_scripts else []
entry_points = (
# Read pyproject.toml.
(scripts_dir.parent / "pyproject.toml")
.read_text()
# Get entrypoint lines.
.partition("[project.scripts]")[2]
.partition("[")[0]
# Split into list of strings, which should each be of the format:
# `entrypoint = "scripts.something:entrypoint"`
.strip()
.split("\n")
)
entry_points = list(
# nf-train = "..." ==> nf-train
e.partition("=")[0].strip()
for e in entry_points
)
script_names = tuple(p.name for p in script_paths) script_names = tuple(p.name for p in script_paths)
assert len(set(script_names)) == len(script_names) assert len(set(script_names)) == len(script_names)
@@ -211,8 +243,10 @@ def main(
with CONSOLE.status("[bold]:writing_hand: Generating completions...", spinner="bouncingBall"): with CONSOLE.status("[bold]:writing_hand: Generating completions...", spinner="bouncingBall"):
completion_paths = list( completion_paths = list(
concurrent_executor.map( concurrent_executor.map(
lambda path_and_shell: _generate_completion(path_and_shell[0], path_and_shell[1], completions_dir), lambda path_or_entrypoint_and_shell: _generate_completion(
itertools.product(script_paths, shells_found), path_or_entrypoint_and_shell[0], path_or_entrypoint_and_shell[1], completions_dir
),
itertools.product(script_paths + entry_points, shells_found),
) )
) )
@@ -233,16 +267,6 @@ def main(
_update_rc(completions_dir, mode, shell) _update_rc(completions_dir, mode, shell)
CONSOLE.print("[bold]All done![/bold]") CONSOLE.print("[bold]All done![/bold]")
if mode == "install":
CONSOLE.print()
CONSOLE.print("Notes:")
CONSOLE.print(
":warning: In bash, completions will trigger via [dim white]./scripts/train.py <TAB>[/dim white], but"
" not [dim white]python ./scripts/train.py <TAB>[/dim white]."
)
CONSOLE.print(
":warning: Completions are in an experimental state. If you run into any issues, please file an issue!"
)
def entrypoint(): def entrypoint():
+7 -2
View File
@@ -21,8 +21,13 @@ for completion_path in ${completions_dir}/*
do do
# /some/path/to/_our_completion_py => _our_completion_py # /some/path/to/_our_completion_py => _our_completion_py
completion_name=${completion_path##*/} completion_name=${completion_path##*/}
# _our_completion_py => our_completion.py if [[ $name == *_py ]]; then
script_name="${completion_name:1:-3}.py" # _our_completion_py => our_completion.py
script_name="${completion_name:1:-3}.py"
else
# _entry-point => entry-point
script_name="${completion_name:1}"
fi
autoload -Uz $completion_path autoload -Uz $completion_path
compdef $completion_name $script_name compdef $completion_name $script_name
+2
View File
@@ -186,6 +186,8 @@ def launch(
def main(config: cfg.Config) -> None: def main(config: cfg.Config) -> None:
"""Main function.""" """Main function."""
config.set_timestamp()
if config.trainer.load_config: if config.trainer.load_config:
logging.info(f"Loading pre-set config from: {config.trainer.load_config}") logging.info(f"Loading pre-set config from: {config.trainer.load_config}")
config = yaml.load(config.trainer.load_config.read_text(), Loader=yaml.Loader) config = yaml.load(config.trainer.load_config.read_text(), Loader=yaml.Loader)