How to efficient batch-process in huggingface?

Viewed 150

I am using Huggingface library and transformers to find whether a sentence is well-formed or not. I am using a masked language model called XLMR. I first tokenize my sentence, and then mask each word of the sentence one by one, and then process the masked sentences and find the probability that the predicted masked word is right.

def calculate_scores(sent, model, tokenizer, device, print_pred=False, maskval=False):

k = 0
dic = {}
ls = tokenizer.batch_encode_plus(sent)
input_list = ls.input_ids
h=0
with torch.no_grad():
    for i in tqdm(range(len(input_list))):

        item = input_list[i]
        real_input = item
        attmask = [1]*len(item)
        seg = [0]*len(item)
        seglist = [seg]
        masked_list = [real_input]
        attlist = [attmask]
        for j in range(1, len(item)-1):
            input = copy.deepcopy(real_input)
            input[j] = 50264
            masked_list.append(input)
            attlist.append(attmask)
            seglist.append(seg)
        
        inid = torch.tensor(masked_list)
        segtensor = torch.tensor(seglist)
        atttensor = torch.tensor(attlist)
        inid=inid.to(device)
        segtensor=segtensor.to(device)
        output = model(inid, segtensor)

        predictions_logits = output.logits
        predictions = torch.softmax(predictions_logits, dim=2)
        ppscore = 0
        for j in range(1, len(item)-1):
            ppscore = ppscore+math.log(predictions[j, j, item[j]], 2)
        try:
            score = math.pow(2, (-1/(len(item)-2))*ppscore)
            dic[sent[i]] = score
        except:
            print(sent[i])
            dic[sent[i]] = 10000000

        # dic[sent[i]]=10000000
return dic

I will explain my code quickly. The function calculate_scores has sent as an input which is a list of sentences. I first batch encode this list of sentences. And then for each encoded sentence that I get, I generate masked sentences where only one word is masked and the rest are un-masked. Then I input these generated sentences to output and get the probability. Then I compute perplexity.

But the way I'm using this is not a very good way of utilizing GPU. I want to process multiple sentences at once but at the same time, I also need to find the perplexity scores for each sentence. How would I go about doing this?

0 Answers
Related