-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'intel:main' into enh/functional_array_api
- Loading branch information
Showing
14 changed files
with
398 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# =============================================================================== | ||
# Copyright 2024 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# =============================================================================== | ||
|
||
import dpctl | ||
import dpctl.tensor as dpt | ||
import numpy as np | ||
from mpi4py import MPI | ||
|
||
from sklearnex.spmd.covariance import IncrementalEmpiricalCovariance | ||
|
||
|
||
def get_local_data(data, comm): | ||
rank = comm.Get_rank() | ||
num_ranks = comm.Get_size() | ||
local_size = (data.shape[0] + num_ranks - 1) // num_ranks | ||
return data[rank * local_size : (rank + 1) * local_size] | ||
|
||
|
||
# We create SYCL queue and MPI communicator to perform computation on multiple GPUs | ||
|
||
q = dpctl.SyclQueue("gpu") | ||
comm = MPI.COMM_WORLD | ||
|
||
num_batches = 2 | ||
seed = 77 | ||
num_samples, num_features = 3000, 3 | ||
drng = np.random.default_rng(seed) | ||
X = drng.random(size=(num_samples, num_features)) | ||
|
||
# Local data are obtained for each GPU and splitted into batches | ||
|
||
X_local = get_local_data(X, comm) | ||
X_split = np.array_split(X_local, num_batches) | ||
|
||
cov = IncrementalEmpiricalCovariance() | ||
|
||
# Partial fit is called for each batch on each GPU | ||
|
||
for i in range(num_batches): | ||
dpt_X = dpt.asarray(X_split[i], usm_type="device", sycl_queue=q) | ||
cov.partial_fit(dpt_X) | ||
|
||
# Finalization of results is performed in a lazy way after requesting results like in non-SPMD incremental estimators. | ||
|
||
print(f"Computed covariance values on rank {comm.Get_rank()}:\n", cov.covariance_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# ============================================================================== | ||
# Copyright 2024 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
|
||
import numpy as np | ||
|
||
from daal4py.sklearn._utils import get_dtype | ||
|
||
from ...covariance import ( | ||
IncrementalEmpiricalCovariance as base_IncrementalEmpiricalCovariance, | ||
) | ||
from ...datatypes import _convert_to_supported, to_table | ||
from ...utils import _check_array | ||
from .._base import BaseEstimatorSPMD | ||
|
||
|
||
class IncrementalEmpiricalCovariance( | ||
BaseEstimatorSPMD, base_IncrementalEmpiricalCovariance | ||
): | ||
def _reset(self): | ||
self._partial_result = super( | ||
base_IncrementalEmpiricalCovariance, self | ||
)._get_backend("covariance", None, "partial_compute_result") | ||
|
||
def partial_fit(self, X, y=None, queue=None): | ||
""" | ||
Computes partial data for the covariance matrix | ||
from data batch X and saves it to `_partial_result`. | ||
Parameters | ||
---------- | ||
X : array-like of shape (n_samples, n_features) | ||
Training data batch, where `n_samples` is the number of samples | ||
in the batch, and `n_features` is the number of features. | ||
y : Ignored | ||
Not used, present for API consistency by convention. | ||
queue : dpctl.SyclQueue | ||
If not None, use this queue for computations. | ||
Returns | ||
------- | ||
self : object | ||
Returns the instance itself. | ||
""" | ||
X = _check_array(X, dtype=[np.float64, np.float32], ensure_2d=True) | ||
|
||
self._queue = queue | ||
|
||
policy = super(base_IncrementalEmpiricalCovariance, self)._get_policy(queue, X) | ||
|
||
X = _convert_to_supported(policy, X) | ||
|
||
if not hasattr(self, "_dtype"): | ||
self._dtype = get_dtype(X) | ||
|
||
params = self._get_onedal_params(self._dtype) | ||
table_X = to_table(X) | ||
self._partial_result = super( | ||
base_IncrementalEmpiricalCovariance, self | ||
)._get_backend( | ||
"covariance", | ||
None, | ||
"partial_compute", | ||
policy, | ||
params, | ||
self._partial_result, | ||
table_X, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# ============================================================================== | ||
# Copyright 2024 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
|
||
from onedal.spmd.covariance import ( | ||
IncrementalEmpiricalCovariance as onedalSPMD_IncrementalEmpiricalCovariance, | ||
) | ||
|
||
from ...covariance import ( | ||
IncrementalEmpiricalCovariance as base_IncrementalEmpiricalCovariance, | ||
) | ||
|
||
|
||
class IncrementalEmpiricalCovariance(base_IncrementalEmpiricalCovariance): | ||
""" | ||
Incremental distributed estimator for covariance. | ||
Allows to distributely compute empirical covariance estimated by maximum | ||
likelihood method if data are splitted into batches. | ||
API is the same as for `sklearnex.covariance.IncrementalEmpiricalCovariance` | ||
""" | ||
|
||
_onedal_incremental_covariance = staticmethod( | ||
onedalSPMD_IncrementalEmpiricalCovariance | ||
) |
Oops, something went wrong.