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

Add a tutorial for control flow operators. #12340

Merged
merged 11 commits into from
Aug 27, 2018

Conversation

zheng-da
Copy link
Contributor

Description

This is to provide a tutorial for control flow operators.

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

  • 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

@zheng-da zheng-da requested a review from szha as a code owner August 24, 2018 21:30
@sandeep-krishnamurthy sandeep-krishnamurthy added Doc Python Backend Issues related to the backend of MXNet pr-awaiting-review PR is waiting for code review and removed Backend Issues related to the backend of MXNet labels Aug 24, 2018
@zheng-da
Copy link
Contributor Author

@aaronmarkham could you review this tutorial, please?

Copy link
Contributor

@aaronmarkham aaronmarkham left a comment

Choose a reason for hiding this comment

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

Nice tutorial. I have a few suggestions though.

MXNet currently provides three control flow operators: `cond`, `foreach` and `while_loop`. Like other MXNet operators, they all have a version for NDArray and a version for Symbol. These two versions have exactly the same semantics. We can take advantage of this and use them in Gluon to hybridize models.

In this tutorial, we use a few examples to demonstrate the use of control flow operators in Gluon and show how a model that requires control flow is hybridized.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any prerequisites worth mentioning?

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 don't think so.

foreach(body, data, init_states, name) => (outputs, states)
```

It iterates over the first dimension of the input data (it can be an array or a list of arrays) and run the Python function defined in `body` for every slice from the input arrays. The signature of the `body` function is defined as follows:
Copy link
Contributor

Choose a reason for hiding this comment

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

runs


The inputs of the `body` function have two parts: `data` is a slice of an array (if there is only one input array in `foreach`) or a list of slices (if there are a list of input arrays); `states` are the arrays from the previous iteration. The outputs of the `body` function also have two parts: `outputs` is an array or a list of arrays; `states` is the computation states of the current iteration. `outputs` from all iterations are concatenated as the outputs of `foreach`.

The pseudocode below illustrates the execution of `foreach`.
Copy link
Contributor

Choose a reason for hiding this comment

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

use "the following" instead of below (different consumption modes means the content isn't always below)

```

### Example 1: foreach works like map
`foreach` can work like a map function of a functional language. In this case, the states of foreach can be an empty list, which means the computation doesn't carry computation states across iterations.
Copy link
Contributor

Choose a reason for hiding this comment

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

tick each use of foreach

### Example 1: foreach works like map
`foreach` can work like a map function of a functional language. In this case, the states of foreach can be an empty list, which means the computation doesn't carry computation states across iterations.

In this example, we use `foreach` to add each element in an array by one.
Copy link
Contributor

Choose a reason for hiding this comment

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

use foreach with an array to increase each element's value by one

```


```python
Copy link
Contributor

Choose a reason for hiding this comment

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

why two different code blocks? Is there supposed to be some commentary here?

from mxnet.gluon import HybridBlock
```

# foreach
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd go to two hashes for your subsections and only use the single hash for the document title.

res, states = lstm(rnn_data, [x for x in init_states], valid_length)
```

# while_loop
Copy link
Contributor

Choose a reason for hiding this comment

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

two hashes

<NDArray 1 @cpu(0)>]


# cond
Copy link
Contributor

Choose a reason for hiding this comment

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

two hashes



# cond
`cond` is defined with the following signature:
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to talk about what it is and what it does before showing the signature. The same goes for the other sections/functions.

@@ -0,0 +1,390 @@

Copy link
Member

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.

how to do that?

@Roshrini
Copy link
Member

@zheng-da @eric-haibin-lin @aaronmarkham Can you check if all the feebdack was addressed and is this PR ready to merge now?

@eric-haibin-lin
Copy link
Member

my comments are addressed

@zheng-da
Copy link
Contributor Author

@Roshrini I have addressed all of @aaronmarkham's comments.

@szha szha merged commit 9e48f70 into apache:master Aug 27, 2018
Roshrini pushed a commit to Roshrini/mxnet that referenced this pull request Aug 27, 2018
* the first version.

* fix.

* add to test.

* fix.

* fix.

* fix

* fix.

* fix.

* add title.

* add link

* fix.
szha pushed a commit that referenced this pull request Aug 27, 2018
* Add a tutorial for control flow operators. (#12340)

* the first version.

* fix.

* add to test.

* fix.

* fix.

* fix

* fix.

* fix.

* add title.

* add link

* fix.

* Update ONNX API docs references (#12317)

* update onnx API references

* update descriptions

* [MXAPPS-581] Disable an additional long test in the SD nightly (#12343)

* Disable an additional test in the SD nightly that also takes over the
  timeout.

* Documentation update related to sparse support (#12367)

* Update sparse.md

* Update sparse.md

* Update csr.md

* Update row_sparse.md

* Update train.md
anirudh2290 pushed a commit to anirudh2290/mxnet that referenced this pull request Sep 19, 2018
* the first version.

* fix.

* add to test.

* fix.

* fix.

* fix

* fix.

* fix.

* add title.

* add link

* fix.
@zheng-da zheng-da deleted the control_flow_tutorial branch September 29, 2018 21:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Doc pr-awaiting-review PR is waiting for code review Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants