Skip to main content

Accera - An optimizing cross-compiler for compute-intensive code

Project description

Accera logo

PyPI package version Python versions MIT License

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

Binder

No installation is required. This will launch a Jupyter notebook with the quickstart example running in the cloud.

Run on your machine

  1. 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))
    
  2. 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 Binder

  3. Install Accera:

    pip install accera
    
  4. Generate the library that implements two versions of matmul + ReLU:

    python quickstart.py
    
  5. 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)
    
  6. 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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

accera-1.2.18-cp310-cp310-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

accera-1.2.18-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

accera-1.2.18-cp310-cp310-macosx_11_0_x86_64.whl (33.5 MB view details)

Uploaded CPython 3.10 macOS 11.0+ x86-64

accera-1.2.18-cp39-cp39-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.9 Windows x86-64

accera-1.2.18-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

accera-1.2.18-cp39-cp39-macosx_11_0_x86_64.whl (33.5 MB view details)

Uploaded CPython 3.9 macOS 11.0+ x86-64

accera-1.2.18-cp38-cp38-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.8 Windows x86-64

accera-1.2.18-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

accera-1.2.18-cp38-cp38-macosx_10_15_x86_64.whl (33.5 MB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

accera-1.2.18-cp37-cp37m-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.7m Windows x86-64

accera-1.2.18-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.9 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

accera-1.2.18-cp37-cp37m-macosx_10_15_x86_64.whl (33.5 MB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

Details for the file accera-1.2.18-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.18-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.2 CPython/3.10.9

File hashes

Hashes for accera-1.2.18-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1ca12b383093511d2ee321f91d8bbac2a444d074e29b19547ccd0ed785842ca3
MD5 35bb90bc724979aba2d70fd34b2cdbb1
BLAKE2b-256 3c13a06305623f31f61e3975e6248b351591d7e5c1f65bd74e61e2f823558a64

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.18-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d9e6ae47713ea2a8bacb3684726bc96e4de9dc9350ddfcff0ab0d506c413b436
MD5 f79a2978e8f0f07d81bf4e9523a2b935
BLAKE2b-256 8f44a8122e6e7f226e169eac7472cc1a55d64c50eb19daf7e662833b58f90aa2

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.18-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 73bddcb3a7911b743188abf63026e8e6bde9f6e83294dc77b9631fb03e86498f
MD5 e5566284e976dd5350dbd7e1c5ec071b
BLAKE2b-256 c6a32e773804a1dcbcf76f61c9c60890e3452bf19f2f187591e1cf08924e02e7

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.18-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.2 CPython/3.9.13

File hashes

Hashes for accera-1.2.18-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 4504b3f87d2c70b1757e225d4eb4e8def3b520fcba651e55ee6d6b809c15b425
MD5 72bbc2550437b8be7aad9686165f6ad3
BLAKE2b-256 4223c704d7a04f82b3c8e107f3c9afc0f72914b59a25c5d0ff4dfd8fbbbfa7ab

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.18-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e5c0618c07c9ab4fe1589a3f692801ab7473d619dcc869f2460ade8a4cb6715d
MD5 45443cf4129aa3be97dfcc7427d7c925
BLAKE2b-256 a4831f9536d8b44fcee501164b7df1a37e5af173ae12d93a37a7644c9017baa4

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp39-cp39-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.18-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 174d83be9822fcf427b4f4efe7d073f5217865a7786229efd99caba2d7f5327a
MD5 aab2749ed864aca58d749157c8a0532e
BLAKE2b-256 ee45eb62f081b1f54030803637c62afbbc0186edbfcadead4ad99282ea08b4ad

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.18-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.2 CPython/3.8.10

File hashes

Hashes for accera-1.2.18-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 2fd4fefce5633893a9e8067cebae91fb33c49a0c891f367074c5b5089fa1c414
MD5 4dba52130f5d9b497be39f65409ffdad
BLAKE2b-256 81b24325cd62ae18149255fae793796db74cb01a84a4670e39f1b73293bdfca1

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.18-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a53605b8e0b6c31769d5bf3b6c73d2b15ee873b4fd96a378742bb16f7d6bbed3
MD5 ba3214cd52ab8a3e93f533ed844feede
BLAKE2b-256 f40c3d1f721982c318cc06572b4495e1b5e3f1f7b135783c99a119d3911f03b4

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.18-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 106d9bb134c9f503cb4fa567c323b758e8a0aa628ceead369380654a2a9f8f64
MD5 7e2560b4e0e5ea85ac76db1d82c34d14
BLAKE2b-256 a946f6e410399acb94121f0d80a773764fa7751e88446311857ecbe1448ce47e

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.18-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.2 CPython/3.7.9

File hashes

Hashes for accera-1.2.18-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 e5f66113077de77b866ce5f9790ecb6e880f84849f2c8fa2f88caa82e96fe94e
MD5 fbee9cc436f2a1301c6ff807d07b5d1b
BLAKE2b-256 4cf28f4222d618b034d67eb4451b817521a5f9cfdb1df7c1c6ca1fd9c627078f

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.18-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5a5af2afba9300ce278e49903b2a93ba57c8be0d139c735ed9cc24eac1852428
MD5 33650bacf046c4ce86e1b2fc2750fc1b
BLAKE2b-256 c118897e5f75144c8c0a02cb9a9a995178b277d625a807cc9c1c1e6e8c344cbc

See more details on using hashes here.

File details

Details for the file accera-1.2.18-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.18-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 08983b3e75eaf44c7aa695c0d6fa807389006716b4c6a5689b2393137dce0ac7
MD5 80b6ccd370703b498e0e241afdcb90fa
BLAKE2b-256 eee3aa980679947d99a10d07355730538c91a6d0b695d6efaa12e87206c69141

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page