Skip to content

Commit

Permalink
[software] Update data generation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mbertuletti committed Oct 2, 2023
1 parent 58662e1 commit dc4c86e
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 167 deletions.
2 changes: 1 addition & 1 deletion software/runtime/data/data_cfft_radix4_f16.h.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
i = 0
out += '\n'
for a in array:
out += '{}f, '.format(a)
out += '(__fp16){:0.5}f, '.format(a)
i += 1
if i % 8 == 0:
out += '\n'
Expand Down
4 changes: 2 additions & 2 deletions software/runtime/data/data_cfft_radix4_f16.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def main():
src = np.random.rand(Len) + 1.j * np.random.rand(Len)
dst = np.fft.fft(src)

twi_RI = np.zeros(2 * int(3 * Len / 4))
for i in range(int(3 * Len / 4)):
twi_RI = np.zeros(int(2 * 3 * Len / 4), np.float16)
for i in range(0, int(3 * Len / 4)):
twi_RI[2 * i] = np.cos(i * 2 * np.pi / Len).astype(np.float16)
twi_RI[2 * i + 1] = np.sin(i * 2 * np.pi / Len).astype(np.float16)

Expand Down
10 changes: 7 additions & 3 deletions software/runtime/data/data_chest_f16.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def gen_data_header_file(
with file.open('w') as f:
f.write(template.render(**kwargs))


def main():

parser = argparse.ArgumentParser(description='Generate data for kernels')
Expand Down Expand Up @@ -99,10 +100,13 @@ def main():
Hest = pilot_rx[:, np.newaxis] / pilot_tx[np.newaxis, :]

# Interleaved real and imaginary parts
pilot_tx = np.column_stack((pilot_tx.real, pilot_tx.imag)).astype(np.float16).flatten()
pilot_rx = np.column_stack((pilot_rx.real, pilot_rx.imag)).astype(np.float16).flatten()
pilot_tx = np.column_stack(
(pilot_tx.real, pilot_tx.imag)).astype(np.float16).flatten()
pilot_rx = np.column_stack(
(pilot_rx.real, pilot_rx.imag)).astype(np.float16).flatten()
Hest = Hest.flatten()
Hest = np.column_stack((Hest.real, Hest.imag)).astype(np.float16).flatten()
Hest = np.column_stack((Hest.real, Hest.imag)
).astype(np.float16).flatten()

# Output vectors
vector_pilot_tx.append(pilot_tx)
Expand Down
9 changes: 5 additions & 4 deletions software/runtime/data/data_cholesky_f16.h.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
i = 0
out += '\n'
for a in array:
out += '{}f, '.format(a)
out += '(__fp16){:0.5f}f, '.format(a)
i += 1
if i % 8 == 0:
out += '\n'
out = out[:-2] + '}'
return out
%> \

#define N (${N})
#define dim_N (${n_matrix})
#define N_SAMPLES (${n_samples})

__fp16 In_G[${2 * N * N}] = ${array_to_cstr(G)};
__fp16 In_G[2 * ${n_samples * n_matrix * n_matrix}] = ${array_to_cstr(G)};

__fp16 Out_L[${2 * N * N}] = ${array_to_cstr(L)};
__fp16 Out_L[2 * ${n_samples * n_matrix * n_matrix}] = ${array_to_cstr(L)};
70 changes: 41 additions & 29 deletions software/runtime/data/data_cholesky_f16.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ def gen_data_header_file(outdir: pathlib.Path.cwd(), tpl: pathlib.Path.cwd(), **
with file.open('w') as f:
f.write(template.render(**kwargs))


def main():

parser = argparse.ArgumentParser(description='Generate data for kernels')
parser.add_argument(
"-o",
"--outdir",
type=pathlib.Path,
default=pathlib.Path( __file__ ).parent.absolute(),
default=pathlib.Path(__file__).parent.absolute(),
required=False,
help='Select out directory of generated data files'
)
Expand All @@ -44,7 +45,8 @@ def main():
"--tpl",
type=pathlib.Path,
required=False,
default=pathlib.Path( __file__ ).parent.absolute() / "data_cholesky_f16.h.tpl",
default=pathlib.Path(__file__).parent.absolute() /
"data_cholesky_f16.h.tpl",
help='Path to mako template'
)
parser.add_argument(
Expand All @@ -61,36 +63,46 @@ def main():
default=4,
help='Matrix dimension'
)
parser.add_argument(
"-s",
"--num_samples",
type=int,
required=False,
default=256,
help='Number samples'
)

args = parser.parse_args()
N=args.dimension

# Create hermitian matrix
H = np.random.rand(N, N) + 1.j * np.random.rand(N, N)
# Matrix to be inverted
G = H*(np.asmatrix(H).H)
# Cholesky decomposition
L = np.linalg.cholesky(G)

G = np.reshape(G, (N*N, -1), order='C')
L = np.reshape(L, (N*N, -1), order='C')
G_RI = np.zeros(2*N*N)
L_RI = np.zeros(2*N*N)

for i in range(N*N):
G_RI[2*i] = G[i].real
G_RI[2*i+1] = G[i].imag
L_RI[2*i] = L[i].real
L_RI[2*i+1] = L[i].imag

G_RI = G_RI.astype(np.float16)
L_RI = L_RI.astype(np.float16)
print("Input matrix in (Re, Im) format:\n", G_RI)
print("Output matrix in (Re, Im) format:\n", L_RI)


kwargs = {'name': 'data_cholesky_f16', 'G': G_RI, 'L' : L_RI, 'N' : N}
n_matrix = args.dimension
n_samples = args.num_samples

vector_G = []
vector_L = []
for k in range(n_samples):
# Create hermitian matrix
H = np.random.rand(n_matrix, n_matrix) + 1.j * \
np.random.rand(n_matrix, n_matrix)
# Matrix to be inverted
# H_H = np.asmatrix(H).H
G = np.matmul(H, np.asmatrix(H).H)
# Cholesky decomposition
L = np.linalg.cholesky(G)
# Reshape
G = np.reshape(np.asarray(G), (n_matrix * n_matrix), order='C')
L = np.reshape(np.asarray(L), (n_matrix * n_matrix), order='C')
G = np.column_stack((G.real, G.imag)).astype(np.float16).flatten()
L = np.column_stack((L.real, L.imag)).astype(np.float16).flatten()
# Output vectors
vector_G.append(G)
vector_L.append(L)

vector_G = np.concatenate(vector_G, axis=0)
vector_L = np.concatenate(vector_L, axis=0)

kwargs = {'name': 'data_cholesky_f16', 'G': vector_G,
'L': vector_L, 'n_matrix': n_matrix, 'n_samples': n_samples}
gen_data_header_file(args.outdir, args.tpl, **kwargs)


if __name__ == "__main__":
main()
6 changes: 3 additions & 3 deletions software/runtime/data/data_cmatmul_f16.h.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
return out
%> \

#define matrix_M (${matrix_M})
#define matrix_N (${matrix_N})
#define matrix_P (${matrix_P})
#define dim_M (${matrix_M})
#define dim_N (${matrix_N})
#define dim_P (${matrix_P})

__fp16 A[${2 * matrix_M * matrix_N}] = ${array_to_cstr(A)};

Expand Down
7 changes: 5 additions & 2 deletions software/runtime/data/data_cmatmul_f16.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ def main():
matrix_P = args.dim_p

# Create sparse matrix
A = np.random.rand(matrix_M, matrix_N) + 1j * np.random.rand(matrix_M, matrix_N)
B = np.random.rand(matrix_N, matrix_P) + 1j * np.random.rand(matrix_N, matrix_P)
A = np.random.rand(matrix_M, matrix_N) + 1j * \
np.random.rand(matrix_M, matrix_N)
B = np.random.rand(matrix_N, matrix_P) + 1j * \
np.random.rand(matrix_N, matrix_P)
C = np.matmul(A, B)

A = np.reshape(A, (matrix_M * matrix_N), order='C')
B = np.reshape(B, (matrix_N * matrix_P), order='C')
C = np.reshape(C, (matrix_M * matrix_P), order='C')
print(A)

A = np.column_stack((A.real, A.imag)).astype(np.float16).flatten()
B = np.column_stack((B.real, B.imag)).astype(np.float16).flatten()
Expand Down
111 changes: 44 additions & 67 deletions software/runtime/data/data_mimo_mmse_f16.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ def gen_data_header_file(outdir: pathlib.Path.cwd(), tpl: pathlib.Path.cwd(), **
with file.open('w') as f:
f.write(template.render(**kwargs))


def gen_input_data(N_rx, N_tx):
# Create channel matrix
H = np.random.rand(N_rx, N_tx).astype(np.float16) + 1.j * np.random.rand(N_rx, N_tx).astype(np.float16)
H = np.random.rand(N_rx, N_tx).astype(np.float16) + 1.j * \
np.random.rand(N_rx, N_tx).astype(np.float16)
# Create input vector
b = np.random.rand(N_rx).astype(np.float16) + 1.j * np.random.rand(N_rx).astype(np.float16)
b = np.random.rand(N_rx).astype(np.float16) + 1.j * \
np.random.rand(N_rx).astype(np.float16)
# Generate noise variance
sigma = np.diag(np.random.rand(N_tx, N_tx).astype(np.float16))

Expand All @@ -48,52 +51,20 @@ def gen_input_data(N_rx, N_tx):
y = solve_triangular(L, s, lower=True)
x = solve_triangular(np.asmatrix(L).H, y)

H = np.reshape(H, (N_tx*N_rx, -1), order='C')
G = np.reshape(G, (N_tx*N_tx, -1), order='C')
L = np.reshape(L, (N_tx*N_tx, -1), order='C')
H_RI = np.zeros(2*N_tx*N_rx)
G_RI = np.zeros(2*N_tx*N_tx)
L_RI = np.zeros(2*N_tx*N_tx)
b_RI = np.zeros(2*N_rx)
s_RI = np.zeros(2*N_tx)
x_RI = np.zeros(2*N_tx)
y_RI = np.zeros(2*N_tx)

for i in range(N_tx*N_rx):
H_RI[2*i] = H[i].real
H_RI[2*i+1] = H[i].imag
for i in range(N_tx*N_tx):
G_RI[2*i] = G[i].real
G_RI[2*i+1] = G[i].imag
L_RI[2*i] = L[i].real
L_RI[2*i+1] = L[i].imag

for i in range(N_rx):
b_RI[2*i] = b[i].real
b_RI[2*i+1] = b[i].imag
for i in range(N_tx):
s_RI[2*i] = s[i].real
s_RI[2*i+1] = s[i].imag
x_RI[2*i] = x[i].real
x_RI[2*i+1] = x[i].imag
y_RI[2*i] = y[i].real
y_RI[2*i+1] = y[i].imag

sigma = sigma.astype(np.float16)
H_RI = H_RI.astype(np.float16)
G_RI = G_RI.astype(np.float16)
L_RI = L_RI.astype(np.float16)
b_RI = b_RI.astype(np.float16)
x_RI = x_RI.astype(np.float16)
y_RI = y_RI.astype(np.float16)
s_RI = s_RI.astype(np.float16)
# print("Channel matrix in (Re, Im) format:\n", H_RI)
# print("Hermitian matrix in (Re, Im) format:\n", G_RI)
# print("Cholesky dec. in (Re, Im) format:\n", L_RI)
# print("Input vector in (Re, Im) format:\n", b_RI)
# print("Output vector in (Re, Im) format:\n", x_RI)

return sigma, H_RI, G_RI, b_RI, x_RI
H = np.reshape(np.asarray(H), (N_tx*N_rx), order='C')
G = np.reshape(np.asarray(G), (N_tx*N_tx), order='C')
L = np.reshape(np.asarray(L), (N_tx*N_tx), order='C')
H = np.column_stack((H.real, H.imag)).astype(np.float16).flatten()
G = np.column_stack((G.real, G.imag)).astype(np.float16).flatten()
L = np.column_stack((L.real, L.imag)).astype(np.float16).flatten()

b = np.column_stack((b.real, b.imag)).astype(np.float16).flatten()
s = np.column_stack((s.real, s.imag)).astype(np.float16).flatten()
x = np.column_stack((x.real, x.imag)).astype(np.float16).flatten()
y = np.column_stack((y.real, y.imag)).astype(np.float16).flatten()

return sigma, H, G, b, x


def main():

Expand All @@ -102,7 +73,7 @@ def main():
"-o",
"--outdir",
type=pathlib.Path,
default=pathlib.Path( __file__ ).parent.absolute(),
default=pathlib.Path(__file__).parent.absolute(),
required=False,
help='Select out directory of generated data files'
)
Expand All @@ -111,7 +82,8 @@ def main():
"--tpl",
type=pathlib.Path,
required=False,
default=pathlib.Path( __file__ ).parent.absolute() / "data_mimo_mmse_f16.h.tpl",
default=pathlib.Path(__file__).parent.absolute() /
"data_mimo_mmse_f16.h.tpl",
help='Path to mako template'
)
parser.add_argument(
Expand Down Expand Up @@ -146,32 +118,37 @@ def main():
)

