Repository: pykeen/pykeen
Issue: Error loading a model
Root cause: Environment Incompatibility

**Describe the bug**
For some reason, I cannot load a model. When I try other models I trained on a different dataset (FB15k-237), it loads fine. But when I load a dataset trained on YAGO, I get an error. The error is coming from pytorch, but it says module not found pykeen.nn.

**To Reproduce**
Steps to reproduce the behavior:
1. Train any model on the YAGO3-10 dataset
2. Try to load the model to make predictions
3. At the model loading line `torch.load(model_location)`, an error occurs: 
  - if python 3.6: `ModuleNotFoundError: No module named 'pykeen.nn'`
  - if python 3.7: `ModuleNotFoundError: No module named 'pykeen.nn.norm'`

**Expected behavior**
The model loads.

**Environment:**

- For python 3.7 :  
| Key             | Value                    |
    |-----------------|--------------------------|
    | OS              | posix                    |
    | Platform        | Linux                    |
    | Release         | 3.10.0-862.el7.x86_64    |
    | User            | acb11732rl               |
    | Time            | Fri Jun 18 03:35:10 2021 |
    | Python          | 3.7.10                   |
    | PyKEEN          | 1.5.0                    |
    | PyKEEN Hash     | 12c4dfd3                 |
    | PyKEEN Branch   |                          |
    | PyTorch         | 1.9.0+cu102              |
    | CUDA Available? | true                     |
    | CUDA Version    | 10.2                     |
    | cuDNN Version   | 7605                     |


**Additional information**
The full error here: 
```
----> 1 model = torch.load("trained_model.pkl")

/lib/python3.7/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
    605                     opened_file.seek(orig_position)
    606                     return torch.jit.load(opened_file)
--> 607                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
    608         return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
    609 

/lib/python3.7/site-packages/torch/serialization.py in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
    880     unpickler = UnpicklerWrapper(data_file, **pickle_load_args)
    881     unpickler.persistent_load = persistent_load
--> 882     result = unpickler.load()
    883 
    884     torch._utils._validate_loaded_sparse_tensors()

/lib/python3.7/site-packages/torch/serialization.py in find_class(self, mod_name, name)
    873         def find_class(self, mod_name, name):
    874             mod_name = load_module_mapping.get(mod_name, mod_name)
--> 875             return super().find_class(mod_name, name)

```


Can you show how you saved the model?

The error about not working on py36 is because we haven't supported py36 in several releases. I'm training YAGO-310 myself now to see if I can reproduce your error on more recent version of python

Here's my environment (on 1.5.0 with a few extra irrelevant commits)

| Key             | Value                    |
|-----------------|--------------------------|
| OS              | posix                    |
| Platform        | Darwin                   |
| Release         | 20.5.0                   |
| User            | cthoyt                   |
| Time            | Thu Jun 17 16:10:26 2021 |
| Python          | 3.9.5                    |
| PyKEEN          | 1.5.1-dev                |
| PyKEEN Hash     | 28d5145a                 |
| PyKEEN Branch   | easy-epochs              |
| PyTorch         | 1.9.0                    |
| CUDA Available? | false                    |
| CUDA Version    | N/A                      |
| cuDNN Version   | N/A                      |

I'm able to run the following code with no problem:

```python
import pykeen
from pykeen.pipeline import pipeline
import pystow

d = pystow.join('pykeen', 'tests', 'yago')

r = pipeline(
    dataset='YAGO-310',
    model='PairRE',
    model_kwargs=dict(embedding_dim=2), 
    training_kwargs=dict(num_epochs=1)
)
r.save_to_directory(d)

import torch
new_model = torch.load(d.joinpath('trained_model.pkl'))
new_model
>>> PairRE(
  (loss): MarginRankingLoss(
    (margin_activation): ReLU()
  )
  (interaction): PairREInteraction()
  (entity_representations): ModuleList(
    (0): Embedding(
      (_embeddings): Embedding(123143, 2)
    )
  )
  (relation_representations): ModuleList(
    (0): Embedding(
      (_embeddings): Embedding(37, 2)
    )
    (1): Embedding(
      (_embeddings): Embedding(37, 2)
    )
  )
  (weight_regularizers): ModuleList()
)
```

@PhaelIshall interestingly, your environment points to a commit 12c4dfd3 that does not exist. How have you managed to get this?

@cthoyt I saved the model the same way you did, with `save_to_directory`

As for the pykeen version, I just made a python3.7 virtualenv then I ran `pip install pykeen`. I tried uninstalling and installing, it always gives me this commit. 


Also, if I try installing from the source, I get this message 

        cloning https://github.com/pykeen/pykeen.git to /tmp/7379400.1.gpu/pip-req-build-j343398z
         Running command git clone -q https://github.com/pykeen/pykeen.git /tmp/7379400.1.gpu/pip-req-build-j343398z` 

Then this version


| Key                  | Value                 |
|----------------------|-----------------------|
| `os.name`            | posix                 |
| `platform.system()`  | Linux                 |
| `platform.release()` | 3.10.0-862.el7.x86_64 |
| python               | 3.7.10                |
| pykeen               | 1.3.1-dev-UNHASHED    |
| torch                | 1.7.1                 |
| cuda available       | true                  |
| cuda                 | 10.2                  |
| cudnn                | 7605                  |

and then if I try importing pykeen, it is not recognized either.  

I'm not sure it makes sense that if you do `pip install pykeen` that you get commands related to cloning from github - PyPI has no direct connection to anything in source control. I'm even further confused that you're getting version `1.3.1-dev` - this makes me thing that you've got a funny situation where you've checked out an old version of the code from version control and that's getting picked up instead of using PyPI. Is it the case that you're doing these operations where the current working directory contains a folder called `pykeen`?

No, I only cloned the repository when I first started using pykeen, months ago. I never pulled any changes, and it's not in the directory I am working in. Is there a way to make sure I have everything deleted so I can install the new version? 

You can try something like `pip freeze | grep pykeen` to see where the pykeen installation in your current environment is. Since I've installed in development mode, it looks like this:

```
(pykeen) cthoyt@galvac ~/d/y0> pip freeze | grep pykeen
-e git+https://github.com/pykeen/pykeen.git@cf169906800e1f99274bc290d8d3b266d3659dd3#egg=pykeen
```

If you're installed from PyPI, it will look like this:

```
(tempenv-1f19266001265) cthoyt@galvac ~/d/y0> pip freeze | grep pykeen
pykeen==1.5.0
```

If you want to stick with a development installation, go into your pykeen folder and run `git pull` to get the latest code. You might still have to do `pip uninstall pykeen` before `pip install -e .` in case some entrypoints have changed. Otherwise, I'd suggest deleting the pykeen source code folder to make sure that's not causing any of the issues

Hi @PhaelIshall, did you get a chance to look into this? I hope it all worked out but if not, let me know if there's something else I can do

closing because no response

If the root cause was differing PyKEEN versions, https://github.com/pykeen/pykeen/pull/963 might also solve the issue.