Source code for nvflare.app_common.decomposers.numpy_decomposers

# Copyright (c) 2024, NVIDIA CORPORATION.  All rights reserved.
#
# 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.
"""Decomposers for types from app_common and Machine Learning libraries."""
import os
from abc import ABC
from io import BytesIO
from typing import Any

import numpy as np

from nvflare.fuel.utils import fobs
from nvflare.fuel.utils.fobs.datum import DatumManager


[docs]class NumpyScalarDecomposer(fobs.Decomposer, ABC): """Decomposer base class for all numpy types with item method."""
[docs] def decompose(self, target: Any, manager: DatumManager = None) -> Any: return target.item()
[docs] def recompose(self, data: Any, manager: DatumManager = None) -> np.ndarray: return self.supported_type()(data)
[docs]class Float64ScalarDecomposer(NumpyScalarDecomposer):
[docs] def supported_type(self): return np.float64
[docs]class Float32ScalarDecomposer(NumpyScalarDecomposer):
[docs] def supported_type(self): return np.float32
[docs]class Int64ScalarDecomposer(NumpyScalarDecomposer):
[docs] def supported_type(self): return np.int64
[docs]class Int32ScalarDecomposer(NumpyScalarDecomposer):
[docs] def supported_type(self): return np.int32
[docs]class NumpyArrayDecomposer(fobs.Decomposer):
[docs] def supported_type(self): return np.ndarray
[docs] def decompose(self, target: np.ndarray, manager: DatumManager = None) -> Any: stream = BytesIO() np.save(stream, target, allow_pickle=False) return stream.getvalue()
[docs] def recompose(self, data: Any, manager: DatumManager = None) -> np.ndarray: stream = BytesIO(data) return np.load(stream, allow_pickle=False)
[docs]def register(): if register.registered: return fobs.register_folder(os.path.dirname(__file__), __package__) register.registered = True
register.registered = False