Package: transpyle
Human-oriented and high-performing transpiler for Python.The main aim of transpyle is to let everyone who can code well enough in Python,
benefit from modern high-performing computer hardware without need to reimplement their application
in one of traditional efficient languages such as C or Fortran.ContentsFramework designFeaturesCommand-line interfaceAPI highlightsLanguage supportC to Python ASTPython AST to CC++ to Python ASTPython AST to C++Cython to Python ASTPython AST to CythonFortran to Python ASTPython AST to FortranOpenCL to Python ASTPython AST to OpenCLPython to Python ASTPython AST to PythonRequirementsInstallationpipDocker imageRelated publicationsFramework designFramework consists of mainly the following kinds of modules:parserabstract syntax tree (AST) generalizerunparsercompilerbinderAt least some of the modules are expected to be implemented for each language
supported by the framework.The modules are responsible for transforming the data between the following states:language-specific codelanguage-specific ASTextended Python ASTcompiled binaryPython interface for compiled binaryAnd thus:parser transforms language-specific code into language-specific ASTAST generalizer transforms language-specific AST into extended Python ASTunparser transforms extended Python AST into language-specific codecompiler transforms language-specific code into compiled binarybinder transforms compiled binary into Python interface for compiled binaryThe intermediate meeting point which effectively allows code to actually be transpiled between
languages, is the extended Python AST.FeaturesUsing Python AST as the intermediate representation, enables the AST to be directly manipulated,
and certain performance-oriented transformations can be applied. Current transpiler implementation
aims at:inlining selected callsdecorating selected loops with compiler-extension pragmasMore optimizations will be introduced in the future.Some (if not all) of the above optimizations may have very limited (if not no) performance impact
in Python, however when C, C++ or Fortran code is generated, the performance gains can be
much greater.Command-line interfaceThe command-line interface (CLI) of transpyle allows one to translate source code files
in supported languages.API highlightsThe API of transpyle allows using it to make your Python code faster.The most notable part of the API is thetranspiledecorator, which in it’s most basic form
is not very different from Numba’sjitdecorator.importtranspyle@transpyle.transpile('Fortran')defmy_function(a:int,b:int)->int:returna+bAdditionally, you can use each of the modules of the transpiler individually, therefore transpyle
can support any transformation sequence you are able to express:importpathlibimporttranspylepath=pathlib.Path('my_script.py')code_reader=transpyle.CodeReader()code=code_reader.read_file(path)from_language=transpyle.Language.find('Python 3.6')to_language=transpyle.Language.find('Fortran 95')translator=transpyle.AutoTranslator(from_language,to_language)fortran_code=translator.translate(code,path)print(fortran_code)As transpyle is under heavy development, the API might change significantly between versions.Language supportTranspyle intends to support selected subsets of: C, C++, Cython, Fortran, OpenCL and Python.For each language pair and direction of translation, the set of supported features may differ.C to Python ASTC-specific AST is created via pycparse, and some of elementary C syntax is transformed into
Python AST.Python AST to CNot implemented yet.C++ to Python ASTParsing declarations, but not definitions (i.e. function signature, not body). And only selected
subset of basic types and basic syntax is supported.Python AST to C++Only very basic syntax is supported currently.Cython to Python ASTNot implemented yet.Python AST to CythonNot implemented yet.Fortran to Python ASTFortran-specific AST is created via Open Fortran Parser, then that AST is translated
into Python AST.Python AST to FortranCurrently, the Fortran unparser uses special attributefortran_metadataattached
to selected Python AST nodes, and therefore unparsing raw Python AST created directly from ordinary
Python file might not work as expected.The above behaviour will change in the future.OpenCL to Python ASTNot implemented yet.Python AST to OpenCLNot implemented yet.Python to Python ASTPython 3.6 with whole-line comments outside expressions is fully supported.
Presence of end-of-line comments or comments in expressions might result in errors.Python AST to PythonPython 3.6 with whole-line comments outside expressions is fully supported.
Presence of end-of-line comments or comments in expressions might result in errors.RequirementsPython 3.5 or later.Python libraries as specified inrequirements.txt.Building and running tests additionally requires packages listed indev_requirements.txt.Support for transpilation from/to specific language requires additional Python packages
specified inextras_requirements.json, which can be installed using the pip extras
installation formulapip3installtranspyle[extras]where thoseextrascan be one or more of the following:All supported languages:allC:cC++:cppCython:cythonFortran:fortranOpenCL:openclTherefore to enable support for all languages, executepip3installtranspyle[all].
Alternatively, to enable support for C++ and Fortran only, executepip3installtranspyle[cpp,fortran].Additionally, full support for some languages requires the following software to be installed:C++:a modern C++ compiler – fully tested with GNU’sg++versions 7 and 8
and partially tested with LLVM’sclang++version 7SWIG (Simplified Wrapper and Interface Generator) – tested with version 3Fortran:a modern Fortran compiler – fully tested with GNU’sgfortranversions 7 and 8
and partially tested with PGI’spgfortranversion 2018The core functionality of transpyle is platform-independent. However, as support of some languages
depends on presence of additional software, some functionality might be limited/unavailable
on selected platforms.Transpyle is fully tested on Linux, and partially tested on OS X and Windows.Installationpippip3installtranspyle[all]Docker imageThere is a docker image prepared so that you can easily try the transpiler.First, download and run the docker container (migth require sudo):dockerpull"mbdevpl/transpyle"dockerrun-htransmachine-it"mbdevpl/transpyle"By default, this will download latest more or less stable development build,
if you wish to use a specific release, use"mbdevpl/transpyle:version"instead.Then, in the container:python3-mjupyternotebook--ip="$(hostname-i)"--port=8080Open the shown link in your host’s web browser, navigate toexamples.ipynb,
and start transpiling!Related publicationsBelow is the list of papers describing various aspects of transpyle and/or principles behind it.
Further research is ongoing, so the list might be extended in the future.M. Bysiek, A. Drozd and S. Matsuoka,Migrating Legacy Fortran to Python While Retaining Fortran-Level Performance
Through Transpilation and Type Hints,
PyHPC 2016: 6th Workshop on Python for High-Performance and Scientific Computing @ SC16,
Salt Lake City, Utah, United States of America, 2016, pp. 9-18Abstract:We propose a method of accelerating Python code by just-in-time compilation leveraging type
hints mechanism introduced in Python 3.5. In our approach performance-critical kernels are
expected to be written as if Python was a strictly typed language, however without the need
to extend Python syntax. This approach can be applied to any Python application, however we
focus on a special case when legacy Fortran applications are automatically translated into
Python for easier maintenance. We developed a framework implementing two-way transpilation
and achieved performance equivalent to that of Python manually translated to Fortran, and
better than using other currently available JIT alternatives (up to 5x times faster than
Numba in some experiments).https://doi.org/10.1109/PyHPC.2016.006M. Bysiek, M. Wahib, A. Drozd and S. Matsuoka,Towards Portable High Performance in Python: Transpilation, High-Level IR,
Code Transformations and Compiler Directives (Unreferred Workshop Manuscript),
2018-HPC-165: 研究報告ハイパフォーマンスコンピューティング,
Kumamoto, Kumamoto, Japan, 2018, pp. 1-7Abstract:We present a method for accelerating the execution of Python programs. We rely on
just-in-time automatic code translation and compilation with Python itself being used as a
high-level intermediate representation. We also employ performance-oriented code
transformations and compiler directives to achieve high performance portability while
enabling end users to keep their codebase in pure Python. To evaluate our method, we
implement an open-source transpilation framework with an easy-to-use interface that
achieves performance better than state-of-the-art methods for accelerating Python.http://id.nii.ac.jp/1001/00190591/