WZMIAOMIAO/deep-learning-for-image-processing · error · Exception
memory efficient not supported in JIT
Error message
memory efficient not supported in JIT
What it means
Inside _DenseLayer.forward, when self.memory_efficient is enabled and at least one input to the layer requires grad, the layer uses gradient checkpointing for the bottleneck. Checkpointing (torch.utils.checkpoint) is incompatible with TorchScript JIT, so the code explicitly raises when torch.jit.is_scripting() is true. This prevents silently producing wrong gradients under JIT compilation.
Source
Thrown at pytorch_classification/Test8_densenet/model.py:67
return False
@torch.jit.unused
def call_checkpoint_bottleneck(self, inputs: List[Tensor]) -> Tensor:
def closure(*inp):
return self.bn_function(inp)
return cp.checkpoint(closure, *inputs)
def forward(self, inputs: Tensor) -> Tensor:
if isinstance(inputs, Tensor):
prev_features = [inputs]
else:
prev_features = inputs
if self.memory_efficient and self.any_requires_grad(prev_features):
if torch.jit.is_scripting():
raise Exception("memory efficient not supported in JIT")
bottleneck_output = self.call_checkpoint_bottleneck(prev_features)
else:
bottleneck_output = self.bn_function(prev_features)
new_features = self.conv2(self.relu2(self.norm2(bottleneck_output)))
if self.drop_rate > 0:
new_features = F.dropout(new_features,
p=self.drop_rate,
training=self.training)
return new_features
class _DenseBlock(nn.ModuleDict):
_version = 2
def __init__(self,View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Set memory_efficient=False on the _DenseLayer (or construct the model without memory-efficient checkpointing) before calling torch.jit.script
- Script the model under torch.jit.is_scripting()-aware branches, or trace with torch.jit.trace instead of script if compatible
- Separate the training model (memory efficient) from the deployment model (plain bn_function path) and script only the latter
Example fix
// before model = densenet121(memory_efficient=True) scripted = torch.jit.script(model) # raises // after model = densenet121(memory_efficient=False) scripted = torch.jit.script(model)
Defensive patterns
Strategy: fallback
Validate before calling
if torch.jit.is_scripting() and model.memory_efficient:
model = rebuild_model_without_checkpointing(model) # set memory_efficient=False
scripted = torch.jit.script(model) Type guard
def is_scripting_with_checkpointing(model) -> bool:
return torch.jit.is_scripting() and getattr(model, 'memory_efficient', False) Try / catch
try:
scripted = torch.jit.script(model)
except Exception as e:
if 'memory efficient not supported' in str(e):
model.memory_efficient = False
scripted = torch.jit.script(model)
else:
raise Prevention
- Keep two model configurations: memory_efficient=True for eager training, False for scripted/exported models
- Check torch.jit.is_scripting() before enabling checkpointing paths
- Smoke-test torch.jit.script on your model in CI before deployment
When it happens
Trigger: Running torch.jit.script (or a model that gets scripted) on a DenseNet built with memory_efficient=True while some prev_features tensors require grad, so forward hits the checkpointing branch during scripting.
Common situations: Users copy the official DenseNet definition and enable memory_efficient=True for training, then export or compile the model with TorchScript for deployment/ONNX export; scripts often forget to disable memory efficient mode before scripting.
Related errors
- not support data format '{self.data_format}'
- image: {} isn't RGB mode.
- Transformer input dimension should be divisible by head dime
- image: {} isn't RGB mode.
- Embedding dim must be divisible by number of heads in {}. Go
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/16c9d98f61d41111.
Report an issue: GitHub.