import torch import torch.nn.functional as F from transformers import XGLMTokenizer,…
- User
import torch import torch.nn.functional as F
from transformers import XGLMTokenizer, XGLMForCausalLM
tokenizer = XGLMTokenizer.from_pretrained("facebook/xglm-564M") model = XGLMForCausalLM.from_pretrained("facebook/xglm-564M")
data_samples = { 'en': [ { "premise": "I wanted to conserve energy.", "choice1": "I swept the floor in the unoccupied room.", "choice2": "I shut off the light in the unoccupied room.", "question": "effect", "label": "1" }, { "premise": "The flame on the candle went out.", "choice1": "I blew on the wick.", "choice2": "I put a match to the wick.", "question": "cause", "label": "0" } ], 'zh': [ { "premise": "我想节约能源。", "choice1": "我在空着的房间里扫了地板。", "choice2": "我把空房间里的灯关了。", "question": "effect", "label": "1" }, { "premise": "蜡烛上的火焰熄灭了。", "choice1": "我吹灭了灯芯。", "choice2": "我把一根火柴放在灯芯上。", "question": "cause", "label": "0" } ], 'hi': [ { "premise": "M te vle konsève enèji.", "choice1": "Mwen te fin baleye chanm lib la.", "choice2": "Mwen te femen limyè nan chanm lib la.", "question": "effect", "label": "1" }, { "premise": "Flam bouji a te etenn.", "choice1": "Mwen te soufle bouji a.", "choice2": "Mwen te limen mèch bouji a.", "question": "cause", "label": "0" } ] }
def get_logprobs(prompt): inputs = tokenizer(prompt, return_tensors="pt") input_ids, output_ids = inputs["input_ids"], inputs["input_ids"][:, 1:] outputs = model(**inputs, labels=input_ids) logits = outputs.logits logprobs = torch.gather(F.log_softmax(logits, dim=2), 2, output_ids.unsqueeze(2)) return logprobs
Zero-shot evaluation for the Choice of Plausible Alternatives (COPA) task.
A return value of 0 indicates that the first alternative is more plausible,
while 1 indicates that the second alternative is more plausible.
def COPA_eval(prompt, alternative1, alternative2): lprob1 = get_logprobs(prompt + "\n" + alternative1).sum() lprob2 = get_logprobs(prompt + "\n" + alternative2).sum() return 0 if lprob1 > lprob2 else 1
for lang in data_samples: for idx, example in enumerate(data_samples[lang]): predict = COPA_eval(example["premise"], example["choice1"], example["choice2"]) print(f'{lang}-{idx}', predict, example['label'])
en-0 1 1
en-1 0 0
zh-0 1 1
zh-1 0 0
hi-0 1 1
hi-1 0 0
- Assistant
The code provided is an example of zero-shot evaluation for the Choice of Plausible Alternatives (COPA) task using XGLM.
The code first initializes the XGLM tokenizer and model using the "facebook/xglm-564M" pre-trained weights.
Then, it defines a dictionary called "data_samples" that contains example COPA tasks in different languages. Each task consists of a prompt (premise), two alternatives (choice1 and choice2), a question (cause or effect), and a label indicating the correct answer.
Next, the code defines a function called "get_logprobs" that takes a prompt as input, tokenizes it using the XGLM tokenizer, and generates the log probabilities for each token in the output sequence. It uses the XGLM model to calculate the log probabilities.
The code also defines a function called "COPA_eval" that takes a prompt and two alternatives as input. It uses the "get_logprobs" function to calculate the log probabilities for each alternative and returns 0 if the first alternative is more plausible and 1 if the second alternative is more plausible.
- User
Process finished with exit code 137 (interrupted by signal 9: SIGKILL)
- Assistant
The exit code 137 means that the program was terminated by a signal, specifically signal 9 (SIGKILL). This signal is usually sent by an external process or system to forcefully terminate the program.
In most cases, this error occurs when the program consumes too much memory or exceeds the allocated resources. This can happen if the model or input data is too large for the available resources, causing the program to be terminated by the operating system.
To resolve this issue, you can try reducing the batch size or input size, optimizing your code for memory efficiency, or running the code on a system with more resources. Alternatively, you can consider using a smaller model or breaking down the input data into smaller chunks to process them in batches.