Skip to content

Commit

Permalink
Add minpy article
Browse files Browse the repository at this point in the history
  • Loading branch information
lryta committed Jan 25, 2017
1 parent 5d980e1 commit 822f552
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ First put the markdown format post in `_posts/` in format
Preview in local:

```
$ gem install jekyll
$ gem install jekyll pygments.rb redcarpet
$ jekyll s
```

Expand Down
178 changes: 178 additions & 0 deletions _posts/2017-01-18-minpy-the-numpy-interface-upon-mxnets-backend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
---
layout: post
title: "MinPy: The NumPy Interface upon MXNet’s Backend"
date: 2017-01-18 00.00.00 -0800
author: Larry Tang and Minjie Wang
comments: true
---

Machine learning is now enjoying its golden age. In the past few years, its effectiveness has been proved by solving many traditionally hard problems in computer vision and natural language processing. At the same time, different machine learning frameworks came out to justify different needs. These frameworks, fall generally into two different categories: symbolic programming and imperative programming.

## Symbolic V.S. Imperative Programming

Symbolic and imperative programing are two different programming models. Imperative programming are represented by TensorFlow, MXNet’s symbol system, and Theano etc. In symbolic programming model, the execution of a neural network is comprised of two steps. The graph of the computational model needs to be defined first, then the defined graph is sent to execution. For example:

~~~python
import numpy as np
A = Variable('A')
B = Variable('B')
C = B * A
D = C + Constant(1)
# compiles the function
f = compile(D)
d = f(A=np.ones(10), B=np.ones(10)*2)
~~~

(Example taken from [[1][]])

The core idea here is that the computational graph is defined beforehand, which means the definition and the execution are separated. The advantage of symbolic programming is that it has specified precise computational boundary. Therefore, it is easier to adopt deep optimizations. However, symbolic programming has its limitation. First, it cannot gracefully work with control dependency. Second, it is hard to master a new symbolic language for a newcomer. Third, since the description and the execution of the computational graph are separated, it is difficult to relate execution to value instantiation in the computation.

So what about imperative programming? In fact most programmers have already known imperative programming quite well. The everyday C, Pascal, or Python code is almost all imperative. The fundamental idea is that every command is executed step by step, without a separated stage to define the computational graph. For example

~~~python
import numpy as np
a = np.ones(10)
b = np.ones(10) * 2
c = b * a
d = c + 1
~~~

(Example taken from [[1][]])

[1]: http://mxnet.io/architecture/program_model.html

When the code executes `c = b * a` and `d = c + 1`, they just run the actual computation. Compared to symbolic programming, imperative programming is much more flexible, since there is no separation of definition and execution. This is important for debugging and visualization. However, its computational boundary is not predefined, leading to harder system optimization. NumPy and Torch adapts imperative programming model.

MXNet is a “mixed” framework that aims to provide both symbolic and imperative style, and leaves the choice to its users. While MXNet has a superb symbolic programming subsystem, its imperative subsystem is not powerful enough compared to other imperative frameworks such as NumPy and Torch. This leads to our goal: a fully functional imperative framework that focuses on flexibility without much performance loss, and works well with MXNet’s existing symbol system.

## What is MinPy

