anirudhacharya opened a new issue #13805: Not able to load float16 parameters 
with Block.load_parameters
URL: https://github.com/apache/incubator-mxnet/issues/13805
 
 
   ## Description
   Not able to load float16 parameters with Block.load_parameters. I get the 
error
   ```
   dtype incompatible expected <type 'numpy.float32'> vs saved <type 
'numpy.float16'>
   ```
   
   ## Environment info (Required)
   
   ```
   ----------Python Info----------
   ('Version      :', '2.7.12')
   ('Compiler     :', 'GCC 5.4.0 20160609')
   ('Build        :', ('default', 'Nov 12 2018 14:36:49'))
   ('Arch         :', ('64bit', 'ELF'))
   ------------Pip Info-----------
   ('Version      :', '10.0.1')
   ('Directory    :', '/usr/local/lib/python2.7/dist-packages/pip')
   ----------MXNet Info-----------
   ('Version      :', '1.5.0')
   ('Directory    :', 
'/usr/local/lib/python2.7/dist-packages/mxnet-1.5.0-py2.7.egg/mxnet')
   Hashtag not found. Not installed from pre-built package.
   ----------System Info----------
   ('Platform     :', 'Linux-4.4.0-1074-aws-x86_64-with-Ubuntu-16.04-xenial')
   ('system       :', 'Linux')
   ('node         :', 'ip-172-31-85-46')
   ('release      :', '4.4.0-1074-aws')
   ('version      :', '#84-Ubuntu SMP Thu Dec 6 08:57:58 UTC 2018')
   ----------Hardware Info----------
   ('machine      :', 'x86_64')
   ('processor    :', 'x86_64')
   Architecture:          x86_64
   CPU op-mode(s):        32-bit, 64-bit
   Byte Order:            Little Endian
   CPU(s):                64
   On-line CPU(s) list:   0-63
   Thread(s) per core:    2
   Core(s) per socket:    16
   Socket(s):             2
   NUMA node(s):          2
   Vendor ID:             GenuineIntel
   CPU family:            6
   Model:                 79
   Model name:            Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz
   Stepping:              1
   CPU MHz:               1200.312
   CPU max MHz:           3000.0000
   CPU min MHz:           1200.0000
   BogoMIPS:              4600.12
   Hypervisor vendor:     Xen
   Virtualization type:   full
   L1d cache:             32K
   L1i cache:             32K
   L2 cache:              256K
   L3 cache:              46080K
   NUMA node0 CPU(s):     0-15,32-47
   NUMA node1 CPU(s):     16-31,48-63
   Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge 
mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx pdpe1gb rdtscp lm 
constant_tsc arch_perfmon rep_good nopl xtopology nonstop_tsc aperfmperf pni 
pclmulqdq monitor est ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt 
tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 
3dnowprefetch invpcid_single kaiser fsgsbase bmi1 hle avx2 smep bmi2 erms 
invpcid rtm rdseed adx xsaveopt ida
   ----------Network Test----------
   Setting timeout: 10
   Timing for MXNet: https://github.com/apache/incubator-mxnet, DNS: 0.0026 
sec, LOAD: 0.3206 sec.
   Timing for PYPI: https://pypi.python.org/pypi/pip, DNS: 0.0036 sec, LOAD: 
0.0824 sec.
   Timing for FashionMNIST: 
https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/fashion-mnist/train-labels-idx1-ubyte.gz,
 DNS: 0.0089 sec, LOAD: 0.5292 sec.
   Timing for Conda: https://repo.continuum.io/pkgs/free/, DNS: 0.0037 sec, 
LOAD: 0.0313 sec.
   Timing for Gluon Tutorial(en): http://gluon.mxnet.io, DNS: 0.1404 sec, LOAD: 
0.0234 sec.
   Timing for Gluon Tutorial(cn): https://zh.gluon.ai, DNS: 0.2387 sec, LOAD: 
0.4812 sec.
   ```
   
   ## Error Message:
   ```
   Traceback (most recent call last):
     File "test_gluon.py", line 45, in <module>
       new_net.load_parameters("net.params", ctx=ctx)
     File 
"/usr/local/lib/python2.7/dist-packages/mxnet-1.5.0-py2.7.egg/mxnet/gluon/block.py",
 line 402, in load_parameters
       params[name]._load_init(loaded[name], ctx)
     File 
"/usr/local/lib/python2.7/dist-packages/mxnet-1.5.0-py2.7.egg/mxnet/gluon/parameter.py",
 line 243, in _load_init
       self.name, str(self.dtype), str(data.dtype))
   AssertionError: Failed loading Parameter 'multiinputmnist1_conv0_weight' 
from saved params: dtype incompatible expected <type 'numpy.float32'> vs saved 
<type 'numpy.float16'>
   ```
   
   ## Minimum reproducible example
   ```python
   import numpy as np                                                           
                                                                              
   import mxnet as mx
   from mxnet import gluon
   mx.random.seed(1)
   from collections import namedtuple
   
   #####################################
   #Create Model in Gluon
   ctx = mx.gpu()
   
   class MultiInputMnist(gluon.nn.HybridBlock):
       def __init__(self, **kwargs):
           gluon.nn.HybridBlock.__init__(self, **kwargs) # attention here to 
pass kwargs to initialization of hybridblock
           with self.name_scope():
               self.conv = gluon.nn.Conv2D(channels=20, kernel_size=3, 
dilation=(1,1), use_bias= True, activation='relu')
               self.pool = gluon.nn.MaxPool2D(pool_size=2, strides=2)
   
       def hybrid_forward(self, F, input_1, input_2): # You don't really need 
*args, **kwards in this case
           add = F.broadcast_add(input_1, input_2)
           c = self.conv(add)
           p = self.pool(c)
           return p
   
   net = MultiInputMnist()
   net.collect_params().initialize(ctx=ctx)
   
   net.cast('float16')
   net.hybridize()
   np_data1 = np.random.randn(64,1,28,28)
   np_data2 = np.random.randn(64,1,28,28)
   inp1 = mx.nd.cast(mx.nd.array(np_data1, ctx=ctx), dtype='float16') 
#mx.nd.array(np_data1)
   inp2 = mx.nd.cast(mx.nd.array(np_data1, ctx=ctx), dtype='float16') 
#mx.nd.array(np_data2)
   #mx.nd.cast(mx.nd.array(np_data), dtype='float16')
   net.forward(inp1, inp2)
   
   #export for reloading in Gluon
   net.save_parameters("net.params")
   
   #export for reloading in Module API
   net.export("mnist16")
   
   ##################################### 
   #load model in Gluon and infer
   new_net = MultiInputMnist()
   new_net.load_parameters("net.params", ctx=ctx)
   print(new_net.forward(inp1, inp2))
   ```
   
   
   ## Steps to reproduce
   1. Run the above code
   
   ## What have you tried to solve it?
   1. save the parameters as float32. Load the f32 params into the gluon block, 
and then cast it to f16.
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to