# Calling Python function JIT compiled with JAX from Julia without overhead

**URL:** <https://discourse.julialang.org/t/calling-python-function-jit-compiled-with-jax-from-julia-without-overhead/123552>\
**Category:** General Usage\
**Tags:** performance, interoperability\
**Created:** [December 6, 2024, 11:04pm UTC](https://discourse.julialang.org/t/calling-python-function-jit-compiled-with-jax-from-julia-without-overhead/123552 "2024-12-06T23:04:21Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [May 28, 2025, 9:34pm UTC](https://discourse.julialang.org/t/calling-python-function-jit-compiled-with-jax-from-julia-without-overhead/123552/21 "2025-05-28T21:34:10Z")

</div>

You generally should not use @jit from reactant outside of one off tests. You should say f = @compile foo(…), then call the compiled function f(…)

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [May 28, 2025, 9:40pm UTC](https://discourse.julialang.org/t/calling-python-function-jit-compiled-with-jax-from-julia-without-overhead/123552/22 "2025-05-28T21:40:32Z")

</div>

@MilesCranmer on my laptop, I see the following. We can probably bring down the overhead further to setup the call (though also note that the overhead is from calling the outermost function so you won’t see any such overhead calling jax code from within a julia function – just from the outermost setup itself).

```julia
julia> using Reactant

julia> using PythonCall: pyimport

julia> using BenchmarkTools: @btime

julia> jax = pyimport("jax")
Python: <module 'jax' from '/Users/wmoses/git/Reactant.jl/py/.CondaPkg/.pixi/envs/default/lib/python3.12/site-packages/jax/ __init__.py'>

julia> jax_sum(x) = Reactant.@jit jax.numpy.sum(x)
jax_sum (generic function with 1 method)

julia> @btime sum(x) setup=(x=randn(Float32,100))
  6.083 ns (0 allocations: 0 bytes)
-7.1881f0

julia> @btime jax_sum(x) setup=(x=Reactant.to_rarray(randn(Float32,100)))
  27.854 ms (1615 allocations: 451.13 KiB)
Reactant.ConcretePJRTNumber{Float32, 1, Reactant.Sharding.ShardInfo{Reactant.Sharding.NoSharding, Nothing}}(-15.242416f0)

julia> jax_sum2 = Reactant.@compile sync=true jax.numpy.sum(Reactant.to_rarray(randn(Float32,100)))
Reactant compiled function <function sum at 0x35dfb9300> (with tag ##<function sum at 0x35dfb9300>_reactant#262)

julia> @btime jax_sum2(x) setup=(x=Reactant.to_rarray(randn(Float32,100)))
  1.708 μs (16 allocations: 704 bytes)
Reactant.ConcretePJRTNumber{Float32, 1, Reactant.Sharding.ShardInfo{Reactant.Sharding.NoSharding, Nothing}}(2.3117552f0)

```

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [May 28, 2025, 9:50pm UTC](https://discourse.julialang.org/t/calling-python-function-jit-compiled-with-jax-from-julia-without-overhead/123552/23 "2025-05-28T21:50:27Z")

</div>

And yes you can call Reactant.to\_rarray(0.0; track\_numbers=true), or alternatively just ConcreteRNumber(0.0)

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 28, 2025, 9:53pm UTC](https://discourse.julialang.org/t/calling-python-function-jit-compiled-with-jax-from-julia-without-overhead/123552/24 "2025-05-28T21:53:48Z")

</div>

Just to elaborate, that’s because `@jit` does two things: compile the function (what `@compile` does) and call it. If you benchmark, even with `@btime`, a `@jit` call you always measure Reactant’s compilation and a first-time call latency of the (re-)compiled function. That’s why separating `@compile` and function call gives much more sensible timing.

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [May 28, 2025, 10:55pm UTC](https://discourse.julialang.org/t/calling-python-function-jit-compiled-with-jax-from-julia-without-overhead/123552/25 "2025-05-28T22:55:08Z")

</div>

Thanks. I’ve been using Julia for so long I forgot that thing about JAX needing static shapes for compilation

Are there any static shape versions of ConcretePJRTArray so that the shape specialization can be done automatically?

```julia-repl
julia> jax_sum_32 = Rx.@compile sync=true jax.numpy.sum(Rx.to_rarray(randn(Float32,32)))
Reactant compiled function <function sum at 0x340b8c860> (with tag ##<function sum at 0x340b8c860>_reactant#250)

julia> jax_sum_32(Rx.to_rarray(randn(Float32,64)))
ERROR: INVALID_ARGUMENT: Executable expected parameter 0 of size 128 but got buffer with incompatible size 256

```

It might be nice to build generic `jax_sum(x)` that automatically compile to each shape, similar to how the `@jax.jit` function wrapper does it

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [May 28, 2025, 11:40pm UTC](https://discourse.julialang.org/t/calling-python-function-jit-compiled-with-jax-from-julia-without-overhead/123552/26 "2025-05-28T23:40:44Z")

</div>

Originally we had the shape in the type, but we removed it so we could in the future support dynamic shapes (which is planned but we only started updating our optimizations to support recently).

But you can always make a separate cache based on the sizes that’s smarter than the current jit macro (contributions welcome).

[Previous page](https://discourse.julialang.org/t/calling-python-function-jit-compiled-with-jax-from-julia-without-overhead/123552.md?page=1)
