xref: /aosp_15_r20/external/tensorflow/tensorflow/python/distribute/reduce_util.py (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Utilities for reduce operations."""
16
17import enum
18
19from tensorflow.python.ops import variable_scope
20from tensorflow.python.util.tf_export import tf_export
21
22
23@tf_export("distribute.ReduceOp")
24class ReduceOp(enum.Enum):
25  """Indicates how a set of values should be reduced.
26
27  * `SUM`: Add all the values.
28  * `MEAN`: Take the arithmetic mean ("average") of the values.
29  """
30  # TODO(priyag): Add the following types:
31  # `MIN`: Return the minimum of all values.
32  # `MAX`: Return the maximum of all values.
33  SUM = "SUM"
34  MEAN = "MEAN"
35
36  @staticmethod
37  def from_variable_aggregation(aggregation):
38    mapping = {
39        variable_scope.VariableAggregation.SUM: ReduceOp.SUM,
40        variable_scope.VariableAggregation.MEAN: ReduceOp.MEAN,
41    }
42
43    reduce_op = mapping.get(aggregation)
44    if not reduce_op:
45      raise ValueError("Could not convert from `tf.VariableAggregation` %s to"
46                       "`tf.distribute.ReduceOp` type" % aggregation)
47    return reduce_op
48