From f5d8063d0fae3590a1372843bf6abf3899be80f7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 9 Aug 2019 10:17:47 +0900 Subject: [PATCH] [locop] Introduce FormattedTensorShape (#6399) This commit introduces FormattedTensorShape class which dumps the content of a given TensorShape into std::ostream. Signed-off-by: Jonghyun Park --- .../locop/include/locop/FormattedTensorShape.h | 58 ++++++++++++++++++++++ compiler/locop/include/locop/Interfaces.h | 43 ++++++++++++++++ compiler/locop/src/FormattedTensorShape.cpp | 50 +++++++++++++++++++ compiler/locop/src/FormattedTensorShape.test.cpp | 33 ++++++++++++ compiler/locop/src/Interfaces.cpp | 28 +++++++++++ 5 files changed, 212 insertions(+) create mode 100644 compiler/locop/include/locop/FormattedTensorShape.h create mode 100644 compiler/locop/include/locop/Interfaces.h create mode 100644 compiler/locop/src/FormattedTensorShape.cpp create mode 100644 compiler/locop/src/FormattedTensorShape.test.cpp create mode 100644 compiler/locop/src/Interfaces.cpp diff --git a/compiler/locop/include/locop/FormattedTensorShape.h b/compiler/locop/include/locop/FormattedTensorShape.h new file mode 100644 index 0000000..cebd984 --- /dev/null +++ b/compiler/locop/include/locop/FormattedTensorShape.h @@ -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 + +namespace locop +{ + +enum class TensorShapeFormat +{ + // [ D_0 x D_1 x D_2 x ... ] + Bracket, +}; + +template class FormattedTensorShape; + +template <> +class FormattedTensorShape final : public Spec +{ +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 FormattedTensorShape fmt(loco::TensorShape *ptr) +{ + return FormattedTensorShape{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 index 0000000..0b4974d --- /dev/null +++ b/compiler/locop/include/locop/Interfaces.h @@ -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 + +namespace locop +{ + +enum class Interface +{ + Formatted, +}; + +template struct Spec; + +template <> struct Spec +{ + virtual ~Spec() = default; + + virtual void dump(std::ostream &os) const = 0; +}; + +std::ostream &operator<<(std::ostream &, const Spec &); + +} // namespace locop + +#endif // __LOCOP_INTERFACES_H__ diff --git a/compiler/locop/src/FormattedTensorShape.cpp b/compiler/locop/src/FormattedTensorShape.cpp new file mode 100644 index 0000000..2741dd7 --- /dev/null +++ b/compiler/locop/src/FormattedTensorShape.cpp @@ -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::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 index 0000000..0f0017a --- /dev/null +++ b/compiler/locop/src/FormattedTensorShape.test.cpp @@ -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 + +#include + +using namespace locop; + +TEST(FormattedTensorShapeTest, BracketFormat) +{ + auto tensor_shape = stdex::make_unique(); + + tensor_shape->rank(2); + tensor_shape->dim(0) = 4; + + std::cout << fmt(tensor_shape.get()) << std::endl; +} diff --git a/compiler/locop/src/Interfaces.cpp b/compiler/locop/src/Interfaces.cpp new file mode 100644 index 0000000..14e0211 --- /dev/null +++ b/compiler/locop/src/Interfaces.cpp @@ -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 &formatted) +{ + formatted.dump(os); + return os; +} + +} // namespace locop -- 2.7.4