Scope Specification
Scope-level sparsity specification over block grid.
- class sparsekit.scope.ScopeSpec(block, shape, name=None)[source]
Bases:
SparseScopeOrganizes blocks from a SparseNode into scopes (decision units).
Divides the block grid into scopes of
shapeblocks. Pruning decisions (hard/soft threshold, mask selection) operate at the scope level: within each scope, blocks compete to survive.- Parameters:
block (SparseBlock) – SparseBlock or BlockCoupling that defines the block grid. Must have
grid_shapedivisible byshape.shape (Tuple[int, ...]) – Number of blocks of scope in each dimension. Use -1 to span the entire dimension.
name (str | None) – Optional name for identification.
- block: SparseBlock
- block_to_scope(b, reorder=True, merge=False)[source]
Reshape a block-grid tensor into scope layout.
- kth_largest(element_values, nnz)[source]
Calculates the k-th largest score across all blocks from all specs. This is used to determine the threshold for pruning.
- kth_mid(element_values, nnz, k_weight=1.0)[source]
Calculates the k-th largest score across all blocks from all specs. This is used to determine the threshold for pruning.
- hard_threshold(thresholds=None, nnz=None, values=None)[source]
Zero out blocks in-place based on thresholds.
Exactly one of
thresholdsornnzmust be given.
- get_masks(nnz, values=None, block_scores=None, block_mask=None, **kwargs)[source]
Compute element-level boolean masks from scope-level scores.
- Parameters:
nnz (int) – Number of blocks to keep per scope.
block_scores (Tensor | None) – Pre-computed scores with shape
(*grid_shape, block_numel).values (Tensor | Mapping[Any, Tensor] | None) – Element values for computing block norms (if scores not given).
block_mask (Tensor | None) – Pre-computed boolean mask to use directly.
- Returns:
Dict mapping each SparseBlock to its element-level boolean mask.
- Return type:
- soft_threshold(thresholds, conditioners=None, scale=False, max_iter=20, atol=1e-08, eps=None)[source]
Apply soft thresholding (L1 proximal operator) to scopes in-place.
- Parameters:
thresholds (Tensor) – Per-scope threshold values with shape
grid_shape.conditioners (Tensor | Mapping[Any, Tensor] | None) – Diagonal preconditioner per SparseBlock.
scale (bool) – If True, scale thresholds by sqrt(block_numel).
max_iter (int) – Maximum bisection iterations for Adam variant.
eps (float | None) – Small constant for numerical stability.
atol (float) – Absolute tolerance for convergence.
- Return type:
None
- class sparsekit.scope.ScopeCoupling(scopes, orders, name=None)[source]
Bases:
SparseScopeCouples multiple ScopeSpec instances for joint pruning.
Aligns scope grids from different parameters via dimension permutations (
orders) so they share a common grid shape. Within each aligned scope, blocks from all specs compete to survive pruning.- Parameters:
- kth_largest(k, values)[source]
Calculates the k-th largest score across all blocks from all specs. This is used to determine the threshold for pruning.
- hard_threshold(thresholds=None, nnz=None, values=None)[source]
Compute kappa-largest block norm among coupled scopes from all specs then sends the threshold to specs to hard-threshold in-place. Note that the threshold is across coupled scopes, so some parameters might be pruned more than others (it’s expected).