Skip to content

Commit

Permalink
Checkin seq_flow_lite (tensorflow#10886)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyoung2778 authored Jan 7, 2023
1 parent 73ce373 commit c533506
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 75 deletions.
6 changes: 3 additions & 3 deletions research/seq_flow_lite/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ http_archive(

http_archive(
name = "org_tensorflow",
strip_prefix = "tensorflow-2.10.0",
sha256 = "d79a95ede8305f14a10dd0409a1e5a228849039c19ccfb90dfe8367295fd04e0",
urls = ["https://github.com/tensorflow/tensorflow/archive/v2.10.0.zip"],
strip_prefix = "tensorflow-2.11.0",
sha256 = "e52cda3bae45f0ae0fccd4055e9fa29892b414f70e2df94df9a3a10319c75fff",
urls = ["https://github.com/tensorflow/tensorflow/archive/v2.11.0.zip"],
)

http_archive(
Expand Down
28 changes: 4 additions & 24 deletions research/seq_flow_lite/demo/colab/emotion_colab.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"id": "4aGnloeD1Mfo"
},
"source": [
"### Install Tensorflow 2.10.0"
"### Install Tensorflow 2.11.0"
]
},
{
Expand All @@ -81,7 +81,7 @@
"id": "MqP5qpAR1W4f"
},
"source": [
"The seq_flow_lite library has been written with the assumption that tensorflow 2.10.0 will be used. It may be necessary to restart the runtime after installing the correct version of Tensorflow."
"The seq_flow_lite library has been written with the assumption that tensorflow 2.11.0 will be used. It may be necessary to restart the runtime after installing the correct version of Tensorflow."
]
},
{
Expand All @@ -92,27 +92,7 @@
},
"outputs": [],
"source": [
"!pip install tensorflow==2.10.0"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PXaKS_JBbyX1"
},
"source": [
"Update CuDNN. The version installed on the Colab machines does not play well with Tensorflow 2.10.0."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ErZoDw_9bwSG"
},
"outputs": [],
"source": [
"!apt install --allow-change-held-packages libcudnn8=8.1.0.77-1+cuda11.2"
"!pip install tensorflow==2.11.0"
]
},
{
Expand Down Expand Up @@ -174,7 +154,7 @@
"!curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -\n",
"!echo \"deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8\" | sudo tee /etc/apt/sources.list.d/bazel.list\n",
"!sudo apt update\n",
"!sudo apt install bazel"
"!sudo apt install bazel=5.4.0"
]
},
{
Expand Down
4 changes: 1 addition & 3 deletions research/seq_flow_lite/demo/prado/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

licenses(["notice"])

package(
default_visibility = ["//:friends"], # sequence projection
)
package(default_visibility = ["//:friends"]) # sequence projection

cc_binary(
name = "prado_tflite_example",
Expand Down
5 changes: 1 addition & 4 deletions research/seq_flow_lite/layers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ py_strict_library = py_library

licenses(["notice"])

package(
default_visibility = ["//:friends"], # sequence projection
)
package(default_visibility = ["//:friends"]) # sequence projection

py_strict_library(
name = "base_layers",
Expand Down Expand Up @@ -87,7 +85,6 @@ py_strict_library(
"//layers:base_layers", # sequence projection
"//layers:conv_layers", # sequence projection
"//layers:dense_layers", # sequence projection
"//layers:normalization_layers", # sequence projection
"//layers:quantization_layers", # sequence projection
],
)
Expand Down
20 changes: 14 additions & 6 deletions research/seq_flow_lite/layers/conv_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
# ==============================================================================
"""Base layer for convolution."""
import copy
import tensorflow as tf

from layers import base_layers # import seq_flow_lite module
Expand All @@ -32,6 +33,7 @@ def __init__(self,
activation=tf.keras.layers.ReLU(),
bias=True,
rank=4,
normalization_fn=None,
**kwargs):
self.out_filters = filters
assert rank >= 3 and rank <= 4
Expand All @@ -43,7 +45,7 @@ def __init__(self,
self.bias = bias
self.padding = padding
self.qoutput = quantization_layers.ActivationQuantization(**kwargs)
self._create_normalizer(**kwargs)
self._create_normalizer(normalization_fn=normalization_fn, **kwargs)
super(EncoderQConvolution, self).__init__(**kwargs)

def _unpack(self, value):
Expand All @@ -63,8 +65,11 @@ def build(self, input_shapes):
if self.bias:
self.b = self.add_bias(shape=[self.out_filters])

def _create_normalizer(self, **kwargs):
self.normalization = normalization_layers.BatchNormalization(**kwargs)
def _create_normalizer(self, normalization_fn, **kwargs):
if normalization_fn is None:
self.normalization = normalization_layers.BatchNormalization(**kwargs)
else:
self.normalization = copy.deepcopy(normalization_fn)

def _conv_r4(self, inputs, normalize_method):
outputs = tf.nn.conv2d(
Expand Down Expand Up @@ -105,9 +110,12 @@ def quantize_using_output_range(self, tensor):
class EncoderQConvolutionVarLen(EncoderQConvolution):
"""Convolution on variable length sequence."""

def _create_normalizer(self, **kwargs):
self.normalization = normalization_layers.VarLenBatchNormalization(
rank=4, **kwargs)
def _create_normalizer(self, normalization_fn, **kwargs):
if normalization_fn is None:
self.normalization = normalization_layers.VarLenBatchNormalization(
rank=4, **kwargs)
else:
self.normalization = copy.deepcopy(normalization_fn)

def call(self, inputs, mask, inverse_normalizer):

Expand Down
20 changes: 14 additions & 6 deletions research/seq_flow_lite/layers/dense_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
# ==============================================================================
"""Basic dense layers."""
import copy
import tensorflow as tf

from layers import base_layers # import seq_flow_lite module
Expand All @@ -30,6 +31,7 @@ def __init__(self,
rank=2,
normalize=True,
quantize_output=True,
normalization_fn=None,
**kwargs):
self.units = units
self.rank = rank
Expand All @@ -40,7 +42,7 @@ def __init__(self,
self.quantize_output = quantize_output
if quantize_output:
self.qoutput = quantization_layers.ActivationQuantization(**kwargs)
self._create_normalizer(**kwargs)
self._create_normalizer(normalization_fn=normalization_fn, **kwargs)
super(BaseQDense, self).__init__(**kwargs)

def build(self, input_shapes):
Expand All @@ -53,8 +55,11 @@ def build(self, input_shapes):
if self.bias:
self.b = self.add_bias(shape=[self.units])

def _create_normalizer(self, **kwargs):
self.normalization = normalization_layers.BatchNormalization(**kwargs)
def _create_normalizer(self, normalization_fn, **kwargs):
if normalization_fn is None:
self.normalization = normalization_layers.BatchNormalization(**kwargs)
else:
self.normalization = copy.deepcopy(normalization_fn)

def _dense_r2(self, inputs, normalize_method):
outputs = tf.matmul(inputs, self.quantize_parameter(self.w))
Expand Down Expand Up @@ -99,9 +104,12 @@ def quantize_using_output_range(self, tensor):
class BaseQDenseVarLen(BaseQDense):
"""Dense on variable length sequence."""

def _create_normalizer(self, **kwargs):
self.normalization = normalization_layers.VarLenBatchNormalization(
rank=2, **kwargs)
def _create_normalizer(self, normalization_fn, **kwargs):
if normalization_fn is None:
self.normalization = normalization_layers.VarLenBatchNormalization(
rank=2, **kwargs)
else:
self.normalization = copy.deepcopy(normalization_fn)

def call(self, inputs, mask, inverse_normalizer=None):
if inverse_normalizer is None:
Expand Down
5 changes: 3 additions & 2 deletions research/seq_flow_lite/layers/misc_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class AttentionPooling(base_layers.BaseLayer):
"""A basic attention pooling layer."""

def __init__(self, scalar=True, **kwargs):
def __init__(self, scalar=True, normalize=True, **kwargs):
self.scalar = scalar
# Attention logits should not have activation post linear layer so it can
# be positive or negative. This would enable the attention distribution to
Expand All @@ -36,7 +36,8 @@ def __init__(self, scalar=True, **kwargs):
# emphasized for making classification decision, all other outputs have
# a non zero probability of influencing the class. This seems to result
# in better backprop.
self.attention = dense_layers.BaseQDenseVarLen(units=1, rank=3, **kwargs)
self.attention = dense_layers.BaseQDenseVarLen(
units=1, rank=3, normalize=normalize, **kwargs)
self.qactivation = quantization_layers.ActivationQuantization(**kwargs)
super(AttentionPooling, self).__init__(**kwargs)

Expand Down
10 changes: 10 additions & 0 deletions research/seq_flow_lite/layers/qrnn_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def __init__(self,
forward=True,
pooling=QUASI_RNN_POOLING_FO,
output_quantized=True,
normalization_fn=None,
**kwargs):
self.forward = forward
self.kwidth = kwidth
Expand All @@ -241,6 +242,7 @@ def __init__(self,
rank=3,
padding="VALID",
activation=None,
normalization_fn=normalization_fn,
**kwargs))
padding = [kwidth - 1, 0] if forward else [0, kwidth - 1]
self.zero_pad = tf.keras.layers.ZeroPadding1D(padding=padding)
Expand Down Expand Up @@ -304,6 +306,7 @@ def __init__(self,
ksize=kwidth,
rank=3,
padding="SAME",
normalization_fn=None,
**kwargs))
self.post_conv_layers.append(
dense_layers.BaseQDense(
Expand Down Expand Up @@ -332,6 +335,7 @@ def __init__(self,
zoneout_probability=0.0,
pooling=QUASI_RNN_POOLING_FO,
bottleneck_size=None,
normalization_fn=None,
**kwargs):
self.pooling = pooling
if bottleneck_size is None:
Expand All @@ -342,6 +346,7 @@ def __init__(self,
output_quantized=False,
zoneout_probability=zoneout_probability,
pooling=pooling,
normalization_fn=normalization_fn,
**kwargs)
self.backward = QRNNUnidirectional(
kwidth=kwidth,
Expand All @@ -350,8 +355,11 @@ def __init__(self,
output_quantized=False,
zoneout_probability=zoneout_probability,
pooling=pooling,
normalization_fn=normalization_fn,
**kwargs)
else:
assert normalization_fn is None, (
"normalization_fn will not take an effect")
self.forward = QRNNUnidirectionalWithBottleneck(
kwidth=kwidth,
state_size=state_size,
Expand Down Expand Up @@ -400,6 +408,7 @@ def __init__(self,
layerwise_decaying_zoneout=True,
pooling=QUASI_RNN_POOLING_FO,
bottleneck_size=None,
normalization_fn=None,
**kwargs):
self.layers = []
zp = zoneout_probability
Expand All @@ -413,6 +422,7 @@ def __init__(self,
zoneout_probability=zp,
pooling=pooling,
bottleneck_size=bottleneck_size,
normalization_fn=normalization_fn,
**kwargs))
super(QRNNBidirectionalStack, self).__init__(**kwargs)

Expand Down
5 changes: 1 addition & 4 deletions research/seq_flow_lite/models/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
licenses(["notice"])

package(
default_visibility = ["//:friends"], # sequence projection
)
package(default_visibility = ["//:friends"]) # sequence projection

py_library(
name = "prado",
Expand Down Expand Up @@ -35,7 +33,6 @@ py_library(
"//layers:qrnn_layers", # sequence projection
"//layers:quantization_layers", # sequence projection
# "//tf_ops:tf_custom_ops" # sequence projection
"//tf_ops:tf_custom_ops_py", # sequence projection
],
)

Expand Down
3 changes: 2 additions & 1 deletion research/seq_flow_lite/models/pqrnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ def _get_params(varname, default_value=None):
_get_params("qrnn_zoneout_probability")
_get_params("number_qrnn_layers")
_get_params("labels", [])
_get_params("num_labels", None)
_get_params("regularizer_scale")
_get_params("quantize")

self.num_classes = len(self.labels)
self.num_classes = self.num_labels or len(self.labels)
self.parameters = base_layers.Parameters(
mode, quantize=self.quantize, regularizer_scale=self.regularizer_scale)

Expand Down
8 changes: 3 additions & 5 deletions research/seq_flow_lite/models/sgnn/BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
licenses(["notice"])

package(
default_visibility = [
"//visibility:public",
],
)
package(default_visibility = [
"//visibility:public",
])

cc_library(
name = "sgnn_projection",
Expand Down
2 changes: 1 addition & 1 deletion research/seq_flow_lite/models/sgnn/run_tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def main(argv):
interpreter.allocate_tensors()
input_string = ' '.join(argv[1:])
print('Input: "{}"'.format(input_string))
input_array = np.array([[input_string]], dtype=np.str)
input_array = np.array([[input_string]], dtype=str)
interpreter.set_tensor(interpreter.get_input_details()[0]['index'],
input_array)
interpreter.invoke()
Expand Down
Loading

0 comments on commit c533506

Please sign in to comment.