aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Nupur Garg <nupurgarg@google.com>2018-09-11 16:37:49 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-09-11 16:42:08 -0700
commit8ebfc2633b355535284e6fc8970fc91dde45ed9d (patch)
tree9b5b710149f11f03c5686b38c91af888f64c1440
parent668c079f4e6020131978b7a812c3b92eea9c47b9 (diff)
Internal change.
PiperOrigin-RevId: 212545735
-rw-r--r--tensorflow/contrib/lite/build_def.bzl57
-rw-r--r--tensorflow/contrib/lite/delegates/eager/delegate_test.cc28
-rw-r--r--tensorflow/contrib/lite/testing/BUILD5
-rw-r--r--tensorflow/contrib/lite/testing/generate_examples.py27
4 files changed, 99 insertions, 18 deletions
diff --git a/tensorflow/contrib/lite/build_def.bzl b/tensorflow/contrib/lite/build_def.bzl
index 9317e2bb6e..0210428026 100644
--- a/tensorflow/contrib/lite/build_def.bzl
+++ b/tensorflow/contrib/lite/build_def.bzl
@@ -295,32 +295,69 @@ def generated_test_models():
"where",
]
-def gen_zip_test(name, test_name, **kwargs):
+def generated_test_conversion_modes():
+ """Returns a list of conversion modes."""
+
+ # TODO(nupurgarg): Add "pb2lite" when it's in open source. b/113614050.
+ return ["toco-extended", ""]
+
+def generated_test_models_all():
+ """Generates a list of all tests with the different converters.
+
+ Returns:
+ List of tuples representing (conversion mode, name of test).
+ """
+ conversion_modes = generated_test_conversion_modes()
+ tests = generated_test_models()
+ options = []
+ for conversion_mode in conversion_modes:
+ for test in tests:
+ if conversion_mode:
+ test += "_%s" % conversion_mode
+ options.append((conversion_mode, test))
+ return options
+
+def gen_zip_test(name, test_name, conversion_mode, **kwargs):
"""Generate a zipped-example test and its dependent zip files.
Args:
- name: Resulting cc_test target name
- test_name: Test targets this model. Comes from the list above.
- **kwargs: tf_cc_test kwargs.
+ name: str. Resulting cc_test target name
+ test_name: str. Test targets this model. Comes from the list above.
+ conversion_mode: str. Which conversion mode to run with. Comes from the
+ list above.
+ **kwargs: tf_cc_test kwargs
"""
+ toco = "//tensorflow/contrib/lite/toco:toco"
+ flags = ""
+ if conversion_mode:
+ # TODO(nupurgarg): Comment in when pb2lite is in open source. b/113614050.
+ # if conversion_mode == "pb2lite":
+ # toco = "//tensorflow/contrib/lite/experimental/pb2lite:pb2lite"
+ flags = "--ignore_toco_errors --run_with_extended"
+ kwargs["tags"].append("skip_already_failing")
+ kwargs["tags"].append("no_oss")
+
gen_zipped_test_file(
name = "zip_%s" % test_name,
file = "%s.zip" % test_name,
+ toco = toco,
+ flags = flags,
)
tf_cc_test(name, **kwargs)
-def gen_zipped_test_file(name, file):
+def gen_zipped_test_file(name, file, toco, flags):
"""Generate a zip file of tests by using :generate_examples.
Args:
- name: Name of output. We will produce "`file`.files" as a target.
- file: The name of one of the generated_examples targets, e.g. "transpose"
+ name: str. Name of output. We will produce "`file`.files" as a target.
+ file: str. The name of one of the generated_examples targets, e.g. "transpose"
+ toco: str. Pathname of toco binary to run
+ flags: str. Any additional flags to include
"""
- toco = "//tensorflow/contrib/lite/toco:toco"
native.genrule(
name = file + ".files",
- cmd = ("$(locations :generate_examples) --toco $(locations %s) " % toco +
- " --zip_to_output " + file + " $(@D)"),
+ cmd = (("$(locations :generate_examples) --toco $(locations {0}) " +
+ " --zip_to_output {1} {2} $(@D)").format(toco, file, flags)),
outs = [file],
tools = [
":generate_examples",
diff --git a/tensorflow/contrib/lite/delegates/eager/delegate_test.cc b/tensorflow/contrib/lite/delegates/eager/delegate_test.cc
index 984f8bbc98..43ec5d53b8 100644
--- a/tensorflow/contrib/lite/delegates/eager/delegate_test.cc
+++ b/tensorflow/contrib/lite/delegates/eager/delegate_test.cc
@@ -157,6 +157,34 @@ TEST_F(DelegateTest, OnlyTFLite) {
ASSERT_THAT(GetValues(2), ElementsAre(1.1f, 4.4f, 9.9f, 17.6f));
}
+TEST_F(DelegateTest, MultipleInvokeCalls) {
+ // Call Invoke() multiple times on the same model.
+ AddTensors(10, {0, 1}, {2}, kTfLiteFloat32, {3});
+ AddTfLiteMulOp({0, 1}, {2});
+
+ ConfigureDelegate();
+
+ SetShape(0, {2, 2, 1});
+ SetValues(0, {1.1f, 2.2f, 3.3f, 4.4f});
+ SetShape(1, {2, 2, 1});
+ SetValues(1, {1.0f, 2.0f, 3.0f, 4.0f});
+
+ ASSERT_TRUE(Invoke());
+
+ ASSERT_THAT(GetShape(2), ElementsAre(2, 2, 1));
+ ASSERT_THAT(GetValues(2), ElementsAre(1.1f, 4.4f, 9.9f, 17.6f));
+
+ SetShape(0, {2, 2, 1});
+ SetValues(1, {4.0f, 3.0f, 2.0f, 1.0f});
+ SetShape(1, {2, 2, 1});
+ SetValues(0, {4.4f, 3.3f, 2.2f, 1.1f});
+
+ ASSERT_TRUE(Invoke());
+
+ ASSERT_THAT(GetShape(2), ElementsAre(2, 2, 1));
+ ASSERT_THAT(GetValues(2), ElementsAre(17.6f, 9.9f, 4.4f, 1.1f));
+}
+
TEST_F(DelegateTest, MultipleInterpretersSameDelegate) {
// Build a graph, configure the delegate and set inputs.
{
diff --git a/tensorflow/contrib/lite/testing/BUILD b/tensorflow/contrib/lite/testing/BUILD
index 3a6c16cafc..a4736bfee9 100644
--- a/tensorflow/contrib/lite/testing/BUILD
+++ b/tensorflow/contrib/lite/testing/BUILD
@@ -7,7 +7,7 @@ licenses(["notice"]) # Apache 2.0
load(
"//tensorflow/contrib/lite:build_def.bzl",
"gen_zip_test",
- "generated_test_models",
+ "generated_test_models_all",
)
load("//tensorflow/contrib/lite:special_rules.bzl", "tflite_portable_test_suite")
load(
@@ -29,6 +29,7 @@ load(
"--unzip_binary_path=/usr/bin/unzip",
],
}),
+ conversion_mode = conversion_mode,
data = [
":zip_%s" % test_name,
],
@@ -59,7 +60,7 @@ load(
"//tensorflow/core:android_tensorflow_test_lib",
],
}),
-) for test_name in generated_test_models()]
+) for conversion_mode, test_name in generated_test_models_all()]
test_suite(
name = "generated_zip_tests",
diff --git a/tensorflow/contrib/lite/testing/generate_examples.py b/tensorflow/contrib/lite/testing/generate_examples.py
index 32f02a4f6c..812385e706 100644
--- a/tensorflow/contrib/lite/testing/generate_examples.py
+++ b/tensorflow/contrib/lite/testing/generate_examples.py
@@ -80,7 +80,10 @@ parser.add_argument(
"--save_graphdefs",
action="store_true",
help="Include intermediate graphdefs in the output zip files.")
-
+parser.add_argument(
+ "--run_with_extended",
+ action="store_true",
+ help="Whether the TFLite Extended converter is being used.")
RANDOM_SEED = 342
TEST_INPUT_DEPTH = 3
@@ -320,10 +323,11 @@ def toco_convert(graph_def_str, input_tensors, output_tensors,
output tflite model, log_txt from conversion
or None, log_txt if it did not convert properly.
"""
+ input_arrays = [x[0] for x in input_tensors]
data_types = [_TF_TYPE_INFO[x[2]][1] for x in input_tensors]
opts = toco_options(
data_types=data_types,
- input_arrays=[x[0] for x in input_tensors],
+ input_arrays=input_arrays,
shapes=[x[1] for x in input_tensors],
output_arrays=output_tensors,
extra_toco_options=extra_toco_options)
@@ -335,6 +339,11 @@ def toco_convert(graph_def_str, input_tensors, output_tensors,
graphdef_file.flush()
# TODO(aselle): Switch this to subprocess at some point.
+ if "pb2lite" in bin_path and FLAGS.run_with_extended:
+ opts = ("--input_arrays={0} --output_arrays={1}".format(
+ ",".join(input_arrays), ",".join(output_tensors)))
+ elif FLAGS.run_with_extended:
+ opts += " --allow_eager_ops --force_eager_ops"
cmd = ("%s --input_file=%s --output_file=%s %s > %s 2>&1" %
(bin_path, graphdef_file.name, output_file.name, opts,
stdout_file.name))
@@ -1502,7 +1511,7 @@ def make_split_tests(zip_path):
dtype=tf.float32, name="input", shape=parameters["input_shape"])
out = tf.split(
input_tensor, parameters["num_or_size_splits"], parameters["axis"])
- return [input_tensor], out
+ return [input_tensor], [out[0]]
def build_inputs(parameters, sess, inputs, outputs):
values = [create_tensor_data(np.float32, parameters["input_shape"])]
@@ -2510,10 +2519,12 @@ def make_topk_tests(zip_path):
shape=parameters["input_shape"])
if parameters["input_k"] is not None:
k = tf.placeholder(dtype=tf.int32, name="input_k", shape=[])
+ inputs = [input_value, k]
else:
k = tf.constant(3, name="k")
+ inputs = [input_value]
out = tf.nn.top_k(input_value, k)
- return [input_value, k], [out[1]]
+ return inputs, [out[1]]
def build_inputs(parameters, sess, inputs, outputs):
input_value = create_tensor_data(parameters["input_dtype"],
@@ -3208,7 +3219,7 @@ def make_unpack_tests(zip_path):
input_tensor = tf.placeholder(
dtype=tf.float32, name=("input"), shape=parameters["base_shape"])
outs = tf.unstack(input_tensor, axis=get_valid_axis(parameters))
- return [input_tensor], outs
+ return [input_tensor], [outs[0]]
def build_inputs(parameters, sess, inputs, outputs):
input_value = create_tensor_data(np.float32, shape=parameters["base_shape"])
@@ -3286,7 +3297,11 @@ def main(unused_args):
out = FLAGS.zip_to_output
bin_path = FLAGS.toco
- test_function = ("make_%s_tests" % out.replace(".zip", ""))
+ # Some zip filenames contain a postfix identifying the conversion mode. The
+ # list of valid conversion modes is defined in
+ # generated_test_conversion_modes() in build_def.bzl.
+ test_function = ("make_%s_tests" % (out.replace(".zip", "").replace(
+ "pb2lite", "").replace("toco-extended", "").rstrip("_")))
if test_function not in globals():
raise RuntimeError("Can't find a test function to create %r. Tried %r" %
(out, test_function))