|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- // 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 "softmax.h"
- #include <float.h>
- #include <math.h>
- #include <algorithm>
-
- namespace ncnn {
-
- DEFINE_LAYER_CREATOR(Softmax)
-
- Softmax::Softmax()
- {
- one_blob_only = true;
- support_inplace = true;
- }
-
- int Softmax::load_param(const ParamDict& pd)
- {
- axis = pd.get(0, 0);
-
- return 0;
- }
-
- int Softmax::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
- {
- // value = exp( value - global max value )
- // sum all value
- // value = value / sum
-
- int dims = bottom_top_blob.dims;
- size_t elemsize = bottom_top_blob.elemsize;
-
- if (dims == 1) // axis == 0
- {
- int w = bottom_top_blob.w;
-
- float* ptr = bottom_top_blob;
-
- float max = -FLT_MAX;
- for (int i=0; i<w; i++)
- {
- max = std::max(max, ptr[i]);
- }
-
- for (int i=0; i<w; i++)
- {
- ptr[i] = exp(ptr[i] - max);
- }
-
- float sum = 0.f;
- for (int i=0; i<w; i++)
- {
- sum += ptr[i];
- }
-
- for (int i=0; i<w; i++)
- {
- ptr[i] /= sum;
- }
-
- return 0;
- }
-
- if (dims == 2 && axis == 0)
- {
- int w = bottom_top_blob.w;
- int h = bottom_top_blob.h;
-
- Mat max;
- max.create(w, elemsize, opt.workspace_allocator);
- if (max.empty())
- return -100;
- max.fill(-FLT_MAX);
-
- for (int i=0; i<h; i++)
- {
- const float* ptr = bottom_top_blob.row(i);
- for (int j=0; j<w; j++)
- {
- max[j] = std::max(max[j], ptr[j]);
- }
- }
-
- for (int i=0; i<h; i++)
- {
- float* ptr = bottom_top_blob.row(i);
- for (int j=0; j<w; j++)
- {
- ptr[j] = exp(ptr[j] - max[j]);
- }
- }
-
- Mat sum;
- sum.create(w, elemsize, opt.workspace_allocator);
- if (sum.empty())
- return -100;
- sum.fill(0.f);
-
- for (int i=0; i<h; i++)
- {
- const float* ptr = bottom_top_blob.row(i);
- for (int j=0; j<w; j++)
- {
- sum[j] += ptr[j];
- }
- }
-
- for (int i=0; i<h; i++)
- {
- float* ptr = bottom_top_blob.row(i);
- for (int j=0; j<w; j++)
- {
- ptr[j] /= sum[j];
- }
- }
-
- return 0;
- }
-
- if (dims == 2 && axis == 1)
- {
- int w = bottom_top_blob.w;
- int h = bottom_top_blob.h;
-
- Mat max;
- max.create(h, elemsize, opt.workspace_allocator);
- if (max.empty())
- return -100;
-
- for (int i=0; i<h; i++)
- {
- const float* ptr = bottom_top_blob.row(i);
-
- float m = -FLT_MAX;
- for (int j=0; j<w; j++)
- {
- m = std::max(m, ptr[j]);
- }
-
- max[i] = m;
- }
-
- for (int i=0; i<h; i++)
- {
- float* ptr = bottom_top_blob.row(i);
-
- float m = max[i];
- for (int j=0; j<w; j++)
- {
- ptr[j] = exp(ptr[j] - m);
- }
- }
-
- Mat sum;
- sum.create(h, elemsize, opt.workspace_allocator);
- if (sum.empty())
- return -100;
-
- for (int i=0; i<h; i++)
- {
- const float* ptr = bottom_top_blob.row(i);
-
- float s = 0.f;
- for (int j=0; j<w; j++)
- {
- s += ptr[j];
- }
-
- sum[i] = s;
- }
-
- for (int i=0; i<h; i++)
- {
- float* ptr = bottom_top_blob.row(i);
-
- float s = sum[i];
- for (int j=0; j<w; j++)
- {
- ptr[j] /= s;
- }
- }
-
- return 0;
- }
-
- if (dims == 3 && axis == 0)
- {
- int w = bottom_top_blob.w;
- int h = bottom_top_blob.h;
- int channels = bottom_top_blob.c;
- int size = w * h;
-
- Mat max;
- max.create(w, h, elemsize, opt.workspace_allocator);
- if (max.empty())
- return -100;
- max.fill(-FLT_MAX);
- for (int q=0; q<channels; q++)
- {
- const float* ptr = bottom_top_blob.channel(q);
-
- for (int i=0; i<size; i++)
- {
- max[i] = std::max(max[i], ptr[i]);
- }
- }
-
- #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] = exp(ptr[i] - max[i]);
- }
- }
-
- Mat sum;
- sum.create(w, h, elemsize, opt.workspace_allocator);
- if (sum.empty())
- return -100;
- sum.fill(0.f);
- for (int q=0; q<channels; q++)
- {
- const float* ptr = bottom_top_blob.channel(q);
-
- for (int i=0; i<size; i++)
- {
- sum[i] += ptr[i];
- }
- }
-
- #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] /= sum[i];
- }
- }
-
- return 0;
- }
-
- if (dims == 3 && axis == 1)
- {
- int w = bottom_top_blob.w;
- int h = bottom_top_blob.h;
- int channels = bottom_top_blob.c;
-
- Mat max;
- max.create(h, channels, elemsize, opt.workspace_allocator);
- if (max.empty())
- return -100;
- max.fill(-FLT_MAX);
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- const float* ptr = bottom_top_blob.channel(q);
- float* maxptr = max.row(q);
-
- for (int i=0; i<h; i++)
- {
- float max = -FLT_MAX;
- for (int j=0; j<w; j++)
- {
- max = std::max(max, ptr[j]);
- }
-
- maxptr[i] = max;
- ptr += w;
- }
- }
-
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- float* ptr = bottom_top_blob.channel(q);
- float* maxptr = max.row(q);
-
- for (int i=0; i<h; i++)
- {
- float max = maxptr[i];
- for (int j=0; j<w; j++)
- {
- ptr[j] = exp(ptr[j] - max);
- }
-
- ptr += w;
- }
- }
-
- Mat sum;
- sum.create(h, channels, elemsize, opt.workspace_allocator);
- if (sum.empty())
- return -100;
- sum.fill(0.f);
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- const float* ptr = bottom_top_blob.channel(q);
- float* sumptr = sum.row(q);
-
- for (int i=0; i<h; i++)
- {
- float sum = 0.f;
- for (int j=0; j<w; j++)
- {
- sum += ptr[j];
- }
-
- sumptr[i] = sum;
- ptr += w;
- }
- }
-
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- float* ptr = bottom_top_blob.channel(q);
- float* sumptr = sum.row(q);
-
- for (int i=0; i<h; i++)
- {
- float sum = sumptr[i];
- for (int j=0; j<w; j++)
- {
- ptr[j] /= sum;
- }
-
- ptr += w;
- }
- }
-
- return 0;
- }
-
- if (dims == 3 && axis == 2)
- {
- int w = bottom_top_blob.w;
- int h = bottom_top_blob.h;
- int channels = bottom_top_blob.c;
-
- Mat max;
- max.create(w, channels, elemsize, opt.workspace_allocator);
- if (max.empty())
- return -100;
- max.fill(-FLT_MAX);
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- const float* ptr = bottom_top_blob.channel(q);
- float* maxptr = max.row(q);
-
- for (int i=0; i<h; i++)
- {
- for (int j=0; j<w; j++)
- {
- maxptr[j] = std::max(maxptr[j], ptr[j]);
- }
-
- ptr += w;
- }
- }
-
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- float* ptr = bottom_top_blob.channel(q);
- float* maxptr = max.row(q);
-
- for (int i=0; i<h; i++)
- {
- for (int j=0; j<w; j++)
- {
- ptr[j] = exp(ptr[j] - maxptr[j]);
- }
-
- ptr += w;
- }
- }
-
- Mat sum;
- sum.create(w, channels, elemsize, opt.workspace_allocator);
- if (sum.empty())
- return -100;
- sum.fill(0.f);
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- const float* ptr = bottom_top_blob.channel(q);
- float* sumptr = sum.row(q);
-
- for (int i=0; i<h; i++)
- {
- for (int j=0; j<w; j++)
- {
- sumptr[j] += ptr[j];
- }
-
- ptr += w;
- }
- }
-
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- float* ptr = bottom_top_blob.channel(q);
- float* sumptr = sum.row(q);
-
- for (int i=0; i<h; i++)
- {
- for (int j=0; j<w; j++)
- {
- ptr[j] /= sumptr[j];
- }
-
- ptr += w;
- }
- }
-
- return 0;
- }
-
- return 0;
- }
-
- } // namespace ncnn
|