scikit-learn-intelex 2024.5.0__py311-none-manylinux1_x86_64.whl → 2024.7.0__py311-none-manylinux1_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of scikit-learn-intelex might be problematic. Click here for more details.

Files changed (73) hide show
  1. {scikit_learn_intelex-2024.5.0.dist-info → scikit_learn_intelex-2024.7.0.dist-info}/METADATA +2 -2
  2. scikit_learn_intelex-2024.7.0.dist-info/RECORD +122 -0
  3. sklearnex/_config.py +3 -15
  4. sklearnex/_device_offload.py +9 -168
  5. sklearnex/basic_statistics/basic_statistics.py +127 -1
  6. sklearnex/basic_statistics/tests/test_basic_statistics.py +251 -0
  7. sklearnex/basic_statistics/tests/test_incremental_basic_statistics.py +1 -1
  8. sklearnex/cluster/dbscan.py +3 -1
  9. sklearnex/cluster/k_means.py +8 -0
  10. sklearnex/cluster/tests/test_dbscan.py +8 -6
  11. sklearnex/cluster/tests/test_kmeans.py +15 -3
  12. sklearnex/conftest.py +11 -1
  13. sklearnex/covariance/incremental_covariance.py +64 -13
  14. sklearnex/covariance/tests/test_incremental_covariance.py +35 -0
  15. sklearnex/decomposition/pca.py +25 -1
  16. sklearnex/decomposition/tests/test_pca.py +4 -2
  17. sklearnex/dispatcher.py +109 -1
  18. sklearnex/ensemble/_forest.py +121 -57
  19. sklearnex/ensemble/tests/test_forest.py +7 -0
  20. sklearnex/glob/dispatcher.py +16 -2
  21. sklearnex/linear_model/coordinate_descent.py +13 -0
  22. sklearnex/linear_model/incremental_linear.py +102 -25
  23. sklearnex/linear_model/linear.py +25 -39
  24. sklearnex/linear_model/logistic_regression.py +92 -74
  25. sklearnex/linear_model/ridge.py +7 -0
  26. sklearnex/linear_model/tests/test_incremental_linear.py +10 -10
  27. sklearnex/linear_model/tests/test_linear.py +30 -5
  28. sklearnex/linear_model/tests/test_logreg.py +45 -3
  29. sklearnex/manifold/t_sne.py +4 -0
  30. sklearnex/metrics/pairwise.py +5 -0
  31. sklearnex/metrics/ranking.py +3 -0
  32. sklearnex/model_selection/split.py +3 -0
  33. sklearnex/neighbors/_lof.py +9 -0
  34. sklearnex/neighbors/common.py +45 -1
  35. sklearnex/neighbors/knn_classification.py +1 -20
  36. sklearnex/neighbors/knn_regression.py +25 -20
  37. sklearnex/neighbors/knn_unsupervised.py +31 -7
  38. sklearnex/preview/__init__.py +1 -1
  39. sklearnex/preview/decomposition/__init__.py +19 -0
  40. sklearnex/preview/decomposition/incremental_pca.py +228 -0
  41. sklearnex/preview/decomposition/tests/test_incremental_pca.py +266 -0
  42. sklearnex/preview/linear_model/__init__.py +19 -0
  43. sklearnex/preview/linear_model/ridge.py +419 -0
  44. sklearnex/preview/linear_model/tests/test_ridge.py +102 -0
  45. sklearnex/spmd/basic_statistics/tests/test_basic_statistics_spmd.py +107 -0
  46. sklearnex/spmd/cluster/tests/test_dbscan_spmd.py +97 -0
  47. sklearnex/spmd/cluster/tests/test_kmeans_spmd.py +172 -0
  48. sklearnex/spmd/covariance/tests/test_covariance_spmd.py +107 -0
  49. sklearnex/spmd/decomposition/tests/test_pca_spmd.py +128 -0
  50. sklearnex/spmd/ensemble/tests/test_forest_spmd.py +265 -0
  51. sklearnex/spmd/linear_model/tests/test_linear_regression_spmd.py +145 -0
  52. sklearnex/spmd/linear_model/tests/test_logistic_regression_spmd.py +163 -0
  53. sklearnex/spmd/neighbors/tests/test_neighbors_spmd.py +288 -0
  54. sklearnex/svm/_common.py +163 -20
  55. sklearnex/svm/nusvc.py +40 -4
  56. sklearnex/svm/nusvr.py +31 -2
  57. sklearnex/svm/svc.py +40 -4
  58. sklearnex/svm/svr.py +31 -2
  59. sklearnex/svm/tests/test_svm.py +12 -20
  60. sklearnex/tests/_utils.py +185 -30
  61. sklearnex/tests/_utils_spmd.py +185 -0
  62. sklearnex/tests/test_common.py +54 -0
  63. sklearnex/tests/test_config.py +4 -0
  64. sklearnex/tests/test_memory_usage.py +185 -126
  65. sklearnex/tests/test_monkeypatch.py +12 -4
  66. sklearnex/tests/test_patching.py +21 -25
  67. sklearnex/tests/test_run_to_run_stability.py +295 -0
  68. sklearnex/utils/_namespace.py +1 -1
  69. scikit_learn_intelex-2024.5.0.dist-info/RECORD +0 -104
  70. sklearnex/tests/test_run_to_run_stability_tests.py +0 -428
  71. {scikit_learn_intelex-2024.5.0.dist-info → scikit_learn_intelex-2024.7.0.dist-info}/LICENSE.txt +0 -0
  72. {scikit_learn_intelex-2024.5.0.dist-info → scikit_learn_intelex-2024.7.0.dist-info}/WHEEL +0 -0
  73. {scikit_learn_intelex-2024.5.0.dist-info → scikit_learn_intelex-2024.7.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,265 @@
1
+ # ==============================================================================
2
+ # Copyright 2024 Intel Corporation
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ # ==============================================================================
16
+
17
+ import numpy as np
18
+ import pytest
19
+ from sklearn.datasets import make_regression
20
+
21
+ from onedal.tests.utils._dataframes_support import (
22
+ _convert_to_dataframe,
23
+ get_dataframes_and_queues,
24
+ )
25
+ from sklearnex.tests._utils_spmd import (
26
+ _generate_classification_data,
27
+ _generate_regression_data,
28
+ _get_local_tensor,
29
+ _mpi_libs_and_gpu_available,
30
+ _spmd_assert_allclose,
31
+ )
32
+
33
+
34
+ @pytest.mark.skipif(
35
+ not _mpi_libs_and_gpu_available,
36
+ reason="GPU device and MPI libs required for test",
37
+ )
38
+ @pytest.mark.parametrize(
39
+ "dataframe,queue",
40
+ get_dataframes_and_queues(dataframe_filter_="dpnp,dpctl", device_filter_="gpu"),
41
+ )
42
+ @pytest.mark.mpi
43
+ def test_rfcls_spmd_gold(dataframe, queue):
44
+ # Import spmd and batch algo
45
+ from sklearnex.ensemble import RandomForestClassifier as RandomForestClassifier_Batch
46
+ from sklearnex.spmd.ensemble import (
47
+ RandomForestClassifier as RandomForestClassifier_SPMD,
48
+ )
49
+
50
+ # Create gold data and convert to dataframe
51
+ X_train = np.array(
52
+ [
53
+ [0.0, 0.0],
54
+ [0.0, 1.0],
55
+ [1.0, 0.0],
56
+ [0.0, 2.0],
57
+ [2.0, 0.0],
58
+ [1.0, 1.0],
59
+ [0.0, -1.0],
60
+ [-1.0, 0.0],
61
+ [-1.0, -1.0],
62
+ ]
63
+ )
64
+ y_train = np.array([0, 2, 1, 2, 1, 0, 1, 2, 0])
65
+ X_test = np.array(
66
+ [
67
+ [1.0, -1.0],
68
+ [-1.0, 1.0],
69
+ [0.0, 1.0],
70
+ [10.0, -10.0],
71
+ ]
72
+ )
73
+
74
+ local_dpt_X_train = _convert_to_dataframe(
75
+ _get_local_tensor(X_train), sycl_queue=queue, target_df=dataframe
76
+ )
77
+ local_dpt_y_train = _convert_to_dataframe(
78
+ _get_local_tensor(y_train), sycl_queue=queue, target_df=dataframe
79
+ )
80
+ local_dpt_X_test = _convert_to_dataframe(
81
+ _get_local_tensor(X_test), sycl_queue=queue, target_df=dataframe
82
+ )
83
+
84
+ # Ensure predictions of batch algo match spmd
85
+ spmd_model = RandomForestClassifier_SPMD(n_estimators=3, random_state=0).fit(
86
+ local_dpt_X_train, local_dpt_y_train
87
+ )
88
+ batch_model = RandomForestClassifier_Batch(n_estimators=3, random_state=0).fit(
89
+ X_train, y_train
90
+ )
91
+ spmd_result = spmd_model.predict(local_dpt_X_test)
92
+ batch_result = batch_model.predict(X_test)
93
+
94
+ pytest.skip("SPMD and batch random forest results not aligned")
95
+ _spmd_assert_allclose(spmd_result, batch_result)
96
+
97
+
98
+ @pytest.mark.skipif(
99
+ not _mpi_libs_and_gpu_available,
100
+ reason="GPU device and MPI libs required for test",
101
+ )
102
+ @pytest.mark.parametrize("n_samples", [200, 1000])
103
+ @pytest.mark.parametrize("n_features_and_classes", [(5, 2), (25, 2), (25, 10)])
104
+ @pytest.mark.parametrize("n_estimators", [10, 100])
105
+ @pytest.mark.parametrize("max_depth", [3, None])
106
+ @pytest.mark.parametrize(
107
+ "dataframe,queue",
108
+ get_dataframes_and_queues(dataframe_filter_="dpnp,dpctl", device_filter_="gpu"),
109
+ )
110
+ @pytest.mark.parametrize("dtype", [np.float32, np.float64])
111
+ @pytest.mark.mpi
112
+ def test_rfcls_spmd_synthetic(
113
+ n_samples, n_features_and_classes, n_estimators, max_depth, dataframe, queue, dtype
114
+ ):
115
+ n_features, n_classes = n_features_and_classes
116
+ # Import spmd and batch algo
117
+ from sklearnex.ensemble import RandomForestClassifier as RandomForestClassifier_Batch
118
+ from sklearnex.spmd.ensemble import (
119
+ RandomForestClassifier as RandomForestClassifier_SPMD,
120
+ )
121
+
122
+ # Generate data and convert to dataframe
123
+ X_train, X_test, y_train, _ = _generate_classification_data(
124
+ n_samples, n_features, n_classes, dtype=dtype
125
+ )
126
+
127
+ local_dpt_X_train = _convert_to_dataframe(
128
+ _get_local_tensor(X_train), sycl_queue=queue, target_df=dataframe
129
+ )
130
+ local_dpt_y_train = _convert_to_dataframe(
131
+ _get_local_tensor(y_train), sycl_queue=queue, target_df=dataframe
132
+ )
133
+ local_dpt_X_test = _convert_to_dataframe(
134
+ _get_local_tensor(X_test), sycl_queue=queue, target_df=dataframe
135
+ )
136
+
137
+ # Ensure predictions of batch algo match spmd
138
+ spmd_model = RandomForestClassifier_SPMD(
139
+ n_estimators=n_estimators, max_depth=max_depth, random_state=0
140
+ ).fit(local_dpt_X_train, local_dpt_y_train)
141
+ batch_model = RandomForestClassifier_Batch(
142
+ n_estimators=n_estimators, max_depth=max_depth, random_state=0
143
+ ).fit(X_train, y_train)
144
+ spmd_result = spmd_model.predict(local_dpt_X_test)
145
+ batch_result = batch_model.predict(X_test)
146
+
147
+ pytest.skip("SPMD and batch random forest results not aligned")
148
+ _spmd_assert_allclose(spmd_result, batch_result)
149
+
150
+
151
+ @pytest.mark.skipif(
152
+ not _mpi_libs_and_gpu_available,
153
+ reason="GPU device and MPI libs required for test",
154
+ )
155
+ @pytest.mark.parametrize(
156
+ "dataframe,queue",
157
+ get_dataframes_and_queues(dataframe_filter_="dpnp,dpctl", device_filter_="gpu"),
158
+ )
159
+ @pytest.mark.mpi
160
+ def test_rfreg_spmd_gold(dataframe, queue):
161
+ # Import spmd and batch algo
162
+ from sklearnex.ensemble import RandomForestRegressor as RandomForestRegressor_Batch
163
+ from sklearnex.spmd.ensemble import (
164
+ RandomForestRegressor as RandomForestRegressor_SPMD,
165
+ )
166
+
167
+ # Create gold data and convert to dataframe
168
+ X_train = np.array(
169
+ [
170
+ [0.0, 0.0],
171
+ [0.0, 1.0],
172
+ [1.0, 0.0],
173
+ [0.0, 2.0],
174
+ [2.0, 0.0],
175
+ [1.0, 1.0],
176
+ [0.0, -1.0],
177
+ [-1.0, 0.0],
178
+ [-1.0, -1.0],
179
+ ]
180
+ )
181
+ y_train = np.array([3.0, 5.0, 4.0, 7.0, 5.0, 6.0, 1.0, 2.0, 0.0])
182
+ X_test = np.array(
183
+ [
184
+ [1.0, -1.0],
185
+ [-1.0, 1.0],
186
+ [0.0, 1.0],
187
+ [10.0, -10.0],
188
+ ]
189
+ )
190
+
191
+ local_dpt_X_train = _convert_to_dataframe(
192
+ _get_local_tensor(X_train), sycl_queue=queue, target_df=dataframe
193
+ )
194
+ local_dpt_y_train = _convert_to_dataframe(
195
+ _get_local_tensor(y_train), sycl_queue=queue, target_df=dataframe
196
+ )
197
+ local_dpt_X_test = _convert_to_dataframe(
198
+ _get_local_tensor(X_test), sycl_queue=queue, target_df=dataframe
199
+ )
200
+
201
+ # Ensure predictions of batch algo match spmd
202
+ spmd_model = RandomForestRegressor_SPMD(n_estimators=3, random_state=0).fit(
203
+ local_dpt_X_train, local_dpt_y_train
204
+ )
205
+ batch_model = RandomForestRegressor_Batch(n_estimators=3, random_state=0).fit(
206
+ X_train, y_train
207
+ )
208
+ spmd_result = spmd_model.predict(local_dpt_X_test)
209
+ batch_result = batch_model.predict(X_test)
210
+
211
+ pytest.skip("SPMD and batch random forest results not aligned")
212
+ _spmd_assert_allclose(spmd_result, batch_result)
213
+
214
+
215
+ @pytest.mark.skipif(
216
+ not _mpi_libs_and_gpu_available,
217
+ reason="GPU device and MPI libs required for test",
218
+ )
219
+ @pytest.mark.parametrize("n_samples", [200, 1000])
220
+ @pytest.mark.parametrize("n_features", [5, 25])
221
+ @pytest.mark.parametrize("n_estimators", [10, 100])
222
+ @pytest.mark.parametrize("max_depth", [3, None])
223
+ @pytest.mark.parametrize(
224
+ "dataframe,queue",
225
+ get_dataframes_and_queues(dataframe_filter_="dpnp,dpctl", device_filter_="gpu"),
226
+ )
227
+ @pytest.mark.parametrize("dtype", [np.float32, np.float64])
228
+ @pytest.mark.mpi
229
+ def test_rfreg_spmd_synthetic(
230
+ n_samples, n_features, n_estimators, max_depth, dataframe, queue, dtype
231
+ ):
232
+ # Import spmd and batch algo
233
+ from sklearnex.ensemble import RandomForestRegressor as RandomForestRegressor_Batch
234
+ from sklearnex.spmd.ensemble import (
235
+ RandomForestRegressor as RandomForestRegressor_SPMD,
236
+ )
237
+
238
+ # Generate data and convert to dataframe
239
+ X_train, X_test, y_train, _ = _generate_regression_data(
240
+ n_samples, n_features, dtype=dtype
241
+ )
242
+
243
+ local_dpt_X_train = _convert_to_dataframe(
244
+ _get_local_tensor(X_train), sycl_queue=queue, target_df=dataframe
245
+ )
246
+ local_dpt_y_train = _convert_to_dataframe(
247
+ _get_local_tensor(y_train), sycl_queue=queue, target_df=dataframe
248
+ )
249
+ local_dpt_X_test = _convert_to_dataframe(
250
+ _get_local_tensor(X_test), sycl_queue=queue, target_df=dataframe
251
+ )
252
+
253
+ # Ensure predictions of batch algo match spmd
254
+ spmd_model = RandomForestRegressor_Batch(
255
+ n_estimators=n_estimators, max_depth=max_depth, random_state=0
256
+ ).fit(local_dpt_X_train, local_dpt_y_train)
257
+ batch_model = RandomForestRegressor_Batch(
258
+ n_estimators=n_estimators, max_depth=max_depth, random_state=0
259
+ ).fit(X_train, y_train)
260
+ spmd_result = spmd_model.predict(local_dpt_X_test)
261
+ batch_result = batch_model.predict(X_test)
262
+
263
+ # TODO: remove skips when SPMD and batch are aligned
264
+ pytest.skip("SPMD and batch random forest results not aligned")
265
+ _spmd_assert_allclose(spmd_result, batch_result)
@@ -0,0 +1,145 @@
1
+ # ==============================================================================
2
+ # Copyright 2024 Intel Corporation
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ # ==============================================================================
16
+
17
+ import numpy as np
18
+ import pytest
19
+ from numpy.testing import assert_allclose
20
+
21
+ from onedal.tests.utils._dataframes_support import (
22
+ _convert_to_dataframe,
23
+ get_dataframes_and_queues,
24
+ )
25
+ from sklearnex.tests._utils_spmd import (
26
+ _generate_regression_data,
27
+ _get_local_tensor,
28
+ _mpi_libs_and_gpu_available,
29
+ _spmd_assert_allclose,
30
+ )
31
+
32
+
33
+ @pytest.mark.skipif(
34
+ not _mpi_libs_and_gpu_available,
35
+ reason="GPU device and MPI libs required for test",
36
+ )
37
+ @pytest.mark.parametrize(
38
+ "dataframe,queue",
39
+ get_dataframes_and_queues(dataframe_filter_="dpnp,dpctl", device_filter_="gpu"),
40
+ )
41
+ @pytest.mark.mpi
42
+ def test_linear_spmd_gold(dataframe, queue):
43
+ # Import spmd and batch algo
44
+ from sklearnex.linear_model import LinearRegression as LinearRegression_Batch
45
+ from sklearnex.spmd.linear_model import LinearRegression as LinearRegression_SPMD
46
+
47
+ # Create gold data and convert to dataframe
48
+ X_train = np.array(
49
+ [
50
+ [0.0, 0.0],
51
+ [0.0, 1.0],
52
+ [1.0, 0.0],
53
+ [0.0, 2.0],
54
+ [2.0, 0.0],
55
+ [1.0, 1.0],
56
+ [0.0, -1.0],
57
+ [-1.0, 0.0],
58
+ [-1.0, -1.0],
59
+ ]
60
+ )
61
+ y_train = np.array([3.0, 5.0, 4.0, 7.0, 5.0, 6.0, 1.0, 2.0, 0.0])
62
+ X_test = np.array(
63
+ [
64
+ [1.0, -1.0],
65
+ [-1.0, 1.0],
66
+ [0.0, 1.0],
67
+ [10.0, -10.0],
68
+ ]
69
+ )
70
+
71
+ local_dpt_X_train = _convert_to_dataframe(
72
+ _get_local_tensor(X_train), sycl_queue=queue, target_df=dataframe
73
+ )
74
+ local_dpt_y_train = _convert_to_dataframe(
75
+ _get_local_tensor(y_train), sycl_queue=queue, target_df=dataframe
76
+ )
77
+ local_dpt_X_test = _convert_to_dataframe(
78
+ _get_local_tensor(X_test), sycl_queue=queue, target_df=dataframe
79
+ )
80
+
81
+ # ensure trained model of batch algo matches spmd
82
+ spmd_model = LinearRegression_SPMD().fit(local_dpt_X_train, local_dpt_y_train)
83
+ batch_model = LinearRegression_Batch().fit(X_train, y_train)
84
+
85
+ assert_allclose(spmd_model.coef_, batch_model.coef_)
86
+ assert_allclose(spmd_model.intercept_, batch_model.intercept_)
87
+
88
+ # ensure predictions of batch algo match spmd
89
+ spmd_result = spmd_model.predict(local_dpt_X_test)
90
+ batch_result = batch_model.predict(X_test)
91
+
92
+ _spmd_assert_allclose(spmd_result, batch_result)
93
+
94
+
95
+ @pytest.mark.skipif(
96
+ not _mpi_libs_and_gpu_available,
97
+ reason="GPU device and MPI libs required for test",
98
+ )
99
+ @pytest.mark.parametrize("n_samples", [100, 10000])
100
+ @pytest.mark.parametrize("n_features", [10, 100])
101
+ @pytest.mark.parametrize(
102
+ "dataframe,queue",
103
+ get_dataframes_and_queues(dataframe_filter_="dpnp,dpctl", device_filter_="gpu"),
104
+ )
105
+ @pytest.mark.parametrize("dtype", [np.float32, np.float64])
106
+ @pytest.mark.mpi
107
+ def test_linear_spmd_synthetic(n_samples, n_features, dataframe, queue, dtype):
108
+ # Import spmd and batch algo
109
+ from sklearnex.linear_model import LinearRegression as LinearRegression_Batch
110
+ from sklearnex.spmd.linear_model import LinearRegression as LinearRegression_SPMD
111
+
112
+ # Generate data and convert to dataframe
113
+ X_train, X_test, y_train, _ = _generate_regression_data(
114
+ n_samples, n_features, dtype=dtype
115
+ )
116
+
117
+ local_dpt_X_train = _convert_to_dataframe(
118
+ _get_local_tensor(X_train), sycl_queue=queue, target_df=dataframe
119
+ )
120
+ local_dpt_y_train = _convert_to_dataframe(
121
+ _get_local_tensor(y_train), sycl_queue=queue, target_df=dataframe
122
+ )
123
+ local_dpt_X_test = _convert_to_dataframe(
124
+ _get_local_tensor(X_test), sycl_queue=queue, target_df=dataframe
125
+ )
126
+
127
+ # TODO: support linear regression on wide datasets and remove this skip
128
+ if local_dpt_X_train.shape[0] < n_features:
129
+ pytest.skip(
130
+ "SPMD Linear Regression does not support cases where n_rows_rank < n_features"
131
+ )
132
+
133
+ # ensure trained model of batch algo matches spmd
134
+ spmd_model = LinearRegression_SPMD().fit(local_dpt_X_train, local_dpt_y_train)
135
+ batch_model = LinearRegression_Batch().fit(X_train, y_train)
136
+
137
+ tol = 1e-3 if dtype == np.float32 else 1e-7
138
+ assert_allclose(spmd_model.coef_, batch_model.coef_, rtol=tol, atol=tol)
139
+ assert_allclose(spmd_model.intercept_, batch_model.intercept_, rtol=tol, atol=tol)
140
+
141
+ # ensure predictions of batch algo match spmd
142
+ spmd_result = spmd_model.predict(local_dpt_X_test)
143
+ batch_result = batch_model.predict(X_test)
144
+
145
+ _spmd_assert_allclose(spmd_result, batch_result, rtol=tol, atol=tol)
@@ -0,0 +1,163 @@
1
+ # ==============================================================================
2
+ # Copyright 2024 Intel Corporation
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ # ==============================================================================
16
+
17
+ import numpy as np
18
+ import pytest
19
+ from numpy.testing import assert_allclose
20
+
21
+ from onedal.tests.utils._dataframes_support import (
22
+ _as_numpy,
23
+ _convert_to_dataframe,
24
+ get_dataframes_and_queues,
25
+ )
26
+ from sklearnex.tests._utils_spmd import (
27
+ _generate_classification_data,
28
+ _get_local_tensor,
29
+ _mpi_libs_and_gpu_available,
30
+ _spmd_assert_allclose,
31
+ )
32
+
33
+
34
+ @pytest.mark.skipif(
35
+ not _mpi_libs_and_gpu_available,
36
+ reason="GPU device and MPI libs required for test",
37
+ )
38
+ @pytest.mark.parametrize(
39
+ "dataframe,queue",
40
+ get_dataframes_and_queues(dataframe_filter_="dpnp,dpctl", device_filter_="gpu"),
41
+ )
42
+ @pytest.mark.mpi
43
+ def test_logistic_spmd_gold(dataframe, queue):
44
+ # Import spmd and batch algo
45
+ from sklearnex.linear_model import LogisticRegression as LogisticRegression_Batch
46
+ from sklearnex.spmd.linear_model import LogisticRegression as LogisticRegression_SPMD
47
+
48
+ # Create gold data and convert to dataframe
49
+ X_train = np.array(
50
+ [
51
+ [0.0, 0.0],
52
+ [0.0, 1.0],
53
+ [1.0, 0.0],
54
+ [0.0, 2.0],
55
+ [2.0, 0.0],
56
+ [1.0, 1.0],
57
+ [0.0, -1.0],
58
+ [-1.0, 0.0],
59
+ [-1.0, -1.0],
60
+ ]
61
+ )
62
+ y_train = np.array([0, 1, 0, 1, 0, 1, 0, 1, 0])
63
+ X_test = np.array(
64
+ [
65
+ [1.0, -1.0],
66
+ [-1.0, 1.0],
67
+ [0.0, 1.0],
68
+ [10.0, -10.0],
69
+ ]
70
+ )
71
+
72
+ local_dpt_X_train = _convert_to_dataframe(
73
+ _get_local_tensor(X_train), sycl_queue=queue, target_df=dataframe
74
+ )
75
+ local_dpt_y_train = _convert_to_dataframe(
76
+ _get_local_tensor(y_train), sycl_queue=queue, target_df=dataframe
77
+ )
78
+ local_dpt_X_test = _convert_to_dataframe(
79
+ _get_local_tensor(X_test), sycl_queue=queue, target_df=dataframe
80
+ )
81
+ dpt_X_train = _convert_to_dataframe(X_train, sycl_queue=queue, target_df=dataframe)
82
+ dpt_y_train = _convert_to_dataframe(y_train, sycl_queue=queue, target_df=dataframe)
83
+ dpt_X_test = _convert_to_dataframe(X_test, sycl_queue=queue, target_df=dataframe)
84
+
85
+ # Ensure trained model of batch algo matches spmd
86
+ spmd_model = LogisticRegression_SPMD(random_state=0, solver="newton-cg").fit(
87
+ local_dpt_X_train, local_dpt_y_train
88
+ )
89
+ batch_model = LogisticRegression_Batch(random_state=0, solver="newton-cg").fit(
90
+ dpt_X_train, dpt_y_train
91
+ )
92
+
93
+ assert_allclose(spmd_model.coef_, batch_model.coef_, rtol=1e-2)
94
+ assert_allclose(spmd_model.intercept_, batch_model.intercept_, rtol=1e-2)
95
+
96
+ # Ensure predictions of batch algo match spmd
97
+ spmd_result = spmd_model.predict(local_dpt_X_test)
98
+ batch_result = batch_model.predict(dpt_X_test)
99
+
100
+ _spmd_assert_allclose(spmd_result, _as_numpy(batch_result))
101
+
102
+
103
+ # parametrize max_iter, C, tol
104
+ @pytest.mark.skipif(
105
+ not _mpi_libs_and_gpu_available,
106
+ reason="GPU device and MPI libs required for test",
107
+ )
108
+ @pytest.mark.parametrize("n_samples", [100, 10000])
109
+ @pytest.mark.parametrize("n_features", [10, 100])
110
+ @pytest.mark.parametrize("C", [0.5, 1.0, 2.0])
111
+ @pytest.mark.parametrize("tol", [1e-2, 1e-4])
112
+ @pytest.mark.parametrize(
113
+ "dataframe,queue",
114
+ get_dataframes_and_queues(dataframe_filter_="dpnp,dpctl", device_filter_="gpu"),
115
+ )
116
+ @pytest.mark.parametrize("dtype", [np.float32, np.float64])
117
+ @pytest.mark.mpi
118
+ def test_logistic_spmd_synthetic(n_samples, n_features, C, tol, dataframe, queue, dtype):
119
+ pytest.skip("Sporadic failures on coef_ check. Test disabled while fix in progress")
120
+ # TODO: Resolve numerical issues when n_rows_rank < n_cols
121
+ if n_samples <= n_features:
122
+ pytest.skip("Numerical issues when rank rows < columns")
123
+
124
+ # Import spmd and batch algo
125
+ from sklearnex.linear_model import LogisticRegression as LogisticRegression_Batch
126
+ from sklearnex.spmd.linear_model import LogisticRegression as LogisticRegression_SPMD
127
+
128
+ # Generate data and convert to dataframe
129
+ X_train, X_test, y_train, _ = _generate_classification_data(
130
+ n_samples, n_features, dtype=dtype
131
+ )
132
+
133
+ local_dpt_X_train = _convert_to_dataframe(
134
+ _get_local_tensor(X_train), sycl_queue=queue, target_df=dataframe
135
+ )
136
+ local_dpt_y_train = _convert_to_dataframe(
137
+ _get_local_tensor(y_train), sycl_queue=queue, target_df=dataframe
138
+ )
139
+ local_dpt_X_test = _convert_to_dataframe(
140
+ _get_local_tensor(X_test), sycl_queue=queue, target_df=dataframe
141
+ )
142
+ dpt_X_train = _convert_to_dataframe(X_train, sycl_queue=queue, target_df=dataframe)
143
+ dpt_y_train = _convert_to_dataframe(y_train, sycl_queue=queue, target_df=dataframe)
144
+ dpt_X_test = _convert_to_dataframe(X_test, sycl_queue=queue, target_df=dataframe)
145
+
146
+ # Ensure trained model of batch algo matches spmd
147
+ spmd_model = LogisticRegression_SPMD(
148
+ random_state=0, solver="newton-cg", C=C, tol=tol
149
+ ).fit(local_dpt_X_train, local_dpt_y_train)
150
+ batch_model = LogisticRegression_Batch(
151
+ random_state=0, solver="newton-cg", C=C, tol=tol
152
+ ).fit(dpt_X_train, dpt_y_train)
153
+
154
+ # TODO: Logistic Regression coefficients do not align
155
+ tol = 1e-2
156
+ assert_allclose(spmd_model.coef_, batch_model.coef_, rtol=tol, atol=tol)
157
+ assert_allclose(spmd_model.intercept_, batch_model.intercept_, rtol=tol, atol=tol)
158
+
159
+ # Ensure predictions of batch algo match spmd
160
+ spmd_result = spmd_model.predict(local_dpt_X_test)
161
+ batch_result = batch_model.predict(dpt_X_test)
162
+
163
+ _spmd_assert_allclose(spmd_result, _as_numpy(batch_result))