Skip to content

Commit

Permalink
Merge pull request #12 from JuliaTrustworthyAI/11-threads-static-cann…
Browse files Browse the repository at this point in the history
…ot-be-used-concurrently-or-nested

Fixes this
  • Loading branch information
pat-alt authored Nov 6, 2024
2 parents 8534c12 + 31e3020 commit 7dca5f1
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 334 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ jobs:
fail-fast: false
matrix:
version:
- '1.7'
- '1.8'
- '1.9'
- '1.10'
- '1.11'
os:
- ubuntu-latest
arch:
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Version [1.0.4] - 2024-11-06

### Changed

- Moved from `:static` threading to dynamic task scheduling.

10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TaijaParallel"
uuid = "bf1c2c22-5e42-4e78-8b6b-92e6c673eeb0"
authors = ["Patrick Altmeyer <[email protected]>"]
version = "1.0.3"
version = "1.0.4"

[deps]
CounterfactualExplanations = "2f13d31b-18db-44c1-bc43-ebaf2cff0be0"
Expand All @@ -22,16 +22,16 @@ MPIExt = "MPI"
[compat]
Aqua = "0.8"
CounterfactualExplanations = "0.1, 1"
Logging = "1.7, 1.8, 1.9, 1.10"
Logging = "1"
MLUtils = "0.4.4"
MPI = "0.20"
PackageExtensionCompat = "1"
ProgressMeter = "1"
Reexport = "1"
Serialization = "1.7, 1.8, 1.9, 1.10"
Serialization = "1"
TaijaBase = "1"
Test = "1.7, 1.8, 1.9, 1.10"
julia = "1.7, 1.8, 1.9, 1.10"
Test = "1"
julia = "1.10"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Expand Down
29 changes: 21 additions & 8 deletions src/CounterfactualExplanations.jl/threads/evaluate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ function TaijaBase.parallelize(
meta_data = fill(meta_data, length(counterfactuals))
end

# Bundle arguments:
args = zip(counterfactuals, meta_data)

# Preallocate:
evaluations = [[] for _ = 1:Threads.nthreads()]
evaluations = Vector{Vector}(undef, length(args))

# Verbosity:
if verbose
Expand All @@ -49,16 +52,26 @@ function TaijaBase.parallelize(
)
end

# Bundle arguments:
args = zip(counterfactuals, meta_data)
# Training: Use @spawn to dynamically schedule tasks
tasks = []

Threads.@threads :static for (ce, meta) in collect(args)
push!(evaluations[Threads.threadid()], f(ce, meta; kwargs...))
if verbose
ProgressMeter.next!(prog)
# Iterate with index to preserve order
for (i, (ce, meta)) in enumerate(args)
# Each task will execute this function dynamically on an available thread
task = Threads.@spawn begin
evaluation = f(ce, meta; kwargs...)
evaluations[i] = evaluation # Store result in the correct position
if verbose
ProgressMeter.next!(prog)
end
end
push!(tasks, task)
end

# Wait for all tasks to complete
for task in tasks
fetch(task)
end
evaluations = reduce(vcat, evaluations)

return evaluations
end
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,41 @@ function TaijaBase.parallelize(
# Break down into chunks:
args = zip(counterfactuals, target, data, M, generator)

# Preallocate:
ces = [
Vector{CounterfactualExplanations.AbstractCounterfactualExplanation}() for
_ = 1:Threads.nthreads()
]
# Preallocate a vector for storing results in the original order
ces = Vector{CounterfactualExplanations.AbstractCounterfactualExplanation}(undef, length(args))

# Verbosity:
# Verbosity setup:
if verbose
prog = ProgressMeter.Progress(
length(args);
desc = "Generating counterfactuals ...",
showspeed = true,
color = :green,
desc="Generating counterfactuals ...",
showspeed=true,
color=:green,
)
end

# Training:
Threads.@threads :static for (x, target, data, M, generator) in collect(args)
ce = with_logger(NullLogger()) do
f(x, target, data, M, generator; kwargs...)
end
push!(ces[Threads.threadid()], ce)
if verbose
ProgressMeter.next!(prog)
# Training: Use @spawn to dynamically schedule tasks
tasks = []

# Iterate with index to preserve order
for (i, (x, target, data, M, generator)) in enumerate(args)
# Each task will execute this function dynamically on an available thread
task = Threads.@spawn begin
ce = with_logger(NullLogger()) do
f(x, target, data, M, generator; kwargs...)
end
ces[i] = ce # Store result in the correct position
if verbose
ProgressMeter.next!(prog)
end
end
push!(tasks, task)
end

ces = reduce(vcat, ces)
# Wait for all tasks to complete
for task in tasks
fetch(task)
end

return ces
end
1 change: 0 additions & 1 deletion test/CounterfactualExplanations.jl/mpi.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CounterfactualExplanations.DataPreprocessing: CounterfactualData
using CounterfactualExplanations.Evaluation: benchmark
using LaplaceRedux
using Logging
using TaijaData
using TaijaParallel
Expand Down
8 changes: 8 additions & 0 deletions test/CounterfactualExplanations.jl/threads.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CounterfactualExplanations
using CounterfactualExplanations.Convergence
using CounterfactualExplanations.Evaluation
using CounterfactualExplanations.Models
using TaijaData
using TaijaParallel: ThreadsParallelizer, @with_parallelizer

Expand All @@ -24,4 +26,10 @@ ces = @with_parallelizer parallelizer begin
generate_counterfactual(xs, target, counterfactual_data, M, generator; convergence = conv)
end

@test all(CounterfactualExplanations.factual.(ces) .== (x -> x[1]).(collect(xs)))

evals = @with_parallelizer parallelizer begin
evaluate(ces)
end

@test true
Loading

2 comments on commit 7dca5f1

@pat-alt
Copy link
Member Author

@pat-alt pat-alt commented on 7dca5f1 Nov 6, 2024

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/118798

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 v1.0.4 -m "<description of version>" 7dca5f19be808b5591bdf6536db2c2b6c9802d04
git push origin v1.0.4

Please sign in to comment.