# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Distribution tests for keras.layers.preprocessing.text_vectorization."""

import numpy as np

from tensorflow.python import keras
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.distribute import combinations as ds_combinations
from tensorflow.python.framework import config
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import test_combinations as combinations
from tensorflow.python.keras import keras_parameterized
from tensorflow.python.keras.distribute.strategy_combinations import all_strategies
from tensorflow.python.keras.layers.preprocessing import preprocessing_test_utils
from tensorflow.python.keras.layers.preprocessing import text_vectorization
from tensorflow.python.platform import test


@ds_combinations.generate(
    combinations.combine(distribution=all_strategies, mode=["eager"]))
class TextVectorizationDistributionTest(
    keras_parameterized.TestCase,
    preprocessing_test_utils.PreprocessingLayerTest):

  def test_distribution_strategy_output(self, distribution):
    vocab_data = ["earth", "wind", "and", "fire"]
    input_array = np.array([["earth", "wind", "and", "fire"],
                            ["fire", "and", "earth", "michigan"]])
    input_dataset = dataset_ops.Dataset.from_tensor_slices(input_array).batch(
        2, drop_remainder=True)

    expected_output = [[2, 3, 4, 5], [5, 4, 2, 1]]

    config.set_soft_device_placement(True)

    with distribution.scope():
      input_data = keras.Input(shape=(None,), dtype=dtypes.string)
      layer = text_vectorization.TextVectorization(
          max_tokens=None,
          standardize=None,
          split=None,
          output_mode=text_vectorization.INT)
      layer.set_vocabulary(vocab_data)
      int_data = layer(input_data)
      model = keras.Model(inputs=input_data, outputs=int_data)

    output_dataset = model.predict(input_dataset)
    self.assertAllEqual(expected_output, output_dataset)

  def test_distribution_strategy_output_with_adapt(self, distribution):
    vocab_data = [[
        "earth", "earth", "earth", "earth", "wind", "wind", "wind", "and",
        "and", "fire"
    ]]
    vocab_dataset = dataset_ops.Dataset.from_tensors(vocab_data)
    input_array = np.array([["earth", "wind", "and", "fire"],
                            ["fire", "and", "earth", "michigan"]])
    input_dataset = dataset_ops.Dataset.from_tensor_slices(input_array).batch(
        2, drop_remainder=True)

    expected_output = [[2, 3, 4, 5], [5, 4, 2, 1]]

    config.set_soft_device_placement(True)

    with distribution.scope():
      input_data = keras.Input(shape=(None,), dtype=dtypes.string)
      layer = text_vectorization.TextVectorization(
          max_tokens=None,
          standardize=None,
          split=None,
          output_mode=text_vectorization.INT)
      layer.adapt(vocab_dataset)
      int_data = layer(input_data)
      model = keras.Model(inputs=input_data, outputs=int_data)

    output_dataset = model.predict(input_dataset)
    self.assertAllEqual(expected_output, output_dataset)
if __name__ == "__main__":
  test.main()

tensorflow.python.data.ops.dataset_ops.Dataset.from_tensors tensorflow.python.keras.Input tensorflow.python.framework.config.set_soft_device_placement tensorflow.python.data.ops.dataset_ops.Dataset.from_tensor_slices tensorflow.python.keras.Model tensorflow.python.keras.layers.preprocessing.text_vectorization.TextVectorization tensorflow.python.platform.test.main tensorflow.python.framework.test_combinations.combine numpy.array
[(33, 'tensorflow.python.framework.test_combinations.combine', 'combinations.combine', ([], {'distribution': 'all_strategies', 'mode': "['eager']"}), True, 'from tensorflow.python.framework import test_combinations as combinations\n'), (92, 'tensorflow.python.platform.test.main', 'test.main', ([], {}), False, 'from tensorflow.python.platform import test\n'), (40, 'numpy.array', 'np.array', (["[['earth', 'wind', 'and', 'fire'], ['fire', 'and', 'earth', 'michigan']]"], {}), True, 'import numpy as np\n'), (47, 'tensorflow.python.framework.config.set_soft_device_placement', 'config.set_soft_device_placement', (['(True)'], {}), False, 'from tensorflow.python.framework import config\n'), (68, 'tensorflow.python.data.ops.dataset_ops.Dataset.from_tensors', 'dataset_ops.Dataset.from_tensors', (['vocab_data'], {}), False, 'from tensorflow.python.data.ops import dataset_ops\n'), (69, 'numpy.array', 'np.array', (["[['earth', 'wind', 'and', 'fire'], ['fire', 'and', 'earth', 'michigan']]"], {}), True, 'import numpy as np\n'), (76, 'tensorflow.python.framework.config.set_soft_device_placement', 'config.set_soft_device_placement', (['(True)'], {}), False, 'from tensorflow.python.framework import config\n'), (50, 'tensorflow.python.keras.Input', 'keras.Input', ([], {'shape': '(None,)', 'dtype': 'dtypes.string'}), False, 'from tensorflow.python import keras\n'), (51, 'tensorflow.python.keras.layers.preprocessing.text_vectorization.TextVectorization', 'text_vectorization.TextVectorization', ([], {'max_tokens': 'None', 'standardize': 'None', 'split': 'None', 'output_mode': 'text_vectorization.INT'}), False, 'from tensorflow.python.keras.layers.preprocessing import text_vectorization\n'), (58, 'tensorflow.python.keras.Model', 'keras.Model', ([], {'inputs': 'input_data', 'outputs': 'int_data'}), False, 'from tensorflow.python import keras\n'), (79, 'tensorflow.python.keras.Input', 'keras.Input', ([], {'shape': '(None,)', 'dtype': 'dtypes.string'}), False, 'from tensorflow.python import keras\n'), (80, 'tensorflow.python.keras.layers.preprocessing.text_vectorization.TextVectorization', 'text_vectorization.TextVectorization', ([], {'max_tokens': 'None', 'standardize': 'None', 'split': 'None', 'output_mode': 'text_vectorization.INT'}), False, 'from tensorflow.python.keras.layers.preprocessing import text_vectorization\n'), (87, 'tensorflow.python.keras.Model', 'keras.Model', ([], {'inputs': 'input_data', 'outputs': 'int_data'}), False, 'from tensorflow.python import keras\n'), (42, 'tensorflow.python.data.ops.dataset_ops.Dataset.from_tensor_slices', 'dataset_ops.Dataset.from_tensor_slices', (['input_array'], {}), False, 'from tensorflow.python.data.ops import dataset_ops\n'), (71, 'tensorflow.python.data.ops.dataset_ops.Dataset.from_tensor_slices', 'dataset_ops.Dataset.from_tensor_slices', (['input_array'], {}), False, 'from tensorflow.python.data.ops import dataset_ops\n')]