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

Uploaded CPython 3.10 Windows x86-64

accera-1.2.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

accera-1.2.8-cp310-cp310-macosx_10_15_x86_64.whl (33.5 MB view details)

Uploaded CPython 3.10 macOS 10.15+ x86-64

accera-1.2.8-cp39-cp39-win_amd64.whl (24.1 MB view details)

Uploaded CPython 3.9 Windows x86-64

accera-1.2.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.7 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

accera-1.2.8-cp39-cp39-macosx_10_15_x86_64.whl (33.5 MB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

accera-1.2.8-cp38-cp38-win_amd64.whl (24.1 MB view details)

Uploaded CPython 3.8 Windows x86-64

accera-1.2.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.7 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

accera-1.2.8-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.8-cp37-cp37m-win_amd64.whl (24.1 MB view details)

Uploaded CPython 3.7m Windows x86-64

accera-1.2.8-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.7 MB view details)

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

accera-1.2.8-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.8-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.8-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 24.1 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for accera-1.2.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5418a77395f970bff11d8882069f2fccf492150347f5fab412b0551956ce4887
MD5 59a8dc17c25ef869c30e078bb171e9ab
BLAKE2b-256 568ace1e859bbecf03972313e9afc921f09828d923cffa1d9756c3e4068bbdd2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b8d7a0562b122559287d2997ff98d368404f5052ebb6a573e1eb216bb2e2fd9b
MD5 1f3543b78cc6c79d97a872a5a98b41c3
BLAKE2b-256 89bbc62bcef01931b5e445a8fc2991d6b71b77ec820af0dbccbb893d264e62de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.8-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 a3ee729d80facd268f381c2570c49a6476caab0277fbae1891685641fbf87465
MD5 5360b7f36793ad5c70c0de29fc2ddf4c
BLAKE2b-256 27df03583a52dd2d8512b536eaeec66218e0cc89dffe2385098f0a3a08af3ed0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.8-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 24.1 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.8-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3039813cab5c06e54e025a4869e98a171237b4a3a39c005c1489adeccb6a305a
MD5 3845098394e602b03e573c5fee25d7bb
BLAKE2b-256 fe488a4629bd123a5ec496085d9d979cf60e5c5e4ebfa445a5a3b21699ea4085

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ee8a3b25a3d2117bfacb3b49d0ea1024243ef61dbd109219845038c0837edc55
MD5 6adf6a5a254b2c11052e318b7e4a8523
BLAKE2b-256 09ae32203f27a4a2aa10b91c033aa6b3690e160b503efc7ee8934a8387a2dbc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.8-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 5d4aa39f42ef6034d7b8fbd8ac238736ccbc7e55277fc0af2948d053c9a7718e
MD5 7637a4743cea488d685691564e172df9
BLAKE2b-256 8026c0c733f057f792e5d037b5dd5b3cb7f2041529ecee295ae976694280d2c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.8-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 24.1 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.8-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 06d1ea557f15df7ac673056f0de0c2ee8fe166bb58591f2260637c9cf36e275f
MD5 59f05297f42bef9f47a477e95748c1af
BLAKE2b-256 edd8800551fb4f98bf178b7fc1d7b5563cafe2ea052509f90af5260559200f5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e07d7097d857c6ae6e76551644f967368d9ab331ef9ab896a1f2d74688d86d1b
MD5 a153672c718f643c0ead5409c14d48b8
BLAKE2b-256 f8ebc8270e46873135ad73159610ed7b3e616d21b3a00ce95577e39373db76c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.8-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 a6a1cf1eeb1b58f00b3693c9c32b1c65c6a3a0e5ac9cb26fdeb374750079ee7f
MD5 61ec2b0512d9b450a0d0d8d453ff354d
BLAKE2b-256 5dbaa03c5c8e8a8ccade451885c105d15187323e3dcf214dfcf4033423e89f96

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.8-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 24.1 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.8-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 71ea3519755eb14b85ad04462624b32462edd19f882c4d54d35119c70481f003
MD5 944eb6c08ee761f99e7ca7b72256d667
BLAKE2b-256 7c1c7030a1a430e8fa17b7d21c29590b896e7554006338bf9b0ca819beec8d2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.8-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 91640c6b4b8c1cffc4a7698a4e9cfa0e6c6dc99a26f6adc213c7de0243e51e9f
MD5 01ffbb6caef1353450e9f88dd892c786
BLAKE2b-256 f23b91b2d64d452813781bf4ace7db4d1446df85f352bed52b981086941c805d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.8-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 8ab8566e931f01e7c12b965b957037a8c42b8a6f27261d1093f454e3c7147001
MD5 da7f1cb6a80389740d01266eb373fcf4
BLAKE2b-256 9b555024c68158a8136cf60170357b161d73adfb59695b384408997863f24871

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