Terrance/tensorf (#218)

* added tensorf, updated requirements for windows

* cleaning up imports

* tensorf changes

* initial working version of tensorf

* added callback for feature upsampling

* modifying tests to match changes

* reverting requirements changes

* reverting unnecessary changes

* reverting unnecessary changes

* no more upsampling on step 0

* removed density field, fixed error msg

* modified callbacks and upsampling, reinitializing optimzer in training loop

* deleting tensorf_field file"

* checkpointing progress on restructuring callbacks

* updated callback logic for before and after train capabilities

* fixing upsampling stepping

* merging

* adding tensorf to config, fixing some bugs with new callback

* changed the way callbacks detected steps argument in callback func

* fixing typing

* adding pylint disable no self use to get_training_callbacks

* fixing config

* changing callback in vanilla nerf to allow for no density field

* cleanup

* removing config yaml

Co-authored-by: Ethan Weber <eweb0124@gmail.com>
This commit is contained in:
Terrance WangandEthan Weber authored and GitHub committed 2022-08-31 23:29:13 -07:00
1 parent 6ee6934581
commit 8670f19d2d
13 files changed
+394 -52

No files matched your search

+54 -18
View File
@@ -16,13 +16,36 @@
Callback code used for training iterations
"""
from typing import Callable
from dataclasses import InitVar, dataclass
from enum import Enum, auto
from inspect import signature
from typing import Callable, Dict, List, Optional, Tuple
class Callback:
@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]
grad_scaler: Optional[InitVar]
pipeline: Optional[InitVar]
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.
including at iteration 0. The function is called after the training iteration.
Args:
update_every_num_iters: How often to call the function `func`.
@@ -31,26 +54,39 @@ class Callback:
kwargs: kwargs for the function 'func'.
"""
def __init__(self, update_every_num_iters: int, func: Callable, *args, **kwargs):
# TODO(ethan): how do we type args and kwargs?
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
self.kwargs = kwargs
self.args = args if args is not None else []
self.kwargs = kwargs if kwargs is not None else {}
def before_step(self, step: int):
"""callback to run before training step
def run_callback(self, step: int):
"""Callback to run after training step
Args:
step (int): current iteration step
"""
raise NotImplementedError
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 after_step(self, step: int):
"""callback to run after training step
Args:
step (int): current iteration step
"""
if step % self.update_every_num_iters == 0:
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."""
if location in self.where_to_run:
self.run_callback(step=step)