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

add numpy op diagflat [numpy] #16813

Merged
merged 8 commits into from
Nov 21, 2019
Merged

add numpy op diagflat [numpy] #16813

merged 8 commits into from
Nov 21, 2019

Conversation

ryanwentaoxu
Copy link
Contributor

Description

(Brief description on what this PR is about)

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • 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)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Feature1, tests, (and when applicable, API doc)
  • Feature2, tests, (and when applicable, API doc)

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here

@haojin2 haojin2 self-assigned this Nov 14, 2019
@haojin2 haojin2 added the Numpy label Nov 14, 2019
@haojin2 haojin2 added this to In progress in numpy via automation Nov 14, 2019
@@ -39,8 +39,7 @@
'around', 'hypot', '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']

'nan_to_num', 'diagflat']
Copy link
Contributor

Choose a reason for hiding this comment

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

move this to other places to avoid conflicts, probably you can move to after full.
Remember to move the function definitions too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same for all other files.

def diagflat(arr, k=0):
"""
Create a two-dimensional array with the flattened input as a diagonal.
Parameters:
Copy link
Contributor

@haojin2 haojin2 Nov 14, 2019

Choose a reason for hiding this comment

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

    Parameters
    ----------


Diagonal to set; 0, the default, corresponds to the "main" diagonal, a positive (negative) k giving the number of the diagonal above (below) the main.

Returns:
Copy link
Contributor

Choose a reason for hiding this comment

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

    Returns
    -------

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

[0,0,0,0,0,0,0,8]]
"""
return _npi.diagflat(arr, k=k)

Copy link
Contributor

Choose a reason for hiding this comment

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

one less blank line.

@@ -0,0 +1,213 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

put those in np_matrix_op files

struct NumpyDiagflatParam : public dmlc::Parameter<NumpyDiagflatParam> {
int k;
DMLC_DECLARE_PARAMETER(NumpyDiagflatParam) {
DMLC_DECLARE_FIELD(k)
Copy link
Contributor

Choose a reason for hiding this comment

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

2-space indents

Copy link
Contributor

Choose a reason for hiding this comment

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

same for everywhere else.



template<typename xpu, bool back>
void NumpyDiagflatOpProcess(const TBlob& in_data,
Copy link
Contributor

Choose a reason for hiding this comment

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

NumpyDiagflatOpImpl

DMLC_REGISTER_PARAMETER(NumpyDiagflatParam);

NNVM_REGISTER_OP(_npi_diagflat)
.describe(R"code(contructs a diagonal array.
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for doc here.


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

Copy link
Contributor

Choose a reason for hiding this comment

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

1 less blank line here.

Create a two-dimensional array with the flattened input as a diagonal.
Parameters:

v : array_like
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
v : array_like
arr : ndarray

or you can fall back to numpy when arr is not ndarray type

Input data, which is flattened and set as the k-th diagonal of the output.
k : int, optional

Diagonal to set; 0, the default, corresponds to the "main" diagonal, a positive (negative) k giving the number of the diagonal above (below) the main.
Copy link
Contributor

Choose a reason for hiding this comment

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

too long

@@ -4558,6 +4558,61 @@ def rot90(m, k=1, axes=(0, 1)):
return _npi.rot90(m, k=k, axes=axes)


@set_module('mxnet.ndarray.numpy')
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@set_module('mxnet.ndarray.numpy')
@set_module('mxnet.symbol.numpy')


Returns:

out : ndarray
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
out : ndarray
out : _Symbol

Copy link
Contributor

Choose a reason for hiding this comment

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

same for arr

vals_f = _np.array((100 * get_mat(5) + 1), order = 'F', dtype = 'l')
vals_f = np.array(vals_f)

OpArgMngr.add_workload('diagflat', A, k= 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

get rid of all spaces after the =

Copy link
Contributor

@haojin2 haojin2 left a comment

Choose a reason for hiding this comment

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

LGTM, will merge as soon as CI passes

numpy automation moved this from In progress to Reviewer approved Nov 21, 2019
@haojin2 haojin2 merged commit ece027c into apache:master Nov 21, 2019
numpy automation moved this from Reviewer approved to Done Nov 21, 2019
@haojin2 haojin2 mentioned this pull request Dec 10, 2019
7 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
No open projects
numpy
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants