Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ function should_rewrite_ft(@nospecialize(ft))
return false
end
if ft <: Core.Function
mod = ft.name.module
# Don't rewrite primitive ops, tracing utilities, or any MLIR-based functions
if has_ancestor(mod, Reactant.Ops) ||
has_ancestor(mod, Reactant.TracedUtils) ||
has_ancestor(mod, Reactant.MLIR) ||
has_ancestor(mod, Reactant.TracedRandom)
return false
# We need this for closures to work
if hasfield(typeof(ft), :name) && hasfield(typeof(ft.name), :module)
mod = ft.name.module
# Don't rewrite primitive ops, tracing utilities, or any MLIR-based functions
if has_ancestor(mod, Reactant.Ops) ||
has_ancestor(mod, Reactant.TracedUtils) ||
has_ancestor(mod, Reactant.MLIR) ||
has_ancestor(mod, Reactant.TracedRandom)
return false
end
end
end
# Don't rewrite Val
Expand Down
19 changes: 19 additions & 0 deletions test/compile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,22 @@ end
@test !occursin("subtract", repr(hlo))
@test !occursin("add", repr(hlo))
end

# While a bit specific, the following is used to check for a bug in `should_rewrite_ft`
function sinusoidal_embedding(
x::AbstractArray{T,4}, min_freq, max_freq, embedding_dims::Int
) where {T}
if size(x)[1:3] != (1, 1, 1)
throw(DimensionMismatch("Input shape must be (1, 1, 1, batch)"))
end

lower, upper = log(T(min_freq)), log(T(max_freq))
n = embedding_dims ÷ 2
x_ = 2 .* x .* exp.(reshape(range(lower, upper; length=n), 1, 1, n, 1))
return cat(sinpi.(x_), cospi.(x_); dims=Val(3))
end

@testset "sinusoidal_embedding" begin
x_ra = Reactant.to_rarray(rand(Float32, 1, 1, 1, 4))
hlo = @code_hlo sinusoidal_embedding(x_ra, 0.1, 10.0, 4)
end
Loading