提交 941a3192 authored 作者: notoraptor's avatar notoraptor

Wrap Op params for theano.gpuarray.dnn.GpuDnnBatchNorm.

上级 ce92345f
...@@ -894,7 +894,7 @@ If you pass a function name to the ``__init__()`` method of the ...@@ -894,7 +894,7 @@ If you pass a function name to the ``__init__()`` method of the
theano Types) of your inputs and outputs. theano Types) of your inputs and outputs.
* You can sepcify the number of inputs and outputs for your op * You can sepcify the number of inputs and outputs for your op
by setting the `_cop_num_inputs` and `_cop_num_outputs` by setting the ``_cop_num_inputs`` and ``_cop_num_outputs``
attributes on your op. The main function will always be attributes on your op. The main function will always be
called with that number of arguments, using NULL to fill in called with that number of arguments, using NULL to fill in
for missing values at the end. This can be used if your op for missing values at the end. This can be used if your op
......
...@@ -1666,13 +1666,20 @@ class GpuDnnBatchNorm(DnnBase): ...@@ -1666,13 +1666,20 @@ class GpuDnnBatchNorm(DnnBase):
__props__ = ('mode', 'running_averages', 'inplace_running_mean', __props__ = ('mode', 'running_averages', 'inplace_running_mean',
'inplace_running_var', 'inplace_output') 'inplace_running_var', 'inplace_output')
check_input = False
params_type = ParamsType(mode=cudnn.cudnnBatchNormMode_t,
inplace_output=bool_t,
inplace_running_mean=bool_t,
inplace_running_var=bool_t,
handle=handle_type)
def __init__(self, mode='per-activation', running_averages=False, def __init__(self, mode='per-activation', running_averages=False,
inplace_running_mean=False, inplace_running_var=False, inplace_running_mean=False, inplace_running_var=False,
inplace_output=False): inplace_output=False):
DnnBase.__init__(self, ['dnn_batchnorm_base.c', 'dnn_batchnorm.c'], DnnBase.__init__(self, ['dnn_batchnorm_base.c', 'dnn_batchnorm.c'],
'dnn_batchnorm_op') 'dnn_batchnorm_op')
assert (mode in ('per-activation', 'spatial')) assert cudnn.cudnnBatchNormMode_t.has_alias(mode)
self.mode = mode self.mode = mode
self.running_averages = running_averages self.running_averages = running_averages
self.inplace_output = inplace_output self.inplace_output = inplace_output
...@@ -1700,24 +1707,12 @@ class GpuDnnBatchNorm(DnnBase): ...@@ -1700,24 +1707,12 @@ class GpuDnnBatchNorm(DnnBase):
self.inplace_output = False self.inplace_output = False
self.destroy_map = {} self.destroy_map = {}
def get_op_params(self):
params = []
if self.inplace_output:
params.append(('INPLACE_OUTPUT', '1'))
if self.running_averages:
params.append(('RUNNING_AVERAGES', '1'))
if self.inplace_running_mean:
params.append(('INPLACE_RUNNING_MEAN', '1'))
if self.inplace_running_var:
params.append(('INPLACE_RUNNING_VAR', '1'))
params.append(('MODE', ("CUDNN_BATCHNORM_SPATIAL"
if self.mode == "spatial"
else "CUDNN_BATCHNORM_PER_ACTIVATION")))
return params
def infer_shape(self, node, shape): def infer_shape(self, node, shape):
return [shape[0]] + [shape[1]] * (len(node.outputs) - 1) return [shape[0]] + [shape[1]] * (len(node.outputs) - 1)
_cop_num_inputs = 7
_cop_num_outputs = 5
def make_node(self, x, scale, bias, epsilon=1e-4, def make_node(self, x, scale, bias, epsilon=1e-4,
running_average_factor=0.1, running_average_factor=0.1,
running_mean=None, running_var=None): running_mean=None, running_var=None):
......
...@@ -3,18 +3,17 @@ ...@@ -3,18 +3,17 @@
int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale, int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale,
PyGpuArrayObject *bias, npy_float64 epsilon, PyGpuArrayObject *bias, npy_float64 epsilon,
npy_float64 running_average_factor, npy_float64 running_average_factor,
#ifdef RUNNING_AVERAGES PyGpuArrayObject *in_running_mean, // may be NULL
PyGpuArrayObject *in_running_mean, PyGpuArrayObject *in_running_var, // may be NULL
PyGpuArrayObject *in_running_var,
#endif
PyGpuArrayObject **outp, PyGpuArrayObject **outp,
PyGpuArrayObject **x_mean, PyGpuArrayObject **x_mean,
PyGpuArrayObject **x_invstd, PyGpuArrayObject **x_invstd,
#ifdef RUNNING_AVERAGES PyGpuArrayObject **out_running_mean, // may be NULL
PyGpuArrayObject **out_running_mean, PyGpuArrayObject **out_running_var, // may be NULL
PyGpuArrayObject **out_running_var, PARAMS_TYPE* params) {
#endif /* Note: based on Python code, in_running_mean, in_running_var, out_running_mean and out_running_var
cudnnHandle_t _handle) { are together NULL (or not NULL) at same time, so we just need to check only one of them. */
bool running_averages = (in_running_mean != NULL);
PyGpuContextObject *c = inp->context; PyGpuContextObject *c = inp->context;
if (c_set_tensorNd(inp, bn_input) != 0) if (c_set_tensorNd(inp, bn_input) != 0)
...@@ -27,14 +26,14 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale, ...@@ -27,14 +26,14 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale,
return 1; return 1;
} }
#ifdef INPLACE_OUTPUT if (params->inplace_output) {
Py_XDECREF(*outp); Py_XDECREF(*outp);
*outp = inp; *outp = inp;
Py_INCREF(*outp); Py_INCREF(*outp);
#else } else if (theano_prep_output(outp, inp->ga.nd, inp->ga.dimensions, inp->ga.typecode, GA_C_ORDER, c) != 0) {
if (theano_prep_output(outp, inp->ga.nd, inp->ga.dimensions, inp->ga.typecode, GA_C_ORDER, c) != 0)
return 1; return 1;
#endif }
if (theano_prep_output(x_mean, scale->ga.nd, scale->ga.dimensions, scale->ga.typecode, GA_C_ORDER, c) != 0) if (theano_prep_output(x_mean, scale->ga.nd, scale->ga.dimensions, scale->ga.typecode, GA_C_ORDER, c) != 0)
return 1; return 1;
if (theano_prep_output(x_invstd, scale->ga.nd, scale->ga.dimensions, scale->ga.typecode, GA_C_ORDER, c) != 0) if (theano_prep_output(x_invstd, scale->ga.nd, scale->ga.dimensions, scale->ga.typecode, GA_C_ORDER, c) != 0)
...@@ -43,30 +42,32 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale, ...@@ -43,30 +42,32 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale,
if (c_set_tensorNd(*outp, bn_output) != 0) if (c_set_tensorNd(*outp, bn_output) != 0)
return 1; return 1;
#ifdef RUNNING_AVERAGES PyGpuArrayObject *running_mean = NULL;
#ifdef INPLACE_RUNNING_MEAN PyGpuArrayObject *running_var = NULL;
Py_XDECREF(*out_running_mean); if (running_averages) {
PyGpuArrayObject *running_mean = in_running_mean; if (params->inplace_running_mean) {
Py_INCREF(running_mean); Py_XDECREF(*out_running_mean);
#else running_mean = in_running_mean;
PyGpuArrayObject *running_mean = *out_running_mean; Py_INCREF(running_mean);
running_mean = theano_try_copy(running_mean, in_running_mean); } else {
if (running_mean == NULL) { running_mean = *out_running_mean;
return 1; running_mean = theano_try_copy(running_mean, in_running_mean);
} if (running_mean == NULL) {
#endif return 1;
#ifdef INPLACE_RUNNING_VAR }
Py_XDECREF(*out_running_var); }
PyGpuArrayObject *running_var = in_running_var; if (params->inplace_running_var) {
Py_INCREF(running_var); Py_XDECREF(*out_running_var);
#else running_var = in_running_var;
PyGpuArrayObject *running_var = *out_running_var; Py_INCREF(running_var);
running_var = theano_try_copy(running_var, in_running_var); } else {
if (running_var == NULL) { running_var = *out_running_var;
return 1; running_var = theano_try_copy(running_var, in_running_var);
if (running_var == NULL) {
return 1;
}
}
} }
#endif
#endif
{ {
const float falpha = 1.; const float falpha = 1.;
...@@ -83,8 +84,8 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale, ...@@ -83,8 +84,8 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale,
beta = (void *)&fbeta; beta = (void *)&fbeta;
} }
cudnnStatus_t err = cudnnBatchNormalizationForwardTraining( cudnnStatus_t err = cudnnBatchNormalizationForwardTraining(
_handle, params->handle,
MODE, params->mode,
alpha, alpha,
beta, beta,
bn_input, bn_input,
...@@ -94,15 +95,9 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale, ...@@ -94,15 +95,9 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale,
bn_params, bn_params,
PyGpuArray_DEV_DATA(scale), PyGpuArray_DEV_DATA(scale),
PyGpuArray_DEV_DATA(bias), PyGpuArray_DEV_DATA(bias),
#ifdef RUNNING_AVERAGES running_averages ? running_average_factor : 0,
running_average_factor, running_averages ? PyGpuArray_DEV_DATA(running_mean) : NULL,
PyGpuArray_DEV_DATA(running_mean), running_averages ? PyGpuArray_DEV_DATA(running_var): NULL,
PyGpuArray_DEV_DATA(running_var),
#else
0,
NULL, // running mean, deliberately unused
NULL, // running var, deliberately unused
#endif
epsilon, epsilon,
PyGpuArray_DEV_DATA(*x_mean), PyGpuArray_DEV_DATA(*x_mean),
PyGpuArray_DEV_DATA(*x_invstd) PyGpuArray_DEV_DATA(*x_invstd)
...@@ -112,10 +107,10 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale, ...@@ -112,10 +107,10 @@ int dnn_batchnorm_op(PyGpuArrayObject *inp, PyGpuArrayObject *scale,
cudnnGetErrorString(err)); cudnnGetErrorString(err));
return 1; return 1;
} }
#ifdef RUNNING_AVERAGES if (running_averages) {
*out_running_mean = running_mean; *out_running_mean = running_mean;
*out_running_var = running_var; *out_running_var = running_var;
#endif }
} }
return 0; return 0;
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论