fp16 kernel

This commit is contained in:
Richard Yan
2024-08-06 02:43:44 -07:00
parent ea4819702e
commit 4fddca3d1a
2 changed files with 51 additions and 10 deletions

View File

@@ -0,0 +1,35 @@
import numpy as np
# Function to generate random fp16 values
def generate_fp16_matrix(size):
return np.random.rand(size, size).astype(np.float16)
# Function to save the matrix to a binary file
def save_matrix_to_bin(file_name, matrix):
matrix.tofile(file_name)
# Function to perform matrix multiplication and truncate to specified size
def truncated_matrix_multiplication(matrix_a, matrix_b, size):
truncated_a = matrix_a.flatten()[:size * size].reshape(size, size)
truncated_b = matrix_b.flatten()[:size * size].reshape(size, size)
result = np.matmul(truncated_a, truncated_b)
return result.astype(np.float16)
# Generate the 512x512 matrices
size = 512
matrix_a = generate_fp16_matrix(size)
matrix_b = generate_fp16_matrix(size)
# Save the operand matrices to binary files
# save_matrix_to_bin("input.a.bin", matrix_a)
# save_matrix_to_bin("input.b.bin", matrix_b)
# Generate and save the reference matrices for 128x128, 256x256, and 512x512 sizes
sizes = [128, 256, 512]
for s in sizes:
ref_matrix = truncated_matrix_multiplication(matrix_a, matrix_b, s)
print(ref_matrix)
# save_matrix_to_bin(f"ref{s}.bin", ref_matrix)
print("All files generated successfully.")