args = parser.parse_args()
N_tx=args.transmitters
N_rx=args.receivers
itr=args.iterations
N_tx = args.transmitters
N_rx = args.receivers
itr = args.iterations

sigma = np.zeros([itr, N_tx])
H_RI = np.zeros([itr, 2*N_tx*N_rx])
G_RI = np.zeros([itr, 2*N_tx*N_tx])
b_RI = np.zeros([itr, 2*N_rx])
x_RI = np.zeros([itr, 2*N_tx])
for k in range(itr):
sigma[k,:], H_RI[k,:], G_RI[k,:], b_RI[k,:], x_RI[k,:] = gen_input_data(N_rx, N_tx)
sigma[k, :], H_RI[k, :], G_RI[k, :], b_RI[k,
:], x_RI[k, :] = gen_input_data(N_rx, N_tx)

sigma = np.reshape(sigma, (N_tx*itr)).astype(np.float16)
H_RI = np.reshape(H_RI, (2*N_rx*N_tx*itr)).astype(np.float16)
G_RI = np.reshape(G_RI, (2*N_tx*N_tx*itr)).astype(np.float16)
b_RI = np.reshape(b_RI, (2*N_rx*itr)).astype(np.float16)
x_RI = np.reshape(x_RI, (2*N_tx*itr)).astype(np.float16)

