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.28-cp310-cp310-win_amd64.whl (24.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

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

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.7m Windows x86-64

accera-1.2.28-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.28-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.28-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.28-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.28-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b7e527ad5c07a368b500e83e760867ae4398730af9405fe5bcef72a633a07e85
MD5 2d4969ae465f6fc5768b184f27299228
BLAKE2b-256 9c2adffdcf83fddb409b39ff66ece52edf80860b4c3a23d260e4ef01c5008903

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.28-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8fb60e763c9ac595ac3222ca5fe7413e1d541116ae0d0bcf10f232a8b48544ee
MD5 75da3e219c6903ae41c9aae9e1e331d9
BLAKE2b-256 768a6ffe8bd0d82dd69dfae5ee0a41fca8d8136e2cacf68f1c4277058c5241ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.28-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 ebe8402423dab9aa903983fa0688a648c8acd2936d604ba249009f79c55075e7
MD5 f388fba7818b171f6ea6681679a63a88
BLAKE2b-256 b4e3ad6715404a1c05adae3db995d2ef6529b1de933f26d9b6a9b9c1e2cc8024

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.28-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.28-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 8ad19dc90de4b8723f48ded5a7007c5fb96b701f5751d3f46e25706329f3adca
MD5 b0e3121c06d076744051accb078401a8
BLAKE2b-256 5920093f76b49a3438d99bfe233ce1e49c4be28d66cb5ad50ada5c8021187c31

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.28-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 57e7c71eac326c885ce3bfeb5513047651f280e0ee9c0d3f187e376ec419402c
MD5 bea05fb810f19a1ae3098c6027e066c6
BLAKE2b-256 6474e8e0daf3b90b3ccc19f74e8b3f82bca232fb8ac374ce6e7ca57bb8707e28

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.28-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 fb5141aa8cb089c33bcbd0f6ba38607d87466cff3bb44218551c43a610947079
MD5 245a210d4d1dfba1259c1297debc89e8
BLAKE2b-256 818312348ac81b21da7e5db6684e014520dbe4be89182e9fcda23e41727b14e9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.28-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.28-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 e7f071b6f94f86ed531d7cba8d7f31634c6eb0de3b0992d1c81db0bbeaf223e0
MD5 c07e626e31f1c868e1f6f8937e0186e8
BLAKE2b-256 8790042e791fee462240eedafe43fb152d6db81db974b8b3a8b70db5afe14379

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.28-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f5322f8741fb1d58cc18d44db908a548f3902ec976c87502e9490b2144ded4b9
MD5 c326d94008817888185ee1ee5f6cf204
BLAKE2b-256 e928706db85612fa52be16b4133d0c08d43c3b4600d6405fc856bed8f6309d4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.28-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 f96fafad82594dcc10c6cb770818297df279005f9878873262fa26b3d2f27e02
MD5 05bae24fd57af8aa12aafa62df13315d
BLAKE2b-256 95ece6f7b2fb7169970f07f9457f4d0181b58f335bd2b8fdc1b3650c2a09cf84

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.28-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.28-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 cfa3ffacec17a8442e8a8622a6493a4f23ecb3cfe06be29bcea1dc011e9c52bd
MD5 db7c197032fcf71ece813a8ce4e2d64e
BLAKE2b-256 435aa21f6d8f416df7530805780f81ffbfe8e035ff6815a8966cf9478e976160

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.28-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e2c0ad2f21068e4b722de03bfd03ed71313079c834804b05b25e262b9b09526c
MD5 54920b267a0cbc46aaf8991ff3d0e6e3
BLAKE2b-256 48dbf0a6b11be1a4c9849b49cca507163f600e5f43aa91f86cf1236b67abbd88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.28-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 2c67f37fb1c6330d80237d845a43086d30bd8646fd4c435e1fddb8267c3cae77
MD5 10535cb5d46e513ec48509fb2b5fb0d2
BLAKE2b-256 cad3852d866df726d43ac99e3bd056fa514a00c398a07ef25fc2bb513cb28df2

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