tensor: Fix correctness script
This commit is contained in:
@@ -1,16 +1,11 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
A_array = np.zeros((16, 8))
|
|
||||||
B_array = np.zeros((8, 16))
|
|
||||||
C_array = np.zeros((16, 16))
|
|
||||||
|
|
||||||
file = input("simulator output filename: ")
|
|
||||||
|
|
||||||
def hex2float(float_hex_str):
|
def hex2float(float_hex_str):
|
||||||
# print(float_hex_str.strip())
|
# print(float_hex_str.strip())
|
||||||
return struct.unpack(">f",struct.pack(">i",int(float_hex_str,16)))[0]
|
return struct.unpack(">f",struct.pack(">i",int(float_hex_str,16)))[0]
|
||||||
|
|
||||||
|
|
||||||
def C_index(threadgroup, thread, register):
|
def C_index(threadgroup, thread, register):
|
||||||
"""
|
"""
|
||||||
col = ((tg % 4) / 2) * 8;
|
col = ((tg % 4) / 2) * 8;
|
||||||
@@ -43,65 +38,75 @@ def C_index(threadgroup, thread, register):
|
|||||||
return (row, col)
|
return (row, col)
|
||||||
|
|
||||||
|
|
||||||
with open(file) as f:
|
def check_sim_output():
|
||||||
for line in f.readlines():
|
file = input("simulator output filename: ")
|
||||||
line = line.strip()
|
|
||||||
if "warp" in line:
|
|
||||||
a, b, c = line.split(',')
|
|
||||||
_, a = a.split(' ')
|
|
||||||
_, b = b.strip().split(' ')
|
|
||||||
c, d = c.strip().split(':')
|
|
||||||
_, c = c.split(' ')
|
|
||||||
warp = int(a)
|
|
||||||
thread = int(b)
|
|
||||||
register = int(c)
|
|
||||||
value = d.strip()
|
|
||||||
|
|
||||||
if warp != 0:
|
A_array = np.zeros((16, 8))
|
||||||
continue
|
B_array = np.zeros((8, 16))
|
||||||
if not (32 <= register < 32+24):
|
C_array = np.zeros((16, 16))
|
||||||
continue
|
|
||||||
|
|
||||||
register = register - 32
|
with open(file) as f:
|
||||||
|
for line in f.readlines():
|
||||||
# threadgroups 0, 4, 1, 5 have all elements of A
|
line = line.strip()
|
||||||
threadgroup = thread // 4
|
if "warp" in line:
|
||||||
if threadgroup in [0, 4, 1, 5]:
|
a, b, c = line.split(',')
|
||||||
row = [0, 4, 1, 5].index(threadgroup) * 4 + thread % 4
|
_, a = a.split(' ')
|
||||||
if 0 <= register < 8:
|
_, b = b.strip().split(' ')
|
||||||
A_array[row, register] = hex2float(value)
|
c, d = c.strip().split(':')
|
||||||
|
_, c = c.split(' ')
|
||||||
if threadgroup in [0, 4, 2, 6]:
|
warp = int(a)
|
||||||
col = [0, 4, 2, 6].index(threadgroup) * 4 + thread % 4
|
thread = int(b)
|
||||||
if 8 <= register < 16:
|
register = int(c)
|
||||||
B_array[register-8, col] = hex2float(value)
|
value = d.strip()
|
||||||
|
|
||||||
if 16 <= register < 24:
|
if warp != 0:
|
||||||
# print(value)
|
continue
|
||||||
C_array[C_index(threadgroup, thread, register)] = hex2float(value)
|
if not (32 <= register < 32+24):
|
||||||
|
continue
|
||||||
|
|
||||||
expected = np.load("abc.npz")
|
register = register - 32
|
||||||
# expected_A = expected['A_array']
|
|
||||||
# expected_B = expected['B_array']
|
|
||||||
# expected_C = expected['C_array']
|
|
||||||
expected_A = expected['A_array'][0:8, 0:8]
|
|
||||||
expected_B = expected['B_array'][0:8, 0:8]
|
|
||||||
expected_C = expected['C_array'][0:8, 0:8]
|
|
||||||
expected_C = expected_C + expected_A @ expected_B
|
|
||||||
print('expected A:')
|
|
||||||
print(expected_A)
|
|
||||||
print('expected B:')
|
|
||||||
print(expected_B)
|
|
||||||
print('expected C:')
|
|
||||||
print(expected_C[0:8, 0:8])
|
|
||||||
print('got C:')
|
|
||||||
print(C_array[0:8, 0:8])
|
|
||||||
print('diff C:')
|
|
||||||
print(expected_C[0:8, 0:8] - C_array[0:8, 0:8])
|
|
||||||
|
|
||||||
expected_C.astype('float32').tofile("c_expected.bin")
|
# threadgroups 0, 4, 1, 5 have all elements of A
|
||||||
|
threadgroup = thread // 4
|
||||||
|
if threadgroup in [0, 4, 1, 5]:
|
||||||
|
row = [0, 4, 1, 5].index(threadgroup) * 4 + thread % 4
|
||||||
|
if 0 <= register < 8:
|
||||||
|
A_array[row, register] = hex2float(value)
|
||||||
|
|
||||||
assert np.allclose(expected_A, A_array)
|
if threadgroup in [0, 4, 2, 6]:
|
||||||
assert np.allclose(expected_B, B_array)
|
col = [0, 4, 2, 6].index(threadgroup) * 4 + thread % 4
|
||||||
assert np.allclose(expected_C, C_array)
|
if 8 <= register < 16:
|
||||||
|
B_array[register-8, col] = hex2float(value)
|
||||||
|
|
||||||
|
if 16 <= register < 24:
|
||||||
|
# print(value)
|
||||||
|
C_array[C_index(threadgroup, thread, register)] = hex2float(value)
|
||||||
|
|
||||||
|
return [A_array, B_array, C_array]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
expected = np.load("abc.npz")
|
||||||
|
# expected_A = expected['A_array']
|
||||||
|
# expected_B = expected['B_array']
|
||||||
|
# expected_C = expected['C_array']
|
||||||
|
expected_A = expected['A_array'][0:8, 0:8]
|
||||||
|
expected_B = expected['B_array'][0:8, 0:8]
|
||||||
|
expected_C = expected['C_array'][0:8, 0:8]
|
||||||
|
expected_C = expected_C + expected_A @ expected_B
|
||||||
|
print('expected A:')
|
||||||
|
print(expected_A)
|
||||||
|
print('expected B:')
|
||||||
|
print(expected_B)
|
||||||
|
print('expected C:')
|
||||||
|
print(expected_C[0:8, 0:8])
|
||||||
|
expected_C.astype('float32').tofile("c_expected.bin")
|
||||||
|
|
||||||
|
[got_A, got_B, got_C] = check_sim_output()
|
||||||
|
print('got C:')
|
||||||
|
print(C_array[0:8, 0:8])
|
||||||
|
print('diff C:')
|
||||||
|
print(expected_C[0:8, 0:8] - C_array[0:8, 0:8])
|
||||||
|
assert np.allclose(expected_A, got_A)
|
||||||
|
assert np.allclose(expected_B, got_B)
|
||||||
|
assert np.allclose(expected_C, got_C)
|
||||||
|
|||||||
Reference in New Issue
Block a user