Skip to content

fix(pruning): validate multidimensional tensor in channel-structured pruning (#107) - #108

Open
rohith500 wants to merge 1 commit into
apple:mainfrom
rohith500:fix/pruning-channel-structured-1d
Open

rohith500 wants to merge 1 commit into
apple:mainfrom
rohith500:fix/pruning-channel-structured-1d

Conversation

@rohith500

@rohith500 rohith500 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

Closes #107.

Per maintainer alignment with @u-simha in Issue #107, this PR validates that ChannelStructured magnitude pruning is applied to tensors with at least 2 dimensions, raising an informative ValueError for 1D or 0D tensors instead of crashing with ATen RuntimeError: selected index k out of range.

  1. Multidimensional Validation in _compute_channel_mask:
    • In src/coreai_opt/pruning/spec/prune.py:
      if weight.ndim < 2:
          raise ValueError(
              f"Channel-structured pruning requires a tensor with at least 2 dimensions, "
              f"got shape {tuple(weight.shape)} with {weight.ndim} dims. "
              f"For 1D tensors, use Unstructured pruning instead."
          )
    • Adheres to PyTorch core convention (torch.nn.utils.prune._validate_structured_pruning), ensuring that the concept of "channels" is well-defined.
    • Clear error message guides users to Unstructured pruning for 1D parameters (e.g. bias or normalization weights).
  2. Early Scheme Dispatch in compute_mask:
    • Dispatches ChannelStructured directly to _compute_channel_mask so scheme-specific structural constraints are checked even if the schedule's initial sparsity is 0.0.
    • For multidimensional tensors ($N \ge 2$), _compute_channel_mask continues to handle sparsity == 0.0 (num_prune == 0) and sparsity >= 1.0 directly.

Automated Tests Added

In tests/pruning/test_magnitude_pruner.py:

  • test_channel_structured_1d_tensor_raises:
    • Parametrically verifies that applying ChannelStructured to a 1D tensor via _MagnitudePruneImpl.compute_mask raises ValueError matching "Channel-structured pruning requires a tensor with at least 2 dimensions" across axis=0, axis=-1, and multiple sparsity levels (including 0.0).
  • test_channel_structured_0d_scalar_raises:
    • Verifies that applying ChannelStructured to a 0-D scalar raises ValueError.
  • test_channel_structured_1d_parameter_model_raises:
    • Tests end-to-end model preparation (MagnitudePruner.prepare) when a 1D parameter (bias of nn.Linear) is configured with ChannelStructured, verifying it raises ValueError.
  • test_unstructured_1d_tensor_supported:
    • Verifies that Unstructured pruning properly supports 1D parameters (bias), confirming the suggested alternative in the error message.

Verification

  • pytest tests/pruning/test_magnitude_pruner.py: 46 passed, 1 xfailed in 0.31s.
  • pytest tests/pruning/: 85 passed, 1 xfailed in 3m 03s.
  • make check: All checks passed (26/26):
    • License header verification: Passed
    • Formatting & line-length (ruff, darker $\le$ 100): Passed
    • Linters (ruff-lint, checkmake, pymarkdown 0.9.40): Passed
    • Project rules (__all__, import aliasing): Passed
    • Towncrier changelog validation: Passed

Checklist

  • Follows Conventional Commits formatting (fix(pruning): validate multidimensional tensor in channel-structured pruning (#107))
  • Commit is cryptographically signed with SSH key (SHA256:0wr/nMrZodwxsOOHBGpcwRXPk8c3rePKishJLzmhj98)
  • Includes Towncrier fragment (changelog.d/107.fixed)
  • Passes make check without warnings or diff pollution
  • Includes automated tests reproducing the bug and proving the fix

@rohith500
rohith500 force-pushed the fix/pruning-channel-structured-1d branch from bf9fe4b to 7f91afb Compare September 18, 2026 20:41
@rohith500 rohith500 changed the title fix(pruning): handle 1D tensors in channel-structured pruning (#107) fix(pruning): validate multidimensional tensor in channel-structured pruning (#107) Sep 18, 2026
@rohith500

Copy link
Copy Markdown
Contributor Author

Hi @u-simha,

Per your feedback and confirmation on #107, I've updated PR #108 to enforce multidimensional tensor validation:

  1. Validation Check: In _compute_channel_mask, added if weight.ndim < 2: raise ValueError(...) requiring $\ge 2$ dimensions and directing users to Unstructured pruning for 1D tensors.
  2. Early Dispatch: In compute_mask, ChannelStructured is dispatched to _compute_channel_mask so structural constraints are validated upfront even if schedule sparsity is 0.0.
  3. Automated Tests: Updated tests/pruning/test_magnitude_pruner.py with tests verifying ValueError is raised for 1D and 0-D tensors with ChannelStructured across multiple axes and sparsities, and confirming that Unstructured() properly prunes 1D parameters.
  4. Quality Gates: All 26 checks in make check pass cleanly, commit is cryptographically signed, and the Towncrier fragment is updated.

Ready for your review!

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.

[Bug]: ChannelStructured pruning crashes with RuntimeError on 1D tensors (PyTorch sum(dim=[]) scalar collapse)

2 participants