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

Uploaded CPython 3.10 Windows x86-64

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

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.7m Windows x86-64

accera-1.2.9-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.9-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.9-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.9-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.9-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 baf3df68a1666970e9ed27808d9141f9468bca6ae2b0d1ca56968c7c7b78ba2b
MD5 722700a762f072b95a3a0deb3e29f5ff
BLAKE2b-256 32b3dcf8e2055e587daf833cbe01336ebdb46409c1101f2c06b80f78ea6b750a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 207cdc2206d1f4a10c49d2feb45b0cc202c4e9c1889dd8bcbf42e9052a3a8b9a
MD5 15c945dc57de0fcdb36a3201dee7ab7a
BLAKE2b-256 7cf1bbf4d9fefc1255756bdb50303b08e22db31ca97a568a11eff785ded1aecd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.9-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 d9476386217359c2d39991daff579a08acf709127f9d564d0e72adb70a5c31a3
MD5 8101d6fb87301cc60abedfedca49bafe
BLAKE2b-256 7e1f44132992ff86212583b4db5302c415215d16ecab4e710b0fe170bd417274

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.9-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.9-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 8b92ac75c8d6d5c0b2851d8f11e90934ecf40a109ea566e8a2a4e42c41dea6ab
MD5 f2d6ff0298b285fdc544fd0866838a4c
BLAKE2b-256 3afb7c74f2efc660ae0205f5800f1712b3e970a2b6e1a427e909023302973bea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c60ed4137c00e3e480caf917f4b9e5425e8b1ca05836d04c2b5362cff93e853e
MD5 d37f35dfdf7e5445606323886ee8bd43
BLAKE2b-256 0fbd03dd4ecbe55c0b4c51ccd87fb35254ce2165e9d18c60588c2cc0e2430124

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.9-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 e9402a8f08b7d49436d1670303872739ff9c6a914d3819be5fd467d8fea6a819
MD5 873f220a6d5b8a8b745b445f8d3f6ccd
BLAKE2b-256 b66e42a7a738f3db9e7aa92f9925f850084bc63aad6b749020dc06a2bde41916

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.9-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.9-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 6942fea71108dd9dfda0f8d88edba4e396509a042994f30609a833d9c49ad491
MD5 d55e1a76f68186a51b2668f029f2eae0
BLAKE2b-256 b8612383ce654b3ce18dcd7afad03b7baba4578fe19c43f997e4f91051ef2a19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1539c06da0f67594359dd2ae1eb7b067c4e9d3a7d2f580dc716fa743f7181938
MD5 7e528917189742b486abd0de1dc919eb
BLAKE2b-256 06ed841f4f8395d8ff9facbfd7a74df2dfe27d4fbdfa68eeea8b76a0daf28864

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.9-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 c8ee01ee881e12493db9d65a98035da8b9f56a83efb6c6e0f97a1947b485f520
MD5 e0ee6d4253a9ffb93de8a8354e9500c6
BLAKE2b-256 04cdc3a1fd3793aa17e1c66abda1678179e5b36537541ec699fb8dc765c2bbb3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.9-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.9-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 3e1b6191a1e94d39022838039971b69ba3bf788ed1296d0ce7e900a047b52715
MD5 a7b01e08d813667c5b57adb188156b86
BLAKE2b-256 5feedcc9a5ab38787052d3ed946ff7aa47277244b4987fadd37f9765e9df6236

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.9-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ff2552870fa7749bbc5a6df0acfcfa128faca11e1c4ece1d6768e37cf8acb566
MD5 cb648926b31c9c2b918ed80af06e69b4
BLAKE2b-256 ab60bbee0bbcbb776ae13f75b6c8304b6dc872894b85e8a8de51ba4495fc45e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.9-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 e5a67d59b809aefa056c2c2413f9ec9eae353451e0d3ca378b78fa6062b510e8
MD5 eda6294a42bd3eebd4a1c9400a0ab07d
BLAKE2b-256 1c9215b42c8c9e7752b813c6d5b01640a11c76830377041698f32fb8fc01ebdc

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