<commit_before># 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.
# ==============================================================================
"""Helper class to write metadata into TFLite models."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import abc
import os

import tensorflow as tf
# pylint: disable=g-direct-tensorflow-import
from tflite_support import schema_py_generated as _schema_fb
from tflite_support import metadata as _metadata
# pylint: enable=g-direct-tensorflow-import


class MetadataWriter(abc.ABC):
  """Writes the metadata and associated file into a TFLite model."""

  def __init__(self, model_file, export_directory, associated_files):
    """Constructs the MetadataWriter.

    Args:
      model_file: the path to the model to be populated.
      export_directory: path to the directory where the model and json file will
        be exported to.
      associated_files: path to the associated files to be populated.
    """
    self.model_file = model_file
    self.export_directory = export_directory
    self.associated_files = associated_files
    self.metadata_buf = None

  def populate(self, export_metadata_json_file=False):
    """Creates metadata and then populates it into a TFLite model."""
    self._create_metadata()
    self._populate_metadata(export_metadata_json_file)

  @abc.abstractmethod
  def _create_metadata(self):
    pass

  def _get_subgraph(self):
    """Gets the input / output tensor names into lists."""

    with open(self.model_file, "rb") as f:
      model_buf = f.read()

    model = _schema_fb.Model.GetRootAsModel(model_buf, 0)
    # There should be exactly one SubGraph in the model.
    if model.SubgraphsLength() != 1:
      raise ValueError(
          "The model should have exactly one subgraph, but found {0}.".format(
              model.SubgraphsLength()))

    return model.Subgraphs(0)

  def _get_input_tensor_names(self):
    """Gets the input tensor names into a list."""
    input_names = []
    subgraph = self._get_subgraph()<commit_msg>Loosen the constraint for number_subgraph == 1 in metadata writer<commit_after># 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.
# ==============================================================================
"""Helper class to write metadata into TFLite models."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import abc
import os

import tensorflow as tf
# pylint: disable=g-direct-tensorflow-import
from tflite_support import schema_py_generated as _schema_fb
from tflite_support import metadata as _metadata
# pylint: enable=g-direct-tensorflow-import


class MetadataWriter(abc.ABC):
  """Writes the metadata and associated file into a TFLite model."""

  def __init__(self, model_file, export_directory, associated_files):
    """Constructs the MetadataWriter.

    Args:
      model_file: the path to the model to be populated.
      export_directory: path to the directory where the model and json file will
        be exported to.
      associated_files: path to the associated files to be populated.
    """
    self.model_file = model_file
    self.export_directory = export_directory
    self.associated_files = associated_files
    self.metadata_buf = None

  def populate(self, export_metadata_json_file=False):
    """Creates metadata and then populates it into a TFLite model."""
    self._create_metadata()
    self._populate_metadata(export_metadata_json_file)

  @abc.abstractmethod
  def _create_metadata(self):
    pass

  def _get_subgraph(self):
    """Gets the input / output tensor names into lists."""

    with open(self.model_file, "rb") as f:
      model_buf = f.read()

    model = _schema_fb.Model.GetRootAsModel(model_buf, 0)

    # Use the first subgraph as default. TFLite Interpreter doesn't support
    # multiple subgraphs yet, but models with mini-benchmark may have multiple
    # subgraphs for acceleration evaluation purpose.
    return model.Subgraphs(0)

  def _get_input_tensor_names(self):
    """Gets the input tensor names into a list."""
    input_names = []
    subgraph = self._get_subgraph()
{'fraction_non_alphanumeric': 0.10016945961212578, 'fraction_numerical': 0.004330634532103182, 'mean_word_length': 4.162293488824101, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6724095', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1400, 'n_words': 551}