Python torch.utils.checkpoint.checkpoint() Examples
The following are 30
code examples of torch.utils.checkpoint.checkpoint().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.utils.checkpoint
, or try the search function
.
Example #1
Source File: hrfpn.py From mmdetection with Apache License 2.0 | 7 votes |
def forward(self, inputs): """Forward function.""" assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #2
Source File: hrfpn.py From Feature-Selective-Anchor-Free-Module-for-Single-Shot-Object-Detection with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #3
Source File: hrfpn.py From mmdetection_with_SENet154 with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #4
Source File: hrfpn.py From Libra_R-CNN with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #5
Source File: se_efficient_densenet.py From SE_DenseNet with MIT License | 6 votes |
def forward(self, *prev_features): """原有的两次BN层需要消耗的两块显存空间, 通过使用checkpoint,实现了只开辟一块空间用来存储中间特征 """ bn_function = _bn_function_factory(self.norm1, self.relu1, self.conv1) # requires_grad is True means that model is in train status # checkpoint implement shared memory storage function if self.efficient and any(prev_feature.requires_grad for prev_feature in prev_features): bottleneck_output = cp.checkpoint(bn_function, *prev_features) else: bottleneck_output = 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
Example #6
Source File: hrfpn.py From mmdetection-annotated with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #7
Source File: hrfpn.py From FoveaBox with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #8
Source File: hrfpn.py From Cascade-RPN with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #9
Source File: hrfpn.py From IoU-Uniform-R-CNN with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #10
Source File: hrfpn.py From GCNet with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #11
Source File: hrfpn.py From RDSNet with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #12
Source File: hrfpn.py From PolarMask with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #13
Source File: hrfpn.py From kaggle-kuzushiji-recognition with MIT License | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #14
Source File: PolyFace.py From trojans-face-recognizer with MIT License | 6 votes |
def forward(self, x): if self.downsample is not None: residual = self.downsample(x) else: residual = x if self.use_checkpoint: out = checkpoint(self.group1, x) else: out = self.group1(x) if self.use_se: weight = F.adaptive_avg_pool2d(out, output_size=1) weight = self.se_block(weight) out = out * weight out = out + residual return self.act2(out)
Example #15
Source File: hrfpn.py From AerialDetection with Apache License 2.0 | 6 votes |
def forward(self, inputs): assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example #16
Source File: resnet.py From mmaction with Apache License 2.0 | 5 votes |
def forward(self, x): def _inner_forward(x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: identity = self.downsample(x) out += identity return out if self.with_cp and x.requires_grad: out = cp.checkpoint(_inner_forward, x) else: out = _inner_forward(x) out = self.relu(out) return out
Example #17
Source File: resnet_i3d.py From mmaction with Apache License 2.0 | 5 votes |
def forward(self, x): def _inner_forward(x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: identity = self.downsample(x) out += identity return out if self.with_cp and x.requires_grad: out = cp.checkpoint(_inner_forward, x) else: out = _inner_forward(x) out = self.relu(out) if self.nonlocal_block is not None: out = self.nonlocal_block(out) return out
Example #18
Source File: densenet_pono_kmax.py From attn2d with MIT License | 5 votes |
def forward(self, prev_features, encoder_mask=None, decoder_mask=None, incremental_state=None): """ Memory efficient forward pass with checkpointing Each DenseLayer splits its forward into: - bottleneck_function - therest_function Prev_features as list of features in (B, C, Tt, Ts) Returns the new features alone (B, g, Tt, Ts) """ if self.memory_efficient and any( prev_feature.requires_grad for prev_feature in prev_features ): # Does not keep intermediate values, # but recompute them in the backward pass: # tradeoff btw memory & compute x = cp.checkpoint( self.bottleneck_function, *prev_features ) else: x = self.bottleneck_function(*prev_features) x = self.norm2(x) x = F.relu(x) x = self.mconv2(x, incremental_state) if encoder_mask is not None: x = x.masked_fill(encoder_mask.unsqueeze(1).unsqueeze(1), 0) if decoder_mask is not None: x = x.masked_fill(decoder_mask.unsqueeze(1).unsqueeze(-1), 0) if self.drop_rate: x = F.dropout(x, p=self.drop_rate, training=self.training) return x
Example #19
Source File: densenet.py From attn2d with MIT License | 5 votes |
def forward(self, prev_features, encoder_mask=None, decoder_mask=None, incremental_state=None): """ Memory efficient forward pass with checkpointing Each DenseLayer splits its forward into: - bottleneck_function - therest_function Prev_features as list of features in (B, C, Tt, Ts) Returns the new features alone (B, g, Tt, Ts) """ if self.memory_efficient and any( prev_feature.requires_grad for prev_feature in prev_features ): # Does not keep intermediate values, # but recompute them in the backward pass: # tradeoff btw memory & compute x = cp.checkpoint( self.bottleneck_function, *prev_features ) else: x = self.bottleneck_function(*prev_features) x = self.norm2(x) x = F.relu(x) x = self.mconv2(x, incremental_state) if encoder_mask is not None: x = x.masked_fill(encoder_mask.unsqueeze(1).unsqueeze(1), 0) if decoder_mask is not None: x = x.masked_fill(decoder_mask.unsqueeze(1).unsqueeze(-1), 0) if self.drop_rate: x = F.dropout(x, p=self.drop_rate, training=self.training) return x
Example #20
Source File: densenet_pono.py From attn2d with MIT License | 5 votes |
def forward(self, prev_features, encoder_mask=None, decoder_mask=None, incremental_state=None): """ Memory efficient forward pass with checkpointing Each DenseLayer splits its forward into: - bottleneck_function - therest_function Prev_features as list of features in (B, C, Tt, Ts) Returns the new features alone (B, g, Tt, Ts) """ if self.memory_efficient and any( prev_feature.requires_grad for prev_feature in prev_features ): # Does not keep intermediate values, # but recompute them in the backward pass: # tradeoff btw memory & compute x = cp.checkpoint( self.bottleneck_function, *prev_features ) else: x = self.bottleneck_function(*prev_features) x = self.norm2(x) x = F.relu(x) x = self.mconv2(x, incremental_state) if encoder_mask is not None: x = x.masked_fill(encoder_mask.unsqueeze(1).unsqueeze(1), 0) if decoder_mask is not None: x = x.masked_fill(decoder_mask.unsqueeze(1).unsqueeze(-1), 0) if self.drop_rate: x = F.dropout(x, p=self.drop_rate, training=self.training) return x
Example #21
Source File: densenet_ln.py From attn2d with MIT License | 5 votes |
def forward(self, prev_features, incremental_state=None): """ Memory efficient forward pass with checkpointing Each DenseLayer splits its forward into: - bottleneck_function - therest_function Prev_features as list of features in (B, C, Tt, Ts) Returns the new features alone (B, g, Tt, Ts) """ if self.memory_efficient and any( prev_feature.requires_grad for prev_feature in prev_features ): # Does not keep intermediate values, # but recompute them in the backward pass: # tradeoff btw memory & compute x = cp.checkpoint( self.bottleneck_function, *prev_features ) else: x = self.bottleneck_function(*prev_features) x = self.norm2(x) x = F.relu(x) x = self.mconv2(x, incremental_state) if self.drop_rate: x = F.dropout(x, p=self.drop_rate, training=self.training) return x
Example #22
Source File: resnet_s3d.py From mmaction with Apache License 2.0 | 5 votes |
def forward(self, x): def _inner_forward(xx): x, traj_src = xx identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) if self.if_inflate: if self.with_trajectory: assert traj_src is not None out = self.conv2_t(out, traj_src[0]) else: out = self.conv2_t(out) out = self.bn2_t(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: identity = self.downsample(x) out += identity return out, traj_src[1:] if self.with_cp and x.requires_grad: out, traj_remains = cp.checkpoint(_inner_forward, x) else: out, traj_remains = _inner_forward(x) out = self.relu(out) return out, traj_remains
Example #23
Source File: resnet.py From mmdetection with Apache License 2.0 | 5 votes |
def forward(self, x): """Forward function.""" def _inner_forward(x): identity = x out = self.conv1(x) out = self.norm1(out) out = self.relu(out) out = self.conv2(out) out = self.norm2(out) if self.downsample is not None: identity = self.downsample(x) out += identity return out if self.with_cp and x.requires_grad: out = cp.checkpoint(_inner_forward, x) else: out = _inner_forward(x) out = self.relu(out) return out
Example #24
Source File: densenet.py From temperature_scaling with MIT License | 5 votes |
def forward(self, *prev_features): bn_function = _bn_function_factory(self.norm1, self.relu1, self.conv1) if self.efficient and any(prev_feature.requires_grad for prev_feature in prev_features): bottleneck_output = cp.checkpoint(bn_function, *prev_features) else: bottleneck_output = 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
Example #25
Source File: densenet.py From pytorch-image-models with Apache License 2.0 | 5 votes |
def call_checkpoint_bottleneck(self, x): # type: (List[torch.Tensor]) -> torch.Tensor def closure(*xs): return self.bottleneck_fn(xs) return cp.checkpoint(closure, *x)
Example #26
Source File: ctmrg.py From tensorgrad with Apache License 2.0 | 5 votes |
def CTMRG(T, chi, max_iter, use_checkpoint=False): # T(up, left, down, right) threshold = 1E-12 if T.dtype is torch.float64 else 1E-6 # ctmrg convergence threshold # C(down, right), E(up,right,down) C = T.sum((0,1)) # E = T.sum(1).permute(0,2,1) truncation_error = 0.0 sold = torch.zeros(chi, dtype=T.dtype, device=T.device) diff = 1E1 for n in range(max_iter): tensors = C, E, T, torch.tensor(chi) if use_checkpoint: # use checkpoint to save memory C, E, s, error = checkpoint(renormalize, *tensors) else: C, E, s, error = renormalize(*tensors) Enorm = E.norm() E = E/Enorm truncation_error += error.item() if (s.numel() == sold.numel()): diff = (s-sold).norm().item() #print( s, sold ) #print( 'n: %d, Enorm: %g, error: %e, diff: %e' % (n, Enorm, error.item(), diff) ) if (diff < threshold): break sold = s #print ('ctmrg converged at iterations %d to %.5e, truncation error: %.5f'%(n, diff, truncation_error/n)) return C, E
Example #27
Source File: PyDenseNetBc.py From rafiki with Apache License 2.0 | 5 votes |
def forward(self, *prev_features): bn_function = _bn_function_factory(self.norm1, self.relu1, self.conv1) if self.efficient and any(prev_feature.requires_grad for prev_feature in prev_features): bottleneck_output = cp.checkpoint(bn_function, *prev_features) else: bottleneck_output = 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
Example #28
Source File: resnet.py From Reasoning-RCNN with Apache License 2.0 | 5 votes |
def forward(self, x): def _inner_forward(x): identity = x out = self.conv1(x) out = self.norm1(out) out = self.relu(out) if not self.with_dcn: out = self.conv2(out) elif self.with_modulated_dcn: offset_mask = self.conv2_offset(out) offset = offset_mask[:, :18, :, :] mask = offset_mask[:, -9:, :, :].sigmoid() out = self.conv2(out, offset, mask) else: offset = self.conv2_offset(out) out = self.conv2(out, offset) out = self.norm2(out) out = self.relu(out) out = self.conv3(out) out = self.norm3(out) if self.downsample is not None: identity = self.downsample(x) out += identity return out if self.with_cp and x.requires_grad: out = cp.checkpoint(_inner_forward, x) else: out = _inner_forward(x) out = self.relu(out) return out
Example #29
Source File: densenet.py From efficient_densenet_pytorch with MIT License | 5 votes |
def forward(self, *prev_features): bn_function = _bn_function_factory(self.norm1, self.relu1, self.conv1) if self.efficient and any(prev_feature.requires_grad for prev_feature in prev_features): bottleneck_output = cp.checkpoint(bn_function, *prev_features) else: bottleneck_output = 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
Example #30
Source File: densenet.py From pytorch-tools with MIT License | 5 votes |
def forward(self, *inputs): bn_function = _bn_function_factory(self.norm1, self.conv1) if self.memory_efficient and any(x.requires_grad for x in inputs): out = cp.checkpoint(bn_function, *inputs) else: out = bn_function(*inputs) out = self.conv2(self.norm2(out)) out = self.dropout(out) return out