Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No module named 'fast_transformers.causal_product.causal_product_cpu' (solved: needed to at CUDA to the PATH) #12

Closed
ghost opened this issue Jul 19, 2020 · 8 comments

Comments

@ghost
Copy link

ghost commented Jul 19, 2020

Hi there,

I am having some trouble using this library. I cloned this repo (July 19th) and ran the setup file, the setup ran but now I am getting this error (the same error occurs with pip install):

  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/builders/__init__.py", line 29, in <module>
    from .transformer_encoder_builder import TransformerEncoderBuilder
  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/builders/transformer_encoder_builder.py", line 31, in <module>
    from ..attention import AttentionLayer, FullAttention, \
  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/attention/__init__.py", line 13, in <module>
    from .causal_linear_attention import CausalLinearAttention
  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/attention/causal_linear_attention.py", line 12, in <module>
    from fast_transformers.causal_product import causal_dot_product 
  File "/usr/local/lib/python3.6/dist-No module named 'fast_transformers.causal_product.causal_product_cpu'packages/fast_transformers/causal_product/__init__.py", line 9, in <module>
    from .causal_product_cpu import causal_dot_product as causal_dot_product_cpu, \
ModuleNotFoundError: 

When I comment out importing this file (above), I get an import error on the hashing files instead, so I think the issues is these CUDA files. I am using Ubuntu 18.04 and PyTorch 1.5.1 with CUDA 10.2. However using the exact same setup procedure on Google Colab, I have no issues - Colab uses PyTorch 1.5.1 but CUDA 10.1.

Could the CUDA version difference be the issue?

Thanks :)

@angeloskath
Copy link
Collaborator

It doesn't seem that the CUDA version could be the issue since it complains it cannot find the causal_product_cpu module. This is the CPU operations for autoregressive linear attention.

Is this error caused by simply running the following example from the docs?

import torch
from fast_transformers.builders import TransformerEncoderBuilder

# Build a transformer encoder
bert = TransformerEncoderBuilder.from_kwargs(
    n_layers=12,
    n_heads=12,
    query_dimensions=64,
    value_dimensions=64,
    feed_forward_dimensions=3072,
    attention_type="full", # change this to use another
                           # attention implementation
    activation="gelu"
).get()

y = bert(torch.rand(
    10,    # batch_size
    512,   # sequence length
    64*12  # features
))

Also, is there a copy-paste issue in the error message? Because it seems that the last trace overlaps with the exception message.

Finally, could you perhaps fetch the latest from the repository and just build it and report the files created? For instance using the following code.

git clone git@github.com:idiap/fast-transformers.git
cd fast-transformers
python setup.py build_ext --inplace
find fast_transformers/ | grep so$

Cheers,
Angelos

@ghost
Copy link
Author

ghost commented Jul 20, 2020

