Skip to content

Commit

Permalink
Merge branch 'Tencent:master' into arm-conv-unified-elempack-3
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui authored Sep 14, 2023
2 parents 79513f8 + a22998b commit d09e84b
Show file tree
Hide file tree
Showing 37 changed files with 1,943 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
python-version: '3.x'

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
uses: docker/setup-qemu-action@v3
with:
platforms: all

Expand Down
26 changes: 26 additions & 0 deletions docs/developer-guide/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [BinaryOp](#binaryop)
* [BNLL](#bnll)
* [Cast](#cast)
* [CELU](#celu)
* [Clip](#clip)
* [Concat](#concat)
* [Convolution](#convolution)
Expand All @@ -25,6 +26,7 @@
* [DeconvolutionDepthWise3D](#deconvolutiondepthwise3d)
* [DeformableConv2D](#deformableconv2d)
* [Dequantize](#dequantize)
* [Diag](#diag)
* [Dropout](#dropout)
* [Eltwise](#eltwise)
* [ELU](#elu)
Expand Down Expand Up @@ -196,6 +198,19 @@ Element type:
- 3 = int8
- 4 = bfloat16

# CELU
```
if x < 0 y = (exp(x / alpha) - 1.f) * alpha
else y = x
```

* one_blob_only
* support_inplace

| param id | name | type | default | description |
| --------- | ------------- | ----- | --------- | ----------------- |
| 0 | alpha | float | 1.f | |

# Clip
```
y = clamp(x, min, max)
Expand Down Expand Up @@ -749,6 +764,17 @@ y = x * scale + bias
| scale_data | float | [scale_data_size] |
| bias_data | float | [bias_data_size] |

# Diag
```
y = diag(x, diagonal)
```

* one_blob_only

| param id | name | type | default | description |
| --------- | ------------- | ----- | --------- | ----------------- |
| 0 | diagonal | int | 0 | |

# Dropout
```
y = x * scale
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ ncnn_add_layer(Unfold)
ncnn_add_layer(GridSample)
ncnn_add_layer(CumulativeSum)
ncnn_add_layer(CopyTo)
ncnn_add_layer(Erf)
ncnn_add_layer(Diag)
ncnn_add_layer(CELU)

if(NCNN_VULKAN)
ncnn_add_shader(${CMAKE_CURRENT_SOURCE_DIR}/convert_ycbcr.comp)
Expand Down
57 changes: 57 additions & 0 deletions src/layer/celu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2023 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 "celu.h"

#include <math.h>

namespace ncnn {

CELU::CELU()
{
one_blob_only = true;
support_inplace = true;
}

int CELU::load_param(const ParamDict& pd)
{
alpha = pd.get(0, 1.f);

return 0;
}

int CELU::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
{
int w = bottom_top_blob.w;
int h = bottom_top_blob.h;
int d = bottom_top_blob.d;
int channels = bottom_top_blob.c;
int size = w * h * d;

#pragma omp parallel for num_threads(opt.num_threads)
for (int q = 0; q < channels; q++)
{
float* ptr = bottom_top_blob.channel(q);

for (int i = 0; i < size; i++)
{
if (ptr[i] < 0.f)
ptr[i] = (expf(ptr[i] / alpha) - 1.f) * alpha;
}
}

return 0;
}

} // namespace ncnn
37 changes: 37 additions & 0 deletions src/layer/celu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2023 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_CELU_H
#define LAYER_CELU_H

#include "layer.h"

namespace ncnn {

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

virtual int load_param(const ParamDict& pd);

virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;

public:
float alpha;
};

} // namespace ncnn

#endif // LAYER_CELU_H
91 changes: 91 additions & 0 deletions src/layer/diag.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2023 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 "diag.h"

namespace ncnn {

Diag::Diag()
{
one_blob_only = true;
support_inplace = false;
}

int Diag::load_param(const ParamDict& pd)
{
diagonal = pd.get(0, 0);

return 0;
}

int Diag::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
{
int dims = bottom_blob.dims;
size_t elemsize = bottom_blob.elemsize;

if (dims == 1)
{
int w = bottom_blob.w;
int top_w = w + ((diagonal >= 0) ? diagonal : -diagonal);

top_blob.create(top_w, top_w, elemsize, opt.blob_allocator);
if (top_blob.empty())
return -100;

top_blob.fill(0.0f);

int bias_r = -std::min(diagonal, 0);
int bias_c = std::max(diagonal, 0);

for (int i = 0; i < w; i++)
{
top_blob.row(i + bias_r)[i + bias_c] = bottom_blob[i];
}
}
if (dims == 2)
{
int w = bottom_blob.w;
int h = bottom_blob.h;

int len = 0;
int minimum = std::min(w - h, 0);
int maximum = std::max(w - h, 0);
if (diagonal <= maximum && diagonal >= minimum)
len = std::min(w, h);
else if (diagonal > -h && diagonal < minimum)
len = diagonal + h;
else if (diagonal > maximum && diagonal < w)
len = -diagonal + w;

top_blob.create(len, elemsize, opt.blob_allocator);
if (top_blob.empty())
{
if (len == 0)
return 0;
return -100;
}

int bias_r = -std::min(diagonal, 0);
int bias_c = std::max(diagonal, 0);

for (int i = 0; i < len; i++)
{
top_blob[i] = bottom_blob.row(i + bias_r)[i + bias_c];
}
}

return 0;
}

} // namespace ncnn
37 changes: 37 additions & 0 deletions src/layer/diag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2023 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_DIAG_H
#define LAYER_DIAG_H

#include "layer.h"

namespace ncnn {

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

virtual int load_param(const ParamDict& pd);

virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const;

public:
int diagonal;
};

} // namespace ncnn

#endif // LAYER_DIAG_H
47 changes: 47 additions & 0 deletions src/layer/erf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 "erf.h"
#include <math.h>

namespace ncnn {

Erf::Erf()
{
one_blob_only = true;
support_inplace = true;
}

int Erf::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
{
int w = bottom_top_blob.w;
int h = bottom_top_blob.h;
int channels = bottom_top_blob.c;
int size = w * h;

#pragma omp parallel for num_threads(opt.num_threads)
for (int q = 0; q < channels; q++)
{
float* ptr = bottom_top_blob.channel(q);

for (int i = 0; i < size; i++)
{
ptr[i] = erf(ptr[i]);
}
}

return 0;
}

} // namespace ncnn
32 changes: 32 additions & 0 deletions src/layer/erf.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_ERF_H
#define LAYER_ERF_H

#include "layer.h"

namespace ncnn {

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

virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;
};

} // namespace ncnn

#endif // LAYER_ERF_H
Loading

0 comments on commit d09e84b

Please sign in to comment.