Thanks for the code snippet.
I cannot reproduce the issue using a source build with:
torch.__version__
'1.9.0a0+git6d45d7a'

torchvision.__version__
'0.11.0a0+882e11d'

and the optimization step doesn’t raise an error, so you might want to update to the latest nightly binary (or build from source). In case you are using an older stable release, you might also consider updating to the latest stable one (I haven’t checked it with 1.8.1).