# Copyright 2024 Flower Labs GmbH. 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.# =============================================================================="""Mods that report statistics about message communication."""fromloggingimportINFOimportnumpyasnpfromflwr.client.typingimportClientAppCallablefromflwr.common.contextimportContextfromflwr.common.loggerimportlogfromflwr.common.messageimportMessage
[docs]defmessage_size_mod(msg:Message,ctxt:Context,call_next:ClientAppCallable)->Message:"""Message size mod. This mod logs the size in bytes of the message being transmited. """message_size_in_bytes=0forrecordinmsg.content.values():message_size_in_bytes+=record.count_bytes()log(INFO,"Message size: %i bytes",message_size_in_bytes)returncall_next(msg,ctxt)
[docs]defarrays_size_mod(msg:Message,ctxt:Context,call_next:ClientAppCallable)->Message:"""Arrays size mod. This mod logs the number of array elements transmitted in ``ArrayRecord``s of the message as well as their sizes in bytes. """model_size_stats={}arrays_size_in_bytes=0forrecord_name,arr_recordinmsg.content.array_records.items():arr_record_bytes=arr_record.count_bytes()arrays_size_in_bytes+=arr_record_byteselement_count=0forarrayinarr_record.values():element_count+=(int(np.prod(array.shape))ifarray.shapeelsearray.numpy().size)model_size_stats[f"{record_name}"]={"elements":element_count,"bytes":arr_record_bytes,}ifmodel_size_stats:log(INFO,model_size_stats)log(INFO,"Total array elements transmitted: %i bytes",arrays_size_in_bytes)returncall_next(msg,ctxt)