Ok well the only things you should have to edit are when you define your model also include
model.to(device)

then in both your training and validation loops when defining the x, y, mask, and lengths do this
x = x.to(device)
y = y.to(device)
mask = mask.to(device)
lengths = lengths.to(device)

and also when you append the loss to your list do this
loss.cpu().item()

That should work but some of your functions might return errors if so just send which line the error is on and I should be able to help.