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

Numpy compatible diagflat operator #15468

Closed
wants to merge 70 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
ffd690d
[Do not review] [Do not merge] New numpy-compatible sum (#14739)
haojin2 Apr 21, 2019
2f71241
[numpy] Infra for supporting numpy ops in imperative mode and Gluon A…
reminisce May 3, 2019
355254e
Enable np op compat check with name prefix (#14897)
reminisce May 6, 2019
7a12654
[numpy] Numpy dot (#14831)
haojin2 May 8, 2019
4325676
numpy-compatible mean (#14859)
haojin2 May 9, 2019
0ee508e
[numpy] Some np ops for d2l (#14924)
reminisce May 10, 2019
043f01e
[numpy] Refactor np modules (#14989)
reminisce May 18, 2019
308bcc6
[numpy] Refactor np module (example runs through) (#15055)
reminisce May 27, 2019
6e33cc1
Change np_compat to np_shape
reminisce May 27, 2019
877e0e9
Temporarily disable test_amp
reminisce May 27, 2019
9911675
Numpy-compatible stack (#15027)
haojin2 May 31, 2019
ac92ec3
Numpy Unary Ops (#15010)
haojin2 Jun 2, 2019
0010c43
[numpy] Fix np branch after rebase (#15086)
reminisce Jun 2, 2019
aa5153f
numpy concatenate (#15104)
haojin2 Jun 4, 2019
517451b
[WIP][numpy] Fix for D2L Chapters 2/3/4 (#15139)
reminisce Jun 5, 2019
df47457
[numpy] Fix d2l performance regression (#15173)
reminisce Jun 7, 2019
4e1274f
Fix (#15188)
reminisce Jun 9, 2019
4ad56dc
fix for chapter6 conv nn (#15224)
haojin2 Jun 12, 2019
fb10e28
[numpy] Fix d2l chapter8 (#15237)
reminisce Jun 13, 2019
a00a2ce
fix for ch11 (#15244)
haojin2 Jun 14, 2019
93679fe
Numpy-compatible split (#15049)
haojin2 Jun 17, 2019
bf4dc33
[numpy] [DO NOT MERGE] Fix d2l chapters 9 and 13 (#15246)
reminisce Jun 17, 2019
96520cb
[numpy] Fix d2l chapter 5 (#15264)
reminisce Jun 18, 2019
3409578
Numpy compatible max (#15161)
stu1130 Jun 19, 2019
1b62426
Numpy compatible multinomial (#15219)
stu1130 Jun 20, 2019
2260667
Numpy compatible linspace (#15256)
stu1130 Jun 20, 2019
f803dde
numpy-compatible cumsum (#15309)
haojin2 Jun 23, 2019
a563b56
[numpy] Misc fix for other chapters (#15332)
reminisce Jun 23, 2019
6b7525c
[numpy] Change d2l chapters cv and gan to use numpy (#15368)
reminisce Jun 27, 2019
4dfb7b9
add doc for multinomial, dot, cumsum, clip, abs, exp, arctan (#15386)
hzfan Jun 28, 2019
d60e105
[numpy] Fix several places in numpy (#15398)
reminisce Jun 28, 2019
052f90d
[numpy] fix cython (#15418)
hzfan Jul 2, 2019
81747cb
[numpy][doc-fix] sum, copy, tile, argmax, sign, log, degrees (#15382)
zoeygxy Jul 2, 2019
ab55a70
added untracked files
endvroy Jul 3, 2019
9470a2c
Merge branch 'numpy' of https://github.com/apache/incubator-mxnet int…
endvroy Jul 3, 2019
9eca4db
removed test script
endvroy Jul 3, 2019
44e5b25
[numpy][doc-fix] mean, transpose, stack, split, log2, rint and radian…
Jul 3, 2019
e9b73e1
[numpy][doc-fix] zeros_like, linspace, reciprocal, square, and arcsin…
hgt312 Jul 4, 2019
fb329be
Merge branch 'numpy' of https://github.com/apache/incubator-mxnet int…
endvroy Jul 9, 2019
546f3e8
Numpy Trace (#15258)
hzfan Jul 9, 2019
1bff19b
[Numpy] Numpy compatible argsort (#15501)
Jul 10, 2019
19c7c6e
fix memory override bug in multinomial (#15397)
stu1130 Jul 12, 2019
3ea5f8c
numpy eye op (#15282)
stu1130 Jul 12, 2019
a26b201
[Numpy] Numpy hstack (#15302)
Jul 12, 2019
5c89a52
Merge branch 'numpy' of https://github.com/apache/incubator-mxnet int…
endvroy Jul 15, 2019
09ccb0b
shape inference completed
endvroy Jul 3, 2019
18ab342
fixed bugs in shape inference
endvroy Jul 3, 2019
27a259e
fixed typo
endvroy Jul 3, 2019
083666d
first version of forward
endvroy Jul 3, 2019
0bdbe91
added safeguard for zero-size arrays
endvroy Jul 3, 2019
fff7d09
added check against array access overflow
endvroy Jul 4, 2019
230d322
added tests for forward
endvroy Jul 4, 2019
9598847
fixed array access error
endvroy Jul 4, 2019
b9c8993
fixed bugs in test
endvroy Jul 4, 2019
0165505
fixed bug in shape dim check
endvroy Jul 4, 2019
b6f31c9
fixed bug in out-of-bound checking
endvroy Jul 4, 2019
87054c7
fixed bug in out-of-bound checking
endvroy Jul 4, 2019
470ae2e
backward completed and tested
endvroy Jul 4, 2019
00dde0d
fixed bug in backward
endvroy Jul 4, 2019
3737f0a
added gpu support
endvroy Jul 4, 2019
cfbf7f8
reformatted
endvroy Jul 5, 2019
226d920
removed unused var
endvroy Jul 5, 2019
22cd5ed
fixed ambiguous call of div in CI
endvroy Jul 5, 2019
38bee62
fixed bug on gpu
endvroy Jul 5, 2019
0c72766
several code style fixes
endvroy Jul 8, 2019
16c7f3c
fixed sanity problem of readability
endvroy Jul 8, 2019
9f0b54a
added doc for diagflat
endvroy Jul 9, 2019
f0dba73
moved diagflat to init_op
endvroy Jul 15, 2019
d94d664
removed redundant blank lines
endvroy Jul 15, 2019
df05a4f
merged np_init.h
endvroy Jul 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
441 changes: 441 additions & 0 deletions example/numpy/demo.ipynb

Large diffs are not rendered by default.

308 changes: 308 additions & 0 deletions example/numpy/numpy_semantics.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to Use NumPy Semantics in MXNet with `mxnet.numpy` Module\n",
"\n",
"## NumPy Shape Semantics\n",
"\n",
"### Example \n",
"\n",
"| Shape Example | MXNet (before) | MXNet/NumPy |\n",
"|:---:|:---:|:---:|\n",
"| `()` | unknown | Scalar tensor |\n",
"| `(2, 0, 1)` | Second dimension unknown | Zero-size tensor |\n",
"| `None`(Python) | N/A | Unknown |\n",
"| `(2, -1, 0)`(C++) | N/A | Second dim uknown|\n",
"\n",
"### Affected modules\n",
"- Shape inference: imperative, symbolic, Gluon\n",
"- Legacy operators (not recommended to use)\n",
"- MXNet/NumPy operators\n",
"\n",
"## NumPy Array Semantics\n",
"**Definition:** The type of created ndarrays is `mxnet.numpy.ndarray`/`mxnet.symbol.numpy._Symbol`, instead of `mxnet.ndarray.NDArray`/`mxnet.symbol.Symbol` (only affects Gluon modules).\n",
"- Block/HybridBlock\n",
" - Parameter creation and initialization.\n",
" - Inputs/outputs (symbol/ndarray) of `__call__`/`forward`/`hybrid_forward`.\n",
" - Computational graph construction.\n",
"- Dataloader\n",
"\n",
"## Dependency of Two Types of Semantics\n",
"- It is required to keep NumPy shape semantics active while activating NumPy array semantics.\n",
"- Deactivating NumPy shape semantics while NumPy array semantics is still active is not allowed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"import mxnet as mx\n",
"from mxnet import np, npx, gluon\n",
"\n",
"logging.basicConfig(level=logging.INFO)\n",
"\n",
"try:\n",
" npx.set_np(shape=False, array=True)\n",
"except ValueError as e:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to Enable NumPy Shape semantics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" a = mx.nd.random.uniform(shape=())\n",
"except mx.MXNetError as e:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" b = mx.nd.random.uniform(shape=(2, 0, 1))\n",
"except mx.MXNetError as e:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" c = np.random.uniform()\n",
"except mx.MXNetError as e:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" d = np.random.uniform(size=(2, 0, 1))\n",
"except mx.MXNetError as e:\n",
" print(e) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"npx.set_np(shape=True, array=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = mx.nd.random.uniform(shape=())\n",
"b = mx.nd.random.uniform(shape=(2, 0, 1))\n",
"c = np.random.uniform()\n",
"d = np.random.uniform(size=(2, 0, 1))\n",
"\n",
"print('type(a) =', type(a))\n",
"print('a.shape = ', a.shape)\n",
"print('a.size = ', a.size)\n",
"\n",
"print('type(b) =', type(b))\n",
"print('b.shape = ', b.shape)\n",
"print('b.size = ', b.size)\n",
"\n",
"print('type(c) =', type(c))\n",
"print('c.shape = ', c.shape)\n",
"print('c.size = ', c.size)\n",
"\n",
"print('type(d) =', type(d))\n",
"print('d.shape = ', d.shape)\n",
"print('d.size = ', d.size)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to Enable NumPy Array Semantics\n",
"\n",
"### Parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"npx.reset_np() # reset two types of semantics to the default state, which is False for both of them\n",
"\n",
"from mxnet.gluon import nn\n",
"class Net(gluon.Block):\n",
" def __init__(self, in_units=0, **kwargs): # 0 means in_units is unknown and must be inferred at runtime\n",
" super(Net, self).__init__(**kwargs)\n",
" with self.name_scope():\n",
" self.dense0 = nn.Dense(5, in_units=in_units)\n",
" self.dense1 = nn.Dense(5, in_units=in_units)\n",
" \n",
" def forward(self, x):\n",
" return self.dense1(self.dense0(x))\n",
"\n",
"net1 = Net()\n",
"net1.initialize()\n",
"net1(mx.nd.zeros((3, 10)))\n",
"for k, v in net1.collect_params().items():\n",
" print('parameter {}, type {}'.format(k, str(type(v.data()))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"npx.set_np()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"net2 = Net()\n",
"net2.initialize()\n",
"net2(np.zeros((3, 10)))\n",
"for k, v in net2.collect_params().items():\n",
" print('parameter {}, type {}'.format(k, str(type(v.data()))))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dataloader"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"from mxnet.gluon import data as gdata\n",
"\n",
"\n",
"npx.reset_np()\n",
"\n",
"\n",
"def load_data_fashion_mnist(batch_size, resize=None, root=os.path.join(\n",
" '~', '.mxnet', 'datasets', 'fashion-mnist')):\n",
" \"\"\"Download the Fashion-MNIST dataset and then load into memory.\"\"\"\n",
" root = os.path.expanduser(root)\n",
" transformer = []\n",
" if resize:\n",
" transformer += [gdata.vision.transforms.Resize(resize)]\n",
" transformer += [gdata.vision.transforms.ToTensor()]\n",
" transformer = gdata.vision.transforms.Compose(transformer)\n",
"\n",
" mnist_train = gdata.vision.FashionMNIST(root=root, train=True)\n",
" mnist_test = gdata.vision.FashionMNIST(root=root, train=False)\n",
" num_workers = 0 if sys.platform.startswith('win32') else 4\n",
"\n",
" train_iter = gdata.DataLoader(mnist_train.transform_first(transformer),\n",
" batch_size, shuffle=True,\n",
" num_workers=num_workers)\n",
" test_iter = gdata.DataLoader(mnist_test.transform_first(transformer),\n",
" batch_size, shuffle=False,\n",
" num_workers=num_workers)\n",
" return train_iter, test_iter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_iter, test_iter = load_data_fashion_mnist(16)\n",
"\n",
"for X, y in train_iter:\n",
" print('type(X) = ', type(X))\n",
" print('type(y) = ', type(y))\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"npx.set_np()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_iter, test_iter = load_data_fashion_mnist(16)\n",
"\n",
"for X, y in train_iter:\n",
" print('type(X) = ', type(X))\n",
" print('type(y) = ', type(y))\n",
" break"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
4 changes: 3 additions & 1 deletion include/mxnet/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,9 @@ inline int32_t Context::GetGPUCount() {
#if MXNET_USE_CUDA
int32_t count;
cudaError_t e = cudaGetDeviceCount(&count);
if (e == cudaErrorNoDevice) {
// TODO(junwu): Remove e == 35
// This is skipped for working around wheel build system with older CUDA driver.
if (e == cudaErrorNoDevice || e == 35) {
return 0;
}
CHECK_EQ(e, cudaSuccess) << " CUDA: " << cudaGetErrorString(e);
Expand Down
12 changes: 12 additions & 0 deletions include/mxnet/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2811,6 +2811,18 @@ MXNET_DLL int MXEnginePushSync(EngineSyncFunc sync_func, void* func_param,
EngineVarHandle mutable_vars_handle, int num_mutable_vars,
EngineFnPropertyHandle prop_handle DEFAULT(NULL),
int priority DEFAULT(0), const char* opr_name DEFAULT(NULL));
/*!
* \brief Create an NDArray from source sharing the same data chunk.
* \param src source NDArray
* \param out new NDArray sharing the same data chunck with src
*/
MXNET_DLL int MXShallowCopyNDArray(NDArrayHandle src, NDArrayHandle* out);
/*!
* \brief Create an Symbol from source sharing the same graph structure.
* \param src source Symbol
* \param out new Symbol sharing the same graph structure with src
*/
MXNET_DLL int MXShallowCopySymbol(SymbolHandle src, SymbolHandle * out);

#ifdef __cplusplus
}
Expand Down
15 changes: 15 additions & 0 deletions include/mxnet/tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ class Tuple {
is.get();
if (ch == '(' || ch == '[') break;
if (!isspace(ch)) {
if (ch == 'N') {
std::string tmp_val;
is >> tmp_val;
if (tmp_val == "one") { // is stores "None"
t.SetDim(-1);
return is;
}
}
is.setstate(std::ios::failbit);
return is;
}
Expand Down Expand Up @@ -653,6 +661,13 @@ inline bool shape_is_known(const TShape& x) {
return true;
}

inline bool shape_is_known(const std::vector<TShape>& shapes) {
for (const TShape& shape : shapes) {
if (!shape_is_known(shape)) return false;
}
return true;
}

/*! \brief helper function to cast type of container elements */
template<typename SrcIter, typename DstIter>
inline DstIter ShapeTypeCast(const SrcIter begin,
Expand Down
5 changes: 5 additions & 0 deletions python/mxnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
from . import engine
from .base import MXNetError
from .util import is_np_shape, set_np_shape, np_shape, use_np_shape
from .util import is_np_array, np_array, use_np_array, use_np
from . import base
from . import contrib
from . import ndarray
from . import ndarray as nd
from . import numpy
from . import numpy_extension
from . import numpy as np
from . import numpy_extension as npx
from . import name
# use mx.sym as short for symbol
from . import symbol as sym
Expand Down
Loading