AOCL-DA provides a patching mechanism that allows you to use AOCL-DA’s optimized implementations while maintaining compatibility with scikit-learn’s API. This is particularly useful for existing codebases that use scikit-learn.
1# Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without modification,
4# are permitted provided that the following conditions are met:
5# 1. Redistributions of source code must retain the above copyright notice,
6# this list of conditions and the following disclaimer.
7# 2. Redistributions in binary form must reproduce the above copyright notice,
8# this list of conditions and the following disclaimer in the documentation
9# and/or other materials provided with the distribution.
10# 3. Neither the name of the copyright holder nor the names of its contributors
11# may be used to endorse or promote products derived from this software without
12# specific prior written permission.
13#
14# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23# POSSIBILITY OF SUCH DAMAGE.
24#
25
26"""
27Sklearn patching with model persistence example
28"""
29
30import sys
31import os
32import pickle
33import numpy as np
34from aoclda.sklearn import skpatch
35
36
37def model_persistence_skpatch_pca_example():
38 """
39 Using sklearn patching with model persistence
40 """
41
42 # Patch sklearn to use AOCL-DA's PCA implementation
43 print("Patching sklearn to use AOCL-DA's PCA implementation...")
44 skpatch('PCA')
45
46 from sklearn.decomposition import PCA
47
48 # Generate sample data
49 rng = np.random.default_rng(42)
50 X_train = rng.standard_normal((1000, 10))
51
52 # Fit the model using sklearn's API
53 print("Training PCA model using sklearn API...")
54 pca = PCA(n_components=5)
55 pca.fit(X_train)
56
57 # Transform some data
58 X_transformed = pca.transform(X_train[:10])
59 print(f"Transformed data shape: {X_transformed.shape}")
60
61 # Verify AOCL-DA is being used
62 if hasattr(pca, 'aocl') and pca.aocl:
63 print("Using AOCL-DA implementation")
64 else:
65 print("Warning: Not using AOCL-DA implementation")
66
67 # Save the trained model
68 print("\nSaving model to 'pca_sklearn_model.pkl'...")
69 with open('pca_sklearn_model.pkl', 'wb') as f:
70 pickle.dump(pca, f)
71
72 # Delete the original model
73 del pca
74
75 # Later, load and use the model
76 print("Loading model from 'pca_sklearn_model.pkl'...")
77 with open('pca_sklearn_model.pkl', 'rb') as f:
78 pca_loaded = pickle.load(f)
79
80 X_new_transformed = pca_loaded.transform(X_train[:10])
81
82 # Verify the results match
83 if np.array_equal(X_transformed, X_new_transformed):
84 print("\nModel persistence verified - transformations match!")
85 else:
86 print("\nError: Transformations do not match")
87 max_diff = np.max(np.abs(X_transformed - X_new_transformed))
88 print(f"Maximum difference: {max_diff}")
89 os.remove('pca_sklearn_model.pkl')
90 sys.exit(1)
91
92 # Clean up created files
93 os.remove('pca_sklearn_model.pkl')
94
95
96if __name__ == "__main__":
97 try:
98 model_persistence_skpatch_pca_example()
99 except Exception as e:
100 print(f"Error: {e}")
101 sys.exit(1)