# tff.aggregators.EncodedSumFactory

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/federated/blob/v0.87.0 Version 2.0, January 2004 Licensed under the Apache License, Version 2.0 (the) |

`UnweightedAggregationFactory` for encoded sum.

Inherits From: [`UnweightedAggregationFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory)

    tff.aggregators.EncodedSumFactory(
        encoder_fn: _EncoderConstructor
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Tuning recommended aggregations for learning](https://www.tensorflow.org/federated/tutorials/tuning_recommended_aggregators) |

The created [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess) aggregates values placed at
`CLIENTS` according to the provided `GatherEncoder` objects, and outputs the
result placed at `SERVER`.

A `GatherEncoder` defines encode and decode methods, which are applied by
`CLIENTS` before aggregation and by `SERVER` after aggregation. Usually these
are such that the encoded representation communicated from `CLIENTS` to
`SERVER` is smaller than communicating the original value, possibly at a cost
of communicating slightly inaccurate values. A common example is to apply
quantization. In addition, `GatherEncoder` enables the decoding logic to be
split based on which part of its decode part commutes with sum, which is
automatically utilized by the implementation.

> [!NOTE]
> **Note:** The current implementation does not allow for specification of an "inner" aggregation factory. This will be possible in a future version, when used together with a `GatherEncoder` of which the entire decode logic commutes with sum, as per its `fully_commutes_with_sum` property. Contributions welcome.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `encoder_fn` | A one-arg callable, mapping a [`tf.TensorSpec`](https://www.tensorflow.org/api_docs/python/tf/TensorSpec), to a `GatherEncoder`. |

<br />

## Methods

### `create`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    create(
        value_type: factory.ValueType
    ) -> https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess

Creates a `tff.aggregators.AggregationProcess` without weights.

The provided `value_type` is a non-federated [`tff.Type`](https://www.tensorflow.org/federated/api_docs/python/tff/types/Type), that is, not a
[`tff.FederatedType`](https://www.tensorflow.org/federated/api_docs/python/tff/types/FederatedType).

The returned `tff.aggregators.AggregationProcess` will be created for
aggregation of values matching `value_type` placed at [`tff.CLIENTS`](https://www.tensorflow.org/federated/api_docs/python/tff#CLIENTS).
That is, its `next` method will expect type
`<S@SERVER, {value_type}@CLIENTS>`, where `S` is the unplaced return type of
its `initialize` method.

<br />

<br />

<br />

| Args ||
|---|---|
| `value_type` | A non-federated [`tff.Type`](https://www.tensorflow.org/federated/api_docs/python/tff/types/Type). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess). ||

<br />

### `quantize_above_threshold`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    @classmethod
    quantize_above_threshold(
        quantization_bits=8, threshold=20000, **kwargs
    )

Quantization of values with at least `threshold` elements.

Given a `value_type` in the `create` method, this classmethod configures the
`EncodedSumFactory` to apply uniform quantization to all instances of
[`tff.TensorType`](https://www.tensorflow.org/federated/api_docs/python/tff/types/TensorType) in the `value_type` which have more than `threshold`
elements.

Precisely, for each tensor `t`, this operation corresponds to
`t = round((t - min(t)) / (max(t) - min(t)) * (2**quantizaton_bits - 1))`.

If a type does not have more than `threshold` elements, it is summed
directly without being modified.

<br />

<br />

<br />

| Args ||
|---|---|
| `quantization_bits` | A integer specifying the quantization bitwidth. |
| `threshold` | A non-negative integer. Only tensors with more than this number of elements are quantized. |
| `**kwargs` | Keyword arguments. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An `EncodedSumFactory`. ||

<br />