The code works for me:
import matplotlib.pyplot as plt
import numpy as np 

class down(torch.nn.Module):
    def __init__(self,channel_in,channel_out,ksize=1):
        super(down, self).__init__()
        self.conv1=torch.nn.Conv2d(channel_in,channel_out,ksize)
        self.conv2=torch.nn.Conv2d(channel_out,channel_out,ksize)
    def forward(self,x):
        relu=self.conv1(x).clamp(min=0)
        #relu=self.conv2(relu).clamp(min=0)
        return relu

class up(torch.nn.Module):
    def __init__(self,channel_in,channel_out,ksize=3,space_dropout=False):
        super(up, self).__init__()
        self.up1=torch.nn.ConvTranspose2d(channel_in,channel_out,ksize)
        self.conv3=torch.nn.Conv2d(channel_out,channel_out,ksize)
        self.conv4=torch.nn.Conv2d(channel_out,channel_out,ksize)    
    def forward(self,x):
        relu=self.up1(x)
        relu=self.conv3(relu).clamp(min=0)
        relu=self.conv4(relu).clamp(min=0)
        return relu

class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()  
        self.d1=down(3,1)
        self.d2=down(1,1)
        self.d3=down(1,1)
        self.d4=down(1,1)
        self.u11=up(1,1)
        self.u1=up(1,1)
        self.u2=up(1,1)
        self.u3=up(1,1)
    def forward(self,x):
        b=self.d1(x)
        b=self.d2(b)
        b=self.d3(b)
        b=self.d4(b)
        b=self.u11(b)
        b=self.u1(b)
        b=self.u2(b)
        b=self.u3(b)
        return b

net=Net()
net=net.float()
lr=1e-05


x = torch.randn(1, 3, 128, 128)
print(len(X))
y = torch.randn(1, 1, 120, 120)

loss_function=torch.nn.BCELoss()
optimizer=torch.optim.Adam(net.parameters())
for i in range(1):
    y_pred=net(x.float())
    loss=loss_function(y_pred.float(),y.float())
    print('loss '+str(i)+'th iter=',loss)
    loss.backward()
    net.zero_grad()
    optimizer.step()

torch.save(net.state_dict(),'tmp.pt')

I can’t even find a call to detach(), so I’m not sure what’s causing this error.
Could you post the stack trace?