Skip to content

Possible incorrect use of and with multiple context managers #238

Description

@mariosgly

Hi,

I noticed a small thing while looking through the training code and wanted to ask for your opinion. There are a few places where multiple context managers seem to be combined with and, for example:

with torch.cuda.amp.autocast() and torch.no_grad():

My understanding is that Python does not treat this as entering both context managers. Instead, it evaluates A() and B() first, so the with statement only receives one context manager, usually the second one if the first object is "true". So this may effectively behave like:

with torch.no_grad():

rather than:

with torch.cuda.amp.autocast(), torch.no_grad():

It does not seem like a huge issue, but it may mean that autocast() is not actually active in those blocks. In the opposite order, for example:

with torch.no_grad() and torch.cuda.amp.autocast():

it may mean that no_grad() is not active.

I may be missing something, so I wanted to check with you: do you think replacing these with comma-separated context managers would be the intended behavior?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions