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

Uploaded CPython 3.10 Windows x86-64

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

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.7m Windows x86-64

accera-1.2.16-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.16-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.16-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.16-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.16-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b5f1740df5087ea52c24edaf164794a36f30798b15a7ed3484e22019a9de3c55
MD5 b5e8f5409139d31a45f496b46c2a1b7f
BLAKE2b-256 5d002deb2ab1bdf48a39497ef6b430ff131fef2b4dbb1cebd2d24ecc99e97abe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.16-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9bc29a3938ded8f9eae5de94ed2f15f8512796785d28aacbfb0e62a6b752a2d7
MD5 432077e37110b4b9f93c8f80c8dd0351
BLAKE2b-256 1327c3f9e839b7c12680c94dbe4b4814d2ba2ec0b840c536de11091c19c6ec77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.16-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 a5e1cb77c1ea75c2f669fe69f50b2d36e8c4e86f2c835a3d5df60e12f16cdf20
MD5 f563150b0eaf2cec5cb97317235aa638
BLAKE2b-256 62b7141b5ae1771a2985b7effae7f4f7dee94e4a318157ee238bc1a68a4ea815

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.16-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.16-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 dca07c167b10d833d44a06e082a11e2a5bb2c28539921708bb862a7e5d7e33ac
MD5 88896d0d0e130b3642b237fad5ac4f17
BLAKE2b-256 52a6c9eb5f465d5ffb6803d38ad831d7904a94e511a376a8951eca5dcca53a44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.16-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bdb8968c1a9fc17ee57680a8df07bb4212ffd53cc574869f6abd65a10809912c
MD5 658757cb45c9aa30a1a52ac58098846d
BLAKE2b-256 8117e1ba31e2a47901ea893fd063299c5effd17c0093cdb5f8880376c70bae3b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.16-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 99455d061125a6b068ae5f5b56e13b698a8f0336cbb37ff2d0fe230a92b4d109
MD5 077a89ca3860a6b4140f0315fffba87a
BLAKE2b-256 293c0913066e69ba033af6c2c602d52420af044ea268d6fd2ef961587f549fb8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.16-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.16-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 b1f1e14feaa221c1282adc899494d0f21a9696c19943a4b19ae706b7d249ddcd
MD5 510dd1608b70e1c404bb31971aa222af
BLAKE2b-256 2d5786d0d241b5354c004a660ee6cc48758b78bc35504820e690a68abb0775cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.16-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1b4d90da9a13cf8bdb839d5f9a4ef91c84d58039a8b84691194bf0fd7c724bb0
MD5 a40c80d35cf712a3109e39d04150f28c
BLAKE2b-256 cb05de7829f6c0f0640a49aab73f484633927957cc078509627d45648f0796b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.16-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 119a9e4b3c86da3f6ad2d48346d86bb1ff3a83c4fd11a887a7a3c5e41abea2f3
MD5 9b7027b3b758dde80aa74b03a81f5b11
BLAKE2b-256 63e7be49dbbec081fc8878d736fffb1079eae841a5f1e24138fb0b86eb0d05bd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.16-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.16-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 9257d2414ebe4bc655530aaf267f951c810d812834dfb20476c4fc035fb31260
MD5 3ed0118965bc75e4b46e935ea6f542ad
BLAKE2b-256 fd83ae22b3f15fd720850be28a7b12afed7124683917993a5028f5f1b3ed184f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.16-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f0b054689b5fbd14a28a9cdb072c4eee9db651c0a43b075e846c88195521ce26
MD5 ca0f2f04236f5aafbd17fee2004afffc
BLAKE2b-256 7e82e35c878e9552104612e9fdebc57546eed195068078b3cd92490cc72bf1f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.16-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 3ca7cdf46478ebd38019c201a6bd51836baec3f9a434270565d86c9a57c42fa5
MD5 e1795a5aca1f87cfd6fdd115b524ee92
BLAKE2b-256 46c154e55bc8d12c860df3278f1f57ffc98387e97f2c229797836fee2faf15a8

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