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

Uploaded CPython 3.10 Windows x86-64

accera-1.2.11-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

accera-1.2.11-cp310-cp310-macosx_10_15_x86_64.whl (33.4 MB view details)

Uploaded CPython 3.10 macOS 10.15+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

accera-1.2.11-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.8 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

accera-1.2.11-cp39-cp39-macosx_10_15_x86_64.whl (33.4 MB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

accera-1.2.11-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.8 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

accera-1.2.11-cp38-cp38-macosx_10_15_x86_64.whl (33.4 MB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

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

Uploaded CPython 3.7m Windows x86-64

accera-1.2.11-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.8 MB view details)

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

accera-1.2.11-cp37-cp37m-macosx_10_15_x86_64.whl (33.4 MB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: accera-1.2.11-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.1 CPython/3.10.7

File hashes

Hashes for accera-1.2.11-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1c884254b1002151de71fb6984c84fcebb436e3fd2fe0ab14e8192070a6dfc4c
MD5 ee8cba664f48673c558cbca0a759856f
BLAKE2b-256 61ad6d494196ef04413a349238eafdb7f4e3b0c45c381b3cf3fa66f4f5745ee6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.11-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9cfd7e09399d582ec50f9dd1d807c10766956c240863bcee57adf98b303993b4
MD5 41c687e1a0e043102d2aea3d5a79ad27
BLAKE2b-256 84b02e575dc95130bde6111bfedaa020022b915787a14f126416adf78ed5f8ed

See more details on using hashes here.

File details

Details for the file accera-1.2.11-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.11-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 3584956ab2214694210cd4fa39559218bee861e1c1e95c7d3546b7457e1c1bbc
MD5 6beeb443bc63481c52b8868f1deed471
BLAKE2b-256 34ebb9979954ac3972ef0e7a2ecdc650ec899a9998f0fab1ab48479e06f9c354

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for accera-1.2.11-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 9bcd9baf760c83b76c1f0a2ebbc52163e84aaf25fac193eb4475c8c5f54d1f56
MD5 0af03914a631cb82c5d310e9862aa289
BLAKE2b-256 f2749e56a430cdafc89fe6f12df810dcacd78ad41988451e11afd6075e7ae94d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.11-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c59491d6e77c714dd376ee966165eb0a947de863e8b3240a59430e1e481dad9e
MD5 3ed506ec22df89f37f7a27bfc535fb70
BLAKE2b-256 779e68eb940b4784904f5cbc238530c8994f66d5684d40b673edb10bec8449e5

See more details on using hashes here.

File details

Details for the file accera-1.2.11-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for accera-1.2.11-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 90c267573001e08dd9ac178583e7944970c6a803a1c6d38d94a4a437946f7806
MD5 345b45f22e1b363023cbc6b4872a1c02
BLAKE2b-256 b8798cc45d2e4674e65fef71db26ae68366725e74b1c44a35e36e784be957cd3

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for accera-1.2.11-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 c36f9fcf73694475b416612e16f73d65bc70090fe53fcf209167997a84ba6f90
MD5 e6a4a906ac34883ca9aeef3f0f42dee9
BLAKE2b-256 db0cd9c1621c4cb4e88360cfdf2f5e8cd3441e14f98ec4df09803d7cbae9563a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.11-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0e73dc917865fac37e2458473e7649bb3c2933903d4e396c370ac3b05460c068
MD5 3944ec8a59442c4057ff84f0252a58f0
BLAKE2b-256 f71c050f21eb40ba42aa9de7496cb3dc16d3bdcf0505acc33088d0133a934129

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.11-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 2a96870d508b8d8e0858765c53ea27ad99d214b2a15914a90bb7bef184e87b9a
MD5 12aa0093222d592e414fdb25ff21d45a
BLAKE2b-256 e48c39e740784f4699f1eca559aa77c4174c5405882f32da3030d9802d7061c3

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for accera-1.2.11-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 af02103be7caad635e51d9975f563064868d5febff54f55b6359b5956943c85f
MD5 9fcb2856e0e78f6facbe074d8cad021d
BLAKE2b-256 ee3c0f81f0802214d108e259fc4653d58d8e02ca3a4f016fbd24abd89236b505

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.11-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5b21098fec13b53786f2605f66084c88a5e55374d65928d56ddb91b698c601db
MD5 35d00e6365b3c874f54826fb7c88b7b9
BLAKE2b-256 60f6622799f5341c0cff3af0a99a8450c3cb2a617455437ffa2e76c3455b006b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.11-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 41a31ce0f9cd4e680e3757918fd3f90d5969bfdeef9b831d2bab1c386c11c4fd
MD5 c08dcfd480b92ca073d53b88ac5795cc
BLAKE2b-256 24d91819a71cd035256c48d698397b214b101a4247807ef5d67017378632ff6f

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