diff --git a/benchmark.py b/benchmark.py index 56c75f6c1e..db8fe4308d 100755 --- a/benchmark.py +++ b/benchmark.py @@ -321,7 +321,7 @@ def _step(): f'Running inference benchmark on {self.model_name} for {self.num_bench_iter} steps w/ ' f'input size {self.input_size} and batch size {self.batch_size}.') - with torch.no_grad(): + with torch.inference_mode(): self._init_input() for _ in range(self.num_warm_iter): diff --git a/hfdocs/source/models/adversarial-inception-v3.mdx b/hfdocs/source/models/adversarial-inception-v3.mdx index 9768564226..b3980e4ff9 100644 --- a/hfdocs/source/models/adversarial-inception-v3.mdx +++ b/hfdocs/source/models/adversarial-inception-v3.mdx @@ -37,7 +37,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/advprop.mdx b/hfdocs/source/models/advprop.mdx index 9ac7c754b3..b109dd50cf 100644 --- a/hfdocs/source/models/advprop.mdx +++ b/hfdocs/source/models/advprop.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/big-transfer.mdx b/hfdocs/source/models/big-transfer.mdx index 50d6f96efe..fa287fdaf1 100644 --- a/hfdocs/source/models/big-transfer.mdx +++ b/hfdocs/source/models/big-transfer.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/csp-darknet.mdx b/hfdocs/source/models/csp-darknet.mdx index b924be7c28..f5aaac8c1a 100644 --- a/hfdocs/source/models/csp-darknet.mdx +++ b/hfdocs/source/models/csp-darknet.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/csp-resnet.mdx b/hfdocs/source/models/csp-resnet.mdx index 2c74275d0c..b74aaee06d 100644 --- a/hfdocs/source/models/csp-resnet.mdx +++ b/hfdocs/source/models/csp-resnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/csp-resnext.mdx b/hfdocs/source/models/csp-resnext.mdx index 57bb090ef8..ecefe512b9 100644 --- a/hfdocs/source/models/csp-resnext.mdx +++ b/hfdocs/source/models/csp-resnext.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/densenet.mdx b/hfdocs/source/models/densenet.mdx index 58393c2106..ff6d2853a5 100644 --- a/hfdocs/source/models/densenet.mdx +++ b/hfdocs/source/models/densenet.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/dla.mdx b/hfdocs/source/models/dla.mdx index 2cfcd1055c..8de9499f3f 100644 --- a/hfdocs/source/models/dla.mdx +++ b/hfdocs/source/models/dla.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/dpn.mdx b/hfdocs/source/models/dpn.mdx index 0c28c66f52..c3d2601ed8 100644 --- a/hfdocs/source/models/dpn.mdx +++ b/hfdocs/source/models/dpn.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/ecaresnet.mdx b/hfdocs/source/models/ecaresnet.mdx index d4aa372e0e..cee2aca7c9 100644 --- a/hfdocs/source/models/ecaresnet.mdx +++ b/hfdocs/source/models/ecaresnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/efficientnet-pruned.mdx b/hfdocs/source/models/efficientnet-pruned.mdx index d6bc760ee2..791f4bd3f9 100644 --- a/hfdocs/source/models/efficientnet-pruned.mdx +++ b/hfdocs/source/models/efficientnet-pruned.mdx @@ -39,7 +39,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/efficientnet.mdx b/hfdocs/source/models/efficientnet.mdx index bb86ab8303..489a5d356b 100644 --- a/hfdocs/source/models/efficientnet.mdx +++ b/hfdocs/source/models/efficientnet.mdx @@ -37,7 +37,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/ensemble-adversarial.mdx b/hfdocs/source/models/ensemble-adversarial.mdx index 19373ffe71..7d66abf326 100644 --- a/hfdocs/source/models/ensemble-adversarial.mdx +++ b/hfdocs/source/models/ensemble-adversarial.mdx @@ -37,7 +37,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/ese-vovnet.mdx b/hfdocs/source/models/ese-vovnet.mdx index 51a41144ff..73386708bd 100644 --- a/hfdocs/source/models/ese-vovnet.mdx +++ b/hfdocs/source/models/ese-vovnet.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/fbnet.mdx b/hfdocs/source/models/fbnet.mdx index b96eaed7c0..140ce6c472 100644 --- a/hfdocs/source/models/fbnet.mdx +++ b/hfdocs/source/models/fbnet.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/gloun-inception-v3.mdx b/hfdocs/source/models/gloun-inception-v3.mdx index b47533f434..87882d6d8f 100644 --- a/hfdocs/source/models/gloun-inception-v3.mdx +++ b/hfdocs/source/models/gloun-inception-v3.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/gloun-resnet.mdx b/hfdocs/source/models/gloun-resnet.mdx index 85e5ddbecf..50e9152f4f 100644 --- a/hfdocs/source/models/gloun-resnet.mdx +++ b/hfdocs/source/models/gloun-resnet.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/gloun-resnext.mdx b/hfdocs/source/models/gloun-resnext.mdx index 89f9d24bf9..0d5eb6ece7 100644 --- a/hfdocs/source/models/gloun-resnext.mdx +++ b/hfdocs/source/models/gloun-resnext.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/gloun-senet.mdx b/hfdocs/source/models/gloun-senet.mdx index f8df6d87f9..359c93d498 100644 --- a/hfdocs/source/models/gloun-senet.mdx +++ b/hfdocs/source/models/gloun-senet.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/gloun-seresnext.mdx b/hfdocs/source/models/gloun-seresnext.mdx index aedf70919e..4df3231a1d 100644 --- a/hfdocs/source/models/gloun-seresnext.mdx +++ b/hfdocs/source/models/gloun-seresnext.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/gloun-xception.mdx b/hfdocs/source/models/gloun-xception.mdx index e79b8d3109..61846db9c4 100644 --- a/hfdocs/source/models/gloun-xception.mdx +++ b/hfdocs/source/models/gloun-xception.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/hrnet.mdx b/hfdocs/source/models/hrnet.mdx index b3f453e422..43486761b2 100644 --- a/hfdocs/source/models/hrnet.mdx +++ b/hfdocs/source/models/hrnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/ig-resnext.mdx b/hfdocs/source/models/ig-resnext.mdx index 8fd5dbe808..974c034e63 100644 --- a/hfdocs/source/models/ig-resnext.mdx +++ b/hfdocs/source/models/ig-resnext.mdx @@ -37,7 +37,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/inception-resnet-v2.mdx b/hfdocs/source/models/inception-resnet-v2.mdx index c746cead19..5c4f81f0bd 100644 --- a/hfdocs/source/models/inception-resnet-v2.mdx +++ b/hfdocs/source/models/inception-resnet-v2.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/inception-v3.mdx b/hfdocs/source/models/inception-v3.mdx index 437aaea257..e480b5128b 100644 --- a/hfdocs/source/models/inception-v3.mdx +++ b/hfdocs/source/models/inception-v3.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/inception-v4.mdx b/hfdocs/source/models/inception-v4.mdx index 4072fbec15..c4233fd46c 100644 --- a/hfdocs/source/models/inception-v4.mdx +++ b/hfdocs/source/models/inception-v4.mdx @@ -32,7 +32,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/legacy-se-resnet.mdx b/hfdocs/source/models/legacy-se-resnet.mdx index d96c75d689..595fce07e7 100644 --- a/hfdocs/source/models/legacy-se-resnet.mdx +++ b/hfdocs/source/models/legacy-se-resnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/legacy-se-resnext.mdx b/hfdocs/source/models/legacy-se-resnext.mdx index eef0c977db..769ad5dd98 100644 --- a/hfdocs/source/models/legacy-se-resnext.mdx +++ b/hfdocs/source/models/legacy-se-resnext.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/legacy-senet.mdx b/hfdocs/source/models/legacy-senet.mdx index 750621ff09..1d2d5d017c 100644 --- a/hfdocs/source/models/legacy-senet.mdx +++ b/hfdocs/source/models/legacy-senet.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/mixnet.mdx b/hfdocs/source/models/mixnet.mdx index 2ef8885e6c..f402f8c6ea 100644 --- a/hfdocs/source/models/mixnet.mdx +++ b/hfdocs/source/models/mixnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/mnasnet.mdx b/hfdocs/source/models/mnasnet.mdx index 23f217206a..21818a70cb 100644 --- a/hfdocs/source/models/mnasnet.mdx +++ b/hfdocs/source/models/mnasnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/mobilenet-v2.mdx b/hfdocs/source/models/mobilenet-v2.mdx index 2ea949e932..2a0478cfe9 100644 --- a/hfdocs/source/models/mobilenet-v2.mdx +++ b/hfdocs/source/models/mobilenet-v2.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/mobilenet-v3.mdx b/hfdocs/source/models/mobilenet-v3.mdx index 35795ad0c9..36a56defe4 100644 --- a/hfdocs/source/models/mobilenet-v3.mdx +++ b/hfdocs/source/models/mobilenet-v3.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/nasnet.mdx b/hfdocs/source/models/nasnet.mdx index 2332131a57..511ba18416 100644 --- a/hfdocs/source/models/nasnet.mdx +++ b/hfdocs/source/models/nasnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/noisy-student.mdx b/hfdocs/source/models/noisy-student.mdx index 480d48dd20..0450730594 100644 --- a/hfdocs/source/models/noisy-student.mdx +++ b/hfdocs/source/models/noisy-student.mdx @@ -42,7 +42,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/pnasnet.mdx b/hfdocs/source/models/pnasnet.mdx index 58141a641f..7319436e26 100644 --- a/hfdocs/source/models/pnasnet.mdx +++ b/hfdocs/source/models/pnasnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/regnetx.mdx b/hfdocs/source/models/regnetx.mdx index 1e9548bd9e..a3f0c70f60 100644 --- a/hfdocs/source/models/regnetx.mdx +++ b/hfdocs/source/models/regnetx.mdx @@ -37,7 +37,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/regnety.mdx b/hfdocs/source/models/regnety.mdx index 04d869280b..7f89672e6c 100644 --- a/hfdocs/source/models/regnety.mdx +++ b/hfdocs/source/models/regnety.mdx @@ -39,7 +39,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/res2net.mdx b/hfdocs/source/models/res2net.mdx index bd5598d566..f788c89a61 100644 --- a/hfdocs/source/models/res2net.mdx +++ b/hfdocs/source/models/res2net.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/res2next.mdx b/hfdocs/source/models/res2next.mdx index 11ef5ece57..e51981cf1a 100644 --- a/hfdocs/source/models/res2next.mdx +++ b/hfdocs/source/models/res2next.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/resnest.mdx b/hfdocs/source/models/resnest.mdx index b6f73dcf68..6f7fad0195 100644 --- a/hfdocs/source/models/resnest.mdx +++ b/hfdocs/source/models/resnest.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/resnet-d.mdx b/hfdocs/source/models/resnet-d.mdx index 8eb7f0457c..c281a7b9f2 100644 --- a/hfdocs/source/models/resnet-d.mdx +++ b/hfdocs/source/models/resnet-d.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/resnet.mdx b/hfdocs/source/models/resnet.mdx index 5aeb50c5a4..33f41fa8e1 100644 --- a/hfdocs/source/models/resnet.mdx +++ b/hfdocs/source/models/resnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/resnext.mdx b/hfdocs/source/models/resnext.mdx index d89191ecc6..4037ce6e79 100644 --- a/hfdocs/source/models/resnext.mdx +++ b/hfdocs/source/models/resnext.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/rexnet.mdx b/hfdocs/source/models/rexnet.mdx index f805729a74..1a13c3c699 100644 --- a/hfdocs/source/models/rexnet.mdx +++ b/hfdocs/source/models/rexnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/se-resnet.mdx b/hfdocs/source/models/se-resnet.mdx index db1133cde5..66f2c380a4 100644 --- a/hfdocs/source/models/se-resnet.mdx +++ b/hfdocs/source/models/se-resnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/selecsls.mdx b/hfdocs/source/models/selecsls.mdx index aa43c8219f..fd59591dfb 100644 --- a/hfdocs/source/models/selecsls.mdx +++ b/hfdocs/source/models/selecsls.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/seresnext.mdx b/hfdocs/source/models/seresnext.mdx index 997d39629a..1759b6a5d2 100644 --- a/hfdocs/source/models/seresnext.mdx +++ b/hfdocs/source/models/seresnext.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/skresnet.mdx b/hfdocs/source/models/skresnet.mdx index 1229db5bdb..a91a11db96 100644 --- a/hfdocs/source/models/skresnet.mdx +++ b/hfdocs/source/models/skresnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/skresnext.mdx b/hfdocs/source/models/skresnext.mdx index 036f895a59..7070a0b5f9 100644 --- a/hfdocs/source/models/skresnext.mdx +++ b/hfdocs/source/models/skresnext.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/spnasnet.mdx b/hfdocs/source/models/spnasnet.mdx index d7adf93b3b..f3ba1362db 100644 --- a/hfdocs/source/models/spnasnet.mdx +++ b/hfdocs/source/models/spnasnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/ssl-resnet.mdx b/hfdocs/source/models/ssl-resnet.mdx index e781c576bb..b3c8e21ed0 100644 --- a/hfdocs/source/models/ssl-resnet.mdx +++ b/hfdocs/source/models/ssl-resnet.mdx @@ -37,7 +37,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/swsl-resnet.mdx b/hfdocs/source/models/swsl-resnet.mdx index 1e20024f20..9392d31c65 100644 --- a/hfdocs/source/models/swsl-resnet.mdx +++ b/hfdocs/source/models/swsl-resnet.mdx @@ -37,7 +37,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/swsl-resnext.mdx b/hfdocs/source/models/swsl-resnext.mdx index f551b87587..f84d7c6536 100644 --- a/hfdocs/source/models/swsl-resnext.mdx +++ b/hfdocs/source/models/swsl-resnext.mdx @@ -37,7 +37,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/tf-efficientnet-condconv.mdx b/hfdocs/source/models/tf-efficientnet-condconv.mdx index f41db0608b..e8665e09be 100644 --- a/hfdocs/source/models/tf-efficientnet-condconv.mdx +++ b/hfdocs/source/models/tf-efficientnet-condconv.mdx @@ -41,7 +41,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/tf-efficientnet-lite.mdx b/hfdocs/source/models/tf-efficientnet-lite.mdx index 75e093ac67..de5feea981 100644 --- a/hfdocs/source/models/tf-efficientnet-lite.mdx +++ b/hfdocs/source/models/tf-efficientnet-lite.mdx @@ -41,7 +41,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/tf-efficientnet.mdx b/hfdocs/source/models/tf-efficientnet.mdx index 890ccd2c6f..d7e44a1573 100644 --- a/hfdocs/source/models/tf-efficientnet.mdx +++ b/hfdocs/source/models/tf-efficientnet.mdx @@ -39,7 +39,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/tf-inception-v3.mdx b/hfdocs/source/models/tf-inception-v3.mdx index c907782117..182f929439 100644 --- a/hfdocs/source/models/tf-inception-v3.mdx +++ b/hfdocs/source/models/tf-inception-v3.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/tf-mixnet.mdx b/hfdocs/source/models/tf-mixnet.mdx index 68def0aabc..7c653174f7 100644 --- a/hfdocs/source/models/tf-mixnet.mdx +++ b/hfdocs/source/models/tf-mixnet.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/tf-mobilenet-v3.mdx b/hfdocs/source/models/tf-mobilenet-v3.mdx index 9460f53650..609ca3aeec 100644 --- a/hfdocs/source/models/tf-mobilenet-v3.mdx +++ b/hfdocs/source/models/tf-mobilenet-v3.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/tresnet.mdx b/hfdocs/source/models/tresnet.mdx index 8ff291057c..f5cb4b0d9c 100644 --- a/hfdocs/source/models/tresnet.mdx +++ b/hfdocs/source/models/tresnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/wide-resnet.mdx b/hfdocs/source/models/wide-resnet.mdx index e8837607bb..a8812a0b41 100644 --- a/hfdocs/source/models/wide-resnet.mdx +++ b/hfdocs/source/models/wide-resnet.mdx @@ -33,7 +33,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/hfdocs/source/models/xception.mdx b/hfdocs/source/models/xception.mdx index 79f97f4937..45f123d67b 100644 --- a/hfdocs/source/models/xception.mdx +++ b/hfdocs/source/models/xception.mdx @@ -35,7 +35,7 @@ To get the model predictions: ```py >>> import torch ->>> with torch.no_grad(): +>>> with torch.inference_mode(): ... out = model(tensor) >>> probabilities = torch.nn.functional.softmax(out[0], dim=0) >>> print(probabilities.shape) diff --git a/inference.py b/inference.py index 46232534d1..4645799c89 100755 --- a/inference.py +++ b/inference.py @@ -273,7 +273,7 @@ def main(): all_labels = [] all_outputs = [] use_probs = args.output_type == 'prob' - with torch.no_grad(): + with torch.inference_mode(): for batch_idx, (input, _) in enumerate(loader): with amp_autocast(): diff --git a/tests/test_models.py b/tests/test_models.py index a7e9ccf00d..028440179d 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -145,7 +145,7 @@ def test_model_inference(model_name, batch_size): owl_tensors = safetensors.torch.load_file(os.path.join(temp_dir, 'test', 'owl_tensors.safetensors')) test_owl = Image.open(os.path.join(temp_dir, 'test', 'test_owl.jpg')) - with torch.no_grad(): + with torch.inference_mode(): rand_output = model(rand_tensors['input']) rand_features = model.forward_features(rand_tensors['input']) rand_pre_logits = model.forward_head(rand_features, pre_logits=True) @@ -631,7 +631,7 @@ def test_model_forward_fx(model_name, batch_size): input_size = _get_input_size(model=model, target=TARGET_FWD_FX_SIZE) if max(input_size) > MAX_FWD_FX_SIZE: pytest.skip("Fixed input size model > limit.") - with torch.no_grad(): + with torch.inference_mode(): inputs = torch.randn((batch_size, *input_size)) outputs = model(inputs) if isinstance(outputs, tuple): @@ -711,7 +711,7 @@ def test_model_forward_fx_torchscript(model_name, batch_size): model.eval() model = torch.jit.script(_create_fx_model(model)) - with torch.no_grad(): + with torch.inference_mode(): outputs = tuple(model(torch.randn((batch_size, *input_size))).values()) if isinstance(outputs, tuple): outputs = torch.cat(outputs) @@ -742,7 +742,7 @@ def test_model_forward_torchscript_with_features_fx(model_name, batch_size): model.eval() model = torch.jit.script(model) - with torch.no_grad(): + with torch.inference_mode(): outputs = model(torch.randn((batch_size, *input_size))) assert isinstance(outputs, list) diff --git a/timm/utils/onnx.py b/timm/utils/onnx.py index 2ee920ff1e..22a357cb5a 100644 --- a/timm/utils/onnx.py +++ b/timm/utils/onnx.py @@ -54,7 +54,7 @@ def onnx_export( # Opset >= 11 should allow for dynamic padding, however I cannot get it to work due to # issues in the tracing of the dynamic padding or errors attempting to export the model after jit # scripting it (an approach that should work). Perhaps in a future PyTorch or ONNX versions... - with torch.no_grad(): + with torch.inference_mode(): original_out = model(example_input) input_names = input_names or ["input0"] diff --git a/train.py b/train.py index eaa2d213e9..ca48538771 100755 --- a/train.py +++ b/train.py @@ -1310,7 +1310,7 @@ def validate( end = time.time() last_idx = len(loader) - 1 - with torch.no_grad(): + with torch.inference_mode(): for batch_idx, (input, target) in enumerate(loader): last_batch = batch_idx == last_idx if not args.prefetcher: diff --git a/validate.py b/validate.py index 25781f18f5..afca5e561b 100755 --- a/validate.py +++ b/validate.py @@ -343,7 +343,7 @@ def validate(args): top5 = AverageMeter() model.eval() - with torch.no_grad(): + with torch.inference_mode(): # warmup, reduce variability of first batch time, especially for comparing torchscript vs non if not args.naflex_loader: input = torch.randn((args.batch_size,) + tuple(data_config['input_size'])).to(device=device, dtype=model_dtype)