im doing this now. im detaching x but im also adding requires_grad=True for the loss.
in_features = 12000
out_features = 6
model = nn.Sequential(nn.Linear(in_features, out_features))
optimizer = torch.optim.SGD(model.parameters(), lr=0.000001)
print(model)
x = get_data()
for n in range(10):
    x.requires_grad_() 
    output = model(x)
    output1 = output.detach()
    x = x.detach()
    list_of_gaps = calculate_gap(output1, x, Wks, s_prime)
    loss = my_loss(list_of_gaps)
    loss = Variable(loss, requires_grad = True)
    print('LOSS', loss)
    loss.backward()
    optimizer.step()