Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range operator #5046

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
6 changes: 6 additions & 0 deletions docs/developer-guide/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* [Power](#power)
* [PReLU](#prelu)
* [Quantize](#quantize)
* [Range](#range)
* [Reduction](#reduction)
* [ReLU](#relu)
* [Reorg](#reorg)
Expand Down Expand Up @@ -1523,6 +1524,11 @@ y = float2int8(x * scale)
| ------------- | ----- | --------------------- |
| scale_data | float | [scale_data_size] |

# Range
```
y = range(x, limit, delta)
```

# Reduction
```
y = reduce_op(x * coeff)
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ ncnn_add_layer(Erf)
ncnn_add_layer(Diag)
ncnn_add_layer(CELU)
ncnn_add_layer(Shrink)
ncnn_add_layer(Range)

if(NCNN_VULKAN)
ncnn_add_shader(${CMAKE_CURRENT_SOURCE_DIR}/convert_ycbcr.comp)
Expand Down
87 changes: 87 additions & 0 deletions src/layer/range.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// 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 "range.h"

#include <math.h>

namespace ncnn {

Range::Range()
{
one_blob_only = false;
support_inplace = false;
}

int Range::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
{
if (bottom_blobs.size() < 2 || bottom_blobs.size() > 3 || top_blobs.size() != 1)
return -100;

const Mat& start = bottom_blobs[0];
if (start.empty())
return -100;

const Mat& limit = bottom_blobs[1];
if (limit.empty())
return -100;

const Mat& delta = bottom_blobs.size() == 3 ? bottom_blobs[2] : Mat();

Mat& output = top_blobs[0];

if (start.w * start.h * start.d * start.c != 1 || limit.w * limit.h * limit.d * limit.c != 1 || (!delta.empty() && delta.w * delta.h * delta.d * delta.c != 1))
return -100;

if (start.elemsize != limit.elemsize || (!delta.empty() && start.elemsize != delta.elemsize))
return -100;

const float* start_ptr = start;
const float* limit_ptr = limit;

float start_val = start_ptr[0];
float limit_val = limit_ptr[0];
float delta_val = 1.0f;
if (!delta.empty())
{
const float* delta_ptr = delta;
delta_val = delta_ptr[0];
}

if (delta_val == 0.0f || (limit_val - start_val) * delta_val <= 0.0f)
return -100;

if (limit_val < start_val && delta_val > 0.0f)
delta_val = -delta_val;

int number_of_elements = static_cast<int>(ceil((limit_val - start_val) / delta_val));
if (number_of_elements < 0)
number_of_elements = 0;

output.create(number_of_elements, start.elemsize, start.elempack, opt.blob_allocator);
if (output.empty())
return -100;

float* outptr = output;

#pragma omp parallel for num_threads(opt.num_threads)
for (int i = 0; i < number_of_elements; i++)
{
((float*)outptr)[i] = start_val + (i * delta_val);
}

return 0;
}

} // namespace ncnn
32 changes: 32 additions & 0 deletions src/layer/range.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// 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 LAYER_RANGE_H
#define LAYER_RANGE_H

#include "layer.h"

namespace ncnn {

class Range : public Layer
{
public:
Range();

virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const;
};

} // namespace ncnn

#endif // LAYER_RANGE_H
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ ncnn_add_layer_test(Power)
ncnn_add_layer_test(PReLU)
ncnn_add_layer_test(PriorBox)
ncnn_add_layer_test(Quantize)
ncnn_add_layer_test(Range)
ncnn_add_layer_test(Reduction)
ncnn_add_layer_test(ReLU)
ncnn_add_layer_test(Reorg)
Expand Down
76 changes: 76 additions & 0 deletions tests/test_range.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// 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 "layer/range.h"
#include "testutil.h"

static int test_range(const ncnn::Mat& _a, const ncnn::Mat& _b, const ncnn::Mat& _c)
{
ncnn::Mat a = _a;
ncnn::Mat b = _b;
ncnn::Mat c = _c;

// the values should be greater than 0
a = a.clone();
Randomize(a, 0.0f, 100.0f);
float* a_ptr = a;
b = b.clone();
Randomize(b, a_ptr[0] + 10.0f, a_ptr[0] + 100.0f);
if (!c.empty())
{
c = c.clone();
Randomize(c, 1.0f, 5.0f);
}
ncnn::ParamDict pd;

std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> inputs(!c.empty() ? 3 : 2);
inputs[0] = a;
inputs[1] = b;
if (!c.empty())
inputs[2] = c;

int ret = test_layer<ncnn::Range>("Range", pd, weights, inputs);
if (ret != 0)
{
fprintf(stderr, "test_range failed a.dims=%d a=(%d %d %d %d), b.dims=%d, b=(%d, %d, %d, %d), c.dims=%d, c=(%d, %d, %d, %d)\n", a.dims, a.w, a.h, a.d, a.c, b.dims, b.w, b.h, b.d, b.c, b.dims, b.w, b.h, b.d, b.c);
}

return ret;
}

static int test_range_0()
{
return 0
|| test_range(RandomMat(1), RandomMat(1), ncnn::Mat())
|| test_range(RandomMat(1), RandomMat(1), ncnn::Mat())
|| test_range(RandomMat(1), RandomMat(1), ncnn::Mat());
}

static int test_range_1()
{
return 0
|| test_range(RandomMat(1), RandomMat(1), RandomMat(1))
|| test_range(RandomMat(1), RandomMat(1), RandomMat(1))
|| test_range(RandomMat(1), RandomMat(1), RandomMat(1));
}

int main()
{
SRAND(7767517);

return 0
|| test_range_0()
|| test_range_1();
}
8 changes: 8 additions & 0 deletions tools/onnx/onnx2ncnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3862,6 +3862,10 @@ int main(int argc, char** argv)
{
fprintf(pp, "%-16s", "PReLU");
}
else if (op == "Range")
{
fprintf(pp, "%-16s", "Range");
}
else if (op == "Reciprocal")
{
fprintf(pp, "%-16s", "UnaryOp");
Expand Down Expand Up @@ -5469,6 +5473,10 @@ int main(int argc, char** argv)

fwrite_tensor_proto_data(slope, bp);
}
else if (op == "Range")
{
// no param
}
else if (op == "Reciprocal")
{
int op_type = 15;
Expand Down