Lightening_Transformer/tile_array_verify.py
2025-06-09 19:11:01 +02:00

65 lines
1.8 KiB
Python

import numpy as np
# Parameters (must match testbench!)
Np = 4
Nm = 4
Nq = 4
def read_matrix(filename):
with open(filename, 'r') as f:
# read all lines
lines = f.readlines()
matrix = []
for line in lines:
# Split the line into numbers (space-separated), Convert to float
row = [float(x) for x in line.strip().split()]
# Add row to the matrix list
matrix.append(row)
# returns a numpy array → this is a 2D matrix
return np.array(matrix)
# Load matrices
m1 = read_matrix("/mnt/f/Thesis/L_T/m1.txt")
m2 = read_matrix("/mnt/f/Thesis/L_T/m2.txt")
output_lines = open("/mnt/f/Thesis/L_T/tile_array_out.txt", 'r').readlines()
# Find where actual data starts (skip header lines)
start_idx = 0
for i, line in enumerate(output_lines):
# The tile_array_out.txt file starts with some header text
if "FINAL RESULT" in line:
# set start_idx to the next line — this is where the matrix rows begin
start_idx = i + 1
break
# Read output result matrix
result = []
# For each line after start_idx
for line in output_lines[start_idx:]:
# Skip empty lines
if line.strip() == "":
continue
# Split line into floats
row = [float(x) for x in line.strip().split()]
# Add row to result list
result.append(row)
# result is a 2D numpy array
result = np.array(result)
# Compute expected result
expected = np.dot(m1, m2.T)
# Comparing results
# checks if all elements of two arrays are close to each other — allowing for small floating-point error (set to 1e-5 here).
if np.allclose(result, expected, atol=1e-5):
# If result matches
print("✅ Result is correct!")
else:
# If not
print("❌ Result mismatch!")
print("Expected:")
print(expected)
print("Got:")
print(result)