Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix non-tensor scalar result aggregation #3540

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pytorch_lightning/core/step_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ def log(
sync_dist_op: Union[Any, str] = 'mean',
sync_dist_group: Optional[Any] = None,
):

# no metrics should be logged with graphs
if not enable_graph and isinstance(value, torch.Tensor):
value = value.detach()

if not isinstance(value, (torch.Tensor, numbers.Number)):
value = torch.tensor(value, device=self.device)

# sync across ddp
if sync_dist and isinstance(value, (torch.Tensor, numbers.Number)):
if sync_dist:
value = sync_ddp_if_available(value, group=sync_dist_group, reduce_op=sync_dist_op)

if 'meta' not in self:
Expand Down
3 changes: 3 additions & 0 deletions tests/base/model_train_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def training_step_result_obj(self, batch, batch_idx, optimizer_idx=None):

result = TrainResult(loss_val)
result.log('some_val', log_val * log_val, prog_bar=True, logger=False)
assert isinstance(result.some_val, torch.Tensor)
result.log('train_some_val', log_val * log_val)
assert isinstance(result.train_some_val, torch.Tensor)
result.log('epoch_some_val', log_val * log_val, on_step=False, on_epoch=True)
return result

def training_step__inf_loss(self, batch, batch_idx, optimizer_idx=None):
Expand Down
1 change: 1 addition & 0 deletions tests/core/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,4 @@ def test_result_retrieve_last_logged_item():
assert result['epoch_a'] == 5.
assert result['step_a'] == 5.
assert result['a'] == 5.
assert isinstance(result.a, torch.Tensor)