Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

add reshape op and ut #367

Merged
merged 19 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions saber/funcs/reshape.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/* Copyright (c) 2018 Anakin Authors, Inc. 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 ANAKIN_SABER_FUNCS_RESHAPE_H
#define ANAKIN_SABER_FUNCS_RESHAPE_H

#include "saber/funcs/base.h"
#include "saber/funcs/impl/impl_base.h"
#include "saber/saber_funcs_param.h"

namespace anakin {

namespace saber {

template <typename TargetType, DataType OpDtype>
class Reshape : public BaseFunc<
TargetType,
OpDtype,
ImplBase,
ReshapeParam>
{
public:
using BaseFunc<
TargetType,
OpDtype,
ImplBase,
ReshapeParam >::BaseFunc;
Reshape() = default;

typedef ReshapeParam<TargetType> Param_t;
typedef std::vector<Tensor<TargetType> *> Input_v;
typedef std::vector<Tensor<TargetType> *> Output_v;
typedef std::vector<Shape> Shape_v;

virtual SaberStatus compute_output_shape(const Input_v& input, \
Output_v &output, Param_t& param) override {

Shape output_shape;
output_shape.resize(param.shape_params.size());

CHECK_EQ(input[0] -> is_continue_mem(), true) << "input tensor must not have roi";

Shape input_shape = input[0] -> valid_shape();
int valid_size = input[0] -> valid_size();
int infer_axis = -1;
int count_axis = 1;
for (int i = 0; i < param.shape_params.size(); ++i) {
if (param.shape_params[i] == 0){
CHECK_LT(i, input_shape.size()) << "wrong parameters, exceed input dims";
output_shape[i] = input_shape[i];
count_axis *= input_shape[i];
} else if (param.shape_params[i] > 0){
output_shape[i] = param.shape_params[i];
count_axis *= param.shape_params[i];
} else {
output_shape[i] = -1;
infer_axis = i;
}
}

if (infer_axis >= 0){
output_shape[infer_axis] = valid_size / count_axis;
}
return output[0] -> set_shape(output_shape);
}
//Reshape ops do nothing
virtual SaberStatus init_impl(ImplEnum implenum) override {

return SaberSuccess;

}

//Reshape ops do nothing
virtual SaberStatus init(const Input_v& input, Output_v& output, Param_t& param,
SaberImplStrategy strategy, ImplEnum implenum, Context<TargetType > &ctx) {
return SaberSuccess;
}
//Reshape ops do nothing
virtual SaberStatus operator()(const Input_v& input, Output_v& output, Param_t& param, \
Context<TargetType> &ctx) {
return SaberSuccess;
}
private:

virtual void pick_best_static() override {
//saber impl
this -> _best_impl = this -> _impl[0];
}

//virtual void pick_best_runtime(Input_v input, Output_v output, Param_t& param) override {}

virtual void pick_best_specify(ImplEnum implenum) override {
//saber impl
this -> _best_impl = this -> _impl[0];
}

};

} //namespace saber

} //namespace anakin
#endif //ANAKIN_SABER_FUNCS_RESHAPE_H
34 changes: 33 additions & 1 deletion saber/saber_funcs_param.h
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,38 @@ struct PriorBoxParam {
std::vector<PriorType> order;
};

template <typename TargetType>
struct ReshapeParam {
ReshapeParam() = default;
explicit ReshapeParam(std::vector<int> shape_param_in){
int count = 0;
for (int i = 0; i < shape_param_in.size(); ++i) {
if (shape_param_in[i] == -1){
count ++;
}
}
CHECK_LE(count, 1) << "shape parameter contains multiple -1 dims";
shape_params = shape_param_in;
}
ReshapeParam(const ReshapeParam<TargetType> &right) {
shape_params = right.shape_params;
}
ReshapeParam<TargetType> &operator=(const ReshapeParam<TargetType> &right) {
shape_params = right.shape_params;
return *this;
}
bool operator==(const ReshapeParam &right) {
bool comp_eq = shape_params.size() == right.shape_params.size();
for (int i = 0; i < shape_params.size(); ++i) {
if (!comp_eq){
return false;
}
comp_eq = shape_params[i] == right.shape_params[i];
}
return true;
}
std::vector<int> shape_params;
};

template<typename TargetType>
struct ResizeParam {
Expand Down Expand Up @@ -1896,4 +1928,4 @@ struct TransposeParam {

}
}
#endif //SABER_FUNCS_PARAM_H
#endif //SABER_FUNCS_PARAM_H
29 changes: 12 additions & 17 deletions test/saber/test_saber_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "saber/saber_types.h"
#include "saber/core/tensor_op.h"
#include "test/saber/test_saber_func.h"
#include "saber/core/data_traits.h"
#include "utils/unit_test/aktest.h"
#include "utils/logger/logger.h"

Expand Down Expand Up @@ -175,7 +176,7 @@ class TestSaberBase{
clear_datas();
std::vector<Shape> shape_v;
for (int i=0; i<_op_input_num; ++i){
shape_v.push_back(input[0] -> shape());
shape_v.push_back(input[0] -> valid_shape());
}
add_inputs_shape(shape_v);
for(int i = 0; i < _op_input_num; ++i)
Expand Down Expand Up @@ -206,10 +207,10 @@ class TestSaberBase{
_outputs_dev[i][j] -> re_alloc(sh, Dtype);
_outputs_host[i][j] -> re_alloc(sh, Dtype);
_outputs_hd[i][j] -> re_alloc(sh, Dtype);


//init output_dev and output_host to equal
fill_tensor_const(*_outputs_dev[i][j],0);
fill_tensor_const(*_outputs_host[i][j],0);

}
}
}
Expand Down Expand Up @@ -253,12 +254,10 @@ class TestSaberBase{
return status;
}
typename TensorD :: API :: stream_t stream = ctx.get_compute_stream();
//always 0?
_outputs_dev[input_index][0] -> record_event(stream);
_outputs_dev[input_index][0] -> sync();//
_outputs_dev[input_index][0] -> sync();

}
//print_tensor(*_outputs_hd[0][0]);
}
t.end(ctx);
float ts = t.get_average_ms();
Expand Down Expand Up @@ -286,19 +285,16 @@ class TestSaberBase{
Shape sh = _inputs_host[0][0] -> shape();
for(int i = 0; i < _outputs_host.size(); ++i){
for(int j = 0; j<_op_output_num; ++j){
// LOG(INFO) << "_outputs_hd: ";
// print_tensor(*_outputs_hd[i][j]);
// LOG(INFO) << "_outputs_host: ";
// print_tensor(*_outputs_host[i][j]);
tensor_cmp_host<OpDataType>((const OpDataType*)_outputs_hd[i][j] -> data(), (const OpDataType*)_outputs_host[i][j] -> data(),
tensor_cmp_host<OpDataType>(static_cast<const OpDataType*>(_outputs_hd[i][j] -> data()),
static_cast<const OpDataType*>(_outputs_host[i][j] -> data()),
_outputs_hd[i][j] -> valid_size(), max_ratio[i], max_diff[i]);
LOG(INFO) << "input_shape:(" << sh.num() << "," << sh.channel() << "," << sh.height() << "," << sh.width() << ")";
LOG(INFO) << "max_ratio:" << max_ratio[i];
if(max_ratio[i] <= succ_ratio)
if(max_ratio[i] <= succ_ratio && (_outputs_hd[i][0]->valid_shape() == _outputs_host[i][0]->valid_shape())){
LOG(INFO) << "Test Passed!";
else
} else {
LOG(FATAL) << "Test Failed!!"<< "output:(" << i << "-" << j << ")";
//LOG(ERROR)<<"Test Failed!!";
}
}
}
}
Expand All @@ -318,8 +314,8 @@ class TestSaberBase{
Env<TargetType_D> :: env_init();
Env<TargetType_H> :: env_init();

std :: vector<std :: string> runtype{"STATIC","RUNTIME","SPECIFY"};
std :: vector<std :: string> impltype{"VENDER","SABER"};
std :: vector<std :: string> runtype{"STATIC", "RUNTIME", "SPECIFY"};
std :: vector<std :: string> impltype{"VENDER", "SABER"};
for(auto strate : {SPECIFY, RUNTIME, STATIC}){
for(auto implenum : {VENDER_IMPL, SABER_IMPL}){
LOG(INFO) << "TESTING: strategy:" << runtype[strate-1] << ",impltype:" << impltype[(int)implenum];
Expand Down Expand Up @@ -351,7 +347,6 @@ class TestSaberBase{
std :: vector<Param_t> _params;

};//testsaberbase

}//namespace saber
}//namespace anakin

Expand Down
123 changes: 123 additions & 0 deletions test/saber/test_saber_reshape.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

#include "saber/funcs/reshape.h"

#include <vector>
#include <algorithm>

#include "saber/core/context.h"
#include "saber/saber_types.h"
#include "saber/core/tensor_op.h"
#include "test/saber/test_saber_func.h"
#include "test/saber/test_saber_base.h"
#include "saber/core/tensor_op.h"

using namespace anakin::saber;

template <typename dtype,typename TargetType_D,typename TargetType_H>
void reshape_cpu_func(const std::vector<Tensor<TargetType_H>*>& input, std::vector<Tensor<TargetType_H>*>& output, ReshapeParam<TargetType_D>& param) {
Shape in_shape = input[0] -> valid_shape();
Shape param_shape = param.shape_params;
Shape out_shape;
out_shape.resize(param_shape.size());
int infer_axis = -1;
int num_axis = 1;
int infer_count=0;
for (int i=0; i < param_shape.size(); ++i){
CHECK_LT(i, in_shape.size()) << "param index exceed input dims";
if ( param_shape[i] == 0){
out_shape[i] = in_shape[i];
num_axis *= out_shape[i];
} else if (param_shape[i] == -1){
infer_axis = i;
++infer_count;
} else {
out_shape[i] = param_shape[i];
num_axis *= out_shape[i];
}
}
CHECK_LE(infer_count, 1) << "infer axises cannot exceed 1";
if (infer_axis >= 0){
out_shape[infer_axis] = input[0] -> valid_size() / num_axis;
}
output[0] -> set_shape(out_shape);
}

TEST(TestSaberFunc, test_func_reshape) {
#ifdef USE_CUDA
//Init the test_base
TestSaberBase<NV, NVHX86, AK_FLOAT, Reshape, ReshapeParam> testbase;
auto param_check = [](std::vector<int> new_shape, std::vector<int> in_shape) -> bool {
CHECK_EQ(new_shape.size(), in_shape.size()) << "invalid check";
int new_count=1;
int in_count=1;
for(int i=0; i<new_shape.size(); ++i){
if (new_shape[i] > 0){
in_count *= in_shape[i];
if (new_shape[i] !=-1){
new_count *= new_shape[i];
}
}
}
return new_count <= in_count;
};
//test shape contain -1 and 0
for (int rs0 : {0, -1, 2}){
for (int rs1 : {0, -1, 4}){
for (int rs2 : {0, -1, 8}){
for (int rs3 : {0, -1, 16}){
std::vector<int> new_shape{rs0, rs1, rs2, rs3};
if (std::count(new_shape.begin(), new_shape.end(), -1) == 1){
ReshapeParam<NV> param(new_shape);
LOG(INFO) << "new_shape:"<<rs0<<" "<<rs1<<" "<<rs2<<" "<<rs3;
for (int n : {1, 2}){
for (int c : {1, 4}){
for (int h: {32, 64}){
for (int w : {32, 64}){
Shape in_shape({n, c, h, w});
if (param_check(new_shape, in_shape)){
testbase.set_param(param);
testbase.set_input_shape(in_shape);
testbase.run_test(reshape_cpu_func<float, NV, NVHX86>);
}
}
}
}
}
}
}
}
}
}//for rs0
//test shape normal
std::vector<Shape> new_shapes;
std::vector<Shape> in_shapes;
new_shapes.emplace_back(Shape({1, 1, 3, 64}));
in_shapes.emplace_back(Shape({1, 3, 4, 16}));
new_shapes.emplace_back(Shape({1, 4, 3, 64}));
in_shapes.emplace_back(Shape({1, 1, 1, 3*64*4}));
new_shapes.emplace_back(Shape({2, 2, 3, 64}));
in_shapes.emplace_back(Shape({1, 2, 1, 2*3*64}));
new_shapes.emplace_back(Shape({32, 32, 3, 64}));
in_shapes.emplace_back(Shape({1, 3, 64, 32*32}));
for (int i=0; i<new_shapes.size(); ++i){
ReshapeParam<NV> param(new_shapes[i]);
testbase.set_param(param);
testbase.set_input_shape(in_shapes[i]);
testbase.run_test(reshape_cpu_func<float, NV, NVHX86>);
}

#endif

}



int main(int argc, const char** argv) {
// initial logger
//logger::init(argv[0]);

InitTest();
RUN_ALL_TESTS(argv[0]);
return 0;
}