(this option ain’t pretty, it’s more like a hack). For example here’s a forward method of resnet class:
    def _forward_impl(self, x):
        #out = {}
        x = self.conv1(x)
        #out['conv1'] = x.detach()
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        #out['maxpool'] = x.detach()
        x = self.layer1(x)
        #out['layer1'] = x.detach()
        x = self.layer2(x)
        #out['layer2'] = x.detach()
        x = self.layer3(x)
        #out['layer3'] = x.detach()
        x = self.layer4(x)
        #out['layer4'] = x.detach()
        x = self.avgpool(x)
        #out['avgpool'] = x.detach()
        x = torch.flatten(x, 1)
        x = self.fc(x)
        return x, out #instead of return x



commented lines will create dict with copies of intermediate values, detached from graph