Skip to content

Is the warning emitted by self.log-ing an integer intentional? #18739

@awaelchli

Description

@awaelchli

Bug description

When you call

self.log("integer", 1)

you get the warning:

/Users/adrian/repositories/lightning/src/lightning/pytorch/trainer/connectors/logger_connector/result.py:232: UserWarning: You called self.log('integer', ...) in your training_step but the value needs to be floating point. Converting it to torch.float32.

Is this intentional?

What version are you seeing the problem on?

v1.9, v2.0, master

How to reproduce the bug

import torch
from lightning.pytorch import LightningModule, Trainer
from torch.utils.data import DataLoader, Dataset


class RandomDataset(Dataset):
    def __init__(self, size, length):
        self.len = length
        self.data = torch.randn(length, size)

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return self.len


class BoringModel(LightningModule):
    def __init__(self):
        super().__init__()
        self.layer = torch.nn.Linear(32, 2)

    def training_step(self, batch, batch_idx):
        loss = self.layer(batch).sum()
        self.log("integer", 1)
        return {"loss": loss}

    def configure_optimizers(self):
        return torch.optim.SGD(self.layer.parameters(), lr=0.1)


train_data = DataLoader(RandomDataset(32, 64), batch_size=2)
model = BoringModel()
trainer = Trainer(max_steps=1)
trainer.fit(model, train_data)

Error messages and logs

/Users/adrian/repositories/lightning/src/lightning/pytorch/trainer/connectors/logger_connector/result.py:232: UserWarning: You called self.log('integer', ...) in your training_step but the value needs to be floating point. Converting it to torch.float32.

Environment

Current environment
#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow):
#- PyTorch Lightning Version (e.g., 1.5.0): 1.9+
#- Lightning App Version (e.g., 0.5.2): -
#- PyTorch Version (e.g., 2.0): 2.1
#- Python version (e.g., 3.9): 3.11
#- OS (e.g., Linux): MacOs
#- CUDA/cuDNN version: -
#- GPU models and configuration: -
#- How you installed Lightning(`conda`, `pip`, source): source
#- Running environment of LightningApp (e.g. local, cloud): -

More info

The question was raised here #18723 (comment), I'm not sure if there is a good reason for it.

cc @carmocca @Blaizzy @stas00

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions