Deactivate .cuda() output

Harpiye: <p>Due to the error that I had tensors on both the cpu and on cuda, I had to add <code>.cuda()</code> to my code (as suggested <a href="https://discuss.pytorch.org/t/which-device-is-model-tensor-stored-on/4908/3">here</a>), such that it contains the following line:</p>
<p><code>loss_fct = nn.CrossEntropyLoss(weight=torch.tensor([1.0, 100.0])).cuda() </code></p>
<p>This works but outputs the tensors which really interrupts my training output and makes it unreadable. How can I deactivate this output?</p>

ptrblck: <p>Could you describe the issue in more detail, please? I don’t fully understand which output is causing the issue.</p>

Harpiye: <p>The output looks like this:</p>
<p>0%|                                                                  | 0/12 [00:00&lt;?, ?it/s]<br>
17%|█████████▋                                                | 2/12 [00:20&lt;01:40, 10.02s/it]<br>
25%|██████████████▌                                           | 3/12 [00:40&lt;02:07, 14.20s/it]<br>
33%|███████████████████▎                                      | 4/12 [01:00&lt;02:11, 16.38s/it]<br>
42%|████████████████████████▏                                 | 5/12 [01:20&lt;02:03, 17.65s/it]<br>
50%|█████████████████████████████                             | 6/12 [01:40&lt;01:50, 18.45s/it]<br>
58%|█████████████████████████████████▊                        | 7/12 [02:00&lt;01:34, 18.96s/it]<br>
67%|██████████████████████████████████████▋                   | 8/12 [02:20&lt;01:17, 19.30s/it]<br>
75%|███████████████████████████████████████████▌              | 9/12 [02:40&lt;00:58, 19.54s/it]<br>
83%|███████████████████████████████████████████████▌         | 10/12 [03:00&lt;00:39, 19.69s/it]<br>
92%|████████████████████████████████████████████████████▎    | 11/12 [03:20&lt;00:19, 19.80s/it]<br>
100%|█████████████████████████████████████████████████████████| 12/12 [03:41&lt;00:00, 18.46s/it]</p>
<p>I’m running my models in screen session and when I later come back to it, this output blocks in parts the output that I actually need to see. (I tried to redirect my output to a file instead but that didn’t work either and is a bit awkward either way.)</p>

ptrblck: <p>This seems to be a <code>tqdm</code> issue rather than a PyTorch one or are you only seeing it when the loss function is moved to the GPU?<br>
If so, could you post a minimal and executable code snippet showing this behavior?</p>

Harpiye: <p>I use my own dataset but I guess this also shows the behavior although with this model and dataset combination it doesn’t output as many lines:</p>
<pre><code class="lang-auto">from transformers import DefaultDataCollator, AutoTokenizer, \
    AutoModelForSequenceClassification, TrainingArguments, Trainer
from datasets import load_dataset
import torch
from torch import nn

num_train_epochs = 3
# use GPU instead of CPU
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
tokenizer = AutoTokenizer.from_pretrained("ishan/distilbert-base-uncased-mnli")  # PyTorch model
dataset = load_dataset("rotten_tomatoes")
encoded_dataset = dataset.map(lambda examples: tokenizer(examples['text'], padding='max_length', truncation=True, max_length=8))
train_dataset = encoded_dataset["train"]
test_dataset = encoded_dataset["test"]


class CustomTrainer(Trainer):
    def __init__(self, model, train_dataset, eval_dataset, args):
        super().__init__(model=model, args=args, train_dataset=train_dataset, eval_dataset=eval_dataset)
    def compute_loss(self, some_model, inputs, return_outputs=False):
        labels = inputs.get("labels")
        outputs = some_model(**inputs)
        logits = outputs.get("logits")
        loss_fct = nn.CrossEntropyLoss(weight=torch.tensor([1.0, 10.0])).cuda()
        loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1))
        return (loss, outputs) if return_outputs else loss


model = AutoModelForSequenceClassification.from_pretrained("ishan/distilbert-base-uncased-mnli", num_labels=2,
                                                           ignore_mismatched_sizes=True)
model.to(device)
data_collator = DefaultDataCollator(return_tensors="pt")
training_args = TrainingArguments(
    output_dir="./output",
    num_train_epochs=num_train_epochs, 
    per_device_train_batch_size=16,  
    per_device_eval_batch_size=64,  
)
trainer = CustomTrainer(
    model=model,  
    args=training_args, 
    train_dataset=train_dataset,  
    eval_dataset=test_dataset,  
)
trainer.train()
</code></pre>
<p>I can’t say for sure if it only happens when moving the loss function to the GPU. I tried removing the lines I thought were responsible for moving everything to the GPU but apparently I didn’t caught every line as I got the error <code>RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument weight in method wrapper_nll_loss_forward)</code> and already had problems with that when introducing the loss function.<br>
`</p>

ptrblck: <p>Your code is unfortunately not a minimal code snippet as it uses the <code>transformers</code> library which is apparently using <code>tqdm</code> somewhere internally. If this issue is specific to HuggingFace you might want to create an issue in their GitHub repository or in their discussion board. If not and you can reproduce it in “plain PyTorch”, please post the code here and I’ll try to see what might be causing this output.</p>