Skip to content

Scalar-operand maps: scale, shift, and the ops #5 listed but did not ship #38

Closed

Description

@feitreim

#5's own issue text lists these as wanted, and the map mechanism landed without
them:

  • scale / shift on RegTile and RegVec (multiply/add by a scalar)
  • a free rsqrt(f32) beside exp2_approx / log2_approx
  • RegTile::add_assign

Surfaced again by #6, which found these are the only arithmetic left on any
example kernel's blocked-on list
.

There is a structural reason they were missed, and it decides the fix

trait UnaryOp { fn apply(x: f32) -> f32; } is stateless — a unit struct
with an associated function. So scale(k) cannot be written as a UnaryOp at
all: there is nowhere to put k. That is why the mechanism covered ~30
functions and silently left this family behind; it is a hole in the trait shape,
not an oversight in the op list.

The clean fix reuses the existing BinaryOp set rather than adding a stateful
op trait — treat the scalar as the second operand:

impl<const M: usize, const N: usize, L: FragmentLayout<M, N>> RegTile<M, N, L> {
    pub fn scalar_map<Op: BinaryOp>(self, k: f32) -> Self;
}

Then scale is scalar_map::<Mul>, shift is scalar_map::<Add>, and
division-by-constant, clamping against a bound, and every other
tile-against-scalar op come free from ops that already exist. Same for RegVec
and ColVec.

A free rsqrt(f32) is separately trivial — the Rsqrt op struct already
exists, so it is a convenience wrapper. Worth adding for symmetry with
exp2_approx / log2_approx, which kernels call directly.

Note on add_assign

RegTile::add_assign is in-place and therefore #31's territory — by-value
row_map::<Mul> measured 168 -> 255 registers/thread at 128 columns against the
in-place scale_rows. Land the in-place map variants there and add_assign
falls out; do not add a one-off here that #31 would have to unify.

The same question applies to scalar_map: measure the by-value form at 128
columns
(modal run modal_app.py::regcount, CPU-only). The 32-wide probes are
insensitive — see #31 — though note #6 found the reverse at its shapes, so
measure both rather than trusting either as the sensitive one.

Depends on #5. Related: #31.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compute

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions