Chris is right, CUDA side errors are not usually useful. You can do the following to see the full stack trace on GPU
import os
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"

Also, a simple solution for an automated last layer would be to pass the target column of your csv to the model when you initialize it. Then use the number of unique values in that to set the last linear layer.
For example
class ClassificationBert(nn.Module):
    def __init__(self, target_col):
        super().__init__()
        self.bert = BertModel.from_pretrained('bert-base-uncased')
        self.num_classes = target_col.nunique() # considering its a pandas series

and then using the num_classes for your linear layer.