From cbf188974283f0c6b19b355ec05d3f057884ff32 Mon Sep 17 00:00:00 2001 From: David Collier Date: Fri, 19 Mar 2021 08:20:34 +0000 Subject: [PATCH 1/4] Remove ksc-mlir tests of examples from test/ksc These can be reinstated later, but the examples will need to be run through ksc first, rather than expecting ksc-mlir to be able to process these files directly. (We don't expect ksc-mlir to be able to deal with gdefs, for example.) --- mlir/run-test-examples.sh | 1 - mlir/test/Ksc/examples.ks | 69 --------------------------------------- 2 files changed, 70 deletions(-) delete mode 100755 mlir/run-test-examples.sh delete mode 100644 mlir/test/Ksc/examples.ks diff --git a/mlir/run-test-examples.sh b/mlir/run-test-examples.sh deleted file mode 100755 index 2a32f18ef..000000000 --- a/mlir/run-test-examples.sh +++ /dev/null @@ -1 +0,0 @@ -grep 'RUN.*%K' mlir/test/Ksc/examples.ks | sed -e 's|^; RUN: |./build/bin/|' | sed -e 's/%KNOSSOS/./' | sh -x diff --git a/mlir/test/Ksc/examples.ks b/mlir/test/Ksc/examples.ks deleted file mode 100644 index 9ca6f53c2..000000000 --- a/mlir/test/Ksc/examples.ks +++ /dev/null @@ -1,69 +0,0 @@ -; Knossos tests, should be parseable into MLIR and LLVM. -; However, there are a number of tests that do not pass for various reasons. -; Here are the ones that do pass. If new ones are supported, add them here. - -; For the inlined tests here: -; RUN: ksc-mlir LLVM %s > /dev/null - -; Unsupported tests need: - -; adbench-lstm.ks -; * Vector of Tuples of Vectors -; * constVec -; derivative-selection.ks: -; * $inline -; edef.ks: -; * LM type -; ex3.ks: -; * Unknown -; fold.ks: -; * Tuple of Vectors -; * $check, $ranhashdoub -; * Auto-derivation (rev$, fwd$) -; generated_deltavecs.ks: -; * sumbuild, assert -; gmm.ks: -; * Vector of Vectors, Tuple of Vectors -; * assert, lgamma -; inline.ks: -; * $inline -; mnistcnn.ks: -; * Vector of Vectors -; * sumbuild, ts_add -; power.ks: -; * Auto-derivation (rev$, fwd$) -; test[0-4].ks -; * Strings -; * $check -; vprod.ks: -; * (lam) as function argument - -; Assorted tests - -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/awf.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/bool.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/comment.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/CA-subst.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/hello-world.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/lmzero.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/logsumexp.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/negative-float-literals.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/sum.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/test_stopgrad.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/tm-rev.ks > /dev/null - -; Examples - -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/ex0.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/ex1.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/ex2.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/ex4.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/ex5.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/ex6.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/ex7.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/ex8.ks > /dev/null - -; Mul - -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/mul4.ks > /dev/null -; DISABLED: ksc-mlir LLVM %KNOSSOS/test/ksc/mul8.ks > /dev/null From e390c272a41163417fc84d62be8877ce9058429a Mon Sep 17 00:00:00 2001 From: David Collier Date: Fri, 19 Mar 2021 13:00:59 +0000 Subject: [PATCH 2/4] Test ksc-mlir on examples from test/ksc --- test/builds/build_and_test_mlir.yml | 15 ++++++++++++++- test/builds/test_kscmlir_examples.sh | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/builds/test_kscmlir_examples.sh diff --git a/test/builds/build_and_test_mlir.yml b/test/builds/build_and_test_mlir.yml index de157c8e4..56b8e4bb5 100644 --- a/test/builds/build_and_test_mlir.yml +++ b/test/builds/build_and_test_mlir.yml @@ -11,6 +11,9 @@ jobs: steps: + - script: sh ./test/builds/mkdirs.sh + displayName: 'Make directories [userInstall]' + - script: echo "##vso[task.setvariable variable=LLVM_BRANCH]$(cat $(Build.Repository.LocalPath)/etc/llvm-branch.txt)" - task: Cache@2 @@ -27,4 +30,14 @@ jobs: condition: ne(variables.CACHE_RESTORED, 'true') - script: sh ./test/builds/build_and_test_kscmlir.sh - displayName: Testing Ksc MLIR [userTest] + displayName: Building and testing Ksc MLIR [userTest] + + - script: sh ./test/builds/install_linux.sh || (sleep 30 && sh ./test/builds/install_linux.sh) || (sleep 30 && sh ./test/builds/install_linux.sh) + displayName: 'Install dependencies [userInstall]' + + - script: /opt/cabal/3.0/bin/cabal v2-install --with-ghc /opt/ghc/8.6.5/bin/ghc-8.6.5 --installdir=build/bin --overwrite-policy=always --install-method=copy + displayName: 'Building ksc [userInstall]' + + - script: sh ./test/builds/test_kscmlir_examples.sh + displayName: Testing Ksc MLIR on examples from ksc [userTest] + diff --git a/test/builds/test_kscmlir_examples.sh b/test/builds/test_kscmlir_examples.sh new file mode 100644 index 000000000..a22318668 --- /dev/null +++ b/test/builds/test_kscmlir_examples.sh @@ -0,0 +1,13 @@ +set -e + +mkdir -p obj/test/kscmlir/examples + +for example in ex0; do + echo "Testing example $example.ks" + ./build/bin/ksc --generate-cpp \ + --ks-source-file src/runtime/prelude.ks \ + --ks-source-file test/ksc/$example.ks \ + --ks-output-file obj/test/kscmlir/examples/$example.kso \ + --cpp-output-file /dev/null + ./build/bin/ksc-mlir MLIR obj/test/kscmlir/examples/$example.kso > /dev/null +done From 872d9d92f3c40f6198efb3878bb9000388596282 Mon Sep 17 00:00:00 2001 From: David Collier Date: Thu, 2 Sep 2021 14:08:38 +0100 Subject: [PATCH 3/4] Prune unused defs in prelude before testing with ksc-mlir --- src/python/ksc/prune_and_compile.py | 72 ++++++++++++++++++++++++++++ test/builds/test_kscmlir_examples.sh | 6 +-- 2 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/python/ksc/prune_and_compile.py diff --git a/src/python/ksc/prune_and_compile.py b/src/python/ksc/prune_and_compile.py new file mode 100644 index 000000000..301f8d2d5 --- /dev/null +++ b/src/python/ksc/prune_and_compile.py @@ -0,0 +1,72 @@ +# Invoke ksc on a file, treating any def which appears in the file as a root. +# (But any unused functions in the prelude are pruned.) + +import subprocess +import sys + +from ksc import utils +from ksc.parse_ks import parse_ks_filename +from ksc.expr import Def, GDef +from ksc.type_propagate import type_propagate_decls + + +def function_name(decl): + if isinstance(decl, Def): + return str(decl.name) + # elif isinstance(decl, GDef): + # return str(decl.name()) + else: + return None + + +def prune_and_compile(source_file, output_file): + ksc_path, ksc_runtime_dir = utils.get_ksc_paths() + + symtab = dict() + decls_prelude = list(parse_ks_filename(f"{ksc_runtime_dir}/prelude.ks")) + type_propagate_decls(decls_prelude, symtab) + decls = list(parse_ks_filename(source_file)) + type_propagate_decls(decls, symtab) + + function_names = [ + fn for fn in (function_name(decl) for decl in decls) if fn is not None + ] + + ksc_command = [ + ksc_path, + "--generate-cpp", + "--ks-source-file", + f"{ksc_runtime_dir}/prelude.ks", + "--ks-source-file", + source_file, + "--ks-output-file", + output_file, + "--cpp-include", + "prelude.h", + "--cpp-output-file", + "/dev/null", + "--remove-unused", + *(opt for function_name in function_names for opt in ("--used", function_name)), + ] + + try: + e = subprocess.run(ksc_command, capture_output=True, check=True) + except subprocess.CalledProcessError as e: + ksc_command_str = " ".join(ksc_command) + print(f"Command failed:\n{ksc_command_str}") + print("KSC output:\n") + print(e.output.decode("ascii")) + ksc_stderr = e.stderr.decode("ascii") + ksc_stderr_filtered = "> " + "\n> ".join(ksc_stderr.split("\n")[:-1]) + print(ksc_stderr_filtered + "\n") + raise Exception( + f"Command failed:\n" + f"{ksc_command_str}\n" + f"KSC output:\n" + f"{ksc_stderr_filtered}\n" + ) from e + + +if __name__ == "__main__": + assert len(sys.argv) == 3 + prune_and_compile(sys.argv[1], sys.argv[2]) diff --git a/test/builds/test_kscmlir_examples.sh b/test/builds/test_kscmlir_examples.sh index a22318668..6ea8e289b 100644 --- a/test/builds/test_kscmlir_examples.sh +++ b/test/builds/test_kscmlir_examples.sh @@ -4,10 +4,6 @@ mkdir -p obj/test/kscmlir/examples for example in ex0; do echo "Testing example $example.ks" - ./build/bin/ksc --generate-cpp \ - --ks-source-file src/runtime/prelude.ks \ - --ks-source-file test/ksc/$example.ks \ - --ks-output-file obj/test/kscmlir/examples/$example.kso \ - --cpp-output-file /dev/null + python src/python/ksc/prune_and_compile.py test/ksc/$example.ks obj/test/kscmlir/examples/$example.kso ./build/bin/ksc-mlir MLIR obj/test/kscmlir/examples/$example.kso > /dev/null done From ab7d17dde8ff049f59413574673c2860ec7cd4df Mon Sep 17 00:00:00 2001 From: David Collier Date: Thu, 2 Sep 2021 14:35:22 +0100 Subject: [PATCH 4/4] Fix CI --- test/builds/build_and_test_mlir.yml | 9 +++++++-- test/builds/test_kscmlir_examples.sh | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/test/builds/build_and_test_mlir.yml b/test/builds/build_and_test_mlir.yml index 56b8e4bb5..7de91280c 100644 --- a/test/builds/build_and_test_mlir.yml +++ b/test/builds/build_and_test_mlir.yml @@ -10,7 +10,12 @@ jobs: vmImage: 'ubuntu-18.04' steps: - + + - task: UsePythonVersion@0 + inputs: + versionSpec: '3.8' + architecture: 'x64' + - script: sh ./test/builds/mkdirs.sh displayName: 'Make directories [userInstall]' @@ -38,6 +43,6 @@ jobs: - script: /opt/cabal/3.0/bin/cabal v2-install --with-ghc /opt/ghc/8.6.5/bin/ghc-8.6.5 --installdir=build/bin --overwrite-policy=always --install-method=copy displayName: 'Building ksc [userInstall]' - - script: sh ./test/builds/test_kscmlir_examples.sh + - script: sh ./test/builds/test_kscmlir_examples.sh . displayName: Testing Ksc MLIR on examples from ksc [userTest] diff --git a/test/builds/test_kscmlir_examples.sh b/test/builds/test_kscmlir_examples.sh index 6ea8e289b..42dc9603e 100644 --- a/test/builds/test_kscmlir_examples.sh +++ b/test/builds/test_kscmlir_examples.sh @@ -1,9 +1,15 @@ set -e +echo Installing dependencies... +python3 -m pip install -r src/python/requirements.txt -f https://download.pytorch.org/whl/torch_stable.html + +KNOSSOS=$1 +KSCPY=$KNOSSOS/src/python + mkdir -p obj/test/kscmlir/examples for example in ex0; do echo "Testing example $example.ks" - python src/python/ksc/prune_and_compile.py test/ksc/$example.ks obj/test/kscmlir/examples/$example.kso + PYTHONPATH=$KSCPY python3 src/python/ksc/prune_and_compile.py test/ksc/$example.ks obj/test/kscmlir/examples/$example.kso ./build/bin/ksc-mlir MLIR obj/test/kscmlir/examples/$example.kso > /dev/null done