Unable to import graphormer from transformers
Author: techthiyanes
Pull request: no

### System Info

transformers : 4.29.1
Python : 3.10

### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

from transformers.models.graphormer.collating_graphormer import preprocess_item, GraphormerDataCollator

In the above import command the system is unable to import algos_graphormer  from cython (pyx) file.

The below error message is popping up.
ImportError: cannot import name 'algos_graphormer' from 'transformers.models.graphormer' (/usr/local/lib/python3.10/dist-packages/transformers/models/graphormer/__init__.py)


### Expected behavior

It needs to be imported without any errors.

Comments:
By importing cython file manually, fixed the issue. Thanks a lot.

> 

i got the same queestion,but only

 ```
from transformers.models.graphormer.collating_graphormer import preprocess_item, GraphormerDataCollator
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/miniconda3/envs/g/lib/python3.8/site-packages/transformers/models/graphormer/collating_graphormer.py", line 16, in <module>
    from . import algos_graphormer  # noqa E402
ImportError: cannot import name 'algos_graphormer' from 'transformers.models.graphormer' (/root/miniconda3/envs/g/lib/python3.8/site-packages/transformers/models/graphormer/__init__.py)
```
could u please tell me about the details of your solution. where is the cython file? 

Kindly copy the below cython file inside your transformer installed whl folder.(Under src/transformers/models/graphormer)

https://github.com/huggingface/transformers/blob/main/src/transformers/models/graphormer/algos_graphormer.pyx


> Kindly copy the below cython file inside your transformer installed whl folder.(Under src/transformers/models/graphormer)
> 
> https://github.com/huggingface/transformers/blob/main/src/transformers/models/graphormer/algos_graphormer.pyx

thanks for you replay.

i found this file in my 

`/miniconda3/envs/payldet/lib/python3.9/site-packages/transformers/models/graphormer` folder:

```
/miniconda3/envs/payldet/lib/python3.9/site-packages/transformers/models/graphormer# ls
__init__.py  __pycache__  algos_graphormer.c  algos_graphormer.pyx  collating_graphormer.py  configuration_graphormer.py  modeling_graphormer.py
```

do i need replace this file?

i try to compile this pyx file manually,but meet fatal error now:

`fatal error: numpy/arrayobject.h: No such file or directory` 



> i

I would request you to stage this .pyx file inside graphormer folder. ( The place where transformer gets installed) - Most likely under /usr/local/python<version>/.....

> 
Thanks for your reply.

 I installed Transformer in a Conda environment, so I don't have the path you replied. However, I finally resolved this question in another way. 

By compiling this Pyx file manually, I got a .so file. To import it manually, I changed `transformers/models/graphormer/configuration_graphormer.py` file and added the specific path.

```
# line 15
# before:
if is_cython_available():
    import pyximport

    pyximport.install(setup_args={"include_dirs": np.get_include()})
    from . import algos_graphormer  # noqa E402

# after:
if is_cython_available():
    import pyximport

    pyximport.install(setup_args={"include_dirs": np.get_include()})
    import sys
    sys.path.append('/path/to/.so file')
    import algos_graphormer

```

Successfully ran model.py. But I'm not sure this way will not have a bad influence in the future.

```
# just test
from datasets import load_dataset 
from datasets import load_metric
import evaluate
import cython
from transformers.models.graphormer.collating_graphormer import preprocess_item, GraphormerDataCollator

dataset = load_dataset("OGB/ogbg-molhiv")
metric = evaluate.load("accuracy")
# print(dataset["train"].features)

dataset_processed = dataset.map(preprocess_item, batched=False)
# split up training into training + validation
train_ds = dataset_processed['train']
val_ds = dataset_processed['validation']

print(train_ds[0].keys())
```
 and the result is :

```
python model.py 
Found cached dataset json (/root/.cache/huggingface/datasets/OGB___json/OGB--ogbg-molhiv-8591baabc5d95f2f/0.0.0/e347ab1c932092252e717ff3f949105a4dd28b27e842dd53157d2f72e276c2e4)
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00, 557.48it/s]
dict_keys(['edge_index', 'edge_attr', 'y', 'num_nodes', 'node_feat', 'input_nodes', 'attn_bias', 'attn_edge_type', 'spatial_pos', 'in_degree', 'out_degree', 'input_edges', 'labels'])
```

> mport sys




If you are using colab, Kindly stage your cython file at /usr/local/lib/python3.10/dist-packages/transformers/models/graphormer/algos_graphormer.pyx
Thanks for sharing the solution. After completing the training, would you able to write prediction code? ( without the influence of trainer.predict()).

By compiling this Pyx file manually, --> Did you create this file manually executing? May I know how you created this file? I just exported this pyx file and just used same as it's.

> Thanks for sharing the solution. After completing the training, would you able to write prediction code? ( without the influence of trainer.predict()).

Hi,

I do not think I should put this pyx file in the `/usr/local/lib/python3.10/dist-packages/transformers/models/graphormer/` folder because I am running this code in a Conda environment, not on Colab.

Regarding the prediction, I have to tell you that I am a novice in Graphormer (I downloaded it yesterday actually) and have not written any code about it. However, my main task is to get graph embeddings, so I'll try to finish it. Maybe we can do it together and learn Graphormer together.

How do I compile it? This is the code,named setup.py:

```
from setuptools import Extension, setup
import numpy

ext_modules = [
    Extension(
        name='example',
        sources=['example.pyx'],
        include_dirs=[numpy.get_include()]
    )
]

setup(
    name='example',
    ext_modules=ext_modules,
)
```

after execute `python setup.py build_ext --inplace` ,you will get the .so file.

> > Thanks for sharing the solution. After completing the training, would you able to write prediction code? ( without the influence of trainer.predict()).
> 
> Hi,
> 
> I do not think I should put this pyx file in the `/usr/local/lib/python3.10/dist-packages/transformers/models/graphormer/` folder because I am running this code in a Conda environment, not on Colab.
> 
> Regarding the prediction, I have to tell you that I am a novice in Graphormer (I downloaded it yesterday actually) and have not written any code about it. However, my main task is to get graph embeddings, so I'll try to finish it. Maybe we can do it together and learn Graphormer together.
> 
> How do I compile it? This is the code,named setup.py:
> 
> ```
> from setuptools import Extension, setup
> import numpy
> 
> ext_modules = [
>     Extension(
>         name='example',
>         sources=['example.pyx'],
>         include_dirs=[numpy.get_include()]
>     )
> ]
> 
> setup(
>     name='example',
>     ext_modules=ext_modules,
> )
> ```
> 
> after execute `python setup.py build_ext --inplace` ,you will get the .so file.



Please check this one for prediction code : https://github.com/huggingface/transformers/issues/23642

@techthiyanes  Hi,Thank you for sharing. I'm going to check it out now.