IVGCVSW-2682 Add Serializer and Deserializer for BatchToSpaceNd

Change-Id: I8b8ac99b4ecf282b796a8af4b30594ade4e061e6
Signed-off-by: Nattapat Chaimanowong <nattapat.chaimanowong@arm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0823f08..639d745 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -597,6 +597,7 @@
                 src/armnnSerializer/test/SerializerTests.cpp
                 src/armnnDeserializer/test/DeserializeActivation.cpp
                 src/armnnDeserializer/test/DeserializeAdd.cpp
+                src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp
                 src/armnnDeserializer/test/DeserializeConstant.cpp
                 src/armnnDeserializer/test/DeserializeConvolution2d.cpp
                 src/armnnDeserializer/test/DeserializeFullyConnected.cpp
diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp
index 64f8e44..30f7d05 100644
--- a/src/armnnDeserializer/Deserializer.cpp
+++ b/src/armnnDeserializer/Deserializer.cpp
@@ -187,6 +187,7 @@
     // register supported layers
     m_ParserFunctions[Layer_ActivationLayer]             = &Deserializer::ParseActivation;
     m_ParserFunctions[Layer_AdditionLayer]               = &Deserializer::ParseAdd;
+    m_ParserFunctions[Layer_BatchToSpaceNdLayer]         = &Deserializer::ParseBatchToSpaceNd;
     m_ParserFunctions[Layer_ConstantLayer]               = &Deserializer::ParseConstant;
     m_ParserFunctions[Layer_Convolution2dLayer]          = &Deserializer::ParseConvolution2d;
     m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &Deserializer::ParseDepthwiseConvolution2d;
@@ -209,6 +210,8 @@
             return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base();
         case Layer::Layer_AdditionLayer:
             return graphPtr->layers()->Get(layerIndex)->layer_as_AdditionLayer()->base();
+        case Layer::Layer_BatchToSpaceNdLayer:
+            return graphPtr->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->base();
         case Layer::Layer_ConstantLayer:
             return graphPtr->layers()->Get(layerIndex)->layer_as_ConstantLayer()->base();
         case Layer::Layer_Convolution2dLayer:
@@ -778,6 +781,49 @@
     RegisterOutputSlots(graph, layerIndex, layer);
 }
 
+void Deserializer::ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex)
+{
+    CHECK_LAYERS(graph, 0, layerIndex);
+
+    Deserializer::TensorRawPtrVector inputs = GetInputs(graph, layerIndex);
+    CHECK_VALID_SIZE(inputs.size(), 1);
+
+    Deserializer::TensorRawPtrVector outputs = GetOutputs(graph, layerIndex);
+    CHECK_VALID_SIZE(outputs.size(), 1);
+
+    auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->descriptor();
+    auto flatBufferCrops = flatBufferDescriptor->crops();
+    auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
+
+    if (flatBufferCrops->Length() % 2 != 0)
+    {
+        throw ParseException(boost::str(
+            boost::format("The size of crops must be divisible by 2 %1%") % CHECK_LOCATION().AsString()));
+    }
+
+    std::vector<std::pair<unsigned int, unsigned int>> crops;
+    crops.reserve(flatBufferCrops->Length() / 2);
+    for (unsigned int i = 0; i < flatBufferCrops->Length() - 1; i += 2)
+    {
+        crops.emplace_back(flatBufferCrops->Get(i), flatBufferCrops->Get(i+1));
+    }
+
+    armnn::BatchToSpaceNdDescriptor descriptor;
+    descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout());
+    descriptor.m_BlockShape =
+        std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
+    descriptor.m_Crops = crops;
+
+    auto layerName = GetLayerName(graph, layerIndex);
+    IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(descriptor, layerName.c_str());
+
+    armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
+    layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
+
+    RegisterInputSlots(graph, layerIndex, layer);
+    RegisterOutputSlots(graph, layerIndex, layer);
+}
+
 void Deserializer::ParseConstant(GraphPtr graph, unsigned int layerIndex)
 {
     CHECK_LAYERS(graph, 0, layerIndex);
diff --git a/src/armnnDeserializer/Deserializer.hpp b/src/armnnDeserializer/Deserializer.hpp
index c88459d..0e0261f 100644
--- a/src/armnnDeserializer/Deserializer.hpp
+++ b/src/armnnDeserializer/Deserializer.hpp
@@ -70,6 +70,7 @@
     void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex);
     void ParseActivation(GraphPtr graph, unsigned int layerIndex);
     void ParseAdd(GraphPtr graph, unsigned int layerIndex);
