--- /dev/null
+/*
+ * 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 "NodeExecution.h"
+
+#include "NodeDataImpl.h"
+#include "NodeDomain.h"
+#include "Validation.h"
+
+#include <nncc/core/ADT/tensor/IndexEnumerator.h>
+#include <nncc/core/ADT/tensor/LexicalLayout.h>
+
+#include <cassert>
+
+using nncc::core::ADT::tensor::Shape;
+using nncc::core::ADT::tensor::Index;
+using nncc::core::ADT::tensor::IndexEnumerator;
+using nncc::core::ADT::tensor::LexicalLayout;
+using nncc::core::ADT::tensor::make_buffer;
+
+namespace locomotiv
+{
+
+void NodeExecution::execute(loco::TensorConstantPad *pad)
+{
+ auto input_data = annot_data(pad->input());
+ auto input_domain = annot_domain(pad->input());
+ validate(input_data, "Input not ready");
+ validate(input_domain == loco::Domain::Tensor, "Input domain of TensorConstantPad is not Tensor");
+
+ auto input_shape = input_data->shape();
+ const uint32_t input_rank = input_shape->rank();
+
+ auto padding = pad->padding();
+ validate(input_rank == padding->rank(), "input and padding should have same rank");
+
+ auto constant_node = pad->constant();
+ auto constant_data = annot_data(constant_node);
+ validate(constant_data->dtype() == input_data->dtype(), "constant and input have same data type");
+ validate(constant_data->shape()->rank() == 1 && constant_data->shape()->dim(0) == 1,
+ "constant should have one rank with one dimension at zero axis");
+
+ std::unique_ptr<NodeData> pad_data = nullptr;
+ Index base_index;
+ base_index.resize(input_rank);
+
+ // Tensor is padded by relocating its base.
+ // padded output index = input index + base index
+ for (uint32_t axis = 0; axis < padding->rank(); axis++)
+ {
+ base_index.at(axis) = padding->front(axis);
+ }
+
+ // calculate output shape
+ Shape output_shape;
+ output_shape.resize(input_rank);
+ for (uint32_t i = 0; i < input_rank; i++)
+ {
+ output_shape.dim(i) = input_shape->dim(i) + padding->front(i) + padding->back(i);
+ }
+
+ switch (input_data->dtype())
+ {
+ case loco::DataType::FLOAT32:
+ {
+ auto input_buf = input_data->as_f32_bufptr();
+ auto constant_data_buf = constant_data->as_f32_bufptr();
+ const auto constant_value = constant_data_buf->at(Index{0});
+
+ auto output_buf = make_buffer<float, LexicalLayout>(output_shape);
+
+ for (IndexEnumerator ie{*input_shape}, oe{output_shape}; oe.valid(); oe.advance())
+ {
+ auto input_index = ie.current();
+ auto output_index = oe.current();
+
+ if ((input_index + base_index) == output_index)
+ {
+ output_buf.at(output_index) = input_buf->at(input_index);
+ ie.advance();
+ }
+ else
+ {
+ output_buf.at(output_index) = constant_value;
+ }
+ }
+
+ pad_data = make_data(output_buf);
+ break;
+ }
+ default:
+ throw std::runtime_error("NYI for this DataType");
+ }
+
+ assert(pad_data != nullptr);
+ annot_data(pad, std::move(pad_data));
+ annot_domain(pad, annot_domain(pad->input()));
+}
+
+} // namespace locomotiv
--- /dev/null
+/*
+ * 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 "NodeExecution.h"
+
+#include "locomotiv/NodeData.h"
+#include "NodeDataImpl.h"
+#include "NodeDomain.h"
+
+#include <nncc/core/ADT/tensor/Shape.h>
+#include <nncc/core/ADT/tensor/LexicalLayout.h>
+
+#include <gtest/gtest.h>
+
+using nncc::core::ADT::tensor::Index;
+using nncc::core::ADT::tensor::LexicalLayout;
+using nncc::core::ADT::tensor::make_buffer;
+using nncc::core::ADT::tensor::Shape;
+
+TEST(NodeExecution_Pad, tensor_constant_pad_4_dim)
+{
+ auto g = loco::make_graph();
+
+ auto inputTensor = g->nodes()->create<loco::Pull>();
+ inputTensor->dtype(loco::DataType::FLOAT32);
+ inputTensor->shape({1, 2, 2, 1});
+ auto inputTensor_buf = make_buffer<float, LexicalLayout>(Shape{1, 2, 2, 1});
+ inputTensor_buf.at(Index{0, 0, 0, 0}) = 1.0f;
+ inputTensor_buf.at(Index{0, 0, 1, 0}) = 2.0f;
+ inputTensor_buf.at(Index{0, 1, 0, 0}) = 3.0f;
+ inputTensor_buf.at(Index{0, 1, 1, 0}) = 4.0f;
+ auto inputTensor_data = locomotiv::make_data(inputTensor_buf);
+ locomotiv::annot_data(inputTensor, std::move(inputTensor_data));
+ locomotiv::annot_domain(inputTensor, loco::Domain::Tensor);
+
+ auto constant = g->nodes()->create<loco::ConstGen>();
+ constant->dtype(loco::DataType::FLOAT32);
+ constant->shape({1});
+ auto constant_buf = make_buffer<float, LexicalLayout>(Shape{1});
+ constant_buf.at(Index{0}) = 0.0f;
+ auto constant_data = locomotiv::make_data(constant_buf);
+ locomotiv::annot_data(constant, std::move(constant_data));
+ locomotiv::annot_domain(constant, loco::Domain::Tensor);
+
+ auto pad = g->nodes()->create<loco::TensorConstantPad>();
+ pad->input(inputTensor);
+ pad->constant(constant);
+
+ auto padding = pad->padding();
+ padding->rank(4);
+ padding->front(0) = 0;
+ padding->back(0) = 0;
+ padding->front(1) = 3;
+ padding->back(1) = 1;
+ padding->front(2) = 1;
+ padding->back(2) = 1;
+ padding->front(3) = 0;
+ padding->back(3) = 0;
+
+ locomotiv::NodeExecution::get().run(pad);
+
+ auto pad_data = locomotiv::annot_data(pad);
+ ASSERT_NE(pad_data, nullptr);
+ ASSERT_EQ(pad_data->dtype(), loco::DataType::FLOAT32);
+ ASSERT_EQ(*(pad_data->shape()), Shape({1, 6, 4, 1}));
+
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{0, 3, 1, 0}), 1.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{0, 3, 2, 0}), 2.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{0, 4, 1, 0}), 3.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{0, 4, 2, 0}), 4.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{0, 0, 0, 0}), 0.0f);
+
+ ASSERT_EQ(locomotiv::annot_domain(pad), loco::Domain::Tensor);
+}
+
+TEST(NodeExecution_Pad, tensor_constant_pad_1_dim)
+{
+ auto g = loco::make_graph();
+
+ auto inputTensor = g->nodes()->create<loco::Pull>();
+ inputTensor->dtype(loco::DataType::FLOAT32);
+ inputTensor->shape({3});
+ auto inputTensor_buf = make_buffer<float, LexicalLayout>(Shape{3});
+ inputTensor_buf.at(Index{0}) = 1.0f;
+ inputTensor_buf.at(Index{1}) = 5.0f;
+ inputTensor_buf.at(Index{2}) = 3.0f;
+ auto inputTensor_data = locomotiv::make_data(inputTensor_buf);
+ locomotiv::annot_data(inputTensor, std::move(inputTensor_data));
+ locomotiv::annot_domain(inputTensor, loco::Domain::Tensor);
+
+ auto constant = g->nodes()->create<loco::ConstGen>();
+ constant->dtype(loco::DataType::FLOAT32);
+ constant->shape({1});
+ auto constant_buf = make_buffer<float, LexicalLayout>(Shape{1});
+ constant_buf.at(Index{0}) = 0.0f;
+ auto constant_data = locomotiv::make_data(constant_buf);
+ locomotiv::annot_data(constant, std::move(constant_data));
+ locomotiv::annot_domain(constant, loco::Domain::Tensor);
+
+ auto pad = g->nodes()->create<loco::TensorConstantPad>();
+ pad->input(inputTensor);
+ pad->constant(constant);
+ auto padding = pad->padding();
+ padding->rank(1);
+ padding->front(0) = 2;
+ padding->back(0) = 1;
+
+ locomotiv::NodeExecution::get().run(pad);
+
+ auto pad_data = locomotiv::annot_data(pad);
+ ASSERT_NE(pad_data, nullptr);
+ ASSERT_EQ(pad_data->dtype(), loco::DataType::FLOAT32);
+ ASSERT_EQ(*(pad_data->shape()), Shape({6}));
+
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{0}), 0.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1}), 0.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{2}), 1.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{3}), 5.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{4}), 3.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{5}), 0.0f);
+
+ ASSERT_EQ(locomotiv::annot_domain(pad), loco::Domain::Tensor);
+}
+
+TEST(NodeExecution_Pad, tensor_constant_pad_6_dim)
+{
+ auto g = loco::make_graph();
+
+ auto inputTensor = g->nodes()->create<loco::Pull>();
+ inputTensor->dtype(loco::DataType::FLOAT32);
+ inputTensor->shape({2, 1, 3, 2, 1, 2});
+ auto inputTensor_buf = make_buffer<float, LexicalLayout>(Shape{2, 1, 3, 2, 1, 2});
+ int a, b, c, d, e, f;
+ float dummy = 1.0f;
+ for (uint32_t a = 0; a < 2; a++)
+ {
+ for (uint32_t b = 0; b < 1; b++)
+ {
+ for (uint32_t c = 0; c < 3; c++)
+ {
+ for (uint32_t d = 0; d < 2; d++)
+ {
+ for (uint32_t e = 0; e < 1; e++)
+ {
+ for (uint32_t f = 0; f < 2; f++)
+ {
+ inputTensor_buf.at(Index{a, b, c, d, e, f}) = dummy++;
+ }
+ }
+ }
+ }
+ }
+ }
+ auto inputTensor_data = locomotiv::make_data(inputTensor_buf);
+ locomotiv::annot_data(inputTensor, std::move(inputTensor_data));
+ locomotiv::annot_domain(inputTensor, loco::Domain::Tensor);
+
+ auto constant = g->nodes()->create<loco::ConstGen>();
+ constant->dtype(loco::DataType::FLOAT32);
+ constant->shape({1});
+ auto constant_buf = make_buffer<float, LexicalLayout>(Shape{1});
+ constant_buf.at(Index{0}) = 0.0f;
+ auto constant_data = locomotiv::make_data(constant_buf);
+ locomotiv::annot_data(constant, std::move(constant_data));
+ locomotiv::annot_domain(constant, loco::Domain::Tensor);
+
+ auto pad = g->nodes()->create<loco::TensorConstantPad>();
+ pad->input(inputTensor);
+ pad->constant(constant);
+ auto padding = pad->padding();
+
+ padding->rank(6);
+ padding->front(0) = 1;
+ padding->back(0) = 1;
+ padding->front(1) = 0;
+ padding->back(1) = 0;
+ padding->front(2) = 1;
+ padding->back(2) = 2;
+ padding->front(3) = 2;
+ padding->back(3) = 1;
+ padding->front(4) = 0;
+ padding->back(4) = 0;
+ padding->front(5) = 1;
+ padding->back(5) = 2;
+
+ locomotiv::NodeExecution::get().run(pad);
+
+ auto pad_data = locomotiv::annot_data(pad);
+ ASSERT_NE(pad_data, nullptr);
+ ASSERT_EQ(pad_data->dtype(), loco::DataType::FLOAT32);
+ ASSERT_EQ(*(pad_data->shape()), Shape({4, 1, 6, 5, 1, 5}));
+
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 1, 2, 0, 1}), 1.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 1, 2, 0, 2}), 2.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 1, 3, 0, 1}), 3.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 1, 3, 0, 2}), 4.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 2, 2, 0, 1}), 5.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 2, 2, 0, 2}), 6.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 2, 3, 0, 1}), 7.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 2, 3, 0, 2}), 8.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 3, 2, 0, 1}), 9.0f);
+ ASSERT_FLOAT_EQ(pad_data->as_f32_bufptr()->at(Index{1, 0, 3, 2, 0, 2}), 10.0f);
+
+ ASSERT_EQ(locomotiv::annot_domain(pad), loco::Domain::Tensor);
+}