﻿# StableHLO dynamic broadcast: runtime shape computation must accompany dynamic types

Represent dynamic StableHLO computation with runtime shape operands and explicit constraints, rather than making a static program dynamic by replacing type extents alone.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-stablehlo-dynamic-shape-runtime-contract/
Language: en

## Replacing a type extent does not generalize every operation

A developer exports a function that adds a constant to a variable-length input. Editing the visible result type to contain a dynamic dimension does not automatically replace a statically sized broadcast with a runtime-sized computation. The program must still construct the appropriate result for each accepted input shape. Trace the shape-producing operations as well as the data-producing operations, because a dynamic annotation cannot override a contradictory static operation inside the body.

## Dynamic programs carry executable shape information

The [dynamism guide](https://openxla.org/stablehlo/dynamism) explains different forms of dynamism and an example that broadcasts a constant to match an input. Distinguish dimensions determined by input shapes from dimensions depending on tensor contents. The former and latter impose different requirements on compilation and allocation. State which form the exported function actually uses rather than promising support for arbitrary changing dimensions based solely on a question mark in its type.

## Dynamic broadcast has explicit operands and constraints

The [StableHLO specification](https://openxla.org/stablehlo/spec) defines `dynamic_broadcast_in_dim` with output dimension data and a mapping of input axes. Review the mapping and output shape computation together. The [broadcasting axis guide](/journal/broadcasting-attention-mask-axes/) explains why a compatible rank is insufficient to establish intended semantics. A program can have a dynamic-looking type and still broadcast along the wrong axis or compute an output extent from the wrong input dimension.

## Specialization passes can remove resolved dynamism

The [StableHLO passes reference](https://openxla.org/stablehlo/generated/stablehlo_passes) describes canonicalization of dynamic operations when their shape information becomes constant. That transformation has a precondition; it does not justify substituting one observed request’s dimensions into a generally dynamic model. Preserve the distinction between a deliberate specialized artifact and a program expected to handle a range of shapes. Record which constraints were resolved and which remain requirements on the caller or runtime.

## Validate shape relationships rather than one sample

Exercise inputs that change the relevant extent while preserving the declared relationships. Compare the output shape and numerical result, and verify rejection of unsupported combinations at the appropriate boundary. The [tensor descriptor guide](/journal/learn-tensor-memory-descriptor/) supplies background for interpreting the resulting arrays. An effective review follows how runtime shape data reaches each dynamic operation, instead of counting dynamic type markers and assuming that number measures the program’s actual flexibility.
