Skip to content

Commit

Permalink
Create CUDA extension
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion committed Oct 5, 2023
1 parent cf6e3c0 commit 8a42cef
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 139 deletions.
9 changes: 8 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ StatsBase = "0.32, 0.33, 0.34"
Tables = "1.9"
julia = "1.6"

[extensions]
EvoTreesCUDAExt = "CUDA"

[extras]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
MLJBase = "a7f614a8-145f-11e9-1d2a-a57a1082229d"
Expand All @@ -37,4 +41,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
docs = ["Documenter"]
test = ["DataFrames", "Test", "MLJBase", "MLJTestInterface"]
test = ["CUDA", "DataFrames", "Test", "MLJBase", "MLJTestInterface"]

[weakdeps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
22 changes: 22 additions & 0 deletions ext/EvoTreesCUDAExt/EvoTreesCUDAExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module EvoTreesCUDAExt

using EvoTrees
using CUDA

# This should be different on CPUs and GPUs
EvoTrees.device_ones(::Type{<:GPU}, ::Type{T}, n::Int) where {T} = CUDA.ones(T, n)
EvoTrees.device_array_type(::Type{<:GPU}) = CuArray
function EvoTrees.post_fit_gc(::Type{<:GPU})
GC.gc(true)
CUDA.reclaim()

Check warning on line 11 in ext/EvoTreesCUDAExt/EvoTreesCUDAExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/EvoTreesCUDAExt.jl#L7-L11

Added lines #L7 - L11 were not covered by tests
end

include("loss.jl")
include("eval.jl")
include("predict.jl")
include("init.jl")
include("subsample.jl")
include("fit-utils.jl")
include("fit.jl")

end # module
22 changes: 11 additions & 11 deletions src/gpu/eval.jl → ext/EvoTreesCUDAExt/eval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function eval_mse_kernel!(eval::CuDeviceVector{T}, p::CuDeviceMatrix{T}, y::CuDe
end
return nothing
end
function mse(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}
function EvoTrees.mse(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}

Check warning on line 11 in ext/EvoTreesCUDAExt/eval.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/eval.jl#L11

Added line #L11 was not covered by tests
threads = min(MAX_THREADS, length(y))
blocks = cld(length(y), threads)
@cuda blocks = blocks threads = threads eval_mse_kernel!(eval, p, y, w)
Expand All @@ -19,8 +19,8 @@ end
########################
# RMSE
########################
rmse(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat} =
sqrt(rmse(p, y, w; MAX_THREADS, kwargs...))
EvoTrees.rmse(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat} =

Check warning on line 22 in ext/EvoTreesCUDAExt/eval.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/eval.jl#L22

Added line #L22 was not covered by tests
sqrt(EvoTrees.rmse(p, y, w; MAX_THREADS, kwargs...))

########################
# MAE
Expand All @@ -32,7 +32,7 @@ function eval_mae_kernel!(eval::CuDeviceVector{T}, p::CuDeviceMatrix{T}, y::CuDe
end
return nothing
end
function mae(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}
function EvoTrees.mae(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}

Check warning on line 35 in ext/EvoTreesCUDAExt/eval.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/eval.jl#L35

Added line #L35 was not covered by tests
threads = min(MAX_THREADS, length(y))
blocks = cld(length(y), threads)
@cuda blocks = blocks threads = threads eval_mae_kernel!(eval, p, y, w)
Expand All @@ -51,7 +51,7 @@ function eval_logloss_kernel!(eval::CuDeviceVector{T}, p::CuDeviceMatrix{T}, y::
end
return nothing
end
function logloss(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}
function EvoTrees.logloss(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}

Check warning on line 54 in ext/EvoTreesCUDAExt/eval.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/eval.jl#L54

Added line #L54 was not covered by tests
threads = min(MAX_THREADS, length(y))
blocks = cld(length(y), threads)
@cuda blocks = blocks threads = threads eval_logloss_kernel!(eval, p, y, w)
Expand All @@ -70,7 +70,7 @@ function eval_gaussian_kernel!(eval::CuDeviceVector{T}, p::CuDeviceMatrix{T}, y:
end
return nothing
end
function gaussian_mle(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}
function EvoTrees.gaussian_mle(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}

Check warning on line 73 in ext/EvoTreesCUDAExt/eval.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/eval.jl#L73

Added line #L73 was not covered by tests
threads = min(MAX_THREADS, length(y))
blocks = cld(length(y), threads)
@cuda blocks = blocks threads = threads eval_gaussian_kernel!(eval, p, y, w)
Expand All @@ -91,7 +91,7 @@ function eval_poisson_kernel!(eval::CuDeviceVector{T}, p::CuDeviceMatrix{T}, y::
return nothing
end

function poisson(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}
function EvoTrees.poisson(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}

Check warning on line 94 in ext/EvoTreesCUDAExt/eval.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/eval.jl#L94

Added line #L94 was not covered by tests
threads = min(MAX_THREADS, length(y))
blocks = cld(length(y), threads)
@cuda blocks = blocks threads = threads eval_poisson_kernel!(eval, p, y, w)
Expand All @@ -111,7 +111,7 @@ function eval_gamma_kernel!(eval::CuDeviceVector{T}, p::CuDeviceMatrix{T}, y::Cu
return nothing
end

function gamma(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}
function EvoTrees.gamma(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}

Check warning on line 114 in ext/EvoTreesCUDAExt/eval.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/eval.jl#L114

Added line #L114 was not covered by tests
threads = min(MAX_THREADS, length(y))
blocks = cld(length(y), threads)
@cuda blocks = blocks threads = threads eval_gamma_kernel!(eval, p, y, w)
Expand All @@ -133,7 +133,7 @@ function eval_tweedie_kernel!(eval::CuDeviceVector{T}, p::CuDeviceMatrix{T}, y::
return nothing
end

function tweedie(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}
function EvoTrees.tweedie(p::CuMatrix{T}, y::CuVector{T}, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}

Check warning on line 136 in ext/EvoTreesCUDAExt/eval.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/eval.jl#L136

Added line #L136 was not covered by tests
threads = min(MAX_THREADS, length(y))
blocks = cld(length(y), threads)
@cuda blocks = blocks threads = threads eval_tweedie_kernel!(eval, p, y, w)
Expand All @@ -158,10 +158,10 @@ function eval_mlogloss_kernel!(eval::CuDeviceVector{T}, p::CuDeviceMatrix{T}, y:
return nothing
end

function mlogloss(p::CuMatrix{T}, y::CuVector, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}
function EvoTrees.mlogloss(p::CuMatrix{T}, y::CuVector, w::CuVector{T}, eval::CuVector{T}; MAX_THREADS=1024, kwargs...) where {T<:AbstractFloat}

Check warning on line 161 in ext/EvoTreesCUDAExt/eval.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/eval.jl#L161

Added line #L161 was not covered by tests
threads = min(MAX_THREADS, length(y))
blocks = cld(length(y), threads)
@cuda blocks = blocks threads = threads eval_mlogloss_kernel!(eval, p, y, w)
CUDA.synchronize()
return sum(eval) / sum(w)
end
end
File renamed without changes.
44 changes: 22 additions & 22 deletions src/gpu/fit.jl → ext/EvoTreesCUDAExt/fit.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
function grow_evotree!(evotree::EvoTree{L,K}, cache, params::EvoTypes{L}, ::Type{GPU}) where {L,K}
function EvoTrees.grow_evotree!(evotree::EvoTree{L,K}, cache, params::EvoTrees.EvoTypes{L}, ::Type{GPU}) where {L,K}

Check warning on line 1 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L1

Added line #L1 was not covered by tests

# compute gradients
update_grads!(cache.∇, cache.pred, cache.y, params)
EvoTrees.update_grads!(cache.∇, cache.pred, cache.y, params)

Check warning on line 4 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L4

Added line #L4 was not covered by tests
# subsample rows
cache.nodes[1].is =
subsample(cache.is_in, cache.is_out, cache.mask, params.rowsample, params.rng)
EvoTrees.subsample(cache.is_in, cache.is_out, cache.mask, params.rowsample, params.rng)
# subsample cols
sample!(params.rng, cache.js_, cache.js, replace=false, ordered=true)
EvoTrees.sample!(params.rng, cache.js_, cache.js, replace=false, ordered=true)

Check warning on line 9 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L9

Added line #L9 was not covered by tests

# assign a root and grow tree
tree = Tree{L,K}(params.max_depth)
tree = EvoTrees.Tree{L,K}(params.max_depth)

Check warning on line 12 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L12

Added line #L12 was not covered by tests
grow! = params.tree_type == "oblivious" ? grow_otree! : grow_tree!
grow!(
tree,
Expand All @@ -34,9 +34,9 @@ end

# grow a single binary tree - grow through all depth
function grow_tree!(
tree::Tree{L,K},
tree::EvoTrees.Tree{L,K},
nodes::Vector{N},
params::EvoTypes{L},
params::EvoTrees.EvoTypes{L},
::CuMatrix,
edges,
js,
Expand Down Expand Up @@ -66,7 +66,7 @@ function grow_tree!(

# initialize summary stats
nodes[1].∑ .= Vector(vec(sum(∇[:, nodes[1].is], dims=2)))
nodes[1].gain = get_gain(params, nodes[1].∑) # should use a GPU version?
nodes[1].gain = EvoTrees.get_gain(params, nodes[1].∑) # should use a GPU version?

Check warning on line 69 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L69

Added line #L69 was not covered by tests

# grow while there are remaining active nodes
while length(n_current) > 0 && depth <= params.max_depth
Expand All @@ -91,13 +91,13 @@ function grow_tree!(
end
end
@threads for n sort(n_current)
update_gains!(nodes[n], js, params, feattypes, monotone_constraints)
EvoTrees.update_gains!(nodes[n], js, params, feattypes, monotone_constraints)

Check warning on line 94 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L94

Added line #L94 was not covered by tests
end
end

for n sort(n_current)
if depth == params.max_depth || nodes[n].∑[end] <= params.min_weight
pred_leaf_cpu!(tree.pred, n, nodes[n].∑, params, ∇, nodes[n].is)
EvoTrees.pred_leaf_cpu!(tree.pred, n, nodes[n].∑, params, ∇, nodes[n].is)

Check warning on line 100 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L100

Added line #L100 was not covered by tests
else
best = findmax(findmax.(nodes[n].gains))
best_gain = best[1][1]
Expand Down Expand Up @@ -126,8 +126,8 @@ function grow_tree!(
nodes[n<<1].is, nodes[n<<1+1].is = _left, _right
nodes[n<<1].∑ .= nodes[n].hL[best_feat][:, best_bin]
nodes[n<<1+1].∑ .= nodes[n].hR[best_feat][:, best_bin]
nodes[n<<1].gain = get_gain(params, nodes[n<<1].∑)
nodes[n<<1+1].gain = get_gain(params, nodes[n<<1+1].∑)
nodes[n<<1].gain = EvoTrees.get_gain(params, nodes[n<<1].∑)
nodes[n<<1+1].gain = EvoTrees.get_gain(params, nodes[n<<1+1].∑)

Check warning on line 130 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L129-L130

Added lines #L129 - L130 were not covered by tests

if length(_right) >= length(_left)
push!(n_next, n << 1)
Expand All @@ -137,7 +137,7 @@ function grow_tree!(
push!(n_next, n << 1)
end
else
pred_leaf_cpu!(tree.pred, n, nodes[n].∑, params, ∇, nodes[n].is)
EvoTrees.pred_leaf_cpu!(tree.pred, n, nodes[n].∑, params, ∇, nodes[n].is)

Check warning on line 140 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L140

Added line #L140 was not covered by tests
end
end
end
Expand All @@ -151,9 +151,9 @@ end

# grow a single oblivious tree - grow through all depth
function grow_otree!(
tree::Tree{L,K},
tree::EvoTrees.Tree{L,K},
nodes::Vector{N},
params::EvoTypes{L},
params::EvoTrees.EvoTypes{L},
::CuMatrix,
edges,
js,
Expand Down Expand Up @@ -183,7 +183,7 @@ function grow_otree!(

# initialize summary stats
nodes[1].∑ .= Vector(vec(sum(∇[:, nodes[1].is], dims=2)))
nodes[1].gain = get_gain(params, nodes[1].∑) # should use a GPU version?
nodes[1].gain = EvoTrees.get_gain(params, nodes[1].∑) # should use a GPU version?

Check warning on line 186 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L186

Added line #L186 was not covered by tests

# grow while there are remaining active nodes
while length(n_current) > 0 && depth <= params.max_depth
Expand All @@ -197,7 +197,7 @@ function grow_otree!(
if depth == params.max_depth || min_weight_flag
for n in n_current
# @info "length(nodes[n].is)" length(nodes[n].is) depth n
pred_leaf_cpu!(tree.pred, n, nodes[n].∑, params, ∇, nodes[n].is)
EvoTrees.pred_leaf_cpu!(tree.pred, n, nodes[n].∑, params, ∇, nodes[n].is)

Check warning on line 200 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L200

Added line #L200 was not covered by tests
end
else
# update histograms
Expand All @@ -218,7 +218,7 @@ function grow_otree!(
end
end
@threads for n n_current
update_gains!(nodes[n], js, params, feattypes, monotone_constraints)
EvoTrees.update_gains!(nodes[n], js, params, feattypes, monotone_constraints)

Check warning on line 221 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L221

Added line #L221 was not covered by tests
end

# initialize gains for node 1 in which all gains of a given depth will be accumulated
Expand Down Expand Up @@ -273,8 +273,8 @@ function grow_otree!(
nodes[n<<1].is, nodes[n<<1+1].is = _left, _right
nodes[n<<1].∑ .= nodes[n].hL[best_feat][:, best_bin]
nodes[n<<1+1].∑ .= nodes[n].hR[best_feat][:, best_bin]
nodes[n<<1].gain = get_gain(params, nodes[n<<1].∑)
nodes[n<<1+1].gain = get_gain(params, nodes[n<<1+1].∑)
nodes[n<<1].gain = EvoTrees.get_gain(params, nodes[n<<1].∑)
nodes[n<<1+1].gain = EvoTrees.get_gain(params, nodes[n<<1+1].∑)

Check warning on line 277 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L276-L277

Added lines #L276 - L277 were not covered by tests

if length(_right) >= length(_left)
push!(n_next, n << 1)
Expand All @@ -286,7 +286,7 @@ function grow_otree!(
end
else
for n in n_current
pred_leaf_cpu!(tree.pred, n, nodes[n].∑, params, ∇, nodes[n].is)
EvoTrees.pred_leaf_cpu!(tree.pred, n, nodes[n].∑, params, ∇, nodes[n].is)

Check warning on line 289 in ext/EvoTreesCUDAExt/fit.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/fit.jl#L289

Added line #L289 was not covered by tests
end
end
end
Expand All @@ -295,4 +295,4 @@ function grow_otree!(
end # end of loop over current nodes for a given depth

return nothing
end
end
46 changes: 23 additions & 23 deletions src/gpu/init.jl → ext/EvoTreesCUDAExt/init.jl
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
function init_core(params::EvoTypes{L}, ::Type{GPU}, data, fnames, y_train, w, offset) where {L}
function EvoTrees.init_core(params::EvoTrees.EvoTypes{L}, ::Type{EvoTrees.GPU}, data, fnames, y_train, w, offset) where {L}

Check warning on line 1 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L1

Added line #L1 was not covered by tests

# binarize data into quantiles
edges, featbins, feattypes = get_edges(data; fnames, nbins=params.nbins, rng=params.rng)
x_bin = CuArray(binarize(data; fnames, edges))
edges, featbins, feattypes = EvoTrees.get_edges(data; fnames, nbins=params.nbins, rng=params.rng)
x_bin = CuArray(EvoTrees.binarize(data; fnames, edges))

Check warning on line 5 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L4-L5

Added lines #L4 - L5 were not covered by tests
nobs, nfeats = size(x_bin)
T = Float32

target_levels = nothing
if L == Logistic
if L == EvoTrees.Logistic

Check warning on line 10 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L10

Added line #L10 was not covered by tests
@assert eltype(y_train) <: Real && minimum(y_train) >= 0 && maximum(y_train) <= 1
K = 1
y = T.(y_train)
μ = [logit(mean(y))]
!isnothing(offset) && (offset .= logit.(offset))
elseif L in [Poisson, Gamma, Tweedie]
μ = [EvoTrees.logit(EvoTrees.mean(y))]
!isnothing(offset) && (offset .= EvoTrees.logit.(offset))
elseif L in [EvoTrees.Poisson, EvoTrees.Gamma, EvoTrees.Tweedie]

Check warning on line 16 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L14-L16

Added lines #L14 - L16 were not covered by tests
@assert eltype(y_train) <: Real
K = 1
y = T.(y_train)
μ = fill(log(mean(y)), 1)
μ = fill(log(EvoTrees.mean(y)), 1)

Check warning on line 20 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L20

Added line #L20 was not covered by tests
!isnothing(offset) && (offset .= log.(offset))
elseif L == MLogLoss
if eltype(y_train) <: CategoricalValue
target_levels = CategoricalArrays.levels(y_train)
y = UInt32.(CategoricalArrays.levelcode.(y_train))
elseif L == EvoTrees.MLogLoss
if eltype(y_train) <: EvoTrees.CategoricalValue
target_levels = EvoTrees.CategoricalArrays.levels(y_train)
y = UInt32.(EvoTrees.CategoricalArrays.levelcode.(y_train))

Check warning on line 25 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L22-L25

Added lines #L22 - L25 were not covered by tests
elseif eltype(y_train) <: Integer || eltype(y_train) <: Bool || eltype(y_train) <: String || eltype(y_train) <: Char
target_levels = sort(unique(y_train))
yc = CategoricalVector(y_train, levels=target_levels)
y = UInt32.(CategoricalArrays.levelcode.(yc))
yc = EvoTrees.CategoricalVector(y_train, levels=target_levels)
y = UInt32.(EvoTrees.CategoricalArrays.levelcode.(yc))

Check warning on line 29 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L28-L29

Added lines #L28 - L29 were not covered by tests
else
@error "Invalid target eltype: $(eltype(y_train))"
end
K = length(target_levels)
μ = T.(log.(proportions(y, UInt32(1):UInt32(K))))
μ = T.(log.(EvoTrees.proportions(y, UInt32(1):UInt32(K))))

Check warning on line 34 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L34

Added line #L34 was not covered by tests
μ .-= maximum(μ)
!isnothing(offset) && (offset .= log.(offset))
elseif L == GaussianMLE
elseif L == EvoTrees.GaussianMLE

Check warning on line 37 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L37

Added line #L37 was not covered by tests
@assert eltype(y_train) <: Real
K = 2
y = T.(y_train)
μ = [mean(y), log(std(y))]
μ = [EvoTrees.mean(y), log(EvoTrees.std(y))]

Check warning on line 41 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L41

Added line #L41 was not covered by tests
!isnothing(offset) && (offset[:, 2] .= log.(offset[:, 2]))
elseif L == LogisticMLE
elseif L == EvoTrees.LogisticMLE

Check warning on line 43 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L43

Added line #L43 was not covered by tests
@assert eltype(y_train) <: Real
K = 2
y = T.(y_train)
μ = [mean(y), log(std(y) * sqrt(3) / π)]
μ = [EvoTrees.mean(y), log(EvoTrees.std(y) * sqrt(3) / π)]

Check warning on line 47 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L47

Added line #L47 was not covered by tests
!isnothing(offset) && (offset[:, 2] .= log.(offset[:, 2]))
else
@assert eltype(y_train) <: Real
K = 1
y = T.(y_train)
μ = [mean(y)]
μ = [EvoTrees.mean(y)]

Check warning on line 53 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L53

Added line #L53 was not covered by tests
end
y = CuArray(y)
μ = T.(μ)
Expand Down Expand Up @@ -94,8 +94,8 @@ function init_core(params::EvoTypes{L}, ::Type{GPU}, data, fnames, y_train, w, o
)

# initialize model
nodes = [TrainNode(featbins, K, view(is_in, 1:0)) for n = 1:2^params.max_depth-1]
bias = [Tree{L,K}(μ)]
nodes = [EvoTrees.TrainNode(featbins, K, view(is_in, 1:0)) for n = 1:2^params.max_depth-1]
bias = [EvoTrees.Tree{L,K}(μ)]

Check warning on line 98 in ext/EvoTreesCUDAExt/init.jl

View check run for this annotation

Codecov / codecov/patch

ext/EvoTreesCUDAExt/init.jl#L97-L98

Added lines #L97 - L98 were not covered by tests
m = EvoTree{L,K}(bias, info)

# build cache
Expand Down Expand Up @@ -125,4 +125,4 @@ function init_core(params::EvoTypes{L}, ::Type{GPU}, data, fnames, y_train, w, o
monotone_constraints=monotone_constraints,
)
return m, cache
end
end
Loading

0 comments on commit 8a42cef

Please sign in to comment.