diff options
Diffstat (limited to 'runtimes/neurun/src/model')
43 files changed, 2580 insertions, 0 deletions
diff --git a/runtimes/neurun/src/model/operand/Data.h b/runtimes/neurun/src/model/operand/Data.h new file mode 100644 index 000000000..506cb185a --- /dev/null +++ b/runtimes/neurun/src/model/operand/Data.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERAND_DATA_H__ +#define __NEURUN_MODEL_OPERAND_DATA_H__ + +#include <algorithm> + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +struct Data +{ + virtual ~Data() = default; + + virtual size_t size(void) const = 0; + virtual const uint8_t *base(void) const = 0; +}; + +class CachedData final : public Data +{ +public: + CachedData(const uint8_t *base, size_t size) : _base{new uint8_t[size]}, _size{size} + { + std::copy(base, base + size, _base); + } + +public: + ~CachedData() { delete[] _base; } + +public: + size_t size(void) const override { return _size; } + const uint8_t *base(void) const override { return _base; } + +private: + uint8_t *_base; + size_t _size; +}; + +class ExternalData final : public Data +{ +public: + ExternalData(const uint8_t *base, size_t size) : _base{base}, _size{size} + { + // DO NOTHING + } + +public: + size_t size(void) const override { return _size; } + const uint8_t *base(void) const override { return _base; } + +private: + const uint8_t *_base; + const size_t _size; +}; + +} // namespace operand +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERAND_DATA_H__ diff --git a/runtimes/neurun/src/model/operand/DataType.h b/runtimes/neurun/src/model/operand/DataType.h new file mode 100644 index 000000000..d75a0dbf1 --- /dev/null +++ b/runtimes/neurun/src/model/operand/DataType.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERAND_DATATYPE_H__ +#define __NEURUN_MODEL_OPERAND_DATATYPE_H__ + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +enum class DataType +{ + SCALAR_FLOAT32 = 0, + SCALAR_INT32 = 1, + SCALAR_UINT32 = 2, + + TENSOR_FLOAT32 = 3, + TENSOR_INT32 = 4, + + TENSOR_QUANT8_ASYMM = 5, +}; + +} // namespace operand +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERAND_DATATYPE_H__ diff --git a/runtimes/neurun/src/model/operand/Index.h b/runtimes/neurun/src/model/operand/Index.h new file mode 100644 index 000000000..1c84ba451 --- /dev/null +++ b/runtimes/neurun/src/model/operand/Index.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERAND_INDEX_H__ +#define __NEURUN_MODEL_OPERAND_INDEX_H__ + +#include "graph/Index.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +using Index = ::neurun::graph::Index<uint32_t, struct IndexTag>; + +} // namespace operand +} // namespace model +} // namespace neurun + +namespace neurun +{ +namespace model +{ +namespace operand +{ +namespace IO +{ + +using Index = ::neurun::graph::Index<uint32_t, struct IndexTag>; + +} // namespace IO +} // namespace operand +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERAND_INDEX_H__ diff --git a/runtimes/neurun/src/model/operand/IndexSet.cc b/runtimes/neurun/src/model/operand/IndexSet.cc new file mode 100644 index 000000000..b83d314e4 --- /dev/null +++ b/runtimes/neurun/src/model/operand/IndexSet.cc @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018 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 "IndexSet.h" + +#include <algorithm> + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +IndexSet::IndexSet(std::initializer_list<Index> list) : _set(list) +{ + // DO NOTHING +} + +IndexSet::IndexSet(std::initializer_list<int32_t> list) +{ + for (auto val : list) + { + _set.emplace_back(static_cast<uint32_t>(val)); + } +} + +IndexSet::IndexSet(std::initializer_list<uint32_t> list) +{ + for (auto val : list) + { + _set.emplace_back(val); + } +} + +bool IndexSet::contains(const Index &index) const +{ + return std::find(_set.begin(), _set.end(), index) != _set.end(); +} + +void IndexSet::replace(const Index &from, const Index &to) +{ + std::replace(_set.begin(), _set.end(), from, to); +} + +} // namespace operand +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/IndexSet.h b/runtimes/neurun/src/model/operand/IndexSet.h new file mode 100644 index 000000000..e8827de9c --- /dev/null +++ b/runtimes/neurun/src/model/operand/IndexSet.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERAND_INDEX_SET_H__ +#define __NEURUN_MODEL_OPERAND_INDEX_SET_H__ + +#include <initializer_list> +#include <vector> + +#include "Index.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +class IndexSet +{ +public: + IndexSet(void) = default; + IndexSet(std::initializer_list<Index> list); + IndexSet(std::initializer_list<int32_t> list); + IndexSet(std::initializer_list<uint32_t> list); + +public: + void append(const Index &index) { _set.emplace_back(index); } + +public: + uint32_t size() const { return static_cast<uint32_t>(_set.size()); } + const Index &at(IO::Index set_index) const { return _set.at(set_index.asInt()); } + const Index &at(uint32_t index) const { return _set.at(index); } + bool contains(const Index &index) const; + void replace(const Index &from, const Index &to); + +public: + std::vector<Index>::const_iterator begin(void) const { return _set.begin(); } + std::vector<Index>::const_iterator end(void) const { return _set.end(); } + +private: + std::vector<Index> _set; +}; + +} // namespace operand +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERAND_INDEX_SET_H__ diff --git a/runtimes/neurun/src/model/operand/Object.cc b/runtimes/neurun/src/model/operand/Object.cc new file mode 100644 index 000000000..63cf29bd3 --- /dev/null +++ b/runtimes/neurun/src/model/operand/Object.cc @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2018 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 "Object.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +size_t Object::operandSize(void) const +{ + const uint32_t ranks = _shape.rank(); + int32_t elements = 1; + + for (uint32_t rank = 0; rank < ranks; rank++) + { + elements *= _shape.dim(rank); + } + + DataType type = _type.type(); + size_t element_size = 0; + + // Value of type is matched with OperandCode enum in NeuralNetworks.h + switch (type) + { + case DataType::SCALAR_FLOAT32: + case DataType::TENSOR_FLOAT32: + element_size = sizeof(float); + break; + case DataType::SCALAR_INT32: + case DataType::TENSOR_INT32: + element_size = sizeof(int32_t); + break; + case DataType::SCALAR_UINT32: + element_size = sizeof(uint32_t); + break; + case DataType::TENSOR_QUANT8_ASYMM: + element_size = sizeof(uint8_t); + break; + default: + throw std::runtime_error{"Unsuppported type size"}; + } + + return element_size * elements; +} + +bool Object::setUsage(const OperandUsage usage) +{ + if (usageIsDefined() && (_usage != usage)) + { + // Already set as different type + return false; + } + + _usage = usage; + + return true; +} + +void Object::appendUse(const ::neurun::model::operation::Index &idx) +{ + assert(_usage != OperandUsage::NOT_DEFINED); + assert(!_uses.contains(idx)); + + _uses.append(idx); +} + +void Object::removeUse(const ::neurun::model::operation::Index &idx) +{ + assert(_usage != OperandUsage::NOT_DEFINED); + assert(_uses.contains(idx)); + + _uses.remove(idx); +} + +void Object::appendDef(const ::neurun::model::operation::Index &idx) +{ + assert(_usage != OperandUsage::NOT_DEFINED && _usage != OperandUsage::CONSTANT); + assert(_def.size() == 0); + + _def.append(idx); +} + +void Object::removeDef(const ::neurun::model::operation::Index &idx) +{ + assert(_usage != OperandUsage::NOT_DEFINED); + assert(_def.contains(idx)); + + _def.remove(idx); +} + +void Object::lower_info(std::unique_ptr<graph::operand::LowerInfo> &&lower_info) +{ + _lower_info = std::move(lower_info); +} + +const graph::operand::LowerInfo *Object::lower_info() const { return _lower_info.get(); } + +graph::operand::LowerInfo *Object::lower_info() { return _lower_info.get(); } + +void Object::parent_info(std::unique_ptr<graph::operand::ParentInfo> &&parent_info) +{ + _parent_info = std::move(parent_info); +} + +const graph::operand::ParentInfo *Object::parent_info() const { return _parent_info.get(); } + +graph::operand::ParentInfo *Object::parent_info() { return _parent_info.get(); } + +} // namespace operand +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/Object.h b/runtimes/neurun/src/model/operand/Object.h new file mode 100644 index 000000000..eb5f6275e --- /dev/null +++ b/runtimes/neurun/src/model/operand/Object.h @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERAND_OBJECT_H__ +#define __NEURUN_MODEL_OPERAND_OBJECT_H__ + +#include <cassert> +#include <cstdint> +#include <memory> +#include <algorithm> + +#include "Shape.h" +#include "Data.h" +#include "TypeInfo.h" +#include "graph/operand/LowerInfo.h" // TODO Remove this dependency +#include "graph/operand/ParentInfo.h" // TODO Remove this dependency +#include "model/operation/IndexList.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +// Operand usage should be exact one of these +enum class OperandUsage +{ + NOT_DEFINED, + MODEL_INPUT, + CONSTANT, + OPERATION_OUTPUT, +}; + +class Object +{ +public: + explicit Object(const Shape &shape, const TypeInfo &type) + : _shape{shape}, _type{type}, _usage{OperandUsage::NOT_DEFINED} + { + // DO NOTHING + } + +public: + const Shape &shape(void) const { return _shape; } + const TypeInfo &typeInfo(void) const { return _type; } + size_t operandSize(void) const; + bool setAsConstant() { return setUsage(OperandUsage::CONSTANT); } + bool setAsModelInput() { return setUsage(OperandUsage::MODEL_INPUT); } + bool setAsOperationOutput() { return setUsage(OperandUsage::OPERATION_OUTPUT); } + bool usageIsDefined(void) const { return _usage != OperandUsage::NOT_DEFINED; } + bool isModelInput(void) const { return _usage == OperandUsage::MODEL_INPUT; } + OperandUsage getUsage() const { return _usage; } + + const operation::IndexList &getUses() const { return _uses; } + const operation::IndexList &getDef() const { return _def; } + void appendUse(const operation::Index &idx); + void removeUse(const operation::Index &idx); + void appendDef(const operation::Index &idx); + void removeDef(const operation::Index &idx); + +private: + bool setUsage(OperandUsage usage); + +public: + void data(std::unique_ptr<Data> &&data) { _data = std::move(data); } + const Data &data(void) const { return *_data; } + +public: + template <typename T, typename... Args> void data(Args &&... args) + { + data(std::unique_ptr<T>(new T{std::forward<Args>(args)...})); + } + +public: + template <typename T> T asScalar(void) const + { + assert((_shape.rank() == 0) || ((_shape.rank() == 1) && (_shape.dim(0) == 1))); + assert(_data != nullptr); + assert((_data->base() != nullptr) && (_data->size() == sizeof(T))); + + return *(reinterpret_cast<const T *>(_data->base())); + } + +public: + void lower_info(std::unique_ptr<graph::operand::LowerInfo> &&lower_info); + const graph::operand::LowerInfo *lower_info() const; + graph::operand::LowerInfo *lower_info(); + /** + * @brief Set parent information + * @param[in] parent_info Parent information + */ + void parent_info(std::unique_ptr<graph::operand::ParentInfo> &&parent_info); + /** + * @brief Return parent information pointer as constant + * @return Parent information pointer + */ + const graph::operand::ParentInfo *parent_info() const; + /** + * @brief Return parent information pointer + * @return Perent information pointer + */ + graph::operand::ParentInfo *parent_info(); + +private: + const Shape _shape; + const TypeInfo _type; + std::unique_ptr<Data> _data; + OperandUsage _usage; + + operation::IndexList _uses; + operation::IndexList _def; // size is 0 (constant) or 1 (from def operation) + + std::unique_ptr<graph::operand::LowerInfo> _lower_info; + std::unique_ptr<graph::operand::ParentInfo> _parent_info; +}; + +} // namespace operand +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERAND_OBJECT_H__ diff --git a/runtimes/neurun/src/model/operand/Set.cc b/runtimes/neurun/src/model/operand/Set.cc new file mode 100644 index 000000000..d93c21514 --- /dev/null +++ b/runtimes/neurun/src/model/operand/Set.cc @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2018 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 "Set.h" + +#include "cpp14/memory.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +const Index Set::generateIndex() +{ + assert((_index_count) <= 0x7fffffff); + + return Index{_index_count++}; +} + +Index Set::append(const Shape &shape, const TypeInfo &type) +{ + auto index = generateIndex(); + + _objects[index] = nnfw::cpp14::make_unique<Object>(shape, type); + + return index; +} + +const Object &Set::at(const Index &index) const { return *(_objects.at(index)); } + +Object &Set::at(const Index &index) { return *(_objects.at(index)); } + +bool Set::exist(const Index &index) const { return index.value() < _objects.size(); } + +void Set::iterate(const std::function<void(const Index &, const Object &)> &fn) const +{ + for (const auto &e : _objects) + { + fn(e.first, *e.second); + } +} + +void Set::iterate(const std::function<void(const Index &, Object &)> &fn) +{ + // TODO Remove this workaround + // This implementation is a workaround in case of adding operands while iteration + // + // // Original Implementation (We probably should be back to this) + // for (auto &e : _objects) + // { + // fn(e.first, *e.second); + // } + + std::list<Index> l; + + for (auto &e : _objects) + { + l.push_back(e.first); + } + + for (auto index : l) + { + fn(index, *_objects[index]); + } +} + +} // namespace operand +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/Set.h b/runtimes/neurun/src/model/operand/Set.h new file mode 100644 index 000000000..9dff7ec3c --- /dev/null +++ b/runtimes/neurun/src/model/operand/Set.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERAND_SET_H__ +#define __NEURUN_MODEL_OPERAND_SET_H__ + +#include <memory> +#include <unordered_map> + +#include "Object.h" +#include "Index.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +class Set +{ +public: + Set() : _index_count(0) {} + +public: + Index append(const Shape &, const TypeInfo &); + void remove(const Index &index) { _objects.erase(index); }; + +public: + const Object &at(const Index &) const; + Object &at(const Index &); + bool exist(const Index &) const; + void iterate(const std::function<void(const Index &, const Object &)> &fn) const; + void iterate(const std::function<void(const Index &, Object &)> &fn); + +private: + const Index generateIndex(); + +private: + std::unordered_map<Index, std::unique_ptr<Object>> _objects; + uint32_t _index_count; +}; + +} // namespace operand +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERAND_SET_H__ diff --git a/runtimes/neurun/src/model/operand/Shape.cc b/runtimes/neurun/src/model/operand/Shape.cc new file mode 100644 index 000000000..f74c48d88 --- /dev/null +++ b/runtimes/neurun/src/model/operand/Shape.cc @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2018 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 <cassert> + +#include "Shape.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +Shape::Shape(uint32_t rank) { _dims.resize(rank); } + +int32_t Shape::asVector(void) const +{ + assert(rank() == 1); + + return dim(0); +} + +nnfw::misc::matrix::Shape Shape::asMatrix(void) const +{ + assert(rank() == 2); + + const auto height = dim(0); + const auto width = dim(1); + + return nnfw::misc::matrix::Shape(height, width); +} + +nnfw::misc::feature::Shape Shape::asFeature(void) const +{ + assert(rank() == 4); + + // Feature Map in NNAPI + // - Dimension(0) -> Batch + // - Dimension(1) -> Height + // - Dimension(2) -> Width + // - Dimension(3) -> Depth + const auto batch = dim(0); + const auto depth = dim(3); + const auto height = dim(1); + const auto width = dim(2); + + return nnfw::misc::feature::Shape(batch, depth, height, width); +} + +nnfw::misc::kernel::Shape Shape::asKernel(void) const +{ + assert(rank() == 4); + + // Convolution Kernel in NNAPI + // - Dimension(0) -> Count + // - Dimension(1) -> Height + // - Dimension(2) -> Width + // - Dimension(3) -> Depth + const auto count = dim(0); + const auto depth = dim(3); + const auto height = dim(1); + const auto width = dim(2); + + return nnfw::misc::kernel::Shape(count, depth, height, width); +} + +nnfw::misc::tensor::Shape Shape::asTensor(void) const +{ + nnfw::misc::tensor::Shape shape{}; + for (uint32_t i = 0; i < rank(); ++i) + { + shape.append(dim(i)); + } + + return shape; // this shape represents shape of NNAPI +} + +} // namespace operand +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/Shape.h b/runtimes/neurun/src/model/operand/Shape.h new file mode 100644 index 000000000..b80f647d5 --- /dev/null +++ b/runtimes/neurun/src/model/operand/Shape.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERAND_SHAPE_H__ +#define __NEURUN_MODEL_OPERAND_SHAPE_H__ + +#include <vector> +#include <cstdint> + +#include "misc/feature/Shape.h" +#include "misc/kernel/Shape.h" +#include "misc/matrix/Shape.h" +#include "misc/tensor/Shape.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +struct Shape +{ +public: + Shape(uint32_t rank = 0); + +public: + uint32_t rank(void) const { return _dims.size(); } + +public: + int32_t dim(uint32_t n) const { return _dims.at(n); } + int32_t &dim(uint32_t n) { return _dims.at(n); } + const std::vector<int32_t> &dims() const { return _dims; } + +public: + int32_t asVector(void) const; + nnfw::misc::matrix::Shape asMatrix(void) const; + nnfw::misc::feature::Shape asFeature(void) const; + nnfw::misc::kernel::Shape asKernel(void) const; + nnfw::misc::tensor::Shape asTensor(void) const; + +private: + std::vector<int32_t> _dims; +}; + +} // namespace operand +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERAND_SHAPE_H__ diff --git a/runtimes/neurun/src/model/operand/TypeInfo.cc b/runtimes/neurun/src/model/operand/TypeInfo.cc new file mode 100644 index 000000000..0b9f63c93 --- /dev/null +++ b/runtimes/neurun/src/model/operand/TypeInfo.cc @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018 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 "TypeInfo.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +DataType TypeInfo::typeFromOperandCode(OperandCode type) +{ + // Now neurun::model::operand::DataType share same enum value with OperandCode + // in NeuralNetworks.h. + return static_cast<DataType>(static_cast<uint32_t>(type)); +} + +} // namespace operand +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/TypeInfo.h b/runtimes/neurun/src/model/operand/TypeInfo.h new file mode 100644 index 000000000..d16172a09 --- /dev/null +++ b/runtimes/neurun/src/model/operand/TypeInfo.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERAND_TYPEINFO_H__ +#define __NEURUN_MODEL_OPERAND_TYPEINFO_H__ + +#include <cstdint> + +#include <NeuralNetworks.h> + +#include "DataType.h" + +namespace neurun +{ +namespace model +{ +namespace operand +{ + +class TypeInfo +{ +public: + TypeInfo() = default; + + TypeInfo(OperandCode type, float scale, int32_t offset) + : _type(typeFromOperandCode(type)), _scale(scale), _offset(offset) + { + // DO NOTHING + } + +public: + DataType type() const { return _type; } + float scale() const { return _scale; } + int32_t offset() const { return _offset; } + +private: + // Now neurun::model::operand::DataType share same enum value with OperandCode + // in NeuralNetworks.h. + // If we don't share same value, we must fix this mapping function. + DataType typeFromOperandCode(OperandCode type); + +private: + DataType _type; + float _scale; + int32_t _offset; +}; +} // namespace operand +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERAND_TYPEINFO_H__ diff --git a/runtimes/neurun/src/model/operation/AddNode.cc b/runtimes/neurun/src/model/operation/AddNode.cc new file mode 100644 index 000000000..0c9d4e09b --- /dev/null +++ b/runtimes/neurun/src/model/operation/AddNode.cc @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018 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 "AddNode.h" + +#include <cassert> + +#include "NodeVisitor.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +void AddNode::accept(NodeVisitor &&v) const { v.visit(*this); } + +AddNode::AddNode(const model::operation::Node::InitParam &init_param) + : model::operation::Node{OperandConstraint::createExact(2u)} +{ + assert(init_param.input_count == 2); + assert(init_param.output_count == 1); + + // Each input should be interpreted as follows: + // + // 0 -> Lefthand side operand + // 1 -> Righthand side operand + + setInputs({init_param.inputs[0], init_param.inputs[1]}); + setOutputs({init_param.outputs[0]}); +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/AddNode.h b/runtimes/neurun/src/model/operation/AddNode.h new file mode 100644 index 000000000..533fb0ab3 --- /dev/null +++ b/runtimes/neurun/src/model/operation/AddNode.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_ADD_NODE_H__ +#define __NEURUN_MODEL_OPERATION_ADD_NODE_H__ + +#include "model/operation/Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class AddNode : public model::operation::Node +{ +public: + AddNode(const model::operation::Node::InitParam &init_param); + + enum Input + { + LHS = 0, + RHS + }; + + struct Param + { + operand::Index activation_index; + }; + +public: + virtual void accept(NodeVisitor &&) const override; + virtual std::string getName() const override { return "Add"; } +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_ADD_H__ diff --git a/runtimes/neurun/src/model/operation/AvgPool2DNode.cc b/runtimes/neurun/src/model/operation/AvgPool2DNode.cc new file mode 100644 index 000000000..8c688e60a --- /dev/null +++ b/runtimes/neurun/src/model/operation/AvgPool2DNode.cc @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2018 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 "AvgPool2DNode.h" + +#include <cassert> + +#include "NodeVisitor.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +void AvgPool2DNode::accept(NodeVisitor &&v) const { v.visit(*this); } + +AvgPool2DNode::AvgPool2DNode(const model::operation::Node::InitParam &init_param) + : model::operation::Node{OperandConstraint::createExact(1u)} +{ + assert(init_param.input_count == 7); + assert(init_param.output_count == 1); + + // Each input should be interpreted as follows: + // + // 0 -> IFM Tensor Index + // 1 -> Padding Code (ANEURALNETWORKS_PADDING_SAME or ANEURALNETWORKS_PADDING_VALID) Index + // 2 -> Horizontal (over width) Stride Index + // 3 -> Vertial (over height) Stride Index + // 4 -> Filter Width Index + // 5 -> Filter Height Index + // 6 -> FuseCode (activation) Index + + setInputs({init_param.inputs[0]}); + setOutputs({init_param.outputs[0]}); + + _param.padding_index = operand::Index{init_param.inputs[1]}; + _param.hstride_index = operand::Index{init_param.inputs[2]}; + _param.vstride_index = operand::Index{init_param.inputs[3]}; + + _param.kw_index = operand::Index{init_param.inputs[4]}; + _param.kh_index = operand::Index{init_param.inputs[5]}; + _param.activation_index = operand::Index{init_param.inputs[6]}; +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/AvgPool2DNode.h b/runtimes/neurun/src/model/operation/AvgPool2DNode.h new file mode 100644 index 000000000..e66e6146e --- /dev/null +++ b/runtimes/neurun/src/model/operation/AvgPool2DNode.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_AVGPOOL2D_NODE_H__ +#define __NEURUN_MODEL_OPERATION_AVGPOOL2D_NODE_H__ + +#include <memory> + +#include "model/operation/Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class AvgPool2DNode : public model::operation::Node +{ +public: + AvgPool2DNode(const model::operation::Node::InitParam &init_param); + + enum Input + { + INPUT = 0 + }; + + struct Param + { + operand::Index kw_index; + operand::Index kh_index; + + operand::Index hstride_index; + operand::Index vstride_index; + + operand::Index padding_index; + operand::Index activation_index; + }; + +public: + virtual void accept(NodeVisitor &&) const override; + virtual std::string getName() const override { return "AvgPool2D"; } + +public: + const Param ¶m() const { return _param; } + +private: + Param _param; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_AVGPOOL2D_H__ diff --git a/runtimes/neurun/src/model/operation/ConcatNode.cc b/runtimes/neurun/src/model/operation/ConcatNode.cc new file mode 100644 index 000000000..23cfef294 --- /dev/null +++ b/runtimes/neurun/src/model/operation/ConcatNode.cc @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2018 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 "ConcatNode.h" + +#include <cassert> + +#include "NodeVisitor.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +void ConcatNode::accept(NodeVisitor &&v) const { v.visit(*this); } + +ConcatNode::ConcatNode(const model::operation::Node::InitParam &init_param) + : model::operation::Node{OperandConstraint::createAtLeast(2u)} +{ + assert(init_param.input_count >= 2); // At least one one input tensor and axis + assert(init_param.output_count == 1); + + // When there are N + 1 inputs, each input should be interpreted as follows: + // + // [0, N) -> Input tensors + // N -> Axis + // + + { + operand::IndexSet inds; + for (uint32_t n = 0; n < init_param.input_count - 1; ++n) + { + inds.append(operand::Index{init_param.inputs[n]}); + } + setInputs(inds); + } + setOutputs({init_param.outputs[0]}); + + _param.axis_index = operand::Index{init_param.inputs[init_param.input_count - 1]}; +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/ConcatNode.h b/runtimes/neurun/src/model/operation/ConcatNode.h new file mode 100644 index 000000000..b69ee2f23 --- /dev/null +++ b/runtimes/neurun/src/model/operation/ConcatNode.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_CONCAT_NODE_H__ +#define __NEURUN_MODEL_OPERATION_CONCAT_NODE_H__ + +#include <memory> + +#include "model/operation/Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class ConcatNode : public model::operation::Node +{ +public: + ConcatNode(const model::operation::Node::InitParam &init_param); + + struct Param + { + operand::Index axis_index; + }; + +public: + virtual void accept(NodeVisitor &&) const override; + virtual std::string getName() const override { return "Concat"; } + +public: + const Param ¶m() const { return _param; } + +private: + Param _param; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_CONCAT_H__ diff --git a/runtimes/neurun/src/model/operation/Conv2DNode.cc b/runtimes/neurun/src/model/operation/Conv2DNode.cc new file mode 100644 index 000000000..7eb2b183d --- /dev/null +++ b/runtimes/neurun/src/model/operation/Conv2DNode.cc @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2018 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 "Conv2DNode.h" + +#include <cassert> + +#include "NodeVisitor.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +void Conv2DNode::accept(NodeVisitor &&v) const { v.visit(*this); } + +Conv2DNode::Conv2DNode(const model::operation::Node::InitParam &init_param) + : model::operation::Node{OperandConstraint::createExact(3u)} +{ + assert(init_param.input_count == 7 && init_param.output_count == 1); + + // Each input should be interpreted as follows: + // + // + // 0 -> IFM Tensor Index + // 1 -> Kernel Tensor Index + // 2 -> Bias Tensor Index + // 3 -> Padding Code (ANEURALNETWORKS_PADDING_SAME or ANEURALNETWORKS_PADDING_VALID) Index + // 4 -> Stride (width) Index + // 5 -> Stride (height) INdex + // 6 -> Activation Index + + setInputs({init_param.inputs[0], init_param.inputs[1], init_param.inputs[2]}); + setOutputs({init_param.outputs[0]}); + + _param.padding_index = operand::Index{init_param.inputs[3]}; + _param.hstride_index = operand::Index{init_param.inputs[4]}; + _param.vstride_index = operand::Index{init_param.inputs[5]}; + _param.activation_index = operand::Index{init_param.inputs[6]}; +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/Conv2DNode.h b/runtimes/neurun/src/model/operation/Conv2DNode.h new file mode 100644 index 000000000..34a95f0d9 --- /dev/null +++ b/runtimes/neurun/src/model/operation/Conv2DNode.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_CONV2D_NODE_H__ +#define __NEURUN_MODEL_OPERATION_CONV2D_NODE_H__ + +#include <memory> + +#include "model/operation/Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class Conv2DNode : public model::operation::Node +{ +public: + Conv2DNode(const model::operation::Node::InitParam &); + + enum Input + { + INPUT = 0, + KERNEL, + BIAS + }; + + struct Param + { + operand::Index hstride_index; + operand::Index vstride_index; + + operand::Index padding_index; + operand::Index activation_index; + }; + +public: + virtual void accept(NodeVisitor &&) const override; + virtual std::string getName() const override { return "Conv2D"; } + +public: + const Param ¶m() const { return _param; } + +private: + Param _param; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_CONV2D_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/FullyConnectedNode.cc b/runtimes/neurun/src/model/operation/FullyConnectedNode.cc new file mode 100644 index 000000000..0fde5182d --- /dev/null +++ b/runtimes/neurun/src/model/operation/FullyConnectedNode.cc @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018 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 "FullyConnectedNode.h" + +#include <cassert> + +#include "NodeVisitor.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +void FullyConnectedNode::accept(NodeVisitor &&v) const { v.visit(*this); } + +FullyConnectedNode::FullyConnectedNode(const model::operation::Node::InitParam &init_param) + : model::operation::Node{OperandConstraint::createExact(3u)} +{ + assert(init_param.input_count == 4 && init_param.output_count == 1); + + // Each input should be interpreted as follows: + // + // 0 -> A tensor, specifying the input. + // 1 -> A 2-D tensor, specifying the weights + // 2 -> A 1-D tensor, specifying the bias + // 3 -> An INT32 value, and has to be one of the FuseCode values + + setInputs({init_param.inputs[0], init_param.inputs[1], init_param.inputs[2]}); + setOutputs({init_param.outputs[0]}); + + _param.activation_index = operand::Index{init_param.inputs[3]}; +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/FullyConnectedNode.h b/runtimes/neurun/src/model/operation/FullyConnectedNode.h new file mode 100644 index 000000000..9820ddc8c --- /dev/null +++ b/runtimes/neurun/src/model/operation/FullyConnectedNode.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_FULLYCONNECTED_NODE_H__ +#define __NEURUN_MODEL_OPERATION_FULLYCONNECTED_NODE_H__ + +#include <memory> + +#include "model/operation/Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class FullyConnectedNode : public model::operation::Node +{ +public: + FullyConnectedNode(const model::operation::Node::InitParam &init_param); + + enum Input + { + INPUT = 0, + WEIGHT, + BIAS + }; + + struct Param + { + operand::Index activation_index; + }; + +public: + virtual void accept(NodeVisitor &&) const override; + virtual std::string getName() const override { return "FullyConnected"; } + +public: + const Param ¶m() const { return _param; } + +private: + Param _param; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_FULLYCONNECTED_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/Index.h b/runtimes/neurun/src/model/operation/Index.h new file mode 100644 index 000000000..e03dd74d6 --- /dev/null +++ b/runtimes/neurun/src/model/operation/Index.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_INDEX_H__ +#define __NEURUN_MODEL_OPERATION_INDEX_H__ + +#include "graph/Index.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +using Index = ::neurun::graph::Index<uint32_t, struct IndexTag>; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_INDEX_H__ diff --git a/runtimes/neurun/src/model/operation/IndexList.cc b/runtimes/neurun/src/model/operation/IndexList.cc new file mode 100644 index 000000000..e46987036 --- /dev/null +++ b/runtimes/neurun/src/model/operation/IndexList.cc @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2018 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 "IndexList.h" + +#include <algorithm> + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +IndexList::IndexList(std::initializer_list<Index> list) : _list(list) +{ + // DO NOTHING +} + +bool IndexList::contains(const ::neurun::model::operation::Index &index) const +{ + return std::find(_list.begin(), _list.end(), index) != _list.end(); +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/IndexList.h b/runtimes/neurun/src/model/operation/IndexList.h new file mode 100644 index 000000000..c0af29829 --- /dev/null +++ b/runtimes/neurun/src/model/operation/IndexList.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_INDEX_LIST_H__ +#define __NEURUN_MODEL_OPERATION_INDEX_LIST_H__ + +#include <initializer_list> +#include <list> + +#include "Index.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class IndexList +{ +public: + IndexList(void) = default; + IndexList(std::initializer_list<Index> list); + +public: + void append(const Index &index) { _list.push_back(index); } + void remove(const Index &index) { _list.remove(index); } + +public: + uint32_t size() const { return static_cast<uint32_t>(_list.size()); } + const std::list<Index> &list() const { return _list; } + bool contains(const Index &index) const; + +private: + std::list<Index> _list; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_INDEX_LIST_H__ diff --git a/runtimes/neurun/src/model/operation/MaxPool2DNode.cc b/runtimes/neurun/src/model/operation/MaxPool2DNode.cc new file mode 100644 index 000000000..3d3686b0e --- /dev/null +++ b/runtimes/neurun/src/model/operation/MaxPool2DNode.cc @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2018 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 "MaxPool2DNode.h" + +#include <cassert> + +#include "NodeVisitor.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +void MaxPool2DNode::accept(NodeVisitor &&v) const { v.visit(*this); } + +MaxPool2DNode::MaxPool2DNode(const model::operation::Node::InitParam &init_param) + : model::operation::Node{OperandConstraint::createExact(1u)} +{ + assert(init_param.input_count == 7); + assert(init_param.output_count == 1); + + // Each input should be interpreted as follows: + // + // 0 -> IFM Tensor Index + // 1 -> Padding Code (ANEURALNETWORKS_PADDING_SAME or ANEURALNETWORKS_PADDING_VALID) Index + // 2 -> Horizontal (over width) Stride Index + // 3 -> Vertial (over height) Stride Index + // 4 -> Filter Width Index + // 5 -> Filter Height Index + // 6 -> FuseCode (activation) Index + + setInputs({init_param.inputs[0]}); + setOutputs({init_param.outputs[0]}); + + _param.padding_index = operand::Index{init_param.inputs[1]}; + _param.hstride_index = operand::Index{init_param.inputs[2]}; + _param.vstride_index = operand::Index{init_param.inputs[3]}; + + _param.kw_index = operand::Index{init_param.inputs[4]}; + _param.kh_index = operand::Index{init_param.inputs[5]}; + _param.activation_index = operand::Index{init_param.inputs[6]}; +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/MaxPool2DNode.h b/runtimes/neurun/src/model/operation/MaxPool2DNode.h new file mode 100644 index 000000000..96d1210a7 --- /dev/null +++ b/runtimes/neurun/src/model/operation/MaxPool2DNode.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_MAXPOOL2D_NODE_H__ +#define __NEURUN_MODEL_OPERATION_MAXPOOL2D_NODE_H__ + +#include <memory> + +#include "model/operation/Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class MaxPool2DNode : public model::operation::Node +{ +public: + MaxPool2DNode(const model::operation::Node::InitParam &init_param); + + enum Input + { + INPUT = 0 + }; + + struct Param + { + operand::Index kw_index; + operand::Index kh_index; + + operand::Index hstride_index; + operand::Index vstride_index; + + operand::Index padding_index; + operand::Index activation_index; + }; + +public: + virtual void accept(NodeVisitor &&) const override; + virtual std::string getName() const override { return "MaxPool2D"; } + +public: + const Param ¶m() const { return _param; } + +private: + Param _param; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_MAXPOOL2D_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/Node.Include.h b/runtimes/neurun/src/model/operation/Node.Include.h new file mode 100644 index 000000000..95e78c7b5 --- /dev/null +++ b/runtimes/neurun/src/model/operation/Node.Include.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018 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. + */ + +// This file has no ifdef guard intentionally + +#include "Conv2DNode.h" +#include "MaxPool2DNode.h" +#include "AvgPool2DNode.h" +#include "ConcatNode.h" +#include "ReshapeNode.h" +#include "FullyConnectedNode.h" +#include "SoftmaxNode.h" +#include "PermuteNode.h" +#include "AddNode.h" diff --git a/runtimes/neurun/src/model/operation/Node.cc b/runtimes/neurun/src/model/operation/Node.cc new file mode 100644 index 000000000..76397afde --- /dev/null +++ b/runtimes/neurun/src/model/operation/Node.cc @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2018 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 "Node.h" + +#include <cassert> + +#include "graph/operation/LowerInfo.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +Node::Node(OperandConstraint input_constr) : _input_constr{input_constr} {} + +Node::~Node() = default; + +void Node::setInputs(const operand::IndexSet &indexes) +{ + assert(_input_constr.check(indexes.size())); + _inputs = indexes; +} + +void Node::setOutputs(const operand::IndexSet &indexes) { _outputs = indexes; } + +void Node::replaceInput(const operand::Index &from, const operand::Index &to) +{ + _inputs.replace(from, to); +} + +void Node::replaceOutput(const operand::Index &from, const operand::Index &to) +{ + _outputs.replace(from, to); +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/Node.h b/runtimes/neurun/src/model/operation/Node.h new file mode 100644 index 000000000..76f0d2d00 --- /dev/null +++ b/runtimes/neurun/src/model/operation/Node.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_NODE_H__ +#define __NEURUN_MODEL_OPERATION_NODE_H__ + +#include <memory> + +#include "model/operand/Object.h" +#include "model/operand/IndexSet.h" +#include "OperandConstraint.h" + +namespace neurun +{ +namespace graph +{ +namespace operation +{ +class LowerInfo; +} // namespace operation +} // namespace graph +} // namespace neurun + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +struct NodeVisitor; + +class Node +{ +public: + struct InitParam + { + uint32_t input_count; + const uint32_t *inputs; + uint32_t output_count; + const uint32_t *outputs; + }; + +public: + Node(OperandConstraint input_constr); + virtual ~Node(); + +public: + virtual void accept(NodeVisitor &&) const = 0; + virtual std::string getName() const = 0; + +public: + void replaceInput(const operand::Index &from, const operand::Index &to); + void replaceOutput(const operand::Index &from, const operand::Index &to); + const operand::IndexSet &getInputs() const { return _inputs; } + const operand::IndexSet &getOutputs() const { return _outputs; } + // It's for only input/output tensors but const data. + void setInputs(const operand::IndexSet &indexes); + void setOutputs(const operand::IndexSet &indexes); + +private: + operand::IndexSet _inputs; + operand::IndexSet _outputs; + OperandConstraint _input_constr; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/NodeVisitor.h b/runtimes/neurun/src/model/operation/NodeVisitor.h new file mode 100644 index 000000000..8420de998 --- /dev/null +++ b/runtimes/neurun/src/model/operation/NodeVisitor.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_NODE_VISITOR_H__ +#define __NEURUN_MODEL_OPERATION_NODE_VISITOR_H__ + +#include "Node.Include.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +struct NodeVisitor +{ + virtual ~NodeVisitor() = default; + +#define OP(InternalName, IsNnApi, NnApiName) \ + virtual void visit(const InternalName &) {} +#include "model/operation/Op.lst" +#undef OP +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_NODE_VISITOR_H__ diff --git a/runtimes/neurun/src/model/operation/Op.lst b/runtimes/neurun/src/model/operation/Op.lst new file mode 100644 index 000000000..23f4b5118 --- /dev/null +++ b/runtimes/neurun/src/model/operation/Op.lst @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2018 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 OP +#error Define OP before including this file +#endif + +// NOTE The relation between "Internal Name" and "NN API Name" is "1 : N". + +// Internal Name | NN API? | NN API Name +OP(AddNode , true , ADD) +OP(Conv2DNode , true , CONV_2D) +OP(AvgPool2DNode , true , AVERAGE_POOL_2D) +OP(MaxPool2DNode , true , MAX_POOL_2D) +OP(ConcatNode , true , CONCATENATION) +OP(FullyConnectedNode , true , FULLY_CONNECTED) +OP(ReshapeNode , true , RESHAPE) +OP(SoftmaxNode , true , SOFTMAX) +OP(PermuteNode , false , NOT_AVAILABLE) diff --git a/runtimes/neurun/src/model/operation/OperandConstraint.cc b/runtimes/neurun/src/model/operation/OperandConstraint.cc new file mode 100644 index 000000000..5c69de928 --- /dev/null +++ b/runtimes/neurun/src/model/operation/OperandConstraint.cc @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2018 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 "OperandConstraint.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/OperandConstraint.h b/runtimes/neurun/src/model/operation/OperandConstraint.h new file mode 100644 index 000000000..d1cd8aa2c --- /dev/null +++ b/runtimes/neurun/src/model/operation/OperandConstraint.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_OPERAND_CONSTRAINT_H__ +#define __NEURUN_MODEL_OPERATION_OPERAND_CONSTRAINT_H__ + +#include <stdint.h> +#include <limits> +#include <set> + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class OperandConstraint +{ +private: + static const uint32_t INF = std::numeric_limits<uint32_t>::max(); + +public: + static OperandConstraint createAny() { return OperandConstraint{0u, INF}; } + static OperandConstraint createExact(uint32_t exact) { return OperandConstraint{exact, exact}; } + static OperandConstraint createAtMost(uint32_t end) { return OperandConstraint{0u, end}; } + static OperandConstraint createAtLeast(uint32_t begin) { return OperandConstraint{begin, INF}; } + static OperandConstraint createInRange(uint32_t begin, uint32_t end) + { + return OperandConstraint{begin, end}; + } + +private: + OperandConstraint(uint32_t begin, uint32_t end) : _begin{begin}, _end{end} {} + +public: + bool check(uint32_t ind) const { return _begin <= ind && ind <= _end; } + +private: + uint32_t _begin; + uint32_t _end; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_OPERAND_CONSTRAINT_H__ diff --git a/runtimes/neurun/src/model/operation/PermuteNode.cc b/runtimes/neurun/src/model/operation/PermuteNode.cc new file mode 100644 index 000000000..174d2a86b --- /dev/null +++ b/runtimes/neurun/src/model/operation/PermuteNode.cc @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2018 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 "PermuteNode.h" + +#include <cassert> + +#include "NodeVisitor.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +void PermuteNode::accept(NodeVisitor &&v) const { v.visit(*this); } + +PermuteNode::PermuteNode(const operand::Index &input, const operand::Index &output, Type type) + : model::operation::Node{OperandConstraint::createExact(1u)}, _param{type} +{ + setInputs({input}); + setOutputs({output}); +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/PermuteNode.h b/runtimes/neurun/src/model/operation/PermuteNode.h new file mode 100644 index 000000000..b589975be --- /dev/null +++ b/runtimes/neurun/src/model/operation/PermuteNode.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_PERMUTE_NODE_H__ +#define __NEURUN_MODEL_OPERATION_PERMUTE_NODE_H__ + +#include "model/operation/Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class PermuteNode : public model::operation::Node +{ +public: + enum class Type + { + NHWC_TO_NCHW, + NCHW_TO_NHWC, + COPY + }; + + struct Param + { + Type type; + }; + +public: + virtual void accept(NodeVisitor &&) const override; + virtual std::string getName() const override { return "Permute"; } + +public: + PermuteNode(const operand::Index &input, const operand::Index &output, Type type); + +public: + const Param ¶m() const { return _param; } + +private: + Param _param; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_PERMUTE_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/ReshapeNode.cc b/runtimes/neurun/src/model/operation/ReshapeNode.cc new file mode 100644 index 000000000..616b8cd65 --- /dev/null +++ b/runtimes/neurun/src/model/operation/ReshapeNode.cc @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018 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 "ReshapeNode.h" + +#include <cassert> + +#include "NodeVisitor.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +void ReshapeNode::accept(NodeVisitor &&v) const { v.visit(*this); } + +ReshapeNode::ReshapeNode(const model::operation::Node::InitParam &init_param) + : model::operation::Node{OperandConstraint::createExact(1u)} +{ + assert(init_param.input_count == 2 && init_param.output_count == 1); + + // Each input should be interpreted as follows: + // + // 0 -> A tensor, specifying the tensor to be reshaped. + // 1 -> A 1-D tensor of type ANEURALNETWORKS_TENSOR_INT32, defining the shape of the output + // tensor + + // TODO Second input should be shape tensor (init_param.inputs[1]) + setInputs({init_param.inputs[0] /* , init_param.inputs[1] */}); + setOutputs({init_param.outputs[0]}); +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/ReshapeNode.h b/runtimes/neurun/src/model/operation/ReshapeNode.h new file mode 100644 index 000000000..1758e9ec8 --- /dev/null +++ b/runtimes/neurun/src/model/operation/ReshapeNode.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_RESHAPE_NODE_H__ +#define __NEURUN_MODEL_OPERATION_RESHAPE_NODE_H__ + +#include <memory> + +#include "model/operation/Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class ReshapeNode : public model::operation::Node +{ +public: + ReshapeNode(const model::operation::Node::InitParam &init_param); + + enum Input + { + INPUT = 0 + }; + +public: + virtual void accept(NodeVisitor &&) const override; + virtual std::string getName() const override { return "Reshape"; } +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_RESHAPE_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/Set.cc b/runtimes/neurun/src/model/operation/Set.cc new file mode 100644 index 000000000..14bd4f584 --- /dev/null +++ b/runtimes/neurun/src/model/operation/Set.cc @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018 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 "Set.h" + +#include <cassert> + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +const Index Set::generateIndex() +{ + assert((_index_count) <= 0x7fffffff); + + return Index{_index_count++}; +} + +Index Set::append(std::unique_ptr<Node> &&node) +{ + auto index = generateIndex(); + + _nodes[index] = std::move(node); + return index; +} + +const Node &Set::at(const Index &index) const { return *(_nodes.at(index)); } + +Node &Set::at(const Index &index) { return *(_nodes.at(index)); } + +bool Set::exist(const Index &index) const { return _nodes.find(index) != _nodes.end(); } + +void Set::iterate(const std::function<void(const Index &, const Node &)> &fn) const +{ + for (auto it = _nodes.begin(); it != _nodes.end(); ++it) + { + fn(it->first, *it->second); + } +} + +void Set::iterate(const std::function<void(const Index &, Node &)> &fn) +{ + for (auto it = _nodes.begin(); it != _nodes.end(); ++it) + { + fn(it->first, *it->second); + } +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/Set.h b/runtimes/neurun/src/model/operation/Set.h new file mode 100644 index 000000000..eebf91e65 --- /dev/null +++ b/runtimes/neurun/src/model/operation/Set.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_SET_H__ +#define __NEURUN_MODEL_OPERATION_SET_H__ + +#include <memory> + +#include "model/operation/Index.h" +#include "Node.h" + +#include <unordered_map> + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class Set +{ +public: + Set() : _index_count(0) {} + +public: + Index append(std::unique_ptr<Node> &&node); + void remove(const Index &index) { _nodes.erase(index); }; + +public: + const Node &at(const Index &) const; + Node &at(const Index &); + bool exist(const Index &) const; + uint32_t size() const { return _nodes.size(); } + void iterate(const std::function<void(const Index &, const Node &)> &fn) const; + void iterate(const std::function<void(const Index &, Node &)> &fn); + +private: + const Index generateIndex(); + +private: + std::unordered_map<Index, std::unique_ptr<Node>> _nodes; + uint32_t _index_count; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_SET_H__ diff --git a/runtimes/neurun/src/model/operation/SoftmaxNode.cc b/runtimes/neurun/src/model/operation/SoftmaxNode.cc new file mode 100644 index 000000000..d157aa4a7 --- /dev/null +++ b/runtimes/neurun/src/model/operation/SoftmaxNode.cc @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018 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 "SoftmaxNode.h" + +#include <cassert> + +#include "NodeVisitor.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +void SoftmaxNode::accept(NodeVisitor &&v) const { v.visit(*this); } + +SoftmaxNode::SoftmaxNode(const model::operation::Node::InitParam &init_param) + : model::operation::Node{OperandConstraint::createExact(1u)} +{ + assert(init_param.input_count == 2 && init_param.output_count == 1); + + // Each input should be interpreted as follows: + // + // 0 -> A 2-D or 4-D tensor, specifying the tensor to be reshaped. + // 1 -> FLOAT32 value, specifying the positive scaling factor for the exponent, beta. + + setInputs({init_param.inputs[0]}); + setOutputs({init_param.outputs[0]}); + + _param.scale_index = operand::Index{init_param.inputs[1]}; +} + +} // namespace operation +} // namespace model +} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/SoftmaxNode.h b/runtimes/neurun/src/model/operation/SoftmaxNode.h new file mode 100644 index 000000000..4a5a72e5a --- /dev/null +++ b/runtimes/neurun/src/model/operation/SoftmaxNode.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018 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 __NEURUN_MODEL_OPERATION_SOFTMAX_NODE_H__ +#define __NEURUN_MODEL_OPERATION_SOFTMAX_NODE_H__ + +#include <memory> + +#include "model/operation/Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +class SoftmaxNode : public model::operation::Node +{ +public: + SoftmaxNode(const model::operation::Node::InitParam &init_param); + enum Input + { + INPUT = 0 + }; + + struct Param + { + operand::Index scale_index; + }; + +public: + virtual void accept(NodeVisitor &&) const override; + virtual std::string getName() const override { return "SoftMax"; } + +public: + const Param ¶m() const { return _param; } + +private: + Param _param; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_SOFTMAX_NODE_H__ |