Code Monkey home page Code Monkey logo

reactant.jl's Introduction

Reactant.jl

Stable Dev Build Status Coverage

[!WARNING]
This package is under active development at the moment and may change its API and supported end systems at any time. End-users are advised to wait until a corresponding release with broader availability is made. Package developers are suggested to try out Reactant for integration, but should be advised of the same risks.

Reactant takes Julia function and compile it into MLIR and run fancy optimizations on top of it, including using EnzymeMLIR for automatic differentiation, and create relevant executables for CPU/GPU/TPU via XLA. It presently operates as a tracing system based off of Cassette. Compiled functions will assume the same control flow pattern as was original taken by objects used at compile time, and control flow (e.g. if, for) as well as any type instabilities will be removed. The benefits of this approach is immediately making all such code available for advanced optimization with little developer effort. This system and corresponding semantics is subject to change to a (potentially partial) source rewriter in the future.

Reactant provides two new array types at its core, a ConcreteRArray and a TracedRArray. A ConcreteRArray is an underlying buffer to whatever device data you wish to store and can be created by converting from a regular Julia Array.

using Reactant

julia_data = ones(2, 10)
reactant_data = Reactant.ConcreteRArray(julia_data)

You can also create a ConcreteRArray-version of an arbitrary data type by tracing through the structure, like below. This method will automatically handle recursive data structures or shared objects.

struct Pair{A,B}
   x::A
   y::B
end

pair = Pair(ones(3), ones(10))

reactant_pair = Reactant.make_tracer(IdDict(), pair, (), Reactant.ArrayToConcrete, nothing)

To compile programs using ConcreteRArray's, one uses the compile function, like as follows:

input1 = Reactant.ConcreteRArray(ones(10))
input2 = Reactant.ConcreteRArray(ones(10))

function sinsum_add(x, y)
   return sum(sin.(x) .+ y)
end

f=Reactant.compile(sinsum_add, (input1,input2))

# one can now run the program
f(input1, input2)

The generated function can be called with data structures which match the same types (and sizes) as were originally compiled with. Reactant (presently, see above) only captures updates to ConcreteRArray and as such any updates to other data (such as a regular int counter) will not be reflected in generated compiled functions. Similarly if there are any branches or conditional behavior based on any non-ConcreteRArray data, they will not be reflected in the generated function.

When being compiled, functions will not have access to the actual values of data within ConcreteRArrays, and will instead be passed TracedRArrays to reflect this semantic distinction.

One can automatically leverage Reactant to run programs on accelerators, by specifying the desired device client. For example, to run programs on GPU one can initialize the default device as follows (or alternatively construct RArrays with a device).

using Reactant
Reactant.set_default_backend("gpu")

# ones favorite code will now all be executed on GPU, no CUDA.jl dependency even required!

reactant.jl's People

Contributors

wsmoses avatar vchuravy avatar

Stargazers

 avatar Guillaume Dalle avatar Ryuta Suzuki avatar Andre Nogueira Souza avatar Jerry Ling avatar Paul Tiede avatar Chetan Vardhan avatar Lasse Peters avatar Orestis Ousoultzoglou avatar Peter avatar Avik Pal avatar Sergio Sánchez Ramírez avatar

Watchers

 avatar Jose Manuel Monsalve Diaz avatar Paul Tiede avatar Peter avatar Manuel Drehwald avatar  avatar

Forkers

luxdl mofeing

reactant.jl's Issues

Crash on `Reactant.compile`

When trying to run the test on my aarch64 macOS, I'm getting the following fatal error on line

f=Reactant.compile(fastmax, (a,))

Precompiling project...
  7 dependencies successfully precompiled in 20 seconds. 122 already precompiled.
     Testing Running tests...
Test Summary: | Pass  Total  Time
Layout        |    5      5  0.1s
F0504 21:01:25.451342 38805431 simple_orc_jit.cc:310] Check failed: target_machine != nullptr 
*** Check failure stack trace: ***
    @        0x1601222b0  absl::lts_20230802::log_internal::LogMessage::SendToLog()
    @        0x160121cac  absl::lts_20230802::log_internal::LogMessage::Flush()
    @        0x16012268c  absl::lts_20230802::log_internal::LogMessageFatal::~LogMessageFatal()
    @        0x1601226ac  absl::lts_20230802::log_internal::LogMessageFatal::~LogMessageFatal()
    @        0x15c6435c0  xla::cpu::SimpleOrcJIT::InferTargetMachineForJIT()
    @        0x15be5668c  xla::cpu::CpuCompiler::RunHloPasses()
    @        0x15be2c130  xla::TfrtCpuClient::Compile()
    @        0x15be2ceb8  xla::TfrtCpuClient::Compile()
    @        0x15b960608  ClientCompile
    @        0x122db1ccc  0x0
    @        0x122db27d0  0x0
    @        0x122b80ce4  0x0
    @        0x122b880bc  0x0
    @        0x1015c23b4  do_call
    @        0x1015c08ec  eval_body
    @        0x1015c0e08  eval_body
    @        0x1015c0e08  eval_body
    @        0x1015c11d0  jl_interpret_toplevel_thunk
    @        0x1015d96f8  jl_toplevel_eval_flex
    @        0x1015d94c0  jl_toplevel_eval_flex
    @        0x1015da3a8  ijl_toplevel_eval_in
    @        0x130aa1ab8  japi1_include_string_81107.4
    @        0x1015a7cb4  ijl_apply_generic
    @        0x13125fcbc  japi1__include_81115.4
    @        0x1015c23b4  do_call
    @        0x1015c0bb0  eval_body
    @        0x1015c11d0  jl_interpret_toplevel_thunk
    @        0x1015d96f8  jl_toplevel_eval_flex
    @        0x1015d94c0  jl_toplevel_eval_flex
    @        0x1015da3a8  ijl_toplevel_eval_in
    @        0x130aa1ab8  japi1_include_string_81107.4
    @        0x1015a7cb4  ijl_apply_generic
    @        0x13125fcbc  japi1__include_81115.4
    @        0x1015c23b4  do_call
    @        0x1015c0bb0  eval_body
    @        0x1015c11d0  jl_interpret_toplevel_thunk
    @        0x1015d96f8  jl_toplevel_eval_flex
    @        0x1015d94c0  jl_toplevel_eval_flex
    @        0x1015da3a8  ijl_toplevel_eval_in
    @        0x13172133c  jlplt_ijl_toplevel_eval_in_25935.3
    @        0x1016057b0  true_main
    @        0x1016056a4  jl_repl_entrypoint
    @        0x100b93f6c  main
    @        0x18d02a0e0  start

[46954] signal (6): Abort trap: 6
in expression starting at /Users/mofeing/Developer/Reactant.jl/test/basic.jl:7
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 5647750 (Pool: 5642220; Big: 5530); GC: 7
ERROR: Package Reactant errored during testing (received signal: 6)

I'm opening an issue because I don't see this error on CI.

Define `similar` for Reactant.ConcreteRArray

The inplace Lux functions currently fail because it constructs a matrix

using Reactant, Lux, Random
using Test

# Generate some data for the XOR problem: vectors of length 2, as columns of a matrix:
noisy = rand(Float32, 2, 1000)                                        # 2×1000 Matrix{Float32}
truth = [xor(col[1] > 0.5, col[2] > 0.5) for col in eachcol(noisy)]   # 1000-element Vector{Bool}

# Define our model, a multi-layer perceptron with one hidden layer of size 3:
model = Chain(Dense(2 => 3, tanh),   # activation function inside layer
    BatchNorm(3), Dense(3 => 2), softmax)
ps, st = Lux.setup(Xoshiro(123), model)

using BenchmarkTools

origout, _ = model(noisy, ps, st)
@show origout[3]
@btime model($noisy, $ps, $st)

cmodel = Reactant.make_tracer(IdDict(), model, (), Reactant.ArrayToConcrete, nothing)
cps = Reactant.make_tracer(IdDict(), ps, (), Reactant.ArrayToConcrete, nothing)
cst = Reactant.make_tracer(IdDict(), st, (), Reactant.ArrayToConcrete, nothing)
cnoisy = Reactant.ConcreteRArray(noisy)

f = Reactant.compile((a, b, c, d) -> a(b, c, d), (cmodel, cnoisy, cps, CST))

Minimal version

using Reactant

noisy = rand(Float32, 2, 1000)
cnoisy = Reactant.ConcreteRArray(noisy)
similar(cnoisy)  # Matrix

Crash on precompiling `ReactantNNlibExt`

When trying to run the tests, I get the following error:

Precompiling project...
  ✗ Reactant  ReactantNNlibExt
  1 dependency successfully precompiled in 7 seconds. 127 already precompiled.
  1 dependency had output during precompilation:
┌ Reactant
│  error in running finalizer: TypeError(func=Symbol("ccall: first argument not a pointer or valid constant expression"), context="", expected=Ptr{T} where T, got=(:PjRtBufferFree, 0x000000008fc4a150))
│  ijl_type_error_rt at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/rtutils.c:119
│  ijl_type_error at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/rtutils.c:127#5 at /Users/mofeing/Developer/Reactant.jl/src/XLA.jl:114
│  unknown function (ip: 0x105c080e3)
│  _jl_invoke at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:0 [inlined]
│  ijl_apply_generic at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:3077
│  run_finalizer at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:318
│  jl_gc_run_finalizers_in_list at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:408
│  run_finalizers at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:454
│  ijl_atexit_hook at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/init.c:299
│  jl_repl_entrypoint at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/jlapi.c:732
└  
     Testing Running tests...
libc++abi: terminating due to uncaught exception of type xla::XlaRuntimeError: NOT_FOUND: Could not find registered platform with name: "cuda". Available platform names are: 

[42937] signal (6): Abort trap: 6
in expression starting at /Users/mofeing/Developer/Reactant.jl/test/runtests.jl:1
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 2908 (Pool: 2900; Big: 8); GC: 0
ERROR: Package Reactant errored during testing (received signal: 6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.