Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deferred codegen #54

Merged
merged 36 commits into from
May 14, 2021
Merged

Deferred codegen #54

merged 36 commits into from
May 14, 2021

Conversation

vchuravy
Copy link
Member

using Enzyme

import Enzyme: Compiler
import .Compiler.GPUCompiler: deferred_codegen_jobs
@generated function deferred_codegen(::Val{f}, ::Val{tt}) where {f,tt}
    primal, adjoint = Compiler.fspec(f, tt)
    target = Compiler.EnzymeTarget()
    params = Compiler.EnzymeCompilerParams(adjoint, false)
    job    = Compiler.CompilerJob(target, primal, params)

    push!(deferred_codegen_jobs, job)
    id = length(deferred_codegen_jobs)

    quote
        # TODO: add an edge to this method instance to support method redefinitions
        ccall("extern deferred_codegen", llvmcall, Ptr{Cvoid}, (Int,), $id)
    end
end

f(x) = x^3

function test(x)
    ptr = deferred_codegen(Val(f), Val(Tuple{Active{Float64}}))
    ccall(ptr, Float64, (Float64, Float64), x, 1.0)
end

Co-authored-by: William Moses <gh@wsmoses.com>
@vchuravy
Copy link
Member Author

using Enzyme

f(x) = x*x^3

function first_order(x)
    x′, = autodiff(f, Active(x))
    x′
end

function second_order(x)
    x′, = autodiff(first_order, Active(x))
    x′
end

@show first_order(1.0)
@show autodiff(first_order, Active(1.0))
@show second_order(1.0)
@show autodiff(second_order, Active(1.0))

@info "Testing GPUs"
using CUDA
A = CUDA.zeros(2)

function kernel(A,x)
    A[1] = first_order(x)
    A[2] = second_order(x)
    nothing
end

@show A

@vchuravy
Copy link
Member Author

Project.toml Outdated Show resolved Hide resolved
src/Enzyme.jl Show resolved Hide resolved
# end

function EnzymeRemoveTrivialAtomicIncrements(func)
ccall((:EnzymeRemoveTrivialAtomicIncrements, libEnzyme), Cvoid, (LLVMValueRef,), func)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that one never landed in Enzyme proper so we should remove

src/compiler.jl Show resolved Hide resolved
src/compiler.jl Outdated Show resolved Hide resolved
src/compiler.jl Outdated Show resolved Hide resolved
src/compiler.jl Outdated Show resolved Hide resolved
src/compiler/cassette.jl Show resolved Hide resolved
src/compiler/cassette.jl Show resolved Hide resolved
src/compiler/validation.jl Outdated Show resolved Hide resolved
@vchuravy vchuravy marked this pull request as ready for review May 7, 2021 17:01
test/runtests.jl Outdated Show resolved Hide resolved
@@ -4,17 +4,25 @@ authors = ["William Moses <wmoses@mit.edu>", "Valentin Churavy <vchuravy@mit.edu
version = "0.4.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move the adapt rules from the test case into this Enzyme.jl

Project.toml Outdated Show resolved Hide resolved
Project.toml Outdated Show resolved Hide resolved
Project.toml Outdated Show resolved Hide resolved
Project.toml Outdated Show resolved Hide resolved
Project.toml Outdated Show resolved Hide resolved
src/compiler.jl Outdated Show resolved Hide resolved
src/compiler.jl Outdated Show resolved Hide resolved
src/compiler/validation.jl Outdated Show resolved Hide resolved
src/compiler/validation.jl Outdated Show resolved Hide resolved
test/abi.jl Outdated Show resolved Hide resolved
@vchuravy vchuravy changed the title [WIP] Deferred codegen Deferred codegen May 13, 2021
@vchuravy vchuravy merged commit c8de77a into master May 14, 2021
@vchuravy vchuravy deleted the vc/deferred_codegen branch May 14, 2021 16:21
hnd = LLVM.name(hnd)
end

@show flib, fname, hnd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover?

@@ -1,4 +1,5 @@
using LLVM
using ObjectFile
Copy link
Contributor

@giordano giordano Jun 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jlk9 jlk9 mentioned this pull request Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants