# SPDX-FileCopyrightText: © 2025 Tenstorrent USA, Inc. # SPDX-License-Identifier: Apache-2.0 def clamp(value, min_value, max_value): if value < min_value: return min_value elif value > max_value: return max_value return value def is_default_value(values, default): """Check if values match a default, handling None, scalar, and iterable inputs.""" if values is None: return True if isinstance(values, (int, float)): return values == default return all(value == default for value in values) def filter_none(kwargs: dict) -> dict: return {k: v for k, v in kwargs.items() if v is not None} def split_list(lst, n): """Split list into n equal parts.""" chunk_size = len(lst) // n return [list(lst[i * chunk_size : (i + 1) * chunk_size]) for i in range(n)] def is_power_of_2(n): return n > 0 and (n & (n - 1)) == 0 def upper_power_of_2(n: int) -> int: if n <= 1: return 1 return 1 << (n - 1).bit_length() def topk_would_route_to_large_indices(x, k) -> bool: """Return the authoritative C++ route decision for the sampling call shape.""" import ttnn return ttnn._ttnn.operations.reduction._sampling_topk_would_route_to_large_indices(x, k)