Preface
At the end of 2022, the emergence of ChatGPT changed people's understanding of artificial intelligence and brought a phase change to the field of Natural Language Processing (Natural Language Process, NLP). Large Language Models (Large Language Model, LLM), represented by the GPT series models, have become the mainstream research in NLP and even the artificial intelligence field. Since 2023, LLM has always been the core topic in the field of artificial intelligence, triggering a wave of technological innovation.
LLM is actually a derivative result of the classical research methods in the NLP field, pre-trained language models (Pretrain Language Model, PLM). The NLP field focuses on the processing, understanding, and generation of human-written natural language texts. Since its inception, it has undergone multiple transformations, including the symbolic stage, statistical learning stage, deep learning stage, pre-training model stage, and now the large model stage. PLMs represented by GPT and BERT are the core research results of the previous stage in the NLP field. They use attention mechanisms as their model architecture and achieve powerful natural language understanding through the idea of pre-training and fine-tuning on massive unsupervised text. However, traditional PLMs still rely on a certain amount of supervised data for downstream task fine-tuning, and their performance in natural language generation tasks is not yet satisfactory. There is still a big gap between the performance of NLP systems and the expected general artificial intelligence.
LLM is a breakthrough achievement based on PLM, achieved by significantly expanding the model parameters and pre-training data scale, and introducing techniques such as instruction tuning and human feedback reinforcement learning. Compared with traditional PLMs, LLM has emergent capabilities, with strong context learning ability, instruction understanding ability, and text generation ability. In the large model stage, NLP researchers can partially abandon the extensive supervised data annotation work. By providing a small number of supervised examples, LLM can achieve performance comparable to large-scale fine-tuned PLMs on specified downstream tasks. At the same time, the strong instruction understanding and text generation abilities allow LLM to directly, efficiently, and accurately respond to user instructions, thus approaching the goal of general artificial intelligence.
The breakthrough progress of LLM has sparked a research boom in the field of NLP and even the entire artificial intelligence field. Universities, research institutes, big companies, and many traditional enterprises at home and abroad have joined the wave of LLM research. Since 2023, there have been continuous phased achievements in LLM, with model performance constantly breaking records. From the initial ChatGPT, to GPT-4, and then to more powerful and customized models such as the reasoning large model DeepSeek-R1 and the multimodal large model Qwen-VL, LLM applications have continuously emerged with innovative applications that enhance practical productivity and empower users' daily lives. From the "battle of hundreds of models" to the "Year of Agents," the foundation research of LLM may be approaching a stable pattern. Research on LLM is still in full swing. It is certain that in the not-so-distant future, LLM and its applications based on LLM will become an infrastructure in people's lives, closely related to everyone's life, study, and work.
In this context, deeply understanding and mastering the principles of LLM, and being able to apply and train any LLM, is crucial for every NLP researcher and other AI researchers. At the end of 2023, we created two original open-source tutorials for large models: self-llm (an open-source guide for large models: https://github.com/datawhalechina/self-llm) and llm-universe (hands-on learning for large model application development: https://github.com/datawhalechina/llm-universe). The former aims to provide developers with a one-stop tutorial for deploying, inferring, and fine-tuning open-source LLMs, while the latter aims to guide developers to build their own LLM applications from scratch. Both tutorials have helped a wide range of domestic and international developers and have received support and recognition from many developers. In the feedback from learners, we found that there is still a lack of a complete tutorial that starts from scratch to explain the principles of LLM and guides learners to build and train LLMs themselves.
Based on this, we have written this tutorial combining the principles and practical aspects of LLM. This book starts from the basic research methods of NLP, and gradually delves into the principles and ideas of LLM, analyzing the architectural basis and training process of LLM for readers. At the same time, we will combine the most mainstream code frameworks in the current LLM field, demonstrating how to build and train an LLM by hand, aiming to teach both fish and fishing. We hope that readers can enter the vast world of LLM starting from this book and explore the infinite possibilities of LLM.
Suggestions for Readers
This book includes the theoretical foundations, principles, and project practices of LLM. It covers the core ideas, formula analysis, and code practice of LLM and NLP, aiming to help developers deeply understand and master the basic principles and applications of LLM. Therefore, this book is suitable for college students, researchers, and LLM enthusiasts. Before reading this book, you need to have some programming experience, especially to have a certain understanding of the Python programming language. At the same time, it is best to have knowledge of deep learning and understand relevant concepts and terminology in the NLP field to read this book more easily.
This book is divided into two parts: basic knowledge and practical applications. Chapters 1 to 4 are the basic knowledge part, which introduces the basic principles of LLM in a gradual way. Chapter 1 briefly introduces the basic tasks and development of NLP, providing references for researchers outside the NLP field. Chapter 2 introduces the basic architecture of LLM - Transformer, including principle introduction and code implementation, serving as the most important theoretical basis for LLM. Chapter 3 gives an overall introduction to classic PLMs, including three architectures: Encoder-Only, Encoder-Decoder, and Decoder-Only. It also introduces the architecture and ideas of some mainstream LLMs today. Chapter 4 formally enters the LLM section, detailing the characteristics, capabilities, and overall training process of LLM. Chapters 5 to 7 are the practical application part, gradually guiding readers to delve into the details of LLM. Chapter 5 will lead readers to build an LLM by hand based on PyTorch layers and implement the entire process of pre-training and supervised fine-tuning. Chapter 6 introduces the current industry's mainstream LLM training framework Transformers, leading readers to quickly and efficiently implement the LLM training process based on this framework. Chapter 7 will introduce various applications based on LLM, completing readers' understanding of the LLM system, including LLM evaluation, Retrieval-Augmented Generation (RAG), the concept and simple implementation of agents (Agent). You can choose to read relevant chapters based on your personal interests and needs.
During the reading process, it is recommended that you combine theory with practice. LLM is a rapidly developing and practical field. We recommend that you invest more in practice, reproduce the various codes provided in this book, and actively participate in LLM-related projects and competitions, truly immersing yourself in the wave of LLM development. We encourage you to follow Datawhale and other open-source communities related to LLM. When you encounter problems, you can ask questions anytime in the Datawhale community. Datawhale will always keep up with the development of LLM and other artificial intelligence technologies. Welcome to follow or join the co-construction of the Datawhale community.
Finally, welcome every reader to join the ranks of LLM developers after reading this book. As a domestic AI open-source community, we hope to fully gather contributors to enrich this open-source LLM world and create more comprehensive and unique LLM tutorials. A spark can light a fire, and many sparks can form a sea. We hope to become the ladder between LLM and the general public, embracing a broader and more magnificent LLM world with the spirit of open source, freedom, and equality.
Thank you for choosing this book. We wish you a pleasant reading experience!
Note: The content of this section is referenced from the open-source project Happy-LLM - A Tutorial on the Principles and Practices of Large Language Models from Scratch, authored by Datawhale. Some content has been adjusted and deleted for learning and reference purposes only.