Views
No views yet
forgetting-transformer repository as a Python package and some needed dependencies (we pin the versions to make sure that this works, but you don't have to):1# We recommend you keep track of the commit hash you used. We may introduce breaking changes in the future.
2# First, uninstall to prevent potential issues
3pip uninstall forgetting_transformer && pip install -U git+https://github.com/zhixuan-lin/forgetting-transformer
4pip install pytest einops numpy
5pip install torch==2.4.0
6pip install transformers==4.44.0
7# No guarantee other commits would work; we may fix this later
8pip install --no-deps --force-reinstall git+https://github.com/sustcsonglin/flash-linear-attention.git@1c5937eeeb8b0aa17bed5ee6dae345b353196bd41import forgetting_transformer.model.register_all # Needed to register the model classes
2import forgetting_transformer.tokenizer # Needed to register the tokenizer class
3from transformers import AutoModelForCausalLM, AutoTokenizer
4import torch
5
6model = AutoModelForCausalLM.from_pretrained("zhixuan-lin/transformer-llama-760m-longcrawl64-48b")
7tokenizer = AutoTokenizer.from_pretrained("zhixuan-lin/transformer-llama-760m-longcrawl64-48b", add_bos_token=True, clean_up_tokenization_spaces=False)
8
9# Generation using HF api
10prompt = "The best thing to do in San Francisco is"
11model = model.cuda()
12encoded = tokenizer(prompt, return_tensors="pt").input_ids.cuda()
13with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
14 output = model.generate(
15 encoded,
16 max_new_tokens=30,
17 )[0]
18pred = tokenizer.decode(output, skip_special_tokens=True)
19print(pred)
20
21# Of course you can also compute the logits or loss given proper inputs
22batch_size, seq_len = encoded.shape
23labels = encoded
24input_ids = torch.roll(labels, shifts=1, dims=-1)
25input_ids[:, 0] = tokenizer.bos_token_id # 50256
26out = model(input_ids=input_ids, labels=labels)
27assert out.loss.size() == (batch_size, seq_len)
28# Logits are not returned (to save memory) if labels are given
29assert out.logits is None
30# To get logits don't provide labels
31out = model(input_ids=input_ids)
32assert out.logits.size() == (batch_size, seq_len, tokenizer.vocab_size)@inproceedings{
lin2025forgetting,
title={Forgetting Transformer: Softmax Attention with a Forget Gate},
author={Zhixuan Lin and Evgenii Nikishin and Xu He and Aaron Courville},
booktitle={The Thirteenth International Conference on Learning Representations},
year={2025},
url={https://openreview.net/forum?id=q2Lnyegkr8}
}