# tff.templates.AggregationProcess

<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) |

A stateful process that aggregates values.

Inherits From: [`MeasuredProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/MeasuredProcess), [`IterativeProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/IterativeProcess)

    tff.templates.AggregationProcess(
        initialize_fn: https://www.tensorflow.org/federated/api_docs/python/tff/Computation,
        next_fn: https://www.tensorflow.org/federated/api_docs/python/tff/Computation
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Implementing Custom Aggregations](https://www.tensorflow.org/federated/tutorials/custom_aggregators) |

This class inherits the constraints documented by
[`tff.templates.MeasuredProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/MeasuredProcess).

A [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess) is a [`tff.templates.MeasuredProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/MeasuredProcess)
that formalizes the type signature of `initialize_fn` and `next_fn` for
aggregation.

Compared to the [`tff.templates.MeasuredProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/MeasuredProcess), this class requires a second
input argument, which is a value placed at `CLIENTS` and to be aggregated.
The `result` field of returned [`tff.templates.MeasuredProcessOutput`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/MeasuredProcessOutput),
representing the aggregate, must be placed at `SERVER` and does not
necessarily need to have type signature equal to the type signature of the
second input argument.

The intended composition pattern for [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess) is
that of nesting. An aggregation will broadly consist of three logical parts:

- A pre-aggregation computation placed at `CLIENTS`.
- Actual aggregation.
- A post-aggregation computation placed at `SERVER`. The second step can be realized by direct application of appropriate intrinsic such as [`tff.federated_sum`](https://www.tensorflow.org/federated/api_docs/python/tff/federated_sum), or by delegation to (one or more) "inner" aggregation processes.

Both `initialize` and `next` must be [`tff.Computation`](https://www.tensorflow.org/federated/api_docs/python/tff/Computation)s with the following
type signatures:

- initialize: `( -> S@SERVER)`
- next: `(<S@SERVER, V@CLIENTS, *> ->
  <state=S@SERVER, result=V'@SERVER, measurements=M@SERVER>)` where `*` represents optional other arguments placed at `CLIENTS`. This can be used for weighted aggregation, where the third parameter is the weight.

Note that while the value type to be aggregated will often be preserved
(i.e., `V == V'`), it is not required. An example is sampling-based
aggregation.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `initialize_fn` | A no-arg [`tff.Computation`](https://www.tensorflow.org/federated/api_docs/python/tff/Computation) that returns the initial state of the aggregation process. The returned state must be a server-placed federated value. Let the type of this state be called `S@SERVER`. |
| `next_fn` | A [`tff.Computation`](https://www.tensorflow.org/federated/api_docs/python/tff/Computation) that represents the iterated function. `next_fn` must accept at least two arguments, the first of which is of a type assignable from the state type `S@SERVER` and the second of which is client-placed data of type `V@CLIENTS`. `next_fn` must return a `MeasuredProcessOutput` where the `state` attribute is assignable to the first argument and the `result` is value placed at `SERVER`. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | If `initialize_fn` and `next_fn` are not instances of [`tff.Computation`](https://www.tensorflow.org/federated/api_docs/python/tff/Computation). |
| `TemplateInitFnParamNotEmptyError` | If `initialize_fn` has any input arguments. |
| `TemplateStateNotAssignableError` | If the `state` returned by either `initialize_fn` or `next_fn` is not assignable to the first input argument of `next_fn`. |
| `TemplateNotMeasuredProcessOutputError` | If `next_fn` does not return a `MeasuredProcessOutput`. |
| `TemplateNextFnNumArgsError` | If `next_fn` does not have at least two input arguments. |
| `AggregationNotFederatedError` | If `initialize_fn` and `next_fn` are not computations operating on federated types. |
| `AggregationPlacementError` | If the placements of `initialize_fn` and `next_fn` are not matching the expected type signature. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `initialize` | A no-arg [`tff.Computation`](https://www.tensorflow.org/federated/api_docs/python/tff/Computation) that returns the initial state. |
| `is_weighted` | True if `next` takes a third argument for weights. |
| `next` | A [`tff.Computation`](https://www.tensorflow.org/federated/api_docs/python/tff/Computation) that runs one iteration of the process. <br /> Its first argument should always be the current state (originally produced by the `initialize` attribute), the second argument must be the input placed at `CLIENTS`, and the return type must be a [`tff.templates.MeasuredProcessOutput`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/MeasuredProcessOutput) with each field placed at `SERVER`. |
| `state_type` | The [`tff.Type`](https://www.tensorflow.org/federated/api_docs/python/tff/types/Type) of the state of the process. |

<br />