```python:inference/core/workflows/execution_engine/v1/compiler/graph_constructor.py
from typing import Dict, List, Set


def establish_step_execution_dimensionality(
    inputs_dimensionalities: Dict[str, Set[int]],
    control_flow_lineage_support: List[str],
    output_dimensionality_offset: int,
) -> int:
    """
    Determine how many batch dimensions (execution slices) a step runs with.

    Used during workflow compilation in denote_data_flow_for_step. The result
    is stored on StepNode.step_execution_dimensionality and consumed at
    execution time to:
    - Drive how many times the step is executed (which batch indices/slices).
    - Align and expand inputs (e.g. auto-batch casting) to match this size.
    - Validate that parameter dimensionalities are compatible (runtime checks
      in step_input_assembler and manager).

    Logic:
    - If no input has non-zero dimensionality but the step is gated by
      control flow (control_flow_lineage_support non-empty), the
      dimensionality is the number of control-flow branches.
    - Otherwise, the minimum non-zero input dimensionality is used; if
      output_dimensionality_offset < 0 (step reduces batch dimension),
      one is subtracted.

    Args:
        inputs_dimensionalities: Per-input sets of dimensionalities (from
            get_inputs_dimensionalities).
        control_flow_lineage_support: Lineage identifiers for control-flow
            branches that gate this step (from establish_batch_oriented_step_lineage).
        output_dimensionality_offset: Block's output dimensionality offset
            (positive = expand, negative = reduce batch dimension).

    Returns:
        The number of batch dimensions (execution slices) for this step.
    """
    step_execution_dimensionality = 0
    # Find the smallest positive dimensionality without building a set.
    min_positive = None
    outer_break = False
    for dimensionalities in inputs_dimensionalities.values():
        for dimensionality in dimensionalities:
            if dimensionality > 0:
                if min_positive is None or dimensionality < min_positive:
                    min_positive = dimensionality
                    # Minimum possible positive dimensionality is 1; we can stop early.
                    if min_positive == 1:
                        outer_break = True
                        break
        if outer_break:
            break

    if min_positive is None and len(control_flow_lineage_support) > 0:
        return len(control_flow_lineage_support)
    if min_positive is not None:
        step_execution_dimensionality = min_positive
        if output_dimensionality_offset < 0:
            step_execution_dimensionality -= 1
    return step_execution_dimensionality
```