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

[numpy] numpy einsum #15493

Closed
wants to merge 48 commits into from
Closed

[numpy] numpy einsum #15493

wants to merge 48 commits into from

Conversation

hzfan
Copy link
Contributor

@hzfan hzfan commented Jul 9, 2019

Description

numpy compatible einsum

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 http://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

  • add einsum
  • add einsum doc
  • add path optimization for imperative mode
  • add benchmark
  • add forward and backward test

Comments

  • support both explicit and implicit mode
  • support multiple operands
  • support broadcast
  • support 0-dim NDArray and 0-size NDArray

TODO

  • translate einsum_path into c, so that optimization can work in symbol mode
  • take advantage of tensordot when it is merged
  • support "greedy" optimization
  • support "optimal" optimization

Thank @reminisce and @haojin2 for guidance and review.

@hzfan hzfan requested a review from szha as a code owner July 9, 2019 08:05
@hzfan
Copy link
Contributor Author

hzfan commented Jul 9, 2019

@mxnet-label-bot add [Numpy]

Chained array operations. For more complicated contractions, speed ups
might be achieved by repeatedly computing a 'greedy' path or pre-computing the
'optimal' path and repeatedly applying it, using an
`einsum_path` insertion (since version 1.12.0). Performance improvements can be
Copy link
Contributor

Choose a reason for hiding this comment

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

1.12.0 is the official numpy version. We'd better not include that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

`einsum_path` insertion (since version 1.12.0). Performance improvements can be
particularly significant with larger arrays:

>>> a = np.ones(64).reshape(2,4,8)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the benchmark from the mxnet.numpy.einsum or the official np.einsum?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't benchmarked yet. The benchmark here is from official np.einsum. I will remove them.

int newdim;

const TShape& shape = op.shape_;
TShape stride;
Copy link
Contributor

Choose a reason for hiding this comment

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

Combine this line and the next line into one line.

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

TShape* newshape,
TShape* newstride) {
using namespace mxnet_op;
int idim, ndim, icombine, combineoffset;
Copy link
Contributor

Choose a reason for hiding this comment

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

Define the variables where they are first used, i.e. narrow down the scope of variables as much as possible.

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

namespace mxnet {
namespace op {

inline bool NumpyEinsumShape(const nnvm::NodeAttrs& attrs,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: no need to inline

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

#define NPY_MAXDIMS 32
#define NPY_MAXARGS 32


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 here.

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

return ret;
}


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 here

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.

// std::cout << output_labels[i] << " ";
// }
// std::cout << std::endl;
// std::cout << "max_broadcast = " << max_broadcast << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

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

remove dead code

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

@hzfan
Copy link
Contributor Author

hzfan commented Jul 18, 2019

cherry-picked from #15565. Tensordot optimization has been enabled. @reminisce

ptrendx and others added 25 commits August 9, 2019 16:05
* Fix backward_clip num inputs and type of clip params

* Clip test

* Trigger CI

* Changes to clip docs

* Fix docstring

* Trigger CI
* Modify ndarray slice to have numpy compatbile behaviou

* Minor syntax fix

* Fix slice inconsistency

* Allow empty outputs after slicing ndarrays

* Fix
* remove test images

* add script and .gitignore

* add test helper to download images

* remove unlicensed pic

* add license header
* Implements tensordot and dot.

* Change tests.

* Add spaces.

* Reorganize codes.

* Remove np_matrix_op.h
* Adding Large Index Support for slice operator

* adding changes to fix py2 related error in CI/CD

* fixing base.py

* rearrange system call and slower Feature() call

* refactoring c_api, c_symbolic_api, c_api_common

* templatizing code

* caching results of runtime features and minor refactoring

* fixing local caching in ndarray shape
* Remove old cudnn support (<v7).

* Simplify conv impl selection.

* Remove comments justifying STATIC_ASSERT_CUDNN_VERSION_GE.
* Add squeeze/flatten/transpose/reshape

Add more ops back

Add new files

Add unit tests

Add unit test for broadcast_arrays

Add more ops and tests

Add arange

* clean up
* test rdiv

* floating_point exception handle

* add 10 other ops

* added rpow and made numpy consistent

* attempt to solve memory issue

* linting fix

* Trigger notification

* lint
)

* Allow operators with multiple outputs in get_atomic_symbol

* Added unittest
* random ops

* replace array with uniform

* remove dtype

* randn add

* add multinomial

* multi,randn small fix

* add negative bino

* fix memory issue - Failed to allocate CPU Memory

* Trigger notification

* linting fix

* Trigger notification
* sequence_last, sequence_reverse, sequence_mask

* working softmax_cross_entropy

* fix linting, add index_copy

* add softmax output

* add leaky relu

* add pooling

* add layernorm

* add dropout, activation, batchnorm and update layernorm

* address comments to remove some comments

* handling imports
* numpy-compatible concatenate upstream

* extend ci deadline
* numpy-compatible concatenate upstream

* extend ci deadline
* numpy-compatible concatenate upstream

* extend ci deadline
* numpy-compatible concatenate upstream

* extend ci deadline
* Fix ConcatType and add test

* Remove return false

* Change error message

* Run RNN test only when CUDNN enabled

* set default context for test_contrib_amp
@hzfan
Copy link
Contributor Author

hzfan commented Aug 15, 2019

PRed to the master branch. Closing this one.

@hzfan hzfan closed this Aug 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet