From c9839533afd58835ad901a7b0fcf16a0ad5668ba Mon Sep 17 00:00:00 2001 From: Slava Shklyaev Date: Mon, 6 Apr 2020 15:39:27 +0100 Subject: Add operand type variations for WHILE tests Bug: 149199424 Test: NNT_static Test: VTS 1.3 Change-Id: I887be1b8530aad4568db9e75d3260b6b78884d23 --- nn/runtime/test/specs/V1_3/if_simple.mod.py | 7 +- nn/runtime/test/specs/V1_3/while_fib.mod.py | 18 ++-- .../test/specs/V1_3/while_infinite_loop.mod.py | 6 +- .../test/specs/V1_3/while_sum_of_powers.mod.py | 20 ++-- .../specs/V1_3/while_sum_of_powers_quant8.mod.py | 119 +++++++++++++++++++++ .../V1_3/while_sum_of_powers_quant8_signed.mod.py | 119 +++++++++++++++++++++ 6 files changed, 268 insertions(+), 21 deletions(-) create mode 100644 nn/runtime/test/specs/V1_3/while_sum_of_powers_quant8.mod.py create mode 100644 nn/runtime/test/specs/V1_3/while_sum_of_powers_quant8_signed.mod.py (limited to 'nn/runtime/test/specs/V1_3') diff --git a/nn/runtime/test/specs/V1_3/if_simple.mod.py b/nn/runtime/test/specs/V1_3/if_simple.mod.py index 65ec65abc..8e3754029 100644 --- a/nn/runtime/test/specs/V1_3/if_simple.mod.py +++ b/nn/runtime/test/specs/V1_3/if_simple.mod.py @@ -28,8 +28,7 @@ def MakeBranchModel(operation_name): z = Output("z", ValueType) return Model().Operation(operation_name, y, [10.0], 0).To(z) -def Test(x, y, z, name): - x_data, y_data, z_data = x, y, z +def Test(x_data, y_data, z_data, name): x = Input("x", BoolType) y = Input("y", ValueType) z = Output("z", ValueType) @@ -44,5 +43,5 @@ def Test(x, y, z, name): example.AddVariations("relaxed", "float16", "int32", quant8, quant8_signed) example.AddVariations(AllOutputsAsInternalCoverter()) -Test(x=True, y=input_data, z=output_add, name="true") -Test(x=False, y=input_data, z=output_sub, name="false") +Test(x_data=True, y_data=input_data, z_data=output_add, name="true") +Test(x_data=False, y_data=input_data, z_data=output_sub, name="false") diff --git a/nn/runtime/test/specs/V1_3/while_fib.mod.py b/nn/runtime/test/specs/V1_3/while_fib.mod.py index 9555c678d..963c24bfc 100644 --- a/nn/runtime/test/specs/V1_3/while_fib.mod.py +++ b/nn/runtime/test/specs/V1_3/while_fib.mod.py @@ -77,14 +77,14 @@ def MakeBodyModel(): return SubgraphReference("body", model), quant8, quant8_signed -def Test(n, fib): - n_ = Input("n", CounterType) +def Test(n_data, fib_data): + n = Input("n", CounterType) fib_out = Output("fib_out", FibType) cond, cond_quant8, cond_quant8_signed = MakeConditionModel() body, body_quant8, body_quant8_signed = MakeBodyModel() fib_init = Parameter("fib_init", FibType, [1, 1]) i_init = [1] - model = Model().Operation("WHILE", cond, body, fib_init, i_init, n_).To(fib_out) + model = Model().Operation("WHILE", cond, body, fib_init, i_init, n).To(fib_out) quant8 = DataTypeConverter().Identify({ fib_init: FibTypeQuant8, @@ -99,15 +99,15 @@ def Test(n, fib): body: body_quant8_signed, }) - example = Example({n_: [n], fib_out: fib}, name=str(n), model=model) + example = Example({n: [n_data], fib_out: fib_data}, name="n_{}".format(n_data)) example.AddVariations("relaxed", "float16", quant8, quant8_signed) example.AddVariations(AllOutputsAsInternalCoverter()) for use_shm_for_weights in [False, True]: Configuration.use_shm_for_weights = use_shm_for_weights # Fibonacci numbers: 1 1 2 3 5 8 - Test(n=1, fib=[1, 1]) - Test(n=2, fib=[1, 2]) - Test(n=3, fib=[2, 3]) - Test(n=4, fib=[3, 5]) - Test(n=5, fib=[5, 8]) + Test(n_data=1, fib_data=[1, 1]) + Test(n_data=2, fib_data=[1, 2]) + Test(n_data=3, fib_data=[2, 3]) + Test(n_data=4, fib_data=[3, 5]) + Test(n_data=5, fib_data=[5, 8]) diff --git a/nn/runtime/test/specs/V1_3/while_infinite_loop.mod.py b/nn/runtime/test/specs/V1_3/while_infinite_loop.mod.py index bae61aa5b..9e974378f 100644 --- a/nn/runtime/test/specs/V1_3/while_infinite_loop.mod.py +++ b/nn/runtime/test/specs/V1_3/while_infinite_loop.mod.py @@ -51,7 +51,11 @@ body = MakeBodyModel() i_init = [1.0] model = Model().Operation("WHILE", cond, body, i_init, n).To(i_out) -example = Example({n: [0.0], i_out: [999.9]}, model=model) +quant8 = DataTypeConverter("quant8", scale=1.0, zeroPoint=127) +quant8_signed = DataTypeConverter("quant8_signed", scale=1.0, zeroPoint=0) + +example = Example({n: [0.0], i_out: [0.0]}, model=model) +example.AddVariations("relaxed", "float16", quant8, quant8_signed) example.DisableLifeTimeVariation() example.DisableDynamicOutputShapeVariation() example.ExpectFailure() diff --git a/nn/runtime/test/specs/V1_3/while_sum_of_powers.mod.py b/nn/runtime/test/specs/V1_3/while_sum_of_powers.mod.py index 057f19808..8d619d357 100644 --- a/nn/runtime/test/specs/V1_3/while_sum_of_powers.mod.py +++ b/nn/runtime/test/specs/V1_3/while_sum_of_powers.mod.py @@ -89,8 +89,7 @@ def MakeOuterBodyModel(): model.Operation("ADD", sum, xi, 0).To(sum_out) return model -def Test(x, n, sum): - x_data, n_data, sum_data = x, n, sum +def Test(x_data, n_data, sum_data): x = Input("x", DataType) n = Input("n", CounterType) sum = Output("sum", DataType) @@ -99,12 +98,19 @@ def Test(x, n, sum): sum_init = Parameter("sum_init", DataType, [1, 1]) i_init = [1] model = Model().Operation("WHILE", cond, body, sum_init, i_init, n, x).To(sum) - example = Example({x: x_data, n: [n_data], sum: sum_data}, name=str(n_data)) + + example = Example({ + x: x_data, + n: [n_data], + sum: sum_data, + }, name="n_{}".format(n_data)) + example.AddVariations("relaxed", "float16") example.AddVariations(AllOutputsAsInternalCoverter()) for use_shm_for_weights in [False, True]: Configuration.use_shm_for_weights = use_shm_for_weights - Test(x=[2, 3], n=1, sum=[1 + 2, 1 + 3]) - Test(x=[2, 3], n=2, sum=[1 + 2 + 4, 1 + 3 + 9]) - Test(x=[2, 3], n=3, sum=[1 + 2 + 4 + 8, 1 + 3 + 9 + 27]) - Test(x=[2, 3], n=4, sum=[1 + 2 + 4 + 8 + 16, 1 + 3 + 9 + 27 + 81]) + Test(x_data=[2, 3], n_data=0, sum_data=[1, 1]) + Test(x_data=[2, 3], n_data=1, sum_data=[1 + 2, 1 + 3]) + Test(x_data=[2, 3], n_data=2, sum_data=[1 + 2 + 4, 1 + 3 + 9]) + Test(x_data=[2, 3], n_data=3, sum_data=[1 + 2 + 4 + 8, 1 + 3 + 9 + 27]) + Test(x_data=[2, 3], n_data=4, sum_data=[1 + 2 + 4 + 8 + 16, 1 + 3 + 9 + 27 + 81]) diff --git a/nn/runtime/test/specs/V1_3/while_sum_of_powers_quant8.mod.py b/nn/runtime/test/specs/V1_3/while_sum_of_powers_quant8.mod.py new file mode 100644 index 000000000..5f71070a0 --- /dev/null +++ b/nn/runtime/test/specs/V1_3/while_sum_of_powers_quant8.mod.py @@ -0,0 +1,119 @@ +# +# Copyright (C) 2020 The Android Open Source Project +# +# 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. +# + +# Model: given x = [x0, x1] and n, produces [sum(x0 ^ i), sum(x1 ^ i)] for i in [1, n]. +# +# sum = [1, 1] // More generarlly, sum = ones_like(x). +# i = 1 +# while i <= n: +# xi = x // x to the power of i (element-wise) +# j = 1 +# while j < i: +# xi = xi * x +# j += 1 +# sum = sum + xi +# i = i + 1 + +DataType10 = ["TENSOR_QUANT8_ASYMM", [1, 2], 1.0, 128] +DataType05 = ["TENSOR_QUANT8_ASYMM", [1, 2], 0.5, 128] +CounterType = ["TENSOR_INT32", [1]] +BoolType = ["TENSOR_BOOL8", [1]] + +def quantize(data, scale, offset): + return [max(0, min(255, int(round(x / scale)) + offset)) for x in data] + +def MakeInnerConditionModel(): + xi = Input("xi", DataType10) + j = Input("j", CounterType) + i = Input("i", CounterType) + x = Input("x", DataType05) + out = Output("out", BoolType) + model = Model() + model.IdentifyInputs(xi, j, i, x) + model.IdentifyOutputs(out) + model.Operation("LESS", j, i).To(out) + return model + +def MakeInnerBodyModel(): + xi = Input("xi", DataType10) + j = Input("j", CounterType) + i = Input("i", CounterType) + x = Input("x", DataType05) + xi_out = Output("xi_out", DataType10) + j_out = Output("j_out", CounterType) + model = Model() + model.IdentifyInputs(xi, j, i, x) + model.IdentifyOutputs(xi_out, j_out) + model.Operation("MUL", xi, x, 0).To(xi_out) + model.Operation("ADD", j, [1], 0).To(j_out) + return model + +def MakeOuterConditionModel(): + sum = Input("sum", DataType10) + i = Input("i", CounterType) + n = Input("n", CounterType) + x = Input("x", DataType05) + out = Output("out", BoolType) + model = Model() + model.IdentifyInputs(sum, i, n, x) + model.IdentifyOutputs(out) + model.Operation("LESS_EQUAL", i, n).To(out) + return model + +def MakeOuterBodyModel(): + sum = Input("sum", DataType10) + i = Input("i", CounterType) + n = Input("n", CounterType) + x = Input("x", DataType05) + sum_out = Output("sum_out", DataType10) + i_out = Output("i_out", CounterType) + xi_init = Internal("xi_init", DataType10) + j_init = [1] + cond = MakeInnerConditionModel() + body = MakeInnerBodyModel() + xi = Internal("xi", DataType10) + zero = Parameter("zero", DataType10, quantize([0, 0], 1.0, 128)) + model = Model() + model.IdentifyInputs(sum, i, n, x) + model.IdentifyOutputs(sum_out, i_out) + model.Operation("ADD", x, zero, 0).To(xi_init) + model.Operation("WHILE", cond, body, xi_init, j_init, i, x).To(xi) + model.Operation("ADD", i, [1], 0).To(i_out) + model.Operation("ADD", sum, xi, 0).To(sum_out) + return model + +def Test(x_data, n_data, sum_data): + x = Input("x", DataType05) + n = Input("n", CounterType) + sum = Output("sum", DataType10) + cond = MakeOuterConditionModel() + body = MakeOuterBodyModel() + sum_init = Parameter("sum_init", DataType10, quantize([1, 1], 1.0, 128)) + i_init = [1] + model = Model().Operation("WHILE", cond, body, sum_init, i_init, n, x).To(sum) + + example = Example({ + x: quantize(x_data, 0.5, 128), + n: [n_data], + sum: quantize(sum_data, 1.0, 128), + }, name="n_{}".format(n_data)) + example.AddVariations(AllOutputsAsInternalCoverter()) + +Test(x_data=[2, 3], n_data=0, sum_data=[1, 1]) +Test(x_data=[2, 3], n_data=1, sum_data=[1 + 2, 1 + 3]) +Test(x_data=[2, 3], n_data=2, sum_data=[1 + 2 + 4, 1 + 3 + 9]) +Test(x_data=[2, 3], n_data=3, sum_data=[1 + 2 + 4 + 8, 1 + 3 + 9 + 27]) +Test(x_data=[2, 3], n_data=4, sum_data=[1 + 2 + 4 + 8 + 16, 1 + 3 + 9 + 27 + 81]) diff --git a/nn/runtime/test/specs/V1_3/while_sum_of_powers_quant8_signed.mod.py b/nn/runtime/test/specs/V1_3/while_sum_of_powers_quant8_signed.mod.py new file mode 100644 index 000000000..7f11e8f42 --- /dev/null +++ b/nn/runtime/test/specs/V1_3/while_sum_of_powers_quant8_signed.mod.py @@ -0,0 +1,119 @@ +# +# Copyright (C) 2020 The Android Open Source Project +# +# 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. +# + +# Model: given x = [x0, x1] and n, produces [sum(x0 ^ i), sum(x1 ^ i)] for i in [1, n]. +# +# sum = [1, 1] // More generarlly, sum = ones_like(x). +# i = 1 +# while i <= n: +# xi = x // x to the power of i (element-wise) +# j = 1 +# while j < i: +# xi = xi * x +# j += 1 +# sum = sum + xi +# i = i + 1 + +DataType10 = ["TENSOR_QUANT8_ASYMM_SIGNED", [1, 2], 1.0, 12] +DataType05 = ["TENSOR_QUANT8_ASYMM_SIGNED", [1, 2], 0.5, 12] +CounterType = ["TENSOR_INT32", [1]] +BoolType = ["TENSOR_BOOL8", [1]] + +def quantize(data, scale, offset): + return [max(-128, min(127, int(round(x / scale)) + offset)) for x in data] + +def MakeInnerConditionModel(): + xi = Input("xi", DataType10) + j = Input("j", CounterType) + i = Input("i", CounterType) + x = Input("x", DataType05) + out = Output("out", BoolType) + model = Model() + model.IdentifyInputs(xi, j, i, x) + model.IdentifyOutputs(out) + model.Operation("LESS", j, i).To(out) + return model + +def MakeInnerBodyModel(): + xi = Input("xi", DataType10) + j = Input("j", CounterType) + i = Input("i", CounterType) + x = Input("x", DataType05) + xi_out = Output("xi_out", DataType10) + j_out = Output("j_out", CounterType) + model = Model() + model.IdentifyInputs(xi, j, i, x) + model.IdentifyOutputs(xi_out, j_out) + model.Operation("MUL", xi, x, 0).To(xi_out) + model.Operation("ADD", j, [1], 0).To(j_out) + return model + +def MakeOuterConditionModel(): + sum = Input("sum", DataType10) + i = Input("i", CounterType) + n = Input("n", CounterType) + x = Input("x", DataType05) + out = Output("out", BoolType) + model = Model() + model.IdentifyInputs(sum, i, n, x) + model.IdentifyOutputs(out) + model.Operation("LESS_EQUAL", i, n).To(out) + return model + +def MakeOuterBodyModel(): + sum = Input("sum", DataType10) + i = Input("i", CounterType) + n = Input("n", CounterType) + x = Input("x", DataType05) + sum_out = Output("sum_out", DataType10) + i_out = Output("i_out", CounterType) + xi_init = Internal("xi_init", DataType10) + j_init = [1] + cond = MakeInnerConditionModel() + body = MakeInnerBodyModel() + xi = Internal("xi", DataType10) + zero = Parameter("zero", DataType10, quantize([0, 0], 1.0, 12)) + model = Model() + model.IdentifyInputs(sum, i, n, x) + model.IdentifyOutputs(sum_out, i_out) + model.Operation("ADD", x, zero, 0).To(xi_init) + model.Operation("WHILE", cond, body, xi_init, j_init, i, x).To(xi) + model.Operation("ADD", i, [1], 0).To(i_out) + model.Operation("ADD", sum, xi, 0).To(sum_out) + return model + +def Test(x_data, n_data, sum_data): + x = Input("x", DataType05) + n = Input("n", CounterType) + sum = Output("sum", DataType10) + cond = MakeOuterConditionModel() + body = MakeOuterBodyModel() + sum_init = Parameter("sum_init", DataType10, quantize([1, 1], 1.0, 12)) + i_init = [1] + model = Model().Operation("WHILE", cond, body, sum_init, i_init, n, x).To(sum) + + example = Example({ + x: quantize(x_data, 0.5, 12), + n: [n_data], + sum: quantize(sum_data, 1.0, 12), + }, name="n_{}".format(n_data)) + example.AddVariations(AllOutputsAsInternalCoverter()) + +Test(x_data=[2, 3], n_data=0, sum_data=[1, 1]) +Test(x_data=[2, 3], n_data=1, sum_data=[1 + 2, 1 + 3]) +Test(x_data=[2, 3], n_data=2, sum_data=[1 + 2 + 4, 1 + 3 + 9]) +Test(x_data=[2, 3], n_data=3, sum_data=[1 + 2 + 4 + 8, 1 + 3 + 9 + 27]) +Test(x_data=[2, 3], n_data=4, sum_data=[1 + 2 + 4 + 8 + 16, 1 + 3 + 9 + 27 + 81]) -- cgit v1.2.3