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.Role.INPUT, shape=(512, 512))
    B = acc.Array(role=acc.Role.INPUT, shape=(512, 512))
    C = acc.Array(role=acc.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.20-cp310-cp310-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

accera-1.2.20-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.20-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.20-cp39-cp39-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.9 Windows x86-64

accera-1.2.20-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.20-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.20-cp38-cp38-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.8 Windows x86-64

accera-1.2.20-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.20-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.20-cp37-cp37m-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.7m Windows x86-64

accera-1.2.20-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.20-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.20-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.20-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.20-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 61a9862ad459ec18fa8b9a85e62aae02365d03faed2d1517df003b993aeedee0
MD5 2d3e517246e4be2e4a2742b011792ade
BLAKE2b-256 9d8d66a2760845d40a600f88ca986e62dc08785488ba9b8fa774cf2eaa0cd7e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.20-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e9fa5a6cbe720254f4cf44f2106d19cde7d8f25b499b57a0643d311c74eac347
MD5 fc435eae4d06f0e8dea787a3e980d77a
BLAKE2b-256 2b77921f6e3b72e5810c0bd6b12c4310d064f52dd1f66df86f264d1f75505b36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.20-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 cb4dc025822669e2347072ca3f7130e3642912879faa820983267317a5373bb8
MD5 b2ac297959fe6086ab95969c1470dddc
BLAKE2b-256 7190e94509551c1ff0228bb9743733226235b871798d79c83c5f20f250d35d59

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.20-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.20-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 bdba46a64d4ead9800f8e46c5f53ca1aaf0b1303a06305330839ef3fcfe32847
MD5 0358dd27cbc518e1bfb799d4e996907c
BLAKE2b-256 bcc257c401ab3f1ec4ccc702a55e614bc0004efeffa1cd79712af232d085ea32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.20-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5f6b745a690ecae898386d69356433338bbeb7865b839723c3604c33ec2186de
MD5 1b0f3df9dbcb13582405cdbeb74ea874
BLAKE2b-256 767fc9b842b88fc37a58952754fcaae128e7554714976c5a33b620e70a7547b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.20-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 2db03ca7e39396fdc6b5598b27349af477f6b7693f65cd46d9ef2c5b7fd9119f
MD5 0ec88c58ba648043fc16d14a048bab2c
BLAKE2b-256 64321250afdc0df7c27a628f7e1bfee5bc786481841e7f09e9d473d2f32110ef

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.20-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.20-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 9df2690ebfd26ebdcffe0a52a51f071ac9586f99388653877f13009219ee2cab
MD5 8a2d6aa2cce321150fce354ec740a5ee
BLAKE2b-256 4a89c461353adfa300b74674970d139af95daa1b8ff88d0378c7cb1e00cc4c53

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.20-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cd98bfb0cde842646a44a761fbca3441358dc168a46b6313595bce6824b7ed3f
MD5 d11cf66eb6ed50bb0ce07f4be2ed2af2
BLAKE2b-256 61a886dd6980f116d9417000fc4302a7175cdd411db34b8ccd8ecd9f956ca172

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.20-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 8f1f898a071aea61d4766c3e77e364908594eba88880568edc1117f7dad45f45
MD5 d1b417485bd9d70a88d3e67f51239f8c
BLAKE2b-256 5712a57db1590715987269797c25b2314325c6ec0a020d7805a62caf18156233

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.20-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.20-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 4dc8a6b281830206764173a42f3df1c284f940046f5ee6795f208bdc1c27ddf6
MD5 5ec79ede8de935b5861fbc0d1c6f4b5e
BLAKE2b-256 fa218652925978aee7fd42406a6e4182a0aa7ac7dce87e0ad3dd638e1d6eb558

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.20-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fdb1da78ef05b205e2e0b52017613ba8bd807cf9114355a5a60fb098cc2c0792
MD5 19ac64df9876e1748cece4b98815448c
BLAKE2b-256 2081fe1ae590f60ef0105dc2da12f09b684f6b7907273a0373709440f72db5e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.20-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 005841630003f84f1d4c57241e08e83fa652aebf2f73c25f705ce7fa7db383c3
MD5 d5378341553e1d4ed6d86f9eb5f33cc8
BLAKE2b-256 e3fac3b6754c48c136143865f302da53854594a519de3b53808fc04e8fb95e9d

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