File size: 918 Bytes
179df43
 
 
 
 
 
 
 
 
 
 
 
 
 
b9db84c
 
179df43
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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