You would have to define kernel as an nn.Parameter:
kernel = torch.tensor(kernel,dtype=dtype,device=device)
kernel = kernel[None,None,:,:]
kernel = nn.Parameter(kernel)

and pass it to the optimizer:
optimizer = torch.optim.SGD([kernel], lr=1e-3)

After that’s done, remove the manual update step, and call optimizer.step().
Also, don’t forget the zero out the gradients afterwards via optimizer.zero_grad().
PS: I’ve formatted your code and you can post code snippets by wrapping them into three backticks ```, which makes debugging easier