If I execute your code I get a shape mismatch error:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1024x1 and 512x32)

since you are not flattening the activation.
This should work:
class Resnet18(torch.nn.Module):
    def __init__(self):
        super(Resnet18, self).__init__()
        resnet18_pretrained = models.resnet18(pretrained=True)

        self.model = resnet18_pretrained
        self.model.fc =  nn.Sequential(nn.Linear(512, 32),nn.Linear(32, 5))
        self.couches_before_fc = list(self.model.children())[:-1]
        self.resnet_before_fc = nn.Sequential(*self.couches_before_fc)
        self.resnet_before_fc.fc = nn.Sequential(
            nn.Flatten(),
            self.model.fc[0]
        )

    def forward(self, x):

        before_last_fc = self.resnet_before_fc(x)

        x=self.model(x)

        return x, before_last_fc
    
model = Resnet18()
x = torch.randn(2, 3, 224, 224)
out = model(x)

The error is created, since you are wrapping all child modules into an nn.Sequential container, which will be missing all functional API calls from the original forward method (and thus also the flattening operation), so make sure to verify that the nn.Sequential container is indeed calling all needed operations/modules.