do you mean something like this?
import torch.nn as nn, torch

class A(nn.Module):
  def __init__(self):
    super().__init__()
    self.lin = nn.Linear(3, 3)
  def forward(self, x):
    return self.lin(x)

class B(nn.Module):
  def __init__(self):
    super().__init__()
    self.lin = nn.Linear(3, 3)
  def forward(self, x):
    return self.lin(x)

net1 = A()
net2 = B()

optimizer1 = torch.optim.SGD(net1.parameters(), lr=0.01)
optimizer2 = torch.optim.SGD(net2.parameters(), lr=0.01)

for i in range(2):
  input = torch.randn(3, 3)
  loss = (net1(input) + net2(input)).sum()
  loss.backward()
  for name1, param1 in net1.named_parameters():
    for name2, param2 in net2.named_parameters():
      print(name1, param1.grad, name2, param2.grad)
      print('\n')
      if(name1 == name2):
        param2.grad += param1.grad
        print(name1, param1.grad, name2, param2.grad)
  optimizer1.step()
  optimizer2.step()