Skip to content

Commit

Permalink
fix #274 (#275)
Browse files Browse the repository at this point in the history
* fix #274

fix GPU preds

* fix #274

fix GPU preds

* fix #274

fix GPU preds

* fix #274

fix GPU preds
  • Loading branch information
jeremiedb authored Jan 23, 2025
1 parent a16368b commit 6a0284e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "EvoTrees"
uuid = "f6006082-12f8-11e9-0c9c-0d5d367ab1e5"
authors = ["jeremiedb <jeremie.db@evovest.com>"]
version = "0.16.8"
version = "0.16.9"

[deps]
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
Expand Down
2 changes: 1 addition & 1 deletion ext/EvoTreesCUDAExt/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function EvoTrees.init_core(params::EvoTrees.EvoTypes{L}, ::Type{<:EvoTrees.GPU}
elseif L == EvoTrees.MLogLoss
if eltype(y_train) <: EvoTrees.CategoricalValue
target_levels = EvoTrees.CategoricalArrays.levels(y_train)
target_isordered = isordered(y_train)
target_isordered = EvoTrees.isordered(y_train)
y = UInt32.(EvoTrees.CategoricalArrays.levelcode.(y_train))
elseif eltype(y_train) <: Integer || eltype(y_train) <: Bool || eltype(y_train) <: String || eltype(y_train) <: Char
target_levels = sort(unique(y_train))
Expand Down
6 changes: 3 additions & 3 deletions ext/EvoTreesCUDAExt/predict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ function EvoTrees.predict!(
end

# prediction for EvoTree model
function predict(
m::EvoTree{L,K},
function EvoTrees._predict(
m::EvoTrees.EvoTree{L,K},
data,
::Type{<:EvoTrees.GPU};
ntree_limit=length(m.trees)) where {L,K}

Tables.istable(data) ? data = Tables.columntable(data) : nothing
EvoTrees.Tables.istable(data) ? data = EvoTrees.Tables.columntable(data) : nothing
ntrees = length(m.trees)
ntree_limit > ntrees && error("ntree_limit is larger than number of trees $ntrees.")
x_bin = CuArray(EvoTrees.binarize(data; fnames=m.info[:fnames], edges=m.info[:edges]))
Expand Down
20 changes: 20 additions & 0 deletions src/models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ function EvoTreeRegressor(; kwargs...)
:rng => 123,
)

args_ignored = setdiff(keys(kwargs), keys(args))
length(args_ignored) > 0 &&
@info "The following kwargs are not supported and will be ignored: $(args_ignored)."

args_override = intersect(keys(args), keys(kwargs))
for arg in args_override
args[arg] = kwargs[arg]
Expand Down Expand Up @@ -163,6 +167,10 @@ function EvoTreeCount(; kwargs...)
:rng => 123,
)

args_ignored = setdiff(keys(kwargs), keys(args))
length(args_ignored) > 0 &&
@info "The following kwargs are not supported and will be ignored: $(args_ignored)."

args_override = intersect(keys(args), keys(kwargs))
for arg in args_override
args[arg] = kwargs[arg]
Expand Down Expand Up @@ -231,6 +239,10 @@ function EvoTreeClassifier(; kwargs...)
:rng => 123,
)

args_ignored = setdiff(keys(kwargs), keys(args))
length(args_ignored) > 0 &&
@info "The following kwargs are not supported and will be ignored: $(args_ignored)."

args_override = intersect(keys(args), keys(kwargs))
for arg in args_override
args[arg] = kwargs[arg]
Expand Down Expand Up @@ -301,6 +313,10 @@ function EvoTreeMLE(; kwargs...)
:rng => 123,
)

args_ignored = setdiff(keys(kwargs), keys(args))
length(args_ignored) > 0 &&
@info "The following kwargs are not supported and will be ignored: $(args_ignored)."

args_override = intersect(keys(args), keys(kwargs))
for arg in args_override
args[arg] = kwargs[arg]
Expand Down Expand Up @@ -387,6 +403,10 @@ function EvoTreeGaussian(; kwargs...)
:rng => 123,
)

args_ignored = setdiff(keys(kwargs), keys(args))
length(args_ignored) > 0 &&
@info "The following kwargs are not supported and will be ignored: $(args_ignored)."

args_override = intersect(keys(args), keys(kwargs))
for arg in args_override
args[arg] = kwargs[arg]
Expand Down
13 changes: 10 additions & 3 deletions src/predict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,23 @@ function predict!(pred::Matrix{T}, tree::Tree{L,K}, x_bin::Matrix{UInt8}, featty
return nothing
end


"""
predict(model::EvoTree, X::AbstractMatrix; ntree_limit = length(model.trees))
predict(m::EvoTree, data; ntree_limit=length(m.trees), device=:cpu)
Predictions from an EvoTree model - sums the predictions from all trees composing the model.
Use `ntree_limit=N` to only predict with the first `N` trees.
"""
function predict(
function predict(m::EvoTree, data; ntree_limit=length(m.trees), device=:cpu)
@assert Symbol(device) [:cpu, :gpu]
_device = Symbol(device) == :cpu ? CPU : GPU
_predict(m, data, _device; ntree_limit)
end

function _predict(
m::EvoTree{L,K},
data,
::Type{<:Device}=CPU;
::Type{<:CPU};
ntree_limit=length(m.trees)) where {L,K}

Tables.istable(data) ? data = Tables.columntable(data) : nothing
Expand Down
10 changes: 4 additions & 6 deletions src/structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,10 @@ struct EvoTree{L,K}
trees::Vector{Tree{L,K}}
info::Dict
end
# (m::EvoTree)(data, device::Type{D}=CPU; ntree_limit=length(m.trees)) where {D<:Device} =
# predict(m, data, device; ntree_limit)
function (m::EvoTree)(data; ntree_limit=length(m.trees), device="cpu")
@assert string(device) ["cpu", "gpu"]
_device = string(device) == "cpu" ? CPU : GPU
return predict(m, data, _device; ntree_limit)
function (m::EvoTree)(data; ntree_limit=length(m.trees), device=:cpu)
@assert Symbol(device) [:cpu, :gpu]
_device = Symbol(device) == :cpu ? CPU : GPU
return _predict(m, data, _device; ntree_limit)
end

_get_struct_loss(::EvoTree{L,K}) where {L,K} = L
Expand Down
9 changes: 9 additions & 0 deletions test/issue-274.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using CUDA
using EvoTrees
using CategoricalArrays

x_train = x=rand(10, 2)
y_train = categorical(rand("abc", 10), ordered=true)
config = EvoTreeClassifier(; device=:gpu, L2=123)
m = fit_evotree(config; x_train, y_train, device=:gpu)
yhat = m(x_train; device=:gpu)

2 comments on commit 6a0284e

@jeremiedb
Copy link
Member 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/123538

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.16.9 -m "<description of version>" 6a0284e672491e5a15e8044e333a406c9e4d2735
git push origin v0.16.9

Please sign in to comment.