GitXplorerGitXplorer
h

shitty_jit

public
3 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
47b5748cd8d46fd0c672585fbc70dd07cb2333b4

shitty_jit: add caveats

hhauntsaninja committed 2 years ago
Unverified
ca53d29e82f7186fef8ff8d8308cba916e9fc9a9

shitty_jit: print function we're compiling

hhauntsaninja committed 2 years ago
Unverified
36a960869d5a782c6e249d1b90313bb29b17f948

shitty_jit: add README

hhauntsaninja committed 2 years ago
Unverified
d830b55a4d06b5b1110a45f5dd620c5996749f44

shitty_jit: add tests

hhauntsaninja committed 2 years ago
Unverified
57b69a4df5ee6c6115a88d19e92cbe380cf6e1a7

shitty_jit: hello world!

hhauntsaninja committed 2 years ago
Unverified
784343d29858c13fed75ce1deeba45fcbb9a2f37

shitty_jit: add gitignore

hhauntsaninja committed 2 years ago

README

The README file for this repository.

shitty_jit

A quick introduction to the incredibly powerful PEP 523 APIs.

The frame evaluation API is intended as a hook for JIT compilers. It's what PyTorch 2.0 uses for its magical new torch.compile feature.

But today, we're going to use it to...

Turn addition into subtraction

from shitty_jit import *

def simple_addition():
    a = 1
    b = 2
    print(f"{a + b = }")

print("Calling simple_addition normally...")
simple_addition()

print("Setting our PEP 523 frame evaluator...")
turn_interpreted_addition_into_subtraction()

print("Calling simple_addition again...")
simple_addition()
print("and again (note the code object is cached, so we won't hit our frame evaluator)...")
simple_addition()

print("Unsetting our PEP 523 frame evaluator...")
reset_shitty_jit()

print("Calling simple_addition one last time...")
simple_addition()

This will output:

Calling simple_addition normally...
a + b = 3
Setting our PEP 523 frame evaluator...
Calling simple_addition again...
Hello from inside our frame evaluator! (compiling simple_addition)
a + b = -1
and again (note the code object is cached, so we won't hit our frame evaluator)...
a + b = -1
Unsetting our PEP 523 frame evaluator...
Hello from inside our frame evaluator! (compiling reset_shitty_jit)
Calling simple_addition one last time...
a + b = 3

Caveats

  • The CPython interpreter does constant folding when compiling to bytecode, so if you try and add two constants, the operation will be evaluated by the compiler, not the bytecode interpreter. This means that turn_interpreted_addition_into_subtraction will not have an opportunity to change the addition into subtraction.
  • This is just a toy (and a dangerous one at that)! If you're looking for a real life use of this API, check out TorchDynamo.
  • This code currently only works on CPython 3.9 and 3.10.