From eb2123e12dc0ce1f6920aefa12b684f01bf9ca17 Mon Sep 17 00:00:00 2001 From: dengkaipeng Date: Wed, 27 Mar 2019 06:17:28 +0000 Subject: [PATCH] fix doc and jit. test=develop --- paddle/fluid/API.spec | 2 +- paddle/fluid/operators/jit/kernel_base.h | 4 ++-- paddle/fluid/operators/jit/more/mix/mix.cc | 5 +++-- paddle/fluid/operators/jit/more/mkl/mkl.h | 1 + paddle/fluid/operators/jit/refer/refer.h | 1 + paddle/fluid/operators/jit/test.cc | 6 ++---- python/paddle/fluid/layers/nn.py | 5 ++++- 7 files changed, 14 insertions(+), 10 deletions(-) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 51c3c7bbf97..6b6081d2cd8 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -86,7 +86,7 @@ paddle.fluid.layers.conv2d (ArgSpec(args=['input', 'num_filters', 'filter_size', paddle.fluid.layers.conv3d (ArgSpec(args=['input', 'num_filters', 'filter_size', 'stride', 'padding', 'dilation', 'groups', 'param_attr', 'bias_attr', 'use_cudnn', 'act', 'name'], varargs=None, keywords=None, defaults=(1, 0, 1, None, None, None, True, None, None)), ('document', '37042620f9bd3a2da6e5d3138b2f724b')) paddle.fluid.layers.sequence_pool (ArgSpec(args=['input', 'pool_type', 'is_test'], varargs=None, keywords=None, defaults=(False,)), ('document', 'a194fb80614023f543df3949fbd0d0b8')) paddle.fluid.layers.sequence_softmax (ArgSpec(args=['input', 'use_cudnn', 'name'], varargs=None, keywords=None, defaults=(False, None)), ('document', '19ef6f9cdd27feac8a1ae060f19c10b4')) -paddle.fluid.layers.softmax (ArgSpec(args=['input', 'use_cudnn', 'name', 'axis'], varargs=None, keywords=None, defaults=(False, None, -1)), ('document', '502bad9e8bc7ef24817d0d4b20f61df3')) +paddle.fluid.layers.softmax (ArgSpec(args=['input', 'use_cudnn', 'name', 'axis'], varargs=None, keywords=None, defaults=(False, None, -1)), ('document', '59b1c6bf2f0fa9dc649c85fef3a3b2ea')) paddle.fluid.layers.pool2d (ArgSpec(args=['input', 'pool_size', 'pool_type', 'pool_stride', 'pool_padding', 'global_pooling', 'use_cudnn', 'ceil_mode', 'name', 'exclusive'], varargs=None, keywords=None, defaults=(-1, 'max', 1, 0, False, True, False, None, True)), ('document', 'bbd84e855e660cd1084bb71a2fd0cdaa')) paddle.fluid.layers.pool3d (ArgSpec(args=['input', 'pool_size', 'pool_type', 'pool_stride', 'pool_padding', 'global_pooling', 'use_cudnn', 'ceil_mode', 'name', 'exclusive'], varargs=None, keywords=None, defaults=(-1, 'max', 1, 0, False, True, False, None, True)), ('document', '043de7333b79ee0ac55053c14ed81625')) paddle.fluid.layers.adaptive_pool2d (ArgSpec(args=['input', 'pool_size', 'pool_type', 'require_index', 'name'], varargs=None, keywords=None, defaults=('max', False, None)), ('document', '859b887174d06f361658f69cb7c06d95')) diff --git a/paddle/fluid/operators/jit/kernel_base.h b/paddle/fluid/operators/jit/kernel_base.h index fdd41a830a2..6e0393b820f 100644 --- a/paddle/fluid/operators/jit/kernel_base.h +++ b/paddle/fluid/operators/jit/kernel_base.h @@ -38,6 +38,8 @@ typedef enum { kNCHW16CMulNC, kSeqPool, kSoftmax, + kStrideASum, + kStrideScal, kVAdd, kVAddBias, kVAddRelu, @@ -53,8 +55,6 @@ typedef enum { kVSquare, kVSub, kVTanh, - kStrideASum, - kStrideScal, } KernelType; typedef enum { diff --git a/paddle/fluid/operators/jit/more/mix/mix.cc b/paddle/fluid/operators/jit/more/mix/mix.cc index 1a9fc9ed7bf..f5b7bfff898 100644 --- a/paddle/fluid/operators/jit/more/mix/mix.cc +++ b/paddle/fluid/operators/jit/more/mix/mix.cc @@ -50,11 +50,12 @@ void VTanh(const T* x, T* y, int n) { compute_addbias(&b, y, y, n); } +// remain is the product of dimension shapes after the axis dimension void Softmax(const T* x, T* y, int n, int bs, int remain) { auto compute_hmax = KernelFuncs, CPUPlace>::Cache().At(n); auto compute_hsum = KernelFuncs, CPUPlace>::Cache().At(n); auto compute_vscal = KernelFuncs, CPUPlace>::Cache().At(n); - auto compute_stridesum = + auto compute_strideasum = KernelFuncs, CPUPlace>::Cache().At(n); auto compute_stridescal = KernelFuncs, CPUPlace>::Cache().At(n); @@ -74,7 +75,7 @@ void Softmax(const T* x, T* y, int n, int bs, int remain) { compute_vscal(&scalar, y, y, n); } else { for (int j = 0; j < remain; ++j) { - compute_stridesum(&y[j], &scalar, n, remain); + compute_strideasum(&y[j], &scalar, n, remain); scalar = static_cast(1) / scalar; compute_stridescal(&scalar, &y[j], &y[j], n, remain); } diff --git a/paddle/fluid/operators/jit/more/mkl/mkl.h b/paddle/fluid/operators/jit/more/mkl/mkl.h index 968895bb6f8..b38cc107b8e 100644 --- a/paddle/fluid/operators/jit/more/mkl/mkl.h +++ b/paddle/fluid/operators/jit/more/mkl/mkl.h @@ -134,6 +134,7 @@ void StrideASum(const T* x, T* res, int n, int stride); template void StrideScal(const T* a, const T* x, T* y, int n, int stride); +// remain is the product of dimension shapes after the axis dimension template void Softmax(const T* x, T* y, int n, int bs, int remain = 1) { std::vector entities(bs); diff --git a/paddle/fluid/operators/jit/refer/refer.h b/paddle/fluid/operators/jit/refer/refer.h index 4aeb2fd6282..136b99e0aef 100644 --- a/paddle/fluid/operators/jit/refer/refer.h +++ b/paddle/fluid/operators/jit/refer/refer.h @@ -432,6 +432,7 @@ void StrideScal(const T* a, const T* x, T* y, int n, int stride) { // y = e^(x - max(x)) // y = y / sum(y) +// remain is the product of dimension shapes after the axis dimension template void Softmax(const T* x, T* y, int n, int bs = 1, int remain = 1) { for (int i = 0; i < bs; ++i) { diff --git a/paddle/fluid/operators/jit/test.cc b/paddle/fluid/operators/jit/test.cc index d8a0b2cbf53..178418f4a75 100644 --- a/paddle/fluid/operators/jit/test.cc +++ b/paddle/fluid/operators/jit/test.cc @@ -798,10 +798,8 @@ template void TestKernelStrideScal() { using T = typename KernelTuple::data_type; VLOG(10) << "Test JITKernel: " << jit::to_string(KernelTuple::kernel_type); - // for (int d : TestSizes()) { - // for (int m : {1, 2, 3}) { // stride - for (int d : {4}) { - for (int m : {2}) { // stride + for (int d : TestSizes()) { + for (int m : {1, 2, 3}) { // stride if (m > d || d % m != 0) { continue; } diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 19c9734a9e0..215720417e6 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -1826,7 +1826,7 @@ def softmax(input, use_cudnn=False, name=None, axis=-1): The dimension :attr:`axis` of the input tensor will be permuted to the last. Then the input tensor will be logically flattened to a 2-D matrix. The matrix's - second dimension(row length) is as same as the dimension :attr:`axis` of the input + second dimension(row length) is the same as the dimension :attr:`axis` of the input tensor, and the first dimension(column length) is the product of all other dimensions of the input tensor. For each row of the matrix, the softmax operator squashes the K-dimensional(K is the width of the matrix, which is also the size @@ -1864,7 +1864,10 @@ def softmax(input, use_cudnn=False, name=None, axis=-1): .. code-block:: python fc = fluid.layers.fc(input=x, size=10) + # perform softmax in the second dimension softmax = fluid.layers.softmax(input=fc, axis=1) + # perform softmax in the last dimension + softmax = fluid.layers.softmax(input=fc, axis=-1) """ helper = LayerHelper('softmax', **locals()) -- GitLab