# Getting unexpected NotImplementedError when running model

**URL:** <https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604>\
**Category:** Uncategorized\
**Created:** [January 25, 2020, 6:40am UTC](https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604 "2020-01-25T06:40:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![seankala](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/seankala/32/22144_2.png) [@seankala](https://discuss.pytorch.org/u/seankala)\
**Post date:** [January 25, 2020, 6:40am UTC](https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604/1 "2020-01-25T06:40:08Z")

</div>

Hello. I’m currently working on debugging a model I wrote using PyTorch.

When I first ran `output = model(data)` I got the error

```auto
*** RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.cuda.DoubleTensor instead (while checking arguments for embedding)

```

No problem, I simply ran `output = model(data.long())`. Then I got a:

```auto
*** RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _th_index_select

```

I wasn’t sure what the error message exactly meant, but I recalled that I hadn’t put my model on the GPU device. So I ran:

```auto
model = model.to('cuda')
output = model(data.long())

```

but this time I got a:

```auto
*** NotImplementedError

```

This was a bit unexpected, and I checked if I had put in a `NotImplementedError` anywhere in the code but I hadn’t. I’m having some difficulty finding helpful resources, as I don’t even know what the causing piece of code is.

Would anybody have any idea how I should handle this issue? Just an FYI, these errors occurred while I was in the PDB interactive shell.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![G.M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/g.m/32/19266_2.png) [@G.M](https://discuss.pytorch.org/u/G.M)\
**Post date:** [January 25, 2020, 6:56am UTC](https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604/2 "2020-01-25T06:56:04Z")

</div>

1. The `.to()` method needs the device code of the device, for gpus, the code is usually something like “cuda:0”. You can also use `.cuda()`.

2. Idk what device is `data` on, but calling `.long()` only changes it to Long type on its original device. If ur model is on gpu, u also have to change the inputs to gpu. In this case, I guess `data` is not on gpu.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [January 25, 2020, 7:01am UTC](https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604/3 "2020-01-25T07:01:26Z")

</div>

As an addition to what @G.M said, could you please print the complete stack trace you get from the `NotImplementedError`?  
You would get this error, e.g. if you have a typo in a method name (e.g. `forwrad` instead of `forward`) or if you use a wrong indentation.

---

<div class="post-metadata">

**Author:** ![seankala](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/seankala/32/22144_2.png) [@seankala](https://discuss.pytorch.org/u/seankala)\
**Post date:** [January 25, 2020, 7:39am UTC](https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604/4 "2020-01-25T07:39:57Z")

</div>

@G.M I’ve also put the `data` on GPU via `data.to('cuda')`. I’ve never had a problem from not specifying the device and just using `'cuda'`. Double checking right now `data` is a PyTorch tensor with `device='cuda:0'`.

---

<div class="post-metadata">

**Author:** ![G.M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/g.m/32/19266_2.png) [@G.M](https://discuss.pytorch.org/u/G.M)\
**Post date:** [January 25, 2020, 9:18am UTC](https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604/5 "2020-01-25T09:18:54Z")

</div>

Then could u please print the whole stack trace as @ptrblck said ?

---

<div class="post-metadata">

**Author:** ![seankala](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/seankala/32/22144_2.png) [@seankala](https://discuss.pytorch.org/u/seankala)\
**Post date:** [January 26, 2020, 7:55am UTC](https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604/6 "2020-01-26T07:55:40Z")

</div>

Here’s the traceback that I’ve been able to get:

```auto
Traceback (most recent call last):
  File "./main.py", line 27, in <module>
    main()
  File "./main.py", line 20, in main
    output = model(data.long())
  File "C:\Users\user1\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\user1\Desktop\github\transformer_implementation\transformer.py", line 25, in forward
    enc_output = self.encoder(x)
  File "C:\Users\user1\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\user1\Desktop\github\transformer_implementation\encoder.py", line 54, in forward
    output = self.encoder_stacked(x)
  File "C:\Users\user1\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\user1\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 97, in forward
    raise NotImplementedError
NotImplementedError

```

I didn’t initially include too many details here in the original post, but the `model` that I’m using has a `nn.ModuleList` inside of it. After going through parts of the code, I found out that this was the problem. More specifically, it looks like:

```auto
self.encoder_stacked = nn.ModuleList([Encoder(self.config) for _ in range(self.N)])

```

According to [this Stack Overflow question](https://stackoverflow.com/questions/52241680/pytorch-notimplementederror-in-forward) it seems that there are cases where the indentation may cause problems, but that’s not the case for me.

The `Encoder` module looks like this:

```auto
class Encoder(nn.Module):
    def __init__ (self, config, dropout=True):
        super(). __init__ ()
        self.config = config
        self.d_model = self.config.d_model

        self.attention = MultiheadAttention(self.config)

        if dropout:
            self.dropout = nn.Dropout(p=self.config.dropout_rate)
        else:
            self.dropout = nn.Dropout(p=0.0)

        self.norm = Normalization(features=self.d_model)
        self.ffnn = FeedforwardNN(self.config)

    def forward(self, x):
        attn_output = self.attention(x, x, x)
        sublayer1_output = self.norm(x + attn_output)
        sublayer1_output = self.dropout(sublayer1_output)

        ffnn_output = self.ffnn(sublayer1_output)
        sublayer2_output = self.norm(sublayer1_output + ffnn_output)
        sublayer2_output = self.dropout(sublayer2_output)

        return sublayer2_output

```

---

<div class="post-metadata">

**Author:** ![seankala](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/seankala/32/22144_2.png) [@seankala](https://discuss.pytorch.org/u/seankala)\
**Post date:** [January 26, 2020, 8:43am UTC](https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604/7 "2020-01-26T08:43:06Z")

</div>

I think I may have found a solution [here](https://discuss.pytorch.org/t/when-should-i-use-nn-modulelist-and-when-should-i-use-nn-sequential/5463/4). I’ll do some more research based off of this answer.

---

<div class="post-metadata">

**Author:** ![G.M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/g.m/32/19266_2.png) [@G.M](https://discuss.pytorch.org/u/G.M)\
**Post date:** [January 26, 2020, 10:36am UTC](https://discuss.pytorch.org/t/getting-unexpected-notimplementederror-when-running-model/67604/8 "2020-01-26T10:36:41Z")

</div>

Yes, ModuleList does not have a `forward` method. So u definitely can’t call on any ModuleList object as u did.
