Tensor View Layout

Strided parameter views with write-through to original storage.

class sparsekit.view.View(param, shape, stride)[source]

Bases: object

A strided view of a Parameter that duck-types as a Parameter.

Wraps an nn.Parameter with an arbitrary (size, stride) view via torch.as_strided. The .data property returns a live view into the original parameter’s storage – no copy is made, so in-place operations on the data write through to the underlying parameter.

Workflow:

view  = View(param, shape, stride)
block = BlockSpec(view, shape=...)
scope = ScopeSpec(block, shape=...)
# ... pruning / thresholding -- writes go to param directly ...
Parameters:
  • param (Parameter | Tensor) – The underlying Tensor/Parameter.

  • shape (Tuple[int, ...]) – Shape of the strided view.

  • stride (Tuple[int, ...]) – Strides (in elements) for each dimension.

param: Parameter | Tensor
shape: Tuple[int, ...]
stride: Tuple[int, ...]
classmethod from_existing(param)[source]

Wrap a Parameter or pass through an existing View.

Parameters:

param (Tensor | Parameter | View)

Return type:

View

property data: Tensor

Live as_strided view into the parameter’s storage.

property ndim: int

Number of dimensions in the view.

numel()[source]
Return type:

int

property cosize: int

Maximum linear offset reachable by this view + 1.

static block_view_of(t, block_shape, reorder=True, merge=False)[source]

Block-structured view of t via as_strided.

Parameters:
  • t (Tensor) – Tensor of shape (s0, s1, …, sm).

  • block_shape (Tuple[int, ...]) – (b0, b1, …, bm) with si % bi == 0.

  • reorder (bool) – Permute grid dims before block dims.

  • merge (bool) – Flatten block dims into a single trailing dim (implies reorder).

Returns:

(B0, b0, B1, b1, …) interleaved view. reorder=True: (B0, B1, …, b0, b1, …) reordered view. merge=True: (B0, B1, …, b0*b1*…) merged view.

Return type:

reorder=False

static broadcast_block_to_element(block_values, block_shape, fake=False)[source]

Expand a grid-shaped tensor to the full element shape.

Parameters:
  • block_values (Tensor) – (B0, B1, …) grid tensor.

  • block_shape (Tuple[int, ...]) – (b0, b1, …) Block shape.

  • fake (bool) – If True only unsqueeze (for broadcasting against an interleaved view) without expanding.

Returns:

(B0, 1, B1, 1, …) fake=False: (s0, s1, …) with si = Bi * bi.

Return type:

fake=True

apply_multiplier(multiplier, block_shape)[source]

In-place multiply each block of self.data by a grid scalar.

Parameters:
  • multiplier (Tensor) – (B0, B1, …) grid-shaped tensor.

  • block_shape (Tuple[int, ...]) – Block shape.

apply_mask(mask, block_shape)[source]

Zero out blocks of self.data where mask is True.

Parameters:
  • mask (Tensor) – (B0, B1, …) boolean grid tensor.

  • block_shape (Tuple[int, ...]) – Block shape.

linear_offset(idx)[source]

Map multi-dim view indices to linear offsets in param’s storage.

Parameters:

idx (Tensor) – (..., ndim) long tensor of view-space indices.

Returns:

(...,) tensor of flat storage offsets.

Return type:

Tensor

to_param_rc(idx)[source]

Map view-space indices to (row, col) in the 2-D param.

Parameters:

idx (Tensor) – (..., ndim) long tensor of view-space indices.

Returns:

(rows, cols) — each (...,) tensor of param-space indices.

Return type:

Tuple[Tensor, Tensor]