提交 192b0e47 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Try to give better shape information about params.

上级 d1e64669
...@@ -2019,6 +2019,7 @@ class _RNNSplitParams(DnnBase): ...@@ -2019,6 +2019,7 @@ class _RNNSplitParams(DnnBase):
void *w; void *w;
void *o; void *o;
ptrdiff_t off; ptrdiff_t off;
size_t bshp;
cudnnStatus_t err; cudnnStatus_t err;
cudnnDataType_t dt; cudnnDataType_t dt;
cudnnTensorFormat_t tf; cudnnTensorFormat_t tf;
...@@ -2084,78 +2085,73 @@ class _RNNSplitParams(DnnBase): ...@@ -2084,78 +2085,73 @@ class _RNNSplitParams(DnnBase):
nshp[0] = PyGpuArray_DIM(%(w)s, 0); nshp[0] = PyGpuArray_DIM(%(w)s, 0);
nshp[1] = 1; nshp[1] = 1;
""" % kw """ % kw
def get_matrix(id, out): def get_params(id, m, b):
kw2 = kw.copy() kw2 = kw.copy()
kw2['id'] = id kw2['id'] = id
kw2['out'] = out kw2['m'] = m
kw2['b'] = b
return """ return """
err = cudnnGetRNNLinLayerMatrixParams(%(handle)s, %(desc)s, %(layer)s, xdesc, wdesc, w, %(id)s, odesc, &o); err = cudnnGetRNNLinLayerBiasParams(%(handle)s, %(desc)s, %(layer)s, xdesc, wdesc, w, %(id)s, odesc, &o);
if (err != CUDNN_STATUS_SUCCESS) { if (err != CUDNN_STATUS_SUCCESS) {
cudnnDestroyTensorDescriptor(xdesc); cudnnDestroyTensorDescriptor(xdesc);
cudnnDestroyFilterDescriptor(wdesc); cudnnDestroyFilterDescriptor(wdesc);
cudnnDestroyFilterDescriptor(odesc); cudnnDestroyFilterDescriptor(odesc);
PyErr_SetString(PyExc_RuntimeError, "can't fetch matrix for id %(id)s"); PyErr_SetString(PyExc_RuntimeError, "can't fetch bias for id %(id)s");
%(fail)s %(fail)s
} }
off = (intptr_t)o - (intptr_t)w; off = (intptr_t)o - (intptr_t)w;
assert(off >= 0 && "matrix"); assert(off >= 0 && "bias");
// This is 3d because of cudnn limitations.
err = cudnnGetFilterNdDescriptor(odesc, 3, &dt, &tf, &nd, dims); err = cudnnGetFilterNdDescriptor(odesc, 3, &dt, &tf, &nd, dims);
if (err != CUDNN_STATUS_SUCCESS) { if (err != CUDNN_STATUS_SUCCESS) {
cudnnDestroyTensorDescriptor(xdesc); cudnnDestroyTensorDescriptor(xdesc);
cudnnDestroyFilterDescriptor(wdesc); cudnnDestroyFilterDescriptor(wdesc);
cudnnDestroyFilterDescriptor(odesc); cudnnDestroyFilterDescriptor(odesc);
PyErr_SetString(PyExc_RuntimeError, "could not get matrix shape for id %(id)s"); PyErr_SetString(PyExc_RuntimeError, "could not get bias shape for id %(id)s");
%(fail)s; %(fail)s;
} }
assert(dims[2] == 1);
// We assume that the typecode matches // We assume that the typecode matches
%(out)s = pygpu_reshape(%(w)s, 2, nshp, GA_C_ORDER, 1, -1); assert(dims[2] == 1);
%(out)s->ga.offset = off; assert(dims[1] == 1);
%(out)s->ga.dimensions[0] = dims[0]; %(b)s = pygpu_view(%(w)s, Py_None);
%(out)s->ga.dimensions[1] = dims[1]; %(b)s->ga.offset = off;
%(out)s->ga.strides[0] = dims[1] * gpuarray_get_elsize(%(out)s->ga.typecode); %(b)s->ga.dimensions[0] = dims[0];
// strides[1] is already ok bshp = dims[0];
""" % kw2
def get_bias(id, out): err = cudnnGetRNNLinLayerMatrixParams(%(handle)s, %(desc)s, %(layer)s, xdesc, wdesc, w, %(id)s, odesc, &o);
kw2 = kw.copy()
kw2['id'] = id
kw2['out'] = out
return """
err = cudnnGetRNNLinLayerBiasParams(%(handle)s, %(desc)s, %(layer)s, xdesc, wdesc, w, %(id)s, odesc, &o);
if (err != CUDNN_STATUS_SUCCESS) { if (err != CUDNN_STATUS_SUCCESS) {
cudnnDestroyTensorDescriptor(xdesc); cudnnDestroyTensorDescriptor(xdesc);
cudnnDestroyFilterDescriptor(wdesc); cudnnDestroyFilterDescriptor(wdesc);
cudnnDestroyFilterDescriptor(odesc); cudnnDestroyFilterDescriptor(odesc);
PyErr_SetString(PyExc_RuntimeError, "can't fetch bias for id %(id)s"); PyErr_SetString(PyExc_RuntimeError, "can't fetch matrix for id %(id)s");
%(fail)s %(fail)s
} }
off = (intptr_t)o - (intptr_t)w; off = (intptr_t)o - (intptr_t)w;
assert(off >= 0 && "bias"); assert(off >= 0 && "matrix");
// This is 3d because of cudnn limitations.
err = cudnnGetFilterNdDescriptor(odesc, 3, &dt, &tf, &nd, dims); err = cudnnGetFilterNdDescriptor(odesc, 3, &dt, &tf, &nd, dims);
if (err != CUDNN_STATUS_SUCCESS) { if (err != CUDNN_STATUS_SUCCESS) {
cudnnDestroyTensorDescriptor(xdesc); cudnnDestroyTensorDescriptor(xdesc);
cudnnDestroyFilterDescriptor(wdesc); cudnnDestroyFilterDescriptor(wdesc);
cudnnDestroyFilterDescriptor(odesc); cudnnDestroyFilterDescriptor(odesc);
PyErr_SetString(PyExc_RuntimeError, "could not get bias shape for id %(id)s"); PyErr_SetString(PyExc_RuntimeError, "could not get matrix shape for id %(id)s");
%(fail)s; %(fail)s;
} }
// We assume that the typecode matches
assert(dims[2] == 1);
assert(dims[1] == 1); assert(dims[1] == 1);
%(out)s = pygpu_view(%(w)s, Py_None); assert(dims[2] == 1);
%(out)s->ga.offset = off; // We assume that the typecode matches
%(out)s->ga.dimensions[0] = dims[0]; %(m)s = pygpu_reshape(%(w)s, 2, nshp, GA_C_ORDER, 1, -1);
%(m)s->ga.offset = off;
%(m)s->ga.dimensions[0] = dims[0] / bshp;
%(m)s->ga.dimensions[1] = bshp;
%(m)s->ga.strides[0] = bshp * gpuarray_get_elsize(%(m)s->ga.typecode);
// strides[1] is already ok
""" % kw2 """ % kw2
for i, o in enumerate(outputs): for i in range(len(outputs)//2):
if i % 2 == 0: code += get_params(i, outputs[2 * i], outputs[(2 * i) + 1])
code += get_matrix(i // 2, o)
else:
code += get_bias(i // 2, o)
code += """ code += """
cudnnDestroyTensorDescriptor(xdesc); cudnnDestroyTensorDescriptor(xdesc);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论