├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── Llama3_Repo.jpeg ├── MODEL_CARD.md ├── README.md ├── README_en.md ├── USE_POLICY.md ├── download.sh ├── eval_details.md ├── example_chat_completion.py ├── example_text_completion.py ├── llama ├── __init__.py ├── generation.py ├── model.py ├── test_tokenizer.py └── tokenizer.py ├── requirements.txt └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to make participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies within all project spaces, and it also applies when 49 | an individual is representing the project or its community in public spaces. 50 | Examples of representing a project or community include using an official 51 | project e-mail address, posting via an official social media account, or acting 52 | as an appointed representative at an online or offline event. Representation of 53 | a project may be further defined and clarified by project maintainers. 54 | 55 | This Code of Conduct also applies outside the project spaces when there is a 56 | reasonable belief that an individual's behavior may have a negative impact on 57 | the project or its community. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported by contacting the project team at . All 63 | complaints will be reviewed and investigated and will result in a response that 64 | is deemed necessary and appropriate to the circumstances. The project team is 65 | obligated to maintain confidentiality with regard to the reporter of an incident. 66 | Further details of specific enforcement policies may be posted separately. 67 | 68 | Project maintainers who do not follow or enforce the Code of Conduct in good 69 | faith may face temporary or permanent repercussions as determined by other 70 | members of the project's leadership. 71 | 72 | ## Attribution 73 | 74 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 75 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 76 | 77 | [homepage]: https://www.contributor-covenant.org 78 | 79 | For answers to common questions about this code of conduct, see 80 | https://www.contributor-covenant.org/faq 81 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Llama 3 2 | We want to make contributing to this project as easy and transparent as 3 | possible. 4 | 5 | ## Our Development Process 6 | ... (in particular how this is synced with internal changes to the project) 7 | 8 | ## Pull Requests 9 | We actively welcome your pull requests. 10 | 11 | 1. Fork the repo and create your branch from `main`. 12 | 2. If you've added code that should be tested, add tests. 13 | 3. If you've changed APIs, update the documentation. 14 | 4. Ensure the test suite passes. 15 | 5. Make sure your code lints. 16 | 6. If you haven't already, complete the Contributor License Agreement ("CLA"). 17 | 18 | ## Contributor License Agreement ("CLA") 19 | In order to accept your pull request, we need you to submit a CLA. You only need 20 | to do this once to work on any of Meta's open source projects. 21 | 22 | Complete your CLA here: 23 | 24 | ## Issues 25 | We use GitHub issues to track public bugs. Please ensure your description is 26 | clear and has sufficient instructions to be able to reproduce the issue. 27 | 28 | Meta has a [bounty program](http://facebook.com/whitehat/info) for the safe 29 | disclosure of security bugs. In those cases, please go through the process 30 | outlined on that page and do not file a public issue. 31 | 32 | ## Coding Style 33 | * 2 spaces for indentation rather than tabs 34 | * 80 character line length 35 | * ... 36 | 37 | ## License 38 | By contributing to Llama 3, you agree that your contributions will be licensed 39 | under the LICENSE file in the root directory of this source tree. 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | META LLAMA 3 COMMUNITY LICENSE AGREEMENT 2 | 3 | Meta Llama 3 Version Release Date: April 18, 2024 4 | “Agreement” means the terms and conditions for use, reproduction, distribution and modification of the Llama Materials set forth herein. 5 | 6 | “Documentation” means the specifications, manuals and documentation accompanying Meta Llama 3 distributed by Meta at https://llama.meta.com/get-started/. 7 | 8 | “Licensee” or “you” means you, or your employer or any other person or entity (if you are entering into this Agreement on such person or entity’s behalf), of the age required under applicable laws, rules or regulations to provide legal consent and that has legal authority to bind your employer or such other person or entity if you are entering in this Agreement on their behalf. 9 | 10 | “Meta Llama 3” means the foundational large language models and software and algorithms, including machine-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 at https://llama.meta.com/llama-downloads. 11 | 12 | “Llama Materials” means, collectively, Meta’s proprietary Meta Llama 3 and Documentation (and any portion thereof) made available under this Agreement. 13 | 14 | “Meta” or “we” means Meta Platforms Ireland Limited (if you are located in or, if you are an entity, your principal place of business is in the EEA or Switzerland) and Meta Platforms, Inc. (if you are located outside of the EEA or Switzerland). 15 | 16 | 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. 17 | 18 | 1. License Rights and Redistribution. 19 | 20 | a. Grant of Rights. You are granted a non-exclusive, worldwide, non-transferable and royalty-free limited license under Meta’s intellectual property or other rights owned by Meta embodied in the Llama Materials to use, reproduce, distribute, copy, create derivative works of, and make modifications to the Llama Materials. 21 | b. Redistribution and Use. 22 | i. If you distribute or make available the Llama Materials (or any derivative works thereof), or a product or service that uses any of them, including another AI model, you shall (A) provide a copy of this Agreement with any such Llama Materials; and (B) prominently display “Built with Meta Llama 3” on a related website, user interface, blogpost, about page, or product documentation. If you use the Llama Materials to create, train, fine tune, or otherwise improve an AI model, which is distributed or made available, you shall also include “Llama 3” at the beginning of any such AI model name. 23 | 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. 24 | iii. You must retain in all copies of the Llama Materials that you distribute the following attribution notice within a “Notice” text file distributed as a part of such copies: “Meta Llama 3 is licensed under the Meta Llama 3 Community License, Copyright © Meta Platforms, Inc. All Rights Reserved.” 25 | 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 Llama Materials (available at https://llama.meta.com/llama3/use-policy), which is hereby incorporated by reference into this Agreement. 26 | v. You will not use the Llama Materials or any output or results of the Llama Materials to improve any other large language model (excluding Meta Llama 3 or derivative works thereof). 27 | 28 | 2. Additional Commercial Terms. If, on the Meta Llama 3 version release date, the monthly active users of the products or services made available by or for Licensee, or Licensee’s affiliates, is greater than 700 million 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 the rights under this Agreement unless or until Meta otherwise expressly grants you such rights. 29 | 30 | 3. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE LLAMA MATERIALS AND ANY OUTPUT AND RESULTS THEREFROM ARE PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OF ANY 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 FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE LLAMA MATERIALS AND ASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE LLAMA MATERIALS AND ANY OUTPUT AND RESULTS. 31 | 32 | 4. Limitation of Liability. IN NO EVENT WILL META OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING OUT 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 ADVISED OF THE POSSIBILITY OF ANY OF THE FOREGOING. 33 | 34 | 5. Intellectual Property. 35 | a. No trademark licenses are granted under this Agreement, and in connection with the Llama Materials, neither Meta nor Licensee may use any name or mark owned by or associated with the other or any of its affiliates, except as required for reasonable and customary use in describing and redistributing the Llama Materials or as set forth in this Section 5(a). Meta hereby grants you a license to use “Llama 3” (the “Mark”) solely as required to comply with the last sentence of Section 1.b.i. You will comply with Meta’s brand guidelines (currently accessible at https://about.meta.com/brand/resources/meta/company-brand/ ). All goodwill arising out of your use of the Mark will inure to the benefit of Meta. 36 | b. Subject to Meta’s ownership of Llama Materials and derivatives made by or for Meta, with respect to any derivative works and modifications of the Llama Materials that are made by you, as between you and Meta, you are and will be the owner of such derivative works and modifications. 37 | c. If you institute litigation or other proceedings against Meta or any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Llama Materials or Meta Llama 3 outputs or results, or any portion of any of the foregoing, constitutes infringement of intellectual property or other rights owned or licensable by you, then any licenses granted to you under this Agreement shall terminate as of the date such litigation or claim is filed or instituted. You will indemnify and hold harmless Meta from and against any claim by any third party arising out of or related to your use or distribution of the Llama Materials. 38 | 39 | 6. Term and Termination. The term of this Agreement will commence upon your acceptance of this Agreement or access to the Llama Materials and will continue in full force and effect until terminated in accordance with the terms and conditions herein. Meta may terminate this Agreement if you are in breach of any term or condition of this Agreement. Upon termination of this Agreement, you shall delete and cease use of the Llama Materials. Sections 3, 4 and 7 shall survive the termination of this Agreement. 40 | 41 | 7. Governing Law and Jurisdiction. This Agreement will be governed and construed under the laws of the State of California without regard to choice of law principles, and the UN Convention on Contracts for the International Sale of Goods does not apply to this Agreement. The courts of California shall have exclusive jurisdiction of any dispute arising out of this Agreement. 42 | 43 | 44 | Meta Llama 3 Acceptable Use Policy 45 | Meta is committed to promoting safe and fair use of its tools and features, including Meta Llama 3. If you access or use Meta Llama 3, you agree to this Acceptable Use Policy (“Policy”). The most recent copy of this policy can be found at https://llama.meta.com/llama3/use-policy 46 | Prohibited Uses 47 | We want everyone to use Meta Llama 3 safely and responsibly. You agree you will not use, or allow others to use, Meta Llama 3 to: 48 | 1. Violate the law or others’ rights, including to: 49 | a. Engage in, promote, generate, contribute to, encourage, plan, incite, or further illegal or unlawful activity or content, such as: 50 | i. Violence or terrorism 51 | ii. Exploitation or harm to children, including the solicitation, creation, acquisition, or dissemination of child exploitative content or failure to report Child Sexual Abuse Material 52 | iii. Human trafficking, exploitation, and sexual violence 53 | iv. The illegal distribution of information or materials to minors, including obscene materials, or failure to employ legally required age-gating in connection with such information or materials. 54 | v. Sexual solicitation 55 | vi. Any other criminal activity 56 | b. Engage in, promote, incite, or facilitate the harassment, abuse, threatening, or bullying of individuals or groups of individuals 57 | c. Engage in, promote, incite, or facilitate discrimination or other unlawful or harmful conduct in the provision of employment, employment benefits, credit, housing, other economic benefits, or other essential goods and services 58 | d. Engage in the unauthorized or unlicensed practice of any profession including, but not limited to, financial, legal, medical/health, or related professional practices 59 | e. Collect, process, disclose, generate, or infer health, demographic, or other sensitive personal or private information about individuals without rights and consents required by applicable laws 60 | f. Engage in or facilitate any action or generate any content that infringes, misappropriates, or otherwise violates any third-party rights, including the outputs or results of any products or services using the Llama Materials 61 | g. Create, generate, or facilitate the creation of malicious code, malware, computer viruses or do anything else that could disable, overburden, interfere with or impair the proper working, integrity, operation or appearance of a website or computer system 62 | 63 | 2. Engage in, promote, incite, facilitate, or assist in the planning or development of activities that present a risk of death or bodily harm to individuals, including use of Meta Llama 3 related to the following: 64 | a. Military, warfare, nuclear industries or applications, espionage, use for materials or activities that are subject to the International Traffic Arms Regulations (ITAR) maintained by the United States Department of State 65 | b. Guns and illegal weapons (including weapon development) 66 | c. Illegal drugs and regulated/controlled substances 67 | d. Operation of critical infrastructure, transportation technologies, or heavy machinery 68 | e. Self-harm or harm to others, including suicide, cutting, and eating disorders 69 | f. Any content intended to incite or promote violence, abuse, or any infliction of bodily harm to an individual 70 | 71 | 3. Intentionally deceive or mislead others, including use of Meta Llama 3 related to the following: 72 | a. Generating, promoting, or furthering fraud or the creation or promotion of disinformation 73 | b. Generating, promoting, or furthering defamatory content, including the creation of defamatory statements, images, or other content 74 | c. Generating, promoting, or further distributing spam 75 | d. Impersonating another individual without consent, authorization, or legal right 76 | e. Representing that the use of Meta Llama 3 or outputs are human-generated 77 | f. Generating or facilitating false online engagement, including fake reviews and other means of fake online engagement 78 | g. Fail to appropriately disclose to end users any known dangers of your AI system 79 | 80 | Please report any violation of this Policy, software “bug,” or other problems that could lead to a violation of this Policy through one of the following means: 81 | * Reporting issues with the model: https://github.com/meta-llama/llama3 82 | * Reporting risky content generated by the model: developers.facebook.com/llama_output_feedback 83 | * Reporting bugs and security concerns: facebook.com/whitehat/info 84 | * Reporting violations of the Acceptable Use Policy or unlicensed uses of Meta Llama 3: LlamaUseReport@meta.com 85 | -------------------------------------------------------------------------------- /Llama3_Repo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ArtificialZeng/llama3_explained/ec89928516c2959cefb3d6dac47301830be73478/Llama3_Repo.jpeg -------------------------------------------------------------------------------- /MODEL_CARD.md: -------------------------------------------------------------------------------- 1 | ## Model Details 2 | 3 | Meta developed and released the Meta Llama 3 family of large language models (LLMs), a collection of pretrained and instruction tuned generative text models in 8 and 70B sizes. The Llama 3 instruction tuned models are optimized for dialogue use cases and outperform many of the available open source chat models on common industry benchmarks. Further, in developing these models, we took great care to optimize helpfulness and safety. 4 | 5 | **Model developers** Meta 6 | 7 | **Variations** Llama 3 comes in two sizes — 8B and 70B parameters — in pre-trained and instruction tuned variants. 8 | 9 | **Input** Models input text only. 10 | 11 | **Output** Models generate text and code only. 12 | 13 | **Model Architecture** Llama 3 is an auto-regressive language model that uses an optimized transformer architecture. The tuned versions use supervised fine-tuning (SFT) and reinforcement learning with human feedback (RLHF) to align with human preferences for helpfulness and safety. 14 | 15 | 16 | 17 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 33 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 49 | 50 | 52 | 54 | 56 | 58 | 59 |
19 | Training Data 21 | Params 23 | Context length 25 | GQA 27 | Token count 29 | Knowledge cutoff 31 |
Llama 3 35 | A new mix of publicly available online data. 37 | 8B 39 | 8k 41 | Yes 43 | 15T+ 45 | March, 2023 47 |
70B 51 | 8k 53 | Yes 55 | December, 2023 57 |
60 | 61 | 62 | **Llama 3 family of models**. Token counts refer to pretraining data only. Both the 8 and 70B versions use Grouped-Query Attention (GQA) for improved inference scalability. 63 | 64 | **Model Release Date** April 18, 2024. 65 | 66 | **Status** This is a static model trained on an offline dataset. Future versions of the tuned models will be released as we improve model safety with community feedback. 67 | 68 | **License** A custom commercial license is available at: [https://llama.meta.com/llama3/license](https://llama.meta.com/llama3/license) 69 | 70 | Where to send questions or comments about the model Instructions on how to provide feedback or comments on the model can be found in the model [README](https://github.com/meta-llama/llama3). For more technical information about generation parameters and recipes for how to use Llama 3 in applications, please go [here](https://github.com/meta-llama/llama-recipes). 71 | 72 | 73 | ## Intended Use 74 | 75 | **Intended Use Cases** Llama 3 is intended for commercial and research use in English. Instruction tuned models are intended for assistant-like chat, whereas pretrained models can be adapted for a variety of natural language generation tasks. 76 | 77 | **Out-of-scope** Use in any manner that violates applicable laws or regulations (including trade compliance laws). Use in any other way that is prohibited by the Acceptable Use Policy and Llama 3 Community License. Use in languages other than English**. 78 | 79 | **Note: Developers may fine-tune Llama 3 models for languages beyond English provided they comply with the Llama 3 Community License and the Acceptable Use Policy. 80 | 81 | 82 | ## Hardware and Software 83 | 84 | **Training Factors** We used custom training libraries, Meta's Research SuperCluster, and production clusters for pretraining. Fine-tuning, annotation, and evaluation were also performed on third-party cloud compute. 85 | 86 | **Carbon Footprint Pretraining utilized a cumulative** 7.7M GPU hours of computation on hardware of type H100-80GB (TDP of 700W). Estimated total emissions were 2290 tCO2eq, 100% of which were offset by Meta’s sustainability program. 87 | 88 | 89 | 90 | 91 | 93 | 95 | 97 | 99 | 100 | 101 | 103 | 105 | 107 | 109 | 110 | 111 | 113 | 115 | 117 | 119 | 120 | 121 | 123 | 125 | 127 | 129 | 130 |
92 | Time (GPU hours) 94 | Power Consumption (W) 96 | Carbon Emitted(tCO2eq) 98 |
Llama 3 8B 102 | 1.3M 104 | 700 106 | 390 108 |
Llama 3 70B 112 | 6.4M 114 | 700 116 | 1900 118 |
Total 122 | 7.7M 124 | 126 | 2290 128 |
131 | 132 | 133 | 134 | **CO2 emissions during pre-training**. Time: total GPU time required for training each model. Power Consumption: peak power capacity per GPU device for the GPUs used adjusted for power usage efficiency. 100% of the emissions are directly offset by Meta's sustainability program, and because we are openly releasing these models, the pretraining costs do not need to be incurred by others. 135 | 136 | 137 | ## Training Data 138 | 139 | **Overview** Llama 3 was pretrained on over 15 trillion tokens of data from publicly available sources. The fine-tuning data includes publicly available instruction datasets, as well as over 10M human-annotated examples. Neither the pretraining nor the fine-tuning datasets include Meta user data. 140 | 141 | **Data Freshness** The pretraining data has a cutoff of March 2023 for the 8B and December 2023 for the 70B models respectively. 142 | 143 | 144 | ## Benchmarks 145 | 146 | In this section, we report the results for Llama 3 models on standard automatic benchmarks. For all the evaluations, we use our internal evaluations library. For details on the methodology see [here](https://github.com/meta-llama/llama3/blob/main/eval_details.md). 147 | 148 | 149 | ### Base pretrained models 150 | 151 | 152 | 153 | 154 | 156 | 158 | 160 | 162 | 164 | 166 | 168 | 169 | 170 | 172 | 174 | 176 | 178 | 180 | 182 | 184 | 185 | 186 | 188 | 190 | 192 | 194 | 196 | 198 | 199 | 200 | 202 | 204 | 206 | 208 | 210 | 212 | 213 | 214 | 216 | 218 | 220 | 222 | 224 | 226 | 227 | 228 | 230 | 232 | 234 | 236 | 238 | 240 | 241 | 242 | 244 | 246 | 248 | 250 | 252 | 254 | 255 | 256 | 258 | 260 | 262 | 264 | 266 | 268 | 270 | 271 | 272 | 274 | 276 | 278 | 280 | 282 | 284 | 286 | 287 | 288 | 290 | 292 | 294 | 296 | 298 | 300 | 301 | 302 | 304 | 306 | 308 | 310 | 312 | 314 | 315 | 316 | 318 | 320 | 322 | 324 | 326 | 328 | 329 |
Category 155 | Benchmark 157 | Llama 3 8B 159 | Llama2 7B 161 | Llama2 13B 163 | Llama 3 70B 165 | Llama2 70B 167 |
General 171 | MMLU (5-shot) 173 | 66.6 175 | 45.7 177 | 53.8 179 | 79.5 181 | 69.7 183 |
AGIEval English (3-5 shot) 187 | 45.9 189 | 28.8 191 | 38.7 193 | 63.0 195 | 54.8 197 |
CommonSenseQA (7-shot) 201 | 72.6 203 | 57.6 205 | 67.6 207 | 83.8 209 | 78.7 211 |
Winogrande (5-shot) 215 | 76.1 217 | 73.3 219 | 75.4 221 | 83.1 223 | 81.8 225 |
BIG-Bench Hard (3-shot, CoT) 229 | 61.1 231 | 38.1 233 | 47.0 235 | 81.3 237 | 65.7 239 |
ARC-Challenge (25-shot) 243 | 78.6 245 | 53.7 247 | 67.6 249 | 93.0 251 | 85.3 253 |
Knowledge reasoning 257 | TriviaQA-Wiki (5-shot) 259 | 78.5 261 | 72.1 263 | 79.6 265 | 89.7 267 | 87.5 269 |
Reading comprehension 273 | SQuAD (1-shot) 275 | 76.4 277 | 72.2 279 | 72.1 281 | 85.6 283 | 82.6 285 |
QuAC (1-shot, F1) 289 | 44.4 291 | 39.6 293 | 44.9 295 | 51.1 297 | 49.4 299 |
BoolQ (0-shot) 303 | 75.7 305 | 65.5 307 | 66.9 309 | 79.0 311 | 73.1 313 |
DROP (3-shot, F1) 317 | 58.4 319 | 37.9 321 | 49.8 323 | 79.7 325 | 70.2 327 |
330 | 331 | 332 | 333 | ### Instruction tuned models 334 | 335 | 336 | 337 | 338 | 340 | 342 | 344 | 346 | 348 | 350 | 351 | 352 | 354 | 356 | 358 | 360 | 362 | 364 | 365 | 366 | 368 | 370 | 372 | 374 | 376 | 378 | 379 | 380 | 382 | 384 | 386 | 388 | 390 | 392 | 393 | 394 | 396 | 398 | 400 | 402 | 404 | 406 | 407 | 408 | 410 | 412 | 414 | 416 | 418 | 420 | 421 |
Benchmark 339 | Llama 3 8B 341 | Llama 2 7B 343 | Llama 2 13B 345 | Llama 3 70B 347 | Llama 2 70B 349 |
MMLU (5-shot) 353 | 68.4 355 | 34.1 357 | 47.8 359 | 82.0 361 | 52.9 363 |
GPQA (0-shot) 367 | 34.2 369 | 21.7 371 | 22.3 373 | 39.5 375 | 21.0 377 |
HumanEval (0-shot) 381 | 62.2 383 | 7.9 385 | 14.0 387 | 81.7 389 | 25.6 391 |
GSM-8K (8-shot, CoT) 395 | 79.6 397 | 25.7 399 | 77.4 401 | 93.0 403 | 57.5 405 |
MATH (4-shot, CoT) 409 | 30.0 411 | 3.8 413 | 6.7 415 | 50.4 417 | 11.6 419 |
422 | 423 | 424 | 425 | ### Responsibility & Safety 426 | 427 | We believe that an open approach to AI leads to better, safer products, faster innovation, and a bigger overall market. We are committed to Responsible AI development and took a series of steps to limit misuse and harm and support the open source community. 428 | 429 | Foundation models are widely capable technologies that are built to be used for a diverse range of applications. They are not designed to meet every developer preference on safety levels for all use cases, out-of-the-box, as those by their nature will differ across different applications. 430 | 431 | Rather, responsible LLM-application deployment is achieved by implementing a series of safety best practices throughout the development of such applications, from the model pre-training, fine-tuning and the deployment of systems composed of safeguards to tailor the safety needs specifically to the use case and audience. 432 | 433 | 434 | As part of the Llama 3 release, we updated our [Responsible Use Guide](https://llama.meta.com/responsible-use-guide/) to outline the steps and best practices for developers to implement model and system level safety for their application. We also provide a set of resources including [Meta Llama Guard 2](https://llama.meta.com/purple-llama/) and [Code Shield](https://llama.meta.com/purple-llama/) safeguards. These tools have proven to drastically reduce residual risks of LLM Systems, while maintaining a high level of helpfulness. We encourage developers to tune and deploy these safeguards according to their needs and we provide a [reference implementation](https://github.com/meta-llama/llama-recipes/tree/main/recipes/responsible_ai) to get you started. 435 | 436 | 437 | #### Llama 3-Instruct 438 | 439 | As outlined in the Responsible Use Guide, some trade-off between model helpfulness and model alignment is likely unavoidable. Developers should exercise discretion about how to weigh the benefits of alignment and helpfulness for their specific use case and audience. Developers should be mindful of residual risks when using Llama models and leverage additional safety tools as needed to reach the right safety bar for their use case. 440 | 441 | Safety 442 | 443 | For our instruction tuned model, we conducted extensive red teaming exercises, performed adversarial evaluations and implemented safety mitigations techniques to lower residual risks. As with any Large Language Model, residual risks will likely remain and we recommend that developers assess these risks in the context of their use case. In parallel, we are working with the community to make AI safety benchmark standards transparent, rigorous and interpretable. 444 | 445 | Refusals 446 | 447 | In addition to residual risks, we put a great emphasis on model refusals to benign prompts. Over-refusing not only can impact the user experience but could even be harmful in certain contexts as well. We’ve heard the feedback from the developer community and improved our fine tuning to ensure that Llama 3 is significantly less likely to falsely refuse to answer prompts than Llama 2. 448 | 449 | We built internal benchmarks and developed mitigations to limit false refusals making Llama 3 our most helpful model to date. 450 | 451 | 452 | #### Responsible release 453 | 454 | In addition to responsible use considerations outlined above, we followed a rigorous process that requires us to take extra measures against misuse and critical risks before we make our release decision. 455 | 456 | Misuse 457 | 458 | If you access or use Llama 3, you agree to the Acceptable Use Policy. The most recent copy of this policy can be found at [https://llama.meta.com/llama3/use-policy/](https://llama.meta.com/llama3/use-policy/). 459 | 460 | 461 | #### Critical risks 462 | 463 | CBRNE (Chemical, Biological, Radiological, Nuclear, and high yield Explosives) 464 | 465 | We have conducted a two fold assessment of the safety of the model in this area: 466 | 467 | 468 | 469 | * Iterative testing during model training to assess the safety of responses related to CBRNE threats and other adversarial risks. 470 | * Involving external CBRNE experts to conduct an uplift test assessing the ability of the model to accurately provide expert knowledge and reduce barriers to potential CBRNE misuse, by reference to what can be achieved using web search (without the model). 471 | 472 | 473 | ### Cyber Security 474 | 475 | We have evaluated Llama 3 with CyberSecEval, Meta’s cybersecurity safety eval suite, measuring Llama 3’s propensity to suggest insecure code when used as a coding assistant, and Llama 3’s propensity to comply with requests to help carry out cyber attacks, where attacks are defined by the industry standard MITRE ATT&CK cyber attack ontology. On our insecure coding and cyber attacker helpfulness tests, Llama 3 behaved in the same range or safer than models of [equivalent coding capability](https://huggingface.co/spaces/facebook/CyberSecEval). 476 | 477 | 478 | ### Child Safety 479 | 480 | Child Safety risk assessments were conducted using a team of experts, to assess the model’s capability to produce outputs that could result in Child Safety risks and inform on any necessary and appropriate risk mitigations via fine tuning. We leveraged those expert red teaming sessions to expand the coverage of our evaluation benchmarks through Llama 3 model development. For Llama 3, we conducted new in-depth sessions using objective based methodologies to assess the model risks along multiple attack vectors. We also partnered with content specialists to perform red teaming exercises assessing potentially violating content while taking account of market specific nuances or experiences. 481 | 482 | 483 | ### Community 484 | 485 | Generative AI safety requires expertise and tooling, and we believe in the strength of the open community to accelerate its progress. We are active members of open consortiums, including the AI Alliance, Partnership in AI and MLCommons, actively contributing to safety standardization and transparency. We encourage the community to adopt taxonomies like the MLCommons Proof of Concept evaluation to facilitate collaboration and transparency on safety and content evaluations. Our Purple Llama tools are open sourced for the community to use and widely distributed across ecosystem partners including cloud service providers. We encourage community contributions to our [Github repository](https://github.com/meta-llama/PurpleLlama). 486 | 487 | Finally, we put in place a set of resources including an [output reporting mechanism](https://developers.facebook.com/llama_output_feedback) and [bug bounty program](https://www.facebook.com/whitehat) to continuously improve the Llama technology with the help of the community. 488 | 489 | 490 | ## Ethical Considerations and Limitations 491 | 492 | The core values of Llama 3 are openness, inclusivity and helpfulness. It is meant to serve everyone, and to work for a wide range of use cases. It is thus designed to be accessible to people across many different backgrounds, experiences and perspectives. Llama 3 addresses users and their needs as they are, without insertion unnecessary judgment or normativity, while reflecting the understanding that even content that may appear problematic in some cases can serve valuable purposes in others. It respects the dignity and autonomy of all users, especially in terms of the values of free thought and expression that power innovation and progress. 493 | 494 | But Llama 3 is a new technology, and like any new technology, there are risks associated with its use. Testing conducted to date has been in English, and has not covered, nor could it cover, all scenarios. For these reasons, as with all LLMs, Llama 3’s potential outputs cannot be predicted in advance, and the model may in some instances produce inaccurate, biased or other objectionable responses to user prompts. Therefore, before deploying any applications of Llama 3 models, developers should perform safety testing and tuning tailored to their specific applications of the model. As outlined in the Responsible Use Guide, we recommend incorporating [Purple Llama](https://github.com/facebookresearch/PurpleLlama) solutions into your workflows and specifically [Llama Guard](https://ai.meta.com/research/publications/llama-guard-llm-based-input-output-safeguard-for-human-ai-conversations/) which provides a base model to filter input and output prompts to layer system-level safety on top of model-level safety. 495 | 496 | Please see the Responsible Use Guide available at [http://llama.meta.com/responsible-use-guide](http://llama.meta.com/responsible-use-guide) 497 | 498 | 499 | ## Citation instructions 500 | 501 | ``` 502 | @article{llama3modelcard, 503 | title={Llama 3 Model Card}, 504 | author={AI@Meta}, 505 | year={2024}, 506 | url = {https://github.com/meta-llama/llama3/blob/main/MODEL_CARD.md} 507 | } 508 | ``` 509 | 510 | ## Contributors 511 | 512 | Aaditya Singh; Aaron Grattafiori; Abhimanyu Dubey; Abhinav Jauhri; Abhinav Pandey; Abhishek Kadian; Adam Kelsey; Adi Gangidi; Ahmad Al-Dahle; Amit Sangani; Ahuva Goldstand; Aiesha Letman; Ajay Menon; Akhil Mathur; Alan Schelten; Alex Vaughan; Amy Yang; Andrei Lupu; Andres Alvarado; Andrew Gallagher; Andrew Gu; Andrew Ho; Andrew Poulton; Andrew Ryan; Angela Fan; Ankit Ramchandani; Anthony Hartshorn; Archi Mitra; Archie Sravankumar; Artem Korenev; Arun Rao; Ashley Gabriel; Ashwin Bharambe; Assaf Eisenman; Aston Zhang; Ash JJhaveri; Aurelien Rodriguez; Austen Gregerson; Ava Spataru; Baptiste Roziere; Ben Maurer; Benjamin Leonhardi; Bernie Huang; Bhargavi Paranjape; Bing Liu; Binh Tang; Bobbie Chern; Brani Stojkovic; Brian Fuller; Catalina Mejia Arenas; Chao Zhou; Charlotte Caucheteux; Chaya Nayak; Ching-Hsiang Chu; Chloe Bi; Chris Cai; Chris Cox; Chris Marra; Chris McConnell; Christian Keller; Christoph Feichtenhofer; Christophe Touret; Chunyang Wu; Corinne Wong; Cristian Canton Ferrer; Damien Allonsius; Daniel Kreymer; Daniel Haziza; Daniel Li; Danielle Pintz; Danny Livshits; Danny Wyatt; David Adkins; David Esiobu; David Xu; Davide Testuggine; Delia David; Devi Parikh; Dhruv Choudhary; Dhruv Mahajan; Diana Liskovich; Diego Garcia-Olano; Diego Perino; Dieuwke Hupkes; Dingkang Wang; Dustin Holland; Egor Lakomkin; Elina Lobanova; Xiaoqing Ellen Tan; Emily Dinan; Eric Smith; Erik Brinkman; Esteban Arcaute; Filip Radenovic; Firat Ozgenel; Francesco Caggioni; Frank Seide; Frank Zhang; Gabriel Synnaeve; Gabriella Schwarz; Gabrielle Lee; Gada Badeer; Georgia Anderson; Graeme Nail; Gregoire Mialon; Guan Pang; Guillem Cucurell; Hailey Nguyen; Hamid Shojanazeri; Hannah Korevaar; Hannah Wang; Haroun Habeeb; Harrison Rudolph; Henry Aspegren; Hu Xu; Hugo Touvron; Iga Kozlowska; Igor Molybog; Igor Tufanov; Iliyan Zarov; Imanol Arrieta Ibarra; Irina-Elena Veliche; Isabel Kloumann; Ishan Misra; Ivan Evtimov; Jacob Xu; Jade Copet; Jake Weissman; Jan Geffert; Jana Vranes; Japhet Asher; Jason Park; Jay Mahadeokar; Jean-Baptiste Gaya; Jeet Shah; Jelmer van der Linde; Jennifer Chan; Jenny Hong; Jenya Lee; Jeremy Fu; Jeremy Teboul; Jianfeng Chi; Jianyu Huang; Jie Wang; Jiecao Yu; Joanna Bitton; Joe Spisak; Joelle Pineau; Jon Carvill; Jongsoo Park; Joseph Rocca; Joshua Johnstun; Junteng Jia; Kalyan Vasuden Alwala; Kam Hou U; Kate Plawiak; Kartikeya Upasani; Kaushik Veeraraghavan; Ke Li; Kenneth Heafield; Kevin Stone; Khalid El-Arini; Krithika Iyer; Kshitiz Malik; Kuenley Chiu; Kunal Bhalla; Kyle Huang; Lakshya Garg; Lauren Rantala-Yeary; Laurens van der Maaten; Lawrence Chen; Leandro Silva; Lee Bell; Lei Zhang; Liang Tan; Louis Martin; Lovish Madaan; Luca Wehrstedt; Lukas Blecher; Luke de Oliveira; Madeline Muzzi; Madian Khabsa; Manav Avlani; Mannat Singh; Manohar Paluri; Mark Zuckerberg; Marcin Kardas; Martynas Mankus; Mathew Oldham; Mathieu Rita; Matthew Lennie; Maya Pavlova; Meghan Keneally; Melanie Kambadur; Mihir Patel; Mikayel Samvelyan; Mike Clark; Mike Lewis; Min Si; Mitesh Kumar Singh; Mo Metanat; Mona Hassan; Naman Goyal; Narjes Torabi; Nicolas Usunier; Nikolay Bashlykov; Nikolay Bogoychev; Niladri Chatterji; Ning Dong; Oliver Aobo Yang; Olivier Duchenne; Onur Celebi; Parth Parekh; Patrick Alrassy; Paul Saab; Pavan Balaji; Pedro Rittner; Pengchuan Zhang; Pengwei Li; Petar Vasic; Peter Weng; Polina Zvyagina; Prajjwal Bhargava; Pratik Dubal; Praveen Krishnan; Punit Singh Koura; Qing He; Rachel Rodriguez; Ragavan Srinivasan; Rahul Mitra; Ramon Calderer; Raymond Li; Robert Stojnic; Roberta Raileanu; Robin Battey; Rocky Wang; Rohit Girdhar; Rohit Patel; Romain Sauvestre; Ronnie Polidoro; Roshan Sumbaly; Ross Taylor; Ruan Silva; Rui Hou; Rui Wang; Russ Howes; Ruty Rinott; Saghar Hosseini; Sai Jayesh Bondu; Samyak Datta; Sanjay Singh; Sara Chugh; Sargun Dhillon; Satadru Pan; Sean Bell; Sergey Edunov; Shaoliang Nie; Sharan Narang; Sharath Raparthy; Shaun Lindsay; Sheng Feng; Sheng Shen; Shenghao Lin; Shiva Shankar; Shruti Bhosale; Shun Zhang; Simon Vandenhende; Sinong Wang; Seohyun Sonia Kim; Soumya Batra; Sten Sootla; Steve Kehoe; Suchin Gururangan; Sumit Gupta; Sunny Virk; Sydney Borodinsky; Tamar Glaser; Tamar Herman; Tamara Best; Tara Fowler; Thomas Georgiou; Thomas Scialom; Tianhe Li; Todor Mihaylov; Tong Xiao; Ujjwal Karn; Vedanuj Goswami; Vibhor Gupta; Vignesh Ramanathan; Viktor Kerkez; Vinay Satish Kumar; Vincent Gonguet; Vish Vogeti; Vlad Poenaru; Vlad Tiberiu Mihailescu; Vladan Petrovic; Vladimir Ivanov; Wei Li; Weiwei Chu; Wenhan Xiong; Wenyin Fu; Wes Bouaziz; Whitney Meers; Will Constable; Xavier Martinet; Xiaojian Wu; Xinbo Gao; Xinfeng Xie; Xuchao Jia; Yaelle Goldschlag; Yann LeCun; Yashesh Gaur; Yasmine Babaei; Ye Qi; Yenda Li; Yi Wen; Yiwen Song; Youngjin Nam; Yuchen Hao; Yuchen Zhang; Yun Wang; Yuning Mao; Yuzi He; Zacharie Delpierre Coudert; Zachary DeVito; Zahra Hankir; Zhaoduo Wen; Zheng Yan; Zhengxing Chen; Zhenyu Yang; Zoe Papakipos 513 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 |

6 | 🤗 Hugging Face 上的模型  | 博客  | 网站  | 开始使用  7 |
8 | 9 | --- 10 | 11 | 12 | # Meta Llama 3 13 | 14 | 我们正在释放大语言模型的力量。我们最新版本的 Llama 现在可供个人、创作者、研究人员和各种规模的企业使用,以便他们可以负责任地试验、创新和扩展他们的想法。 15 | 16 | 此版本包括预训练和指令调优的 Llama 3 语言模型的模型权重和起始代码,包括 8B 到 70B 参数的大小。 17 | 18 | 此存储库旨在作为加载 Llama 3 模型并运行推理的最小示例。有关更详细的示例,请参阅 [llama-recipes](https://github.com/facebookresearch/llama-recipes/)。 19 | 20 | ## 下载 21 | 22 | 为了下载模型权重和分词器,请访问 [Meta Llama 网站](https://llama.meta.com/llama-downloads/)并接受我们的许可协议。 23 | 24 | 提交请求后,您将通过电子邮件收到一个签名的 URL。然后运行 download.sh 脚本,在提示时传递提供的 URL 以开始下载。 25 | 26 | 先决条件:确保您已安装 `wget` 和 `md5sum`。然后运行脚本:`./download.sh`。 27 | 28 | 请记住,链接会在 24 小时和一定次数的下载后过期。如果开始看到 `403: Forbidden` 之类的错误,您始终可以重新请求链接。 29 | 30 | ### 访问 Hugging Face 31 | 32 | 我们还在 [Hugging Face](https://huggingface.co/meta-llama) 上提供下载,包括 transformers 和原生 `llama3` 格式。要从 Hugging Face 下载权重,请按照以下步骤操作: 33 | 34 | - 访问其中一个仓库,例如 [meta-llama/Meta-Llama-3-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct)。 35 | - 阅读并接受许可。请求获得批准后,您将获得所有 Llama 3 模型的访问权限。请注意,处理请求通常需要长达一个小时。 36 | - 要下载原始原生权重以与此仓库一起使用,请单击"Files and versions"选项卡,然后下载 `original` 文件夹的内容。如果你安装了 `pip install huggingface-hub`,也可以从命令行下载它们: 37 | 38 | ```bash 39 | huggingface-cli download meta-llama/Meta-Llama-3-8B-Instruct --include "original/*" --local-dir meta-llama/Meta-Llama-3-8B-Instruct 40 | ``` 41 | - 要与transformers一起使用,以下pipeline代码片段将下载并缓存权重: 42 | ```python 43 | import transformers 44 | import torch 45 | 46 | model_id = "meta-llama/Meta-Llama-3-8B-Instruct" 47 | 48 | pipeline = transformers.pipeline( 49 | "text-generation", 50 | model="meta-llama/Meta-Llama-3-8B-Instruct", 51 | model_kwargs={"torch_dtype": torch.bfloat16}, 52 | device="cuda", 53 | ) 54 | ``` 55 | ## 快速开始 56 | 57 | 您可以按照以下步骤快速开始使用 Llama 3 模型。这些步骤将让您能够在本地进行快速推理。更多示例,请查看[Llama 配方仓库](https://github.com/facebookresearch/llama-recipes)。 58 | 59 | 1. 在一个已安装 PyTorch / CUDA 的 conda 环境中克隆并下载此仓库。 60 | 61 | 2. 在顶级目录运行: 62 | ```bash 63 | pip install -e . 64 | ``` 65 | 3. 访问[Meta Llama 网站](https://llama.meta.com/llama-downloads/)并注册以下载模型。 66 | 67 | 4. 注册后,您将收到一封带有下载模型 URL 的电子邮件。在运行 download.sh 脚本时,您将需要此 URL。 68 | 69 | 5. 收到电子邮件后,导航至您下载的 llama 仓库并运行 download.sh 脚本。 70 | - 确保授予 download.sh 脚本执行权限 71 | - 在此过程中,系统会提示您输入电子邮件中的 URL。 72 | - 不要使用“复制链接”选项,而是确保手动从电子邮件复制链接。 73 | 74 | 6. 下载所需的模型后,您可以使用以下命令在本地运行模型: 75 | ```bash 76 | torchrun --nproc_per_node 1 example_chat_completion.py \ 77 | --ckpt_dir Meta-Llama-3-8B-Instruct/ \ 78 | --tokenizer_path Meta-Llama-3-8B-Instruct/tokenizer.model \ 79 | --max_seq_len 512 --max_batch_size 6 80 | ``` 81 | **注意** 82 | - 将 `Meta-Llama-3-8B-Instruct/` 替换为您的检查点目录路径,将 `Meta-Llama-3-8B-Instruct/tokenizer.model` 替换为您的分词器模型路径。 83 | - `–nproc_per_node` 应设置为您使用的模型的 [MP](#inference) 值。 84 | - 根据需要调整 `max_seq_len` 和 `max_batch_size` 参数。 85 | - 此示例运行此仓库中找到的 [example_chat_completion.py](example_chat_completion.py),但您可以更改为不同的 .py 文件。 86 | 87 | ## 推理 88 | 89 | 不同的模型需要不同的模型并行(MP)值: 90 | 91 | | 模型 | MP | 92 | |--------|----| 93 | | 8B | 1 | 94 | | 70B | 8 | 95 | 96 | 所有模型支持最多 8192 令牌的序列长度,但我们会根据 `max_seq_len` 和 `max_batch_size` 的值预分配缓存。因此,请根据您的硬件设置这些值。 97 | 98 | ### 预训练模型 99 | 100 | 这些模型未针对聊天或问答进行微调。应该设定提示,使得预期答案是提示的自然延续。 101 | 102 | 请参见 `example_text_completion.py` 以获取一些示例。为了说明,参见下面的命令,以使用 llama-3-8b 模型运行它(`nproc_per_node` 需要设置为 `MP` 值): 103 | 104 | torchrun --nproc_per_node 1 example_text_completion.py 105 | --ckpt_dir Meta-Llama-3-8B/ 106 | --tokenizer_path Meta-Llama-3-8B/tokenizer.model 107 | --max_seq_len 128 --max_batch_size 4 108 | 109 | 110 | ### 指令调整模型 111 | 112 | 微调模型是为对话应用培训的。为了获得它们的预期特性和性能,需要遵循 [`ChatFormat`](https://github.com/meta-llama/llama3/blob/main/llama/tokenizer.py#L202) 中定义的特定格式:提示以特殊令牌 `<|begin_of_text|>` 开始,之后跟随一个或多个消息。每条消息以标签 `<|start_header_id|>` 开始,角色为 `system`、`user` 或 `assistant`,并以标签 `<|end_header_id|>` 结束。在双换行 `\n\n` 之后,消息的内容随之而来。每条消息的结尾由 `<|eot_id|>` 令牌标记。 113 | 114 | 您还可以部署额外的分类器,以过滤掉被认为不安全的输入和输出。请参见 llama-recipes 仓库中的 [一个示例](https://github.com/meta-llama/llama-recipes/blob/main/recipes/inference/local_inference/inference.py),了解如何在您的推理代码的输入和输出中添加安全检查器。 115 | 116 | 使用 llama-3-8b-chat 的示例: 117 | 118 | torchrun --nproc_per_node 1 example_chat_completion.py 119 | --ckpt_dir Meta-Llama-3-8B-Instruct/ 120 | --tokenizer_path Meta-Llama-3-8B-Instruct/tokenizer.model 121 | --max_seq_len 512 --max_batch_size 6 122 | 123 | 124 | 125 | Llama 3 是一项新技术,使用时带有潜在风险。迄今为止进行的测试没有——也不可能——覆盖所有情况。 126 | 为了帮助开发者应对这些风险,我们创建了 [负责任使用指南](https://ai.meta.com/static-resource/responsible-use-guide/)。 127 | 128 | ## 问题 129 | 130 | 请通过以下方式之一报告软件“bug”或模型的其他问题: 131 | - 报告模型问题:[https://github.com/meta-llama/llama3/issues](https://github.com/meta-llama/llama3/issues) 132 | - 报告模型生成的风险内容:[developers.facebook.com/llama_output_feedback](http://developers.facebook.com/llama_output_feedback) 133 | - 报告漏洞和安全问题:[facebook.com/whitehat/info](http://facebook.com/whitehat/info) 134 | 135 | ## 模型卡片 136 | 参见 [MODEL_CARD.md](MODEL_CARD.md)。 137 | 138 | ## 许可证 139 | 140 | 我们的模型和权重为研究者和商业实体授权,坚持开放原则。我们的使命是通过这一机会赋能个人和行业,同时促进发现和道德 AI 进步的环境。 141 | 142 | 请查看 [LICENSE](LICENSE) 文件,以及我们的 [可接受使用政策](USE_POLICY.md) 143 | 144 | ## 问题 145 | 146 | 对于常见问题,可以在此处找到 FAQ [https://llama.meta.com/faq](https://llama.meta.com/faq),随着新问题的出现,这将不断更新。 -------------------------------------------------------------------------------- /README_en.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 |

6 | 🤗 Models on Hugging Face  | Blog  | Website  | Get Started  7 |
8 | 9 | --- 10 | 11 | 12 | # Meta Llama 3 13 | 14 | We are unlocking the power of large language models. Our latest version of Llama is now accessible to individuals, creators, researchers, and businesses of all sizes so that they can experiment, innovate, and scale their ideas responsibly. 15 | 16 | This release includes model weights and starting code for pre-trained and instruction tuned Llama 3 language models — including sizes of 8B to 70B parameters. 17 | 18 | This repository is intended as a minimal example to load Llama 3 models and run inference. For more detailed examples, see [llama-recipes](https://github.com/facebookresearch/llama-recipes/). 19 | 20 | ## Download 21 | 22 | In order to download the model weights and tokenizer, please visit the [Meta Llama website](https://llama.meta.com/llama-downloads/) and accept our License. 23 | 24 | Once your request is approved, you will receive a signed URL over email. Then run the download.sh script, passing the URL provided when prompted to start the download. 25 | 26 | Pre-requisites: Make sure you have `wget` and `md5sum` installed. Then run the script: `./download.sh`. 27 | 28 | Keep in mind that the links expire after 24 hours and a certain amount of downloads. If you start seeing errors such as `403: Forbidden`, you can always re-request a link. 29 | 30 | ### Access to Hugging Face 31 | 32 | We are also providing downloads on [Hugging Face](https://huggingface.co/meta-llama), in both transformers and native `llama3` formats. To download the weights from Hugging Face, please follow these steps: 33 | 34 | - Visit one of the repos, for example [meta-llama/Meta-Llama-3-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct). 35 | - Read and accept the license. Once your request is approved, you'll be granted access to all the Llama 3 models. Note that requests use to take up to one hour to get processed. 36 | - To download the original native weights to use with this repo, click on the "Files and versions" tab and download the contents of the `original` folder. You can also download them from the command line if you `pip install huggingface-hub`: 37 | 38 | ```bash 39 | huggingface-cli download meta-llama/Meta-Llama-3-8B-Instruct --include "original/*" --local-dir meta-llama/Meta-Llama-3-8B-Instruct 40 | ``` 41 | 42 | - To use with transformers, the following [pipeline](https://huggingface.co/docs/transformers/en/main_classes/pipelines) snippet will download and cache the weights: 43 | 44 | ```python 45 | import transformers 46 | import torch 47 | 48 | model_id = "meta-llama/Meta-Llama-3-8B-Instruct" 49 | 50 | pipeline = transformers.pipeline( 51 | "text-generation", 52 | model="meta-llama/Meta-Llama-3-8B-Instruct", 53 | model_kwargs={"torch_dtype": torch.bfloat16}, 54 | device="cuda", 55 | ) 56 | ``` 57 | 58 | ## Quick Start 59 | 60 | You can follow the steps below to quickly get up and running with Llama 3 models. These steps will let you run quick inference locally. For more examples, see the [Llama recipes repository](https://github.com/facebookresearch/llama-recipes). 61 | 62 | 1. In a conda env with PyTorch / CUDA available clone and download this repository. 63 | 64 | 2. In the top-level directory run: 65 | ```bash 66 | pip install -e . 67 | ``` 68 | 3. Visit the [Meta Llama website](https://llama.meta.com/llama-downloads/) and register to download the model/s. 69 | 70 | 4. Once registered, you will get an email with a URL to download the models. You will need this URL when you run the download.sh script. 71 | 72 | 5. Once you get the email, navigate to your downloaded llama repository and run the download.sh script. 73 | - Make sure to grant execution permissions to the download.sh script 74 | - During this process, you will be prompted to enter the URL from the email. 75 | - Do not use the “Copy Link” option but rather make sure to manually copy the link from the email. 76 | 77 | 6. Once the model/s you want have been downloaded, you can run the model locally using the command below: 78 | ```bash 79 | torchrun --nproc_per_node 1 example_chat_completion.py \ 80 | --ckpt_dir Meta-Llama-3-8B-Instruct/ \ 81 | --tokenizer_path Meta-Llama-3-8B-Instruct/tokenizer.model \ 82 | --max_seq_len 512 --max_batch_size 6 83 | ``` 84 | **Note** 85 | - Replace `Meta-Llama-3-8B-Instruct/` with the path to your checkpoint directory and `Meta-Llama-3-8B-Instruct/tokenizer.model` with the path to your tokenizer model. 86 | - The `–nproc_per_node` should be set to the [MP](#inference) value for the model you are using. 87 | - Adjust the `max_seq_len` and `max_batch_size` parameters as needed. 88 | - This example runs the [example_chat_completion.py](example_chat_completion.py) found in this repository but you can change that to a different .py file. 89 | 90 | ## Inference 91 | 92 | Different models require different model-parallel (MP) values: 93 | 94 | | Model | MP | 95 | |--------|----| 96 | | 8B | 1 | 97 | | 70B | 8 | 98 | 99 | All models support sequence length up to 8192 tokens, but we pre-allocate the cache according to `max_seq_len` and `max_batch_size` values. So set those according to your hardware. 100 | 101 | ### Pretrained Models 102 | 103 | These models are not finetuned for chat or Q&A. They should be prompted so that the expected answer is the natural continuation of the prompt. 104 | 105 | See `example_text_completion.py` for some examples. To illustrate, see the command below to run it with the llama-3-8b model (`nproc_per_node` needs to be set to the `MP` value): 106 | 107 | ``` 108 | torchrun --nproc_per_node 1 example_text_completion.py \ 109 | --ckpt_dir Meta-Llama-3-8B/ \ 110 | --tokenizer_path Meta-Llama-3-8B/tokenizer.model \ 111 | --max_seq_len 128 --max_batch_size 4 112 | ``` 113 | 114 | ### Instruction-tuned Models 115 | 116 | The fine-tuned models were trained for dialogue applications. To get the expected features and performance for them, a specific formatting defined in [`ChatFormat`](https://github.com/meta-llama/llama3/blob/main/llama/tokenizer.py#L202) 117 | needs to be followed: The prompt begins with a `<|begin_of_text|>` special token, after which one or more messages follow. Each message starts with the `<|start_header_id|>` tag, the role `system`, `user` or `assistant`, and the `<|end_header_id|>` tag. After a double newline `\n\n` the contents of the message follow. The end of each message is marked by the `<|eot_id|>` token. 118 | 119 | You can also deploy additional classifiers for filtering out inputs and outputs that are deemed unsafe. See the llama-recipes repo for [an example](https://github.com/meta-llama/llama-recipes/blob/main/recipes/inference/local_inference/inference.py) of how to add a safety checker to the inputs and outputs of your inference code. 120 | 121 | Examples using llama-3-8b-chat: 122 | 123 | ``` 124 | torchrun --nproc_per_node 1 example_chat_completion.py \ 125 | --ckpt_dir Meta-Llama-3-8B-Instruct/ \ 126 | --tokenizer_path Meta-Llama-3-8B-Instruct/tokenizer.model \ 127 | --max_seq_len 512 --max_batch_size 6 128 | ``` 129 | 130 | Llama 3 is a new technology that carries potential risks with use. Testing conducted to date has not — and could not — cover all scenarios. 131 | In order to help developers address these risks, we have created the [Responsible Use Guide](https://ai.meta.com/static-resource/responsible-use-guide/). 132 | 133 | ## Issues 134 | 135 | Please report any software “bug”, or other problems with the models through one of the following means: 136 | - Reporting issues with the model: [https://github.com/meta-llama/llama3/issues](https://github.com/meta-llama/llama3/issues) 137 | - Reporting risky content generated by the model: [developers.facebook.com/llama_output_feedback](http://developers.facebook.com/llama_output_feedback) 138 | - Reporting bugs and security concerns: [facebook.com/whitehat/info](http://facebook.com/whitehat/info) 139 | 140 | ## Model Card 141 | See [MODEL_CARD.md](MODEL_CARD.md). 142 | 143 | ## License 144 | 145 | Our model and weights are licensed for both researchers and commercial entities, upholding the principles of openness. Our mission is to empower individuals, and industry through this opportunity, while fostering an environment of discovery and ethical AI advancements. 146 | 147 | See the [LICENSE](LICENSE) file, as well as our accompanying [Acceptable Use Policy](USE_POLICY.md) 148 | 149 | ## Questions 150 | 151 | For common questions, the FAQ can be found [here](https://llama.meta.com/faq) which will be kept up to date over time as new questions arise. 152 | -------------------------------------------------------------------------------- /USE_POLICY.md: -------------------------------------------------------------------------------- 1 | # Meta Llama 3 Acceptable Use Policy 2 | 3 | Meta is committed to promoting safe and fair use of its tools and features, including Llama 3. If you access or use Llama 3, you agree to this Acceptable Use Policy (“Policy”). The most recent copy of this policy can be found at [ai.meta.com/llama/use-policy](http://ai.meta.com/llama/use-policy). 4 | 5 | ## Prohibited Uses 6 | We want everyone to use Llama 3 safely and responsibly. You agree you will not use, or allow others to use, Llama 3 to: 7 | 8 | 1. Violate the law or others’ rights, including to: 9 | 1. Engage in, promote, generate, contribute to, encourage, plan, incite, or further illegal or unlawful activity or content, such as: 10 | 1. Violence or terrorism 11 | 2. Exploitation or harm to children, including the solicitation, creation, acquisition, or dissemination of child exploitative content or failure to report Child Sexual Abuse Material 12 | 3. Human trafficking, exploitation, and sexual violence 13 | 4. The illegal distribution of information or materials to minors, including obscene materials, or failure to employ legally required age-gating in connection with such information or materials. 14 | 5. Sexual solicitation 15 | 6. Any other criminal activity 16 | 2. Engage in, promote, incite, or facilitate the harassment, abuse, threatening, or bullying of individuals or groups of individuals 17 | 3. Engage in, promote, incite, or facilitate discrimination or other unlawful or harmful conduct in the provision of employment, employment benefits, credit, housing, other economic benefits, or other essential goods and services 18 | 4. Engage in the unauthorized or unlicensed practice of any profession including, but not limited to, financial, legal, medical/health, or related professional practices 19 | 5. Collect, process, disclose, generate, or infer health, demographic, or other sensitive personal or private information about individuals without rights and consents required by applicable laws 20 | 6. Engage in or facilitate any action or generate any content that infringes, misappropriates, or otherwise violates any third-party rights, including the outputs or results of any products or services using the Llama 3 Materials 21 | 7. Create, generate, or facilitate the creation of malicious code, malware, computer viruses or do anything else that could disable, overburden, interfere with or impair the proper working, integrity, operation or appearance of a website or computer system 22 | 23 | 24 | 25 | 2. Engage in, promote, incite, facilitate, or assist in the planning or development of activities that present a risk of death or bodily harm to individuals, including use of Llama 3 related to the following: 26 | 1. Military, warfare, nuclear industries or applications, espionage, use for materials or activities that are subject to the International Traffic Arms Regulations (ITAR) maintained by the United States Department of State 27 | 2. Guns and illegal weapons (including weapon development) 28 | 3. Illegal drugs and regulated/controlled substances 29 | 4. Operation of critical infrastructure, transportation technologies, or heavy machinery 30 | 5. Self-harm or harm to others, including suicide, cutting, and eating disorders 31 | 6. Any content intended to incite or promote violence, abuse, or any infliction of bodily harm to an individual 32 | 33 | 34 | 35 | 3. Intentionally deceive or mislead others, including use of Llama 3 related to the following: 36 | 1. Generating, promoting, or furthering fraud or the creation or promotion of disinformation 37 | 2. Generating, promoting, or furthering defamatory content, including the creation of defamatory statements, images, or other content 38 | 3. Generating, promoting, or further distributing spam 39 | 4. Impersonating another individual without consent, authorization, or legal right 40 | 5. Representing that the use of Llama 3 or outputs are human-generated 41 | 6. Generating or facilitating false online engagement, including fake reviews and other means of fake online engagement 42 | 4. Fail to appropriately disclose to end users any known dangers of your AI system 43 | 44 | Please report any violation of this Policy, software “bug,” or other problems that could lead to a violation of this Policy through one of the following means: 45 | 46 | * Reporting issues with the model: [github.com/facebookresearch/llama](http://github.com/facebookresearch/llama) 47 | * Reporting risky content generated by the model: [developers.facebook.com/llama_output_feedback](http://developers.facebook.com/llama_output_feedback) 48 | * Reporting bugs and security concerns: [facebook.com/whitehat/info](http://facebook.com/whitehat/info) 49 | * Reporting violations of the Acceptable Use Policy or unlicensed uses of Llama: [LlamaUseReport@meta.com](mailto:LlamaUseReport@meta.com) 50 | 51 | -------------------------------------------------------------------------------- /download.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright (c) Meta Platforms, Inc. and affiliates. 4 | # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. 5 | 6 | set -e 7 | 8 | read -p "Enter the URL from email: " PRESIGNED_URL 9 | echo "" 10 | read -p "Enter the list of models to download without spaces (8B,8B-instruct,70B,70B-instruct), or press Enter for all: " MODEL_SIZE 11 | TARGET_FOLDER="." # where all files should end up 12 | mkdir -p ${TARGET_FOLDER} 13 | 14 | if [[ $MODEL_SIZE == "" ]]; then 15 | MODEL_SIZE="8B,8B-instruct,70B,70B-instruct" 16 | fi 17 | 18 | echo "Downloading LICENSE and Acceptable Usage Policy" 19 | wget --continue ${PRESIGNED_URL/'*'/"LICENSE"} -O ${TARGET_FOLDER}"/LICENSE" 20 | wget --continue ${PRESIGNED_URL/'*'/"USE_POLICY"} -O ${TARGET_FOLDER}"/USE_POLICY" 21 | 22 | for m in ${MODEL_SIZE//,/ } 23 | do 24 | if [[ $m == "8B" ]] || [[ $m == "8b" ]]; then 25 | SHARD=0 26 | MODEL_FOLDER_PATH="Meta-Llama-3-8B" 27 | MODEL_PATH="8b_pre_trained" 28 | elif [[ $m == "8B-instruct" ]] || [[ $m == "8b-instruct" ]] || [[ $m == "8b-Instruct" ]] || [[ $m == "8B-Instruct" ]]; then 29 | SHARD=0 30 | MODEL_FOLDER_PATH="Meta-Llama-3-8B-Instruct" 31 | MODEL_PATH="8b_instruction_tuned" 32 | elif [[ $m == "70B" ]] || [[ $m == "70b" ]]; then 33 | SHARD=7 34 | MODEL_FOLDER_PATH="Meta-Llama-3-70B" 35 | MODEL_PATH="70b_pre_trained" 36 | elif [[ $m == "70B-instruct" ]] || [[ $m == "70b-instruct" ]] || [[ $m == "70b-Instruct" ]] || [[ $m == "70B-Instruct" ]]; then 37 | SHARD=7 38 | MODEL_FOLDER_PATH="Meta-Llama-3-70B-Instruct" 39 | MODEL_PATH="70b_instruction_tuned" 40 | fi 41 | 42 | echo "Downloading ${MODEL_PATH}" 43 | mkdir -p ${TARGET_FOLDER}"/${MODEL_FOLDER_PATH}" 44 | 45 | for s in $(seq -f "0%g" 0 ${SHARD}) 46 | do 47 | wget --continue ${PRESIGNED_URL/'*'/"${MODEL_PATH}/consolidated.${s}.pth"} -O ${TARGET_FOLDER}"/${MODEL_FOLDER_PATH}/consolidated.${s}.pth" 48 | done 49 | 50 | wget --continue ${PRESIGNED_URL/'*'/"${MODEL_PATH}/params.json"} -O ${TARGET_FOLDER}"/${MODEL_FOLDER_PATH}/params.json" 51 | wget --continue ${PRESIGNED_URL/'*'/"${MODEL_PATH}/tokenizer.model"} -O ${TARGET_FOLDER}"/${MODEL_FOLDER_PATH}/tokenizer.model" 52 | wget --continue ${PRESIGNED_URL/'*'/"${MODEL_PATH}/checklist.chk"} -O ${TARGET_FOLDER}"/${MODEL_FOLDER_PATH}/checklist.chk" 53 | echo "Checking checksums" 54 | if [ "$CPU_ARCH" = "arm64" ]; then 55 | (cd ${TARGET_FOLDER}"/${MODEL_FOLDER_PATH}" && md5 checklist.chk) 56 | else 57 | (cd ${TARGET_FOLDER}"/${MODEL_FOLDER_PATH}" && md5sum -c checklist.chk) 58 | fi 59 | done 60 | -------------------------------------------------------------------------------- /eval_details.md: -------------------------------------------------------------------------------- 1 | ### Llama 3 Evaluation Details 2 | This document contains additional context on the settings and parameters for how we evaluated the Llama 3 pre-trained and instruct-aligned models. 3 | ### Auto-eval benchmark notes 4 | #### MMLU 5 | - We are reporting macro averages for MMLU benchmarks. The micro average numbers for MMLU are: 65.4 and 67.4 for the 8B pre-trained and instruct-aligned models, 78.9 and 82.0 for the 70B pre-trained and instruct-aligned models 6 | - For the instruct-aligned MMLU we ask the model to generate the best choice character 7 | #### AGI English 8 | - We use the default few-shot and prompt settings as specified [here](https://github.com/ruixiangcui/AGIEval). The score is averaged over the english subtasks. 9 | #### CommonSenseQA 10 | - We use the same 7-shot chain-of-thought prompt as in [Wei et al. (2022)](https://arxiv.org/pdf/2201.11903.pdf). 11 | #### Winogrande 12 | - We use a choice based setup for evaluation where we fill in the missing blank with the two possible choices and then compute log-likelihood over the suffix. We use 5 shots for evaluation. 13 | #### BIG-Bench Hard 14 | - We use a 3-shot chain of thought style prompting and compute the average exact match over the subsets in this task. 15 | #### ARC-Challenge 16 | - We use the arc-challenge subset from the arc benchmark. We use 25 shots and use the MMLU setup for evaluation where we provide all the choices in the prompt and calculate likelihood over choice characters 17 | #### TriviaQA-WIKI 18 | - We evaluate on the Wiki validation set and use 5 few-shot examples. 19 | #### SQuAD 20 | - We are using SQuAD v2 and compute exact match in a 1-shot setting. 21 | #### QuAC 22 | - Same setting as Llama 2 (1-shot, f1). 23 | #### BoolQ 24 | - Same setting as Llama 1 and Llama 2 (0-shot, accuracy). 25 | #### DROP 26 | - For each validation example, we draw 3 random few-shot examples from the train split. 27 | #### GPQA 28 | - We report 0-shot exact match scores over the possible options using the Main subset for our models and other open-source models (Mistral, Gemma). 29 | #### HumanEval 30 | - Same setting as Llama 1 and Llama 2 (pass@1). 31 | #### GSM8K 32 | - We use the same 8-shot chain-of-thought prompt as in [Wei et al. (2022)](https://arxiv.org/pdf/2201.11903.pdf) (maj@1). 33 | #### MATH 34 | - We use the 4-shot problem available in [Lewkowycz et al. (2022)](https://arxiv.org/pdf/2206.14858.pdf) (maj@1). 35 | ### Human evaluation notes 36 | This evaluation set contains 1,800 prompts that cover 12 key use cases: asking for advice, brainstorming, classification, closed question answering, coding, creative writing, extraction, inhabiting a character/persona, open question answering, reasoning, rewriting, and summarization. 37 | |Category|Count| 38 | |--------|-----| 39 | |Coding|150| 40 | |Mathematical reasoning|150| 41 | |Asking for Advice|150| 42 | |Brainstorming|150| 43 | |Classification|150| 44 | |Closed Question Answering|150| 45 | |Creative Writing|150| 46 | |Extraction|150| 47 | |Inhabiting a Character/Persona|150| 48 | |Open Question Answering|150| 49 | |Rewriting|150| 50 | |Summarization|150| 51 | -------------------------------------------------------------------------------- /example_chat_completion.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # This software may be used and distributed in accordance with the terms of the Llama 3 Community License Agreement. 3 | 4 | from typing import List, Optional 5 | 6 | import fire 7 | 8 | from llama import Dialog, Llama 9 | 10 | 11 | def main( 12 | ckpt_dir: str, 13 | tokenizer_path: str, 14 | temperature: float = 0.6, 15 | top_p: float = 0.9, 16 | max_seq_len: int = 512, 17 | max_batch_size: int = 4, 18 | max_gen_len: Optional[int] = None, 19 | ): 20 | """ 21 | Examples to run with the models finetuned for chat. Prompts correspond of chat 22 | turns between the user and assistant with the final one always being the user. 23 | 24 | An optional system prompt at the beginning to control how the model should respond 25 | is also supported. 26 | 27 | The context window of llama3 models is 8192 tokens, so `max_seq_len` needs to be <= 8192. 28 | 29 | `max_gen_len` is optional because finetuned models are able to stop generations naturally. 30 | """ 31 | generator = Llama.build( 32 | ckpt_dir=ckpt_dir, 33 | tokenizer_path=tokenizer_path, 34 | max_seq_len=max_seq_len, 35 | max_batch_size=max_batch_size, 36 | ) 37 | 38 | dialogs: List[Dialog] = [ 39 | [{"role": "user", "content": "what is the recipe of mayonnaise?"}], 40 | [ 41 | {"role": "user", "content": "I am going to Paris, what should I see?"}, 42 | { 43 | "role": "assistant", 44 | "content": """\ 45 | Paris, the capital of France, is known for its stunning architecture, art museums, historical landmarks, and romantic atmosphere. Here are some of the top attractions to see in Paris: 46 | 47 | 1. The Eiffel Tower: The iconic Eiffel Tower is one of the most recognizable landmarks in the world and offers breathtaking views of the city. 48 | 2. The Louvre Museum: The Louvre is one of the world's largest and most famous museums, housing an impressive collection of art and artifacts, including the Mona Lisa. 49 | 3. Notre-Dame Cathedral: This beautiful cathedral is one of the most famous landmarks in Paris and is known for its Gothic architecture and stunning stained glass windows. 50 | 51 | These are just a few of the many attractions that Paris has to offer. With so much to see and do, it's no wonder that Paris is one of the most popular tourist destinations in the world.""", 52 | }, 53 | {"role": "user", "content": "What is so great about #1?"}, 54 | ], 55 | [ 56 | {"role": "system", "content": "Always answer with Haiku"}, 57 | {"role": "user", "content": "I am going to Paris, what should I see?"}, 58 | ], 59 | [ 60 | { 61 | "role": "system", 62 | "content": "Always answer with emojis", 63 | }, 64 | {"role": "user", "content": "How to go from Beijing to NY?"}, 65 | ], 66 | ] 67 | results = generator.chat_completion( 68 | dialogs, 69 | max_gen_len=max_gen_len, 70 | temperature=temperature, 71 | top_p=top_p, 72 | ) 73 | 74 | for dialog, result in zip(dialogs, results): 75 | for msg in dialog: 76 | print(f"{msg['role'].capitalize()}: {msg['content']}\n") 77 | print( 78 | f"> {result['generation']['role'].capitalize()}: {result['generation']['content']}" 79 | ) 80 | print("\n==================================\n") 81 | 82 | 83 | if __name__ == "__main__": 84 | fire.Fire(main) 85 | -------------------------------------------------------------------------------- /example_text_completion.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # This software may be used and distributed in accordance with the terms of the Llama 3 Community License Agreement. 3 | 4 | from typing import List 5 | 6 | import fire 7 | 8 | from llama import Llama 9 | 10 | 11 | def main( 12 | ckpt_dir: str, 13 | tokenizer_path: str, 14 | temperature: float = 0.6, 15 | top_p: float = 0.9, 16 | max_seq_len: int = 128, 17 | max_gen_len: int = 64, 18 | max_batch_size: int = 4, 19 | ): 20 | """ 21 | Examples to run with the pre-trained models (no fine-tuning). Prompts are 22 | usually in the form of an incomplete text prefix that the model can then try to complete. 23 | 24 | The context window of llama3 models is 8192 tokens, so `max_seq_len` needs to be <= 8192. 25 | `max_gen_len` is needed because pre-trained models usually do not stop completions naturally. 26 | """ 27 | generator = Llama.build( 28 | ckpt_dir=ckpt_dir, 29 | tokenizer_path=tokenizer_path, 30 | max_seq_len=max_seq_len, 31 | max_batch_size=max_batch_size, 32 | ) 33 | 34 | prompts: List[str] = [ 35 | # For these prompts, the expected answer is the natural continuation of the prompt 36 | "I believe the meaning of life is", 37 | "Simply put, the theory of relativity states that ", 38 | """A brief message congratulating the team on the launch: 39 | 40 | Hi everyone, 41 | 42 | I just """, 43 | # Few shot prompt (providing a few examples before asking model to complete more); 44 | """Translate English to French: 45 | 46 | sea otter => loutre de mer 47 | peppermint => menthe poivrée 48 | plush girafe => girafe peluche 49 | cheese =>""", 50 | ] 51 | results = generator.text_completion( 52 | prompts, 53 | max_gen_len=max_gen_len, 54 | temperature=temperature, 55 | top_p=top_p, 56 | ) 57 | for prompt, result in zip(prompts, results): 58 | print(prompt) 59 | print(f"> {result['generation']}") 60 | print("\n==================================\n") 61 | 62 | 63 | if __name__ == "__main__": 64 | fire.Fire(main) 65 | -------------------------------------------------------------------------------- /llama/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # This software may be used and distributed in accordance with the terms of the Llama 3 Community License Agreement. 3 | 4 | from .generation import Llama 5 | from .model import ModelArgs, Transformer 6 | from .tokenizer import Dialog, Tokenizer 7 | -------------------------------------------------------------------------------- /llama/generation.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # This software may be used and distributed in accordance with the terms of the Llama 3 Community License Agreement. 3 | 4 | import json 5 | import os 6 | import sys 7 | import time 8 | from pathlib import Path 9 | from typing import List, Optional, Tuple, TypedDict 10 | 11 | import torch 12 | import torch.nn.functional as F 13 | from fairscale.nn.model_parallel.initialize import ( 14 | get_model_parallel_rank, 15 | initialize_model_parallel, 16 | model_parallel_is_initialized, 17 | ) 18 | 19 | from llama.model import ModelArgs, Transformer 20 | from llama.tokenizer import ChatFormat, Dialog, Message, Tokenizer 21 | 22 | 23 | class CompletionPrediction(TypedDict, total=False): 24 | generation: str 25 | tokens: List[str] # not required 26 | logprobs: List[float] # not required 27 | 28 | 29 | class ChatPrediction(TypedDict, total=False): 30 | generation: Message 31 | tokens: List[str] # not required 32 | logprobs: List[float] # not required 33 | 34 | 35 | class Llama: 36 | @staticmethod 37 | def build( 38 | ckpt_dir: str, 39 | tokenizer_path: str, 40 | max_seq_len: int, 41 | max_batch_size: int, 42 | model_parallel_size: Optional[int] = None, 43 | seed: int = 1, 44 | ) -> "Llama": 45 | """ 46 | Build a Llama instance by initializing and loading a model checkpoint. 47 | 48 | Args: 49 | ckpt_dir (str): Path to the directory containing checkpoint files. 50 | tokenizer_path (str): Path to the tokenizer file. 51 | max_seq_len (int): Maximum sequence length for input text. 52 | max_batch_size (int): Maximum batch size for inference. 53 | model_parallel_size (Optional[int], optional): Number of model parallel processes. 54 | If not provided, it's determined from the environment. Defaults to None. 55 | 56 | Returns: 57 | Llama: An instance of the Llama class with the loaded model and tokenizer. 58 | 59 | Raises: 60 | AssertionError: If there are no checkpoint files in the specified directory, 61 | or if the model parallel size does not match the number of checkpoint files. 62 | 63 | Note: 64 | This method initializes the distributed process group, sets the device to CUDA, 65 | and loads the pre-trained model and tokenizer. 66 | """ 67 | if not torch.distributed.is_initialized(): 68 | torch.distributed.init_process_group("nccl") 69 | if not model_parallel_is_initialized(): 70 | if model_parallel_size is None: 71 | model_parallel_size = int(os.environ.get("WORLD_SIZE", 1)) 72 | initialize_model_parallel(model_parallel_size) 73 | 74 | local_rank = int(os.environ.get("LOCAL_RANK", 0)) 75 | torch.cuda.set_device(local_rank) 76 | 77 | # seed must be the same in all processes 78 | torch.manual_seed(seed) 79 | 80 | if local_rank > 0: 81 | sys.stdout = open(os.devnull, "w") 82 | 83 | start_time = time.time() 84 | checkpoints = sorted(Path(ckpt_dir).glob("*.pth")) 85 | assert len(checkpoints) > 0, f"no checkpoint files found in {ckpt_dir}" 86 | assert model_parallel_size == len( 87 | checkpoints 88 | ), f"Loading a checkpoint for MP={len(checkpoints)} but world size is {model_parallel_size}" 89 | ckpt_path = checkpoints[get_model_parallel_rank()] 90 | checkpoint = torch.load(ckpt_path, map_location="cpu") 91 | with open(Path(ckpt_dir) / "params.json", "r") as f: 92 | params = json.loads(f.read()) 93 | 94 | model_args: ModelArgs = ModelArgs( 95 | max_seq_len=max_seq_len, 96 | max_batch_size=max_batch_size, 97 | **params, 98 | ) 99 | tokenizer = Tokenizer(model_path=tokenizer_path) 100 | assert model_args.vocab_size == tokenizer.n_words 101 | if torch.cuda.is_bf16_supported(): 102 | torch.set_default_tensor_type(torch.cuda.BFloat16Tensor) 103 | else: 104 | torch.set_default_tensor_type(torch.cuda.HalfTensor) 105 | model = Transformer(model_args) 106 | model.load_state_dict(checkpoint, strict=False) 107 | print(f"Loaded in {time.time() - start_time:.2f} seconds") 108 | 109 | return Llama(model, tokenizer) 110 | 111 | def __init__(self, model: Transformer, tokenizer: Tokenizer): 112 | self.model = model 113 | self.tokenizer = tokenizer 114 | self.formatter = ChatFormat(tokenizer) 115 | 116 | @torch.inference_mode() 117 | def generate( 118 | self, 119 | prompt_tokens: List[List[int]], 120 | max_gen_len: int, 121 | temperature: float = 0.6, 122 | top_p: float = 0.9, 123 | logprobs: bool = False, 124 | echo: bool = False, 125 | ) -> Tuple[List[List[int]], Optional[List[List[float]]]]: 126 | """ 127 | Generate text sequences based on provided prompts using the language generation model. 128 | 129 | Args: 130 | prompt_tokens (List[List[int]]): List of tokenized prompts, where each prompt is represented as a list of integers. 131 | max_gen_len (int): Maximum length of the generated text sequence. 132 | temperature (float, optional): Temperature value for controlling randomness in sampling. Defaults to 0.6. 133 | top_p (float, optional): Top-p probability threshold for nucleus sampling. Defaults to 0.9. 134 | logprobs (bool, optional): Flag indicating whether to compute token log probabilities. Defaults to False. 135 | echo (bool, optional): Flag indicating whether to include prompt tokens in the generated output. Defaults to False. 136 | 137 | Returns: 138 | Tuple[List[List[int]], Optional[List[List[float]]]]: A tuple containing generated token sequences and, if logprobs is True, corresponding token log probabilities. 139 | 140 | Note: 141 | This method uses the provided prompts as a basis for generating text. It employs nucleus sampling to produce text with controlled randomness. 142 | If logprobs is True, token log probabilities are computed for each generated token. 143 | 144 | """ 145 | params = self.model.params 146 | bsz = len(prompt_tokens) 147 | assert bsz <= params.max_batch_size, (bsz, params.max_batch_size) 148 | 149 | min_prompt_len = min(len(t) for t in prompt_tokens) 150 | max_prompt_len = max(len(t) for t in prompt_tokens) 151 | assert max_prompt_len <= params.max_seq_len 152 | total_len = min(params.max_seq_len, max_gen_len + max_prompt_len) 153 | 154 | pad_id = self.tokenizer.pad_id 155 | tokens = torch.full((bsz, total_len), pad_id, dtype=torch.long, device="cuda") 156 | for k, t in enumerate(prompt_tokens): 157 | tokens[k, : len(t)] = torch.tensor(t, dtype=torch.long, device="cuda") 158 | if logprobs: 159 | token_logprobs = torch.zeros_like(tokens, dtype=torch.float) 160 | 161 | prev_pos = 0 162 | eos_reached = torch.tensor([False] * bsz, device="cuda") 163 | input_text_mask = tokens != pad_id 164 | if min_prompt_len == total_len: 165 | logits = self.model.forward(tokens, prev_pos) 166 | token_logprobs = -F.cross_entropy( 167 | input=logits.transpose(1, 2), 168 | target=tokens, 169 | reduction="none", 170 | ignore_index=pad_id, 171 | ) 172 | 173 | stop_tokens = torch.tensor(list(self.tokenizer.stop_tokens)) 174 | 175 | for cur_pos in range(min_prompt_len, total_len): 176 | logits = self.model.forward(tokens[:, prev_pos:cur_pos], prev_pos) 177 | if temperature > 0: 178 | probs = torch.softmax(logits[:, -1] / temperature, dim=-1) 179 | next_token = sample_top_p(probs, top_p) 180 | else: 181 | next_token = torch.argmax(logits[:, -1], dim=-1) 182 | 183 | next_token = next_token.reshape(-1) 184 | # only replace token if prompt has already been generated 185 | next_token = torch.where( 186 | input_text_mask[:, cur_pos], tokens[:, cur_pos], next_token 187 | ) 188 | tokens[:, cur_pos] = next_token 189 | if logprobs: 190 | token_logprobs[:, prev_pos + 1 : cur_pos + 1] = -F.cross_entropy( 191 | input=logits.transpose(1, 2), 192 | target=tokens[:, prev_pos + 1 : cur_pos + 1], 193 | reduction="none", 194 | ignore_index=pad_id, 195 | ) 196 | eos_reached |= (~input_text_mask[:, cur_pos]) & ( 197 | torch.isin(next_token, stop_tokens) 198 | ) 199 | prev_pos = cur_pos 200 | if all(eos_reached): 201 | break 202 | 203 | if logprobs: 204 | token_logprobs = token_logprobs.tolist() 205 | out_tokens, out_logprobs = [], [] 206 | for i, toks in enumerate(tokens.tolist()): 207 | # cut to max gen len 208 | start = 0 if echo else len(prompt_tokens[i]) 209 | toks = toks[start : len(prompt_tokens[i]) + max_gen_len] 210 | probs = None 211 | if logprobs: 212 | probs = token_logprobs[i][start : len(prompt_tokens[i]) + max_gen_len] 213 | # cut to after eos tok if any 214 | for stop_token in self.tokenizer.stop_tokens: 215 | try: 216 | eos_idx = toks.index(stop_token) 217 | toks = toks[:eos_idx] 218 | probs = probs[:eos_idx] if logprobs else None 219 | except ValueError: 220 | pass 221 | out_tokens.append(toks) 222 | out_logprobs.append(probs) 223 | return (out_tokens, out_logprobs if logprobs else None) 224 | 225 | def text_completion( 226 | self, 227 | prompts: List[str], 228 | temperature: float = 0.6, 229 | top_p: float = 0.9, 230 | max_gen_len: Optional[int] = None, 231 | logprobs: bool = False, 232 | echo: bool = False, 233 | ) -> List[CompletionPrediction]: 234 | """ 235 | Perform text completion for a list of prompts using the language generation model. 236 | 237 | Args: 238 | prompts (List[str]): List of text prompts for completion. 239 | temperature (float, optional): Temperature value for controlling randomness in sampling. Defaults to 0.6. 240 | top_p (float, optional): Top-p probability threshold for nucleus sampling. Defaults to 0.9. 241 | max_gen_len (Optional[int], optional): Maximum length of the generated completion sequence. 242 | If not provided, it's set to the model's maximum sequence length minus 1. 243 | logprobs (bool, optional): Flag indicating whether to compute token log probabilities. Defaults to False. 244 | echo (bool, optional): Flag indicating whether to include prompt tokens in the generated output. Defaults to False. 245 | 246 | Returns: 247 | List[CompletionPrediction]: List of completion predictions, each containing the generated text completion. 248 | 249 | Note: 250 | This method generates text completions for the provided prompts, employing nucleus sampling to introduce controlled randomness. 251 | If logprobs is True, token log probabilities are computed for each generated token. 252 | 253 | """ 254 | if max_gen_len is None: 255 | max_gen_len = self.model.params.max_seq_len - 1 256 | prompt_tokens = [self.tokenizer.encode(x, bos=True, eos=False) for x in prompts] 257 | generation_tokens, generation_logprobs = self.generate( 258 | prompt_tokens=prompt_tokens, 259 | max_gen_len=max_gen_len, 260 | temperature=temperature, 261 | top_p=top_p, 262 | logprobs=logprobs, 263 | echo=echo, 264 | ) 265 | if logprobs: 266 | return [ 267 | { 268 | "generation": self.tokenizer.decode(t), 269 | "tokens": [self.tokenizer.decode([x]) for x in t], 270 | "logprobs": logprobs_i, 271 | } 272 | for t, logprobs_i in zip(generation_tokens, generation_logprobs) 273 | ] 274 | return [{"generation": self.tokenizer.decode(t)} for t in generation_tokens] 275 | 276 | def chat_completion( 277 | self, 278 | dialogs: List[Dialog], 279 | temperature: float = 0.6, 280 | top_p: float = 0.9, 281 | max_gen_len: Optional[int] = None, 282 | logprobs: bool = False, 283 | ) -> List[ChatPrediction]: 284 | """ 285 | Generate assistant responses for a list of conversational dialogs using the language generation model. 286 | 287 | Args: 288 | dialogs (List[Dialog]): List of conversational dialogs, where each dialog is a list of messages. 289 | temperature (float, optional): Temperature value for controlling randomness in sampling. Defaults to 0.6. 290 | top_p (float, optional): Top-p probability threshold for nucleus sampling. Defaults to 0.9. 291 | max_gen_len (Optional[int], optional): Maximum length of the generated response sequence. 292 | If not provided, it's set to the model's maximum sequence length minus 1. 293 | logprobs (bool, optional): Flag indicating whether to compute token log probabilities. Defaults to False. 294 | 295 | Returns: 296 | List[ChatPrediction]: List of chat predictions, each containing the assistant's generated response. 297 | 298 | Note: 299 | This method generates assistant responses for the provided conversational dialogs. 300 | It employs nucleus sampling to introduce controlled randomness in text generation. 301 | If logprobs is True, token log probabilities are computed for each generated token. 302 | """ 303 | if max_gen_len is None: 304 | max_gen_len = self.model.params.max_seq_len - 1 305 | 306 | prompt_tokens = [ 307 | self.formatter.encode_dialog_prompt(dialog) for dialog in dialogs 308 | ] 309 | generation_tokens, generation_logprobs = self.generate( 310 | prompt_tokens=prompt_tokens, 311 | max_gen_len=max_gen_len, 312 | temperature=temperature, 313 | top_p=top_p, 314 | logprobs=logprobs, 315 | ) 316 | if logprobs: 317 | return [ 318 | { 319 | "generation": { 320 | "role": "assistant", 321 | "content": self.tokenizer.decode(t), 322 | }, 323 | "tokens": [self.tokenizer.decode([x]) for x in t], 324 | "logprobs": logprobs_i, 325 | } 326 | for t, logprobs_i in zip(generation_tokens, generation_logprobs) 327 | ] 328 | return [ 329 | { 330 | "generation": { 331 | "role": "assistant", 332 | "content": self.tokenizer.decode(t), 333 | }, 334 | } 335 | for t in generation_tokens 336 | ] 337 | 338 | 339 | def sample_top_p(probs, p): 340 | """ 341 | Perform top-p (nucleus) sampling on a probability distribution. 342 | 343 | Args: 344 | probs (torch.Tensor): Probability distribution tensor. 345 | p (float): Probability threshold for top-p sampling. 346 | 347 | Returns: 348 | torch.Tensor: Sampled token indices. 349 | 350 | Note: 351 | Top-p sampling selects the smallest set of tokens whose cumulative probability mass 352 | exceeds the threshold p. The distribution is renormalized based on the selected tokens. 353 | """ 354 | probs_sort, probs_idx = torch.sort(probs, dim=-1, descending=True) 355 | probs_sum = torch.cumsum(probs_sort, dim=-1) 356 | mask = probs_sum - probs_sort > p 357 | probs_sort[mask] = 0.0 358 | probs_sort.div_(probs_sort.sum(dim=-1, keepdim=True)) 359 | next_token = torch.multinomial(probs_sort, num_samples=1) 360 | next_token = torch.gather(probs_idx, -1, next_token) 361 | return next_token 362 | -------------------------------------------------------------------------------- /llama/model.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # This software may be used and distributed in accordance with the terms of the Llama 3 Community License Agreement. 3 | 4 | import math 5 | from dataclasses import dataclass 6 | from typing import Optional, Tuple 7 | 8 | import fairscale.nn.model_parallel.initialize as fs_init 9 | import torch 10 | import torch.nn.functional as F 11 | from fairscale.nn.model_parallel.layers import ( 12 | ColumnParallelLinear, 13 | RowParallelLinear, 14 | VocabParallelEmbedding, 15 | ) 16 | from torch import nn 17 | 18 | 19 | @dataclass 20 | class ModelArgs: 21 | dim: int = 4096 22 | n_layers: int = 32 23 | n_heads: int = 32 24 | n_kv_heads: Optional[int] = None 25 | vocab_size: int = -1 26 | multiple_of: int = 256 # make SwiGLU hidden layer size multiple of large power of 2 27 | ffn_dim_multiplier: Optional[float] = None 28 | norm_eps: float = 1e-5 29 | rope_theta: float = 500000 30 | 31 | max_batch_size: int = 32 32 | max_seq_len: int = 2048 33 | 34 | 35 | class RMSNorm(torch.nn.Module): 36 | def __init__(self, dim: int, eps: float = 1e-6): 37 | super().__init__() 38 | self.eps = eps 39 | self.weight = nn.Parameter(torch.ones(dim)) 40 | 41 | def _norm(self, x): 42 | return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) 43 | 44 | def forward(self, x): 45 | output = self._norm(x.float()).type_as(x) 46 | return output * self.weight 47 | 48 | 49 | def precompute_freqs_cis(dim: int, end: int, theta: float = 10000.0): 50 | freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim)) 51 | t = torch.arange(end, device=freqs.device, dtype=torch.float32) 52 | freqs = torch.outer(t, freqs) 53 | freqs_cis = torch.polar(torch.ones_like(freqs), freqs) # complex64 54 | return freqs_cis 55 | 56 | 57 | def reshape_for_broadcast(freqs_cis: torch.Tensor, x: torch.Tensor): 58 | ndim = x.ndim 59 | assert 0 <= 1 < ndim 60 | assert freqs_cis.shape == (x.shape[1], x.shape[-1]) 61 | shape = [d if i == 1 or i == ndim - 1 else 1 for i, d in enumerate(x.shape)] 62 | return freqs_cis.view(*shape) 63 | 64 | 65 | def apply_rotary_emb( 66 | xq: torch.Tensor, 67 | xk: torch.Tensor, 68 | freqs_cis: torch.Tensor, 69 | ) -> Tuple[torch.Tensor, torch.Tensor]: 70 | xq_ = torch.view_as_complex(xq.float().reshape(*xq.shape[:-1], -1, 2)) 71 | xk_ = torch.view_as_complex(xk.float().reshape(*xk.shape[:-1], -1, 2)) 72 | freqs_cis = reshape_for_broadcast(freqs_cis, xq_) 73 | xq_out = torch.view_as_real(xq_ * freqs_cis).flatten(3) 74 | xk_out = torch.view_as_real(xk_ * freqs_cis).flatten(3) 75 | return xq_out.type_as(xq), xk_out.type_as(xk) 76 | 77 | 78 | def repeat_kv(x: torch.Tensor, n_rep: int) -> torch.Tensor: 79 | """torch.repeat_interleave(x, dim=2, repeats=n_rep)""" 80 | bs, slen, n_kv_heads, head_dim = x.shape 81 | if n_rep == 1: 82 | return x 83 | return ( 84 | x[:, :, :, None, :] 85 | .expand(bs, slen, n_kv_heads, n_rep, head_dim) 86 | .reshape(bs, slen, n_kv_heads * n_rep, head_dim) 87 | ) 88 | 89 | 90 | class Attention(nn.Module): 91 | def __init__(self, args: ModelArgs): 92 | super().__init__() 93 | self.n_kv_heads = args.n_heads if args.n_kv_heads is None else args.n_kv_heads 94 | model_parallel_size = fs_init.get_model_parallel_world_size() 95 | self.n_local_heads = args.n_heads // model_parallel_size 96 | self.n_local_kv_heads = self.n_kv_heads // model_parallel_size 97 | self.n_rep = self.n_local_heads // self.n_local_kv_heads 98 | self.head_dim = args.dim // args.n_heads 99 | 100 | self.wq = ColumnParallelLinear( 101 | args.dim, 102 | args.n_heads * self.head_dim, 103 | bias=False, 104 | gather_output=False, 105 | init_method=lambda x: x, 106 | ) 107 | self.wk = ColumnParallelLinear( 108 | args.dim, 109 | self.n_kv_heads * self.head_dim, 110 | bias=False, 111 | gather_output=False, 112 | init_method=lambda x: x, 113 | ) 114 | self.wv = ColumnParallelLinear( 115 | args.dim, 116 | self.n_kv_heads * self.head_dim, 117 | bias=False, 118 | gather_output=False, 119 | init_method=lambda x: x, 120 | ) 121 | self.wo = RowParallelLinear( 122 | args.n_heads * self.head_dim, 123 | args.dim, 124 | bias=False, 125 | input_is_parallel=True, 126 | init_method=lambda x: x, 127 | ) 128 | 129 | self.cache_k = torch.zeros( 130 | ( 131 | args.max_batch_size, 132 | args.max_seq_len, 133 | self.n_local_kv_heads, 134 | self.head_dim, 135 | ) 136 | ).cuda() 137 | self.cache_v = torch.zeros( 138 | ( 139 | args.max_batch_size, 140 | args.max_seq_len, 141 | self.n_local_kv_heads, 142 | self.head_dim, 143 | ) 144 | ).cuda() 145 | 146 | def forward( 147 | self, 148 | x: torch.Tensor, 149 | start_pos: int, 150 | freqs_cis: torch.Tensor, 151 | mask: Optional[torch.Tensor], 152 | ): 153 | bsz, seqlen, _ = x.shape 154 | xq, xk, xv = self.wq(x), self.wk(x), self.wv(x) 155 | 156 | xq = xq.view(bsz, seqlen, self.n_local_heads, self.head_dim) 157 | xk = xk.view(bsz, seqlen, self.n_local_kv_heads, self.head_dim) 158 | xv = xv.view(bsz, seqlen, self.n_local_kv_heads, self.head_dim) 159 | 160 | xq, xk = apply_rotary_emb(xq, xk, freqs_cis=freqs_cis) 161 | 162 | self.cache_k = self.cache_k.to(xq) 163 | self.cache_v = self.cache_v.to(xq) 164 | 165 | self.cache_k[:bsz, start_pos : start_pos + seqlen] = xk 166 | self.cache_v[:bsz, start_pos : start_pos + seqlen] = xv 167 | 168 | keys = self.cache_k[:bsz, : start_pos + seqlen] 169 | values = self.cache_v[:bsz, : start_pos + seqlen] 170 | 171 | # repeat k/v heads if n_kv_heads < n_heads 172 | keys = repeat_kv( 173 | keys, self.n_rep 174 | ) # (bs, cache_len + seqlen, n_local_heads, head_dim) 175 | values = repeat_kv( 176 | values, self.n_rep 177 | ) # (bs, cache_len + seqlen, n_local_heads, head_dim) 178 | 179 | xq = xq.transpose(1, 2) # (bs, n_local_heads, seqlen, head_dim) 180 | keys = keys.transpose(1, 2) # (bs, n_local_heads, cache_len + seqlen, head_dim) 181 | values = values.transpose( 182 | 1, 2 183 | ) # (bs, n_local_heads, cache_len + seqlen, head_dim) 184 | scores = torch.matmul(xq, keys.transpose(2, 3)) / math.sqrt(self.head_dim) 185 | if mask is not None: 186 | scores = scores + mask # (bs, n_local_heads, seqlen, cache_len + seqlen) 187 | scores = F.softmax(scores.float(), dim=-1).type_as(xq) 188 | output = torch.matmul(scores, values) # (bs, n_local_heads, seqlen, head_dim) 189 | output = output.transpose(1, 2).contiguous().view(bsz, seqlen, -1) 190 | return self.wo(output) 191 | 192 | 193 | class FeedForward(nn.Module): 194 | def __init__( 195 | self, 196 | dim: int, 197 | hidden_dim: int, 198 | multiple_of: int, 199 | ffn_dim_multiplier: Optional[float], 200 | ): 201 | super().__init__() 202 | hidden_dim = int(2 * hidden_dim / 3) 203 | # custom dim factor multiplier 204 | if ffn_dim_multiplier is not None: 205 | hidden_dim = int(ffn_dim_multiplier * hidden_dim) 206 | hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of) 207 | 208 | self.w1 = ColumnParallelLinear( 209 | dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x 210 | ) 211 | self.w2 = RowParallelLinear( 212 | hidden_dim, dim, bias=False, input_is_parallel=True, init_method=lambda x: x 213 | ) 214 | self.w3 = ColumnParallelLinear( 215 | dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x 216 | ) 217 | 218 | def forward(self, x): 219 | return self.w2(F.silu(self.w1(x)) * self.w3(x)) 220 | 221 | 222 | class TransformerBlock(nn.Module): 223 | def __init__(self, layer_id: int, args: ModelArgs): 224 | super().__init__() 225 | self.n_heads = args.n_heads 226 | self.dim = args.dim 227 | self.head_dim = args.dim // args.n_heads 228 | self.attention = Attention(args) 229 | self.feed_forward = FeedForward( 230 | dim=args.dim, 231 | hidden_dim=4 * args.dim, 232 | multiple_of=args.multiple_of, 233 | ffn_dim_multiplier=args.ffn_dim_multiplier, 234 | ) 235 | self.layer_id = layer_id 236 | self.attention_norm = RMSNorm(args.dim, eps=args.norm_eps) 237 | self.ffn_norm = RMSNorm(args.dim, eps=args.norm_eps) 238 | 239 | def forward( 240 | self, 241 | x: torch.Tensor, 242 | start_pos: int, 243 | freqs_cis: torch.Tensor, 244 | mask: Optional[torch.Tensor], 245 | ): 246 | h = x + self.attention(self.attention_norm(x), start_pos, freqs_cis, mask) 247 | out = h + self.feed_forward(self.ffn_norm(h)) 248 | return out 249 | 250 | 251 | class Transformer(nn.Module): 252 | def __init__(self, params: ModelArgs): 253 | super().__init__() 254 | self.params = params 255 | self.vocab_size = params.vocab_size 256 | self.n_layers = params.n_layers 257 | 258 | self.tok_embeddings = VocabParallelEmbedding( 259 | params.vocab_size, params.dim, init_method=lambda x: x 260 | ) 261 | 262 | self.layers = torch.nn.ModuleList() 263 | for layer_id in range(params.n_layers): 264 | self.layers.append(TransformerBlock(layer_id, params)) 265 | 266 | self.norm = RMSNorm(params.dim, eps=params.norm_eps) 267 | self.output = ColumnParallelLinear( 268 | params.dim, params.vocab_size, bias=False, init_method=lambda x: x 269 | ) 270 | 271 | self.freqs_cis = precompute_freqs_cis( 272 | params.dim // params.n_heads, 273 | params.max_seq_len * 2, 274 | params.rope_theta, 275 | ) 276 | 277 | @torch.inference_mode() 278 | def forward(self, tokens: torch.Tensor, start_pos: int): 279 | _bsz, seqlen = tokens.shape 280 | h = self.tok_embeddings(tokens) 281 | self.freqs_cis = self.freqs_cis.to(h.device) 282 | freqs_cis = self.freqs_cis[start_pos : start_pos + seqlen] 283 | 284 | mask = None 285 | if seqlen > 1: 286 | mask = torch.full((seqlen, seqlen), float("-inf"), device=tokens.device) 287 | 288 | mask = torch.triu(mask, diagonal=1) 289 | 290 | # When performing key-value caching, we compute the attention scores 291 | # only for the new sequence. Thus, the matrix of scores is of size 292 | # (seqlen, cache_len + seqlen), and the only masked entries are (i, j) for 293 | # j > cache_len + i, since row i corresponds to token cache_len + i. 294 | mask = torch.hstack( 295 | [torch.zeros((seqlen, start_pos), device=tokens.device), mask] 296 | ).type_as(h) 297 | 298 | for layer in self.layers: 299 | h = layer(h, start_pos, freqs_cis, mask) 300 | h = self.norm(h) 301 | output = self.output(h).float() 302 | return output 303 | -------------------------------------------------------------------------------- /llama/test_tokenizer.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # This software may be used and distributed in accordance with the terms of the Llama 3 Community License Agreement. 3 | 4 | import os 5 | from unittest import TestCase 6 | from llama.tokenizer import ChatFormat, Tokenizer 7 | 8 | # TOKENIZER_PATH= python -m unittest llama/test_tokenizer.py 9 | 10 | class TokenizerTests(TestCase): 11 | def setUp(self): 12 | self.tokenizer = Tokenizer(os.environ["TOKENIZER_PATH"]) 13 | self.format = ChatFormat(self.tokenizer) 14 | 15 | def test_special_tokens(self): 16 | self.assertEqual( 17 | self.tokenizer.special_tokens["<|begin_of_text|>"], 18 | 128000, 19 | ) 20 | 21 | def test_encode(self): 22 | self.assertEqual( 23 | self.tokenizer.encode( 24 | "This is a test sentence.", 25 | bos=True, 26 | eos=True 27 | ), 28 | [128000, 2028, 374, 264, 1296, 11914, 13, 128001], 29 | ) 30 | 31 | def test_decode(self): 32 | self.assertEqual( 33 | self.tokenizer.decode( 34 | [128000, 2028, 374, 264, 1296, 11914, 13, 128001], 35 | ), 36 | "<|begin_of_text|>This is a test sentence.<|end_of_text|>", 37 | ) 38 | 39 | def test_encode_message(self): 40 | message = { 41 | "role": "user", 42 | "content": "This is a test sentence.", 43 | } 44 | self.assertEqual( 45 | self.format.encode_message(message), 46 | [ 47 | 128006, # <|start_header_id|> 48 | 882, # "user" 49 | 128007, # <|end_of_header|> 50 | 271, # "\n\n" 51 | 2028, 374, 264, 1296, 11914, 13, # This is a test sentence. 52 | 128009, # <|eot_id|> 53 | ] 54 | ) 55 | 56 | def test_encode_dialog(self): 57 | dialog = [ 58 | { 59 | "role": "system", 60 | "content": "This is a test sentence.", 61 | }, 62 | { 63 | "role": "user", 64 | "content": "This is a response.", 65 | } 66 | ] 67 | self.assertEqual( 68 | self.format.encode_dialog_prompt(dialog), 69 | [ 70 | 128000, # <|begin_of_text|> 71 | 128006, # <|start_header_id|> 72 | 9125, # "system" 73 | 128007, # <|end_of_header|> 74 | 271, # "\n\n" 75 | 2028, 374, 264, 1296, 11914, 13, # "This is a test sentence." 76 | 128009, # <|eot_id|> 77 | 128006, # <|start_header_id|> 78 | 882, # "user" 79 | 128007, # <|end_of_header|> 80 | 271, # "\n\n" 81 | 2028, 374, 264, 2077, 13, # "This is a response.", 82 | 128009, # <|eot_id|> 83 | 128006, # <|start_header_id|> 84 | 78191, # "assistant" 85 | 128007, # <|end_of_header|> 86 | 271, # "\n\n" 87 | ] 88 | ) 89 | -------------------------------------------------------------------------------- /llama/tokenizer.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # This software may be used and distributed in accordance with the terms of the Llama 3 Community License Agreement. 3 | 4 | import os 5 | from logging import getLogger 6 | from pathlib import Path 7 | from typing import ( 8 | AbstractSet, 9 | cast, 10 | Collection, 11 | Dict, 12 | Iterator, 13 | List, 14 | Literal, 15 | Sequence, 16 | TypedDict, 17 | Union, 18 | ) 19 | 20 | import tiktoken 21 | from tiktoken.load import load_tiktoken_bpe 22 | 23 | 24 | logger = getLogger(__name__) 25 | 26 | 27 | Role = Literal["system", "user", "assistant"] 28 | 29 | 30 | class Message(TypedDict): 31 | role: Role 32 | content: str 33 | 34 | 35 | Dialog = Sequence[Message] 36 | 37 | 38 | class Tokenizer: 39 | """ 40 | Tokenizing and encoding/decoding text using the Tiktoken tokenizer. 41 | """ 42 | 43 | special_tokens: Dict[str, int] 44 | 45 | num_reserved_special_tokens = 256 46 | 47 | pat_str = r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+" # noqa: E501 48 | 49 | def __init__(self, model_path: str): 50 | """ 51 | Initializes the Tokenizer with a Tiktoken model. 52 | 53 | Args: 54 | model_path (str): The path to the Tiktoken model file. 55 | """ 56 | assert os.path.isfile(model_path), model_path 57 | 58 | mergeable_ranks = load_tiktoken_bpe(model_path) 59 | num_base_tokens = len(mergeable_ranks) 60 | special_tokens = [ 61 | "<|begin_of_text|>", 62 | "<|end_of_text|>", 63 | "<|reserved_special_token_0|>", 64 | "<|reserved_special_token_1|>", 65 | "<|reserved_special_token_2|>", 66 | "<|reserved_special_token_3|>", 67 | "<|start_header_id|>", 68 | "<|end_header_id|>", 69 | "<|reserved_special_token_4|>", 70 | "<|eot_id|>", # end of turn 71 | ] + [ 72 | f"<|reserved_special_token_{i}|>" 73 | for i in range(5, self.num_reserved_special_tokens - 5) 74 | ] 75 | self.special_tokens = { 76 | token: num_base_tokens + i for i, token in enumerate(special_tokens) 77 | } 78 | self.model = tiktoken.Encoding( 79 | name=Path(model_path).name, 80 | pat_str=self.pat_str, 81 | mergeable_ranks=mergeable_ranks, 82 | special_tokens=self.special_tokens, 83 | ) 84 | logger.info(f"Reloaded tiktoken model from {model_path}") 85 | 86 | self.n_words: int = self.model.n_vocab 87 | # BOS / EOS token IDs 88 | self.bos_id: int = self.special_tokens["<|begin_of_text|>"] 89 | self.eos_id: int = self.special_tokens["<|end_of_text|>"] 90 | self.pad_id: int = -1 91 | self.stop_tokens = { 92 | self.special_tokens["<|end_of_text|>"], 93 | self.special_tokens["<|eot_id|>"], 94 | } 95 | logger.info( 96 | f"#words: {self.n_words} - BOS ID: {self.bos_id} - EOS ID: {self.eos_id}" 97 | ) 98 | 99 | def encode( 100 | self, 101 | s: str, 102 | *, 103 | bos: bool, 104 | eos: bool, 105 | allowed_special: Union[Literal["all"], AbstractSet[str]] = set(), 106 | disallowed_special: Union[Literal["all"], Collection[str]] = (), 107 | ) -> List[int]: 108 | """ 109 | Encodes a string into a list of token IDs. 110 | 111 | Args: 112 | s (str): The input string to be encoded. 113 | bos (bool): Whether to prepend the beginning-of-sequence token. 114 | eos (bool): Whether to append the end-of-sequence token. 115 | allowed_tokens ("all"|set[str]): allowed special tokens in string 116 | disallowed_tokens ("all"|set[str]): special tokens that raise an error when in string 117 | 118 | Returns: 119 | list[int]: A list of token IDs. 120 | 121 | By default, setting disallowed_special=() encodes a string by ignoring 122 | special tokens. Specifically: 123 | - Setting `disallowed_special` to () will cause all text corresponding 124 | to special tokens to be encoded as natural text (insteading of raising 125 | an error). 126 | - Setting `allowed_special` to "all" will treat all text corresponding 127 | to special tokens to be encoded as special tokens. 128 | """ 129 | assert type(s) is str 130 | 131 | # The tiktoken tokenizer can handle <=400k chars without 132 | # pyo3_runtime.PanicException. 133 | TIKTOKEN_MAX_ENCODE_CHARS = 400_000 134 | 135 | # https://github.com/openai/tiktoken/issues/195 136 | # Here we iterate over subsequences and split if we exceed the limit 137 | # of max consecutive non-whitespace or whitespace characters. 138 | MAX_NO_WHITESPACES_CHARS = 25_000 139 | 140 | substrs = ( 141 | substr 142 | for i in range(0, len(s), TIKTOKEN_MAX_ENCODE_CHARS) 143 | for substr in self._split_whitespaces_or_nonwhitespaces( 144 | s[i : i + TIKTOKEN_MAX_ENCODE_CHARS], MAX_NO_WHITESPACES_CHARS 145 | ) 146 | ) 147 | t: List[int] = [] 148 | for substr in substrs: 149 | t.extend( 150 | self.model.encode( 151 | substr, 152 | allowed_special=allowed_special, 153 | disallowed_special=disallowed_special, 154 | ) 155 | ) 156 | if bos: 157 | t.insert(0, self.bos_id) 158 | if eos: 159 | t.append(self.eos_id) 160 | return t 161 | 162 | def decode(self, t: Sequence[int]) -> str: 163 | """ 164 | Decodes a list of token IDs into a string. 165 | 166 | Args: 167 | t (List[int]): The list of token IDs to be decoded. 168 | 169 | Returns: 170 | str: The decoded string. 171 | """ 172 | # Typecast is safe here. Tiktoken doesn't do anything list-related with the sequence. 173 | return self.model.decode(cast(List[int], t)) 174 | 175 | @staticmethod 176 | def _split_whitespaces_or_nonwhitespaces( 177 | s: str, max_consecutive_slice_len: int 178 | ) -> Iterator[str]: 179 | """ 180 | Splits the string `s` so that each substring contains no more than `max_consecutive_slice_len` 181 | consecutive whitespaces or consecutive non-whitespaces. 182 | """ 183 | current_slice_len = 0 184 | current_slice_is_space = s[0].isspace() if len(s) > 0 else False 185 | slice_start = 0 186 | 187 | for i in range(len(s)): 188 | is_now_space = s[i].isspace() 189 | 190 | if current_slice_is_space ^ is_now_space: 191 | current_slice_len = 1 192 | current_slice_is_space = is_now_space 193 | else: 194 | current_slice_len += 1 195 | if current_slice_len > max_consecutive_slice_len: 196 | yield s[slice_start:i] 197 | slice_start = i 198 | current_slice_len = 1 199 | yield s[slice_start:] 200 | 201 | 202 | class ChatFormat: 203 | def __init__(self, tokenizer: Tokenizer): 204 | self.tokenizer = tokenizer 205 | 206 | def encode_header(self, message: Message) -> List[int]: 207 | tokens = [] 208 | tokens.append(self.tokenizer.special_tokens["<|start_header_id|>"]) 209 | tokens.extend(self.tokenizer.encode(message["role"], bos=False, eos=False)) 210 | tokens.append(self.tokenizer.special_tokens["<|end_header_id|>"]) 211 | tokens.extend(self.tokenizer.encode("\n\n", bos=False, eos=False)) 212 | return tokens 213 | 214 | def encode_message(self, message: Message) -> List[int]: 215 | tokens = self.encode_header(message) 216 | tokens.extend( 217 | self.tokenizer.encode(message["content"].strip(), bos=False, eos=False) 218 | ) 219 | tokens.append(self.tokenizer.special_tokens["<|eot_id|>"]) 220 | return tokens 221 | 222 | def encode_dialog_prompt(self, dialog: Dialog) -> List[int]: 223 | tokens = [] 224 | tokens.append(self.tokenizer.special_tokens["<|begin_of_text|>"]) 225 | for message in dialog: 226 | tokens.extend(self.encode_message(message)) 227 | # Add the start of an assistant message for the model to complete. 228 | tokens.extend(self.encode_header({"role": "assistant", "content": ""})) 229 | return tokens 230 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | torch 2 | fairscale 3 | fire 4 | tiktoken==0.4.0 5 | blobfile 6 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # This software may be used and distributed in accordance with the terms of the Llama 3 Community License Agreement. 3 | 4 | from setuptools import find_packages, setup 5 | 6 | 7 | def get_requirements(path: str): 8 | return [l.strip() for l in open(path)] 9 | 10 | 11 | setup( 12 | name="llama3", 13 | version="0.0.1", 14 | packages=find_packages(), 15 | install_requires=get_requirements("requirements.txt"), 16 | ) 17 | --------------------------------------------------------------------------------