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

Added the diag() operator #11643

Merged
merged 21 commits into from Jul 19, 2018
Merged

Conversation

ifeherva
Copy link
Contributor

@ifeherva ifeherva commented Jul 11, 2018

Description

Added a new tensor operator called diag() replicating numpy.diag().

The only difference to numpy.diag() is that for invalid k numpy diag returns an empty array while mxnet is going to LOG FATAL.

Checklist

Essentials

  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Comments

  • This is my first operator, please be gentle :)

@ifeherva ifeherva mentioned this pull request Jul 11, 2018
@ifeherva
Copy link
Contributor Author

Unsure why CI fails, I did not touch that part:
/work/mxnet/3rdparty/mkldnn/install/lib//libmklml_intel.so: file not recognized: File truncated

@ifeherva ifeherva requested a review from szha as a code owner July 11, 2018 21:11
@ifeherva ifeherva force-pushed the diagonal_operator branch 2 times, most recently from 66f6335 to 1bca54a Compare July 11, 2018 21:22
Copy link
Member

@zhreshold zhreshold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM.

struct DiagParam : public dmlc::Parameter<DiagParam> {
int32_t k;
DMLC_DECLARE_PARAMETER(DiagParam) {
DMLC_DECLARE_FIELD(k)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation looks weird, I think the lint suggest 2 spaces or 4 spaces

r = mx.nd.diag(a)

for i in range(r.shape[0]):
assert r[i] == a[i][i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use numpy for consistency check. It's more stable than manually setting the desired values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorporated the changes.

@ifeherva ifeherva force-pushed the diagonal_operator branch 2 times, most recently from b764ef9 to d2ef62e Compare July 13, 2018 00:17
@ifeherva
Copy link
Contributor Author

Any idea why it does not pass CI?

@zhreshold
Copy link
Member

@ifeherva Try locally simulate the CI with the docker build script. Currently we can get nothing from the log.

@ifeherva
Copy link
Contributor Author

Ran on my p3.2x.large

python3 ci/build.py --platform ubuntu_build_cuda /work/runtime_functions.sh build_ubuntu_gpu_cuda91_cudnn7

without issues

@ifeherva ifeherva force-pushed the diagonal_operator branch 5 times, most recently from 871f510 to f54b7ac Compare July 17, 2018 05:17
@ifeherva
Copy link
Contributor Author

Operator is now passing all tests, I consider this work finished.

@szha
Copy link
Member

szha commented Jul 18, 2018

@eric-haibin-lin seems like there should be a sparse version for this too. Thoughts?

@zhreshold
Copy link
Member

LGTM now. @eric-haibin-lin Can you do a quick review?

@@ -1302,6 +1302,14 @@ def flip(self, *args, **kwargs):
"""
return op.flip(self, *args, **kwargs)

def diag(self, k=0, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also update https://github.com/apache/incubator-mxnet/blob/master/docs/api/python/ndarray/ndarray.md and symbol/symbol.md (probably add it to the section of array creation routines like https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html)
@reminisce maybe we should also mention adding documentation in the operator tutorial ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, added it.

@eric-haibin-lin
Copy link
Member

@ifeherva thanks for the contribution! Good work.

@szha I think sparse.diag which returns a CSR ndarray (like https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.diags.html) is a good thing to have. It will be great if we can have that, but if not, this PR shall not be block by this extra feature

@szha
Copy link
Member

szha commented Jul 18, 2018

@eric-haibin-lin Let's have the sparse feature in a separate PR.

@ifeherva
Copy link
Contributor Author

I am happy to do the sparse array support in a future PR.

Done:
2d input forward pass
Missing:
1d input forward
all backward
Copy link
Member

@eric-haibin-lin eric-haibin-lin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending one minor comment for doc

@@ -131,6 +131,7 @@ The `ndarray` package provides several classes:
NDArray.flatten
NDArray.expand_dims
NDArray.split
NDArray.diag
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't make it clear - there're two places to add per file. For ndarray.md, One is NDArray.diag (fluent method) and the other is (ndarray.)diag at line 360.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@eric-haibin-lin eric-haibin-lin merged commit f15b1b8 into apache:master Jul 19, 2018
KellenSunderland pushed a commit to KellenSunderland/incubator-mxnet that referenced this pull request Jul 21, 2018
* Added np.diag as mxnet operator, WIP

Done:
2d input forward pass
Missing:
1d input forward
all backward

* Added a simple gradient transfer backwards operator for diag

Fixed small typos as well

* Finished backward operation

* Added full support for k

* Finished added the 1D case to the diag operator

Finished function documentation
Added unit tests

* Fixed cpplinter errors in the diag operator

Issues were extra white spaces and include order

* Fixed indentation in diag_op-inl.h

* Changed diag operator tests to use np.diag() as comparison

* Fixed kernel bug in gpu diag operator

* Replaced the min operator with an inline if statement.

* Added diag to ndarray and symbol

* Replaced the type of parameter k from int32 to nnvm::dim

* Added default argument to k in ndarray and symbol

* Fixed ndarray and symbol diag calls

* Fixed the optional k parameter

* Fixed cpp linting error

* Changed test data datatype to float32

* K values resulting into 0-sized diagonals will now throw an exception.

Added matching test case

* Fixed unittest

* Added diag to NDArray and Symbol api doc

* Added missing api doc
XinYao1994 pushed a commit to XinYao1994/incubator-mxnet that referenced this pull request Aug 29, 2018
* Added np.diag as mxnet operator, WIP

Done:
2d input forward pass
Missing:
1d input forward
all backward

* Added a simple gradient transfer backwards operator for diag

Fixed small typos as well

* Finished backward operation

* Added full support for k

* Finished added the 1D case to the diag operator

Finished function documentation
Added unit tests

* Fixed cpplinter errors in the diag operator

Issues were extra white spaces and include order

* Fixed indentation in diag_op-inl.h

* Changed diag operator tests to use np.diag() as comparison

* Fixed kernel bug in gpu diag operator

* Replaced the min operator with an inline if statement.

* Added diag to ndarray and symbol

* Replaced the type of parameter k from int32 to nnvm::dim

* Added default argument to k in ndarray and symbol

* Fixed ndarray and symbol diag calls

* Fixed the optional k parameter

* Fixed cpp linting error

* Changed test data datatype to float32

* K values resulting into 0-sized diagonals will now throw an exception.

Added matching test case

* Fixed unittest

* Added diag to NDArray and Symbol api doc

* Added missing api doc
@ifeherva ifeherva deleted the diagonal_operator branch February 10, 2019 04:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants