<commit_before># Copyright 2020, The TensorFlow Federated Authors.
#
# 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.
"""Library of helper functions for working with TensorFlow `tf.Variable`."""

import contextlib
import operator

import numpy as np
import tensorflow as tf


@contextlib.contextmanager
def record_variable_creation_scope():
  """Creates a single use contextmanager for capture variable creation calls."""
  variable_list = []

  def logging_variable_creator(next_creator, **kwargs):
    variable = next_creator(**kwargs)
    variable_list.append(variable)
    return variable

  with contextlib.ExitStack() as stack:
    stack.enter_context(tf.variable_creator_scope(logging_variable_creator))
    yield variable_list


class TensorVariable:
  """A class that is duck-typed to `tf.Variable` but only uses `tf.Tensor`.

  This class implements the interface contract of `tf.Variable`, for
  documentation see http://www.tensorflow.org/api_docs/python/tf/Variable. To
  be true to the API sometimes arguments are ignored (e.g. `use_locking`).

  This is intended for creating `tff.learning.models.FunctionalModel` and is
  *not* compatible with `tf.distribute` strategies.

  IMPORTANT: this class behaves as if
  `tf.autograph.experimental.Feature.AUTO_CONTROL_DEPS` (ACD) was applied, which
  is the same behavior as inside a `tf.function`. This may have surprising
  side-effects if code authors were not expecting it, but also is more similar
  to standard Python code where the line ordering implies execution ordering.

  IMPORTANT: the `name` attribute does not behave the same as `tf.Variable`.
  Notably, it does not do name deduplication in graph contexts (no `_#` suffix
  is applied), and the returned name string does not refer to the fetchable
  resource from a session.
  """

  def __init__(self,
               initial_value,
               dtype=None,
               validate_shape=True,
               shape=None,
               **kwargs):
    """For details see https://www.tensorflow.org/api_docs/python/tf/Variable#args_1."""
    if callable(initial_value):
      if dtype is None:
        raise ValueError('When `initial_value` is a callable, `dtype` must be '
                         'specified.')
      initial_value = initial_value()
    if tf.is_tensor(initial_value):
      self._initial_value = initial_value
    else:
      if dtype is not None:
        self._initial_value = tf.convert_to_tensor(initial_value, dtype)
      else:
        self._initial_value = tf.convert_to_tensor(initial_value)
    self._tensor = self._initial_value
    self._validate_shape = validate_shape
    if shape is None:
      self._shape = self._initial_value.shape
    else:
      if not isinstance(shape, tf.TensorShape):
        shape = tf.TensorShape(shape)
      self._shape = shape
      self._check_shape(self._tensor)
    self._name = kwargs.get('name', 'Variable')
    self._save_slice_info = None

  @property
  def shape(self) -> tf.TensorShape:
    return self._tensor.shape

  @property
  def dtype(self) -> tf.dtypes.DType:
    return self._tensor.dtype

  @property
  def name(self) -> str:
    return self._name

  def assign(self, value, use_locking=False, name=None, read_value=True):
    del use_locking  # Unused.
    value = tf.convert_to_tensor(value)
    self._check_shape(value)
    self._tensor = value
    if tf.executing_eagerly() and not read_value:
      return None
    else:
      return tf.identity(self._tensor, name)

  def _check_shape(self, value):
    if not self._validate_shape:
      return
    if not self._shape.is_compatible_with(tf.TensorShape(value.shape)):
      raise ValueError(
          'Cannot assign value to variable {self!r}. The TensorVariable shape '
          f'{self._shape}, and the value shape {value.shape} are incompatible.')

  def assign_add(self, value, use_locking=False, name=None, read_value=True):
    del use_locking  # Unused.
    value = tf.convert_to_tensor(value)<commit_msg>Fix missing format string prefix in error message.<commit_after># Copyright 2020, The TensorFlow Federated Authors.
#
# 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.
"""Library of helper functions for working with TensorFlow `tf.Variable`."""

import contextlib
import operator

import numpy as np
import tensorflow as tf


@contextlib.contextmanager
def record_variable_creation_scope():
  """Creates a single use contextmanager for capture variable creation calls."""
  variable_list = []

  def logging_variable_creator(next_creator, **kwargs):
    variable = next_creator(**kwargs)
    variable_list.append(variable)
    return variable

  with contextlib.ExitStack() as stack:
    stack.enter_context(tf.variable_creator_scope(logging_variable_creator))
    yield variable_list


class TensorVariable:
  """A class that is duck-typed to `tf.Variable` but only uses `tf.Tensor`.

  This class implements the interface contract of `tf.Variable`, for
  documentation see http://www.tensorflow.org/api_docs/python/tf/Variable. To
  be true to the API sometimes arguments are ignored (e.g. `use_locking`).

  This is intended for creating `tff.learning.models.FunctionalModel` and is
  *not* compatible with `tf.distribute` strategies.

  IMPORTANT: this class behaves as if
  `tf.autograph.experimental.Feature.AUTO_CONTROL_DEPS` (ACD) was applied, which
  is the same behavior as inside a `tf.function`. This may have surprising
  side-effects if code authors were not expecting it, but also is more similar
  to standard Python code where the line ordering implies execution ordering.

  IMPORTANT: the `name` attribute does not behave the same as `tf.Variable`.
  Notably, it does not do name deduplication in graph contexts (no `_#` suffix
  is applied), and the returned name string does not refer to the fetchable
  resource from a session.
  """

  def __init__(self,
               initial_value,
               dtype=None,
               validate_shape=True,
               shape=None,
               **kwargs):
    """For details see https://www.tensorflow.org/api_docs/python/tf/Variable#args_1."""
    if callable(initial_value):
      if dtype is None:
        raise ValueError('When `initial_value` is a callable, `dtype` must be '
                         'specified.')
      initial_value = initial_value()
    if tf.is_tensor(initial_value):
      self._initial_value = initial_value
    else:
      if dtype is not None:
        self._initial_value = tf.convert_to_tensor(initial_value, dtype)
      else:
        self._initial_value = tf.convert_to_tensor(initial_value)
    self._tensor = self._initial_value
    self._validate_shape = validate_shape
    if shape is None:
      self._shape = self._initial_value.shape
    else:
      if not isinstance(shape, tf.TensorShape):
        shape = tf.TensorShape(shape)
      self._shape = shape
      self._check_shape(self._tensor)
    self._name = kwargs.get('name', 'Variable')
    self._save_slice_info = None

  @property
  def shape(self) -> tf.TensorShape:
    return self._tensor.shape

  @property
  def dtype(self) -> tf.dtypes.DType:
    return self._tensor.dtype

  @property
  def name(self) -> str:
    return self._name

  def assign(self, value, use_locking=False, name=None, read_value=True):
    del use_locking  # Unused.
    value = tf.convert_to_tensor(value)
    self._check_shape(value)
    self._tensor = value
    if tf.executing_eagerly() and not read_value:
      return None
    else:
      return tf.identity(self._tensor, name)

  def _check_shape(self, value):
    if not self._validate_shape:
      return
    if not self._shape.is_compatible_with(tf.TensorShape(value.shape)):
      raise ValueError(
          f'Cannot assign value to variable {self!r}. The TensorVariable shape '
          f'{self._shape}, and the value shape {value.shape} are incompatible.')

  def assign_add(self, value, use_locking=False, name=None, read_value=True):
    del use_locking  # Unused.
    value = tf.convert_to_tensor(value)
{'fraction_non_alphanumeric': 0.08541202672605791, 'fraction_numerical': 0.0020044543429844097, 'mean_word_length': 3.4570719602977666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2402819', 'n_tokens_mistral': 2567, 'n_tokens_neox': 2509, 'n_words': 857}