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

Uploaded CPython 3.10 Windows x86-64

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

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.7m Windows x86-64

accera-1.2.27-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.27-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.27-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: accera-1.2.27-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.27-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d0e2f22af096fad332703bbc5f1acb1b49cd136413d429583470671f36c09add
MD5 992ab99a821d6687ee059c19bcac2a51
BLAKE2b-256 c4031f0a5679cb2f64a07b9f30e32fa063441882e8f2e4f7f146902c5023ec11

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.27-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 25e18eb571be31c3b2b5470e9a63e87e8950e8c3e2b919fd3032e2fd2808d8a3
MD5 1cd071b5e53f7e646a2cccccd03a2316
BLAKE2b-256 3fb4081f743eab48887a366c028b848fb636a83a6f6cfb0ba7e0bd18668ee855

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.27-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 06513e11775b7a38963ccac418d072423facbedd118232cc2c726a671e54b30d
MD5 ca10d8aa3e46886a94a6c488e44c4e5a
BLAKE2b-256 3e6f78de22d3ce0f5a660e6efdbfd12e964cc99066cc4019dc941a6a52d56237

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.27-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.27-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 58d8a293472401788109772963aea19fb83758d96fbcb6d945807d41e1163cad
MD5 d9256be0f8af5a15a5910f0c17df9eec
BLAKE2b-256 60b858951b15e64dc3908af473bc58328cad24205a1e2cc76ee4abedc0427508

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.27-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 08197820f194318accc2da07825928144e999c418845b9706ddaaafac6ad11ae
MD5 9f37b0b8aec7dddfc88b7086f16d706a
BLAKE2b-256 fbcc9f188e6309eea29d749c01e3a730b125919b2d315ff583117883c060cea8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.27-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 6dbe44ac0ca0f74ecf6f23ddfdaa10ec2074230dab3c2bfd57bb4c770e64dce2
MD5 966b4e60dbd54d79e24a57d75dd2c575
BLAKE2b-256 83868c7aa81865316a2ea6b72930cfca2d5e986be25da72229d6e2b8a0a3ca4a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.27-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.27-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 d12b2f5864650e2bd77a368b134d4ad73fa6a68b0ae5a67dccbe3bbe171a052d
MD5 1e033f8e872bb5563098239b57022e15
BLAKE2b-256 aeb2f14b2bb330b3e91341f73f7d3d33aa60e5d58893e400576733f6773dd83e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.27-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9896afcf817847cedd59a7e3ac3d045c0109aa05e1afbecd04b5f7c3da7c8a45
MD5 ee0111c3b6be74f8eb212beaa066a0ba
BLAKE2b-256 bcab39c5d1beb7a36747feae576c6cc63802cb8ebe52026751ddfc4e04525e3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.27-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 ae40d0d40be8cbf067a9785ffcfe1501aae0bb23a43db04820f66577e65cd460
MD5 e29b0b76052df1d844d89cb87de455dd
BLAKE2b-256 9188b5224ab41dd5a63dcd6e026d3285bc78e44c836880eed6d0404cea3af36e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: accera-1.2.27-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.27-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 7da0ee7af6016d80339988f5e0d7401a35fab5a0e55db3272e103aeb5e41afa9
MD5 fb702f1147e368c8c99655751dcfd626
BLAKE2b-256 74343e9578635b1e3f53036dcbf8ca54c9b0196fe3eea3f1fa81b406c9a8768a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.27-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c2d975bbab6060626f748597a7464b1b5bd8549c8304caf2baf8e30dbde583c2
MD5 472159f356a7745bc58c3cfda1be4548
BLAKE2b-256 3149c37708a070cdd4d561f1aa63e8c5dc206a6676e18e778f0fb20beadf654d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for accera-1.2.27-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 d64889693f4ba1021996c544d8c29bffad07e6abb78db98fdc8da8226761d107
MD5 50af3350c1e3263e496cdd88196f7871
BLAKE2b-256 2aa5dae28a394fba0f94903b6c76e641a8b134263b7ce688f0ff7dba99de8dc6

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