2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
6 #include <boost/test/unit_test.hpp>
7 #include "armnnTfParser/ITfParser.hpp"
8 #include "ParserPrototxtFixture.hpp"
10 #include "Network.hpp"
13 BOOST_AUTO_TEST_SUITE(TensorflowParser)
15 // In Tensorflow fully connected layers are expressed as a MatMul followed by an Add.
16 // The TfParser must detect this case and convert them to a FullyConnected layer.
17 struct FullyConnectedFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser>
19 FullyConnectedFixture()
21 // Input = tf.placeholder(tf.float32, [1, 1], "input")
22 // Weights = tf.constant([2], tf.float32, [1, 1])
23 // Matmul = tf.matmul(input, weights)
24 // Bias = tf.constant([1], tf.float32)
25 // Output = tf.add(matmul, bias, name="output")
138 SetupSingleInputSingleOutput({ 1, 1 }, "input", "output");
142 BOOST_FIXTURE_TEST_CASE(FullyConnected, FullyConnectedFixture)
144 RunTest<1>({ 3 }, { 7 });
147 // Similar to FullyConnectedFixture, but this time the MatMul's output is used by two Adds. This should result
148 // in two FullyConnected layers being created.
156 struct MatMulUsedInTwoFcFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser>
158 MatMulUsedInTwoFcFixture()
320 SetupSingleInputSingleOutput({ 1, 1 }, "input", "output");
324 BOOST_FIXTURE_TEST_CASE(MatMulUsedInTwoFc, MatMulUsedInTwoFcFixture)
326 RunTest<1>({ 3 }, { 32 });
327 // Ideally we would check here that the armnn network has 5 layers:
328 // Input, 2 x FullyConnected (biased), Add and Output.
329 // This would make sure the parser hasn't incorrectly added some unconnected layers corresponding to the MatMul.
332 // Similar to MatMulUsedInTwoFc, but this time the Adds are 'staggered' (see diagram), which means that only one
333 // FullyConnected layer can be created (the other should just be an Add).
341 struct MatMulUsedInTwoFcStaggeredFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser>
343 MatMulUsedInTwoFcStaggeredFixture()
345 // Input = tf.placeholder(tf.float32, shape=[1,1], name = "input")
346 // Const1 = tf.constant([17], tf.float32, [1,1])
347 // Mul = tf.matmul(input, const1)
348 // Monst2 = tf.constant([7], tf.float32, [1])
349 // Fc = tf.add(mul, const2)
350 // Output = tf.add(mul, fc, name="output")
475 SetupSingleInputSingleOutput({ 1, 1 }, "input", "output");
479 BOOST_FIXTURE_TEST_CASE(MatMulUsedInTwoFcStaggered, MatMulUsedInTwoFcStaggeredFixture)
481 RunTest<1>({ 2 }, { 75 });
482 // Ideally we would check here that the armnn network has 5 layers:
483 // Input, FullyConnected (biased), FullyConnected (non biased), Add and Output.
486 // A MatMul in isolation, not connected to an add. Should result in a non-biased FullyConnected layer.
487 struct MatMulFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser>
491 // Input = tf.placeholder(tf.float32, shape = [1, 1], name = "input")
492 // Const = tf.constant([17], tf.float32, [1, 1])
493 // Output = tf.matmul(input, const, name = "output")
570 SetupSingleInputSingleOutput({ 1, 1 }, "input", "output");
574 BOOST_FIXTURE_TEST_CASE(MatMul, MatMulFixture)
576 RunTest<1>({ 2 }, { 34 });
579 BOOST_AUTO_TEST_SUITE_END()