+    void ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex);
     void ParseConstant(GraphPtr graph, unsigned int layerIndex);
     void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex);
     void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex);
diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md
index 4ae9780..31a5356 100644
--- a/src/armnnDeserializer/DeserializerSupport.md
+++ b/src/armnnDeserializer/DeserializerSupport.md
@@ -8,6 +8,7 @@
 
 * Activation
 * Addition
+* BatchToSpaceNd
 * Constant
 * Convolution2d
 * DepthwiseConvolution2d
diff --git a/src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp b/src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp
new file mode 100644
index 0000000..e85f4ad
--- /dev/null
+++ b/src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp
@@ -0,0 +1,136 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include <boost/test/unit_test.hpp>
+#include "ParserFlatbuffersSerializeFixture.hpp"
+#include "../Deserializer.hpp"
+
+#include <string>
+
+BOOST_AUTO_TEST_SUITE(Deserializer)
+
+struct BatchToSpaceNdFixture : public ParserFlatbuffersSerializeFixture
+{
+    explicit BatchToSpaceNdFixture(const std::string &inputShape,
+                                   const std::string &blockShape,
+                                   const std::string &crops,
+                                   const std::string &dataLayout,
+                                   const std::string &outputShape,
+                                   const std::string &dataType)
+    {
+        m_JsonString = R"(
+            {
+                inputIds: [0],
+                outputIds: [2],
+                layers: [
+                    {
+                        layer_type: "InputLayer",
+                        layer: {
+                            base: {
+                                layerBindingId: 0,
+                                base: {
+                                    index: 0,
+                                    layerName: "InputLayer",
+                                    layerType: "Input",
+                                    inputSlots: [{
+                                        index: 0,
+                                        connection: {sourceLayerIndex:0, outputSlotIndex:0 },
+                                    }],
+                                    outputSlots: [{
+                                        index: 0,
+                                        tensorInfo: {
+                                            dimensions: )" + inputShape + R"(,
+                                            dataType: )" + dataType + R"(
+                                        }
+                                    }]
+                                }
+                            }
+                        }
+                    },
+                    {
+                        layer_type: "BatchToSpaceNdLayer",
+                        layer: {
+                            base: {
+                                index: 1,
+                                layerName: "BatchToSpaceNdLayer",
+                                layerType: "BatchToSpaceNd",
+                                inputSlots: [{
+                                    index: 0,
+                                    connection: {sourceLayerIndex:0, outputSlotIndex:0 },
+                                }],
+                                outputSlots: [{
+                                    index: 0,
+                                    tensorInfo: {
+                                        dimensions: )" + outputShape + R"(,
+                                        dataType: )" + dataType + R"(
+                                    }
+                                }]
+                            },
+                            descriptor: {
+                                blockShape: )" + blockShape + R"(,
+                                crops: )" + crops + R"(,
+                                dataLayout: )" + dataLayout + R"(,
+                            }
+                        }
+                    },
+                    {
+                        layer_type: "OutputLayer",
+                        layer: {
+                            base:{
+                                layerBindingId: 2,
+                                base: {
+                                    index: 2,
+                                    layerName: "OutputLayer",
+                                    layerType: "Output",
+                                    inputSlots: [{
+                                        index: 0,
+                                        connection: {sourceLayerIndex:1, outputSlotIndex:0 },
+                                    }],
+                                    outputSlots: [{
+                                        index: 0,
+                                        tensorInfo: {
+                                            dimensions: )" + outputShape + R"(,
+                                            dataType: )" + dataType + R"(
+                                        },
+                                    }],
+                                }
+                            }
+                        },
+                    }
+                ]
+            }
+        )";
+        SetupSingleInputSingleOutput("InputLayer", "OutputLayer");
+    }
+};
+
+struct SimpleBatchToSpaceNdFixture : BatchToSpaceNdFixture
+{
+    SimpleBatchToSpaceNdFixture() : BatchToSpaceNdFixture("[ 4, 2, 2, 1 ]",
+                                                          "[ 2, 2 ]",
+                                                          "[ 0, 0, 0, 0 ]",
+                                                          "NHWC",
+                                                          "[ 1, 4, 4, 1 ]",
+                                                          "Float32") {}
+};
+
+BOOST_FIXTURE_TEST_CASE(SimpleBatchToSpaceNdFloat32, SimpleBatchToSpaceNdFixture)
+{
+    RunTest<4, armnn::DataType::Float32>(0,
+                                         {
+                                             1.0f, 3.0f,  9.0f, 11.0f,
+                                             2.0f, 4.0f, 10.0f, 12.0f,
+                                             5.0f, 7.0f, 13.0f, 15.0f,
+                                             6.0f, 8.0f, 14.0f, 16.0f
+                                         },
+                                         {
+                                              1.0f,  2.0f,  3.0f,  4.0f,
+                                              5.0f,  6.0f,  7.0f,  8.0f,
+                                              9.0f, 10.0f, 11.0f, 12.0f,
+                                             13.0f, 14.0f, 15.0f, 16.0f
+                                         });
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/armnnSerializer/ArmnnSchema.fbs b/src/armnnSerializer/ArmnnSchema.fbs
index c1e4e38..7d64dca 100644
--- a/src/armnnSerializer/ArmnnSchema.fbs
+++ b/src/armnnSerializer/ArmnnSchema.fbs
@@ -94,7 +94,8 @@
     Permute = 10,
     FullyConnected = 11,
     Constant = 12,
-    SpaceToBatchNd = 13
+    SpaceToBatchNd = 13,
+    BatchToSpaceNd = 14
 }
 
 // Base layer table to be used as part of other layers
