{"id":15600996,"url":"https://github.com/lucidrains/bidirectional-cross-attention","last_synced_at":"2025-04-07T19:16:09.728Z","repository":{"id":51227533,"uuid":"474692323","full_name":"lucidrains/bidirectional-cross-attention","owner":"lucidrains","description":"A simple cross attention that updates both the source and target in one step","archived":false,"fork":false,"pushed_at":"2024-05-07T14:40:35.000Z","size":13,"stargazers_count":166,"open_issues_count":0,"forks_count":12,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-31T15:20:04.664Z","etag":null,"topics":["artificial-intelligence","attention-mechanism","deep-learning"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucidrains.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-27T16:20:53.000Z","updated_at":"2025-03-28T12:38:32.000Z","dependencies_parsed_at":"2024-05-07T15:48:28.460Z","dependency_job_id":"4afe60b4-1146-4bf6-a574-88fd30d5b508","html_url":"https://github.com/lucidrains/bidirectional-cross-attention","commit_stats":{"total_commits":8,"total_committers":1,"mean_commits":8.0,"dds":0.0,"last_synced_commit":"331ed5174d13f06f33306b6c039efd3763c29e66"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fbidirectional-cross-attention","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fbidirectional-cross-attention/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fbidirectional-cross-attention/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fbidirectional-cross-attention/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucidrains","download_url":"https://codeload.github.com/lucidrains/bidirectional-cross-attention/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247713258,"owners_count":20983683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","attention-mechanism","deep-learning"],"created_at":"2024-10-03T02:11:14.899Z","updated_at":"2025-04-07T19:16:09.696Z","avatar_url":"https://github.com/lucidrains.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Bidirectional Cross Attention\n\nA simple cross attention that updates both the source and target in one step. The key insight is that one can do \u003ca href=\"https://arxiv.org/abs/2001.04451\"\u003eshared query / key attention\u003c/a\u003e and use the attention matrix twice to update both ways. Used for a contracting project for predicting DNA / protein binding \u003ca href=\"https://github.com/lucidrains/tf-bind-transformer\"\u003ehere\u003c/a\u003e.\n\n## Install\n\n```bash\n$ pip install bidirectional-cross-attention\n```\n\n## Usage\n\n```python\nimport torch\nfrom bidirectional_cross_attention import BidirectionalCrossAttention\n\nvideo = torch.randn(1, 4096, 512)\naudio = torch.randn(1, 8192, 386)\n\nvideo_mask = torch.ones((1, 4096)).bool()\naudio_mask = torch.ones((1, 8192)).bool()\n\njoint_cross_attn = BidirectionalCrossAttention(\n    dim = 512,\n    heads = 8,\n    dim_head = 64,\n    context_dim = 386\n)\n\nvideo_out, audio_out = joint_cross_attn(\n    video,\n    audio,\n    mask = video_mask,\n    context_mask = audio_mask\n)\n\n# attended output should have the same shape as input\n\nassert video_out.shape == video.shape\nassert audio_out.shape == audio.shape\n```\n\n## Todo\n\n- [ ] allow for cosine sim attention\n\n## Citations\n\n```bibtex\n@article{Hiller2024PerceivingLS,\n    title   = {Perceiving Longer Sequences With Bi-Directional Cross-Attention Transformers},\n    author  = {Markus Hiller and Krista A. Ehinger and Tom Drummond},\n    journal = {ArXiv},\n    year    = {2024},\n    volume  = {abs/2402.12138},\n    url     = {https://api.semanticscholar.org/CorpusID:267751060}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fbidirectional-cross-attention","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucidrains%2Fbidirectional-cross-attention","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fbidirectional-cross-attention/lists"}