Introduce asKernelShape into Convert of ncnn (#8772)
author장지섭/On-Device Lab(SR)/Engineer/삼성전자 <jiseob.jang@samsung.com>
Tue, 5 Nov 2019 09:17:45 +0000 (18:17 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Tue, 5 Nov 2019 09:17:45 +0000 (18:17 +0900)
This commit introduces asKernelShape into Convert of ncnn for converting operand of weights.

Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
runtime/neurun/backend/srcn/Convert.cc
runtime/neurun/backend/srcn/Convert.h

index bc79ddf..76beb38 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "Convert.h"
 
+#include <cassert>
 #include <cpp14/memory.h>
 #include <model/DataType.h>
 #include "Swizzle.h"
@@ -28,6 +29,40 @@ namespace backend
 namespace srcn
 {
 
+model::Shape asKernelShape(const model::Shape &shape, kernel::FilterLayout frontend_layout,
+                           kernel::FilterLayout backend_layout)
+{
+  assert(shape.rank() == 4);
+  if (frontend_layout == backend_layout)
+  {
+    return model::Shape{shape.dim(0), shape.dim(1), shape.dim(2), shape.dim(3)};
+  }
+  else if (frontend_layout == kernel::FilterLayout::OHWI &&
+           backend_layout == kernel::FilterLayout::HWOI)
+  {
+    return model::Shape{shape.dim(1), shape.dim(2), shape.dim(0), shape.dim(3)};
+  }
+  else if (frontend_layout == kernel::FilterLayout::OHWI &&
+           backend_layout == kernel::FilterLayout::OIHW)
+  {
+    return model::Shape{shape.dim(0), shape.dim(3), shape.dim(1), shape.dim(2)};
+  }
+  else if (frontend_layout == kernel::FilterLayout::OIHW &&
+           backend_layout == kernel::FilterLayout::HWOI)
+  {
+    return model::Shape{shape.dim(2), shape.dim(3), shape.dim(0), shape.dim(1)};
+  }
+  else if (frontend_layout == kernel::FilterLayout::OIHW &&
+           backend_layout == kernel::FilterLayout::OHWI)
+  {
+    return model::Shape{shape.dim(0), shape.dim(2), shape.dim(3), shape.dim(1)};
+  }
+  else
+  {
+    throw std::runtime_error("Not supported FilterLayout");
+  }
+}
+
 model::Shape asTensorShape(const model::Shape &shape, model::Layout frontend_layout,
                            model::Layout backend_layout)
 {
index caa6447..67c2d9b 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef __NEURUN_BACKEND_SRCN_CONVERT_H__
 #define __NEURUN_BACKEND_SRCN_CONVERT_H__
 
+#include "kernel/OperationUtils.h"
 #include <model/Layout.h>
 #include <model/Shape.h>
 #include <model/TypeInfo.h>
@@ -29,6 +30,9 @@ namespace backend
 namespace srcn
 {
 
+model::Shape asKernelShape(const model::Shape &shape, kernel::FilterLayout frontend_layout,
+                           kernel::FilterLayout backend_layout);
+
 model::Shape asTensorShape(const model::Shape &shape, model::Layout frontend_layout,
                            model::Layout backend_layout);