MinPy aims at providing a high performing and flexible deep learning platform, by prototyping a pure [NumPy](http://www.numpy.org/) interface above [MXNet](https://github.com/dmlc/mxnet) backend. In one word, you get the following automatically with your NumPy code:

~~~python
import minpy.numpy as np
~~~

- Operators with GPU support will be ran on GPU.
- Graceful fallback for missing operations to NumPy on CPU.
- Automatic gradient generation with Autograd support.
- Seamless MXNet symbol integration.

## Pure NumPy, purely imperative

Why obsessed with NumPy interface? First of all, NumPy is an extension to the Python programming language, with support for large, multi-dimensional arrays, matrices, and a large library of high-level mathematical functions to operate on these abstractions. If you just begin to learn deep learning, you should absolutely start from NumPy to gain a firm grasp of its concepts (see, for example, the Stanford's [CS231n course](http://cs231n.stanford.edu/syllabus.html)). For quick prototyping of advanced deep learning algorithms, you may often start composing with NumPy as well.

Second, as an extension of Python, your implementation follows the intuitive imperative style. This is the **only** style, and there is **no** new syntax constructs to learn. To have a taste of this, let's look at some examples below.

## Printing and Debugging

![Debug and print variables](https://raw.githubusercontent.com/dmlc/web-data/master/minpy/p1.png)

In symbolic programming, the control dependency before the print statement is required, otherwise the print operator will not appear on the critical dependency path and thus not being executed. In contrast, MinPy is simply NumPy, as straightforward as Python's hello world.

## Data-dependent branches

![Data dependent branching](https://raw.githubusercontent.com/dmlc/web-data/master/minpy/p2.png)

In symbolic programming, the `lambda` is required in each branch to lazily expand the dataflow graph during runtime, which can be quite confusing. Again, MinPy is NumPy, and you freely use the if statement anyway you like.

Tensorflow is just one typical example, many other packages (e.g. Theano, or even MXNet) have similar problems. The underlying reason is the trade-off between *symbolic programming* and *imperative programming*. Code in symbolic programs (Tensorflow and Theano) generates dataflow graph instead of performing concrete computation. This enables extensive optimizations, but requires reinventing almost all language constructs (like if and loop). Imperative programs (NumPy) generates dataflow graph *along with* the computation, enabling you freely query or use the value just computed.
In MinPy, we use NumPy syntax to ease your programming, while simultaneously achieving good performance.

## Dynamic automatic gradient computation

Automatic gradient computation has become essential in modern deep learning systems. In MinPy, we adopt [Autograd](https://github.com/HIPS/autograd)'s approach to compute gradients. Since the dataflow graph is generated along with the computation, all kinds of native control flow are supported during gradient computation. For example:

~~~python
import minpy
from minpy.core import grad

def foo(x):
if x >= 0:
return x
else:
return 2 * x

foo_grad = grad(foo)
print foo_grad(3) # should print 1.0
print foo_grad(-1) # should print 2.0
~~~

Here, feel free to use native `if` statement. A complete tutorial about auto-gradient computation can be found [here](https://minpy.readthedocs.io/en/latest/tutorial/autograd_tutorial.html).

## Elegant fallback for missing operators

You never like `NotImplementedError`, so do we. NumPy is a very large library. In MinPy, we automatically fallback to NumPy if some operators have not been implemented in MXNet yet. For example, the following code runs smoothly and you don't need to worry about copying arrays back and forth from GPU to CPU; MinPy handles the fallback and its side effect transparently.

~~~python
import minpy.numpy as np
x = np.zeros((2, 3)) # Use MXNet GPU implementation
y = np.ones((2, 3)) # Use MXNet GPU implementation
z = np.logaddexp(x, y) # Use NumPy CPU implementation
~~~

## Seamless MXNet symbol support

Although we pick the imperative side, we understand that symbolic programming is necessary for operators like convolution. Therefore, MinPy allows you to "wrap" a symbol into a function that could be called together with other imperative calls. From a programmer's eye, these functions is just as other NumPy calls, thus we preserve the imperative style throughout:

~~~python
import mxnet as mx
import minpy.numpy as np
from minpy.core import Function
# Create Function from symbol.
net = mx.sym.Variable('x')
net = mx.sym.Convolution(net, name='conv', kernel=(3, 3), num_filter=32, no_bias=True)
conv = Function(net, input_shapes={'x', (8, 3, 10, 10)}
# Call Function as normal function.
x = np.zeros((8, 3, 10, 10))
w = np.ones((32, 3, 3, 3,))
y = np.exp(conv(x=x, conv_weight=w))
~~~

## Is MinPy fast?

The imperative interface does raise many challenges, especially it foregoes some of the deep optimization that only (currently) embodied in symbolic programming. However, MinPy manages to retain reasonable performance, especially when the actual computation is intense. Our next target is to get back the performance with advanced system techniques.

![Benchmark](https://raw.githubusercontent.com/dmlc/web-data/master/minpy/benchmark.png)

## CS231n -- a Perfect Intro for Newcomers

As for deep learning learners, MinPy is a perfect tool to begin with. One of the reasons is that MinPy is fully compatible with NumPy, which means almost no modification to the existing NumPy code. In addition, our team also provides a modified version of CS231n assignments to address the amenity of MinPy.

[CS231n](http://cs231n.stanford.edu/) is an introductory course to deep learning taught by Professor [Fei-Fei Li](http://vision.stanford.edu/feifeili/) and her Ph.D students [Andrej Karpathy](http://cs.stanford.edu/people/karpathy/) and [Justin Johnson](http://cs.stanford.edu/people/jcjohns/) at Stanford University. The curriculum covers all the basics of deep learning, including convolutional neural network and recurrent neural network. The course assignments are not only a simple practice of the lecture contents, but a in-depth journey for students to explore deep learning’s latest applications step-by-step. Since MinPy has similar interface with NumPy, our team modified assignments in CS231n to demonstrate the features MinPy provided, in order to create an integrated learning experience for the students who learn deep learning for their first time. The MinPy version of CS231n has already been used in deep learning course at Shanghai Jiao Tong University and ShanghaiTech University.

## Summary

MinPy development team began with [Minerva](https://github.com/dmlc/minerva) project. After co-founded MXNet project with the community, we have contributed to the core code of MXNet, including its executor engine, IO, and Caffe operator plugin. Having completed that part, the team decided to take a step back and rethink the user experience, before moving to yet another ambitious stage of performance optimizations. We strive to provide maximum flexibility for users, while creating space for more advanced system optimization. MinPy is pure NumPy and purely imperative. It will be merged into MXNet in the near future.

Enjoy! Please send us feedbacks.

## Links

* GitHub: <https://github.com/dmlc/minpy>
* MinPy documentation: <http://minpy.readthedocs.io/en/latest/>

## Acknowledgements

- [MXNet Community](http://dmlc.ml/)
- Sean Welleck (Ph.D at NYU), Alex Gai and Murphy Li (Undergrads at NYU Shanghai)
- Professor [Yi Ma](http://sist.shanghaitech.edu.cn/StaffDetail.asp?id=387) and Postdoc Xu Zhou at ShanghaiTech University; Professor [Kai Yu](https://speechlab.sjtu.edu.cn/~kyu/) and Professor [Weinan Zhang](http://wnzhang.net/) at Shanghai Jiao Tong University.

## MinPy Developers

- Project Lead Minjie Wang (NYU) [GitHub](https://github.com/jermainewang)
- Larry Tang (U of Michigan*) [GitHub](https://github.com/lryta)
- Yutian Li (Stanford) [GitHub](https://github.com/hotpxl)
- Haoran Wang (CMU) [GitHub](https://github.com/HrWangChengdu)
- Tianjun Xiao (Tesla) [GitHub](https://github.com/sneakerkg)
- Ziheng Jiang (Fudan U*) [GitHub](https://github.com/ZihengJiang)
- Professor [Zheng Zhang](https://shanghai.nyu.edu/academics/faculty/directory/zheng-zhang) (NYU Shanghai) [GitHub](https://github.com/zzhang-cn)

*: MinPy is completed during NYU Shanghai research internship

## Reference
1. <http://mxnet.io/architecture/program_model.html>




Binary file added img/logo/um-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ <h2><a href="https://github.com/dmlc/xgboost" >XGBoost</a></h2>


<div class="col-md-3">
<h2><a href="https://github.com/dmlc/minerva">Minerva</a></h2>
<iframe src="https://ghbtns.com/github-btn.html?user=dmlc&repo=minerva&type=star&count=true&v=2"
<h2><a href="https://github.com/dmlc/minpy">MinPy</a></h2>
<iframe src="https://ghbtns.com/github-btn.html?user=dmlc&repo=minpy&type=star&count=true&v=2"
frameborder="0" scrolling="0" width="100px" height="20px"></iframe>
<iframe src="https://ghbtns.com/github-btn.html?user=dmlc&repo=minerva&type=fork&count=true&v=2"
<iframe src="https://ghbtns.com/github-btn.html?user=dmlc&repo=minpy&type=fork&count=true&v=2"
frameborder="0" scrolling="0" width="100px" height="20px"></iframe>
<p> NDarray programming interface (like Numpy) for deep learning
<p> The NumPy interface upon MXNet’s backend
</p>
</div>
</div>
Expand Down Expand Up @@ -144,6 +144,8 @@ <h2>Acknowledgment</h2>
<img height="70px" src="http://www.washington.edu/brand/files/2014/09/Signature_Stacked_Purple_Hex.png">
&nbsp;
<img height="70px" src="https://upload.wikimedia.org/wikipedia/en/thumb/2/2e/McGill_Wordmark.svg/640px-McGill_Wordmark.svg.png">
&nbsp;
<img height="110px" src="img/logo/um-logo.png">
</div>
</div>
<br>
Expand Down

0 comments on commit 822f552

Please sign in to comment.