[locop] Introduce FormattedTensorShape (#6399)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 9 Aug 2019 01:17:47 +0000 (10:17 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 9 Aug 2019 01:17:47 +0000 (10:17 +0900)
This commit introduces FormattedTensorShape class which dumps the
content of a given TensorShape into std::ostream.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
compiler/locop/include/locop/FormattedTensorShape.h [new file with mode: 0644]
compiler/locop/include/locop/Interfaces.h [new file with mode: 0644]
compiler/locop/src/FormattedTensorShape.cpp [new file with mode: 0644]
compiler/locop/src/FormattedTensorShape.test.cpp [new file with mode: 0644]
compiler/locop/src/Interfaces.cpp [new file with mode: 0644]

diff --git a/compiler/locop/include/locop/FormattedTensorShape.h b/compiler/locop/include/locop/FormattedTensorShape.h
new file mode 100644 (file)
index 0000000..cebd984
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * 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.
+ */
+
+#ifndef __LOCOP_FORMATTED_TENSOR_SHAPE_H__
+#define __LOCOP_FORMATTED_TENSOR_SHAPE_H__
+
+#include "locop/Interfaces.h"
+
+#include <loco/IR/TensorShape.h>
+
+namespace locop
+{
+
+enum class TensorShapeFormat
+{
+  // [ D_0 x D_1 x D_2 x ... ]
+  Bracket,
+};
+
+template <TensorShapeFormat Format> class FormattedTensorShape;
+
+template <>
+class FormattedTensorShape<TensorShapeFormat::Bracket> final : public Spec<Interface::Formatted>
+{
+public:
+  FormattedTensorShape(const loco::TensorShape *ptr) : _ptr{ptr}
+  {
+    // DO NOTHING
+  }
+
+public:
+  void dump(std::ostream &os) const final;
+
+private:
+  const loco::TensorShape *_ptr = nullptr;
+};
+
+template <TensorShapeFormat F> FormattedTensorShape<F> fmt(loco::TensorShape *ptr)
+{
+  return FormattedTensorShape<F>{ptr};
+}
+
+} // namespace locop
+
+#endif // __LOCOP_FORMATTED_TENSOR_SHAPE_H__
diff --git a/compiler/locop/include/locop/Interfaces.h b/compiler/locop/include/locop/Interfaces.h
new file mode 100644 (file)
index 0000000..0b4974d
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * 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.
+ */
+
+#ifndef __LOCOP_INTERFACES_H__
+#define __LOCOP_INTERFACES_H__
+
+#include <ostream>
+
+namespace locop
+{
+
+enum class Interface
+{
+  Formatted,
+};
+
+template <Interface I> struct Spec;
+
+template <> struct Spec<Interface::Formatted>
+{
+  virtual ~Spec() = default;
+
+  virtual void dump(std::ostream &os) const = 0;
+};
+
+std::ostream &operator<<(std::ostream &, const Spec<Interface::Formatted> &);
+
+} // namespace locop
+
+#endif // __LOCOP_INTERFACES_H__
diff --git a/compiler/locop/src/FormattedTensorShape.cpp b/compiler/locop/src/FormattedTensorShape.cpp
new file mode 100644 (file)
index 0000000..2741dd7
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * 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.
+ */
+
+#include "locop/FormattedTensorShape.h"
+
+namespace loco
+{
+
+std::ostream &operator<<(std::ostream &os, const loco::Dimension &d)
+{
+  os << (d.known() ? std::to_string(d.value()) : std::string{"?"});
+  return os;
+}
+
+} // namespace
+
+namespace locop
+{
+
+void FormattedTensorShape<TensorShapeFormat::Bracket>::dump(std::ostream &os) const
+{
+  os << "[";
+
+  if (_ptr->rank() > 0)
+  {
+    os << " " << _ptr->dim(0);
+
+    for (uint32_t axis = 1; axis < _ptr->rank(); ++axis)
+    {
+      os << " x " << _ptr->dim(axis);
+    }
+  }
+
+  os << " ]";
+}
+
+} // namespace locop
diff --git a/compiler/locop/src/FormattedTensorShape.test.cpp b/compiler/locop/src/FormattedTensorShape.test.cpp
new file mode 100644 (file)
index 0000000..0f0017a
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * 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.
+ */
+
+#include "locop/FormattedTensorShape.h"
+
+#include <stdex/Memory.h>
+
+#include <gtest/gtest.h>
+
+using namespace locop;
+
+TEST(FormattedTensorShapeTest, BracketFormat)
+{
+  auto tensor_shape = stdex::make_unique<loco::TensorShape>();
+
+  tensor_shape->rank(2);
+  tensor_shape->dim(0) = 4;
+
+  std::cout << fmt<TensorShapeFormat::Bracket>(tensor_shape.get()) << std::endl;
+}
diff --git a/compiler/locop/src/Interfaces.cpp b/compiler/locop/src/Interfaces.cpp
new file mode 100644 (file)
index 0000000..14e0211
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * 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.
+ */
+
+#include "locop/Interfaces.h"
+
+namespace locop
+{
+
+std::ostream &operator<<(std::ostream &os, const Spec<Interface::Formatted> &formatted)
+{
+  formatted.dump(os);
+  return os;
+}
+
+} // namespace locop