Skip to content

Conversation

@ytl0623
Copy link
Contributor

@ytl0623 ytl0623 commented Dec 19, 2025

Fixes #8601

Description

Support alpha as a list, tuple, or tensor of floats, in addition to the existing scalar support.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 19, 2025

Walkthrough

The PR updates monai/losses/focal_loss.py so FocalLoss.init, softmax_focal_loss, and sigmoid_focal_loss accept alpha: float | Sequence[float] | None (and softmax/sigmoid accept torch.Tensor). The forward path computes an intermediate alpha_arg from self.alpha, validates per-class sequences against number of classes, and broadcasts per-class alpha tensors to match prediction shapes. Scalar alpha is still supported; when include_background is False and use_softmax is True, scalar alpha is ignored with a warning while per-class sequences are validated and used. Public signatures and internal uses were adjusted accordingly.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Validate sequence-length checks and raised errors for mismatches in softmax/sigmoid functions.
  • Confirm broadcasting logic converts per-class alpha to correct dtype/device and shape (broadcastable to input).
  • Ensure alpha_arg is derived without mutating self.alpha and is used consistently in both softmax and sigmoid paths.
  • Review warning messaging and include_background=False branching for correctness.

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly and concisely describes the main change: adding per-class alpha weights to FocalLoss.
Description check ✅ Passed Description adequately documents the change—support for alpha as list/tuple/tensor—with issue reference and appropriate checklist items marked.
Linked Issues check ✅ Passed Code changes fully implement the objectives: FocalLoss alpha now accepts sequences of floats for per-class weights with proper validation and broadcasting.
Out of Scope Changes check ✅ Passed All changes are scoped to focal_loss.py and directly address per-class alpha support; no out-of-scope modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
monai/losses/focal_loss.py (1)

68-120: Add tests for sequence alpha feature.

The new alpha parameter now accepts sequences but has no test coverage. Add tests for:

  • Sequence alpha with correct length (both softmax and sigmoid modes)
  • Sequence alpha with incorrect length (should raise ValueError)
  • Sequence alpha with include_background=False (should work)
  • Broadcasting behavior across spatial dimensions
🧹 Nitpick comments (5)
monai/losses/focal_loss.py (5)

81-87: Clarify docstring with example.

The interaction between include_background, use_softmax, and alpha type is complex. Consider adding a brief example showing sequence alpha usage, e.g., alpha=[0.25, 0.35, 0.4] for 3-class case.


167-167: Add stacklevel to warning.

Per static analysis and best practice, specify stacklevel=2 so the warning points to the user's code, not this internal method.

🔎 Proposed fix
-                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.")
+                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.", stacklevel=2)

222-237: Sequence length validation deferred to runtime.

The check that alpha sequence length matches class count (lines 229-232) occurs inside the loss function, not at initialization or start of forward. This means the error surfaces during training rather than at model construction. Consider validating alpha length earlier if class count can be inferred.


230-232: Simplify exception message.

Per static analysis (TRY003), extract long messages into a constant or use shorter inline text.


272-274: Simplify exception message.

Per static analysis (TRY003), extract long messages into a constant or use shorter inline text.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 15fd428 and 88b1182.

📒 Files selected for processing (1)
  • monai/losses/focal_loss.py (6 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/losses/focal_loss.py
🧬 Code graph analysis (1)
monai/losses/focal_loss.py (1)
monai/utils/enums.py (1)
  • LossReduction (253-264)
🪛 Ruff (0.14.8)
monai/losses/focal_loss.py

166-166: Local variable alpha_arg is assigned to but never used

Remove assignment to unused variable alpha_arg

(F841)


167-167: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)


230-232: Avoid specifying long messages outside the exception class

(TRY003)


272-274: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-pytorch (2.5.1)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: min-dep-os (windows-latest)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: flake8-py3 (pytype)
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: build-docs
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: packaging
🔇 Additional comments (2)
monai/losses/focal_loss.py (2)

73-73: Type hint correctly extended.

The signature now accepts scalar or sequence for per-class alpha weighting.


265-281: Sequence alpha logic correct; validation deferred.

The per-class alpha handling properly validates length (lines 271-274) and broadcasts (lines 276-277). However, like softmax_focal_loss, validation occurs at runtime rather than earlier. The broadcasting and alpha_factor computation are correct.

Similar to softmax_focal_loss, consider validating alpha sequence length earlier in the lifecycle.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
monai/losses/focal_loss.py (4)

161-170: Critical bug from previous review is fixed.

The code now correctly passes the local variable alpha_arg instead of the undefined self.alpha_arg. The logic properly handles include_background=False by nullifying scalar alpha (which assumes background weighting) while preserving sequence alpha.

Add stacklevel=2 to the warning.

Line 167 should include stacklevel=2 for proper warning attribution.

🔎 Fix for warning stacklevel
-                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.")
+                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.", stacklevel=2)

210-239: Softmax focal loss correctly handles scalar and sequence alpha.

The scalar path implements standard focal loss weighting (1-alpha for background, alpha for foreground). Sequence validation ensures length matches the number of classes, and broadcasting is correct.

Minor: Long error message inside ValueError.

Lines 230-232 define a long message inline. Consider extracting to a constant if this pattern recurs, but acceptable as-is.


242-283: Sigmoid focal loss correctly handles scalar and sequence alpha.

The scalar path applies alpha when target=1 and 1-alpha when target=0. Sequence validation and per-channel broadcasting are implemented correctly.

Minor: Long error message inside ValueError.

Lines 272-274 define a long message inline. Consider extracting to a constant if this pattern recurs, but acceptable as-is.


73-73: Verify test coverage for new sequence alpha functionality.

The PR adds per-class alpha support, but test checklist items are unchecked. Ensure tests cover:

  • Sequence alpha with include_background=True and False
  • Length validation errors
  • Both softmax and sigmoid variants
  • Edge cases (single class, empty sequence, etc.)

Do you want me to generate a shell script to check for existing test files that cover FocalLoss, or open an issue to track test coverage?

Also applies to: 211-211, 243-243

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 88b1182 and 820ce94.

📒 Files selected for processing (1)
  • monai/losses/focal_loss.py (6 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/losses/focal_loss.py
🧬 Code graph analysis (1)
monai/losses/focal_loss.py (1)
monai/utils/enums.py (1)
  • LossReduction (253-264)
🪛 Ruff (0.14.8)
monai/losses/focal_loss.py

167-167: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)


230-232: Avoid specifying long messages outside the exception class

(TRY003)


272-274: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: build-docs
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: packaging
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: flake8-py3 (pytype)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-pytorch (2.5.1)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: min-dep-os (windows-latest)
🔇 Additional comments (1)
monai/losses/focal_loss.py (1)

73-73: Type signature and documentation updated correctly.

The alpha parameter now accepts sequences, and the docstring clearly specifies the length requirement relative to include_background.

Also applies to: 81-81, 85-87

Signed-off-by: ytl0623 <[email protected]>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
monai/losses/focal_loss.py (3)

81-87: Clarify softmax scalar alpha convention in docstring.

The docstring describes sequence alpha but doesn't explain that scalar alpha in softmax mode applies 1-alpha to the background class (index 0) and alpha to all other classes. This differs from sigmoid mode where alpha is applied symmetrically based on target value. Document this convention for user clarity.


223-238: Softmax alpha implementation correct.

The implementation properly handles both scalar (asymmetric background weighting) and sequence (explicit per-class) alpha. Length validation and broadcasting are correct.

Optional: The docstring states alpha "should be in [0, 1]" but there's no runtime validation. Consider adding a check if strict enforcement is desired, though this may be left as a user responsibility.


267-281: Sigmoid alpha implementation correct.

The implementation properly handles scalar and sequence alpha with standard focal loss formulation. Length validation and per-class broadcasting are correct.

Optional: Same as softmax—consider adding [0, 1] range validation if strict enforcement is desired.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 820ce94 and 50cc7e9.

📒 Files selected for processing (1)
  • monai/losses/focal_loss.py (6 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/losses/focal_loss.py
🧬 Code graph analysis (1)
monai/losses/focal_loss.py (1)
monai/utils/enums.py (1)
  • LossReduction (253-264)
🪛 Ruff (0.14.8)
monai/losses/focal_loss.py

167-167: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)


231-233: Avoid specifying long messages outside the exception class

(TRY003)


273-275: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: flake8-py3 (pytype)
  • GitHub Check: packaging
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: build-docs
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-pytorch (2.5.1)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-os (windows-latest)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: min-dep-pytorch (2.7.1)
🔇 Additional comments (2)
monai/losses/focal_loss.py (2)

73-73: Type hint correctly updated.

The signature now accepts scalar, sequence, or None for per-class alpha weighting.


162-170: Forward logic correct.

The alpha_arg local variable correctly handles the special case where scalar alpha is nulled for softmax with include_background=False, while preserving sequence alpha for explicit per-class control.

loss = softmax_focal_loss(input, target, self.gamma, self.alpha)
if isinstance(self.alpha, (float, int)):
alpha_arg = None
warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.")
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add stacklevel=2 to warning.

Set explicit stacklevel=2 so the warning points to the caller rather than this internal line.

🔎 Proposed fix
-                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.")
+                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.", stacklevel=2)
🧰 Tools
🪛 Ruff (0.14.8)

167-167: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)

