Skip to content

Conversation

rschiewer
Copy link

@rschiewer rschiewer commented Jun 19, 2025

This modification of the original code adds two additional arguments to the NeptuneLogger initialization. The param_preproc argument is a callable that is applied to the parameter before logging instead of the default param.norm(). The grad_prerpoc functions analogously when logging gradients.

The original motivation for these changes was to provide an easy way to handle inf/NaN values before logging. However, it can also be used to choose aggregation functions different from the tensor norm.

Summary by Sourcery

Add optional preprocessing callbacks for parameters and gradients in NeptuneLogger to enable custom aggregation and handle NaN/Inf values.

New Features:

  • Add optional param_preproc and grad_preproc arguments to NeptuneLogger for custom preprocessing of parameters and gradients before logging

Documentation:

  • Update CHANGELOG.md to document new preprocessing parameters

Copy link

sourcery-ai bot commented Jun 19, 2025

Reviewer's Guide

Enhance NeptuneLogger by introducing optional preprocessing hooks for parameters and gradients, integrating them into existing logging flows, and documenting the additions in the changelog.

Sequence diagram for parameter and gradient preprocessing during logging

sequenceDiagram
    participant Model
    participant NeptuneLogger
    participant NamespaceHandler
    Model->>NeptuneLogger: Forward pass triggers parameter/gradient logging
    alt log_gradients
        NeptuneLogger->>NeptuneLogger: Apply grad_preproc or .norm() to gradient
        NeptuneLogger->>NamespaceHandler: Append processed gradient
    end
    alt log_parameters
        NeptuneLogger->>NeptuneLogger: Apply param_preproc or .norm() to parameter
        NeptuneLogger->>NamespaceHandler: Append processed parameter
    end
Loading

Class diagram for NeptuneLogger with param_preproc and grad_preproc

classDiagram
    class NeptuneLogger {
        +__init__(..., param_preproc: Optional[Callable], grad_preproc: Optional[Callable])
        -_grad_preproc: Optional[Callable]
        -_param_preproc: Optional[Callable]
        -_add_hooks_for_grads()
        -_add_hooks_for_params()
    }
Loading

File-Level Changes

Change Details Files
Introduce optional preprocessing arguments in the constructor
  • Add param_preproc and grad_preproc parameters to init signature
  • Assign them to self._param_preproc and self._grad_preproc when logging is enabled
  • Retain existing type checks and initialization logic
src/neptune_pytorch/impl/__init__.py
Apply preprocessing in hooks before logging
  • Wrap grad.norm() in a conditional using grad_preproc if provided
  • Wrap param.norm() in a conditional using param_preproc if provided
  • Maintain log frequency and namespace handling
src/neptune_pytorch/impl/__init__.py
Update CHANGELOG with new preprocessing options
  • Bump version to 2.0.1
  • Document param_preproc and grad_preproc additions in changes section
CHANGELOG.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @rschiewer - I've reviewed your changes - here's some feedback:

  • Initialize self._param_preproc and self._grad_preproc unconditionally in __init__ to ensure they always exist, avoiding potential attribute errors.
  • Extract the preprocessing-vs-default-norm logic in both hooks into a shared helper method to reduce duplication.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Initialize `self._param_preproc` and `self._grad_preproc` unconditionally in `__init__` to ensure they always exist, avoiding potential attribute errors.
- Extract the preprocessing-vs-default-norm logic in both hooks into a shared helper method to reduce duplication.

## Individual Comments

### Comment 1
<location> `src/neptune_pytorch/impl/__init__.py:102` </location>
<code_context>
         log_gradients: bool = False,
         log_parameters: bool = False,
         log_freq: int = 100,
+        param_preproc: Optional[Callable[[torch.Tensor], torch.Tensor]] = None,
+        grad_preproc: Optional[Callable[[torch.Tensor], torch.Tensor]] = None,
     ):
         verify_type("run", run, (Run, Handler))
</code_context>

<issue_to_address>
Consider clarifying the expected output type of the preproc callables.

The current type hints allow any tensor output, but downstream code expects outputs suitable for list appending (e.g., scalars or 1D tensors). Please clarify this in the documentation or enforce the expected output shape.

Suggested implementation:

```python
        param_preproc: Optional[Callable[[torch.Tensor], Union[torch.Tensor, float, int]]] = None,
        grad_preproc: Optional[Callable[[torch.Tensor], Union[torch.Tensor, float, int]]] = None,

```

```python
    ):
        """
        Args:
            ...
            param_preproc: Optional callable to preprocess parameter tensors before logging.
                The callable should return a scalar, 1D tensor, or a value suitable for appending to a list.
            grad_preproc: Optional callable to preprocess gradient tensors before logging.
                The callable should return a scalar, 1D tensor, or a value suitable for appending to a list.
        """

```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

rschiewer and others added 4 commits July 7, 2025 10:48
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant