Skip to content

Commit

Permalink
Snakemake: Add tiled_add kernel (#318)
Browse files Browse the repository at this point in the history
* Change gendata to accept variable header includes

* Add snakefile for tiled_add compilation

* Add benchmark test in snakefile, run in right CI

* Solve other gendata problems

* Make intermittent files temporary

* Include with basename, not full path

* Remove benchmark test

* Remove genbenchmark.py

* Remove Makefile

* Address reviewer comments
  • Loading branch information
JosseVanDelm authored Dec 20, 2024
1 parent 23c92ab commit e79b968
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 191 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-run-kernel-snake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
working-directory: kernels/${{ matrix.kernel }}
strategy:
matrix:
kernel: [alloc, simple_copy, transform_copy, gemm, rescale, gemmini, streamer_alu, streamer_matmul]
kernel: [alloc, simple_copy, transform_copy, gemm, rescale, gemmini, streamer_alu, streamer_matmul, tiled_add]
23 changes: 0 additions & 23 deletions .github/workflows/build-run-kernel.yml

This file was deleted.

4 changes: 2 additions & 2 deletions kernels/gemm/gendata.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ def create_data_files():
"D_golden": D_golden,
}

create_header("data.h", sizes, variables)
create_data("data.c", variables)
create_header("data", sizes, variables)
create_data("data", variables)
4 changes: 2 additions & 2 deletions kernels/rescale/gendata.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ def create_data_files():
sizes = {"MODE": 0, "DATA_LEN": data_len}
variables = {"A": A, "O": O, "G": G}

create_header("data.h", sizes, variables)
create_data("data.c", variables)
create_header("data", sizes, variables)
create_data("data", variables)
4 changes: 2 additions & 2 deletions kernels/simple_copy/gendata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ def create_data_files():
A = np.linspace(1, array_size, array_size, dtype=np.int32)
sizes = {"N": array_size}
variables = {"A": A}
create_header("data.h", sizes, variables)
create_data("data.c", variables)
create_header("data", sizes, variables)
create_data("data", variables)
4 changes: 2 additions & 2 deletions kernels/streamer_alu/gendata.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ def create_data_files():
sizes = {"MODE": 0, "DATA_LEN": array_size, "LOOP_ITER": loop_iter}
variables = {"A": A, "B": B, "O": O, "G": G}

create_header("data.h", sizes, variables)
create_data("data.c", variables)
create_header("data", sizes, variables)
create_data("data", variables)
61 changes: 0 additions & 61 deletions kernels/tiled_add/Makefile

This file was deleted.

148 changes: 148 additions & 0 deletions kernels/tiled_add/Snakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
from util.snake.configs import get_snax_alu_config

config = get_snax_alu_config()

# Untiled example needs preprocessing:
for entry in config["mlirpreprocflags"]:
entry.append("--allow-unregistered-dialect")


def get_snax_opt_flags(options):
flags = []
match options:
case "deduponly":
flags = ["accfg-dedup"]
case "overlaponly":
flags = ["accfg-config-overlap"]
case "accfgboth":
flags = ["accfg-dedup", "accfg-config-overlap"]
return ",".join(
[
"insert-accfg-op{accelerator=snax_alu}",
"convert-linalg-to-accfg",
"mlir-opt{"
+ "\ ".join(
[
"executable=mlir-opt",
"generic=true",
"arguments='"
+ ",".join(
[
"-cse",
"-canonicalize",
"-allow-unregistered-dialect",
"-mlir-print-op-generic",
]
),
]
)
+ "'}",
*flags,
"convert-accfg-to-csr",
"snax-copy-to-dma",
"memref-to-snax",
"snax-to-func",
"clear-memory-space",
"snax-lower-mcycle",
]
)


module default_rules:
snakefile:
"../../util/snake/default_rules.smk"
config:
config


use rule * from default_rules as default_*


rule size_mlir:
input:
"{file}.preprocfinal.mlir",
output:
temp("{file}_{array_size}_{tile_size}.preprocfinal.sized.mlir"),
params:
sed_script_array_size=lambda wildcards: f"'s/128/{wildcards.array_size}/gm;t'",
sed_script_tile_size=lambda wildcards: f"'s/%tile_size = arith.constant 16 : index/%tile_size = arith.constant {wildcards.tile_size} : index/gm;t'",
shell:
"cat {input} |"
"sed -E {params.sed_script_array_size} |"
"sed -E {params.sed_script_tile_size}"
"> {output} "


rule snax_compile_mlir:
input:
"{file}.preprocfinal.sized.mlir",
output:
temp("{file}_{compiler_opt}.snax-opt.mlir"),
wildcard_constraints:
compiler_opt="noaccfgopt|deduponly|overlaponly|accfgboth",
params:
# hardcoded to no accfg for now
snax_opt_flags=lambda wildcards: get_snax_opt_flags(wildcards.compiler_opt),
shell:
"{config[snax-opt]} -p {params.snax_opt_flags} -o {output} {input}"


rule compile_main:
input:
"main_{array_size}.rendered.c",
"data_{array_size}.o",
output:
temp("main_{array_size}.o"),
shell:
"{config[cc]} {config[cflags]} -c {input} -o {output}"


rule compile_snax_binary:
input:
"{file}_{array_size}_{tile_size}_{compiler_opt}.o",
"main_{array_size}.o",
"data_{array_size}.o",
output:
"{file}_{array_size}_{tile_size}_{compiler_opt}.x",
shell:
"{config[ld]} {config[ldflags]} {input} -o {output}"


rule render_main:
input:
"main.c",
output:
temp("main_{array_size}.rendered.c"),
shell:
'echo "#include \\"data_{wildcards.array_size}.h\\"" | cat - {input} > {output}'


ARRAY_SIZES = ["256"]
TILE_SIZES = ["16"]
FILES = ["tiled", "tiled_pipelined", "untiled"]
COMPILER_OPTS = ["accfgboth", "noaccfgopt"]


rule all:
input:
expand(
"{file}_{array_size}_{tile_size}_{compiler_opt}.x",
file=FILES,
array_size=ARRAY_SIZES,
tile_size=TILE_SIZES,
compiler_opt=COMPILER_OPTS,
),
run:
for item in input:
shell("{config[vltsim]} {item}")


from gendata import generate_data


rule generate_data:
output:
"data_{array_size}.c",
"data_{array_size}.h",
run:
generate_data(int(wildcards.array_size))
53 changes: 0 additions & 53 deletions kernels/tiled_add/genbenchmark.py

This file was deleted.

16 changes: 3 additions & 13 deletions kernels/tiled_add/gendata.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import argparse

import numpy as np

from util.gendata import create_data, create_header

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate data for snax-alu operations."
)
parser.add_argument(
"--array_size", type=int, default=1024, help="Size of the arrays to generate"
)
args = parser.parse_args()

def generate_data(array_size):
low_bound = -128
high_bound = 127
array_size = args.array_size

# snax-alu design-time spatial parallelism
spatial_par = 4
Expand All @@ -37,5 +27,5 @@
sizes = {"MODE": 0, "DATA_LEN": array_size, "LOOP_ITER": loop_iter}
variables = {"A": A, "B": B, "O": O, "G": G}

create_header("data.h", sizes, variables)
create_data("data.c", variables)
create_header(f"data_{array_size}", sizes, variables)
create_data(f"data_{array_size}", variables)
1 change: 0 additions & 1 deletion kernels/tiled_add/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "data.h"
#include "memref.h"
#include "snax_rt.h"
#include "stdint.h"
Expand Down
4 changes: 2 additions & 2 deletions kernels/transform_copy/gendata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ def create_files(filename: str):
B = B.flatten()
sizes = {"N": array_size, "N_sqrt": sqrt(array_size)}
variables = {"A": A, "B": B}
create_header(f"{filename}.h", sizes, variables)
create_data(f"{filename}.c", variables)
create_header(filename, sizes, variables)
create_data(filename, variables)
22 changes: 0 additions & 22 deletions tests/benchmark/test_snax_benchmark.py

This file was deleted.

Loading

0 comments on commit e79b968

Please sign in to comment.