@@ -268,8 +268,8 @@ cdef inline double log1pexp(double x) noexcept nogil:
268
268
269
269
cdef inline void sum_exp_minus_max(
270
270
const int i,
271
- const Y_DTYPE_C [:, :] raw_prediction, # IN
272
- Y_DTYPE_C *p # OUT
271
+ const floating_in [:, :] raw_prediction, # IN
272
+ floating_in *p # OUT
273
273
) noexcept nogil:
274
274
# Thread local buffers are used to stores results of this function via p.
275
275
# The results are stored as follows:
@@ -744,7 +744,7 @@ cdef inline double_pair cgrad_hess_half_binomial(
744
744
double raw_prediction
745
745
) noexcept nogil:
746
746
# with y_pred = expit(raw)
747
- # hessian = y_pred * (1 - y_pred) = exp(raw) / (1 + exp(raw))**2
747
+ # hessian = y_pred * (1 - y_pred) = exp( raw) / (1 + exp( raw))**2
748
748
# = exp(-raw) / (1 + exp(-raw))**2
749
749
cdef double_pair gh
750
750
gh.val2 = exp(-raw_prediction) # used as temporary
@@ -835,7 +835,9 @@ cdef class CyLossFunction:
835
835
"""
836
836
pass
837
837
838
- cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) noexcept nogil:
838
+ cdef double_pair cy_grad_hess(
839
+ self, double y_true, double raw_prediction
840
+ ) noexcept nogil:
839
841
"""Compute gradient and hessian.
840
842
841
843
Gradient and hessian of loss w.r.t. raw_prediction for a single sample.
@@ -862,10 +864,10 @@ cdef class CyLossFunction:
862
864
863
865
def loss(
864
866
self,
865
- const Y_DTYPE_C [::1] y_true, # IN
866
- const Y_DTYPE_C [::1] raw_prediction, # IN
867
- const Y_DTYPE_C [::1] sample_weight, # IN
868
- G_DTYPE_C [::1] loss_out, # OUT
867
+ const floating_in [::1] y_true, # IN
868
+ const floating_in [::1] raw_prediction, # IN
869
+ const floating_in [::1] sample_weight, # IN
870
+ floating_out [::1] loss_out, # OUT
869
871
int n_threads=1
870
872
):
871
873
"""Compute the pointwise loss value for each input.
@@ -892,10 +894,10 @@ cdef class CyLossFunction:
892
894
893
895
def gradient(
894
896
self,
895
- const Y_DTYPE_C [::1] y_true, # IN
896
- const Y_DTYPE_C [::1] raw_prediction, # IN
897
- const Y_DTYPE_C [::1] sample_weight, # IN
898
- G_DTYPE_C [::1] gradient_out, # OUT
897
+ const floating_in [::1] y_true, # IN
898
+ const floating_in [::1] raw_prediction, # IN
899
+ const floating_in [::1] sample_weight, # IN
900
+ floating_out [::1] gradient_out, # OUT
899
901
int n_threads=1
900
902
):
901
903
"""Compute gradient of loss w.r.t raw_prediction for each input.
@@ -922,11 +924,11 @@ cdef class CyLossFunction:
922
924
923
925
def loss_gradient(
924
926
self,
925
- const Y_DTYPE_C [::1] y_true, # IN
926
- const Y_DTYPE_C [::1] raw_prediction, # IN
927
- const Y_DTYPE_C [::1] sample_weight, # IN
928
- G_DTYPE_C [::1] loss_out, # OUT
929
- G_DTYPE_C [::1] gradient_out, # OUT
927
+ const floating_in [::1] y_true, # IN
928
+ const floating_in [::1] raw_prediction, # IN
929
+ const floating_in [::1] sample_weight, # IN
930
+ floating_out [::1] loss_out, # OUT
931
+ floating_out [::1] gradient_out, # OUT
930
932
int n_threads=1
931
933
):
932
934
"""Compute loss and gradient of loss w.r.t raw_prediction.
@@ -960,11 +962,11 @@ cdef class CyLossFunction:
960
962
961
963
def gradient_hessian(
962
964
self,
963
- const Y_DTYPE_C [::1] y_true, # IN
964
- const Y_DTYPE_C [::1] raw_prediction, # IN
965
- const Y_DTYPE_C [::1] sample_weight, # IN
966
- G_DTYPE_C [::1] gradient_out, # OUT
967
- G_DTYPE_C [::1] hessian_out, # OUT
965
+ const floating_in [::1] y_true, # IN
966
+ const floating_in [::1] raw_prediction, # IN
967
+ const floating_in [::1] sample_weight, # IN
968
+ floating_out [::1] gradient_out, # OUT
969
+ floating_out [::1] hessian_out, # OUT
968
970
int n_threads=1
969
971
):
970
972
"""Compute gradient and hessian of loss w.r.t raw_prediction.
@@ -1022,10 +1024,10 @@ cdef class {{name}}(CyLossFunction):
1022
1024
1023
1025
def loss(
1024
1026
self,
1025
- const Y_DTYPE_C [::1] y_true, # IN
1026
- const Y_DTYPE_C [::1] raw_prediction, # IN
1027
- const Y_DTYPE_C [::1] sample_weight, # IN
1028
- G_DTYPE_C [::1] loss_out, # OUT
1027
+ const floating_in [::1] y_true, # IN
1028
+ const floating_in [::1] raw_prediction, # IN
1029
+ const floating_in [::1] sample_weight, # IN
1030
+ floating_out [::1] loss_out, # OUT
1029
1031
int n_threads=1
1030
1032
):
1031
1033
cdef:
@@ -1048,11 +1050,11 @@ cdef class {{name}}(CyLossFunction):
1048
1050
{{if closs_grad is not None}}
1049
1051
def loss_gradient(
1050
1052
self,
1051
- const Y_DTYPE_C [::1] y_true, # IN
1052
- const Y_DTYPE_C [::1] raw_prediction, # IN
1053
- const Y_DTYPE_C [::1] sample_weight, # IN
1054
- G_DTYPE_C [::1] loss_out, # OUT
1055
- G_DTYPE_C [::1] gradient_out, # OUT
1053
+ const floating_in [::1] y_true, # IN
1054
+ const floating_in [::1] raw_prediction, # IN
1055
+ const floating_in [::1] sample_weight, # IN
1056
+ floating_out [::1] loss_out, # OUT
1057
+ floating_out [::1] gradient_out, # OUT
1056
1058
int n_threads=1
1057
1059
):
1058
1060
cdef:
@@ -1080,10 +1082,10 @@ cdef class {{name}}(CyLossFunction):
1080
1082
1081
1083
def gradient(
1082
1084
self,
1083
- const Y_DTYPE_C [::1] y_true, # IN
1084
- const Y_DTYPE_C [::1] raw_prediction, # IN
1085
- const Y_DTYPE_C [::1] sample_weight, # IN
1086
- G_DTYPE_C [::1] gradient_out, # OUT
1085
+ const floating_in [::1] y_true, # IN
1086
+ const floating_in [::1] raw_prediction, # IN
1087
+ const floating_in [::1] sample_weight, # IN
1088
+ floating_out [::1] gradient_out, # OUT
1087
1089
int n_threads=1
1088
1090
):
1089
1091
cdef:
@@ -1105,11 +1107,11 @@ cdef class {{name}}(CyLossFunction):
1105
1107
1106
1108
def gradient_hessian(
1107
1109
self,
1108
- const Y_DTYPE_C [::1] y_true, # IN
1109
- const Y_DTYPE_C [::1] raw_prediction, # IN
1110
- const Y_DTYPE_C [::1] sample_weight, # IN
1111
- G_DTYPE_C [::1] gradient_out, # OUT
1112
- G_DTYPE_C [::1] hessian_out, # OUT
1110
+ const floating_in [::1] y_true, # IN
1111
+ const floating_in [::1] raw_prediction, # IN
1112
+ const floating_in [::1] sample_weight, # IN
1113
+ floating_out [::1] gradient_out, # OUT
1114
+ floating_out [::1] hessian_out, # OUT
1113
1115
int n_threads=1
1114
1116
):
1115
1117
cdef:
@@ -1158,18 +1160,18 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1158
1160
# opposite are welcome.
1159
1161
def loss(
1160
1162
self,
1161
- const Y_DTYPE_C [::1] y_true, # IN
1162
- const Y_DTYPE_C [:, :] raw_prediction, # IN
1163
- const Y_DTYPE_C [::1] sample_weight, # IN
1164
- G_DTYPE_C [::1] loss_out, # OUT
1163
+ const floating_in [::1] y_true, # IN
1164
+ const floating_in [:, :] raw_prediction, # IN
1165
+ const floating_in [::1] sample_weight, # IN
1166
+ floating_out [::1] loss_out, # OUT
1165
1167
int n_threads=1
1166
1168
):
1167
1169
cdef:
1168
1170
int i, k
1169
1171
int n_samples = y_true.shape[0]
1170
1172
int n_classes = raw_prediction.shape[1]
1171
- Y_DTYPE_C max_value, sum_exps
1172
- Y_DTYPE_C * p # temporary buffer
1173
+ floating_in max_value, sum_exps
1174
+ floating_in * p # temporary buffer
1173
1175
1174
1176
# We assume n_samples > n_classes. In this case having the inner loop
1175
1177
# over n_classes is a good default.
@@ -1181,7 +1183,7 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1181
1183
with nogil, parallel(num_threads=n_threads):
1182
1184
# Define private buffer variables as each thread might use its
1183
1185
# own.
1184
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1186
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1185
1187
1186
1188
for i in prange(n_samples, schedule='static'):
1187
1189
sum_exp_minus_max(i, raw_prediction, p)
@@ -1197,7 +1199,7 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1197
1199
free(p)
1198
1200
else:
1199
1201
with nogil, parallel(num_threads=n_threads):
1200
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1202
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1201
1203
1202
1204
for i in prange(n_samples, schedule='static'):
1203
1205
sum_exp_minus_max(i, raw_prediction, p)
@@ -1218,26 +1220,26 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1218
1220
1219
1221
def loss_gradient(
1220
1222
self,
1221
- const Y_DTYPE_C [::1] y_true, # IN
1222
- const Y_DTYPE_C [:, :] raw_prediction, # IN
1223
- const Y_DTYPE_C [::1] sample_weight, # IN
1224
- G_DTYPE_C [::1] loss_out, # OUT
1225
- G_DTYPE_C [:, :] gradient_out, # OUT
1223
+ const floating_in [::1] y_true, # IN
1224
+ const floating_in [:, :] raw_prediction, # IN
1225
+ const floating_in [::1] sample_weight, # IN
1226
+ floating_out [::1] loss_out, # OUT
1227
+ floating_out [:, :] gradient_out, # OUT
1226
1228
int n_threads=1
1227
1229
):
1228
1230
cdef:
1229
1231
int i, k
1230
1232
int n_samples = y_true.shape[0]
1231
1233
int n_classes = raw_prediction.shape[1]
1232
- Y_DTYPE_C max_value, sum_exps
1233
- Y_DTYPE_C * p # temporary buffer
1234
+ floating_in max_value, sum_exps
1235
+ floating_in * p # temporary buffer
1234
1236
1235
1237
if sample_weight is None:
1236
1238
# inner loop over n_classes
1237
1239
with nogil, parallel(num_threads=n_threads):
1238
1240
# Define private buffer variables as each thread might use its
1239
1241
# own.
1240
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1242
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1241
1243
1242
1244
for i in prange(n_samples, schedule='static'):
1243
1245
sum_exp_minus_max(i, raw_prediction, p)
@@ -1256,7 +1258,7 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1256
1258
free(p)
1257
1259
else:
1258
1260
with nogil, parallel(num_threads=n_threads):
1259
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1261
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1260
1262
1261
1263
for i in prange(n_samples, schedule='static'):
1262
1264
sum_exp_minus_max(i, raw_prediction, p)
@@ -1280,25 +1282,25 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1280
1282
1281
1283
def gradient(
1282
1284
self,
1283
- const Y_DTYPE_C [::1] y_true, # IN
1284
- const Y_DTYPE_C [:, :] raw_prediction, # IN
1285
- const Y_DTYPE_C [::1] sample_weight, # IN
1286
- G_DTYPE_C [:, :] gradient_out, # OUT
1285
+ const floating_in [::1] y_true, # IN
1286
+ const floating_in [:, :] raw_prediction, # IN
1287
+ const floating_in [::1] sample_weight, # IN
1288
+ floating_out [:, :] gradient_out, # OUT
1287
1289
int n_threads=1
1288
1290
):
1289
1291
cdef:
1290
1292
int i, k
1291
1293
int n_samples = y_true.shape[0]
1292
1294
int n_classes = raw_prediction.shape[1]
1293
- Y_DTYPE_C sum_exps
1294
- Y_DTYPE_C * p # temporary buffer
1295
+ floating_in sum_exps
1296
+ floating_in * p # temporary buffer
1295
1297
1296
1298
if sample_weight is None:
1297
1299
# inner loop over n_classes
1298
1300
with nogil, parallel(num_threads=n_threads):
1299
1301
# Define private buffer variables as each thread might use its
1300
1302
# own.
1301
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1303
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1302
1304
1303
1305
for i in prange(n_samples, schedule='static'):
1304
1306
sum_exp_minus_max(i, raw_prediction, p)
@@ -1312,7 +1314,7 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1312
1314
free(p)
1313
1315
else:
1314
1316
with nogil, parallel(num_threads=n_threads):
1315
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1317
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1316
1318
1317
1319
for i in prange(n_samples, schedule='static'):
1318
1320
sum_exp_minus_max(i, raw_prediction, p)
@@ -1329,26 +1331,26 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1329
1331
1330
1332
def gradient_hessian(
1331
1333
self,
1332
- const Y_DTYPE_C [::1] y_true, # IN
1333
- const Y_DTYPE_C [:, :] raw_prediction, # IN
1334
- const Y_DTYPE_C [::1] sample_weight, # IN
1335
- G_DTYPE_C [:, :] gradient_out, # OUT
1336
- G_DTYPE_C [:, :] hessian_out, # OUT
1334
+ const floating_in [::1] y_true, # IN
1335
+ const floating_in [:, :] raw_prediction, # IN
1336
+ const floating_in [::1] sample_weight, # IN
1337
+ floating_out [:, :] gradient_out, # OUT
1338
+ floating_out [:, :] hessian_out, # OUT
1337
1339
int n_threads=1
1338
1340
):
1339
1341
cdef:
1340
1342
int i, k
1341
1343
int n_samples = y_true.shape[0]
1342
1344
int n_classes = raw_prediction.shape[1]
1343
- Y_DTYPE_C sum_exps
1344
- Y_DTYPE_C * p # temporary buffer
1345
+ floating_in sum_exps
1346
+ floating_in * p # temporary buffer
1345
1347
1346
1348
if sample_weight is None:
1347
1349
# inner loop over n_classes
1348
1350
with nogil, parallel(num_threads=n_threads):
1349
1351
# Define private buffer variables as each thread might use its
1350
1352
# own.
1351
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1353
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1352
1354
1353
1355
for i in prange(n_samples, schedule='static'):
1354
1356
sum_exp_minus_max(i, raw_prediction, p)
@@ -1364,7 +1366,7 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1364
1366
free(p)
1365
1367
else:
1366
1368
with nogil, parallel(num_threads=n_threads):
1367
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1369
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1368
1370
1369
1371
for i in prange(n_samples, schedule='static'):
1370
1372
sum_exp_minus_max(i, raw_prediction, p)
@@ -1387,26 +1389,26 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1387
1389
# diagonal (in the classes) approximation as implemented above.
1388
1390
def gradient_proba(
1389
1391
self,
1390
- const Y_DTYPE_C [::1] y_true, # IN
1391
- const Y_DTYPE_C [:, :] raw_prediction, # IN
1392
- const Y_DTYPE_C [::1] sample_weight, # IN
1393
- G_DTYPE_C [:, :] gradient_out, # OUT
1394
- G_DTYPE_C [:, :] proba_out, # OUT
1392
+ const floating_in [::1] y_true, # IN
1393
+ const floating_in [:, :] raw_prediction, # IN
1394
+ const floating_in [::1] sample_weight, # IN
1395
+ floating_out [:, :] gradient_out, # OUT
1396
+ floating_out [:, :] proba_out, # OUT
1395
1397
int n_threads=1
1396
1398
):
1397
1399
cdef:
1398
1400
int i, k
1399
1401
int n_samples = y_true.shape[0]
1400
1402
int n_classes = raw_prediction.shape[1]
1401
- Y_DTYPE_C sum_exps
1402
- Y_DTYPE_C * p # temporary buffer
1403
+ floating_in sum_exps
1404
+ floating_in * p # temporary buffer
1403
1405
1404
1406
if sample_weight is None:
1405
1407
# inner loop over n_classes
1406
1408
with nogil, parallel(num_threads=n_threads):
1407
1409
# Define private buffer variables as each thread might use its
1408
1410
# own.
1409
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1411
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1410
1412
1411
1413
for i in prange(n_samples, schedule='static'):
1412
1414
sum_exp_minus_max(i, raw_prediction, p)
@@ -1420,7 +1422,7 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1420
1422
free(p)
1421
1423
else:
1422
1424
with nogil, parallel(num_threads=n_threads):
1423
- p = <Y_DTYPE_C *> malloc(sizeof(Y_DTYPE_C ) * (n_classes + 2))
1425
+ p = <floating_in *> malloc(sizeof(floating_in ) * (n_classes + 2))
1424
1426
1425
1427
for i in prange(n_samples, schedule='static'):
1426
1428
sum_exp_minus_max(i, raw_prediction, p)
0 commit comments