Tensor is changing when appended to the list

maralm: <p>I have a weird issue when appending tensors to a list.</p>
<p>I have a code similar to this:</p>
<pre><code class="lang-auto">tensor_list = []
for i in range(24):
   current_tensor = function (...)
   tensor_list.append(current_tensor)
   #print (tensor_list)

print (tensor_list)
</code></pre>
<p>when I uncomment the print inside the loop, the values in the last print are correct. But when I comment it, I get wrong values in the final print. Any idea what could cause this?</p>

ptrblck: <p>Could you post the definition of <code>function</code> so that we can have a look?<br>
Are you seeing this issue using the JIT or plain PyTorch code?</p>

maralm: <p>It’s plain pytorch.</p>
<p>Here is the function class that is repeated in the loop and output of the function at each iteration will be the input to the next iteration call:</p>
<pre><code class="lang-auto">class BertEncoder(nn.Module):
    def __init__(self, config):
        super(BertEncoder, self).__init__()
        self.output_attentions = config.output_attentions
        self.output_hidden_states = config.output_hidden_states
        #self.layer = nn.ModuleList([BertLayer(config) for _ in range(config.num_hidden_layers)])
        self.layer = nn.ModuleList([BertLayer(config) for _ in range(1)])

    def forward(self, hidden_states, attention_mask, head_mask=None, is_last_layer=False,
            mask_attention_probs_dropout_prob=None,
            mask_hidden_dropout_prob=None,
            layer=None):

        if is_last_layer == True:
            self.output_hidden_states = True
        all_hidden_states = ()
        all_attentions = ()
        for i, layer_module in enumerate(self.layer):
            if self.output_hidden_states:
                all_hidden_states = all_hidden_states + (hidden_states,)

            layer_outputs = layer_module(hidden_states, attention_mask, head_mask[i],
                                        mask_attention_probs_dropout_prob=mask_attention_probs_dropout_prob, 
                                        mask_hidden_dropout_prob=mask_hidden_dropout_prob,
                                        layer=layer)
            hidden_states = layer_outputs[0]

        return layer_outputs
</code></pre>

ptrblck: <p>Could you post the missing definitions of the code or link to the repository?</p>

maralm: <p>This is the original script for the code:<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/huggingface/transformers/blob/master/src/transformers/modeling_bert.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/huggingface/transformers/blob/master/src/transformers/modeling_bert.py" target="_blank" rel="nofollow noopener">huggingface/transformers/blob/master/src/transformers/modeling_bert.py</a></h4>
<pre><code class="lang-py"># coding=utf-8
# Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
# Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""PyTorch BERT model. """


import logging
import math
</code></pre>

  This file has been truncated. <a href="https://github.com/huggingface/transformers/blob/master/src/transformers/modeling_bert.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>The change I have made is in “BertModel” class which I add a loop for the encoder function call (line 731).<br>
The issue is that when I print something in the loop, it changes the list values.</p>

maralm: <p>I think I solved that. I was using cuda_stream for a tensor transfer before the function call. When I removed it, the values are correct. Maybe a bug in cuda_stream?</p>

ptrblck: <p>I’m glad you solved the issue.<br>
If you are manually using CUDA streams, you would have to take care of the synchronizations as described <a href="https://pytorch.org/docs/stable/notes/cuda.html#cuda-streams">here</a>, which might explain this issue, if you haven’t done so.</p>