sam3-p150 / code /tt_sam3 /utils.py
changh95's picture
Optimized build (2026-10-03): forward_image 443 -> 85 ms, /predict 96.8 ms
b9db84c verified
Raw History Blame Contribute Delete
918 Bytes
# SPDX-FileCopyrightText: (c) 2026 Tenstorrent AI ULC
# SPDX-License-Identifier: Apache-2.0
"""PCC utilities — standalone replacements for tests.ttnn.utils_for_testing."""
import torch
def comp_pcc(a: torch.Tensor, b: torch.Tensor, pcc_threshold: float = 0.99):
"""Compute Pearson correlation coefficient between two tensors.
Returns:
(bool, float): (passes_threshold, pcc_value)
"""
a = a.detach().double().flatten()
b = b.detach().double().flatten()
a = a - a.mean()
b = b - b.mean()
denom = (a.norm() * b.norm()).item() + 1e-12
pcc_val = float((a @ b).item() / denom)
return pcc_val >= pcc_threshold, pcc_val
def assert_with_pcc(a: torch.Tensor, b: torch.Tensor, pcc: float = 0.99):
"""Assert PCC between two tensors meets threshold."""
passes, pcc_val = comp_pcc(a, b, pcc)
assert passes, f"PCC {pcc_val:.6f} < {pcc}"
return passes, pcc_val