TypeError: blank() got an unexpected keyword argument 'disable'

I am running a text classification model in production that has been using fastai 1.0.60. I chose not to update to fastai v2 because there were bugs around using load_learner() and loading a pretrained model. Up until early January, this method was working perfectly, but now I am getting this error when running learn.predict(). The input is a string, so no idea why this error is popping up. Has anyone else faced this issue? Thanks!

torch == 1.4.0
fastai == 1.0.60

path = ‘files/modelsavelocation’
learn = load_learner(path, ‘final_whole’)
learn.predict(‘How can I find bios?’)

TypeError Traceback (most recent call last)
in
----> 1 learn.predict(‘How can I find bios?’)

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/basic_train.py in predict(self, item, return_x, batch_first, with_dropout, **kwargs)
370 def predict(self, item:ItemBase, return_x:bool=False, batch_first:bool=True, with_dropout:bool=False, **kwargs):
371 “Return predicted class, label and probabilities for item.”
–> 372 batch = self.data.one_item(item)
373 res = self.pred_batch(batch=batch, with_dropout=with_dropout)
374 raw_pred,x = grab_idx(res,0,batch_first=batch_first),batch[0]

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/basic_data.py in one_item(self, item, detach, denorm, cpu)
179 “Get item into a batch. Optionally detach and denorm.”
180 ds = self.single_ds
–> 181 with ds.set_item(item):
182 return self.one_batch(ds_type=DatasetType.Single, detach=detach, denorm=denorm, cpu=cpu)
183

/usr/lib/python3.7/contextlib.py in enter(self)
110 del self.args, self.kwds, self.func
111 try:
–> 112 return next(self.gen)
113 except StopIteration:
114 raise RuntimeError(“generator didn’t yield”) from None

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/data_block.py in set_item(self, item)
613 def set_item(self,item):
614 “For inference, will briefly replace the dataset with one that only contains item.”
–> 615 self.item = self.x.process_one(item)
616 yield None
617 self.item = None

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/data_block.py in process_one(self, item, processor)
89 if processor is not None: self.processor = processor
90 self.processor = listify(self.processor)
—> 91 for p in self.processor: item = p.process_one(item)
92 return item
93

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/text/data.py in process_one(self, item)
289
290 def process_one(self, item):
–> 291 return self.tokenizer._process_all_1(_join_texts([item], self.mark_fields, self.include_bos, self.include_eos))[0]
292
293 def process(self, ds):

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/text/transform.py in _process_all_1(self, texts)
110 def _process_all_1(self, texts:Collection[str]) -> List[List[str]]:
111 “Process a list of texts in one process.”
–> 112 tok = self.tok_func(self.lang)
113 if self.special_cases: tok.add_special_cases(self.special_cases)
114 return [self.process_text(str(t), tok) for t in texts]

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/text/transform.py in init(self, lang)
23 “Wrapper around a spacy tokenizer to make it a BaseTokenizer.”
24 def init(self, lang:str):
—> 25 self.tok = spacy.blank(lang, disable=[“parser”,“tagger”,“ner”])
26
27 def tokenizer(self, t:str) -> List[str]:

TypeError: blank() got an unexpected keyword argument ‘disable’

What’s your version of spacy?

spacy is version 3.0.1

Can you try again? Jeremy pinned the spacy version to 3.0.0

