|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys |
|
|
import os |
|
|
from importlib import __import__ |
|
|
import time |
|
|
import numpy as np |
|
|
|
|
|
BENCHMARK = 32 |
|
|
|
|
|
|
|
|
def verify_tensor_decomposition( |
|
|
decomposition: tuple[np.ndarray, np.ndarray, np.ndarray], n: int, m: int, p: int, rank: int |
|
|
): |
|
|
"""Verifies the correctness of the tensor decomposition.""" |
|
|
|
|
|
|
|
|
if not all(isinstance(arr, np.ndarray) for arr in decomposition) or not decomposition: |
|
|
raise ValueError("Decomposition must be a tuple of NumPy arrays.") |
|
|
if any(arr.size == 0 for arr in decomposition): |
|
|
print("Warning: One or more decomposition arrays are empty. Verification skipped.") |
|
|
return |
|
|
|
|
|
|
|
|
factor_matrix_1, factor_matrix_2, factor_matrix_3 = decomposition |
|
|
if factor_matrix_1.shape != (n * m, rank): |
|
|
raise ValueError( |
|
|
f"Expected shape of factor matrix 1 is {(n * m, rank)}. Actual shape is {factor_matrix_1.shape}." |
|
|
) |
|
|
if factor_matrix_2.shape != (m * p, rank): |
|
|
raise ValueError( |
|
|
f"Expected shape of factor matrix 2 is {(m * p, rank)}. Actual shape is {factor_matrix_2.shape}." |
|
|
) |
|
|
if factor_matrix_3.shape != (n * p, rank): |
|
|
raise ValueError( |
|
|
f"Expected shape of factor matrix 3 is {(n * p, rank)}. Actual shape is {factor_matrix_3.shape}." |
|
|
) |
|
|
|
|
|
|
|
|
matmul_tensor = np.zeros((n * m, m * p, n * p), dtype=np.float32) |
|
|
for i in range(n): |
|
|
for j in range(m): |
|
|
for k in range(p): |
|
|
|
|
|
matmul_tensor[i * m + j, j * p + k, k * n + i] = 1 |
|
|
|
|
|
|
|
|
constructed_tensor = np.einsum("ir,jr,kr -> ijk", *decomposition) |
|
|
|
|
|
|
|
|
if not np.array_equal(constructed_tensor, matmul_tensor): |
|
|
|
|
|
diff = np.max(np.abs(constructed_tensor - matmul_tensor)) |
|
|
raise ValueError( |
|
|
f"Tensor constructed by decomposition does not exactly match the target tensor. Maximum difference is {diff:.6e}." |
|
|
) |
|
|
|
|
|
|
|
|
def evaluate(program_path: str): |
|
|
try: |
|
|
abs_program_path = os.path.abspath(program_path) |
|
|
program_dir = os.path.dirname(abs_program_path) |
|
|
module_name = os.path.splitext(os.path.basename(program_path))[0] |
|
|
|
|
|
try: |
|
|
sys.path.insert(0, program_dir) |
|
|
program = __import__(module_name) |
|
|
start_time = time.time() |
|
|
decomposition, n, m, p, loss, rank = program.run() |
|
|
end_time = time.time() |
|
|
eval_time = end_time - start_time |
|
|
except Exception as err: |
|
|
raise err |
|
|
finally: |
|
|
if program_dir in sys.path: |
|
|
sys.path.remove(program_dir) |
|
|
|
|
|
verify_tensor_decomposition(decomposition, n, m, p, rank) |
|
|
|
|
|
success_threshold = 1e-6 |
|
|
if loss > success_threshold: |
|
|
print( |
|
|
f"\nWarning: Final loss {loss:.2e} is above the success threshold of {success_threshold:.2e}." |
|
|
) |
|
|
|
|
|
inverse_rank = BENCHMARK / rank |
|
|
|
|
|
return { |
|
|
"combined_score": inverse_rank, |
|
|
"loss": loss, |
|
|
"rank": rank, |
|
|
"eval_time": float(eval_time), |
|
|
} |
|
|
except Exception as e: |
|
|
return {"combined_score": 0.0, "error": str(e)} |
|
|
|