That’s a weird issue.
I tried to reproduce it using your code snippets, but they seem to work fine:
class SiameseNetwork(nn.Module):
    def __init__(self):
        super(SiameseNetwork, self).__init__()
        # Define layers
        self.layer1 = nn.Linear(1, 1)
        self.layer2 = nn.Linear(1, 1)

    def layers(self, input):
        X = self.layer1(input)
        X = self.layer2(X)
        return X
    
    def forward(self, input1, input2):
        output1 = self.layers(input1)
        output2 = self.layers(input2)
        return output1, output2

model = SiameseNetwork()
criterion = nn.MSELoss()

x1 = torch.randn(1, 1)
x2 = torch.randn(1, 1)
out1, out2 = model(x1, x2)
loss = criterion(out1, out2)
loss.backward()

#############################

class CoreModel(nn.Module):
    def __init__(self):
        super(CoreModel, self).__init__()
        # Define layers 
        self.layer1 = nn.Linear(1, 1)
        self.layer2 = nn.Linear(1, 1)

    def forward(self, input):
        X = self.layer1(input)
        X = self.layer2(X)
        return X

class SiameseNetwork(nn.Module):
    def __init__(self, core_model):
        super(SiameseNetwork, self).__init__()
        self.core_model = core_model
    
    def forward(self, input1, input2):
        output1 = self.core_model(input1)
        output2 = self.core_model(input2)
        return output1, output2

core_model = CoreModel()
model = SiameseNetwork(core_model)

criterion = nn.MSELoss()

x1 = torch.randn(1, 1)
x2 = torch.randn(1, 1)
out1, out2 = model(x1, x2)
loss = criterion(out1, out2)
loss.backward()

Could you please check, what might be different between your code any my simple reproduction approach?