My code doesn't work after update to Pytorch 2.0

After a while I came back to working with Pytoch I updated
I use VS 2022 for it everything worked fine but I got now an error

from torchmetrics import R2Score

def train(model, optimizer, x1,x2, y):
    
    # Reset gradient
    optimizer.zero_grad()
  
    # Forward
    fx = model.forward(x1,x2)
    #loss = model.N1L(fx, y)
    loss = 0
    r2 = 0
    count = 0
    count2 = 0
    for i,result in enumerate(fx):
        loss += model.r2_dd_loss(result, y[i], 3)
        count += 1
    #loss= model.r3_loss(fx,y)
    #print(count)
    loss /= count
    for i,result in enumerate(fx):
        r2 += model.r2(result,y[i])
        count2 += 1
    # Backward
    r2 /= count2
    loss.backward()
    
    # Update parameters
    optimizer.step()

basically the model.r2(result,y[i]) which is the R2 function gives the error: R2Score’ object has no attribute ‘_backward_pre_hooks’. I actually don’t need any backwards tracking because its just collecting data so not sure what going on here

Could you add the missing code pieces to create a minimal and executable code snippet reproducing the issue, please?

Unfortunately the project is a bit too big to break it easy down . I tested a few things when R2 from torchmetrics is part of the backtracking / losing function everything works great when not doesn’t matter where I put it it always cause errors. I solved it by using" from torcheval.metrics.functional import r2_score" instead. Also the torchmetrics library seems not have been updated in a while

FWIW torchmetrics and torcheval are 2 different but similar projects