🤖 Prompt for AI Agents
In monai/losses/focal_loss.py around line 167, the warnings.warn call should
include stacklevel=2 so the warning points to the caller; update the call to
pass stacklevel=2 (e.g., warnings.warn("`include_background=False`, scalar
`alpha` ignored when using softmax.", stacklevel=2)) so the warning's traceback
references the caller rather than this internal line.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
monai/losses/focal_loss.py (1)

167-167: Add stacklevel=2 to warning.

Set explicit stacklevel=2 so the warning points to the caller.

🔎 Proposed fix
-                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.")
+                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.", stacklevel=2)
🧹 Nitpick comments (2)
monai/losses/focal_loss.py (2)

223-238: LGTM.

Per-class alpha logic is correct: scalar creates standard background/foreground weighting; sequence validates length and broadcasts properly.

Optional: Consider validating that alpha values are in [0,1] as documented in the docstring.


267-281: LGTM.

Per-class alpha logic is correct: scalar preserves original behavior; sequence validates length and applies per-class weighting with proper broadcasting.

Optional: Consider validating that alpha values are in [0,1] as documented in the docstring.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 50cc7e9 and 6d41595.

📒 Files selected for processing (1)
  • monai/losses/focal_loss.py (7 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/losses/focal_loss.py
🧬 Code graph analysis (1)
monai/losses/focal_loss.py (1)
monai/utils/enums.py (1)
  • LossReduction (253-264)
🪛 Ruff (0.14.8)
monai/losses/focal_loss.py

167-167: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)


231-233: Avoid specifying long messages outside the exception class

(TRY003)


273-275: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: min-dep-os (windows-latest)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-pytorch (2.5.1)
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: build-docs
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: packaging
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: flake8-py3 (pytype)
🔇 Additional comments (4)
monai/losses/focal_loss.py (4)

16-16: LGTM.

Import correctly added for sequence type hints.


73-73: LGTM.

Signature correctly extends alpha to accept per-class sequences while preserving backward compatibility.


81-87: LGTM.

Docstring accurately describes new per-class alpha behavior and interaction with include_background.


162-170: LGTM.

Logic correctly handles scalar alpha with include_background=False by nullifying it and warning. The alpha_arg intermediate variable prevents mutation of self.alpha.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
monai/losses/focal_loss.py (1)

167-167: Add stacklevel=2 to warning.

Still missing from previous review.

🧹 Nitpick comments (1)
monai/losses/focal_loss.py (1)

210-218: Add docstrings for alpha parameter.

Both softmax_focal_loss and sigmoid_focal_loss lack documentation for the alpha parameter, including its new sequence support.

Also applies to: 243-250

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 6d41595 and 4211ca5.

📒 Files selected for processing (1)
  • monai/losses/focal_loss.py (7 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/losses/focal_loss.py
🧬 Code graph analysis (1)
monai/losses/focal_loss.py (1)
monai/utils/enums.py (1)
  • LossReduction (253-264)
🪛 Ruff (0.14.8)
monai/losses/focal_loss.py

73-73: Undefined name Sequence

(F821)


74-74: Undefined name Sequence

(F821)


167-167: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)


231-233: Avoid specifying long messages outside the exception class

(TRY003)


273-275: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-pytorch (2.5.1)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-os (windows-latest)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: flake8-py3 (pytype)
  • GitHub Check: packaging
🔇 Additional comments (4)
monai/losses/focal_loss.py (4)

84-87: LGTM!

Docstring clearly describes the new per-class alpha behavior and validation requirements.


162-170: LGTM!

The alpha_arg logic correctly preserves original alpha while handling the scalar edge case for include_background=False.


222-238: LGTM!

Scalar vs sequence handling is clean. Validation and broadcasting are correct.


266-282: LGTM!

Per-channel alpha handling correctly extends the scalar semantics to sequence form.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
monai/losses/focal_loss.py (1)

175-175: Add stacklevel=2 to warning.

The warning should specify stacklevel=2 so it points to the caller rather than this internal line.

🔎 Proposed fix
-                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.")
+                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.", stacklevel=2)
🧹 Nitpick comments (5)
monai/losses/focal_loss.py (5)

81-87: Docstring is accurate but validation is missing.

Line 85 states alpha values should be in [0, 1], but the code doesn't enforce this. Consider adding validation in __init__.

Optionally, adding a brief example of per-class alpha usage (e.g., alpha=[0.1, 0.3, 0.6] for 3 classes) would improve clarity.

🔎 Suggested validation
         if isinstance(alpha, (list, tuple)):
             self.alpha = torch.tensor(alpha)
+            if (self.alpha < 0).any() or (self.alpha > 1).any():
+                raise ValueError("All alpha values must be in the range [0, 1].")
         else:
             self.alpha = alpha
+            if isinstance(alpha, (float, int)) and not (0 <= alpha <= 1):
+                raise ValueError("Alpha must be in the range [0, 1].")

167-170: Alpha device handling is correct.

Properly transfers tensor alpha to the input device. Minor optimization: could skip device transfer for scalar alpha, but current implementation is safe and correct.


230-246: Alpha handling logic is correct.

Properly distinguishes scalar (background/foreground weighting) from sequence (per-class weighting). Validation on line 238 ensures sequence length matches number of classes.

Minor: Static analysis suggests shorter exception messages (TRY003), but this is stylistic and the descriptive message is helpful.


274-289: Per-channel alpha implementation is correct.

Scalar alpha applies standard focal loss weighting, while sequence alpha provides per-channel control. Broadcasting on lines 284-286 properly handles multi-dimensional targets.

Minor: Consider shorter exception message per TRY003, though current message is clear.


73-73: Verify test coverage for per-class alpha.

Ensure tests cover:

  • Sequence alpha for both softmax and sigmoid modes
  • Validation error when sequence length mismatches number of classes
  • Behavior with include_background=False + sequence alpha
  • Edge cases: empty sequence, values outside [0,1] if validation is added

Do you want me to generate test cases for these scenarios?

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 4211ca5 and 1b24834.

📒 Files selected for processing (1)
  • monai/losses/focal_loss.py (7 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/losses/focal_loss.py
🧬 Code graph analysis (1)
monai/losses/focal_loss.py (1)
monai/utils/enums.py (1)
  • LossReduction (253-264)
🪛 Ruff (0.14.8)
monai/losses/focal_loss.py

175-175: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)


239-241: Avoid specifying long messages outside the exception class

(TRY003)


281-283: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: build-docs
  • GitHub Check: flake8-py3 (pytype)
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: packaging
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: min-dep-os (windows-latest)
  • GitHub Check: min-dep-pytorch (2.5.1)
🔇 Additional comments (4)
monai/losses/focal_loss.py (4)

14-16: Imports are correct.

Sequence from collections.abc is properly imported and used consistently throughout the file.


73-74: Signature extension looks good.

The alpha parameter now correctly accepts per-class weights via Sequence[float].


120-125: Conversion logic is correct.

Properly converts sequence types to torch.Tensor while preserving scalar floats.


176-178: Loss function calls are correct.

Both softmax and sigmoid paths properly forward the alpha_arg parameter.

Signed-off-by: ytl0623 <[email protected]>
Signed-off-by: ytl0623 <[email protected]>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
monai/losses/focal_loss.py (1)

176-176: Add stacklevel=2 to warning.

The warning should include stacklevel=2 to point to the caller.

🔎 Proposed fix
-                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.")
+                    warnings.warn("`include_background=False`, scalar `alpha` ignored when using softmax.", stacklevel=2)
🧹 Nitpick comments (3)
monai/losses/focal_loss.py (3)

85-87: Clarify that range applies to each alpha element.

The docstring states "The value should be in [0, 1]" but doesn't explicitly say this applies to each element when alpha is a sequence. Consider: "Each value should be in [0, 1]."


237-246: Document scalar alpha behavior for softmax.

When alpha is scalar, lines 239-240 construct [1-alpha, alpha, alpha, ...] treating alpha as the weight for all foreground classes vs. 1-alpha for background. This design choice isn't documented in the function docstring or main class docstring.

Consider adding a note explaining this convention for multi-class softmax with scalar alpha.


73-73: Verify test coverage for sequence alpha.

The PR adds significant new functionality (per-class alpha via sequences). Ensure test coverage includes:

  • Valid sequence alpha for both softmax and sigmoid paths
  • include_background=False with sequence alpha
  • Length mismatch errors (lines 242-245, 288-291)
  • Device and dtype handling for tensor alpha

Do you want me to help generate test cases for these scenarios?

Also applies to: 220-220, 256-256

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 1b24834 and a574d7b.

📒 Files selected for processing (1)
  • monai/losses/focal_loss.py (7 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/losses/focal_loss.py
🧬 Code graph analysis (1)
monai/losses/focal_loss.py (1)
monai/utils/enums.py (1)
  • LossReduction (253-264)
🪛 Ruff (0.14.8)
monai/losses/focal_loss.py

176-176: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)


243-245: Avoid specifying long messages outside the exception class

(TRY003)


289-291: Avoid specifying long messages outside the exception class

(TRY003)

🔇 Additional comments (1)
monai/losses/focal_loss.py (1)

279-296: Sigmoid sequence alpha implementation looks correct.

The per-class alpha handling properly extends the binary focal loss formula to multi-class multi-label scenarios. Device/dtype handling and broadcasting are correct.

@ytl0623
Copy link
Contributor Author

ytl0623 commented Dec 19, 2025

Hi @ericspod,

sorry to bother u. the CI is failing with [Errno 28] No space left on device during the build-docs and packaging steps. This seems to be a server-side issue. Could u plz re-trigger the workflows?

Thanks in advance!

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.

Weights in alpha for FocalLoss

1 participant