Predicting with model and trying to save at the same time gives different prediction results and doesnt save

I am using bert uncased model
I am predicting my model like this:


def predict_label(text,username):
    # input_ids = torch.tensor(tokenizer.encode(text)).unsqueeze(0)
    model=getmodelfromusername(username)
    input_ids=tokenizer(text, padding=True, truncation=True, max_length=500, return_tensors="pt")
    logits = model(**input_ids)[0]
    probs = torch.nn.functional.softmax(logits, dim=1)
    
    return probs

I am training like this:

def train_and_update_model(model, parseddata, code, username,number):

    optimizer = torch.optim.AdamW(model.parameters(), lr=4e-5)

    lr_scheduler = get_scheduler(
        name="linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=2
    )

    input_ids = torch.tensor([tokenizer.encode(str(parseddata), add_special_tokens=True)])
    labels = torch.tensor([number])

    # del banmodel

    model.train(mode=True)

    for i in range(2):
        outputs = model(input_ids, labels=labels)
        loss = outputs[0]
        loss.backward()
        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()

    with lock:
        model.save_pretrained(username + "/CustomModel")

    with lock:
        model = BertForSequenceClassification.from_pretrained(username + '/CustomModel')
    changemodelwithcode(code, model)

In my application sometimes Im gonna need to predict alot of text that is incoming and also at the same time I need to be able to train at any moment. However, i get this error when I try to do that

at: model.save_pretrained(username + "/CustomModel")
RuntimeError: File test/CustomModel\pytorch_model.bin cannot be opened.

Any help would be extremely appreciated thanks