Unverified Commit 20cc2190 authored by pyoung2778's avatar pyoung2778 Committed by GitHub
Browse files

Check in seq_flow_lite (#10750)

parent fdecf385
...@@ -16,14 +16,11 @@ http_archive( ...@@ -16,14 +16,11 @@ http_archive(
http_archive( http_archive(
name = "org_tensorflow", name = "org_tensorflow",
sha256 = "40d3203ab5f246d83bae328288a24209a2b85794f1b3e2cd0329458d8e7c1985", strip_prefix = "tensorflow-2.9.1",
strip_prefix = "tensorflow-2.6.0", sha256 = "9f2dac244e5af6c6a13a7dad6481e390174ac989931942098e7a4373f1bccfc2",
urls = [ urls = ["https://github.com/tensorflow/tensorflow/archive/v2.9.1.zip"],
"https://github.com/tensorflow/tensorflow/archive/v2.6.0.zip",
],
) )
http_archive( http_archive(
name = "org_tflite_support", name = "org_tflite_support",
strip_prefix = "tflite-support-0861599711ef31de58f62ed3ff6bbcc1e4817ef6", strip_prefix = "tflite-support-0861599711ef31de58f62ed3ff6bbcc1e4817ef6",
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""A tool to export TFLite model.""" """A tool to export TFLite model."""
import importlib import importlib
...@@ -22,7 +21,7 @@ import os ...@@ -22,7 +21,7 @@ import os
from absl import app from absl import app
from absl import flags from absl import flags
import tensorflow.compat.v1 as tf import tensorflow.compat.v1 as tf
import tensorflow_text as tftext
from layers import base_layers # import seq_flow_lite module from layers import base_layers # import seq_flow_lite module
from layers import projection_layers # import seq_flow_lite module from layers import projection_layers # import seq_flow_lite module
from utils import tflite_utils # import seq_flow_lite module from utils import tflite_utils # import seq_flow_lite module
...@@ -48,25 +47,33 @@ def main(_): ...@@ -48,25 +47,33 @@ def main(_):
with tf.Graph().as_default() as graph: with tf.Graph().as_default() as graph:
with tf.Session(graph=graph) as session: with tf.Session(graph=graph) as session:
text = tf.placeholder(tf.string, shape=[1], name="Input") text = tf.placeholder(tf.string, shape=[1], name="Input")
inputs = [text]
if "pqrnn" in runner_config["name"]:
prxlayer = projection_layers.ProjectionLayer(model_config, prxlayer = projection_layers.ProjectionLayer(model_config,
base_layers.TFLITE) base_layers.TFLITE)
encoder = model.Encoder(model_config, base_layers.TFLITE) encoder = model.Encoder(model_config, base_layers.TFLITE)
projection, seq_lengh = prxlayer(text) projection, seq_length = prxlayer(text)
logits = encoder(projection, seq_lengh) logits = encoder(projection, seq_length)
else:
byte_int = tftext.ByteSplitter().split(text)
token_ids = tf.cast(byte_int, tf.int32).to_tensor()
token_ids = tf.reshape(token_ids, [1, -1])
token_ids += 3
encoder = model.Encoder(model_config, base_layers.TFLITE)
logits = encoder(token_ids, None)
if FLAGS.output == "logits": if FLAGS.output == "logits":
outputs = logits outputs = [logits]
elif FLAGS.output == "sigmoid": elif FLAGS.output == "sigmoid":
outputs = tf.math.sigmoid(logits) outputs = [tf.math.sigmoid(logits)]
else: else:
assert FLAGS.output == "softmax", "Unexpected output" assert FLAGS.output == "softmax", "Unexpected output"
outputs = tf.nn.softmax(logits) outputs = [tf.nn.softmax(logits)]
session.run(tf.global_variables_initializer()) session.run(tf.global_variables_initializer())
session.run(tf.local_variables_initializer()) session.run(tf.local_variables_initializer())
saver = tf.train.Saver() saver = tf.train.Saver()
saver.restore(session, tf.train.latest_checkpoint(FLAGS.output_dir)) saver.restore(session, tf.train.latest_checkpoint(FLAGS.output_dir))
tflite_fb = tflite_utils.generate_tflite(session, graph, [text], tflite_fb = tflite_utils.generate_tflite(session, graph, inputs, outputs)
[outputs])
output_file_name = os.path.join(FLAGS.output_dir, "tflite.fb") output_file_name = os.path.join(FLAGS.output_dir, "tflite.fb")
with tf.gfile.Open(output_file_name, "wb") as f: with tf.gfile.Open(output_file_name, "wb") as f:
f.write(tflite_fb) f.write(tflite_fb)
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Methods related to input datasets and readers.""" """Methods related to input datasets and readers."""
import functools import functools
...@@ -21,6 +20,7 @@ import sys ...@@ -21,6 +20,7 @@ import sys
from absl import logging from absl import logging
import tensorflow as tf import tensorflow as tf
from tensorflow import estimator as tf_estimator
import tensorflow_datasets as tfds import tensorflow_datasets as tfds
import tensorflow_text as tftext import tensorflow_text as tftext
...@@ -83,13 +83,13 @@ def create_input_fn(runner_config, mode, drop_remainder): ...@@ -83,13 +83,13 @@ def create_input_fn(runner_config, mode, drop_remainder):
def _input_fn(params): def _input_fn(params):
"""Method to be used for reading the data.""" """Method to be used for reading the data."""
assert mode != tf.estimator.ModeKeys.PREDICT assert mode != tf_estimator.ModeKeys.PREDICT
split = "train" if mode == tf.estimator.ModeKeys.TRAIN else "test" split = "train" if mode == tf_estimator.ModeKeys.TRAIN else "test"
ds = tfds.load(runner_config["dataset"], split=split) ds = tfds.load(runner_config["dataset"], split=split)
ds = ds.batch(params["batch_size"], drop_remainder=drop_remainder) ds = ds.batch(params["batch_size"], drop_remainder=drop_remainder)
ds = ds.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) ds = ds.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
ds = ds.shuffle(buffer_size=100) ds = ds.shuffle(buffer_size=100)
ds = ds.repeat(count=1 if mode == tf.estimator.ModeKeys.EVAL else None) ds = ds.repeat(count=1 if mode == tf_estimator.ModeKeys.EVAL else None)
ds = ds.map( ds = ds.map(
functools.partial(_post_processor, batch_size=params["batch_size"]), functools.partial(_post_processor, batch_size=params["batch_size"]),
num_parallel_calls=tf.data.experimental.AUTOTUNE, num_parallel_calls=tf.data.experimental.AUTOTUNE,
......
...@@ -82,12 +82,12 @@ py_strict_library( ...@@ -82,12 +82,12 @@ py_strict_library(
srcs = ["misc_layers.py"], srcs = ["misc_layers.py"],
srcs_version = "PY3", srcs_version = "PY3",
deps = [ deps = [
# package tensorflow
":embedding_layers", ":embedding_layers",
# package tensorflow
"//layers:base_layers", # sequence projection "//layers:base_layers", # sequence projection
"//layers:conv_layers", "//layers:conv_layers", # sequence projection
"//layers:dense_layers", # sequence projection "//layers:dense_layers", # sequence projection
"//layers:normalization_layers", "//layers:normalization_layers", # sequence projection
"//layers:quantization_layers", # sequence projection "//layers:quantization_layers", # sequence projection
], ],
) )
...@@ -112,8 +112,8 @@ py_strict_library( ...@@ -112,8 +112,8 @@ py_strict_library(
srcs_version = "PY3", srcs_version = "PY3",
deps = [ deps = [
# package tensorflow # package tensorflow
"//layers:base_layers", "//layers:base_layers", # sequence projection
"//layers:quantization_layers", "//layers:quantization_layers", # sequence projection
], ],
) )
...@@ -124,11 +124,11 @@ py_strict_library( ...@@ -124,11 +124,11 @@ py_strict_library(
deps = [ deps = [
":embedding_layers", ":embedding_layers",
# package tensorflow # package tensorflow
"//layers:base_layers", "//layers:base_layers", # sequence projection
"//layers:dense_layers", "//layers:dense_layers", # sequence projection
"//layers:normalization_layers", "//layers:normalization_layers", # sequence projection
"//layers:quantization_layers", "//layers:quantization_layers", # sequence projection
"//tf_ops:tf_custom_ops", # "//tf_ops:tf_custom_ops" # sequence projection
"//tf_ops:tf_custom_ops_py", "//tf_ops:tf_custom_ops_py", # sequence projection
], ],
) )
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Base layer for building models trained with quantization.""" """Base layer for building models trained with quantization."""
import tensorflow as tf import tensorflow as tf
...@@ -57,7 +56,7 @@ class BaseLayer(tf.keras.layers.Layer): ...@@ -57,7 +56,7 @@ class BaseLayer(tf.keras.layers.Layer):
def add_weight_wrapper(self, shape): def add_weight_wrapper(self, shape):
"""Return a weight variable for the given shape.""" """Return a weight variable for the given shape."""
if self.parameters.initializer is not None: if self.parameters.initializer is not None:
initializer = self.parameters.initializer initializer = clone_initializer(self.parameters.initializer)
else: else:
initializer = tf.keras.initializers.GlorotUniform() initializer = tf.keras.initializers.GlorotUniform()
weight = self.add_weight( weight = self.add_weight(
...@@ -136,3 +135,9 @@ class BaseLayer(tf.keras.layers.Layer): ...@@ -136,3 +135,9 @@ class BaseLayer(tf.keras.layers.Layer):
maxval=(1.0 - zero_probability), maxval=(1.0 - zero_probability),
dtype=tensor.dtype) dtype=tensor.dtype)
return tf.math.ceil(rnd) return tf.math.ceil(rnd)
def clone_initializer(initializer):
if isinstance(initializer, tf.keras.initializers.Initializer):
return initializer.__class__.from_config(initializer.get_config())
return initializer
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Base layer for convolution.""" """Base layer for convolution."""
import tensorflow as tf import tensorflow as tf
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Basic dense layers.""" """Basic dense layers."""
import tensorflow as tf import tensorflow as tf
...@@ -30,6 +29,7 @@ class BaseQDense(base_layers.BaseLayer): ...@@ -30,6 +29,7 @@ class BaseQDense(base_layers.BaseLayer):
bias=True, bias=True,
rank=2, rank=2,
normalize=True, normalize=True,
quantize_output=True,
**kwargs): **kwargs):
self.units = units self.units = units
self.rank = rank self.rank = rank
...@@ -37,6 +37,8 @@ class BaseQDense(base_layers.BaseLayer): ...@@ -37,6 +37,8 @@ class BaseQDense(base_layers.BaseLayer):
self.activation = activation self.activation = activation
self.bias = bias self.bias = bias
self.normalize = normalize self.normalize = normalize
self.quantize_output = quantize_output
if quantize_output:
self.qoutput = quantization_layers.ActivationQuantization(**kwargs) self.qoutput = quantization_layers.ActivationQuantization(**kwargs)
self._create_normalizer(**kwargs) self._create_normalizer(**kwargs)
super(BaseQDense, self).__init__(**kwargs) super(BaseQDense, self).__init__(**kwargs)
...@@ -62,7 +64,10 @@ class BaseQDense(base_layers.BaseLayer): ...@@ -62,7 +64,10 @@ class BaseQDense(base_layers.BaseLayer):
outputs = normalize_method(outputs) outputs = normalize_method(outputs)
if self.activation: if self.activation:
outputs = self.activation(outputs) outputs = self.activation(outputs)
if self.quantize_output:
return self.qoutput(outputs) return self.qoutput(outputs)
else:
return outputs
def _dense_r34(self, inputs, normalize_method): def _dense_r34(self, inputs, normalize_method):
bsz = self.get_batch_dimension(inputs) bsz = self.get_batch_dimension(inputs)
......
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
"""Layers for embedding.""" """Layers for embedding."""
import tensorflow as tf import tensorflow as tf
from layers import base_layers from layers import base_layers # import seq_flow_lite module
from layers import quantization_layers from layers import quantization_layers # import seq_flow_lite module
class EmbeddingLayer(base_layers.BaseLayer): class EmbeddingLayer(base_layers.BaseLayer):
......
...@@ -12,15 +12,14 @@ ...@@ -12,15 +12,14 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Layers for embedding.""" """Layers for embedding."""
import math import math
import tensorflow as tf import tensorflow as tf
from layers import base_layers # import seq_flow_lite module from layers import base_layers # import seq_flow_lite module
from layers import conv_layers from layers import conv_layers # import seq_flow_lite module
from layers import dense_layers # import seq_flow_lite module from layers import dense_layers # import seq_flow_lite module
from layers import embedding_layers from layers import embedding_layers # import seq_flow_lite module
from layers import quantization_layers # import seq_flow_lite module from layers import quantization_layers # import seq_flow_lite module
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Layers for normalization.""" """Layers for normalization."""
import tensorflow as tf import tensorflow as tf
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Layers for QRNN.""" """Layers for QRNN."""
import tensorflow as tf import tensorflow as tf
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Layers for quantization.""" """Layers for quantization."""
import tensorflow as tf import tensorflow as tf
......
...@@ -16,12 +16,12 @@ ...@@ -16,12 +16,12 @@
# pylint: disable=arguments-renamed # pylint: disable=arguments-renamed
import tensorflow as tf import tensorflow as tf
from layers import base_layers from layers import base_layers # import seq_flow_lite module
from layers import dense_layers from layers import dense_layers # import seq_flow_lite module
from layers import embedding_layers from layers import embedding_layers # import seq_flow_lite module
from layers import normalization_layers from layers import normalization_layers # import seq_flow_lite module
from layers import quantization_layers from layers import quantization_layers # import seq_flow_lite module
from tf_ops import tf_custom_ops_py from tf_ops import tf_custom_ops_py # import seq_flow_lite module
class SelfAttention(base_layers.BaseLayer): class SelfAttention(base_layers.BaseLayer):
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Metric functions.""" """Metric functions."""
import tensorflow.compat.v1 as tf import tensorflow.compat.v1 as tf
......
...@@ -45,13 +45,13 @@ py_library( ...@@ -45,13 +45,13 @@ py_library(
srcs_version = "PY3", srcs_version = "PY3",
deps = [ deps = [
# package tensorflow # package tensorflow
"//layers:base_layers", "//layers:base_layers", # sequence projection
"//layers:dense_layers", "//layers:dense_layers", # sequence projection
"//layers:embedding_layers", "//layers:embedding_layers", # sequence projection
"//layers:misc_layers", "//layers:misc_layers", # sequence projection
"//layers:qrnn_layers", "//layers:qrnn_layers", # sequence projection
# //tf_ops:tf_custom_ops", # "//tf_ops:tf_custom_ops" # sequence projection
"//tf_ops:tf_custom_ops_py", "//tf_ops:tf_custom_ops_py", # sequence projection
], ],
) )
...@@ -62,13 +62,13 @@ py_library( ...@@ -62,13 +62,13 @@ py_library(
deps = [ deps = [
":transformer_encoder", ":transformer_encoder",
# package tensorflow # package tensorflow
"//layers:base_layers", "//layers:base_layers", # sequence projection
"//layers:embedding_layers", "//layers:embedding_layers", # sequence projection
"//layers:misc_layers", "//layers:misc_layers", # sequence projection
"//layers:normalization_layers", "//layers:normalization_layers", # sequence projection
"//layers:quantization_layers", "//layers:quantization_layers", # sequence projection
# "//tf_ops:tf_custom_ops", # "//tf_ops:tf_custom_ops" # sequence projection
"//tf_ops:tf_custom_ops_py", "//tf_ops:tf_custom_ops_py", # sequence projection
], ],
) )
...@@ -79,11 +79,11 @@ py_library( ...@@ -79,11 +79,11 @@ py_library(
deps = [ deps = [
# package absl/logging # package absl/logging
# package tensorflow # package tensorflow
"//layers:base_layers", "//layers:base_layers", # sequence projection
"//layers:embedding_layers", "//layers:embedding_layers", # sequence projection
"//layers:transformer_layers", "//layers:transformer_layers", # sequence projection
# "//tf_ops:tf_custom_ops", # "//tf_ops:tf_custom_ops" # sequence projection
"//tf_ops:tf_custom_ops_py", "//tf_ops:tf_custom_ops_py", # sequence projection
], ],
) )
...@@ -93,13 +93,13 @@ py_library( ...@@ -93,13 +93,13 @@ py_library(
srcs_version = "PY3", srcs_version = "PY3",
deps = [ deps = [
# package absl/logging # package absl/logging
# package tensor2tensor/utils:beam_search
# package tensorflow # package tensorflow
# tensor2tensor/utils:beam_search", "//layers:base_layers", # sequence projection
"//layers:base_layers", "//layers:embedding_layers", # sequence projection
"//layers:embedding_layers", "//layers:misc_layers", # sequence projection
"//layers:misc_layers", "//layers:transformer_layers", # sequence projection
"//layers:transformer_layers", # "//tf_ops:tf_custom_ops" # sequence projection
"//tf_ops:tf_custom_ops", "//tf_ops:tf_custom_ops_py", # sequence projection
"//tf_ops:tf_custom_ops_py",
], ],
) )
...@@ -33,11 +33,11 @@ Sample model params: ...@@ -33,11 +33,11 @@ Sample model params:
from absl import logging from absl import logging
import tensorflow as tf import tensorflow as tf
from layers import base_layers from layers import base_layers # import seq_flow_lite module
from layers import dense_layers from layers import dense_layers # import seq_flow_lite module
from layers import embedding_layers from layers import embedding_layers # import seq_flow_lite module
from layers import misc_layers from layers import misc_layers # import seq_flow_lite module
from layers import qrnn_layers from layers import qrnn_layers # import seq_flow_lite module
class Encoder(tf.keras.layers.Layer): class Encoder(tf.keras.layers.Layer):
......
...@@ -16,13 +16,13 @@ ...@@ -16,13 +16,13 @@
from absl import logging from absl import logging
import tensorflow as tf import tensorflow as tf
from layers import base_layers from layers import base_layers # import seq_flow_lite module
from layers import dense_layers from layers import dense_layers # import seq_flow_lite module
from layers import embedding_layers from layers import embedding_layers # import seq_flow_lite module
from layers import misc_layers from layers import misc_layers # import seq_flow_lite module
from layers import normalization_layers from layers import normalization_layers # import seq_flow_lite module
from layers import quantization_layers from layers import quantization_layers # import seq_flow_lite module
from models import transformer_encoder from models import transformer_encoder # import seq_flow_lite module
class Encoder(tf.keras.layers.Layer): class Encoder(tf.keras.layers.Layer):
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Implementation of pQRNN model.""" """Implementation of pQRNN model."""
from absl import logging from absl import logging
...@@ -43,7 +42,7 @@ class Encoder(tf.keras.layers.Layer): ...@@ -43,7 +42,7 @@ class Encoder(tf.keras.layers.Layer):
_get_params("qrnn_kernel_width", 3) _get_params("qrnn_kernel_width", 3)
_get_params("qrnn_zoneout_probability") _get_params("qrnn_zoneout_probability")
_get_params("number_qrnn_layers") _get_params("number_qrnn_layers")
_get_params("labels") _get_params("labels", [])
_get_params("regularizer_scale") _get_params("regularizer_scale")
_get_params("quantize") _get_params("quantize")
...@@ -66,6 +65,7 @@ class Encoder(tf.keras.layers.Layer): ...@@ -66,6 +65,7 @@ class Encoder(tf.keras.layers.Layer):
self.attention_pool = misc_layers.AttentionPooling( self.attention_pool = misc_layers.AttentionPooling(
parameters=self.parameters) parameters=self.parameters)
if self.num_classes:
self.final_fc = dense_layers.BaseQDense( self.final_fc = dense_layers.BaseQDense(
units=self.num_classes, units=self.num_classes,
rank=2, rank=2,
...@@ -82,7 +82,11 @@ class Encoder(tf.keras.layers.Layer): ...@@ -82,7 +82,11 @@ class Encoder(tf.keras.layers.Layer):
bottleneck = self.bottleneck_layer(projection, maskr3, inverse_normalizer) bottleneck = self.bottleneck_layer(projection, maskr3, inverse_normalizer)
outputs = self.qrnn_stack(bottleneck, maskr3, inverse_normalizer) outputs = self.qrnn_stack(bottleneck, maskr3, inverse_normalizer)
pre_logits = self.attention_pool(outputs, maskr3, inverse_normalizer) pre_logits = self.attention_pool(outputs, maskr3, inverse_normalizer)
if self.num_classes:
return self.final_fc(pre_logits) return self.final_fc(pre_logits)
else:
return pre_logits
class Model(Encoder): class Model(Encoder):
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Implementation of PRADO model.""" """Implementation of PRADO model."""
import copy import copy
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# Lint as: python3
"""Tests for seq_flow_lite.sgnn.""" """Tests for seq_flow_lite.sgnn."""
import tensorflow as tf import tensorflow as tf
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment