Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matmul2x2 #289

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions benchmarks/float/matmul2x2.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@main {
v0: float = const 8;
a11: float = id v0;
v1: float = const 3;
a12: float = id v1;
v2: float = const 2;
a21: float = id v2;
v3: float = const 4;
a22: float = id v3;
v4: float = const 2;
b11: float = id v4;
v5: float = const 7;
b12: float = id v5;
v6: float = const 4;
b21: float = id v6;
v7: float = const 5;
b22: float = id v7;
v8: float = id a11;
v9: float = id b11;
v10: float = fmul v8 v9;
c111: float = id v10;
v11: float = id a12;
v12: float = id b21;
v13: float = fmul v11 v12;
c112: float = id v13;
v14: float = id c111;
v15: float = id c112;
v16: float = fadd v14 v15;
c11: float = id v16;
v17: float = id a11;
v18: float = id b12;
v19: float = fmul v17 v18;
c121: float = id v19;
v20: float = id a12;
v21: float = id b22;
v22: float = fmul v20 v21;
c122: float = id v22;
v23: float = id c121;
v24: float = id c122;
v25: float = fadd v23 v24;
c12: float = id v25;
v26: float = id a21;
v27: float = id b11;
v28: float = fmul v26 v27;
c211: float = id v28;
v29: float = id a22;
v30: float = id b21;
v31: float = fmul v29 v30;
c212: float = id v31;
v32: float = id c211;
v33: float = id c212;
v34: float = fadd v32 v33;
c21: float = id v34;
v35: float = id a21;
v36: float = id b12;
v37: float = fmul v35 v36;
c221: float = id v37;
v38: float = id a22;
v39: float = id b22;
v40: float = fmul v38 v39;
c222: float = id v40;
v41: float = id c221;
v42: float = id c222;
v43: float = fadd v41 v42;
c22: float = id v43;
v44: float = id c11;
print v44;
v45: int = const 0;
v46: float = id c12;
print v46;
v47: int = const 0;
v48: float = id c21;
print v48;
v49: int = const 0;
v50: float = id c22;
print v50;
v51: int = const 0;
ret;
}
4 changes: 4 additions & 0 deletions benchmarks/float/matmul2x2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
28.00000000000000000
71.00000000000000000
20.00000000000000000
34.00000000000000000
1 change: 1 addition & 0 deletions benchmarks/float/matmul2x2.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 77
93 changes: 93 additions & 0 deletions benchmarks/float/matmulti2x2/gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# generate 10 2x2 float matrix multiply programs in bril

from jinja2 import Template
import numpy as np

NUM_EXAMPLES = 12

# Define the template for Bril code
template_str = '''
@main {
{% for i in range(NUM_EXAMPLES) %}
a11_{{i}}: float = const {{matrices[2*i][0][0]}};
a12_{{i}}: float = const {{matrices[2*i][0][1]}};
a21_{{i}}: float = const {{matrices[2*i][1][0]}};
a22_{{i}}: float = const {{matrices[2*i][1][1]}};
b11_{{i}}: float = const {{matrices[2*i+1][0][0]}};
b12_{{i}}: float = const {{matrices[2*i+1][0][1]}};
b21_{{i}}: float = const {{matrices[2*i+1][1][0]}};
b22_{{i}}: float = const {{matrices[2*i+1][1][1]}};

c11_{{i}}_part1: float = fmul a11_{{i}} b11_{{i}};
c11_{{i}}_part2: float = fmul a12_{{i}} b21_{{i}};
c11_{{i}}: float = fadd c11_{{i}}_part1 c11_{{i}}_part2;

c12_{{i}}_part1: float = fmul a11_{{i}} b12_{{i}};
c12_{{i}}_part2: float = fmul a12_{{i}} b22_{{i}};
c12_{{i}}: float = fadd c12_{{i}}_part1 c12_{{i}}_part2;

c21_{{i}}_part1: float = fmul a21_{{i}} b11_{{i}};
c21_{{i}}_part2: float = fmul a22_{{i}} b21_{{i}};
c21_{{i}}: float = fadd c21_{{i}}_part1 c21_{{i}}_part2;

c22_{{i}}_part1: float = fmul a21_{{i}} b12_{{i}};
c22_{{i}}_part2: float = fmul a22_{{i}} b22_{{i}};
c22_{{i}}: float = fadd c22_{{i}}_part1 c22_{{i}}_part2;

print c11_{{i}};
print c12_{{i}};
print c21_{{i}};
print c22_{{i}};
{% endfor %}
ret;
}
'''

# Define the hardcoded matrices
# [[8,3] * [[2,7] = [[28,71]
# [2,4]] [4,5]] 20,34]]
matrices = [
[[8,3],[2,4]],
[[2,7],[4,5]],
[[0,1],[2,3]],
[[0,1],[2,3]],
[[1, 2], [3, 4]],
[[5, 6], [7, 8]],
[[9, 10], [11, 12]],
[[13, 14], [15, 16]],
[[17, 18], [19, 20]],
[[21, 22], [23, 24]],
[[25, 26], [27, 28]],
[[29, 30], [31, 32]],
[[33, 34], [35, 36]],
[[37, 38], [39, 40]],
[[41, 42], [43, 44]],
[[45, 46], [47, 48]],
[[49, 50], [51, 52]],
[[53, 54], [55, 56]],
[[57, 58], [59, 60]],
[[61, 62], [63, 64]],
[[65, 66], [67, 68]],
[[69, 70], [71, 72]],
[[73, 74], [75, 76]],
[[77, 78], [79, 80]]
]

# Generate reference results using NumPy and write to matmulti2x2.ref
# TODO write to filename first argument to program
with open("matmulti2x2.ref", "w") as ref_file:
for i in range(NUM_EXAMPLES):
mat1 = np.array(matrices[2*i])
mat2 = np.array(matrices[2*i + 1])
result = np.matmul(mat1, mat2)
for row in result:
for element in row:
ref_file.write(f"{element:.17f}\n")

# Create a Jinja2 template and render it
template = Template(template_str)
rendered_str = template.render(matrices=matrices)

# Write the rendered Bril code to matmulti2x2.bril
with open("matmulti2x2.bril", "w") as bril_file:
bril_file.write(rendered_str)
Loading