kwargs = {'name': 'data_mimo_mmse_f16',
'H': H_RI,
'G': G_RI,
'sigma': sigma,
'b': b_RI,
'x': x_RI,
'N_tx': N_tx,
'N_rx': N_rx,
'N_itr': itr}

sigma = np.reshape(sigma, (N_tx*itr))
H_RI = np.reshape(H_RI, (2*N_rx*N_tx*itr))
G_RI = np.reshape(G_RI, (2*N_tx*N_tx*itr))
b_RI = np.reshape(b_RI, (2*N_rx*itr))
x_RI = np.reshape(x_RI, (2*N_tx*itr))

sigma = sigma.astype(np.float16)
H_RI = H_RI.astype(np.float16)
G_RI = G_RI.astype(np.float16)
b_RI = b_RI.astype(np.float16)
x_RI = x_RI.astype(np.float16)

kwargs = {'name': 'data_mimo_mmse_f16', 'H': H_RI, 'G': G_RI, 'sigma': sigma, 'b' : b_RI, 'x' : x_RI, 'N_tx' : N_tx, 'N_rx' : N_rx, 'N_itr' : itr}
gen_data_header_file(args.outdir, args.tpl, **kwargs)


if __name__ == "__main__":
main()
Loading

0 comments on commit dc4c86e

Please sign in to comment.