Hardware-Aware Model Optimization › Weight Packing and Kernel Co-Design

Michael BrenndoerferDecember 1, 20265 min read
Language AI HandbookAll chapters
Part 43 of 67Small and On-Device Language Models
Chapter 328Hardware-Aware Model Optimization › Weight Packing and Kernel Co-Design
Reading section 4 of 11 · Weight Packing and Kernel Co-Design · 5 minSection 4 of 11 · 5 min

Section 4 of 11 · 5 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

The weight layout needs to match how the kernel reads and unpacks it. The model producer and kernel author agree on such details as group size, alignment, byte order, and where scales live.

When unpacking erases the savings

An INT4 kernel needs to recover the represented values or work directly with their encoded form. A separate unpack pass can read the compressed weights, write a larger temporary tensor, and make the dot product read that temporary again. A fused kernel can keep intermediate values in registers or scratch memory where the hardware and layout allow it.

A packed path can combine these operations in one kernel. A useful sequence is the following.

  1. Arrange packed weights so the kernel loads complete tiles.
  2. Use vector instructions or a specialized arithmetic kernel that can consume the representation.
  3. Apply each scale to the contributions from its own group.

The toy sets separate rates for memory transfer and unpack work. It uses packed weight traffic in both INT4 cases and varies only the assumed unpack operations per weight. The model assumes full overlap and takes the longer of the two components. The helper can add fixed overhead. It omits expanded temporary reads and writes, multiply-add time, scale traffic, and dispatch. Comparing actual kernels would also require measuring their scheduling and any vector instructions.

PythonIn [18]
CollapseExpand
# Toy model of per-token matrix-vector decode time (arbitrary units).
N_WEIGHTS = 100_000_000  # 100M weight elements
BW_BYTES_PER_UNIT = 4e9  # bytes per time unit in our toy machine
UNPACK_OPS_PER_UNIT = 8e9  # unpack ops per time unit


def decode_time(bits, unpack_per_weight, fixed_overhead=0.0):
    bytes_moved = N_WEIGHTS * bits / 8
    unpack_ops = N_WEIGHTS * unpack_per_weight
    t_mem = bytes_moved / BW_BYTES_PER_UNIT
    t_unpack = unpack_ops / UNPACK_OPS_PER_UNIT
    return max(t_mem, t_unpack) + fixed_overhead


# One modeled unpack operation per weight, overlapped with weight traffic.
t_int4_one_op = decode_time(4, unpack_per_weight=1.0)
# Fewer effective unpack operations, modeled as one per eight weights.
t_int4_one_op_per_eight = decode_time(4, unpack_per_weight=1.0 / 8.0)
# INT8 baseline: no unpack, 8 bits per weight.
t_int8_baseline = decode_time(8, unpack_per_weight=0.0)
ConsoleOut [19]
CollapseExpand
INT8, no modeled unpack work: 0.0250 units
INT4, one unpack op/weight: 0.0125 units
INT4, one unpack op/eight weights: 0.0125 units
Modeled time ratio, INT8 to lower-work INT4: 2.00x
Modeled time ratio, higher-work INT4 to lower-work INT4: 1.00x

The two INT4 examples both take 0.0125 time units because moving the packed bytes sets the same floor. The INT8 baseline, with no modeled unpack work, takes 0.025. At two unpack operations per weight, the INT4 curve reaches that baseline. Above two, INT4 takes longer under the model. The break-even depends on the assumed memory and unpack rates.

Sweep the modeled unpack work while holding weight count and bandwidth fixed. The INT8 baseline becomes faster once the INT4 curve crosses it.

PythonIn [20]
CollapseExpand
import numpy as np

unpack_sweep = np.linspace(0.0, 3.0, 121)
int4_time_sweep = [decode_time(4, cost) for cost in unpack_sweep]
int8_time_sweep = np.full_like(unpack_sweep, t_int8_baseline)
VisualizationOut [21]
CollapseExpand
The INT4 timing line starts flat, then rises through the flat INT8 baseline.
In this toy model, INT4 beats INT8 at low unpack work and loses above the crossing. The flat section reflects the time needed to move the packed bytes, and the crossing shows where modeled unpack work erases the byte savings.

The packing layout

Packing 4-bit weights means two weights per byte. If group size is 32, a group occupies 16 bytes plus a 4-byte scale. Device kernels can use several layouts:

  • Packed nibbles put two four-bit values in one byte. A mask and a shift can recover the low and high codes.
  • Lane-oriented packing groups codes according to how the target vector kernel consumes them. The exact interleaving and shuffle cost depend on the layout.
  • Scale placement keeps a group’s metadata easy to find when its weights are loaded. A kernel may store scales beside the packed blocks or in a separate array.

The target runtime decides how this maps to code:

  • WebGPU compute shaders can unpack packed buffer values before multiplication.
  • On Android, use the documented acceleration path for the specific runtime and device. Android 15 deprecated NNAPI, so treat it as a legacy path when testing an existing application.

Then trace one block through the fast path: check which bits encode its weights, where the scale is loaded from, and whether the unpacked values spill to memory. If a format uses signed weights, apply its defined decoding rule before interpreting the four-bit codes as numbers. Tracing also helps spot a mismatch between a model exporter and a kernel. W3C, WGSL Android, NNAPI Migration Guide

Kernel availability as a first-class constraint

Before settling on a format, find out which kernel the runtime actually dispatches. A compatible serialization format can still take a generic path for a particular tensor shape. Check the following.

  • Does the runtime have a fused kernel for W4A16 with this group size?
  • Does the device's NPU support the format at all, or does it fall back to CPU with a slow path?
  • Is the unpack fused, or does the runtime spend cycles on it?
  • Does the runtime support the model's activation functions fused (e.g., SwiGLU) or does it materialize intermediates?

Record the runtime version, variant, actual kernel, prompt size and batch width for each measurement. A smaller representation can be slower if it uses a fallback. Timing the full request also captures transitions between kernels and between devices.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy