Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
diagonal fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommliu committed Dec 9, 2019
1 parent d7ac7ce commit 81062ce
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 158 deletions.
54 changes: 53 additions & 1 deletion python/mxnet/_numpy_op_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,58 @@ def _np_diag(array, k=0):
pass


def _np_diagonal(a, offset=0, axis1=0, axis2=1):
"""
If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of
the form a[i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and
axis2 are used to determine the 2-D sub-array whose diagonal is returned. The shape of the
resulting array can be determined by removing axis1 and axis2 and appending an index to the
right equal to the size of the resulting diagonals.
Parameters
----------
a : Symbol
Input data from which diagonal are taken.
offset: int, Optional
Offset of the diagonal from the main diagonal
axis1: int, Optional
Axis to be used as the first axis of the 2-D sub-arrays
axis2: int, Optional
Axis to be used as the second axis of the 2-D sub-arrays
Returns
-------
out : Symbol
Output result
Raises
-------
ValueError: If the dimension of a is less than 2.
Examples
--------
>>> a = np.arange(4).reshape(2,2)
>>> a
array([[0, 1],
[2, 3]])
>>> np.diagonal(a)
array([0, 3])
>>> np.diagonal(a, 1)
array([1])
>>> a = np.arange(8).reshape(2,2,2)
>>>a
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> np.diagonal(a, 0, 0, 1)
array([[0, 6],
[1, 7]])
"""
pass


def _np_diagflat(array, k=0):
"""
Create a two-dimensional array with the flattened input as a diagonal.
Expand Down Expand Up @@ -1157,4 +1209,4 @@ def _np_diagflat(array, k=0):
[0, 0, 2],
[0, 0, 0]])
"""
pass
pass
55 changes: 1 addition & 54 deletions python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
'flip', 'around', 'hypot', 'bitwise_xor', 'bitwise_or', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril',
'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less',
'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide', 'nonzero', 'shares_memory',
'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where', 'diagonal']
'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where']

@set_module('mxnet.ndarray.numpy')
def shape(a):
Expand Down Expand Up @@ -5525,56 +5525,3 @@ def where(condition, x=None, y=None):
return nonzero(condition)
else:
return _npi.where(condition, x, y, out=None)


@set_module('mxnet.ndarray.numpy')
def diagonal(a, offset=0, axis1=0, axis2=1):
"""
If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of
the form a[i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and
axis2 are used to determine the 2-D sub-array whose diagonal is returned. The shape of the
resulting array can be determined by removing axis1 and axis2 and appending an index to the
right equal to the size of the resulting diagonals.
Parameters
----------
a : Symbol
Input data from which diagonal are taken.
offset: int, Optional
Offset of the diagonal from the main diagonal
axis1: int, Optional
Axis to be used as the first axis of the 2-D sub-arrays
axis2: int, Optional
Axis to be used as the second axis of the 2-D sub-arrays
Returns
-------
out : Symbol
Output result
Raises
-------
ValueError: If the dimension of a is less than 2.
Examples
--------
>>> a = np.arange(4).reshape(2,2)
>>> a
array([[0, 1],
[2, 3]])
>>> np.diagonal(a)
array([0, 3])
>>> np.diagonal(a, 1)
array([1])
>>> a = np.arange(8).reshape(2,2,2)
>>>a
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> np.diagonal(a, 0, 0, 1)
array([[0, 6],
[1, 7]])
"""
return _npi.diagonal(a, k=offset, axis1=axis1, axis2=axis2)
57 changes: 1 addition & 56 deletions python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
'hanning', 'hamming', 'blackman', 'flip', 'around', 'arctan2', 'hypot', 'bitwise_xor', 'bitwise_or',
'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer',
'equal', 'not_equal', 'greater', 'less', 'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum',
'true_divide', 'nonzero', 'shares_memory', 'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where',
'diagonal']
'true_divide', 'nonzero', 'shares_memory', 'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where']

# Return code for dispatching indexing function call
_NDARRAY_UNSUPPORTED_INDEXING = -1
Expand Down Expand Up @@ -7512,57 +7511,3 @@ def where(condition, x=None, y=None):
[ 0., 3., -1.]])
"""
return _mx_nd_np.where(condition, x, y)


@set_module('mxnet.numpy')
def diagonal(a, offset=0, axis1=0, axis2=1):
"""
If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of
the form a[i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and
axis2 are used to determine the 2-D sub-array whose diagonal is returned. The shape of the
resulting array can be determined by removing axis1 and axis2 and appending an index to the
right equal to the size of the resulting diagonals.
Parameters
----------
a : Symbol
Input data from which diagonal are taken.
offset: int, Optional
Offset of the diagonal from the main diagonal
axis1: int, Optional
Axis to be used as the first axis of the 2-D sub-arrays
axis2: int, Optional
Axis to be used as the second axis of the 2-D sub-arrays
Returns
-------
out : Symbol
Output result
Raises
-------
ValueError: If the dimension of a is less than 2.
Examples
--------
>>> a = np.arange(4).reshape(2,2)
>>> a
array([[0, 1],
[2, 3]])
>>> np.diagonal(a)
array([0, 3])
>>> np.diagonal(a, 1)
array([1])
>>> a = np.arange(8).reshape(2,2,2)
>>>a
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> np.diagonal(a, 0, 0, 1)
array([[0, 6],
[1, 7]])
"""
return _mx_nd_np.diagonal(a, offset=offset, axis1=axis1, axis2=axis2)

34 changes: 1 addition & 33 deletions python/mxnet/symbol/numpy/_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
'flip', 'around', 'hypot', 'bitwise_xor', 'bitwise_or', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril',
'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less',
'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide', 'shares_memory',
'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where', 'diagonal']
'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where']


@set_module('mxnet.symbol.numpy')
Expand Down Expand Up @@ -5161,36 +5161,4 @@ def load_json(json_str):
return _Symbol(handle)


@set_module('mxnet.symbol.numpy')
def diagonal(a, offset=0, axis1=0, axis2=1):
"""
If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of
the form a[i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and
axis2 are used to determine the 2-D sub-array whose diagonal is returned. The shape of the
resulting array can be determined by removing axis1 and axis2 and appending an index to the
right equal to the size of the resulting diagonals.
Parameters
----------
a : Symbol
Input data from which diagonal are taken.
offset: int, Optional
Offset of the diagonal from the main diagonal
axis1: int, Optional
Axis to be used as the first axis of the 2-D sub-arrays
axis2: int, Optional
Axis to be used as the second axis of the 2-D sub-arrays
Returns
-------
out : Symbol
Output result
Raises
-------
ValueError: If the dimension of a is less than 2.
"""
return _npi.diagonal(a, k=offset, axis1=axis1, axis2=axis2)


_set_np_symbol_class(_Symbol)
20 changes: 11 additions & 9 deletions src/operator/numpy/np_matrix_op-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1151,11 +1151,11 @@ void NumpyDiagOpBackward(const nnvm::NodeAttrs &attrs,
}

struct NumpyDiagonalParam : public dmlc::Parameter<NumpyDiagonalParam> {
int k;
int offset;
int32_t axis1;
int32_t axis2;
DMLC_DECLARE_PARAMETER(NumpyDiagonalParam) {
DMLC_DECLARE_FIELD(k)
DMLC_DECLARE_FIELD(offset)
.set_default(0)
.describe("Diagonal in question. The default is 0. "
"Use k>0 for diagonals above the main diagonal, "
Expand All @@ -1173,7 +1173,7 @@ struct NumpyDiagonalParam : public dmlc::Parameter<NumpyDiagonalParam> {
};

inline mxnet::TShape NumpyDiagonalShapeImpl(const mxnet::TShape& ishape, const int k,
const int32_t axis1, const int32_t axis2) {
const int32_t axis1, const int32_t axis2) {
int32_t x1 = CheckAxis(axis1, ishape.ndim());
int32_t x2 = CheckAxis(axis2, ishape.ndim());

Expand Down Expand Up @@ -1217,7 +1217,7 @@ inline bool NumpyDiagonalOpShape(const nnvm::NodeAttrs& attrs,
}

const NumpyDiagonalParam& param = nnvm::get<NumpyDiagonalParam>(attrs.parsed);
mxnet::TShape oshape = NumpyDiagonalShapeImpl(ishape, param.k, param.axis1,
mxnet::TShape oshape = NumpyDiagonalShapeImpl(ishape, param.offset, param.axis1,
param.axis2);
if (shape_is_none(oshape)) {
LOG(FATAL) << "Diagonal does not exist.";
Expand Down Expand Up @@ -1296,7 +1296,7 @@ void NumpyDiagonalOpImpl(const TBlob& in_data,

if (x1 == maxx) std::swap(stride1, stride2);
index_t offset;
int k = param.k;
int k = param.offset;
if (k > 0) {
offset = stride2 * k;
} else if (k < 0) {
Expand All @@ -1316,8 +1316,8 @@ void NumpyDiagonalOpImpl(const TBlob& in_data,
stride1 + stride2, offset, oshape[odim - 1]);
} else {
Kernel<diag_n<3, req_type, back>, xpu>::Launch(s, dsize, out_data.dptr<DType>(),
in_data.dptr<DType>(), Shape3(oleading, obody, otrailing), Shape3(ileading, ibody, itrailing),
stride1 + stride2, offset, oshape[odim - 1]);
in_data.dptr<DType>(), Shape3(oleading, obody, otrailing),
Shape3(ileading, ibody, itrailing), stride1 + stride2, offset, oshape[odim - 1]);
}
});
});
Expand All @@ -1342,7 +1342,8 @@ void NumpyDiagonalOpForward(const nnvm::NodeAttrs& attrs,
const mxnet::TShape& oshape = outputs[0].shape_;
const NumpyDiagonalParam& param = nnvm::get<NumpyDiagonalParam>(attrs.parsed);

NumpyDiagonalOpImpl<xpu, false>(in_data, out_data, ishape, oshape, out_data.Size(), param, s, req);
NumpyDiagonalOpImpl<xpu, false>(in_data, out_data, ishape, oshape,
out_data.Size(), param, s, req);
}

template<typename xpu>
Expand All @@ -1363,7 +1364,8 @@ void NumpyDiagonalOpBackward(const nnvm::NodeAttrs& attrs,
const mxnet::TShape& oshape = outputs[0].shape_;
const NumpyDiagonalParam& param = nnvm::get<NumpyDiagonalParam>(attrs.parsed);

NumpyDiagonalOpImpl<xpu, true>(in_data, out_data, oshape, ishape, in_data.Size(), param, s, req);
NumpyDiagonalOpImpl<xpu, true>(in_data, out_data, oshape, ishape,
in_data.Size(), param, s, req);
}

struct NumpyDiagflatParam : public dmlc::Parameter<NumpyDiagflatParam> {
Expand Down
6 changes: 3 additions & 3 deletions src/operator/numpy/np_matrix_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ NNVM_REGISTER_OP(_backward_np_diag)
.set_attr<nnvm::TIsBackward>("TIsBackward", true)
.set_attr<FCompute>("FCompute<cpu>", NumpyDiagOpBackward<cpu>);

NNVM_REGISTER_OP(_npi_diagonal)
NNVM_REGISTER_OP(_np_diagonal)
.set_attr_parser(ParamParser<NumpyDiagonalParam>)
.set_num_inputs(1)
.set_num_outputs(1)
Expand All @@ -1338,11 +1338,11 @@ NNVM_REGISTER_OP(_npi_diagonal)
.set_attr<mxnet::FInferShape>("FInferShape", NumpyDiagonalOpShape)
.set_attr<nnvm::FInferType>("FInferType", NumpyDiagonalOpType)
.set_attr<FCompute>("FCompute<cpu>", NumpyDiagonalOpForward<cpu>)
.set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseNone{"_backward_npi_diagonal"})
.set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseNone{"_backward_np_diagonal"})
.add_argument("data", "NDArray-or-Symbol", "Input ndarray")
.add_arguments(NumpyDiagonalParam::__FIELDS__());

NNVM_REGISTER_OP(_backward_npi_diagonal)
NNVM_REGISTER_OP(_backward_np_diagonal)
.set_attr_parser(ParamParser<NumpyDiagonalParam>)
.set_num_inputs(1)
.set_num_outputs(1)
Expand Down
4 changes: 2 additions & 2 deletions src/operator/numpy/np_matrix_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ NNVM_REGISTER_OP(_np_diag)
NNVM_REGISTER_OP(_backward_np_diag)
.set_attr<FCompute>("FCompute<gpu>", NumpyDiagOpBackward<gpu>);

NNVM_REGISTER_OP(_npi_diagonal)
NNVM_REGISTER_OP(_np_diagonal)
.set_attr<FCompute>("FCompute<gpu>", NumpyDiagonalOpForward<gpu>);

NNVM_REGISTER_OP(_backward_npi_diagonal)
NNVM_REGISTER_OP(_backward_np_diagonal)
.set_attr<FCompute>("FCompute<gpu>", NumpyDiagonalOpBackward<gpu>);

NNVM_REGISTER_OP(_np_diagflat)
Expand Down

0 comments on commit 81062ce

Please sign in to comment.