Package: rotary-embedding-tensorflow
Rotary Embeddings - TensorflowA standalone library for addingrotary embeddingsto transformers in Tesnorflow, following its success asrelative positional encoding. Specifically it will make rotating information into any axis of a tensor easy and efficient, whether they be fixed positional or learned. This library will give you state of the art results for positional embedding, at little costs.My gut also tells me there is somethingmoreto rotations that can be exploited in artificial neural networks.NoteAn implemented version of Pytorch is available in thisrepository.This version is written by converting to the version of Pytorch.The three functions of rearrange, irearrange and repeat have been written due to the incompatibility of the einops library with tensorflow 2.x.Install$pipinstallrotary-embedding-tensorflowUsageimporttensorflowastffromrotary_embedding_tensorflowimportapply_rotary_emb,RotaryEmbedding# instantiate the positional embedding in your transformer and pass to all your attention layerspos_emb=RotaryEmbedding(dim=32)# generate the rotationsfreqs=pos_emb(tf.range(1024),cache_key=1024)# cache with a key that is the sequence length, so that it does not need to recompute# mock queries and keysq=tf.random.normal((1,1024,64))# queries - (batch, seq len, dimension of head)k=tf.random.normal((1,1024,64))# keys# apply the rotations to your queries and keys after the heads have been split out, but prior to the dot product and subsequent softmax (attention)freqs=freqs[None,...]# expand dimension for batch dimensionq=apply_rotary_emb(freqs,q)k=apply_rotary_emb(freqs,k)# then do your attention with your queries (q) and keys (k)If you do all the steps above correctly, you should see a dramatic improvement during trainingAxial Rotary EmbeddingsFor easy use of 2d axial relative positional embedding, ie. vision transformersimporttensorflowastffromrotary_embedding_tensorflowimportapply_rotary_emb,RotaryEmbedding,broadcatpos_emb=RotaryEmbedding(dim=32,freqs_for='pixel')# queries and keys for frequencies to be rotated intoq=tf.random.normal((1,256,256,64))k=tf.random.normal((1,256,256,64))# get frequencies for each axial# -1 to 1 has been shown to be a good choice for images and audiofreqs_h=pos_emb(tf.linspace(-1,1,num=256),cache_key=256)freqs_w=pos_emb(tf.linspace(-1,1,num=256),cache_key=256)# concat the frequencies along each axial# broadcat function makes this easy without a bunch of expandsfreqs=broadcat((freqs_h[None,:,None,:],freqs_w[None,None,:,:]),dim=-1)# rotate in frequenciesq=apply_rotary_emb(freqs,q)k=apply_rotary_emb(freqs,k)Learned RotationsFor injecting learned rotations into a network. Experiments pendingUpdate: doesn't seem to do anything -_-, will keep trying...importtensorflowastffromtensorflow.kerasimportlayersfromrotary_embedding_tensorflowimportapply_learned_rotationsx=tf.random.normal((1,1024,512))# you can only rotate in (dim // 2) values# ex. for 512, you can only rotate in 256 values# say you have two sets of learned rotations of 128 values eachrots1=layers.Dense(128)(x)rots2=layers.Dense(128)(x)# you rotate in 256 (128 x 2) at firstx=apply_learned_rotations(rots1,x,start_index=0)# then you start at index 256 and rotate in the last (128 x 2)x=apply_learned_rotations(rots2,x,start_index=256)# you could also concat the rotations together and pass it in all at oncerots=tf.concat((rots1,rots2),axis=-1)x=apply_learned_rotations(rots,x)Citations@misc{su2021roformer,title={RoFormer: Enhanced Transformer with Rotary Position Embedding},author={Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu},year={2021},eprint={2104.09864},archivePrefix={arXiv},primaryClass={cs.CL}}@misc{rotary-embedding-torch,title={Rotary Embeddings - Pytorch},author={Phil Wang (lucidrains)},year={2021},url={https://github.com/lucidrains/rotary-embedding-torch},publisher={Github},}