Repository: OpenNMT/OpenNMT-py

Issue:
ImportError for get_rank function
While running the **preprocess.py** I am getting an ImportError of get_rank function from Pytorch. What I have found on the internet that it is now deprecated and is inside the deprecated module. I can send a pull request for a fix of that?


Discussion:
Please post:
your python, pytorch version.
are you on master here ?
post your command line as well.
Hi, I am getting the same error. Using Pytorch version 1.0.0 and python version 3.6. 
@vince62s 
Python: 3.6.4
Pytorch: 1.0
I am on master.
This is the screenshot of my command line.
![screenshot 2018-12-24 at 1 03 11 pm](https://user-images.githubusercontent.com/25135893/50393503-a8bfec00-077c-11e9-97d9-91ea059e8a38.png)

can you post
pip list | grep torch

@vince62s Here is my screenshot
![screenshot 2018-12-24 at 4 54 00 pm](https://user-images.githubusercontent.com/25135893/50398485-f436c200-079d-11e9-804b-311270acddfe.png)

I would uninstall and reinstall it and check in the distribution if "distributed" is properly installed.
it should work.
 I solve this problem, because get_rank function in pytorch 1.0 is deprecated.

i just change the requirements.txt `torch<=0.9` and run `pip3 install -r requirements.txt` again.

another solution is change the code   ` from torch.distributed import get_rank` to ` from torch.distributed.deprecated import get_rank` and  then  we can use pytorch 1.0.
I don’t think this is right.
There is a new get_rank search it in the doc of Pytorch
Something must be wrong with your install
@vince62s yea, I can see the get_rank in pytorch 1.0's source ,and still get this error.

I'm pretty sure i install a pytorch 1.0 

I just use ipython3 to import get_rank with pytorch 1.0 , OS : macOS .10.14 brew python3 pip3 installed. I think maybe it's a bug in this whl package. 
2 ideas:
try to uninstall and remove manually all the torch related dist-package things and reinstall it.
if not working
make your point in the Pytorch forum with a minimal example showing the import get_rank not working.
Good luck
cheers.
I can confirm this with the `pip install`d version: 

```
$ pip freeze
atomicwrites==1.2.1
attrs==18.2.0
certifi==2018.11.29
chardet==3.0.4
ConfigArgParse==0.14.0
future==0.17.1
idna==2.8
more-itertools==5.0.0
numpy==1.16.0
pluggy==0.8.1
py==1.7.0
pytest==4.1.1
requests==2.21.0
six==1.12.0
torch==1.0.0
torchtext==0.4.0
tqdm==4.29.1
urllib3==1.24.1
(opennmt)
~/Code/OpenNMT-py/onmt on  master ⌚ 7:58:03
$ python3 -c "from torch.distributed import get_rank"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'get_rank' from 'torch.distributed' (/Users/erippeth/anaconda3/envs/tester3/lib/python3.7/site-packages/torch/distributed/__init__.py)
```

It also doesn't seem to work when `conda install`ing:

```
~/Code/OpenNMT-py/onmt on  master ⌚ 8:01:23
$ conda activate tester3
(tester3)
~/Code/OpenNMT-py/onmt on  master ⌚ 8:01:27
$ conda install -c pytorch pytorch
Solving environment: done

## Package Plan ##

  environment location: /Users/erippeth/anaconda3/envs/tester3

  added / updated specs:
    - pytorch


The following NEW packages will be INSTALLED:

    blas:            1.0-mkl
    ca-certificates: 2018.12.5-0
    certifi:         2018.11.29-py37_0
    cffi:            1.11.5-py37h6174b99_1
    intel-openmp:    2019.1-144
    libcxx:          4.0.1-hcfea43d_1
    libcxxabi:       4.0.1-hcfea43d_1
    libedit:         3.1.20181209-hb402a30_0
    libffi:          3.2.1-h475c297_4
    libgfortran:     3.0.1-h93005f0_2
    mkl:             2019.1-144
    mkl_fft:         1.0.10-py37h5e564d8_0
    mkl_random:      1.0.2-py37h27c97d8_0
    ncurses:         6.1-h0a44026_1
    ninja:           1.8.2-py37h04f5b5a_1
    numpy:           1.15.4-py37hacdab7b_0
    numpy-base:      1.15.4-py37h6575580_0
    openssl:         1.1.1a-h1de35cc_0
    pip:             18.1-py37_0
    pycparser:       2.19-py37_0
    python:          3.7.2-haf84260_0
    pytorch:         1.0.0-py3.7_1           pytorch
    readline:        7.0-h1de35cc_5
    setuptools:      40.6.3-py37_0
    sqlite:          3.26.0-ha441bb4_0
    tk:              8.6.8-ha441bb4_0
    wheel:           0.32.3-py37_0
    xz:              5.2.4-h1de35cc_4
    zlib:            1.2.11-h1de35cc_3

Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(tester3)
~/Code/OpenNMT-py/onmt on  master ⌚ 8:01:47
$ python3 -c "from torch.distributed import get_rank"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'get_rank' from 'torch.distributed' (/Users/erippeth/anaconda3/envs/tester3/lib/python3.7/site-packages/torch/distributed/__init__.py)
````
well it's torch.distributed
not distribution
@vince62s Updated -- sorry. :-) I ran into the same error and tried to minify. I fixed the above comment.

It's really not there, though:

```
$ cat /Users/erippeth/anaconda3/envs/tester3/lib/python3.7/site-packages/torch/distributed/__init__.py
import torch


def is_available():
    return hasattr(torch._C, "_c10d_init")


if is_available() and not torch._C._c10d_init():
    raise RuntimeError("Failed to initialize PyTorch distributed support")


if is_available():
    from .distributed_c10d import *
    # Variables prefixed with underscore are not auto imported
    # See the comment in `distributed_c10d.py` above `_backend` on why we expose
    # this.
    from .distributed_c10d import _backend
```

But it is in the `.distributed_c10d` and in `deprecated` per @sahiljajodia01's comments.
so you need to understand why the is_available returns false.
Presumably this is an issue with a missing CUDA toolkit. It would be nice if it were documented that CUDA 10.x is required to even run tests. 
can you try this:
here https://github.com/OpenNMT/OpenNMT-py/blob/e084cf3ff717c1edea080f1046eaeb97cb5391bb/onmt/utils/statistics.py#L7-L8
these two lines, move them in line 57 in the def function where they are used.
let me know if it fixes the issue
I can confirm that after moving those imports and issuing a `pip install pyrouge` allows tests to run (at least via `pytest`).
do you have time to send a PR? that would be great, thanks.
I'm happy to. ~It looks like `pyrouge` is only required for tests. Should I include it in the `requirements.txt`? I can create a separate `requirements.test.txt` or similar.~

Resolving patch:
diff --git a/onmt/utils/statistics.py b/onmt/utils/statistics.py
--- a/onmt/utils/statistics.py
+++ b/onmt/utils/statistics.py
@@ -4,8 +4,6 @@
 import math
 import sys
 
-from torch.distributed import get_rank
-from onmt.utils.distributed import all_gather_list
 from onmt.utils.logging import logger
 
 
@@ -55,6 +53,9 @@ def all_gather_stats_list(stat_list, max_size=4096):
         Returns:
             our_stats(list([`Statistics`])): list of updated stats
         """
+        from torch.distributed import get_rank
+        from onmt.utils.distributed import all_gather_list
+
         # Get a list of world_size lists with len(stat_list) Statistics objects
         all_stats = all_gather_list(stat_list, max_size=max_size)
 


Test patch: