Skip to content

Commit

Permalink
Merge pull request #526 from willow-ahrens/wma/verbose_mode
Browse files Browse the repository at this point in the history
add verbose mode to executor
  • Loading branch information
willow-ahrens authored May 6, 2024
2 parents c7cd3e9 + af2bc26 commit 1f53b8b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Finch"
uuid = "9177782c-1635-4eb9-9bfb-d9dfa25e6bce"
authors = ["Willow Ahrens"]
version = "0.6.23"
version = "0.6.24"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
10 changes: 5 additions & 5 deletions src/interface/lazy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,17 @@ function fused(f, args...; optimizer=DefaultOptimizer())
compute(f(map(LazyTensor, args...)), optimizer)
end

default_scheduler = LogicExecutor(DefaultLogicOptimizer(LogicCompiler()))
default_scheduler(;verbose=false) = LogicExecutor(DefaultLogicOptimizer(LogicCompiler()), verbose=verbose)

"""
compute(args..., ctx=default_scheduler) -> Any
compute(args..., ctx=default_scheduler()) -> Any
Compute the value of a lazy tensor. The result is the argument itself, or a
tuple of arguments if multiple arguments are passed.
"""
compute(args...; ctx=default_scheduler) = compute_parse(ctx, args)
compute(arg; ctx=default_scheduler) = compute_parse(ctx, (arg,))[1]
compute(args::Tuple; ctx=default_scheduler) = compute_parse(ctx, args)
compute(args...; ctx=default_scheduler(), kwargs...) = compute_parse(set_options(ctx; kwargs...), args)
compute(arg; ctx=default_scheduler(), kwargs...) = compute_parse(set_options(ctx; kwargs...), (arg,))[1]
compute(args::Tuple; ctx=default_scheduler(), kwargs...) = compute_parse(set_options(ctx; kwargs...), args)
function compute_parse(ctx, args::Tuple)
args = collect(args)
vars = map(arg -> alias(gensym(:A)), args)
Expand Down
4 changes: 4 additions & 0 deletions src/scheduler/LogicCompiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ COMPUTE_QUERY := query(ALIAS, reformat(IMMEDIATE, arg::(REORDER | MAPREDUCE)))
mode = :fast
end

function set_options(ctx::LogicCompiler; mode = ctx.mode, kwargs...)
LogicCompiler(mode = mode)
end

function (ctx::LogicCompiler)(prgm::LogicNode)
prgm = format_queries(prgm, true)
LogicLowerer(mode=ctx.mode)(prgm)
Expand Down
12 changes: 11 additions & 1 deletion src/scheduler/LogicExecutor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,31 @@ function logic_executor_code(ctx, prgm)
end

"""
LogicExecutor(ctx)
LogicExecutor(ctx, verbose=false)
Executes a logic program by compiling it with the given compiler `ctx`. Compiled
codes are cached, and are only compiled once for each program with the same
structure.
"""
struct LogicExecutor
ctx
verbose
end

LogicExecutor(ctx; verbose = false) = LogicExecutor(ctx, verbose)
function set_options(ctx::LogicExecutor; verbose = ctx.verbose, kwargs...)
LogicExecutor(set_options(ctx.ctx; kwargs...), verbose)
end

codes = Dict()
function (ctx::LogicExecutor)(prgm)
f = get!(codes, get_structure(prgm)) do
eval(logic_executor_code(ctx.ctx, prgm))
end
if ctx.verbose
println("Executing:")
display(logic_executor_code(ctx.ctx, prgm))
end
return Base.invokelatest(f, prgm)
end

Expand Down
4 changes: 4 additions & 0 deletions src/scheduler/LogicInterpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ COMPUTE_QUERY := query(ALIAS, reformat(IMMEDIATE, arg::(REORDER | MAPREDUCE)))
mode = :fast
end

function set_options(ctx::LogicInterpreter; verbose = ctx.verbose, mode = ctx.mode, kwargs...)
LogicInterpreter(verbose=verbose, mode=mode)
end

function (ctx::LogicInterpreter)(prgm)
prgm = format_queries(prgm)
LogicMachine(verbose = ctx.verbose, mode = ctx.mode)(prgm)
Expand Down
4 changes: 4 additions & 0 deletions src/scheduler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,8 @@ end
function (ctx::DefaultLogicOptimizer)(prgm)
prgm = optimize(prgm)
ctx.ctx(prgm)
end

function set_options(ctx::DefaultLogicOptimizer; kwargs...)
DefaultLogicOptimizer(set_options(ctx.ctx; kwargs...))
end
10 changes: 10 additions & 0 deletions test/test_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ using Finch: AsArray
let
using Test

@testset "Verbose" begin
a = [1 2; 3 4]
b = [5 6; 7 8]
a_l = lazy(a)
b_l = lazy(b)

c = permutedims(broadcast(.+, permutedims(a_l, (2, 1)), permutedims(b_l, (2, 1))), (2, 1))
compute(c, verbose=true)
end

@testset "Einsum Tests" begin
# Test 0
A = [1 2; 3 4]
Expand Down

2 comments on commit 1f53b8b

@willow-ahrens
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/106261

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.24 -m "<description of version>" 1f53b8b24f19ae7fe3be2134ff1a05e44ac52585
git push origin v0.6.24

Please sign in to comment.