no grad if frozen in forward pass

This commit is contained in:
YannAhlgrim
2026-04-25 11:00:14 +02:00
parent 417319457a
commit 02991a7eb5
+5 -1
View File
@@ -13,7 +13,11 @@ class ViTClassifier(nn.Module):
def forward(self, x): def forward(self, x):
# ViT -> (B, N, D) # ViT -> (B, N, D)
features = self.encoder(x) if any(p.requires_grad for p in self.encoder.parameters()):
features = self.encoder(x)
else:
with torch.no_grad():
features = self.encoder(x)
# Average Pool -> (B, D) # Average Pool -> (B, D)
avg_embed = features.mean(dim=1) avg_embed = features.mean(dim=1)