Thanks for the reply. I launched a fresh GC instance and installed this library via your method above. I should have mentioned this issue only happens when I use cluster-based attention, linear works no problem (and I haven't tried the others).

The example from the docs runs fine only when not running on CUDA. That is, this code kicks an error:

import torch
from fast_transformers.builders import TransformerEncoderBuilder

# Build a transformer encoder
bert = TransformerEncoderBuilder.from_kwargs(
    n_layers=12,
    n_heads=12,
    query_dimensions=64,
    value_dimensions=64,
    feed_forward_dimensions=3072,
    attention_type="improved-clustered", # change this to use another
                           # attention implementation
    activation="gelu"
).get().cuda()

y = bert(x=torch.rand(
    10,    # batch_size
    512,   # sequence length
    64*12  # features
).cuda())

Here is the error:

Traceback (most recent call last):
  File "cluster_example.py", line 19, in <module>
    64*12  # features
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/transformers.py", line 131, in forward
    x = layer(x, attn_mask=attn_mask, length_mask=length_mask)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/transformers.py", line 77, in forward
    key_lengths=length_mask
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/attention/attention_layer.py", line 98, in forward
    key_lengths
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/attention/improved_clustered_attention.py", line 196, in forward
    clusters, counts = self._create_query_groups(queries, query_lengths)
  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/attention/improved_clustered_attention.py", line 107, in _create_query_groups
    hashes = compute_hashes(Q.view(N*H*L, E), planes).view(N, H, L)
  File "/usr/local/lib/python3.6/dist-packages/fast_transformers/hashing/__init__.py", line 29, in compute_hashes
    compute_hashes_cuda(X, A, H)
NameError: name 'compute_hashes_cuda' is not defined

And I double checked the hashing folder, and it does indeed contain all files:

__init__.py  __pycache__  hash_cpu.cpp  hash_cpu.cpython-36m-x86_64-linux-gnu.so  hash_cuda.cu

There were also warnings while building the library (via python setup.py build_ext --inplace):

In file included from /usr/local/lib/python3.6/dist-packages/torch/include/ATen/Tensor.h:11:0,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/ATen/Context.h:4,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/ATen/ATen.h:5,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/types.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data/dataloader.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/all.h:4,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/extension.h:4,
                 from fast_transformers/hashing/hash_cpu.cpp:9:
/usr/local/lib/python3.6/dist-packages/torch/include/ATen/core/TensorBody.h:392:54: note: declared here
   GenericPackedTensorAccessor<T,N,PtrTraits,index_t> packed_accessor() const & {
                                                      ^~~~~~~~~~~~~~~
fast_transformers/hashing/hash_cpu.cpp:49:49: warning: ‘at::GenericPackedTensorAccessor<T, N, PtrTraits, index_t> at::Tensor::packed_accessor() const & [with T = long int; long unsigned int N = 1; PtrTraits = at::DefaultPtrTraits; index_t = long int]’ is deprecated: packed_accessor is deprecated, use packed_accessor32 or packed_accessor64 instead [-Wdeprecated-declarations]
     int64_t *h = &H.packed_accessor<int64_t, 1>()[0];
                                                 ^
In file included from /usr/local/lib/python3.6/dist-packages/torch/include/ATen/Tensor.h:11:0,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/ATen/Context.h:4,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/ATen/ATen.h:5,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/types.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data/dataloader.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/data.h:3,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include/torch/all.h:4,
                 from /usr/local/lib/python3.6/dist-packages/torch/include/torch/extension.h:4,
                 from fast_transformers/hashing/hash_cpu.cpp:9:
/usr/local/lib/python3.6/dist-packages/torch/include/ATen/core/TensorBody.h:392:54: note: declared here
   GenericPackedTensorAccessor<T,N,PtrTraits,index_t> packed_accessor() const & {

That is just one of the install warnings, they all looked like this though.

@angeloskath
Copy link
Collaborator

Ok, that is quite a different error than before. I can see that the CUDA extension is not built. This means that the machine which compiled the package lacks a CUDA compiler or a CUDA capable pytorch.

If you look into the setup.py code it checks if a CUDA compiler is available and only then compiles the CUDA extensions. Your error has nothing to do with the clustered attention it would fail similarly with causal-linear or any other implementation which requires custom kernels.

@ghost
Copy link
Author

ghost commented Jul 20, 2020

I am having trouble copying the full output upon building, here is the start:

/usr/lib/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'
  warnings.warn(msg)
running build_ext
/usr/local/lib/python3.6/dist-packages/torch/utils/cpp_extension.py:305: UserWarning: Attempted to use ninja as the BuildExtension backend but we could not f
ind ninja.. Falling back to using the slow distutils backend.
  warnings.warn(msg.format('we could not find ninja.'))
building 'fast_transformers.hashing.hash_cpu' extension
creating build
creating build/temp.linux-x86_64-3.6
creating build/temp.linux-x86_64-3.6/fast_transformers
creating build/temp.linux-x86_64-3.6/fast_transformers/hashing
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPI
C -I/usr/local/lib/python3.6/dist-packages/torch/include -I/usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include -I/usr/local/lib/pytho
n3.6/dist-packages/torch/include/TH -I/usr/local/lib/python3.6/dist-packages/torch/include/THC -I/usr/include/python3.6m -c fast_transformers/hashing/hash_cp
u.cpp -o build/temp.linux-x86_64-3.6/fast_transformers/hashing/hash_cpu.o -fopenmp -ffast-math -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=hash_cp
u -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++14
fast_transformers/hashing/hash_cpu.cpp: In function ‘void compute_hashes(at::Tensor, at::Tensor, at::Tensor)’:
fast_transformers/hashing/hash_cpu.cpp:17:45: warning: ‘at::GenericPackedTensorAccessor<T, N, PtrTraits, index_t> at::Tensor::packed_accessor() const & [with
 T = float; long unsigned int N = 2; PtrTraits = at::DefaultPtrTraits; index_t = long int]’ is deprecated: packed_accessor is deprecated, use packed_accessor
32 or packed_accessor64 instead [-Wdeprecated-declarations]
     float *x = &X.packed_accessor<float, 2>()[0][0];
                                             ^
In file included from /usr/local/lib/python3.6/dist-packages/torch/include/ATen/Tensor.h:11:0,

@angeloskath
Copy link
Collaborator

Don't worry about the warnings, they are simply deprecation warnings for the CPU code (they shouldn't be there but they are not causing any problem).

Could you run nvcc in a shell? It is the CUDA compiler. If it is not accessible the kernels cannot be compiled; hence the errors. In case it is not accessible I suspect you are missing the CUDA toolkit.

@ghost
Copy link
Author

ghost commented Jul 20, 2020

Yes, I am missing the CUDA toolkit. Which is odd. Sorry for the trouble. I'll try to figure this out...

Thanks again :)

@ghost ghost closed this as completed Jul 20, 2020
@ghost
Copy link
Author

ghost commented Jul 20, 2020

Final update: The issue is solved after adding CUDA to the PATH. I had installed CUDA fine, and never added it to the PATH. I had been running PyTorch and HuggingFace's transformers without issue - so I assumed my CUDA setup was not the problem. But this install requires CUDA to be on the PATH.

@ghost ghost changed the title No module named 'fast_transformers.causal_product.causal_product_cpu' No module named 'fast_transformers.causal_product.causal_product_cpu' (solved: needed to at CUDA to the PATH) Jul 20, 2020
@angeloskath
Copy link
Collaborator

No trouble at all 😄 .

I am glad that it was easy to solve, perhaps in the future we could also provide binaries however it is a lot more trouble to provide binaries for many architectures than build then binaries on the host machine.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant