Skip to content

Fix weighted_average reduction for dim=0 - #3337

Open
Robertboy18 wants to merge 1 commit into
awslabs:devfrom
Robertboy18:fix-weighted-average-dim-zero
Open

Fix weighted_average reduction for dim=0#3337
Robertboy18 wants to merge 1 commit into
awslabs:devfrom
Robertboy18:fix-weighted-average-dim-zero

Conversation

@Robertboy18

@Robertboy18 Robertboy18 commented Aug 7, 2026

Copy link
Copy Markdown

Issue #, if available:

None.

Description of changes:

Hi! I was playing around with formalizing the behavior of weighted_average in Lean with TorchLean, and noticed that dim=0 takes the all-dimensions branch because zero is falsey.

When weights are provided, this collapses the whole tensor to a scalar instead of reducing along the first axis. This changes the condition to distinguish dim=0 from dim=None and adds a regression test with nonuniform weights. The existing all-dimensions behavior for dim=None stays the same.

Tests:

  • PYTHONPATH=src python -m pytest test/torch/test_torch_util.py -q (8 passed)
  • ruff format --check src/gluonts/torch/util.py test/torch/test_torch_util.py
  • ruff check src/gluonts/torch/util.py test/torch/test_torch_util.py

I also have a corresponding TorchLean tensor formalization of the axis-zero
shape and value if that would be useful :)

By submitting this pull request, I confirm that you can use, modify, copy, and
redistribute this contribution, under the terms of your choice.

Suggested label: bug fix

Copilot AI lite review requested due to automatic review settings August 7, 2026 19:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants