Add files using upload-large-folder tool
Browse filesFiles changed (21)
- LICENSEAdded+75
- LLAMA3_LICENSEAdded+117
- README.mdAdded+108
- README_zh.mdAdded+85
- config.jsonAdded+40
- configuration.jsonAdded+1
- configuration_cogvlm.pyAdded+46
- generation_config.jsonAdded+11
- model-00001-of-00006.safetensorsAdded+3
- model-00002-of-00006.safetensorsAdded+3
- model-00003-of-00006.safetensorsAdded+3
- model-00004-of-00006.safetensorsAdded+3
- model-00005-of-00006.safetensorsAdded+3
- model-00006-of-00006.safetensorsAdded+3
- model.safetensors.index.jsonAdded
- modeling_cogvlm.pyAdded+841
- special_tokens_map.jsonAdded+4
- tokenizer.jsonAdded
- tokenizer_config.jsonAdded+2064
- util.pyAdded+472
- visual.pyAdded+177
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475The CogVLM License
1. Definitions
“Licensor” means the CogVLM Model Team that distributes its Software.
“Software” means the CogVLM model parameters made available under this license.
2. License Grant
Under the terms and conditions of this license, the Licensor hereby grants you a non-exclusive, worldwide, non-transferable, non-sublicensable, revocable, royalty-free copyright license.This license permits you to use all open-source models in this repository for academic research free. Users who wish to use the models for commercial purposes must register [here](https://open.bigmodel.cn/mla/form).Registered users may use the models for commercial activities free of charge, but must comply with all terms and conditions of this license.The license notice shall be included in all copies or substantial portions of the Software.
3. Restriction
You will not use, copy, modify, merge, publish, distribute, reproduce, or create derivative works of the Software, in whole or in part, for any military, or illegal purposes.
You will not use the Software for any act that may undermine China's national security and national unity, harm the public interest of society, or infringe upon the rights and interests of human beings.
4. Disclaimer
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5. Limitation of Liability
EXCEPT TO THE EXTENT PROHIBITED BY APPLICABLE LAW, IN NO EVENT AND UNDER NO LEGAL THEORY, WHETHER BASED IN TORT, NEGLIGENCE, CONTRACT, LIABILITY, OR OTHERWISE WILL ANY LICENSOR BE LIABLE TO YOU FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES, OR ANY OTHER COMMERCIAL LOSSES, EVEN IF THE LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
6. Dispute Resolution
This license shall be governed and construed in accordance with the laws of People’s Republic of China. Any dispute arising from or in connection with this License shall be submitted to Haidian District People's Court in Beijing.
Note that the license is subject to update to a more comprehensive version. For any questions related to the license and copyright, please contact us at license@zhipuai.cn.
7. Llama3 and EVA-CLIP2 License
For the CogVLM2 open source model based on the LLama3 series model as the base model, the Llama3 license conditions (https://llama.meta.com/llama3/license/, a copy of this repository license conditions) and the EVA-CLIP2 license conditions (MIT , https://github.com/baaivision/EVA/blob/master/LICENSE) for model weights.
1. 定义
“许可方”是指分发其软件的 CogVLM 模型团队。
“软件”是指根据本许可提供的 CogVLM 模型参数。
2. 许可授予
根据本许可的条款和条件,许可方特此授予您非排他性、全球性、不可转让、不可再许可、可撤销、免版税的版权许可。本许可允许您免费使用本仓库中的所有开源模型进行学术研究,对于希望将模型用于商业目的的用户,需在[这里](https://open.bigmodel.cn/mla/form)完成登记。经过登记的用户可以免费使用本模型进行商业活动,但必须遵守本许可的所有条款和条件。上述版权声明和本许可声明应包含在本软件的所有副本或重要部分中。
3.限制
您不得出于任何军事或非法目的使用、复制、修改、合并、发布、分发、复制或创建本软件的全部或部分衍生作品。
您不得利用本软件从事任何危害国家安全和国家统一、危害社会公共利益、侵犯人身权益的行为。
4.免责声明
本软件“按原样”提供,不提供任何明示或暗示的保证,包括但不限于对适销性、特定用途的适用性和非侵权性的保证。 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是在合同诉讼、侵权行为还是其他方面,由软件或软件的使用或其他交易引起、由软件引起或与之相关 软件。
5. 责任限制
除适用法律禁止的范围外,在任何情况下且根据任何法律理论,无论是基于侵权行为、疏忽、合同、责任或其他原因,任何许可方均不对您承担任何直接、间接、特殊、偶然、示范性、 或间接损害,或任何其他商业损失,即使许可人已被告知此类损害的可能性。
6.争议解决
本许可受中华人民共和国法律管辖并按其解释。 因本许可引起的或与本许可有关的任何争议应提交北京市海淀区人民法院。
请注意,许可证可能会更新到更全面的版本。 有关许可和版权的任何问题,请���过 license@zhipuai.cn 与我们联系。
7. Llama3 和 EVA-CLIP2 许可
针对基于以 LLama3 系列模型作为基座模型的 CogVLM2 开源模型, Llama3 许可条件 (https://llama.meta.com/llama3/license/ ,本仓库副本一份许可条件) 和 EVA-CLIP2 许可条件 (MIT, https://github.com/baaivision/EVA/blob/master/LICENSE) 适用于模型权重。123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117META LLAMA 3 COMMUNITY LICENSE AGREEMENTMeta Llama 3 Version Release Date: April 18, 2024
“Agreement” means the terms and conditions for use, reproduction, distribution and modification of theLlama Materials set forth herein.
“Documentation” means the specifications, manuals and documentation accompanying Meta Llama 3distributed by Meta at https://llama.meta.com/get-started/.
“Licensee” or “you” means you, or your employer or any other person or entity (if you are entering intothis Agreement on such person or entity’s behalf), of the age required under applicable laws, rules orregulations to provide legal consent and that has legal authority to bind your employer or such otherperson or entity if you are entering in this Agreement on their behalf.
“Meta Llama 3” means the foundational large language models and software and algorithms, includingmachine-learning model code, trained model weights, inference-enabling code, training-enabling code,fine-tuning enabling code and other elements of the foregoing distributed by Meta athttps://llama.meta.com/llama-downloads.
“Llama Materials” means, collectively, Meta’s proprietary Meta Llama 3 and Documentation (and anyportion thereof) made available under this Agreement.
“Meta” or “we” means Meta Platforms Ireland Limited (if you are located in or, if you are an entity, yourprincipal place of business is in the EEA or Switzerland) and Meta Platforms, Inc. (if you are locatedoutside of the EEA or Switzerland).
By clicking “I Accept” below or by using or distributing any portion or element of the Llama Materials,you agree to be bound by this Agreement.
1. License Rights and Redistribution.
a. Grant of Rights. You are granted a non-exclusive, worldwide, non-transferable and royalty-freelimited license under Meta’s intellectual property or other rights owned by Meta embodied in the LlamaMaterials to use, reproduce, distribute, copy, create derivative works of, and make modifications to theLlama Materials.
b. Redistribution and Use.
i. If you distribute or make available the Llama Materials (or any derivative worksthereof), or a product or service that uses any of them, including another AI model, you shall (A) providea copy of this Agreement with any such Llama Materials; and (B) prominently display “Built with MetaLlama 3” on a related website, user interface, blogpost, about page, or product documentation. If youuse the Llama Materials to create, train, fine tune, or otherwise improve an AI model, which isdistributed or made available, you shall also include “Llama 3” at the beginning of any such AI modelname.
ii. If you receive Llama Materials, or any derivative works thereof, from a Licensee as part of an integrated end user product, then Section 2 of this Agreement will not apply to you.
iii. You must retain in all copies of the Llama Materials that you distribute the followingattribution notice within a “Notice” text file distributed as a part of such copies: “Meta Llama 3 islicensed under the Meta Llama 3 Community License, Copyright © Meta Platforms, Inc. All RightsReserved.”
iv. Your use of the Llama Materials must comply with applicable laws and regulations(including trade compliance laws and regulations) and adhere to the Acceptable Use Policy for the LlamaMaterials (available at https://llama.meta.com/llama3/use-policy), which is hereby incorporated byreference into this Agreement.
v. You will not use the Llama Materials or any output or results of the Llama Materials toimprove any other large language model (excluding Meta Llama 3 or derivative works thereof).
2. Additional Commercial Terms. If, on the Meta Llama 3 version release date, the monthly active usersof the products or services made available by or for Licensee, or Licensee’s affiliates, is greater than 700million monthly active users in the preceding calendar month, you must request a license from Meta,which Meta may grant to you in its sole discretion, and you are not authorized to exercise any of therights under this Agreement unless or until Meta otherwise expressly grants you such rights.
3. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE LLAMA MATERIALS AND ANYOUTPUT AND RESULTS THEREFROM ARE PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OFANY KIND, AND META DISCLAIMS ALL WARRANTIES OF ANY KIND, BOTH EXPRESS AND IMPLIED,INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE, NON-INFRINGEMENT,MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE FORDETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE LLAMA MATERIALS ANDASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE LLAMA MATERIALS AND ANY OUTPUT ANDRESULTS.
4. Limitation of Liability. IN NO EVENT WILL META OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISINGOUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY INDIRECT, SPECIAL, CONSEQUENTIAL,INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF META OR ITS AFFILIATES HAVE BEEN ADVISEDOF THE POSSIBILITY OF ANY OF THE FOREGOING.
5. Intellectual Property.
a. No trademark licenses are granted under this Agreement, and in connection with the LlamaMaterials, neither Meta nor Licensee may use any name or mark owned by or associated with the otheror any of its affiliates, except as required for reasonable and customary use in describing andredistributing the Llama Materials or as set forth in this Section 5(a). Meta hereby grants you a license touse “Llama 3” (the “Mark”) solely as required to comply with the last sentence of Section 1.b.i. You willcomply with Meta’s brand guidelines (currently accessible athttps://about.meta.com/brand/resources/meta/company-brand/ ). All goodwill arising out of your useof the Mark will inure to the benefit of Meta.
b. Subject to Meta’s ownership of Llama Materials and derivatives made by or for Meta, withrespect to any derivative works and modifications of the Llama Materials that are made by you, asbetween you and Meta, you are and will be the owner of such derivative works and modifications.
c. If you institute litigation or other proceedings against Meta or any entity (including across-claim or counterclaim in a lawsuit) alleging that the Llama Materials or Meta Llama 3 outputs orresults, or any portion of any of the foregoing, constitutes infringement of intellectual property or otherrights owned or licensable by you, then any licenses granted to you under this Agreement shallterminate as of the date such litigation or claim is filed or instituted. You will indemnify and holdharmless Meta from and against any claim by any third party arising out of or related to your use ordistribution of the Llama Materials.
6. Term and Termination. The term of this Agreement will commence upon your acceptance of thisAgreement or access to the Llama Materials and will continue in full force and effect until terminated inaccordance with the terms and conditions herein. Meta may terminate this Agreement if you are inbreach of any term or condition of this Agreement. Upon termination of this Agreement, you shall deleteand cease use of the Llama Materials. Sections 3, 4 and 7 shall survive the termination of thisAgreement.
7. Governing Law and Jurisdiction. This Agreement will be governed and construed under the laws ofthe State of California without regard to choice of law principles, and the UN Convention on Contractsfor the International Sale of Goods does not apply to this Agreement. The courts of California shall haveexclusive jurisdiction of any dispute arising out of this Agreement.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108---license: otherlicense_name: cogvlm2license_link: https://huggingface.co/THUDM/cogvlm2-video-llama3-chat/blob/main/LICENSE
language:- enpipeline_tag: text-generationtags:- chat- cogvlm2- cogvlm--video
inference: false---
# CogVLM2-Video-Llama3-Chat
[中文版本README](README_zh.md)
## Introduction
CogVLM2-Video achieves state-of-the-art performance on multiple video question answering tasks. It can achieve videounderstanding within one minute. We provide two example videos to demonstrate CogVLM2-Video's video understanding andvideo temporal grounding capabilities.
<table> <tr> <td> <video width="100%" controls> <source src="https://github.com/THUDM/CogVLM2/raw/main/resources/videos/lion.mp4" type="video/mp4"> </video> </td> <td> <video width="100%" controls> <source src="https://github.com/THUDM/CogVLM2/raw/main/resources/videos/basketball.mp4" type="video/mp4"> </video> </td> </tr></table>
## BenchMark
The following diagram shows the performance of CogVLM2-Video onthe [MVBench](https://github.com/OpenGVLab/Ask-Anything), [VideoChatGPT-Bench](https://github.com/mbzuai-oryx/Video-ChatGPT)and Zero-shot VideoQA datasets (MSVD-QA, MSRVTT-QA, ActivityNet-QA). Where VCG-* refers to the VideoChatGPTBench, ZS-*refers to Zero-Shot VideoQA datasets and MV-* refers to main categories in the MVBench.

Performance on VideoChatGPT-Bench and Zero-shot VideoQA dataset:
| Models | VCG-AVG | VCG-CI | VCG-DO | VCG-CU | VCG-TU | VCG-CO | ZS-AVG ||-----------------------|----------|----------|----------|----------|----------|----------|-----------|| IG-VLM GPT4V | 3.17 | 3.40 | 2.80 | 3.61 | 2.89 | 3.13 | 65.70 || ST-LLM | 3.15 | 3.23 | 3.05 | 3.74 | 2.93 | 2.81 | 62.90 || ShareGPT4Video | N/A | N/A | N/A | N/A | N/A | N/A | 46.50 || VideoGPT+ | 3.28 | 3.27 | 3.18 | 3.74 | 2.83 | **3.39** | 61.20 || VideoChat2_HD_mistral | 3.10 | 3.40 | 2.91 | 3.72 | 2.65 | 2.84 | 57.70 || PLLaVA-34B | 3.32 | **3.60** | 3.20 | **3.90** | 2.67 | 3.25 | **68.10** | | CogVLM2-Video | **3.41** | 3.49 | **3.46** | 3.87 | **2.98** | 3.23 | 66.60 |
Performance on MVBench dataset:
| Models | AVG | AA | AC | AL | AP | AS | CO | CI | EN | ER | FA | FP | MA | MC | MD | OE | OI | OS | ST | SC | UA ||-----------------------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|| IG-VLM GPT4V | 43.7 | 72.0 | 39.0 | 40.5 | 63.5 | 55.5 | 52.0 | 11.0 | 31.0 | 59.0 | 46.5 | 47.5 | 22.5 | 12.0 | 12.0 | 18.5 | 59.0 | 29.5 | 83.5 | 45.0 | 73.5 || ST-LLM | 54.9 | 84.0 | 36.5 | 31.0 | 53.5 | 66.0 | 46.5 | 58.5 | 34.5 | 41.5 | 44.0 | 44.5 | 78.5 | 56.5 | 42.5 | 80.5 | 73.5 | 38.5 | 86.5 | 43.0 | 58.5 || ShareGPT4Video | 51.2 | 79.5 | 35.5 | 41.5 | 39.5 | 49.5 | 46.5 | 51.5 | 28.5 | 39.0 | 40.0 | 25.5 | 75.0 | 62.5 | 50.5 | 82.5 | 54.5 | 32.5 | 84.5 | 51.0 | 54.5 || VideoGPT+ | 58.7 | 83.0 | 39.5 | 34.0 | 60.0 | 69.0 | 50.0 | 60.0 | 29.5 | 44.0 | 48.5 | 53.0 | 90.5 | 71.0 | 44.0 | 85.5 | 75.5 | 36.0 | 89.5 | 45.0 | 66.5 || VideoChat2_HD_mistral | **62.3** | 79.5 | **60.0** | **87.5** | 50.0 | 68.5 | **93.5** | 71.5 | 36.5 | 45.0 | 49.5 | **87.0** | 40.0 | **76.0** | **92.0** | 53.0 | 62.0 | **45.5** | 36.0 | 44.0 | 69.5 || PLLaVA-34B | 58.1 | 82.0 | 40.5 | 49.5 | 53.0 | 67.5 | 66.5 | 59.0 | **39.5** | **63.5** | 47.0 | 50.0 | 70.0 | 43.0 | 37.5 | 68.5 | 67.5 | 36.5 | 91.0 | 51.5 | **79.0** || CogVLM2-Video | **62.3** | **85.5** | 41.5 | 31.5 | **65.5** | **79.5** | 58.5 | **77.0** | 28.5 | 42.5 | **54.0** | 57.0 | **91.5** | 73.0 | 48.0 | **91.0** | **78.0** | 36.0 | **91.5** | **47.0** | 68.5 |
## Evaluation details
We follow the previous works to evaluate the performance of our model. In different benchmarks, we craft task-specificprompts for each benchmark:
``` python # For MVBenchprompt = f"Carefully watch the video and pay attention to the cause and sequence of events, the detail and movement of objects, and the action and pose of persons. Based on your observations, select the best option that accurately addresses the question.\n " + f"{prompt.replace('Short Answer.', '')}\n" + "Short Answer:"# For VideoChatGPT-Benchprompt = f"Carefully watch the video and pay attention to the cause and sequence of events, the detail and movement of objects, and the action and pose of persons. Based on your observations, comprehensively answer the following question. Your answer should be long and cover all the related aspects\n " + f"{prompt.replace('Short Answer.', '')}\n" + "Answer:"# For Zero-shot VideoQAprompt = f"The input consists of a sequence of key frames from a video. Answer the question comprehensively including all the possible verbs and nouns that can discribe the events, followed by significant events, characters, or objects that appear throughout the frames.\n " + f"{prompt.replace('Short Answer.', '')}\n" + "Answer:"```
For evaluation codes, please refer tothe [evaluation script](https://github.com/magic-research/PLLaVA/blob/main/README.md) in PLLaVA.
## Using This Model
This repository is a `chat` version model and it support single-round chat.
You can quickly install the Python package dependencies and run model inference inour [github](https://github.com/THUDM/CogVLM2/tree/main/video_demo).
## License
This model is released under theCogVLM2 [LICENSE](./LICENSE).For models built with Meta Llama 3, please also adhere tothe [LLAMA3_LICENSE](./LLAMA3_LICENSE).
## Training details
Pleaser refer to our technical report for training formula and hyperparameters.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485# CogVLM2-Video-Llama3-Chat
CogVLM2-Video 在多个视频问答任务上达到了 state-of-the-art 的性能,能够实现一分钟内的视频理解。 我们提供了两个示例视频,分别展现了 CogVLM2-Video 的 视频理解和时间序列定位能力。
<table> <tr> <td> <video width="100%" controls> <source src="https://github.com/THUDM/CogVLM2/raw/main/resources/videos/lion.mp4" type="video/mp4"> </video> </td> <td> <video width="100%" controls> <source src="https://github.com/THUDM/CogVLM2/raw/main/resources/videos/basketball.mp4" type="video/mp4"> </video> </td> </tr></table>
下图显示了 CogVLM2-Video在 [MVBench](https://github.com/OpenGVLab/Ask-Anything)、[VideoChatGPT-Bench](https://github.com/mbzuai-oryx/Video-ChatGPT)和 Zero-shot VideoQA 数据集 (MSVD-QA、MSRVTT-QA、ActivityNet-QA) 上的性能。

其中 VCG 指的是 VideoChatGPTBench,ZS 指的是零样本 VideoQA 数据集,MV-* 指的是 MVBench 中的主要类别。
## 评估结论
具体榜单测试数据如下:
| Models | VCG-AVG | VCG-CI | VCG-DO | VCG-CU | VCG-TU | VCG-CO | ZS-AVG ||-----------------------|----------|----------|----------|----------|----------|----------|-----------|| IG-VLM GPT4V | 3.17 | 3.40 | 2.80 | 3.61 | 2.89 | 3.13 | 65.70 || ST-LLM | 3.15 | 3.23 | 3.05 | 3.74 | 2.93 | 2.81 | 62.90 || ShareGPT4Video | N/A | N/A | N/A | N/A | N/A | N/A | 46.50 || VideoGPT+ | 3.28 | 3.27 | 3.18 | 3.74 | 2.83 | **3.39** | 61.20 || VideoChat2_HD_mistral | 3.10 | 3.40 | 2.91 | 3.72 | 2.65 | 2.84 | 57.70 || PLLaVA-34B | 3.32 | **3.60** | 3.20 | **3.90** | 2.67 | 3.25 | **68.10** | | CogVLM2-Video | **3.41** | 3.49 | **3.46** | 3.87 | **2.98** | 3.23 | 66.60 |
CogVLM2-Video 在 MVBench 数据集上的表现
| Model | AVG | AA | AC | AL | AP | AS | CO | CI | EN | ER | FA | FP | MA | MC | MD | OE | OI | OS | ST | SC | UA ||-----------------------|----------|----------|----------|----------|----------|----------|----------|----------|-------|----------|----------|----------|----------|----------|----------|----------|----------|------|----------|------|----------|| IG-VLM GPT4V | 43.7 | 72.0 | 39.0 | 40.5 | **63.5** | 55.5 | 52.0 | 11.0 | 31.0 | 59.0 | 46.5 | 47.5 | 22.5 | 12.0 | 12.0 | 18.5 | 59.0 | 29.5 | 83.5 | 45.0 | 73.5 || ST-LLM | 54.9 | 84.0 | 36.5 | 31.0 | 53.5 | 66.0 | 46.5 | 58.5 | 34.5 | 41.5 | 44.0 | 44.5 | 78.5 | 56.5 | 42.5 | 80.5 | 73.5 | 38.5 | 86.5 | 43.0 | 58.5 || ShareGPT4Video | 51.2 | 79.5 | 35.5 | 41.5 | 39.5 | 49.5 | 46.5 | 51.5 | 28.5 | 39.0 | 40.0 | 25.5 | 75.0 | 62.5 | 50.5 | 82.5 | 54.5 | 32.5 | 84.5 | 51.0 | 54.5 || VideoGPT+ | 58.7 | 83.0 | 39.5 | 34.0 | 60.0 | **69.0** | 50.0 | 60.0 | 29.5 | 44.0 | 48.5 | 53.0 | 90.5 | 71.0 | 44.0 | **85.5** | 75.5 | 36.0 | 89.5 | 45.0 | 66.5 || VideoChat2_HD_mistral | 62.3 | 79.5 | **60.0** | **87.5** | 50.0 | 68.5 | **93.5** | 71.5 | 36.5 | 45.0 | 49.5 | **87.0** | 40.0 | **76.0** | **92.0** | 53.0 | 62.0 | 45.5 | 36.0 | 44.0 | 69.5 || PLLaVA-34B | 58.1 | 82.0 | 40.5 | 49.5 | 53.0 | 67.5 | 66.5 | 59.0 | l39.5 | **63.5** | 47.0 | 50.0 | 70.0 | 43.0 | 37.5 | 68.5 | 67.5 | 36.5 | **91.0** | 51.5 | **79.0** || CogVLM2-Video | **62.3** | **85.5** | 41.5 | 31.5 | 65.5 | 79.5 | 58.5 | **77.0** | 28.5 | 42.5 | **54.0** | 57.0 | **91.5** | 73.0 | 48.0 | **91.0** | **78.0** | 36.0 | **91.5** | 47.0 | 68.5 |
## 评估和复现
我们遵循以前的研究来评估我们模型的性能。在不同的基准测试中,我们为每个基准测试制作特定于任务的提示:
``` python # For MVBenchprompt = f"Carefully watch the video and pay attention to the cause and sequence of events, the detail and movement of objects, and the action and pose of persons. Based on your observations, select the best option that accurately addresses the question.\n " + f"{prompt.replace('Short Answer.', '')}\n" + "Short Answer:"# For VideoChatGPT-Benchprompt = f"Carefully watch the video and pay attention to the cause and sequence of events, the detail and movement of objects, and the action and pose of persons. Based on your observations, comprehensively answer the following question. Your answer should be long and cover all the related aspects\n " + f"{prompt.replace('Short Answer.', '')}\n" + "Answer:"# For Zero-shot VideoQAprompt = f"The input consists of a sequence of key frames from a video. Answer the question comprehensively including all the possible verbs and nouns that can discribe the events, followed by significant events, characters, or objects that appear throughout the frames.\n " + f"{prompt.replace('Short Answer.', '')}\n" + "Answer:"```
有关评估代码,请参阅 PLLaVA 中的 [评估脚本](https://github.com/magic-research/PLLaVA/blob/main/README.md)。
## 快速调用
本仓库为 `chat` 版本模型,支持单轮对话。
您可以在我们的 [github](https://github.com/THUDM/CogVLM2/tree/main/video_demo) 中快速安装对应的 Python包 依赖和运行模型推理。
## 模型协议
此模型根据CogVLM2 [LICENSE](./LICENSE)发布。对于使用 Meta Llama 3 构建的模型,还请遵守[LLAMA3_LICENSE](./LLAMA3_LICENSE)。## 引用
我们即将发布技术报告,尽情期待。12345678910111213141516171819202122232425262728293031323334353637383940{ "architectures": [ "CogVLMVideoForCausalLM" ], "auto_map": { "AutoConfig": "configuration_cogvlm.CogVLMConfig", "AutoModelForCausalLM": "modeling_cogvlm.CogVLMVideoForCausalLM" }, "bos_token_id": 128000, "eos_token_id": 128001, "pad_token_id": 128002, "hidden_act": "silu", "hidden_size": 4096, "initializer_range": 0.02, "intermediate_size": 14336, "max_position_embeddings": 2048, "num_attention_heads": 32, "num_hidden_layers": 32, "num_multi_query_heads": 8, "rms_norm_eps": 1e-05, "template_version": "base", "tie_word_embeddings": false, "torch_dtype": "bfloat16", "transformers_version": "4.43.1", "use_cache": true, "vision_config": { "dropout_prob": 0.0, "hidden_act": "gelu", "hidden_size": 1792, "image_size": 224, "in_channels": 3, "intermediate_size": 15360, "layer_norm_eps": 1e-06, "num_heads": 16, "num_hidden_layers": 63, "num_positions": 257, "patch_size": 14 }, "vocab_size": 128256}1{"framework":"Pytorch","task":"video-question-answering"}12345678910111213141516171819202122232425262728293031323334353637383940414243444546from typing import Literal
from transformers import PretrainedConfig
class CogVLMConfig(PretrainedConfig):
_auto_class = "AutoConfig"
def __init__(
self,
vocab_size=32000,
hidden_size=4096,
intermediate_size=11008,
num_hidden_layers=32,
num_attention_heads=32,
num_multi_query_heads=8,
hidden_act='silu',
max_position_embeddings=2048,
initializer_range=0.02,
rms_norm_eps=1e-06,
template_version: Literal["base", "chat"] = "chat",
pad_token_id=128002,
bos_token_id=128001,
eos_token_id=128002,
tie_word_embeddings=False,
use_cache=True,
**kwargs,
):
self.hidden_size = hidden_size
self.intermediate_size = intermediate_size
self.num_attention_heads = num_attention_heads
self.num_multi_query_heads = num_multi_query_heads
self.max_position_embeddings = max_position_embeddings
self.rms_norm_eps = rms_norm_eps
self.initializer_range = initializer_range
self.vocab_size = vocab_size
self.num_hidden_layers = num_hidden_layers
self.hidden_act = hidden_act
self.template_version = template_version
self.use_cache = use_cache
super().__init__(
pad_token_id=pad_token_id,
bos_token_id=bos_token_id,
eos_token_id=eos_token_id,
tie_word_embeddings=tie_word_embeddings,
**kwargs,
)1234567891011{ "bos_token_id": 128000, "eos_token_id": 128001, "pad_token_id": 128002, "do_sample": true, "temperature": 0.1, "max_length": 2048, "top_p": 0.1, "top_k": 1, "transformers_version": "4.43.1"}123version https://git-lfs.github.com/spec/v1oid sha256:f53e34de951ee7205398dc1e03a8a0b797ddc4b7a6714181adb51b2a906a94c5size 4976699712123version https://git-lfs.github.com/spec/v1oid sha256:555733b658ef1733b5afa979a6c999d2f3310ac8e2f851eb6c53b9b73a10c4a9size 4999803504123version https://git-lfs.github.com/spec/v1oid sha256:d90e87d1db7f29deb7a59817476da9bba04339cd3c7029d3fc4ca97c6aa4b776size 4915917160123version https://git-lfs.github.com/spec/v1oid sha256:cf5581fa2fb9be9bf89ded6c82607fec36c809e200b00ce10893e80501036a25size 4956242104123version https://git-lfs.github.com/spec/v1oid sha256:40c0ca93426ca37f04b66d9e1e6cb9687e0bcbaa02b4827f282b564577b2b6dasize 4115863248123version https://git-lfs.github.com/spec/v1oid sha256:73b20e2d92146829390d468bb37bb9bacf23df3e22f6000754cd94c1dfc83f23size 1050673280123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841"""largely copy from llama and adapt for cogvlm"""
import warnings
from typing import TYPE_CHECKING, Optional, Tuple, List, Union, Literal, Dict, Any
import math
import torch
from torch import nn
from torch.nn import CrossEntropyLoss
from torchvision import transforms
from einops import rearrange
from transformers import PreTrainedModel, PreTrainedTokenizer
from transformers.utils.logging import get_logger
from transformers.activations import ACT2FN
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
from torchvision.transforms import Lambda
from torchvision.transforms._transforms_video import NormalizeVideo, CenterCropVideo
from pytorchvideo.transforms import ShortSideScale
from .configuration_cogvlm import CogVLMConfig
from .util import FastRotaryEmbedding
from .visual import EVA2CLIPModel
if TYPE_CHECKING:
from transformers.utils import ModelOutput
logger = get_logger(__name__)
LANGUAGE_TOKEN_TYPE = 0
VISION_TOKEN_TYPE = 1
# Copied from transformers.models.bart.modeling_bart._make_causal_mask
def _make_causal_mask(
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
):
"""
Make causal mask used for bi-directional self-attention.
"""
bsz, tgt_len = input_ids_shape
mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device)
mask_cond = torch.arange(mask.size(-1), device=device)
mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
mask = mask.to(dtype)
if past_key_values_length > 0:
mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
# Copied from transformers.models.bart.modeling_bart._expand_mask
def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
"""
Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
"""
bsz, src_len = mask.size()
tgt_len = tgt_len if tgt_len is not None else src_len
expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
inverted_mask = 1.0 - expanded_mask
return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
class RMSNorm(nn.Module):
def __init__(self, hidden_size, eps=1e-5):
super().__init__()
self.weight = nn.Parameter(torch.ones(hidden_size))
self.variance_epsilon = eps
def forward(self, hidden_states):
input_dtype = hidden_states.dtype
hidden_states = hidden_states.to(torch.float32)
variance = hidden_states.pow(2).mean(-1, keepdim=True)
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
return (self.weight * hidden_states).to(input_dtype)
class MLP(nn.Module):
def __init__(self, config):
super().__init__()
self.hidden_size = config.hidden_size
self.intermediate_size = config.intermediate_size
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
self.act_fn = ACT2FN[config.hidden_act]
def forward(self, x):
down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
return down_proj
def get_expert_mask(token_type_ids: "torch.LongTensor(B, L)") -> "[torch.BoolTensor(B, L), torch.BoolTensor(B, L)]":
vision_token_mask = torch.zeros_like(token_type_ids, dtype=torch.bool)
vision_token_mask[:, :-1] = (token_type_ids[:, :-1] == VISION_TOKEN_TYPE) & (
token_type_ids[:, 1:] == VISION_TOKEN_TYPE)
language_token_mask = ~vision_token_mask
return vision_token_mask, language_token_mask
class VisionExpertMLP(nn.Module):
def __init__(self, config):
super().__init__()
self.language_mlp = MLP(config)
# self.vision_mlp = MLP(config)
def forward(self, hidden_states: "torch.Tensor(B, L, D)", token_type_ids: "torch.LongTensor(B, L)"):
# output = torch.empty(hidden_states.shape, dtype=hidden_states.dtype, device=hidden_states.device)
# vision_token_mask, language_token_mask = get_expert_mask(token_type_ids)
# output[vision_token_mask] = self.vision_mlp(hidden_states[vision_token_mask])
# output[language_token_mask] = self.language_mlp(hidden_states[language_token_mask])
output = self.language_mlp(hidden_states)
return output
def attention_fn(
query_layer: "torch.tensor(B, H, L, HD)",
key_layer: "torch.tensor(B, H, L, HD)",
value_layer: "torch.tensor(B, H, L, HD)",
attention_mask: "torch.tensor(B, H, L, HD)",
*,
scaling_attention_score: bool = True,
attention_dropout: nn.Module = None
):
attention_mask_bool = (attention_mask == 0)
is_low_triangle = (attention_mask_bool == torch.ones_like(attention_mask_bool, dtype=torch.float).tril()).all()
is_full = (attention_mask_bool > 0).all()
if not (int(torch.__version__.split('.')[0]) >= 2):
warnings.warn("It's recommended to use torch2.0 or higher.")
if int(torch.__version__.split('.')[0]) >= 2 and scaling_attention_score and (is_full or is_low_triangle):
dropout_p = 0. if attention_dropout is None or not attention_dropout.training else attention_dropout.p
return torch.nn.functional.scaled_dot_product_attention(
query_layer, key_layer, value_layer,
attn_mask=None,
dropout_p=dropout_p,
is_causal=not is_full
)
else:
if scaling_attention_score:
query_layer = query_layer / math.sqrt(query_layer.shape[-1])
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
attention_scores = attention_scores + attention_mask
attention_scores = nn.functional.softmax(attention_scores, dim=-1, dtype=torch.float32).to(query_layer.dtype)
if attention_dropout is not None:
attention_scores = attention_dropout(attention_scores)
context_layer = torch.matmul(attention_scores, value_layer)
return context_layer
class VisionExpertAttention(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
self.hidden_size = config.hidden_size
self.num_attention_heads = config.num_attention_heads
self.num_multi_query_heads = config.num_multi_query_heads
self.hidden_size_per_attention_head = self.hidden_size // self.num_attention_heads
self.stride = [self.num_attention_heads, self.num_multi_query_heads, self.num_multi_query_heads]
self.qkv_size = self.hidden_size + self.hidden_size_per_attention_head * self.num_multi_query_heads * 2
self.head_dim = self.hidden_size // self.num_attention_heads
self.max_position_embeddings = config.max_position_embeddings
self.rotary_emb = FastRotaryEmbedding(dim=self.head_dim, pos_idx_in_fp32=False, base=500000)
# self.vision_expert_query_key_value = nn.Linear(self.hidden_size, self.qkv_size, bias=True)
# self.vision_expert_dense = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
self.language_expert_query_key_value = nn.Linear(self.hidden_size, self.qkv_size, bias=False)
self.language_expert_dense = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
def _transpose_for_scores(self, tensor):
"""Transpose a 3D tensor [B, L, H*HD] into a 4D tensor with size [B H L HD]."""
new_tensor_shape = tensor.size()[:-1] + \
(-1, # flexible for multi-query
self.hidden_size_per_attention_head)
tensor = tensor.view(*new_tensor_shape)
return tensor.permute(0, 2, 1, 3)
def forward(
self,
hidden_states: torch.Tensor,
token_type_ids: torch.LongTensor,
position_ids: torch.LongTensor,
attention_mask: Optional[torch.Tensor] = None,
past_key_value: Optional[Tuple[torch.Tensor]] = None,
output_attentions: bool = False,
use_cache: bool = False,
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
bsz, q_len, _ = hidden_states.size()
# vision_token_mask, language_token_mask = get_expert_mask(token_type_ids)
shape = list(hidden_states.shape)
shape[-1] = self.qkv_size
# mixed_raw_layer = torch.empty(shape, dtype=hidden_states.dtype, device=hidden_states.device)
# mixed_raw_layer[vision_token_mask] = self.vision_expert_query_key_value(hidden_states[vision_token_mask])
# mixed_raw_layer[language_token_mask] = self.language_expert_query_key_value(hidden_states[language_token_mask])
mixed_raw_layer = self.language_expert_query_key_value(hidden_states)
# query_states, key_states, value_states = torch.split(mixed_raw_layer, self.hidden_size, dim=-1)
factor = mixed_raw_layer.size()[-1] // sum(self.stride)
query_states, key_states, value_states = torch.split(mixed_raw_layer, [factor * x for x in self.stride], dim=-1)
query_states = self._transpose_for_scores(query_states) # B, H, L, HD
key_states = self._transpose_for_scores(key_states) # B, H, L, HD
value_states = self._transpose_for_scores(value_states) # B, H, L, HD
kv_seq_len = key_states.shape[-2]
if past_key_value is not None:
kv_seq_len += past_key_value[0].shape[-2]
query_states, key_states = self.rotary_emb(query_states, key_states, position_ids=position_ids,
max_seqlen=position_ids.max() + 1)
if past_key_value is not None:
key_states = torch.cat([past_key_value[0], key_states], dim=2)
value_states = torch.cat([past_key_value[1], value_states], dim=2)
past_key_value = (key_states, value_states) if use_cache else None
key_states = key_states.unsqueeze(2).expand(-1, -1, self.num_attention_heads // self.num_multi_query_heads, -1,
-1).contiguous().view(
bsz, self.num_attention_heads, *key_states.shape[2:])
value_states = value_states.unsqueeze(2).expand(-1, -1, self.num_attention_heads // self.num_multi_query_heads,
-1,
-1).contiguous().view(bsz, self.num_attention_heads,
*value_states.shape[2:])
context_layer = attention_fn(
query_layer=query_states, key_layer=key_states, value_layer=value_states, attention_mask=attention_mask,
scaling_attention_score=True, attention_dropout=None)
if context_layer.size() != (bsz, self.num_attention_heads, q_len, self.head_dim):
raise ValueError(
f"`attn_output` should be of size {(bsz, self.num_attention_heads, q_len, self.head_dim)}, but is"
f" {context_layer.size()}"
)
context_layer = context_layer.transpose(1, 2).contiguous().reshape(bsz, q_len, self.hidden_size)
# attn_output = torch.empty(context_layer.shape, dtype=hidden_states.dtype, device=hidden_states.device)
# attn_output[vision_token_mask] = self.vision_expert_dense(context_layer[vision_token_mask])
# attn_output[language_token_mask] = self.language_expert_dense(context_layer[language_token_mask])
attn_output = self.language_expert_dense(context_layer)
if output_attentions:
warnings.warn("output_attentions is not implemented.")
return attn_output, None, past_key_value
class CogVLMDecoderLayer(nn.Module):
def __init__(self, config):
super().__init__()
self.hidden_size = config.hidden_size
self.self_attn = VisionExpertAttention(config=config)
self.mlp = VisionExpertMLP(config)
self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.post_attention_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
def forward(
self,
hidden_states: torch.Tensor,
token_type_ids: torch.LongTensor,
position_ids: torch.LongTensor,
attention_mask: Optional[torch.Tensor] = None,
past_key_value: Optional[Tuple[torch.Tensor]] = None,
output_attentions: Optional[bool] = False,
use_cache: Optional[bool] = False,
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
residual = hidden_states
hidden_states = self.input_layernorm(hidden_states)
# Self Attention
hidden_states, self_attn_weights, present_key_value = self.self_attn(
hidden_states=hidden_states,
token_type_ids=token_type_ids,
position_ids=position_ids,
attention_mask=attention_mask,
past_key_value=past_key_value,
output_attentions=output_attentions,
use_cache=use_cache,
)
hidden_states = residual + hidden_states
# Fully Connected
residual = hidden_states
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states, token_type_ids=token_type_ids)
hidden_states = residual + hidden_states
outputs = (hidden_states,)
if output_attentions:
outputs += (self_attn_weights,)
if use_cache:
outputs += (present_key_value,)
return outputs # type: ignore
class CogVLMPreTrainedModel(PreTrainedModel):
config_class = CogVLMConfig
base_model_prefix = "model"
supports_gradient_checkpointing = False
_no_split_modules = ["CogVLMDecoderLayer"]
_skip_keys_device_placement = "past_key_values"
def _init_weights(self, module):
std = self.config.initializer_range
if isinstance(module, nn.Linear):
module.weight.data.normal_(mean=0.0, std=std)
if module.bias is not None:
module.bias.data.zero_()
elif isinstance(module, nn.Embedding):
module.weight.data.normal_(mean=0.0, std=std)
if module.padding_idx is not None:
module.weight.data[module.padding_idx].zero_()
def is_empty(images_list: Optional[List[List[torch.Tensor]]]):
if images_list is None or len(images_list) == 0:
return True
for image_list in images_list:
if len(image_list):
return False
return True
def build_position_ids(x: "torch.BoolTensor(B, L)",
attention_mask: Optional["torch.BoolTensor(B, L)"] = None) -> "torch.LongTensor(B, L)":
if attention_mask is not None:
tmp = x.clone()
tmp[~(attention_mask.bool())] = -1
else:
tmp = x.clone()
# image boi eoi token as LANGUAGE_TOKEN_TYPE
is_boi_eoi = torch.zeros_like(x, dtype=torch.bool)
is_boi_eoi[:, 1:] |= (tmp[:, 1:] == VISION_TOKEN_TYPE) & (tmp[:, :-1] == LANGUAGE_TOKEN_TYPE)
is_boi_eoi[:, 0] |= (tmp[:, 0] == VISION_TOKEN_TYPE)
is_boi_eoi[:, :-1] |= (tmp[:, :-1] == VISION_TOKEN_TYPE) & (tmp[:, 1:] == LANGUAGE_TOKEN_TYPE)
is_boi_eoi[:, -1] |= (tmp[:, -1] == VISION_TOKEN_TYPE)
tmp[is_boi_eoi] = LANGUAGE_TOKEN_TYPE
# final position ids
y = torch.zeros_like(x, dtype=torch.long)
y[:, 1:] = (tmp[:, 1:] == LANGUAGE_TOKEN_TYPE) | (
(tmp[:, 1:] == VISION_TOKEN_TYPE) & (tmp[:, :-1] == LANGUAGE_TOKEN_TYPE))
y = y.cumsum(dim=-1)
return y
class CogVLMVideoModel(CogVLMPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.padding_idx = 128002
self.vocab_size = config.vocab_size
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
self.layers = nn.ModuleList([CogVLMDecoderLayer(config) for _ in range(config.num_hidden_layers)])
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.vision = EVA2CLIPModel(config)
self.gradient_checkpointing = False
# Initialize weights and apply final processing
self.post_init()
def encode_images(self, images: List[List[torch.Tensor]], ) -> torch.Tensor:
images_list, images = images, []
images = []
for image_list in images_list:
for image in image_list:
images.append(image)
# images = torch.stack(images) # video images is already stacked
images_features = self.vision(images[0])
return images_features
def forward(
self,
input_ids: torch.LongTensor = None,
images: List[List[torch.Tensor]] = None,
token_type_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple, BaseModelOutputWithPast]:
"""take care of image_encode, token_type_ids, position_ids and (attention_mask = None is fine)"""
if past_key_values is not None:
pass # generate mode with past_key_values. the image features are already mapped
else:
# not allow for inputs_embeds, because we want to process image feature
assert input_ids is not None and inputs_embeds is None, f"{input_ids} {inputs_embeds}"
if not is_empty(images): # multi-modality
assert token_type_ids is not None, f"multi-modality requires `token_type_ids`!"
assert len(input_ids) == len(images), f"{len(input_ids)} {len(images)}"
inputs_embeds = self.embed_tokens(input_ids)
images_features = self.encode_images(images)
images_features = rearrange(images_features, 'b n d -> (b n) d')
images_features = images_features.to(dtype=inputs_embeds.dtype, device=inputs_embeds.device)
inputs_embeds = inputs_embeds.index_put([token_type_ids == VISION_TOKEN_TYPE], images_features)
else: # single-modality
if token_type_ids is None:
token_type_ids = torch.ones_like(input_ids, dtype=torch.long,
device=input_ids.device) * LANGUAGE_TOKEN_TYPE
assert not (token_type_ids == VISION_TOKEN_TYPE).any(), f"{(token_type_ids == VISION_TOKEN_TYPE).sum()}"
inputs_embeds = self.embed_tokens(input_ids)
if position_ids is None:
position_ids = build_position_ids(token_type_ids, attention_mask)
input_ids = None
return self.llm_forward(
input_ids=input_ids,
token_type_ids=token_type_ids,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
def llm_forward(
self,
input_ids: torch.LongTensor = None,
token_type_ids: torch.LongTensor = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple, BaseModelOutputWithPast]:
"""largely copy from llama forward and adapt for cogvlm with `token_type_ids`"""
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
use_cache = use_cache if use_cache is not None else self.config.use_cache
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
# retrieve input_ids and inputs_embeds
if input_ids is not None and inputs_embeds is not None:
raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
elif input_ids is not None:
batch_size, seq_length = input_ids.shape
elif inputs_embeds is not None:
batch_size, seq_length, _ = inputs_embeds.shape
else:
raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
seq_length_with_past = seq_length
past_key_values_length = 0
if past_key_values is not None:
past_key_values_length = past_key_values[0][0].shape[2]
seq_length_with_past = seq_length_with_past + past_key_values_length
if position_ids is None:
device = input_ids.device if input_ids is not None else inputs_embeds.device
position_ids = torch.arange(
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
)
position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
else:
position_ids = position_ids.view(-1, seq_length).long()
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids)
# embed positions
if attention_mask is None:
attention_mask = torch.ones(
(batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device
)
attention_mask = self._prepare_decoder_attention_mask(
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
)
hidden_states = inputs_embeds
# decoder layers
all_hidden_states = () if output_hidden_states else None
all_self_attns = () if output_attentions else None
next_decoder_cache = () if use_cache else None
for idx, decoder_layer in enumerate(self.layers):
if output_hidden_states:
all_hidden_states += (hidden_states,)
past_key_value = past_key_values[idx] if past_key_values is not None else None
layer_outputs = decoder_layer(
hidden_states,
token_type_ids=token_type_ids,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_value=past_key_value,
output_attentions=output_attentions,
use_cache=use_cache,
)
hidden_states = layer_outputs[0]
if use_cache:
next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
if output_attentions:
all_self_attns += (layer_outputs[1],)
hidden_states = self.norm(hidden_states)
# add hidden states from the last decoder layer
if output_hidden_states:
all_hidden_states += (hidden_states,)
next_cache = next_decoder_cache if use_cache else None
if not return_dict:
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
return BaseModelOutputWithPast(
last_hidden_state=hidden_states,
past_key_values=next_cache,
hidden_states=all_hidden_states,
attentions=all_self_attns,
)
def get_input_embeddings(self):
return self.embed_tokens
def set_input_embeddings(self, value):
self.embed_tokens = value
# noinspection PyMethodMayBeStatic
# Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask
def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
# create causal mask
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
combined_attention_mask = None
if input_shape[-1] > 1:
combined_attention_mask = _make_causal_mask(
input_shape,
inputs_embeds.dtype,
device=inputs_embeds.device,
past_key_values_length=past_key_values_length,
)
if attention_mask is not None:
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
inputs_embeds.device
)
combined_attention_mask = (
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
)
return combined_attention_mask
def _history_to_prompt(signal_type, history, query):
if signal_type == 'base':
return query
elif signal_type == 'vqa':
answer_format = 'Short answer:'
elif signal_type == 'chat':
answer_format = 'Answer:'
else:
assert False, f"Unknown signal type {signal_type}"
prompt = ''
for i, (old_query, response) in enumerate(history):
prompt += 'Question: ' + old_query + " {} ".format(answer_format) + response + "\n"
prompt += 'Question: {} {}'.format(query, answer_format)
return prompt
class CogVLMVideoForCausalLM(CogVLMPreTrainedModel):
_auto_class = "AutoModelForCausalLM"
def __init__(self, config):
super().__init__(config)
self.model = CogVLMVideoModel(config)
self.vocab_size = config.vocab_size
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
self.video_downsample = 1 # TODO: change this to config
# Initialize weights and apply final processing
self.post_init()
def get_input_embeddings(self):
return self.model.embed_tokens
def set_input_embeddings(self, value):
self.model.embed_tokens = value
def get_output_embeddings(self):
return self.lm_head
def set_output_embeddings(self, new_embeddings):
self.lm_head = new_embeddings
def set_decoder(self, decoder):
self.model = decoder
def get_decoder(self):
return self.model
def forward(
self,
input_ids: torch.LongTensor = None,
images: List[List[torch.Tensor]] = None,
token_type_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
labels: Optional[torch.LongTensor] = None,
) -> Union[Tuple, CausalLMOutputWithPast]:
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
outputs = self.model(
input_ids=input_ids,
images=images,
token_type_ids=token_type_ids,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
hidden_states = outputs[0]
logits = self.lm_head(hidden_states)
logits = logits.float()
loss = None
if labels is not None:
# Shift so that tokens < n predict n
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
# Flatten the tokens
loss_fct = CrossEntropyLoss()
shift_logits = shift_logits.view(-1, self.config.vocab_size)
shift_labels = shift_labels.view(-1)
# Enable model parallelism
shift_labels = shift_labels.to(shift_logits.device)
loss = loss_fct(shift_logits, shift_labels)
if not return_dict:
output = (logits,) + outputs[1:]
return (loss,) + output if loss is not None else output
return CausalLMOutputWithPast(
loss=loss,
logits=logits,
past_key_values=outputs.past_key_values,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
def _prepare_attention_mask_for_generation(
self,
inputs: torch.Tensor,
pad_token_id: Optional[int],
eos_token_id: Optional[Union[int, List[int]]],
) -> torch.LongTensor:
return torch.ones(inputs.shape[:2], dtype=torch.long, device=inputs.device) # type: ignore
def prepare_inputs_for_generation(
self, input_ids, token_type_ids, images=None, past_key_values=None, attention_mask=None, inputs_embeds=None,
**kwargs
):
# build position_ids if needed
position_ids = kwargs.get("position_ids", None)
if position_ids is None:
position_ids = build_position_ids(token_type_ids, attention_mask)
if past_key_values:
input_ids = input_ids[:, -1:]
token_type_ids = token_type_ids[:, -1:]
position_ids = position_ids[:, -1:]
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
if inputs_embeds is not None and past_key_values is None:
model_inputs = {"inputs_embeds": inputs_embeds}
else:
model_inputs = {"input_ids": input_ids}
model_inputs.update(
{
"token_type_ids": token_type_ids,
"images": images,
"position_ids": position_ids,
"past_key_values": past_key_values,
"use_cache": kwargs.get("use_cache"),
"attention_mask": attention_mask,
}
)
return model_inputs
def _update_model_kwargs_for_generation(
self,
outputs: "ModelOutput",
model_kwargs: Dict[str, Any],
is_encoder_decoder: bool = False,
standardize_cache_format: bool = False,
) -> Dict[str, Any]:
# update past_key_values
cache_name, cache = self._extract_past_from_model_output(
outputs, standardize_cache_format=standardize_cache_format
)
model_kwargs[cache_name] = cache
if getattr(outputs, "state", None) is not None:
model_kwargs["state"] = outputs.state
# update token_type_ids with last value
if "token_type_ids" in model_kwargs:
token_type_ids = model_kwargs["token_type_ids"]
new_token_type_ids = torch.ones(size=(token_type_ids.shape[0], 1), dtype=token_type_ids.dtype,
device=token_type_ids.device) * LANGUAGE_TOKEN_TYPE
model_kwargs["token_type_ids"] = torch.cat([token_type_ids, new_token_type_ids], dim=-1)
if not is_encoder_decoder:
# update attention mask
if "attention_mask" in model_kwargs:
attention_mask = model_kwargs["attention_mask"]
model_kwargs["attention_mask"] = torch.cat(
[attention_mask, attention_mask.new_ones((attention_mask.shape[0], 1))], dim=-1
)
else:
# update decoder attention mask
if "decoder_attention_mask" in model_kwargs:
decoder_attention_mask = model_kwargs["decoder_attention_mask"]
model_kwargs["decoder_attention_mask"] = torch.cat(
[decoder_attention_mask, decoder_attention_mask.new_ones((decoder_attention_mask.shape[0], 1))],
dim=-1,
)
return model_kwargs
def _reorder_cache(self, past_key_values, beam_idx):
reordered_past = ()
for layer_past in past_key_values:
reordered_past += (
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
)
return reordered_past
def build_conversation_input_ids(
self,
tokenizer: "PreTrainedTokenizer",
*,
query: str,
history: Optional[List[Tuple[str, str]]] = None,
images: Optional[List["PIL.Image"]] = None,
template_version: Optional[Literal["base", "chat", "vqa"]] = None,
answer: str = None,
):
image_size: int = self.config.vision_config['image_size']
template_version = template_version or self.config.template_version
assert images is None or len(images) <= 1, f"not support multi images by now."
history = history or []
text = _history_to_prompt(template_version, history, query)
input_ids = [tokenizer.bos_token_id]
token_type_ids = [LANGUAGE_TOKEN_TYPE]
add_time_indices = True if template_version == 'chat' else False
if images is not None and len(images) == 1:
# vision
transform = transforms.Compose(
[
# UniformTemporalSubsample(num_frames),
Lambda(lambda x: x / 255.0),
NormalizeVideo(mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711)),
ShortSideScale(size=image_size),
CenterCropVideo(image_size),
# RandomHorizontalFlipVideo(p=0.5),
]
)
images = [transform(images[0]).transpose(0, 1)] # (T, C, H, W)
num_eois = len(images[0])
tokenizer.pad_token_id = 128002
if not add_time_indices:
vision_token_num = (64 + 2) * num_eois
input_ids += [tokenizer.pad_token_id] * vision_token_num # add spetial token
token_type_ids += [VISION_TOKEN_TYPE] * vision_token_num
else:
video_ids, video_type_ids = [], []
sing_vision_token_num = (64 + 2)
for _time_idx in range(num_eois):
video_ids += [tokenizer.pad_token_id] * sing_vision_token_num
video_type_ids += [VISION_TOKEN_TYPE] * sing_vision_token_num
# add time indices
time_indices = tokenizer.encode(str(_time_idx), add_special_tokens=False)
video_ids += time_indices
video_type_ids += [LANGUAGE_TOKEN_TYPE] * len(time_indices)
# llama3 adapt for cogvlm
input_ids += video_ids
token_type_ids += video_type_ids
text_ids = tokenizer.encode(text, add_special_tokens=False)
if answer is not None:
answer_ids = tokenizer.encode(answer, add_special_tokens=False)
answer_ids += [tokenizer.eos_token_id]
text_ids += answer_ids
input_ids += text_ids
token_type_ids += [LANGUAGE_TOKEN_TYPE] * len(text_ids)
attention_mask = [1] * len(input_ids)
if answer is not None:
labels = [-100 for _ in range(len(input_ids) - len(answer_ids))] + answer_ids
labels = torch.tensor(labels, dtype=torch.long)
else:
labels = None
return {
'input_ids': torch.tensor(input_ids, dtype=torch.long),
'token_type_ids': torch.tensor(token_type_ids, dtype=torch.long),
'attention_mask': torch.tensor(attention_mask, dtype=torch.long),
'images': images,
'labels': labels,
}
1234{ "bos_token": "<|begin_of_text|>", "eos_token": "<|end_of_text|>"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064{ "added_tokens_decoder": { "128000": { "content": "<|begin_of_text|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128001": { "content": "<|end_of_text|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128002": { "content": "<|reserved_special_token_0|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128003": { "content": "<|reserved_special_token_1|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128004": { "content": "<|reserved_special_token_2|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128005": { "content": "<|reserved_special_token_3|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128006": { "content": "<|start_header_id|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128007": { "content": "<|end_header_id|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128008": { "content": "<|reserved_special_token_4|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128009": { "content": "<|eot_id|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128010": { "content": "<|reserved_special_token_5|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128011": { "content": "<|reserved_special_token_6|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128012": { "content": "<|reserved_special_token_7|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128013": { "content": "<|reserved_special_token_8|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128014": { "content": "<|reserved_special_token_9|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128015": { "content": "<|reserved_special_token_10|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128016": { "content": "<|reserved_special_token_11|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128017": { "content": "<|reserved_special_token_12|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128018": { "content": "<|reserved_special_token_13|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128019": { "content": "<|reserved_special_token_14|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128020": { "content": "<|reserved_special_token_15|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128021": { "content": "<|reserved_special_token_16|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128022": { "content": "<|reserved_special_token_17|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128023": { "content": "<|reserved_special_token_18|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128024": { "content": "<|reserved_special_token_19|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128025": { "content": "<|reserved_special_token_20|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128026": { "content": "<|reserved_special_token_21|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128027": { "content": "<|reserved_special_token_22|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128028": { "content": "<|reserved_special_token_23|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128029": { "content": "<|reserved_special_token_24|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128030": { "content": "<|reserved_special_token_25|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128031": { "content": "<|reserved_special_token_26|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128032": { "content": "<|reserved_special_token_27|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128033": { "content": "<|reserved_special_token_28|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128034": { "content": "<|reserved_special_token_29|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128035": { "content": "<|reserved_special_token_30|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128036": { "content": "<|reserved_special_token_31|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128037": { "content": "<|reserved_special_token_32|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128038": { "content": "<|reserved_special_token_33|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128039": { "content": "<|reserved_special_token_34|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128040": { "content": "<|reserved_special_token_35|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128041": { "content": "<|reserved_special_token_36|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128042": { "content": "<|reserved_special_token_37|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128043": { "content": "<|reserved_special_token_38|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128044": { "content": "<|reserved_special_token_39|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128045": { "content": "<|reserved_special_token_40|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128046": { "content": "<|reserved_special_token_41|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128047": { "content": "<|reserved_special_token_42|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128048": { "content": "<|reserved_special_token_43|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128049": { "content": "<|reserved_special_token_44|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128050": { "content": "<|reserved_special_token_45|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128051": { "content": "<|reserved_special_token_46|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128052": { "content": "<|reserved_special_token_47|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128053": { "content": "<|reserved_special_token_48|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128054": { "content": "<|reserved_special_token_49|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128055": { "content": "<|reserved_special_token_50|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128056": { "content": "<|reserved_special_token_51|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128057": { "content": "<|reserved_special_token_52|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128058": { "content": "<|reserved_special_token_53|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128059": { "content": "<|reserved_special_token_54|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128060": { "content": "<|reserved_special_token_55|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128061": { "content": "<|reserved_special_token_56|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128062": { "content": "<|reserved_special_token_57|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128063": { "content": "<|reserved_special_token_58|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128064": { "content": "<|reserved_special_token_59|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128065": { "content": "<|reserved_special_token_60|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128066": { "content": "<|reserved_special_token_61|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128067": { "content": "<|reserved_special_token_62|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128068": { "content": "<|reserved_special_token_63|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128069": { "content": "<|reserved_special_token_64|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128070": { "content": "<|reserved_special_token_65|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128071": { "content": "<|reserved_special_token_66|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128072": { "content": "<|reserved_special_token_67|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128073": { "content": "<|reserved_special_token_68|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128074": { "content": "<|reserved_special_token_69|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128075": { "content": "<|reserved_special_token_70|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128076": { "content": "<|reserved_special_token_71|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128077": { "content": "<|reserved_special_token_72|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128078": { "content": "<|reserved_special_token_73|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128079": { "content": "<|reserved_special_token_74|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128080": { "content": "<|reserved_special_token_75|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128081": { "content": "<|reserved_special_token_76|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128082": { "content": "<|reserved_special_token_77|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128083": { "content": "<|reserved_special_token_78|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128084": { "content": "<|reserved_special_token_79|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128085": { "content": "<|reserved_special_token_80|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128086": { "content": "<|reserved_special_token_81|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128087": { "content": "<|reserved_special_token_82|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128088": { "content": "<|reserved_special_token_83|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128089": { "content": "<|reserved_special_token_84|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128090": { "content": "<|reserved_special_token_85|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128091": { "content": "<|reserved_special_token_86|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128092": { "content": "<|reserved_special_token_87|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128093": { "content": "<|reserved_special_token_88|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128094": { "content": "<|reserved_special_token_89|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128095": { "content": "<|reserved_special_token_90|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128096": { "content": "<|reserved_special_token_91|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128097": { "content": "<|reserved_special_token_92|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128098": { "content": "<|reserved_special_token_93|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128099": { "content": "<|reserved_special_token_94|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128100": { "content": "<|reserved_special_token_95|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128101": { "content": "<|reserved_special_token_96|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128102": { "content": "<|reserved_special_token_97|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128103": { "content": "<|reserved_special_token_98|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128104": { "content": "<|reserved_special_token_99|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128105": { "content": "<|reserved_special_token_100|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128106": { "content": "<|reserved_special_token_101|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128107": { "content": "<|reserved_special_token_102|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128108": { "content": "<|reserved_special_token_103|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128109": { "content": "<|reserved_special_token_104|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128110": { "content": "<|reserved_special_token_105|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128111": { "content": "<|reserved_special_token_106|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128112": { "content": "<|reserved_special_token_107|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128113": { "content": "<|reserved_special_token_108|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128114": { "content": "<|reserved_special_token_109|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128115": { "content": "<|reserved_special_token_110|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128116": { "content": "<|reserved_special_token_111|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128117": { "content": "<|reserved_special_token_112|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128118": { "content": "<|reserved_special_token_113|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128119": { "content": "<|reserved_special_token_114|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128120": { "content": "<|reserved_special_token_115|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128121": { "content": "<|reserved_special_token_116|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128122": { "content": "<|reserved_special_token_117|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128123": { "content": "<|reserved_special_token_118|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128124": { "content": "<|reserved_special_token_119|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128125": { "content": "<|reserved_special_token_120|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128126": { "content": "<|reserved_special_token_121|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128127": { "content": "<|reserved_special_token_122|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128128": { "content": "<|reserved_special_token_123|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128129": { "content": "<|reserved_special_token_124|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128130": { "content": "<|reserved_special_token_125|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128131": { "content": "<|reserved_special_token_126|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128132": { "content": "<|reserved_special_token_127|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128133": { "content": "<|reserved_special_token_128|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128134": { "content": "<|reserved_special_token_129|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128135": { "content": "<|reserved_special_token_130|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128136": { "content": "<|reserved_special_token_131|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128137": { "content": "<|reserved_special_token_132|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128138": { "content": "<|reserved_special_token_133|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128139": { "content": "<|reserved_special_token_134|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128140": { "content": "<|reserved_special_token_135|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128141": { "content": "<|reserved_special_token_136|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128142": { "content": "<|reserved_special_token_137|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128143": { "content": "<|reserved_special_token_138|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128144": { "content": "<|reserved_special_token_139|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128145": { "content": "<|reserved_special_token_140|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128146": { "content": "<|reserved_special_token_141|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128147": { "content": "<|reserved_special_token_142|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128148": { "content": "<|reserved_special_token_143|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128149": { "content": "<|reserved_special_token_144|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128150": { "content": "<|reserved_special_token_145|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128151": { "content": "<|reserved_special_token_146|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128152": { "content": "<|reserved_special_token_147|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128153": { "content": "<|reserved_special_token_148|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128154": { "content": "<|reserved_special_token_149|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128155": { "content": "<|reserved_special_token_150|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128156": { "content": "<|reserved_special_token_151|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128157": { "content": "<|reserved_special_token_152|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128158": { "content": "<|reserved_special_token_153|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128159": { "content": "<|reserved_special_token_154|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128160": { "content": "<|reserved_special_token_155|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128161": { "content": "<|reserved_special_token_156|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128162": { "content": "<|reserved_special_token_157|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128163": { "content": "<|reserved_special_token_158|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128164": { "content": "<|reserved_special_token_159|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128165": { "content": "<|reserved_special_token_160|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128166": { "content": "<|reserved_special_token_161|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128167": { "content": "<|reserved_special_token_162|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128168": { "content": "<|reserved_special_token_163|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128169": { "content": "<|reserved_special_token_164|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128170": { "content": "<|reserved_special_token_165|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128171": { "content": "<|reserved_special_token_166|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128172": { "content": "<|reserved_special_token_167|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128173": { "content": "<|reserved_special_token_168|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128174": { "content": "<|reserved_special_token_169|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128175": { "content": "<|reserved_special_token_170|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128176": { "content": "<|reserved_special_token_171|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128177": { "content": "<|reserved_special_token_172|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128178": { "content": "<|reserved_special_token_173|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128179": { "content": "<|reserved_special_token_174|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128180": { "content": "<|reserved_special_token_175|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128181": { "content": "<|reserved_special_token_176|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128182": { "content": "<|reserved_special_token_177|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128183": { "content": "<|reserved_special_token_178|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128184": { "content": "<|reserved_special_token_179|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128185": { "content": "<|reserved_special_token_180|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128186": { "content": "<|reserved_special_token_181|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128187": { "content": "<|reserved_special_token_182|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128188": { "content": "<|reserved_special_token_183|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128189": { "content": "<|reserved_special_token_184|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128190": { "content": "<|reserved_special_token_185|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128191": { "content": "<|reserved_special_token_186|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128192": { "content": "<|reserved_special_token_187|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128193": { "content": "<|reserved_special_token_188|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128194": { "content": "<|reserved_special_token_189|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128195": { "content": "<|reserved_special_token_190|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128196": { "content": "<|reserved_special_token_191|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128197": { "content": "<|reserved_special_token_192|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128198": { "content": "<|reserved_special_token_193|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128199": { "content": "<|reserved_special_token_194|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128200": { "content": "<|reserved_special_token_195|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128201": { "content": "<|reserved_special_token_196|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128202": { "content": "<|reserved_special_token_197|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128203": { "content": "<|reserved_special_token_198|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128204": { "content": "<|reserved_special_token_199|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128205": { "content": "<|reserved_special_token_200|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128206": { "content": "<|reserved_special_token_201|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128207": { "content": "<|reserved_special_token_202|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128208": { "content": "<|reserved_special_token_203|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128209": { "content": "<|reserved_special_token_204|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128210": { "content": "<|reserved_special_token_205|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128211": { "content": "<|reserved_special_token_206|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128212": { "content": "<|reserved_special_token_207|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128213": { "content": "<|reserved_special_token_208|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128214": { "content": "<|reserved_special_token_209|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128215": { "content": "<|reserved_special_token_210|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128216": { "content": "<|reserved_special_token_211|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128217": { "content": "<|reserved_special_token_212|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128218": { "content": "<|reserved_special_token_213|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128219": { "content": "<|reserved_special_token_214|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128220": { "content": "<|reserved_special_token_215|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128221": { "content": "<|reserved_special_token_216|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128222": { "content": "<|reserved_special_token_217|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128223": { "content": "<|reserved_special_token_218|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128224": { "content": "<|reserved_special_token_219|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128225": { "content": "<|reserved_special_token_220|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128226": { "content": "<|reserved_special_token_221|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128227": { "content": "<|reserved_special_token_222|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128228": { "content": "<|reserved_special_token_223|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128229": { "content": "<|reserved_special_token_224|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128230": { "content": "<|reserved_special_token_225|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128231": { "content": "<|reserved_special_token_226|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128232": { "content": "<|reserved_special_token_227|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128233": { "content": "<|reserved_special_token_228|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128234": { "content": "<|reserved_special_token_229|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128235": { "content": "<|reserved_special_token_230|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128236": { "content": "<|reserved_special_token_231|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128237": { "content": "<|reserved_special_token_232|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128238": { "content": "<|reserved_special_token_233|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128239": { "content": "<|reserved_special_token_234|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128240": { "content": "<|reserved_special_token_235|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128241": { "content": "<|reserved_special_token_236|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128242": { "content": "<|reserved_special_token_237|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128243": { "content": "<|reserved_special_token_238|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128244": { "content": "<|reserved_special_token_239|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128245": { "content": "<|reserved_special_token_240|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128246": { "content": "<|reserved_special_token_241|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128247": { "content": "<|reserved_special_token_242|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128248": { "content": "<|reserved_special_token_243|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128249": { "content": "<|reserved_special_token_244|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128250": { "content": "<|reserved_special_token_245|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128251": { "content": "<|reserved_special_token_246|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128252": { "content": "<|reserved_special_token_247|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128253": { "content": "<|reserved_special_token_248|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128254": { "content": "<|reserved_special_token_249|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "128255": { "content": "<|reserved_special_token_250|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true } }, "bos_token": "<|begin_of_text|>", "chat_template": "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% set loop_messages = messages %}{% for message in loop_messages %}{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' %}{% if loop.index0 == 0 %}{% set content = bos_token + content %}{% endif %}{{ content }}{% endfor %}{% if add_generation_prompt %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}{% else %}{{ eos_token }}{% endif %}", "clean_up_tokenization_spaces": true, "eos_token": "<|end_of_text|>", "model_input_names": [ "input_ids", "token_type_ids", "attention_mask", "images" ], "model_max_length": 2048, "tokenizer_class": "PreTrainedTokenizerFast"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472from typing import Optional, Tuple, Union
import torch
from einops import rearrange
import torch.nn.functional as F
import triton
import triton.language as tl
@triton.jit
def rotary_kernel(
OUT,
X,
COS,
SIN,
CU_SEQLENS,
SEQLEN_OFFSETS,
seqlen,
nheads,
rotary_dim,
seqlen_ro,
CACHE_KEY_SEQLEN,
# strides
stride_out_batch,
stride_out_nheads,
stride_out_seqlen,
stride_out_headdim,
stride_x_batch,
stride_x_nheads,
stride_x_seqlen,
stride_x_headdim,
BLOCK_K: tl.constexpr,
IS_SEQLEN_OFFSETS_TENSOR: tl.constexpr,
IS_VARLEN: tl.constexpr,
INTERLEAVED: tl.constexpr,
CONJUGATE: tl.constexpr,
BLOCK_M: tl.constexpr,
):
pid_m = tl.program_id(axis=0)
pid_batch = tl.program_id(axis=1)
pid_head = tl.program_id(axis=2)
rotary_dim_half = rotary_dim // 2
if not IS_VARLEN:
X = X + pid_batch * stride_x_batch + pid_head * stride_x_nheads
OUT = OUT + pid_batch * stride_out_batch + pid_head * stride_out_nheads
COS = COS + pid_batch * seqlen_ro * rotary_dim_half
SIN = SIN + pid_batch * seqlen_ro * rotary_dim_half
else:
start_idx = tl.load(CU_SEQLENS + pid_batch)
seqlen = tl.load(CU_SEQLENS + pid_batch + 1) - start_idx
X = X + start_idx * stride_x_seqlen + pid_head * stride_x_nheads
OUT = OUT + start_idx * stride_out_seqlen + pid_head * stride_out_nheads
if pid_m * BLOCK_M >= seqlen:
return
rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
if not IS_SEQLEN_OFFSETS_TENSOR:
rm_cs = rm + SEQLEN_OFFSETS
else:
rm_cs = rm + tl.load(SEQLEN_OFFSETS + pid_batch)
rk = tl.arange(0, BLOCK_K)
rk_half = tl.arange(0, BLOCK_K // 2)
if not INTERLEAVED:
# Load the 1st and 2nd halves of X, do calculation, then store to 1st and 2nd halves of OUT
X = X + (rm[:, None] * stride_x_seqlen + rk_half[None, :] * stride_x_headdim)
COS = COS + (rm_cs[:, None] * rotary_dim_half + rk_half[None, :])
SIN = SIN + (rm_cs[:, None] * rotary_dim_half + rk_half[None, :])
cos = tl.load(
COS, mask=(rm_cs[:, None] < seqlen_ro) & (rk_half[None, :] < rotary_dim_half), other=1.0
)
sin = tl.load(
SIN, mask=(rm_cs[:, None] < seqlen_ro) & (rk_half[None, :] < rotary_dim_half), other=0.0
)
x0 = tl.load(
X, mask=(rm[:, None] < seqlen) & (rk_half[None, :] < rotary_dim_half), other=0.0
)
x1 = tl.load(
X + rotary_dim_half * stride_x_headdim,
mask=(rm[:, None] < seqlen) & (rk_half[None, :] < rotary_dim_half),
other=0.0,
)
if CONJUGATE:
sin = -sin
o0 = x0 * cos - x1 * sin
o1 = x0 * sin + x1 * cos
# write back result
OUT = OUT + (rm[:, None] * stride_out_seqlen + rk_half[None, :] * stride_out_headdim)
tl.store(OUT, o0, mask=(rm[:, None] < seqlen) & (rk_half[None, :] < rotary_dim_half))
tl.store(
OUT + rotary_dim_half * stride_out_headdim,
o1,
mask=(rm[:, None] < seqlen) & (rk_half[None, :] < rotary_dim_half),
)
else:
# We don't want to load X[0, 2, 4, ...] and X[1, 3, 5, ...] separately since both are slow.
# Instead, we load x0 = X[0, 1, 2, 3, ...] and x1 = X[1, 0, 3, 2, ...].
# Loading x0 will be fast but x1 will be slow.
# Then we load cos = COS[0, 0, 1, 1, ...] and sin = SIN[0, 0, 1, 1, ...].
# Then we do the calculation and use tl.where to pick put the right outputs for the even
# and for the odd indices.
rk_swap = rk + ((rk + 1) % 2) * 2 - 1 # 1, 0, 3, 2, 5, 4, ...
rk_repeat = tl.arange(0, BLOCK_K) // 2
X0 = X + (rm[:, None] * stride_x_seqlen + rk[None, :] * stride_x_headdim)
X1 = X + (rm[:, None] * stride_x_seqlen + rk_swap[None, :] * stride_x_headdim)
COS = COS + (rm_cs[:, None] * rotary_dim_half + rk_repeat[None, :])
SIN = SIN + (rm_cs[:, None] * rotary_dim_half + rk_repeat[None, :])
cos = tl.load(
COS,
mask=(rm_cs[:, None] < seqlen_ro) & (rk_repeat[None, :] < rotary_dim_half),
other=1.0,
).to(tl.float32)
sin = tl.load(
SIN,
mask=(rm_cs[:, None] < seqlen_ro) & (rk_repeat[None, :] < rotary_dim_half),
other=0.0,
).to(tl.float32)
x0 = tl.load(X0, mask=(rm[:, None] < seqlen) & (rk[None, :] < rotary_dim), other=0.0).to(
tl.float32
)
x1 = tl.load(
X1, mask=(rm[:, None] < seqlen) & (rk_swap[None, :] < rotary_dim), other=0.0
).to(tl.float32)
if CONJUGATE:
sin = -sin
x0_cos = x0 * cos
x1_sin = x1 * sin
out = tl.where(rk[None, :] % 2 == 0, x0_cos - x1_sin, x0_cos + x1_sin)
OUT = OUT + (rm[:, None] * stride_out_seqlen + rk[None, :] * stride_out_headdim)
tl.store(OUT, out, mask=(rm[:, None] < seqlen) & (rk[None, :] < rotary_dim))
def apply_rotary(
x: torch.Tensor,
cos: torch.Tensor,
sin: torch.Tensor,
seqlen_offsets: Union[int, torch.Tensor] = 0,
cu_seqlens: Optional[torch.Tensor] = None,
max_seqlen: Optional[int] = None,
interleaved=False,
inplace=False,
conjugate=False,
) -> torch.Tensor:
"""
Arguments:
x: (batch, seqlen, nheads, headdim) if cu_seqlens is None
else (total_seqlen, nheads, headdim).
cos: (seqlen_ro, rotary_dim / 2)
sin: (seqlen_ro, rotary_dim / 2)
seqlen_offsets: integer or integer tensor of size (batch,)
cu_seqlens: (batch + 1,) or None
max_seqlen: int
Returns:
y: (batch, seqlen, nheads, headdim)
"""
batch, nheads, seqlen, headdim = x.shape
batch_ro, seqlen_ro, rotary_dim = cos.shape
assert batch == batch_ro
assert sin.shape == cos.shape
rotary_dim *= 2
assert rotary_dim <= headdim, "rotary_dim must be <= headdim"
assert headdim <= 256, "Only support headdim <= 256"
assert seqlen_ro >= seqlen, "seqlen_ro must be >= seqlen"
assert (
cos.dtype == sin.dtype
), f"cos and sin must have the same dtype, got {cos.dtype} and {sin.dtype}"
assert (
x.dtype == cos.dtype
), f"Input and cos/sin must have the same dtype, got {x.dtype} and {cos.dtype}"
cos, sin = cos.contiguous(), sin.contiguous()
if isinstance(seqlen_offsets, torch.Tensor):
assert seqlen_offsets.shape == (batch,)
assert seqlen_offsets.dtype in [torch.int32, torch.int64]
seqlen_offsets = seqlen_offsets.contiguous()
else:
assert seqlen_offsets + seqlen <= seqlen_ro
output = torch.empty_like(x) if not inplace else x
if rotary_dim < headdim and not inplace:
output[..., rotary_dim:].copy_(x[..., rotary_dim:])
BLOCK_K = (
32
if rotary_dim <= 32
else (64 if rotary_dim <= 64 else (128 if rotary_dim <= 128 else 256))
)
grid = lambda META: (triton.cdiv(seqlen, META["BLOCK_M"]), batch, nheads) # noqa
BLOCK_M = 4 if interleaved else (8 if rotary_dim <= 64 else 4)
# Need this, otherwise Triton tries to launch from cuda:0 and we get
# ValueError: Pointer argument (at 0) cannot be accessed from Triton (cpu tensor?)
with torch.cuda.device(x.device.index):
rotary_kernel[grid](
output, # data ptrs
x,
cos,
sin,
cu_seqlens,
seqlen_offsets,
seqlen, # shapes
nheads,
rotary_dim,
seqlen_ro,
seqlen // 128, # key for triton cache (limit number of compilations)
output.stride(0), # batch_strides
output.stride(-3), # nheads_stride
output.stride(-2), # seqlen_stride
output.stride(-1), # headdim_stride
x.stride(0), # batch_strides
x.stride(-3), # nheads stride
x.stride(-2), # seqlen stride
x.stride(-1), # headdim stride
BLOCK_K,
isinstance(seqlen_offsets, torch.Tensor),
False,
interleaved,
conjugate,
BLOCK_M,
)
return output
class ApplyRotaryEmb(torch.autograd.Function):
@staticmethod
def forward(
ctx,
x,
cos,
sin,
interleaved=False,
inplace=False,
seqlen_offsets: Union[int, torch.Tensor] = 0,
cu_seqlens: Optional[torch.Tensor] = None,
max_seqlen: Optional[int] = None,
):
out = apply_rotary(
x,
cos,
sin,
seqlen_offsets=seqlen_offsets,
cu_seqlens=cu_seqlens,
max_seqlen=max_seqlen,
interleaved=interleaved,
inplace=inplace,
)
if isinstance(seqlen_offsets, int):
ctx.save_for_backward(cos, sin, cu_seqlens) # Can't save int with save_for_backward
ctx.seqlen_offsets = seqlen_offsets
else:
ctx.save_for_backward(cos, sin, cu_seqlens, seqlen_offsets)
ctx.seqlen_offsets = None
ctx.interleaved = interleaved
ctx.inplace = inplace
ctx.max_seqlen = max_seqlen
return out if not inplace else x
@staticmethod
def backward(ctx, do):
seqlen_offsets = ctx.seqlen_offsets
if seqlen_offsets is None:
cos, sin, cu_seqlens, seqlen_offsets = ctx.saved_tensors
else:
cos, sin, cu_seqlens = ctx.saved_tensors
# TD [2023-09-02]: For some reason Triton (2.0.0.post1) errors with
# "[CUDA]: invalid device context", and cloning makes it work. Idk why. Triton 2.1.0 works.
if not ctx.interleaved and not ctx.inplace:
do = do.clone()
dx = apply_rotary(
do,
cos,
sin,
seqlen_offsets=seqlen_offsets,
cu_seqlens=cu_seqlens,
max_seqlen=ctx.max_seqlen,
interleaved=ctx.interleaved,
inplace=ctx.inplace,
conjugate=True,
)
return dx, None, None, None, None, None, None, None
def apply_rotary_emb(
x,
cos,
sin,
interleaved=False,
inplace=False,
seqlen_offsets: Union[int, torch.Tensor] = 0,
cu_seqlens: Optional[torch.Tensor] = None,
max_seqlen: Optional[int] = None,
):
"""
Arguments:
x: (batch_size, seqlen, nheads, headdim) if cu_seqlens is None
else (total_seqlen, nheads, headdim)
cos, sin: (seqlen_rotary, rotary_dim / 2)
interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead
of 1st half and 2nd half (GPT-NeoX style).
inplace: if True, apply rotary embedding in-place.
seqlen_offsets: (batch_size,) or int. Each sequence in x is shifted by this amount.
Most commonly used in inference when we have KV cache.
cu_seqlens: (batch + 1,) or None
max_seqlen: int
Return:
out: (batch_size, seqlen, nheads, headdim) if cu_seqlens is None
else (total_seqlen, nheads, headdim)
rotary_dim must be <= headdim
Apply rotary embedding to the first rotary_dim of x.
"""
return ApplyRotaryEmb.apply(
x, cos, sin, interleaved, inplace, seqlen_offsets, cu_seqlens, max_seqlen
)
# For backward compatibility
apply_rotary_emb_func = apply_rotary_emb
class FastRotaryEmbedding(torch.nn.Module):
"""
The rotary position embeddings from RoFormer_ (Su et. al).
A crucial insight from the method is that the query and keys are
transformed by rotation matrices which depend on the relative positions.
Other implementations are available in the Rotary Transformer repo_ and in
GPT-NeoX_, GPT-NeoX was an inspiration
.. _RoFormer: https://arxiv.org/abs/2104.09864
.. _repo: https://github.com/ZhuiyiTechnology/roformer
.. _GPT-NeoX: https://github.com/EleutherAI/gpt-neox
If scale_base is not None, this implements XPos (Sun et al., https://arxiv.org/abs/2212.10554).
A recommended value for scale_base is 512: https://github.com/HazyResearch/flash-attention/issues/96
Reference: https://github.com/sunyt32/torchscale/blob/main/torchscale/component/xpos_relative_position.py
"""
def __init__(
self,
dim: int,
base=10000,
interleaved=False,
scale_base=None,
pos_idx_in_fp32=True,
device=None,
):
"""
interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead
of 1st half and 2nd half (GPT-NeoX style).
pos_idx_in_fp32: if True, the position indices [0.0, ..., seqlen - 1] are in fp32,
otherwise they might be in lower precision.
This option was added because previously (before 2023-07-02), when we construct
the position indices, we use the dtype of self.inv_freq. In most cases this would
be fp32, but if the model is trained in pure bf16 (not mixed precision), then
self.inv_freq would be bf16, and the position indices are also in bf16.
Because of the limited precision of bf16 (e.g. 1995.0 is rounded to 2000.0), the
embeddings for some positions will coincide.
To maintain compatibility with models previously trained in pure bf16,
we add this option.
"""
super().__init__()
self.dim = dim
self.base = base
self.pos_idx_in_fp32 = pos_idx_in_fp32
# Generate and save the inverse frequency buffer (non trainable)
inv_freq = self._compute_inv_freq(device)
self.register_buffer("inv_freq", inv_freq)
self.interleaved = interleaved
self.scale_base = scale_base
scale = (
(torch.arange(0, dim, 2, device=device, dtype=torch.float32) + 0.4 * dim) / (1.4 * dim)
if scale_base is not None
else None
)
self.register_buffer("scale", scale, persistent=False)
self._seq_len_cached = 0
self._cos_cached = None
self._sin_cached = None
self._cos_k_cached = None
self._sin_k_cached = None
self.cos = None
self.sin = None
def _compute_inv_freq(self, device=None):
return 1.0 / (
self.base
** (torch.arange(0, self.dim, 2, device=device) / self.dim)
# ** (torch.arange(0, self.dim, 2, device=device).float() / self.dim)
)
def _update_cos_sin_cache(self, seqlen, position_id, device=None, dtype=None):
if (
seqlen > self._seq_len_cached
):
self._seq_len_cached = seqlen
# We want fp32 here, not self.inv_freq.dtype, since the model could be loaded in bf16
# And the output of arange can be quite large, so bf16 would lose a lot of precision.
# However, for compatibility reason, we add an option to use the dtype of self.inv_freq.
if self.pos_idx_in_fp32:
t = torch.arange(seqlen, device=device, dtype=torch.float32)
# We want fp32 here as well since inv_freq will be multiplied with t, and the output
# will be large. Having it in bf16 will lose a lot of precision and cause the
# cos & sin output to change significantly.
# We want to recompute self.inv_freq if it was not loaded in fp32
if self.inv_freq.dtype != torch.float32:
inv_freq = self._compute_inv_freq(device=device)
else:
inv_freq = self.inv_freq
else:
t = torch.arange(seqlen, device=device, dtype=self.inv_freq.dtype)
inv_freq = self.inv_freq
freqs = torch.einsum("i,j->ij", t, inv_freq)
if self.scale is None:
self._cos_cached = torch.cos(freqs).to(dtype)
self._sin_cached = torch.sin(freqs).to(dtype)
else:
power = (
torch.arange(seqlen, dtype=self.scale.dtype, device=self.scale.device)
- seqlen // 2
) / self.scale_base
scale = self.scale.to(device=power.device) ** rearrange(power, "s -> s 1")
# We want the multiplication by scale to happen in fp32
self._cos_cached = (torch.cos(freqs) * scale).to(dtype)
self._sin_cached = (torch.sin(freqs) * scale).to(dtype)
self._cos_k_cached = (torch.cos(freqs) / scale).to(dtype)
self._sin_k_cached = (torch.sin(freqs) / scale).to(dtype)
def forward(
self,
q: torch.Tensor,
k: torch.Tensor,
position_ids: torch.Tensor,
max_seqlen,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
q: (batch, nheads, seqlen, headdim)
k: (batch, nheads, seqlen, headdim)
position_id: (batch, seqlen)
max_seqlen: int
layer_id: int
only if layer_id == 0, then update cons and sin
Apply rotary embedding *inplace* to q k.
"""
self._update_cos_sin_cache(max_seqlen, position_ids, device=q.device, dtype=q.dtype)
cos, sin = F.embedding(position_ids, self._cos_cached), F.embedding(position_ids, self._sin_cached)
q = apply_rotary_emb_func(
q,
cos,
sin,
interleaved=self.interleaved,
inplace=True
)
k = apply_rotary_emb_func(
k,
cos,
sin,
interleaved=self.interleaved,
inplace=True
)
return q, k123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177import torch
from torch import nn
from argparse import Namespace
import torch.nn.functional as F
from transformers.activations import ACT2FN
import math
def standard_attention(query_layer, key_layer, value_layer, scaling_attention_score=True):
if scaling_attention_score:
query_layer = query_layer / math.sqrt(query_layer.shape[-1])
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
attention_probs = F.softmax(attention_scores, dim=-1)
context_layer = torch.matmul(attention_probs, value_layer)
return context_layer
def attention_fn_default(query_layer, key_layer, value_layer, scaling_attention_score=True):
# expand head dim to query dim, if necessary
# only useful for multi-query attention
batch_size, num_query_heads = query_layer.shape[:2] # [b, np, s, hn]
num_kv_heads = key_layer.shape[1] # [b, np, s, hn]
key_layer = key_layer.unsqueeze(2).expand(-1, -1, num_query_heads//num_kv_heads, -1, -1).contiguous().view(batch_size, num_query_heads, *key_layer.shape[2:])
value_layer = value_layer.unsqueeze(2).expand(-1, -1, num_query_heads//num_kv_heads, -1, -1).contiguous().view(batch_size, num_query_heads, *value_layer.shape[2:])
if int(torch.__version__.split('.')[0]) >= 2 and scaling_attention_score:
# Pytorch 2.0 attention uses very much memory if attention_mask is float, and has NaN bug if attention_mask is None.
attn_output = torch.nn.functional.scaled_dot_product_attention(
query_layer, key_layer, value_layer,
attn_mask=None,
dropout_p=0.,
is_causal=False
)
return attn_output
else:
return standard_attention(
query_layer, key_layer, value_layer, scaling_attention_score=scaling_attention_score
)
class PatchEmbedding(nn.Module):
def __init__(self, config):
super().__init__()
self.proj = nn.Conv2d(config.in_channels, config.hidden_size, kernel_size=config.patch_size, stride=config.patch_size)
self.cls_embedding = nn.Parameter(torch.zeros(1, config.hidden_size))
self.position_embedding = nn.Embedding(config.num_positions, config.hidden_size)
def forward(self, images: "tensor(B, C, H, W)") -> "tensor(B, L, D)":
x = self.proj(images)
x = x.flatten(2).transpose(1, 2)
cls_token = self.cls_embedding.expand(x.shape[0], -1, -1)
x = torch.cat((cls_token, x), dim=1)
x += self.position_embedding.weight.unsqueeze(0)
return x
class Attention(nn.Module):
def __init__(self, config):
super().__init__()
self.num_heads = config.num_heads
head_dim = config.hidden_size // config.num_heads
self.scale = head_dim ** -0.5
self.query_key_value = nn.Linear(config.hidden_size, config.hidden_size * 3)
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.output_dropout = torch.nn.Dropout(config.dropout_prob)
def forward(self, x: "tensor(B, L, D)") -> "tensor(B, L, D)":
B, L, _ = x.shape
qkv = self.query_key_value(x)
qkv = qkv.reshape(B, L, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4) # 3, B, H, L, D
q, k, v = qkv[0], qkv[1], qkv[2]
out = attention_fn_default(
q, k, v
) # 24 x 3 x
out = out.transpose(2, 1)
# breakpoint()
# output = self.dense(out.reshape(B, L, -1))
output = self.dense(out.view(B, L, -1))
output = self.output_dropout(output)
return output
def attention(self, q, k, v):
attn_weights = torch.matmul(q * self.scale, k.transpose(-2, -1))
attn_weights = attn_weights.softmax(dim=-1)
output = torch.matmul(attn_weights, v)
return output
class MLP(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
self.activation_fn = ACT2FN[config.hidden_act]
self.fc1 = nn.Linear(config.hidden_size, config.intermediate_size)
self.fc2 = nn.Linear(config.intermediate_size, config.hidden_size)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.fc1(x)
x = self.activation_fn(x)
x = self.fc2(x)
return x
class TransformerLayer(nn.Module):
def __init__(self, config):
super().__init__()
self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
self.attention = Attention(config)
self.mlp = MLP(config)
self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
def forward(self, hidden_states):
attention_input = hidden_states
attention_output = self.input_layernorm(self.attention(attention_input))
hidden_states = attention_input + attention_output
mlp_input = hidden_states
mlp_output = self.post_attention_layernorm(self.mlp(mlp_input))
output = mlp_input + mlp_output
return output
class Transformer(nn.Module):
def __init__(self, config):
super().__init__()
self.layers = nn.ModuleList([TransformerLayer(config) for _ in range(config.num_hidden_layers)])
def forward(self, hidden_states):
for layer_module in self.layers:
hidden_states = layer_module(hidden_states)
return hidden_states
class GLU(nn.Module):
def __init__(self, config, in_features):
super().__init__()
self.linear_proj = nn.Linear(in_features, config.hidden_size, bias=False)
self.norm1 = nn.LayerNorm(config.hidden_size)
self.act1 = nn.GELU()
self.act2 = nn.functional.silu
self.dense_h_to_4h = nn.Linear(config.hidden_size, config.intermediate_size, bias=False)
self.gate_proj = nn.Linear(config.hidden_size, config.intermediate_size, bias=False)
self.dense_4h_to_h = nn.Linear(config.intermediate_size, config.hidden_size, bias=False)
def forward(self, x):
x = self.linear_proj(x)
x = self.act1(self.norm1(x))
x = self.act2(self.gate_proj(x)) * self.dense_h_to_4h(x)
x = self.dense_4h_to_h(x)
return x
class EVA2CLIPModel(nn.Module):
def __init__(self, config):
super().__init__()
vision_config = Namespace(**config.vision_config)
self.patch_embedding = PatchEmbedding(vision_config)
self.transformer = Transformer(vision_config)
self.linear_proj = GLU(config, in_features=vision_config.hidden_size)
self.conv = nn.Conv2d(in_channels=vision_config.hidden_size, out_channels=vision_config.hidden_size, kernel_size=2, stride=2)
self.boi = nn.Parameter(torch.zeros(1, 1, config.hidden_size))
self.eoi = nn.Parameter(torch.zeros(1, 1, config.hidden_size))
def forward(self, images: "tensor(B, C, H, W)") -> "tensor(B, L, D)":
x = self.patch_embedding(images)
x = self.transformer(x)
x = x[:, 1:]
b, s, h = x.shape
grid_size = int(s**0.5)
x = x.view(b, grid_size, grid_size, h).permute(0, 3, 1, 2)
x = self.conv(x)
x = x.flatten(2).transpose(1, 2)
x = self.linear_proj(x)
boi = self.boi.expand(x.shape[0], -1, -1)
eoi = self.eoi.expand(x.shape[0], -1, -1)
x = torch.cat((boi, x, eoi), dim=1)
return xLICENSEAdded+75
LLAMA3_LICENSEAdded+117
README.mdAdded+108
README_zh.mdAdded+85
config.jsonAdded+40
configuration.jsonAdded+1
configuration_cogvlm.pyAdded+46
generation_config.jsonAdded+11
model-00001-of-00006.safetensorsAdded+3
model-00002-of-00006.safetensorsAdded+3
model-00003-of-00006.safetensorsAdded+3
model-00004-of-00006.safetensorsAdded+3
model-00005-of-00006.safetensorsAdded+3
model-00006-of-00006.safetensorsAdded+3
model.safetensors.index.jsonAdded
modeling_cogvlm.pyAdded+841
special_tokens_map.jsonAdded+4
tokenizer.jsonAdded
tokenizer_config.jsonAdded+2064
util.pyAdded+472
visual.pyAdded+177