Hi, I think you have any other issue.
import torch

model = torch.nn.Conv2d(1, 2, 3)
opt = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
inp = torch.rand(1, 1, 10, 10)
out = model(inp)
scalar = out.mean()

for _ in range(10):
    print(_)
    opt.zero_grad()
    # with torch.no_grad():
    gt = torch.rand_like(out)
    loss = (scalar * gt).mean() #just  a dummy loss
    loss.backward(retain_graph=True)
    print(list(model.parameters())[0].grad)
    opt.step()

This minimal example emulates what you want to do in code B. It’s runable and
0
tensor([[[[0.1069, 0.1032, 0.1004],
          [0.1086, 0.1056, 0.1034],
          [0.1055, 0.1049, 0.1043]]],


        [[[0.1069, 0.1032, 0.1004],
          [0.1086, 0.1056, 0.1034],
          [0.1055, 0.1049, 0.1043]]]])
1
tensor([[[[0.1168, 0.1128, 0.1097],
          [0.1187, 0.1154, 0.1129],
          [0.1152, 0.1146, 0.1139]]],


        [[[0.1168, 0.1128, 0.1097],
          [0.1187, 0.1154, 0.1129],
          [0.1152, 0.1146, 0.1139]]]])
2
tensor([[[[0.1250, 0.1206, 0.1174],
          [0.1270, 0.1234, 0.1208],
          [0.1233, 0.1226, 0.1219]]],


        [[[0.1250, 0.1206, 0.1174],
          [0.1270, 0.1234, 0.1208],
          [0.1233, 0.1226, 0.1219]]]])
3
tensor([[[[0.1225, 0.1182, 0.1150],
          [0.1244, 0.1210, 0.1184],
          [0.1208, 0.1201, 0.1194]]],


        [[[0.1225, 0.1182, 0.1150],
          [0.1244, 0.1210, 0.1184],
          [0.1208, 0.1201, 0.1194]]]])
4
tensor([[[[0.1216, 0.1173, 0.1142],
          [0.1235, 0.1201, 0.1175],
          [0.1199, 0.1192, 0.1185]]],


        [[[0.1216, 0.1173, 0.1142],
          [0.1235, 0.1201, 0.1175],
          [0.1199, 0.1192, 0.1185]]]])
5
tensor([[[[0.1166, 0.1125, 0.1095],
          [0.1184, 0.1151, 0.1127],
          [0.1150, 0.1144, 0.1137]]],


        [[[0.1166, 0.1125, 0.1095],
          [0.1184, 0.1151, 0.1127],
          [0.1150, 0.1144, 0.1137]]]])
6
tensor([[[[0.1281, 0.1236, 0.1203],
          [0.1301, 0.1265, 0.1238],
          [0.1264, 0.1256, 0.1249]]],


        [[[0.1281, 0.1236, 0.1203],
          [0.1301, 0.1265, 0.1238],
          [0.1264, 0.1256, 0.1249]]]])
7
tensor([[[[0.1114, 0.1075, 0.1046],
          [0.1131, 0.1100, 0.1077],
          [0.1099, 0.1092, 0.1086]]],


        [[[0.1114, 0.1075, 0.1046],
          [0.1131, 0.1100, 0.1077],
          [0.1099, 0.1092, 0.1086]]]])
8
tensor([[[[0.1179, 0.1138, 0.1107],
          [0.1197, 0.1164, 0.1139],
          [0.1163, 0.1156, 0.1149]]],


        [[[0.1179, 0.1138, 0.1107],
          [0.1197, 0.1164, 0.1139],
          [0.1163, 0.1156, 0.1149]]]])
9
tensor([[[[0.1183, 0.1141, 0.1111],
          [0.1202, 0.1168, 0.1143],
          [0.1167, 0.1160, 0.1153]]],


        [[[0.1183, 0.1141, 0.1111],
          [0.1202, 0.1168, 0.1143],
          [0.1167, 0.1160, 0.1153]]]])

gradients properly flow
Your error seems to be related to None. Is the graph ok? Have you tried at least one run checking gradients are not None?