This repository contains the
Olifant model, an implementation of memory-based language modeling, presented in the paper
Memory-based Language Models: An Efficient, Explainable, and Eco-friendly Approach to Large Language Modeling.
For more details, installation instructions, and further usage examples, please refer to the
official GitHub repository.
Note: For actual inference, you will need a trained
.ibase classifier file. The
CLASSIFIER_PATH in the example below should point to your
.ibase file. You can generate this file by following the training instructions in the
Olifant GitHub repository.
1import torch
2from transformers import AutoTokenizer, AutoConfig
3from olifant.model.hf_wrapper import TimblHuggingFaceModel
4from olifant.classifier import timbl
5
6# Define paths and arguments
7# IMPORTANT: Replace "path/to/your/textfile_tok.l4r0.ibase" with the actual path to your .ibase file.
8CLASSIFIER_PATH = "path/to/your/textfile_tok.l4r0.ibase"
9TOKENIZER_NAME = "gpt2" # The tokenizer used during training (e.g., 'gpt2' as per olifant-tok)
10TIMBL_ARGS = "-a4" # For TRIBL2 k-NN approximation (as recommended for inference)
11
12# Initialize the tokenizer
13tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_NAME)
14tokenizer.add_special_tokens({'pad_token': '_'})
15tokenizer.pad_token = "_" # Ensure pad token is set
16
17# Initialize the Timbl classifier
18classifier_core = timbl.TimblClassifier(CLASSIFIER_PATH, TIMBL_ARGS)
19classifier_core.load()
20
21# Load the model configuration from the Hugging Face Hub
22config = AutoConfig.from_pretrained("antalvdb/mblm-chatbot-instruction-prompts-igtree")
23
24# Initialize the TimblHuggingFaceModel
25model = TimblHuggingFaceModel(config, classifier_core, tokenizer)
26
27# Example text generation
28input_text = "The quick brown fox jumps over the lazy"
29input_ids = tokenizer.encode(input_text, return_tensors="pt")
30
31# Perform text generation
32with torch.no_grad():
33 output_ids = model.generate(
34 input_ids,
35 max_new_tokens=10,
36 num_beams=1,
37 do_sample=False, # Use greedy decoding for simplicity
38 pad_token_id=tokenizer.pad_token_id,
39 eos_token_id=tokenizer.eos_token_id,
40 )
41
42generated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
43print(f"Input: {input_text}")
44print(f"Generated: {generated_text}")
1@article{van_den_bosch_risco_paton_buijse_berck_van_gompel_2025,
2 title={Memory-based language models: An efficient, explainable, and eco-friendly approach to large language modeling},
3 author={Van den Bosch, Antal and Risco Patón, Ainhoa and Buijse, Teun and Berck, Peter and Van Gompel, Maarten},
4 year={2025},
5 eprint={2510.22317},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL},
8 url={https://arxiv.org/abs/2510.22317},
9}