If Amp uses master params distinct from the model params,
Ampcallstheparamsowneddirectlybytheoptimizer's ``param_groups`` the "master params."
then the params ``step()``\ ed by the optimizer are the master params,
and it is the master gradients (rather than the model gradients) that must be clipped.
If Amp is not using master params distinct from the model params, then the optimizer
These master params may be fully or partially distinct from ``model.parameters()``.
directly steps the model params, and the model grads must be clipped.
For example, with `opt_level="O2"`_, ``amp.initialize`` casts most model params to FP16,
creates an FP32 master param outside the model for each newly-FP16 model param,
and updates the optimizer's``param_groups``topointtotheseFP32params.
In both cases, correct practice is to clip the gradients of the params that are about to be stepped **by the optimizer** (which may be distinct from ``model.parameters()``).
Themasterparamsownedbytheoptimizer's ``param_groups`` may also fully coincide with the
model params, which is typically true for ``opt_level``\s ``O0``, ``O1``, and ``O3``.
Also, if Amp uses loss scaling, gradients must be clipped after they have been unscaled.
In all cases, correct practice is to clip the gradients of the params that are guaranteed to be
owned **by the optimizer's**``param_groups``,insteadofthoseretrievedvia``model.parameters()``.
The following pattern accounts for all possibilities, and should be correct for
print("\nWarning: Cuda extensions are being compiled with a version of Cuda that does "
"not match the version used to compile Pytorch binaries.\n")
print("Pytorch binaries were compiled with Cuda {}\n".format(torch.version.cuda))
if"--cuda_ext"insys.argv:
if"--cuda_ext"insys.argv:
fromtorch.utils.cpp_extensionimportCUDAExtension
fromtorch.utils.cpp_extensionimportCUDAExtension
sys.argv.remove("--cuda_ext")
sys.argv.remove("--cuda_ext")
...
@@ -39,6 +59,8 @@ if "--cuda_ext" in sys.argv:
...
@@ -39,6 +59,8 @@ if "--cuda_ext" in sys.argv:
iftorch.utils.cpp_extension.CUDA_HOMEisNone:
iftorch.utils.cpp_extension.CUDA_HOMEisNone:
raiseRuntimeError("--cuda_ext was requested, but nvcc was not found. Are you sure your environment has nvcc available? If you're installing within a container from https://hub.docker.com/r/pytorch/pytorch, only images whose names contain 'devel' will provide nvcc.")
raiseRuntimeError("--cuda_ext was requested, but nvcc was not found. Are you sure your environment has nvcc available? If you're installing within a container from https://hub.docker.com/r/pytorch/pytorch, only images whose names contain 'devel' will provide nvcc.")