-
Notifications
You must be signed in to change notification settings - Fork 10
/
wrapper.jl
786 lines (668 loc) · 23.3 KB
/
wrapper.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
const DatasetHandle = Ptr{Nothing}
const BoosterHandle = Ptr{Nothing}
const C_API_DTYPE_FLOAT32 = 0
const C_API_DTYPE_FLOAT64 = 1
const C_API_DTYPE_INT32 = 2
const C_API_DTYPE_INT64 = 3
const C_API_MATRIX_TYPE_CSC = 1
const C_API_MATRIX_TYPE_CSR = 0
mutable struct Dataset
handle::DatasetHandle
function Dataset(handle::DatasetHandle)
ds = new(handle)
finalizer(Dataset_finalizer, ds)
return ds
end
function Dataset_finalizer(ds::Dataset)
if ds.handle != C_NULL
LGBM_DatasetFree(ds)
end
end
end
mutable struct Booster
handle::BoosterHandle
datasets::Vector{Dataset}
function Booster(handle::BoosterHandle, datasets::Vector{Dataset})
bst = new(handle, datasets)
finalizer(Booster_finalizer, bst)
return bst
end
function Booster_finalizer(bst::Booster)
if bst.handle != C_NULL
LGBM_BoosterFree(bst)
end
end
end
function Booster()
return Booster(C_NULL, Dataset[])
end
function Booster(handle::BoosterHandle)
return Booster(handle, Dataset[])
end
# deepcopy utils, but we can't reasonably do this for datasets
function Base.deepcopy_internal(x::Booster, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
end
if x.handle == C_NULL
# just init a new object
return Booster()
end
serialised = LGBM_BoosterSaveModelToString(x)
y = LGBM_BoosterLoadModelFromString(serialised)
return y
end
function jltype_to_lgbmid(datatype::Type)
if datatype == Float32
return C_API_DTYPE_FLOAT32
elseif datatype == Float64
return C_API_DTYPE_FLOAT64
elseif datatype == Int32
return C_API_DTYPE_INT32
elseif datatype == Int64
return C_API_DTYPE_INT64
else
error("unsupported datatype, got ", datatype)
end
end
function lgbmid_to_jltype(id::Integer)
if id == C_API_DTYPE_FLOAT32
return Float32
elseif id == C_API_DTYPE_FLOAT64
return Float64
elseif id == C_API_DTYPE_INT32
return Int32
elseif id == C_API_DTYPE_INT64
return Int64
else
error("unknown LightGBM return type id, got ", id)
end
end
# sparse helpers for getting the API enums
sparsetypes(x::SparseArrays.SparseMatrixCSC) = sparseidxtype(x), sparsedatatype(x)
sparseidxtype(x::SparseArrays.SparseMatrixCSC{<:Any, Int64}) = C_API_DTYPE_INT64
sparseidxtype(x::SparseArrays.SparseMatrixCSC{<:Any, Int32}) = C_API_DTYPE_INT32
sparsedatatype(x::SparseArrays.SparseMatrixCSC{Float32, <:Integer}) = C_API_DTYPE_FLOAT32
sparsedatatype(x::SparseArrays.SparseMatrixCSC{Float64, <:Integer}) = C_API_DTYPE_FLOAT64
sparsedatatype(x::SparseArrays.SparseMatrixCSC{<:Any, <:Integer}) = throw(TypeError(:sparsedatatype, AbstractFloat, one(eltype(x.nzval))))
# Floating point conversion helpers
tofloat32(x::Vector{<:AbstractFloat}) = Float32.(x)
tofloat32(x::Vector{Float32}) = x
macro lightgbm(f, params...)
return quote
call_sym = Libdl.dlsym(LGBM_library[], $f)
err_sym = Libdl.dlsym(LGBM_library[], :LGBM_GetLastError)
err = ccall(call_sym, Cint,
($((esc(i.args[end]) for i in params)...),),
$((esc(i.args[end - 1]) for i in params)...))
if err != 0
msg = unsafe_string(ccall(err_sym, Cstring, ()))
error("call to LightGBM's ", string($(esc(f))), " failed: ", msg)
end
end
end
# function LGBM_DatasetCreateFromFile()
function LGBM_DatasetCreateFromCSC(
data::SparseArrays.SparseMatrixCSC,
parameters::String,
reference::Dataset = Dataset(C_NULL),
)
if data.m > typemax(Int32)
throw(DomainError(data.m, "Cannot accept CSC matrices with more than $(typemax(Int32)) rows"))
end
idx_type, data_type = sparsetypes(data)
out = Ref{DatasetHandle}()
@lightgbm(
:LGBM_DatasetCreateFromCSC,
data.colptr .- 1 => Ptr{Nothing},
idx_type => Cint,
Int32.(data.rowval .- 1) => Ptr{Cint},
data.nzval => Ptr{Nothing},
data_type => Cint,
data.n + 1 => Clonglong,
SparseArrays.nnz(data) => Clonglong,
data.m => Clonglong,
parameters => Cstring,
reference.handle => DatasetHandle,
out => Ref{DatasetHandle}
)
return Dataset(out[])
end
function LGBM_DatasetCreateFromMat(
data::Matrix{T},
parameters::String,
is_row_major::Bool = false
) where T<:Union{Float32,Float64}
lgbm_data_type = jltype_to_lgbmid(T)
nrow, ncol = ifelse(is_row_major, reverse(size(data)), size(data))
out = Ref{DatasetHandle}()
@lightgbm(
:LGBM_DatasetCreateFromMat,
data => Ptr{Nothing},
lgbm_data_type => Cint,
nrow => Int32,
ncol => Int32,
is_row_major => Cint,
parameters => Cstring,
C_NULL => Ptr{Nothing},
out => Ref{DatasetHandle}
)
return Dataset(out[])
end
function LGBM_DatasetCreateFromMat(data::Matrix{T}, parameters::String, is_row_major::Bool = false) where T<:Real
return LGBM_DatasetCreateFromMat(convert(Matrix{Float64}, data), parameters, is_row_major)
end
function LGBM_DatasetCreateFromMat(
data::Matrix{T},
parameters::String,
reference::Dataset,
is_row_major::Bool = false
) where T<:Union{Float32,Float64}
lgbm_data_type = jltype_to_lgbmid(T)
nrow, ncol = ifelse(is_row_major, reverse(size(data)), size(data))
out = Ref{DatasetHandle}()
@lightgbm(
:LGBM_DatasetCreateFromMat,
data => Ptr{Nothing},
lgbm_data_type => Cint,
nrow => Int32,
ncol => Int32,
is_row_major => Cint,
parameters => Cstring,
reference.handle => DatasetHandle,
out => Ref{DatasetHandle}
)
return Dataset(out[])
end
function LGBM_DatasetCreateFromMat(data::Matrix{T}, parameters::String,
reference::Dataset, is_row_major::Bool = false) where T<:Real
return LGBM_DatasetCreateFromMat(convert(Matrix{Float64}, data), parameters, reference,
is_row_major)
end
# Automatically converts to C's zero-based indices.
function LGBM_DatasetGetSubset(ds::Dataset, used_row_indices::Vector{Int32}, parameters::String)
num_used_row_indices = length(used_row_indices)
for idx in 1:num_used_row_indices
@inbounds used_row_indices[idx] -= 1
end
out = Ref{DatasetHandle}()
@lightgbm(:LGBM_DatasetGetSubset,
ds.handle => DatasetHandle,
used_row_indices => Ref{Int32},
num_used_row_indices => Int32,
parameters => Cstring,
out => Ref{DatasetHandle})
for idx in 1:num_used_row_indices
@inbounds used_row_indices[idx] += 1
end
return Dataset(out[])
end
function LGBM_DatasetGetSubset(ds::Dataset, used_row_indices::Vector{Int64}, parameters::String)
LGBM_DatasetGetSubset(ds, convert(Vector{Int32}, used_row_indices), parameters)
end
function LGBM_DatasetSetFeatureNames(ds::Dataset, feature_names::Vector{String})
num_feature_names = length(feature_names)
@lightgbm(:LGBM_DatasetSetFeatureNames,
ds.handle => DatasetHandle,
feature_names => Ref{Cstring},
num_feature_names => Cint)
end
function LGBM_DatasetGetFeatureNames(ds::Dataset)
len = Cint(2)
buffer_len = Csize_t(2)
feature_names = [Vector{UInt8}(undef, buffer_len) for i in 1:len]
# setting these so that the first C API call informs us of their allocations
num_feature_names = Ref{Cint}()
out_buffer_len = Ref{Csize_t}()
@lightgbm(
:LGBM_DatasetGetFeatureNames,
ds.handle => DatasetHandle,
len => Cint,
num_feature_names => Ref{Cint},
buffer_len => Csize_t,
out_buffer_len => Ref{Csize_t},
feature_names => Ref{Ptr{UInt8}}
)
# allocating memory
new_len = num_feature_names[]
new_buffer_len = out_buffer_len[]
feature_names = [Vector{UInt8}(undef, new_buffer_len) for i in 1:new_len]
@lightgbm(
:LGBM_DatasetGetFeatureNames,
ds.handle => DatasetHandle,
new_len => Cint,
num_feature_names => Ref{Cint},
new_buffer_len => Csize_t,
out_buffer_len => Ref{Csize_t},
feature_names => Ref{Ptr{UInt8}}
)
return [unsafe_string(pointer(feature_name)) for feature_name in feature_names[1:num_feature_names[]]]
end
function LGBM_DatasetFree(ds::Dataset)
@lightgbm(:LGBM_DatasetFree,
ds.handle => DatasetHandle)
ds.handle = C_NULL # avoid a class of double free bugs please
return nothing
end
function LGBM_DatasetSaveBinary(ds::Dataset, filename::String)
@lightgbm(:LGBM_DatasetSaveBinary,
ds.handle => DatasetHandle,
filename => Cstring)
return nothing
end
function _LGBM_DatasetSetField(ds::Dataset, field_name::String,
field_data::Vector{T}) where T <:Union{Float32,Float64,Int32}
data_type = jltype_to_lgbmid(T)
num_element = length(field_data)
@lightgbm(
:LGBM_DatasetSetField,
ds.handle => DatasetHandle,
field_name => Cstring,
field_data => Ptr{Nothing},
num_element => Cint,
data_type => Cint
)
return nothing
end
# the below type conversions are required by C API for the different fieldnames
# ideally we should enforce the correct types through signature and multiple dispatch accordingly
# but this would restrict the inputs from MLJInterface which don't necessarily have
# exact matching data types for these fields hence leaving the conversions here before passing to the
# wrapper function
function LGBM_DatasetSetField(ds::Dataset, field_name::String, field_data::Vector{T}) where T<:Real
if field_name == "label" || field_name == "weight"
_LGBM_DatasetSetField(ds, field_name, convert(Vector{Float32}, field_data))
elseif field_name == "init_score"
_LGBM_DatasetSetField(ds, field_name, convert(Vector{Float64}, field_data))
elseif field_name == "group"
_LGBM_DatasetSetField(ds, field_name, convert(Vector{Int32}, field_data))
end
return nothing
end
function LGBM_DatasetGetField(ds::Dataset, field_name::String)
out_len = Ref{Cint}()
out_ptr = Ref{Ptr{Nothing}}()
out_type = Ref{Cint}()
@lightgbm(
:LGBM_DatasetGetField,
ds.handle => DatasetHandle,
field_name => Cstring,
out_len => Ref{Cint},
out_ptr => Ref{Ptr{Nothing}},
out_type => Ref{Cint}
)
jl_out_type = lgbmid_to_jltype(out_type[])
jl_out_ptr = convert(Ptr{jl_out_type}, out_ptr[])
return copy(unsafe_wrap(Vector{jl_out_type}, jl_out_ptr, out_len[], own=false))
end
function LGBM_DatasetGetNumData(ds::Dataset)
out = Ref{Cint}()
@lightgbm(:LGBM_DatasetGetNumData,
ds.handle => DatasetHandle,
out => Ref{Cint})
return out[]
end
function LGBM_DatasetGetNumFeature(ds::Dataset)
out = Ref{Cint}()
@lightgbm(:LGBM_DatasetGetNumFeature,
ds.handle => DatasetHandle,
out => Ref{Cint})
return out[]
end
function LGBM_BoosterCreate(train_data::Dataset, parameters::String)
out = Ref{BoosterHandle}()
@lightgbm(:LGBM_BoosterCreate,
train_data.handle => DatasetHandle,
parameters => Cstring,
out => Ref{BoosterHandle})
return Booster(out[], [train_data])
end
function LGBM_BoosterCreateFromModelfile(filename::String)
out_num_iterations = Ref{Cint}()
out = Ref{BoosterHandle}()
@lightgbm(:LGBM_BoosterCreateFromModelfile,
filename => Cstring,
out_num_iterations => Ref{Cint},
out => Ref{BoosterHandle})
return Booster(out[])
end
function LGBM_BoosterLoadModelFromString(model_str::String)
out_num_iterations = Ref{Cint}()
out = Ref{BoosterHandle}()
@lightgbm(:LGBM_BoosterLoadModelFromString,
model_str => Cstring,
out_num_iterations => Ref{Cint},
out => Ref{BoosterHandle})
return Booster(out[])
end
function LGBM_BoosterFree(bst::Booster)
@lightgbm(:LGBM_BoosterFree,
bst.handle => BoosterHandle)
bst.handle = C_NULL # avoid a class of double free bugs
return nothing
end
function LGBM_BoosterMerge(bst::Booster, other_booster::Booster)
@lightgbm(:LGBM_BoosterMerge,
bst.handle => BoosterHandle,
other_booster.handle => BoosterHandle)
return nothing
end
function LGBM_BoosterAddValidData(bst::Booster, valid_data::Dataset)
@lightgbm(:LGBM_BoosterAddValidData,
bst.handle => BoosterHandle,
valid_data.handle => DatasetHandle)
push!(bst.datasets, valid_data)
return nothing
end
function LGBM_BoosterResetTrainingData(bst::Booster, train_data::Dataset)
@lightgbm(:LGBM_BoosterResetTrainingData,
bst.handle => BoosterHandle,
train_data.handle => DatasetHandle)
if length(bst.datasets) > 0
@inbounds bst.datasets[1] = train_data
else
bst.datasets = [train_data]
end
return nothing
end
function LGBM_BoosterResetParameter(bst::Booster, parameters::String)
@lightgbm(:LGBM_BoosterResetParameter,
bst.handle => BoosterHandle,
parameters => Cstring)
return nothing
end
function LGBM_BoosterGetNumClasses(bst::Booster)
out_len = Ref{Cint}()
@lightgbm(:LGBM_BoosterGetNumClasses,
bst.handle => BoosterHandle,
out_len => Ref{Cint})
return out_len[]
end
function LGBM_BoosterUpdateOneIter(bst::Booster)
is_finished = Ref{Cint}()
@lightgbm(:LGBM_BoosterUpdateOneIter,
bst.handle => BoosterHandle,
is_finished => Ref{Cint})
return is_finished[]
end
"""
LGBM_BoosterUpdateOneIterCustom
Pass grads and 2nd derivatives corresponding to some custom loss function
grads and 2nd derivatives must be same cardinality as training data * number of models
Also, trying to run this on a booster without data will fail.
"""
function LGBM_BoosterUpdateOneIterCustom(bst::Booster, grads::Vector{<:AbstractFloat}, hessian::Vector{<:AbstractFloat})
if length(bst.datasets) == 0
throw(ErrorException("Booster does not have any training data associated"))
end
numdata = LGBM_DatasetGetNumData(first(bst.datasets))
nummodels = LGBM_BoosterNumModelPerIteration(bst)
if !((numdata*nummodels) == length(grads) == length(hessian))
throw(DimensionMismatch(
"Gradients sizes ($(length(grads)), $(length(hessian))) don't match training data size ($numdata) * ($nummodels)"
))
end
grads = tofloat32(grads)
hessian = tofloat32(hessian)
is_finished = Ref{Cint}()
@lightgbm(:LGBM_BoosterUpdateOneIterCustom,
bst.handle => BoosterHandle,
grads => Ptr{Cfloat},
hessian => Ptr{Cfloat},
is_finished => Ref{Cint})
return is_finished[]
end
function LGBM_BoosterRollbackOneIter(bst::Booster)
@lightgbm(:LGBM_BoosterRollbackOneIter,
bst.handle => BoosterHandle)
return nothing
end
function LGBM_BoosterGetCurrentIteration(bst::Booster)
out_iteration = Ref{Cint}()
@lightgbm(:LGBM_BoosterGetCurrentIteration,
bst.handle => BoosterHandle,
out_iteration => Ref{Cint})
return out_iteration[]
end
function LGBM_BoosterGetEvalCounts(bst::Booster)
out_len = Ref{Cint}()
@lightgbm(:LGBM_BoosterGetEvalCounts,
bst.handle => BoosterHandle,
out_len => Ref{Cint})
return out_len[]
end
function LGBM_BoosterGetEvalNames(bst::Booster)
len = Cint(2)
buffer_len = Csize_t(2)
out_strs = [Vector{UInt8}(undef, buffer_len) for i in 1:len]
# setting these so that the first C API call informs us of their allocations
out_len = Ref{Cint}()
out_buffer_len = Ref{Csize_t}()
@lightgbm(
:LGBM_BoosterGetEvalNames,
bst.handle => BoosterHandle,
len => Cint,
out_len => Ref{Cint},
buffer_len => Csize_t,
out_buffer_len => Ref{Csize_t},
out_strs => Ref{Ptr{UInt8}}
)
# allocating memory
new_len = out_len[]
new_buffer_len = out_buffer_len[]
out_strs = [Vector{UInt8}(undef, new_buffer_len) for i in 1:new_len]
@lightgbm(
:LGBM_BoosterGetEvalNames,
bst.handle => BoosterHandle,
new_len => Cint,
out_len => Ref{Cint},
new_buffer_len => Csize_t,
out_buffer_len => Ref{Csize_t},
out_strs => Ref{Ptr{UInt8}}
)
jl_out_strs = [unsafe_string(pointer(out_str)) for out_str in out_strs[1:out_len[]]]
return jl_out_strs
end
function LGBM_BoosterGetFeatureNames(bst::Booster)
len = Cint(2)
buffer_len = Csize_t(2)
out_strs = [Vector{UInt8}(undef, buffer_len) for i in 1:len]
# setting these so that the first C API call informs us of their allocations
out_len = Ref{Cint}()
out_buffer_len = Ref{Csize_t}()
@lightgbm(
:LGBM_BoosterGetFeatureNames,
bst.handle => BoosterHandle,
len => Cint,
out_len => Ref{Cint},
buffer_len => Csize_t,
out_buffer_len => Ref{Csize_t},
out_strs => Ref{Ptr{UInt8}}
)
# allocating memory
new_len = out_len[]
new_buffer_len = out_buffer_len[]
out_strs = [Vector{UInt8}(undef, new_buffer_len) for i in 1:new_len]
@lightgbm(
:LGBM_BoosterGetFeatureNames,
bst.handle => BoosterHandle,
new_len => Cint,
out_len => Ref{Cint},
new_buffer_len => Csize_t,
out_buffer_len => Ref{Csize_t},
out_strs => Ref{Ptr{UInt8}}
)
jl_out_strs = [unsafe_string(pointer(out_str)) for out_str in out_strs[1:out_len[]]]
return jl_out_strs
end
function LGBM_BoosterGetNumFeature(bst::Booster)
out_len = Ref{Cint}()
@lightgbm(:LGBM_BoosterGetNumFeature,
bst.handle => BoosterHandle,
out_len => Ref{Cint})
return out_len[]
end
function LGBM_BoosterGetEval(bst::Booster, data::Integer)
n_metrics = LGBM_BoosterGetEvalCounts(bst)
out_results = Array{Cdouble}(undef, n_metrics)
out_len = Ref{Cint}()
@lightgbm(:LGBM_BoosterGetEval,
bst.handle => BoosterHandle,
data => Cint,
out_len => Ref{Cint},
out_results => Ref{Cdouble})
return out_results[1:out_len[]]
end
function LGBM_BoosterGetNumPredict(bst::Booster, data_idx::Integer)
out_len = Ref{Int64}()
@lightgbm(:LGBM_BoosterGetNumPredict,
bst.handle => BoosterHandle,
data_idx => Cint,
out_len => Ref{Int64})
return out_len[]
end
function LGBM_BoosterGetPredict(bst::Booster, data_idx::Integer)
out_len = Ref{Int64}()
num_class = LGBM_BoosterGetNumClasses(bst)
num_data = LGBM_BoosterGetNumPredict(bst, data_idx)
out_results = Array{Cdouble}(undef, num_class * num_data)
@lightgbm(:LGBM_BoosterGetPredict,
bst.handle => BoosterHandle,
data_idx => Cint,
out_len => Ref{Int64},
out_results => Ref{Cdouble})
return out_results[1:out_len[]]
end
# function LGBM_BoosterPredictForFile()
function LGBM_BoosterCalcNumPredict(bst::Booster, num_row::Integer, predict_type::Integer, start_iteration::Integer,
num_iteration::Int)
out_len = Ref{Int64}()
@lightgbm(
:LGBM_BoosterCalcNumPredict,
bst.handle => BoosterHandle,
num_row => Cint,
predict_type => Cint,
start_iteration => Cint,
num_iteration => Cint,
out_len => Ref{Int64}
)
return out_len[]
end
# function LGBM_BoosterPredictForCSR()
# function LGBM_BoosterPredictForCSC()
function LGBM_BoosterPredictForMat(
bst::Booster,
data::Matrix{T},
predict_type::Integer,
start_iteration::Integer,
num_iteration::Integer,
is_row_major::Bool = false,
num_threads::Integer = -1
) where T<:Union{Float32,Float64}
lgbm_data_type = jltype_to_lgbmid(T)
nrow, ncol = ifelse(is_row_major, reverse(size(data)), size(data))
out_len = Ref{Int64}()
alloc_len = LGBM_BoosterCalcNumPredict(bst, nrow, predict_type, start_iteration, num_iteration)
out_result = Array{Cdouble}(undef, alloc_len)
parameter = "" # full prediction, no early stopping
if num_threads > 0
parameter = "num_threads=$num_threads"
end
@lightgbm(
:LGBM_BoosterPredictForMat,
bst.handle => BoosterHandle,
data => Ptr{Nothing},
lgbm_data_type => Cint,
nrow => Int32,
ncol => Int32,
is_row_major => Cint,
predict_type => Cint,
start_iteration => Cint,
num_iteration => Cint,
parameter => Cstring,
out_len => Ref{Int64},
out_result => Ref{Cdouble}
)
return out_result[1:out_len[]]
end
function LGBM_BoosterPredictForMat(bst::Booster, data::Matrix{T}, predict_type::Integer,
num_iteration::Integer) where T<:Real
return LGBM_BoosterPredictForMat(bst, convert(Matrix{Float64}, data), predict_type,
num_iteration)
end
function LGBM_BoosterSaveModel(
bst::Booster,
start_iteration::Integer,
num_iteration::Integer,
feature_importance_type::Integer,
filename::String
)
@lightgbm(
:LGBM_BoosterSaveModel,
bst.handle => BoosterHandle,
start_iteration => Cint,
num_iteration => Cint,
feature_importance_type => Cint,
filename => Cstring
)
return nothing
end
function LGBM_BoosterSaveModelToString(
bst::Booster,
start_iteration::Integer=0,
num_iteration::Integer=0,
feature_importance_type::Integer=0
)::String
# places for the call to write to
out_len = Ref{Int64}()
out_str = Vector{UInt8}(undef, 2)
buffer_len = Int64(1)
# first time will not work, we calling it to be told out_len
@lightgbm(
:LGBM_BoosterSaveModelToString,
bst.handle => BoosterHandle,
start_iteration => Cint,
num_iteration => Cint,
feature_importance_type => Cint,
buffer_len => Int64,
out_len => Ref{Int64},
out_str => Ref{UInt8}
)
out_str = Vector{UInt8}(undef, out_len[] + 1)
buffer_len = out_len[]
# now it works, and we have our serialised model in out_str, in c-memory
@lightgbm(
:LGBM_BoosterSaveModelToString,
bst.handle => BoosterHandle,
start_iteration => Cint,
num_iteration => Cint,
feature_importance_type => Cint,
buffer_len => Int64,
out_len => Ref{Int64},
out_str => Ref{UInt8}
)
jl_out_str = unsafe_string(pointer(out_str))
return jl_out_str
end
function LGBM_BoosterFeatureImportance(bst::Booster, num_iteration::Integer, importance_type::Integer)::Vector{Float64}
num_features = LGBM_BoosterGetNumFeature(bst)
out_result = Array{Cdouble}(undef, num_features)
@lightgbm(
:LGBM_BoosterFeatureImportance,
bst.handle => BoosterHandle,
num_iteration => Cint,
importance_type => Cint,
out_result => Ref{Cdouble},
)
return out_result
end
# function LGBM_BoosterDumpModel()
# function LGBM_BoosterGetLeafValue()
# function LGBM_BoosterSetLeafValue()
function LGBM_BoosterNumModelPerIteration(bst::Booster)
out_models = Ref{Cint}()
@lightgbm(:LGBM_BoosterNumModelPerIteration,
bst.handle => BoosterHandle,
out_models => Ref{Cint})
return out_models[]
end