(You’ll need to install with pip install git+https://github.com/fastai/fastai @helenas)

Unfortunately, doing this gives me this error:

ImportError                               Traceback (most recent call last)
<command-600356928450651> in <module>
      1 import pandas as pd
      2 import fastai
----> 3 from fastai.text import load_learner
      4 print(fastai.__version__)

ImportError: cannot import name 'load_learner' from 'fastai.text' (/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/text/__init__.py)

I am running this model in Databricks, so I load fastai and torch locally in the notebook to make sure the load_learner error doesn’t happen. .

%pip install torch==1.4.0
%pip install fastai==1.0.60

Pretty sure you need to do:

from fastai.learner import load_learner

Hi Zachary,

Thank you for the help. I had trained and saved my model using fastai v1 as ‘final_whole’ into the path location listed. Now when I run the following to load the model back, there is this error:

path = '/dbfs/mnt/sdslake/sds/lake/workspaces/eservices/' 
learn = load_learner(path, 'final_whole')

IsADirectoryError Traceback (most recent call last)
in
1 path = ‘/dbfs/mnt/sdslake/sds/lake/workspaces/eservices/’ #/mnt/sdslake/sds/lake/workspaces/eservices/CSAT_data
----> 2 learn = load_learner(path, ‘final_whole’)

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/learner.py in load_learner(fname, cpu, pickle_module)
372 "Load a Learner object in fname, optionally putting it on the cpu"
373 distrib_barrier()
–> 374 res = torch.load(fname, map_location=‘cpu’ if cpu else None, pickle_module=pickle_module)
375 if hasattr(res, ‘to_fp32’): res = res.to_fp32()
376 if cpu: res.dls.cpu()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
579 pickle_load_args[‘encoding’] = ‘utf-8’
580
–> 581 with _open_file_like(f, ‘rb’) as opened_file:
582 if _is_zipfile(opened_file):
583 # The zipfile reader is going to advance the current file position.

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/torch/serialization.py in _open_file_like(name_or_buffer, mode)
228 def _open_file_like(name_or_buffer, mode):
229 if _is_path(name_or_buffer):
–> 230 return _open_file(name_or_buffer, mode)
231 else:
232 if ‘w’ in mode:

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/torch/serialization.py in init(self, name, mode)
209 class _open_file(_opener):
210 def init(self, name, mode):
–> 211 super(_open_file, self).init(open(name, mode))
212
213 def exit(self, *args):

IsADirectoryError: [Errno 21] Is a directory: ‘/dbfs/mnt/sdslake/sds/lake/workspaces/eservices/’

Alternatively, running this code instead gives the following error:
learn = load_learner(’/dbfs/mnt/sdslake/sds/lake/workspaces/eservices/final_whole’)

AttributeError: Can’t get attribute ‘FlattenedLoss’ on <module ‘fastai.layers’ from ‘/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/layers.py’>

AttributeError Traceback (most recent call last)
in
1 path = ‘/dbfs/mnt/sdslake/sds/lake/workspaces/eservices/’ #/mnt/sdslake/sds/lake/workspaces/eservices/CSAT_data
----> 2 learn = load_learner(’/dbfs/mnt/sdslake/sds/lake/workspaces/eservices/final_whole’)

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/learner.py in load_learner(fname, cpu, pickle_module)
372 "Load a Learner object in fname, optionally putting it on the cpu"
373 distrib_barrier()
–> 374 res = torch.load(fname, map_location=‘cpu’ if cpu else None, pickle_module=pickle_module)
375 if hasattr(res, ‘to_fp32’): res = res.to_fp32()
376 if cpu: res.dls.cpu()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
593 return torch.jit.load(opened_file)
594 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
–> 595 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
596
597

/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/torch/serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
772 unpickler = pickle_module.Unpickler(f, **pickle_load_args)
773 unpickler.persistent_load = persistent_load
–> 774 result = unpickler.load()
775
776 deserialized_storage_keys = pickle_module.load(f, **pickle_load_args)

AttributeError: Can’t get attribute ‘FlattenedLoss’ on <module ‘fastai.layers’ from ‘/local_disk0/.ephemeral_nfs/envs/pythonEnv-5caaef5d-ec1e-4e25-80d3-38d7a857d092/lib/python3.7/site-packages/fastai/layers.py’>

I think I was facing this issue previously in dealing with the update from fastai v1 to v2, and reverting to fastai==1.0.60 fixed the issue, as the model I’m loading was trained with v1. Do you know what is causing this and how to fix? Thanks!

Yeah, fastai isn’t backwards compatible. So you must use and train fastai and deploy it in whatever version it was, you can’t use a learner trained in v2 with v1 and vice versa

Thanks Zachary for your help :slight_smile: I think that brings me back to my original error:

TypeError: blank() got an unexpected keyword argument ‘disable’

It looks like the error is linked to backwards incompatibility with the new spaCy v3.0 release, so pinning to spacy==2.3.5 resolved the issue. Thanks for your help!

5 Likes

Even with spacy==2.3.5 I still get “TypeError: blank() got an unexpected keyword argument ‘disable’” when creating the IMDB model in 01 intro notebook. Any other suggestions to resolve? Thanks.

You should restart your notebook.

(WordTokenizer Error) this answer works for me

Hello Zachary,

I have same problem with error message with “spacy = WordTokenizer()” getting the error message

I am using JupiterLab, I also installed “!pip install git+https://github.com/fastai/fastai” to get spacy version to 3.0.0 as you recommended and I still get the same error message.

to be honest, I get this type of incompatibility often with Fastai lessons, makes me waste lots of time figuring out the issue instead of focusing on real machine learning learning, I wonder if there are any ways to make the lessons more user friendly? I understand that this may be part of the learning, but not sure…

Thanks!

Christian

1 Like

As others have mentioned in the thread (such as the post just before this) you need spacy to be less than 3.0.0 to 2.2.4

yes, I also tried 2.2.4 before and get the same error message. the following window shows that I have installed spacy 2.2.4:

sorry, I closed my JupyterLab session and came back, reinstall spacy==2.2.4 and now it works…I do not know why, but since it works I am happy.

Thanks for the help, really appreciate

1 Like

I created a new class similar to SpacyTokenizer and

import spacy

from fastcore.utils import ifnone, defaults
from fastcore.foundation import L
from spacy.symbols import ORTH

UNK, PAD, BOS, EOS, FLD, TK_REP, TK_WREP, TK_UP, TK_MAJ = "xxunk xxpad xxbos xxeos xxfld xxrep xxwrep xxup xxmaj".split()
defaults.text_spec_tok = [UNK, PAD, BOS, EOS, FLD, TK_REP, TK_WREP, TK_UP, TK_MAJ]

class SpacyTokenizerV3():
"Spacy tokenizer for `lang`"
def __init__(self, lang='en', special_toks=None, buf_sz=5000):
    self.special_toks = ifnone(special_toks, defaults.text_spec_tok)
    nlp = spacy.blank(lang)
    for w in self.special_toks: nlp.tokenizer.add_special_case(w, [{ORTH: w}])
    self.pipe, self.buf_sz = nlp.pipe, buf_sz

def __call__(self, items):
    return (L(doc).attrgot('text') for doc in self.pipe(map(str,items), batch_size=self.buf_sz))

Then I could use the new class to create a tokenizer, doing that I could keep a Spacy at a version higher or equal to 3.0.0.

from functools import partial
from fastai.data.all import RandomSplitter

get_corpus = partial(get_text_files)
tokenizer = SpacyTokenizerV3(lang="de")

dls_lm = DataBlock(
    blocks=TextBlock.from_folder(path, tok=tokenizer, is_lm=True),
    get_items=get_corpus, splitter=RandomSplitter(0.1)
).dataloaders(path, path=path, bs=128, seq_len=80)
1 Like

The latest fastai now supports spacy 3.x

I just got the same issue when I started learning NLP by Fastai Courses on Youtube. I used Google Colab to do exercises from these courses. I just pasted everything from the Jupyter guidelines to my notebook but it didn’t run as well. It took me a lot of time to make it run in Google Colab. This issue relates to spacy version. But there’s something strange that I could not explain. If I use GPU runtime environment, I need spacy 2.3.5 version, otherwise, I had to change spacy to 2.2.4 version. I hope that my answer could help someone who are in the same situation. Make sure that you are using fastai version 1 when practicing for the first lessons.

1 Like