• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fully compatible CPython jit compiler. Optimising Dynamic, Interpreted, and Object-oriented(DIO) programs.

DIO-JIT: General-purpose Python JIT

中文README PyPI version shields.io JIT

Important:

  1. DIO-JIT now works for Python >= 3.8. We heavily rely on the LOAD_METHOD bytecode instruction.
  2. DIO-JIT is not production-ready. a large number of specialisation rules are required to make DIO-JIT batteries-included.
  3. This document is mainly provided for prospective developers. Users are not required to write any specialisation rules, which means that users need to learn nothing but @jit.jit and jit.spec_call.

Benchmark

Item PY38 JIT PY38 PY39 JIT PY39
BF 265.74 134.23 244.50 140.34
append3 23.94 10.70 22.29 11.21
DNA READ 16.96 14.82 15.03 14.38
fib(15) 11.63 1.54 10.41 1.51
hypot(str, str) 6.19 3.87 6.53 4.29
selectsort 46.95 33.88 38.71 29.49
trans 24.22 7.79 23.23 7.71

The benchmark item "DNA READ" does not show a significant performance gain, this is because "DNA READ" heavily uses bytearray and bytes, whose specialised C-APIs are not exposed. In this case, although the JIT can infer the types, we have to fall back to CPython's default behaviour, or even worse: after all, the interpreter can access internal things, while we cannot.

P.S: DIO-JIT can do very powerful partial evaluation, which is disabled in default but you can leverage it in your domain specific tasks. Here is an example of achieving 500x speed up against pure Python: fibs.py

Install Instructions

Step 1: Install Julia as an in-process native code compiler for DIO-JIT

There are several options for you to install Julia:

$ pip install jill && jill install 1.6 --upstream Official
  • jill (Mac and Linux only!):
$ bash -ci "$(curl -fsSL https://raw.githubusercontent.com/abelsiqueira/jill/master/jill.sh)"

Step 2: Install DIO.jl in Julia

Type julia and open the REPL, then

julia>
# press ]
pkg> add https://github.com/thautwarm/DIO.jl
# press backspace
julia> using DIO # precompile

Step 3: Install Python Package

$ pip install git+https://github.com/thautwarm/diojit

How to fetch latest DIO-JIT?(if you have installed DIO)

$ pip install -U diojit
$ julia -e "using Pkg; Pkg.update(string(:DIO));using DIO"

Usage from Python side is quite similar to that from Numba.

import diojit
from math import sqrt
# eagerjit: assuming all global references are fixed
@diojit.eagerjit
def fib(a):
    if a <= 2:
        return 1
    return fib(a + -1) + fib(a + -2)

jit_fib = diojit.spec_call(fib, diojit.oftype(int), diojit.oftype(int))
jit_fib(15) # 600% faster than pure python

It might look strange to you that we use a + -1 and a + -2 here.

Clever observation! And that's the point!

DIO-JIT relies on specilisation rules. We have written one for additions, more specifically, operator.__add__: specilisation for operator.__add__.

However, due to the bandwidth limitation, rules for operator.__sub__ is not implemented yet.

(P.S: why operator.__add__.)

Although specilisation is common in the scope of optimisation, unlike many other JIT attempts, DIO-JIT doesn't need to hard encode rules at compiler level. The DIO-JIT compiler implements the skeleton of abstract interpretation, but concrete rules for specialisation and other inferences can be added within Python itself in an extensible way!

See an example below.

Contribution Example: Add a specialisation rule for list.append

  1. Python Side:
import diojit as jit
import timeit
jit.create_shape(list, oop=True)
@jit.register(list, attr="append")
def list_append_analysis(self: jit.Judge, *args: jit.AbsVal):
    if len(args) != 2:
        # rollback to CPython's default code
        return NotImplemented
    lst, elt = args

    return jit.CallSpec(
        instance=None,  # return value is not static
        e_call=jit.S(jit.intrinsic("PyList_Append"))(lst, elt),
        possibly_return_types=tuple({jit.S(type(None))}),
    )

jit.intrinsic("PyList_Append") mentioned in above code means the intrinsic provided by the Julia codegen backend. Usually it's calling a CPython C API, but sometimes may not.

No matter if it is an existing CPython C API, we can implement intrinsics in Julia.

You can either do step 2) at Python side. It might looks more intuitive.

import diojit as jit
from diojit.runtime.julia_rt import jl_eval
jl_implemented_intrinsic = """
function PyList_Append(lst::Ptr, elt::PyPtr)
    if ccall(PyAPI.PyList_Append, Cint, (PyPtr, PyPtr), lst, elt) == -1
        return Py_NULL
    end
    nothing # automatically maps to a Python None
end
DIO.DIO_ExceptCode(::typeof(PyList_Append)) != Py_NULL
"""
jl_eval(jl_implemented_intrinsic)

You immediately get a >100% time speed up:

@jit.jit
def append3(xs, x):
    xs.append(x)
    xs.append(x)
    xs.append(x)

jit_append3 = jit.spec_call(append3, jit.oftype(list), jit.Top) # 'Top' means 'Any'
xs = [1]
jit_append3(xs, 3)

print("test jit_append3, [1] append 3 for 3 times:", xs)
# test jit func, [1] append 3 for 3 times: [1, 3, 3, 3]

xs = []
%timeit append3(xs, 1)
# 293 ns ± 26.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

xs = []
%timeit jit_append3(xs, 1)
# 142 ns ± 14.9 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Why Julia?

We don't want to maintain a C compiler, and calling gcc or others will introduce cross-process IO, which is slow. We prefer compiling JITed code with LLVM, and Julia is quite a killer tool for this use case.

Current Limitations

  1. Support for *varargs and **kwargs are not ready: we do can immediately support them with very tiny JIT performance gain, but considering backward compatibility we decide not to do this.

  2. Exception handling is not yet supported inside JIT functions.

    Why?

    We haven't implemented the translation from exception handling bytecode to untyped DIO IR (jit.absint.abs.In_Stmt).

    Will support?

    Yes.

    In fact, now a callsite in any JIT function can raise an exception. It will not be handled by JIT functions, instead, it is lifted up to the root call, which is a pure Python call.

    Exception handling will be supported when we have efforts on translating CPython bytecode about exception handling into untyped DIO IR (jit.absint.abs.In_Stmt).

    P.S: This will be finished simultaneously with the support for for loop.

  3. Support for for loop is missing.

    Why?

    Firstly, in CPython, for loop relies on exception handling, which is not supported yet.

    Secondly, we're considering a fast path for for loop, maybe proposing a __citer__ protocol for faster iteration for JIT functions, which requires communications with Python developers.

    Will support?

    Yes.

    This will be finished simultaneously with support for exception handling (faster for loop might come later).

  4. Closure support is missing.

    Why?

    In imperative languages, closures use cell structures to achieve mutable free/cell variables.

    However, a writable cell makes it hard to optimise in a dynamic language.

    We recommend using types.MethodType to create immutable closures,which can be highly optimised in DIO-JIT(near future).

    import types
    def f(freevars, z):
            x, y = freevars
            return x + y + z
    
    def hof(x, y):
        return types.MethodType(f, (x, y))

    Will support?

    Still yes. However, don't expect much about the performance gain for Python's vanilla closures.

  5. Specifying fixed global references(@diojit.jit(fixed_references=['isinstance', 'str', ...]) too annoying?

    Sorry, you have to. We are thinking about the possibility about automatic JIT covering all existing CPython code, but the biggest impediment is the volatile global variables.

    You might use @eagerjit, and in this case you'd be cautious in making global variables unchangeable.

    Possibility?

    Recently we found CPython's newly(:)) added feature Dict.ma_version_tag might be used to automatically notifying JITed functions to re-compile when the global references change.

    More research is required.

Contributions

  1. Add more prescribed specialisation rules at jit.absint.prescr.
  2. TODO

Benchmarks

Check benchmarks directory.

More Repositories

1

MLStyle.jl

Julia functional programming infrastructures and metaprogramming facilities
Julia
404
star
2

moshmosh

An amazing syntax extension system in pure Python, the way to coding efficiency.
Python
108
star
3

restrain-jit

The first and yet the only CPython compatible Python JIT, over the world.(julia backend: https://github.com/thautwarm/RestrainJIT.jl)
Python
107
star
4

flowpython

tasty feature extensions for python3(NO MAINTENANCE!).
C
65
star
5

EBNFParser

Convenient parser generator for Python(check out https://github.com/thautwarm/RBNF for an advanced version).
Python
63
star
6

Typed-BNF

Statically typed BNF with semantic actions; safe parser generator applicable to every programming language.
TypeScript
59
star
7

CanonicalTraits.jl

Full-featured traits in Julia. Without full features how dare I say this?
Julia
59
star
8

Traffy.UnityPython

An efficient Python implementation in C#, running on every platform via Unity IL2CPP.
C#
44
star
9

FSTan

Higher abstraction infrastructures in F#(ad-hoc polymorphism, subtypeclassing, monad, hkt...), exactly what we've dreamed about for so long
F#
43
star
10

RBNF.jl

A DSL for modern parsing
Julia
40
star
11

RBNF

This project's lifetime has ended. The successor is https://github.com/thautwarm/frontend-for-free which is WIP. You can check lark-parser project which is a good alt.
Python
39
star
12

frontend-for-free

end the parsing problem
Python
39
star
13

Virtual.jl

Julia
38
star
14

graphviz-artist

A chance to focus on graph drawing itself, forget APIs and other stuffs.
Python
37
star
15

idris-python

Successor project: https://github.com/thautwarm/Quick-Backend
Python
25
star
16

idris-cam

Sucessor: https://github.com/thautwarm/Quick-Backend
Idris
24
star
17

Rem

(Deprecated)Rem Programming Language: a playful dynamic language with all modern syntax sugars.
Python
24
star
18

DIO.jl

Julia implementation for Python Restrain JIT
Julia
22
star
19

LLAST

A high level LLVM IR AST provider for GraphEngine JIT.
F#
22
star
20

Quick-Backend

Idris, make back end, in 15 minutes, reusable, concise: https://bitbucket.org/thautwarm/ppl2020-idris-codegen-backend/src/master
TeX
22
star
21

gkdhighlight

syntax highligher for GkdTeX
TeX
21
star
22

PrettyPrint.jl

pretty print that makes sense
Julia
20
star
23

DevOnly.jl

Using runtime-free macro packages as dev-only dependencies.
Julia
20
star
24

MLStyle-Playground

Examples for MLStyle.jl
Julia
19
star
25

DianaScript-JIT

C#
19
star
26

RSolve

Ask for solutions.
Haskell
19
star
27

MLFS.jl

MLFS type system: raising ML to the power of system F in a Simplest way
Python
19
star
28

JuliaPythonAdaptor.jl

Relocatable Julia-Python bidirectional bridging solution.
Python
17
star
29

simple-pl-example

Implementing a programming language within 20 minutes, also a tutorial for syntax driven parsing with frontend-for-free.
Python
16
star
30

PySExpr

S-expressions in Python. Python cross-version compiler. Clean and efficient LISP back end.
Python
16
star
31

MatchCore.jl

non-extensible/hardcoded pattern matching, core of MLStyle
Julia
16
star
32

Fable.Sedlex

get you an ultimate lexer generator using Fable; port OCaml sedlex to FSharp, Python and more!
JavaScript
15
star
33

cross-editor-syntax-highlighter

cross-editor syntax highlighter for Lua, showing some merit of Typed BNF: https://github.com/thautwarm/typed-bnf
Python
14
star
34

Sequent.jl

formally and easily, describe the semantics.
Julia
13
star
35

UPL

A compiler for higher rank ML with type classes
F#
13
star
36

goto.py

modern implementation of real goto/label statements in python 3.5+.
Python
12
star
37

vscode-diana

a VSCode plugin to provide basic language support for DianaScript
ANTLR
11
star
38

Incantation

Say incantations to enjoy web designing.
Python
11
star
39

do-you-like-wan-you-si

脑洞,让自己的生活更美好。
Python
11
star
40

DianaVM

Diana... 🥳🥳🥳Diana, suki🤤🤤🤤
C#
11
star
41

Redy

thautwarm utilities.
Python
11
star
42

autojmp

autojump implementation that is applicable to any shell and any OS
Python
10
star
43

reley

haskell-like compiled language based on Python VM
Python
10
star
44

original-posting

Original posting, a.k.a. OP, is an ALL-IN-ONE markup language for cyber wizards to create documentations and blog pages.
Python
10
star
45

plfp

my slides for sharing in our Lab, integrated as the series Programming Languages for Fun and Practice
OCaml
10
star
46

HigherKindedPolymorphisms.jl

A refined implementation of Lightweighted Higher Kinded Types in Julia(via typeclasses/traits)
Julia
9
star
47

ParameterisedModule.jl

Full featured parameterised ML-modules in Julia
Julia
9
star
48

Site-32

thautwarm's blog page.
HTML
8
star
49

rmalt

the malt language implemented by rbnf. https://github.com/malt-project/cmalt
Python
8
star
50

Metagen

Dependently type-safe code generator .
Haskell
8
star
51

ml-to-scheme

Allow using ML to teach courses that're usually given in LISPs
Python
8
star
52

RBNF.hs

Cross-language context-sensitive parsing with type inference, left recursion resolutions and decision tree optimizations.
Haskell
8
star
53

Site-33

https://thautwarm.github.io/Site-33/
Python
8
star
54

MLBI

ML with Best Instantiations, for arbitrary-rank types.
F#
8
star
55

FastParse.fs

fast, easy, typed, minimal
F#
8
star
56

gkd

build confidence with LaTeX. appreciations to @iExploder as we together figure out a valid and much better way to call python in LaTex.
Python
8
star
57

The-Algorithms-in-Numerical-Analysis-with-Python-Implementation

教学轮-数学运算库
Python
7
star
58

JML.jl

A ML dialect based on Julia, for MLStyle examples.
Julia
7
star
59

simplenote

a programming language to encode numbered musical notations. I run this in the virtual conference of PPL 2022..
Python
7
star
60

paper-implementation

I.have(a_cat).then( _.cat.play_with)
Python
6
star
61

LanguageCollections

programming languages invented/implemented by myself.
6
star
62

Ruiko.fs

context sensitive grammar + parser combinator + .NET
F#
6
star
63

SequentExamples.jl

Julia
6
star
64

PrettyDoc.jl

An easy-to-use and lightweight text layout combinator library, serving high-quality pretty printing, text code generation, etc.
Julia
6
star
65

rbnf-rts

Fastest python pgen with rich features. Runtime support for generated parsers of RBNF.hs and a bootstrap with menhir-like syntax sugars.
Python
5
star
66

lua-parser.cs

An out-of-box Lua parser, by antlr4, in csharp.
C#
5
star
67

Sedlex.jl

Julia runtime support Fable Sedlex
Julia
4
star
68

schema-provider.py

Python
4
star
69

typed-jsonrpc

Python
4
star
70

dot-parser-example

Implementing a parser for the dot language in Julia, using our new parser generator.
Julia
4
star
71

NumScala

fOr data analysis with Scala.
Scala
4
star
72

typed-bnf-csharp-demo

A json parser implemented by typed-bnf and generated to Antlr4-CSharp.
C#
4
star
73

julia-android-example

Running Julia Code on Android with Flutter (UI) & Rust (Build System) & SyslabCC (Julia AOT Compiler)
C
4
star
74

rbnfrbnf

Deprecated. See https://github.com/thautwarm/rbnf-rts
Python
4
star
75

NLPWorks

I offered quite a lot with coding and algorithms to some private project, and I share the public parts here to log my life.
Python
4
star
76

rsolve.py

For constraint satisfaction problems in Python
Python
4
star
77

panpanneed

Your local cloud drive implemented in one-liner code.
Python
4
star
78

Benchmarkplotting.jl

Benchmark intuitively!
Julia
3
star
79

DianaScript

check https://github.com/thautwarm/DianaScript-JIT
C#
3
star
80

scripts

Jupyter Notebook
3
star
81

SquirrelLanguage

此坑已久,代码很丑。以记过往,不宜奉读。
Python
3
star
82

AOP

reliable and efficient python meta-programming for building productivity tools
Python
3
star
83

FrontendForFreeParsing.jl

A wrapper of https://github.com/thautwarm/frontend-for-free
Julia
3
star
84

alva

The Mu programming language
3
star
85

formulapy

external tools to type math formulas in Python files.
Python
3
star
86

Recognition

开心~
Python
3
star
87

AbstractPattern

optimize pattern matching compilation and separate front-end syntatic constructs from semantics
Julia
3
star
88

gkdbnf

convert bnf files, to LaTex notations, supporting multiple LaTex BNF packages as back ends
Python
3
star
89

latmap

Ideas of convenient editor development.
Python
3
star
90

gkdtex

A programmable, TeX-compatible and 2-stage typesetting language.
Python
3
star
91

voicecontrol

thorough voice-control for emacs-like editors
Python
2
star
92

HMRowUnification.jl

Providing HM unification with row polymorphism support
Julia
2
star
93

Dealing-with-Features

在数据挖掘实战中锻炼处理实际数据特征的能力
Python
2
star
94

scoping-resolver

Resolve python's scopes for compiler usage
Python
2
star
95

thautwarm

2
star
96

inline-cache-impl

Implementation of inline cache in Python.
Python
2
star
97

pykg

A generic dependency solver; designed as a proposal Fable-Python package manager.
PowerShell
2
star
98

voice-typing-editor

voice-typing-editor via existing input methods
Python
2
star
99

lua-parser-lark

Python
2
star
100

pretty-doc

composable text objects.
Python
2
star