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
    i, j, f, 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.23-cp310-cp310-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

accera-1.2.23-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.0 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

accera-1.2.23-cp310-cp310-macosx_11_0_x86_64.whl (33.6 MB view details)

Uploaded CPython 3.10 macOS 11.0+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

accera-1.2.23-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.0 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

accera-1.2.23-cp39-cp39-macosx_11_0_x86_64.whl (33.6 MB view details)

Uploaded CPython 3.9 macOS 11.0+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

accera-1.2.23-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.0 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

accera-1.2.23-cp38-cp38-macosx_10_15_x86_64.whl (33.6 MB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

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

Uploaded CPython 3.7m Windows x86-64

accera-1.2.23-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.0 MB view details)

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

accera-1.2.23-cp37-cp37m-macosx_10_15_x86_64.whl (33.6 MB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: accera-1.2.23-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.10

File hashes

Hashes for accera-1.2.23-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d89f140ea7915bfd4737b85efdcfad45ffe3d31ef41faf8c9b196c81eeb73a57
MD5 0b348c23f9f8c57fa96f6d803a372dc9
BLAKE2b-256 26f27b62a42ddc1245fd4f6ed4b621f2e42afb148e9f3a036d3d0149f97448c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.23-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0deb4d527486d5c6c45cc348c227bd7e2b59e474d2fb42d77d223065d4d62f4e
MD5 fd43314fdc34fd141d9a794851096ff2
BLAKE2b-256 5fc83da67c12b7ee6775d38dd273df218af6a85db14bbf3c432522cb3d96cfd4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.23-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 1230b3aea0355acfb81bdfbd82d394e7dbb1027fe0f9c25a9ce9b2734b166ed7
MD5 a7ce09d3187244eb60d56dcb9293e9df
BLAKE2b-256 cd691c9eac5f80243202bc9b3b01140e40d515cc58c74eb69b9e4e3c179fb063

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.23-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.23-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 39ceee7ece4f845b303842a07ee647f3ccad0ffedf9f71cfa65d28f84beba31b
MD5 ff3d9197e730b1c07448c63b14ed349c
BLAKE2b-256 96f7c1d14424824541372076854fa93dd55261f21e8b7e124a60c19101487e42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.23-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ce31fa64af26844c9aed8c1f0e19382b3e146e3be5756f3436e023b7946d322f
MD5 1e0087f04a408a7f7f5daac6de6ba044
BLAKE2b-256 e53ef9b420119f478d9e93ed70d6ecebe450963cdf19bbf2212dee2e9ce2cf99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.23-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 cdd407714ffb38a0f2344563c28328ec244cbf9f3f3983bfed014fd380336a27
MD5 abe5a9b7823334db0bee8d0589df5115
BLAKE2b-256 665dd2826a560310f9ed822def2985711baaa161928bce6bd10edc3247aa6942

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.23-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.23-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 5b9145e0f44c340d8d1951733c58d9e2eb648d46b587cdb9eef249e9b240d788
MD5 be1e65898aea8081eb4c95b0198d8517
BLAKE2b-256 df7362b4f24986948b4de605f4723daf3030746f8d218a4b155d716118d59ca6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.23-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 61c3155fc1b621bd2da715edf314cf725a9047540267bf672e3e6aedf72227e1
MD5 6ee1423473720daffed67291fe4212bc
BLAKE2b-256 842a7d4ada6696d5d27ac82102df6ec0d994a5c67082eade190f39dc55211536

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.23-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 6e1195d6aeb84af1d5380a42eeb3990a94d5659bf7a8ace8fd2d403548980443
MD5 fb9ff30f7ce7357502a4c81262b95bef
BLAKE2b-256 d60edd985b2fc3a09e98722e7c50ba4a9fb900749805fa3929aa98eea28ba69f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.23-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.23-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 a8900114750f567ab2eeffdac734883b96bf1414bc1aa5e951df9f7e9b28bbea
MD5 00267d4dcaf2354bbe4a323f929e9213
BLAKE2b-256 9eae0de9dbbb20e94e0b9653550476b1b5f05142cd16ec6e34fe224bb2c47503

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.23-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8f28170ba896756ba917f0b93a9ef6d62e29ddec16fc738f278dc7f1a333d016
MD5 6b367e2fd5d07a4478ffa47de7b68692
BLAKE2b-256 c21442c351857d259b2a4c25942b1e3b26fbdd0ffa28e4498c90ecd357a8cd21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.23-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 c13dc7cf6fbff19932dbd7a85372e27e0dc410660bd89c4ef9b52df2abe15115
MD5 821c96eaebc26e1192470bb4c768e3be
BLAKE2b-256 86690be99c5b9dfc847bd471355b1f6de8bec2774cba2c066408b2af1334d705

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