Skip to content

Commit

Permalink
Merge branch 'master' into feature/20180401-file-format-converter
Browse files Browse the repository at this point in the history
  • Loading branch information
YukioOobuchi committed Apr 19, 2018
2 parents 24b86ad + d7d7918 commit d8db4b2
Show file tree
Hide file tree
Showing 12 changed files with 828 additions and 1 deletion.
6 changes: 6 additions & 0 deletions build-tools/code_generator/function_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ MatrixDiagPart:
Dropout:
float: [float]
half: [Half]
TopKData:
float: [float]
half: [Half]
TopKGrad:
float: [float]
half: [Half]
Rand:
float: [float]
half: [Half]
Expand Down
76 changes: 76 additions & 0 deletions build-tools/code_generator/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,82 @@ Stochasticity:
outputs:
y:
doc: N-D array with the same shape as x
TopKData:
snake_name: top_k_data
doc: |2
Select the `k` largest values from each sample in `x` to
propagate unmodified and set all other values to 0. If `abs` is
True, the `k` largest values are selected by magnitude. If
`reduce` is True (the default), all feature dimensions are
reduced to a single dimension of size `k` that propagates only
the `k` largest values. Otherwise, if `reduce` is False, input
and output dimensions are identical. Dimensions before
`base_axis` are treated as number of sample dimensions and `k`
values get selected from all elements of a sample (dimensions
from `base_axis`) regardless of shape.
>>> import nnabla as nn, nnabla.functions as F
>>> x = nn.Variable((4, 5, 6))
>>> F.top_k_data(x, 3, reduce=False).shape
(4, 5, 6)
>>> F.top_k_data(x, 3, reduce=True).shape
(4, 3)
>>> F.top_k_data(x, 3, reduce=True, base_axis=2).shape
(4, 5, 3)
inputs:
x:
doc: N-D array
arguments:
k:
doc: Number of largest data values to propgate.
type: int64
abs:
doc: Determine largest data values by magnitude.
type: bool
default: 'False'
reduce:
doc: Reduce feature size to one dimension of size `k`.
type: bool
default: 'True'
base_axis:
doc: First dimension of the sample shape.
type: int64
default: '1'
outputs:
y:
doc: N-D array.
TopKGrad:
snake_name: top_k_grad
doc: |2
Select the `k` largest gradients for each sample in `x` to
back-propagate unmodified and set all other gradients to 0. If
`abs` is True, the `k` largest gradients are selected by
magnitude. Dimensions before `base_axis` are treated as number
of sample dimensions and `k` gradients get selected from all
gradients of a sample (dimensions from `base_axis`) regardless
of shape.
inputs:
x:
doc: N-D array
arguments:
k:
doc: Number of largest gradients to propgate.
type: int64
abs:
doc: Determine largest gradients by magnitude.
type: bool
default: 'False'
base_axis:
doc: First dimension of the sample shape.
type: int64
default: '1'
outputs:
y:
doc: N-D array with same shape and data as `x`.
Rand:
snake_name: rand
doc: |2
Expand Down
114 changes: 114 additions & 0 deletions doc/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3876,6 +3876,120 @@ Note:
- N-D array with the same shape as x
-

TopKData
^^^^^^^^

Select the `k` largest values from each sample in `x` to propagate
unmodified and set all other values to 0. If `abs` is True, the `k`
largest values are selected by magnitude. If `reduce` is True (the
default), all feature dimensions are reduced to a single dimension of
size `k` that propagates only the `k` largest values. Otherwise, if
`reduce` is False, input and output dimensions are identical.
Dimensions before `base_axis` are treated as number of sample
dimensions and `k` values get selected from all elements of a
sample (dimensions from `base_axis`) regardless of shape.

* Input(s)

.. list-table::

* - Name
- Description
- Options
* - x
- N-D array
-

* Argument(s)

.. list-table::

* - Name
- Type
- Default
- Description
* - k
- int64
-
- Number of largest data values to propgate.
* - abs
- bool
- False
- Determine largest data values by magnitude.
* - reduce
- bool
- True
- Reduce feature size to one dimension of size `k`.
* - base_axis
- int64
- 1
- First dimension of the sample shape.

* Output(s)

.. list-table::

* - Name
- Description
- Options
* - y
- N-D array.
-

TopKGrad
^^^^^^^^

Select the `k` largest gradients for each sample in `x` to
back-propagate unmodified and set all other gradients to 0.
If `abs` is True, the `k` largest gradients are selected by
magnitude. Dimensions before `base_axis` are treated as number
of sample dimensions and `k` gradients get selected from all
gradients of a sample (dimensions from `base_axis`)
regardless of shape.

