on 23 Jul 2018 2:21 PM

In this message we introduce and illustrate how to use APYFAL, which is an abstraction layer to easily operate ready-to-use accelerated functions that are available on AccelStore and that are deployed in a public, private or hybrid Cloud environment.


1. What is APYFAL

APYFAL is an open source Python library that provides a REST API (REpresentational State Transfer Application Programming Interface) to operate functions that are accelerated using FPGA devices. This API allows Cloud application developers to operate accelerated functions that are deployed in public cloud infrastructures such as on AWS EC2 F1 instances and OVH Public Cloud instances, and on premise.

A remote execution with the APYFAL REST API allows multiple instances to speak to multiple accelerators in parallel, while a local execution on an cloud instance is also possible for tighter integration. These 2 scenarios are illustrated below. Running APYFAL locally on a Cloud instance allows to minimize the impact of the network bandwidth while reducing the end-to-end processing latency.


2. How to operate accelerator functions using APYFAL

APYFAL allows Cloud application developers to control the accelerated processing of files that are stored on a machine, on a cloud storage bucket (AWS S3, OpenStack Swift…), or on public Internet (using a HTTP URL).

APYFAL mainly consists of the Accelerator class that represents the Accelerator and its host (the FPGA cloud instance). Application developers use three simple methods to operate any accelerated function:

1. Accelerator.start

This method handles the Accelerator configuration:

2. Accelerator.process

This method is the core of APYFAL. It runs the accelerated function and returns the result.

3. Accelerator.stop

This method terminates the Cloud instance according to a set of predefined rules that you can select.


3. How to use APYFAL: examples

3.1 Controlling the accelerated compression of a file stored in a bucket

The following example shows how to use the gzip compression accelerator developed by CAST to boost the compression of a file stored in a AWS S3 bucket. Files are compressed up to 25x faster than CPU compression when using simple APYFAL methods to operate the gzip compression accelerator.

import os
import apyfal

with apyfal.Accelerator(accelerator='cast_gzip') as myaccel:         # instantiate and configure accelerator
   # the with statement helps to terminate the accelerator and instance once finished by calling transparently "stop"
   myaccel.start()                                                                                   # start and configure accelerator

   myaccel.process( file_in='s3://my_bucket/data/huge_file.txt',     # compress a file stored in a bucket on AWS S3
       file_out='s3://my_bucket/data_compressed/huge_file.gzip')    # store compressed file in the same bucket


3.2 Controlling the accelerated compression of all files in a directory

The following example illustrates how to use APYFAL to control the execution of an FPGA accelerator to compress all files in a directory.

import os
import apyfal

with apyfal.Accelerator(accelerator='cast_gzip') as myaccel:                           # instantiate and configure accelerator 
   myaccel.start()                                                                                                     # start accelerator

   os.makedirs('compressed', exist_ok=True)
   for input_file in os.listdir('.'):
       if os.path.isfile(input_file):
           compressed_file = os.path.join( 'compressed', f'{os.path.splitext(input_file)[0]}.gzip')
           myaccel.process(file_in=input_file, file_out=compressed_file)             # compress all files in a directory


3.3 Replacing a Python function with an equivalent accelerated function

Python is a powerful language that allows software developers to replace existing functions with their equivalent FPGA accelerated function. Here we illustrate how to patch Python Pseudo Random Number Generator (PRNG) with the TRNG accelerator (True Random Number Generator) developed by Secure-IC so as improve cryptographic applications. This accelerator generates true random numbers (based on electronic noise, which is better than any software-based random number generator) while significantly decreasing random number generation time.

When using Python to develop cryptographic functions, developers often rely on the os.urandom function to generate Pseudo random numbers. Patching this function with the TRNG accelerator improves the quality of generated random numbers. This also applies to any function that calls os.urandom under the hood, such as in the Python secrets library and cryptography package. The following lines of code illustrate how to easily leverage the TRNG accelerator when patching the os.urandom function using APYFAL. Application developers only need a few minutes to integrate the APYFAL library and accelerate functions.

import io
import os
import apyfal

with apyfal.Accelerator(accelerator='secureic_trng') as trng:   # Instantiate and configure accelerator

   def trng_urandom(size):                                                   # Redefines os.urandom to use TRNG instead of system PRNG 
       buffer = io.BytesIO()                                                     # Use a buffer to avoid a temporary file
       trng.process(file_out=buffer, nbBytes=size)           # The process function generates 'size' randoms bytes
       buffer.seek(0)                                                               # Gets bytes in buffer as result
       return buffer.read()

   os.urandom = trng_urandom                                                               
   # The patched function will now be used automatically on "os.urandom" calls.
   # All functions that call it will use TRNG under the hood instead of PRNG.

   # Example using the Python standard library "secrets" module. TRNG can be used to generate a random URL safe token

   import secrets
   token = secrets.token_urlsafe(nbytes=32)

   # Example using the "cryptography" package.
   # TRNG is used to generate key and AES initialization vector, to improve the whole encryption process

   from cryptography.fernet import Fernet
   encryption_key = Fernet.generate_key()
   fernet = Fernet(encryption_key)
   encrypted_data = fernet.encrypt(b"my_secret_data")


The same approach can be applied to accelerate and improve the generation of random numbers in gaming/lottery, banking/financial applications.


3.4 Operating a pool of accelerated functions

The following example illustrates how to use APYFAL to orchestrate a pool 1,000 accelerated functions in order to boost search and replace operations in a set of 1,000 large files (e.g. log files, DNA sequences...). We use the Ultra Fast Search & Replace for Data Analytics accelerator, which has been developed by Axonerve to boost search and replace operations in ascii files more than 1000x faster than a sed (stream editor) command. In this case, all the accelerators in the pool are configured to perform the same search and replace operations on different files. This is achieved by configuring all the accelerators with the same corpus file. Each accelerator function (each FPGA instance) in the pool is then used to process a single large file.

from concurrent.futures import ThreadPoolExecutor
import os
import apyfal

def start_and_process(file_in, file_out):
     with apyfal.Accelerator(accelerator='axonerve_hyperfire') as myaccel:  # Instantiate and configure accelerator
          myaccel.start(datafile="corpus.csv")                                            # Start accelerator and specify corpus
          myaccel.process(file_in=file_in, file_out=file_out)                       # Process file

# "input_files" is a list of 1,000 large files to process. "output_files"  is a list of 1,000 output files.
with ThreadPoolExecutor(max_workers=len(input_files)) as executor:      # Run "start_and_process" in parallel
      executor.map(start_and_process, input_files, output_files)                  # Create a pool of 1,000 accelerators
# Each input file is processed with a dedicated accelerator


4. Conclusion

The open source APYFAL library provides a powerful abstraction layer to leverage the power of accelerated functions in your Cloud context. APYFAL operates the accelerated functions that are available on AccelStore, and that are currently deployed on AWS F1 instances and OVH FPGA instances.

The APYFAL library is easy to use and brings tremendous acceleration benefits to a large number of Cloud Application developers in many spaces such as Video Transcoding, Imaging , Security, Compression, Big Data Analytics, AI, Genomics, etc...

APYFAL is also available for deployment on on-premise servers, and testing it through AccelStore only takes a few minutes, so anyone interested in leveraging it should contact Accelize to get more details.