|
| 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 | +from warnings import warn |
| 18 | + |
| 19 | +import dpctl |
| 20 | +import dpctl.tensor as dpt |
| 21 | +import numpy as np |
| 22 | +from mpi4py import MPI |
| 23 | +from scipy.special import expit |
| 24 | +from sklearn.datasets import make_classification |
| 25 | +from sklearn.metrics import accuracy_score |
| 26 | +from sklearn.model_selection import train_test_split |
| 27 | + |
| 28 | +from sklearnex.spmd.linear_model import LogisticRegression |
| 29 | + |
| 30 | + |
| 31 | +def generate_X_y(par, seed): |
| 32 | + np.random.seed() |
| 33 | + ns, nf = par["ns"], par["nf"] |
| 34 | + |
| 35 | + assert nf > 2 |
| 36 | + # 2 last features will be redundant, weights are same for all ranks |
| 37 | + np.random.seed(42) |
| 38 | + intercept = np.random.normal(0, 5) |
| 39 | + weights = np.hstack([np.random.normal(0, 4, nf - 2), np.zeros(2)]) |
| 40 | + |
| 41 | + np.random.seed(seed) |
| 42 | + X = np.random.normal(0, 3, (ns, nf)) |
| 43 | + noise = np.random.normal(0, 4, ns) |
| 44 | + y = expit(X @ weights + noise + intercept) >= 0.5 |
| 45 | + y = y.astype(np.int32) |
| 46 | + return X, y |
| 47 | + |
| 48 | + |
| 49 | +comm = MPI.COMM_WORLD |
| 50 | +rank = comm.Get_rank() |
| 51 | +size = comm.Get_size() |
| 52 | + |
| 53 | +if dpctl.has_gpu_devices: |
| 54 | + q = dpctl.SyclQueue("gpu") |
| 55 | +else: |
| 56 | + raise RuntimeError( |
| 57 | + "GPU devices unavailable. Currently, " |
| 58 | + "SPMD execution mode is implemented only for this device type." |
| 59 | + ) |
| 60 | + |
| 61 | +params = {"ns": 100000, "nf": 8} |
| 62 | + |
| 63 | +X, y = generate_X_y(params, rank) |
| 64 | +X_train, X_test, y_train, y_test = train_test_split( |
| 65 | + X, y, test_size=0.2, random_state=rank |
| 66 | +) |
| 67 | + |
| 68 | +dpt_X_train = dpt.asarray(X_train, usm_type="device", sycl_queue=q) |
| 69 | +dpt_y_train = dpt.asarray(y_train, usm_type="device", sycl_queue=q) |
| 70 | +dpt_X_test = dpt.asarray(X_test, usm_type="device", sycl_queue=q) |
| 71 | +dpt_y_test = dpt.asarray(y_test, usm_type="device", sycl_queue=q) |
| 72 | + |
| 73 | +model_spmd = LogisticRegression() |
| 74 | +model_spmd.fit(dpt_X_train, dpt_y_train) |
| 75 | + |
| 76 | +y_predict = model_spmd.predict(dpt_X_test) |
| 77 | + |
| 78 | +print("Distributed LogisticRegression results:") |
| 79 | +print("Coeficients on rank {}:\n{}:".format(rank, model_spmd.coef_)) |
| 80 | +print("Intercept on rank {}:\n{}:".format(rank, model_spmd.intercept_)) |
| 81 | +print("Ground truth (first 5 observations on rank {}):\n{}".format(rank, y_test[:5])) |
| 82 | +print( |
| 83 | + "Classification results (first 5 observations on rank {}):\n{}".format( |
| 84 | + rank, dpt.to_numpy(y_predict)[:5] |
| 85 | + ) |
| 86 | +) |
| 87 | +print( |
| 88 | + "Accuracy for entire rank {} (2 classes): {}\n".format( |
| 89 | + rank, accuracy_score(y_test, dpt.to_numpy(y_predict)) |
| 90 | + ) |
| 91 | +) |
0 commit comments