Skip to content

Fix quantization error on Reference Scripts #4722

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

Closed
wants to merge 1 commit into from

Conversation

datumbox
Copy link
Contributor

@datumbox datumbox commented Oct 22, 2021

Running the quantization script after #4609 leads to:

Traceback (most recent call last):
  File "./vision/references/classification/train_quantization.py", line 257, in <module>
    main(args)
  File "./vision/references/classification/train_quantization.py", line 103, in main
    evaluate(model, criterion, data_loader_test, device=device)
  File "./vision/references/classification/train.py", line 92, in evaluate
    metric_logger.synchronize_between_processes()
  File "./vision/references/classification/utils.py", line 95, in synchronize_between_processes
    meter.synchronize_between_processes()
  File "./vision/references/classification/utils.py", line 36, in synchronize_between_processes
    t = t.tolist()
AttributeError: 'list' object has no attribute 'tolist'

This is because the quantization script doesn't run in distributed mode so the reduce_across_processes() returns a list not a tensor.

@datumbox datumbox requested a review from NicolasHug October 22, 2021 11:53
@datumbox datumbox changed the title Fix quantization error Fix quantization error on Reference Scripts Oct 22, 2021
@@ -407,4 +406,4 @@ def reduce_across_processes(val):
t = torch.tensor(val, device="cuda")
dist.barrier()
dist.all_reduce(t)
return t
return t.tolist()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work if we kept return t here?

The problem with using t.tolist() here is that we would need to change https://github.com/pytorch/vision/blob/main/references/classification/train.py#L78 from

num_processed_samples = utils.reduce_across_processes(num_processed_samples)

to

num_processed_samples = utils.reduce_across_processes(num_processed_samples)[0]

because otherwise the code after that wouldn't work as expected: comparing an int to a tensor of length 1 works, but we can't compare an int with a list of length 1 in the same way.

But then if we used num_processed_samples = utils.reduce_across_processes(num_processed_samples)[0], we would have a similar problem in the non-distributed setting: we can't index an integer.

I feel like just removing the tolist() call is actually enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding was that val was supposed to be a list (because of line 32). The issue is that when not in a distributed setting, the return of line 405 will cause the subsequent tolist call to fail.

If val can also be an integer, I think that's an issue. Perhaps specifying the typing info of val can make things clearer. Or alternatively the val parameter should be renamed and be of a single type (for example list).

I'm going to close the PR and let you choose the solution you would like for this bug. Let me know when you have it to help you with the reviw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants