Skip to content

Commit

Permalink
update test file
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyuanyin committed Sep 3, 2023
1 parent 2b3d4e9 commit 05bc610
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions tests/test_runner/test_gradient_checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest import TestCase

import torch
import torch.nn.functional as F
from torch import nn

from mmengine.runner.gradient_checkpoint import turn_on_gradient_checkpoint
Expand All @@ -12,27 +13,33 @@ class Model(nn.Module):

def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(6, 6, 6)
self.bn1 = nn.BatchNorm2d(6)
self.conv2 = nn.Conv2d(6, 6, 6)
self.bn2 = nn.BatchNorm2d(6)
self.conv3 = nn.Conv2d(6, 6, 6)
self.bn3 = nn.BatchNorm2d(6)
self.linear = nn.Linear(6, 6)
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(16)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(32)
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(64)
self.pool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(64, 10)

def forward(self, x):
x = self.bn1(self.conv1(x))
x = F.relu(x)
x = self.bn2(self.conv2(x))
x = F.relu(x)
x = self.bn3(self.conv3(x))
x = self.linear(x)
x = F.relu(x)
x = self.pool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x


class TestGradientCheckpoint(TestCase):

def test_gradient_checkpoint(self):
model = Model()
input = torch.randn(64, 6, 32, 32)
input = torch.randn(16, 3, 224, 224)
input.requires_grad = True

output = model(input)
Expand All @@ -45,4 +52,4 @@ def test_gradient_checkpoint(self):
grad2 = input.grad.clone()

assert_allclose(output, output2)
assert_allclose(grad, grad2)
assert_allclose(grad, grad2, rtol=1e-3, atol=1e-3)

0 comments on commit 05bc610

Please sign in to comment.