Views
No views yet


[!TIP] We recommend installing Flash Attention 2 andtorch.compile-ing your models to get the highest training and inference efficiency.
modeling_norbert.py, you should therefore load the model with trust_remote_code=True.1import torch
2from transformers import AutoTokenizer, AutoModelForMaskedLM
3
4# Import model
5tokenizer = AutoTokenizer.from_pretrained(
6 "ltg/norbert4-xsmall"
7)
8model = AutoModelForMaskedLM.from_pretrained(
9 "ltg/norbert4-xsmall",
10 trust_remote_code=True
11)
12
13# Tokenize text (with a mask token inside)
14input_text = tokenizer(
15 f"Nå ønsker de seg en{tokenizer.mask_token} bolig.",
16 return_tensors="pt"
17)
18
19# Inference
20with torch.inference_mode:
21 output_p = model(**input_text)
22
23# Unmask the text
24output_text = torch.where(
25 input_text.input_ids == tokenizer.mask_token_id,
26 output_p.logits.argmax(-1),
27 input_text.input_ids
28)
29
30# Decoding; should output: '<s>Nå ønsker de seg en ny bolig.'
31print(tokenizer.decode(output_text[0].tolist()))1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4# Import model
5tokenizer = AutoTokenizer.from_pretrained(
6 "ltg/norbert4-xsmall"
7)
8model = AutoModelForCausalLM.from_pretrained(
9 "ltg/norbert4-xsmall",
10 trust_remote_code=True
11)
12
13# Define zero-shot translation prompt template
14prompt = """Engelsk: {0}
15Bokmål:"""
16
17# Define tokens that should end the generation (any token with a newline)
18eos_token_ids = [
19 token_id
20 for token_id in range(tokenizer.vocab_size)
21 if '\n' in tokenizer.decode([token_id])
22]
23
24# Generation function
25@torch.inference_mode()
26def generate(text):
27 text = prompt.format(text)
28 input_ids = tokenizer(text, return_tensors='pt').input_ids
29 prediction = model.generate(
30 input_ids,
31 max_new_tokens=64,
32 do_sample=False,
33 eos_token_id=eos_token_ids
34 )
35 return tokenizer.decode(prediction[0, input_ids.size(1):]).strip()
36
37# Example usage
38generate("I'm a model that can generate text!")AutoModel, AutoModelMaskedLM, AutoModelForCausalLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoModelForQuestionAnswering and AutoModeltForMultipleChoice.davisamu@uio.no1@inproceedings{charpentier-samuel-2024-bert,
2 title = "{GPT} or {BERT}: why not both?",
3 author = "Charpentier, Lucas Georges Gabriel and
4 Samuel, David",
5 booktitle = "The 2nd BabyLM Challenge at the 28th Conference on Computational Natural Language Learning",
6 month = nov,
7 year = "2024",
8 address = "Miami, FL, USA",
9 publisher = "Association for Computational Linguistics",
10 url = "https://aclanthology.org/2024.conll-babylm.24/",
11 pages = "262--283"
12}1@inproceedings{samuel-etal-2023-norbench,
2 title = "{N}or{B}ench {--} A Benchmark for {N}orwegian Language Models",
3 author = "Samuel, David and
4 Kutuzov, Andrey and
5 Touileb, Samia and
6 Velldal, Erik and
7 {\O}vrelid, Lilja and
8 R{\o}nningstad, Egil and
9 Sigdel, Elina and
10 Palatkina, Anna",
11 booktitle = "Proceedings of the 24th Nordic Conference on Computational Linguistics (NoDaLiDa)",
12 month = may,
13 year = "2023",
14 address = "T{\'o}rshavn, Faroe Islands",
15 publisher = "University of Tartu Library",
16 url = "https://aclanthology.org/2023.nodalida-1.61",
17 pages = "618--633"
18}