@@ -266,9 +267,21 @@
     dataLayout:DataLayout;
 }
 
+table BatchToSpaceNdLayer {
+    base:LayerBase;
+    descriptor:BatchToSpaceNdDescriptor;
+}
+
+table BatchToSpaceNdDescriptor {
+    blockShape:[uint];
+    crops:[uint];
+    dataLayout:DataLayout;
+}
+
 union Layer {
     ActivationLayer,
     AdditionLayer,
+    BatchToSpaceNdLayer,
     ConstantLayer,
     Convolution2dLayer,
     DepthwiseConvolution2dLayer,
diff --git a/src/armnnSerializer/Serializer.cpp b/src/armnnSerializer/Serializer.cpp
index 1c1cefd..dc25bab 100644
--- a/src/armnnSerializer/Serializer.cpp
+++ b/src/armnnSerializer/Serializer.cpp
@@ -141,6 +141,35 @@
     CreateAnyLayer(flatBufferAdditionLayer.o, serializer::Layer::Layer_AdditionLayer);
 }
 
+// Build FlatBuffer for BatchToSpaceNd Layer
+void SerializerVisitor::VisitBatchToSpaceNdLayer(const armnn::IConnectableLayer* layer,
+                                                 const armnn::BatchToSpaceNdDescriptor& descriptor,
+                                                 const char* name)
+{
+    // Create FlatBuffer BaseLayer
+    auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_BatchToSpaceNd);
+
+    std::vector<unsigned int> crops;
+    crops.reserve(descriptor.m_Crops.size() * 2);
+    for (auto& crop : descriptor.m_Crops)
+    {
+        crops.push_back(crop.first);
+        crops.push_back(crop.second);
+    }
+
+    auto flatBufferDescriptor =
+        CreateBatchToSpaceNdDescriptor(m_flatBufferBuilder,
+                                       m_flatBufferBuilder.CreateVector(descriptor.m_BlockShape),
+                                       m_flatBufferBuilder.CreateVector(crops),
+                                       GetFlatBufferDataLayout(descriptor.m_DataLayout));
+
+    auto flatBufferLayer = serializer::CreateBatchToSpaceNdLayer(m_flatBufferBuilder,
+                                                                 flatBufferBaseLayer,
+                                                                 flatBufferDescriptor);
+
+    CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_BatchToSpaceNdLayer);
+}
+
 // Build FlatBuffer for Constant Layer
 void SerializerVisitor::VisitConstantLayer(const armnn::IConnectableLayer* layer,
                                            const armnn::ConstTensor& input,
diff --git a/src/armnnSerializer/Serializer.hpp b/src/armnnSerializer/Serializer.hpp
index a86e04f..f8e2b16 100644
--- a/src/armnnSerializer/Serializer.hpp
+++ b/src/armnnSerializer/Serializer.hpp
@@ -49,6 +49,10 @@
     void VisitAdditionLayer(const armnn::IConnectableLayer* layer,
                             const char* name = nullptr) override;
 
+    void VisitBatchToSpaceNdLayer(const armnn::IConnectableLayer* layer,
+                                  const armnn::BatchToSpaceNdDescriptor& descriptor,
+                                  const char* name = nullptr) override;
+
     void VisitConstantLayer(const armnn::IConnectableLayer* layer,
                             const armnn::ConstTensor& input,
                             const char* = nullptr) override;
diff --git a/src/armnnSerializer/SerializerSupport.md b/src/armnnSerializer/SerializerSupport.md
index 776f612..6340dd2 100644
--- a/src/armnnSerializer/SerializerSupport.md
+++ b/src/armnnSerializer/SerializerSupport.md
@@ -8,6 +8,7 @@
 
 * Activation
 * Addition
+* BatchToSpaceNd
 * Constant
 * Convolution2d
 * DepthwiseConvolution2d
diff --git a/src/armnnSerializer/test/SerializerTests.cpp b/src/armnnSerializer/test/SerializerTests.cpp
index f64c14a..8ce9049 100644
--- a/src/armnnSerializer/test/SerializerTests.cpp
+++ b/src/armnnSerializer/test/SerializerTests.cpp
@@ -682,4 +682,50 @@
                                             outputTensorInfo.GetShape());
 }
 
+BOOST_AUTO_TEST_CASE(SerializeDeserializeBatchToSpaceNd)
+{
+    class VerifyBatchToSpaceNdName : public armnn::LayerVisitorBase<armnn::VisitorNoThrowPolicy>
+    {
+    public:
+        void VisitBatchToSpaceNdLayer(const armnn::IConnectableLayer*,
+                                      const armnn::BatchToSpaceNdDescriptor& descriptor,
+                                      const char* name) override
+        {
+            BOOST_TEST(name == "BatchToSpaceNdLayer");
+        }
+    };
+
+    unsigned int inputShape[] = {4, 1, 2, 2};
+    unsigned int outputShape[] = {1, 1, 4, 4};
+
+    armnn::BatchToSpaceNdDescriptor desc;
+    desc.m_DataLayout = armnn::DataLayout::NCHW;
+    desc.m_BlockShape = {2, 2};
+    desc.m_Crops = {{0, 0}, {0, 0}};
+
+    auto inputTensorInfo = armnn::TensorInfo(4, inputShape, armnn::DataType::Float32);
+    auto outputTensorInfo = armnn::TensorInfo(4, outputShape, armnn::DataType::Float32);
+
+    armnn::INetworkPtr network = armnn::INetwork::Create();
+    armnn::IConnectableLayer* const inputLayer = network->AddInputLayer(0);
+    armnn::IConnectableLayer* const batchToSpaceNdLayer = network->AddBatchToSpaceNdLayer(desc, "BatchToSpaceNdLayer");
+    armnn::IConnectableLayer* const outputLayer = network->AddOutputLayer(0);
+
+    inputLayer->GetOutputSlot(0).Connect(batchToSpaceNdLayer->GetInputSlot(0));
+    inputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
+    batchToSpaceNdLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
+    batchToSpaceNdLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
+
+    armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(SerializeNetwork(*network));
+    BOOST_CHECK(deserializedNetwork);
+
+    VerifyBatchToSpaceNdName nameChecker;
+    deserializedNetwork->Accept(nameChecker);
+
+    CheckDeserializedNetworkAgainstOriginal(*network,
+                                            *deserializedNetwork,
+                                            inputTensorInfo.GetShape(),
+                                            outputTensorInfo.GetShape());
+}
+
 BOOST_AUTO_TEST_SUITE_END()