[GNA] support for 3D [1,1,X] import/output (#2893)
authorAnna Alberska <anna.alberska@intel.com>
Thu, 5 Nov 2020 16:31:55 +0000 (17:31 +0100)
committerGitHub <noreply@github.com>
Thu, 5 Nov 2020 16:31:55 +0000 (19:31 +0300)
* [GNA] support for 3D [1,1,X] import/output

* [GNA] add check for C == 1 and H == 1

* [GNA] fix input dimensions restrictions for 3d

inference-engine/src/gna_plugin/gna_plugin.cpp
inference-engine/tests/functional/plugin/gna/shared_tests_instances/single_layer_tests/eltwise.cpp

index d7b704f..8a69d04 100644 (file)
@@ -872,14 +872,24 @@ uint32_t GNAPlugin::QueueInference(const InferenceEngine::BlobMap &inputs, Infer
     int inputNum = 0;
     for (auto &input : inputs) {
         auto inputLayout = input.second->getTensorDesc().getLayout();
-        if (inputLayout != Layout::NC && inputLayout != Layout::CN && inputLayout != NCHW) {
+        if (inputLayout != Layout::NC && inputLayout != Layout::CN && inputLayout != Layout::CHW && inputLayout != Layout::NCHW) {
             THROW_GNA_EXCEPTION << "Expected input blob to have Layout::NC or Layout::CN, but was: "
                                 << input.second->getTensorDesc().getLayout();
         }
-        if (inputLayout == NCHW) {
-            inputLayout = NC;
+
+        auto dims = input.second->getTensorDesc().getDims();
+        if (inputLayout == Layout::CHW && (dims[0] != 1 || dims[1] != 1)) {
+            THROW_GNA_EXCEPTION << "For Layout::CHW only dimension with height = 1 and channel = 1 is supported, but was: "
+                                << dims;
+        }
+
+        if (inputLayout == Layout::NCHW || inputLayout == Layout::CHW) {
+            // specific case that can be squeezed to 2d
+            inputLayout = Layout::NC;
         }
+
         auto is2D = input.second->getTensorDesc().getLayout() == Layout::NC || input.second->getTensorDesc().getLayout() == Layout::CN;
+        auto is3D = input.second->getTensorDesc().getLayout() == Layout::CHW;
 
         if (!inputsDesc->ptr_inputs_global_id.count(input.first)) {
             // should not happen in user code however might happen if there any non executable network based integration of GNAPlugin instance
@@ -904,10 +914,8 @@ uint32_t GNAPlugin::QueueInference(const InferenceEngine::BlobMap &inputs, Infer
             }
         }
 
-        auto dims = input.second->getTensorDesc().getDims();
-
         auto  importedElements = is2D ? dims[dims.size() - 1] : dims[dims.size() - 1] * dims[dims.size() - 2] * dims[dims.size() - 3];
-        auto  importedFrames = dims[0];
+        auto  importedFrames = is3D ? 1 : dims[0];
         auto  targetGroups = is2D ? dims[dims.size() - 2] : dims[0]; // TODO: no proper support for groups yet
 
         auto  importedElementSizeBytes = gnaFlags->sw_fp32 ? 4 : 2;
@@ -1017,7 +1025,7 @@ GnaWaitStatus GNAPlugin::WaitFor(uint32_t request_idx, int64_t millisTimeout) {
         auto & outputBlob = outputBlobIt.second;
         auto & outputDesc = outputsDesc[output_idx];
         if (outputBlob->getTensorDesc().getLayout() == Layout::NC || outputBlob->getTensorDesc().getLayout() == Layout::CN
-            || outputBlob->getTensorDesc().getLayout() == Layout::NCHW || outputBlob->getTensorDesc().getLayout() == Layout::NHWC) {
+            || outputBlob->getTensorDesc().getLayout() == Layout::NCHW || outputBlob->getTensorDesc().getLayout() == Layout::CHW) {
             // TODO: rotate can be incorporated with exporting - used only in unit tests so far
             // TODO: restore:
 //        if (orientation_out != kDnnInterleavedOrientation) {
@@ -1034,8 +1042,9 @@ GnaWaitStatus GNAPlugin::WaitFor(uint32_t request_idx, int64_t millisTimeout) {
 //                           dims[dims.size() - 1]);
 //        }
             auto is2D = outputBlob->getTensorDesc().getLayout() == Layout::NC || outputBlob->getTensorDesc().getLayout() == Layout::CN;
+            auto is3D = outputBlob->getTensorDesc().getLayout() == Layout::CHW;
             auto& exportOutputDims = outputBlob->getTensorDesc().getDims();
-            auto batchSize = exportOutputDims[0];
+            auto batchSize = is3D ? 1 : exportOutputDims[0];
             auto elementsPerBatch = is2D ? exportOutputDims[exportOutputDims.size() - 1]
                 : exportOutputDims[exportOutputDims.size() - 1]
                   * exportOutputDims[exportOutputDims.size() - 2]
@@ -1090,7 +1099,7 @@ GnaWaitStatus GNAPlugin::WaitFor(uint32_t request_idx, int64_t millisTimeout) {
 #endif
             }
         } else {
-            THROW_GNA_EXCEPTION << "Expected output blob to have Layout::NC, Layout::CN, Layout::NCHW or Layout::NHWC. But was "
+            THROW_GNA_EXCEPTION << "Expected output blob to have Layout::NC, Layout::CN, Layout::NCHW or Layout::CHW. But was "
                 << outputBlob->getTensorDesc().getLayout();
         }
 
@@ -1125,7 +1134,7 @@ Blob::Ptr GNAPlugin::GetOutputBlob(const std::string& name, InferenceEngine::Pre
     // need to have intermediate blob for interleave conversion
     InferenceEngine::Blob::Ptr outputBlob;
     auto outputDims = outputsDataMap[name]->getTensorDesc().getDims();
-    outputBlob = make_blob_with_precision(TensorDesc(precision, outputDims, outputDims.size() == 2 ? NC : NCHW));
+    outputBlob = make_blob_with_precision(TensorDesc(precision, outputDims, outputDims.size() == 2 ? NC : (outputDims.size() == 3 ? CHW : NCHW)));
     outputBlob->allocate();
     return outputBlob;
 }
@@ -1135,7 +1144,7 @@ Blob::Ptr GNAPlugin::GetInputBlob(const std::string& name, InferenceEngine::Prec
     // need to have intermediate blob for interleave conversion
     // TODO: NCHW format support is experimental = c++ MO did insert reshape, while TF mo - not
     auto inputDims = inputsDataMap[name]->getTensorDesc().getDims();
-    inputBlob = make_blob_with_precision(TensorDesc(precision, inputDims, inputDims.size() == 2 ? NC : NCHW));
+    inputBlob = make_blob_with_precision(TensorDesc(precision, inputDims, inputDims.size() == 2 ? NC : (inputDims.size() == 3 ? CHW : NCHW)));
     inputBlob->allocate();
     return inputBlob;
 }
index d1a8312..3c8e3e8 100644 (file)
@@ -13,7 +13,7 @@ std::vector<std::vector<std::vector<size_t>>> inShapes = {
         {{1, 200}},
         // TODO: Issue 32544
         // {{2}},
-        // {{1, 1, 1, 3}},
+        {{1, 1, 1, 3}},
         // {{1, 2, 4}},
         // {{1, 4, 4}},
         // {{1, 4, 4, 1}}
@@ -60,4 +60,4 @@ const auto multiply_params = ::testing::Combine(
         ::testing::Values(additional_config));
 
 INSTANTIATE_TEST_CASE_P(smoke_CompareWithRefs, EltwiseLayerTest, multiply_params, EltwiseLayerTest::getTestCaseName);
-}  // namespace
\ No newline at end of file
+}  // namespace