Repository: chainer/chainer

Issue:
Feature : warning for vecLib on Mac
Running examples/train_mnist.py resulted in unreasonable behaviour (exploding loss & low accuracy) as follows:
```
Masaki-no-MacBook-Pro:mnist omeometo$ python train_mnist.py
GPU: -1
# unit: 1000
# Minibatch-size: 100
# epoch: 20
epoch       main/loss   validation/main/loss  main/accuracy  validation/main/accuracy  elapsed_time
1           60.7941     84.565                0.52985        0.5502                    47.3283       
2           235.41      484.166               0.532617       0.3074                    89.3148       
3           587.228     244.073               0.55655        0.7105                    123.146       
4           738.536     386.792               0.60925        0.6935                    154.823       
5           1135.55     2467.39               0.6131         0.5234                    185.989       
6           1712.49     916.105               0.60735        0.6731                    216.959       
7           2311.43     3468.54               0.62715        0.5642                    248.193       
8           2610.56     2006.75               0.633933       0.6788                    278.72        
9           3358.19     1443.68               0.644533       0.723                     310.921       
10          4369.74     2402.41               0.6331         0.6985                    342.471       
11          4168.4      1578.52               0.661133       0.7946                    374.207       
12          6144.09     2845.26               0.654467       0.7004                    405.498       
13          6025.8      2245.78               0.66355        0.8174                    436.602       
14          6675.77     7182.15               0.661317       0.6203                    467.895       
15          7697.48     5312.16               0.66345        0.6928                    499.086       
16          7775.43     5914.33               0.6727         0.737                     530.525       
17          10321.6     6359.2                0.655067       0.7048                    561.185       
18          9819.28     18361.4               0.680817       0.5946                    592.306       
19          11861.1     13093.2               0.678433       0.6231                    623.2         
20          12883.1     5928.4                0.67095        0.7723                    654.457      
```

It seems to have something to do with vecLib on MacOS. Here is the result of ```numpy.show_config()``` :

```
blas_mkl_info:
  NOT AVAILABLE
blis_info:
  NOT AVAILABLE
openblas_info:
  NOT AVAILABLE
atlas_3_10_blas_threads_info:
  NOT AVAILABLE
atlas_3_10_blas_info:
  NOT AVAILABLE
atlas_blas_threads_info:
  NOT AVAILABLE
atlas_blas_info:
  NOT AVAILABLE
blas_opt_info:
    extra_compile_args = ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers']
    extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']
    define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]
lapack_mkl_info:
  NOT AVAILABLE
openblas_lapack_info:
  NOT AVAILABLE
atlas_3_10_threads_info:
  NOT AVAILABLE
atlas_3_10_info:
  NOT AVAILABLE
atlas_threads_info:
  NOT AVAILABLE
atlas_info:
  NOT AVAILABLE
lapack_opt_info:
    extra_compile_args = ['-msse3']
    extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']
    define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]
```
Reinstalling numpy with ```conda install numpy``` solved the problem.

This seems to be the same problem as in [this post](https://groups.google.com/forum/?hl=ja#!topic/chainer-jp/X3K-vZlQd_Y). It would be desirable to have some warning message about using chainer with vecLib. 


Discussion:
I also encountered the same problem before and took quite some time to track down the cause.
Displaying warning would be very helpful for users who are unfamiliar with numpy backends.
I have noticed that this problem is already warned in [Tips & FAQs in Chainer document](https://docs.chainer.org/en/stable/tips.html) :bowing_man:. But IMO it would be still good to have a warning message or a (sample output + link to Tips) in [Chainer Tutorial > Introduction to Chainer > Example: Multi-layer Perceptron on MNIST](https://docs.chainer.org/en/stable/tutorial/basic.html#example-multi-layer-perceptron-on-mnist) because, some would run MNIST example without noticing strange behavior (i.e. without noticing that accuracy 6~70% is too low, for example) and it would be quite troublesome then.

Resolving patch:
diff --git a/chainer/__init__.py b/chainer/__init__.py
--- a/chainer/__init__.py
+++ b/chainer/__init__.py
@@ -1,7 +1,6 @@
 import collections
 import os
 import pkg_resources
-import sys
 import threading
 import warnings
 
@@ -62,16 +61,11 @@
 from chainer.variable import Variable  # NOQA
 
 
-if sys.version_info[:3] == (3, 5, 0):
-    if not int(os.getenv('CHAINER_PYTHON_350_FORCE', '0')):
-        msg = """
-Chainer does not work with Python 3.5.0.
+from chainer import _environment_check
 
-We strongly recommend to use another version of Python.
-If you want to use Chainer with Python 3.5.0 at your own risk,
-set 1 to CHAINER_PYTHON_350_FORCE environment variable."""
 
-        raise Exception(msg)
+# Check environment conditions
+_environment_check.check()
 
 
 __version__ = pkg_resources.get_distribution('chainer').version
diff --git a/chainer/_environment_check.py b/chainer/_environment_check.py
new file mode 100644
--- /dev/null
+++ b/chainer/_environment_check.py
@@ -0,0 +1,43 @@
+import os
+import sys
+import warnings
+
+import numpy.distutils.system_info
+
+
+def _check_python_350():
+    if sys.version_info[:3] == (3, 5, 0):
+        if not int(os.getenv('CHAINER_PYTHON_350_FORCE', '0')):
+            msg = """
+    Chainer does not work with Python 3.5.0.
+
+    We strongly recommend to use another version of Python.
+    If you want to use Chainer with Python 3.5.0 at your own risk,
+    set 1 to CHAINER_PYTHON_350_FORCE environment variable."""
+
+            raise Exception(msg)
+
+
+def _check_osx_numpy_backend():
+    if sys.platform != 'darwin':
+        return
+
+    blas_opt_info = numpy.distutils.system_info.get_info('blas_opt')
+    if blas_opt_info:
+        extra_link_args = blas_opt_info.get('extra_link_args')
+        if extra_link_args and '-Wl,Accelerate' in extra_link_args:
+            warnings.warn('''\
+Accelerate has been detected as a NumPy backend library.
+vecLib, which is a part of Accelerate, is known not to work correctly with Chainer.
+We recommend using other BLAS libraries such as OpenBLAS.
+For details of the issue, please see
+https://docs.chainer.org/en/stable/tips.html#mnist-example-does-not-converge-in-cpu-mode-on-mac-os-x.
+
+Also note that Chainer does not officially support Mac OS X.
+Please use it at your own risk.
+''')  # NOQA
+
+
+def check():
+    _check_python_350()
+    _check_osx_numpy_backend()


Test patch: