Jax Decompiler
Project description
JaxDecompiler
Jax Decompiler
The JAX decompiler takes jaxpr code and produces Python code. Even if some information about the original function is lost (obfuscated code) like variable names being lost, it is an important tool for reverse-engineering. There are many applications where decompiling gradient functions are useful.
Associated pr: https://github.com/google/jax/issues/13398
Installation
pip3 install JaxDecompiler
Usage example
Given any jaxpr function, here "df", we want to generate the associated Python code.
import jax
def f(x, smooth_rate):
local_minimums = (1 - smooth_rate) * jax.numpy.cos(x)
global_minimum = smooth_rate * x**2
return global_minimum + local_minimums
df = jax.grad(f, (0,))
Function df is implemented with jaxpr code. You can display it with:
from JaxDecompiler import decompiler
decompiler.display_wrapped_jaxpr(df, (1.0, 1.0))
returns:
===== HEADER =======
invars: [a, b]
outvars: [p]
constvars: []
===== CODE =======
{ lambda ; a:f32[] b:f32[]. let
c:f32[] = sub 1.0 b
d:f32[] = cos a
e:f32[] = sin a
[...]
The below code decompiles it automatically. It generates the python function and its python code as text.
from JaxDecompiler import decompiler
decompiled_df, python_code = decompiler.python_jaxpr_python(
df, (1.0, 1.0), is_python_returned=True
)
Let's check df and decompiled_df behave the same:
print("df: ", df(4.0, 0.99)) # ~7.927568
print("decompiled df: ", decompiled_df(4.0, 0.99)) # ~7.927568
They produce the same result in spite to be written in different languages!
Now Let's display what is inside decompiled_df:
print(python_code)
Display:
def f(a, b):
c = 1.0 - b
d = cos(a)
e = sin(a)
f = c * d
g = a ** 2
h = a ** 1
i = 2.0 * h
j = b * g
_ = j + f
k = c * 1.0
l = -k
m = l * e
n = b * 1.0
o = n * i
p = m + o
return p
Now, the user owns its derivative code and may easily refactor/edit it! This is a reverse-engineering tool, for example, we can now improving arithemtic stability, manually optimize the code, ...
Notice: python_jaxpr_python create out/ folder in the current directory.
Next steps
There are the next steps:
-
More operators. Today ~60 jaxpr operators are implemented ('add', 'mul', 'cos', ...). The exhaustive list of the implemented operators is in the file "primitive_mapping.py". This python file aims to map jaxpr operator (the name of the functions) into python code (string returned by the function).
-
Automatic refactoring. There is room for improvement to make the automatically produced Python code easier to read/maintain. An automatic refactoring tool should be able to translate this low-level Python style into a more readable one for humans.
-
Automatic detection of useless codes. In the example above, "j" variable is useless.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for JaxDecompiler-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 347f0027b7ab2f068f4b9c66ff38b889aa263bf40fee8d0b0c28a712b47fbbd8 |
|
MD5 | cf255abfccb2a8cbc20d67a93363dd3d |
|
BLAKE2b-256 | 90f954f59e5d65e86de4edfb82c1f2e0e42a7700f4a95fb44f4ddc2dbc8f2aa5 |