this is my code. It’s very kind of you to reproduce.
I have tried that feed the same batch into these two model, there is no difference of loss and gradient.
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor, Scale, Compose
from torchvision.models import resnet18
from torch.utils.data import DataLoader, TensorDataset
import torch
from torch.nn.functional import one_hot
import numpy as np
import copy
import random


def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True



setup_seed(20)

model = resnet18(pretrained=False, num_classes=10)
model1 = copy.deepcopy(model)

opt = torch.optim.Adam(model.parameters(), lr=0.001)
loss_func = torch.nn.BCEWithLogitsLoss(reduction='sum')

mnist = MNIST('./data/', download=True, transform=Compose([ToTensor()]))

dataloader = DataLoader(mnist, batch_size=32, shuffle=False)
dataloader1 = DataLoader(mnist, batch_size=2, shuffle=False)



grads_set = set()
data_list = []
for data in dataloader:
    data, label = data
    data = data.repeat(1, 3, 1, 1)
    print(label)
    y_one_hot = one_hot(label, 10).float()
    output = model(data)
    loss = loss_func(output, y_one_hot)
    print(loss)
    loss = torch.sum(loss)
    loss.backward()

    grads = []
    data_list = data.numpy()

    break

print()
num = 0
grads = []
data_list1 = []
loss_sum = 0
for data in dataloader1:
    data, label = data
    data = data.repeat(1, 3, 1, 1)
    print(label)
    y_one_hot = one_hot(label, 10).float()
    output = model1(data)
    loss = loss_func(output, y_one_hot)
    loss = torch.sum(loss)
    loss.backward()
    loss_sum = loss_sum + loss.detach().numpy()
    if len(grads) == 0:
        for name, params in model1.named_parameters():
            grads.append(params.grad.data.numpy())
    else:
        for idx, (name, params) in enumerate(model1.named_parameters()):
            grads[idx] += params.grad.data.numpy()
    data_list1.extend(data.numpy())
    num += 1
    if num == 16:
        break

print(loss_sum)
grad_mean = []
for name, param in model.named_parameters():
    grad_mean.append(np.mean(param.grad.data.numpy()))

grad_mean1 = []
for name, param in model1.named_parameters():
    grad_mean1.append(np.mean(param.grad.data.numpy()))

print(np.array(grad_mean) - np.array(grad_mean1))

opt.zero_grad()
for data in dataloader:
    data, label = data
    data = data.repeat(1, 3, 1, 1)
    print(label)
    y_one_hot = one_hot(label, 10).float()
    output = model(data)
    loss = loss_func(output, y_one_hot)
    loss.backward()

    grads = []
    data_list = data.numpy()

    break


grad_mean = []
for name, param in model.named_parameters():
    grad_mean.append(np.mean(param.grad.data.numpy()))


print(np.array(grad_mean) - np.array(grad_mean1))