Flair: use BERT embedding as instruction and have ModuleNotFoundError: No module named 'fused_layer_norm_cuda'

Created on 20 Feb 2019  路  6Comments  路  Source: flairNLP/flair

Describe the bug
File "run_classifier_torch.py", line 686, in
main()
File "run_classifier_torch.py", line 525, in main
num_labels = num_labels)
File "/home/yuwei/anaconda2/envs/py36/lib/python3.6/site-packages/pytorch_pretrained_bert/modeling.py", line 580, in from_pretrained
model = cls(config, inputs, *kwargs)
File "/home/yuwei/anaconda2/envs/py36/lib/python3.6/site-packages/pytorch_pretrained_bert/modeling.py", line 963, in __init__
self.bert = BertModel(config)
File "/home/yuwei/anaconda2/envs/py36/lib/python3.6/site-packages/pytorch_pretrained_bert/modeling.py", line 684, in __init__
self.embeddings = BertEmbeddings(config)
File "/home/yuwei/anaconda2/envs/py36/lib/python3.6/site-packages/pytorch_pretrained_bert/modeling.py", line 246, in __init__
self.LayerNorm = BertLayerNorm(config.hidden_size, eps=1e-12)
File "/home/yuwei/anaconda2/envs/py36/lib/python3.6/site-packages/apex-0.1-py3.6.egg/apex/normalization/fused_layer_norm.py", line 126, in __init__
File "/home/yuwei/anaconda2/envs/py36/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'fused_layer_norm_cuda'

To Reproduce
from flair.embeddings import BertEmbeddings
from flair.data import Sentence

init embedding

embedding = BertEmbeddings()

create a sentence

sentence = Sentence('The grass is green .')

embed words in the sentence

embedding.embed(sentence)

for token in sentence:
print(token)
print(token.embedding)
Expected behavior
seeing error

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • OS Linux ubuntu 18
  • Version [e.g. flair-0.3.2]:

Additional context
Add any other context about the problem here.

bug

Most helpful comment

fused_layer_norm_cuda is a module from https://github.com/NVIDIA/apex. To reproduce the error, I just had to install apex in a virtual environment with Python 3.7, flair 0.4, pytorch 1.01 and pytorch_pretrained_bert 0.6.1 on a Windows 10 machine without CUDA.

In https://github.com/NVIDIA/apex/blob/master/setup.py you can see that 'fused_layer_norm_cuda' is only installed if you add '--cuda_ext' to the install command and only if the CUDA_HOME variable is set to the folder where Cuda is installed.

@PaulZhangIsing maybe one of these 2 is the case for you?
Can you confirm that you can run it without apex installed?

All 6 comments

Hello @PaulZhangIsing which version of Flair do you use? and which version of pytorch-pretrained-bert do you have installed?

Hello @PaulZhangIsing which version of Flair do you use? and which version of pytorch-pretrained-bert do you have installed?

I am using python3.6
flair version 0.4.0
pytorch 1.0.1

fused_layer_norm_cuda is a module from https://github.com/NVIDIA/apex. To reproduce the error, I just had to install apex in a virtual environment with Python 3.7, flair 0.4, pytorch 1.01 and pytorch_pretrained_bert 0.6.1 on a Windows 10 machine without CUDA.

In https://github.com/NVIDIA/apex/blob/master/setup.py you can see that 'fused_layer_norm_cuda' is only installed if you add '--cuda_ext' to the install command and only if the CUDA_HOME variable is set to the folder where Cuda is installed.

@PaulZhangIsing maybe one of these 2 is the case for you?
Can you confirm that you can run it without apex installed?

fused_layer_norm_cuda is a module from https://github.com/NVIDIA/apex. To reproduce the error, I just had to install apex in a virtual environment with Python 3.7, flair 0.4, pytorch 1.01 and pytorch_pretrained_bert 0.6.1 on a Windows 10 machine without CUDA.

In https://github.com/NVIDIA/apex/blob/master/setup.py you can see that 'fused_layer_norm_cuda' is only installed if you add '--cuda_ext' to the install command and only if the CUDA_HOME variable is set to the folder where Cuda is installed.

@PaulZhangIsing maybe one of these 2 is the case for you?
Can you confirm that you can run it without apex installed?

Thanks!! seems I didn't install apex correctly. I ran setup.py install without --cuda_ext command
I shall try and get back

Problem solved.

  1. install apex as instructed above
  2. run codes, if encounter issue #421
    doing this would help
    from flair.embeddings import BertEmbeddings
    from flair.data import Sentence
    from flair.embeddings import FlairEmbeddings

init embedding

flair_embedding_forward = FlairEmbeddings('news-forward')
bert_embedding = BertEmbeddings('bert-large-cased').cuda()

create a sentence

sentence = Sentence('The grass is green .')

embed words in sentence

x = bert_embedding.embed(sentence)

for token in sentence:
print(token)
print(token.embedding)

import torch

print(torch.__version__)

That's great! @PaulZhangIsing thanks for reporting this. @MichaelHintz thanks for fixing this!

Was this page helpful?
0 / 5 - 0 ratings