Error while loading encoder from fastai v1 to fastai v2

Hello everyone,

I’m trying to load an encoder that I trained with fastai v1 when creating a text classifier with fastai v2. The strange thing is that I get an error only when I’m using learn.lr_find().

I’m using the vocab.itos from my DataBunch that I created with v1. This part seems to work.

dblock = DataBlock(blocks=(TextBlock.from_df('text', seq_len=sl, vocab=data_lm.vocab.itos), CategoryBlock),
                      get_x=ColReader('text'),
                      get_y=ColReader('target'),
                      splitter=ColSplitter('is_valid'))
dls_cls = dblock.dataloaders(df, bs=bs, seq_len=sl)

Then I’m creating the learner and load the encoder:

learn = text_classifier_learner(dls_cls, AWD_LSTM, drop_mult=0.5, metrics=[accuracy,F1Score(average='macro')],
                               loss_func=LabelSmoothingCrossEntropy()).to_fp16()
learn.load_encoder('lm_octa_30_enc');

If I now run fit_one_cycle as below everything works fine.

lr, n_epoch, wd = 2e-2, 1, 0.01
learn.fit_one_cycle(n_epoch, lr, wd=wd)

Only when I run learn.lr_find() first and then start fitting I’m getting the following error message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-80-31c072db3bda> in <module>
      1 lr, n_epoch, wd = 2e-2, 1, 0.01
----> 2 learn.fit_one_cycle(n_epoch, lr, wd=wd)

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    110     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    111               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 112     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    113 
    114 # Cell

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    195 
    196             except CancelFitException:             self('after_cancel_fit')
--> 197             finally:                               self('after_fit')
    198 
    199     def validate(self, ds_idx=1, dl=None, cbs=None):

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/learner.py in __call__(self, event_name)
    121     def ordered_cbs(self, cb_func): return [cb for cb in sort_by_run(self.cbs) if hasattr(cb, cb_func)]
    122 
--> 123     def __call__(self, event_name): L(event_name).map(self._call_one)
    124     def _call_one(self, event_name):
    125         assert hasattr(event, event_name)

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/foundation.py in map(self, f, *args, **kwargs)
    360              else f.format if isinstance(f,str)
    361              else f.__getitem__)
--> 362         return self._new(map(g, self))
    363 
    364     def filter(self, f, negate=False, **kwargs):

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/foundation.py in _new(self, items, *args, **kwargs)
    313     @property
    314     def _xtra(self): return None
--> 315     def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
    316     def __getitem__(self, idx): return self._get(idx) if is_indexer(idx) else L(self._get(idx), use_list=None)
    317     def copy(self): return self._new(self.items.copy())

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/foundation.py in __call__(cls, x, *args, **kwargs)
     39             return x
     40 
---> 41         res = super().__call__(*((x,) + args), **kwargs)
     42         res._newchk = 0
     43         return res

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/foundation.py in __init__(self, items, use_list, match, *rest)
    304         if items is None: items = []
    305         if (use_list is not None) or not _is_array(items):
--> 306             items = list(items) if use_list else _listify(items)
    307         if match is not None:
    308             if is_coll(match): match = len(match)

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/foundation.py in _listify(o)
    240     if isinstance(o, list): return o
    241     if isinstance(o, str) or _is_array(o): return [o]
--> 242     if is_iter(o): return list(o)
    243     return [o]
    244 

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/foundation.py in __call__(self, *args, **kwargs)
    206             if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
    207         fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 208         return self.fn(*fargs, **kwargs)
    209 
    210 # Cell

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/learner.py in _call_one(self, event_name)
    124     def _call_one(self, event_name):
    125         assert hasattr(event, event_name)
--> 126         [cb(event_name) for cb in sort_by_run(self.cbs)]
    127 
    128     def _bn_bias_state(self, with_bias): return bn_bias_params(self.model, with_bias).map(self.opt.state)

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/learner.py in <listcomp>(.0)
    124     def _call_one(self, event_name):
    125         assert hasattr(event, event_name)
--> 126         [cb(event_name) for cb in sort_by_run(self.cbs)]
    127 
    128     def _bn_bias_state(self, with_bias): return bn_bias_params(self.model, with_bias).map(self.opt.state)

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/callback/core.py in __call__(self, event_name)
     21         _run = (event_name not in _inner_loop or (self.run_train and getattr(self, 'training', True)) or
     22                (self.run_valid and not getattr(self, 'training', False)))
---> 23         if self.run and _run: getattr(self, event_name, noop)()
     24         if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
     25 

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/callback/fp16.py in after_fit(self)
     66     run_before=TrainEvalCallback
     67     def begin_fit(self): self.learn.model = convert_network(self.model, dtype=torch.float16)
---> 68     def after_fit(self): self.learn.model = convert_network(self.model, dtype=torch.float32)
     69 
     70 # Cell

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/fp16_utils.py in convert_network(network, dtype)
     68         convert_module(module, dtype)
     69         if isinstance(module, torch.nn.RNNBase) or isinstance(module, torch.nn.modules.rnn.RNNBase):
---> 70             module.flatten_parameters()
     71     return network
     72 

/opt/conda/envs/fastai/lib/python3.7/site-packages/torch/nn/modules/rnn.py in flatten_parameters(self)
    127                     all_weights, (4 if self.bias else 2),
    128                     self.input_size, rnn.get_cudnn_mode(self.mode), self.hidden_size, self.num_layers,
--> 129                     self.batch_first, bool(self.bidirectional))
    130 
    131     def _apply(self, fn):

RuntimeError: param_from.type() == param_to.type() INTERNAL ASSERT FAILED at /opt/conda/conda-bld/pytorch_1579022060824/work/aten/src/ATen/native/cudnn/RNN.cpp:541, please report a bug to PyTorch. parameter types mismatch

Any ideas? Loading a pre-trained model from v1 into v2 should be generally working, right?

Second this question. Did you find a solution?