Added param step for slice & added unit test (#352)
Signed-off-by: Chen Xin <jack.chen@verisilicon.com>
This commit is contained in:
parent
20e27ed550
commit
e8ca6b8ee3
|
|
@ -40,17 +40,20 @@ namespace ops {
|
|||
|
||||
class Slice : public DirectMapOp {
|
||||
public:
|
||||
Slice(Graph* graph,
|
||||
uint32_t dims,
|
||||
const std::vector<int32_t>& start,
|
||||
Slice(Graph* graph, uint32_t dims, const std::vector<int32_t>& start,
|
||||
const std::vector<int32_t>& length);
|
||||
Slice(Graph* graph, uint32_t dims, const std::vector<int32_t>& start,
|
||||
const std::vector<int32_t>& length, const std::vector<int32_t>& step);
|
||||
|
||||
std::shared_ptr<Operation> Clone(std::shared_ptr<Graph>& graph) const override;
|
||||
std::shared_ptr<Operation> Clone(
|
||||
std::shared_ptr<Graph>& graph) const override;
|
||||
|
||||
protected:
|
||||
uint32_t dims_;
|
||||
const std::vector<int32_t> start_;
|
||||
const std::vector<int32_t> length_;
|
||||
std::vector<int32_t> end_dims_;
|
||||
const std::vector<int32_t> step_;
|
||||
};
|
||||
|
||||
} // namespace ops
|
||||
|
|
|
|||
|
|
@ -43,8 +43,36 @@ Slice::Slice(Graph* graph, uint32_t dims, const std::vector<int32_t>& start,
|
|||
reinterpret_cast<const uint32_t*>(length_.data());
|
||||
}
|
||||
|
||||
Slice::Slice(Graph* graph, uint32_t dims, const std::vector<int32_t>& start,
|
||||
const std::vector<int32_t>& length,
|
||||
const std::vector<int32_t>& step)
|
||||
: DirectMapOp(graph, VSI_NN_OP_STRIDED_SLICE),
|
||||
dims_(dims),
|
||||
start_(std::move(start)),
|
||||
length_(std::move(length)),
|
||||
step_(std::move(step)) {
|
||||
for (uint32_t i = 0; i < length_.size(); ++i) {
|
||||
end_dims_.push_back(start_.at(i) + length_.at(i));
|
||||
}
|
||||
this->impl()->node()->nn_param.strided_slice.begin_mask = 0;
|
||||
this->impl()->node()->nn_param.strided_slice.end_mask = 0;
|
||||
this->impl()->node()->nn_param.strided_slice.shrink_axis_mask = 0;
|
||||
this->impl()->node()->nn_param.strided_slice.begin_dims = start_.data();
|
||||
this->impl()->node()->nn_param.strided_slice.begin_dims_num = start_.size();
|
||||
this->impl()->node()->nn_param.strided_slice.end_dims = end_dims_.data();
|
||||
this->impl()->node()->nn_param.strided_slice.end_dims_num = end_dims_.size();
|
||||
this->impl()->node()->nn_param.strided_slice.stride_dims = step_.data();
|
||||
this->impl()->node()->nn_param.strided_slice.stride_dims_num = step_.size();
|
||||
}
|
||||
|
||||
std::shared_ptr<Operation> Slice::Clone(std::shared_ptr<Graph>& graph) const {
|
||||
return graph->CreateOperation<Slice>(this->dims_, this->start_, this->length_);
|
||||
if (this->impl()->kind_ == VSI_NN_OP_STRIDED_SLICE) {
|
||||
return graph->CreateOperation<Slice>(this->dims_, this->start_,
|
||||
this->length_, this->step_);
|
||||
} else {
|
||||
return graph->CreateOperation<Slice>(this->dims_, this->start_,
|
||||
this->length_);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ops
|
||||
|
|
|
|||
|
|
@ -0,0 +1,144 @@
|
|||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2021 Vivante Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*****************************************************************************/
|
||||
#include "tim/vx/context.h"
|
||||
#include "tim/vx/graph.h"
|
||||
#include "tim/vx/ops/slice.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
TEST(Slice, shape_2_3) {
|
||||
auto ctx = tim::vx::Context::Create();
|
||||
auto graph = ctx->CreateGraph();
|
||||
|
||||
tim::vx::ShapeType input_shape({4, 3});
|
||||
tim::vx::ShapeType output_shape({2, 1});
|
||||
tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, input_shape,
|
||||
tim::vx::TensorAttribute::INPUT);
|
||||
tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, output_shape,
|
||||
tim::vx::TensorAttribute::OUTPUT);
|
||||
|
||||
auto input_tensor = graph->CreateTensor(input_spec);
|
||||
auto output_tensor = graph->CreateTensor(output_spec);
|
||||
|
||||
std::vector<float> in_data = {
|
||||
1, 4, 2, 5,
|
||||
3, 6, 1, 4,
|
||||
2, 5, 3, 6,
|
||||
};
|
||||
std::vector<float> golden = {
|
||||
1, 4,
|
||||
};
|
||||
|
||||
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
|
||||
in_data.size() * sizeof(float)));
|
||||
std::vector<int32_t> start = {0, 0};
|
||||
std::vector<int32_t> length = {2, 1};
|
||||
auto op = graph->CreateOperation<tim::vx::ops::Slice>(0, start, length);
|
||||
(*op).BindInputs({input_tensor}).BindOutputs({output_tensor});
|
||||
|
||||
EXPECT_TRUE(graph->Compile());
|
||||
EXPECT_TRUE(graph->Run());
|
||||
|
||||
std::vector<float> output(golden.size());
|
||||
EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data()));
|
||||
EXPECT_EQ(golden, output);
|
||||
}
|
||||
|
||||
TEST(Slice, shape_2_3_step_1_1) {
|
||||
auto ctx = tim::vx::Context::Create();
|
||||
auto graph = ctx->CreateGraph();
|
||||
|
||||
tim::vx::ShapeType input_shape({4, 3});
|
||||
tim::vx::ShapeType output_shape({2, 1});
|
||||
tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, input_shape,
|
||||
tim::vx::TensorAttribute::INPUT);
|
||||
tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, output_shape,
|
||||
tim::vx::TensorAttribute::OUTPUT);
|
||||
|
||||
auto input_tensor = graph->CreateTensor(input_spec);
|
||||
auto output_tensor = graph->CreateTensor(output_spec);
|
||||
|
||||
std::vector<float> in_data = {
|
||||
1, 4, 2, 5,
|
||||
3, 6, 1, 4,
|
||||
2, 5, 3, 6,
|
||||
};
|
||||
std::vector<float> golden = {
|
||||
1, 4,
|
||||
};
|
||||
|
||||
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
|
||||
in_data.size() * sizeof(float)));
|
||||
std::vector<int32_t> start = {0, 0};
|
||||
std::vector<int32_t> length = {2, 1};
|
||||
std::vector<int32_t> step = {1, 1};
|
||||
auto op = graph->CreateOperation<tim::vx::ops::Slice>(0, start, length, step);
|
||||
(*op).BindInputs({input_tensor}).BindOutputs({output_tensor});
|
||||
|
||||
EXPECT_TRUE(graph->Compile());
|
||||
EXPECT_TRUE(graph->Run());
|
||||
|
||||
std::vector<float> output(golden.size());
|
||||
EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data()));
|
||||
EXPECT_EQ(golden, output);
|
||||
}
|
||||
|
||||
TEST(Slice, shape_4_3_step_2_1) {
|
||||
auto ctx = tim::vx::Context::Create();
|
||||
auto graph = ctx->CreateGraph();
|
||||
|
||||
tim::vx::ShapeType input_shape({4, 3});
|
||||
tim::vx::ShapeType output_shape({2, 2});
|
||||
tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, input_shape,
|
||||
tim::vx::TensorAttribute::INPUT);
|
||||
tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, output_shape,
|
||||
tim::vx::TensorAttribute::OUTPUT);
|
||||
|
||||
auto input_tensor = graph->CreateTensor(input_spec);
|
||||
auto output_tensor = graph->CreateTensor(output_spec);
|
||||
|
||||
std::vector<float> in_data = {
|
||||
1, 4, 2, 5,
|
||||
3, 6, 1, 4,
|
||||
2, 5, 3, 6,
|
||||
};
|
||||
std::vector<float> golden = {
|
||||
1, 2, 2, 3,
|
||||
};
|
||||
|
||||
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(),
|
||||
in_data.size() * sizeof(float)));
|
||||
std::vector<int32_t> start = {0, 0};
|
||||
std::vector<int32_t> length = {4, 3};
|
||||
std::vector<int32_t> step = {2, 2};
|
||||
auto op = graph->CreateOperation<tim::vx::ops::Slice>(0, start, length, step);
|
||||
(*op).BindInputs({input_tensor}).BindOutputs({output_tensor});
|
||||
|
||||
EXPECT_TRUE(graph->Compile());
|
||||
EXPECT_TRUE(graph->Run());
|
||||
|
||||
std::vector<float> output(golden.size());
|
||||
EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data()));
|
||||
EXPECT_EQ(golden, output);
|
||||
}
|
||||
Loading…
Reference in New Issue