moving around files (#616)

moved around files
This commit is contained in:
Ethan Weber authored and GitHub committed 2022-10-02 16:54:34 -07:00
1 parent 9d15ba6a2a
commit c20fed83bb
61 files changed
+226 -332

No files matched your search

-106
View File
@@ -1,106 +0,0 @@
# Copyright 2022 The Plenoptix Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Callback code used for training iterations
"""
from dataclasses import InitVar, dataclass
from enum import Enum, auto
from inspect import signature
from typing import Callable, Dict, List, Optional, Tuple
from nerfstudio.configs.base_config import TrainerConfig
@dataclass
class TrainingCallbackAttributes:
"""Attributes that can be used to configure training callbacks.
The callbacks can be specified in the Dataloader or Model implementations.
Instead of providing access to the entire Trainer object, we only provide these attributes.
This should be least prone to errors and fairly clean from a user perspective."""
# TODO(ethan): type this without circular imports
optimizers: Optional[InitVar]
"""optimizers for training"""
grad_scaler: Optional[InitVar]
"""gradient scalers"""
pipeline: Optional[InitVar]
"""reference to training pipeline"""
config: TrainerConfig
"""the trainer config"""
class TrainingCallbackLocation(Enum):
"""Enum for specifying where the training callback should be run."""
BEFORE_TRAIN_ITERATION = auto()
AFTER_TRAIN_ITERATION = auto()
class TrainingCallback:
"""Callback class used during training.
The function 'func' with 'args' and 'kwargs' will be called every 'update_every_num_iters' training iterations,
including at iteration 0. The function is called after the training iteration.
Args:
where_to_run: List of locations for when to run callbak (before/after iteration)
func: The function that will be called.
update_every_num_iters: How often to call the function `func`.
iters: Tuple of iteration steps to perform callback
args: args for the function 'func'.
kwargs: kwargs for the function 'func'.
"""
def __init__(
self,
where_to_run: List[TrainingCallbackLocation],
func: Callable,
update_every_num_iters: Optional[int] = None,
iters: Optional[Tuple[int, ...]] = None,
args: Optional[List] = None,
kwargs: Optional[Dict] = None,
):
assert (
"step" in signature(func).parameters.keys()
), f"'step: int' must be an argument in the callback function 'func': {func.__name__}"
self.where_to_run = where_to_run
self.update_every_num_iters = update_every_num_iters
self.iters = iters
self.func = func
self.args = args if args is not None else []
self.kwargs = kwargs if kwargs is not None else {}
def run_callback(self, step: int):
"""Callback to run after training step
Args:
step: current iteration step
"""
if self.update_every_num_iters is not None:
if step % self.update_every_num_iters == 0:
self.func(*self.args, **self.kwargs, step=step)
elif self.iters is not None:
if step in self.iters:
self.func(*self.args, **self.kwargs, step=step)
def run_callback_at_location(self, step: int, location: TrainingCallbackLocation):
"""Runs the callback if it's supposed to be run at the given location.
Args:
step: current iteration step
location: when to run callback (before/after iteration)
"""
if location in self.where_to_run:
self.run_callback(step=step)
File renamed without changes.
+1 -25
View File
@@ -15,10 +15,9 @@
"""
Input/output utils.
"""
import json
import pickle
from pathlib import Path
from typing import Any
def load_from_json(filename: Path):
@@ -42,26 +41,3 @@ def write_to_json(filename: Path, content: dict):
assert filename.suffix == ".json"
with open(filename, "w", encoding="UTF-8") as file:
json.dump(content, file)
def load_from_pkl(filename: Path):
"""Load from a pickle file.
Args:
filename: The filename to load from.
"""
assert filename.suffix == ".pkl"
with open(filename, "rb") as f:
return pickle.load(f)
def write_to_pkl(filename: Path, content: Any):
"""Write to a pickle file.
Args:
filename (str): The filename to write to.
content (Any): The data to write.
"""
assert filename.suffix == ".pkl"
with open(filename, "wb") as f:
pickle.dump(content, f)
+1 -34
View File
@@ -16,9 +16,6 @@
Miscellaneous helper code.
"""
import hashlib
import json
from math import floor, log
from typing import Any, Callable, Dict, List, Optional, Union
import torch
@@ -59,11 +56,6 @@ def get_dict_to_cpu(stuff: Any):
return stuff
def is_not_none(var: Any) -> bool:
"""Return True if the variable var is None."""
return not isinstance(var, type(None))
def get_masked_dict(d, mask):
"""Return a masked dictionary.
TODO(ethan): add more asserts/checks so this doesn't have unpredictable behavior.
@@ -78,19 +70,6 @@ def get_masked_dict(d, mask):
return masked_dict
def get_hash_str_from_dict(dictionary: Dict[str, Any]) -> str:
"""MD5 hash of a dictionary. Based on
https://www.doc.ic.ac.uk/~nuric/coding/how-to-hash-a-dictionary-in-python.html
Args:
dictionary: dict to process
"""
dhash = hashlib.md5()
encoded = json.dumps(dictionary, sort_keys=True).encode()
dhash.update(encoded)
return dhash.hexdigest()
class IterableWrapper: # pylint: disable=too-few-public-methods
"""A helper that will allow an instance of a class to return multiple kinds of iterables bound
to different functions of that class.
@@ -133,18 +112,6 @@ class IterableWrapper: # pylint: disable=too-few-public-methods
return self
def human_format(num):
"""Format a number in a more human readable way
Args:
num: number to format
"""
units = ["", "K", "M", "B", "T", "P"]
k = 1000.0
magnitude = int(floor(log(num, k)))
return f"{(num / k**magnitude):.2f} {units[magnitude]}"
def scale_dict(dictionary: Dict[Any, Any], coefficients: Dict[str, float]) -> Dict[Any, Any]:
"""Scale a dictionary in-place given a coefficients dictionary.
@@ -168,7 +135,7 @@ def step_check(step, step_size, run_at_zero=False) -> bool:
return (run_at_zero or step != 0) and step % step_size == 0
def _update_avg(prev_avg: float, new_val: float, step: int) -> float:
def update_avg(prev_avg: float, new_val: float, step: int) -> float:
"""helper to calculate the running average
Args:
File renamed without changes.
@@ -14,6 +14,8 @@
"""A collection of common strings and print statements used throughout the codebase."""
from math import floor, log
from rich.console import Console
console = Console(width=120)
@@ -29,3 +31,15 @@ def print_tcnn_speed_warning(method_name: str):
)
console.print("[yellow]pip install git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch")
console.line()
def human_format(num):
"""Format a number in a more human readable way
Args:
num: number to format
"""
units = ["", "K", "M", "B", "T", "P"]
k = 1000.0
magnitude = int(floor(log(num, k)))
return f"{(num / k**magnitude):.2f} {units[magnitude]}"
+1 -1
View File
@@ -32,7 +32,7 @@ from torchtyping import TensorType
from nerfstudio.configs import base_config as cfg
from nerfstudio.utils.decorators import check_main_thread, decorate_all
from nerfstudio.utils.misc import human_format
from nerfstudio.utils.printing import human_format
CONSOLE = console.Console()