```python:inference/core/workflows/execution_engine/v1/executor/execution_data_manager/step_input_assembler.py
from inference.core.workflows.execution_engine.v1.compiler.entities import StepNode
from inference.core.workflows.execution_engine.v1.executor.execution_data_manager.branching_manager import BranchingManager
from inference.core.workflows.execution_engine.v1.executor.execution_data_manager.dynamic_batches_manager import DynamicBatchIndex
from typing import Any, Dict, List, Optional, Set, Tuple


def construct_mask_for_all_inputs_dimensionalities(
    step_node: StepNode,
    branching_manager: BranchingManager,
) -> Tuple[Any, bool]:
    inputs_dimensionalities = collect_inputs_dimensionalities(step_node=step_node)
    all_dimensionalities = {dim for dim in inputs_dimensionalities.values() if dim > 0}
    batch_masks, non_batch_masks = [], set()
    
    # Cache dictionary and method lookups to avoid repeated attribute access
    masks_dict = branching_manager._masks
    batch_compat_dict = branching_manager._batch_compatibility
    
    for execution_branch in step_node.execution_branches_impacting_inputs:
        if execution_branch not in masks_dict:
            non_batch_masks.add(False)
            continue
        if batch_compat_dict[execution_branch]:
            mask = masks_dict[execution_branch]
            batch_masks.append(mask)
        else:
            mask = masks_dict[execution_branch]
            non_batch_masks.add(mask)
    scalar_mask_contains_false = False in non_batch_masks
    if scalar_mask_contains_false:
        return {
            dimension: set() for dimension in all_dimensionalities
        }, scalar_mask_contains_false
    return (
        get_masks_intersection_for_dimensions(
            batch_masks=batch_masks,
            dimensions=all_dimensionalities,
        ),
        scalar_mask_contains_false,
    )


def collect_inputs_dimensionalities(
    step_node: StepNode,
) -> Dict[str, int]:
    result = {}
    for parameter_name, parameter_specs in step_node.input_data.items():
        if parameter_specs.is_compound_input():
            dimensionalities = [
                nested_definition.get_dimensionality()
                for nested_definition in parameter_specs.iterate_through_definitions()
            ]
            # to help for empty values
            dimensionalities.append(0)
            result[parameter_name] = max(dimensionalities)
        else:
            result[parameter_name] = parameter_specs.get_dimensionality()
    return result


def get_masks_intersection_for_dimensions(
    batch_masks: List[Set[DynamicBatchIndex]],
    dimensions: Set[int],
) -> Dict[int, Optional[Set[DynamicBatchIndex]]]:
    # TODO: verify with unit tests! Claude implementation
    if not batch_masks:
        return {dim: None for dim in dimensions}

    sorted_dims = sorted(dimensions)

    # Group indices by their tuple length, only keeping relevant dimensions
    by_dim: Dict[int, Set[DynamicBatchIndex]] = {dim: set() for dim in sorted_dims}
    for mask in batch_masks:
        for idx in mask:
            by_dim[len(idx)].add(idx)


    if len(sorted_dims) <= 1:
        return {dim: by_dim[dim] for dim in sorted_dims}

    prev_dim = {sorted_dims[i]: sorted_dims[i - 1] for i in range(1, len(sorted_dims))}

    # Bottom-up: mark indices that have at least one descendant
    has_child: Set[DynamicBatchIndex] = set()
    for dim in reversed(sorted_dims):
        for idx in by_dim[dim]:
            if dim == sorted_dims[-1] or idx in has_child:
                parent = idx[:-1]
                if parent:
                    has_child.add(parent)

    # Top-down: keep indices only if full prefix chain exists
    valid: Dict[int, Set[DynamicBatchIndex]] = {dim: set() for dim in sorted_dims}
    for dim in sorted_dims:
        for idx in by_dim[dim]:
            parent = idx[:-1]
            if dim == sorted_dims[0]:
                if idx in has_child:
                    valid[dim].add(idx)
            elif parent in valid[prev_dim[dim]]:
                if dim == sorted_dims[-1] or idx in has_child:
                    valid[dim].add(idx)

    return valid
```