﻿# CUDA half2: lane meaning, aligned storage, and an odd-length tail

Use CUDA packed half arithmetic without confusing two lanes with a wider scalar, and separate lane packing, address alignment, and tail handling.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cuda-half2-lanes-alignment-odd-tail/
Language: en

## A pair is not a larger floating-point number

A pointwise activation can process neighboring half-precision elements in pairs. CUDA `__half2` represents paired values, so the optimization must preserve which logical element occupies each lane. Reinterpreting the pair as a higher-precision scalar would describe a different numerical object.

The [type reference](https://docs.nvidia.com/cuda/cuda-math-api/cuda_math_api/struct____half2.html) defines the packed CUDA type. It is useful to write the element-to-lane mapping before changing a scalar kernel, especially when the input is a sliced tensor.

## Construct lane order deliberately

The [conversion and movement reference](https://docs.nvidia.com/cuda/cuda-math-api/cuda_math_api/group__CUDA__MATH____HALF__MISC.html) specifies that `__halves2half2` places its first argument in the low half and its second in the high half. Extraction operations provide the corresponding lane interpretation.

A lane swap can remain invisible for symmetric operations but become visible when coefficients differ between neighboring features. Include an asymmetric input pattern when testing the conversion path. Packing an identical scalar into both lanes does not validate order.

## Distinguish arithmetic contracts

The [packed arithmetic reference](https://docs.nvidia.com/cuda/cuda-math-api/cuda_math_api/group__CUDA__MATH____HALF2__ARITHMETIC.html) documents operations including `__hadd2` and `__hfma2`. The latter is fused multiply-add; it should not automatically be treated as numerically identical to separately rounded multiplication and addition.

Select the intended rounding and saturation behavior from the actual intrinsic contract. The [mixed-precision guide](/journal/learn-mixed-precision-accumulation/) explains why storage precision alone does not describe the complete numerical computation.

## Address validity belongs to the memory view

Casting a pointer does not repair an unsuitable address alignment or extend the underlying allocation. A tensor slice can change the starting address even when its original allocation was well aligned. Validate the packed view against the type and access requirements of the actual implementation.

For an odd logical length, the final scalar has no valid partner inside that domain. Handle it separately or use an explicitly provisioned padded layout with correct semantics. Do not let a convenient vector width silently enlarge the consumer range.

## Compare paths that expose different mistakes

Test lane-dependent coefficients, sliced starts, and odd lengths separately. Compare fused arithmetic against an appropriate numerical reference rather than demanding an unjustified equality with a differently rounded scalar expression.

The [tensor-padding article](/journal/learn-tensor-kernel-padding/) helps distinguish logical validity from spare storage. Once those contracts hold, inspect the generated implementation and measure the real workload; packed syntax alone does not establish a universal throughput improvement.
