Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log: add histogram metrics for gradients #424

Merged
merged 9 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion d3rlpy/algos/qlearning/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
convert_to_torch,
convert_to_torch_recursively,
eval_api,
get_gradients,
hard_sync,
sync_optimizer_state,
train_api,
Expand Down Expand Up @@ -378,6 +379,7 @@ def fit(
experiment_name: Optional[str] = None,
with_timestamp: bool = True,
logging_steps: int = 500,
gradient_logging_steps: Optional[int] = None,
logging_strategy: LoggingStrategy = LoggingStrategy.EPOCH,
logger_adapter: LoggerAdapterFactory = FileAdapterFactory(),
show_progress: bool = True,
Expand All @@ -403,6 +405,7 @@ def fit(
directory name.
logging_steps: Number of steps to log metrics. This will be ignored
if logging_strategy is EPOCH.
gradient_logging_steps: Number of steps to log gradients.
logging_strategy: Logging strategy to use.
logger_adapter: LoggerAdapterFactory object.
show_progress: Flag to show progress bar for iterations.
Expand All @@ -425,6 +428,7 @@ def fit(
experiment_name=experiment_name,
with_timestamp=with_timestamp,
logging_steps=logging_steps,
gradient_logging_steps=gradient_logging_steps,
logging_strategy=logging_strategy,
logger_adapter=logger_adapter,
show_progress=show_progress,
Expand All @@ -442,6 +446,7 @@ def fitter(
n_steps: int,
n_steps_per_epoch: int = 10000,
logging_steps: int = 500,
gradient_logging_steps: Optional[int] = None,
logging_strategy: LoggingStrategy = LoggingStrategy.EPOCH,
experiment_name: Optional[str] = None,
with_timestamp: bool = True,
Expand Down Expand Up @@ -471,7 +476,8 @@ def fitter(
with_timestamp: Flag to add timestamp string to the last of
directory name.
logging_steps: Number of steps to log metrics. This will be ignored
if loggig_strategy is EPOCH.
if logging_strategy is EPOCH.
gradient_logging_steps: Number of steps to log gradients.
logging_strategy: Logging strategy to use.
logger_adapter: LoggerAdapterFactory object.
show_progress: Flag to show progress bar for iterations.
Expand Down Expand Up @@ -520,6 +526,10 @@ def fitter(
# save hyperparameters
save_config(self, logger)

# watch model gradients
if gradient_logging_steps is not None:
logger.watch_model(gradient_logging_steps, self)

# training loop
n_epochs = n_steps // n_steps_per_epoch
total_step = 0
Expand Down Expand Up @@ -559,6 +569,15 @@ def fitter(

total_step += 1

if (
gradient_logging_steps is not None
and total_step % gradient_logging_steps == 0
):
for name, grad in get_gradients(
self.impl.modules.get_torch_modules()
):
logger.add_histogram(name=name, values=grad)

if (
logging_strategy == LoggingStrategy.STEPS
and total_step % logging_steps == 0
Expand Down Expand Up @@ -608,6 +627,7 @@ def fit_online(
experiment_name: Optional[str] = None,
with_timestamp: bool = True,
logging_steps: int = 500,
gradient_logging_steps: Optional[int] = None,
logging_strategy: LoggingStrategy = LoggingStrategy.EPOCH,
logger_adapter: LoggerAdapterFactory = FileAdapterFactory(),
show_progress: bool = True,
Expand Down Expand Up @@ -636,6 +656,7 @@ def fit_online(
directory name.
logging_steps: Number of steps to log metrics. This will be ignored
if logging_strategy is EPOCH.
gradient_logging_steps: Number of steps to log gradients.
logging_strategy: Logging strategy to use.
logger_adapter: LoggerAdapterFactory object.
show_progress: Flag to show progress bar for iterations.
Expand Down Expand Up @@ -673,6 +694,10 @@ def fit_online(
# save hyperparameters
save_config(self, logger)

# watch model gradients
if gradient_logging_steps is not None:
logger.watch_model(gradient_logging_steps, self)

# switch based on show_progress flag
xrange = trange if show_progress else range

Expand Down Expand Up @@ -747,6 +772,15 @@ def fit_online(
):
logger.commit(epoch, total_step)

if (
gradient_logging_steps is not None
and total_step % gradient_logging_steps == 0
):
for name, grad in get_gradients(
self.impl.modules.get_torch_modules()
):
logger.add_histogram(name=name, values=grad)

# call callback if given
if callback:
callback(self, epoch, total_step)
Expand Down
27 changes: 26 additions & 1 deletion d3rlpy/logging/file_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

import numpy as np

from .logger import LOG, LoggerAdapter, LoggerAdapterFactory, SaveProtocol
from ..types import Float32NDArray
from .logger import (
LOG,
LoggerAdapter,
LoggerAdapterFactory,
SaveProtocol,
TorchModuleProtocol,
)

__all__ = ["FileAdapter", "FileAdapterFactory"]

Expand Down Expand Up @@ -60,6 +67,19 @@ def write_metric(
with open(path, "a") as f:
print(f"{epoch},{step},{value}", file=f)

def write_histogram(
self, epoch: int, step: int, name: str, value: Float32NDArray
) -> None:
path = os.path.join(self._logdir, f"{name}.csv")
with open(path, "a") as f:
min_value = value.min()
max_value = value.max()
mean_value = value.mean()
print(
f"{epoch},{step},{name},{min_value},{max_value},{mean_value}",
file=f,
)

def after_write_metric(self, epoch: int, step: int) -> None:
pass

Expand All @@ -76,6 +96,11 @@ def close(self) -> None:
def logdir(self) -> str:
return self._logdir

def watch_model(
self, logging_steps: int, algo: TorchModuleProtocol
) -> None:
pass


class FileAdapterFactory(LoggerAdapterFactory):
r"""FileAdapterFactory class.
Expand Down
49 changes: 49 additions & 0 deletions d3rlpy/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
from datetime import datetime
from typing import Any, DefaultDict, Dict, Iterator, List

import numpy as np
import structlog
from torch import nn
from typing_extensions import Protocol

from ..types import Float32NDArray

__all__ = [
"LOG",
"set_log_context",
Expand Down Expand Up @@ -39,6 +43,18 @@ class SaveProtocol(Protocol):
def save(self, fname: str) -> None: ...


class ModuleProtocol(Protocol):
def get_torch_modules(self) -> List[nn.Module]: ...


class ImplProtocol(Protocol):
modules: ModuleProtocol


class TorchModuleProtocol(Protocol):
impl: ImplProtocol


class LoggerAdapter(Protocol):
r"""Interface of LoggerAdapter."""

Expand Down Expand Up @@ -69,6 +85,18 @@ def write_metric(
value: Metric value.
"""

def write_histogram(
self, epoch: int, step: int, name: str, values: Float32NDArray
) -> None:
r"""Writes histogram.

Args:
epoch: Epoch.
step: Training step.
name: Histogram name.
values: Histogram values.
"""

def after_write_metric(self, epoch: int, step: int) -> None:
r"""Callback executed after write_metric method.

Expand All @@ -88,6 +116,16 @@ def save_model(self, epoch: int, algo: SaveProtocol) -> None:
def close(self) -> None:
r"""Closes this LoggerAdapter."""

def watch_model(
self, logging_steps: int, algo: TorchModuleProtocol
) -> None:
r"""Watch model parameters / gradients during training.

Args:
logging_steps: Training step.
algo: Algorithm.
"""


class LoggerAdapterFactory(Protocol):
r"""Interface of LoggerAdapterFactory."""
Expand Down Expand Up @@ -123,6 +161,7 @@ def __init__(
self._experiment_name = experiment_name
self._adapter = adapter_factory.create(self._experiment_name)
self._metrics_buffer = defaultdict(list)
self._histogram_metrics_buffer = defaultdict(list)

def add_params(self, params: Dict[str, Any]) -> None:
self._adapter.write_params(params)
Expand All @@ -131,6 +170,9 @@ def add_params(self, params: Dict[str, Any]) -> None:
def add_metric(self, name: str, value: float) -> None:
self._metrics_buffer[name].append(value)

def add_histogram(self, name: str, values: Float32NDArray) -> None:
self._histogram_metrics_buffer[name].append(values)

def commit(self, epoch: int, step: int) -> Dict[str, float]:
self._adapter.before_write_metric(epoch, step)

Expand All @@ -140,6 +182,10 @@ def commit(self, epoch: int, step: int) -> Dict[str, float]:
self._adapter.write_metric(epoch, step, name, metric)
metrics[name] = metric

for name, buffer in self._histogram_metrics_buffer.items():
histogram_values = np.concatenate(buffer)
self._adapter.write_histogram(epoch, step, name, histogram_values)

LOG.info(
f"{self._experiment_name}: epoch={epoch} step={step}",
epoch=epoch,
Expand Down Expand Up @@ -171,3 +217,6 @@ def measure_time(self, name: str) -> Iterator[None]:
@property
def adapter(self) -> LoggerAdapter:
return self._adapter

def watch_model(self, logging_steps: int, algo: TorchModuleProtocol) -> None:
self._adapter.watch_model(logging_steps, algo)
18 changes: 17 additions & 1 deletion d3rlpy/logging/noop_adapter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from typing import Any, Dict

from .logger import LoggerAdapter, LoggerAdapterFactory, SaveProtocol
from ..types import Float32NDArray
from .logger import (
LoggerAdapter,
LoggerAdapterFactory,
SaveProtocol,
TorchModuleProtocol,
)

__all__ = ["NoopAdapter", "NoopAdapterFactory"]

Expand All @@ -23,6 +29,11 @@ def write_metric(
) -> None:
pass

def write_histogram(
self, epoch: int, step: int, name: str, values: Float32NDArray
) -> None:
pass

def after_write_metric(self, epoch: int, step: int) -> None:
pass

Expand All @@ -32,6 +43,11 @@ def save_model(self, epoch: int, algo: SaveProtocol) -> None:
def close(self) -> None:
pass

def watch_model(
self, logging_steps: int, algo: TorchModuleProtocol
) -> None:
pass


class NoopAdapterFactory(LoggerAdapterFactory):
r"""NoopAdapterFactory class.
Expand Down
18 changes: 17 additions & 1 deletion d3rlpy/logging/tensorboard_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

import numpy as np

from .logger import LoggerAdapter, LoggerAdapterFactory, SaveProtocol
from ..types import Float32NDArray
from .logger import (
LoggerAdapter,
LoggerAdapterFactory,
SaveProtocol,
TorchModuleProtocol,
)

__all__ = ["TensorboardAdapter", "TensorboardAdapterFactory"]

Expand Down Expand Up @@ -50,6 +56,11 @@ def write_metric(
self._writer.add_scalar(f"metrics/{name}", value, epoch)
self._metrics[name] = value

def write_histogram(
self, epoch: int, step: int, name: str, value: Float32NDArray
) -> None:
self._writer.add_histogram(f"histograms/{name}_grad", value, epoch)

def after_write_metric(self, epoch: int, step: int) -> None:
self._writer.add_hparams(
self._params,
Expand All @@ -64,6 +75,11 @@ def save_model(self, epoch: int, algo: SaveProtocol) -> None:
def close(self) -> None:
self._writer.close()

def watch_model(
self, logging_steps: int, algo: TorchModuleProtocol
) -> None:
pass


class TensorboardAdapterFactory(LoggerAdapterFactory):
r"""TensorboardAdapterFactory class.
Expand Down
20 changes: 19 additions & 1 deletion d3rlpy/logging/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from typing import Any, Dict, Sequence

from .logger import LoggerAdapter, LoggerAdapterFactory, SaveProtocol
from ..types import Float32NDArray
from .logger import (
LoggerAdapter,
LoggerAdapterFactory,
SaveProtocol,
TorchModuleProtocol,
)

__all__ = ["CombineAdapter", "CombineAdapterFactory"]

Expand Down Expand Up @@ -32,6 +38,12 @@ def write_metric(
for adapter in self._adapters:
adapter.write_metric(epoch, step, name, value)

def write_histogram(
self, epoch: int, step: int, name: str, values: Float32NDArray
) -> None:
for adapter in self._adapters:
adapter.write_histogram(epoch, step, name, values)

def after_write_metric(self, epoch: int, step: int) -> None:
for adapter in self._adapters:
adapter.after_write_metric(epoch, step)
Expand All @@ -44,6 +56,12 @@ def close(self) -> None:
for adapter in self._adapters:
adapter.close()

def watch_model(
self, logging_steps: int, algo: TorchModuleProtocol
) -> None:
for adapter in self._adapters:
adapter.watch_model(logging_steps, algo)


class CombineAdapterFactory(LoggerAdapterFactory):
r"""CombineAdapterFactory class.
Expand Down
Loading
Loading