ml_switcheroo.testing.fuzzer.utils

String Parsing and Shape Utilities for the Fuzzer.

This module provides helper functions for parsing complex type strings (e.g., handling nested brackets in generics) and resolving symbolic dimensions for tensor shapes.

Functions

is_pipe_top_level(→ bool)

Checks if a pipe | exists outside of brackets [].

split_outside_brackets(→ List[str])

Splits a string by commas, respecting nested brackets.

resolve_symbolic_shape(→ Tuple[int, Ellipsis])

Parses dimension strings like "'B', 32" or "N, M" into integer tuples.

adjust_shape_rank(→ Tuple[int, Ellipsis])

Adjusts a shape tuple to match a required rank by padding or truncation.

Module Contents

ml_switcheroo.testing.fuzzer.utils.is_pipe_top_level(text: str) bool

Checks if a pipe | exists outside of brackets [].

Used to detect Union types (A | B) in Python 3.10+ syntax without getting confused by nested types like List[int | str].

Parameters:

text (str) – The type hint string to analyze.

Returns:

True if a top-level pipe is found.

Return type:

bool

ml_switcheroo.testing.fuzzer.utils.split_outside_brackets(text: str) List[str]

Splits a string by commas, respecting nested brackets.

Used to parse generic arguments (e.g., Tuple[int, List[int]] -> [‘int’, ‘List[int]’]).

Parameters:

text (str) – The content inside brackets.

Returns:

Split parts.

Return type:

List[str]

ml_switcheroo.testing.fuzzer.utils.resolve_symbolic_shape(dims_str: str, symbol_map: Dict[str, int]) Tuple[int, Ellipsis]

Parses dimension strings like “‘B’, 32” or “N, M” into integer tuples.

Resolves symbolic names using the provided symbol_map. If a symbol is encountered for the first time, a random size is assigned and stored in the map to ensure consistency across arguments.

Parameters:
  • dims_str (str) – The dimensions definition string (e.g. “‘N’, 32”).

  • symbol_map (Dict[str, int]) – Context for resolving symbols like ‘N’.

Returns:

The concrete shape tuple.

Return type:

Tuple[int, …]

ml_switcheroo.testing.fuzzer.utils.adjust_shape_rank(shape: Tuple[int, Ellipsis], required_rank: int) Tuple[int, Ellipsis]

Adjusts a shape tuple to match a required rank by padding or truncation.

Parameters:
  • shape (Tuple[int, ...]) – The base shape.

  • required_rank (int) – The target number of dimensions.

Returns:

The adjusted shape.

Return type:

Tuple[int, …]