* Input(s)

.. list-table::

* - Name
- Description
- Options
* - x
- N-D array
-

* Argument(s)

.. list-table::

* - Name
- Type
- Default
- Description
* - k
- int64
-
- Number of largest data values to propgate.
* - abs
- bool
- False
- Determine largest data values by magnitude.
* - base_axis
- int64
- 1
- First dimension of the sample shape.

* Output(s)

.. list-table::

* - Name
- Description
- Options
* - y
- N-D array with same shape and data as `x`.
-

Rand
^^^^

Expand Down
2 changes: 2 additions & 0 deletions doc/python/api/function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ Stochasticity
.. autofunction:: randint
.. autofunction:: randn
.. autofunction:: dropout
.. autofunction:: top_k_data
.. autofunction:: top_k_grad
.. autofunction:: random_crop
.. autofunction:: random_flip
.. autofunction:: random_shift
Expand Down
3 changes: 2 additions & 1 deletion include/nbla/function/function_impl.hpp.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@

<%
from utils.type_conv import type_from_proto
from collections import OrderedDict
dec_targs = ', '.join(['typename ' + t for t in ttypes])
targs = ', '.join(ttypes)
args = ''.join([', ' + k for k, v in arguments.items()])
args_ = ''.join([', ' + k + '_' for k, v in arguments.items()])

arg_types = {k: type_from_proto[v['type']]['cpp'] for k, v in arguments.items()}
arg_types = OrderedDict([(k, type_from_proto[v['type']]['cpp']) for k, v in arguments.items()])
dec_args = ''.join([', %s %s' % (v, k) for k, v in arg_types.items()])

def get_min(inputs):
Expand Down
98 changes: 98 additions & 0 deletions include/nbla/function/top_k_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) 2017 Sony Corporation. 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 NBLA_FUNCTION_TOP_K_DATA_HPP
#define NBLA_FUNCTION_TOP_K_DATA_HPP

#include <nbla/cpu.hpp>
#include <nbla/function.hpp>
#include <nbla/function_registry.hpp>

namespace nbla {

NBLA_REGISTER_FUNCTION_HEADER(TopKData, int, bool, bool, int);

/** Select the `k` largest values from each sample in `x` to propagate
unmodified and set all other values to 0. If `abs` is True, the `k`
largest values are selected by magnitude. If `reduce` is True (the
default), all feature dimensions are reduced to a single dimension of
size `k` that propagates only the `k` largest values. Otherwise, if
`reduce` is False, input and output dimensions are identical.
Dimensions before `base_axis` are treated as number of sample
dimensions and `k` values get selected from all elements of a
sample (dimensions from `base_axis`) regardless of shape.
Inputs:
- N-D array
Outputs:
- N-D array.
@tparam T Data type for computation.
@param k Number of largest data values to propgate.
@param abs Determine largest data values by magnitude.
@param reduce Reduce feature size to one dimension of size `k`.
@param base_axis First dimension of the sample shape.
\ingroup FunctionImplGrp
*/
template <typename T>
class TopKData : public BaseFunction<int, bool, bool, int> {
protected:
int k_;
bool abs_;
bool reduce_;
int base_axis_;

Size_t ns_; // number of input samples
Size_t ss_; // input sample size
Size_t fs_; // output feature size
Variable top_k_idx_; // top-k indices needed for backprop when reducing
bool forward_done_; // prevent execution of backward before forward

public:
TopKData(const Context &ctx, int k, bool abs, bool reduce, int base_axis)
: BaseFunction(ctx, k, abs, reduce, base_axis), k_(k), abs_(abs),
reduce_(reduce), base_axis_(base_axis) {}
virtual ~TopKData() {}
virtual shared_ptr<Function> copy() const {
return create_TopKData(ctx_, k_, abs_, reduce_, base_axis_);
}
virtual int min_inputs() { return 1; }
virtual int min_outputs() { return 1; }
virtual vector<dtypes> in_types() { return vector<dtypes>{get_dtype<T>()}; }
virtual vector<dtypes> out_types() { return vector<dtypes>{get_dtype<T>()}; }
virtual vector<string> allowed_array_classes() {
return SingletonManager::get<Cpu>()->array_classes();
}
virtual string name() { return "TopKData"; }

protected:
NBLA_API virtual void setup_impl(const Variables &inputs,
const Variables &outputs);
NBLA_API virtual void forward_impl(const Variables &inputs,
const Variables &outputs);
NBLA_API virtual void backward_impl(const Variables &inputs,
const Variables &outputs,
const vector<bool> &propagate_down,
const vector<bool> &accum);
};
}
#endif

0 comments on commit d8db4b2

Please sign in to comment.