Class - TensorCodec#

class openfl.pipelines.tensor_codec.TensorCodec(compression_pipeline)[source]#

Bases: object

TensorCodec is responsible for the following.

1. Tracking the compression/decompression related dependencies of a given tensor. 2. Acting as a TensorKey aware wrapper for the compression_pipeline functionality.

compression_pipeline#

The pipeline used for compression.

lossless_pipeline#

The pipeline used for lossless compression.

__init__(compression_pipeline)[source]#

Initialize the TensorCodec.

Parameters:

compression_pipeline – The pipeline used for compression.

Methods

__init__(compression_pipeline)

Initialize the TensorCodec.

apply_delta(tensor_key, delta, ...[, ...])

Add delta to the nparray.

compress(tensor_key, data[, require_lossless])

Function-wrapper around the tensor_pipeline.forward function.

decompress(tensor_key, data, ...[, ...])

Function-wrapper around the tensor_pipeline.backward function.

find_dependencies(tensor_key, send_model_deltas)

Resolve the tensors required to do the specified operation.

generate_delta(tensor_key, nparray, ...)

Create delta from the updated layer and base layer.

set_lossless_pipeline(lossless_pipeline)

Set lossless pipeline.

static apply_delta(tensor_key, delta, base_model_nparray, creates_model=False)[source]#

Add delta to the nparray.

Parameters:
  • tensor_key – This is the tensor_key associated with the delta. Should have a tag of ‘trained’ or ‘aggregated’.

  • delta – Weight delta between the new model and old model.

  • base_model_nparray – The nparray that corresponds to the prior weights.

  • creates_model – If flag is set, the tensorkey returned will correspond to the aggregator model.

Returns:

Latest model layer tensorkey. new_model_nparray: Latest layer weights.

Return type:

new_model_tensor_key

compress(tensor_key, data, require_lossless=False, **kwargs)[source]#

Function-wrapper around the tensor_pipeline.forward function.

It also keeps track of the tensorkeys associated with the compressed nparray.

Parameters:
  • tensor_key – TensorKey is provided to verify it should be compressed, and new TensorKeys returned will be derivatives of the existing tensor_name.

  • data – (uncompressed) numpy array associated with the tensor_key.

  • require_lossless – boolean. Does tensor require compression.

Returns:

Tensorkey corresponding to the decompressed

tensor.

compressed_nparray: The compressed tensor. metadata: metadata associated with compressed tensor.

Return type:

compressed_tensor_key

decompress(tensor_key, data, transformer_metadata, require_lossless=False, **kwargs)[source]#

Function-wrapper around the tensor_pipeline.backward function.

It also keeps track of the tensorkeys associated with the decompressed nparray.

Parameters:
  • tensor_key – TensorKey is provided to verify it should be decompressed, and new TensorKeys returned will be derivatives of the existing tensor_name.

  • data – (compressed) numpy array associated with the tensor_key.

  • transformer_metadata – metadata associated with the compressed tensor.

  • require_lossless – boolean, does data require lossless decompression.

Returns:

Tensorkey corresponding to the

decompressed tensor.

decompressed_nparray: The decompressed tensor.

Return type:

decompressed_tensor_key

find_dependencies(tensor_key, send_model_deltas)[source]#

Resolve the tensors required to do the specified operation.

Parameters:
  • tensor_key – A tuple containing the tensor name, origin, round number, report, and tags.

  • send_model_deltas – A boolean flag indicating whether to send model deltas.

Returns:

A list of tensor keys that are

dependencies of the given tensor key.

Return type:

tensor_key_dependencies

static generate_delta(tensor_key, nparray, base_model_nparray)[source]#

Create delta from the updated layer and base layer.

Parameters:
  • tensor_key – This is the tensor_key associated with the nparray. Should have a tag of ‘trained’ or ‘aggregated’

  • nparray – The nparray that corresponds to the tensorkey.

  • base_model_nparray – The base model tensor that will be subtracted from the new weights.

Returns:

Tensorkey that corresponds to the delta weight

array.

delta: Difference between the provided tensors.

Return type:

delta_tensor_key

set_lossless_pipeline(lossless_pipeline)[source]#

Set lossless pipeline.

Parameters:

lossless_pipeline – The pipeline to be set as the lossless pipeline. It should be a pipeline that is not lossy.

Raises:

AssertionError – If the provided pipeline is not lossless.