Accera - An optimizing cross-compiler for compute-intensive code
Project description
Problem at Hand
Writing highly optimized compute-intensive code in a traditional programming language is strenuous and time-consuming. Not only does it require advanced engineering skills such as fluency in Assembly language, but a deep understanding of computer architecture is also indispensable. Manual optimization of even the simplest numerical algorithms demands a significant engineering effort. Needless to say, a highly optimized numerical code is often prone to bugs, lacks readability, and offers little to no usability. Code maintenance becomes a nightmare resulting in the reimplementation of the same logic every time an architecture level change is introduced.
Accera: An Optimized Solution
Accera is a compiler that enables you to experiment with loop optimizations without hand-writing Assembly code. With Accera, these problems and impediments can be addressed in an optimized way. It is available as a Python library and supports cross-compiling to a wide range of processor targets.
Accera has THREE primary goals:
- Performance: To guarantee the fastest implementation for any compute-intensive algorithm.
- Readability: To ensure effective implementation of algorithms without sacrificing the readability of code.
- Writability: To provide a user-friendly programming model, designed for agility and maintainability.
Install
To install for Linux, macOS, or Windows (requires Python 3.7-3.10):
pip install accera
See the Install Instructions for more details on installing pre-built Python 3 packages and how to build Accera from the source.
Quickstart
In this example, we will:
- Implement matrix multiplication with a ReLU activation (matmul + ReLU), commonly used in machine learning algorithms.
- Generate two implementations: a naive algorithm and loop-based transformations.
- Compare the execution time of both implementations.
Run in your browser
No installation is required. This will launch a Jupyter notebook with the quickstart example running in the cloud.
Run on your machine
-
Create a Python 3 script called
quickstart.py
:import accera as acc # define placeholder inputs/output A = acc.Array(role=acc.Array.Role.INPUT, shape=(512, 512)) B = acc.Array(role=acc.Array.Role.INPUT, shape=(512, 512)) C = acc.Array(role=acc.Array.Role.INPUT_OUTPUT, shape=(512, 512)) # implement the logic for matmul and relu matmul = acc.Nest(shape=(512, 512, 512)) i1, j1, k1 = matmul.get_indices() @matmul.iteration_logic def _(): C[i1, j1] += A[i1, k1] * B[k1, j1] relu = acc.Nest(shape=(512, 512)) i2, j2 = relu.get_indices() @relu.iteration_logic def _(): C[i2, j2] = acc.max(C[i2, j2], 0.0) package = acc.Package() # fuse the i and j indices of matmul and relu, add to the package schedule = acc.fuse(matmul.create_schedule(), relu.create_schedule(), partial=2) package.add(schedule, args=(A, B, C), base_name="matmul_relu_fusion_naive") # transform the schedule, add to the package f, i, j, k = schedule.get_indices() ii, jj = schedule.tile({ i: 16, j: 16 }) # loop tiling schedule.reorder(j, i, f, k, jj, ii) # loop reordering plan = schedule.create_plan() plan.unroll(ii) # loop unrolling package.add(plan, args=(A, B, C), base_name="matmul_relu_fusion_transformed") # build a dynamically-linked package (a .dll or .so) that exports both functions print(package.build(name="hello_accera", format=acc.Package.Format.HAT_DYNAMIC))
-
Ensure that you have a compiler in your PATH:
- Windows: Install Microsoft Visual Studio and run
vcvars64.bat
to setup the command prompt. - Linux/macOS: Install gcc
Don't have a compiler handy? We recommend trying Accera in your browser instead
- Windows: Install Microsoft Visual Studio and run
-
Install Accera:
pip install accera
-
Generate the library that implements two versions of matmul + ReLU:
python quickstart.py
-
To consume and compare the library functions, create a file called
benchmark.py
in the same location:import hatlib as hat import numpy as np # load the package _, functions = hat.load("hello_accera.hat") # call one of the functions with test inputs A_test = np.random.rand(512, 512).astype(np.float32) B_test = np.random.rand(512, 512).astype(np.float32) C_test = np.zeros((512, 512)).astype(np.float32) C_numpy = np.maximum(C_test + A_test @ B_test, 0.0) matmul_relu = functions["matmul_relu_fusion_transformed"] matmul_relu(A_test, B_test, C_test) # check correctness np.testing.assert_allclose(C_test, C_numpy, atol=1e-3) # benchmark all functions hat.run_benchmark("hello_accera.hat", batch_size=5, min_time_in_sec=5)
-
Run the benchmark to get the execution time results:
python benchmark.py
Next Steps
The Manual is the best introductory resource for the Accera Python programming model.
In particular, the schedule transformations describe how you can experiment with different loop transformations with just a few lines of Python code.
Finally, the .hat
format is just a C header file containing the metadata. Learn more about the HAT format and benchmarking.
How it works
In a nutshell, Accera takes the Python code that defines the loop schedule and algorithm while converting it into MLIR intermediate representation (IR). Accera's compiler then takes this IR through a series of MLIR pipelines to perform transformations. The result is a binary library with a C header file. The library implements the algorithms that are defined in Python and it is compatible with the target.
To peek into the stages of IR transformation that Accera does, try replacing format=acc.Package.Format.HAT_DYNAMIC
with format=acc.Package.Format.MLIR_DYNAMIC
in quickstart.py
, re-run the script, and search the _tmp
subfolder for the intermediate *.mlir
files. We plan to document these IR constructs in the future.
Documentation
Get familiar with Accera's concepts and Python constructs in the Documentation page.
Tutorials
Step-by-step examples are available on the Tutorials page. We're working on adding more complementary examples and tutorials.
Contributions
Accera is a research platform-in-progress that can certainly benefit from your contributions. We would love your feedback, recommendations, and feature requests. Not to mention that we are excited to answer your questions. Let’s collaborate! Please file a Github issue or send us a pull request. Please review the Microsoft Code of Conduct to learn more.
Credits
Accera is built using several open source libraries, including: LLVM, pybind11, toml++, tomlkit, vcpkg, pyyaml, and HAT. For testing, we used numpy and catch2.
License
This project is released under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file accera-1.2.12-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: accera-1.2.12-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 24.2 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6823c4dc0f401cd20a4664bfaefce5d70379af0ee2deed23106c1ed6e2c59a4a |
|
MD5 | 93e03910206ee58e971e9e82e61221e0 |
|
BLAKE2b-256 | 0cdcad6da26fd32e1f591d7004b4fb0fcb2bbdbf9837102696181fcdb0fa14ff |
File details
Details for the file accera-1.2.12-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: accera-1.2.12-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 30.8 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87dd8da3970d87dc53d963aedc882b92931b95f138f53a8b77d1764d470a60a0 |
|
MD5 | 119c31ff649f8a0323e7f3a3f5e25705 |
|
BLAKE2b-256 | 302117f3d34f1d9e3a2aaf28da65eb4b53a09b32a29981b5221e8361ba8713ea |
File details
Details for the file accera-1.2.12-cp310-cp310-macosx_10_15_x86_64.whl
.
File metadata
- Download URL: accera-1.2.12-cp310-cp310-macosx_10_15_x86_64.whl
- Upload date:
- Size: 33.4 MB
- Tags: CPython 3.10, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77fc3120fe7759e92a9f5704d9ad0b18ce801052fa5ee50b36341367a3bb963a |
|
MD5 | f9d45218d1cd758c8d32fdc2df5accfa |
|
BLAKE2b-256 | a410353a24858285d7046334437b757b6b9ffaa99983aee193e0e2842f33693a |
File details
Details for the file accera-1.2.12-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: accera-1.2.12-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 24.2 MB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcefd55eec25fc7025aca099a8e427ccb0ee8541f29d692024091bb195857435 |
|
MD5 | 9d61eee6ba2c13f9f9a1302be843b319 |
|
BLAKE2b-256 | 233715b86f51af932a99c6df87849e700def15b20b36e75bc95417f5b4b60660 |
File details
Details for the file accera-1.2.12-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: accera-1.2.12-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 30.8 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c38e5f56db2323f73e4e937c4d0c4dfe22cc556d6ac4553e1f407fa9723a6114 |
|
MD5 | 97871a86d723b503ed1edfebb00f0716 |
|
BLAKE2b-256 | 66281b7251826375bfc1884d47a9a89d0709c3e5acc8dd3e21fccaa5e162b31a |
File details
Details for the file accera-1.2.12-cp39-cp39-macosx_10_15_x86_64.whl
.
File metadata
- Download URL: accera-1.2.12-cp39-cp39-macosx_10_15_x86_64.whl
- Upload date:
- Size: 33.5 MB
- Tags: CPython 3.9, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3024273f97353643f01378a65d53f32bf8f1679c02b9f9cbab08084492748440 |
|
MD5 | b45e378c9026b7020c4b8c6c0dda590a |
|
BLAKE2b-256 | 9af1d1e7c53beac4df4c7552c2ddc4367d7ef12939f74824772e472bfe97185e |
File details
Details for the file accera-1.2.12-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: accera-1.2.12-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 24.2 MB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d04c644a92cf787d494517fe53f7071c2f23075a39119f5cd968a68b06863d7 |
|
MD5 | 703cd75c9108d52b4d30fffc184813af |
|
BLAKE2b-256 | a4be1c8cc76b887ae4758bb4fe42e97ad989327fd03dc28bd371f3aef5f119f6 |
File details
Details for the file accera-1.2.12-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: accera-1.2.12-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 30.8 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 566844130856a9b0074b2630c4a1075f60b72ebb6ef5be28eec10af88dc23dff |
|
MD5 | 74a0adfebbdafc1d3369c8c7e8198d0e |
|
BLAKE2b-256 | 0f4df6169b9fa64c14b14bccee33ca0d0b59311bc0229b9359acbdf298d9c1af |
File details
Details for the file accera-1.2.12-cp38-cp38-macosx_10_15_x86_64.whl
.
File metadata
- Download URL: accera-1.2.12-cp38-cp38-macosx_10_15_x86_64.whl
- Upload date:
- Size: 33.4 MB
- Tags: CPython 3.8, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 564b79fd97780a85f48f43174aa53019e9d2905863296357868f9c26fe815caf |
|
MD5 | 1225ef1d6ab64e66b24364b7f30df4a2 |
|
BLAKE2b-256 | 0c7c9882b690d536335e4837ff151be97f80a4540f2d965fc61d6774fd5f0fd7 |
File details
Details for the file accera-1.2.12-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: accera-1.2.12-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 24.2 MB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d31ec67c36a5b127a3a6918b11b3dbbaf3087a71e212070d0110c6284635d20e |
|
MD5 | 303b409f947ae2c5d34e63b78ee568b3 |
|
BLAKE2b-256 | 70312147b06c8b14e4a353c66d448671797161a21a1c2d39164ae2f87d7244fd |
File details
Details for the file accera-1.2.12-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: accera-1.2.12-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 30.8 MB
- Tags: CPython 3.7m, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.7.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f3570730df4ae9d5916f37bd8da1650bf11aed39c39fa1e575cbd26bc4609e2 |
|
MD5 | a00676edfacbf0c79a194dc2442bae67 |
|
BLAKE2b-256 | e0ac50bb64d5127548f53d48979916d1764d909002a372640f5ed63d73c04f99 |
File details
Details for the file accera-1.2.12-cp37-cp37m-macosx_10_15_x86_64.whl
.
File metadata
- Download URL: accera-1.2.12-cp37-cp37m-macosx_10_15_x86_64.whl
- Upload date:
- Size: 33.4 MB
- Tags: CPython 3.7m, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 236fcc5362a38111039683c2c69958c3d5742111099130a953e27027161c1b2f |
|
MD5 | ad088cbbeadb809ecbf1e87f6e6048f9 |
|
BLAKE2b-256 | f3e90688bc94800027d616ea84f0826a2669f303217b5068044d06df5d85b1ae |