@@ -211,8 +211,6 @@ WARNING: Do not edit `sklearn/_loss/_loss.pyx` file directly, as it is generated
211 211
# checking like None -> np.empty().
212
212
#
213
213
# Note: We require 1-dim ndarrays to be contiguous.
214
- # TODO: Use const memoryviews with fused types with Cython 3.0 where
215
- # appropriate (arguments marked by "# IN").
216
214
217
215
from cython.parallel import parallel, prange
218
216
import numpy as np
@@ -245,8 +243,8 @@ cdef inline double log1pexp(double x) nogil:
245
243
246
244
cdef inline void sum_exp_minus_max(
247
245
const int i,
248
- Y_DTYPE_C[:, :] raw_prediction, # IN
249
- Y_DTYPE_C *p # OUT
246
+ const Y_DTYPE_C[:, :] raw_prediction, # IN
247
+ Y_DTYPE_C *p # OUT
250
248
) nogil:
251
249
# Thread local buffers are used to stores results of this function via p.
252
250
# The results are stored as follows:
@@ -754,15 +752,11 @@ cdef class CyLossFunction:
754
752
"""
755
753
pass
756
754
757
- # Note: With Cython 3.0, fused types can be used together with const:
758
- # const Y_DTYPE_C double[::1] y_true
759
- # See release notes 3.0.0 alpha1
760
- # https://cython.readthedocs.io/en/latest/src/changes.html#alpha-1-2020-04-12
761
755
def loss(
762
756
self,
763
- Y_DTYPE_C[::1] y_true, # IN
764
- Y_DTYPE_C[::1] raw_prediction, # IN
765
- Y_DTYPE_C[::1] sample_weight, # IN
757
+ const Y_DTYPE_C[::1] y_true, # IN
758
+ const Y_DTYPE_C[::1] raw_prediction, # IN
759
+ const Y_DTYPE_C[::1] sample_weight, # IN
766
760
G_DTYPE_C[::1] loss_out, # OUT
767
761
int n_threads=1
768
762
):
@@ -790,9 +784,9 @@ cdef class CyLossFunction:
790
784
791
785
def gradient(
792
786
self,
793
- Y_DTYPE_C[::1] y_true, # IN
794
- Y_DTYPE_C[::1] raw_prediction, # IN
795
- Y_DTYPE_C[::1] sample_weight, # IN
787
+ const Y_DTYPE_C[::1] y_true, # IN
788
+ const Y_DTYPE_C[::1] raw_prediction, # IN
789
+ const Y_DTYPE_C[::1] sample_weight, # IN
796
790
G_DTYPE_C[::1] gradient_out, # OUT
797
791
int n_threads=1
798
792
):
@@ -820,9 +814,9 @@ cdef class CyLossFunction:
820
814
821
815
def loss_gradient(
822
816
self,
823
- Y_DTYPE_C[::1] y_true, # IN
824
- Y_DTYPE_C[::1] raw_prediction, # IN
825
- Y_DTYPE_C[::1] sample_weight, # IN
817
+ const Y_DTYPE_C[::1] y_true, # IN
818
+ const Y_DTYPE_C[::1] raw_prediction, # IN
819
+ const Y_DTYPE_C[::1] sample_weight, # IN
826
820
G_DTYPE_C[::1] loss_out, # OUT
827
821
G_DTYPE_C[::1] gradient_out, # OUT
828
822
int n_threads=1
@@ -858,9 +852,9 @@ cdef class CyLossFunction:
858
852
859
853
def gradient_hessian(
860
854
self,
861
- Y_DTYPE_C[::1] y_true, # IN
862
- Y_DTYPE_C[::1] raw_prediction, # IN
863
- Y_DTYPE_C[::1] sample_weight, # IN
855
+ const Y_DTYPE_C[::1] y_true, # IN
856
+ const Y_DTYPE_C[::1] raw_prediction, # IN
857
+ const Y_DTYPE_C[::1] sample_weight, # IN
864
858
G_DTYPE_C[::1] gradient_out, # OUT
865
859
G_DTYPE_C[::1] hessian_out, # OUT
866
860
int n_threads=1
@@ -920,10 +914,10 @@ cdef class {{name}}(CyLossFunction):
920
914
921
915
def loss(
922
916
self,
923
- Y_DTYPE_C[::1] y_true, # IN
924
- Y_DTYPE_C[::1] raw_prediction, # IN
925
- Y_DTYPE_C[::1] sample_weight, # IN
926
- G_DTYPE_C[::1] loss_out, # OUT
917
+ const Y_DTYPE_C[::1] y_true, # IN
918
+ const Y_DTYPE_C[::1] raw_prediction, # IN
919
+ const Y_DTYPE_C[::1] sample_weight, # IN
920
+ G_DTYPE_C[::1] loss_out, # OUT
927
921
int n_threads=1
928
922
):
929
923
cdef:
@@ -946,11 +940,11 @@ cdef class {{name}}(CyLossFunction):
946
940
{{if closs_grad is not None}}
947
941
<
10000
code class="diff-text syntax-highlighted-line"> def loss_gradient(
948
942
self,
949
- Y_DTYPE_C[::1] y_true, # IN
950
- Y_DTYPE_C[::1] raw_prediction, # IN
951
- Y_DTYPE_C[::1] sample_weight, # IN
952
- G_DTYPE_C[::1] loss_out, # OUT
953
- G_DTYPE_C[::1] gradient_out, # OUT
943
+ const Y_DTYPE_C[::1] y_true, # IN
944
+ const Y_DTYPE_C[::1] raw_prediction, # IN
945
+ const Y_DTYPE_C[::1] sample_weight, # IN
946
+ G_DTYPE_C[::1] loss_out, # OUT
947
+ G_DTYPE_C[::1] gradient_out, # OUT
954
948
int n_threads=1
955
949
):
956
950
cdef:
@@ -978,10 +972,10 @@ cdef class {{name}}(CyLossFunction):
978
972
979
973
def gradient(
980
974
self,
981
- Y_DTYPE_C[::1] y_true, # IN
982
- Y_DTYPE_C[::1] raw_prediction, # IN
983
- Y_DTYPE_C[::1] sample_weight, # IN
984
- G_DTYPE_C[::1] gradient_out, # OUT
975
+ const Y_DTYPE_C[::1] y_true, # IN
976
+ const Y_DTYPE_C[::1] raw_prediction, # IN
977
+ const Y_DTYPE_C[::1] sample_weight, # IN
978
+ G_DTYPE_C[::1] gradient_out, # OUT
985
979
int n_threads=1
986
980
):
987
981
cdef:
@@ -1003,11 +997,11 @@ cdef class {{name}}(CyLossFunction):
1003
997
1004
998
def gradient_hessian(
1005
999
self,
1006
- Y_DTYPE_C[::1] y_true, # IN
1007
- Y_DTYPE_C[::1] raw_prediction, # IN
1008
- Y_DTYPE_C[::1] sample_weight, # IN
1009
- G_DTYPE_C[::1] gradient_out, # OUT
1010
- G_DTYPE_C[::1] h
F42D
essian_out, # OUT
1000
+ const Y_DTYPE_C[::1] y_true, # IN
1001
+ const Y_DTYPE_C[::1] raw_prediction, # IN
1002
+ const Y_DTYPE_C[::1] sample_weight, # IN
1003
+ G_DTYPE_C[::1] gradient_out, # OUT
1004
+ G_DTYPE_C[::1] hessian_out, # OUT
1011
1005
int n_threads=1
1012
1006
):
1013
1007
cdef:
@@ -1056,10 +1050,10 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1056
1050
# opposite are welcome.
1057
1051
def loss(
1058
1052
self,
1059
- Y_DTYPE_C[::1] y_true, # IN
1060
- Y_DTYPE_C[:, :] raw_prediction, # IN
1061
- Y_DTYPE_C[::1] sample_weight, # IN
1062
- G_DTYPE_C[::1] loss_out, # OUT
1053
+ const Y_DTYPE_C[::1] y_true, # IN
1054
+ const Y_DTYPE_C[:, :] raw_prediction, # IN
1055
+ const Y_DTYPE_C[::1] sample_weight, # IN
1056
+ G_DTYPE_C[::1] loss_out, # OUT
1063
1057
int n_threads=1
1064
1058
):
1065
1059
cdef:
@@ -1116,11 +1110,11 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1116
1110
1117
1111
def loss_gradient(
1118
1112
self,
1119
- Y_DTYPE_C[::1] y_true, # IN
1120
- Y_DTYPE_C[:, :] raw_prediction, # IN
1121
- Y_DTYPE_C[::1] sample_weight, # IN
1122
- G_DTYPE_C[::1] loss_out, # OUT
1123
- G_DTYPE_C[:, :] gradient_out, # OUT
1113
+ const Y_DTYPE_C[::1] y_true, # IN
1114
+ const Y_DTYPE_C[:, :] raw_prediction, # IN
1115
+ const Y_DTYPE_C[::1] sample_weight, # IN
1116
+ G_DTYPE_C[::1] loss_out, # OUT
1117
+ G_DTYPE_C[:, :] gradient_out, # OUT
1124
1118
int n_threads=1
1125
1119
):
1126
1120
cdef:
@@ -1178,10 +1172,10 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1178
1172
1179
1173
def gradient(
1180
1174
self,
1181
- Y_DTYPE_C[::1] y_true, # IN
1182
- Y_DTYPE_C[:, :] raw_prediction, # IN
1183
- Y_DTYPE_C[::1] sample_weight, # IN
1184
- G_DTYPE_C[:, :] gradient_out, # OUT
1175
+ const Y_DTYPE_C[::1] y_true, # IN
1176
+ const Y_DTYPE_C[:, :] raw_prediction, # IN
1177
+ const Y_DTYPE_C[::1] sample_weight, # IN
1178
+ G_DTYPE_C[:, :] gradient_out, # OUT
1185
1179
int n_threads=1
1186
1180
):
1187
1181
cdef:
@@ -1227,11 +1221,11 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1227
1221
1228
1222
def gradient_hessian(
1229
1223
self,
1230
- Y_DTYPE_C[::1] y_true, # IN
1231
- Y_DTYPE_C[:, :] raw_prediction, # IN
1232
- Y_DTYPE_C[::1] sample_weight, # IN
1233
- G_DTYPE_C[:, :] gradient_out, # OUT
1234
- G_DTYPE_C[:, :] hessian_out, # OUT
1224
+ const Y_DTYPE_C[::1] y_true, # IN
1225
+ const Y_DTYPE_C[:, :] raw_prediction, # IN
1226
+ const Y_DTYPE_C[::1] sample_weight, # IN
1227
+ G_DTYPE_C[:, :] gradient_out, # OUT
1228
+ G_DTYPE_C[:, :] hessian_out, # OUT
1235
1229
int n_threads=1
1236
1230
):
1237
1231
cdef:
@@ -1285,11 +1279,11 @@ cdef class CyHalfMultinomialLoss(CyLossFunction):
1285
1279
# diagonal (in the classes) approximation as implemented above.
1286
1280
def gradient_proba(
1287
1281
self,
1288
- Y_DTYPE_C[::1] y_true, # IN
1289
- Y_DTYPE_C[:, :] raw_prediction, # IN
1290
- Y_DTYPE_C[::1] sample_weight, # IN
1291
- G_DTYPE_C[:, :] gradient_out, # OUT
1292
- G_DTYPE_C[:, :] proba_out, # OUT
1282
+ const Y_DTYPE_C[::1] y_true, # IN
1283
+ const Y_DTYPE_C[:, :] raw_prediction, # IN
1284
+ const Y_DTYPE_C[::1] sample_weight, # IN
1285
+ G_DTYPE_C[:, :] gradient_out, # OUT
1286
+ G_DTYPE_C[:, :] proba_out, # OUT
1293
1287
int n_threads=1
1294
1288
):
1295
1289
cdef:
0 commit comments