Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

transformer_vq

> 数据库
Open source

Official implementation of 'Transformer-VQ: Linear-Time Transformers via Vector Quantization' (ICLR 2024)

199 stars0 likes1 views
WebsiteGitHub

About

Official implementation of 'Transformer-VQ: Linear-Time Transformers via Vector Quantization' (ICLR 2024)

Transformer-VQ

Official implementation of 'Transformer-VQ: Linear-Time Transformers via Vector Quantization'.

Single-Host Launch

The scripts use W&B for logging; it is free for personal and academic use.

Clone the repo and install the dependencies:

bash
git clone https://github.com/transformer-vq/transformer_vq/;
cd transformer_vq;
##### CPU or GPU
pip3 install -e '.[no_tpu]';
##### TPU
pip3 install -e '.[tpu]' -f https://storage.googleapis.com/jax-releases/libtpu_releases.html;

To launch an experiment, run

bash
export WANDB_API_KEY=WB_KEY;
chmod +x ./scripts/lm_DATASET.sh;
./scripts/lm_DATASET.sh \
    -c COMMAND \
    -r RNG_SEED \
    -p PRECISION \
    -t SEQUENCE_LEN \
    -l BLOCK_LEN \
    -m MEM_LEN \
    -s CODEBOOK_SIZE \
    -a AGG_CACHE \
    -g GRAD_THRU_CACHE \
    -e EXTRA_STEPS \
    -i IN_CHECKPOINT_DIR \
    -o OUT_CHECKPOINT_DIR \
    -d DATASETS_DIR \
    -v VOCAB_PATH \
    [-w WB_RUN_ID]

where

  • DATASET is one of {enwik8,pg19,imagenet64}
  • COMMAND is one of {train_vocab,train,val,test,sample}
  • RNG_SEED is the experiment seed or sampling seed
  • PRECISION is one of {float32,bfloat16}
  • SEQUENCE_LEN is the sequence length
  • BLOCK_LEN is an integer divisor of the sequence length and update length
  • MEM_LEN is the uncompressed key/value cache length (set to BLOCK_LEN in our experiments)
  • CODEBOOK_SIZE is the number of codebook rows/compressed cache length
  • AGG_CACHE is 0/1 to exclude/include the compressive cache
  • GRAD_THRU_CACHE is 0/1 to stop/allow gradients through the caches
  • EXTRA_STEPS is the number of constant learning rate steps to run after the cosine learning rate schedule (set to 0 in our experiments)
  • INPUT_CHECKPOINT_DIR is a folder name for loading checkpoints
  • OUTPUT_CHECKPOINT_DIR is a folder name for saving checkpoints
  • DATASETS_DIR is a path for saving downloaded datasets locally or in Google Cloud Storage
  • VOCAB_PATH is a path for a sentencepiece vocabulary, used for the PG-19 model
  • WB_KEY can be obtained from https://wandb.ai/authorize
  • WB_RUN_ID should be the run ID from the W&B run URL, if resuming a run

Training a vocabulary

To use the PG-19 model, you need a SentencePiece vocabulary.

  • An external SentencePiece vocabulary hosted on Google Cloud Storage can be used. For example, you can use the T5 vocabulary.

  • Alternately, a SentencePiece vocabulary can be trained on the PG-19 training corpus. To do so, launch lm_pg19.sh with COMMAND set to train_vocab. The trained SentencePiece model will be written to VOCAB_PATH, and this path can be supplied for training, evaluation, and sampling.

Multi-Host Launch - TPU Pod Slice

To launch on a TPU pod or pod slice, all commands can be run remotely as follows:

…

The last line uses -x as a flag for the control script lm_DATASET.sh to enable multi-host mode, and uses ... as a placeholder for other arguments, which follow single-host launch.

Attaching and detaching tmux sessions

To view the script output, you can SSH into any of the TPU hosts

gcloud compute tpus tpu-vm ssh TPU_POD_SLICE_NAME --zone ZONE --worker=WORKER_ID

Then attach the tmux session with

tmux attach -t transformer_vq_session

To return the session to detached mode, allowing it to continue running after you leave the ssh session, type Ctrl+b, then type d.

Killing scripts and tmux sessions

To kill the script running in the tmux session on all hosts, you can run the following on your local machine: it will SSH to each host and kill the script.

./scripts/kill.sh -n TPU_POD_SLICE_NAME -z ZONE -c NUM_HOSTS

To kill the tmux session you can run

bash
gcloud compute tpus tpu-vm ssh TPU_POD_SLICE_NAME \
  --zone ZONE \
  --worker=all \
  --command="tmux kill-session -t transformer_vq_session" 

Deleting the instance

You can delete the pod/slice instance as follows:

gcloud compute tpus tpu-vm delete TPU_POD_SLICE_NAME --zone ZONE

Multi-Host Launch - GPU Clusters

Multi-host mode for GPUs is not currently supported by our scripts, as it requires rendezvous information to be provided to Jax. Support will be added in the future.

Issues· 2 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Pythondeep-learningefficient-attentionjaxlong-context

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库