Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable serialization in daal4py algorithm classes #2067

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions generator/wrapper_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,8 @@ def __cinit__(self):

# this is our actual algorithm class for Python
cdef class {{algo}}{{'('+iface[0]|lower+'__iface__)' if iface[0] else ''}}:
cdef tuple _params

'''
{{algo}}
{{params_all|fmt('{}', 'sphinx', sep='\n')|indent(4)}}
Expand All @@ -1017,6 +1019,17 @@ def __cinit__(self,
self.c_ptr = mk_{{algo}}(
{{params_all|fmt('{}', 'arg_cyext', sep=',\n')|indent(25+(algo|length))}}
)
current_locals = locals()
ordered_input_args = '''
{{params_all|fmt('{}', 'name', sep=' ')|indent(0)}}
'''.strip().split()
self._params = tuple(
current_locals[arg]
for arg in ordered_input_args
)

def __reduce__(self):
return (self.__class__, self._params)

{% if not iface[0] %}
# the C++ manager__iface__ (de-templatized)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_daal4py_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# ==============================================================================
# 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 pickle
import unittest

import numpy as np

import daal4py


class Test(unittest.TestCase):
def test_serialization_of_qr(self):
obj_original = daal4py.qr(fptype="float")
obj_deserialized = pickle.loads(pickle.dumps(obj_original))

Check warning on line 28 in tests/test_daal4py_serialization.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_daal4py_serialization.py#L28

Pickle and modules that wrap it can be unsafe when used to deserialize untrusted data, possible security issue. (B301)

rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(10, 5))

Q_orig = obj_original.compute(X).matrixQ
Q_deserialized = obj_deserialized.compute(X).matrixQ
np.testing.assert_almost_equal(Q_orig, Q_deserialized)
assert Q_orig.dtype == Q_deserialized.dtype

def test_serialization_of_kmeans(self):
obj_original = daal4py.kmeans_init(nClusters=4)
obj_deserialized = pickle.loads(pickle.dumps(obj_original))

Check warning on line 40 in tests/test_daal4py_serialization.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_daal4py_serialization.py#L40

Pickle and modules that wrap it can be unsafe when used to deserialize untrusted data, possible security issue. (B301)

rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(100, 20))

np.testing.assert_almost_equal(
obj_original.compute(X).centroids,
obj_deserialized.compute(X).centroids,
)
Loading