Yes, it should work as long as you don’t change the output type. In your case you could return an empty tensor in the if path as seen here:
class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(1, 10)
        self.fc2 = nn.Linear(1, 2)
        self.flag = True
        
    def forward(self, x):
        if self.flag:
            out = self.fc1(x)
            return out, torch.empty_like(out)
        else:
            out = self.fc2(x)
            out2 = out + 1
            return out, out2
    
# eager mode
model = MyModel()
x = torch.randn(1, 1)

out = model(x)
print(out[0].shape)
> torch.Size([1, 10])

model.flag = False
out = model(x)
print(out[0].shape)
> torch.Size([1, 2])

# script
model = torch.jit.script(model)
out = model(x)
print(out[0].shape)
> torch.Size([1, 2])

model.flag = True
out = model(x)
print(out[0].shape)
> torch.Size([1, 10])