Skip to content
Merged
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
18 changes: 10 additions & 8 deletions tests/ignite/metrics/test_accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,22 +488,22 @@ def _test_distrib_accumulator_device(device):
def _test_distrib_integration_list_of_tensors_or_numbers(device):

rank = idist.get_rank()
torch.manual_seed(12)

def _test(n_epochs, metric_device):
metric_device = torch.device(metric_device)
n_iters = 80
s = 16
batch_size = 16
n_classes = 10

offset = n_iters * s
y_true = torch.randint(0, n_classes, size=(offset * idist.get_world_size(),)).to(device)
y_preds = torch.rand(offset * idist.get_world_size(), n_classes).to(device)
torch.manual_seed(12 + rank)

y_true = torch.randint(0, n_classes, size=(n_iters * batch_size,)).to(device)
y_preds = torch.rand(n_iters * batch_size, n_classes).to(device)

def update(_, i):
return (
[v for v in y_preds[i * s + rank * offset : (i + 1) * s + rank * offset, :]],
[v.item() for v in y_true[i * s + rank * offset : (i + 1) * s + rank * offset]],
[v for v in y_preds[i * batch_size : (i + 1) * batch_size, ...]],
[v.item() for v in y_true[i * batch_size : (i + 1) * batch_size]],
)

engine = Engine(update)
Expand All @@ -514,6 +514,9 @@ def update(_, i):
data = list(range(n_iters))
engine.run(data=data, max_epochs=n_epochs)

y_true = idist.all_gather(y_true)
y_preds = idist.all_gather(y_preds)

assert (
acc._num_correct.device == metric_device
), f"{type(acc._num_correct.device)}:{acc._num_correct.device} vs {type(metric_device)}:{metric_device}"
Expand All @@ -524,7 +527,6 @@ def update(_, i):
res = res.cpu().numpy()

true_res = accuracy_score(y_true.cpu().numpy(), torch.argmax(y_preds, dim=1).cpu().numpy())

assert pytest.approx(res) == true_res

metric_devices = ["cpu"]
Expand Down