diff --git a/.github/workflows/build_documentation.yml b/.github/workflows/build_documentation.yml index 9ebde4c42..2cb0fae14 100644 --- a/.github/workflows/build_documentation.yml +++ b/.github/workflows/build_documentation.yml @@ -14,6 +14,6 @@ jobs: package: course path_to_docs: course/chapters/ additional_args: --not_python_module - languages: ar bn de en es fa fr gj he hi id it ja ko ne pl pt ru ro te th tr vi zh-CN zh-TW + languages: ar bn de en es fa fr gj he hi id it ja ko my ne pl pt ru ro te th tr vi zh-CN zh-TW secrets: hf_token: ${{ secrets.HF_DOC_BUILD_PUSH }} diff --git a/.github/workflows/build_pr_documentation.yml b/.github/workflows/build_pr_documentation.yml index a0f408054..8e9259cd5 100644 --- a/.github/workflows/build_pr_documentation.yml +++ b/.github/workflows/build_pr_documentation.yml @@ -16,4 +16,4 @@ jobs: package: course path_to_docs: course/chapters/ additional_args: --not_python_module - languages: ar bn de en es fa fr gj he hi id it ja ko ne pl pt ru ro te th tr vi zh-CN zh-TW + languages: ar bn de en es fa fr gj he hi id it ja ko my ne pl pt ru ro te th tr vi zh-CN zh-TW diff --git a/chapters/bn/chapter2/1.mdx b/chapters/bn/chapter2/1.mdx index 358c1f474..fd19b9784 100644 --- a/chapters/bn/chapter2/1.mdx +++ b/chapters/bn/chapter2/1.mdx @@ -20,6 +20,5 @@ তারপরে আমরা টোকেনাইজার API দেখব, যা `pipeline()` ফাংশনের অন্য একটি প্রধান উপাদান। টোকেনাইজার জিনিসটা প্রথম ও শেষ প্রসেসিং স্টেপগুলোতে মেইনলি কাজে লাগে, নিউরাল নেটওয়ার্কের জন্য টেক্সট ডাটা থেকে সংখ্যাসূচক ইনপুটে রূপান্তর এবং পরে আবার প্রয়োজন অনুযায়ী সংখ্যাসূচক ডাটা থেকে টেক্সট ডাটাতে রূপান্তর করার সময়। পরিশেষে, আমরা আপনাকে দেখাব কিভাবে ব্যাচের মাধ্যমে একাধিক বাক্যকে একটি মডেলে পাঠানো যায়। তারপরে আরেকবার হাই-লেভেলে `tokenizer()` ফাংশনটিকে একনজরে দেখার মাধ্যমে পুরো অধ্যায়ের ইতি টানব। - -⚠️ Model Hub এবং 🤗 Transformers এর সাথে উপলব্ধ সমস্ত বৈশিষ্ট্যগুলি থেকে উপকৃত হওয়ার জন্য, আমরা সাজেস্ট করি এখানে একটি একাউন্ট তৈরি করার জন্যে।. - \ No newline at end of file +> [!TIP] +> ⚠️ Model Hub এবং 🤗 Transformers এর সাথে উপলব্ধ সমস্ত বৈশিষ্ট্যগুলি থেকে উপকৃত হওয়ার জন্য, আমরা সাজেস্ট করি এখানে একটি একাউন্ট তৈরি করার জন্যে।. \ No newline at end of file diff --git a/chapters/de/chapter1/3.mdx b/chapters/de/chapter1/3.mdx index 7db9cb48a..9e15b54d2 100644 --- a/chapters/de/chapter1/3.mdx +++ b/chapters/de/chapter1/3.mdx @@ -9,11 +9,10 @@ In diesem Abschnitt schauen wir uns an, was Transformer-Modelle zu leisten imstande sind. Zudem verwenden wir unser erstes Werkzeug aus der 🤗 Transformers-Bibliothek: die Funktion `pipeline()`. - -👀 Siehst du rechts oben die Schaltfläche Open in Colab? Klicke darauf, um ein Google Colab Notebook, das alle Codebeispiele dieses Abschnitts enthält, zu öffnen. Diese Schaltfläche ist in jedem Abschnitt, der Codebeispiele enthält, zu finden. - -Wenn du die Beispiele lieber lokal ausführen möchtest, empfehlen wir dir, einen Blick auf das Kapitel Einrichtung zu werfen. - +> [!TIP] +> 👀 Siehst du rechts oben die Schaltfläche Open in Colab? Klicke darauf, um ein Google Colab Notebook, das alle Codebeispiele dieses Abschnitts enthält, zu öffnen. Diese Schaltfläche ist in jedem Abschnitt, der Codebeispiele enthält, zu finden. +> +> Wenn du die Beispiele lieber lokal ausführen möchtest, empfehlen wir dir, einen Blick auf das Kapitel Einrichtung zu werfen. ## Transformer-Modelle sind überall anzutreffen! @@ -23,9 +22,8 @@ Transformer-Modelle werden verwendet, um alle Arten von CL-Aufgaben (engl. Tasks Die [🤗 Transformers-Bibliothek](https://github.com/huggingface/transformers) bietet die Funktionalität, um diese geteilten Modelle zu erstellen und zu nutzen. Der [Model Hub](https://huggingface.co/models) enthält Tausende von vortrainierten Modellen, die jeder herunterladen und nutzen kann. Auch du kannst dort deine eigenen Modelle hochladen! - -⚠️ Der Hugging Face Hub ist nicht auf Transformer-Modelle beschränkt. Jede bzw. jeder kann die von ihr bzw. ihm gewünschten Arten von Modellen oder Datensätzen teilen! Erstelle ein Konto auf huggingface.co, um alle verfügbaren Features nutzen zu können! - +> [!TIP] +> ⚠️ Der Hugging Face Hub ist nicht auf Transformer-Modelle beschränkt. Jede bzw. jeder kann die von ihr bzw. ihm gewünschten Arten von Modellen oder Datensätzen teilen! Erstelle ein Konto auf huggingface.co, um alle verfügbaren Features nutzen zu können! Bevor wir uns ansehen, wie Transformer-Modelle im Einzelnen funktionieren, widmen wir uns ein paar Beispielen, die veranschaulichen, wie sie zur Lösung interessanter CL-Problemstellungen eingesetzt werden können. @@ -104,11 +102,8 @@ classifier( Diese Pipeline heißt _zero-shot_, weil du das Modell nicht erst auf deine Daten feintunen musst, ehe du es verwenden kannst. Sie kann direkt die Wahrscheinlichkeiten für jede beliebige von dir vorgegebene Liste von Labels liefern! - - -✏️ **Probiere es aus!** Spiel mit deinen eigenen Sequenzen und Labels herum und beobachte, wie sich das Modell verhält. - - +> [!TIP] +> ✏️ **Probiere es aus!** Spiel mit deinen eigenen Sequenzen und Labels herum und beobachte, wie sich das Modell verhält. ## Textgenerierung @@ -132,11 +127,8 @@ generator("In this course, we will teach you how to") Mit dem Argument `num_return_sequences` kannst du steuern, wie viele verschiedene Sequenzen erzeugt werden und mit dem Argument `max_length`, wie lang der Ausgabetext insgesamt sein soll. - - -✏️ **Probiere es aus!** Wähle die Argumente `num_return_sequences` und `max_length` so, dass zwei Sätze mit jeweils 15 Wörtern erzeugt werden. - - +> [!TIP] +> ✏️ **Probiere es aus!** Wähle die Argumente `num_return_sequences` und `max_length` so, dass zwei Sätze mit jeweils 15 Wörtern erzeugt werden. ## Verwendung eines beliebigen Modells vom Hub in einer Pipeline @@ -168,11 +160,8 @@ Du kannst deine Suche nach einem Modell verfeinern, indem du auf eines der `Lang Nachdem du auf ein Modell geklickt und es ausgewählt hast, siehst du, dass es ein Widget gibt, mit dem du es direkt online ausprobieren kannst. Dementsprechend kannst du die Fähigkeiten eines Modells erst schnell testen, bevor du dich dazu entschließt, es herunterzuladen. - - -✏️ **Probiere es aus!** Verwende die Filter, um ein Textgenerierungsmodell für eine andere Sprache zu finden. Experimentiere ruhig ein wenig mit dem Widget und verwende das Modell in einer Pipeline! - - +> [!TIP] +> ✏️ **Probiere es aus!** Verwende die Filter, um ein Textgenerierungsmodell für eine andere Sprache zu finden. Experimentiere ruhig ein wenig mit dem Widget und verwende das Modell in einer Pipeline! ### Die Inference API @@ -204,11 +193,8 @@ unmasker("This course will teach you all about models.", top_k=2) Mit dem Argument `top_k` kannst du bestimmen, wie viele Möglichkeiten dir ausgegeben werden sollen. Beachte, dass das Modell hier das spezielle Wort `` auffüllt, das oft als *Mask-Token* bezeichnet wird. Andere Modelle, die dazu dienen, Maskierungen aufzufüllen, können andere Mask Tokens haben. Deshalb ist es immer gut, erst das verwendete Mask Token zu ermitteln, wenn du andere Modelle nutzen möchtest. Eine Möglichkeit, zu überprüfen, welches Mask Token verwendet wird, ist das Widget. - - -✏️ **Probiere es aus!** Suche im Hub nach dem Modell `bert-base-cased` und finde sein Mask Token im Widget, das auf der Inference API basiert, heraus. Was sagt dieses Modell für den oben in der Pipeline verwendeten Satz vorher? - - +> [!TIP] +> ✏️ **Probiere es aus!** Suche im Hub nach dem Modell `bert-base-cased` und finde sein Mask Token im Widget, das auf der Inference API basiert, heraus. Was sagt dieses Modell für den oben in der Pipeline verwendeten Satz vorher? ## Named Entity Recognition @@ -232,11 +218,8 @@ Hier hat das Modell richtig erkannt, dass Sylvain eine Person (PER), Hugging Fac In der Funktion zur Erstellung der Pipeline übergeben wir die Option `grouped_entities=True`, um die Pipeline anzuweisen, die Teile des Satzes, die der gleichen Entität entsprechen, zu gruppieren: Hier hat das Modell "Hugging" und "Face" richtigerweise als eine einzelne Organisation gruppiert, auch wenn der Name aus mehreren Wörtern besteht. Wie wir im nächsten Kapitel sehen werden, werden bei der Vorverarbeitung (engl. Preprocessing) sogar einige Wörter in kleinere Teile zerlegt. Zum Beispiel wird `Sylvain` in vier Teile zerlegt: `S`, `##yl`, `##va` und `##in`. Im Nachverarbeitungsschritt (engl. Post-Processing) hat die Pipeline diese Teile erfolgreich neu gruppiert. - - -✏️ **Probiere es aus!** Suche im Model Hub nach einem Modell, das in der Lage ist, Part-of-Speech-Tagging (in der Regel als POS abgekürzt) im Englischen durchzuführen (Anm.: d. h. Wortarten zuzuordnen). Was sagt dieses Modell für den Satz im obigen Beispiel vorher? - - +> [!TIP] +> ✏️ **Probiere es aus!** Suche im Model Hub nach einem Modell, das in der Lage ist, Part-of-Speech-Tagging (in der Regel als POS abgekürzt) im Englischen durchzuführen (Anm.: d. h. Wortarten zuzuordnen). Was sagt dieses Modell für den Satz im obigen Beispiel vorher? ## Frage-Antwort-Systeme (Question Answering) @@ -320,10 +303,7 @@ translator("Ce cours est produit par Hugging Face.") Wie bei der Textgenerierung und -zusammenfassung kannst du auch hier `max_length` oder `min_length` als Argumente für das Ergebnis angeben. - - -✏️ **Probiere es aus!** Suche nach Übersetzungsmodellen in anderen Sprachen und versuche, den vorangegangenen Satz in mehrere verschiedene Sprachen zu übersetzen. - - +> [!TIP] +> ✏️ **Probiere es aus!** Suche nach Übersetzungsmodellen in anderen Sprachen und versuche, den vorangegangenen Satz in mehrere verschiedene Sprachen zu übersetzen. Die bisher gezeigten Pipelines dienen hauptsächlich zu Demonstrationszwecken. Sie wurden für bestimmte Aufgabenstellungen programmiert und sind nicht für Abwandlungen geeignet. Im nächsten Kapitel erfährst du, was sich hinter einer `pipeline()`-Funktion verbirgt und wie du ihr Verhalten anpassen kannst. diff --git a/chapters/de/chapter3/2.mdx b/chapters/de/chapter3/2.mdx index f77dc0141..1c9aaad0a 100644 --- a/chapters/de/chapter3/2.mdx +++ b/chapters/de/chapter3/2.mdx @@ -149,11 +149,8 @@ raw_train_dataset.features Hinter den Kulissen ist `label` vom Typ `ClassLabel`, und die Zuordnung von Ganzzahlen zum Labelnamen wird im Ordner *names* gespeichert. `0` entspricht `not_equivalent`, also "nicht äquivalent", und `1` entspricht `equivalent`, also "äquivalent". - - -✏️ **Probier es aus!** Sieh dir das Element 15 der Trainingsdaten und Element 87 des Validierungsdaten an. Was sind ihre Labels? - - +> [!TIP] +> ✏️ **Probier es aus!** Sieh dir das Element 15 der Trainingsdaten und Element 87 des Validierungsdaten an. Was sind ihre Labels? ### Vorverarbeitung eines Datensatzes @@ -191,11 +188,8 @@ inputs In [Kapitel 2](/course/chapter2) haben wir die Schlüsselwerte `input_ids` und `attention_mask` behandelt, allerdings haben wir es aufgeschoben, über `token_type_ids` zu sprechen. In diesem Beispiel teilt diese dem Modell mit, welcher Teil des Input der erste Satz und welcher der zweite Satz ist. - - -✏️ **Probier es aus!** Nimm Element 15 der Trainingsdaten und tokenisiere die beiden Sätze separat und als Paar. Wo liegt der Unterschied zwischen den beiden Ergebnissen? - - +> [!TIP] +> ✏️ **Probier es aus!** Nimm Element 15 der Trainingsdaten und tokenisiere die beiden Sätze separat und als Paar. Wo liegt der Unterschied zwischen den beiden Ergebnissen? Wenn wir die IDs in `input_ids` zurück in Worte dekodieren: @@ -353,11 +347,8 @@ Das sieht gut aus! Jetzt, da wir vom Rohtext zu Batches übergegangen sind, mit {/if} - - -✏️ **Probier es aus!** Repliziere die Vorverarbeitung auf dem GLUE SST-2-Datensatz. Es ist ein bisschen anders, da es aus einzelnen Sätzen statt aus Paaren besteht, aber der Rest von dem, was wir gemacht haben, sollte gleich aussehen. Alternative wäre eine schwierigere Herausforderung, eine Vorverarbeitungsfunktion zu schreiben, die bei allen GLUE-Aufgaben funktioniert. - - +> [!TIP] +> ✏️ **Probier es aus!** Repliziere die Vorverarbeitung auf dem GLUE SST-2-Datensatz. Es ist ein bisschen anders, da es aus einzelnen Sätzen statt aus Paaren besteht, aber der Rest von dem, was wir gemacht haben, sollte gleich aussehen. Alternative wäre eine schwierigere Herausforderung, eine Vorverarbeitungsfunktion zu schreiben, die bei allen GLUE-Aufgaben funktioniert. {#if fw === 'tf'} diff --git a/chapters/de/chapter3/3.mdx b/chapters/de/chapter3/3.mdx index 202251577..ef20299a4 100644 --- a/chapters/de/chapter3/3.mdx +++ b/chapters/de/chapter3/3.mdx @@ -42,11 +42,8 @@ from transformers import TrainingArguments training_args = TrainingArguments("test-trainer") ``` - - -💡 Wenn du dein Modell während des Trainings automatisch in das Hub hochladen möchtest, kann in `TrainingArguments` das Argument `push_to_hub=True` angegeben werden. Darüber erfahren wir in [Kapitel 4](/course/chapter4/3) mehr. - - +> [!TIP] +> 💡 Wenn du dein Modell während des Trainings automatisch in das Hub hochladen möchtest, kann in `TrainingArguments` das Argument `push_to_hub=True` angegeben werden. Darüber erfahren wir in [Kapitel 4](/course/chapter4/3) mehr. Der zweite Schritt ist die Definition unseres Modells. Wie im [vorherigen Kapitel](/course/chapter2) verwenden wir die Klasse `AutoModelForSequenceClassification` mit zwei Labels: @@ -164,9 +161,6 @@ Der `Trainer` funktioniert sofort auf mehreren GPUs oder TPUs und bietet zahlrei Damit ist die Einführung in das Fein-tunen mit der `Trainer` API abgeschlossen. Beispiele für die gängigsten CL-Aufgaben werden in Kapitel 7 gegeben, aber jetzt schauen wir uns erst einmal an, wie man das Gleiche in PyTorch bewerkstelligen kann. - - -✏️ **Probier es aus!** Fein-tune ein Modell mit dem GLUE SST-2 Datensatz, indem du die Datenverarbeitung aus Abschnitt 2 verwendest. - - +> [!TIP] +> ✏️ **Probier es aus!** Fein-tune ein Modell mit dem GLUE SST-2 Datensatz, indem du die Datenverarbeitung aus Abschnitt 2 verwendest. diff --git a/chapters/de/chapter3/3_tf.mdx b/chapters/de/chapter3/3_tf.mdx index 970d06835..4d4cbb18b 100644 --- a/chapters/de/chapter3/3_tf.mdx +++ b/chapters/de/chapter3/3_tf.mdx @@ -70,11 +70,8 @@ Im Gegensatz zu [Kapitel 2](/course/chapter2) wird eine Warnung angezeigt, nachd Um das Modell mit unserem Datensatz fein-tunen zu können, müssen wir das Modell `kompilieren()` und unsere Daten an die `fit()`-Methode übergeben. Damit wird das Fein-tuning gestartet (dies sollte auf einer GPU ein paar Minuten dauern) und der Trainingsverlust sowie der Validierungsverlust am Ende jeder Epoche gemeldet. - - -🤗 Transformer Modelle haben eine besondere Fähigkeit, die die meisten Keras Modelle nicht haben - sie können automatisch einen geeigneten Verlust verwenden, der intern berechnet wird. Dieser Verlust wird standardmäßig verwendet, wenn in `compile()` kein Verlustargument angegeben wird. Um den internen Verlust zu verwenden, musst du deine Labels als Teil des Input übergeben und nicht als separates Label, wie es normalerweise bei Keras-Modellen der Fall ist. Beispiele dafür gibt es in Teil 2 des Kurses, wobei die Definition der richtigen Verlustfunktion schwierig sein kann. Für die Klassifizierung von Sequenzen eignet sich jedoch eine der Standardverlustfunktionen von Keras, die wir hier verwenden werden. - - +> [!TIP] +> 🤗 Transformer Modelle haben eine besondere Fähigkeit, die die meisten Keras Modelle nicht haben - sie können automatisch einen geeigneten Verlust verwenden, der intern berechnet wird. Dieser Verlust wird standardmäßig verwendet, wenn in `compile()` kein Verlustargument angegeben wird. Um den internen Verlust zu verwenden, musst du deine Labels als Teil des Input übergeben und nicht als separates Label, wie es normalerweise bei Keras-Modellen der Fall ist. Beispiele dafür gibt es in Teil 2 des Kurses, wobei die Definition der richtigen Verlustfunktion schwierig sein kann. Für die Klassifizierung von Sequenzen eignet sich jedoch eine der Standardverlustfunktionen von Keras, die wir hier verwenden werden. ```py from tensorflow.keras.losses import SparseCategoricalCrossentropy @@ -90,11 +87,8 @@ model.fit( ) ``` - - -Hier gibt es einen sehr häufigen Stolperstein - du *kannst* Keras einfach den Namen des Verlusts als String übergeben, aber standardmäßig geht Keras davon aus, dass du bereits einen Softmax auf die Outputs angewendet hast. Viele Modelle geben jedoch die Werte direkt vor der Anwendung des Softmax als *Logits* aus. Hier ist es wichtig der Keras Verlustfunktion mitzuteilen, dass unser Modell genau diess tut, und das geht nur indem sie direkt aufgerufen wird, und nicht über den Namen mit einem String. - - +> [!WARNING] +> Hier gibt es einen sehr häufigen Stolperstein - du *kannst* Keras einfach den Namen des Verlusts als String übergeben, aber standardmäßig geht Keras davon aus, dass du bereits einen Softmax auf die Outputs angewendet hast. Viele Modelle geben jedoch die Werte direkt vor der Anwendung des Softmax als *Logits* aus. Hier ist es wichtig der Keras Verlustfunktion mitzuteilen, dass unser Modell genau diess tut, und das geht nur indem sie direkt aufgerufen wird, und nicht über den Namen mit einem String. ### Verbesserung der Trainingsperformance @@ -122,11 +116,8 @@ from tensorflow.keras.optimizers import Adam opt = Adam(learning_rate=lr_scheduler) ``` - - -Die 🤗 Transformer Bibliothek hat eine `create_optimizer()`-Funktion, die einen `AdamW`-Optimierer mit Lernratenabfall erzeugt. Das ist eine praktisches Tool, auf das wir in den nächsten Abschnitten des Kurses im Detail eingehen werden. - - +> [!TIP] +> Die 🤗 Transformer Bibliothek hat eine `create_optimizer()`-Funktion, die einen `AdamW`-Optimierer mit Lernratenabfall erzeugt. Das ist eine praktisches Tool, auf das wir in den nächsten Abschnitten des Kurses im Detail eingehen werden. Somit haben wir einen neuen Optimierer definiert und können ihn zum Training verwenden. Zuerst laden wir das Modell neu, um die Änderungen an der Gewichtung aus dem letzten Trainingslauf zurückzusetzen, und dann können wir es mit dem neuen Optimierer kompilieren: @@ -144,11 +135,8 @@ Jetzt starten wir einen erneuten Trainingslauf mit `fit`: model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3) ``` - - -💡 Wenn du dein Modell während des Trainings automatisch in den Hub hochladen möchtest, kannst du in der Methode `model.fit()` einen `PushToHubCallback` mitgeben. Mehr darüber erfahren wir in [Kapitel 4](/course/chapter4/3) - - +> [!TIP] +> 💡 Wenn du dein Modell während des Trainings automatisch in den Hub hochladen möchtest, kannst du in der Methode `model.fit()` einen `PushToHubCallback` mitgeben. Mehr darüber erfahren wir in [Kapitel 4](/course/chapter4/3) ### Modell-Vorhersagen @@ -188,8 +176,5 @@ Die genauen Ergebnisse können variieren, da die zufällige Initialisierung des Damit ist die Einführung in das Fein-tunen mit der Keras-API abgeschlossen. Beispiele für die gängigsten CL-Aufgaben findest du in Kapitel 7. - - -✏️ **Probier es aus!** Fein-tune ein Modell mit dem GLUE SST-2 Datensatz, indem du die Datenverarbeitung aus Abschnitt 2 verwendest. - - +> [!TIP] +> ✏️ **Probier es aus!** Fein-tune ein Modell mit dem GLUE SST-2 Datensatz, indem du die Datenverarbeitung aus Abschnitt 2 verwendest. diff --git a/chapters/de/chapter3/4.mdx b/chapters/de/chapter3/4.mdx index bf63ada9c..6395de2e0 100644 --- a/chapters/de/chapter3/4.mdx +++ b/chapters/de/chapter3/4.mdx @@ -196,11 +196,8 @@ metric.compute() Auch hier werden deine Ergebnisse wegen der Zufälligkeit bei der Initialisierung des Modellkopfes und der Datenverteilung etwas anders ausfallen, aber sie sollten in etwa gleich sein. - - -✏️ **Probier es selbt!** Ändere die vorherige Trainingsschleife, um dein Modell auf dem SST-2-Datensatz fein zu tunen. - - +> [!TIP] +> ✏️ **Probier es selbt!** Ändere die vorherige Trainingsschleife, um dein Modell auf dem SST-2-Datensatz fein zu tunen. ### Verbessere deine Trainingsschleife mit 🤗 Accelerate @@ -292,9 +289,8 @@ Die erste Zeile, die hinzugefügt werden muss, ist die Import-Zeile. Die zweite Der Hauptteil der Arbeit wird dann in der Zeile erledigt, die die Dataloader, das Modell und den Optimierer an `accelerator.prepare()` sendet. Dadurch werden diese Objekte in den richtigen Container verpackt, damit das verteilte Training wie vorgesehen funktioniert. Die verbleibenden Änderungen sind das Entfernen der Zeile, die das Batch auf dem Gerät mit `device` ablegt (wenn du das beibehalten willst, kannst du es einfach in `accelerator.device` ändern) und das Ersetzen von `loss.backward()` durch `accelerator.backward(loss)`. - -⚠️ Um von dem Geschwindigkeitsvorteil der Cloud TPUs zu profitieren, empfehlen wir, deine Samples mit den Argumenten `padding="max_length"` und `max_length` des Tokenizers auf eine feste Länge aufzufüllen. - +> [!TIP] +> ⚠️ Um von dem Geschwindigkeitsvorteil der Cloud TPUs zu profitieren, empfehlen wir, deine Samples mit den Argumenten `padding="max_length"` und `max_length` des Tokenizers auf eine feste Länge aufzufüllen. Wenn du damit experimentieren möchtest, siehst du hier, wie die komplette Trainingsschleife mit 🤗 Accelerate aussieht: diff --git a/chapters/de/chapter4/2.mdx b/chapters/de/chapter4/2.mdx index a445d6f60..082e5040c 100644 --- a/chapters/de/chapter4/2.mdx +++ b/chapters/de/chapter4/2.mdx @@ -91,6 +91,5 @@ model = TFAutoModelForMaskedLM.from_pretrained("camembert-base") ``` {/if} - -Wenn du ein vortrainiertes Modell verwendest, prüf erstmal, wie genau das traininert wurde, mit welchen Datensätzen, sowie seine Einschränkungen und Biases. All diese Informationen sollten auf der Modellbeschreibungskarte stehen. - +> [!TIP] +> Wenn du ein vortrainiertes Modell verwendest, prüf erstmal, wie genau das traininert wurde, mit welchen Datensätzen, sowie seine Einschränkungen und Biases. All diese Informationen sollten auf der Modellbeschreibungskarte stehen. diff --git a/chapters/de/chapter4/3.mdx b/chapters/de/chapter4/3.mdx index 343221b0b..6a9b99441 100644 --- a/chapters/de/chapter4/3.mdx +++ b/chapters/de/chapter4/3.mdx @@ -171,11 +171,8 @@ Click auf den Tab "Files and versions" und da solltest du die Dateien finden, di {/if} - - -✏️ **Probier das selber aus!** Lade das Modell und den Tokenizer vom Checkpoint `bert-base-cased` mit der Methode `push_to_hub()` hoch. Überprüfe, dass der Repository auf deiner Seite richtig erscheint, bevor du den löschst. - - +> [!TIP] +> ✏️ **Probier das selber aus!** Lade das Modell und den Tokenizer vom Checkpoint `bert-base-cased` mit der Methode `push_to_hub()` hoch. Überprüfe, dass der Repository auf deiner Seite richtig erscheint, bevor du den löschst. Wie du schon gesehen hast, akzeptiert die Methode `push_to_hub()` mehrere Argumente. Dies erlaub das Hochladen auf den Namespace eines spezifischen Repositorys oder einer Organisation, sowie die Möglichkeit, einen anderen API Token zu benutzten. Wir empfehlen dir, die Dokumentation der Methode direkt auf [🤗 Transformers documentation](https://huggingface.co/transformers/model_sharing.html) zu lesen, um dir eine Vorstellung zu schaffen, was alles damit möglich ist. @@ -459,9 +456,8 @@ Wenn du dir die Dateigrößen anschaust (z.B. mit `ls -lh`), solltest du sehen, {/if} - -✏️ Wenn ein Repository mittels der Webinterface kreiert wird, wird die *.gitattributes* Datei automatisch gesetzt, um bestimmte Dateiendungen wie *.bin* und *.h5* als große Dateien zu betrachten, sodass git-lfs sie tracken kann, ohne dass du weiteres konfigurieren musst. - +> [!TIP] +> ✏️ Wenn ein Repository mittels der Webinterface kreiert wird, wird die *.gitattributes* Datei automatisch gesetzt, um bestimmte Dateiendungen wie *.bin* und *.h5* als große Dateien zu betrachten, sodass git-lfs sie tracken kann, ohne dass du weiteres konfigurieren musst. Nun können wir weitermachen und so arbeiten wie wir es mit normalen Git Repositories machen. Wir können die Dateien stagen mit dem Git-Befehl `git add`: diff --git a/chapters/en/chapter1/1.mdx b/chapters/en/chapter1/1.mdx index ff9f55560..9294f9af1 100644 --- a/chapters/en/chapter1/1.mdx +++ b/chapters/en/chapter1/1.mdx @@ -146,9 +146,8 @@ For some languages, the [course YouTube videos](https://youtube.com/playlist?lis Activating subtitles for the Hugging Face course YouTube videos - -Don't see your language in the above table or you'd like to contribute to an existing translation? You can help us translate the course by following the instructions here. - +> [!TIP] +> Don't see your language in the above table or you'd like to contribute to an existing translation? You can help us translate the course by following the instructions here. ## Let's go 🚀 diff --git a/chapters/en/chapter1/2.mdx b/chapters/en/chapter1/2.mdx index 13fa4eef2..ebfb179a4 100644 --- a/chapters/en/chapter1/2.mdx +++ b/chapters/en/chapter1/2.mdx @@ -27,11 +27,8 @@ NLP isn't limited to written text though. It also tackles complex challenges in In recent years, the field of NLP has been revolutionized by Large Language Models (LLMs). These models, which include architectures like GPT (Generative Pre-trained Transformer) and [Llama](https://huggingface.co/meta-llama), have transformed what's possible in language processing. - - -A large language model (LLM) is an AI model trained on massive amounts of text data that can understand and generate human-like text, recognize patterns in language, and perform a wide variety of language tasks without task-specific training. They represent a significant advancement in the field of natural language processing (NLP). - - +> [!TIP] +> A large language model (LLM) is an AI model trained on massive amounts of text data that can understand and generate human-like text, recognize patterns in language, and perform a wide variety of language tasks without task-specific training. They represent a significant advancement in the field of natural language processing (NLP). LLMs are characterized by: - **Scale**: They contain millions, billions, or even hundreds of billions of parameters diff --git a/chapters/en/chapter1/3.mdx b/chapters/en/chapter1/3.mdx index df13888ec..2865476fb 100644 --- a/chapters/en/chapter1/3.mdx +++ b/chapters/en/chapter1/3.mdx @@ -9,11 +9,10 @@ In this section, we will look at what Transformer models can do and use our first tool from the 🤗 Transformers library: the `pipeline()` function. - -👀 See that Open in Colab button on the top right? Click on it to open a Google Colab notebook with all the code samples of this section. This button will be present in any section containing code examples. - -If you want to run the examples locally, we recommend taking a look at the setup. - +> [!TIP] +> 👀 See that Open in Colab button on the top right? Click on it to open a Google Colab notebook with all the code samples of this section. This button will be present in any section containing code examples. +> +> If you want to run the examples locally, we recommend taking a look at the setup. ## Transformers are everywhere![[transformers-are-everywhere]] @@ -23,11 +22,8 @@ Transformer models are used to solve all kinds of tasks across different modalit The [🤗 Transformers library](https://github.com/huggingface/transformers) provides the functionality to create and use those shared models. The [Model Hub](https://huggingface.co/models) contains millions of pretrained models that anyone can download and use. You can also upload your own models to the Hub! - - -⚠️ The Hugging Face Hub is not limited to Transformer models. Anyone can share any kind of models or datasets they want! Create a huggingface.co account to benefit from all available features! - - +> [!TIP] +> ⚠️ The Hugging Face Hub is not limited to Transformer models. Anyone can share any kind of models or datasets they want! Create a huggingface.co account to benefit from all available features! Before diving into how Transformer models work under the hood, let's look at a few examples of how they can be used to solve some interesting NLP problems. @@ -75,11 +71,8 @@ The `pipeline()` function supports multiple modalities, allowing you to work wit Here's an overview of what's available: - - -For a full and updated list of pipelines, see the [🤗 Transformers documentation](https://huggingface.co/docs/hub/en/models-tasks). - - +> [!TIP] +> For a full and updated list of pipelines, see the [🤗 Transformers documentation](https://huggingface.co/docs/hub/en/models-tasks). ### Text pipelines @@ -130,11 +123,8 @@ classifier( This pipeline is called _zero-shot_ because you don't need to fine-tune the model on your data to use it. It can directly return probability scores for any list of labels you want! - - -✏️ **Try it out!** Play around with your own sequences and labels and see how the model behaves. - - +> [!TIP] +> ✏️ **Try it out!** Play around with your own sequences and labels and see how the model behaves. ## Text generation[[text-generation]] @@ -158,11 +148,8 @@ generator("In this course, we will teach you how to") You can control how many different sequences are generated with the argument `num_return_sequences` and the total length of the output text with the argument `max_length`. - - -✏️ **Try it out!** Use the `num_return_sequences` and `max_length` arguments to generate two sentences of 15 words each. - - +> [!TIP] +> ✏️ **Try it out!** Use the `num_return_sequences` and `max_length` arguments to generate two sentences of 15 words each. ## Using any model from the Hub in a pipeline[[using-any-model-from-the-hub-in-a-pipeline]] @@ -193,11 +180,8 @@ You can refine your search for a model by clicking on the language tags, and pic Once you select a model by clicking on it, you'll see that there is a widget enabling you to try it directly online. This way you can quickly test the model's capabilities before downloading it. - - -✏️ **Try it out!** Use the filters to find a text generation model for another language. Feel free to play with the widget and use it in a pipeline! - - +> [!TIP] +> ✏️ **Try it out!** Use the filters to find a text generation model for another language. Feel free to play with the widget and use it in a pipeline! ### Inference Providers[[inference-providers]] @@ -229,11 +213,8 @@ unmasker("This course will teach you all about models.", top_k=2) The `top_k` argument controls how many possibilities you want to be displayed. Note that here the model fills in the special `` word, which is often referred to as a *mask token*. Other mask-filling models might have different mask tokens, so it's always good to verify the proper mask word when exploring other models. One way to check it is by looking at the mask word used in the widget. - - -✏️ **Try it out!** Search for the `bert-base-cased` model on the Hub and identify its mask word in the Inference API widget. What does this model predict for the sentence in our `pipeline` example above? - - +> [!TIP] +> ✏️ **Try it out!** Search for the `bert-base-cased` model on the Hub and identify its mask word in the Inference API widget. What does this model predict for the sentence in our `pipeline` example above? ## Named entity recognition[[named-entity-recognition]] @@ -257,11 +238,8 @@ Here the model correctly identified that Sylvain is a person (PER), Hugging Face We pass the option `grouped_entities=True` in the pipeline creation function to tell the pipeline to regroup together the parts of the sentence that correspond to the same entity: here the model correctly grouped "Hugging" and "Face" as a single organization, even though the name consists of multiple words. In fact, as we will see in the next chapter, the preprocessing even splits some words into smaller parts. For instance, `Sylvain` is split into four pieces: `S`, `##yl`, `##va`, and `##in`. In the post-processing step, the pipeline successfully regrouped those pieces. - - -✏️ **Try it out!** Search the Model Hub for a model able to do part-of-speech tagging (usually abbreviated as POS) in English. What does this model predict for the sentence in the example above? - - +> [!TIP] +> ✏️ **Try it out!** Search the Model Hub for a model able to do part-of-speech tagging (usually abbreviated as POS) in English. What does this model predict for the sentence in the example above? ## Question answering[[question-answering]] @@ -345,11 +323,8 @@ translator("Ce cours est produit par Hugging Face.") Like with text generation and summarization, you can specify a `max_length` or a `min_length` for the result. - - -✏️ **Try it out!** Search for translation models in other languages and try to translate the previous sentence into a few different languages. - - +> [!TIP] +> ✏️ **Try it out!** Search for translation models in other languages and try to translate the previous sentence into a few different languages. ## Image and audio pipelines diff --git a/chapters/en/chapter1/4.mdx b/chapters/en/chapter1/4.mdx index 3870b541f..7500f6376 100644 --- a/chapters/en/chapter1/4.mdx +++ b/chapters/en/chapter1/4.mdx @@ -7,11 +7,8 @@ In this section, we will take a look at the architecture of Transformer models and dive deeper into the concepts of attention, encoder-decoder architecture, and more. - - -🚀 We're taking things up a notch here. This section is detailed and technical, so don't worry if you don't understand everything right away. We'll come back to these concepts later in the course. - - +> [!WARNING] +> 🚀 We're taking things up a notch here. This section is detailed and technical, so don't worry if you don't understand everything right away. We'll come back to these concepts later in the course. ## A bit of Transformer history[[a-bit-of-transformer-history]] @@ -34,8 +31,7 @@ The [Transformer architecture](https://arxiv.org/abs/1706.03762) was introduced - **May 2020**, [GPT-3](https://huggingface.co/papers/2005.14165), an even bigger version of GPT-2 that is able to perform well on a variety of tasks without the need for fine-tuning (called _zero-shot learning_) -- **January 2022**: [InstructGPT](https://huggingface.co/papers/2203.02155), a version of GPT-3 that was trained to follow instructions better -This list is far from comprehensive, and is just meant to highlight a few of the different kinds of Transformer models. Broadly, they can be grouped into three categories: +- **January 2022**: [InstructGPT](https://huggingface.co/papers/2203.02155), a version of GPT-3 that was trained to follow instructions better. - **January 2023**: [Llama](https://huggingface.co/papers/2302.13971), a large language model that is able to generate text in a variety of languages. @@ -45,6 +41,8 @@ This list is far from comprehensive, and is just meant to highlight a few of the - **November 2024**: [SmolLM2](https://huggingface.co/papers/2502.02737), a state-of-the-art small language model (135 million to 1.7 billion parameters) that achieves impressive performance despite its compact size, and unlocking new possibilities for mobile and edge devices. +This list is far from comprehensive, and is just meant to highlight a few of the different kinds of Transformer models. Broadly, they can be grouped into three categories: + - GPT-like (also called _auto-regressive_ Transformer models) - BERT-like (also called _auto-encoding_ Transformer models) - T5-like (also called _sequence-to-sequence_ Transformer models) diff --git a/chapters/en/chapter1/5.mdx b/chapters/en/chapter1/5.mdx index 339fe62a4..f00f6643b 100644 --- a/chapters/en/chapter1/5.mdx +++ b/chapters/en/chapter1/5.mdx @@ -4,11 +4,8 @@ In [Transformers, what can they do?](/course/chapter1/3), you learned about natural language processing (NLP), speech and audio, computer vision tasks, and some important applications of them. This page will look closely at how models solve these tasks and explain what's happening under the hood. There are many ways to solve a given task, some models may implement certain techniques or even approach the task from a new angle, but for Transformer models, the general idea is the same. Owing to its flexible architecture, most models are a variant of an encoder, a decoder, or an encoder-decoder structure. - - -Before diving into specific architectural variants, it's helpful to understand that most tasks follow a similar pattern: input data is processed through a model, and the output is interpreted for a specific task. The differences lie in how the data is prepared, what model architecture variant is used, and how the output is processed. - - +> [!TIP] +> Before diving into specific architectural variants, it's helpful to understand that most tasks follow a similar pattern: input data is processed through a model, and the output is interpreted for a specific task. The differences lie in how the data is prepared, what model architecture variant is used, and how the output is processed. To explain how tasks are solved, we'll walk through what goes on inside the model to output useful predictions. We'll cover the following models and their corresponding tasks: @@ -21,11 +18,8 @@ To explain how tasks are solved, we'll walk through what goes on inside the mode - [GPT2](https://huggingface.co/docs/transformers/model_doc/gpt2) for NLP tasks like text generation that use a decoder - [BART](https://huggingface.co/docs/transformers/model_doc/bart) for NLP tasks like summarization and translation that use an encoder-decoder - - -Before you go further, it is good to have some basic knowledge of the original Transformer architecture. Knowing how encoders, decoders, and attention work will aid you in understanding how different Transformer models work. Be sure to check out our [the previous section](https://huggingface.co/course/chapter1/4?fw=pt) for more information! - - +> [!TIP] +> Before you go further, it is good to have some basic knowledge of the original Transformer architecture. Knowing how encoders, decoders, and attention work will aid you in understanding how different Transformer models work. Be sure to check out our [the previous section](https://huggingface.co/course/chapter1/4?fw=pt) for more information! ## Transformer models for language @@ -59,11 +53,8 @@ As we covered in the previous section, language models are typically pretrained In the following sections, we'll explore specific model architectures and how they're applied to various tasks across speech, vision, and text domains. - - -Understanding which part of the Transformer architecture (encoder, decoder, or both) is best suited for a particular NLP task is key to choosing the right model. Generally, tasks requiring bidirectional context use encoders, tasks generating text use decoders, and tasks converting one sequence to another use encoder-decoders. - - +> [!TIP] +> Understanding which part of the Transformer architecture (encoder, decoder, or both) is best suited for a particular NLP task is key to choosing the right model. Generally, tasks requiring bidirectional context use encoders, tasks generating text use decoders, and tasks converting one sequence to another use encoder-decoders. ### Text generation @@ -83,9 +74,8 @@ GPT-2's pretraining objective is based entirely on [causal language modeling](ht Ready to try your hand at text generation? Check out our complete [causal language modeling guide](https://huggingface.co/docs/transformers/tasks/language_modeling#causal-language-modeling) to learn how to finetune DistilGPT-2 and use it for inference! - -For more information about text generation, check out the [text generation strategies](generation_strategies) guide! - +> [!TIP] +> For more information about text generation, check out the [text generation strategies](https://huggingface.co/docs/transformers/generation_strategies#generation-strategies) guide! ### Text classification @@ -121,11 +111,8 @@ To use BERT for question answering, add a span classification head on top of the Ready to try your hand at question answering? Check out our complete [question answering guide](https://huggingface.co/docs/transformers/tasks/question_answering) to learn how to finetune DistilBERT and use it for inference! - - -💡 Notice how easy it is to use BERT for different tasks once it's been pretrained. You only need to add a specific head to the pretrained model to manipulate the hidden states into your desired output! - - +> [!TIP] +> 💡 Notice how easy it is to use BERT for different tasks once it's been pretrained. You only need to add a specific head to the pretrained model to manipulate the hidden states into your desired output! ### Summarization @@ -143,11 +130,8 @@ Encoder-decoder models like [BART](https://huggingface.co/docs/transformers/mode Ready to try your hand at summarization? Check out our complete [summarization guide](https://huggingface.co/docs/transformers/tasks/summarization) to learn how to finetune T5 and use it for inference! - - -For more information about text generation, check out the [text generation strategies](https://huggingface.co/docs/transformers/generation_strategies) guide! - - +> [!TIP] +> For more information about text generation, check out the [text generation strategies](https://huggingface.co/docs/transformers/generation_strategies) guide! ### Translation @@ -158,11 +142,8 @@ BART has since been followed up by a multilingual version, mBART, intended for t Ready to try your hand at translation? Check out our complete [translation guide](https://huggingface.co/docs/transformers/tasks/translation) to learn how to finetune T5 and use it for inference! - - -As you've seen throughout this guide, many models follow similar patterns despite addressing different tasks. Understanding these common patterns can help you quickly grasp how new models work and how to adapt existing models to your specific needs. - - +> [!TIP] +> As you've seen throughout this guide, many models follow similar patterns despite addressing different tasks. Understanding these common patterns can help you quickly grasp how new models work and how to adapt existing models to your specific needs. ## Modalities beyond text @@ -190,11 +171,8 @@ Whisper was pretrained on a massive and diverse dataset of 680,000 hours of labe Now that Whisper is pretrained, you can use it directly for zero-shot inference or finetune it on your data for improved performance on specific tasks like automatic speech recognition or speech translation! - - -The key innovation in Whisper is its training on an unprecedented scale of diverse, weakly supervised audio data from the internet. This allows it to generalize remarkably well to different languages, accents, and tasks without task-specific finetuning. - - +> [!TIP] +> The key innovation in Whisper is its training on an unprecedented scale of diverse, weakly supervised audio data from the internet. This allows it to generalize remarkably well to different languages, accents, and tasks without task-specific finetuning. ### Automatic speech recognition @@ -223,11 +201,8 @@ There are two ways to approach computer vision tasks: 1. Split an image into a sequence of patches and process them in parallel with a Transformer. 2. Use a modern CNN, like [ConvNeXT](https://huggingface.co/docs/transformers/model_doc/convnext), which relies on convolutional layers but adopts modern network designs. - - -A third approach mixes Transformers with convolutions (for example, [Convolutional Vision Transformer](https://huggingface.co/docs/transformers/model_doc/cvt) or [LeViT](https://huggingface.co/docs/transformers/model_doc/levit)). We won't discuss those because they just combine the two approaches we examine here. - - +> [!TIP] +> A third approach mixes Transformers with convolutions (for example, [Convolutional Vision Transformer](https://huggingface.co/docs/transformers/model_doc/cvt) or [LeViT](https://huggingface.co/docs/transformers/model_doc/levit)). We won't discuss those because they just combine the two approaches we examine here. ViT and ConvNeXT are commonly used for image classification, but for other vision tasks like object detection, segmentation, and depth estimation, we'll look at DETR, Mask2Former and GLPN, respectively; these models are better suited for those tasks. @@ -256,8 +231,5 @@ The main change ViT introduced was in how images are fed to a Transformer: Ready to try your hand at image classification? Check out our complete [image classification guide](https://huggingface.co/docs/transformers/tasks/image_classification) to learn how to fine-tune ViT and use it for inference! - - -Notice the parallel between ViT and BERT: both use a special token ([CLS]) to capture the overall representation, both add position information to their embeddings, and both use a Transformer encoder to process the sequence of tokens/patches. - - +> [!TIP] +> Notice the parallel between ViT and BERT: both use a special token ([CLS]) to capture the overall representation, both add position information to their embeddings, and both use a Transformer encoder to process the sequence of tokens/patches. diff --git a/chapters/en/chapter1/6.mdx b/chapters/en/chapter1/6.mdx index e540c8624..049078cf3 100644 --- a/chapters/en/chapter1/6.mdx +++ b/chapters/en/chapter1/6.mdx @@ -5,16 +5,13 @@ # Transformer Architectures[[transformer-architectures]] -In the previous sections, we introduced the general Transformer architecture and explored how these models can solve various tasks. Now, let's take a closer look at the three main architectural variants of Transformer models and understand when to use each one. Then, we looked at how those architectures are applied to different language tasks. +In the previous sections, we introduced the general Transformer architecture and explored how these models can solve various tasks. Now, let's take a closer look at the three main architectural variants of Transformer models and understand when to use each one. Then, we look at how those architectures are applied to different language tasks. In this section, we're going to dive deeper into the three main architectural variants of Transformer models and understand when to use each one. - - -Remember that most Transformer models use one of three architectures: encoder-only, decoder-only, or encoder-decoder (sequence-to-sequence). Understanding these differences will help you choose the right model for your specific task. - - +> [!TIP] +> Remember that most Transformer models use one of three architectures: encoder-only, decoder-only, or encoder-decoder (sequence-to-sequence). Understanding these differences will help you choose the right model for your specific task. ## Encoder models[[encoder-models]] @@ -26,11 +23,8 @@ The pretraining of these models usually revolves around somehow corrupting a giv Encoder models are best suited for tasks requiring an understanding of the full sentence, such as sentence classification, named entity recognition (and more generally word classification), and extractive question answering. - - -As we saw in [How 🤗 Transformers solve tasks](/chapter1/5), encoder models like BERT excel at understanding text because they can look at the entire context in both directions. This makes them perfect for tasks where comprehension of the whole input is important. - - +> [!TIP] +> As we saw in [How 🤗 Transformers solve tasks](https://huggingface.co/learn/llm-course/chapter1/5), encoder models like BERT excel at understanding text because they can look at the entire context in both directions. This makes them perfect for tasks where comprehension of the whole input is important. Representatives of this family of models include: @@ -48,11 +42,8 @@ The pretraining of decoder models usually revolves around predicting the next wo These models are best suited for tasks involving text generation. - - -Decoder models like GPT are designed to generate text by predicting one token at a time. As we explored in [How 🤗 Transformers solve tasks](/chapter1/5), they can only see previous tokens, which makes them excellent for creative text generation but less ideal for tasks requiring bidirectional understanding. - - +> [!TIP] +> Decoder models like GPT are designed to generate text by predicting one token at a time. As we explored in [How 🤗 Transformers solve tasks](https://huggingface.co/learn/llm-course/chapter1/5), they can only see previous tokens, which makes them excellent for creative text generation but less ideal for tasks requiring bidirectional understanding. Representatives of this family of models include: @@ -85,7 +76,7 @@ Modern decoder-based LLMs have demonstrated impressive capabilities: | Reasoning | Working through problems step by step | Solving math problems or logical puzzles | | Few-shot learning | Learning from a few examples in the prompt | Classifying text after seeing just 2-3 examples | -You can experiment with decoder-based LLMs directly in your browser via model repo pages on the Hub. Here's an an example with the classic [GPT-2](https://huggingface.co/openai-community/gpt2) (OpenAI's finest open source model!): +You can experiment with decoder-based LLMs directly in your browser via model repo pages on the Hub. Here's an example with the classic [GPT-2](https://huggingface.co/openai-community/gpt2) (OpenAI's finest open source model!): + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Quiz**: သင်၏ ဗဟုသုတ သို့မဟုတ် စွမ်းရည်များကို စမ်းသပ်ရန်အတွက် မေးခွန်းတိုများ။ +* **Hugging Face Account**: Hugging Face ပလက်ဖောင်းပေါ်ရှိ သုံးစွဲသူအကောင့်။ ၎င်းသည် မော်ဒယ်များ၊ datasets များနှင့် အခြားအရင်းအမြစ်များကို ဝင်ရောက်ကြည့်ရှုရန် ခွင့်ပြုသည်။ +* **Supervised Finetuning**: ကြိုတင်လေ့ကျင့်ထားပြီးသား (pre-trained) မော်ဒယ်တစ်ခုကို တံဆိပ်တပ်ထားသော (labeled) ဒေတာများဖြင့် သီးခြားလုပ်ငန်းတစ်ခု (specific task) အတွက် ထပ်မံလေ့ကျင့်ပေးသော လုပ်ငန်းစဉ်။ "Supervised" ဆိုသည်မှာ မှန်ကန်သောအဖြေ (label) ကို မော်ဒယ်က သင်ယူရန်အတွက် ပေးထားသည်ကို ရည်ညွှန်းသည်။ \ No newline at end of file diff --git a/chapters/my/chapter1/2.mdx b/chapters/my/chapter1/2.mdx new file mode 100644 index 000000000..08a9fd9de --- /dev/null +++ b/chapters/my/chapter1/2.mdx @@ -0,0 +1,83 @@ +# Natural Language Processing နှင့် Large Language Models များ[[natural-language-processing-and-large-language-models]] + + + +Transformer မော်ဒယ်တွေထဲကို မဝင်ခင်မှာ Natural Language Processing (NLP) ဆိုတာ ဘာလဲ၊ Large Language Models (LLMs) တွေက ဒီနယ်ပယ်ကို ဘယ်လို ပြောင်းလဲပစ်ခဲ့လဲ၊ ဘာကြောင့် ဒါတွေကို ကျွန်တော်တို့ ဂရုစိုက်သင့်လဲဆိုတာကို အကျဉ်းချုပ်လေး ကြည့်ရအောင်။ + +## NLP ဆိုတာ ဘာလဲ။[[what-is-nlp]] + + + +Natural Language Processing (NLP) ဆိုတာက ဘာသာဗေဒပညာရပ်နဲ့ Machine Learning နယ်ပယ်ရဲ့ ဘာသာစကားဆိုင်ရာ အားလုံးကို နားလည်ဖို့ အဓိကထားတဲ့ ဘာသာရပ်ခွဲတစ်ခု ဖြစ်ပါတယ်။ NLP လုပ်ငန်းတာဝန်တွေရဲ့ ရည်ရွယ်ချက်ကတော့ တစ်လုံးချင်းစီရဲ့ စကားလုံးတွေကို နားလည်ဖို့တင်မဟုတ်ဘဲ အဲဒီစကားလုံးတွေရဲ့ အကြောင်းအရာ (context) ကိုပါ နားလည်နိုင်ဖို့ ဖြစ်ပါတယ်။ + +အောက်ပါတို့ကတော့ အသုံးများတဲ့ NLP လုပ်ငန်းတာဝန်တွေနဲ့ ဥပမာအချို့ ဖြစ်ပါတယ်။ + +- **စာကြောင်းအပြည့်အစုံကို အမျိုးအစားခွဲခြားခြင်း (Classifying whole sentences)**- ဝေဖန်သုံးသပ်ချက်တစ်ခုရဲ့ စိတ်ခံစားမှု (sentiment) ကို ရှာဖွေခြင်း၊ email တစ်ခုက spam ဟုတ်မဟုတ် သိရှိခြင်း၊ စာကြောင်းတစ်ကြောင်းရဲ့ သဒ္ဒါမှန်ကန်မှုကို ဆုံးဖြတ်ခြင်း၊ သို့မဟုတ် စာကြောင်းနှစ်ကြောင်းက ယုတ္တိရှိရှိ ဆက်စပ်မှုရှိမရှိ ဆုံးဖြတ်ခြင်း။ +- **စာကြောင်းတစ်ကြောင်းရှိ စကားလုံးတစ်လုံးစီကို အမျိုးအစားခွဲခြားခြင်း (Classifying each word in a sentence)**- စာကြောင်းတစ်ခုရဲ့ သဒ္ဒါဆိုင်ရာ အစိတ်အပိုင်းများ (နာမ်၊ ကြိယာ၊ နာမဝိသေသန) ကို ဖော်ထုတ်ခြင်း၊ သို့မဟုတ် သီးခြားအမည်များ (လူ၊ နေရာ၊ အဖွဲ့အစည်း) ကို ဖော်ထုတ်ခြင်း။ +- **စာသားအကြောင်းအရာ ဖန်တီးခြင်း (Generating text content)**- အလိုအလျောက်ဖန်တီးထားသော စာသားဖြင့် မေးခွန်းတစ်ခုကို ဖြည့်စွက်ခြင်း၊ စာသားတစ်ခုရှိ ကွက်လပ်များကို ဝှက်ထားသော စကားလုံးများဖြင့် ဖြည့်ဆည်းခြင်း။ +- **စာသားမှ အဖြေထုတ်ယူခြင်း (Extracting an answer from a text)**- မေးခွန်းတစ်ခုနဲ့ အကြောင်းအရာတစ်ခု ပေးထားပြီးနောက်၊ အဲဒီအကြောင်းအရာမှာ ပါဝင်တဲ့ အချက်အလက်တွေအပေါ် အခြေခံပြီး မေးခွန်းရဲ့အဖြေကို ထုတ်ယူခြင်း။ +- **input စာသားတစ်ခုမှ စာကြောင်းအသစ် ဖန်တီးခြင်း (Generating a new sentence from an input text)**- စာသားတစ်ခုကို အခြားဘာသာစကားတစ်ခုသို့ ဘာသာပြန်ခြင်း၊ စာသားတစ်ခုကို အကျဉ်းချုပ်ခြင်း။ + +NLP ဟာ စာဖြင့်ရေးသားထားတဲ့ စာသားတွေအတွက်ပဲလို့ ကန့်သတ်ထားတာ မဟုတ်ပါဘူး။ ၎င်းဟာ အသံဖိုင်တစ်ခုရဲ့ မှတ်တမ်းကို ဖန်တီးခြင်း ဒါမှမဟုတ် ပုံတစ်ပုံကို ဖော်ပြခြင်းစတဲ့ speech recognition နဲ့ computer vision နယ်ပယ်တွေမှာ ရှုပ်ထွေးတဲ့ စိန်ခေါ်မှုတွေကိုပါ ဖြေရှင်းပေးပါတယ်။ + +## Large Language Models (LLMs) တွေ ပေါ်ပေါက်လာခြင်း[[rise-of-llms]] + +မကြာသေးခင်နှစ်များအတွင်းမှာတော့ Natural Language Processing (NLP) နယ်ပယ်ဟာ Large Language Models (LLMs) တွေကြောင့် တော်လှန်ပြောင်းလဲမှုတွေ ကြုံခဲ့ရပါတယ်။ GPT (Generative Pre-trained Transformer) နဲ့ [Llama](https://huggingface.co/meta-llama) လို architecture တွေပါဝင်တဲ့ ဒီမော်ဒယ်တွေဟာ ဘာသာစကားလုပ်ဆောင်ခြင်း နယ်ပယ်မှာ ဖြစ်နိုင်ခြေရှိတဲ့ အရာတွေကို ပြောင်းလဲပစ်ခဲ့ပါတယ်။ + +> [!TIP] +> Large Language Model (LLM) ဆိုတာ များပြားလှတဲ့ စာသားဒေတာတွေနဲ့ လေ့ကျင့်ထားတဲ့ Artificial Intelligence (AI) မော်ဒယ်တစ်ခုဖြစ်ပြီး လူသားဆန်တဲ့ စာသားတွေကို နားလည်၊ ဖန်တီးနိုင်ပါတယ်။ ဘာသာစကားပုံစံတွေကို မှတ်မိနိုင်ပြီး သီးခြားလုပ်ငန်းအတွက် လေ့ကျင့်မှုမရှိဘဲ ဘာသာစကားလုပ်ငန်းမျိုးစုံကို လုပ်ဆောင်နိုင်ပါတယ်။ ၎င်းတို့ဟာ Natural Language Processing (NLP) နယ်ပယ်မှာ သိသိသာသာ တိုးတက်မှုတစ်ခုကို ကိုယ်စားပြုပါတယ်။ + +LLMs တွေရဲ့ ထူးခြားချက်တွေကတော့: +- **အတိုင်းအတာ (Scale)**: ၎င်းတို့မှာ parameters သန်းပေါင်းများစွာ၊ ဘီလီယံပေါင်းများစွာ သို့မဟုတ် ရာပေါင်းများစွာသော ဘီလီယံပေါင်းများစွာ ပါဝင်ပါတယ်။ +- **ယေဘုယျစွမ်းရည်များ (General capabilities)**: သီးခြားလုပ်ငန်းအတွက် လေ့ကျင့်မှုမရှိဘဲ လုပ်ငန်းတာဝန်ပေါင်းများစွာကို လုပ်ဆောင်နိုင်ပါတယ်။ +- **အကြောင်းအရာပေါ်မူတည်၍ သင်ယူခြင်း (In-context learning)**: prompt မှာ ပေးထားတဲ့ ဥပမာတွေကနေ သင်ယူနိုင်ပါတယ်။ +- **ပေါ်ထွက်လာသော စွမ်းရည်များ (Emergent abilities)**: ဒီမော်ဒယ်တွေရဲ့ အရွယ်အစား ကြီးမားလာတာနဲ့အမျှ ၎င်းတို့ဟာ ရှင်းရှင်းလင်းလင်း programming လုပ်ထားခြင်း မရှိတဲ့ ဒါမှမဟုတ် ကြိုတင်မမျှော်လင့်ထားတဲ့ စွမ်းရည်တွေကို ပြသလာပါတယ်။ + +LLMs တွေ ပေါ်ပေါက်လာတာနဲ့အမျှ NLP လုပ်ငန်းတာဝန်အမျိုးမျိုးအတွက် သီးသန့်မော်ဒယ်တွေ တည်ဆောက်တဲ့ ပုံစံကနေ ဘာသာစကားလုပ်ငန်းအမျိုးမျိုးကို ဖြေရှင်းနိုင်ဖို့ prompt ပေးနိုင်တဲ့ ဒါမှမဟုတ် fine-tune လုပ်နိုင်တဲ့ မော်ဒယ်ကြီးတစ်လုံးကို အသုံးပြုတဲ့ ပုံစံကို ပြောင်းလဲသွားစေခဲ့ပါတယ်။ ဒါက အဆင့်မြင့် ဘာသာစကားလုပ်ဆောင်မှုကို ပိုမိုလက်လှမ်းမီစေခဲ့ပေမယ့် ထိရောက်မှု၊ ကျင့်ဝတ်နဲ့ အသုံးပြုမှု (deployment) စတဲ့ နယ်ပယ်တွေမှာ စိန်ခေါ်မှုအသစ်တွေကိုလည်း မိတ်ဆက်ပေးခဲ့ပါတယ်။ + +သို့သော် LLMs တွေမှာ အရေးကြီးတဲ့ ကန့်သတ်ချက်တွေလည်း ရှိပါတယ်။ +- **ထင်ယောင်ထင်မှားဖြစ်ခြင်း (Hallucinations)**: ၎င်းတို့ဟာ မမှန်ကန်တဲ့ အချက်အလက်တွေကို ယုံကြည်မှုရှိရှိ ထုတ်လုပ်နိုင်ပါတယ်။ +- **စစ်မှန်သော နားလည်မှု ကင်းမဲ့ခြင်း (Lack of true understanding)**: ၎င်းတို့ဟာ ကမ္ဘာကြီးကို စစ်မှန်စွာ နားလည်ခြင်းမရှိဘဲ စာရင်းအင်းဆိုင်ရာ ပုံစံတွေပေါ်မှာသာ လည်ပတ်ပါတယ်။ +- **ဘက်လိုက်မှု (Bias)**: ၎င်းတို့ရဲ့ သင်ကြားမှုဒေတာ ဒါမှမဟုတ် inputs တွေမှာ ပါဝင်တဲ့ ဘက်လိုက်မှုတွေကို ပြန်လည်ထုတ်လုပ်နိုင်ပါတယ်။ +- **Context windows ကန့်သတ်ချက် (Limited context windows)**: ၎င်းတို့မှာ ကန့်သတ်ထားတဲ့ context windows များ ရှိပါတယ်။ (ဒါပေမယ့် တိုးတက်လာနေပါပြီ) +- **ကွန်ပျူတာ အရင်းအမြစ်များ (Computational resources)**: ၎င်းတို့ဟာ ကွန်ပျူတာ အရင်းအမြစ်များစွာ လိုအပ်ပါတယ်။ + + +## ဘာကြောင့် ဘာသာစကား လုပ်ဆောင်ခြင်း (language processing) က ခက်ခဲရတာလဲ။[[why-is-it-challenging]] + +ကွန်ပျူတာတွေဟာ လူသားတွေလိုမျိုး သတင်းအချက်အလက်တွေကို လုပ်ဆောင်တာ မဟုတ်ပါဘူး။ ဥပမာအနေနဲ့ "I am hungry" ဆိုတဲ့ စာကြောင်းကို ဖတ်တဲ့အခါ ကျွန်တော်တို့က အဓိပ္ပာယ်ကို အလွယ်တကူ နားလည်နိုင်ပါတယ်။ အလားတူပဲ "I am hungry" နဲ့ "I am sad" ဆိုတဲ့ စာကြောင်းနှစ်ကြောင်း ပေးထားရင် ၎င်းတို့ ဘယ်လောက်တူညီလဲဆိုတာကို အလွယ်တကူ ဆုံးဖြတ်နိုင်ပါတယ်။ Machine Learning (ML) မော်ဒယ်တွေအတွက်တော့ ဒီလိုလုပ်ငန်းတာဝန်တွေက ပိုခက်ခဲပါတယ်။ စာသားကို မော်ဒယ်ကနေ သင်ယူနိုင်တဲ့ ပုံစံမျိုးနဲ့ လုပ်ဆောင်ဖို့ လိုပါတယ်။ ဘာသာစကားက ရှုပ်ထွေးတာကြောင့် ဒီလုပ်ငန်းစဉ်ကို ဘယ်လိုလုပ်ရမလဲဆိုတာ သေချာစဉ်းစားဖို့ လိုပါတယ်။ စာသားတွေကို ဘယ်လိုကိုယ်စားပြုမလဲဆိုတာနဲ့ ပတ်သက်ပြီး သုတေသနတွေ အများကြီးလုပ်ထားပြီး နောက်အခန်းမှာ နည်းလမ်းအချို့ကို ကြည့်ရှုသွားမှာပါ။ + +LLMs တွေမှာ တိုးတက်မှုတွေ ရှိလာပေမယ့်လည်း၊ အဓိပ္ပာယ်ဝေဝါးမှု၊ ယဉ်ကျေးမှုဆိုင်ရာ အကြောင်းအရာ (cultural context)၊ လှောင်ပြောင်မှု (sarcasm) နဲ့ ဟာသ (humor) တွေကို နားလည်ခြင်း စတဲ့ အခြေခံကျတဲ့ စိန်ခေါ်မှုများစွာ ကျန်ရှိနေပါသေးတယ်။ LLMs တွေက မတူညီတဲ့ ဒေတာအစုအဝေးများစွာပေါ်မှာ အကြီးအကျယ် လေ့ကျင့်ထားခြင်းဖြင့် ဒီစိန်ခေါ်မှုတွေကို ဖြေရှင်းပေမယ့်၊ ရှုပ်ထွေးတဲ့ အခြေအနေများစွာမှာ လူသားအဆင့် နားလည်မှုအထိတော့ မရောက်နိုင်သေးပါဘူး။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Natural Language Processing (NLP)**: ကွန်ပျူတာတွေ လူသားဘာသာစကားကို နားလည်၊ အဓိပ္ပာယ်ဖော်ပြီး၊ ဖန်တီးနိုင်အောင် လုပ်ဆောင်ပေးတဲ့ Artificial Intelligence (AI) ရဲ့ နယ်ပယ်ခွဲတစ်ခု ဖြစ်ပါတယ်။ ဥပမာအားဖြင့် စာသားခွဲခြမ်းစိတ်ဖြာခြင်း၊ ဘာသာပြန်ခြင်း စသည်တို့ ပါဝင်ပါတယ်။ +* **Large Language Models (LLMs)**: လူသားဘာသာစကားကို နားလည်ပြီး ထုတ်လုပ်ပေးနိုင်တဲ့ အလွန်ကြီးမားတဲ့ Artificial Intelligence (AI) မော်ဒယ်တွေ ဖြစ်ပါတယ်။ ၎င်းတို့ဟာ ဒေတာအမြောက်အမြားနဲ့ သင်ကြားလေ့ကျင့်ထားပြီး စာရေးတာ၊ မေးခွန်းဖြေတာ စတဲ့ ဘာသာစကားဆိုင်ရာ လုပ်ငန်းမျိုးစုံကို လုပ်ဆောင်နိုင်ပါတယ်။ +* **Transformer Models**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Machine Learning (ML)**: ကွန်ပျူတာတွေဟာ ဒေတာတွေကနေ သင်ယူပြီး လုပ်ငန်းဆောင်တာတွေကို လူသားတွေရဲ့ ညွှန်ကြားချက်မပါဘဲ ကိုယ်တိုင်လုပ်ဆောင်နိုင်အောင် လုပ်ဆောင်ပေးတဲ့ Artificial Intelligence (AI) ရဲ့ နယ်ပယ်ခွဲတစ်ခု ဖြစ်ပါတယ်။ +* **Context**: စကားလုံး၊ စာကြောင်း သို့မဟုတ် အကြောင်းအရာတစ်ခုရဲ့ အဓိပ္ပာယ်ကို နားလည်စေရန် ကူညီပေးသော ပတ်ဝန်းကျင်ရှိ အချက်အလက်များ။ +* **Sentiment**: လူတစ်ဦးရဲ့ ခံစားချက်၊ သဘောထား ဒါမှမဟုတ် အမြင်ကို ဖော်ပြတဲ့ အရာ။ (ဥပမာ- ကောင်းတယ်၊ ဆိုးတယ်၊ ကြားနေ) +* **Spam**: မလိုအပ်ဘဲ အစုလိုက်အပြုံလိုက် ပေးပို့သော email များ သို့မဟုတ် မက်ဆေ့ခ်ျများ။ +* **Grammatically Correct**: သဒ္ဒါစည်းမျဉ်းစည်းကမ်းများနှင့် ကိုက်ညီခြင်း။ +* **Named Entities**: စာသားတစ်ခုထဲတွင် ပါဝင်သော လူအမည်၊ နေရာအမည်၊ အဖွဲ့အစည်းအမည် သို့မဟုတ် အခြားသီးခြားအမည်များ။ +* **Text Generation**: AI မော်ဒယ်များကို အသုံးပြု၍ လူသားကဲ့သို့သော စာသားအသစ်များ ဖန်တီးခြင်း။ +* **Masked Words**: စာသားတစ်ခုထဲတွင် ဝှက်ထားသော သို့မဟုတ် ဖုံးကွယ်ထားသော စကားလုံးများ။ +* **Speech Recognition**: ပြောဆိုသော ဘာသာစကားကို ကွန်ပျူတာက စာသားအဖြစ် ပြောင်းလဲနားလည်နိုင်သည့် နည်းပညာ။ +* **Computer Vision**: ကွန်ပျူတာများကို ပုံရိပ်များ၊ ဗီဒီယိုများကို လူသားများကဲ့သို့ မြင်၊ နားလည်နိုင်အောင် သင်ကြားပေးသည့် Artificial Intelligence (AI) နယ်ပယ်။ +* **Transcript**: အသံ သို့မဟုတ် စကားပြောကို စာသားအဖြစ် ရေးသားထားခြင်း။ +* **GPT (Generative Pre-trained Transformer)**: OpenAI မှ တီထွင်ထားသော Transformer-based Large Language Model (LLM) အမျိုးအစားတစ်ခု။ +* **Llama**: Meta မှ တီထွင်ထားသော Transformer-based Large Language Model (LLM) အမျိုးအစားတစ်ခု။ +* **Parameters**: Machine Learning မော်ဒယ်တစ်ခု၏ သင်ယူနိုင်သော အစိတ်အပိုင်းများ။ ၎င်းတို့သည် လေ့ကျင့်နေစဉ်အတွင်း ဒေတာများမှ ပုံစံများကို သင်ယူကာ ချိန်ညှိပေးသည်။ +* **Prompt**: Large Language Models (LLMs) ကို တိကျသောလုပ်ငန်းတစ်ခု လုပ်ဆောင်ရန် သို့မဟုတ် အချက်အလက်ပေးရန်အတွက် ပေးပို့သော input text သို့မဟုတ် မေးခွန်း။ +* **Fine-tune**: ကြိုတင်လေ့ကျင့်ထားပြီးသား (pre-trained) မော်ဒယ်တစ်ခုကို သီးခြားလုပ်ငန်းတစ်ခု (specific task) အတွက် အနည်းငယ်သော ဒေတာနဲ့ ထပ်မံလေ့ကျင့်ပေးခြင်းကို ဆိုလိုပါတယ်။ +* **Deployment**: Machine Learning မော်ဒယ်တစ်ခုကို အမှန်တကယ် အသုံးပြုနိုင်သော စနစ် သို့မဟုတ် environment တစ်ခုထဲသို့ ထည့်သွင်းခြင်း။ +* **Hallucinations**: Artificial Intelligence (AI) မော်ဒယ်များမှ မှန်ကန်မှုမရှိသော သို့မဟုတ် အဓိပ္ပာယ်မရှိသော အချက်အလက်များကို ယုံကြည်မှုရှိရှိ ထုတ်လုပ်ပေးခြင်း။ +* **Bias**: ဒေတာအစုအဝေး (dataset) သို့မဟုတ် မော်ဒယ်၏ လေ့ကျင့်မှုပုံစံကြောင့် ဖြစ်ပေါ်လာသော ဘက်လိုက်မှုများ။ +* **Context Windows**: Large Language Models (LLMs) တစ်ခုက တစ်ပြိုင်နက်တည်း ပြန်လည်ကြည့်ရှုနိုင်သော သို့မဟုတ် စီမံဆောင်ရွက်နိုင်သော input text ၏ ပမာဏ။ +* **Computational Resources**: ကွန်ပျူတာစနစ်တစ်ခု၏ လုပ်ဆောင်နိုင်စွမ်းများ (ဥပမာ - CPU, GPU, memory, storage)။ +* **Cultural Context**: လူ့အဖွဲ့အစည်းတစ်ခု၏ ဓလေ့ထုံးတမ်းများ၊ ယုံကြည်မှုများ၊ တန်ဖိုးများနှင့် အပြုအမူများ။ +* **Sarcasm**: ပြက်ရယ်ပြုခြင်း သို့မဟုတ် ဆန့်ကျင်ဘက် အဓိပ္ပာယ်ကို ဖော်ပြရန် စကားလုံးများကို အသုံးပြုခြင်း။ +* **Humor**: ရယ်စရာကောင်းသော သို့မဟုတ် ပျော်ရွှင်စေသော အရာ။ \ No newline at end of file diff --git a/chapters/my/chapter1/3.mdx b/chapters/my/chapter1/3.mdx new file mode 100644 index 000000000..401da476b --- /dev/null +++ b/chapters/my/chapter1/3.mdx @@ -0,0 +1,434 @@ +# Transformers တွေက ဘာတွေလုပ်နိုင်လဲ။[[transformers-what-can-they-do]] + + + +ဒီအပိုင်းမှာတော့ Transformer မော်ဒယ်တွေ ဘာတွေလုပ်နိုင်လဲဆိုတာကို ကြည့်ရှုသွားမှာဖြစ်ပြီး Hugging Face ရဲ့ 🤗 Transformers library ထဲက ပထမဆုံး ကိရိယာဖြစ်တဲ့ `pipeline()` function ကို အသုံးပြုသွားမှာပါ။ + +> [!TIP] +> 👀 ညာဘက်အပေါ်ထောင့်မှာရှိတဲ့ Open in Colab ခလုတ်ကို မြင်ရလား။ ဒီကဏ္ဍက code ဥပမာတွေ အားလုံးပါဝင်တဲ့ Google Colab notebook ကို ဖွင့်ဖို့ အဲဒီခလုတ်ကို နှိပ်လိုက်ပါ။ ဒီခလုတ်က code ဥပမာတွေ ပါဝင်တဲ့ မည်သည့်ကဏ္ဍမှာမဆို ရှိနေမှာပါ။ +> +> ဥပမာတွေကို ကိုယ်တိုင် run ချင်တယ်ဆိုရင်တော့ setup ကို လေ့လာကြည့်ဖို့ ကျွန်တော်တို့ အကြံပြုပါတယ်။ + +## Transformers တွေက နေရာတိုင်းမှာ ရှိနေပါတယ်![[transformers-are-everywhere]] + +Transformer မော်ဒယ်တွေကို Natural Language Processing (NLP), computer vision, audio processing အပါအဝင် မတူညီတဲ့ နယ်ပယ်အသီးသီးက လုပ်ငန်းတာဝန်မျိုးစုံကို ဖြေရှင်းဖို့ အသုံးပြုပါတယ်။ အောက်ဖော်ပြပါ ကုမ္ပဏီတွေနဲ့ အဖွဲ့အစည်းအချို့ကတော့ Hugging Face နဲ့ Transformer မော်ဒယ်တွေကို အသုံးပြုနေကြပြီး ၎င်းတို့ရဲ့ မော်ဒယ်တွေကို မျှဝေခြင်းဖြင့် လူ့အဖွဲ့အစည်းကိုလည်း ပံ့ပိုးကူညီပေးနေကြပါတယ်။ + +Companies using Hugging Face + +[🤗 Transformers library](https://huggingface.co/docs/transformers/index) ကတော့ အဲဒီမျှဝေထားတဲ့ မော်ဒယ်တွေကို ဖန်တီးပြီး အသုံးပြုနိုင်တဲ့ functionality တွေကို ပံ့ပိုးပေးပါတယ်။ [Model Hub](https://huggingface.co/models) မှာတော့ လူတိုင်း download လုပ်ပြီး အသုံးပြုနိုင်တဲ့ ကြိုတင်လေ့ကျင့်ထားသော (pretrained) မော်ဒယ်ပေါင်း သန်းချီ ပါဝင်ပါတယ်။ သင်ရဲ့ ကိုယ်ပိုင်မော်ဒယ်တွေကိုလည်း Hub ကို upload တင်နိုင်ပါတယ်။ + +> [!TIP] +> ⚠️ Hugging Face Hub ဟာ Transformer မော်ဒယ်တွေအတွက်ပဲ ကန့်သတ်ထားတာ မဟုတ်ပါဘူး။ လူတိုင်းက သူတို့လိုချင်တဲ့ မည်သည့်မော်ဒယ် သို့မဟုတ် dataset ကိုမဆို မျှဝေနိုင်ပါတယ်။ ရရှိနိုင်တဲ့ အင်္ဂါရပ်အားလုံးကို ရယူဖို့ huggingface.co account တစ်ခု ဖန်တီးပါ! + +Transformer မော်ဒယ်တွေ ဘယ်လိုအလုပ်လုပ်လဲဆိုတာကို အသေးစိတ် မလေ့လာခင်မှာ စိတ်ဝင်စားစရာကောင်းတဲ့ NLP ပြဿနာအချို့ကို ဖြေရှင်းဖို့ ဘယ်လိုအသုံးပြုနိုင်လဲဆိုတာကို ဥပမာအချို့နဲ့ ကြည့်ရအောင်။ + +## Pipelines တွေနဲ့ အလုပ်လုပ်ခြင်း[[working-with-pipelines]] + + + +🤗 Transformers library ရဲ့ အခြေခံအကျဆုံး object ကတော့ `pipeline()` function ဖြစ်ပါတယ်။ ၎င်းက မော်ဒယ်တစ်ခုကို ၎င်းရဲ့ လိုအပ်တဲ့ preprocessing နဲ့ postprocessing အဆင့်တွေနဲ့ ချိတ်ဆက်ပေးပြီး ကျွန်တော်တို့ စာသားတစ်ခုခုကို တိုက်ရိုက်ထည့်သွင်းပြီး နားလည်လွယ်တဲ့ အဖြေတစ်ခု ရယူနိုင်စေပါတယ်။ + +```python +from transformers import pipeline + +classifier = pipeline("sentiment-analysis") +classifier("I've been waiting for a HuggingFace course my whole life.") +``` + +```python out +[{'label': 'POSITIVE', 'score': 0.9598047137260437}] +``` + +ကျွန်တော်တို့ စာကြောင်းများစွာကိုတောင် ပေးပို့နိုင်ပါတယ်။ + +```python +classifier( + ["I've been waiting for a HuggingFace course my whole life.", "I hate this so much!"] +) +``` + +```python out +[{'label': 'POSITIVE', 'score': 0.9598047137260437}, + {'label': 'NEGATIVE', 'score': 0.9994558095932007}] +``` + +ပုံမှန်အားဖြင့် ဒီ pipeline ဟာ အင်္ဂလိပ်ဘာသာစကားမှာ sentiment analysis အတွက် fine-tune လုပ်ထားတဲ့ သီးခြား pretrained model တစ်ခုကို ရွေးချယ်ပေးပါတယ်။ `classifier` object ကို ဖန်တီးတဲ့အခါ မော်ဒယ်ကို download လုပ်ပြီး cache လုပ်ထားပါတယ်။ ဒီ command ကို ထပ် run တဲ့အခါ cache လုပ်ထားတဲ့ မော်ဒယ်ကို ပြန်သုံးမှာဖြစ်ပြီး မော်ဒယ်ကို ထပ် download လုပ်ဖို့ မလိုအပ်တော့ပါဘူး။ + +pipeline ထဲကို စာသားအချို့ ပေးပို့တဲ့အခါ အဓိက အဆင့်သုံးဆင့် ပါဝင်ပါတယ်။ + +1. စာသားကို မော်ဒယ် နားလည်နိုင်တဲ့ ပုံစံအဖြစ် preprocessing လုပ်ပါတယ်။ +2. preprocessing လုပ်ထားတဲ့ inputs တွေကို မော်ဒယ်ဆီ ပေးပို့ပါတယ်။ +3. မော်ဒယ်ရဲ့ ခန့်မှန်းချက်တွေကို post-processing လုပ်ပြီး နားလည်လွယ်အောင် ပြန်ထုတ်ပေးပါတယ်။ + +## မတူညီသော နယ်ပယ်များအတွက် ရနိုင်သော pipelines များ + +The `pipeline()` function ဟာ မတူညီတဲ့ နယ်ပယ်များစွာ (multimodal) ကို ထောက်ပံ့ပေးပြီး စာသား၊ ပုံရိပ်၊ အသံနဲ့ multimodal လုပ်ငန်းတာဝန်တွေအထိ လုပ်ဆောင်နိုင်ပါတယ်။ ဒီသင်တန်းမှာတော့ စာသားလုပ်ငန်းတာဝန်တွေကို အဓိကထားမှာဖြစ်ပေမယ့် Transformer architecture ရဲ့ အလားအလာကို နားလည်ထားဖို့က အသုံးဝင်တာကြောင့် အကျဉ်းချုပ် ဖော်ပြပေးပါမယ်။ + +အောက်ဖော်ပြပါတို့ကတော့ ရရှိနိုင်တဲ့ အရာတွေရဲ့ အကျဉ်းချုပ် ဖြစ်ပါတယ်။ + +> [!TIP] +> pipelines တွေရဲ့ ပြည့်စုံပြီး နောက်ဆုံးပေါ် စာရင်းအတွက် [🤗 Transformers documentation](https://huggingface.co/docs/hub/en/models-tasks) ကို ကြည့်ရှုပါ။ + +### စာသား Pipelines (Text pipelines) + +- `text-generation`: prompt တစ်ခုမှ စာသားကို ဖန်တီးခြင်း။ +- `text-classification`: စာသားကို ကြိုတင်သတ်မှတ်ထားသော အမျိုးအစားများအဖြစ် ခွဲခြားခြင်း။ +- `summarization`: အဓိက အချက်အလက်များကို ထိန်းသိမ်းထားရင်း စာသားတစ်ခုကို ပိုမိုတိုတောင်းသော ပုံစံအဖြစ် ပြုလုပ်ခြင်း။ +- `translation`: စာသားတစ်ခုကို ဘာသာစကားတစ်ခုမှ အခြားဘာသာစကားတစ်ခုသို့ ဘာသာပြန်ခြင်း။ +- `zero-shot-classification`: သီးခြား label များပေါ်တွင် ကြိုတင်လေ့ကျင့်မှုမရှိဘဲ စာသားကို အမျိုးအစားခွဲခြားခြင်း။(Classify text without prior training on specific labels) +- `feature-extraction`: စာသားများ၏ vector representations များကို ထုတ်ယူခြင်း။ + +### ပုံရိပ် Pipelines (Image pipelines) + +- `image-to-text`: ပုံရိပ်များ၏ စာသားဖော်ပြချက်များကို ဖန်တီးခြင်း။ +- `image-classification`: ပုံရိပ်တစ်ခုရှိ အရာဝတ္ထုများကို ခွဲခြားသတ်မှတ်ခြင်း။ +- `object-detection`: ပုံရိပ်များရှိ အရာဝတ္ထုများကို နေရာရှာပြီး ခွဲခြားသတ်မှတ်ခြင်း။ + +### အသံ Pipelines (Audio pipelines) + +- `automatic-speech-recognition`: စကားပြောကို စာသားအဖြစ် ပြောင်းလဲခြင်း။ +- `audio-classification`: အသံများကို အမျိုးအစားများအဖြစ် ခွဲခြားခြင်း။ +- `text-to-speech`: စာသားကို ပြောဆိုသောအသံအဖြစ် ပြောင်းလဲခြင်း။ + +### Multimodal pipelines + +- `image-text-to-text`: စာသား prompt တစ်ခုအပေါ် အခြေခံပြီး ပုံရိပ်တစ်ခုကို တုံ့ပြန်ခြင်း။ + +ဒီ pipelines အချို့ကို ပိုမိုအသေးစိတ် လေ့လာကြည့်ရအောင်။ + +## Zero-shot classification[[zero-shot-classification]] + +ကျွန်တော်တို့ စိန်ခေါ်မှု ပိုများတဲ့ လုပ်ငန်းတာဝန်တစ်ခုနဲ့ စတင်ပါမယ်။ အဲဒါကတော့ label မတပ်ရသေးတဲ့ စာသားတွေကို အမျိုးအစားခွဲခြားဖို့ လိုအပ်တဲ့ အလုပ်ပါ။ ဒါဟာ လက်တွေ့ကမ္ဘာ project တွေမှာ အဖြစ်များတဲ့ အခြေအနေတစ်ခုပါ။ ဘာလို့လဲဆိုတော့ စာသားတွေကို label တပ်ဖို့က အချိန်ကုန်လေ့ရှိပြီး နယ်ပယ်ဆိုင်ရာ ကျွမ်းကျင်မှု (domain expertise) လိုအပ်လို့ပါ။ ဒီလိုကိစ္စမျိုးအတွက် `zero-shot-classification` pipeline က အလွန်အစွမ်းထက်ပါတယ်။ ၎င်းက classification အတွက် အသုံးပြုရမယ့် labels တွေကို သတ်မှတ်နိုင်စေတာကြောင့် pretrained model ရဲ့ labels တွေကို အားကိုးစရာ မလိုတော့ပါဘူး။ မော်ဒယ်က စာကြောင်းတစ်ကြောင်းကို positive သို့မဟုတ် negative လို့ ဘယ်လိုခွဲခြားနိုင်လဲဆိုတာ သင်မြင်ပြီးပါပြီ — ဒါပေမယ့် သင်လိုချင်တဲ့ အခြား labels အစုအဝေးနဲ့လည်း စာသားကို ခွဲခြားနိုင်ပါသေးတယ်။ + +```python +from transformers import pipeline + +classifier = pipeline("zero-shot-classification") +classifier( + "This is a course about the Transformers library", + candidate_labels=["education", "politics", "business"], +) +``` + +```python out +{'sequence': 'This is a course about the Transformers library', + 'labels': ['education', 'business', 'politics'], + 'scores': [0.8445963859558105, 0.111976258456707, 0.043427448719739914]} +``` + +ဒီ pipeline ကို _zero-shot_ လို့ခေါ်တာကတော့ သင်ရဲ့ဒေတာပေါ်မှာ မော်ဒယ်ကို fine-tune လုပ်ဖို့ မလိုအပ်ဘဲ အသုံးပြုနိုင်လို့ပါ။ ဒါဟာ သင်လိုချင်တဲ့ labels စာရင်းအတွက် ဖြစ်နိုင်ခြေရမှတ် (probability scores) တွေကို တိုက်ရိုက်ပြန်ပေးနိုင်ပါတယ်။ + +> [!TIP] +> ✏️ **ကိုယ်တိုင် စမ်းကြည့်ပါဦး။** သင်ရဲ့ ကိုယ်ပိုင် sequences နဲ့ labels တွေနဲ့ စမ်းသပ်ပြီး မော်ဒယ် ဘယ်လို အလုပ်လုပ်လဲ ကြည့်ပါ။ + +## စာသား ဖန်တီးခြင်း (Text generation)[[text-generation]] + +အခုတော့ စာသားအချို့ ဖန်တီးဖို့ pipeline ကို ဘယ်လိုအသုံးပြုလဲဆိုတာ ကြည့်ရအောင်။ ဒီနေရာမှာ အဓိကစိတ်ကူးက သင် prompt တစ်ခု ပေးလိုက်ရင် မော်ဒယ်က ကျန်ရှိတဲ့ စာသားကို ဖန်တီးပေးခြင်းဖြင့် auto-complete လုပ်ပေးပါလိမ့်မယ်။ ဒါက ဖုန်းတွေမှာ တွေ့ရတဲ့ predictive text feature နဲ့ ဆင်တူပါတယ်။ စာသားဖန်တီးခြင်းမှာ randomness ပါဝင်တာကြောင့် အောက်မှာ ပြထားတဲ့ ရလဒ်တွေအတိုင်း အတိအကျမရရင်လည်း ပုံမှန်ပါပဲ။ + +```python +from transformers import pipeline + +generator = pipeline("text-generation") +generator("In this course, we will teach you how to") +``` + +```python out +[{'generated_text': 'In this course, we will teach you how to understand and use ' + 'data flow and data interchange when handling user data. We ' + 'will be working with one or more of the most commonly used ' + 'data flows — data flows of various types, as seen by the ' + 'HTTP'}] +``` + +`num_return_sequences` argument နဲ့ ဖန်တီးမယ့် sequence အရေအတွက်ကို ထိန်းချုပ်နိုင်ပြီး `max_length` argument နဲ့ ထွက်ပေါ်လာမယ့် စာသားရဲ့ စုစုပေါင်းအရှည်ကို ထိန်းချုပ်နိုင်ပါတယ်။ + +> [!TIP] +> ✏️ **ကိုယ်တိုင် စမ်းကြည့်ပါဦး။** `num_return_sequences` နဲ့ `max_length` arguments တွေကို အသုံးပြုပြီး စကားလုံး ၁၅ လုံးစီ ပါဝင်တဲ့ စာကြောင်းနှစ်ကြောင်း ဖန်တီးပါ။ + +## Hub မှ မည်သည့်မော်ဒယ်ကိုမဆို pipeline ထဲတွင် အသုံးပြုခြင်း[[using-any-model-from-the-hub-in-a-pipeline]] + +အထက်ပါ ဥပမာတွေမှာတော့ လုပ်ငန်းတာဝန်အတွက် ပုံမှန်မော်ဒယ်ကို အသုံးပြုခဲ့တာဖြစ်ပေမယ့်၊ သီးခြားလုပ်ငန်းတစ်ခု — ဥပမာ စာသားဖန်တီးခြင်းအတွက် Hub ကနေ သီးခြားမော်ဒယ်တစ်ခုကိုလည်း pipeline ထဲမှာ ရွေးချယ်အသုံးပြုနိုင်ပါတယ်။ [Model Hub](https://huggingface.co/models) ကိုသွားပြီး ဘယ်ဘက်ခြမ်းမှာရှိတဲ့ သက်ဆိုင်ရာ tag ကို နှိပ်လိုက်ရင် အဲဒီလုပ်ငန်းအတွက် ထောက်ပံ့ပေးတဲ့ မော်ဒယ်တွေကိုသာ ပြသပေးပါလိမ့်မယ်။ [ဒီစာမျက်နှာ](https://huggingface.co/models?pipeline_tag=text-generation) လို စာမျက်နှာကို သင်ရောက်သွားပါလိမ့်မယ်။ + +[`HuggingFaceTB/SmolLM2-360M`](https://huggingface.co/HuggingFaceTB/SmolLM2-360M) မော်ဒယ်ကို စမ်းကြည့်ရအောင်။ အောက်ပါအတိုင်း အရင်ကအတိုင်း pipeline ထဲမှာ ထည့်သွင်း load လုပ်နိုင်ပါတယ်။ + +```python +from transformers import pipeline + +generator = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M") +generator( + "In this course, we will teach you how to", + max_length=30, + num_return_sequences=2, +) +``` + +```python out +[{'generated_text': 'In this course, we will teach you how to manipulate the world and ' + 'move your mental and physical capabilities to your advantage.'}, + {'generated_text': 'In this course, we will teach you how to become an expert and ' + 'practice realtime, and with a hands on experience on both real ' + 'time and real'}] +``` + +ဘာသာစကား tags တွေကို နှိပ်ပြီး သင်ရဲ့မော်ဒယ်ရှာဖွေမှုကို ပိုမိုတိကျအောင် လုပ်ဆောင်နိုင်ပြီး အခြားဘာသာစကားနဲ့ စာသားဖန်တီးပေးမယ့် မော်ဒယ်တစ်ခုကို ရွေးချယ်နိုင်ပါတယ်။ Model Hub မှာ ဘာသာစကားမျိုးစုံကို ထောက်ပံ့ပေးတဲ့ multilingual model တွေအတွက် checkpoints တွေလည်း ပါဝင်ပါတယ်။ + +မော်ဒယ်တစ်ခုကို နှိပ်ပြီး ရွေးချယ်လိုက်တာနဲ့ ၎င်းကို တိုက်ရိုက် online မှာ စမ်းသပ်နိုင်တဲ့ widget တစ်ခုကို တွေ့ရပါလိမ့်မယ်။ ဒီနည်းနဲ့ မော်ဒယ်ကို download မလုပ်ခင်မှာ ၎င်းရဲ့စွမ်းဆောင်ရည်တွေကို အမြန်စမ်းသပ်နိုင်ပါတယ်။ + +> [!TIP] +> ✏️ ** ကိုယ်တိုင် စမ်းကြည့်ပါဦး။** အခြားဘာသာစကားတစ်ခုအတွက် text generation မော်ဒယ်တစ်ခုကို ရှာဖွေဖို့ filters တွေကို အသုံးပြုပါ။ widget နဲ့ စမ်းသပ်ပြီး pipeline ထဲမှာ အသုံးပြုနိုင်ပါတယ်။ + +### Inference Providers များ[[inference-providers]] + +မော်ဒယ်အားလုံးကို Hugging Face [website](https://huggingface.co/docs/inference-providers/en/index) မှာ ရရှိနိုင်တဲ့ Inference Providers ကို အသုံးပြုပြီး သင့် browser ကနေ တိုက်ရိုက် စမ်းသပ်နိုင်ပါတယ်။ ဒီစာမျက်နှာမှာ စိတ်ကြိုက်စာသားထည့်သွင်းပြီး မော်ဒယ်က input data ကို ဘယ်လိုလုပ်ဆောင်လဲဆိုတာကို ကြည့်ရှုခြင်းဖြင့် မော်ဒယ်နဲ့ တိုက်ရိုက်ကစားနိုင်ပါတယ်။ + +Widget ကို စွမ်းဆောင်ပေးတဲ့ Inference Providers ကိုလည်း ငွေပေးချေရတဲ့ ထုတ်ကုန်အဖြစ် ရရှိနိုင်ပါတယ်။ ဒါက သင်ရဲ့ workflow တွေအတွက် လိုအပ်ရင် အလွန်အသုံးဝင်ပါတယ်။ အသေးစိတ်အတွက်[pricing page](https://huggingface.co/docs/inference-providers/en/pricing) ကို ကြည့်ပါ။ + +## Mask filling[[mask-filling]] + +သင်စမ်းသပ်ရမယ့် နောက်ထပ် pipeline ကတော့ `fill-mask` ဖြစ်ပါတယ်။ ဒီလုပ်ငန်းတာဝန်ရဲ့ စိတ်ကူးက ပေးထားတဲ့ စာသားတစ်ခုမှာရှိတဲ့ ကွက်လပ်တွေကို ဖြည့်ဆည်းပေးဖို့ပါပဲ။ + +```python +from transformers import pipeline + +unmasker = pipeline("fill-mask") +unmasker("This course will teach you all about models.", top_k=2) +``` + +```python out +[{'sequence': 'This course will teach you all about mathematical models.', + 'score': 0.19619831442832947, + 'token': 30412, + 'token_str': ' mathematical'}, + {'sequence': 'This course will teach you all about computational models.', + 'score': 0.04052725434303284, + 'token': 38163, + 'token_str': ' computational'}] +``` + +`top_k` argument က သင်ပြသ၊လိုတဲ့ ဖြစ်နိုင်ခြေ အရေအတွက်ကို ထိန်းချုပ်ပါတယ်။ ဒီနေရာမှာ မော်ဒယ်က `` ဆိုတဲ့ အထူးစကားလုံးကို ဖြည့်ပေးတာကို သတိပြုပါ။ ဒါကို *mask token* လို့ မကြာခဏ ရည်ညွှန်းလေ့ရှိပါတယ်။ အခြား mask-filling မော်ဒယ်တွေမှာ မတူညီတဲ့ mask token တွေ ရှိနိုင်တာကြောင့် အခြားမော်ဒယ်တွေကို လေ့လာတဲ့အခါ မှန်ကန်တဲ့ mask word ကို အမြဲစစ်ဆေးတာ ကောင်းပါတယ်။ စစ်ဆေးဖို့ နည်းလမ်းတစ်ခုကတော့ widget မှာ အသုံးပြုထားတဲ့ mask word ကို ကြည့်တာပါပဲ။ + +> [!TIP] +> ✏️ **ကိုယ်တိုင် စမ်းကြည့်ပါဦး။** Hub ပေါ်မှာ `bert-base-cased` မော်ဒယ်ကို ရှာဖွေပြီး Inference API widget မှာ ၎င်းရဲ့ mask word ကို ခွဲခြားသတ်မှတ်ပါ။ ကျွန်တော်တို့ရဲ့ `pipeline` ဥပမာမှာပါတဲ့ စာကြောင်းအတွက် ဒီမော်ဒယ်က ဘာကို ခန့်မှန်းပေးမလဲ။ + +## Named entity recognition[[named-entity-recognition]] + +Named Entity Recognition (NER) ဆိုတာက input text ထဲက ဘယ်အပိုင်းတွေက လူ၊ နေရာ ဒါမှမဟုတ် အဖွဲ့အစည်းလို entities တွေနဲ့ ကိုက်ညီလဲဆိုတာကို မော်ဒယ်က ရှာဖွေရတဲ့ လုပ်ငန်းတာဝန်တစ်ခု ဖြစ်ပါတယ်။ ဥပမာတစ်ခုကို ကြည့်ရအောင်။ + +```python +from transformers import pipeline + +ner = pipeline("ner", grouped_entities=True) +ner("My name is Sylvain and I work at Hugging Face in Brooklyn.") +``` + +```python out +[{'entity_group': 'PER', 'score': 0.99816, 'word': 'Sylvain', 'start': 11, 'end': 18}, + {'entity_group': 'ORG', 'score': 0.97960, 'word': 'Hugging Face', 'start': 33, 'end': 45}, + {'entity_group': 'LOC', 'score': 0.99321, 'word': 'Brooklyn', 'start': 49, 'end': 57} +] +``` + +ဒီနေရာမှာ မော်ဒယ်က Sylvain ဟာ လူ (PER) ဖြစ်ကြောင်း၊ Hugging Face က အဖွဲ့အစည်း (ORG) ဖြစ်ကြောင်း၊ Brooklyn က နေရာ (LOC) ဖြစ်ကြောင်း မှန်ကန်စွာ ဖော်ထုတ်ခဲ့ပါတယ်။ + +ကျွန်တော်တို့ `grouped_entities=True option` ကို pipeline ဖန်တီးတဲ့ function မှာ ပေးလိုက်တာက စာကြောင်းရဲ့ အစိတ်အပိုင်းတွေကို တူညီတဲ့ entity နဲ့ ကိုက်ညီတဲ့ အစိတ်အပိုင်းတွေကို အတူတကွ ပြန်လည်စုစည်းဖို့ pipeline ကို ပြောတာပါ။ ဒီနေရာမှာ မော်ဒယ်က "Hugging" နဲ့ "Face" ကို စကားလုံးများစွာနဲ့ ဖွဲ့စည်းထားတဲ့ နာမည်ဖြစ်ပေမယ့် တစ်ခုတည်းသော အဖွဲ့အစည်းအဖြစ် မှန်ကန်စွာ စုစည်းခဲ့ပါတယ်။ တကယ်တော့ နောက်အခန်းမှာ ကျွန်တော်တို့ မြင်ရမှာဖြစ်သလို preprocessing က စကားလုံးအချို့ကို ပိုမိုသေးငယ်တဲ့ အစိတ်အပိုင်းတွေအဖြစ် ခွဲထုတ်တာတောင် လုပ်ပါတယ်။ ဥပမာ၊ `Sylvain` ကို `S`၊ `##yl`၊ `##va`၊ `##in` ဆိုပြီး လေးပိုင်းခွဲပါတယ်။ post-processing အဆင့်မှာတော့ pipeline က အဲဒီအပိုင်းတွေကို အောင်မြင်စွာ ပြန်လည်စုစည်းပေးပါတယ်။ + +> [!TIP] +> ✏️ **ကိုယ်တိုင် စမ်းကြည့်ပါဦး။** Model Hub မှာ အင်္ဂလိပ်ဘာသာစကားမှာ part-of-speech tagging (အတိုကောက်အားဖြင့် POS) လုပ်ဆောင်နိုင်တဲ့ မော်ဒယ်တစ်ခုကို ရှာဖွေပါ။ အထက်ပါ ဥပမာမှာပါတဲ့ စာကြောင်းအတွက် ဒီမော်ဒယ်က ဘာကို ခန့်မှန်းပေးမလဲ။ + +## မေးခွန်းဖြေဆိုခြင်း (Question answering)[[question-answering]] + +`question-answering` pipeline ကတော့ ပေးထားတဲ့ အကြောင်းအရာတစ်ခုကနေ အချက်အလက်တွေကို အသုံးပြုပြီး မေးခွန်းတွေကို ဖြေပေးပါတယ်။ + +```python +from transformers import pipeline + +question_answerer = pipeline("question-answering") +question_answerer( + question="Where do I work?", + context="My name is Sylvain and I work at Hugging Face in Brooklyn", +) +``` + +```python out +{'score': 0.6385916471481323, 'start': 33, 'end': 45, 'answer': 'Hugging Face'} +``` + +ဒီ pipeline က ပေးထားတဲ့ context ကနေ အချက်အလက်တွေကို ထုတ်ယူပြီး အလုပ်လုပ်တာကို သတိပြုပါ။ ၎င်းက အဖြေကို ဖန်တီးပေးတာ မဟုတ်ပါဘူး။ + +## အနှစ်ချုပ်ခြင်း(Summarization)[[summarization]] + +Summarization ဆိုတာက စာသားတစ်ခုကို ပိုမိုတိုတောင်းတဲ့ စာသားတစ်ခုအဖြစ် လျှော့ချပြီး စာသားမှာ ရည်ညွှန်းထားတဲ့ အရေးကြီးတဲ့ အချက်အလက်အားလုံး (သို့မဟုတ် အများစု) ကို ထိန်းသိမ်းထားတဲ့ လုပ်ငန်းတာဝန် ဖြစ်ပါတယ်။ ဥပမာတစ်ခုကို ကြည့်ရအောင်။ + +```python +from transformers import pipeline + +summarizer = pipeline("summarization") +summarizer( + """ + America has changed dramatically during recent years. Not only has the number of + graduates in traditional engineering disciplines such as mechanical, civil, + electrical, chemical, and aeronautical engineering declined, but in most of + the premier American universities engineering curricula now concentrate on + and encourage largely the study of engineering science. As a result, there + are declining offerings in engineering subjects dealing with infrastructure, + the environment, and related issues, and greater concentration on high + technology subjects, largely supporting increasingly complex scientific + developments. While the latter is important, it should not be at the expense + of more traditional engineering. + + Rapidly developing economies such as China and India, as well as other + industrial countries in Europe and Asia, continue to encourage and advance + the teaching of engineering. Both China and India, respectively, graduate + six and eight times as many traditional engineers as does the United States. + Other industrial countries at minimum maintain their output, while America + suffers an increasingly serious decline in the number of engineering graduates + and a lack of well-educated engineers. +""" +) +``` + +```python out +[{'summary_text': ' America has changed dramatically during recent years . The ' + 'number of engineering graduates in the U.S. has declined in ' + 'traditional engineering disciplines such as mechanical, civil ' + ', electrical, chemical, and aeronautical engineering . Rapidly ' + 'developing economies such as China and India, as well as other ' + 'industrial countries in Europe and Asia, continue to encourage ' + 'and advance engineering .'}] +``` + +Text generation နဲ့ အလားတူပဲ၊ ရလဒ်အတွက် `max_length` သို့မဟုတ် `min_length` ကို သတ်မှတ်နိုင်ပါတယ်။ + +## ဘာသာပြန်ခြင်း (Translation) [[translation]] + +ဘာသာပြန်ခြင်းအတွက်တော့ လုပ်ငန်းတာဝန်အမည်မှာ ဘာသာစကားတွဲ (ဥပမာ- `"translation_en_to_fr"`) ကို ပေးလိုက်ရင် default မော်ဒယ်ကို အသုံးပြုနိုင်ပါတယ်။ ဒါပေမယ့် အလွယ်ကူဆုံးနည်းလမ်းကတော့ [Model Hub](https://huggingface.co/models) ကနေ သင်အသုံးပြုလိုတဲ့ မော်ဒယ်ကို ရွေးချယ်တာပါပဲ။ ဒီနေရာမှာတော့ ပြင်သစ်ဘာသာကနေ အင်္ဂလိပ်ဘာသာကို ဘာသာပြန်ကြည့်ပါမယ်။ + +```python +from transformers import pipeline + +translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en") +translator("Ce cours est produit par Hugging Face.") +``` + +```python out +[{'translation_text': 'This course is produced by Hugging Face.'}] +``` + +Text generation နဲ့ summarization တွေလိုပဲ ရလဒ်အတွက် `max_length` သို့မဟုတ် `min_length` ကို သတ်မှတ်နိုင်ပါတယ်။ + +> [!TIP] +> ✏️ **ကိုယ်တိုင် စမ်းကြည့်ပါဦး။** အခြားဘာသာစကားတွေမှာ ဘာသာပြန်မော်ဒယ်တွေကို ရှာဖွေပြီး အရင်စာကြောင်းကို မတူညီတဲ့ ဘာသာစကားအချို့ဆီ ဘာသာပြန်ကြည့်ပါ။ + +## ပုံရိပ်နှင့် အသံ Pipelines များ (Image and audio pipelines) + +စာသားအပြင် Transformer မော်ဒယ်တွေဟာ ပုံရိပ်နဲ့ အသံတွေနဲ့လည်း အလုပ်လုပ်နိုင်ပါတယ်။ ဥပမာအချို့ကတော့- + +### Image classification + +```python +from transformers import pipeline + +image_classifier = pipeline( + task="image-classification", model="google/vit-base-patch16-224" +) +result = image_classifier( + "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +) +print(result) +``` + +```python out +[{'label': 'lynx, catamount', 'score': 0.43350091576576233}, + {'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor', + 'score': 0.034796204417943954}, + {'label': 'snow leopard, ounce, Panthera uncia', + 'score': 0.03240183740854263}, + {'label': 'Egyptian cat', 'score': 0.02394474856555462}, + {'label': 'tiger cat', 'score': 0.02288915030658245}] +``` + +### Automatic speech recognition + +```python +from transformers import pipeline + +transcriber = pipeline( + task="automatic-speech-recognition", model="openai/whisper-large-v3" +) +result = transcriber( + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac" +) +print(result) +``` + +```python out +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + +## ဒေတာ အရင်းအမြစ်မျိုးစုံမှ ဒေတာများကို ပေါင်းစပ်ခြင်း (Combining data from multiple sources) + +Transformer မော်ဒယ်တွေရဲ့ အစွမ်းထက်တဲ့ အသုံးချမှုတစ်ခုကတော့ ဒေတာအရင်းအမြစ်မျိုးစုံကနေ ဒေတာတွေကို ပေါင်းစပ်ပြီး လုပ်ဆောင်နိုင်စွမ်းပါပဲ။ ဒါက အောက်ပါကိစ္စတွေအတွက် အထူးအသုံးဝင်ပါတယ်။ + +1. ဒေတာဘေ့စ်(databases)များစွာ သို့မဟုတ် repository များစွာကို ရှာဖွေခြင်း။ +2. တူညီတဲ့ format တွေ (စာသား၊ ပုံရိပ်၊ အသံ) ကနေ အချက်အလက်တွေကို စုစည်းခြင်း။ +3. ဆက်စပ်အချက်အလက်တွေရဲ့ ပေါင်းစပ်ထားသော မြင်ကွင်းတစ်ခုကို ဖန်တီးခြင်း။ + + + +ဥပမာအားဖြင့်၊ သင်ဟာ အောက်ပါတို့ကို လုပ်ဆောင်နိုင်တဲ့ စနစ်တစ်ခုကို တည်ဆောက်နိုင်ပါတယ်။ +- စာသားနဲ့ ပုံရိပ်လို နယ်ပယ်အမျိုးစုံက ဒေတာဘေ့စ်တွေမှာ အချက်အလက်တွေကို ရှာဖွေခြင်း။ +- မတူညီတဲ့ အရင်းအမြစ်တွေကနေ ရရှိတဲ့ ရလဒ်တွေကို တစ်ခုတည်းသော တုံ့ပြန်မှုအဖြစ် ပေါင်းစပ်ခြင်း။ ဥပမာ- အသံဖိုင်တစ်ခုနဲ့ စာသားဖော်ပြချက်တစ်ခုကနေ။ +- document တွေနဲ့ metadata တွေရဲ့ ဒေတာဘေ့စ်တစ်ခုကနေ အသက်ဆိုင်ဆုံး အချက်အလက်တွေကို တင်ပြခြင်း။ + +## နိဂုံးချုပ် + +ဒီအခန်းမှာ ပြသထားတဲ့ pipelines တွေကတော့ အများစုက သရုပ်ပြရန်အတွက်သာ ဖြစ်ပါတယ်။ ၎င်းတို့ကို သီးခြားလုပ်ငန်းတာဝန်များအတွက် ရေးဆွဲထားတာဖြစ်ပြီး ၎င်းတို့ရဲ့ မူကွဲတွေကိုတော့ လုပ်ဆောင်နိုင်ခြင်း မရှိပါဘူး။ နောက်အခန်းမှာတော့ `pipeline()` function ထဲမှာ ဘာတွေပါလဲ၊ ၎င်းရဲ့ လုပ်ဆောင်ပုံကို ဘယ်လိုစိတ်ကြိုက် ပြုပြင်နိုင်လဲဆိုတာကို သင်လေ့လာရမှာ ဖြစ်ပါတယ်။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Transformer Models**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **🤗 Transformers library**: Hugging Face က ထုတ်လုပ်ထားတဲ့ library တစ်ခုဖြစ်ပြီး Transformer မော်ဒယ်တွေကို အသုံးပြုပြီး Natural Language Processing (NLP), computer vision, audio processing စတဲ့ နယ်ပယ်တွေမှာ အဆင့်မြင့် AI မော်ဒယ်တွေကို တည်ဆောက်ပြီး အသုံးပြုနိုင်စေပါတယ်။ +* **`pipeline()` function**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ လုပ်ဆောင်ချက်တစ်ခုဖြစ်ပြီး မော်ဒယ်တွေကို သီးခြားလုပ်ငန်းတာဝန်များ (ဥပမာ- စာသားခွဲခြားသတ်မှတ်ခြင်း၊ စာသားထုတ်လုပ်ခြင်း) အတွက် အသုံးပြုရလွယ်ကူအောင် ပြုလုပ်ပေးပါတယ်။ +* **Natural Language Processing (NLP)**: ကွန်ပျူတာတွေ လူသားဘာသာစကားကို နားလည်၊ အဓိပ္ပာယ်ဖော်ပြီး၊ ဖန်တီးနိုင်အောင် လုပ်ဆောင်ပေးတဲ့ Artificial Intelligence (AI) ရဲ့ နယ်ပယ်ခွဲတစ်ခု ဖြစ်ပါတယ်။ ဥပမာအားဖြင့် စာသားခွဲခြမ်းစိတ်ဖြာခြင်း၊ ဘာသာပြန်ခြင်း စသည်တို့ ပါဝင်ပါတယ်။ +* **Computer Vision**: ကွန်ပျူတာများကို ပုံရိပ်များ၊ ဗီဒီယိုများကို လူသားများကဲ့သို့ မြင်၊ နားလည်နိုင်အောင် သင်ကြားပေးသည့် Artificial Intelligence (AI) နယ်ပယ်။ +* **Audio Processing**: အသံအချက်အလက်များကို ခွဲခြမ်းစိတ်ဖြာခြင်း၊ ပြောင်းလဲခြင်း သို့မဟုတ် ဖန်တီးခြင်း လုပ်ငန်းများ။ +* **Model Hub**: Hugging Face က ထုတ်လုပ်ထားတဲ့ အွန်လိုင်း platform တစ်ခုဖြစ်ပြီး AI မော်ဒယ်တွေ၊ datasets တွေနဲ့ demo တွေကို အခြားသူတွေနဲ့ မျှဝေဖို့၊ ရှာဖွေဖို့နဲ့ ပြန်လည်အသုံးပြုဖို့အတွက် ဖြစ်ပါတယ်။ +* **Pretrained Model**: ကြီးမားသော ဒေတာအစုအဝေးများဖြင့် ကြိုတင်လေ့ကျင့်ထားသော Machine Learning မော်ဒယ်။ ၎င်းတို့ကို အခြားလုပ်ငန်းများအတွက် fine-tune ပြန်လုပ်နိုင်ပါတယ်။ +* **Fine-tuned**: ကြိုတင်လေ့ကျင့်ထားပြီးသား (pre-trained) မော်ဒယ်တစ်ခုကို သီးခြားလုပ်ငန်းတစ်ခု (specific task) အတွက် အနည်းငယ်သော ဒေတာနဲ့ ထပ်မံလေ့ကျင့်ပေးခြင်းကို ဆိုလိုပါတယ်။ +* **Sentiment Analysis**: စာသားတစ်ခုရဲ့ စိတ်ခံစားမှု (အပြုသဘော၊ အနုတ်သဘော၊ ကြားနေ) ကို ခွဲခြမ်းစိတ်ဖြာခြင်း။ +* **Preprocessing**: ဒေတာများကို Machine Learning မော်ဒယ်တစ်ခုက နားလည်နိုင်သော ပုံစံသို့ ပြောင်းလဲရန် ပြင်ဆင်ခြင်းလုပ်ငန်းစဉ်။ +* **Postprocessing**: Machine Learning မော်ဒယ်၏ ရလဒ်များကို လူသားများ နားလည်လွယ်သော ပုံစံသို့ ပြန်လည်ပြောင်းလဲခြင်းလုပ်ငန်းစဉ်။ +* **Multimodal**: မတူညီသော ဒေတာအမျိုးအစားများ (ဥပမာ- စာသား၊ ပုံရိပ်၊ အသံ) ကို အသုံးပြုနိုင်စွမ်းရှိခြင်း။ +* **Text Generation**: AI မော်ဒယ်များကို အသုံးပြု၍ လူသားကဲ့သို့သော စာသားအသစ်များ ဖန်တီးခြင်း။ +* **Text Classification**: စာသားတစ်ခုကို ကြိုတင်သတ်မှတ်ထားသော အမျိုးအစားများအဖြစ် ခွဲခြားခြင်း။ +* **Summarization**: အဓိက အချက်အလက်များကို ထိန်းသိမ်းထားရင်း စာသားတစ်ခုကို ပိုမိုတိုတောင်းသော ပုံစံအဖြစ် ပြုလုပ်ခြင်း။ +* **Translation**: စာသားတစ်ခုကို ဘာသာစကားတစ်ခုမှ အခြားဘာသာစကားတစ်ခုသို့ ဘာသာပြန်ခြင်း။ +* **Zero-shot Classification**: သီးခြား label များပေါ်တွင် ကြိုတင်လေ့ကျင့်မှုမရှိဘဲ စာသားကို အမျိုးအစားခွဲခြားခြင်း။ +* **Feature Extraction**: ဒေတာများမှ အရေးကြီးသော လက္ခဏာရပ်များကို ထုတ်ယူခြင်း။ +* **Image to Text**: ပုံရိပ်တစ်ခုမှ စာသားဖော်ပြချက်များကို ဖန်တီးခြင်း။ +* **Image Classification**: ပုံရိပ်တစ်ခုရှိ အရာဝတ္ထုများကို ခွဲခြားသတ်မှတ်ခြင်း။ +* **Object Detection**: ပုံရိပ်များရှိ အရာဝတ္ထုများကို နေရာရှာပြီး ခွဲခြားသတ်မှတ်ခြင်း။ +* **Automatic Speech Recognition**: ပြောဆိုသော ဘာသာစကားကို ကွန်ပျူတာက စာသားအဖြစ် ပြောင်းလဲနားလည်နိုင်သည့် နည်းပညာ။ +* **Audio Classification**: အသံများကို အမျိုးအစားများအဖြစ် ခွဲခြားခြင်း။ +* **Text to Speech**: စာသားကို ပြောဆိုသောအသံအဖြစ် ပြောင်းလဲခြင်း။ +* **Image-Text to Text**: ပုံရိပ်တစ်ခုနဲ့ ပတ်သက်ပြီး စာသား prompt တစ်ခုအပေါ် အခြေခံပြီး စာသားတုံ့ပြန်မှု ဖန်တီးခြင်း။ +* **Label**: ဒေတာအချက်အလက်တစ်ခုကို ဖော်ပြရန် အသုံးပြုသော အမည် သို့မဟုတ် အမျိုးအစား။ +* **Domain Expertise**: သီးခြားနယ်ပယ်တစ်ခု သို့မဟုတ် ဘာသာရပ်တစ်ခုတွင် နက်ရှိုင်းသော အသိပညာ သို့မဟုတ် ကျွမ်းကျင်မှု။ +* **Prompt**: Large Language Models (LLMs) ကို တိကျသောလုပ်ငန်းတစ်ခု လုပ်ဆောင်ရန် သို့မဟုတ် အချက်အလက်ပေးရန်အတွက် ပေးပို့သော input text သို့မဟုတ် မေးခွန်း။ +* **Randomness**: ကြိုတင်ခန့်မှန်းနိုင်ခြင်းမရှိသော သို့မဟုတ် အစီအစဉ်မရှိသော အခြေအနေများ။ +* **`num_return_sequences`**: text generation pipeline တွင် ဖန်တီးမည့် output sequence အရေအတွက်ကို ထိန်းချုပ်ရန် အသုံးပြုသော argument။ +* **`max_length`**: text generation pipeline တွင် ထွက်ပေါ်လာမည့် output text ၏ အရှည်ဆုံး ဖြစ်နိုင်သော အရှည်ကို သတ်မှတ်ရန် အသုံးပြုသော argument။ +* **Inference Providers**: Hugging Face Hub ပေါ်ရှိ မော်ဒယ်များကို cloud-based API မှတစ်ဆင့် အသုံးပြုနိုင်စေသော ဝန်ဆောင်မှု။ +* **Mask Token**: `fill-mask` လုပ်ငန်းတာဝန်များတွင် စာသားထဲက ကွက်လပ်တစ်ခုကို ကိုယ်စားပြုသော အထူး token။ +* **Named Entity Recognition (NER)**: စာသားတစ်ခုထဲက လူအမည်၊ နေရာအမည်၊ အဖွဲ့အစည်းအမည် စတဲ့ သီးခြားအမည်တွေကို ရှာဖွေဖော်ထုတ်ခြင်း။ +* **Entities**: အချက်အလက်များ သို့မဟုတ် အရာဝတ္ထုများ (ဥပမာ- လူ၊ နေရာ၊ အဖွဲ့အစည်း)။ +* **`grouped_entities`**: NER pipeline တွင် အတူတူရှိသော entity အပိုင်းအစများကို တစ်ခုတည်းအဖြစ် စုစည်းရန် အသုံးပြုသော option။ +* **Part-of-speech Tagging (POS)**: စာကြောင်းတစ်ခုရှိ စကားလုံးတစ်လုံးစီ၏ သဒ္ဒါဆိုင်ရာ အစိတ်အပိုင်း (ဥပမာ- နာမ်၊ ကြိယာ၊ နာမဝိသေသန) ကို ခွဲခြားသတ်မှတ်ခြင်း။ +* **Question Answering**: မေးခွန်းတစ်ခုနဲ့ ပေးထားတဲ့ အကြောင်းအရာတစ်ခုကနေ အချက်အလက်တွေကို ထုတ်ယူပြီး အဖြေရှာတဲ့ လုပ်ငန်းတာဝန်။ +* **`min_length`**: text generation သို့မဟုတ် summarization pipeline တွင် ထွက်ပေါ်လာမည့် output text ၏ အတိုဆုံး ဖြစ်နိုင်သော အရှည်ကို သတ်မှတ်ရန် အသုံးပြုသော argument။ +* **Multilingual Models**: ဘာသာစကားများစွာကို နားလည်ပြီး လုပ်ဆောင်နိုင်သော မော်ဒယ်များ။ diff --git a/chapters/my/chapter1/4.mdx b/chapters/my/chapter1/4.mdx new file mode 100644 index 000000000..43e613876 --- /dev/null +++ b/chapters/my/chapter1/4.mdx @@ -0,0 +1,255 @@ +# Transformers တွေက ဘယ်လိုအလုပ်လုပ်လဲ။[[how-do-transformers-work]] + + + +ဒီအပိုင်းမှာတော့ Transformer မော်ဒယ်တွေရဲ့ architecture ကို ကြည့်ရှုမှာဖြစ်ပြီး attention, encoder-decoder architecture နဲ့ အခြားအကြောင်းအရာတွေကို ပိုမိုနက်ရှိုင်းစွာ လေ့လာသွားမှာ ဖြစ်ပါတယ်။ + +> [!WARNING] +> 🚀 ဒီမှာ အရှိန်မြှင့်လိုက်ပါပြီ။ ဒီအပိုင်းက အသေးစိတ်ပြီး နည်းပညာပိုင်းဆိုင်ရာ အချက်အလက်များပါတယ်။ ဒါကြောင့် ချက်ချင်း နားမလည်ရင် စိတ်မပူပါနဲ့။ ဒီအယူအဆတွေကို သင်တန်းရဲ့ နောက်ပိုင်းမှာ ပြန်ပြီး ပြောပြပေးပါမယ်။ + +## Transformer သမိုင်း အနည်းငယ်[[a-bit-of-transformer-history]] + +Transformer မော်ဒယ်တွေရဲ့ (တိုတောင်းတဲ့) သမိုင်းမှာ အရေးပါတဲ့ အချက်အချို့ကို ဒီမှာ ဖော်ပြထားပါတယ်။ + +
+A brief chronology of Transformers models. + +
+ +[Transformer architecture](https://arxiv.org/abs/1706.03762) ကို ၂၀၁၇ ခုနှစ်၊ ဇွန်လမှာ စတင်မိတ်ဆက်ခဲ့ပါတယ်။ မူလသုတေသနရဲ့ အဓိကအာရုံကတော့ ဘာသာပြန်ခြင်း လုပ်ငန်းတွေ ဖြစ်ပါတယ်။ ထို့နောက်မှာတော့ သြဇာကြီးမားတဲ့ မော်ဒယ်များစွာကို မိတ်ဆက်ခဲ့ပါတယ်။ ၎င်းတို့မှာ- + +- **၂၀၁၈ ခုနှစ်၊ ဇွန်လ**: [GPT](https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf)၊ ပထမဆုံးသော pretrained Transformer model ဖြစ်ပြီး NLP လုပ်ငန်းမျိုးစုံအတွက် fine-tuning လုပ်ရာမှာ အသုံးပြုခဲ့ကာ state-of-the-art ရလဒ်များကို ရရှိခဲ့ပါတယ်။ + +- **၂၀၁၈ ခုနှစ်၊ အောက်တိုဘာလ**: [BERT](https://arxiv.org/abs/1810.04805)၊ နောက်ထပ်ကြီးမားတဲ့ pretrained model တစ်ခုဖြစ်ပြီး စာကြောင်းတွေရဲ့ အနှစ်ချုပ်တွေကို ပိုမိုကောင်းမွန်အောင် ထုတ်လုပ်ဖို့ ဒီဇိုင်းထုတ်ထားပါတယ်။ (နောက်အခန်းမှာ အသေးစိတ်ပြောပါမယ်!) + +- **၂၀၁၉ ခုနှစ်၊ ဖေဖော်ဝါရီလ**: [GPT-2](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf)၊ GPT ရဲ့ ပိုမိုကောင်းမွန်ပြီး (ပိုကြီးတဲ့) ဗားရှင်းတစ်ခုဖြစ်ပြီး ကျင့်ဝတ်ဆိုင်ရာ စိုးရိမ်ပူပန်မှုတွေကြောင့် ချက်ချင်းထုတ်ပြန်ခြင်း မရှိခဲ့ပါဘူး။ + +- **၂၀၁၉ ခုနှစ်၊ အောက်တိုဘာလ**: [T5](https://huggingface.co/papers/1910.10683)၊ sequence-to-sequence Transformer architecture ကို အခြေခံပြီး multi-task focused လုပ်ဆောင်နိုင်သော မော်ဒယ်တစ်ခု။ + +- **၂၀၂၀ ခုနှစ်၊ မေလ**: [GPT-3](https://huggingface.co/papers/2005.14165)၊ GPT-2 ထက် ပိုမိုကြီးမားသော ဗားရှင်းဖြစ်ပြီး fine-tuning လုပ်ရန်မလိုဘဲ လုပ်ငန်းမျိုးစုံကို ကောင်းစွာလုပ်ဆောင်နိုင်ပါတယ်။ (၎င်းကို _zero-shot learning_ ဟု ခေါ်ပါတယ်။) + +- **၂၀၂၂ ခုနှစ်၊ ဇန်နဝါရီလ**: [InstructGPT](https://huggingface.co/papers/2203.02155)၊ ညွှန်ကြားချက်များကို ပိုမိုကောင်းမွန်စွာ လိုက်နာနိုင်ရန် လေ့ကျင့်ပေးထားသော GPT-3 ၏ ဗားရှင်းတစ်ခု။ + +ဒီစာရင်းက ပြည့်စုံလွန်းတာ မဟုတ်ပါဘူး။ Transformer မော်ဒယ်အမျိုးအစားအချို့ကိုသာ မီးမောင်းထိုးပြထားတာ ဖြစ်ပါတယ်။ ယေဘုယျအားဖြင့် ၎င်းတို့ကို အမျိုးအစား သုံးမျိုး ခွဲခြားနိုင်ပါတယ်။ + +- **၂၀၂၃ ခုနှစ်၊ ဇန်နဝါရီလ**: [Llama](https://huggingface.co/papers/2302.13971)၊ ဘာသာစကားမျိုးစုံဖြင့် စာသားများစွာကို ဖန်တီးနိုင်သော Large Language Model တစ်ခု။ + +- **၂၀၂၃ ခုနှစ်၊ မတ်လ**: [Mistral](https://huggingface.co/papers/2310.06825)၊ parameter ၇ ဘီလီယံပါရှိသော language model ဖြစ်ပြီး အကဲဖြတ်ထားသော benchmark အားလုံးတွင် Llama 2 13B ကို သာလွန်သည်။ ၎င်းသည် ပိုမိုမြန်ဆန်သော inference အတွက် grouped-query attention နှင့် စိတ်ကြိုက်အလျားရှိသော sequences များကို ကိုင်တွယ်ရန် sliding window attention ကို အသုံးပြုသည်။ + +- **၂၀၂၄ ခုနှစ်၊ မေလ**: [Gemma 2](https://huggingface.co/papers/2408.00118)၊ 2B မှ 27B parameters အထိ ရှိသော ပေါ့ပါးသော၊ state-of-the-art open models မိသားစုတစ်ခုဖြစ်ပြီး interleaved local-global attentions နှင့် group-query attention တို့ကို ပေါင်းစပ်ထားသည်။ ပိုမိုသေးငယ်သော မော်ဒယ်များကို knowledge distillation အသုံးပြု၍ လေ့ကျင့်ထားပြီး ၎င်းတို့ထက် ၂-၃ ဆ ပိုကြီးသော မော်ဒယ်များနှင့် ယှဉ်ပြိုင်နိုင်သော စွမ်းဆောင်ရည်ကို ပေးသည်။ + +- **၂၀၂၄ ခုနှစ်၊ နိုဝင်ဘာလ**: [SmolLM2](https://huggingface.co/papers/2502.02737)၊ state-of-the-art small language model (135 million မှ 1.7 billion parameters) ဖြစ်ပြီး ၎င်း၏ ကျစ်လျစ်သော အရွယ်အစားရှိသော်လည်း ထူးခြားသော စွမ်းဆောင်ရည်ကို ရရှိစေပြီး mobile နှင့် edge devices များအတွက် ဖြစ်နိုင်ခြေအသစ်များကို ဖွင့်ပေးသည်။ + +- GPT-ကဲ့သို့သော မော်ဒယ်များ (_auto-regressive_ Transformer models လို့လည်းခေါ်ကြပါတယ်) +- BERT-ကဲ့သို့သော မော်ဒယ်များ(_auto-encoding_ Transformer models လို့လည်းခေါ်ကြပါတယ်) +- T5-ကဲ့သို့သော မော်ဒယ်များ (_sequence-to-sequence_ Transformer models လို့လည်းခေါ်ကြပါတယ်) + +ဒီအမျိုးအစားတွေကို နောက်ပိုင်းမှာ ပိုမိုနက်နဲစွာ လေ့လာသွားမှာ ဖြစ်ပါတယ်။ + +## Transformers တွေဟာ language models တွေ ဖြစ်ပါတယ်။ [[transformers-are-language-models]] + +အထက်မှာ ဖော်ပြခဲ့တဲ့ Transformer မော်ဒယ်အားလုံး (GPT, BERT, T5, စသည်ဖြင့်) ကို *language models* အဖြစ် လေ့ကျင့်ထားပါတယ်။ ဒါကတော့ ၎င်းတို့ကို များပြားလှတဲ့ ကုန်ကြမ်းစာသား(raw texts)တွေပေါ်မှာ self-supervised ပုံစံနဲ့ လေ့ကျင့်ထားတယ်လို့ ဆိုလိုပါတယ်။ + +Self-supervised learning ဆိုတာက မော်ဒယ်ရဲ့ input တွေကနေ ရည်ရွယ်ချက်ကို အလိုအလျောက် တွက်ချက်ပေးတဲ့ သင်ယူမှုပုံစံတစ်ခုပါ။ ဒါကြောင့် ဒေတာတွေကို လူသားတွေက label လုပ်ပေးဖို့ မလိုအပ်ပါဘူး။ + +ဒီလို မော်ဒယ်မျိုးက သူ လေ့ကျင့်ထားတဲ့ ဘာသာစကားရဲ့ စာရင်းအင်းဆိုင်ရာ နားလည်မှုကို တည်ဆောက်ပေမယ့်၊ သီးခြားလက်တွေ့လုပ်ငန်းတာဝန်တွေအတွက်တော့ အသုံးဝင်မှု နည်းပါးပါတယ်။ ဒါကြောင့် ယေဘုယျ pretrained မော်ဒယ်ကို *transfer learning* ဒါမှမဟုတ် *fine-tuning* လို့ခေါ်တဲ့ လုပ်ငန်းစဉ်တစ်ခုကို လုပ်ဆောင်ပါတယ်။ ဒီလုပ်ငန်းစဉ်အတွင်းမှာ မော်ဒယ်ကို -- လူသားတွေက annotated လုပ်ထားတဲ့ labels တွေ အသုံးပြုပြီး -- ပေးထားတဲ့ လုပ်ငန်းတာဝန်တစ်ခုပေါ်မှာ supervised ပုံစံနဲ့ fine-tune လုပ်ပါတယ်။ + +လုပ်ငန်းတာဝန်တစ်ခုရဲ့ ဥပမာတစ်ခုကတော့ ယခင် *n* စကားလုံးတွေကို ဖတ်ပြီးနောက် စာကြောင်းတစ်ခုရဲ့ နောက်စကားလုံးကို ခန့်မှန်းခြင်းပါပဲ။ ဒါကို *causal language modeling* လို့ ခေါ်ပါတယ်။ ဘာဖြစ်လို့လဲဆိုတော့ output က အတိတ်နဲ့ ပစ္စုပ္ပန် input တွေပေါ် မူတည်ပေမယ့် အနာဂတ် input တွေပေါ် မမူတည်လို့ပါ။ + +
+Example of causal language modeling in which the next word from a sentence is predicted. + +
+ +နောက်ထပ် ဥပမာတစ်ခုကတော့ *masked language modeling* ဖြစ်ပြီး၊ အဲဒီမှာ မော်ဒယ်က စာကြောင်းထဲက masked word ကို ခန့်မှန်းပါတယ်။ + +
+Example of masked language modeling in which a masked word from a sentence is predicted. + +
+ +## Transformers တွေဟာ မော်ဒယ်ကြီးတွေ ဖြစ်ပါတယ်။[[transformers-are-big-models]] + +DistilBERT လို ထူးခြားချက်အချို့ကလွဲလို့ ပိုမိုကောင်းမွန်တဲ့ စွမ်းဆောင်ရည်ကို ရရှိဖို့အတွက် ယေဘုယျနည်းဗျူဟာကတော့ မော်ဒယ်တွေရဲ့ အရွယ်အစားကို တိုးမြှင့်ခြင်းအပြင် ၎င်းတို့ကို pretrain လုပ်တဲ့ ဒေတာပမာဏကိုပါ တိုးမြှင့်ခြင်း ဖြစ်ပါတယ်။ + +
+Number of parameters of recent Transformers models +
+ +ကံမကောင်းစွာနဲ့ပဲ မော်ဒယ်တစ်ခု၊ အထူးသဖြင့် မော်ဒယ်ကြီးတစ်ခုကို လေ့ကျင့်ဖို့အတွက် ဒေတာအမြောက်အမြား လိုအပ်ပါတယ်။ ဒါကတော့ အချိန်နဲ့ ကွန်ပျူတာ အရင်းအမြစ်တွေအတွက် အလွန်ကုန်ကျများပါတယ်။ အောက်ပါ graph မှာ မြင်ရတဲ့အတိုင်း သဘာဝပတ်ဝန်းကျင်အပေါ် သက်ရောက်မှုတွေလည်း ရှိပါတယ်။ + +
+The carbon footprint of a large language model. + +
+ + + +ဒါကတော့ pretraining ရဲ့ သဘာဝပတ်ဝန်းကျင်အပေါ် သက်ရောက်မှုကို လျှော့ချဖို့ သတိရှိရှိ ကြိုးစားနေတဲ့ အဖွဲ့တစ်ဖွဲ့က ဦးဆောင်တဲ့ (အလွန်ကြီးမားတဲ့) မော်ဒယ်တစ်ခုအတွက် စီမံကိန်းကို ပြသနေတာ ဖြစ်ပါတယ်။ အကောင်းဆုံး hyperparameters တွေ ရရှိဖို့အတွက် စမ်းသပ်မှုများစွာကို လုပ်ဆောင်ခဲ့ရင် ထွက်လာမယ့် ကာဗွန်ခြေရာက ပိုမိုမြင့်မားပါလိမ့်မယ်။ + +သုတေသနအဖွဲ့၊ ကျောင်းသားအဖွဲ့အစည်း ဒါမှမဟုတ် ကုမ္ပဏီတစ်ခုက မော်ဒယ်တစ်ခုကို လေ့ကျင့်ချင်တိုင်း အစကနေ ပြန်လေ့ကျင့်ရမယ်ဆိုရင် ဘယ်လိုဖြစ်မလဲဆိုတာ စဉ်းစားကြည့်ပါ။ ဒါက ကမ္ဘာလုံးဆိုင်ရာ အလွန်ကြီးမားပြီး မလိုအပ်တဲ့ ကုန်ကျစရိတ်တွေ ဖြစ်ပေါ်စေပါလိမ့်မယ်။ + +ဒါကြောင့် language models တွေကို မျှဝေခြင်းဟာ အလွန်အရေးကြီးပါတယ်။ လေ့ကျင့်ပြီးသား weights တွေကို မျှဝေခြင်းနဲ့ ရှိပြီးသား weights တွေပေါ်မှာ တည်ဆောက်ခြင်းက အလုံးစုံ ကွန်ပျူတာကုန်ကျစရိတ်နဲ့ ကာဗွန်ခြေရာကို လျှော့ချပေးပါတယ်။ + +စကားမစပ်၊ သင့်မော်ဒယ်တွေရဲ့ training ကာဗွန်ခြေရာကို ကိရိယာများစွာနဲ့ အကဲဖြတ်နိုင်ပါတယ်။ ဥပမာ [ML CO2 Impact](https://mlco2.github.io/impact/) ဒါမှမဟုတ် 🤗 Transformers မှာ ပေါင်းစပ်ပါဝင်တဲ့ [Code Carbon]( https://codecarbon.io/) စတာတွေ ဖြစ်ပါတယ်။ ဒီအကြောင်း ပိုမိုသိရှိလိုပါက သင်ရဲ့ training ရဲ့ ခြေရာကို ခန့်မှန်းပေးမယ့် `emissions.csv` ဖိုင်တစ်ခုကို ဘယ်လိုထုတ်လုပ်ရမယ်ဆိုတာ ပြသထားတဲ့ [blog post](https://huggingface.co/blog/carbon-emissions-on-the-hub) ကို ဖတ်ရှုနိုင်ပါတယ်။ ဒါ့အပြင် ဒီအကြောင်းအရာကို ဆွေးနွေးထားတဲ့ 🤗 Transformers ရဲ့ [documentation](https://huggingface.co/docs/hub/model-cards-co2) ကိုလည်း ဖတ်ရှုနိုင်ပါတယ်။ + +## Transfer Learning[[transfer-learning]] + + + +*Pretraining* ဆိုတာက မော်ဒယ်တစ်ခုကို အစကနေ လေ့ကျင့်ခြင်းဖြစ်ပါတယ်။ weights တွေကို ကျပန်းသတ်မှတ်ပြီး သင်ယူမှုအတွေ့အကြုံမရှိဘဲ လေ့ကျင့်မှု စတင်ပါတယ်။ + +
+The pretraining of a language model is costly in both time and money. + +
+ +ဒီ pretraining ကို များသောအားဖြင့် များပြားလှတဲ့ ဒေတာတွေပေါ်မှာ လုပ်ဆောင်ပါတယ်။ ဒါကြောင့် ကြီးမားတဲ့ data corpus လိုအပ်ပြီး training ဟာ ရက်သတ္တပတ်ပေါင်းများစွာ ကြာမြင့်နိုင်ပါတယ်။ + +*Fine-tuning* ကတော့ မော်ဒယ်တစ်ခုကို pretrained လုပ်ပြီး **နောက်**မှာ လုပ်ဆောင်တဲ့ training ဖြစ်ပါတယ်။ fine-tuning လုပ်ဖို့အတွက် သင်ဟာ pretrained language model တစ်ခုကို အရင်ရယူပြီးမှ သင်ရဲ့လုပ်ငန်းတာဝန်အတွက် သီးသန့် dataset နဲ့ ထပ်မံ training လုပ်ရပါတယ်။ ဘာလို့ ကျွန်တော်တို့ရဲ့ နောက်ဆုံးအသုံးပြုမှုအတွက် မော်ဒယ်ကို အစကနေ (**scratch**) လုံးဝ မလေ့ကျင့်တာလဲ။ အကြောင်းရင်းအချို့ ရှိပါတယ်။ + +* Pretrained မော်ဒယ်ကို fine-tuning dataset နဲ့ ဆင်တူတဲ့ dataset တစ်ခုပေါ်မှာ လေ့ကျင့်ထားပြီးသား ဖြစ်ပါတယ်။ ဒါကြောင့် fine-tuning လုပ်ငန်းစဉ်ဟာ မူလမော်ဒယ်က pretraining လုပ်စဉ် ရရှိခဲ့တဲ့ အသိပညာ (ဥပမာ- NLP ပြဿနာများအတွက် pretrained မော်ဒယ်ဟာ သင်အသုံးပြုမယ့် ဘာသာစကားရဲ့ စာရင်းအင်းဆိုင်ရာ နားလည်မှု အချို့ကို ရရှိထားမှာပါ) ကို အသုံးချနိုင်ပါတယ်။ +* Pretrained မော်ဒယ်ကို ဒေတာများစွာပေါ်မှာ လေ့ကျင့်ထားပြီးဖြစ်တာကြောင့် fine-tuning လုပ်ဖို့အတွက် သင့်တင့်တဲ့ ရလဒ်တွေရဖို့ ဒေတာပမာဏ အများကြီး လျော့နည်းစွာ လိုအပ်ပါတယ်။ +* အလားတူပဲ ကောင်းမွန်တဲ့ ရလဒ်တွေရဖို့အတွက် လိုအပ်တဲ့ အချိန်နဲ့ အရင်းအမြစ် ပမာဏဟာ အများကြီး လျော့နည်းပါတယ်။ + +ဥပမာအားဖြင့် English ဘာသာစကားပေါ်မှာ လေ့ကျင့်ထားတဲ့ pretrained model တစ်ခုကို အသုံးပြုပြီး arXiv corpus ပေါ်မှာ fine-tune လုပ်ခြင်းဖြင့် သိပ္ပံ/သုတေသနအခြေခံ မော်ဒယ်တစ်ခုကို ရရှိနိုင်ပါတယ်။ fine-tuning လုပ်ရာမှာ ဒေတာအနည်းငယ်သာ လိုအပ်ပါလိမ့်မယ်။ pretrained model ရရှိထားတဲ့ အသိပညာကို "လွှဲပြောင်းပေးခြင်း" (transferred) ဖြစ်တာကြောင့် *transfer learning* လို့ ခေါ်ဆိုရခြင်း ဖြစ်ပါတယ်။ + +
+The fine-tuning of a language model is cheaper than pretraining in both time and money. + +
+ +ဒါကြောင့် မော်ဒယ်တစ်ခုကို fine-tuning လုပ်ခြင်းက အချိန်၊ ဒေတာ၊ ငွေကြေးနဲ့ သဘာဝပတ်ဝန်းကျင်ဆိုင်ရာ ကုန်ကျစရိတ်တွေကို လျှော့ချပေးပါတယ်။ training လုပ်တာဟာ pretraining အပြည့်အစုံထက် ကန့်သတ်ချက်နည်းတာကြောင့် မတူညီတဲ့ fine-tuning ပုံစံတွေကို ထပ်ခါတလဲလဲ ပြုလုပ်ဖို့ ပိုမြန်ဆန်ပြီး ပိုလွယ်ကူပါတယ်။ + +ဒီလုပ်ငန်းစဉ်ဟာ အစကနေ လေ့ကျင့်တာထက် ပိုမိုကောင်းမွန်တဲ့ ရလဒ်တွေ ရရှိစေပါလိမ့်မယ် (သင့်မှာ ဒေတာအများကြီးမရှိဘူးဆိုရင်)။ ဒါကြောင့် သင်ဟာ သင်လုပ်ဆောင်မယ့် လုပ်ငန်းနဲ့ အနီးစပ်ဆုံးဖြစ်တဲ့ pretrained model တစ်ခုကို အမြဲတမ်း အသုံးပြုပြီး fine-tune လုပ်သင့်ပါတယ်။ + +## ယေဘုယျ Transformer architecture[[general-transformer-architecture]] + +ဒီအပိုင်းမှာတော့ Transformer မော်ဒယ်ရဲ့ ယေဘုယျ architecture ကို ခြုံငုံသုံးသပ်သွားပါမယ်။ အချို့အယူအဆတွေကို နားမလည်ရင် စိတ်မပူပါနဲ့။ အစိတ်အပိုင်းတစ်ခုချင်းစီကို အသေးစိတ် ဖော်ပြထားတဲ့ အပိုင်းတွေ နောက်ပိုင်းမှာ ရှိပါသေးတယ်။ + + + +မော်ဒယ်ဟာ အဓိကအားဖြင့် အပိုင်းနှစ်ပိုင်းနဲ့ ဖွဲ့စည်းထားပါတယ်။ + +* **Encoder (ဘယ်ဘက်)**: Encoder က input ကို လက်ခံပြီး ၎င်းရဲ့ ကိုယ်စားပြုမှု (features) ကို တည်ဆောက်ပါတယ်။ ဒါကတော့ မော်ဒယ်ဟာ input ကနေ နားလည်နိုင်စွမ်းကို ရယူဖို့ အကောင်းဆုံးဖြစ်အောင် ပြင်ဆင်ထားတယ်လို့ ဆိုလိုပါတယ်။ +* **Decoder (ညာဘက်)**: Decoder က encoder ရဲ့ ကိုယ်စားပြုမှု (features) ကို အခြား inputs တွေနဲ့အတူ အသုံးပြုပြီး target sequence တစ်ခုကို ဖန်တီးပေးပါတယ်။ ဒါကတော့ မော်ဒယ်ဟာ outputs တွေကို ဖန်တီးဖို့ အကောင်းဆုံးဖြစ်အောင် ပြင်ဆင်ထားတယ်လို့ ဆိုလိုပါတယ်။ + +
+Architecture of a Transformers models + +
+ +ဒီအပိုင်းတစ်ခုချင်းစီကို လုပ်ငန်းတာဝန်ပေါ်မူတည်ပြီး သီးခြားစီ အသုံးပြုနိုင်ပါတယ်။ + +* **Encoder-only models**: စာကြောင်းခွဲခြားသတ်မှတ်ခြင်း (sentence classification) နဲ့ သီးခြားအမည် ဖော်ထုတ်ခြင်း (named entity recognition) လို input ကို နားလည်ဖို့ လိုအပ်တဲ့ လုပ်ငန်းတွေအတွက် ကောင်းပါတယ်။ +* **Decoder-only models**: စာသားထုတ်လုပ်ခြင်း (text generation) လို ဖန်တီးမှုဆိုင်ရာ လုပ်ငန်းတွေအတွက် ကောင်းပါတယ်။ +* **Encoder-decoder models** သို့မဟုတ် **sequence-to-sequence models**: ဘာသာပြန်ခြင်း ဒါမှမဟုတ် အကျဉ်းချုပ်ခြင်း လို input လိုအပ်တဲ့ ဖန်တီးမှုဆိုင်ရာ လုပ်ငန်းတွေအတွက် ကောင်းပါတယ်။ + +ဒီ architecture တွေကို နောက်ပိုင်းအပိုင်းတွေမှာ သီးခြားစီ နက်နက်နဲနဲ လေ့လာသွားမှာ ဖြစ်ပါတယ်။ + +## Attention layers[[attention-layers]] + +Transformer မော်ဒယ်တွေရဲ့ အဓိကအင်္ဂါရပ်တစ်ခုကတော့ ၎င်းတို့ကို *attention layers* လို့ခေါ်တဲ့ အထူး layers တွေနဲ့ တည်ဆောက်ထားခြင်း ဖြစ်ပါတယ်။ တကယ်တော့ Transformer architecture ကို မိတ်ဆက်တဲ့ စာတမ်းရဲ့ ခေါင်းစဉ်က ["Attention Is All You Need"](https://arxiv.org/abs/1706.03762) ပါပဲ။ attention layers တွေရဲ့ အသေးစိတ်အချက်အလက်တွေကို သင်တန်းရဲ့ နောက်ပိုင်းမှာ လေ့လာသွားပါမယ်။ အခုအတွက်တော့ ဒီ layer က မော်ဒယ်ကို သင်ပေးပို့လိုက်တဲ့ စာကြောင်းထဲက တချို့စကားလုံးတွေကို သီးခြားအာရုံစိုက်ဖို့ (ကျန်တာတွေကိုတော့ လျစ်လျူရှုဖို့) စကားလုံးတစ်ခုစီရဲ့ ကိုယ်စားပြုမှု (representation) ကို လုပ်ဆောင်နေစဉ်မှာ ပြောပြပေးတယ်ဆိုတာကိုပဲ သိထားဖို့ လိုပါတယ်။ + +ဒီအကြောင်းအရာကို နားလည်လွယ်အောင် ဥပမာတစ်ခုနဲ့ ပြောရရင် အင်္ဂလိပ်ဘာသာစကားကနေ ပြင်သစ်ဘာသာစကားကို ဘာသာပြန်ခြင်း လုပ်ငန်းကို စဉ်းစားကြည့်ပါ။ "You like this course" ဆိုတဲ့ input ကို ပေးတဲ့အခါ ဘာသာပြန်မော်ဒယ်က "like" ဆိုတဲ့ စကားလုံးအတွက် မှန်ကန်တဲ့ ဘာသာပြန်ကို ရရှိဖို့အတွက် ဘေးကပ်လျက်ရှိတဲ့ "You" ဆိုတဲ့ စကားလုံးကိုလည်း အာရုံစိုက်ဖို့ လိုပါတယ်။ ဘာလို့လဲဆိုတော့ ပြင်သစ်ဘာသာစကားမှာ "like" ဆိုတဲ့ ကြိယာကို subject ပေါ်မူတည်ပြီး ကွဲပြားစွာ တွဲစပ်ရလို့ပါ။ ဒါပေမယ့် စာကြောင်းရဲ့ ကျန်တဲ့အပိုင်းတွေကတော့ အဲဒီစကားလုံးရဲ့ ဘာသာပြန်ဖို့အတွက် အသုံးမဝင်ပါဘူး။ အလားတူပဲ "this" ကို ဘာသာပြန်တဲ့အခါ မော်ဒယ်က "course" ဆိုတဲ့ စကားလုံးကိုပါ အာရုံစိုက်ဖို့ လိုပါလိမ့်မယ်။ ဘာလို့လဲဆိုတော့ "this" က သက်ဆိုင်ရာနာမ်က ပုလ္လင် (masculine) လား၊ ဣတ္ထိလင် (feminine) လားဆိုတာပေါ်မူတည်ပြီး ကွဲပြားစွာ ဘာသာပြန်လို့ပါ။ ဒီမှာလည်း စာကြောင်းထဲက အခြားစကားလုံးတွေက "course" ကို ဘာသာပြန်ဖို့အတွက် အရေးမကြီးပါဘူး။ ပိုမိုရှုပ်ထွေးတဲ့ စာကြောင်းတွေ (နဲ့ ပိုမိုရှုပ်ထွေးတဲ့ သဒ္ဒါစည်းမျဉ်းတွေ) နဲ့ဆိုရင် မော်ဒယ်က စာကြောင်းထဲမှာ ဝေးကွာနေတဲ့ စကားလုံးတွေကိုပါ သီးခြားအာရုံစိုက်ဖို့ လိုအပ်ပါလိမ့်မယ်။ + +ဒီလို အယူအဆမျိုးက သဘာဝဘာသာစကားနဲ့ သက်ဆိုင်တဲ့ လုပ်ငန်းတာဝန်တွေ အားလုံးမှာ အကျုံးဝင်ပါတယ်။ စကားလုံးတစ်ခုတည်းက သူ့ဘာသာသူ အဓိပ္ပာယ်ရှိပေမယ့်၊ အဲဒီအဓိပ္ပာယ်ဟာ context ကြောင့် နက်နက်နဲနဲ သက်ရောက်မှုရှိပါတယ်။ အဲဒီ context ဟာ လေ့လာနေတဲ့ စကားလုံးရဲ့ အရင် ဒါမှမဟုတ် နောက်ကပ်လျက်ရှိတဲ့ အခြားစကားလုံး (သို့မဟုတ် စကားလုံးများ) ဖြစ်နိုင်ပါတယ်။ + +Attention layers တွေ ဘာအကြောင်းလဲဆိုတာကို နားလည်ပြီးပြီဆိုတော့ Transformer architecture ကို ပိုမိုနီးကပ်စွာ လေ့လာကြည့်ရအောင်။ + +## မူလ architecture[[the-original-architecture]] + +Transformer architecture ကို မူလက ဘာသာပြန်ခြင်းအတွက် ဒီဇိုင်းထုတ်ခဲ့တာပါ။ training လုပ်နေစဉ်အတွင်း encoder က သတ်မှတ်ထားတဲ့ ဘာသာစကားတစ်ခုနဲ့ input တွေ (စာကြောင်းတွေ) ကို လက်ခံရရှိပြီး၊ decoder ကတော့ တူညီတဲ့ စာကြောင်းတွေကို လိုချင်တဲ့ target language နဲ့ လက်ခံရရှိပါတယ်။ encoder မှာ attention layers တွေက စာကြောင်းတစ်ကြောင်းလုံးရှိ စကားလုံးအားလုံးကို အသုံးပြုနိုင်ပါတယ်။ (ဘာလို့လဲဆိုတော့ ခုဏက မြင်ခဲ့ရတဲ့အတိုင်း စကားလုံးတစ်ခုရဲ့ ဘာသာပြန်ခြင်းက စာကြောင်းထဲမှာ အဲဒီစကားလုံးရဲ့ နောက်က ဒါမှမဟုတ် အရင်က ရှိနေတဲ့အရာတွေပေါ် မူတည်နိုင်လို့ပါ)။ ဒါပေမယ့် decoder ကတော့ တစ်ခုချင်းစီ အစဉ်လိုက် အလုပ်လုပ်ပြီး သူ ဘာသာပြန်ပြီးသား စာကြောင်းထဲက စကားလုံးတွေကိုပဲ အာရုံစိုက်နိုင်ပါတယ်။ (ဒါကြောင့် လက်ရှိ ထုတ်လုပ်နေတဲ့ စကားလုံးရဲ့ အရင်က စကားလုံးတွေကိုသာ)။ ဥပမာအားဖြင့် ဘာသာပြန်ထားတဲ့ target ရဲ့ ပထမစကားလုံး သုံးလုံးကို ခန့်မှန်းပြီးတဲ့အခါ ၎င်းတို့ကို decoder ကို ပေးလိုက်ပါတယ်။ ထို့နောက် decoder က encoder ရဲ့ input တွေအားလုံးကို အသုံးပြုပြီး စတုတ္ထစကားလုံးကို ခန့်မှန်းဖို့ ကြိုးစားပါတယ်။ + +Training လုပ်နေစဉ်အတွင်း (မော်ဒယ်က target sentences တွေကို ဝင်ရောက်ကြည့်ရှုနိုင်တဲ့အခါ) အရှိန်မြှင့်ဖို့အတွက် decoder ကို target အပြည့်အစုံကို ထည့်ပေးပါတယ်။ ဒါပေမယ့် အနာဂတ်စကားလုံးတွေကို အသုံးပြုခွင့် မပြုပါဘူး။ (အကယ်၍ သူက position 2 မှာရှိတဲ့ စကားလုံးကို ခန့်မှန်းဖို့ ကြိုးစားနေစဉ် position 2 မှာရှိတဲ့ စကားလုံးကို ဝင်ရောက်ကြည့်ရှုခွင့်ရရင် ပြဿနာက သိပ်မခက်ခဲတော့ပါဘူး!)။ ဥပမာအားဖြင့် စတုတ္ထစကားလုံးကို ခန့်မှန်းဖို့ ကြိုးစားနေစဉ်မှာ attention layer က position 1 မှ 3 အထိရှိတဲ့ စကားလုံးတွေကိုပဲ ဝင်ရောက်ကြည့်ရှုနိုင်ပါလိမ့်မယ်။ + +မူလ Transformer architecture က ဒီလိုပုံစံဖြစ်ပြီး ဘယ်ဘက်မှာ encoder နဲ့ ညာဘက်မှာ decoder ပါဝင်ပါတယ်။ + +
+Architecture of a Transformers models + +
+ +decoder block မှာရှိတဲ့ ပထမ attention layer က decoder ရဲ့ အတိတ် inputs အားလုံးကို အာရုံစိုက်ပေမယ့် ဒုတိယ attention layer က encoder ရဲ့ output ကို အသုံးပြုတယ်ဆိုတာ သတိပြုပါ။ ဒါကြောင့် လက်ရှိစကားလုံးကို အကောင်းဆုံး ခန့်မှန်းနိုင်ဖို့ input စာကြောင်းတစ်ခုလုံးကို ဝင်ရောက်ကြည့်ရှုနိုင်ပါတယ်။ ဒါက အရမ်းအသုံးဝင်ပါတယ်။ ဘာလို့လဲဆိုတော့ မတူညီတဲ့ ဘာသာစကားတွေမှာ စကားလုံးတွေကို မတူညီတဲ့ အစီအစဉ်တွေနဲ့ ချထားတဲ့ သဒ္ဒါစည်းမျဉ်းတွေ ရှိနိုင်တာကြောင့် ဒါမှမဟုတ် စာကြောင်းထဲမှာ နောက်ပိုင်းမှာ ပေးထားတဲ့ context အချို့က ပေးထားတဲ့ စကားလုံးရဲ့ အကောင်းဆုံးဘာသာပြန်ကို ဆုံးဖြတ်ရာမှာ အထောက်အကူဖြစ်နိုင်လို့ပါ။ + +*Attention mask* ကို encoder/decoder မှာလည်း အသုံးပြုနိုင်ပြီး မော်ဒယ်က အချို့အထူးစကားလုံးတွေကို အာရုံစိုက်ခြင်းမှ ကာကွယ်ပေးပါတယ်။ ဥပမာအားဖြင့် စာကြောင်းတွေကို စုပေါင်းတဲ့အခါ input အားလုံးကို အလျားတူအောင် ပြုလုပ်ဖို့ အသုံးပြုတဲ့ အထူး padding word စတာတွေပါ။ + +## Architectures vs. checkpoints[[architecture-vs-checkpoints]] + +ဒီသင်တန်းမှာ Transformer မော်ဒယ်တွေထဲကို နက်နက်နဲနဲ လေ့လာတဲ့အခါ *architectures* နဲ့ *checkpoints* အပြင် *models* ဆိုတဲ့ အသုံးအနှုန်းတွေကိုလည်း တွေ့ရပါလိမ့်မယ်။ ဒီအသုံးအနှုန်းတွေ အားလုံးမှာ အဓိပ္ပာယ်အနည်းငယ် ကွဲပြားပါတယ်။ + +* **Architecture**: ဒါကတော့ မော်ဒယ်ရဲ့ ပုံစံတည်ဆောက်ပုံ (skeleton) ဖြစ်ပါတယ်။ မော်ဒယ်အတွင်းမှာ ဖြစ်ပျက်နေတဲ့ layer တစ်ခုစီနဲ့ လုပ်ဆောင်မှုတစ်ခုစီရဲ့ အဓိပ္ပာယ်ဖွင့်ဆိုချက်ပါ။ +* **Checkpoints**: ဒါတွေကတော့ ပေးထားတဲ့ architecture မှာ load လုပ်မယ့် weights တွေ ဖြစ်ပါတယ်။ +* **Model**: ဒါကတော့ "architecture" သို့မဟုတ် "checkpoint" လို တိကျတဲ့ အဓိပ္ပာယ်မရှိဘဲ နှစ်ခုစလုံးကို ဆိုလိုနိုင်တဲ့ ယေဘုယျအသုံးအနှုန်းပါ။ ဒီသင်တန်းကတော့ မရေရာမှုတွေကို လျှော့ချဖို့အတွက် အရေးကြီးတဲ့အခါ *architecture* ဒါမှမဟုတ် *checkpoint* လို့ သတ်သတ်မှတ်မှတ် ဖော်ပြပေးပါလိမ့်မယ်။ + +ဥပမာအားဖြင့် BERT က architecture တစ်ခုဖြစ်ပြီး `bert-base-cased` ကတော့ Google အဖွဲ့က BERT ရဲ့ ပထမဆုံးထုတ်ဝေမှုအတွက် လေ့ကျင့်ပေးထားတဲ့ weights အစုအဝေးဖြစ်တာကြောင့် checkpoint တစ်ခု ဖြစ်ပါတယ်။ ဒါပေမယ့် လူတစ်ဦးက "the BERT model" နဲ့ "the `bert-base-cased` model" လို့ နှစ်မျိုးစလုံး ပြောဆိုနိုင်ပါတယ်။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Transformer Models**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Attention**: Transformer model များတွင် အသုံးပြုသော ယန္တရားတစ်ခုဖြစ်ပြီး input sequence အတွင်းရှိ အရေးကြီးသော အစိတ်အပိုင်းများကို မော်ဒယ်အား ပိုမိုအာရုံစိုက်စေသည်။ +* **Encoder-Decoder Architecture**: Encoder နှင့် Decoder နှစ်ခုစလုံး ပါဝင်သော Transformer architecture တစ်မျိုးဖြစ်ပြီး ဘာသာပြန်ခြင်းကဲ့သို့သော input sequence မှ output sequence တစ်ခုသို့ ပြောင်းလဲခြင်း လုပ်ငန်းများအတွက် အသုံးပြုပါတယ်။ +* **Architecture**: Machine Learning မော်ဒယ်တစ်ခု၏ အတွင်းပိုင်းတည်ဆောက်ပုံ၊ အလွှာများ (layers) နှင့် လုပ်ဆောင်မှုများ (operations) ၏ အဓိပ္ပာယ်ဖွင့်ဆိုချက်။ +* **GPT (Generative Pre-trained Transformer)**: OpenAI မှ တီထွင်ထားသော Transformer-based Large Language Model (LLM) အမျိုးအစားတစ်ခု။ +* **Pretrained Model**: ဒေတာအမြောက်အမြားပေါ်တွင် ကြိုတင်လေ့ကျင့်ထားသော မော်ဒယ်။ +* **Fine-tuning**: ကြိုတင်လေ့ကျင့်ထားပြီးသား (pre-trained) မော်ဒယ်တစ်ခုကို သီးခြားလုပ်ငန်းတစ်ခု (specific task) အတွက် အနည်းငယ်သော ဒေတာနဲ့ ထပ်မံလေ့ကျင့်ပေးခြင်းကို ဆိုလိုပါတယ်။ +* **NLP (Natural Language Processing)**: ကွန်ပျူတာတွေ လူသားဘာသာစကားကို နားလည်၊ အဓိပ္ပာယ်ဖော်ပြီး၊ ဖန်တီးနိုင်အောင် လုပ်ဆောင်ပေးတဲ့ Artificial Intelligence (AI) ရဲ့ နယ်ပယ်ခွဲတစ်ခု ဖြစ်ပါတယ်။ +* **BERT (Bidirectional Encoder Representations from Transformers)**: Google က ထုတ်လုပ်ထားတဲ့ Transformer-based Pretrained Model တစ်ခုဖြစ်ပြီး စာသားတွေရဲ့ အဓိပ္ပာယ်ကို နားလည်ဖို့အတွက် အသုံးပြုပါတယ်။ +* **GPT-2**: GPT ရဲ့ ပိုမိုကောင်းမွန်ပြီး ပိုကြီးတဲ့ ဗားရှင်း။ +* **Ethical Concerns**: ကျင့်ဝတ်ဆိုင်ရာ စိုးရိမ်ပူပန်မှုများ။ +* **T5 (Text-to-Text Transfer Transformer)**: Google က ထုတ်လုပ်ထားတဲ့ Transformer-based Model တစ်ခုဖြစ်ပြီး NLP လုပ်ငန်းတာဝန်များစွာကို text-to-text format ဖြင့် ဖြေရှင်းရန် ဒီဇိုင်းထုတ်ထားပါတယ်။ +* **Sequence-to-sequence**: input sequence တစ်ခုကို output sequence တစ်ခုအဖြစ် ပြောင်းလဲပေးသော မော်ဒယ်အမျိုးအစား။ (ဥပမာ- ဘာသာပြန်ခြင်း) +* **GPT-3**: GPT-2 ထက် ပိုမိုကြီးမားသော ဗားရှင်း။ +* **Zero-shot learning**: မော်ဒယ်တစ်ခုကို သီးခြားလုပ်ငန်းတစ်ခုအတွက် လေ့ကျင့်မှုမရှိဘဲ လုပ်ငန်းကို လုပ်ဆောင်စေခြင်း။ +* **InstructGPT**: ညွှန်ကြားချက်များကို ပိုမိုကောင်းမွန်စွာ လိုက်နာနိုင်ရန် လေ့ကျင့်ထားသော GPT-3 ၏ ဗားရှင်းတစ်ခု။ +* **Llama**: Meta မှ တီထွင်ထားသော Transformer-based Large Language Model (LLM) အမျိုးအစားတစ်ခု။ +* **Mistral**: ၇ ဘီလီယံ parameter ပါရှိသော Large Language Model (LLM) တစ်ခု။ +* **Grouped-query attention**: Transformer model များတွင် အသုံးပြုသော attention mechanism တစ်မျိုးဖြစ်ပြီး inference ကို ပိုမိုမြန်ဆန်စေရန် ကူညီပေးသည်။ +* **Inference**: လေ့ကျင့်ပြီးသား မော်ဒယ်တစ်ခုကို အသုံးပြု၍ input အသစ်များမှ ခန့်မှန်းချက်များ သို့မဟုတ် output များထုတ်လုပ်ခြင်း။ +* **Sliding window attention**: Transformer model များတွင် အသုံးပြုသော attention mechanism တစ်မျိုးဖြစ်ပြီး ရှည်လျားသော sequences များကို ထိထိရောက်ရောက် ကိုင်တွယ်နိုင်စေသည်။ +* **Gemma 2**: Google DeepMind မှ ထုတ်လုပ်သော lightweight, state-of-the-art open models မိသားစုတစ်ခု။ +* **Interleaved local-global attentions**: Transformer model များတွင် အသုံးပြုသော attention mechanism တစ်မျိုးဖြစ်ပြီး local နှင့် global information နှစ်ခုလုံးကို အာရုံစိုက်နိုင်စေသည်။ +* **Knowledge distillation**: ပိုမိုကြီးမားသော၊ ပိုမိုရှုပ်ထွေးသော မော်ဒယ် (teacher model) ၏ အသိပညာကို ပိုမိုသေးငယ်သော၊ ရိုးရှင်းသော မော်ဒယ် (student model) သို့ လွှဲပြောင်းပေးသည့် နည်းလမ်း။ +* **SmolLM2**: သေးငယ်သော အရွယ်အစားရှိသော်လည်း ထူးခြားသော စွမ်းဆောင်ရည်ကို ရရှိစေသော Small Language Model (SLM) တစ်ခု။ +* **Mobile and Edge Devices**: စမတ်ဖုန်းများ၊ တက်ဘလက်များ၊ IoT ကိရိယာများကဲ့သို့ ကွန်ပျူတာစွမ်းအား ကန့်သတ်ချက်ရှိသော ကိရိယာများ။ +* **Auto-regressive Transformer models**: GPT ကဲ့သို့ မော်ဒယ်များ၊ နောက်ထပ်လာမည့် token ကို ယခင် token များအပေါ် အခြေခံ၍ ခန့်မှန်းသည်။ +* **Auto-encoding Transformer models**: BERT ကဲ့သို့ မော်ဒယ်များ၊ masked token များကို input sequence တစ်ခုလုံးအပေါ် အခြေခံ၍ ခန့်မှန်းသည်။ +* **Sequence-to-sequence Transformer models**: T5 ကဲ့သို့ မော်ဒယ်များ၊ input sequence တစ်ခုကို output sequence တစ်ခုအဖြစ် ပြောင်းလဲပေးသည်။ +* **Language Models**: လူသားဘာသာစကားကို နားလည်ပြီး ဖန်တီးနိုင်အောင် သင်ကြားထားသော မော်ဒယ်များ။ +* **Self-supervised learning**: မော်ဒယ်၏ input တွေကနေ ရည်ရွယ်ချက်ကို အလိုအလျောက် တွက်ချက်ပေးတဲ့ သင်ယူမှုပုံစံတစ်ခု။ +* **Transfer Learning**: ကြိုတင်လေ့ကျင့်ထားသော မော်ဒယ်မှ ရရှိသောအသိပညာကို အခြားဆက်စပ်လုပ်ငန်းတစ်ခုသို့ လွှဲပြောင်းအသုံးပြုခြင်း။ +* **Supervised Learning**: human-annotated labels တွေကို အသုံးပြုပြီး မော်ဒယ်ကို သင်ကြားပေးတဲ့ သင်ယူမှုပုံစံတစ်ခု။ +* **Annotated Labels**: လူသားများက ဒေတာများကို မှတ်သားထားသော အမှတ်အသားများ သို့မဟုတ် အမျိုးအစားများ။ +* **Causal Language Modeling**: input sequence ၏ ယခင် token များကို အခြေခံ၍ နောက်ထပ်လာမည့် token ကို ခန့်မှန်းခြင်း။ +* **Masked Language Modeling**: input sequence ထဲရှိ masked (ဝှက်ထားသော) token များကို ခန့်မှန်းခြင်း။ +* **Outliers**: အခြားသောအချက်အလက်များနှင့် ကွဲပြားစွာ ထူးခြားနေသော အချက်အလက်များ။ +* **DistilBERT**: BERT မော်ဒယ်၏ ပိုမိုသေးငယ်ပြီး ပိုမိုမြန်ဆန်သော ဗားရှင်း။ +* **Hyperparameters**: Machine Learning မော်ဒယ်တစ်ခုကို လေ့ကျင့်ရာတွင် သတ်မှတ်ထားသော parameter များ (ဥပမာ- learning rate, batch size)။ +* **Carbon Footprint**: ကာဗွန်ဒိုင်အောက်ဆိုဒ် ထုတ်လွှတ်မှုပမာဏ။ +* **Pretraining**: မော်ဒယ်တစ်ခုကို အစကနေ လေ့ကျင့်ခြင်း။ +* **Weights**: Machine Learning မော်ဒယ်တစ်ခု၏ သင်ယူနိုင်သော အစိတ်အပိုင်းများ။ +* **Randomly Initialized**: မော်ဒယ်၏ weights များကို ကျပန်းတန်ဖိုးများဖြင့် စတင်သတ်မှတ်ခြင်း။ +* **Corpus**: စာသားများစွာ၏ စုဆောင်းမှု (Collection of text data)။ +* **arXiv corpus**: သိပ္ပံနည်းကျ စာတမ်းများ၊ သုတေသနစာတမ်းများ စသည်တို့၏ စုဆောင်းမှု။ +* **Encoder**: Transformer Architecture ရဲ့ အစိတ်အပိုင်းတစ်ခုဖြစ်ပြီး input data (ဥပမာ- စာသား) ကို နားလည်ပြီး ကိုယ်စားပြုတဲ့ အချက်အလက် (representation) အဖြစ် ပြောင်းလဲပေးပါတယ်။ +* **Decoder**: Transformer Architecture ရဲ့ အစိတ်အပိုင်းတစ်ခုဖြစ်ပြီး encoder ကနေ ရရှိတဲ့ အချက်အလက် (representation) ကို အသုံးပြုပြီး output data (ဥပမာ- ဘာသာပြန်ထားတဲ့ စာသား) ကို ထုတ်ပေးပါတယ်။ +* **Features**: ဒေတာတစ်ခု၏ ထူးခြားသော လက္ခဏာများ သို့မဟုတ် ဂုဏ်သတ္တိများ။ +* **Sentence Classification**: စာကြောင်းတစ်ခုလုံးကို ကြိုတင်သတ်မှတ်ထားသော အမျိုးအစားများထဲသို့ ခွဲခြားသတ်မှတ်ခြင်း။ +* **Named Entity Recognition (NER)**: စာသားထဲက လူအမည်၊ နေရာအမည်၊ အဖွဲ့အစည်းအမည် စတဲ့ သီးခြားအမည်တွေကို ရှာဖွေဖော်ထုတ်ခြင်း။ +* **Text Generation**: AI မော်ဒယ်များကို အသုံးပြု၍ လူသားကဲ့သို့သော စာသားအသစ်များ ဖန်တီးခြင်း။ +* **Translation**: ဘာသာစကားတစ်ခုကနေ အခြားဘာသာစကားတစ်ခုကို စာသားတွေ ဒါမှမဟုတ် စကားပြောတွေကို အလိုအလျောက် ဘာသာပြန်ဆိုခြင်း။ +* **Summarization**: စာသားတစ်ခုကို အဓိကအချက်အလက်များသာ ပါဝင်သော အကျဉ်းချုပ်အဖြစ် ပြောင်းလဲခြင်း။ +* **Attention Layers**: Transformer model များတွင် input data ၏ မတူညီသော အစိတ်အပိုင်းများအပေါ် အာရုံစိုက်နိုင်ရန် ကူညီပေးသော အလွှာများ။ +* **Attention Mechanism**: Transformer model များတွင် input sequence အတွင်းရှိ မတူညီသော စကားလုံးများ၏ ဆက်နွယ်မှုကို နားလည်ရန် ကူညီပေးသော ယန္တရား။ +* **Conjugated**: ကြိယာတစ်ခု၏ ပုံစံသည် subject သို့မဟုတ် tense ပေါ်မူတည်၍ ပြောင်းလဲခြင်း။ +* **Masculine/Feminine**: ဘာသာစကားအချို့တွင် နာမ်များကို ခွဲခြားထားသော ကျား/မ လိင်ခွဲခြားမှု။ +* **Context**: စကားလုံး၊ စာကြောင်း သို့မဟုတ် အကြောင်းအရာတစ်ခုရဲ့ အဓိပ္ပာယ်ကို နားလည်စေရန် ကူညီပေးသော ပတ်ဝန်းကျင်ရှိ အချက်အလက်များ။ +* **Target Language**: ဘာသာပြန်လိုသော ဘာသာစကား။ +* **Sequentially**: တစ်ခုပြီးတစ်ခု အစီအစဉ်အတိုင်း လုပ်ဆောင်ခြင်း။ +* **Attention Mask**: မော်ဒယ်ကို အချို့သော input token များအပေါ် အာရုံစိုက်ခြင်းမှ တားဆီးရန် အသုံးပြုသော mask (အမှတ်အသား)။ +* **Padding Word**: input sequence များ၏ အလျားကို တူညီစေရန်အတွက် ထပ်ပေါင်းထည့်သော အထူးစကားလုံး။ +* **Batching**: မော်ဒယ်ကို တစ်ကြိမ်တည်း လေ့ကျင့်ရန် သို့မဟုတ် inference လုပ်ရန်အတွက် ဒေတာနမူနာများစွာကို အစုလိုက် စုစည်းခြင်း။ +* **Checkpoint**: သတ်မှတ်ထားသော architecture အတွက် လေ့ကျင့်ပြီးသား weights များ။ \ No newline at end of file diff --git a/chapters/my/chapter1/5.mdx b/chapters/my/chapter1/5.mdx new file mode 100644 index 000000000..6e8cfee59 --- /dev/null +++ b/chapters/my/chapter1/5.mdx @@ -0,0 +1,303 @@ +# 🤗 Transformers တွေက လုပ်ငန်းတာဝန်တွေကို ဘယ်လိုဖြေရှင်းပေးလဲ။[[how-transformers-solve-tasks]] + + + +"[Transformers တွေက ဘာတွေလုပ်နိုင်လဲ။](/course/chapter1/3)" မှာ Natural Language Processing (NLP)၊ speech and audio, computer vision လုပ်ငန်းတာဝန်တွေနဲ့ ၎င်းတို့ရဲ့ အရေးကြီးတဲ့ အသုံးချမှုတွေအကြောင်းကို သင်ယူခဲ့ပြီးပါပြီ။ ဒီစာမျက်နှာကတော့ မော်ဒယ်တွေက ဒီလုပ်ငန်းတာဝန်တွေကို ဘယ်လိုဖြေရှင်းပေးလဲဆိုတာကို အသေးစိတ်လေ့လာပြီး၊ အတွင်းပိုင်းမှာ ဘာတွေဖြစ်ပျက်နေလဲဆိုတာကို ရှင်းပြပေးမှာပါ။ လုပ်ငန်းတာဝန်တစ်ခုကို ဖြေရှင်းဖို့ နည်းလမ်းများစွာရှိပါတယ်။ အချို့မော်ဒယ်တွေက နည်းစနစ်အချို့ကို အသုံးပြုနိုင်သလို လုပ်ငန်းတာဝန်ကို ချဉ်းကပ်ပုံအသစ်တစ်ခုကနေတောင် ချဉ်းကပ်နိုင်ပါတယ်။ ဒါပေမယ့် Transformer မော်ဒယ်တွေအတွက်တော့ အခြေခံသဘောတရားက အတူတူပါပဲ။ ၎င်းတို့ရဲ့ ပြောင်းလွယ်ပြင်လွယ်ရှိတဲ့ architecture ကြောင့် မော်ဒယ်အများစုဟာ encoder, decoder သို့မဟုတ် encoder-decoder ဖွဲ့စည်းပုံရဲ့ အမျိုးအစားခွဲ (variant) တစ်ခု ဖြစ်ပါတယ်။ + +> [!TIP] +> သီးခြား architectural variants တွေထဲ မဝင်ခင်မှာ၊ လုပ်ငန်းတာဝန်အများစုဟာ အလားတူ ပုံစံတစ်ခုကို လိုက်နာတယ်ဆိုတာ နားလည်ထားဖို့ အထောက်အကူဖြစ်ပါတယ်။ input data ကို မော်ဒယ်ကနေတစ်ဆင့် လုပ်ဆောင်ပြီး output ကို သီးခြားလုပ်ငန်းတစ်ခုအတွက် အနက်ပြန်ပါတယ်။ ကွာခြားချက်တွေကတော့ data ကို ဘယ်လိုပြင်ဆင်ထားလဲ၊ ဘယ်မော်ဒယ် architecture variant ကို အသုံးပြုထားလဲ၊ ပြီးတော့ output ကို ဘယ်လို လုပ်ဆောင်ထားလဲဆိုတာတွေပါပဲ။ + +လုပ်ငန်းတာဝန်တွေကို ဘယ်လိုဖြေရှင်းလဲဆိုတာ ရှင်းပြဖို့အတွက် မော်ဒယ်အတွင်းမှာ ဘာတွေဖြစ်ပျက်ပြီး အသုံးဝင်တဲ့ ခန့်မှန်းချက်တွေကို ထုတ်ပေးလဲဆိုတာကို ကြည့်သွားပါမယ်။ ကျွန်တော်တို့ အောက်ပါမော်ဒယ်တွေနဲ့ ၎င်းတို့ရဲ့ လုပ်ငန်းတာဝန်တွေကို လေ့လာပါမယ်။ + +- [Wav2Vec2](https://huggingface.co/docs/transformers/model_doc/wav2vec2) ကို audio classification နဲ့ automatic speech recognition (ASR) အတွက် +- [Vision Transformer (ViT)](https://huggingface.co/docs/transformers/model_doc/vit) နဲ့ [ConvNeXT](https://huggingface.co/docs/transformers/model_doc/convnext) ကို image classification အတွက် +- [DETR](https://huggingface.co/docs/transformers/model_doc/detr) ကို object detection အတွက် +- [Mask2Former](https://huggingface.co/docs/transformers/model_doc/mask2former) ကို image segmentation အတွက် +- [GLPN](https://huggingface.co/docs/transformers/model_doc/glpn) ကို depth estimation အတွက် +- [BERT](https://huggingface.co/docs/transformers/model_doc/bert) ကို encoder ကိုအသုံးပြုတဲ့ text classification, token classification နဲ့ question answering လို NLP လုပ်ငန်းတာဝန်တွေအတွက် +- [GPT2](https://huggingface.co/docs/transformers/model_doc/gpt2) ကို decoder ကိုအသုံးပြုတဲ့ text generation လို NLP လုပ်ငန်းတာဝန်တွေအတွက် +- [BART](https://huggingface.co/docs/transformers/model_doc/bart) ကို encoder-decoder ကိုအသုံးပြုတဲ့ summarization နဲ့ translation လို NLP လုပ်ငန်းတာဝန်တွေအတွက် + +> [!TIP] +> ဆက်မသွားခင်မှာ မူရင်း Transformer architecture ရဲ့ အခြေခံအသိပညာအချို့ ရှိထားတာ ကောင်းပါတယ်။ encoders, decoders နဲ့ attention တွေ ဘယ်လိုအလုပ်လုပ်လဲဆိုတာကို သိထားရင် Transformer မော်ဒယ်အမျိုးမျိုး ဘယ်လိုအလုပ်လုပ်လဲဆိုတာ နားလည်ဖို့ အထောက်အကူဖြစ်ပါလိမ့်မယ်။ အသေးစိတ်အချက်အလက်တွေအတွက် ကျွန်တော်တို့ရဲ့ [အရင်အခန်း](https://huggingface.co/course/chapter1/4?fw=pt) ကို သေချာကြည့်ရှုပေးပါ။ + +## ဘာသာစကားအတွက် Transformer မော်ဒယ်များ[[transformer-models-for-language]] + +ဘာသာစကားမော်ဒယ်တွေဟာ ခေတ်သစ် NLP ရဲ့ အဓိကအချက်အချာမှာ ရှိပါတယ်။ ၎င်းတို့ကို စာသားတွေထဲက စကားလုံးတွေ ဒါမှမဟုတ် tokens တွေကြားက စာရင်းအင်းဆိုင်ရာ ပုံစံတွေနဲ့ ဆက်နွယ်မှုတွေကို သင်ယူခြင်းဖြင့် လူသားဘာသာစကားကို နားလည်ပြီး ဖန်တီးနိုင်အောင် ဒီဇိုင်းထုတ်ထားပါတယ်။ + +Transformer ကို မူလက machine translation အတွက် ဒီဇိုင်းထုတ်ခဲ့တာဖြစ်ပြီး၊ အဲဒီအချိန်ကတည်းက AI လုပ်ငန်းတာဝန်အားလုံးကို ဖြေရှင်းဖို့အတွက် အခြေခံ architecture တစ်ခု ဖြစ်လာခဲ့ပါတယ်။ အချို့လုပ်ငန်းတာဝန်တွေက Transformer ရဲ့ encoder ဖွဲ့စည်းပုံနဲ့ ပိုသင့်တော်ပြီး အချို့ကတော့ decoder နဲ့ ပိုလိုက်ဖက်ပါတယ်။ သို့သော် အချို့လုပ်ငန်းတာဝန်တွေကတော့ Transformer ရဲ့ encoder-decoder ဖွဲ့စည်းပုံ နှစ်ခုလုံးကို အသုံးပြုပါတယ်။ + +### ဘာသာစကားမော်ဒယ်(language models)တွေ ဘယ်လိုအလုပ်လုပ်လဲ။[[how-language-models-work]] + +ဘာသာစကားမော်ဒယ်တွေဟာ ပတ်ဝန်းကျင်ရှိ စကားလုံးတွေရဲ့ အကြောင်းအရာ (context) ကို ပေးထားပြီး စကားလုံးတစ်လုံးရဲ့ ဖြစ်နိုင်ခြေ (probability) ကို ခန့်မှန်းဖို့ လေ့ကျင့်ပေးခြင်းဖြင့် အလုပ်လုပ်ပါတယ်။ ဒါက ၎င်းတို့ကို ဘာသာစကားကို အခြေခံနားလည်စေပြီး အခြားလုပ်ငန်းတာဝန်တွေဆီကို ယေဘုယျ (generalize) လုပ်နိုင်စေပါတယ်။ + +Transformer မော်ဒယ်တစ်ခုကို လေ့ကျင့်ဖို့အတွက် အဓိက ချဉ်းကပ်ပုံနှစ်မျိုးရှိပါတယ်။ + +1. **Masked language modeling (MLM)**: BERT လို encoder မော်ဒယ်တွေ အသုံးပြုတဲ့ ဒီချဉ်းကပ်ပုံက input မှာရှိတဲ့ tokens အချို့ကို ကျပန်းဖုံးကွယ်ထားပြီး၊ ပတ်ဝန်းကျင်ရှိ context ပေါ် အခြေခံပြီး မူရင်း tokens တွေကို ခန့်မှန်းဖို့ မော်ဒယ်ကို လေ့ကျင့်ပေးပါတယ်။ ဒါက မော်ဒယ်ကို နှစ်လမ်းသွား context (ဖုံးကွယ်ထားတဲ့ စကားလုံးရဲ့ အရှေ့နဲ့ အနောက် နှစ်ဖက်လုံးက စကားလုံးတွေကို ကြည့်ခြင်း) ကို သင်ယူနိုင်စေပါတယ်။ + +2. **Causal language modeling (CLM)**: GPT လို decoder မော်ဒယ်တွေ အသုံးပြုတဲ့ ဒီချဉ်းကပ်ပုံကတော့ sequence ထဲမှာရှိတဲ့ အရင် tokens အားလုံးပေါ် အခြေခံပြီး နောက် token ကို ခန့်မှန်းပါတယ်။ မော်ဒယ်ဟာ နောက် token ကို ခန့်မှန်းဖို့ ဘယ်ဘက် (အရင် tokens) က context ကိုသာ အသုံးပြုနိုင်ပါတယ်။ + +### ဘာသာစကားမော်ဒယ် အမျိုးအစားများ[[types-of-language-models]] + +Transformers library မှာ ဘာသာစကားမော်ဒယ်တွေဟာ အများအားဖြင့် architectural categories သုံးမျိုးအောက်မှာ ရှိပါတယ်။ + +1. **Encoder-only models** (BERT ကဲ့သို့): ဒီမော်ဒယ်တွေဟာ နှစ်လမ်းသွား ချဉ်းကပ်ပုံကို အသုံးပြုပြီး context ကို နှစ်ဖက်လုံးကနေ နားလည်ပါတယ်။ ၎င်းတို့ဟာ classification, named entity recognition နဲ့ question answering လို စာသားကို နက်နက်နဲနဲ နားလည်ဖို့ လိုအပ်တဲ့ လုပ်ငန်းတာဝန်တွေအတွက် အသင့်တော်ဆုံး ဖြစ်ပါတယ်။ + +2. **Decoder-only models** (GPT, Llama ကဲ့သို့): ဒီမော်ဒယ်တွေဟာ စာသားကို ဘယ်မှညာသို့ လုပ်ဆောင်ပြီး text generation လုပ်ငန်းတာဝန်တွေမှာ အထူးကောင်းမွန်ပါတယ်။ ၎င်းတို့ဟာ စာကြောင်းတွေ ဖြည့်စွက်တာ၊ စာစီစာကုံးရေးတာ ဒါမှမဟုတ် prompt ပေါ် အခြေခံပြီး code ရေးတာမျိုးတွေတောင် လုပ်နိုင်ပါတယ်။ + +3. **Encoder-decoder models** (T5, BART ကဲ့သို့): ဒီမော်ဒယ်တွေဟာ ချဉ်းကပ်ပုံနှစ်မျိုးလုံးကို ပေါင်းစပ်ထားပါတယ်။ input ကို နားလည်ဖို့ encoder ကို အသုံးပြုပြီး output ကို ထုတ်ပေးဖို့ decoder ကို အသုံးပြုပါတယ်။ ၎င်းတို့ဟာ translation, summarization နဲ့ question answering လို sequence-to-sequence လုပ်ငန်းတာဝန်တွေမှာ ထူးချွန်ပါတယ်။ + +![transformer-models-for-language](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers_architecture.png) + +အရင်အခန်းမှာ ဆွေးနွေးခဲ့သလိုပဲ ဘာသာစကားမော်ဒယ်တွေကို များပြားလှတဲ့ စာသားဒေတာတွေနဲ့ self-supervised နည်းလမ်း (human annotations မပါဘဲ) နဲ့ ကြိုတင်လေ့ကျင့်လေ့ရှိပြီး၊ ပြီးမှ သီးခြားလုပ်ငန်းတာဝန်တွေအတွက် fine-tune လုပ်ပါတယ်။ transfer learning လို့ခေါ်တဲ့ ဒီချဉ်းကပ်ပုံက မော်ဒယ်တွေကို သီးခြားလုပ်ငန်းတာဝန်အတွက် နည်းပါးတဲ့ ဒေတာပမာဏနဲ့ မတူညီတဲ့ NLP လုပ်ငန်းတာဝန်များစွာကို လိုက်လျောညီထွေဖြစ်အောင် လုပ်ဆောင်နိုင်စေပါတယ်။ + +အောက်ပါအခန်းတွေမှာတော့ သီးခြားမော်ဒယ် architecture တွေကို ဘယ်လိုအသုံးပြုပြီး speech, vision နဲ့ text domains တွေတစ်လျှောက် လုပ်ငန်းတာဝန်အမျိုးမျိုးကို ဘယ်လိုဖြေရှင်းတယ်ဆိုတာကို လေ့လာသွားပါမယ်။ + +> [!TIP] +> Transformer architecture ရဲ့ ဘယ်အပိုင်း (encoder, decoder ဒါမှမဟုတ် နှစ်ခုလုံး) က သီးခြား NLP လုပ်ငန်းတာဝန်တစ်ခုအတွက် အသင့်တော်ဆုံးလဲဆိုတာ နားလည်ထားတာဟာ မှန်ကန်တဲ့မော်ဒယ်ကို ရွေးချယ်ဖို့ အဓိကကျပါတယ်။ ယေဘုယျအားဖြင့် နှစ်လမ်းသွား context လိုအပ်တဲ့ လုပ်ငန်းတာဝန်တွေက encoders ကို အသုံးပြုပြီး၊ text generate လုပ်တဲ့ လုပ်ငန်းတာဝန်တွေက decoders ကို အသုံးပြုကာ၊ sequence တစ်ခုကို နောက် sequence တစ်ခုသို့ ပြောင်းလဲတဲ့ လုပ်ငန်းတာဝန်တွေကတော့ encoder-decoders ကို အသုံးပြုပါတယ်။ + +### စာသား ဖန်တီးခြင်း (Text generation)[[text-generation]] + +စာသား ဖန်တီးခြင်းဆိုတာက prompt ဒါမှမဟုတ် input တစ်ခုအပေါ် အခြေခံပြီး ဆက်စပ်မှုရှိတဲ့ စာသားတွေကို ဖန်တီးတာကို ဆိုလိုပါတယ်။ + +[GPT-2](https://huggingface.co/docs/transformers/model_doc/gpt2) ဟာ များပြားလှတဲ့ စာသားတွေနဲ့ ကြိုတင်လေ့ကျင့်ထားတဲ့ decoder-only မော်ဒယ်တစ်ခုပါ။ ၎င်းဟာ prompt တစ်ခုပေးထားရင် ယုံကြည်နိုင်လောက်တဲ့ (အမြဲတမ်းတော့ မဟုတ်ဘူး) စာသားတွေကို ဖန်တီးနိုင်ပြီး၊ မေးခွန်းဖြေတာလို အခြား NLP လုပ်ငန်းတာဝန်တွေကိုလည်း ရှင်းရှင်းလင်းလင်း လေ့ကျင့်ထားခြင်းမရှိဘဲ လုပ်ဆောင်နိုင်ပါတယ်။ + +
+ +
+ +1. GPT-2 ဟာ စကားလုံးတွေကို tokenize လုပ်ပြီး token embedding တွေ ထုတ်ပေးဖို့အတွက် [byte pair encoding (BPE)](https://huggingface.co/docs/transformers/tokenizer_summary#bytepair-encoding-bpe) ကို အသုံးပြုပါတယ်။ sequence ထဲမှာ token တစ်ခုချင်းစီရဲ့ နေရာကို ပြသဖို့ positional encodings တွေကို token embeddings တွေမှာ ထပ်ထည့်ပါတယ်။ input embeddings တွေကို decoder blocks အများအပြားကနေတဆင့် ဖြတ်သန်းပြီး final hidden state အချို့ကို ထုတ်ပေးပါတယ်။ decoder block တစ်ခုစီအတွင်းမှာ GPT-2 က *masked self-attention* layer ကို အသုံးပြုပါတယ်။ ဆိုလိုတာက GPT-2 ဟာ နောက်လာမယ့် tokens တွေကို မကြည့်နိုင်ပါဘူး။ ဘယ်ဘက် (အရင် tokens) က tokens တွေကိုသာ ကြည့်ခွင့်ရှိပါတယ်။ ဒါက BERT ရဲ့ [`mask`] token နဲ့ မတူပါဘူး။ ဘာလို့လဲဆိုတော့ masked self-attention မှာ future tokens တွေအတွက် score ကို `0` သတ်မှတ်ဖို့ attention mask ကို အသုံးပြုထားလို့ပါ။ + +2. Decoder ကနေ ထွက်လာတဲ့ output ကို language modeling head ကို ပေးပို့ပါတယ်။ အဲဒီကနေ linear transformation တစ်ခုကို လုပ်ဆောင်ပြီး hidden states တွေကို logits အဖြစ် ပြောင်းလဲပေးပါတယ်။ label ကတော့ sequence ထဲမှာရှိတဲ့ နောက် token ဖြစ်ပြီး logits တွေကို ညာဘက်သို့ တစ်နေရာ ရွှေ့ခြင်းဖြင့် ဖန်တီးပါတယ်။ shifted logits တွေနဲ့ labels တွေကြားက cross-entropy loss ကို တွက်ချက်ပြီး နောက်လာမယ့် အဖြစ်နိုင်ဆုံး token ကို ထုတ်ပေးပါတယ်။ + +GPT-2 ရဲ့ pretraining ရည်ရွယ်ချက်က [causal language modeling](https://huggingface.co/docs/transformers/glossary#causal-language-modeling) ပေါ် အခြေခံပြီး sequence ထဲက နောက်စကားလုံးကို ခန့်မှန်းတာပါ။ ဒါက GPT-2 ကို စာသားဖန်တီးခြင်းနဲ့ ပတ်သက်တဲ့ လုပ်ငန်းတာဝန်တွေမှာ အထူးကောင်းမွန်စေပါတယ်။ + +စာသား ဖန်တီးခြင်းကို ကိုယ်တိုင်စမ်းကြည့်ဖို့ အဆင်သင့်ဖြစ်ပြီလား။ DistilGPT-2 ကို ဘယ်လို fine-tune လုပ်ပြီး inference အတွက် ဘယ်လိုအသုံးပြုရမလဲဆိုတာ လေ့လာဖို့ ကျွန်တော်တို့ရဲ့ ပြည့်စုံတဲ့ [causal language modeling guide](https://huggingface.co/docs/transformers/tasks/language_modeling#causal-language-modeling) ကို ကြည့်ရှုပါ။ + +> [!TIP] +> စာသား ဖန်တီးခြင်းနဲ့ ပတ်သက်တဲ့ အချက်အလက်အများကြီးအတွက် [text generation strategies](generation_strategies) guide ကို ကြည့်ရှုပါ။ + +### စာသား အမျိုးအစားခွဲခြားခြင်း (Text classification)[[text-classification]] + +စာသား အမျိုးအစားခွဲခြားခြင်းဆိုတာ စာသားမှတ်တမ်းတွေကို ကြိုတင်သတ်မှတ်ထားတဲ့ အမျိုးအစားတွေ (ဥပမာ- sentiment analysis, topic classification, spam detection) သို့ သတ်မှတ်ပေးတာကို ဆိုလိုပါတယ်။ + +[BERT](https://huggingface.co/docs/transformers/model_doc/bert) ဟာ encoder-only မော်ဒယ်တစ်ခုဖြစ်ပြီး စာသားကို နှစ်ဖက်စလုံးက စကားလုံးတွေကို ကြည့်ရှုခြင်းဖြင့် ပိုမိုကြွယ်ဝတဲ့ ကိုယ်စားပြုမှု (representations) တွေကို သင်ယူဖို့အတွက် deep bidirectionality ကို ထိရောက်စွာ အကောင်အထည်ဖော်ခဲ့တဲ့ ပထမဆုံးမော်ဒယ် ဖြစ်ပါတယ်။ + +1. BERT ဟာ စာသားရဲ့ token embedding ကို ထုတ်ပေးဖို့အတွက် [WordPiece](https://huggingface.co/docs/transformers/tokenizer_summary#wordpiece) tokenization ကို အသုံးပြုပါတယ်။ စာကြောင်းတစ်ကြောင်းနဲ့ စာကြောင်းနှစ်ကြောင်းရဲ့ ကွာခြားချက်ကို ပြောပြဖို့အတွက် အထူး `[SEP]` token တစ်ခုကို ခွဲခြားဖို့ ထပ်ထည့်ပါတယ်။ sequence of text တိုင်းရဲ့ အစမှာ အထူး `[CLS]` token တစ်ခုကို ထပ်ထည့်ပါတယ်။ `[CLS]` token ပါတဲ့ နောက်ဆုံး output ကို classification လုပ်ငန်းတာဝန်တွေအတွက် classification head ရဲ့ input အဖြစ် အသုံးပြုပါတယ်။ BERT ဟာ token တစ်ခုက စာကြောင်းတစ်စုံမှာ ပထမစာကြောင်း ဒါမှမဟုတ် ဒုတိယစာကြောင်းမှာ ပါဝင်တယ်ဆိုတာကို ဖော်ပြဖို့ segment embedding တစ်ခုကိုလည်း ထပ်ထည့်ပါတယ်။ + +2. BERT ကို masked language modeling နဲ့ next-sentence prediction ဆိုတဲ့ ရည်ရွယ်ချက်နှစ်ခုနဲ့ ကြိုတင်လေ့ကျင့်ထားပါတယ်။ masked language modeling မှာ input tokens အချို့ ရာခိုင်နှုန်းကို ကျပန်းဖုံးကွယ်ထားပြီး မော်ဒယ်က ဒါတွေကို ခန့်မှန်းဖို့ လိုပါတယ်။ ဒါက မော်ဒယ်က စကားလုံးအားလုံးကို မြင်ပြီး နောက်စကားလုံးကို "ခန့်မှန်း" နိုင်တဲ့ bidirectionality ပြဿနာကို ဖြေရှင်းပေးပါတယ်။ ခန့်မှန်းထားတဲ့ masked tokens တွေရဲ့ final hidden states တွေကို feedforward network တစ်ခုကို ပေးပို့ပြီး vocabulary ပေါ်က softmax နဲ့ ဖုံးကွယ်ထားတဲ့ စကားလုံးကို ခန့်မှန်းပါတယ်။ + + ဒုတိယ pretraining object က next-sentence prediction ဖြစ်ပါတယ်။ မော်ဒယ်ဟာ စာကြောင်း B က စာကြောင်း A နောက်က လိုက်သလားဆိုတာကို ခန့်မှန်းရပါမယ်။ အချိန်ရဲ့ ထက်ဝက်မှာ စာကြောင်း B က နောက်လာမယ့် စာကြောင်းဖြစ်ပြီး ကျန်ထက်ဝက်မှာတော့ စာကြောင်း B က ကျပန်းစာကြောင်းတစ်ကြောင်း ဖြစ်ပါတယ်။ နောက်လာမယ့် စာကြောင်းဟုတ်မဟုတ်ဆိုတဲ့ ခန့်မှန်းချက်ကို feedforward network တစ်ခုကို ပေးပို့ပြီး class နှစ်ခု (`IsNext` နဲ့ `NotNext`) ပေါ်က softmax နဲ့ တွက်ချက်ပါတယ်။ + +3. input embeddings တွေကို encoder layers အများအပြားကနေတဆင့် ဖြတ်သန်းပြီး final hidden states အချို့ကို ထုတ်ပေးပါတယ်။ + +ကြိုတင်လေ့ကျင့်ထားတဲ့ မော်ဒယ်ကို စာသား အမျိုးအစားခွဲခြားခြင်းအတွက် အသုံးပြုဖို့အတွက် base BERT မော်ဒယ်ရဲ့ ထိပ်မှာ sequence classification head တစ်ခုကို ထပ်ထည့်ရပါမယ်။ sequence classification head က linear layer တစ်ခုဖြစ်ပြီး final hidden states တွေကို လက်ခံကာ linear transformation တစ်ခုကို လုပ်ဆောင်ပြီး ၎င်းတို့ကို logits အဖြစ် ပြောင်းလဲပေးပါတယ်။ logits တွေနဲ့ target တွေကြားက cross-entropy loss ကို တွက်ချက်ပြီး အဖြစ်နိုင်ဆုံး label ကို ရှာဖွေပါတယ်။ + +စာသား အမျိုးအစားခွဲခြားခြင်းကို ကိုယ်တိုင်စမ်းကြည့်ဖို့ အဆင်သင့်ဖြစ်ပြီလား။ DistilBERT ကို ဘယ်လို fine-tune လုပ်ပြီး inference အတွက် ဘယ်လိုအသုံးပြုရမလဲဆိုတာ လေ့လာဖို့ ကျွန်တော်တို့ရဲ့ ပြည့်စုံတဲ့ [text classification guide](https://huggingface.co/docs/transformers/tasks/sequence_classification) ကို ကြည့်ရှုပါ။ + +### Token classification[[token-classification]] + +Token classification ဆိုတာ sequence တစ်ခုစီရှိ token တစ်ခုစီကို label တစ်ခု သတ်မှတ်ပေးတာကို ဆိုလိုပါတယ်။ ဥပမာအားဖြင့် named entity recognition သို့မဟုတ် part-of-speech tagging တို့ ဖြစ်ပါတယ်။ + +BERT ကို named entity recognition (NER) လို token classification လုပ်ငန်းတာဝန်တွေအတွက် အသုံးပြုဖို့အတွက် base BERT မော်ဒယ်ရဲ့ ထိပ်မှာ token classification head တစ်ခုကို ထပ်ထည့်ရပါမယ်။ token classification head က linear layer တစ်ခုဖြစ်ပြီး final hidden states တွေကို လက်ခံကာ linear transformation တစ်ခုကို လုပ်ဆောင်ပြီး ၎င်းတို့ကို logits အဖြစ် ပြောင်းလဲပေးပါတယ်။ logits တွေနဲ့ token တစ်ခုစီကြားက cross-entropy loss ကို တွက်ချက်ပြီး အဖြစ်နိုင်ဆုံး label ကို ရှာဖွေပါတယ်။ + +token classification ကို ကိုယ်တိုင်စမ်းကြည့်ဖို့ အဆင်သင့်ဖြစ်ပြီလား။ DistilBERT ကို ဘယ်လို fine-tune လုပ်ပြီး inference အတွက် ဘယ်လိုအသုံးပြုရမလဲဆိုတာ လေ့လာဖို့ ကျွန်တော်တို့ရဲ့ ပြည့်စုံတဲ့ [token classification guide](https://huggingface.co/docs/transformers/tasks/token_classification) ကို ကြည့်ရှုပါ။ + +### မေးခွန်းဖြေခြင်း (Question answering)[[question-answering]] + +မေးခွန်းဖြေခြင်းဆိုတာက ပေးထားတဲ့ context ဒါမှမဟုတ် စာပိုဒ်တစ်ခုအတွင်းမှာ မေးခွန်းရဲ့အဖြေကို ရှာဖွေတာကို ဆိုလိုပါတယ်။ + +BERT ကို မေးခွန်းဖြေခြင်းအတွက် အသုံးပြုဖို့အတွက် base BERT မော်ဒယ်ရဲ့ ထိပ်မှာ span classification head တစ်ခုကို ထပ်ထည့်ရပါမယ်။ ဒီ linear layer က final hidden states တွေကို လက်ခံကာ linear transformation တစ်ခုကို လုပ်ဆောင်ပြီး အဖြေနဲ့ ကိုက်ညီတဲ့ `span` start နဲ့ end logits တွေကို တွက်ချက်ပေးပါတယ်။ logits တွေနဲ့ label position တွေကြားက cross-entropy loss ကို တွက်ချက်ပြီး အဖြေနဲ့ ကိုက်ညီတဲ့ အဖြစ်နိုင်ဆုံး စာသားအပိုင်းကို ရှာဖွေပါတယ်။ + +မေးခွန်းဖြေခြင်းကို ကိုယ်တိုင်စမ်းကြည့်ဖို့ အဆင်သင့်ဖြစ်ပြီလား။ DistilBERT ကို ဘယ်လို fine-tune လုပ်ပြီး inference အတွက် ဘယ်လိုအသုံးပြုရမလဲဆိုတာ လေ့လာဖို့ ကျွန်တော်တို့ရဲ့ ပြည့်စုံတဲ့ [question answering guide](https://huggingface.co/docs/transformers/tasks/question_answering) ကို ကြည့်ရှုပါ။ + +> [!TIP] +> 💡 BERT ကို ကြိုတင်လေ့ကျင့်ပြီးတာနဲ့ မတူညီတဲ့ လုပ်ငန်းတာဝန်တွေအတွက် အသုံးပြုဖို့ ဘယ်လောက်လွယ်ကူလဲဆိုတာ သတိထားမိလား။ သင်လိုချင်တဲ့ output ကို ရရှိဖို့အတွက် ကြိုတင်လေ့ကျင့်ထားတဲ့ မော်ဒယ်ရဲ့ ထိပ်မှာ သီးခြား head တစ်ခုကို ထပ်ထည့်ဖို့ပဲ လိုအပ်ပါတယ်။ + +### အနှစ်ချုပ်ခြင်း (Summarization)[[summarization]] + +အနှစ်ချုပ်ခြင်းဆိုတာက ပိုရှည်တဲ့ စာသားတစ်ခုကို အဓိကအချက်အလက်တွေနဲ့ အဓိပ္ပာယ်ကို ထိန်းသိမ်းထားရင်း ပိုတိုတဲ့ပုံစံအဖြစ် ပြောင်းလဲတာကို ဆိုလိုပါတယ်။ + +[BART](https://huggingface.co/docs/transformers/model_doc/bart) နဲ့ [T5](model_doc/t5) လို encoder-decoder မော်ဒယ်တွေကို summarization လုပ်ငန်းတာဝန်ရဲ့ sequence-to-sequence ပုံစံအတွက် ဒီဇိုင်းထုတ်ထားပါတယ်။ ဒီအပိုင်းမှာ BART ဘယ်လိုအလုပ်လုပ်လဲဆိုတာကို ရှင်းပြပြီး၊ ပြီးရင် T5 ကို fine-tune လုပ်တာကို သင် ကိုယ်တိုင် စမ်းကြည့်နိုင်ပါတယ်။ + +
+ +
+ +1. BART ရဲ့ encoder architecture က BERT နဲ့ အတော်လေး ဆင်တူပြီး စာသားရဲ့ token နဲ့ positional embedding ကို လက်ခံပါတယ်။ BART ကို input ကို ဖျက်စီးပြီး decoder နဲ့ ပြန်လည်တည်ဆောက်ခြင်းဖြင့် ကြိုတင်လေ့ကျင့်ထားပါတယ်။ သီးခြား corruption strategies တွေပါတဲ့ အခြား encoders တွေနဲ့မတူဘဲ BART က ဘယ်လို corruption အမျိုးအစားမဆို အသုံးပြုနိုင်ပါတယ်။ သို့သော် *text infilling* corruption strategy က အကောင်းဆုံး အလုပ်လုပ်ပါတယ်။ text infilling မှာ စာသားအပိုင်းအချို့ကို **တစ်ခုတည်းသော** [`mask`] token နဲ့ အစားထိုးပါတယ်။ ဒါက အရေးကြီးပါတယ်၊ ဘာလို့လဲဆိုတော့ မော်ဒယ်က ဖုံးကွယ်ထားတဲ့ tokens တွေကို ခန့်မှန်းရမှာဖြစ်ပြီး၊ ပျောက်ဆုံးနေတဲ့ tokens အရေအတွက်ကို ခန့်မှန်းဖို့ မော်ဒယ်ကို သင်ကြားပေးပါတယ်။ input embeddings နဲ့ masked spans တွေကို encoder ကနေတဆင့် ဖြတ်သန်းပြီး final hidden states အချို့ကို ထုတ်ပေးပါတယ်။ ဒါပေမယ့် BERT နဲ့မတူဘဲ BART က စကားလုံးတစ်လုံးကို ခန့်မှန်းဖို့ နောက်ဆုံး feedforward network ကို ထပ်ထည့်ထားခြင်း မရှိပါဘူး။ + +2. encoder ရဲ့ output ကို decoder ကို ပေးပို့ပါတယ်။ decoder က ဖုံးကွယ်ထားတဲ့ tokens တွေနဲ့ encoder ရဲ့ output ကနေ uncorrupted tokens တွေကို ခန့်မှန်းရပါမယ်။ ဒါက decoder ကို မူရင်းစာသားကို ပြန်လည်တည်ဆောက်ဖို့ အပို context တွေ ပေးပါတယ်။ decoder ကနေ ထွက်လာတဲ့ output ကို language modeling head ကို ပေးပို့ပါတယ်။ အဲဒီကနေ linear transformation တစ်ခုကို လုပ်ဆောင်ပြီး hidden states တွေကို logits အဖြစ် ပြောင်းလဲပေးပါတယ်။ logits တွေနဲ့ label (ညာဘက်သို့ ရွှေ့ထားတဲ့ token) ကြားက cross-entropy loss ကို တွက်ချက်ပါတယ်။ + +အနှစ်ချုပ်ခြင်းကို ကိုယ်တိုင်စမ်းကြည့်ဖို့ အဆင်သင့်ဖြစ်ပြီလား။ T5 ကို ဘယ်လို fine-tune လုပ်ပြီး inference အတွက် ဘယ်လိုအသုံးပြုရမလဲဆိုတာ လေ့လာဖို့ ကျွန်တော်တို့ရဲ့ ပြည့်စုံတဲ့ [summarization guide](https://huggingface.co/docs/transformers/tasks/summarization) ကို ကြည့်ရှုပါ။ + +> [!TIP] +> စာသား ဖန်တီးခြင်းနဲ့ ပတ်သက်တဲ့ အချက်အလက်အများကြီးအတွက် [text generation strategies](https://huggingface.co/docs/transformers/generation_strategies) guide ကို ကြည့်ရှုပါ။ + +### ဘာသာပြန်ခြင်း (Translation)[[translation]] + +ဘာသာပြန်ခြင်းဆိုတာ စာသားတစ်ခုကို အခြားဘာသာစကားတစ်ခုသို့ အဓိပ္ပာယ်ကို ထိန်းသိမ်းထားရင်း ပြောင်းလဲတာကို ဆိုလိုပါတယ်။ ဘာသာပြန်ခြင်းက sequence-to-sequence လုပ်ငန်းတာဝန်တစ်ခုရဲ့ နောက်ထပ်ဥပမာတစ်ခု ဖြစ်ပါတယ်။ ဆိုလိုတာက [BART](https://huggingface.co/docs/transformers/model_doc/bart) ဒါမှမဟုတ် [T5](model_doc/t5) လို encoder-decoder မော်ဒယ်ကို အသုံးပြုနိုင်ပါတယ်။ ဒီအပိုင်းမှာ BART ဘယ်လိုအလုပ်လုပ်လဲဆိုတာကို ရှင်းပြပြီး၊ ပြီးရင် T5 ကို fine-tune လုပ်တာကို သင် ကိုယ်တိုင် စမ်းကြည့်နိုင်ပါတယ်။ + +BART ဟာ source ဘာသာစကားတစ်ခုကို target ဘာသာစကားသို့ decode လုပ်နိုင်တဲ့ input အဖြစ် map လုပ်ဖို့အတွက် သီးခြား၊ ကျပန်းစတင်ထားတဲ့ encoder တစ်ခုကို ထပ်ထည့်ခြင်းဖြင့် ဘာသာပြန်ခြင်းကို လိုက်လျောညီထွေဖြစ်အောင် လုပ်ဆောင်ပါတယ်။ ဒီ encoder အသစ်ရဲ့ embeddings တွေကို မူရင်း word embeddings အစား ကြိုတင်လေ့ကျင့်ထားတဲ့ encoder ကို ပေးပို့ပါတယ်။ source encoder ကို မော်ဒယ် output ကနေ cross-entropy loss နဲ့ source encoder, positional embeddings နဲ့ input embeddings တွေကို update လုပ်ခြင်းဖြင့် လေ့ကျင့်ပေးပါတယ်။ ဒီပထမအဆင့်မှာ မော်ဒယ် parameters တွေကို freeze ထားပြီး၊ ဒုတိယအဆင့်မှာတော့ မော်ဒယ် parameters အားလုံးကို အတူတကွ လေ့ကျင့်ပေးပါတယ်။ BART ကိုတော့ ဘာသာပြန်ခြင်းအတွက် ရည်ရွယ်ပြီး မတူညီတဲ့ ဘာသာစကားများစွာနဲ့ ကြိုတင်လေ့ကျင့်ထားတဲ့ multilingual version ဖြစ်တဲ့ mBART က ဆက်ခံခဲ့ပါတယ်။ + +ဘာသာပြန်ခြင်းကို ကိုယ်တိုင်စမ်းကြည့်ဖို့ အဆင်သင့်ဖြစ်ပြီလား။ T5 ကို ဘယ်လို fine-tune လုပ်ပြီး inference အတွက် ဘယ်လိုအသုံးပြုရမလဲဆိုတာ လေ့လာဖို့ ကျွန်တော်တို့ရဲ့ ပြည့်စုံတဲ့ [translation guide](https://huggingface.co/docs/transformers/tasks/translation) ကို ကြည့်ရှုပါ။ + +> [!TIP] +> ဒီ guide တစ်လျှောက်လုံးမှာ သင်တွေ့ခဲ့ရတဲ့အတိုင်း မော်ဒယ်များစွာဟာ မတူညီတဲ့ လုပ်ငန်းတာဝန်တွေကို ဖြေရှင်းနေရရင်တောင် အလားတူ ပုံစံတွေကို လိုက်နာကြပါတယ်။ ဒီလို အခြေခံပုံစံတွေကို နားလည်ထားတာက မော်ဒယ်အသစ်တွေ ဘယ်လိုအလုပ်လုပ်လဲဆိုတာကို အမြန်နားလည်ဖို့နဲ့ ရှိပြီးသားမော်ဒယ်တွေကို သင်ရဲ့လိုအပ်ချက်တွေနဲ့ လိုက်လျောညီထွေဖြစ်အောင် လုပ်ဆောင်ဖို့ ကူညီပေးနိုင်ပါတယ်။ + +## စာသားပြင်ပရှိ Modalities များ[[modalities-beyond-text]] + +Transformers တွေဟာ စာသားအတွက်သာ ကန့်သတ်ထားတာ မဟုတ်ပါဘူး။ ၎င်းတို့ကို speech and audio, images နဲ့ video လို အခြား modalities တွေမှာလည်း အသုံးပြုနိုင်ပါတယ်။ ဒီသင်တန်းမှာတော့ ကျွန်တော်တို့က စာသားကို အဓိကထားမှာဖြစ်ပေမယ့် အခြား modalities တွေကို အတိုချုပ် မိတ်ဆက်ပေးပါမယ်။ + +### စကားပြောနှင့် အသံ (Speech and audio)[[speech-and-audio]] + +Transformer မော်ဒယ်တွေက စာသား ဒါမှမဟုတ် ပုံတွေနဲ့ယှဉ်ရင် ထူးခြားတဲ့ စိန်ခေါ်မှုတွေရှိတဲ့ speech နဲ့ audio data တွေကို ဘယ်လိုကိုင်တွယ်လဲဆိုတာကို စလေ့လာရအောင်။ + +[Whisper](https://huggingface.co/docs/transformers/main/en/model_doc/whisper) ဟာ 680,000 နာရီကြာ မှတ်သားထားတဲ့ audio data တွေနဲ့ ကြိုတင်လေ့ကျင့်ထားတဲ့ encoder-decoder (sequence-to-sequence) transformer တစ်ခုဖြစ်ပါတယ်။ ဒီလိုများပြားတဲ့ pretraining data ပမာဏက English နဲ့ အခြားဘာသာစကားများစွာရှိ audio လုပ်ငန်းတာဝန်တွေမှာ zero-shot performance ကို ရရှိစေပါတယ်။ decoder က Whisper ကို encoders တွေ သင်ယူထားတဲ့ speech representations တွေကို စာသားလို အသုံးဝင်တဲ့ outputs တွေအဖြစ် ထပ်မံ fine-tune လုပ်စရာမလိုဘဲ map လုပ်နိုင်စေပါတယ်။ Whisper က box ထဲကနေ တန်းအလုပ်လုပ်နိုင်ပါတယ်။ + +
+ +
+ +ပုံကြမ်းကို [Whisper paper](https://huggingface.co/papers/2212.04356) မှ ရယူထားပါသည်။ + +ဒီမော်ဒယ်မှာ အဓိက အစိတ်အပိုင်းနှစ်ခု ပါဝင်ပါတယ်။ + +1. **Encoder**: input audio ကို လုပ်ဆောင်ပေးပါတယ်။ ကနဦး audio ကို log-Mel spectrogram အဖြစ် ပြောင်းလဲပါတယ်။ ဒီ spectrogram ကို Transformer encoder network ကနေတဆင့် ဖြတ်သန်းပါတယ်။ + +2. **Decoder**: encoded audio representation ကို ယူပြီး သက်ဆိုင်ရာ text tokens တွေကို autoregressively ခန့်မှန်းပါတယ်။ ဒါဟာ အရင် tokens တွေနဲ့ encoder output ကို ပေးထားပြီး နောက် text token ကို ခန့်မှန်းဖို့ လေ့ကျင့်ထားတဲ့ standard Transformer decoder တစ်ခုပါ။ transcription, translation ဒါမှမဟုတ် language identification လို သီးခြားလုပ်ငန်းတာဝန်တွေဆီ မော်ဒယ်ကို ဦးတည်စေဖို့ decoder input ရဲ့ အစမှာ အထူး tokens တွေကို အသုံးပြုပါတယ်။ + +Whisper ကို ဝက်ဘ်မှ စုဆောင်းထားတဲ့ 680,000 နာရီကြာ မှတ်သားထားတဲ့ audio data များစွာနဲ့ မတူညီတဲ့ dataset တစ်ခုပေါ်မှာ ကြိုတင်လေ့ကျင့်ထားပါတယ်။ ဒီလို ကြီးမားတဲ့၊ weakly supervised pretraining ဟာ မတူညီတဲ့ ဘာသာစကားတွေ၊ လေယူလေသိမ်းတွေနဲ့ လုပ်ငန်းတာဝန်တွေမှာ task-specific finetuning မပါဘဲ အစွမ်းထက်တဲ့ zero-shot performance ကို ရရှိစေတဲ့ အဓိကအချက်ပါ။ + +Whisper ကို ကြိုတင်လေ့ကျင့်ပြီးပြီဆိုတော့ zero-shot inference အတွက် တိုက်ရိုက်အသုံးပြုနိုင်သလို automatic speech recognition ဒါမှမဟုတ် speech translation လို သီးခြားလုပ်ငန်းတာဝန်တွေမှာ စွမ်းဆောင်ရည်ပိုမိုကောင်းမွန်စေဖို့ သင်ရဲ့ data ပေါ်မှာ fine-tune လုပ်နိုင်ပါပြီ။ + +> [!TIP] +> Whisper ရဲ့ အဓိက ဆန်းသစ်တီထွင်မှုကတော့ အင်တာနက်ကနေ ရရှိတဲ့ မတူညီတဲ့၊ weakly supervised audio data တွေကို အစဉ်အလာမရှိတဲ့ ပမာဏနဲ့ လေ့ကျင့်ထားခြင်း ဖြစ်ပါတယ်။ ဒါက မတူညီတဲ့ ဘာသာစကားတွေ၊ လေယူလေသိမ်းတွေနဲ့ လုပ်ငန်းတာဝန်တွေဆီကို task-specific finetuning မပါဘဲ ထူးထူးခြားခြား ကောင်းမွန်စွာ ယေဘုယျလုပ်ဆောင်နိုင်စေပါတယ်။ + +### အလိုအလျောက် စကားပြော မှတ်သားခြင်း (Automatic speech recognition)[[automatic-speech-recognition]] + +ကြိုတင်လေ့ကျင့်ထားတဲ့ မော်ဒယ်ကို automatic speech recognition အတွက် အသုံးပြုဖို့အတွက် ၎င်းရဲ့ ပြည့်စုံတဲ့ encoder-decoder ဖွဲ့စည်းပုံကို အသုံးချရပါမယ်။ encoder က audio input ကို လုပ်ဆောင်ပြီး decoder ကတော့ text token တစ်ခုချင်းစီကို autoregressively ထုတ်ပေးပါတယ်။ fine-tuning လုပ်တဲ့အခါ မော်ဒယ်ကို audio input ပေါ် အခြေခံပြီး မှန်ကန်တဲ့ text tokens တွေကို ခန့်မှန်းဖို့အတွက် standard sequence-to-sequence loss (cross-entropy ကဲ့သို့) ကို အသုံးပြုပြီး လေ့ကျင့်လေ့ရှိပါတယ်။ + +Fine-tuned model ကို inference အတွက် အသုံးပြုဖို့ အလွယ်ဆုံးနည်းလမ်းကတော့ `pipeline` အတွင်းမှာပဲ ဖြစ်ပါတယ်။ + +```python +from transformers import pipeline + +transcriber = pipeline( + task="automatic-speech-recognition", model="openai/whisper-base.en" +) +transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +# Output: {'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + +Automatic speech recognition ကို ကိုယ်တိုင်စမ်းကြည့်ဖို့ အဆင်သင့်ဖြစ်ပြီလား။ Whisper ကို ဘယ်လို fine-tune လုပ်ပြီး inference အတွက် ဘယ်လိုအသုံးပြုရမလဲဆိုတာ လေ့လာဖို့ ကျွန်တော်တို့ရဲ့ ပြည့်စုံတဲ့ [automatic speech recognition guide](https://huggingface.co/docs/transformers/tasks/asr) ကို ကြည့်ရှုပါ။ + +### Computer vision[[computer-vision]] + +အခုတော့ computer vision လုပ်ငန်းတာဝန်တွေဆီ ဆက်သွားရအောင်။ ဒါတွေကတော့ ပုံတွေ ဒါမှမဟုတ် ဗီဒီယိုတွေကနေ မြင်နိုင်တဲ့ အချက်အလက်တွေကို နားလည်ပြီး အနက်ပြန်ခြင်းနဲ့ သက်ဆိုင်ပါတယ်။ + +computer vision လုပ်ငန်းတာဝန်တွေကို ချဉ်းကပ်ဖို့ နည်းလမ်းနှစ်မျိုးရှိပါတယ်။ + +1. ပုံတစ်ပုံကို patches အစုအဝေးအဖြစ် ခွဲခြမ်းပြီး Transformer နဲ့ တစ်ပြိုင်နက်တည်း လုပ်ဆောင်ခြင်း။ +2. convolutional layers တွေကို အသုံးပြုပေမယ့် ခေတ်မီ network designs တွေကို လက်ခံထားတဲ့ [ConvNeXT](https://huggingface.co/docs/transformers/model_doc/convnext) လို ခေတ်မီ CNN တစ်ခုကို အသုံးပြုခြင်း။ + +> [!TIP] +> တတိယချဉ်းကပ်ပုံကတော့ Transformers တွေကို convolutions တွေနဲ့ ရောစပ်ခြင်း (ဥပမာ- [Convolutional Vision Transformer](https://huggingface.co/docs/transformers/model_doc/cvt) သို့မဟုတ် [LeViT](https://huggingface.co/docs/transformers/model_doc/levit)) ဖြစ်ပါတယ်။ ဒါတွေကိုတော့ ကျွန်တော်တို့ ဆွေးနွေးမှာ မဟုတ်ပါဘူး၊ ဘာလို့လဲဆိုတော့ ၎င်းတို့ဟာ ဒီနေရာမှာ စစ်ဆေးထားတဲ့ ချဉ်းကပ်ပုံနှစ်ခုကို ပေါင်းစပ်ထားတာပဲ ဖြစ်လို့ပါ။ + +ViT နဲ့ ConvNeXT တို့နှစ်ခုလုံးကို image classification အတွက် အများအားဖြင့် အသုံးပြုပေမယ့် object detection, segmentation နဲ့ depth estimation လို အခြား vision လုပ်ငန်းတာဝန်တွေအတွက်တော့ DETR, Mask2Former နဲ့ GLPN တို့ကို အသီးသီး ကြည့်ရှုသွားပါမယ်။ ဒီမော်ဒယ်တွေကတော့ အဲဒီလုပ်ငန်းတာဝန်တွေအတွက် ပိုမိုသင့်လျော်ပါတယ်။ + +### ရုပ်ပုံ အမျိုးအစားခွဲခြားခြင်း (Image classification)[[image-classification]] + +ရုပ်ပုံ အမျိုးအစားခွဲခြားခြင်းက အခြေခံကျတဲ့ computer vision လုပ်ငန်းတာဝန်တွေထဲက တစ်ခုပါ။ မတူညီတဲ့ မော်ဒယ် architecture တွေက ဒီပြဿနာကို ဘယ်လိုချဉ်းကပ်လဲဆိုတာ ကြည့်ရအောင်။ + +ViT နဲ့ ConvNeXT တို့နှစ်ခုလုံးကို image classification အတွက် အသုံးပြုနိုင်ပါတယ်။ အဓိက ကွာခြားချက်ကတော့ ViT က attention mechanism ကို အသုံးပြုပြီး ConvNeXT က convolutions တွေကို အသုံးပြုတာပါပဲ။ + +[ViT](https://huggingface.co/docs/transformers/model_doc/vit) ဟာ convolutions တွေကို Transformer architecture သန့်သန့်နဲ့ အစားထိုးထားပါတယ်။ မူရင်း Transformer နဲ့ ရင်းနှီးပြီးသားဆိုရင် ViT ကို နားလည်ဖို့ အများကြီး ကျန်တော့မှာ မဟုတ်ပါဘူး။ + +
+ +
+ +ViT က မိတ်ဆက်ခဲ့တဲ့ အဓိကပြောင်းလဲမှုက ပုံတွေကို Transformer ကို ဘယ်လို ထည့်သွင်းလဲဆိုတာပါပဲ။ + +1. ပုံတစ်ပုံကို လေးထောင့်မကျအောင် မထပ်တဲ့ patches တွေအဖြစ် ခွဲခြမ်းပြီး၊ patch တစ်ခုစီကို vector ဒါမှမဟုတ် *patch embedding* အဖြစ် ပြောင်းလဲပါတယ်။ patch embeddings တွေကို convolutional 2D layer ကနေ ထုတ်ပေးပြီး မှန်ကန်တဲ့ input dimensions (base Transformer အတွက် patch embedding တစ်ခုစီအတွက် 768 values) ကို ဖန်တီးပေးပါတယ်။ 224x224 pixel ပုံတစ်ပုံရှိရင် 196 16x16 ပုံ patches တွေအဖြစ် ခွဲခြမ်းနိုင်ပါတယ်။ စာသားကို စကားလုံးတွေအဖြစ် tokenize လုပ်သလိုမျိုး ပုံတစ်ပုံကိုလည်း patches sequence အဖြစ် "tokenize" လုပ်ပါတယ်။ + +2. *learnable embedding* - အထူး `[CLS]` token - ကို BERT လိုပဲ patch embeddings ရဲ့ အစမှာ ထပ်ထည့်ပါတယ်။ `[CLS]` token ရဲ့ final hidden state ကို တွဲထားတဲ့ classification head ရဲ့ input အဖြစ် အသုံးပြုပြီး အခြား outputs တွေကိုတော့ လျစ်လျူရှုပါတယ်။ ဒီ token က မော်ဒယ်ကို ပုံတစ်ပုံရဲ့ representation ကို ဘယ်လို encode လုပ်ရမယ်ဆိုတာ သင်ယူဖို့ ကူညီပေးပါတယ်။ + +3. patch နဲ့ learnable embeddings တွေမှာ ထပ်ထည့်ရမယ့် နောက်ဆုံးအရာကတော့ *position embeddings* တွေ ဖြစ်ပါတယ်။ ဘာလို့လဲဆိုတော့ မော်ဒယ်က image patches တွေရဲ့ အစီအစဉ်ကို မသိလို့ပါပဲ။ position embeddings တွေကလည်း learnable ဖြစ်ပြီး patch embeddings တွေနဲ့ အရွယ်အစားတူညီပါတယ်။ နောက်ဆုံးတော့ embeddings အားလုံးကို Transformer encoder ကို ပေးပို့ပါတယ်။ + +4. output ကို၊ အထူးသဖြင့် `[CLS]` token ပါတဲ့ output ကိုပဲ multilayer perceptron head (MLP) ကို ပေးပို့ပါတယ်။ ViT ရဲ့ pretraining ရည်ရွယ်ချက်ကတော့ classification ပါပဲ။ အခြား classification heads တွေလိုပဲ MLP head က output ကို class labels တွေပေါ်က logits အဖြစ် ပြောင်းလဲပြီး အဖြစ်နိုင်ဆုံး class ကို ရှာဖွေဖို့ cross-entropy loss ကို တွက်ချက်ပါတယ်။ + +ရုပ်ပုံ အမျိုးအစားခွဲခြားခြင်းကို ကိုယ်တိုင်စမ်းကြည့်ဖို့ အဆင်သင့်ဖြစ်ပြီလား။ ViT ကို ဘယ်လို fine-tune လုပ်ပြီး inference အတွက် ဘယ်လိုအသုံးပြုရမလဲဆိုတာ လေ့လာဖို့ ကျွန်တော်တို့ရဲ့ ပြည့်စုံတဲ့ [image classification guide](https://huggingface.co/docs/transformers/tasks/image_classification) ကို ကြည့်ရှုပါ။ + + +> [!TIP] +> ViT နဲ့ BERT ကြားက တူညီမှုကို သတိထားမိပါလိမ့်မယ်။ နှစ်ခုလုံးဟာ အလုံးစုံ ကိုယ်စားပြုမှု (overall representation) ကို ဖမ်းယူဖို့ အထူး token ([CLS]) ကို အသုံးပြုကြပြီး၊ နှစ်ခုလုံးက ၎င်းတို့ရဲ့ embeddings တွေမှာ position information ကို ထပ်ထည့်ကြကာ၊ နှစ်ခုလုံးက tokens/patches တွေရဲ့ sequence ကို လုပ်ဆောင်ဖို့ Transformer encoder ကို အသုံးပြုကြပါတယ်။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Natural Language Processing (NLP)**: ကွန်ပျူတာတွေ လူသားဘာသာစကားကို နားလည်၊ အဓိပ္ပာယ်ဖော်ပြီး၊ ဖန်တီးနိုင်အောင် လုပ်ဆောင်ပေးတဲ့ Artificial Intelligence (AI) ရဲ့ နယ်ပယ်ခွဲတစ်ခု ဖြစ်ပါတယ်။ ဥပမာအားဖြင့် စာသားခွဲခြမ်းစိတ်ဖြာခြင်း၊ ဘာသာပြန်ခြင်း စသည်တို့ ပါဝင်ပါတယ်။ +* **Large Language Models (LLMs)**: လူသားဘာသာစကားကို နားလည်ပြီး ထုတ်လုပ်ပေးနိုင်တဲ့ အလွန်ကြီးမားတဲ့ Artificial Intelligence (AI) မော်ဒယ်တွေ ဖြစ်ပါတယ်။ ၎င်းတို့ဟာ ဒေတာအမြောက်အမြားနဲ့ သင်ကြားလေ့ကျင့်ထားပြီး စာရေးတာ၊ မေးခွန်းဖြေတာ စတဲ့ ဘာသာစကားဆိုင်ရာ လုပ်ငန်းမျိုးစုံကို လုပ်ဆောင်နိုင်ပါတယ်။ +* **Transformer Models**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Encoder**: Transformer Architecture ရဲ့ အစိတ်အပိုင်းတစ်ခုဖြစ်ပြီး input data (ဥပမာ- စာသား) ကို နားလည်ပြီး ကိုယ်စားပြုတဲ့ အချက်အလက် (representation) အဖြစ် ပြောင်းလဲပေးပါတယ်။ +* **Decoder**: Transformer Architecture ရဲ့ အစိတ်အပိုင်းတစ်ခုဖြစ်ပြီး encoder ကနေ ရရှိတဲ့ အချက်အလက် (representation) ကို အသုံးပြုပြီး output data (ဥပမာ- ဘာသာပြန်ထားတဲ့ စာသား) ကို ထုတ်ပေးပါတယ်။ +* **Encoder-Decoder Structure**: Encoder နှင့် Decoder နှစ်ခုစလုံး ပါဝင်သော Transformer architecture တစ်မျိုးဖြစ်ပြီး ဘာသာပြန်ခြင်းကဲ့သို့သော input sequence မှ output sequence တစ်ခုသို့ ပြောင်းလဲခြင်း လုပ်ငန်းများအတွက် အသုံးပြုပါတယ်။ +* **Architecture**: Machine Learning မော်ဒယ်တစ်ခု၏ ဒီဇိုင်း သို့မဟုတ် ဖွဲ့စည်းတည်ဆောက်ပုံ။ +* **Input Data**: မော်ဒယ်တစ်ခုကို ပေးသွင်းသည့် အချက်အလက်များ။ +* **Output**: မော်ဒယ်တစ်ခုမှ ထုတ်ပေးသော ရလဒ်များ။ +* **Predictions**: မော်ဒယ်တစ်ခုမှ ခန့်မှန်းထားသော ရလဒ်များ။ +* **Audio Classification**: အသံနမူနာများကို ကြိုတင်သတ်မှတ်ထားသော အမျိုးအစားများအဖြစ် ခွဲခြားခြင်း။ +* **Automatic Speech Recognition (ASR)**: ပြောဆိုသော ဘာသာစကားကို စာသားအဖြစ် အလိုအလျောက် ပြောင်းလဲပေးသည့် နည်းပညာ။ +* **Image Classification**: ရုပ်ပုံများကို ကြိုတင်သတ်မှတ်ထားသော အမျိုးအစားများအဖြစ် ခွဲခြားခြင်း။ +* **Object Detection**: ပုံတစ်ပုံအတွင်းရှိ အရာဝတ္ထုများကို ရှာဖွေဖော်ထုတ်ပြီး ၎င်းတို့၏ တည်နေရာကို သတ်မှတ်ခြင်း။ +* **Image Segmentation**: ပုံတစ်ပုံအတွင်းရှိ pixel များကို သီးခြားအရာဝတ္ထုများ သို့မဟုတ် ဒေသများအဖြစ် ခွဲခြားခြင်း။ +* **Depth Estimation**: ပုံတစ်ပုံအတွင်းရှိ အရာဝတ္ထုများ၏ ကင်မရာနှင့် ဝေးကွာသော အကွာအဝေးကို ခန့်မှန်းခြင်း။ +* **Text Classification**: စာသားမှတ်တမ်းများကို ကြိုတင်သတ်မှတ်ထားသော အမျိုးအစားများအဖြစ် ခွဲခြားခြင်း။ +* **Token Classification**: စာသား sequence တစ်ခုရှိ token တစ်ခုစီကို label တစ်ခု သတ်မှတ်ပေးခြင်း။ +* **Question Answering**: ပေးထားသော စာသားတစ်ခုအတွင်းမှ မေးခွန်းတစ်ခု၏ အဖြေကို ရှာဖွေခြင်း။ +* **Text Generation**: AI မော်ဒယ်များကို အသုံးပြု၍ လူသားကဲ့သို့သော စာသားအသစ်များ ဖန်တီးခြင်း။ +* **Summarization**: ရှည်လျားသော စာသားတစ်ခုကို အဓိကအချက်အလက်များနှင့် အဓိပ္ပာယ်ကို မပျက်စီးစေဘဲ တိုတောင်းအောင်ပြုလုပ်ခြင်း။ +* **Translation**: စာသားကို ဘာသာစကားတစ်ခုမှ အခြားဘာသာစကားတစ်ခုသို့ အဓိပ္ပာယ်မပျက် ဘာသာပြန်ခြင်း။ +* **Attention Mechanism**: Transformer မော်ဒယ်များတွင် အသုံးပြုသော နည်းစနစ်တစ်ခုဖြစ်ပြီး input sequence ၏ မတူညီသော အစိတ်အပိုင်းများအပေါ် အာရုံစိုက်ပြီး ဆက်နွယ်မှုများကို သင်ယူစေသည်။ +* **Language Models**: လူသားဘာသာစကားကို နားလည်ပြီး ထုတ်ပေးနိုင်ရန် ဒီဇိုင်းထုတ်ထားသော Machine Learning မော်ဒယ်များ။ +* **Tokens**: စာသားတစ်ခု၏ အသေးငယ်ဆုံးသော အစိတ်အပိုင်းများ (ဥပမာ- စကားလုံးများ၊ စာလုံးများ)။ +* **Machine Translation**: ဘာသာစကားတစ်ခုကနေ အခြားဘာသာစကားတစ်ခုကို စာသားတွေ ဒါမှမဟုတ် စကားပြောတွေကို အလိုအလျောက် ဘာသာပြန်ဆိုခြင်း။ +* **Bidirectional Context**: စာသားတစ်ခုကို စကားလုံးတစ်လုံးရဲ့ အရှေ့နဲ့ အနောက် နှစ်ဖက်လုံးကနေ ကြည့်ရှုပြီး နားလည်ခြင်း။ +* **Masked Language Modeling (MLM)**: input tokens အချို့ကို ဖုံးကွယ်ထားပြီး မော်ဒယ်ကို ၎င်းတို့ကို ခန့်မှန်းစေရန် လေ့ကျင့်သော pretraining နည်းလမ်း။ +* **Causal Language Modeling (CLM)**: input sequence ၏ အရင် tokens များပေါ် အခြေခံပြီး နောက် token ကို ခန့်မှန်းစေရန် မော်ဒယ်ကို လေ့ကျင့်သော pretraining နည်းလမ်း။ +* **Named Entity Recognition (NER)**: စာသားထဲက လူအမည်၊ နေရာအမည်၊ အဖွဲ့အစည်းအမည် စတဲ့ သီးခြားအမည်တွေကို ရှာဖွေဖော်ထုတ်ခြင်း။ +* **Part-of-Speech (POS) Tagging**: စာကြောင်းတစ်ခုရှိ စကားလုံးတစ်လုံးစီကို သက်ဆိုင်ရာ သဒ္ဒါအမျိုးအစား (ဥပမာ- နာမ်၊ ကြိယာ၊ နာမဝိသေသန) ကို သတ်မှတ်ပေးခြင်း။ +* **Self-supervised**: ဒေတာများကို လူသားများက လက်ဖြင့် မှတ်သား (annotate) ရန် မလိုအပ်ဘဲ ဒေတာကိုယ်တိုင်ကနေ သင်ယူနိုင်သော လေ့ကျင့်မှုနည်းလမ်း။ +* **Human Annotations**: လူသားများက ဒေတာများကို လက်ဖြင့် မှတ်သားခြင်း သို့မဟုတ် အညွှန်းတပ်ခြင်း။ +* **Transfer Learning**: ကြိုတင်လေ့ကျင့်ထားပြီးသား မော်ဒယ် (pre-trained model) တစ်ခုကို အခြားလုပ်ငန်းတာဝန်အသစ်တစ်ခုအတွက် ပြန်လည်အသုံးပြုခြင်း။ +* **Byte Pair Encoding (BPE)**: စာသားများကို tokens အဖြစ် ပြောင်းလဲရန် အသုံးပြုသော tokenization နည်းလမ်းတစ်ခု။ +* **Token Embedding**: tokens များကို vector ပုံစံဖြင့် ကိုယ်စားပြုခြင်း။ +* **Positional Encodings**: sequence တစ်ခုရှိ token တစ်ခုချင်းစီ၏ တည်နေရာ အချက်အလက်များကို ထပ်ထည့်ပေးခြင်း။ +* **Decoder Blocks**: Transformer decoder ၏ အစိတ်အပိုင်းများ။ +* **Masked Self-Attention**: Transformer decoder တွင် အသုံးပြုသော attention mechanism တစ်မျိုးဖြစ်ပြီး မော်ဒယ်ကို future tokens များသို့ ကြည့်ရှုခွင့်မပြုပါ။ +* **Attention Mask**: attention mechanism တွင် အချို့ tokens များကို လျစ်လျူရှုရန် သို့မဟုတ် ၎င်းတို့၏ score ကို သုညသတ်မှတ်ရန် အသုံးပြုသော mask တစ်ခု။ +* **Language Modeling Head**: မော်ဒယ်၏ hidden states များကို logits အဖြစ် ပြောင်းလဲပေးသည့် layer။ +* **Linear Transformation**: သင်္ချာဆိုင်ရာ အပြောင်းအလဲတစ်ခုဖြစ်ပြီး input vector ကို output vector အဖြစ် ပြောင်းလဲပေးသည်။ +* **Logits**: မော်ဒယ်၏ output မတိုင်မီ raw, unnormalized prediction scores များ။ +* **Cross-Entropy Loss**: classification လုပ်ငန်းတာဝန်များတွင် အသုံးပြုသော loss function တစ်ခုဖြစ်ပြီး မော်ဒယ်၏ ခန့်မှန်းချက်များနှင့် အမှန်တကယ် labels များကြား ခြားနားချက်ကို တိုင်းတာသည်။ +* **WordPiece**: စာသားများကို tokens အဖြစ် ပြောင်းလဲရန် BERT မှ အသုံးပြုသော tokenization နည်းလမ်းတစ်ခု။ +* **`[SEP]` Token**: စာကြောင်းများကြား ခွဲခြားရန် အသုံးပြုသော အထူး token ။ +* **`[CLS]` Token**: စာကြောင်းတစ်ခု၏ အစတွင် ထည့်သွင်းပြီး စာကြောင်းတစ်ခုလုံး၏ ကိုယ်စားပြုမှုကို ဖမ်းယူရန် အသုံးပြုသော အထူး token ။ +* **Segment Embedding**: token တစ်ခုက စာကြောင်းတစ်စုံမှာ ပထမ သို့မဟုတ် ဒုတိယစာကြောင်းမှာ ပါဝင်သည်ကို ဖော်ပြသော embedding။ +* **Feedforward Network**: neural network တစ်ခု၏ အခြေခံ layer တစ်ခု။ +* **Softmax**: multi-class classification တွင် ဖြစ်နိုင်ခြေများကို တွက်ချက်ရန် အသုံးပြုသော activation function တစ်ခု။ +* **Next-Sentence Prediction**: မော်ဒယ်ကို စာကြောင်း B က စာကြောင်း A နောက်က လိုက်သလားဆိုတာ ခန့်မှန်းစေရန် လေ့ကျင့်သော pretraining လုပ်ငန်းတာဝန်။ +* **Sequence Classification Head**: sequence classification လုပ်ငန်းတာဝန်များအတွက် မော်ဒယ်၏ output တွင် ထပ်ထည့်သော linear layer။ +* **Token Classification Head**: token classification လုပ်ငန်းတာဝန်များအတွက် မော်ဒယ်၏ output တွင် ထပ်ထည့်သော linear layer။ +* **Span Classification Head**: question answering လုပ်ငန်းတာဝန်များအတွက် မော်ဒယ်၏ output တွင် ထပ်ထည့်သော linear layer ဖြစ်ပြီး အဖြေ၏ start/end positions များကို ခန့်မှန်းသည်။ +* **Corrupting**: မော်ဒယ်ကို လေ့ကျင့်ရန်အတွက် input data တွင် ရည်ရွယ်ချက်ရှိရှိ အပြောင်းအလဲများ ပြုလုပ်ခြင်း။ +* **Text Infilling**: စာသားအပိုင်းအချို့ကို ဖုံးကွယ်ထားပြီး မော်ဒယ်ကို ၎င်းတို့ကို ခန့်မှန်းစေရန် လေ့ကျင့်သော corruption strategy။ +* **Log-Mel Spectrogram**: အသံအချက်ပြမှုတစ်ခု၏ ကြိမ်နှုန်းနှင့် အချိန်အလိုက် ပြောင်းလဲမှုများကို ပုံရိပ်အဖြစ် ကိုယ်စားပြုခြင်း။ +* **Autoregressively**: အရင်က ခန့်မှန်းထားတဲ့ outputs တွေပေါ် အခြေခံပြီး နောက် output ကို ခန့်မှန်းတဲ့ လုပ်ငန်းစဉ်။ +* **Zero-shot Performance**: မော်ဒယ်တစ်ခုကို သီးခြားလုပ်ငန်းအတွက် လေ့ကျင့်ထားခြင်းမရှိဘဲ လုပ်ငန်းအသစ်တစ်ခုကို လုပ်ဆောင်နိုင်စွမ်း။ +* **Weakly Supervised Pretraining**: လူသားမှတ်သားမှု (human annotations) နည်းပါးသော သို့မဟုတ် မရှိသော ဒေတာများကို အသုံးပြု၍ မော်ဒယ်ကို ကြိုတင်လေ့ကျင့်ခြင်း။ +* **Pipeline**: Hugging Face Transformers library တွင် ပါဝင်သော လုပ်ဆောင်ချက်တစ်ခုဖြစ်ပြီး မော်ဒယ်များကို သီးခြားလုပ်ငန်းတာဝန်များအတွက် အသုံးပြုရလွယ်ကူစေရန် ကူညီပေးသည်။ +* **Patches**: ပုံတစ်ပုံကို ခွဲခြမ်းထားသော သေးငယ်သော အစိတ်အပိုင်းများ။ +* **Convolutional 2D Layer**: ပုံများကို လုပ်ဆောင်ရန် အသုံးပြုသော neural network layer တစ်မျိုး။ +* **Multilayer Perceptron (MLP) Head**: classification လုပ်ငန်းတာဝန်များအတွက် အသုံးပြုသော feedforward neural network layer။ +* **Convolutional Neural Network (CNN)**: ပုံများနှင့် ဗီဒီယိုများကို လုပ်ဆောင်ရန် အထူးဒီဇိုင်းထုတ်ထားသော neural network အမျိုးအစားတစ်ခု။ +* **Convolutional Layers**: CNN ၏ အဓိက အစိတ်အပိုင်းများဖြစ်ပြီး ပုံများမှ features များကို ထုတ်ယူရန် အသုံးပြုသည်။ \ No newline at end of file diff --git a/chapters/my/chapter1/6.mdx b/chapters/my/chapter1/6.mdx new file mode 100644 index 000000000..9619942e3 --- /dev/null +++ b/chapters/my/chapter1/6.mdx @@ -0,0 +1,235 @@ + + +# Transformer Architectures များ[[transformer-architectures]] + +ယခင်အပိုင်းတွေမှာ Transformer architecture အကြောင်းကို မိတ်ဆက်ပေးခဲ့ပြီး ဒီမော်ဒယ်တွေက လုပ်ငန်းတာဝန်အမျိုးမျိုးကို ဘယ်လိုဖြေရှင်းပေးနိုင်လဲဆိုတာကို လေ့လာခဲ့ပါတယ်။ အခုတော့ Transformer မော်ဒယ်တွေရဲ့ အဓိက architectured ပုံစံသုံးမျိုးကို ပိုမိုနက်နဲစွာ လေ့လာပြီး တစ်ခုချင်းစီကို ဘယ်အချိန်မှာ အသုံးပြုသင့်လဲဆိုတာကို နားလည်အောင် လုပ်ဆောင်ကြပါစို့။ ထို့နောက်၊ အဲဒီ architecture တွေကို မတူညီတဲ့ ဘာသာစကားလုပ်ငန်းတာဝန်တွေမှာ ဘယ်လိုအသုံးပြုလဲဆိုတာကို ကြည့်ရှုခဲ့ကြပါတယ်။ + +ဒီအပိုင်းမှာတော့ Transformer မော်ဒယ်တွေရဲ့ အဓိက architectured ပုံစံသုံးမျိုးကို ပိုမိုနက်နဲစွာ လေ့လာပြီး တစ်ခုချင်းစီကို ဘယ်အချိန်မှာ အသုံးပြုသင့်လဲဆိုတာကို နားလည်အောင် လုပ်ဆောင်သွားပါမယ်။ + + +> [!TIP] +> Transformer မော်ဒယ်အများစုဟာ architecture သုံးမျိုးထဲက တစ်ခုကို အသုံးပြုတယ်ဆိုတာ သတိရပါ။ အဲဒါတွေကတော့ encoder-only, decoder-only, ဒါမှမဟုတ် encoder-decoder (sequence-to-sequence) တို့ ဖြစ်ပါတယ်။ ဒီကွာခြားချက်တွေကို နားလည်ထားခြင်းက သင့်ရဲ့ သီးခြားလုပ်ငန်းတာဝန်အတွက် မှန်ကန်တဲ့ မော်ဒယ်ကို ရွေးချယ်နိုင်ဖို့ ကူညီပါလိမ့်မယ်။ + +## Encoder မော်ဒယ်များ[[encoder-models]] + + + +Encoder မော်ဒယ်တွေဟာ Transformer မော်ဒယ်ရဲ့ encoder အပိုင်းကိုသာ အသုံးပြုပါတယ်။ အဆင့်တိုင်းမှာ attention layers တွေက မူလစာကြောင်းထဲက စကားလုံးအားလုံးကို ဝင်ရောက်ကြည့်ရှုနိုင်ပါတယ်။ ဒီမော်ဒယ်တွေကို "bi-directional" attention ရှိတယ်လို့ မကြာခဏ ဖော်ပြလေ့ရှိပြီး *auto-encoding models* လို့လည်း ခေါ်ကြပါတယ်။ + +ဒီမော်ဒယ်တွေရဲ့ pretraining က များသောအားဖြင့် ပေးထားတဲ့ စာကြောင်းတစ်ခုကို တစ်နည်းတစ်ဖုံ ဖျက်ဆီးပြီး (ဥပမာ- ကျပန်းစကားလုံးတွေကို ဝှက်ထားခြင်းဖြင့်) မူလစာကြောင်းကို ရှာဖွေခြင်း သို့မဟုတ် ပြန်လည်တည်ဆောက်ခြင်းတို့ကို မော်ဒယ်ကို တာဝန်ပေးခြင်းအပေါ် အခြေခံပါတယ်။ + +Encoder မော်ဒယ်တွေဟာ စာကြောင်းအပြည့်အစုံကို နားလည်ဖို့ လိုအပ်တဲ့ လုပ်ငန်းတာဝန်များအတွက် အသင့်တော်ဆုံးဖြစ်ပြီး ဥပမာအားဖြင့် sentence classification, named entity recognition (နဲ့ ပိုမိုယေဘုယျအားဖြင့် word classification) နဲ့ extractive question answering တို့ပဲ ဖြစ်ပါတယ်။ + +> [!TIP] +> "[🤗 Transformers တွေက လုပ်ငန်းတာဝန်တွေကို ဘယ်လိုဖြေရှင်းပေးလဲ။](/chapter1/5)" မှာ ကျွန်တော်တို့ မြင်တွေ့ခဲ့ရသလို BERT လို encoder မော်ဒယ်တွေဟာ စာသားကို နားလည်ရာမှာ ထူးချွန်ပါတယ်။ ဘာကြောင့်လဲဆိုတော့ ၎င်းတို့ဟာ input တစ်ခုလုံးရဲ့ အကြောင်းအရာ (context) ကို နှစ်ဖက်စလုံးကနေ ကြည့်ရှုနိုင်လို့ပါ။ ဒါကြောင့် input တစ်ခုလုံးရဲ့ နားလည်မှုက အရေးကြီးတဲ့ လုပ်ငန်းတာဝန်တွေအတွက် ၎င်းတို့ဟာ အကောင်းဆုံးပါပဲ။ + +ဒီမော်ဒယ်မိသားစုရဲ့ ကိုယ်စားပြုမော်ဒယ်တွေကတော့: + +- [BERT](https://huggingface.co/docs/transformers/model_doc/bert) +- [DistilBERT](https://huggingface.co/docs/transformers/model_doc/distilbert) +- [ModernBERT](https://huggingface.co/docs/transformers/en/model_doc/modernbert) + +## Decoder မော်ဒယ်များ[[decoder-models]] + + + +Decoder မော်ဒယ်တွေဟာ Transformer မော်ဒယ်ရဲ့ decoder အပိုင်းကိုသာ အသုံးပြုပါတယ်။ အဆင့်တိုင်းမှာ ပေးထားတဲ့ စကားလုံးတစ်ခုအတွက် attention layers တွေက စာကြောင်းထဲမှာ အရင်ရှိနေတဲ့ စကားလုံးတွေကိုသာ ဝင်ရောက်ကြည့်ရှုနိုင်ပါတယ်။ ဒီမော်ဒယ်တွေကို မကြာခဏ *auto-regressive models* လို့ ခေါ်ကြပါတယ်။ + +Decoder မော်ဒယ်တွေရဲ့ pretraining က များသောအားဖြင့် စာကြောင်းထဲက နောက်ထပ်စကားလုံးကို ခန့်မှန်းခြင်းအပေါ် အခြေခံပါတယ်။ + +ဒီမော်ဒယ်တွေဟာ စာသားဖန်တီးခြင်း (text generation) နဲ့ပတ်သက်တဲ့ လုပ်ငန်းတာဝန်တွေအတွက် အသင့်တော်ဆုံး ဖြစ်ပါတယ်။ + +> [!TIP] +> GPT လို decoder မော်ဒယ်တွေကို တစ်ကြိမ်လျှင် token တစ်ခုချင်းစီကို ခန့်မှန်းခြင်းဖြင့် စာသားတွေ ဖန်တီးဖို့ ဒီဇိုင်းထုတ်ထားပါတယ်။ "[🤗 Transformers တွေက လုပ်ငန်းတာဝန်တွေကို ဘယ်လိုဖြေရှင်းပေးလဲ။](/chapter1/5)" မှာ ကျွန်တော်တို့ လေ့လာခဲ့ရသလို၊ ၎င်းတို့ဟာ ယခင် tokens တွေကိုသာ မြင်နိုင်တာကြောင့် ဖန်တီးမှုဆိုင်ရာ စာသားဖန်တီးခြင်းအတွက် အလွန်ကောင်းမွန်ပေမယ့် bi-directional နားလည်မှု လိုအပ်တဲ့ လုပ်ငန်းတာဝန်တွေအတွက်တော့ သိပ်မသင့်လျော်ပါဘူး။ + +ဒီမော်ဒယ်မိသားစုရဲ့ ကိုယ်စားပြုမော်ဒယ်တွေကတော့: + +- [Hugging Face SmolLM Series](https://huggingface.co/HuggingFaceTB/SmolLM2-1.7B-Instruct) +- [Meta's Llama Series](https://huggingface.co/docs/transformers/en/model_doc/llama4) +- [Google's Gemma Series](https://huggingface.co/docs/transformers/main/en/model_doc/gemma3) +- [DeepSeek's V3](https://huggingface.co/deepseek-ai/DeepSeek-V3) + +### ခေတ်မီ Large Language Models (LLMs) များ + +ခေတ်မီ Large Language Models (LLMs) အများစုဟာ decoder-only architecture ကို အသုံးပြုပါတယ်။ ဒီမော်ဒယ်တွေဟာ လွန်ခဲ့တဲ့ နှစ်အနည်းငယ်အတွင်း အရွယ်အစားနဲ့ စွမ်းရည်တွေ သိသိသာသာ ကြီးထွားလာခဲ့ပြီး အကြီးဆုံးမော်ဒယ်အချို့မှာ parameters ဘီလီယံပေါင်း ရာနဲ့ချီ ပါဝင်ပါတယ်။ + +ခေတ်မီ LLMs တွေကို ပုံမှန်အားဖြင့် အဆင့်နှစ်ဆင့်နဲ့ လေ့ကျင့်ပါတယ်။ +1. **Pretraining**: မော်ဒယ်ဟာ များပြားလှတဲ့ စာသားဒေတာတွေပေါ်မှာ နောက်ထပ် token ကို ခန့်မှန်းဖို့ သင်ယူပါတယ်။ +2. **Instruction tuning**: မော်ဒယ်ဟာ ညွှန်ကြားချက်တွေကို လိုက်နာပြီး အထောက်အကူဖြစ်စေတဲ့ တုံ့ပြန်မှုတွေကို ဖန်တီးဖို့ fine-tune လုပ်ခံရပါတယ်။ + +ဒီချဉ်းကပ်မှုက ကျယ်ပြန့်တဲ့ ခေါင်းစဉ်တွေနဲ့ လုပ်ငန်းတာဝန်မျိုးစုံမှာ လူသားဆန်တဲ့ စာသားတွေကို နားလည်ပြီး ဖန်တီးနိုင်တဲ့ မော်ဒယ်တွေကို ဖြစ်ပေါ်စေခဲ့ပါတယ်။ + +#### ခေတ်မီ LLMs တွေရဲ့ အဓိက စွမ်းရည်များ + +ခေတ်မီ decoder-based LLMs တွေဟာ အထင်ကြီးစရာကောင်းတဲ့ စွမ်းရည်တွေကို ပြသခဲ့ပါတယ်။ + +| စွမ်းရည် | ဖော်ပြချက် | ဥပမာ | +|--------------------|--------------------------------------------------------|------------------------------------------| +| စာသားဖန်တီးခြင်း | ဆက်စပ်မှုရှိပြီး အကြောင်းအရာနှင့်ကိုက်ညီသော စာသားများ ဖန်တီးခြင်း | စာစီစာကုံး၊ ပုံပြင်များ သို့မဟုတ် emails များ ရေးသားခြင်း | +| အကျဉ်းချုပ်ခြင်း | စာရွက်စာတမ်းရှည်များကို ပိုမိုတိုတောင်းသော ပုံစံများဖြင့် အကျဉ်းချုံးခြင်း | အစီရင်ခံစာများ၏ အနှစ်ချုပ်များ ဖန်တီးခြင်း | +| ဘာသာပြန်ခြင်း | ဘာသာစကားများအကြား စာသားများကို ပြောင်းလဲခြင်း | အင်္ဂလိပ်မှ စပိန်သို့ ဘာသာပြန်ခြင်း | +| မေးခွန်းဖြေဆိုခြင်း | အမှန်တကယ်မေးခွန်းများကို အဖြေပေးခြင်း | "ပြင်သစ်နိုင်ငံရဲ့ မြို့တော်က ဘာလဲ" | +| Code ဖန်တီးခြင်း | Code snippets များကို ရေးသားခြင်း သို့မဟုတ် ဖြည့်စွက်ခြင်း | ဖော်ပြချက်အပေါ် အခြေခံ၍ function တစ်ခု ဖန်တီးခြင်း | +| ဆင်ခြင်တုံတရား | ပြဿနာများကို အဆင့်ဆင့်ဖြေရှင်းခြင်း | သင်္ချာပြဿနာများ သို့မဟုတ် ယုတ္တိပဟေဠိများ ဖြေရှင်းခြင်း | +| Few-shot learning | prompt တွင် ဥပမာအနည်းငယ်မှ သင်ယူခြင်း | ဥပမာ ၂-၃ ခုသာ မြင်ပြီးနောက် စာသားများကို အမျိုးအစားခွဲခြားခြင်း | + +Hub ပေါ်ရှိ model repo စာမျက်နှာများမှတစ်ဆင့် သင်၏ browser ထဲတွင် decoder-based LLMs များကို တိုက်ရိုက် စမ်းသပ်နိုင်ပါတယ်။ classic [GPT-2](https://huggingface.co/openai-community/gpt2) (OpenAI ရဲ့ အကောင်းဆုံး open source မော်ဒယ်!) ဥပမာတစ်ခုကတော့ ဒီမှာပါ။ + + + +## Sequence-to-sequence မော်ဒယ်များ[[sequence-to-sequence-models]] + + + +Encoder-decoder မော်ဒယ်တွေ (သို့မဟုတ် *sequence-to-sequence models* လို့လည်း ခေါ်ကြပါတယ်) ဟာ Transformer architecture ရဲ့ အစိတ်အပိုင်းနှစ်ခုလုံးကို အသုံးပြုပါတယ်။ အဆင့်တိုင်းမှာ encoder ရဲ့ attention layers တွေက မူလစာကြောင်းထဲက စကားလုံးအားလုံးကို ဝင်ရောက်ကြည့်ရှုနိုင်ပြီး၊ decoder ရဲ့ attention layers တွေကတော့ input ထဲမှာ ပေးထားတဲ့ စကားလုံးတစ်ခုရဲ့ အရင်ရှိနေတဲ့ စကားလုံးတွေကိုသာ ဝင်ရောက်ကြည့်ရှုနိုင်ပါတယ်။ + +ဒီမော်ဒယ်တွေရဲ့ pretraining က ပုံစံအမျိုးမျိုး ရှိနိုင်ပေမယ့် များသောအားဖြင့် input က တစ်နည်းတစ်ဖုံ ဖျက်ဆီးခံထားရတဲ့ စာကြောင်းတစ်ခုကို ပြန်လည်တည်ဆောက်ခြင်း (ဥပမာ- ကျပန်းစကားလုံးတွေကို ဝှက်ထားခြင်းဖြင့်) တို့ ပါဝင်ပါတယ်။ T5 မော်ဒယ်ရဲ့ pretraining ကတော့ စာသားအပိုင်းအစများ (စကားလုံးများစွာ ပါဝင်နိုင်ပါတယ်) ကို mask special token တစ်ခုတည်းနဲ့ အစားထိုးပြီး အဲဒီ mask token က အစားထိုးထားတဲ့ စာသားကို ခန့်မှန်းဖို့ လုပ်ငန်းတာဝန်ပေးခြင်းတို့ ပါဝင်ပါတယ်။ + +Sequence-to-sequence မော်ဒယ်တွေဟာ ပေးထားတဲ့ input အပေါ် မူတည်ပြီး စာကြောင်းအသစ်တွေ ဖန်တီးခြင်းနဲ့ပတ်သက်တဲ့ လုပ်ငန်းတာဝန်တွေအတွက် အသင့်တော်ဆုံးဖြစ်ပြီး ဥပမာအားဖြင့် summarization, translation, ဒါမှမဟုတ် generative question answering တို့ပဲ ဖြစ်ပါတယ်။ + +> [!TIP] +> "[🤗 Transformers တွေက လုပ်ငန်းတာဝန်တွေကို ဘယ်လိုဖြေရှင်းပေးလဲ။](/chapter1/5)" မှာ ကျွန်တော်တို့ မြင်တွေ့ခဲ့ရသလို BART နဲ့ T5 လို encoder-decoder မော်ဒယ်တွေဟာ architecture နှစ်ခုလုံးရဲ့ အားသာချက်တွေကို ပေါင်းစပ်ထားပါတယ်။ encoder က input ကို နှစ်ဖက်စလုံးကနေ နက်ရှိုင်းစွာ နားလည်မှုကို ပေးပြီး၊ decoder ကတော့ သင့်လျော်တဲ့ output စာသားကို ဖန်တီးပေးပါတယ်။ ဒါကြောင့် ဘာသာပြန်ခြင်း ဒါမှမဟုတ် အကျဉ်းချုပ်ခြင်းလို sequence တစ်ခုကို အခြားတစ်ခုသို့ ပြောင်းလဲပေးတဲ့ လုပ်ငန်းတာဝန်တွေအတွက် ၎င်းတို့ဟာ အကောင်းဆုံးပါပဲ။ + +### လက်တွေ့အသုံးချမှုများ + +Sequence-to-sequence မော်ဒယ်တွေဟာ စာသားတစ်ခုရဲ့ ပုံစံကို အဓိပ္ပာယ်မပျက်စီးစေဘဲ အခြားပုံစံတစ်ခုသို့ ပြောင်းလဲဖို့ လိုအပ်တဲ့ လုပ်ငန်းတာဝန်တွေမှာ ထူးချွန်ပါတယ်။ လက်တွေ့အသုံးချမှုအချို့ကတော့: + +| အသုံးချမှု | ဖော်ပြချက် | ဥပမာ မော်ဒယ် | +|--------------------|--------------------------------------------------------|---------------| +| စက်ဘာသာပြန်ခြင်း | ဘာသာစကားများအကြား စာသားများကို ပြောင်းလဲခြင်း | Marian, T5 | +| စာသားအကျဉ်းချုပ်ခြင်း | စာသားရှည်များကို အကျဉ်းချုံး ဖန်တီးခြင်း | BART, T5 | +| ဒေတာမှ စာသားဖန်တီးခြင်း | ဖွဲ့စည်းထားသော ဒေတာများကို သဘာဝဘာသာစကားအဖြစ် ပြောင်းလဲခြင်း | T5 | +| သဒ္ဒါပြင်ဆင်ခြင်း | စာသားရှိ သဒ္ဒါအမှားများကို ပြင်ဆင်ခြင်း | T5 | +| မေးခွန်းဖြေဆိုခြင်း | အကြောင်းအရာ (context) အပေါ် အခြေခံ၍ အဖြေများ ဖန်တီးခြင်း | BART, T5 | + +ဘာသာပြန်ခြင်းအတွက် sequence-to-sequence မော်ဒယ်ရဲ့ အပြန်အလှန်တုံ့ပြန်နိုင်တဲ့ demo တစ်ခုကတော့ ဒီမှာပါ။ + + + +ဒီမော်ဒယ်မိသားစုရဲ့ ကိုယ်စားပြုမော်ဒယ်တွေကတော့: + +- [BART](https://huggingface.co/docs/transformers/model_doc/bart) +- [mBART](https://huggingface.co/docs/transformers/model_doc/mbart) +- [Marian](https://huggingface.co/docs/transformers/model_doc/marian) +- [T5](https://huggingface.co/docs/transformers/model_doc/t5) + +## မှန်ကန်သော Architecture ကို ရွေးချယ်ခြင်း[[choosing-the-right-architecture]] + +တိကျတဲ့ NLP လုပ်ငန်းတာဝန်တစ်ခုကို လုပ်ဆောင်တဲ့အခါ ဘယ် architecture ကို အသုံးပြုရမယ်ဆိုတာ ဘယ်လိုဆုံးဖြတ်မလဲ။ အောက်ပါကတော့ အမြန်လမ်းညွှန်ချက် ဖြစ်ပါတယ်။ + +| လုပ်ငန်းတာဝန် | အကြံပြုထားသော Architecture | ဥပမာများ | +|-------------------------------------|--------------------------------|-------------------| +| စာသားခွဲခြားသတ်မှတ်ခြင်း (sentiment, topic) | Encoder | BERT, RoBERTa | +| စာသားဖန်တီးခြင်း (creative writing) | Decoder | GPT, LLaMA | +| ဘာသာပြန်ခြင်း | Encoder-Decoder | T5, BART | +| အကျဉ်းချုပ်ခြင်း | Encoder-Decoder | BART, T5 | +| Named entity recognition | Encoder | BERT, RoBERTa | +| မေးခွန်းဖြေဆိုခြင်း (extractive) | Encoder | BERT, RoBERTa | +| မေးခွန်းဖြေဆိုခြင်း (generative) | Encoder-Decoder သို့မဟုတ် Decoder | T5, GPT | +| Conversational AI | Decoder | GPT, LLaMA | + +> [!TIP] +> ဘယ်မော်ဒယ်ကို အသုံးပြုရမယ်ဆိုတာ မသေချာတဲ့အခါ အောက်ပါအချက်တွေကို ထည့်သွင်းစဉ်းစားပါ။ +> +> 1. သင့်လုပ်ငန်းတာဝန်က ဘယ်လိုနားလည်မှုမျိုး လိုအပ်လဲ။ (Bi-directional သို့မဟုတ် Uni-directional) +> 2. သင်ဟာ စာသားအသစ် ဖန်တီးမှာလား ဒါမှမဟုတ် ရှိပြီးသားစာသားကို ခွဲခြမ်းစိတ်ဖြာမှာလား။ +> 3. Sequence တစ်ခုကို အခြားတစ်ခုသို့ ပြောင်းလဲဖို့ လိုအပ်ပါသလား။ +> +> ဒီမေးခွန်းတွေရဲ့ အဖြေတွေက သင့်ကို မှန်ကန်တဲ့ architecture ဆီ ဦးတည်စေပါလိမ့်မယ်။ + +## LLMs တွေရဲ့ ဆင့်ကဲပြောင်းလဲမှု + +Large Language Models တွေဟာ မကြာသေးခင်နှစ်များအတွင်းမှာ အလျင်အမြန် ဆင့်ကဲပြောင်းလဲလာခဲ့ပြီး မျိုးဆက်သစ်တိုင်းမှာ စွမ်းရည်တွေ သိသိသာသာ တိုးတက်လာခဲ့ပါတယ်။ + +## Attention Mechanisms[[attention-mechanisms]] + +Transformer မော်ဒယ်အများစုက attention matrix က စတုရန်းပုံ (square) ဖြစ်တဲ့ full attention ကို အသုံးပြုပါတယ်။ စာသားတွေ ရှည်လျားလာတဲ့အခါမှာ ဒါဟာ တွက်ချက်မှုဆိုင်ရာ အဓိက အဟန့်အတားတစ်ခု ဖြစ်နိုင်ပါတယ်။ Longformer နဲ့ Reformer တို့ဟာ ပိုမိုထိရောက်အောင် ကြိုးစားပြီး လေ့ကျင့်မှုမြန်ဆန်စေရန် attention matrix ရဲ့ sparse version ကို အသုံးပြုကြပါတယ်။ + +> [!TIP] +> Standard attention mechanisms တွေမှာ computational complexity က O(n²) ရှိပါတယ်။ n ဆိုတာ sequence length ပါ။ ဒီဟာက ရှည်လျားလွန်းတဲ့ sequences တွေအတွက် ပြဿနာ ဖြစ်လာပါတယ်။ အောက်မှာဖော်ပြထားတဲ့ specialized attention mechanisms တွေက ဒီကန့်သတ်ချက်ကို ဖြေရှင်းပေးပါတယ်။ + +### LSH attention + +[Reformer](https://huggingface.co/docs/transformers/model_doc/reformer) က LSH attention ကို အသုံးပြုပါတယ်။ softmax(QK^t) မှာ QK^t matrix ရဲ့ အကြီးဆုံး elements (softmax dimension မှာ) တွေကသာ အသုံးဝင်တဲ့ အထောက်အကူတွေကို ပေးပါလိမ့်မယ်။ ဒါကြောင့် Q မှာရှိတဲ့ query q တစ်ခုစီအတွက် q နဲ့ နီးစပ်တဲ့ K မှာရှိတဲ့ key k တွေကိုသာ ထည့်သွင်းစဉ်းစားနိုင်ပါတယ်။ q နဲ့ k နီးစပ်မှုရှိမရှိကို ဆုံးဖြတ်ဖို့ hash function တစ်ခုကို အသုံးပြုပါတယ်။ attention mask ကို လက်ရှိ token ကို ဝှက်ထားဖို့ ပြင်ဆင်ထားပါတယ် (ပထမဆုံး position မှာမှလွဲ၍)။ ဘာလို့လဲဆိုတော့ ဒါဟာ query နဲ့ key ကို တူညီစေပြီး (ဒါကြောင့် အချင်းချင်း အလွန်ဆင်တူပါတယ်)။ hash က ကျပန်းဖြစ်နိုင်တာကြောင့် လက်တွေ့မှာ hash function အများအပြားကို အသုံးပြုပါတယ် (n_rounds parameter နဲ့ ဆုံးဖြတ်ပါတယ်) ပြီးတော့ ၎င်းတို့ကို ပျမ်းမျှယူပါတယ်။ + +### Local attention + +[Longformer](https://huggingface.co/docs/transformers/model_doc/longformer) က local attention ကို အသုံးပြုပါတယ်- များသောအားဖြင့် ဒေသတွင်း အကြောင်းအရာ (ဥပမာ- ဘယ်ဘက်နဲ့ ညာဘက်က tokens နှစ်ခုက ဘာတွေလဲ) က ပေးထားတဲ့ token တစ်ခုအတွက် အရေးယူဖို့ လုံလောက်ပါတယ်။ ဒါ့အပြင် ပြတင်းပေါက်ငယ်တစ်ခုရှိတဲ့ attention layers တွေကို စုစည်းခြင်းဖြင့်၊ နောက်ဆုံး layer က ပြတင်းပေါက်ထဲရှိ tokens တွေထက် ပိုမိုကျယ်ပြန့်တဲ့ receptive field ကို ပိုင်ဆိုင်နိုင်ပြီး စာကြောင်းတစ်ခုလုံးကို ကိုယ်စားပြုတဲ့ အချက်အလက် (representation) ကို တည်ဆောက်နိုင်စေပါတယ်။ + +ကြိုတင်ရွေးချယ်ထားသော input tokens အချို့ကိုလည်း global attention ပေးထားပါတယ်။ ဒီ tokens အနည်းငယ်အတွက် attention matrix က tokens အားလုံးကို ဝင်ရောက်ကြည့်ရှုနိုင်ပြီး ဒီလုပ်ငန်းစဉ်က symmetric ဖြစ်ပါတယ်- အခြား tokens အားလုံးက အဲဒီသီးခြား tokens တွေကို ဝင်ရောက်ကြည့်ရှုနိုင်ပါတယ် (၎င်းတို့ရဲ့ local window ထဲမှာရှိတဲ့ tokens တွေအပြင်)။ ဒါကို စာတမ်းရဲ့ ပုံ 2d မှာ ပြသထားပါတယ်၊ အောက်မှာ attention mask ဥပမာကို ကြည့်ပါ။ + +
+ +
+ +parameters နည်းတဲ့ attention matrices တွေကို အသုံးပြုခြင်းအားဖြင့် မော်ဒယ်က sequence length ပိုကြီးတဲ့ inputs တွေကို လက်ခံနိုင်စေပါတယ်။ + +### Axial positional encodings + +[Reformer](https://huggingface.co/docs/transformers/model_doc/reformer) က axial positional encodings တွေကို အသုံးပြုပါတယ်။ ရိုးရာ Transformer မော်ဒယ်တွေမှာ positional encoding E က \\(l\\) by \\(d\\) matrix တစ်ခုဖြစ်ပြီး \\(l\\) က sequence length ဖြစ်ကာ \\(d\\) က hidden state ရဲ့ dimension ဖြစ်ပါတယ်။ စာသားတွေ အလွန်ရှည်လျားရင် ဒီ matrix က အလွန်ကြီးမားပြီး GPU ပေါ်မှာ နေရာအများကြီး ယူနိုင်ပါတယ်။ ဒါကို ဖြေလျှော့ဖို့အတွက် axial positional encodings တွေက အဲဒီကြီးမားတဲ့ matrix E ကို E1 နဲ့ E2 ဆိုတဲ့ သေးငယ်တဲ့ matrices နှစ်ခုအဖြစ် ခွဲထုတ်တာကို ဆိုလိုပါတယ်။ E1 နဲ့ E2 ရဲ့ dimensions တွေကတော့ \\(l_{1} \times d_{1}\\) နဲ့ \\(l_{2} \times d_{2}\\) ဖြစ်ပြီး \\(l_{1} \times l_{2} = l\\) နဲ့ \\(d_{1} + d_{2} = d\\) ဖြစ်ပါတယ်။ (အလျားတွေအတွက် မြှောက်လဒ်နဲ့ဆိုရင် ဒါက အများကြီး သေးငယ်သွားပါလိမ့်မယ်)။ E မှာရှိတဲ့ time step \\(j\\) အတွက် embedding ကို E1 မှာရှိတဲ့ time step \\(j \% l1\\) အတွက် embedding နဲ့ E2 မှာရှိတဲ့ time step \\(j // l1\\) အတွက် embedding တွေကို ပေါင်းစပ်ခြင်းဖြင့် ရရှိပါတယ်။ + +## နိဂုံးချုပ်[[conclusion]] + +ဒီအပိုင်းမှာ ကျွန်တော်တို့ Transformer architectures သုံးမျိုးနဲ့ အထူးပြု attention mechanisms အချို့ကို လေ့လာခဲ့ပါတယ်။ ဒီ architecture တွေရဲ့ ကွာခြားချက်တွေကို နားလည်ထားတာက သင့်ရဲ့ သီးခြား NLP လုပ်ငန်းတာဝန်အတွက် မှန်ကန်တဲ့ မော်ဒယ်ကို ရွေးချယ်ဖို့အတွက် အရေးကြီးပါတယ်။ + +သင်တန်းမှာ ဆက်လက်လုပ်ဆောင်သွားတဲ့အခါ ဒီမတူညီတဲ့ architecture တွေနဲ့ လက်တွေ့အတွေ့အကြုံတွေ ရရှိလာမှာဖြစ်ပြီး သင့်ရဲ့ သီးခြားလိုအပ်ချက်တွေအတွက် ဘယ်လို fine-tune လုပ်ရမယ်ဆိုတာကို သင်ယူရမှာ ဖြစ်ပါတယ်။ နောက်အပိုင်းမှာတော့ ဒီမော်ဒယ်တွေမှာ ရှိနေတဲ့ ကန့်သတ်ချက်တွေနဲ့ ဘက်လိုက်မှုအချို့ကို လေ့လာသွားမှာဖြစ်ပြီး ၎င်းတို့ကို အသုံးပြုတဲ့အခါ သတိထားသင့်တဲ့ အချက်တွေပဲ ဖြစ်ပါတယ်။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* ** Architecture**: ကွန်ပျူတာစနစ်တစ်ခု၊ ဆော့ဖ်ဝဲလ်တစ်ခု သို့မဟုတ် မော်ဒယ်တစ်ခု၏ အစိတ်အပိုင်းများ စုစည်းပုံနှင့် ၎င်းတို့အချင်းချင်း ဆက်စပ်လုပ်ဆောင်ပုံကို ဖော်ပြသည့် အခြေခံဒီဇိုင်း သို့မဟုတ် ဖွဲ့စည်းပုံ။ +* **Transformer Architecture**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Encoder-only**: Transformer မော်ဒယ်ရဲ့ encoder အစိတ်အပိုင်းကိုသာ အသုံးပြုထားသော architecture အမျိုးအစား။ စာသားနားလည်မှုလုပ်ငန်းများအတွက် သင့်တော်သည်။ +* **Decoder-only**: Transformer မော်ဒယ်ရဲ့ decoder အစိတ်အပိုင်းကိုသာ အသုံးပြုထားသော architecture အမျိုးအစား။ စာသားဖန်တီးမှုလုပ်ငန်းများအတွက် သင့်တော်သည်။ +* **Encoder-Decoder (Sequence-to-sequence)**: Transformer မော်ဒယ်ရဲ့ encoder နှင့် decoder နှစ်ခုစလုံးကို အသုံးပြုထားသော architecture အမျိုးအစား။ စာသားတစ်ခုကို အခြားစာသားတစ်ခုအဖြစ် ပြောင်းလဲခြင်းလုပ်ငန်းများ (ဥပမာ- ဘာသာပြန်ခြင်း) အတွက် သင့်တော်သည်။ +* **Sentence Classification**: စာကြောင်းတစ်ခုလုံး၏ အဓိပ္ပာယ် သို့မဟုတ် ရည်ရွယ်ချက်ကို အမျိုးအစားခွဲခြားခြင်း (ဥပမာ- စိတ်ခံစားမှု၊ ခေါင်းစဉ်)။ +* **Named Entity Recognition (NER)**: စာသားထဲက လူအမည်၊ နေရာအမည်၊ အဖွဲ့အစည်းအမည် စတဲ့ သီးခြားအမည်တွေကို ရှာဖွေဖော်ထုတ်ခြင်း။ +* **Word Classification**: စာကြောင်းတစ်ခုရှိ စကားလုံးတစ်လုံးချင်းစီကို ၎င်း၏ သဒ္ဒါ သို့မဟုတ် အခြားအဓိပ္ပာယ်အရ အမျိုးအစားခွဲခြားခြင်း။ +* **Extractive Question Answering**: ပေးထားသော စာသားအပိုင်းအစမှ မေးခွန်း၏ အဖြေကို တိုက်ရိုက်ထုတ်ယူခြင်း။ +* **Bi-directional Attention**: မော်ဒယ်က စာသားတစ်ခုလုံး၏ အကြောင်းအရာ (context) ကို ရှေ့ဘက်နှင့် နောက်ဘက် နှစ်ဖက်စလုံးမှ ကြည့်ရှုနားလည်နိုင်ခြင်း။ +* **Auto-encoding Models**: စာသားကို ဖျက်ဆီးပြီးနောက် မူလစာသားကို ပြန်လည်တည်ဆောက်ရန် သင်ကြားထားသော မော်ဒယ်များ။ +* **BERT (Bidirectional Encoder Representations from Transformers)**: Google မှ တီထွင်ထားသော encoder-only Transformer မော်ဒယ်။ +* **DistilBERT**: BERT မော်ဒယ်ကို သေးငယ်ပြီး ပိုမိုမြန်ဆန်အောင် ပြုလုပ်ထားသော မော်ဒယ်။ +* **Text Generation**: AI မော်ဒယ်များကို အသုံးပြု၍ လူသားကဲ့သို့သော စာသားအသစ်များ ဖန်တီးခြင်း။ +* **Auto-regressive Models**: နောက်ထပ် token ကို ခန့်မှန်းရန် ယခင် tokens များကိုသာ အသုံးပြု၍ စာသားများကို တစ်ကြိမ်လျှင် token တစ်ခုချင်းစီ ဖန်တီးသော မော်ဒယ်များ။ +* **GPT (Generative Pre-trained Transformer)**: OpenAI မှ တီထွင်ထားသော decoder-only Transformer မော်ဒယ်။ +* **Llama**: Meta မှ တီထွင်ထားသော decoder-only Large Language Model (LLM) အမျိုးအစား။ +* **Gemma**: Google မှ တီထွင်ထားသော decoder-only Large Language Model (LLM) အမျိုးအစား။ +* **DeepSeek**: DeepSeek AI မှ တီထွင်ထားသော decoder-only Large Language Model (LLM) အမျိုးအစား။ +* **Pretraining**: မော်ဒယ်ကို များပြားလှသော အထွေထွေဒေတာများဖြင့် အစောပိုင်းသင်ကြားမှု။ +* **Instruction Tuning**: မော်ဒယ်ကို သီးခြားညွှန်ကြားချက်များကို လိုက်နာပြီး အထောက်အကူဖြစ်စေသော တုံ့ပြန်မှုများ ထုတ်လုပ်ရန် fine-tune လုပ်ခြင်း။ +* **Token**: စာသားကို ပိုင်းခြားထားသော အသေးငယ်ဆုံးယူနစ် (ဥပမာ- စကားလုံး၊ စာလုံးအစိတ်အပိုင်း)။ +* **Summarization**: စာသားရှည်များကို အကျဉ်းချုပ်ဖော်ပြခြင်း။ +* **Translation**: ဘာသာစကားတစ်ခုမှ အခြားတစ်ခုသို့ စာသားများကို ပြောင်းလဲခြင်း။ +* **Generative Question Answering**: မေးခွန်း၏ အဖြေကို ပေးထားသော အကြောင်းအရာ (context) အပေါ် အခြေခံ၍ စာသားအသစ်များ ဖန်တီးခြင်းဖြင့် ထုတ်ပေးခြင်း။ +* **BART (Bidirectional and Auto-Regressive Transformers)**: Encoder-Decoder Transformer မော်ဒယ်တစ်မျိုး။ +* **T5 (Text-to-Text Transfer Transformer)**: Encoder-Decoder Transformer မော်ဒယ်တစ်မျိုးဖြစ်ပြီး လုပ်ငန်းတာဝန်အားလုံးကို "text-to-text" ပုံစံဖြင့် ဖြေရှင်းရန် ဒီဇိုင်းထုတ်ထားသည်။ +* **Marian**: အဓိကအားဖြင့် machine translation အတွက် အသုံးပြုသော encoder-decoder မော်ဒယ်။ +* **mBART**: Multilingual BART (ဘာသာစကားမျိုးစုံအတွက် BART)။ +* **Data-to-text Generation**: ဖွဲ့စည်းထားသော ဒေတာများကို သဘာဝဘာသာစကားစာသားအဖြစ် ပြောင်းလဲခြင်း။ +* **Grammar Correction**: စာသားရှိ သဒ္ဒါအမှားများကို ပြင်ဆင်ခြင်း။ +* **Conversational AI**: လူသားများနှင့် သဘာဝဘာသာစကားဖြင့် အပြန်အလှန်ပြောဆိုနိုင်သော AI စနစ်များ။ +* **RoBERTa**: BERT ကို ပိုမိုကောင်းမွန်အောင် လေ့ကျင့်ထားသော encoder-only မော်ဒယ်။ +* **Attention Matrix**: Transformer မော်ဒယ်များတွင် အသုံးပြုသော matrix တစ်ခုဖြစ်ပြီး input sequence အတွင်းရှိ token များအချင်းချင်း မည်မျှဆက်စပ်နေသည်ကို ဖော်ပြသည်။ +* **Computational Bottleneck**: စနစ်တစ်ခု၏ စွမ်းဆောင်ရည်ကို ကန့်သတ်ထားသော အရင်းအမြစ် သို့မဟုတ် လုပ်ငန်းစဉ်။ +* **Sparse Attention**: attention matrix ၏ အရေးမကြီးသော အစိတ်အပိုင်းများကို လျစ်လျူရှုခြင်းဖြင့် တွက်ချက်မှု ထိရောက်အောင် ပြုလုပ်ထားသော attention mechanism အမျိုးအစား။ +* **LSH (Locality Sensitive Hashing) Attention**: Reformer မော်ဒယ်တွင် အသုံးပြုသော attention အမျိုးအစားဖြစ်ပြီး ဆင်တူသော query နှင့် key များကို ရှာဖွေရန် hash function များကို အသုံးပြုသည်။ +* **Longformer**: ရှည်လျားသော input sequences များကို ကိုင်တွယ်နိုင်ရန် local attention နှင့် global attention တို့ကို ပေါင်းစပ်အသုံးပြုထားသော Transformer မော်ဒယ်။ +* **Local Attention**: ပေးထားသော token တစ်ခုအတွက် အနီးအနားရှိ tokens များကိုသာ အာရုံစိုက်သော attention mechanism။ +* **Receptive Field**: neural network layer တစ်ခု၏ output ယူနစ်တစ်ခုကို လွှမ်းမိုးသော input data ၏ အရွယ်အစား။ +* **Global Attention**: အချို့သော input tokens များအတွက် input sequence ရှိ tokens အားလုံးကို အာရုံစိုက်ခွင့်ပြုသော attention mechanism။ +* **Axial Positional Encodings**: ရှည်လျားသော sequences များအတွက် positional encoding ကို ပိုမိုထိရောက်အောင် ပြုလုပ်ရန် matrix တစ်ခုကို သေးငယ်သော matrices နှစ်ခုအဖြစ် ခွဲထုတ်ခြင်းနည်းလမ်း။ +* **Hidden State**: Transformer မော်ဒယ်များတွင် layer တစ်ခုမှ အခြားတစ်ခုသို့ လက်ဆင့်ကမ်းပေးသော အတွင်းပိုင်း ကိုယ်စားပြုအချက်အလက်။ +* **Dimension**: vector သို့မဟုတ် matrix တစ်ခု၏ အတိုင်းအတာအရေအတွက်။ \ No newline at end of file diff --git a/chapters/my/chapter1/7.mdx b/chapters/my/chapter1/7.mdx new file mode 100644 index 000000000..c51de1dd1 --- /dev/null +++ b/chapters/my/chapter1/7.mdx @@ -0,0 +1,287 @@ + + +# အမှတ်မပေးသော Quiz[[ungraded-quiz]] + + + +ဒီအခန်းက သင်ကြားရမယ့် အကြောင်းအရာတွေ အများကြီးကို ဖော်ပြခဲ့ပြီးပါပြီ။ အသေးစိတ် အချက်အလက်အားလုံးကို နားမလည်သေးရင်လည်း စိတ်မပူပါနဲ့။ ဒါပေမယ့် ဒီ quiz နဲ့ သင် သင်ယူခဲ့တာတွေကို ပြန်လည်သုံးသပ်ကြည့်ရအောင်။ + +ဒီ quiz က အမှတ်မပေးတဲ့အတွက် သင်နှစ်သက်သလောက် အကြိမ်ကြိမ် ကြိုးစားဖြေဆိုနိုင်ပါတယ်။ မေးခွန်းအချို့နဲ့ ရုန်းကန်ရရင် အကြံပြုချက်တွေကို လိုက်နာပြီး သင်ခန်းစာတွေကို ပြန်လည်လေ့လာပါ။ ဒီအကြောင်းအရာတွေကို အသိအမှတ်ပြု စာမေးပွဲမှာ ထပ်မံဖြေဆိုရမှာ ဖြစ်ပါတယ်။ + +### 1. Hub ကို ရှာဖွေပြီး `roberta-large-mnli` checkpoint ကို ရှာပါ။ ၎င်းသည် မည်သည့်လုပ်ငန်းကို လုပ်ဆောင်ပါသနည်း။ + +ပြန်လည်ကြည့်ရှုပါ။" + }, + { + text: "စာသားခွဲခြားသတ်မှတ်ခြင်း (Text classification)", + explain: " ပိုတိတိကျကျပြောရရင် ၎င်းသည် စာကြောင်းနှစ်ကြောင်းက ယုတ္တိရှိရှိ ဆက်စပ်မှုရှိမရှိကို အဆင့်သုံးဆင့် (contradiction, neutral, entailment) နဲ့ ခွဲခြားသတ်မှတ်ပါတယ်။ ဒီလုပ်ငန်းကို natural language inference လို့လည်း ခေါ်ပါတယ်။", + correct: true + }, + { + text: "စာသားထုတ်လုပ်ခြင်း (Text generation)", + explain: "roberta-large-mnli စာမျက်နှာကို ပြန်လည်ကြည့်ရှုပါ။" + } + ]} +/> + +### 2. အောက်ပါ code သည် မည်သည့်အရာကို ပြန်ပေးမည်နည်း။ + +```py +from transformers import pipeline + +ner = pipeline("ner", grouped_entities=True) +ner("My name is Sylvain and I work at Hugging Face in Brooklyn.") +``` + + + +### 3. ဤ code နမူနာတွင် ... နေရာ၌ မည်သည့်အရာကို အစားထိုးသင့်သနည်း။ + +```py +from transformers import pipeline + +filler = pipeline("fill-mask", model="bert-base-cased") +result = filler("...") +``` + + has been waiting for you.", + explain: "ဒါက မမှန်ကန်ပါဘူး။ `bert-base-cased` မော်ဒယ်ကတ်ကို ကြည့်ပြီး သင့်ရဲ့ အမှားကို ရှာဖွေကြည့်ပါ။" + }, + { + text: "This [MASK] has been waiting for you.", + explain: "ဒီမော်ဒယ်ရဲ့ mask token က `[MASK]` ဖြစ်ပါတယ်။", + correct: true + }, + { + text: "This man has been waiting for you.", + explain: "ဒါက မမှန်ကန်ပါဘူး။ ဒီ pipeline က ဝှက်ထားတဲ့ စကားလုံးတွေကို ဖြည့်ဆည်းပေးတာဖြစ်တဲ့အတွက် mask token တစ်ခုခု လိုအပ်ပါတယ်။" + } + ]} +/> + +### 4. ဤ code သည် အဘယ်ကြောင့် အလုပ်မလုပ်နိုင်သနည်း။ + +```py +from transformers import pipeline + +classifier = pipeline("zero-shot-classification") +result = classifier("This is a course about the Transformers library") +``` + + + +### 5. "Transfer learning" ဆိုတာ ဘာကိုဆိုလိုတာလဲ။ + + + +### 6. မှန်လား မှားလား။ Language Model တစ်ခုသည် ၎င်း၏ pretraining အတွက် အညွှန်းများ (labels) မလိုအပ်ပါ။ + + + +### 7. "model"၊ "architecture" နှင့် "weights" ဟူသော ဝေါဟာရများကို အကောင်းဆုံး ဖော်ပြသည့် စာကြောင်းကို ရွေးပါ။ + + + +### 8. ဖန်တီးထားသော စာသားများဖြင့် prompts များကို ဖြည့်စွက်ရန်အတွက် မည်သည့်မော်ဒယ်အမျိုးအစားများကို အသုံးပြုမည်နည်း။ + + + +### 9. စာသားများကို အကျဉ်းချုပ်ရန်အတွက် မည်သည့်မော်ဒယ်အမျိုးအစားများကို အသုံးပြုမည်နည်း။ + + + +### 10. သတ်မှတ်ထားသော အညွှန်းများ (labels) အတိုင်း စာသား inputs များကို အမျိုးအစားခွဲခြားရန်အတွက် မည်သည့်မော်ဒယ်အမျိုးအစားများကို အသုံးပြုမည်နည်း။ + + + +### 11. မော်ဒယ်တစ်ခုတွင် တွေ့ရသော ဘက်လိုက်မှု (bias) သည် မည်သည့်ရင်းမြစ်မှ ဖြစ်ပေါ်လာနိုင်သနည်း။ + + + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Quiz**: သင်ယူခဲ့သည့် အကြောင်းအရာများကို ပြန်လည်စစ်ဆေးရန် မေးခွန်းများ။ +* **Transformer Models**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Hugging Face Hub**: AI မော်ဒယ်တွေ၊ datasets တွေနဲ့ demo တွေကို အခြားသူတွေနဲ့ မျှဝေဖို့၊ ရှာဖွေဖို့နဲ့ ပြန်လည်အသုံးပြုဖို့အတွက် အွန်လိုင်း platform တစ်ခု ဖြစ်ပါတယ်။ +* **Checkpoint**: မော်ဒယ်တစ်ခုကို လေ့ကျင့်နေစဉ်အတွင်း အချိန်အတန်ကြာပြီးနောက် အခြေအနေတစ်ခုကို သိမ်းဆည်းထားသော အမှတ်။ +* **Task**: AI မော်ဒယ်တစ်ခုက လုပ်ဆောင်ရန် လေ့ကျင့်ထားသော သီးခြားလုပ်ငန်း (ဥပမာ- စာသားခွဲခြားသတ်မှတ်ခြင်း၊ စာသားထုတ်လုပ်ခြင်း)။ +* **Summarization**: စာသားတစ်ခုကို အဓိကအချက်အလက်များ မပျောက်ပျက်စေဘဲ ပိုမိုတိုတောင်းသော ပုံစံဖြင့် အကျဉ်းချုပ်ခြင်း။ +* **Text Classification**: စာသားတစ်ခုကို ကြိုတင်သတ်မှတ်ထားသော အမျိုးအစားများ သို့မဟုတ် အညွှန်းများထဲသို့ ခွဲခြားသတ်မှတ်ခြင်း။ +* **Natural Language Inference (NLI)**: စာကြောင်းနှစ်ကြောင်းကြားရှိ ယုတ္တိဆိုင်ရာ ဆက်နွယ်မှုကို ဆုံးဖြတ်သည့် လုပ်ငန်း။ (ဥပမာ- contradiction, neutral, entailment) +* **Text Generation**: AI မော်ဒယ်များကို အသုံးပြု၍ လူသားကဲ့သို့သော စာသားအသစ်များ ဖန်တီးခြင်း။ +* **`pipeline()` function**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ လုပ်ဆောင်ချက်တစ်ခုဖြစ်ပြီး မော်ဒယ်တွေကို သီးခြားလုပ်ငန်းတာဝန်များ (ဥပမာ- စာသားခွဲခြားသတ်မှတ်ခြင်း၊ စာသားထုတ်လုပ်ခြင်း) အတွက် အသုံးပြုရလွယ်ကူအောင် ပြုလုပ်ပေးပါတယ်။ +* **`ner` (Named Entity Recognition)**: စာသားထဲက လူအမည်၊ နေရာအမည်၊ အဖွဲ့အစည်းအမည် စတဲ့ သီးခြားအမည်တွေကို ရှာဖွေဖော်ထုတ်ခြင်း။ +* **`grouped_entities=True`**: `ner` pipeline တွင် အသုံးပြုသည့် parameter တစ်ခုဖြစ်ပြီး တူညီသော entity နှင့် သက်ဆိုင်သည့် စကားလုံးများကို အုပ်စုဖွဲ့ပေးသည်။ +* **`sentiment-analysis` pipeline**: စာသားတစ်ခု၏ စိတ်ခံစားမှု (အပြုသဘော၊ အနုတ်သဘော) ကို ခွဲခြမ်းစိတ်ဖြာရန် အသုံးပြုသော pipeline။ +* **`text-generation` pipeline**: input prompt အပေါ် အခြေခံ၍ စာသားအသစ်များကို ဖန်တီးရန် အသုံးပြုသော pipeline။ +* **`fill-mask` pipeline**: စာသားတစ်ခုရှိ ဝှက်ထားသော စကားလုံးများ (mask tokens) ကို ဖြည့်ဆည်းပေးရန် အသုံးပြုသော pipeline။ +* **`bert-base-cased`**: BERT (Bidirectional Encoder Representations from Transformers) မော်ဒယ်၏ ဗားရှင်းတစ်ခုဖြစ်ပြီး အင်္ဂလိပ်စာလုံးအကြီးအသေးကို ခွဲခြားသိမြင်သည်။ ၎င်း၏ mask token သည် `[MASK]` ဖြစ်သည်။ +* **`zero-shot-classification` pipeline**: လေ့ကျင့်မှုဒေတာတွင် မမြင်ဖူးသေးသော အညွှန်းများဖြင့် စာသားများကို အမျိုးအစားခွဲခြားနိုင်သော pipeline။ +* **`candidate_labels`**: `zero-shot-classification` pipeline တွင် အသုံးပြုသည့် parameter တစ်ခုဖြစ်ပြီး စာသားကို ခွဲခြားသတ်မှတ်ရန်အတွက် ဖြစ်နိုင်ခြေရှိသော အညွှန်းများ (labels) စာရင်းကို ပေးပို့သည်။ +* **Transfer Learning**: ကြိုတင်လေ့ကျင့်ထားသော မော်ဒယ်တစ်ခု၏ အသိပညာကို အခြားလုပ်ငန်းတစ်ခု (new task) အတွက် မော်ဒယ်အသစ်သို့ လွှဲပြောင်းပေးခြင်း။ +* **Pretrained Model**: ကြီးမားသော ဒေတာအစုအဝေးများဖြင့် အစောပိုင်းကတည်းက လေ့ကျင့်ထားသော မော်ဒယ်။ +* **Fine-tuned Model**: ကြိုတင်လေ့ကျင့်ထားသော မော်ဒယ်တစ်ခုကို သီးခြားလုပ်ငန်းတစ်ခု (specific task) အတွက် အနည်းငယ်သော ဒေတာနှင့် ထပ်မံလေ့ကျင့်ပေးထားသော မော်ဒယ်။ +* **Weights**: Machine Learning မော်ဒယ်တစ်ခု၏ သင်ယူနိုင်သော အစိတ်အပိုင်းများ။ ၎င်းတို့သည် လေ့ကျင့်နေစဉ်အတွင်း ဒေတာများမှ ပုံစံများကို သင်ယူကာ ချိန်ညှိပေးသည်။ +* **Architecture**: Machine Learning မော်ဒယ်တစ်ခု၏ တည်ဆောက်ပုံ သို့မဟုတ် ဒီဇိုင်း။ ၎င်းသည် သင်္ချာဆိုင်ရာ functions များ၏ အစီအစဉ်နှင့် ၎င်းတို့ မည်သို့ချိတ်ဆက်ထားသည်ကို သတ်မှတ်သည်။ +* **Self-supervised Learning**: အညွှန်းများ (labels) ကို inputs များမှ အလိုအလျောက် ထုတ်လုပ်နိုင်သည့် သင်ယူမှုပုံစံတစ်မျိုး။ +* **Encoder Model**: Transformer Architecture ၏ အစိတ်အပိုင်းတစ်ခုဖြစ်ပြီး input data (ဥပမာ- စာသား) ကို နားလည်ပြီး ကိုယ်စားပြုတဲ့ အချက်အလက် (representation) အဖြစ် ပြောင်းလဲပေးကာ classification ကဲ့သို့သော လုပ်ငန်းများအတွက် သင့်လျော်သည်။ +* **Decoder Model**: Transformer Architecture ၏ အစိတ်အပိုင်းတစ်ခုဖြစ်ပြီး encoder ကနေ ရရှိတဲ့ အချက်အလက် (representation) ကို အသုံးပြုပြီး output data (ဥပမာ- ဘာသာပြန်ထားတဲ့ စာသား သို့မဟုတ် စာသားထုတ်လုပ်ခြင်း) ကို ထုတ်ပေးသည်။ +* **Sequence-to-sequence Model**: Encoder နှင့် Decoder နှစ်ခုစလုံး ပါဝင်သော Transformer architecture တစ်မျိုးဖြစ်ပြီး input sequence မှ output sequence တစ်ခုသို့ ပြောင်းလဲခြင်း (ဥပမာ- ဘာသာပြန်ခြင်း၊ အကျဉ်းချုပ်ခြင်း) လုပ်ငန်းများအတွက် အသုံးပြုပါတယ်။ +* **Bias**: ဒေတာအစုအဝေး (dataset) သို့မဟုတ် မော်ဒယ်၏ လေ့ကျင့်မှုပုံစံကြောင့် ဖြစ်ပေါ်လာသော ဘက်လိုက်မှုများ။ +* **Metric**: မော်ဒယ်တစ်ခု၏ စွမ်းဆောင်ရည်ကို တိုင်းတာရန် အသုံးပြုသော တိုင်းတာမှုစနစ်။ \ No newline at end of file diff --git a/chapters/my/chapter1/8.mdx b/chapters/my/chapter1/8.mdx new file mode 100644 index 000000000..74d80239c --- /dev/null +++ b/chapters/my/chapter1/8.mdx @@ -0,0 +1,304 @@ +# LLMs များဖြင့် စာသားထုတ်လုပ်မှု မှန်းဆတွက်ချက်ခြင်း (Text Generation Inference) ကို နက်ရှိုင်းစွာ လေ့လာခြင်း[[inference-with-llms]] + + + + + +ခုထိ ကျွန်တော်တို့ဟာ text classification ဒါမှမဟုတ် summarization လိုမျိုး သီးခြားလုပ်ငန်းဆောင်တာအမျိုးမျိုးနဲ့ ပတ်သက်ပြီး Transformer architecture ကို လေ့လာခဲ့ပြီးပါပြီ။ ဒါပေမယ့် Large Language Models တွေကို စာသားထုတ်လုပ်ဖို့ အများဆုံးအသုံးပြုကြပြီး၊ ဒီအခန်းမှာ ဒါကိုပဲ ကျွန်တော်တို့ လေ့လာသွားမှာပါ။ + +ဒီစာမျက်နှာမှာ LLM inference ရဲ့ အဓိကသဘောတရားတွေကို ကျွန်တော်တို့ လေ့လာသွားမှာဖြစ်ပြီး၊ ဒီမော်ဒယ်တွေက စာသားတွေကို ဘယ်လိုထုတ်လုပ်တယ်၊ inference လုပ်ငန်းစဉ်မှာ ဘယ်လိုအဓိက အစိတ်အပိုင်းတွေ ပါဝင်တယ်ဆိုတာကို ပြည့်စုံစွာ နားလည်အောင် ရှင်းပြပေးပါမယ်။ + +## အခြေခံသဘောတရားများကို နားလည်ခြင်း[[understanding-the-basics]] + +အခြေခံသဘောတရားများနဲ့ စလိုက်ရအောင်။ Inference ဆိုတာ လေ့ကျင့်ပြီးသား LLM တစ်ခုကို ပေးထားတဲ့ input prompt ကနေ လူသားဆန်တဲ့ စာသားတွေ ထုတ်လုပ်ဖို့ အသုံးပြုတဲ့ လုပ်ငန်းစဉ် ဖြစ်ပါတယ်။ ဘာသာစကားမော်ဒယ်တွေဟာ လေ့ကျင့်မှုကနေ ရရှိထားတဲ့ အသိပညာတွေကို အသုံးပြုပြီး တစ်ကြိမ်ကို စကားလုံးတစ်လုံးစီ စဉ်းစားကာ အဖြေတွေ ထုတ်ပေးပါတယ်။ မော်ဒယ်က parameters ဘီလီယံပေါင်းများစွာကနေ သင်ယူထားတဲ့ ဖြစ်နိုင်ခြေတွေကို အသုံးပြုပြီး sequence တစ်ခုရဲ့ နောက်ထပ် token ကို ခန့်မှန်းပြီး ထုတ်လုပ်ပေးပါတယ်။ ဒီလို အစဉ်လိုက် ထုတ်လုပ်ခြင်းက LLMs တွေကို ဆက်စပ်မှုရှိပြီး အကြောင်းအရာနဲ့ ကိုက်ညီတဲ့ စာသားတွေကို ထုတ်လုပ်နိုင်စေတာ ဖြစ်ပါတယ်။ + +## Attention ရဲ့ အခန်းကဏ္ဍ[[the-role-of-attention]] + +Attention mechanism ဟာ LLMs တွေကို အကြောင်းအရာ (context) ကို နားလည်ပြီး ဆက်စပ်မှုရှိတဲ့ အဖြေတွေကို ထုတ်ပေးနိုင်စေတဲ့ အရာဖြစ်ပါတယ်။ နောက်ထပ်စကားလုံးကို ခန့်မှန်းတဲ့အခါ စာကြောင်းတစ်ကြောင်းထဲက စကားလုံးတိုင်းက အရေးပါမှု တူညီတာ မဟုတ်ပါဘူး။ ဥပမာ - "The capital of France is ..." ဆိုတဲ့ စာကြောင်းမှာ "France" နဲ့ "capital" ဆိုတဲ့ စကားလုံးတွေက နောက်ထပ် "Paris" ဆိုတဲ့ စကားလုံးကို ဆုံးဖြတ်ဖို့ အရေးကြီးပါတယ်။ ဒီလို သက်ဆိုင်ရာ အချက်အလက်တွေအပေါ် အာရုံစိုက်နိုင်စွမ်းကို ကျွန်တော်တို့ Attention လို့ ခေါ်ပါတယ်။ + +Visual Gif of Attention + +နောက်ထပ် token ကို ခန့်မှန်းဖို့ အသက်ဆိုင်ဆုံး စကားလုံးတွေကို ဖော်ထုတ်တဲ့ ဒီလုပ်ငန်းစဉ်ဟာ အံ့သြစရာကောင်းလောက်အောင် ထိရောက်မှုရှိတယ်ဆိုတာ သက်သေပြခဲ့ပြီးပါပြီ။ LLMs တွေ လေ့ကျင့်တဲ့ အခြေခံမူ—နောက်ထပ် token ကို ခန့်မှန်းခြင်း—ဟာ BERT နဲ့ GPT-2 ကတည်းက ယေဘုယျအားဖြင့် အတူတူပဲ ရှိခဲ့ပေမယ့်၊ neural network တွေကို ချဲ့ထွင်ရာမှာနဲ့ attention mechanism ကို ပိုမိုရှည်လျားတဲ့ sequence တွေအတွက် ကုန်ကျစရိတ်သက်သာစွာ အလုပ်လုပ်နိုင်အောင် လုပ်ဆောင်ရာမှာ သိသိသာသာ တိုးတက်မှုတွေ ရှိခဲ့ပါတယ်။ + +> [!TIP] +> အတိုချုပ်ပြောရရင် attention mechanism ဟာ LLMs တွေ ဆက်စပ်မှုရှိပြီး အကြောင်းအရာကို နားလည်တဲ့ စာသားတွေကို ထုတ်လုပ်နိုင်စေဖို့အတွက် အဓိကသော့ချက် ဖြစ်ပါတယ်။ ဒါက ခေတ်မီ LLMs တွေကို ယခင်မျိုးဆက် ဘာသာစကားမော်ဒယ်တွေနဲ့ ကွဲပြားစေပါတယ်။ + +### Context Length နဲ့ Attention Span[[context-length-and-attention-span]] + +Attention ကို နားလည်ပြီးပြီဆိုတော့ LLM တစ်ခုက ဘယ်လောက်အထိ context ကို ကိုင်တွယ်နိုင်မလဲဆိုတာကို ဆက်လေ့လာကြည့်ရအောင်။ ဒါက model ရဲ့ 'attention span' လို့ခေါ်တဲ့ context length နဲ့ သက်ဆိုင်ပါတယ်။ + +Context length ဆိုတာ LLM တစ်ခုက တစ်ကြိမ်တည်း လုပ်ဆောင်နိုင်တဲ့ အများဆုံး token (စကားလုံး ဒါမှမဟုတ် စကားလုံးရဲ့ အစိတ်အပိုင်း) အရေအတွက်ကို ရည်ညွှန်းပါတယ်။ ဒါကို model ရဲ့ အလုပ်လုပ်တဲ့ မှတ်ဉာဏ် (working memory) ရဲ့ အရွယ်အစားလို့ တွေးကြည့်နိုင်ပါတယ်။ + +ဒီစွမ်းရည်တွေဟာ လက်တွေ့ကျတဲ့ အချက်အလက်အချို့ကြောင့် ကန့်သတ်ထားပါတယ်- +- Model ရဲ့ architecture နဲ့ အရွယ်အစား +- ရရှိနိုင်တဲ့ ကွန်ပျူတာ အရင်းအမြစ်များ +- input နဲ့ ထုတ်လိုတဲ့ output ရဲ့ ရှုပ်ထွေးမှု + +စံပြကမ္ဘာမှာဆိုရင် မော်ဒယ်ကို ကန့်သတ်ချက်မရှိတဲ့ context တွေ ထည့်ပေးနိုင်ပေမယ့်၊ hardware ကန့်သတ်ချက်တွေနဲ့ ကွန်ပျူတာ ကုန်ကျစရိတ်တွေကြောင့် ဒါက လက်တွေ့မကျပါဘူး။ ဒါကြောင့် ကန့်သတ်ချက်နဲ့ ထိရောက်မှုကို မျှတအောင် ထိန်းညှိဖို့အတွက် မတူညီတဲ့ context length တွေနဲ့ မော်ဒယ်တွေကို ဒီဇိုင်းထုတ်ထားတာ ဖြစ်ပါတယ်။ + +> [!TIP] +> Context length ဆိုတာ မော်ဒယ်က အဖြေတစ်ခုကို ထုတ်လုပ်တဲ့အခါ တစ်ကြိမ်တည်းမှာ ထည့်သွင်းစဉ်းစားနိုင်တဲ့ အများဆုံး token အရေအတွက် ဖြစ်ပါတယ်။ + +### Prompting ပညာ[[the-art-of-prompting]] + +ကျွန်တော်တို့ LLMs တွေကို အချက်အလက်တွေ ပေးပို့တဲ့အခါ၊ LLM ရဲ့ ထုတ်လုပ်မှုကို လိုချင်တဲ့ output ဆီ ဦးတည်နိုင်အောင် input ကို ပုံစံချပါတယ်။ ဒါကို _prompting_ လို့ ခေါ်ပါတယ်။ + +LLMs တွေက အချက်အလက်တွေကို ဘယ်လိုလုပ်ဆောင်တယ်ဆိုတာ နားလည်ခြင်းက ပိုကောင်းတဲ့ prompts တွေကို ဖန်တီးနိုင်ဖို့ ကူညီပေးပါတယ်။ မော်ဒယ်ရဲ့ အဓိကတာဝန်က input token တစ်ခုစီရဲ့ အရေးပါမှုကို ခွဲခြမ်းစိတ်ဖြာပြီး နောက်ထပ် token ကို ခန့်မှန်းဖို့ဖြစ်တာကြောင့်၊ သင်ရဲ့ input sequence ရဲ့ စကားလုံးဖွဲ့စည်းပုံက အရေးကြီးလာပါတယ်။ + +> [!TIP] +> Prompt ကို သေချာဒီဇိုင်းထုတ်ခြင်းက **LLM ရဲ့ ထုတ်လုပ်မှုကို လိုချင်တဲ့ output ဆီ ဦးတည်စေရန် ပိုမိုလွယ်ကူစေပါတယ်**။ + +## နှစ်ဆင့်ပါသော Inference လုပ်ငန်းစဉ်[[the-two-phase-inference-process]] + +အခြေခံအစိတ်အပိုင်းတွေကို ကျွန်တော်တို့ နားလည်ပြီးပြီဆိုတော့ LLMs တွေက စာသားတွေကို ဘယ်လိုထုတ်လုပ်တယ်ဆိုတာကို နက်ရှိုင်းစွာ လေ့လာကြည့်ရအောင်။ လုပ်ငန်းစဉ်ကို အဓိက အဆင့်နှစ်ဆင့်ခွဲနိုင်ပါတယ်- prefill နဲ့ decode ပါ။ ဒီအဆင့်တွေက ပူးပေါင်းပြီး အလုပ်လုပ်ကြပြီး၊ စာသားတွေ ဆက်စပ်မှုရှိအောင် ထုတ်လုပ်ရာမှာ အရေးကြီးတဲ့ အခန်းကဏ္ဍတွေကနေ ပါဝင်ပါတယ်။ + +### Prefill အဆင့်[[the-prefill-phase]] + +Prefill အဆင့်ဟာ ချက်ပြုတ်ရာမှာ ပြင်ဆင်မှုအဆင့်နဲ့ တူပါတယ်။ ဒီအဆင့်မှာ ကနဦးပါဝင်ပစ္စည်းအားလုံးကို လုပ်ဆောင်ပြီး အသင့်ပြင်ဆင်ပါတယ်။ ဒီအဆင့်မှာ အဓိကအချက် (၃) ချက် ပါဝင်ပါတယ်- + +1. **Tokenization**: input စာသားကို tokens တွေအဖြစ် ပြောင်းလဲခြင်း (ဒါတွေကို မော်ဒယ်က နားလည်တဲ့ အခြေခံ building blocks တွေလို့ တွေးကြည့်နိုင်ပါတယ်) +2. **Embedding Conversion**: ဒီ tokens တွေကို ၎င်းတို့ရဲ့ အဓိပ္ပာယ်ကို ဖမ်းယူထားတဲ့ ဂဏန်းဆိုင်ရာ ကိုယ်စားပြုမှု (numerical representations) တွေအဖြစ် ပြောင်းလဲခြင်း +3. **ကနဦး လုပ်ဆောင်ခြင်း (Initial Processing)**: context ကို ပြည့်ပြည့်စုံစုံ နားလည်မှု ဖန်တီးဖို့အတွက် ဒီ embeddings တွေကို model ရဲ့ neural network တွေကနေတဆင့် လုပ်ဆောင်ခြင်း + +ဒီအဆင့်ဟာ input tokens အားလုံးကို တစ်ကြိမ်တည်း လုပ်ဆောင်ဖို့ လိုအပ်တာကြောင့် ကွန်ပျူတာအရင်းအမြစ်များစွာ လိုအပ်ပါတယ်။ ဒါကို အဖြေမရေးခင် စာပိုဒ်တစ်ခုလုံးကို ဖတ်ပြီး နားလည်တာနဲ့ တူတယ်လို့ တွေးကြည့်နိုင်ပါတယ်။ + +အောက်က interactive playground မှာ မတူညီတဲ့ tokenizers တွေနဲ့ စမ်းသပ်ကြည့်နိုင်ပါတယ်- + + + +### Decode အဆင့်[[the-decode-phase]] + +Prefill အဆင့်က input ကို လုပ်ဆောင်ပြီးနောက်မှာတော့ decode အဆင့်ကို ရောက်ရှိလာပါပြီ - ဒီနေရာမှာ စာသားထုတ်လုပ်ခြင်း အမှန်တကယ် ဖြစ်လာပါတယ်။ မော်ဒယ်က တစ်ကြိမ်ကို token တစ်ခုစီ ထုတ်လုပ်ပြီး၊ ဒါကို autoregressive process (အသစ်ထွက်လာတဲ့ token တိုင်းက ယခင် tokens အားလုံးပေါ် မူတည်ပါတယ်) လို့ ခေါ်ပါတယ်။ + +Decode အဆင့်မှာ အသစ်ထွက်လာတဲ့ token တိုင်းအတွက် အဓိကအချက်များစွာ ပါဝင်ပါတယ်- +1. **Attention Computation**: context ကို နားလည်ဖို့ ယခင် tokens အားလုံးကို ပြန်ကြည့်ခြင်း +2. **ဖြစ်နိုင်ခြေ တွက်ချက်ခြင်း (Probability Calculation)**: ဖြစ်နိုင်ခြေရှိတဲ့ နောက်ထပ် token တစ်ခုစီရဲ့ ဖြစ်နိုင်ခြေကို ဆုံးဖြတ်ခြင်း +3. **Token ရွေးချယ်ခြင်း (Token Selection)**: ဒီဖြစ်နိုင်ခြေတွေအပေါ် အခြေခံပြီး နောက်ထပ် token ကို ရွေးချယ်ခြင်း +4. **ဆက်လက်လုပ်ဆောင်မှု စစ်ဆေးခြင်း (Continuation Check)**: ဆက်လုပ်မလား ဒါမှမဟုတ် ထုတ်လုပ်မှုကို ရပ်မလားဆိုတာကို ဆုံးဖြတ်ခြင်း + +ဒီအဆင့်ဟာ မော်ဒယ်က ယခင်ထုတ်လုပ်ထားတဲ့ tokens အားလုံးနဲ့ ၎င်းတို့ရဲ့ ဆက်စပ်မှုတွေကို မှတ်ထားဖို့ လိုအပ်တာကြောင့် memory-intensive ဖြစ်ပါတယ်။ + +## Sampling နည်းဗျူဟာများ[[sampling-strategies]] + +မော်ဒယ်က စာသားတွေကို ဘယ်လိုထုတ်လုပ်တယ်ဆိုတာ နားလည်ပြီးပြီဆိုတော့ ဒီထုတ်လုပ်မှု လုပ်ငန်းစဉ်ကို ဘယ်လိုထိန်းချုပ်နိုင်မလဲဆိုတာကို လေ့လာကြည့်ရအောင်။ စာရေးဆရာတစ်ဦးက ပိုမိုတီထွင်ဖန်တီးမှုရှိမလား ဒါမှမဟုတ် ပိုမိုတိကျမလားဆိုတာ ရွေးချယ်နိုင်သလိုပဲ၊ ကျွန်တော်တို့လည်း မော်ဒယ်က tokens တွေကို ဘယ်လိုရွေးချယ်တယ်ဆိုတာကို ချိန်ညှိနိုင်ပါတယ်။ + +ဒီ Space မှာ SmolLM2 နဲ့ အခြေခံ decoding လုပ်ငန်းစဉ်ကို ကိုယ်တိုင် အပြန်အလှန်လုပ်ဆောင်နိုင်ပါတယ် (မှတ်ထားပါ၊ ဒီမော်ဒယ်အတွက် **<|im_end|>** ဖြစ်တဲ့ **EOS** token ကို မရောက်မချင်း decode လုပ်ပါလိမ့်မယ်)- + + + +### Token ရွေးချယ်မှုကို နားလည်ခြင်း- ဖြစ်နိုင်ခြေများမှ Token ရွေးချယ်မှုများဆီသို့[[understanding-token-selection-from-probabilities-to-token-choices]] + +မော်ဒယ်က နောက်ထပ် token ကို ရွေးချယ်ဖို့ လိုအပ်တဲ့အခါ၊ ၎င်းရဲ့ ဝေါဟာရ (vocabulary) ထဲက စကားလုံးတိုင်းအတွက် ကနဦး ဖြစ်နိုင်ခြေ (logits) တွေနဲ့ စတင်ပါတယ်။ ဒါပေမယ့် ဒီဖြစ်နိုင်ခြေတွေကို လက်တွေ့ရွေးချယ်မှုတွေအဖြစ် ဘယ်လိုပြောင်းလဲမလဲ။ လုပ်ငန်းစဉ်ကို ခွဲခြမ်းစိတ်ဖြာကြည့်ရအောင်- + +![image](https://huggingface.co/reasoning-course/images/resolve/main/inference/1.png) + +1. **Raw Logits**: ဒါတွေကို မော်ဒယ်ရဲ့ နောက်ထပ် ဖြစ်နိုင်ခြေရှိတဲ့ စကားလုံးတိုင်းအတွက် ကနဦး ခံစားချက်တွေလို့ တွေးကြည့်ပါ။ +2. **Temperature Control**: တီထွင်ဖန်တီးမှု ခလုတ်လိုပါပဲ - တန်ဖိုးမြင့်လေ (>1.0) ရွေးချယ်မှုတွေ ပိုမိုကျပန်းဆန်ပြီး ဖန်တီးမှုရှိလေ၊ တန်ဖိုးနိမ့်လေ (<1.0) ပိုမိုအာရုံစိုက်ပြီး တိကျလေ ဖြစ်ပါတယ်။ +3. **Top-p (Nucleus) Sampling**: ဖြစ်နိုင်ခြေရှိတဲ့ စကားလုံးအားလုံးကို ထည့်သွင်းစဉ်းစားမယ့်အစား၊ ကျွန်တော်တို့ ရွေးချယ်ထားတဲ့ ဖြစ်နိုင်ခြေ ကန့်သတ်ချက် (ဥပမာ- ထိပ်ဆုံး 90%) နဲ့ ကိုက်ညီတဲ့ အဖြစ်နိုင်ဆုံး စကားလုံးတွေကိုပဲ ကြည့်ပါတယ်။ +4. **Top-k Filtering**: အခြားနည်းလမ်းတစ်ခုဖြစ်ပြီး၊ ကျွန်တော်တို့ဟာ အဖြစ်နိုင်ဆုံး နောက်ထပ်စကားလုံး k လုံးကိုပဲ ထည့်သွင်းစဉ်းစားပါတယ်။ + +### ထပ်ခါတလဲလဲဖြစ်ခြင်းကို ထိန်းချုပ်ခြင်း- Output ကို လတ်ဆတ်နေစေခြင်း[[managing-repetition-keeping-output-fresh]] + +LLMs တွေနဲ့ အဖြစ်များတဲ့ စိန်ခေါ်မှုတစ်ခုကတော့ သူတို့ကိုယ်သူတို့ ထပ်ခါတလဲလဲပြောတတ်တဲ့ သဘောရှိခြင်းပါ - အချက်အလက်တွေကို အကြိမ်ကြိမ် ပြန်ပြောတတ်တဲ့ စကားပြောသူတစ်ဦးနဲ့ တူပါတယ်။ ဒါကို ဖြေရှင်းဖို့အတွက် ကျွန်တော်တို့ဟာ ပြစ်ဒဏ်နှစ်မျိုးကို အသုံးပြုပါတယ်- + +1. **Presence Penalty**: ယခင်က ပါဝင်ခဲ့ဖူးတဲ့ token တစ်ခုစီအတွက် အကြိမ်အရေအတွက် ဘယ်လောက်ပဲ ဖြစ်ပါစေ၊ သတ်မှတ်ထားတဲ့ ပြစ်ဒဏ်တစ်ခုကို ပေးပါတယ်။ ဒါက မော်ဒယ်ကို တူညီတဲ့ စကားလုံးတွေကို ထပ်ခါတလဲလဲ အသုံးမပြုမိအောင် ကူညီပေးပါတယ်။ +2. **Frequency Penalty**: token တစ်ခု ဘယ်နှစ်ကြိမ် အသုံးပြုပြီးပြီလဲဆိုတာပေါ်မူတည်ပြီး တိုးလာတဲ့ ပြစ်ဒဏ်တစ်ခုပါ။ စကားလုံးတစ်ခု ပိုမိုပါဝင်လေ၊ နောက်ထပ်ရွေးချယ်ခံရဖို့ အခွင့်အလမ်း နည်းလေ ဖြစ်ပါတယ်။ + +![image](https://huggingface.co/reasoning-course/images/resolve/main/inference/2.png) + +ဒီပြစ်ဒဏ်တွေကို token ရွေးချယ်မှု လုပ်ငန်းစဉ်ရဲ့ အစောပိုင်းမှာ လိမ်းကျံပြီး၊ အခြား sampling နည်းဗျူဟာတွေကို အသုံးမပြုခင် ကနဦး ဖြစ်နိုင်ခြေတွေကို ချိန်ညှိပေးပါတယ်။ ဒါတွေကို မော်ဒယ်ကို ဝေါဟာရအသစ်တွေ ရှာဖွေဖို့ နူးညံ့စွာ တွန်းအားပေးတာနဲ့ တူတယ်လို့ တွေးကြည့်နိုင်ပါတယ်။ + +### ထုတ်လုပ်မှု အရှည်ကို ထိန်းချုပ်ခြင်း- ကန့်သတ်ချက်များ သတ်မှတ်ခြင်း[[controlling-generation-length-setting-boundaries]] + +ကောင်းမွန်တဲ့ ပုံပြင်တစ်ခုမှာ သင့်လျော်တဲ့ စည်းမျဉ်းစည်းကမ်းနဲ့ အရှည်ရှိဖို့ လိုအပ်သလိုပဲ၊ ကျွန်တော်တို့ရဲ့ LLM က ဘယ်လောက်များများ စာသားထုတ်လုပ်မလဲဆိုတာကို ထိန်းချုပ်ဖို့ နည်းလမ်းတွေ လိုအပ်ပါတယ်။ ဒါက လက်တွေ့အသုံးချမှုတွေအတွက် အရေးကြီးပါတယ် - tweet တစ်ခုလောက်တိုတဲ့ အဖြေဖြစ်စေ၊ blog post အပြည့်အစုံဖြစ်စေပေါ့။ + +ထုတ်လုပ်မှု အရှည်ကို နည်းလမ်းများစွာနဲ့ ထိန်းချုပ်နိုင်ပါတယ်- +1. **Token Limits**: အနည်းဆုံးနဲ့ အများဆုံး token အရေအတွက်ကို သတ်မှတ်ခြင်း +2. **Stop Sequences**: ထုတ်လုပ်မှု ပြီးဆုံးကြောင်း အချက်ပြတဲ့ သီးခြားပုံစံတွေကို သတ်မှတ်ခြင်း +3. **End-of-Sequence Detection**: မော်ဒယ်ကို သူ့ဘာသာသူ အဖြေကို သဘာဝအတိုင်း နိဂုံးချုပ်စေခြင်း + +ဥပမာအားဖြင့်၊ ကျွန်တော်တို့ဟာ စာပိုဒ်တစ်ပိုဒ်တည်းကိုပဲ ထုတ်လုပ်ချင်တယ်ဆိုရင် အများဆုံး tokens 100 သတ်မှတ်ပြီး "\n\n" ကို stop sequence အဖြစ် အသုံးပြုနိုင်ပါတယ်။ ဒါက ကျွန်တော်တို့ရဲ့ output က ရည်ရွယ်ချက်နဲ့ ကိုက်ညီတဲ့ အရွယ်အစားနဲ့ အာရုံစိုက်မှု ရှိစေပါတယ်။ + +![image](https://huggingface.co/reasoning-course/images/resolve/main/inference/3.png) + +### Beam Search: ပိုမိုကောင်းမွန်တဲ့ ဆက်စပ်မှုအတွက် ကြိုတင်မျှော်မှန်းခြင်း[[beam-search-looking-ahead-for-better-coherence]] + +ကျွန်တော်တို့ ခုထိ ဆွေးနွေးခဲ့တဲ့ နည်းဗျူဟာတွေက တစ်ကြိမ်ကို token တစ်ခုစီ ဆုံးဖြတ်ချက်ချပေမယ့်၊ beam search ကတော့ ပိုမိုပြည့်စုံတဲ့ ချဉ်းကပ်မှုကို လုပ်ဆောင်ပါတယ်။ ခြေလှမ်းတိုင်းမှာ ရွေးချယ်မှုတစ်ခုတည်းကိုပဲ လုပ်ဆောင်မယ့်အစား၊ ၎င်းဟာ ကစားသမားတစ်ဦးက အရှေ့ကို အကြိမ်ကြိမ် တွေးတောသလိုမျိုး ဖြစ်နိုင်ခြေရှိတဲ့ လမ်းကြောင်းပေါင်းစုံကို တစ်ပြိုင်နက်တည်း ရှာဖွေပါတယ်။ + +![image](https://huggingface.co/reasoning-course/images/resolve/main/inference/4.png) + +ဒါက ဘယ်လိုအလုပ်လုပ်လဲဆိုတာကို ကြည့်ရအောင်- +1. ခြေလှမ်းတိုင်းမှာ၊ ဖြစ်နိုင်ခြေရှိတဲ့ candidate sequence အများအပြားကို ထိန်းသိမ်းထားပါတယ် (များသောအားဖြင့် ၅-၁၀ ခု)။ +2. candidate တစ်ခုစီအတွက်၊ နောက်ထပ် token ရဲ့ ဖြစ်နိုင်ခြေတွေကို တွက်ချက်ပါတယ်။ +3. sequence တွေနဲ့ နောက်ထပ် token တွေရဲ့ အဖြစ်နိုင်ဆုံး ပေါင်းစပ်မှုတွေကိုသာ ထိန်းသိမ်းထားပါတယ်။ +4. လိုချင်တဲ့ အရှည် ဒါမှမဟုတ် ရပ်တန့်တဲ့အခြေအနေကို ရောက်တဲ့အထိ ဒီလုပ်ငန်းစဉ်ကို ဆက်လုပ်ပါတယ်။ +5. အလုံးစုံ ဖြစ်နိုင်ခြေအမြင့်ဆုံးရှိတဲ့ sequence ကို ရွေးချယ်ပါတယ်။ + +ဒီနေရာမှာ beam search ကို ပုံမှန်ကြည့်ရှုနိုင်ပါတယ်- + + + +ဒီချဉ်းကပ်မှုက ပိုမိုဆက်စပ်မှုရှိပြီး သဒ္ဒါမှန်ကန်တဲ့ စာသားတွေကို မကြာခဏ ထုတ်လုပ်ပေးတတ်ပေမယ့်၊ ရိုးရှင်းတဲ့ နည်းလမ်းတွေထက် ကွန်ပျူတာ အရင်းအမြစ် ပိုမိုလိုအပ်ပါတယ်။ + +## လက်တွေ့ စိန်ခေါ်မှုများနှင့် အကောင်းဆုံးဖြစ်အောင် ပြုလုပ်ခြင်း (Optimization)[[practical-challenges-and-optimization]] + +LLM inference ကို ကျွန်တော်တို့ လေ့လာမှု ပြီးဆုံးခါနီးမှာ ဒီမော်ဒယ်တွေကို အသုံးပြုတဲ့အခါ သင်ရင်ဆိုင်ရမယ့် လက်တွေ့ကျတဲ့ စိန်ခေါ်မှုတွေနဲ့ ၎င်းတို့ရဲ့ စွမ်းဆောင်ရည်ကို ဘယ်လိုတိုင်းတာပြီး အကောင်းဆုံးဖြစ်အောင် လုပ်ရမလဲဆိုတာကို ကြည့်ရအောင်။ + +### အဓိက စွမ်းဆောင်ရည် တိုင်းတာချက်များ (Key Performance Metrics)[[key-performance-metrics]] + +LLMs တွေနဲ့ အလုပ်လုပ်တဲ့အခါ အရေးကြီးတဲ့ တိုင်းတာချက် (၄) ခုက သင်ရဲ့ implement လုပ်မယ့် ဆုံးဖြတ်ချက်တွေကို ပုံဖော်ပေးပါလိမ့်မယ်- + +1. **ပထမဆုံး Token ရရှိချိန် (Time to First Token - TTFT)**: ပထမဆုံး အဖြေကို ဘယ်လောက် မြန်မြန်ရနိုင်မလဲ။ ဒါက user experience အတွက် အရေးကြီးပြီး prefill အဆင့်ကြောင့် အဓိကအားဖြင့် ထိခိုက်ပါတယ်။ +2. **Output Token တစ်ခုစီအတွက် အချိန် (Time Per Output Token - TPOT)**: နောက်ထပ် tokens တွေကို ဘယ်လောက် မြန်မြန်ထုတ်လုပ်နိုင်မလဲ။ ဒါက အလုံးစုံ ထုတ်လုပ်မှု အမြန်နှုန်းကို ဆုံးဖြတ်ပါတယ်။ +3. **Throughput**: တစ်ပြိုင်နက်တည်း request ဘယ်နှစ်ခုကို ကိုင်တွယ်နိုင်မလဲ။ ဒါက scaling နဲ့ ကုန်ကျစရိတ် ထိရောက်မှုအပေါ် သက်ရောက်မှုရှိပါတယ်။ +4. **VRAM အသုံးပြုမှု (VRAM Usage)**: GPU memory ဘယ်လောက်လိုအပ်မလဲ။ ဒါက လက်တွေ့အသုံးချမှုတွေမှာ အဓိက ကန့်သတ်ချက် ဖြစ်လာတတ်ပါတယ်။ + +### Context Length စိန်ခေါ်မှု[[the-context-length-challenge]] + +LLM inference မှာ အရေးအကြီးဆုံး စိန်ခေါ်မှုတစ်ခုကတော့ context length ကို ထိထိရောက်ရောက် စီမံခန့်ခွဲခြင်းပါပဲ။ ပိုမိုရှည်လျားတဲ့ contexts တွေက အချက်အလက် ပိုပေးပေမယ့် ကုန်ကျစရိတ်များစွာနဲ့ လာပါတယ်- + +- **Memory အသုံးပြုမှု**: context length နဲ့အမျှ quadratically တိုးလာပါတယ်။ +- **လုပ်ဆောင်မှု အမြန်နှုန်း (Processing Speed)**: ပိုမိုရှည်လျားတဲ့ contexts တွေနဲ့အမျှ linearly လျော့ကျသွားပါတယ်။ +- **အရင်းအမြစ် ခွဲဝေမှု (Resource Allocation)**: VRAM အသုံးပြုမှုကို သေချာမျှတအောင် ထိန်းညှိဖို့ လိုအပ်ပါတယ်။ + +[Qwen2.5-1M](https://huggingface.co/Qwen/Qwen2.5-14B-Instruct-1M) လို ခေတ်မီမော်ဒယ်တွေကတော့ 1M token context windows တွေကို စွဲမက်ဖွယ်ကောင်းအောင် ပေးစွမ်းနိုင်ပေမယ့်၊ ဒါဟာ inference လုပ်ဆောင်ချိန် သိသိသာသာ နှေးကွေးခြင်းရဲ့ အဖိုးအခနဲ့ လာပါတယ်။ အဓိကသော့ချက်ကတော့ သင်ရဲ့ သီးခြားအသုံးပြုမှုအတွက် မှန်ကန်တဲ့ မျှတမှုကို ရှာဖွေဖို့ပါပဲ။ + +
+
+
+
+ Input Text (Raw) +
+
+
+ Tokenized Input +
+
+
+
+ Context Window
(ဥပမာ- 4K tokens) +
+
+
+
+
+
+
+
+
+
+
+ Memory အသုံးပြုမှု
∝ အရှည်² +
+
+
+
+ လုပ်ဆောင်ချိန်
∝ အရှည် +
+
+
+
+
+ +### KV Cache Optimization[[the-kv-cache-optimization]] + +ဒီစိန်ခေါ်မှုတွေကို ဖြေရှင်းဖို့အတွက် အစွမ်းအထက်ဆုံး optimization တစ်ခုကတော့ KV (Key-Value) caching ပါ။ ဒီနည်းပညာက ကြားဖြတ်တွက်ချက်မှုတွေကို သိုလှောင်ပြီး ပြန်လည်အသုံးပြုခြင်းဖြင့် inference အမြန်နှုန်းကို သိသိသာသာ တိုးတက်စေပါတယ်။ ဒီ optimization က- +- ထပ်ခါတလဲလဲ တွက်ချက်မှုတွေကို လျှော့ချပေးပါတယ် +- ထုတ်လုပ်မှု အမြန်နှုန်းကို တိုးတက်စေပါတယ် +- ရှည်လျားတဲ့ context ထုတ်လုပ်မှုကို လက်တွေ့ကျအောင် လုပ်ဆောင်ပေးပါတယ် + +အားနည်းချက်ကတော့ memory အသုံးပြုမှု ပိုများလာတာပါပဲ၊ ဒါပေမယ့် စွမ်းဆောင်ရည် အကျိုးကျေးဇူးတွေက ဒီကုန်ကျစရိတ်ထက် များသောအားဖြင့် သာလွန်ပါတယ်။ + +## နိဂုံး[[conclusion]] + +LLM inference ကို နားလည်ထားခြင်းက ဒီအစွမ်းထက်တဲ့ မော်ဒယ်တွေကို ထိထိရောက်ရောက် အသုံးပြုပြီး အကောင်းဆုံးဖြစ်အောင် လုပ်ဆောင်ဖို့အတွက် အရေးကြီးပါတယ်။ ကျွန်တော်တို့ဟာ အဓိကအစိတ်အပိုင်းတွေကို လေ့လာခဲ့ပြီးပါပြီ- + +- Attention နဲ့ context ရဲ့ အခြေခံအခန်းကဏ္ဍ +- နှစ်ဆင့်ပါသော inference လုပ်ငန်းစဉ် +- ထုတ်လုပ်မှုကို ထိန်းချုပ်ရန် အမျိုးမျိုးသော sampling နည်းဗျူဟာများ +- လက်တွေ့ စိန်ခေါ်မှုများနှင့် optimization များ + +ဒီသဘောတရားတွေကို ကျွမ်းကျင်ခြင်းဖြင့် LLMs တွေကို ထိရောက်စွာနဲ့ စွမ်းဆောင်ရည်မြင့်စွာ အသုံးပြုနိုင်တဲ့ applications တွေ တည်ဆောက်ဖို့ သင်ပိုပြီး အသင့်ဖြစ်လာပါလိမ့်မယ်။ + +LLM inference နယ်ပယ်ဟာ အဆက်မပြတ် တိုးတက်ပြောင်းလဲနေပြီး၊ နည်းစနစ်အသစ်တွေနဲ့ optimization တွေ ပုံမှန်ပေါ်ထွက်လာနေတာကို သတိရပါ။ သင်ရဲ့ သီးခြားအသုံးပြုမှုအတွက် ဘယ်အရာက အကောင်းဆုံးလဲဆိုတာကို ရှာဖွေဖို့ စူးစမ်းလိုစိတ်ထားပြီး မတူညီတဲ့ ချဉ်းကပ်မှုတွေနဲ့ ဆက်လက်စမ်းသပ်ပါ။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Inference**: လေ့ကျင့်ပြီးသား Artificial Intelligence (AI) မော်ဒယ်တစ်ခုကို အသုံးပြုပြီး input data ကနေ ခန့်မှန်းချက်တွေ ဒါမှမဟုတ် output တွေကို ထုတ်လုပ်တဲ့ လုပ်ငန်းစဉ်။ +* **Large Language Models (LLMs)**: လူသားဘာသာစကားကို နားလည်ပြီး ထုတ်လုပ်ပေးနိုင်တဲ့ အလွန်ကြီးမားတဲ့ Artificial Intelligence (AI) မော်ဒယ်တွေ ဖြစ်ပါတယ်။ ၎င်းတို့ဟာ ဒေတာအမြောက်အမြားနဲ့ သင်ကြားလေ့ကျင့်ထားပြီး စာရေးတာ၊ မေးခွန်းဖြေတာ စတဲ့ ဘာသာစကားဆိုင်ရာ လုပ်ငန်းမျိုးစုံကို လုပ်ဆောင်နိုင်ပါတယ်။ +* **Transformer Architecture**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Text Generation**: AI မော်ဒယ်များကို အသုံးပြု၍ လူသားကဲ့သို့သော စာသားအသစ်များ ဖန်တီးခြင်း။ +* **Text Classification**: စာသားတစ်ခုကို သတ်မှတ်ထားသော အမျိုးအစားများ သို့မဟုတ် အတန်းများထဲသို့ ခွဲခြားသတ်မှတ်ခြင်း။ +* **Summarization**: စာသားရှည်ကြီးတစ်ခုကို အဓိကအချက်အလက်များ မပျောက်ပျက်စေဘဲ အကျဉ်းချုံးဖော်ပြခြင်း။ +* **Prompt**: Large Language Models (LLMs) ကို တိကျသောလုပ်ငန်းတစ်ခု လုပ်ဆောင်ရန် သို့မဟုတ် အချက်အလက်ပေးရန်အတွက် ပေးပို့သော input text သို့မဟုတ် မေးခွန်း။ +* **Parameters**: Machine Learning မော်ဒယ်တစ်ခု၏ သင်ယူနိုင်သော အစိတ်အပိုင်းများ။ ၎င်းတို့သည် လေ့ကျင့်နေစဉ်အတွင်း ဒေတာများမှ ပုံစံများကို သင်ယူကာ ချိန်ညှိပေးသည်။ +* **Token**: စာသား (သို့မဟုတ် အခြားဒေတာ) ကို AI မော်ဒယ်များ စီမံဆောင်ရွက်နိုင်ရန် ပိုင်းခြားထားသော အသေးငယ်ဆုံးယူနစ်။ စကားလုံး၊ စာလုံးတစ်ပိုင်း သို့မဟုတ် တစ်ခုတည်းသော စာလုံးတစ်လုံး ဖြစ်နိုင်သည်။ +* **Sequence**: အစဉ်လိုက် စီစဉ်ထားသော tokens များ။ +* **Coherent**: ယုတ္တိရှိရှိ ဆက်စပ်နေခြင်း၊ နားလည်လွယ်ခြင်း။ +* **Context**: စကားလုံး၊ စာကြောင်း သို့မဟုတ် အကြောင်းအရာတစ်ခုရဲ့ အဓိပ္ပာယ်ကို နားလည်စေရန် ကူညီပေးသော ပတ်ဝန်းကျင်ရှိ အချက်အလက်များ။ +* **Attention Mechanism**: Transformer မော်ဒယ်များတွင် အသုံးပြုသော နည်းစနစ်တစ်ခုဖြစ်ပြီး input sequence အတွင်းရှိ အရေးပါသော အစိတ်အပိုင်းများကို အာရုံစိုက်ပြီး ဆက်နွယ်မှုများကို သင်ယူစေသည်။ +* **BERT (Bidirectional Encoder Representations from Transformers)**: Google မှ တီထွင်ထားသော Transformer-based NLP မော်ဒယ်တစ်ခု။ +* **GPT-2 (Generative Pre-trained Transformer 2)**: OpenAI မှ တီထွင်ထားသော Transformer-based NLP မော်ဒယ်တစ်ခု။ +* **Neural Networks**: လူသားဦးနှောက်၏ လုပ်ဆောင်မှုပုံစံကို အတုယူထားသော ကွန်ပျူတာစနစ်များ။ +* **Context Length**: Large Language Model (LLM) တစ်ခုက တစ်ကြိမ်တည်း လုပ်ဆောင်နိုင်သော အများဆုံး token အရေအတွက်။ +* **Working Memory**: မော်ဒယ်က လက်ရှိလုပ်ငန်းဆောင်တာအတွက် လိုအပ်တဲ့ အချက်အလက်တွေကို ခဏတာ ထိန်းသိမ်းထားတဲ့ မှတ်ဉာဏ်။ +* **Hardware Constraints**: ကွန်ပျူတာစနစ်ရဲ့ ရုပ်ပိုင်းဆိုင်ရာ ကန့်သတ်ချက်များ (ဥပမာ- GPU memory, processing power)။ +* **Computational Costs**: ကွန်ပျူတာအရင်းအမြစ်များ (ဥပမာ- လျှပ်စစ်ဓာတ်အား၊ စက်အချိန်) အသုံးပြုခြင်းအတွက် ကုန်ကျစရိတ်။ +* **Tokenization**: input text ကို AI မော်ဒယ် နားလည်နိုင်တဲ့ tokens တွေအဖြစ် ပြောင်းလဲတဲ့ လုပ်ငန်းစဉ်။ +* **Embeddings**: tokens တွေရဲ့ အဓိပ္ပာယ်ကို ဖမ်းယူထားတဲ့ ဂဏန်းဆိုင်ရာ ကိုယ်စားပြုမှုများ။ +* **Neural Networks**: လူသားဦးနှောက်၏ လုပ်ဆောင်မှုပုံစံကို အတုယူထားသော ကွန်ပျူတာစနစ်များ။ +* **Autoregressive Process**: နောက်ထပ်ထုတ်လုပ်မယ့် output က ယခင်ထုတ်လုပ်ခဲ့တဲ့ outputs အားလုံးပေါ် မူတည်နေတဲ့ လုပ်ငန်းစဉ်။ +* **Logits**: မော်ဒယ်က နောက်ထပ် ဖြစ်နိုင်ခြေရှိတဲ့ token တစ်ခုစီအတွက် ထုတ်ပေးတဲ့ ကနဦး၊ အဆင့်မမီသေးတဲ့ ဖြစ်နိုင်ခြေတန်ဖိုးများ။ +* **Temperature Control**: LLM ရဲ့ output မှာ ကျပန်းဆန်မှု (randomness) သို့မဟုတ် တီထွင်ဖန်တီးမှု (creativity) ပမာဏကို ထိန်းညှိရန် အသုံးပြုသော parameter တစ်ခု။ +* **Top-p (Nucleus) Sampling**: နောက်ထပ် token ကို ရွေးချယ်ရာတွင် ဖြစ်နိုင်ခြေအမြင့်ဆုံး token များ၏ စုစုပေါင်းဖြစ်နိုင်ခြေ ကန့်သတ်ချက်အောက်တွင်ရှိသော token များကိုသာ ထည့်သွင်းစဉ်းစားခြင်း။ +* **Top-k Filtering**: နောက်ထပ် token ကို ရွေးချယ်ရာတွင် အဖြစ်နိုင်ဆုံး k လုံးကိုသာ ထည့်သွင်းစဉ်းစားခြင်း။ +* **Presence Penalty**: ယခင်က ပေါ်ထွက်ဖူးသော token များအတွက် ပုံသေပြစ်ဒဏ်ချမှတ်ခြင်း။ +* **Frequency Penalty**: ယခင်က ပေါ်ထွက်ဖူးသော token များအတွက် ၎င်းတို့ပေါ်ထွက်သည့် အကြိမ်အရေအတွက်အလိုက် ပြစ်ဒဏ်ကို တိုးမြှင့်ချမှတ်ခြင်း။ +* **Token Limits**: ထုတ်လုပ်မည့် token အရေအတွက်အတွက် အနည်းဆုံးနှင့် အများဆုံး ကန့်သတ်ချက်များ။ +* **Stop Sequences**: စာသားထုတ်လုပ်မှုကို ရပ်တန့်ရန် အချက်ပြသည့် သတ်မှတ်ထားသော စာသားပုံစံများ။ +* **End-of-Sequence (EOS) Token**: မော်ဒယ်က စာသားထုတ်လုပ်မှုကို ပြီးဆုံးရန် အချက်ပြသည့် အထူး token တစ်ခု။ +* **Beam Search**: စာသားထုတ်လုပ်ရာတွင် ဖြစ်နိုင်ခြေအကောင်းဆုံး sequence များစွာကို တစ်ပြိုင်နက်တည်း ရှာဖွေပြီး အကောင်းဆုံးကို ရွေးချယ်သည့် နည်းဗျူဟာ။ +* **Time to First Token (TTFT)**: LLM တစ်ခုက input prompt ကို လက်ခံရရှိပြီးနောက် ပထမဆုံး token ကို ထုတ်လုပ်ရန် ကြာမြင့်သော အချိန်။ +* **Time Per Output Token (TPOT)**: LLM တစ်ခုက နောက်ဆက်တွဲ output token တစ်ခုစီကို ထုတ်လုပ်ရန် ကြာမြင့်သော အချိန်။ +* **Throughput**: LLM စနစ်တစ်ခုက သတ်မှတ်ထားသော အချိန်ကာလတစ်ခုအတွင်း လုပ်ဆောင်နိုင်သော requests အရေအတွက်။ +* **VRAM Usage**: GPU (Graphics Processing Unit) ၏ memory အသုံးပြုမှု။ +* **KV (Key-Value) Caching**: LLM inference တွင် အကြားအချက်အလက်များကို သိုလှောင်ပြီး ပြန်လည်အသုံးပြုခြင်းဖြင့် လုပ်ဆောင်မှုအမြန်နှုန်းကို မြှင့်တင်ပေးသော နည်းပညာ။ \ No newline at end of file diff --git a/chapters/my/chapter1/9.mdx b/chapters/my/chapter1/9.mdx new file mode 100644 index 000000000..71141e5da --- /dev/null +++ b/chapters/my/chapter1/9.mdx @@ -0,0 +1,52 @@ +# ဘက်လိုက်မှုနှင့် ကန့်သတ်ချက်များ[[bias-and-limitations]] + + + +အကယ်၍ သင်က pre-trained model တစ်ခုကို ဒါမှမဟုတ် fine-tuned version တစ်ခုကို ထုတ်လုပ်မှု (production) မှာ အသုံးပြုဖို့ ရည်ရွယ်တယ်ဆိုရင်၊ ဒီမော်ဒယ်တွေဟာ အစွမ်းထက်တဲ့ ကိရိယာတွေဖြစ်ပေမယ့် ကန့်သတ်ချက်တွေနဲ့ လာတယ်ဆိုတာကို သတိပြုသင့်ပါတယ်။ အကြီးဆုံး ကန့်သတ်ချက်ကတော့ ဒေတာပမာဏများစွာပေါ်မှာ pre-training လုပ်နိုင်ဖို့အတွက် သုတေသီတွေဟာ အင်တာနက်ပေါ်က တွေ့သမျှ အကြောင်းအရာအားလုံးကို ရယူကြပြီး၊ အကောင်းဆုံးအရာတွေရော အဆိုးဆုံးအရာတွေရော ပါဝင်လာတတ်ပါတယ်။ + +ဥပမာအနေနဲ့ ရှင်းပြရရင် BERT မော်ဒယ်ကို အသုံးပြုထားတဲ့ `fill-mask` pipeline ဥပမာကို ပြန်သွားကြည့်ရအောင်။ + +```python +from transformers import pipeline + +unmasker = pipeline("fill-mask", model="bert-base-uncased") +result = unmasker("This man works as a [MASK].") +print([r["token_str"] for r in result]) + +result = unmasker("This woman works as a [MASK].") +print([r["token_str"] for r in result]) +``` + +```python out +['lawyer', 'carpenter', 'doctor', 'waiter', 'mechanic'] +['nurse', 'waitress', 'teacher', 'maid', 'prostitute'] +``` + +ဒီစာကြောင်းနှစ်ကြောင်းမှာ ပျောက်ဆုံးနေတဲ့ စကားလုံးကို ဖြည့်ဖို့ မေးတဲ့အခါ၊ မော်ဒယ်က လိင်ကွဲပြားမှုမရှိတဲ့ အဖြေတစ်ခု (waiter/waitress) ကိုသာ ပေးပါတယ်။ ကျန်တဲ့ အလုပ်အကိုင်တွေကတော့ သီးခြားလိင်နဲ့ ပုံမှန်အားဖြင့် ဆက်စပ်နေတဲ့ အလုပ်အကိုင်တွေ ဖြစ်ပါတယ်—ဟုတ်ပါတယ်၊ "prostitute" က "woman" နဲ့ "work" တို့နဲ့ မော်ဒယ်က ဆက်စပ်တဲ့ ဖြစ်နိုင်ခြေ ထိပ်ဆုံး ၅ ခုထဲမှာ ပါဝင်ခဲ့ပါတယ်။ BERT ဟာ အင်တာနက်တစ်လျှောက်ကနေ ဒေတာတွေကို ရယူပြီး တည်ဆောက်ထားတဲ့ ရှားပါး Transformer မော်ဒယ်တွေထဲက တစ်ခု မဟုတ်ဘဲ၊ ကြည့်ရတာ ကြားနေတဲ့ ဒေတာ (English Wikipedia နဲ့ BookCorpus datasets တွေနဲ့ လေ့ကျင့်ထားပါတယ်) ကို အသုံးပြုထားတာ ဖြစ်ပေမယ့်လည်း ဒီလိုဖြစ်တတ်ပါတယ်။ + +ဒီကိရိယာတွေကို အသုံးပြုတဲ့အခါ သင်သုံးနေတဲ့ မူရင်းမော်ဒယ်ဟာ Sexist၊ Racist ဒါမှမဟုတ် homophobic အကြောင်းအရာတွေကို အလွန်လွယ်ကူစွာ ထုတ်လုပ်နိုင်တယ်ဆိုတာကို သတိရနေဖို့ လိုအပ်ပါတယ်။ သင်ရဲ့ ဒေတာပေါ်မှာ မော်ဒယ်ကို fine-tuning လုပ်တာဟာ ဒီအတွင်းပိုင်း ဘက်လိုက်မှုကို ပျောက်ကွယ်သွားစေမှာ မဟုတ်ပါဘူး။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Bias**: ဒေတာအစုအဝေး (dataset) သို့မဟုတ် မော်ဒယ်၏ လေ့ကျင့်မှုပုံစံကြောင့် ဖြစ်ပေါ်လာသော ဘက်လိုက်မှုများ။ ဥပမာ - လူမျိုး၊ လိင်၊ ဘာသာ စသည်တို့ကို ခွဲခြားဆက်ဆံခြင်း။ +* **Limitations**: AI မော်ဒယ်များ၏ လုပ်ဆောင်နိုင်စွမ်းနှင့် ပတ်သက်သော ကန့်သတ်ချက်များ၊ အားနည်းချက်များ။ +* **Pretrained Model**: ဒေတာအမြောက်အမြားပေါ်တွင် ကြိုတင်လေ့ကျင့်ထားပြီးသား Artificial Intelligence (AI) မော်ဒယ်တစ်ခု။ ၎င်းတို့ကို အခြားလုပ်ငန်းများအတွက် အခြေခံအဖြစ် ပြန်လည်အသုံးပြုနိုင်သည်။ +* **Fine-tuned Version**: ကြိုတင်လေ့ကျင့်ထားပြီးသား (pre-trained) မော်ဒယ်တစ်ခုကို သီးခြားလုပ်ငန်းတစ်ခု (specific task) အတွက် အနည်းငယ်သော ဒေတာနဲ့ ထပ်မံလေ့ကျင့်ပေးထားသော မော်ဒယ်၏ ပုံစံ။ +* **Production**: ဆော့ဖ်ဝဲလ် သို့မဟုတ် မော်ဒယ်တစ်ခုကို အမှန်တကယ် အသုံးပြုနေသော လက်တွေ့ပတ်ဝန်းကျင် သို့မဟုတ် စနစ်။ +* **Transformer Models**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Scrape**: အင်တာနက်ပေါ်မှ ဒေတာများကို အလိုအလျောက် စုဆောင်းခြင်း။ +* **`fill-mask` pipeline**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ function တစ်ခုဖြစ်ပြီး input text ထဲက `[MASK]` နေရာမှာ ပျောက်ဆုံးနေတဲ့ စကားလုံးကို ခန့်မှန်းပြီး ဖြည့်စွက်ပေးတဲ့ လုပ်ငန်းဆောင်တာ။ +* **BERT (Bidirectional Encoder Representations from Transformers)**: Google မှ တီထွင်ထားသော Transformer-based NLP မော်ဒယ်တစ်ခု။ +* **`bert-base-uncased`**: BERT မော်ဒယ်၏ အခြေခံဗားရှင်း (base version) ဖြစ်ပြီး စာလုံးအကြီးအသေး ခွဲခြားခြင်းမရှိ (uncased) ဘဲ လေ့ကျင့်ထားသည်။ +* **`token_str`**: ထုတ်လုပ်လိုက်သော token ကို ကိုယ်စားပြုသော စာသား string။ +* **English Wikipedia**: အင်္ဂလိပ်ဘာသာစကားဖြင့် ရေးသားထားသော Wikipedia စွယ်စုံကျမ်း၏ အချက်အလက်များ။ +* **BookCorpus**: စာအုပ်များစွာမှ စုဆောင်းထားသော စာသားဒေတာအစုအဝေးတစ်ခု။ +* **Dataset**: AI မော်ဒယ်တွေ လေ့ကျင့်ဖို့အတွက် အသုံးပြုတဲ့ ဒေတာအစုအဝေးတစ်ခုပါ။ +* **Sexist**: လိင်အပေါ်အခြေခံပြီး ခွဲခြားဆက်ဆံခြင်း သို့မဟုတ် ဘက်လိုက်ခြင်း။ +* **Racist**: လူမျိုးအပေါ်အခြေခံပြီး ခွဲခြားဆက်ဆံခြင်း သို့မဟုတ် ဘက်လိုက်ခြင်း။ +* **Homophobic**: လိင်တူချစ်သူများကို မနှစ်သက်ခြင်း သို့မဟုတ် ခွဲခြားဆက်ဆံခြင်း။ \ No newline at end of file diff --git a/chapters/my/chapter2/1.mdx b/chapters/my/chapter2/1.mdx new file mode 100644 index 000000000..a6f0750ad --- /dev/null +++ b/chapters/my/chapter2/1.mdx @@ -0,0 +1,53 @@ +# နိဒါန်း[[introduction]] + + + +[Chapter 1](/course/chapter1) မှာ သင်တွေ့ခဲ့ရသလို Transformer မော်ဒယ်တွေဟာ များသောအားဖြင့် အရွယ်အစား အလွန်ကြီးမားပါတယ်။ Parameters သန်းပေါင်းများစွာကနေ ဘီလီယံပေါင်းများစွာအထိ ရှိတာကြောင့် ဒီမော်ဒယ်တွေကို လေ့ကျင့်တာနဲ့ အသုံးပြုတာ (deploy) ဟာ ရှုပ်ထွေးတဲ့ လုပ်ငန်းစဉ်တစ်ခု ဖြစ်ပါတယ်။ ဒီအပြင်၊ မော်ဒယ်အသစ်တွေ နေ့တိုင်းနီးပါး ထွက်ပေါ်လာပြီး တစ်ခုချင်းစီမှာ သူ့ရဲ့ကိုယ်ပိုင် implement လုပ်ပုံတွေရှိတာကြောင့် ဒါတွေကို အားလုံး စမ်းသပ်ကြည့်ဖို့က မလွယ်ပါဘူး။ + +🤗 Transformers library ကို ဒီပြဿနာကို ဖြေရှင်းဖို့အတွက် ဖန်တီးခဲ့တာပါ။ သူ့ရဲ့ ရည်ရွယ်ချက်ကတော့ Transformer မော်ဒယ်တိုင်းကို load လုပ်နိုင်၊ train လုပ်နိုင်ပြီး save လုပ်နိုင်တဲ့ API တစ်ခုတည်းကို ပံ့ပိုးပေးဖို့ပါပဲ။ library ရဲ့ အဓိကအင်္ဂါရပ်တွေကတော့- + +- **အသုံးပြုရလွယ်ကူမှု**: state-of-the-art Natural Language Processing (NLP) မော်ဒယ်တစ်ခုကို inference အတွက် download လုပ်တာ၊ load လုပ်တာနဲ့ အသုံးပြုတာကို code နှစ်ကြောင်းတည်းနဲ့ လုပ်ဆောင်နိုင်ပါတယ်။ +- **ပြောင်းလွယ်ပြင်လွယ်မှု (Flexibility)**: မူရင်းအားဖြင့် မော်ဒယ်အားလုံးဟာ ရိုးရှင်းတဲ့ PyTorch `nn.Module` classes တွေဖြစ်ပြီး ၎င်းတို့ရဲ့ သက်ဆိုင်ရာ machine learning (ML) frameworks တွေထဲက တခြားမော်ဒယ်တွေလိုမျိုး ကိုင်တွယ်နိုင်ပါတယ်။ +- **ရိုးရှင်းမှု (Simplicity)**: library တစ်လျှောက်လုံးမှာ abstraction တွေဟာ သိပ်မရှိပါဘူး။ "All in one file" ဆိုတာက အဓိကသဘောတရားတစ်ခုပါ- မော်ဒယ်တစ်ခုရဲ့ forward pass ကို file တစ်ခုတည်းမှာ အပြည့်အစုံ သတ်မှတ်ထားတာကြောင့် code ကို နားလည်ရလွယ်ကူပြီး ပြင်ဆင်ရ လွယ်ကူပါတယ်။ + +ဒီနောက်ဆုံးအင်္ဂါရပ်က 🤗 Transformers ကို အခြားသော ML library တွေနဲ့ အတော်လေး ကွဲပြားစေပါတယ်။ မော်ဒယ်တွေကို file တွေတစ်လျှောက် မျှဝေထားတဲ့ modules တွေနဲ့ တည်ဆောက်ထားတာ မဟုတ်ပါဘူး။ အဲဒီအစား မော်ဒယ်တစ်ခုစီမှာ သူ့ကိုယ်ပိုင် layers တွေ ရှိပါတယ်။ ဒါက မော်ဒယ်တွေကို ပိုမိုနားလည်ရလွယ်ကူပြီး လက်လှမ်းမီစေတဲ့အပြင်၊ မော်ဒယ်တစ်ခုပေါ်မှာ အခြားမော်ဒယ်တွေကို မထိခိုက်စေဘဲ အလွယ်တကူ စမ်းသပ်နိုင်စေပါတယ်။ + +ဒီအခန်းကို end-to-end ဥပမာတစ်ခုနဲ့ စတင်ပါမယ်။ ဒီဥပမာမှာ ကျွန်တော်တို့ဟာ [Chapter 1](/course/chapter1) မှာ မိတ်ဆက်ခဲ့တဲ့ `pipeline()` function ကို ပြန်လည်ဖန်တီးဖို့အတွက် မော်ဒယ်တစ်ခုနဲ့ tokenizer တစ်ခုကို ပေါင်းပြီး အသုံးပြုပါမယ်။ နောက်တစ်ဆင့်အနေနဲ့ model API ကို ဆွေးနွေးပါမယ်- မော်ဒယ်နဲ့ configuration classes တွေထဲကို နက်ရှိုင်းစွာ လေ့လာပြီး၊ မော်ဒယ်တစ်ခုကို ဘယ်လို load လုပ်ရမယ်၊ ပြီးတော့ ဂဏန်းဆိုင်ရာ inputs တွေကို output predictions တွေအဖြစ် ဘယ်လိုလုပ်ဆောင်တယ်ဆိုတာကို သင်ပြပါမယ်။ + +အဲဒီနောက် `pipeline()` function ရဲ့ အခြားအဓိက အစိတ်အပိုင်းဖြစ်တဲ့ tokenizer API ကို ကြည့်ပါမယ်။ Tokenizers တွေက ပထမဆုံးနဲ့ နောက်ဆုံး လုပ်ဆောင်မှုအဆင့်တွေကို ကိုင်တွယ်ပေးပြီး၊ စာသားကနေ neural network အတွက် ဂဏန်းဆိုင်ရာ inputs တွေအဖြစ် ပြောင်းလဲခြင်းနဲ့ လိုအပ်တဲ့အခါ စာသားအဖြစ် ပြန်ပြောင်းလဲခြင်းတို့ကို လုပ်ဆောင်ပေးပါတယ်။ နောက်ဆုံးအနေနဲ့၊ မော်ဒယ်တစ်ခုကနေတဆင့် စာကြောင်းများစွာကို batch အဖြစ် ပေးပို့တာကို ဘယ်လိုကိုင်တွယ်ရမလဲဆိုတာ သင်ပြပြီး၊ အဆင့်မြင့် `tokenizer()` function ကို ပိုမိုနက်ရှိုင်းစွာ လေ့လာခြင်းဖြင့် အားလုံးကို အပြီးသတ်ပါမယ်။ + +> [!TIP] +> ⚠️ Model Hub နဲ့ 🤗 Transformers မှာ ရရှိနိုင်တဲ့ အင်္ဂါရပ်အားလုံးကို ရယူဖို့အတွက် [account တစ်ခု ဖန်တီး](https://huggingface.co/join) ဖို့ ကျွန်တော်တို့ အကြံပြုပါတယ်။ + +--- + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Transformer Models**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Parameters**: Machine Learning မော်ဒယ်တစ်ခု၏ သင်ယူနိုင်သော အစိတ်အပိုင်းများ။ ၎င်းတို့သည် လေ့ကျင့်နေစဉ်အတွင်း ဒေတာများမှ ပုံစံများကို သင်ယူကာ ချိန်ညှိပေးသည်။ +* **Deploying**: Machine Learning မော်ဒယ်တစ်ခုကို အမှန်တကယ် အသုံးပြုနိုင်သော စနစ် သို့မဟုတ် environment တစ်ခုထဲသို့ ထည့်သွင်းခြင်း။ +* **🤗 Transformers Library**: Hugging Face က ထုတ်လုပ်ထားတဲ့ library တစ်ခုဖြစ်ပြီး Transformer မော်ဒယ်တွေကို အသုံးပြုပြီး Natural Language Processing (NLP), computer vision, audio processing စတဲ့ နယ်ပယ်တွေမှာ အဆင့်မြင့် AI မော်ဒယ်တွေကို တည်ဆောက်ပြီး အသုံးပြုနိုင်စေပါတယ်။ +* **API (Application Programming Interface)**: ဆော့ဖ်ဝဲလ် နှစ်ခုကြား အပြန်အလှန် ချိတ်ဆက်ဆောင်ရွက်နိုင်ရန် လမ်းကြောင်းဖွင့်ပေးသော အစုအဝေး (set of rules) များ။ +* **State-of-the-art (SOTA)**: လက်ရှိအချိန်တွင် အကောင်းဆုံး သို့မဟုတ် အဆင့်မြင့်ဆုံး စွမ်းဆောင်ရည်ကို ပြသနိုင်သော နည်းပညာ သို့မဟုတ် မော်ဒယ်။ +* **NLP (Natural Language Processing)**: ကွန်ပျူတာတွေ လူသားဘာသာစကားကို နားလည်၊ အဓိပ္ပာယ်ဖော်ပြီး၊ ဖန်တီးနိုင်အောင် လုပ်ဆောင်ပေးတဲ့ Artificial Intelligence (AI) ရဲ့ နယ်ပယ်ခွဲတစ်ခု ဖြစ်ပါတယ်။ +* **Inference**: လေ့ကျင့်ပြီးသား Artificial Intelligence (AI) မော်ဒယ်တစ်ခုကို အသုံးပြုပြီး input data ကနေ ခန့်မှန်းချက်တွေ ဒါမှမဟုတ် output တွေကို ထုတ်လုပ်တဲ့ လုပ်ငန်းစဉ်။ +* **Flexibility**: ပြောင်းလွယ်ပြင်လွယ်ရှိခြင်း၊ အခြေအနေအမျိုးမျိုးနဲ့ လိုက်လျောညီထွေစွာ အသုံးပြုနိုင်ခြင်း။ +* **PyTorch `nn.Module` classes**: PyTorch deep learning framework မှာ Neural Network layers တွေနဲ့ models တွေကို တည်ဆောက်ဖို့အတွက် အသုံးပြုတဲ့ အခြေခံ class တွေ။ +* **Machine Learning (ML) Frameworks**: Machine learning မော်ဒယ်များကို တည်ဆောက်ရန်၊ လေ့ကျင့်ရန်နှင့် အသုံးပြုရန်အတွက် ကိရိယာများနှင့် library များ စုစည်းမှု (ဥပမာ - PyTorch, TensorFlow)။ +* **Abstractions**: ကွန်ပျူတာပရိုဂရမ်းမင်းတွင် ရှုပ်ထွေးသောအသေးစိတ်အချက်အလက်များကို ဝှက်ထားပြီး အရေးကြီးသော အချက်များကိုသာ ပြသခြင်း။ +* **Forward Pass**: Neural Network တစ်ခုတွင် input data ကို ယူပြီး network layers များကို ဖြတ်သန်းကာ output prediction ကို ထုတ်လုပ်သည့် လုပ်ငန်းစဉ်။ +* **End-to-end Example**: စနစ်တစ်ခု၏ စတင်ခြင်းမှ အဆုံးအထိ အပြည့်အစုံ ပြသထားသော ဥပမာ။ +* **`pipeline()` function**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ လုပ်ဆောင်ချက်တစ်ခုဖြစ်ပြီး မော်ဒယ်တွေကို သီးခြားလုပ်ငန်းတာဝန်များ (ဥပမာ- စာသားခွဲခြားသတ်မှတ်ခြင်း၊ စာသားထုတ်လုပ်ခြင်း) အတွက် အသုံးပြုရလွယ်ကူအောင် ပြုလုပ်ပေးပါတယ်။ +* **Tokenizer**: စာသား (သို့မဟုတ် အခြားဒေတာ) ကို AI မော်ဒယ်များ စီမံဆောင်ရွက်နိုင်ရန် tokens တွေအဖြစ် ပိုင်းခြားပေးသည့် ကိရိယာ သို့မဟုတ် လုပ်ငန်းစဉ်။ +* **Model API**: မော်ဒယ်တစ်ခုကို ပရိုဂရမ်ကနေ ဘယ်လို ဝင်ရောက်အသုံးပြုနိုင်မလဲဆိုတာကို သတ်မှတ်ပေးတဲ့ interface။ +* **Configuration Classes**: Transformer မော်ဒယ်တစ်ခု၏ architecture နှင့် hyperparameters များကို သတ်မှတ်ပေးသော Python classes များ။ +* **Numerical Inputs**: ကွန်ပျူတာစနစ်များက လုပ်ဆောင်နိုင်သော ဂဏန်းပုံစံဖြင့် ဖော်ပြထားသော အချက်အလက်များ။ +* **Output Predictions**: မော်ဒယ်က input ကို အခြေခံပြီး ခန့်မှန်းထုတ်ပေးသော ရလဒ်များ။ +* **Tokenizer API**: Tokenizer တစ်ခုကို ပရိုဂရမ်ကနေ ဘယ်လို ဝင်ရောက်အသုံးပြုနိုင်မလဲဆိုတာကို သတ်မှတ်ပေးတဲ့ interface။ +* **Neural Network**: လူသားဦးနှောက်၏ လုပ်ဆောင်မှုပုံစံကို အတုယူထားသော ကွန်ပျူတာစနစ်များ။ +* **Batch**: မော်ဒယ်တစ်ခုက တစ်ပြိုင်နက်တည်း လုပ်ဆောင်ရန်အတွက် စုစည်းထားသော inputs အများအပြား။ +* **Model Hub**: Hugging Face ပေါ်ရှိ pre-trained model များနှင့် datasets များကို ရှာဖွေ၊ မျှဝေပြီး အသုံးပြုနိုင်သော online platform။ +* **Hugging Face Account**: Hugging Face ပလက်ဖောင်းပေါ်ရှိ သုံးစွဲသူအကောင့်။ ၎င်းသည် မော်ဒယ်များ၊ datasets များနှင့် အခြားအရင်းအမြစ်များကို ဝင်ရောက်ကြည့်ရှုရန် ခွင့်ပြုသည်။ \ No newline at end of file diff --git a/chapters/my/chapter2/2.mdx b/chapters/my/chapter2/2.mdx new file mode 100644 index 000000000..582918623 --- /dev/null +++ b/chapters/my/chapter2/2.mdx @@ -0,0 +1,289 @@ + + +# Pipeline နောက်ကွယ်မှ အကြောင်းအရာများ[[behind-the-pipeline]] + + + + + +[Chapter 1](/course/chapter1) မှာ အောက်ပါ code ကို run တဲ့အခါ ဘာတွေဖြစ်ပျက်သွားလဲဆိုတာကို ကြည့်ခြင်းဖြင့် ဥပမာတစ်ခုနဲ့ စလိုက်ရအောင်...။ + +```python +from transformers import pipeline + +classifier = pipeline("sentiment-analysis") +classifier( + [ + "I've been waiting for a HuggingFace course my whole life.", + "I hate this so much!", + ] +) +``` + +အောက်ပါရလဒ်ကို ရရှိခဲ့ပါတယ်။ + +```python out +[{'label': 'POSITIVE', 'score': 0.9598047137260437}, + {'label': 'NEGATIVE', 'score': 0.9994558095932007}] +``` + +[Chapter 1](/course/chapter1) မှာ ကျွန်တော်တို့ တွေ့ခဲ့ရသလို၊ ဒီ pipeline ဟာ အဆင့်သုံးဆင့်ကို ပေါင်းစပ်ထားပါတယ်၊ preprocessing လုပ်ခြင်း၊ model ကနေတဆင့် inputs တွေကို ပေးပို့ခြင်း၊ နဲ့ postprocessing လုပ်ခြင်းတို့ ဖြစ်ပါတယ်။ + +
+The full NLP pipeline: tokenization of text, conversion to IDs, and inference through the Transformer model and the model head. + +
+ +ဒါတွေကို အမြန်ဆုံး တစ်ခုချင်းစီ လေ့လာကြည့်ရအောင်။ + +## Tokenizer ဖြင့် Preprocessing ပြုလုပ်ခြင်း[[preprocessing-with-a-tokenizer]] + +အခြား neural network များကဲ့သို့ Transformer မော်ဒယ်များသည် raw text များကို တိုက်ရိုက်လုပ်ဆောင်၍ မရပါ။ ထို့ကြောင့် ကျွန်တော်တို့ pipeline ၏ ပထမအဆင့်မှာ text inputs များကို မော်ဒယ်နားလည်နိုင်သော ဂဏန်းများအဖြစ် ပြောင်းလဲခြင်းဖြစ်သည်။ ၎င်းကို ပြုလုပ်ရန် ကျွန်တော်တို့သည် *tokenizer* ကို အသုံးပြုပါသည်။ ၎င်းသည် အောက်ပါတို့ကို လုပ်ဆောင်ရန် တာဝန်ရှိသည်- + +- input ကို *tokens* ဟုခေါ်သော စကားလုံးများ၊ subwords များ သို့မဟုတ် သင်္ကေတများ (ဥပမာ- ပုဒ်ဖြတ်သံ) အဖြစ် ပိုင်းခြားခြင်း +- token တစ်ခုစီကို integer တစ်ခုသို့ တွဲချိတ်ခြင်း +- မော်ဒယ်အတွက် အသုံးဝင်နိုင်သော အပို inputs များကို ထည့်သွင်းခြင်း + +ဒီ preprocessing အားလုံးကို မော်ဒယ်ကို pre-trained လုပ်ခဲ့စဉ်က အတိအကျလုပ်ခဲ့တဲ့ နည်းလမ်းအတိုင်း ပြုလုပ်ဖို့ လိုအပ်ပါတယ်။ ဒါကြောင့် ကျွန်တော်တို့ အရင်ဆုံး [Model Hub](https://huggingface.co/models) ကနေ အဲဒီအချက်အလက်တွေကို download လုပ်ဖို့ လိုပါတယ်။ ဒါကို လုပ်ဖို့အတွက် `AutoTokenizer` class နဲ့ သူ့ရဲ့ `from_pretrained()` method ကို ကျွန်တော်တို့ အသုံးပြုပါတယ်။ ကျွန်တော်တို့ model ရဲ့ checkpoint name ကို အသုံးပြုပြီး၊ ၎င်းသည် model ရဲ့ tokenizer နဲ့ ဆက်စပ်နေတဲ့ ဒေတာတွေကို အလိုအလျောက် ရယူပြီး cache လုပ်ပါလိမ့်မယ် (ဒါကြောင့် အောက်က code ကို ပထမဆုံးအကြိမ် run မှသာ download လုပ်ပါလိမ့်မယ်)။ + +`sentiment-analysis` pipeline ရဲ့ default checkpoint က `distilbert-base-uncased-finetuned-sst-2-english` ဖြစ်တာကြောင့် (၎င်းရဲ့ model card ကို [ဒီနေရာမှာ](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english) ကြည့်နိုင်ပါတယ်)၊ အောက်ပါ code ကို ကျွန်တော်တို့ run ပြုလုပ်ပေးပါတယ်။ + +```python +from transformers import AutoTokenizer + +checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" +tokenizer = AutoTokenizer.from_pretrained(checkpoint) +``` + +tokenizer ကို ရရှိပြီဆိုတာနဲ့၊ ကျွန်တော်တို့ရဲ့ စာကြောင်းတွေကို တိုက်ရိုက် ပေးပို့နိုင်ပြီး model ကို ထည့်သွင်းဖို့ အဆင်သင့်ဖြစ်နေတဲ့ dictionary တစ်ခု ပြန်ရပါလိမ့်မယ်။ လုပ်ဆောင်ဖို့ ကျန်ရှိတာကတော့ input IDs တွေရဲ့ list ကို tensors တွေအဖြစ် ပြောင်းလဲဖို့ပါပဲ။ + +သင်ဟာ backend မှာ ဘယ် ML framework ကို အသုံးပြုလဲဆိုတာ စိုးရိမ်စရာမလိုဘဲ 🤗 Transformers ကို အသုံးပြုနိုင်ပါတယ်။ အချို့မော်ဒယ်တွေအတွက် PyTorch ဒါမှမဟုတ် Flax ဖြစ်နိုင်ပါတယ်။ သို့သော် Transformer မော်ဒယ်တွေက *tensors* တွေကိုပဲ input အဖြစ် လက်ခံပါတယ်။ tensors တွေအကြောင်းကို အခုမှ စကြားဖူးတာဆိုရင်၊ ၎င်းတို့ကို NumPy arrays တွေအဖြစ် တွေးကြည့်နိုင်ပါတယ်။ NumPy array တစ်ခုက scalar (0D)၊ vector (1D)၊ matrix (2D) သို့မဟုတ် dimension များစွာရှိနိုင်ပါတယ်။ ဒါက တကယ်တော့ tensor တစ်ခုပါပဲ။ အခြား ML frameworks တွေရဲ့ tensors တွေလည်း အလားတူပဲ အလုပ်လုပ်ပြီး၊ NumPy arrays တွေလိုပဲ လွယ်ကူစွာ instantiate လုပ်နိုင်ပါတယ်။ + +ကျွန်တော်တို့ ပြန်လိုချင်တဲ့ tensors (PyTorch သို့မဟုတ် plain NumPy) အမျိုးအစားကို သတ်မှတ်ဖို့အတွက် `return_tensors` argument ကို အသုံးပြုနိုင်ပါတယ်။ + +```python +raw_inputs = [ + "I've been waiting for a HuggingFace course my whole life.", + "I hate this so much!", +] +inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors="pt") +print(inputs) +``` + +padding နဲ့ truncation အကြောင်းကို အခုထိ စိတ်ပူမနေပါနဲ့၊ ဒါတွေကို နောက်မှ ရှင်းပြပါမယ်။ ဒီနေရာမှာ မှတ်ထားရမယ့် အဓိကအချက်တွေကတော့ သင်ဟာ စာကြောင်းတစ်ကြောင်း ဒါမှမဟုတ် စာကြောင်းများစွာပါတဲ့ list ကို ပေးပို့နိုင်သလို၊ သင်ပြန်လိုချင်တဲ့ tensors အမျိုးအစားကိုလည်း သတ်မှတ်နိုင်ပါတယ် (မည်သည့် type ကိုမျှ မပေးပို့ရင် list of lists အဖြစ် ရလဒ်ရပါလိမ့်မယ်)။ + +PyTorch tensors အဖြစ် ရလဒ်တွေက အောက်ပါအတိုင်း ဖြစ်ပါတယ်။ + +```python out +{ + 'input_ids': tensor([ + [ 101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012, 102], + [ 101, 1045, 5223, 2023, 2061, 2172, 999, 102, 0, 0, 0, 0, 0, 0, 0, 0] + ]), + 'attention_mask': tensor([ + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0] + ]) +} +``` + +output ကိုယ်တိုင်က `input_ids` နဲ့ `attention_mask` ဆိုတဲ့ key နှစ်ခုပါဝင်တဲ့ dictionary တစ်ခု ဖြစ်ပါတယ်။ `input_ids` မှာ integer row နှစ်ခု (စာကြောင်းတစ်ကြောင်းစီအတွက် တစ်ခု) ပါဝင်ပြီး ၎င်းတို့ဟာ စာကြောင်းတစ်ကြောင်းစီရှိ tokens တွေရဲ့ ထူးခြားတဲ့ identifiers တွေ ဖြစ်ပါတယ်။ `attention_mask` ဆိုတာ ဘာလဲဆိုတာကို ဒီအခန်းရဲ့ နောက်ပိုင်းမှာ ကျွန်တော်တို့ ရှင်းပြပါမယ်။ + +## Model ကို ဖြတ်သန်းခြင်း[[going-through-the-model]] + +ကျွန်တော်တို့ tokenizer ကို လုပ်ခဲ့သလိုပဲ pre-trained model ကို download လုပ်နိုင်ပါတယ်။ 🤗 Transformers က `from_pretrained()` method ပါဝင်တဲ့ `AutoModel` class ကို ပံ့ပိုးပေးပါတယ်။ + +```python +from transformers import AutoModel + +checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" +model = AutoModel.from_pretrained(checkpoint) +``` + +ဒီ code snippet မှာ ကျွန်တော်တို့ဟာ ယခင်က pipeline မှာ အသုံးပြုခဲ့တဲ့ checkpoint အတူတူကို download လုပ်ပြီး model တစ်ခုကို instantiate လုပ်ခဲ့ပါတယ်။ (ဒါကို အမှန်တကယ်တော့ cache လုပ်ထားပြီးသား ဖြစ်သင့်ပါတယ်)။ + +ဒီ architecture မှာ base Transformer module သာ ပါဝင်ပါတယ်- inputs အချို့ကို ပေးလိုက်တဲ့အခါ ၎င်းသည် *hidden states* ဟုခေါ်သော အရာများကို ထုတ်ပေးပါတယ်။ ၎င်းတို့ကို *features* ဟုလည်း ခေါ်ပါတယ်။ model input တစ်ခုစီအတွက် **Transformer model က အဲဒီ input ကို အကြောင်းအရာအရ နားလည်ထားမှုကို ကိုယ်စားပြုတဲ့ high-dimensional vector တစ်ခုကို** ကျွန်တော်တို့ ပြန်ရပါလိမ့်မယ်။ + +ဒါကို နားမလည်ရင် စိတ်မပူပါနဲ့။ ဒါတွေကို နောက်မှ အားလုံးရှင်းပြပါမယ်။ + +ဒီ hidden states တွေက သူ့ဘာသာသူ အသုံးဝင်နိုင်ပေမယ့်၊ ၎င်းတို့ဟာ များသောအားဖြင့် *head* လို့ခေါ်တဲ့ model ရဲ့ နောက်ထပ်အစိတ်အပိုင်းတစ်ခုရဲ့ inputs တွေ ဖြစ်ပါတယ်။ [Chapter 1](/course/chapter1) မှာ မတူညီတဲ့ လုပ်ငန်းတာဝန်တွေကို architecture တူတူနဲ့ လုပ်ဆောင်နိုင်ခဲ့ပေမယ့်၊ ဒီလုပ်ငန်းတာဝန်တစ်ခုစီမှာ ၎င်းနဲ့ ဆက်စပ်နေတဲ့ head တစ်ခုစီ ရှိပါတယ်။ + +### High-dimensional vector တစ်ခုလား။[[a-high-dimensional-vector]] + +Transformer module ကနေ ထုတ်ပေးတဲ့ vector ဟာ များသောအားဖြင့် ကြီးမားပါတယ်။ ဒါက အများအားဖြင့် dimensions သုံးခု ရှိပါတယ်- + +- **Batch size**: တစ်ကြိမ်တည်း လုပ်ဆောင်တဲ့ sequence အရေအတွက် (ကျွန်တော်တို့ ဥပမာမှာ ၂ ခု)။ +- **Sequence length**: sequence ရဲ့ ဂဏန်းဆိုင်ရာ ကိုယ်စားပြုမှုရဲ့ အရှည် (ကျွန်တော်တို့ ဥပမာမှာ ၁၆ ခု)။ +- **Hidden size**: model input တစ်ခုစီရဲ့ vector dimension။ + +နောက်ဆုံးတန်ဖိုးကြောင့် "high dimensional" လို့ ခေါ်တာ ဖြစ်ပါတယ်။ hidden size က အလွန်ကြီးမားနိုင်ပါတယ် (768 က ပိုသေးငယ်တဲ့ မော်ဒယ်တွေအတွက် အများအားဖြင့်ဖြစ်ပြီး၊ ပိုကြီးတဲ့ မော်ဒယ်တွေမှာ ဒါက 3072 ဒါမှမဟုတ် ပိုများနိုင်ပါတယ်)။ + +ကျွန်တော်တို့ preprocessing လုပ်ထားတဲ့ inputs တွေကို model ကို ပေးပို့ကြည့်ရင် ဒါကို တွေ့နိုင်ပါတယ်။ + +```python +outputs = model(**inputs) +print(outputs.last_hidden_state.shape) +``` + +```python out +torch.Size([2, 16, 768]) +``` + +🤗 Transformers မော်ဒယ်တွေရဲ့ outputs တွေဟာ `namedtuple` တွေ ဒါမှမဟုတ် dictionaries တွေလို အလုပ်လုပ်တယ်ဆိုတာကို သတိပြုပါ။ attribute တွေ (ကျွန်တော်တို့ လုပ်ခဲ့သလို) ဒါမှမဟုတ် key ( `outputs["last_hidden_state"]` ) နဲ့ ဒါမှမဟုတ် သင်ရှာနေတဲ့အရာ ဘယ်နေရာမှာရှိတယ်ဆိုတာ အတိအကျသိရင် index ( `outputs[0]` ) နဲ့ပါ ဝင်ရောက်ကြည့်ရှုနိုင်ပါတယ်။ + +### Model heads: ဂဏန်းတွေကနေ အဓိပ္ပာယ်ထုတ်ယူခြင်း[[model-heads-making-sense-out-of-numbers]] + +Model heads တွေက hidden states တွေရဲ့ high-dimensional vector ကို input အဖြစ် ယူပြီး ၎င်းတို့ကို မတူညီတဲ့ dimension တစ်ခုပေါ်သို့ project လုပ်ပါတယ်။ ၎င်းတို့ဟာ များသောအားဖြင့် linear layers တစ်ခု ဒါမှမဟုတ် အနည်းငယ်နဲ့ ဖွဲ့စည်းထားပါတယ်- + +
+A Transformer network alongside its head. + +
+ +Transformer model ရဲ့ output ကို model head ကို တိုက်ရိုက်ပို့ပြီး လုပ်ဆောင်ပါတယ်။ + +ဒီပုံမှာ မော်ဒယ်ကို embeddings layer နဲ့ နောက်ဆက်တွဲ layers တွေနဲ့ ကိုယ်စားပြုထားပါတယ်။ embeddings layer က tokenized input ထဲက input ID တစ်ခုစီကို ၎င်းနဲ့ ဆက်စပ်နေတဲ့ token ကို ကိုယ်စားပြုတဲ့ vector တစ်ခုအဖြစ် ပြောင်းလဲပေးပါတယ်။ နောက်ဆက်တွဲ layers တွေက attention mechanism ကို အသုံးပြုပြီး အဲဒီ vectors တွေကို စီမံခန့်ခွဲကာ စာကြောင်းတွေရဲ့ နောက်ဆုံးကိုယ်စားပြုမှုကို ထုတ်ပေးပါတယ်။ + +🤗 Transformers မှာ မတူညီတဲ့ architecture များစွာ ရရှိနိုင်ပြီး၊ တစ်ခုချင်းစီကို သီးခြားလုပ်ငန်းတစ်ခုကို ဖြေရှင်းဖို့ ဒီဇိုင်းထုတ်ထားပါတယ်။ အောက်ပါတို့ကတော့ မပြည့်စုံသေးသော စာရင်းတစ်ခု ဖြစ်ပါတယ်- + +- `*Model` (hidden states များကို ပြန်ရယူခြင်း) +- `*ForCausalLM` +- `*ForMaskedLM` +- `*ForMultipleChoice` +- `*ForQuestionAnswering` +- `*ForSequenceClassification` +- `*ForTokenClassification` +- နဲ့ အခြားအရာများ 🤗 + +ကျွန်တော်တို့ရဲ့ ဥပမာအတွက်၊ sequence classification head ပါဝင်တဲ့ မော်ဒယ်တစ်ခု လိုအပ်ပါလိမ့်မယ် (စာကြောင်းတွေကို positive သို့မဟုတ် negative အဖြစ် ခွဲခြားသတ်မှတ်နိုင်ဖို့)။ ဒါကြောင့် ကျွန်တော်တို့ဟာ `AutoModel` class ကို အမှန်တကယ် အသုံးပြုမှာ မဟုတ်ဘဲ `AutoModelForSequenceClassification` ကို အသုံးပြုပါမယ်။ + +```python +from transformers import AutoModelForSequenceClassification + +checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" +model = AutoModelForSequenceClassification.from_pretrained(checkpoint) +outputs = model(**inputs) +``` + +အခု outputs တွေရဲ့ shape ကို ကြည့်လိုက်ရင်၊ dimensionality က အများကြီး နိမ့်သွားပါလိမ့်မယ်၊ model head က ယခင်က ကျွန်တော်တို့ တွေ့ခဲ့တဲ့ high-dimensional vectors တွေကို input အဖြစ် ယူပြီး၊ တန်ဖိုးနှစ်ခု (label တစ်ခုစီအတွက် တစ်ခု) ပါဝင်တဲ့ vectors တွေကို ထုတ်ပေးပါတယ်။ + +```python +print(outputs.logits.shape) +``` + +```python out +torch.Size([2, 2]) +``` + +ကျွန်တော်တို့မှာ စာကြောင်းနှစ်ကြောင်းနဲ့ label နှစ်ခုပဲ ရှိတာကြောင့်၊ ကျွန်တော်တို့ model ကနေ ရရှိတဲ့ ရလဒ်ဟာ 2 x 2 shape ဖြစ်ပါတယ်။ + +## Output ကို Postprocessing ပြုလုပ်ခြင်း[[postprocessing-the-output]] + +ကျွန်တော်တို့ model ကနေ output အဖြစ် ရရှိတဲ့ တန်ဖိုးတွေက သူ့ဘာသာသူ အဓိပ္ပာယ်ရှိတာ မဟုတ်ပါဘူး။ ကြည့်ကြည့်ရအောင်။ + +```python +print(outputs.logits) +``` + +```python out +tensor([[-1.5607, 1.6123], + [ 4.1692, -3.3464]], grad_fn=) +``` + +ကျွန်တော်တို့ရဲ့ model က ပထမစာကြောင်းအတွက် `[-1.5607, 1.6123]` ကို ခန့်မှန်းခဲ့ပြီး၊ ဒုတိယစာကြောင်းအတွက် `[ 4.1692, -3.3464]` ကို ခန့်မှန်းခဲ့ပါတယ်။ ဒါတွေက ဖြစ်နိုင်ခြေတွေ မဟုတ်ဘဲ *logits* တွေ ဖြစ်ပါတယ်။ ၎င်းတို့က model ရဲ့ နောက်ဆုံး layer ကနေ ထုတ်ပေးတဲ့ raw, unnormalized scores တွေပါ။ ဖြစ်နိုင်ခြေတွေအဖြစ် ပြောင်းလဲဖို့အတွက် [SoftMax](https://en.wikipedia.org/wiki/Softmax_function) layer ကို ဖြတ်သန်းဖို့ လိုအပ်ပါတယ် (🤗 Transformers model အားလုံးက logits တွေကို ထုတ်ပေးပါတယ်၊ ဘာလို့လဲဆိုတော့ training အတွက် loss function က SoftMax လိုမျိုး နောက်ဆုံး activation function နဲ့ cross entropy လိုမျိုး loss function အမှန်တကယ်ကို ပေါင်းစပ်ထားတာ ဖြစ်လေ့ရှိပါတယ်)။ + +```py +import torch + +predictions = torch.nn.functional.softmax(outputs.logits, dim=-1) +print(predictions) +``` + +```python out +tensor([[4.0195e-02, 9.5980e-01], + [9.9946e-01, 5.4418e-04]], grad_fn=) +``` + +အခု ကျွန်တော်တို့ model က ပထမစာကြောင်းအတွက် `[0.0402, 0.9598]` ကို ခန့်မှန်းခဲ့ပြီး၊ ဒုတိယစာကြောင်းအတွက် `[0.9995, 0.0005]` ကို ခန့်မှန်းခဲ့တယ်ဆိုတာ တွေ့ရပါပြီ။ ဒါတွေက အသိအမှတ်ပြုနိုင်တဲ့ ဖြစ်နိုင်ခြေ scores တွေ ဖြစ်ပါတယ်။ + +position တစ်ခုစီနဲ့ ကိုက်ညီတဲ့ labels တွေကို ရယူဖို့အတွက် model config ရဲ့ `id2label` attribute ကို စစ်ဆေးနိုင်ပါတယ် (ဒီအကြောင်းကို နောက်အပိုင်းမှာ ပိုမိုသိရှိရပါလိမ့်မယ်)။ + +```python +model.config.id2label +``` + +```python out +{0: 'NEGATIVE', 1: 'POSITIVE'} +``` + +Nအခု ကျွန်တော်တို့ model က အောက်ပါအတိုင်း ခန့်မှန်းခဲ့တယ်လို့ ကောက်ချက်ချနိုင်ပါပြီ- + +- ပထမစာကြောင်း - NEGATIVE: 0.0402, POSITIVE: 0.9598 +- ဒုတိယစာကြောင်း - NEGATIVE: 0.9995, POSITIVE: 0.0005 + +ကျွန်တော်တို့ pipeline ရဲ့ အဆင့်သုံးဆင့်လုံးကို အောင်မြင်စွာ ပြန်လည်ဖန်တီးနိုင်ခဲ့ပါပြီ- tokenizers တွေနဲ့ preprocessing လုပ်ခြင်း၊ model ကနေတဆင့် inputs တွေကို ပေးပို့ခြင်း၊ နဲ့ postprocessing လုပ်ခြင်းတို့ ဖြစ်ပါတယ်။ အခုတော့ ဒီအဆင့်တစ်ခုချင်းစီကို ပိုပြီး နက်နက်နဲနဲ လေ့လာကြည့်ရအောင်။ + +> [!TIP] +> ✏️ **စမ်းသပ်ကြည့်ပါ။** သင်ကိုယ်တိုင် စာသား (၂) ခု (သို့မဟုတ် ပိုမိုများပြား) ရွေးချယ်ပြီး `sentiment-analysis` pipeline ကနေတဆင့် run ပါ။ ထို့နောက် ဒီနေရာမှာ သင်တွေ့ခဲ့ရတဲ့ အဆင့်တွေကို ကိုယ်တိုင်ပြန်လုပ်ပြီး တူညီတဲ့ ရလဒ်တွေ ရရှိမရရှိ စစ်ဆေးပါ။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Pipeline**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ လုပ်ဆောင်ချက်တစ်ခုဖြစ်ပြီး မော်ဒယ်တွေကို သီးခြားလုပ်ငန်းတာဝန်များ (ဥပမာ- စာသားခွဲခြားသတ်မှတ်ခြင်း၊ စာသားထုတ်လုပ်ခြင်း) အတွက် အသုံးပြုရလွယ်ကူအောင် ပြုလုပ်ပေးပါတယ်။ +* **Preprocessing**: Machine Learning မော်ဒယ်တစ်ခုကို မထည့်သွင်းမီ raw data များကို လုပ်ဆောင်ရန် အသင့်ဖြစ်အောင် ပြင်ဆင်ခြင်း။ +* **Postprocessing**: Machine Learning မော်ဒယ်တစ်ခု၏ output များကို ပိုမိုနားလည်လွယ်သော သို့မဟုတ် အသုံးဝင်သော ပုံစံသို့ ပြောင်းလဲခြင်း။ +* **Neural Networks**: လူသားဦးနှောက်၏ လုပ်ဆောင်မှုပုံစံကို အတုယူထားသော ကွန်ပျူတာစနစ်များ။ +* **Transformer Models**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Raw Text**: မည်သည့်လုပ်ဆောင်မှုမျှ မပြုလုပ်ရသေးသော သို့မဟုတ် ပုံစံမချရသေးသော မူရင်းစာသား။ +* **Tokenizer**: စာသား (သို့မဟုတ် အခြားဒေတာ) ကို AI မော်ဒယ်များ စီမံဆောင်ရွက်နိုင်ရန် tokens တွေအဖြစ် ပိုင်းခြားပေးသည့် ကိရိယာ သို့မဟုတ် လုပ်ငန်းစဉ်။ +* **Tokens**: စာသားကို ခွဲခြမ်းစိတ်ဖြာရာတွင် အသုံးပြုသော အသေးငယ်ဆုံးယူနစ်များ (ဥပမာ- စကားလုံးများ၊ subwords များ သို့မဟုတ် ပုဒ်ဖြတ်သံများ)။ +* **Integer**: အပြည့်အစုံ ကိန်းဂဏန်း။ +* **Pretrained**: ဒေတာအမြောက်အမြားပေါ်တွင် ကြိုတင်လေ့ကျင့်ထားပြီးသား Artificial Intelligence (AI) မော်ဒယ်တစ်ခု။ +* **Model Hub**: Hugging Face ပေါ်ရှိ pre-trained model များနှင့် datasets များကို ရှာဖွေ၊ မျှဝေပြီး အသုံးပြုနိုင်သော online platform။ +* **`AutoTokenizer` Class**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး မော်ဒယ်အမည်ကို အသုံးပြုပြီး သက်ဆိုင်ရာ tokenizer ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **`from_pretrained()` Method**: Pre-trained model သို့မဟုတ် tokenizer ကို load လုပ်ရန် အသုံးပြုသော method။ +* **Checkpoint Name**: အင်တာနက်ပေါ်ရှိ Hugging Face Hub မှ pre-trained model သို့မဟုတ် tokenizer ကို ဖော်ထုတ်ရန် အသုံးပြုသော အမည်။ +* **Cache**: မကြာခဏ အသုံးပြုရသော ဒေတာများကို အမြန်ဆုံး ဝင်ရောက်ရယူနိုင်ရန် ယာယီသိုလှောင်ထားသော နေရာ။ +* **`sentiment-analysis` pipeline**: စာသားတစ်ခု၏ စိတ်ခံစားမှု (အပြုသဘော သို့မဟုတ် အနုတ်သဘော) ကို ခွဲခြမ်းစိတ်ဖြာပေးသော pipeline။ +* **`distilbert-base-uncased-finetuned-sst-2-english`**: `sentiment-analysis` pipeline ၏ default checkpoint အဖြစ် အသုံးပြုသော DistilBERT မော်ဒယ်၏ အမည်။ `base` သည် မော်ဒယ်၏ အရွယ်အစားကို ဖော်ပြပြီး `uncased` သည် စာလုံးအကြီးအသေး ခွဲခြားခြင်းမရှိဘဲ လေ့ကျင့်ထားကြောင်း ဖော်ပြသည်။ `finetuned-sst-2-english` က SST-2 dataset တွင် English ဘာသာစကားအတွက် fine-tune လုပ်ထားသည်ကို ဆိုလိုသည်။ +* **Model Card**: Hugging Face Hub ပေါ်ရှိ မော်ဒယ်တစ်ခု၏ အချက်အလက်များ၊ အသုံးပြုပုံနှင့် စွမ်းဆောင်ရည်များကို အကျဉ်းချုပ်ဖော်ပြထားသော စာမျက်နှာ။ +* **Dictionary**: key-value pair များဖြင့် ဒေတာများကို သိုလှောင်သော ဒေတာဖွဲ့စည်းပုံ။ +* **Tensors**: Machine Learning frameworks (PyTorch, TensorFlow) များတွင် ဒေတာများကို ကိုယ်စားပြုသော multi-dimensional array များ။ +* **NumPy Arrays**: Python တွင် ဂဏန်းတွက်ချက်မှုများအတွက် အသုံးပြုသော multi-dimensional array များအတွက် library။ +* **Scalar (0D)**: Dimension မရှိသော တစ်ခုတည်းသော ကိန်းဂဏန်းတန်ဖိုး။ +* **Vector (1D)**: ကိန်းဂဏန်းတန်ဖိုးများ၏ တစ်ကြောင်းတည်းသော sequence။ +* **Matrix (2D)**: ကိန်းဂဏန်းတန်ဖိုးများ၏ နှစ်ကြောင်းအတန်းလိုက် စီစဉ်ထားသော အစုအဝေး။ +* **`return_tensors` Argument**: tokenizer ကို ခေါ်ဆိုသောအခါ ပြန်လိုချင်သော tensor အမျိုးအစားကို သတ်မှတ်ရန် အသုံးပြုသော argument။ +* **`padding`**: မတူညီသော အရှည်ရှိသည့် input sequence များကို အရှည်တူညီအောင် သတ်မှတ်ထားသော တန်ဖိုးများဖြင့် ဖြည့်စွက်ခြင်း။ +* **`truncation`**: အရှည်ကန့်သတ်ချက်ထက် ပိုနေသော input sequence များကို ဖြတ်တောက်ခြင်း။ +* **`input_ids`**: Tokenizer မှ ထုတ်ပေးသော tokens တစ်ခုစီ၏ ထူးခြားသော ဂဏန်းဆိုင်ရာ ID များ။ +* **`attention_mask`**: မော်ဒယ်ကို အာရုံစိုက်သင့်သည့် tokens များနှင့် လျစ်လျူရှုသင့်သည့် (padding) tokens များကို ခွဲခြားပေးသည့် binary mask။ +* **`AutoModel` Class**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး မော်ဒယ်အမည်ကို အသုံးပြုပြီး Transformer model ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **Hidden States**: Transformer model ၏ အလယ်အလတ် layers များမှ ထုတ်ပေးသော output များ။ ၎င်းတို့သည် input ၏ အကြောင်းအရာဆိုင်ရာ ကိုယ်စားပြုမှုများကို ဖမ်းယူထားသည်။ +* **Features**: Hidden states များကို ရည်ညွှန်းသော အခြားအသုံးအနှုန်းတစ်ခု။ +* **High-dimensional Vector**: dimension များစွာရှိသော vector တစ်ခု။ +* **Batch Size**: မော်ဒယ်က တစ်ပြိုင်နက်တည်း လုပ်ဆောင်သော input sequence အရေအတွက်။ +* **Sequence Length**: input sequence ၏ token အရေအတွက်။ +* **Hidden Size**: hidden states vector တစ်ခု၏ dimension အရွယ်အစား။ +* **`namedtuple`s**: Python တွင် tuple ကဲ့သို့ အလုပ်လုပ်သော်လည်း attribute name များဖြင့် elements များကို ဝင်ရောက်ကြည့်ရှုနိုင်သော data type။ +* **Model Heads**: Transformer model ၏ hidden states များကို သီးခြားလုပ်ငန်းတစ်ခုအတွက် လိုအပ်သော output များအဖြစ် ပြောင်းလဲပေးသော အစိတ်အပိုင်း။ များသောအားဖြင့် linear layers များဖြင့် ဖွဲ့စည်းထားသည်။ +* **Embeddings Layer**: input IDs များကို vector representations များအဖြစ် ပြောင်းလဲပေးသော model layer။ +* **`*Model`**: base Transformer model (hidden states များကို ပြန်ရယူရန်) ကို ကိုယ်စားပြုသော Hugging Face model class family။ +* **`*ForCausalLM`**: Causal Language Modeling (နောက်ထပ် token ကို ခန့်မှန်းခြင်း) အတွက် ဒီဇိုင်းထုတ်ထားသော model class family။ +* **`*ForMaskedLM`**: Masked Language Modeling (ပျောက်ဆုံးနေသော token များကို ဖြည့်စွက်ခြင်း) အတွက် ဒီဇိုင်းထုတ်ထားသော model class family။ +* **`*ForMultipleChoice`**: Multiple Choice question answering အတွက် ဒီဇိုင်းထုတ်ထားသော model class family။ +* **`*ForQuestionAnswering`**: Question Answering လုပ်ငန်းတာဝန်များအတွက် ဒီဇိုင်းထုတ်ထားသော model class family။ +* **`*ForSequenceClassification`**: Sequence Classification လုပ်ငန်းတာဝန်များ (ဥပမာ- sentiment analysis) အတွက် ဒီဇိုင်းထုတ်ထားသော model class family။ +* **`*ForTokenClassification`**: Token Classification လုပ်ငန်းတာဝန်များ (ဥပမာ- Named Entity Recognition) အတွက် ဒီဇိုင်းထုတ်ထားသော model class family။ +* **`AutoModelForSequenceClassification`**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး sequence classification အတွက် pre-trained model ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **`outputs.logits`**: မော်ဒယ်၏ နောက်ဆုံး layer မှ ထုတ်ပေးသော raw, unnormalized scores များ။ +* **Logits**: မော်ဒယ်၏ နောက်ဆုံး layer မှ ထုတ်ပေးသော raw, unnormalized scores များ။ ၎င်းတို့သည် ဖြစ်နိုင်ခြေများအဖြစ်သို့ ပြောင်းလဲခြင်းမရှိသေးပါ။ +* **SoftMax Layer**: input numbers များကို 0 နှင့် 1 ကြားရှိ ဖြစ်နိုင်ခြေများအဖြစ်သို့ ပြောင်းလဲပေးသော activation function တစ်ခု။ ၎င်းတို့၏ စုစုပေါင်းသည် 1 ဖြစ်သည်။ +* **Loss Function**: မော်ဒယ်၏ ခန့်မှန်းချက်များနှင့် အမှန်တကယ်တန်ဖိုးများကြား ကွာခြားမှုကို တိုင်းတာသော function တစ်ခု။ +* **Cross Entropy**: Classification လုပ်ငန်းများတွင် အသုံးများသော loss function တစ်ခု။ +* **`torch.nn.functional.softmax(outputs.logits, dim=-1)`**: PyTorch တွင် softmax function ကို `outputs.logits` ပေါ်တွင် နောက်ဆုံး dimension (dim=-1) အတိုင်း အသုံးပြုခြင်း။ +* **`model.config.id2label`**: Model configuration ထဲတွင် `id` (ဂဏန်း) မှ `label` (စာသား) သို့ တွဲချိတ်ပေးသော dictionary တစ်ခု။ \ No newline at end of file diff --git a/chapters/my/chapter2/3.mdx b/chapters/my/chapter2/3.mdx new file mode 100644 index 000000000..b7d947938 --- /dev/null +++ b/chapters/my/chapter2/3.mdx @@ -0,0 +1,342 @@ + + +# Models[[the-models]] + + + + + +ဒီအပိုင်းမှာတော့ model တွေကို ဘယ်လိုဖန်တီးရမလဲ၊ အသုံးပြုရမလဲဆိုတာကို ပိုမိုနက်နဲစွာ လေ့လာသွားပါမယ်။ checkpoint တစ်ခုကနေ မည်သည့် model ကိုမဆို instantiate လုပ်ချင်တဲ့အခါ အသုံးဝင်တဲ့ `AutoModel` class ကို ကျွန်တော်တို့ အသုံးပြုပါမယ်။ + +## Transformer တစ်ခုကို ဖန်တီးခြင်း[[creating-a-transformer]] + +`AutoModel` တစ်ခုကို instantiate လုပ်တဲ့အခါ ဘာတွေဖြစ်ပျက်လဲဆိုတာကို ကြည့်ခြင်းဖြင့် စတင်လိုက်ရအောင်။ + +```py +from transformers import AutoModel + +model = AutoModel.from_pretrained("bert-base-cased") +``` + +tokenizer နဲ့ ဆင်တူစွာ၊ `from_pretrained()` method က Hugging Face Hub ကနေ model data တွေကို download လုပ်ပြီး cache လုပ်ပါလိမ့်မယ်။ ယခင်က ဖော်ပြခဲ့သလိုပဲ၊ checkpoint name က သီးခြား model architecture နဲ့ weights တွေနဲ့ ကိုက်ညီပါတယ်။ ဒီဥပမာမှာတော့ basic architecture (12 layers, 768 hidden size, 12 attention heads) နဲ့ cased inputs (ဆိုလိုသည်မှာ စာလုံးအကြီးအသေး ခွဲခြားမှုက အရေးကြီးသည်) ပါဝင်တဲ့ BERT model တစ်ခု ဖြစ်ပါတယ်။ Hub မှာ ရရှိနိုင်တဲ့ checkpoints များစွာ ရှိပါတယ် - [ဒီနေရာမှာ](https://huggingface.co/models) ရှာဖွေနိုင်ပါတယ်။ + +`AutoModel` class နဲ့ ၎င်းရဲ့ ဆက်စပ် classes တွေဟာ တကယ်တော့ ပေးထားတဲ့ checkpoint အတွက် သင့်လျော်တဲ့ model architecture ကို ရယူဖို့ ဒီဇိုင်းထုတ်ထားတဲ့ ရိုးရှင်းတဲ့ wrappers တွေပါ။ ဒါက "auto" class တစ်ခုဖြစ်ပြီး သင့်အတွက် သင့်လျော်တဲ့ model architecture ကို ခန့်မှန်းပြီး မှန်ကန်တဲ့ model class ကို instantiate လုပ်ပေးပါလိမ့်မယ်။ သို့သော်၊ သင်အသုံးပြုချင်တဲ့ model အမျိုးအစားကို သိရှိထားတယ်ဆိုရင်၊ ၎င်းရဲ့ architecture ကို တိုက်ရိုက်သတ်မှတ်ပေးတဲ့ class ကို အသုံးပြုနိုင်ပါတယ်။ + +```py +from transformers import BertModel + +model = BertModel.from_pretrained("bert-base-cased") +``` + +## Loading နှင့် Saving[[loading-and-saving]] + +Model တစ်ခုကို save လုပ်တာက tokenizer တစ်ခုကို save လုပ်တာလိုပဲ ရိုးရှင်းပါတယ်။ တကယ်တော့၊ model တွေမှာ model ရဲ့ weights တွေနဲ့ architecture configuration တွေကို save လုပ်ပေးတဲ့ `save_pretrained()` method တူတူကို ပိုင်ဆိုင်ထားပါတယ်။ + +```py +model.save_pretrained("directory_on_my_computer") +``` + +ဒါက သင့် disk ထဲမှာ ဖိုင်နှစ်ခုကို save လုပ်ပါလိမ့်မယ်။ + +``` +ls directory_on_my_computer + +config.json model.safetensors +``` + +*config.json* ဖိုင်ထဲကို ကြည့်လိုက်ရင်၊ model architecture ကို တည်ဆောက်ဖို့ လိုအပ်တဲ့ attributes အားလုံးကို တွေ့ရပါလိမ့်မယ်။ ဒီဖိုင်ထဲမှာ checkpoint ဘယ်ကနေ စတင်ခဲ့သလဲ၊ နောက်ဆုံး checkpoint ကို save လုပ်ခဲ့တုန်းက သင်အသုံးပြုခဲ့တဲ့ 🤗 Transformers version စတဲ့ metadata အချို့လည်း ပါဝင်ပါတယ်။ + +*pytorch_model.safetensors* ဖိုင်ကို state dictionary လို့ခေါ်ပါတယ်။ ၎င်းထဲမှာ သင့် model ရဲ့ weights အားလုံး ပါဝင်ပါတယ်။ ဖိုင်နှစ်ခုစလုံး အတူတူ အလုပ်လုပ်ပါတယ်- configuration file က model architecture အကြောင်း သိရှိဖို့ လိုအပ်ပြီး၊ model weights တွေကတော့ model ရဲ့ parameters တွေ ဖြစ်ပါတယ်။ + +save လုပ်ထားတဲ့ model တစ်ခုကို ပြန်လည်အသုံးပြုဖို့အတွက် `from_pretrained()` method ကို ထပ်မံအသုံးပြုပါ။ + +```py +from transformers import AutoModel + +model = AutoModel.from_pretrained("directory_on_my_computer") +``` + +🤗 Transformers library ရဲ့ အံ့ဖွယ်ကောင်းတဲ့ အင်္ဂါရပ်တစ်ခုကတော့ model တွေနဲ့ tokenizers တွေကို community နဲ့ အလွယ်တကူ မျှဝေနိုင်စွမ်းပါပဲ။ ဒါကို လုပ်ဖို့ Hugging Face မှာ account ရှိဖို့ သေချာပါစေ။ သင် notebook ကို အသုံးပြုနေတယ်ဆိုရင်၊ ဒါနဲ့ အလွယ်တကူ log in လုပ်ဆောင်နိုင်ပါတယ်။ + +```python +from huggingface_hub import notebook_login + +notebook_login() +``` + +မဟုတ်ရင်တော့ သင့် terminal မှာ အောက်ပါအတိုင်း run ပါ။ + +```bash +huggingface-cli login +``` + +အဲဒီနောက် `push_to_hub()` method နဲ့ model ကို Hub ကို push လုပ်နိုင်ပါတယ်။ + +```py +model.push_to_hub("my-awesome-model") +``` + +ဒါက model files တွေကို Hub ကို upload လုပ်ပါလိမ့်မယ်။ သင့် namespace အောက်မှာ *my-awesome-model* လို့ နာမည်ပေးထားတဲ့ repository ထဲမှာပါ။ အဲဒီနောက်၊ မည်သူမဆို သင့် model ကို `from_pretrained()` method နဲ့ load လုပ်နိုင်ပါပြီ။ + +```py +from transformers import AutoModel + +model = AutoModel.from_pretrained("your-username/my-awesome-model") +``` + +Hub API နဲ့ ပိုပြီး လုပ်ဆောင်နိုင်တာတွေ အများကြီး ရှိပါတယ်- +- local repository ကနေ model တစ်ခုကို push လုပ်ခြင်း +- အားလုံးကို ပြန်လည် upload မလုပ်ဘဲ သီးခြား files များကို update လုပ်ခြင်း +- model ရဲ့ စွမ်းဆောင်ရည်၊ ကန့်သတ်ချက်များ၊ သိထားတဲ့ bias စသည်တို့ကို မှတ်တမ်းတင်ဖို့ model cards တွေ ထည့်သွင်းခြင်း + +ဒီအကြောင်းအရာတွေအတွက် ပြည့်စုံတဲ့ tutorial ကို [documentation](https://huggingface.co/docs/huggingface_hub/how-to-upstream) မှာ ကြည့်ရှုနိုင်ပါတယ်၊ ဒါမှမဟုတ် အဆင့်မြင့် [Chapter 4](/course/chapter4) ကို လေ့လာနိုင်ပါတယ်။ + +## Text များကို Encoding လုပ်ခြင်း[[encoding-text]] + +Transformer မော်ဒယ်တွေက inputs တွေကို ဂဏန်းတွေအဖြစ် ပြောင်းလဲခြင်းဖြင့် text တွေကို ကိုင်တွယ်ပါတယ်။ ဒီနေရာမှာ သင်ရဲ့ text ကို tokenizer က ဘယ်လိုလုပ်ဆောင်တယ်ဆိုတာကို အတိအကျ ကြည့်ပါမယ်။ [Chapter 1](/course/chapter1) မှာ tokenizer တွေက text ကို tokens တွေအဖြစ် ပိုင်းခြားပြီး အဲဒီ tokens တွေကို ဂဏန်းတွေအဖြစ် ပြောင်းလဲတယ်ဆိုတာကို ကျွန်တော်တို့ တွေ့ခဲ့ရပါပြီ။ ဒီ conversion ကို ရိုးရှင်းတဲ့ tokenizer တစ်ခုနဲ့ ကြည့်နိုင်ပါတယ်။ + +```py +from transformers import AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") + +encoded_input = tokenizer("Hello, I'm a single sentence!") +print(encoded_input) +``` + +```python out +{'input_ids': [101, 8667, 117, 1000, 1045, 1005, 1049, 2235, 17662, 12172, 1012, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +ကျွန်တော်တို့ဟာ အောက်ပါ fields တွေပါဝင်တဲ့ dictionary တစ်ခုကို ရရှိပါတယ် - +- input_ids: သင့် tokens တွေရဲ့ ဂဏန်းဆိုင်ရာ ကိုယ်စားပြုမှုများ +- token_type_ids: ဒါတွေက model ကို input ရဲ့ ဘယ်အပိုင်းက sentence A ဖြစ်ပြီး ဘယ်အပိုင်းက sentence B ဖြစ်တယ်ဆိုတာ ပြောပြပါတယ် (နောက်အပိုင်းမှာ ပိုမိုဆွေးနွေးပါမယ်) +- attention_mask: ဒါက မည်သည့် tokens များကို အာရုံစိုက်သင့်ပြီး မည်သည့် tokens များကို အာရုံစိုက်ရန် မလိုအပ်ကြောင်း ဖော်ပြပါတယ် (နောက်မှ ပိုမိုဆွေးနွေးပါမယ်) + +မူရင်း text ကို ပြန်ရဖို့အတွက် input IDs တွေကို decode လုပ်နိုင်ပါတယ်။ + +```py +tokenizer.decode(encoded_input["input_ids"]) +``` + +```python out +"[CLS] Hello, I'm a single sentence! [SEP]" +``` + +tokenizer က model လိုအပ်တဲ့ special tokens တွေဖြစ်တဲ့ `[CLS]` နဲ့ `[SEP]` တွေကို ထည့်သွင်းပေးထားတာကို သင်သတိထားမိပါလိမ့်မယ်။ မော်ဒယ်အားလုံးက special tokens တွေ လိုအပ်တာ မဟုတ်ပါဘူး။ ၎င်းတို့ကို မော်ဒယ်ကို pretrained လုပ်ခဲ့တုန်းက အသုံးပြုခဲ့ရင် အသုံးဝင်ပါတယ်။ အဲဒီအခါမှာတော့ tokenizer က ဒီ tokens တွေကို model က မျှော်လင့်ထားတဲ့အတိုင်း ထည့်ပေးဖို့ လိုအပ်ပါတယ်။ + +စာကြောင်းများစွာကို တစ်ကြိမ်တည်း encode လုပ်နိုင်ပါတယ်၊ ဒါကို batch လုပ်ခြင်းဖြင့် (ဒီအကြောင်းကို မကြာမီ ဆွေးနွေးပါမယ်) ဒါမှမဟုတ် list တစ်ခု ပေးပို့ခြင်းဖြင့် လုပ်ဆောင်နိုင်ပါတယ်။ + +```py +encoded_input = tokenizer("How are you?", "I'm fine, thank you!") +print(encoded_input) +``` + +```python out +{'input_ids': [[101, 1731, 1132, 1128, 136, 102], [101, 1045, 1005, 1049, 2503, 117, 5763, 1128, 136, 102]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} +``` + +စာကြောင်းများစွာကို ပေးပို့တဲ့အခါ၊ tokenizer က dictionary value တစ်ခုစီအတွက် စာကြောင်းတစ်ခုစီအတွက် list တစ်ခု ပြန်ပေးတာကို သတိပြုပါ။ tokenizer ကို PyTorch ကနေ tensors တွေကို တိုက်ရိုက်ပြန်ပေးဖို့လည်း တောင်းဆိုနိုင်ပါတယ်။ + +```py +encoded_input = tokenizer("How are you?", "I'm fine, thank you!", return_tensors="pt") +print(encoded_input) +``` + +```python out +{'input_ids': tensor([[ 101, 1731, 1132, 1128, 136, 102], + [ 101, 1045, 1005, 1049, 2503, 117, 5763, 1128, 136, 102]]), + 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), + 'attention_mask': tensor([[1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])} +``` + +ဒါပေမယ့် ပြဿနာတစ်ခု ရှိပါတယ်- list နှစ်ခုရဲ့ အရှည်က မတူပါဘူး။ Arrays နဲ့ tensors တွေဟာ ထောင့်မှန်ပုံစံ (rectangular shapes) ဖြစ်ဖို့ လိုအပ်ပါတယ်။ ဒါကြောင့် ဒီ list တွေကို PyTorch tensor (သို့မဟုတ် NumPy array) အဖြစ် ရိုးရှင်းစွာ ပြောင်းလဲလို့ မရပါဘူး။ tokenizer က ဒါအတွက် ရွေးချယ်စရာတစ်ခု ပေးထားပါတယ်၊ padding ပါ။ + +### Inputs တွေကို Padding လုပ်ခြင်း[[padding-inputs]] + +ကျွန်တော်တို့ inputs တွေကို pad လုပ်ဖို့ tokenizer ကို တောင်းဆိုရင်၊ ၎င်းက အရှည်ဆုံးစာကြောင်းထက် တိုနေတဲ့ စာကြောင်းတွေမှာ special padding token တွေ ထည့်သွင်းခြင်းဖြင့် စာကြောင်းအားလုံးကို အရှည်တူညီအောင် ပြုလုပ်ပေးပါလိမ့်မယ်။ + +```py +encoded_input = tokenizer( + ["How are you?", "I'm fine, thank you!"], padding=True, return_tensors="pt" +) +print(encoded_input) +``` + +```python out +{'input_ids': tensor([[ 101, 1731, 1132, 1128, 136, 102, 0, 0, 0, 0], + [ 101, 1045, 1005, 1049, 2503, 117, 5763, 1128, 136, 102]]), + 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), + 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])} +``` + +အခု ကျွန်တော်တို့မှာ ထောင့်မှန်ပုံစံ tensors တွေ ရပါပြီ။ padding tokens တွေကို ID 0 နဲ့ input IDs တွေအဖြစ် encode လုပ်ထားပြီး၊ ၎င်းတို့မှာ attention mask value ကလည်း 0 ဖြစ်တာကို သတိပြုပါ။ ဒါက ဘာလို့လဲဆိုတော့ အဲဒီ padding tokens တွေကို model က analyze လုပ်ဖို့ မလိုအပ်ပါဘူး- ၎င်းတို့က တကယ့်စာကြောင်းရဲ့ အစိတ်အပိုင်းတွေ မဟုတ်ပါဘူး။ + +### Inputs တွေကို Truncating လုပ်ခြင်း[[truncating-inputs]] + +Tensors တွေက model က လုပ်ဆောင်ဖို့အတွက် အရမ်းကြီးလာနိုင်ပါတယ်။ ဥပမာအားဖြင့်၊ BERT ကို အများဆုံး tokens 512 ခုအထိပဲ sequences တွေနဲ့ pretrain လုပ်ထားတာကြောင့် ပိုရှည်တဲ့ sequences တွေကို လုပ်ဆောင်လို့ မရပါဘူး။ သင့်မှာ model က ကိုင်တွယ်နိုင်တာထက် ပိုရှည်တဲ့ sequences တွေရှိရင်၊ `truncation` parameter နဲ့ ၎င်းတို့ကို ဖြတ်တောက်ဖို့ လိုအပ်ပါလိမ့်မယ်- + +```py +encoded_input = tokenizer( + "This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long sentence.", + truncation=True, +) +print(encoded_input["input_ids"]) +``` + +```python out +[101, 1188, 1110, 170, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1505, 1179, 5650, 119, 102] +``` + +padding နဲ့ truncation arguments တွေကို ပေါင်းစပ်ခြင်းဖြင့်၊ သင်လိုအပ်တဲ့ တိကျတဲ့ size ရှိတဲ့ tensors တွေကို ရရှိကြောင်း သေချာစေနိုင်ပါတယ်။ + +```py +encoded_input = tokenizer( + ["How are you?", "I'm fine, thank you!"], + padding=True, + truncation=True, + max_length=5, + return_tensors="pt", +) +print(encoded_input) +``` + +```python out +{'input_ids': tensor([[ 101, 1731, 1132, 1128, 102], + [ 101, 1045, 1005, 1049, 102]]), + 'token_type_ids': tensor([[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]]), + 'attention_mask': tensor([[1, 1, 1, 1, 1], + [1, 1, 1, 1, 1]])} +``` + +### Special Tokens တွေ ထည့်သွင်းခြင်း + +Special tokens တွေ (သို့မဟုတ် ၎င်းတို့ရဲ့ သဘောတရား) ဟာ BERT နဲ့ ဆင်းသက်လာတဲ့ မော်ဒယ်တွေအတွက် အထူးအရေးကြီးပါတယ်။ ဒီ tokens တွေကို စာကြောင်းရဲ့ အစ ( `[CLS]` ) ဒါမှမဟုတ် စာကြောင်းတွေကြားက ပိုင်းခြားတဲ့နေရာ ( `[SEP]` ) လိုမျိုး စာကြောင်းရဲ့ နယ်နိမိတ်တွေကို ပိုမိုကောင်းမွန်စွာ ကိုယ်စားပြုနိုင်ဖို့ ထည့်သွင်းထားတာပါ။ ရိုးရှင်းတဲ့ ဥပမာတစ်ခုကို ကြည့်ကြည့်ရအောင်။ + +```py +encoded_input = tokenizer("How are you?") +print(encoded_input["input_ids"]) +tokenizer.decode(encoded_input["input_ids"]) +``` + +```python out +[101, 1731, 1132, 1128, 136, 102] +'[CLS] How are you? [SEP]' +``` + +ဒီ special tokens တွေကို tokenizer က အလိုအလျောက် ထည့်သွင်းပေးပါတယ်။ မော်ဒယ်အားလုံးက special tokens တွေ လိုအပ်တာ မဟုတ်ပါဘူး။ ၎င်းတို့ကို မော်ဒယ်ကို pretrained လုပ်ခဲ့တုန်းက အသုံးပြုခဲ့ရင် အဓိကအားဖြင့် အသုံးပြုပါတယ်။ အဲဒီအခါမှာတော့ tokenizer က ဒီ tokens တွေကို model က မျှော်လင့်ထားတဲ့အတိုင်း ထည့်ပေးပါလိမ့်မယ်။ + +### ဒါတွေအားလုံး ဘာကြောင့် လိုအပ်တာလဲ။ + +ဒီနေရာမှာ တိကျတဲ့ ဥပမာတစ်ခု ရှိပါတယ်- encode လုပ်ထားတဲ့ sequences တွေကို စဉ်းစားကြည့်ပါ။ + +```py +sequences = [ + "I've been waiting for a HuggingFace course my whole life.", + "I hate this so much!", +] +``` + +tokenized လုပ်ပြီးတာနဲ့ ကျွန်တော်တို့မှာ အောက်ပါအတိုင်း ရှိပါတယ်။ + +```python +encoded_sequences = [ + [ + 101, + 1045, + 1005, + 2310, + 2042, + 3403, + 2005, + 1037, + 17662, + 12172, + 2607, + 2026, + 2878, + 2166, + 1012, + 102, + ], + [101, 1045, 5223, 2023, 2061, 2172, 999, 102], +] +``` + +ဒါက encode လုပ်ထားတဲ့ sequences တွေရဲ့ list တစ်ခုပါ- list of lists တစ်ခုပေါ့။ Tensors တွေက ထောင့်မှန်ပုံစံ (rectangular shapes) တွေကိုသာ လက်ခံပါတယ် (matrices တွေကို တွေးကြည့်ပါ)။ ဒီ "array" ဟာ ထောင့်မှန်ပုံစံ ဖြစ်နေပြီဆိုတော့ ဒါကို tensor အဖြစ် ပြောင်းလဲဖို့က လွယ်ကူပါတယ်။ + +```py +import torch + +model_inputs = torch.tensor(encoded_sequences) +``` + +### Tensors များကို Model ၏ Inputs များအဖြစ် အသုံးပြုခြင်း[[using-the-tensors-as-inputs-to-the-model]] + +tensors တွေကို model နဲ့ အသုံးပြုတာက အလွန်ရိုးရှင်းပါတယ်- inputs တွေနဲ့ model ကို ခေါ်လိုက်ရုံပါပဲ။ + +```py +output = model(model_inputs) +``` + +model က မတူညီတဲ့ arguments များစွာကို လက်ခံပေမယ့်၊ input IDs တွေကသာ လိုအပ်တဲ့ arguments တွေပါ။ အခြား arguments တွေက ဘာလုပ်တယ်၊ ဘယ်အချိန်မှာ လိုအပ်တယ်ဆိုတာကို နောက်မှ ရှင်းပြပါမယ်။ ဒါပေမယ့် ပထမဆုံး Transformer model က နားလည်နိုင်တဲ့ inputs တွေကို တည်ဆောက်တဲ့ tokenizers တွေအကြောင်းကို ပိုပြီး နက်နဲစွာ လေ့လာဖို့ လိုအပ်ပါတယ်။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Models**: Artificial Intelligence (AI) နယ်ပယ်တွင် အသုံးပြုသော သင်္ချာဆိုင်ရာ ပုံစံများ သို့မဟုတ် algorithms များ။ +* **`AutoModel` Class**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး မော်ဒယ်အမည်ကို အသုံးပြုပြီး Transformer model ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **Checkpoint**: မော်ဒယ်တစ်ခု၏ လေ့ကျင့်မှုအခြေအနေ (weights, architecture configuration) ကို သတ်မှတ်ထားသော အချိန်တစ်ခုတွင် မှတ်တမ်းတင်ထားခြင်း။ +* **`from_pretrained()` Method**: Pre-trained model သို့မဟုတ် tokenizer ကို load လုပ်ရန် အသုံးပြုသော method။ +* **`bert-base-cased`**: BERT မော်ဒယ်၏ အမည်။ `base` သည် မော်ဒယ်၏ အရွယ်အစားကို ဖော်ပြပြီး `cased` သည် စာလုံးအကြီးအသေး ခွဲခြားမှုကို ထည့်သွင်းစဉ်းစားပြီး လေ့ကျင့်ထားကြောင်း ဖော်ပြသည်။ +* **Hugging Face Hub**: AI မော်ဒယ်တွေ၊ datasets တွေနဲ့ demo တွေကို အခြားသူတွေနဲ့ မျှဝေဖို့၊ ရှာဖွေဖို့နဲ့ ပြန်လည်အသုံးပြုဖို့အတွက် အွန်လိုင်း platform တစ်ခု ဖြစ်ပါတယ်။ +* **Model Architecture**: မော်ဒယ်တစ်ခု၏ ဖွဲ့စည်းပုံ၊ layer အမျိုးအစားများ၊ ၎င်းတို့ ချိတ်ဆက်ပုံ စသည်တို့ကို ဖော်ပြသော ဒီဇိုင်း။ +* **Weights**: Machine Learning မော်ဒယ်တစ်ခု၏ သင်ယူနိုင်သော အစိတ်အပိုင်းများ။ ၎င်းတို့သည် လေ့ကျင့်နေစဉ်အတွင်း ဒေတာများမှ ပုံစံများကို သင်ယူကာ ချိန်ညှိပေးသည်။ +* **Layers**: Neural Network တစ်ခု၏ အဆင့်များ။ +* **Hidden Size**: Hidden states vector တစ်ခု၏ dimension အရွယ်အစား။ +* **Attention Heads**: Transformer model ၏ attention mechanism တွင် ပါဝင်သော အပြိုင်လုပ်ဆောင်နိုင်သည့် အစိတ်အပိုင်းများ။ +* **Cased Inputs**: စာလုံးအကြီးအသေး ကွာခြားမှုကို ထည့်သွင်းစဉ်းစားသည့် inputs များ။ +* **Wrappers**: အခြား code များကို ပိုမိုလွယ်ကူစွာ အသုံးပြုနိုင်စေရန် ပတ်ခြုံပေးထားသော code များ။ +* **`BertModel` Class**: BERT model architecture ကို တိုက်ရိုက်သတ်မှတ်ပေးသော class။ +* **`save_pretrained()` Method**: Model သို့မဟုတ် tokenizer ၏ weights များနှင့် architecture configuration ကို save လုပ်ရန် အသုံးပြုသော method။ +* **`config.json`**: Model architecture တည်ဆောက်ရန် လိုအပ်သော attributes များနှင့် metadata များ ပါဝင်သော JSON ဖိုင်။ +* **`model.safetensors` / `pytorch_model.safetensors`**: Model ၏ weights များ ပါဝင်သော ဖိုင်။ +* **State Dictionary**: မော်ဒယ်တစ်ခု၏ သင်ယူထားသော parameters (weights) များကို သိုလှောင်ထားသော dictionary။ +* **Metadata**: ဒေတာအကြောင်းအရာနှင့်ပတ်သက်သော အချက်အလက်များ (ဥပမာ - ဘယ်ကနေ စတင်ခဲ့သည်၊ ဘယ် version ဖြင့် save လုပ်ခဲ့သည်)။ +* **Hugging Face**: AI နှင့် machine learning အတွက် tools များနှင့် platform များ ထောက်ပံ့ပေးသော ကုမ္ပဏီ။ +* **`huggingface_hub`**: Hugging Face Hub နှင့် ချိတ်ဆက်ရန်အတွက် Python library။ +* **`notebook_login()`**: Jupyter/Colab notebook များတွင် Hugging Face Hub ကို log in လုပ်ရန် အသုံးပြုသော function။ +* **`huggingface-cli login`**: terminal တွင် Hugging Face Hub ကို log in လုပ်ရန် အသုံးပြုသော command line tool။ +* **`push_to_hub()` Method**: model ကို Hugging Face Hub သို့ upload လုပ်ရန် အသုံးပြုသော method။ +* **Repository**: Hugging Face Hub ပေါ်ရှိ model files များ သို့မဟုတ် datasets များကို သိုလှောင်ထားသော နေရာ။ +* **Namespace**: Hugging Face Hub တွင် သုံးစွဲသူအကောင့် သို့မဟုတ် အဖွဲ့အစည်းအမည်။ +* **Hub API**: Hugging Face Hub နှင့် ပရိုဂရမ်ဖြင့် အပြန်အလှန် ချိတ်ဆက်ရန်အတွက် API။ +* **Local Repository**: သင့်ကွန်ပျူတာပေါ်ရှိ model files များ သို့မဟုတ် datasets များကို သိုလှောင်ထားသော နေရာ။ +* **Model Cards**: Hugging Face Hub ပေါ်ရှိ မော်ဒယ်တစ်ခု၏ အချက်အလက်များ၊ အသုံးပြုပုံနှင့် စွမ်းဆောင်ရည်များကို အကျဉ်းချုပ်ဖော်ပြထားသော စာမျက်နှာ။ +* **`AutoTokenizer` Class**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး မော်ဒယ်အမည်ကို အသုံးပြုပြီး သက်ဆိုင်ရာ tokenizer ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **Tokens**: စာသားကို ခွဲခြမ်းစိတ်ဖြာရာတွင် အသုံးပြုသော အသေးငယ်ဆုံးယူနစ်များ (ဥပမာ- စကားလုံးများ၊ subwords များ သို့မဟုတ် ပုဒ်ဖြတ်သံများ)။ +* **`encoded_input`**: Tokenizer ကနေ ထွက်လာတဲ့ encode လုပ်ထားတဲ့ input data။ +* **`input_ids`**: Tokenizer မှ ထုတ်ပေးသော tokens တစ်ခုစီ၏ ထူးခြားသော ဂဏန်းဆိုင်ရာ ID များ။ +* **`token_type_ids`**: Multi-sentence inputs တွေမှာ မည်သည့် token က မည်သည့် sentence (A သို့မဟုတ် B) မှ လာသည်ကို model ကို ပြောပြပေးသော IDs များ။ +* **`attention_mask`**: မော်ဒယ်ကို အာရုံစိုက်သင့်သည့် tokens များနှင့် လျစ်လျူရှုသင့်သည့် (padding) tokens များကို ခွဲခြားပေးသည့် binary mask။ +* **`tokenizer.decode()` Method**: Token IDs များကို မူရင်းစာသားသို့ ပြန်ပြောင်းလဲပေးသော method။ +* **Special Tokens**: Transformer model များက စာကြောင်းနယ်နိမိတ်များ သို့မဟုတ် အခြားအချက်အလက်များကို ကိုယ်စားပြုရန် အသုံးပြုသော အထူး tokens များ (ဥပမာ - `[CLS]`, `[SEP]`, `[PAD]`)။ +* **`[CLS]`**: BERT မော်ဒယ်တွင် classification task အတွက် အသုံးပြုသော special token (စာကြောင်း၏ အစတွင် ပေါ်လာသည်)။ +* **`[SEP]`**: BERT မော်ဒယ်တွင် စာကြောင်းများကြား ပိုင်းခြားရန် အသုံးပြုသော special token။ +* **Batching**: မတူညီသော input များစွာကို တစ်ပြိုင်နက်တည်း လုပ်ဆောင်နိုင်ရန် အုပ်စုဖွဲ့ခြင်း။ +* **Tensors**: Machine Learning frameworks (PyTorch, TensorFlow) များတွင် ဒေတာများကို ကိုယ်စားပြုသော multi-dimensional array များ။ +* **Rectangular Shapes**: ညီညာသော အတန်းများနှင့် ကော်လံများပါဝင်သည့် ပုံစံ (matrices ကဲ့သို့)။ +* **Arguments**: function သို့မဟုတ် method တစ်ခုသို့ ပေးပို့သော တန်ဖိုးများ။ \ No newline at end of file diff --git a/chapters/my/chapter2/4.mdx b/chapters/my/chapter2/4.mdx new file mode 100644 index 000000000..c1acefe9a --- /dev/null +++ b/chapters/my/chapter2/4.mdx @@ -0,0 +1,252 @@ + + +# Tokenizers[[tokenizers]] + + + + + +Tokenizers တွေဟာ NLP pipeline ရဲ့ အဓိက အစိတ်အပိုင်းတွေထဲက တစ်ခုပါ။ ၎င်းတို့မှာ ရည်ရွယ်ချက်တစ်ခုတည်းပဲ ရှိပါတယ်၊ text ကို model က လုပ်ဆောင်နိုင်တဲ့ data အဖြစ် ပြောင်းလဲဖို့ပါပဲ။ Model တွေက ဂဏန်းတွေကိုပဲ လုပ်ဆောင်နိုင်တာမို့၊ tokenizers တွေက ကျွန်တော်တို့ရဲ့ text inputs တွေကို numerical data အဖြစ် ပြောင်းလဲပေးဖို့ လိုအပ်ပါတယ်။ ဒီအပိုင်းမှာတော့ tokenization pipeline မှာ ဘာတွေ အတိအကျဖြစ်ပျက်လဲဆိုတာကို လေ့လာသွားပါမယ်။ + +NLP လုပ်ငန်းတွေမှာ အများအားဖြင့် လုပ်ဆောင်တဲ့ data က raw text ပါ။ ဒီလို text ရဲ့ ဥပမာတစ်ခုကို အောက်မှာ ကြည့်ပါ။ + +``` +Jim Henson was a puppeteer +``` + +သို့သော်လည်း၊ model တွေက ဂဏန်းတွေကိုပဲ လုပ်ဆောင်နိုင်တာမို့၊ raw text ကို ဂဏန်းတွေအဖြစ် ပြောင်းလဲဖို့ နည်းလမ်းတစ်ခုကို ကျွန်တော်တို့ ရှာဖွေဖို့ လိုအပ်ပါတယ်။ ဒါက tokenizers တွေ လုပ်ဆောင်တဲ့အရာ ဖြစ်ပြီး၊ ဒါကို လုပ်ဆောင်ဖို့ နည်းလမ်းများစွာ ရှိပါတယ်။ ရည်ရွယ်ချက်ကတော့ အဓိပ္ပာယ်အရှိဆုံး ကိုယ်စားပြုမှု (ဆိုလိုသည်မှာ model အတွက် အဓိပ္ပာယ်အရှိဆုံး ကိုယ်စားပြုမှု) နဲ့ ဖြစ်နိုင်ရင် အသေးငယ်ဆုံး ကိုယ်စားပြုမှုကို ရှာဖွေဖို့ပါပဲ။ + +tokenization algorithm အချို့ရဲ့ ဥပမာတွေကို ကြည့်ပြီး၊ tokenization နဲ့ ပတ်သက်ပြီး သင့်မှာရှိနိုင်တဲ့ မေးခွန်းအချို့ကို ဖြေဆိုဖို့ ကြိုးစားကြရအောင်။ + +## Word-based[[word-based]] + + + +ပထမဆုံး တွေးမိတဲ့ tokenizer အမျိုးအစားကတော့ *word-based* ပါ။ ဒါကို စည်းမျဉ်းအနည်းငယ်နဲ့ တည်ဆောက်ပြီး အသုံးပြုဖို့ အလွန်လွယ်ကူပြီး၊ အများအားဖြင့် ကောင်းမွန်တဲ့ ရလဒ်တွေ ထွက်ပေါ်လာပါတယ်။ ဥပမာအားဖြင့်၊ အောက်က ပုံမှာ၊ ရည်ရွယ်ချက်က raw text ကို စကားလုံးတွေအဖြစ် ပိုင်းခြားပြီး တစ်ခုချင်းစီအတွက် ဂဏန်းဆိုင်ရာ ကိုယ်စားပြုမှုကို ရှာဖွေဖို့ပါပဲ။ + +
+ An example of word-based tokenization. + +
+ +text ကို ပိုင်းခြားဖို့ နည်းလမ်းအမျိုးမျိုး ရှိပါတယ်။ ဥပမာအားဖြင့်၊ Python ရဲ့ `split()` function ကို အသုံးပြုပြီး whitespace ကို သုံးကာ text ကို စကားလုံးတွေအဖြစ် tokenize လုပ်ဆောင်နိုင်ပါတယ်။ + +```py +tokenized_text = "Jim Henson was a puppeteer".split() +print(tokenized_text) +``` + +```python out +['Jim', 'Henson', 'was', 'a', 'puppeteer'] +``` + +punctuation အတွက် အပိုစည်းမျဉ်းတွေရှိတဲ့ word tokenizer အမျိုးအစားတွေလည်း ရှိပါသေးတယ်။ ဒီလို tokenizer အမျိုးအစားနဲ့ဆိုရင် ကျွန်တော်တို့ဟာ အတော်လေး ကြီးမားတဲ့ "vocabularies" တွေနဲ့ အဆုံးသတ်နိုင်ပါတယ်။ vocabulary ဆိုတာက ကျွန်တော်တို့ရဲ့ corpus မှာရှိတဲ့ သီးခြား tokens စုစုပေါင်းအရေအတွက်နဲ့ သတ်မှတ်ပါတယ်။ + +စကားလုံးတစ်ခုစီကို ID တစ်ခုစီ ခွဲပေးပြီး 0 ကနေစပြီး vocabulary အရွယ်အစားအထိ သတ်မှတ်ပေးပါတယ်။ model က ဒီ IDs တွေကို စကားလုံးတစ်ခုစီကို ခွဲခြားသိမြင်ဖို့ အသုံးပြုပါတယ်။ + +ကျွန်တော်တို့ဟာ word-based tokenizer တစ်ခုနဲ့ ဘာသာစကားတစ်ခုကို အပြည့်အစုံ ကာဗာလုပ်ချင်တယ်ဆိုရင်၊ ဘာသာစကားထဲက စကားလုံးတစ်ခုစီအတွက် identifier တစ်ခုစီ ရှိဖို့ လိုအပ်ပါလိမ့်မယ်။ ဒါက ကြီးမားတဲ့ tokens အရေအတွက်ကို ထုတ်ပေးပါလိမ့်မယ်။ ဥပမာအားဖြင့်၊ English ဘာသာစကားမှာ စကားလုံး ၅၀၀,၀၀၀ ကျော်ရှိတာကြောင့် စကားလုံးတစ်ခုစီကနေ input ID တစ်ခုဆီ map လုပ်ဖို့အတွက် ID အရေအတွက် အများကြီးကို မှတ်ထားဖို့ လိုအပ်ပါလိမ့်မယ်။ ဒါ့အပြင် "dog" လို စကားလုံးတွေကို "dogs" လို စကားလုံးတွေနဲ့ မတူအောင် ကိုယ်စားပြုထားပြီး၊ "dog" နဲ့ "dogs" တို့ဟာ ဆင်တူတယ်ဆိုတာကို model က အစပိုင်းမှာ သိဖို့ နည်းလမ်းမရှိပါဘူး- ၎င်းက စကားလုံးနှစ်ခုကို ဆက်စပ်မှုမရှိဘူးလို့ ခွဲခြားသတ်မှတ်ပါလိမ့်မယ်။ "run" နဲ့ "running" လို အခြားဆင်တူစကားလုံးတွေနဲ့လည်း အတူတူပါပဲ၊ model က အစပိုင်းမှာ ဆင်တူတယ်လို့ မမြင်ပါဘူး။ + +နောက်ဆုံးအနေနဲ့၊ ကျွန်တော်တို့ရဲ့ vocabulary မှာ မပါဝင်တဲ့ စကားလုံးတွေကို ကိုယ်စားပြုဖို့ custom token တစ်ခု လိုအပ်ပါတယ်။ ဒါကို "unknown" token လို့ ခေါ်ပြီး၊ မကြာခဏဆိုသလို "[UNK]" သို့မဟုတ် "<unk>" နဲ့ ကိုယ်စားပြုပါတယ်။ tokenizer က ဒီ tokens တွေ အများကြီး ထုတ်ပေးနေတာကို သင်တွေ့ရရင် ဒါက မကောင်းတဲ့ လက္ခဏာတစ်ခုပါ။ ဘာလို့လဲဆိုတော့ ၎င်းက စကားလုံးတစ်ခုရဲ့ အဓိပ္ပာယ်ရှိတဲ့ ကိုယ်စားပြုမှုကို ရယူနိုင်ခြင်းမရှိဘဲ၊ သင်ဟာ အချက်အလက်တွေကို ဆုံးရှုံးနေတာကြောင့်ပါပဲ။ vocabulary ကို ဖန်တီးတဲ့အခါ ရည်ရွယ်ချက်ကတော့ tokenizer က unknown token အဖြစ် စကားလုံးအနည်းဆုံးကို tokenized လုပ်နိုင်အောင် ဖန်တီးဖို့ပါပဲ။ + +unknown tokens အရေအတွက်ကို လျှော့ချဖို့ နည်းလမ်းတစ်ခုကတော့ တစ်ဆင့်နိမ့်ဆင်းပြီး *character-based* tokenizer ကို အသုံးပြုဖို့ပါပဲ။ + +## Character-based[[character-based]] + + + +Character-based tokenizers တွေက text ကို စကားလုံးတွေအစား characters တွေအဖြစ် ပိုင်းခြားပါတယ်။ ဒါက အဓိက အကျိုးကျေးဇူး နှစ်ခု ရှိပါတယ်- + +- Vocabulary က အများကြီး သေးငယ်ပါတယ်။ +- out-of-vocabulary (unknown) tokens တွေ အများကြီး နည်းပါးသွားပါတယ်။ ဘာလို့လဲဆိုတော့ စကားလုံးတိုင်းကို characters တွေကနေ တည်ဆောက်နိုင်လို့ပါ။ + +ဒါပေမယ့် ဒီနေရာမှာလည်း နေရာလွတ်တွေ (spaces) နဲ့ punctuation တွေနဲ့ ပတ်သက်ပြီး မေးခွန်းအချို့ ပေါ်ပေါက်လာပါတယ်။ + +
+ An example of character-based tokenization. + +
+ +ဒီနည်းလမ်းကလည်း ပြီးပြည့်စုံတာ မဟုတ်ပါဘူး။ ကိုယ်စားပြုမှုဟာ စကားလုံးတွေအစား characters တွေပေါ် အခြေခံထားတာကြောင့်၊ တစ်ခုတည်းသော character တစ်ခုက သူ့ဘာသာသူ အဓိပ္ပာယ်သိပ်မရှိဘူးလို့ အလိုလိုဆိုနိုင်ပါတယ်။ စကားလုံးတွေနဲ့ဆိုရင်တော့ အဲလို မဟုတ်ပါဘူး။ ဒါပေမယ့် ဒါက ဘာသာစကားပေါ် မူတည်ပြီး ကွဲပြားပါတယ်။ ဥပမာအားဖြင့်၊ တရုတ်ဘာသာစကားမှာ character တစ်ခုစီက Latin ဘာသာစကားက character တစ်ခုထက် အချက်အလက် ပိုသယ်ဆောင်ပါတယ်။ + +စဉ်းစားရမယ့် နောက်ထပ်အချက်တစ်ခုကတော့ ကျွန်တော်တို့ရဲ့ model က လုပ်ဆောင်ရမယ့် tokens ပမာဏ အများကြီးနဲ့ အဆုံးသတ်ရပါလိမ့်မယ်။ word-based tokenizer တစ်ခုနဲ့ဆိုရင် စကားလုံးတစ်လုံးဟာ token တစ်ခုတည်းသာ ဖြစ်ပေမယ့်၊ characters တွေအဖြစ် ပြောင်းလဲလိုက်တဲ့အခါ token ၁၀ ခု သို့မဟုတ် ပိုများတဲ့အထိ အလွယ်တကူ ဖြစ်သွားနိုင်ပါတယ်။ + +နှစ်ခုစလုံးရဲ့ အကောင်းဆုံးကို ရယူဖို့အတွက်၊ ချဉ်းကပ်မှုနှစ်ခုကို ပေါင်းစပ်ထားတဲ့ တတိယနည်းပညာဖြစ်တဲ့ *subword tokenization* ကို ကျွန်တော်တို့ အသုံးပြုနိုင်ပါတယ်။ + +## Subword Tokenization[[subword-tokenization]] + + + +Subword tokenization algorithm တွေဟာ မကြာခဏ အသုံးပြုတဲ့ စကားလုံးတွေကို ပိုသေးငယ်တဲ့ subwords တွေအဖြစ် ပိုင်းခြားသင့်ပါဘူး၊ ဒါပေမယ့် ရှားပါးတဲ့ စကားလုံးတွေကိုတော့ အဓိပ္ပာယ်ရှိတဲ့ subwords တွေအဖြစ် ခွဲခြမ်းသင့်တယ်ဆိုတဲ့ နိယာမပေါ် အခြေခံပါတယ်။ + +ဥပမာအားဖြင့်၊ "annoyingly" ကို ရှားပါးတဲ့ စကားလုံးအဖြစ် မှတ်ယူနိုင်ပြီး "annoying" နဲ့ "ly" အဖြစ် ခွဲခြားနိုင်ပါတယ်။ ဒါတွေဟာ တစ်ဦးချင်းစီ subwords တွေအဖြစ် ပိုမိုမကြာခဏ ပေါ်လာနိုင်ဖွယ်ရှိပြီး၊ တစ်ချိန်တည်းမှာ "annoyingly" ရဲ့ အဓိပ္ပာယ်ကို "annoying" နဲ့ "ly" ရဲ့ ပေါင်းစပ်အဓိပ္ပာယ်ကနေ ထိန်းသိမ်းထားပါတယ်။ + +ဒီနေရာမှာ subword tokenization algorithm က "Let's do tokenization!" ဆိုတဲ့ sequence ကို ဘယ်လို tokenize လုပ်မယ်ဆိုတာကို ပြသထားတဲ့ ဥပမာတစ်ခု ဖြစ်နိုင်ပါတယ်။ + +
+ A subword tokenization algorithm. + +
+ +ဒီ subwords တွေဟာ အဓိပ္ပာယ်ဆိုင်ရာ အချက်အလက်များစွာကို ပေးစွမ်းပါတယ်။ ဥပမာအားဖြင့်၊ အထက်ပါ ဥပမာမှာ "tokenization" ကို "token" နဲ့ "ization" အဖြစ် ပိုင်းခြားခဲ့ပါတယ်။ ဒါတွေဟာ အဓိပ္ပာယ်ရှိတဲ့ tokens နှစ်ခုဖြစ်ပြီး နေရာလည်းသက်သာပါတယ် (ရှည်လျားတဲ့ စကားလုံးတစ်လုံးကို ကိုယ်စားပြုဖို့ tokens နှစ်ခုပဲ လိုအပ်ပါတယ်)။ ဒါက ကျွန်တော်တို့ကို သေးငယ်တဲ့ vocabularies တွေနဲ့ ကောင်းမွန်တဲ့ coverage ကို ပေးနိုင်ပြီး unknown tokens တွေလည်း မရှိသလောက်ပါပဲ။ + +ဒီနည်းလမ်းက Turkish လိုမျိုး agglutinative languages တွေမှာ အထူးအသုံးဝင်ပါတယ်။ ဘာလို့လဲဆိုတော့ subwords တွေကို ဆက်စပ်ပြီး (နီးပါး) ပမာဏအကန့်အသတ်မရှိ ရှည်လျားတဲ့ ရှုပ်ထွေးတဲ့ စကားလုံးတွေကို ဖွဲ့စည်းနိုင်လို့ပါ။ + +### အခြားနည်းလမ်းများ![[and-more]] + +အံ့သြစရာမလိုဘဲ၊ အခြားနည်းပညာများစွာ ရှိပါသေးတယ်။ အချို့ကို ဖော်ပြရရင်- + +- Byte-level BPE, GPT-2 မှာ အသုံးပြုထားပါတယ်။ +- WordPiece, BERT မှာ အသုံးပြုထားပါတယ်။ +- SentencePiece သို့မဟုတ် Unigram, multilingual models အများအပြားမှာ အသုံးပြုထားပါတယ်။ + +Tokenizer တွေ ဘယ်လိုအလုပ်လုပ်တယ်ဆိုတဲ့ အသိပညာဟာ API နဲ့ စတင်ဖို့ လုံလောက်သင့်ပါပြီ။ + +## Loading နှင့် Saving[[loading-and-saving]] + +Tokenizers တွေကို load လုပ်တာနဲ့ save လုပ်တာက model တွေနဲ့ လုပ်တာလိုပဲ ရိုးရှင်းပါတယ်။ တကယ်တော့၊ ၎င်းဟာ `from_pretrained()` နဲ့ `save_pretrained()` ဆိုတဲ့ methods နှစ်ခုတည်းပေါ် အခြေခံထားတာပါ။ ဒီ methods တွေက tokenizer အသုံးပြုတဲ့ algorithm (model ရဲ့ architecture နဲ့ ဆင်တူပါတယ်) နဲ့ ၎င်းရဲ့ vocabulary (model ရဲ့ weights နဲ့ ဆင်တူပါတယ်) နှစ်ခုလုံးကို load သို့မဟုတ် save လုပ်ပေးပါလိမ့်မယ်။ + +BERT နဲ့ တူညီတဲ့ checkpoint နဲ့ train လုပ်ထားတဲ့ BERT tokenizer ကို load လုပ်တာက model ကို load လုပ်တာနဲ့ နည်းလမ်းတူတူပါပဲ၊ ဒါပေမယ့် ကျွန်တော်တို့က `BertTokenizer` class ကို အသုံးပြုရုံပါပဲ။ + +```py +from transformers import BertTokenizer + +tokenizer = BertTokenizer.from_pretrained("bert-base-cased") +``` + +`AutoModel` နဲ့ ဆင်တူစွာ၊ `AutoTokenizer` class က checkpoint name ကို အခြေခံပြီး library ထဲက မှန်ကန်တဲ့ tokenizer class ကို ရယူပါလိမ့်မယ်၊ ပြီးတော့ မည်သည့် checkpoint နဲ့မဆို တိုက်ရိုက် အသုံးပြုနိုင်စေပါတယ်။ + +```py +from transformers import AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") +``` + +အခု ကျွန်တော်တို့ tokenizer ကို ယခင်အပိုင်းမှာ ပြသခဲ့သလို အသုံးပြုနိုင်ပါပြီ။ + +```python +tokenizer("Using a Transformer network is simple") +``` + +```python out +{'input_ids': [101, 7993, 170, 11303, 1200, 2443, 1110, 3014, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +tokenizer ကို save လုပ်တာက model ကို save လုပ်တာနဲ့ အတူတူပါပဲ။ + +```py +tokenizer.save_pretrained("directory_on_my_computer") +``` + +`token_type_ids` အကြောင်းကို [Chapter 3](/course/chapter3) မှာ ပိုပြီး အသေးစိတ် ဆွေးနွေးပါမယ်။ `attention_mask` key ကိုတော့ နောက်မှ အနည်းငယ် ရှင်းပြပါမယ်။ ပထမဆုံးအနေနဲ့ `input_ids` တွေ ဘယ်လို ထုတ်လုပ်ခဲ့လဲဆိုတာ ကြည့်ရအောင်။ ဒါကို လုပ်ဖို့ tokenizer ရဲ့ ကြားခံ methods တွေကို ကြည့်ဖို့ လိုအပ်ပါလိမ့်မယ်။ + +## Encoding[[encoding]] + + + +Text ကို ဂဏန်းတွေအဖြစ် ပြောင်းလဲတာကို *encoding* လို့ ခေါ်ပါတယ်။ Encoding ကို အဆင့်နှစ်ဆင့်နဲ့ လုပ်ဆောင်ပါတယ်- tokenization လုပ်ခြင်း၊ ပြီးရင် input IDs တွေအဖြစ် ပြောင်းလဲခြင်းတို့ ဖြစ်ပါတယ်။ + +ကျွန်တော်တို့ တွေ့ခဲ့ရသလိုပဲ၊ ပထမအဆင့်က text ကို စကားလုံးတွေအဖြစ် (သို့မဟုတ် စကားလုံးအစိတ်အပိုင်းများ၊ punctuation symbols စသည်တို့) ပိုင်းခြားတာဖြစ်ပြီး၊ ဒါကို အများအားဖြင့် *tokens* လို့ ခေါ်ပါတယ်။ ဒီလုပ်ငန်းစဉ်ကို ထိန်းချုပ်နိုင်တဲ့ စည်းမျဉ်းများစွာ ရှိတာကြောင့် model ကို pretrained လုပ်ခဲ့တုန်းက အသုံးပြုခဲ့တဲ့ စည်းမျဉ်းတွေ အတူတူကို သေချာအသုံးပြုနိုင်ဖို့ model ရဲ့ နာမည်ကို အသုံးပြုပြီး tokenizer ကို instantiate လုပ်ဖို့ လိုအပ်ပါတယ်။ + +ဒုတိယအဆင့်ကတော့ အဲဒီ tokens တွေကို ဂဏန်းတွေအဖြစ် ပြောင်းလဲတာပါ၊ ဒါမှ ၎င်းတို့ကနေ tensor တစ်ခုကို တည်ဆောက်ပြီး model ကို ထည့်သွင်းပေးနိုင်မှာပါ။ ဒါကို လုပ်ဖို့အတွက် tokenizer မှာ *vocabulary* တစ်ခုရှိပြီး၊ ဒါက `from_pretrained()` method နဲ့ instantiate လုပ်တဲ့အခါ ကျွန်တော်တို့ download လုပ်တဲ့ အစိတ်အပိုင်းပါပဲ။ ထပ်မံပြီး၊ model ကို pretrained လုပ်ခဲ့တုန်းက အသုံးပြုခဲ့တဲ့ vocabulary အတူတူကို ကျွန်တော်တို့ အသုံးပြုဖို့ လိုအပ်ပါတယ်။ + +အဆင့်နှစ်ဆင့်ကို ပိုမိုနားလည်နိုင်ဖို့အတွက် ၎င်းတို့ကို သီးခြားစီ လေ့လာသွားပါမယ်။ tokenization pipeline ရဲ့ အစိတ်အပိုင်းအချို့ကို သီးခြားစီ လုပ်ဆောင်တဲ့ methods အချို့ကို ကျွန်တော်တို့ အသုံးပြုသွားမှာပါ။ ဒါက အဲဒီအဆင့်တွေရဲ့ ကြားခံရလဒ်တွေကို သင့်ကို ပြသဖို့ပါပဲ။ ဒါပေမယ့် လက်တွေ့မှာတော့ သင်ဟာ သင်ရဲ့ inputs တွေပေါ်မှာ tokenizer ကို တိုက်ရိုက် ခေါ်ဆိုသင့်ပါတယ် (အပိုင်း ၂ မှာ ပြထားသလို)။ + +### Tokenization[[tokenization]] + +Tokenization လုပ်ငန်းစဉ်ကို tokenizer ရဲ့ `tokenize()` method က လုပ်ဆောင်ပါတယ်။ + +```py +from transformers import AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") + +sequence = "Using a Transformer network is simple" +tokens = tokenizer.tokenize(sequence) + +print(tokens) +``` + +ဒီ method ရဲ့ output ကတော့ strings ဒါမှမဟုတ် tokens တွေရဲ့ list တစ်ခုပါ။ + +```python out +['Using', 'a', 'transform', '##er', 'network', 'is', 'simple'] +``` + +ဒီ tokenizer က subword tokenizer တစ်ခုပါ- ဒါက စကားလုံးတွေကို သူ့ရဲ့ vocabulary က ကိုယ်စားပြုနိုင်တဲ့ tokens တွေ ရရှိတဲ့အထိ ပိုင်းခြားပေးပါတယ်။ ဒီဥပမာမှာ `transformer` ကို `transform` နဲ့ `##er` ဆိုတဲ့ tokens နှစ်ခုအဖြစ် ပိုင်းခြားထားတာကို တွေ့ရပါတယ်။ + +### Tokens တွေကနေ Input IDs တွေဆီသို့[[from-tokens-to-input-ids]] + +Input IDs တွေအဖြစ် ပြောင်းလဲခြင်းကို `convert_tokens_to_ids()` tokenizer method က ကိုင်တွယ်ပါတယ်။ + +```py +ids = tokenizer.convert_tokens_to_ids(tokens) + +print(ids) +``` + +```python out +[7993, 170, 11303, 1200, 2443, 1110, 3014] +``` + +ဒီ outputs တွေကို သင့်လျော်တဲ့ framework tensor အဖြစ် ပြောင်းလဲပြီးတာနဲ့၊ ဒီအခန်းရဲ့ အစောပိုင်းမှာ တွေ့ခဲ့ရသလို model ရဲ့ inputs တွေအဖြစ် အသုံးပြုနိုင်ပါတယ်။ + +> [!TIP] +> ✏️ **စမ်းသပ်ကြည့်ပါ။** အပိုင်း ၂ မှာ အသုံးပြုခဲ့တဲ့ input sentences တွေ ("I've been waiting for a HuggingFace course my whole life." နဲ့ "I hate this so much!") ပေါ်မှာ နောက်ဆုံးအဆင့်နှစ်ခု (tokenization နဲ့ input IDs အဖြစ် ပြောင်းလဲခြင်း) ကို ပြန်လည်လုပ်ဆောင်ပါ။ ကျွန်တော်တို့ အစောပိုင်းက ရရှိခဲ့တဲ့ input IDs တွေ အတူတူ ရရှိမရရှိ စစ်ဆေးပါ။ + +## Decoding[[decoding]] + +*Decoding* ဆိုတာကတော့ ပြောင်းပြန်လုပ်တာပါ၊ vocabulary indices တွေကနေ string တစ်ခုကို ပြန်ရချင်တာပါ။ ဒါကို `decode()` method နဲ့ အောက်ပါအတိုင်း လုပ်ဆောင်နိုင်ပါတယ်။ + +```py +decoded_string = tokenizer.decode([7993, 170, 11303, 1200, 2443, 1110, 3014]) +print(decoded_string) +``` + +```python out +'Using a Transformer network is simple' +``` + +`decode` method က indices တွေကို tokens တွေအဖြစ် ပြန်ပြောင်းပေးရုံသာမကဘဲ၊ တူညီတဲ့ စကားလုံးရဲ့ အစိတ်အပိုင်းဖြစ်တဲ့ tokens တွေကို စုစည်းပြီး ဖတ်လို့ရတဲ့ စာကြောင်းတစ်ခုကို ထုတ်လုပ်ပေးတာကို သတိပြုပါ။ ဒီ behavior က text အသစ်တွေကို ခန့်မှန်းတဲ့ model တွေကို အသုံးပြုတဲ့အခါ (prompt တစ်ခုကနေ ထုတ်လုပ်တဲ့ text ဖြစ်စေ၊ ဒါမှမဟုတ် translation သို့မဟုတ် summarization လို sequence-to-sequence ပြဿနာတွေအတွက် ဖြစ်စေ) အလွန်အသုံးဝင်ပါလိမ့်မယ်။ + +အခုဆိုရင် tokenizer တစ်ခုက ကိုင်တွယ်နိုင်တဲ့ atomic operations တွေကို သင်နားလည်သင့်ပါပြီ- tokenization လုပ်ခြင်း၊ IDs တွေအဖြစ် ပြောင်းလဲခြင်း၊ နဲ့ IDs တွေကို string အဖြစ် ပြန်ပြောင်းလဲခြင်းတို့ ဖြစ်ပါတယ်။ သို့သော်လည်း၊ ကျွန်တော်တို့ဟာ ရေခဲတောင်ရဲ့ ထိပ်ဖျားလေးကိုပဲ ကုတ်ဖဲ့မိပါသေးတယ်။ နောက်အပိုင်းမှာတော့ ကျွန်တော်တို့ရဲ့ နည်းလမ်းကို သူ့ရဲ့ အကန့်အသတ်တွေဆီ ယူဆောင်သွားပြီး ၎င်းတို့ကို ဘယ်လိုကျော်လွှားရမလဲဆိုတာ ကြည့်ရပါလိမ့်မယ်။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Tokenizers**: စာသား (သို့မဟုတ် အခြားဒေတာ) ကို AI မော်ဒယ်များ စီမံဆောင်ရွက်နိုင်ရန် tokens တွေအဖြစ် ပိုင်းခြားပေးသည့် ကိရိယာ သို့မဟုတ် လုပ်ငန်းစဉ်။ +* **NLP Pipeline**: Natural Language Processing (NLP) လုပ်ငန်းတာဝန်တစ်ခုကို ပြီးမြောက်အောင် လုပ်ဆောင်ရန် အဆင့်ဆင့် လုပ်ဆောင်ရမည့် လုပ်ငန်းစဉ်များ။ +* **Raw Text**: မည်သည့်လုပ်ဆောင်မှုမျှ မပြုလုပ်ရသေးသော သို့မဟုတ် ပုံစံမချရသေးသော မူရင်းစာသား။ +* **Numerical Data**: ဂဏန်းပုံစံဖြင့် ဖော်ပြထားသော အချက်အလက်များ။ +* **Tokenization**: စာသားကို tokens များအဖြစ် ပိုင်းခြားသော လုပ်ငန်းစဉ်။ +* **Tokens**: စာသားကို ခွဲခြမ်းစိတ်ဖြာရာတွင် အသုံးပြုသော အသေးငယ်ဆုံးယူနစ်များ (ဥပမာ- စကားလုံးများ၊ subwords များ သို့မဟုတ် ပုဒ်ဖြတ်သံများ)။ +* **Word-based Tokenizer**: စာသားကို စကားလုံးများအဖြစ် ပိုင်းခြားသော tokenizer အမျိုးအစား။ +* **Whitespace**: စာသားထဲရှိ နေရာလွတ်များ (space, tab, newline)။ +* **`split()` Function**: Python တွင် string တစ်ခုကို သတ်မှတ်ထားသော delimiter ဖြင့် ပိုင်းခြားရန် အသုံးပြုသော function။ +* **Vocabulary**: tokenizer သို့မဟုတ် model တစ်ခုက သိရှိနားလည်ပြီး ကိုင်တွယ်နိုင်သော ထူးခြားသည့် tokens များ စုစုပေါင်း။ +* **Corpus**: Machine Learning တွင် အသုံးပြုသော စာသားဒေတာအစုအဝေးကြီး။ +* **ID**: token တစ်ခုစီကို ကိုယ်စားပြုသော ထူးခြားသည့် ဂဏန်း။ +* **Unknown Token (`[UNK]`, ``)**: tokenizer ၏ vocabulary တွင် မပါဝင်သော စကားလုံးများကို ကိုယ်စားပြုရန် အသုံးပြုသော special token။ +* **Character-based Tokenizer**: စာသားကို characters များအဖြစ် ပိုင်းခြားသော tokenizer အမျိုးအစား။ +* **Out-of-vocabulary (OOV) Tokens**: tokenizer ၏ vocabulary တွင် မပါဝင်သော tokens များ။ +* **Subword Tokenization**: မကြာခဏ အသုံးပြုသော စကားလုံးများကို မခွဲဘဲ၊ ရှားပါးသော စကားလုံးများကို အဓိပ္ပာယ်ရှိသော subwords များအဖြစ် ခွဲခြားသော tokenization နည်းလမ်း။ +* **Agglutinative Languages**: စကားလုံးများကို အစိတ်အပိုင်းငယ်လေးများ ပေါင်းစပ်ခြင်းဖြင့် ဖွဲ့စည်းထားသော ဘာသာစကားများ (ဥပမာ- တူရကီဘာသာ)။ +* **Byte-level BPE**: Byte Pair Encoding (BPE) ၏ ပြောင်းလဲထားသော ပုံစံတစ်ခုဖြစ်ပြီး characters များအစား bytes များကို အသုံးပြုသည်။ GPT-2 တွင် အသုံးပြုသည်။ +* **WordPiece**: Google မှ ဖန်တီးထားသော subword tokenization algorithm တစ်ခုဖြစ်ပြီး BERT တွင် အသုံးပြုသည်။ +* **SentencePiece / Unigram**: Google မှ ဖန်တီးထားသော subword tokenization algorithm များဖြစ်ပြီး multilingual models များတွင် အသုံးပြုသည်။ +* **`AutoTokenizer` Class**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး မော်ဒယ်အမည်ကို အသုံးပြုပြီး သက်ဆိုင်ရာ tokenizer ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **`BertTokenizer` Class**: BERT model အတွက် သီးသန့်ဒီဇိုင်းထုတ်ထားသော tokenizer class။ +* **`from_pretrained()` Method**: Pre-trained model သို့မဟုတ် tokenizer ကို load လုပ်ရန် အသုံးပြုသော method။ +* **`save_pretrained()` Method**: Model သို့မဟုတ် tokenizer ၏ weights များနှင့် architecture configuration ကို save လုပ်ရန် အသုံးပြုသော method။ +* **Encoding**: Text ကို ဂဏန်းဆိုင်ရာ ကိုယ်စားပြုမှုအဖြစ် ပြောင်းလဲသော လုပ်ငန်းစဉ်။ +* **`tokenize()` Method**: tokenizer ၏ text ကို tokens များအဖြစ် ပိုင်းခြားပေးသော method။ +* **`convert_tokens_to_ids()` Method**: tokens list ကို input IDs list အဖြစ် ပြောင်းလဲပေးသော tokenizer method။ +* **Decoding**: ဂဏန်းဆိုင်ရာ ကိုယ်စားပြုမှု (vocabulary indices) များမှ မူရင်းစာသားသို့ ပြန်ပြောင်းလဲသော လုပ်ငန်းစဉ်။ +* **`decode()` Method**: input IDs များကို မူရင်းစာသားသို့ ပြန်ပြောင်းလဲပေးသော method။ +* **Sequence-to-sequence Problems**: input sequence တစ်ခုမှ output sequence တစ်ခုသို့ ပြောင်းလဲခြင်း လုပ်ငန်းများ (ဥပမာ- ဘာသာပြန်ခြင်း၊ အနှစ်ချုပ်ခြင်း)။ +* **Prompt**: မော်ဒယ်ကို text ထုတ်လုပ်ရန်အတွက် ပေးသော အစစာသား။ \ No newline at end of file diff --git a/chapters/my/chapter2/5.mdx b/chapters/my/chapter2/5.mdx new file mode 100644 index 000000000..f9ef55615 --- /dev/null +++ b/chapters/my/chapter2/5.mdx @@ -0,0 +1,225 @@ + + +# Sequence များစွာကို ကိုင်တွယ်ခြင်း[[handling-multiple-sequences]] + + + + + +ယခင်အပိုင်းမှာ ကျွန်တော်တို့ဟာ အသေးစား အရှည်ရှိတဲ့ sequence တစ်ခုတည်းပေါ်မှာ inference လုပ်တဲ့ အလွယ်ကူဆုံး အသုံးပြုမှုပုံစံကို လေ့လာခဲ့ပါတယ်။ သို့သော်လည်း၊ အချို့မေးခွန်းတွေ အခုကတည်းက ပေါ်ပေါက်လာပါတယ်- + +- sequence များစွာကို ဘယ်လိုကိုင်တွယ်မလဲ။ +- *အရှည်မတူညီတဲ့* sequence များစွာကို ဘယ်လိုကိုင်တွယ်မလဲ။ +- vocabulary indices တွေဟာ model က ကောင်းကောင်း အလုပ်လုပ်နိုင်ဖို့ တစ်ခုတည်းသော input တွေလား။ +- အရမ်းရှည်လျားတဲ့ sequence မျိုး ရှိပါသလား။ + +ဒီမေးခွန်းတွေက ဘယ်လိုပြဿနာတွေ ဖြစ်စေလဲ၊ Hugging Face Transformers API ကို အသုံးပြုပြီး ဒါတွေကို ဘယ်လိုဖြေရှင်းနိုင်လဲဆိုတာ ကြည့်ရအောင်။ + +## Model များသည် Inputs များ၏ Batch တစ်ခုကို မျှော်လင့်ကြသည်[[models-expect-a-batch-of-inputs]] + +ယခင် လေ့ကျင့်ခန်းမှာ sequence တွေကို ဂဏန်း list တွေအဖြစ် ဘယ်လိုပြောင်းလဲတယ်ဆိုတာကို သင်တွေ့ခဲ့ရပါတယ်။ ဒီဂဏန်း list ကို tensor အဖြစ် ပြောင်းလဲပြီး model ကို ပို့ကြည့်ရအောင်။ + +```py +import torch +from transformers import AutoTokenizer, AutoModelForSequenceClassification + +checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" +tokenizer = AutoTokenizer.from_pretrained(checkpoint) +model = AutoModelForSequenceClassification.from_pretrained(checkpoint) + +sequence = "I've been waiting for a HuggingFace course my whole life." + +tokens = tokenizer.tokenize(sequence) +ids = tokenizer.convert_tokens_to_ids(tokens) +input_ids = torch.tensor(ids) +# This line will fail. +model(input_ids) +``` + +```python out +IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) +``` + +ဟာ မဟုတ်သေးပါဘူး။ ဒါက ဘာကြောင့် အဆင်မပြေတာလဲ။ ကျွန်တော်တို့ အပိုင်း ၂ မှာပါတဲ့ pipeline အဆင့်တွေကို လိုက်နာခဲ့တာပဲလေဗျာ။ + +ပြဿနာကတော့ ကျွန်တော်တို့ model ကို single sequence တစ်ခုပဲ ပို့ခဲ့တာပါ၊ ဒါပေမယ့် 🤗 Transformers model တွေက default အားဖြင့် sentences များစွာကို မျှော်လင့်ထားပါတယ်။ ဒီနေရာမှာ tokenizer က `sequence` တစ်ခုပေါ်မှာ အသုံးပြုခဲ့တုန်းက နောက်ကွယ်မှာ လုပ်ခဲ့တဲ့အရာ အားလုံးကို ကျွန်တော်တို့ လုပ်ဆောင်ဖို့ ကြိုးစားခဲ့ပါတယ်။ ဒါပေမယ့် သေချာကြည့်မယ်ဆိုရင် tokenizer က input IDs list ကို tensor တစ်ခုအဖြစ် ပြောင်းပေးရုံသာမကဘဲ၊ ၎င်းရဲ့ အပေါ်မှာ dimension တစ်ခုကို ထည့်သွင်းခဲ့တယ်ဆိုတာကို သင်တွေ့ရပါလိမ့်မယ်- + +```py +tokenized_inputs = tokenizer(sequence, return_tensors="pt") +print(tokenized_inputs["input_ids"]) +``` + +```python out +tensor([[ 101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, + 2607, 2026, 2878, 2166, 1012, 102]]) +``` + +နောက်တစ်ကြိမ် ထပ်ကြိုးစားပြီး dimension အသစ်တစ်ခု ထည့်ကြည့်ရအောင်။ + +```py +import torch +from transformers import AutoTokenizer, AutoModelForSequenceClassification + +checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" +tokenizer = AutoTokenizer.from_pretrained(checkpoint) +model = AutoModelForSequenceClassification.from_pretrained(checkpoint) + +sequence = "I've been waiting for a HuggingFace course my whole life." + +tokens = tokenizer.tokenize(sequence) +ids = tokenizer.convert_tokens_to_ids(tokens) + +input_ids = torch.tensor([ids]) +print("Input IDs:", input_ids) + +output = model(input_ids) +print("Logits:", output.logits) +``` + +ကျွန်တော်တို့ input IDs တွေရော၊ ထွက်လာတဲ့ logits တွေရော print လုပ်လိုက်ပါတယ်၊ ဒါကတော့ output ပါ။ + +```python out +Input IDs: [[ 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012]] +Logits: [[-2.7276, 2.8789]] +``` + +*Batching* ဆိုတာက model ကို sentences များစွာကို တစ်ပြိုင်နက်တည်း ပေးပို့တဲ့ လုပ်ဆောင်ချက်ပါပဲ။ သင့်မှာ စာကြောင်းတစ်ကြောင်းတည်းသာ ရှိရင်၊ single sequence တစ်ခုတည်းနဲ့ batch တစ်ခုကို တည်ဆောက်နိုင်ပါတယ်။ + +``` +batched_ids = [ids, ids] +``` + +ဒါက အတူတူ sequences နှစ်ခုပါတဲ့ batch တစ်ခုပါပဲ။ + +> [!TIP] +> ✏️ **စမ်းသပ်ကြည့်ပါ။** ဒီ `batched_ids` list ကို tensor အဖြစ် ပြောင်းလဲပြီး သင့် model ကို ဖြတ်သန်းပါ။ ယခင်က ရရှိခဲ့တဲ့ logits တွေ အတူတူ (ဒါပေမယ့် နှစ်ဆ) ရရှိမရရှိ စစ်ဆေးပါ။ + +Batching လုပ်ခြင်းက သင် model ကို sentences များစွာ ထည့်သွင်းတဲ့အခါ အလုပ်လုပ်စေပါတယ်။ sentences များစွာကို အသုံးပြုတာက single sequence နဲ့ batch တစ်ခုတည်ဆောက်တာလိုပဲ ရိုးရှင်းပါတယ်။ ဒါပေမယ့် ဒုတိယပြဿနာတစ်ခု ရှိပါတယ်။ စာကြောင်းနှစ်ခု (သို့မဟုတ် ပိုများ) ကို batch လုပ်ဖို့ ကြိုးစားတဲ့အခါ၊ ၎င်းတို့ရဲ့ အရှည်တွေက မတူညီနိုင်ပါဘူး။ သင် tensors တွေနဲ့ အရင်က အလုပ်လုပ်ဖူးတယ်ဆိုရင်၊ ၎င်းတို့ဟာ ထောင့်မှန်ပုံစံ (rectangular shape) ဖြစ်ဖို့ လိုအပ်တယ်ဆိုတာ သင်သိပါလိမ့်မယ်။ ဒါကြောင့် input IDs list ကို tensor တစ်ခုအဖြစ် တိုက်ရိုက်ပြောင်းလို့ ရမှာ မဟုတ်ပါဘူး။ ဒီပြဿနာကို ဖြေရှင်းဖို့အတွက် ကျွန်တော်တို့ဟာ inputs တွေကို များသောအားဖြင့် *pad* လုပ်ပါတယ်။ + +## Inputs များကို Padding လုပ်ခြင်း[[padding-the-inputs]] + +အောက်ပါ list of lists ကို tensor အဖြစ် ပြောင်းလဲလို့ မရပါဘူး။ + +```py no-format +batched_ids = [ + [200, 200, 200], + [200, 200] +] +``` + +ဒီပြဿနာကို ဖြေရှင်းဖို့အတွက် ကျွန်တော်တို့ tensors တွေကို ထောင့်မှန်ပုံစံ ဖြစ်အောင် ပြုလုပ်ဖို့ *padding* ကို အသုံးပြုပါမယ်။ Padding က ကျွန်တော်တို့ရဲ့ sentences တွေအားလုံးကို အရှည်တူညီအောင် သေချာစေဖို့အတွက် *padding token* လို့ခေါ်တဲ့ special word တစ်ခုကို တန်ဖိုးနည်းတဲ့ sentences တွေမှာ ထည့်ပေးပါတယ်။ ဥပမာအားဖြင့်၊ သင့်မှာ စကားလုံး ၁၀ လုံးပါတဲ့ စာကြောင်း ၁၀ ကြောင်းနဲ့ စကားလုံး ၂၀ လုံးပါတဲ့ စာကြောင်း ၁ ကြောင်းရှိရင်၊ padding က စာကြောင်းအားလုံးမှာ စကားလုံး ၂၀ လုံးစီ ရှိစေမှာပါ။ ကျွန်တော်တို့ရဲ့ ဥပမာမှာ၊ ထွက်လာတဲ့ tensor က ဒီလိုဖြစ်ပါတယ်။ + +```py no-format +padding_id = 100 + +batched_ids = [ + [200, 200, 200], + [200, 200, padding_id], +] +``` + +padding token ID ကို `tokenizer.pad_token_id` မှာ ရှာတွေ့နိုင်ပါတယ်။ ဒါကို အသုံးပြုပြီး ကျွန်တော်တို့ရဲ့ စာကြောင်းနှစ်ကြောင်းကို model ကနေတဆင့် တစ်ဦးချင်းစီနဲ့ batch အဖြစ် ပေါင်းပြီး ပို့ကြည့်ရအောင်။ + +```py no-format +model = AutoModelForSequenceClassification.from_pretrained(checkpoint) + +sequence1_ids = [[200, 200, 200]] +sequence2_ids = [[200, 200]] +batched_ids = [ + [200, 200, 200], + [200, 200, tokenizer.pad_token_id], +] + +print(model(torch.tensor(sequence1_ids)).logits) +print(model(torch.tensor(sequence2_ids)).logits) +print(model(torch.tensor(batched_ids)).logits) +``` + +```python out +tensor([[ 1.5694, -1.3895]], grad_fn=) +tensor([[ 0.5803, -0.4125]], grad_fn=) +tensor([[ 1.5694, -1.3895], + [ 1.3373, -1.2163]], grad_fn=) +``` + +ကျွန်တော်တို့ရဲ့ batched predictions တွေထဲက logits တွေမှာ တစ်ခုခု မှားယွင်းနေပါတယ်။ ဒုတိယ row က ဒုတိယစာကြောင်းအတွက် logits တွေနဲ့ တူညီသင့်ပေမယ့်၊ ကျွန်တော်တို့မှာ လုံးဝမတူညီတဲ့ တန်ဖိုးတွေ ရနေပါတယ်။ + +ဒါက Transformer model တွေရဲ့ အဓိကအင်္ဂါရပ်ဖြစ်တဲ့ attention layers တွေက token တစ်ခုစီကို *contextualize* လုပ်ပေးလို့ ဖြစ်ပါတယ်။ ၎င်းတို့သည် sequence တစ်ခု၏ tokens အားလုံးကို အာရုံစိုက်တာကြောင့် padding tokens တွေကို ထည့်သွင်းစဉ်းစားပါလိမ့်မယ်။ မတူညီတဲ့ အရှည်ရှိတဲ့ တစ်ဦးချင်းစီ စာကြောင်းတွေကို model ကနေတဆင့် ပို့တဲ့အခါ ဒါမှမဟုတ် တူညီတဲ့ စာကြောင်းတွေနဲ့ padding လုပ်ထားတဲ့ batch တစ်ခုကို ပို့တဲ့အခါ တူညီတဲ့ရလဒ် ရရှိဖို့အတွက်၊ အဲဒီ attention layers တွေကို padding tokens တွေကို လျစ်လျူရှုဖို့ ကျွန်တော်တို့ ပြောပြဖို့ လိုအပ်ပါတယ်။ ဒါကို attention mask ကို အသုံးပြုခြင်းဖြင့် လုပ်ဆောင်ပါတယ်။ + +## Attention Masks[[attention-masks]] + +*Attention masks* တွေက input IDs tensor နဲ့ အတိအကျတူညီတဲ့ shape ရှိတဲ့ tensors တွေဖြစ်ပြီး 0 တွေနဲ့ 1 တွေနဲ့ ဖြည့်ထားပါတယ်။ 1 တွေက သက်ဆိုင်ရာ tokens တွေကို အာရုံစိုက်သင့်တယ်လို့ ဖော်ပြပြီး၊ 0 တွေကတော့ သက်ဆိုင်ရာ tokens တွေကို အာရုံစိုက်ရန် မလိုအပ်ဘူး (ဆိုလိုသည်မှာ model ရဲ့ attention layers တွေက လျစ်လျူရှုသင့်တယ်) လို့ ဖော်ပြပါတယ်။ + +ယခင် ဥပမာကို attention mask တစ်ခုနဲ့ ဖြည့်စွက်ကြည့်ရအောင်။ + +```py no-format +batched_ids = [ + [200, 200, 200], + [200, 200, tokenizer.pad_token_id], +] + +attention_mask = [ + [1, 1, 1], + [1, 1, 0], +] + +outputs = model(torch.tensor(batched_ids), attention_mask=torch.tensor(attention_mask)) +print(outputs.logits) +``` + +```python out +tensor([[ 1.5694, -1.3895], + [ 0.5803, -0.4125]], grad_fn=) +``` + +အခု batch ထဲက ဒုတိယစာကြောင်းအတွက် logits တွေ အတူတူကို ရရှိပါပြီ။ + +ဒုတိယ sequence ရဲ့ နောက်ဆုံးတန်ဖိုးက padding ID တစ်ခုဖြစ်ပြီး၊ attention mask ထဲမှာတော့ 0 တန်ဖိုးဖြစ်နေတာကို သတိပြုပါ။ + +> [!TIP] +> ✏️ **စမ်းသပ်ကြည့်ပါ။** အပိုင်း ၂ မှာ အသုံးပြုခဲ့တဲ့ စာကြောင်းနှစ်ကြောင်း ("I've been waiting for a HuggingFace course my whole life." နဲ့ "I hate this so much!") ပေါ်မှာ tokenization ကို ကိုယ်တိုင် လုပ်ဆောင်ပါ။ ၎င်းတို့ကို model ကို ဖြတ်သန်းပြီး အပိုင်း ၂ မှာ ရရှိခဲ့တဲ့ logits တွေ အတူတူ ရရှိမရရှိ စစ်ဆေးပါ။ အခု ၎င်းတို့ကို padding token ကို အသုံးပြုပြီး batch လုပ်ပါ၊ ပြီးရင် မှန်ကန်တဲ့ attention mask ကို ဖန်တီးပါ။ model ကို ဖြတ်သန်းတဲ့အခါ တူညီတဲ့ရလဒ်တွေ ရရှိမရရှိ စစ်ဆေးပါ။ + +## ပိုမိုရှည်လျားသော Sequences များ[[longer-sequences]] + +Transformer model တွေနဲ့ဆိုရင် model တွေကို ပေးပို့နိုင်တဲ့ sequences တွေရဲ့ အရှည်မှာ ကန့်သတ်ချက်တစ်ခု ရှိပါတယ်။ model အများစုက tokens 512 သို့မဟုတ် 1024 အထိ sequences တွေကို ကိုင်တွယ်နိုင်ပြီး၊ ပိုရှည်တဲ့ sequences တွေကို လုပ်ဆောင်ဖို့ တောင်းဆိုတဲ့အခါ crash ဖြစ်ပါလိမ့်မယ်။ ဒီပြဿနာကို ဖြေရှင်းဖို့ နည်းလမ်းနှစ်ခု ရှိပါတယ်- + +- ပိုမိုရှည်လျားတဲ့ sequence length ကို ထောက်ပံ့ပေးတဲ့ model ကို အသုံးပြုပါ။ +- သင်ရဲ့ sequences တွေကို truncate လုပ်ပါ။ + +Model တွေမှာ မတူညီတဲ့ ထောက်ပံ့ပေးထားတဲ့ sequence lengths တွေရှိပြီး၊ အချို့က အလွန်ရှည်လျားတဲ့ sequences တွေကို ကိုင်တွယ်ရာမှာ အထူးပြုပါတယ်။ [Longformer](https://huggingface.co/docs/transformers/model_doc/longformer) က ဥပမာတစ်ခုဖြစ်ပြီး၊ [LED](https://huggingface.co/docs/transformers/model_doc/led) က နောက်ဥပမာတစ်ခု ဖြစ်ပါတယ်။ သင်ဟာ အလွန်ရှည်လျားတဲ့ sequences တွေလိုအပ်တဲ့ လုပ်ငန်းတစ်ခုပေါ်မှာ အလုပ်လုပ်နေတယ်ဆိုရင်၊ အဲဒီ model တွေကို လေ့လာကြည့်ဖို့ ကျွန်တော်တို့ အကြံပြုပါတယ်။ + +မဟုတ်ရင်တော့ သင်ရဲ့ sequences တွေကို `max_sequence_length` parameter ကို သတ်မှတ်ခြင်းဖြင့် truncate လုပ်ဖို့ ကျွန်တော်တို့ အကြံပြုပါတယ်။ + +```py +sequence = sequence[:max_sequence_length] +``` +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Inference**: လေ့ကျင့်ပြီးသား Artificial Intelligence (AI) မော်ဒယ်တစ်ခုကို အသုံးပြုပြီး input data ကနေ ခန့်မှန်းချက်တွေ ဒါမှမဟုတ် output တွေကို ထုတ်လုပ်တဲ့ လုပ်ငန်းစဉ်။ +* **Sequence**: စာသားတစ်ခု သို့မဟုတ် စကားလုံးများ၊ tokens များ၏ အစဉ်လိုက် စီစဉ်ထားသော အစုအဝေး။ +* **Vocabulary Indices**: စာသားကို encode လုပ်ပြီးနောက် ရရှိလာသော tokens တစ်ခုစီ၏ ထူးခြားသော ဂဏန်း ID များ။ +* **🤗 Transformers API**: Hugging Face Transformers library ကို အသုံးပြုရန်အတွက် ပရိုဂရမ်မာများက ခေါ်ဆိုနိုင်သော လုပ်ဆောင်ချက်များ၊ class များနှင့် methods များ။ +* **Batching**: မတူညီသော input များစွာကို တစ်ပြိုင်နက်တည်း လုပ်ဆောင်နိုင်ရန် အုပ်စုဖွဲ့ခြင်း။ +* **Tensor**: Machine Learning frameworks (PyTorch, TensorFlow) များတွင် ဒေတာများကို ကိုယ်စားပြုသော multi-dimensional array များ။ +* **`AutoTokenizer` Class**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး မော်ဒယ်အမည်ကို အသုံးပြုပြီး သက်ဆိုင်ရာ tokenizer ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **`AutoModelForSequenceClassification` Class**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး sequence classification အတွက် pre-trained model ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **`from_pretrained()` Method**: Pre-trained model သို့မဟုတ် tokenizer ကို load လုပ်ရန် အသုံးပြုသော method။ +* **`tokenize()` Method**: tokenizer ၏ text ကို tokens များအဖြစ် ပိုင်းခြားပေးသော method။ +* **`convert_tokens_to_ids()` Method**: tokens list ကို input IDs list အဖြစ် ပြောင်းလဲပေးသော tokenizer method။ +* **`input_ids`**: Tokenizer မှ ထုတ်ပေးသော tokens တစ်ခုစီ၏ ထူးခြားသော ဂဏန်းဆိုင်ရာ ID များ။ +* **`output.logits`**: မော်ဒယ်၏ နောက်ဆုံး layer မှ ထုတ်ပေးသော raw, unnormalized scores များ။ +* **Padding**: မတူညီသော အရှည်ရှိသည့် input sequence များကို အရှည်တူညီအောင် သတ်မှတ်ထားသော တန်ဖိုးများဖြင့် ဖြည့်စွက်ခြင်း။ +* **Padding Token**: Padding လုပ်ရာတွင် အသုံးပြုသော အထူး token (ဥပမာ - `[PAD]`)။ +* **`tokenizer.pad_token_id`**: tokenizer ၏ padding token ၏ ID။ +* **Attention Layers**: Transformer model ၏ အစိတ်အပိုင်းများဖြစ်ပြီး input sequence အတွင်းရှိ မတူညီသော tokens များ၏ အရေးပါမှုကို ဆုံးဖြတ်ရာတွင် အထောက်အကူပြုသည်။ +* **Contextualize**: စကားလုံးတစ်ခု၏ အဓိပ္ပာယ်ကို ၎င်းပါဝင်သော စာကြောင်း သို့မဟုတ် စာသား၏ အခြေအနေအရ နားလည်စေခြင်း။ +* **Attention Mask**: မော်ဒယ်ကို အာရုံစိုက်သင့်သည့် tokens များနှင့် လျစ်လျူရှုသင့်သည့် (padding) tokens များကို ခွဲခြားပေးသည့် binary mask။ +* **Truncate**: sequences ၏ အရှည်ကို ကန့်သတ်ချက်တစ်ခုအထိ ဖြတ်တောက်ခြင်း။ +* **`max_sequence_length` Parameter**: input sequence ၏ အများဆုံး အရှည်ကို သတ်မှတ်သော parameter။ +* **Longformer**: အလွန်ရှည်လျားသော sequences များကို ကိုင်တွယ်နိုင်ရန် ဒီဇိုင်းထုတ်ထားသော Transformer model တစ်မျိုး။ +* **LED (Longformer-Encoder-Decoder)**: Longformer ကို အခြေခံထားသော encoder-decoder Transformer model တစ်မျိုး။ \ No newline at end of file diff --git a/chapters/my/chapter2/6.mdx b/chapters/my/chapter2/6.mdx new file mode 100644 index 000000000..a24a3475a --- /dev/null +++ b/chapters/my/chapter2/6.mdx @@ -0,0 +1,160 @@ + + +# အားလုံးကို ပေါင်းစပ်ခြင်း[[putting-it-all-together]] + + + +နောက်ဆုံးအပိုင်းအချို့မှာတော့ ကျွန်တော်တို့ အလုပ်အများစုကို ကိုယ်တိုင်လုပ်ဆောင်ဖို့ အစွမ်းကုန် ကြိုးစားခဲ့ပါတယ်။ tokenizers တွေ ဘယ်လိုအလုပ်လုပ်တယ်ဆိုတာကို လေ့လာခဲ့ပြီး tokenization, input IDs အဖြစ် ပြောင်းလဲခြင်း၊ padding, truncation, နဲ့ attention masks တွေအကြောင်းကို လေ့လာခဲ့ပါတယ်။ + +သို့သော်လည်း၊ အပိုင်း ၂ မှာ ကျွန်တော်တို့ တွေ့ခဲ့ရသလိုပဲ၊ 🤗 Transformers API က ဒါတွေအားလုံးကို ကျွန်တော်တို့အတွက် အဆင့်မြင့် function တစ်ခုနဲ့ ကိုင်တွယ်ပေးနိုင်ပြီး၊ အဲဒါကို ဒီနေရာမှာ ကျွန်တော်တို့ နက်ရှိုင်းစွာ လေ့လာပါမယ်။ သင်ရဲ့ `tokenizer` ကို စာကြောင်းပေါ်မှာ တိုက်ရိုက် ခေါ်ဆိုတဲ့အခါ၊ သင်ရဲ့ model ကို ဖြတ်သန်းဖို့ အဆင်သင့်ဖြစ်နေတဲ့ inputs တွေကို ပြန်ရပါလိမ့်မယ်။ + +```py +from transformers import AutoTokenizer + +checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" +tokenizer = AutoTokenizer.from_pretrained(checkpoint) + +sequence = "I've been waiting for a HuggingFace course my whole life." + +model_inputs = tokenizer(sequence) +``` + +ဒီနေရာမှာ `model_inputs` variable မှာ model တစ်ခု ကောင်းကောင်း အလုပ်လုပ်နိုင်ဖို့ လိုအပ်တဲ့ အရာအားလုံး ပါဝင်ပါတယ်။ DistilBERT အတွက်ဆိုရင်၊ အဲဒါက input IDs တွေအပြင် attention mask ပါ ပါဝင်ပါတယ်။ အပို inputs တွေကို လက်ခံတဲ့ တခြား model တွေအတွက်လည်း `tokenizer` object က အဲဒါတွေကို output အဖြစ် ထုတ်ပေးပါလိမ့်မယ်။ + +အောက်ပါ ဥပမာအချို့မှာ ကျွန်တော်တို့ မြင်ရမယ့်အတိုင်း၊ ဒီ method က အလွန်အစွမ်းထက်ပါတယ်။ ပထမဆုံးအနေနဲ့၊ ဒါက single sequence တစ်ခုကို tokenize လုပ်ဆောင်နိုင်ပါတယ်။ + +```py +sequence = "I've been waiting for a HuggingFace course my whole life." + +model_inputs = tokenizer(sequence) +``` + +ဒါက API မှာ ဘာမှမပြောင်းလဲဘဲ sequence များစွာကို တစ်ပြိုင်နက်တည်း ကိုင်တွယ်နိုင်ပါတယ်။ + +```py +sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] + +model_inputs = tokenizer(sequences) +``` + +ဒါက ရည်ရွယ်ချက်အမျိုးမျိုးအရ pad လုပ်နိုင်ပါတယ်။ + +```py +# Sequences တွေကို အရှည်ဆုံး sequence length အထိ pad လုပ်ပါလိမ့်မယ်။ +model_inputs = tokenizer(sequences, padding="longest") + +# Sequences တွေကို model ရဲ့ max length (BERT ဒါမှမဟုတ် DistilBERT အတွက် 512) အထိ pad လုပ်ပါလိမ့်မယ်။ +model_inputs = tokenizer(sequences, padding="max_length") + +# Sequences တွေကို သတ်မှတ်ထားတဲ့ max length အထိ pad လုပ်ပါလိမ့်မယ်။ +model_inputs = tokenizer(sequences, padding="max_length", max_length=8) +``` + +ဒါက sequences တွေကို truncate လည်း လုပ်နိုင်ပါတယ်။ + +```py +sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] + +# Model ရဲ့ max length (BERT ဒါမှမဟုတ် DistilBERT အတွက် 512) ထက် ပိုရှည်တဲ့ sequences တွေကို truncate လုပ်ပါလိမ့်မယ်။ +model_inputs = tokenizer(sequences, truncation=True) + +# သတ်မှတ်ထားတဲ့ max length ထက် ပိုရှည်တဲ့ sequences တွေကို truncate လုပ်ပါလိမ့်မယ်။ +model_inputs = tokenizer(sequences, max_length=8, truncation=True) +``` + +`tokenizer` object က သီးခြား framework tensors တွေအဖြစ် ပြောင်းလဲခြင်းကို ကိုင်တွယ်နိုင်ပါတယ်။ ၎င်းတို့ကို model ကို တိုက်ရိုက် ပို့နိုင်ပါတယ်။ ဥပမာအားဖြင့်၊ အောက်ပါ code sample မှာ ကျွန်တော်တို့က tokenizer ကို မတူညီတဲ့ frameworks တွေကနေ tensors တွေကို ပြန်ပေးဖို့ တောင်းဆိုနေတာပါ။ `"pt"` က PyTorch tensors တွေကို ပြန်ပေးပြီး `"np"` က NumPy arrays တွေကို ပြန်ပေးပါတယ်။ + +```py +sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] + +# PyTorch tensors များကို ပြန်ပေးသည်။ +model_inputs = tokenizer(sequences, padding=True, return_tensors="pt") + +# NumPy arrays များကို ပြန်ပေးသည်။ +model_inputs = tokenizer(sequences, padding=True, return_tensors="np") +``` + +## Special Tokens များ[[special-tokens]] + +tokenizer က ပြန်ပေးတဲ့ input IDs တွေကို ကြည့်လိုက်ရင်၊ အစောပိုင်းက ကျွန်တော်တို့ ရရှိခဲ့တာတွေနဲ့ အနည်းငယ် ကွဲပြားနေတာကို တွေ့ရပါလိမ့်မယ်။ + +```py +sequence = "I've been waiting for a HuggingFace course my whole life." + +model_inputs = tokenizer(sequence) +print(model_inputs["input_ids"]) + +tokens = tokenizer.tokenize(sequence) +ids = tokenizer.convert_tokens_to_ids(tokens) +print(ids) +``` + +```python out +[101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012, 102] +[1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012] +``` + +token ID တစ်ခုကို အစမှာ ထည့်သွင်းထားပြီး၊ တစ်ခုကို အဆုံးမှာ ထည့်သွင်းထားပါတယ်။ ဒါက ဘာအကြောင်းလဲဆိုတာ သိဖို့ အထက်ပါ IDs sequence နှစ်ခုကို decode လုပ်ကြည့်ရအောင်။ + +```py +print(tokenizer.decode(model_inputs["input_ids"])) +print(tokenizer.decode(ids)) +``` + +```python out +"[CLS] i've been waiting for a huggingface course my whole life. [SEP]" +"i've been waiting for a huggingface course my whole life." +``` + +tokenizer က အစမှာ `[CLS]` ဆိုတဲ့ special word ကို ထည့်ထားပြီး၊ အဆုံးမှာ `[SEP]` ဆိုတဲ့ special word ကို ထည့်ထားပါတယ်။ ဒါက model ကို အဲဒီ tokens တွေနဲ့ pretrained လုပ်ထားတာကြောင့် ဖြစ်ပြီး၊ inference အတွက် တူညီတဲ့ ရလဒ်တွေ ရရှိဖို့အတွက် ကျွန်တော်တို့လည်း ဒါတွေကို ထည့်ဖို့ လိုအပ်ပါတယ်။ တချို့ model တွေက special words တွေ မထည့်တာ ဒါမှမဟုတ် မတူညီတဲ့ special words တွေ ထည့်တာမျိုး ရှိနိုင်ပါတယ်။ model တွေက special words တွေကို အစမှာပဲ ဒါမှမဟုတ် အဆုံးမှာပဲ ထည့်တာမျိုးလည်း ရှိနိုင်ပါတယ်။ ဘယ်လိုပဲဖြစ်ဖြစ်၊ tokenizer က ဘယ် special tokens တွေ လိုအပ်တယ်ဆိုတာ သိပြီး သင့်အတွက် ဒါတွေကို ကိုင်တွယ်ပေးပါလိမ့်မယ်။ + +## အနှစ်ချုပ်: Tokenizer မှ Model ဆီသို့[[wrapping-up-from-tokenizer-to-model]] + +`tokenizer` object က text တွေပေါ်မှာ အသုံးပြုတဲ့အခါ တစ်ဦးချင်းစီ အဆင့်တွေကို အားလုံး မြင်ပြီးသွားပြီဆိုတော့၊ ဒါက sequences များစွာကို (padding!)၊ အလွန်ရှည်လျားတဲ့ sequences တွေကို (truncation!) နဲ့ မတူညီတဲ့ tensors အမျိုးအစားများစွာကို သူ့ရဲ့ အဓိက API နဲ့ ဘယ်လိုကိုင်တွယ်လဲဆိုတာကို နောက်ဆုံးတစ်ကြိမ် ကြည့်ရအောင်။ + +```py +import torch +from transformers import AutoTokenizer, AutoModelForSequenceClassification + +checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" +tokenizer = AutoTokenizer.from_pretrained(checkpoint) +model = AutoModelForSequenceClassification.from_pretrained(checkpoint) +sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] + +tokens = tokenizer(sequences, padding=True, truncation=True, return_tensors="pt") +output = model(**tokens) +``` + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Tokenizer**: စာသား (သို့မဟုတ် အခြားဒေတာ) ကို AI မော်ဒယ်များ စီမံဆောင်ရွက်နိုင်ရန် tokens တွေအဖြစ် ပိုင်းခြားပေးသည့် ကိရိယာ သို့မဟုတ် လုပ်ငန်းစဉ်။ +* **Tokenization**: စာသားကို tokens များအဖြစ် ပိုင်းခြားသော လုပ်ငန်းစဉ်။ +* **Input IDs**: Tokenizer မှ ထုတ်ပေးသော tokens တစ်ခုစီ၏ ထူးခြားသော ဂဏန်းဆိုင်ရာ ID များ။ +* **Padding**: မတူညီသော အရှည်ရှိသည့် input sequence များကို အရှည်တူညီအောင် သတ်မှတ်ထားသော တန်ဖိုးများဖြင့် ဖြည့်စွက်ခြင်း။ +* **Truncation**: အရှည်ကန့်သတ်ချက်ထက် ပိုနေသော input sequence များကို ဖြတ်တောက်ခြင်း။ +* **Attention Mask**: မော်ဒယ်ကို အာရုံစိုက်သင့်သည့် tokens များနှင့် လျစ်လျူရှုသင့်သည့် (padding) tokens များကို ခွဲခြားပေးသည့် binary mask။ +* **🤗 Transformers API**: Hugging Face Transformers library ကို အသုံးပြုရန်အတွက် ပရိုဂရမ်မာများက ခေါ်ဆိုနိုင်သော လုပ်ဆောင်ချက်များ၊ class များ နှင့် methods များ။ +* **`AutoTokenizer` Class**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး မော်ဒယ်အမည်ကို အသုံးပြုပြီး သက်ဆိုင်ရာ tokenizer ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **`from_pretrained()` Method**: Pre-trained model သို့မဟုတ် tokenizer ကို load လုပ်ရန် အသုံးပြုသော method။ +* **`distilbert-base-uncased-finetuned-sst-2-english`**: `sentiment-analysis` pipeline ၏ default checkpoint အဖြစ် အသုံးပြုသော DistilBERT မော်ဒယ်၏ အမည်။ `base` သည် မော်ဒယ်၏ အရွယ်အစားကို ဖော်ပြပြီး `uncased` သည် စာလုံးအကြီးအသေး ခွဲခြားခြင်းမရှိဘဲ လေ့ကျင့်ထားကြောင်း ဖော်ပြသည်။ `finetuned-sst-2-english` က SST-2 dataset တွင် English ဘာသာစကားအတွက် fine-tune လုပ်ထားသည်ကို ဆိုလိုသည်။ +* **`model_inputs` Variable**: tokenizer ကနေ ထွက်လာတဲ့ model ရဲ့ inputs တွေအားလုံးကို သိမ်းဆည်းထားတဲ့ variable။ +* **PyTorch Tensors**: PyTorch deep learning framework မှာ ဒေတာတွေကို ကိုယ်စားပြုသော multi-dimensional array များ။ +* **NumPy Arrays**: Python တွင် ဂဏန်းတွက်ချက်မှုများအတွက် အသုံးပြုသော multi-dimensional array များအတွက် library။ +* **`padding="longest"`**: Batch အတွင်းရှိ အရှည်ဆုံး sequence အထိ pad လုပ်ခြင်း။ +* **`padding="max_length"`**: Model ၏ အများဆုံး length အထိ pad လုပ်ခြင်း။ +* **`max_length`**: Padding သို့မဟုတ် truncation အတွက် သတ်မှတ်ထားသော အရှည် ကန့်သတ်ချက်။ +* **`truncation=True`**: Sequences များကို သတ်မှတ်ထားသော length အထိ ဖြတ်တောက်ခြင်း။ +* **`return_tensors="pt"`**: PyTorch tensors များကို ပြန်ပေးရန် tokenizer ကို ညွှန်ကြားခြင်း။ +* **`return_tensors="np"`**: NumPy arrays များကို ပြန်ပေးရန် tokenizer ကို ညွှန်ကြားခြင်း။ +* **Special Tokens**: Transformer model များက စာကြောင်းနယ်နိမိတ်များ သို့မဟုတ် အခြားအချက်အလက်များကို ကိုယ်စားပြုရန် အသုံးပြုသော အထူး tokens များ (ဥပမာ - `[CLS]`, `[SEP]`, `[PAD]`)။ +* **`[CLS]`**: BERT မော်ဒယ်တွင် classification task အတွက် အသုံးပြုသော special token (စာကြောင်း၏ အစတွင် ပေါ်လာသည်)။ +* **`[SEP]`**: BERT မော်ဒယ်တွင် စာကြောင်းများကြား ပိုင်းခြားရန် အသုံးပြုသော special token။ +* **`tokenizer.decode()` Method**: Token IDs များကို မူရင်းစာသားသို့ ပြန်ပြောင်းလဲပေးသော method။ +* **Inference**: လေ့ကျင့်ပြီးသား Artificial Intelligence (AI) မော်ဒယ်တစ်ခုကို အသုံးပြုပြီး input data ကနေ ခန့်မှန်းချက်တွေ ဒါမှမဟုတ် output တွေကို ထုတ်လုပ်တဲ့ လုပ်ငန်းစဉ်။ +* **`AutoModelForSequenceClassification` Class**: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး sequence classification အတွက် pre-trained model ကို အလိုအလျောက် load လုပ်ပေးသည်။ +* **`model(**tokens)`**: tokenizer ကနေ ထုတ်ပေးတဲ့ dictionary ကို model ရဲ့ input အဖြစ် ထည့်သွင်းပေးခြင်း။ \ No newline at end of file diff --git a/chapters/my/chapter2/7.mdx b/chapters/my/chapter2/7.mdx new file mode 100644 index 000000000..a2543dcec --- /dev/null +++ b/chapters/my/chapter2/7.mdx @@ -0,0 +1,32 @@ +# အခြေခံ အသုံးပြုမှု ပြီးဆုံးပါပြီ![[basic-usage-completed]] + + + +ဒီသင်တန်းကို ဒီအထိ လိုက်ပါခဲ့တဲ့အတွက် ဂုဏ်ယူပါတယ်။ အနှစ်ချုပ်အနေနဲ့၊ ဒီအခန်းမှာ သင်ဟာ အောက်ပါတို့ကို သင်ယူခဲ့ပါပြီ - + +- Transformer model တစ်ခု၏ အခြေခံတည်ဆောက်ပုံများကို သင်ယူခဲ့သည်။ +- tokenization pipeline ကို ဘာတွေနဲ့ ဖွဲ့စည်းထားတယ်ဆိုတာ သိရှိခဲ့သည်။ +- လက်တွေ့မှာ Transformer model တစ်ခုကို ဘယ်လိုအသုံးပြုရမယ်ဆိုတာကို တွေ့ခဲ့ရသည်။ +- tokenizer ကို အသုံးပြုပြီး text ကို model က နားလည်နိုင်တဲ့ tensors တွေအဖြစ် ဘယ်လိုပြောင်းလဲရမယ်ဆိုတာကို သင်ယူခဲ့သည်။ +- text ကနေ predictions တွေရဖို့ tokenizer နဲ့ model ကို အတူတကွ တည်ဆောက်ခဲ့သည်။ +- input IDs တွေရဲ့ ကန့်သတ်ချက်များကို သင်ယူခဲ့ပြီး attention masks တွေအကြောင်း သိရှိခဲ့သည်။ +- အသုံးဝင်ပြီး စိတ်ကြိုက်ပြင်ဆင်နိုင်သော tokenizer methods များနှင့် ကစားကြည့်ခဲ့သည်။ + +အခုကစပြီး သင်ဟာ 🤗 Transformers docs တွေထဲမှာ လွတ်လပ်စွာ သွားလာနိုင်ပါလိမ့်မယ်၊ Vocabulary တွေက ရင်းနှီးလာမှာဖြစ်ပြီး၊ သင် အချိန်အများစု အသုံးပြုရမယ့် methods တွေကိုလည်း သင် မြင်တွေ့ခဲ့ရပါပြီ။ + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Transformer Model**: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။ ၎င်းတို့ဟာ စာသားတွေထဲက စကားလုံးတွေရဲ့ ဆက်နွယ်မှုတွေကို "attention mechanism" သုံးပြီး နားလည်အောင် သင်ကြားပေးပါတယ်။ +* **Tokenization Pipeline**: စာသားကို AI မော်ဒယ်များ လုပ်ဆောင်နိုင်သော ဂဏန်းဆိုင်ရာ ကိုယ်စားပြုမှုအဖြစ် ပြောင်းလဲရန် လိုအပ်သော အဆင့်များ (ဥပမာ- tokenization, input IDs conversion, padding, truncation)။ +* **Tokenizer**: စာသား (သို့မဟုတ် အခြားဒေတာ) ကို AI မော်ဒယ်များ စီမံဆောင်ရွက်နိုင်ရန် tokens တွေအဖြစ် ပိုင်းခြားပေးသည့် ကိရိယာ သို့မဟုတ် လုပ်ငန်းစဉ်။ +* **Text**: လူသားဘာသာစကားဖြင့် ရေးသားထားသော စာသားအချက်အလက်များ။ +* **Tensors**: Machine Learning frameworks (PyTorch, TensorFlow) များတွင် ဒေတာများကို ကိုယ်စားပြုသော multi-dimensional array များ။ +* **Predictions**: Machine Learning မော်ဒယ်တစ်ခုက input data ကို အခြေခံပြီး ခန့်မှန်းထုတ်ပေးသော ရလဒ်များ။ +* **Input IDs**: Tokenizer မှ ထုတ်ပေးသော tokens တစ်ခုစီ၏ ထူးခြားသော ဂဏန်းဆိုင်ရာ ID များ။ +* **Attention Masks**: မော်ဒယ်ကို အာရုံစိုက်သင့်သည့် tokens များနှင့် လျစ်လျူရှုသင့်သည့် (padding) tokens များကို ခွဲခြားပေးသည့် binary mask။ +* **Configurable Tokenizer Methods**: အသုံးပြုသူ၏ လိုအပ်ချက်များအတိုင်း ပြင်ဆင်သတ်မှတ်နိုင်သော tokenizer functions များ။ +* **🤗 Transformers Docs**: Hugging Face Transformers library ၏ တရားဝင် မှတ်တမ်းများ (documentation)။ +* **Vocabulary**: tokenizer သို့မဟုတ် model တစ်ခုက သိရှိနားလည်ပြီး ကိုင်တွယ်နိုင်သော ထူးခြားသည့် tokens များ စုစုပေါင်း။ \ No newline at end of file diff --git a/chapters/my/chapter2/8.mdx b/chapters/my/chapter2/8.mdx new file mode 100644 index 000000000..74e01168c --- /dev/null +++ b/chapters/my/chapter2/8.mdx @@ -0,0 +1,930 @@ +# Optimization လုပ်ထားသော Inference Deployment[[optimized-inference-deployment]] + +ဒီအပိုင်းမှာတော့ LLM deployments တွေကို optimization လုပ်ဖို့အတွက် အဆင့်မြင့် frameworks တွေဖြစ်တဲ့ Text Generation Inference (TGI), vLLM, နဲ့ llama.cpp တို့ကို လေ့လာသွားပါမယ်။ ဒီ application တွေက အဓိကအားဖြင့် ထုတ်လုပ်မှု ပတ်ဝန်းကျင် (production environments) တွေမှာ LLM တွေကို သုံးစွဲသူများဆီသို့ ဝန်ဆောင်မှုပေးဖို့ အသုံးပြုကြပါတယ်။ ဒီအပိုင်းက ဒီ frameworks တွေကို production မှာ ဘယ်လို deploy လုပ်ရမယ်ဆိုတာကို အဓိကထားပြီး၊ single machine တစ်ခုပေါ်မှာ inference အတွက် ဘယ်လိုအသုံးပြုရမယ်ဆိုတာကို အာရုံစိုက်ထားခြင်း မရှိပါဘူး။ + +ဒီ tools တွေက inference efficiency ကို ဘယ်လိုအမြင့်ဆုံးမြှင့်တင်ပြီး Large Language Models တွေကို production deployments တွေကို ဘယ်လို ရိုးရှင်းအောင် လုပ်ဆောင်တယ်ဆိုတာကို ကျွန်တော်တို့ ဖော်ပြပေးပါမယ်။ + +## Framework ရွေးချယ်မှု လမ်းညွှန်[[framework-selection-guide]] + +TGI, vLLM, နဲ့ llama.cpp တို့ဟာ ရည်ရွယ်ချက်ချင်း တူညီပေမယ့်၊ မတူညီတဲ့ အသုံးပြုမှုပုံစံတွေအတွက် ပိုမိုသင့်လျော်စေတဲ့ ထူးခြားတဲ့ အင်္ဂါရပ်တွေ ရှိပါတယ်။ ၎င်းတို့ကြားက အဓိက ကွာခြားချက်တွေကို စွမ်းဆောင်ရည် (performance) နဲ့ ပေါင်းစပ်မှု (integration) ကို အာရုံစိုက်ပြီး ကြည့်ရအောင်။ + +### Memory Management နှင့် Performance[[memory-management-and-performance]] + +**TGI** ကို production မှာ တည်ငြိမ်ပြီး ခန့်မှန်းနိုင်စေဖို့ ဒီဇိုင်းထုတ်ထားပြီး၊ memory အသုံးပြုမှုကို တသမတ်တည်း ထိန်းထားဖို့အတွက် fixed sequence lengths တွေကို အသုံးပြုပါတယ်။ TGI က Flash Attention 2 နဲ့ continuous batching နည်းစနစ်တွေကို အသုံးပြုပြီး memory ကို စီမံခန့်ခွဲပါတယ်။ ဒါက ၎င်းသည် attention calculations တွေကို အလွန်ထိထိရောက်ရောက် လုပ်ဆောင်နိုင်ပြီး GPU ကို အလုပ်တွေ အဆက်မပြတ် ပေးခြင်းဖြင့် အလုပ်များနေအောင် ထိန်းထားနိုင်တယ်လို့ ဆိုလိုပါတယ်။ လိုအပ်တဲ့အခါ စနစ်က model ရဲ့ အစိတ်အပိုင်းတွေကို CPU နဲ့ GPU ကြား ရွှေ့ပြောင်းနိုင်တာကြောင့် ပိုကြီးတဲ့ model တွေကို ကိုင်တွယ်ရာမှာ အထောက်အကူ ဖြစ်စေပါတယ်။ + +Flash Attention + + + +Flash Attention ဆိုတာ transformer models တွေမှာ attention mechanism ကို memory bandwidth bottlenecks တွေကို ဖြေရှင်းပေးခြင်းဖြင့် optimization လုပ်တဲ့ နည်းပညာတစ်ခုပါ။ [Chapter 1.8](/course/chapter1/8) မှာ ယခင်က ဆွေးနွေးခဲ့သလိုပဲ၊ attention mechanism မှာ quadratic complexity နဲ့ memory usage ရှိတာကြောင့် ရှည်လျားတဲ့ sequences တွေအတွက် ထိရောက်မှု မရှိပါဘူး။ + +အဓိက တီထွင်မှုကတော့ High Bandwidth Memory (HBM) နဲ့ ပိုမြန်တဲ့ SRAM cache ကြား memory transfers တွေကို ဘယ်လို စီမံခန့်ခွဲလဲဆိုတဲ့ အချက်မှာပါပဲ။ ရိုးရာ attention နည်းလမ်းက HBM နဲ့ SRAM ကြား ဒေတာတွေကို အကြိမ်ကြိမ် transfer လုပ်တာကြောင့် GPU ကို အလုပ်မရှိဘဲ ထားခြင်းဖြင့် bottlenecks တွေ ဖြစ်စေပါတယ်။ Flash Attention က ဒေတာတွေကို SRAM ထဲကို တစ်ကြိမ်တည်း load လုပ်ပြီး အဲဒီမှာပဲ calculations တွေအားလုံးကို လုပ်ဆောင်တာကြောင့် ကုန်ကျစရိတ်များတဲ့ memory transfers တွေကို လျှော့ချပေးပါတယ်။ + +အကျိုးကျေးဇူးတွေက training လုပ်နေစဉ်မှာ အရေးအပါဆုံး ဖြစ်ပေမယ့်၊ Flash Attention ရဲ့ လျှော့ချထားတဲ့ VRAM အသုံးပြုမှုနဲ့ တိုးတက်လာတဲ့ efficiency က inference အတွက်ပါ အဖိုးတန်စေပြီး၊ ပိုမိုမြန်ဆန်ပြီး ပိုမို scalable ဖြစ်တဲ့ LLM serving ကို ဖြစ်ပေါ်စေပါတယ်။ + + + +**vLLM** က PagedAttention ကို အသုံးပြုပြီး မတူညီတဲ့ နည်းလမ်းတစ်ခုကို အသုံးပြုပါတယ်။ ကွန်ပျူတာက memory ကို pages တွေနဲ့ စီမံခန့်ခွဲသလိုပဲ၊ vLLM က model ရဲ့ memory ကို ပိုသေးငယ်တဲ့ blocks တွေအဖြစ် ပိုင်းခြားပါတယ်။ ဒီ clever system ကြောင့် ၎င်းသည် မတူညီတဲ့ အရွယ်အစားရှိတဲ့ requests တွေကို ပိုမိုပြောင်းလွယ်ပြင်လွယ် ကိုင်တွယ်နိုင်ပြီး memory space ကို မဖြုန်းတီးပါဘူး။ ဒါက မတူညီတဲ့ requests တွေကြား memory ကို မျှဝေရာမှာ အထူးကောင်းမွန်ပြီး memory fragmentation ကို လျှော့ချပေးတာကြောင့် စနစ်တစ်ခုလုံးကို ပိုမိုထိရောက်စေပါတယ်။ + + + +PagedAttention ဆိုတာ LLM inference မှာ နောက်ထပ် အရေးကြီးတဲ့ bottleneck တစ်ခုဖြစ်တဲ့ KV cache memory management ကို ဖြေရှင်းပေးတဲ့ နည်းပညာတစ်ခုပါ။ [Chapter 1.8](/course/chapter1/8) မှာ ဆွေးနွေးခဲ့သလိုပဲ၊ text generation လုပ်နေစဉ်မှာ model က attention keys နဲ့ values (KV cache) တွေကို ထုတ်လုပ်လိုက်တဲ့ token တစ်ခုစီအတွက် သိမ်းဆည်းထားပြီး ထပ်ခါတလဲလဲ တွက်ချက်မှုတွေကို လျှော့ချပါတယ်။ KV cache က အထူးသဖြင့် ရှည်လျားတဲ့ sequences တွေ ဒါမှမဟုတ် concurrent requests များစွာနဲ့ဆိုရင် အလွန်ကြီးမားလာနိုင်ပါတယ်။ + +vLLM ရဲ့ အဓိက တီထွင်မှုကတော့ ဒီ cache ကို ဘယ်လို စီမံခန့်ခွဲလဲဆိုတဲ့ အချက်မှာပါပဲ- + +1. **Memory Paging**: KV cache ကို ကြီးမားတဲ့ block တစ်ခုအဖြစ် မမှတ်ယူဘဲ၊ ၎င်းကို fixed-size "pages" တွေအဖြစ် ပိုင်းခြားထားပါတယ် (operating systems တွေမှာ virtual memory နဲ့ ဆင်တူပါတယ်)။ +2. **Non-contiguous Storage**: Pages တွေကို GPU memory မှာ ဆက်တိုက် သိမ်းဆည်းထားဖို့ မလိုအပ်တာကြောင့် ပိုမိုပြောင်းလွယ်ပြင်လွယ်ရှိတဲ့ memory allocation ကို ဖြစ်ပေါ်စေပါတယ်။ +3. **Page Table Management**: Page table တစ်ခုက ဘယ် pages တွေက ဘယ် sequence နဲ့ သက်ဆိုင်တယ်ဆိုတာကို ခြေရာခံပြီး၊ ထိရောက်တဲ့ lookup နဲ့ access ကို ဖြစ်ပေါ်စေပါတယ်။ +4. **Memory Sharing**: parallel sampling လို လုပ်ငန်းတွေအတွက်၊ prompt အတွက် KV cache ကို သိမ်းဆည်းထားတဲ့ pages တွေကို sequences များစွာမှာ မျှဝေအသုံးပြုနိုင်ပါတယ်။ + +PagedAttention နည်းလမ်းက ရိုးရာနည်းလမ်းတွေနဲ့ နှိုင်းယှဉ်ရင် throughput ကို ၂၄ ဆအထိ ပိုမိုမြင့်မားစေနိုင်တာကြောင့် production LLM deployments တွေအတွက် game-changer တစ်ခု ဖြစ်ပါတယ်။ PagedAttention ဘယ်လိုအလုပ်လုပ်တယ်ဆိုတာကို တကယ်နက်နက်နဲနဲ လေ့လာချင်တယ်ဆိုရင် [vLLM documentation ရဲ့ လမ်းညွှန်](https://docs.vllm.ai/en/latest/design/kernel/paged_attention.html) ကို ဖတ်ရှုနိုင်ပါတယ်။ + + + +**llama.cpp** ဟာ မူလက LLaMA models တွေကို consumer hardware တွေမှာ run ဖို့ ဒီဇိုင်းထုတ်ထားတဲ့ highly optimized C/C++ implementation တစ်ခုပါ။ ဒါက optional GPU acceleration ပါဝင်တဲ့ CPU efficiency ကို အာရုံစိုက်ပြီး၊ resource-constrained environments တွေအတွက် အကောင်းဆုံးပါပဲ။ llama.cpp က model size နဲ့ memory requirements တွေကို လျှော့ချဖို့အတွက် quantization နည်းစနစ်တွေကို အသုံးပြုပြီး ကောင်းမွန်တဲ့ performance ကို ထိန်းသိမ်းထားပါတယ်။ ဒါက အမျိုးမျိုးသော CPU architectures တွေအတွက် optimized kernels တွေကို implement လုပ်ထားပြီး၊ ထိရောက်တဲ့ token generation အတွက် basic KV cache management ကို ထောက်ပံ့ပေးပါတယ်။ + + + +llama.cpp မှာ Quantization ဆိုတာ model weights တွေရဲ့ precision ကို 32-bit ဒါမှမဟုတ် 16-bit floating point ကနေ 8-bit integers (INT8)၊ 4-bit ဒါမှမဟုတ် ပိုနိမ့်တဲ့ precision formats တွေအဖြစ် လျှော့ချတာပါ။ ဒါက memory အသုံးပြုမှုကို သိသိသာသာ လျှော့ချပေးပြီး အနည်းဆုံး အရည်အသွေး ဆုံးရှုံးမှုနဲ့အတူ inference speed ကို မြှင့်တင်ပေးပါတယ်။ + +llama.cpp မှာ အဓိက quantization features တွေကတော့- +1. **Multiple Quantization Levels**: 8-bit, 4-bit, 3-bit, နဲ့ 2-bit quantization ကိုပါ ထောက်ပံ့ပေးပါတယ်။ +2. **GGML/GGUF Format**: Quantized inference အတွက် optimization လုပ်ထားတဲ့ custom tensor formats တွေကို အသုံးပြုပါတယ်။ +3. **Mixed Precision**: Model ရဲ့ မတူညီတဲ့ အစိတ်အပိုင်းတွေမှာ မတူညီတဲ့ quantization levels တွေကို အသုံးပြုနိုင်ပါတယ်။ +4. **Hardware-Specific Optimizations**: အမျိုးမျိုးသော CPU architectures တွေ (AVX2, AVX-512, NEON) အတွက် optimized code paths တွေ ပါဝင်ပါတယ်။ + +ဒီနည်းလမ်းက limited memory ရှိတဲ့ consumer hardware တွေမှာ billion-parameter models တွေကို run နိုင်စေပြီး၊ local deployments နဲ့ edge devices တွေအတွက် အကောင်းဆုံး ဖြစ်စေပါတယ်။ + + + +### Deployment နှင့် Integration[[deployment-and-integration]] + +frameworks တွေကြားက deployment နဲ့ integration ကွာခြားချက်တွေကို ဆက်သွားရအောင်။ + +**TGI** က သူ့ရဲ့ production-ready features တွေနဲ့ enterprise-level deployment တွေမှာ ထူးချွန်ပါတယ်။ ဒါက built-in Kubernetes support နဲ့ Prometheus နဲ့ Grafana ကနေတဆင့် monitoring လုပ်ခြင်း၊ automatic scaling, နဲ့ ပြည့်စုံတဲ့ safety features တွေလို production မှာ run ဖို့ လိုအပ်တဲ့ အရာအားလုံး ပါဝင်ပါတယ်။ စနစ်က enterprise-grade logging နဲ့ content filtering နဲ့ rate limiting လိုမျိုး အမျိုးမျိုးသော ကာကွယ်မှု နည်းလမ်းတွေပါ ပါဝင်တာကြောင့် သင့် deployment ကို လုံခြုံပြီး တည်ငြိမ်အောင် ထိန်းထားနိုင်ပါတယ်။ + +**vLLM** က deployment အတွက် ပိုမိုပြောင်းလွယ်ပြင်လွယ်ရှိပြီး developer-friendly ဖြစ်တဲ့ ချဉ်းကပ်မှုကို အသုံးပြုပါတယ်။ ဒါက Python ကို အဓိကထားပြီး တည်ဆောက်ထားတာကြောင့် သင့်ရဲ့ လက်ရှိ application တွေမှာ OpenAI ရဲ့ API ကို အလွယ်တကူ အစားထိုးနိုင်ပါတယ်။ framework က raw performance ကို ပေးစွမ်းဖို့ အာရုံစိုက်ပြီး၊ သင့်ရဲ့ သီးခြားလိုအပ်ချက်တွေနဲ့ ကိုက်ညီအောင် စိတ်ကြိုက်ပြင်ဆင်နိုင်ပါတယ်။ ဒါက clusters တွေကို စီမံခန့်ခွဲဖို့အတွက် Ray နဲ့ အထူးကောင်းမွန်စွာ အလုပ်လုပ်တာကြောင့် high performance နဲ့ adaptability လိုအပ်တဲ့အခါ အကောင်းဆုံး ရွေးချယ်မှုတစ်ခု ဖြစ်ပါတယ်။ + +**llama.cpp** က ရိုးရှင်းမှုနဲ့ portability ကို ဦးစားပေးပါတယ်။ သူ့ရဲ့ server implementation က ပေါ့ပါးပြီး hardware အမျိုးမျိုး (powerful servers တွေကနေ consumer laptops တွေနဲ့ အချို့ high-end mobile devices တွေအထိ) မှာ run နိုင်ပါတယ်။ အနည်းဆုံး dependencies တွေနဲ့ ရိုးရှင်းတဲ့ C/C++ core နဲ့ဆိုရင်၊ Python frameworks တွေ install လုပ်ဖို့ ခက်ခဲတဲ့ environments တွေမှာ deploy လုပ်ဖို့ လွယ်ကူပါတယ်။ server က OpenAI-compatible API ကို ပံ့ပိုးပေးထားပြီး အခြား solution တွေထက် resource အသုံးပြုမှုက အများကြီး သေးငယ်ပါတယ်။ + +## စတင်ခြင်း[[getting-started]] + +LLMs တွေကို deploy လုပ်ဖို့ ဒီ frameworks တွေကို ဘယ်လိုအသုံးပြုရမလဲဆိုတာကို လေ့လာကြည့်ရအောင်။ installation နဲ့ basic setup ကနေ စတင်ပါမယ်။ + +### Installation နှင့် Basic Setup[[installation-and-basic-setup]] + + + + + +TGI က install လုပ်ဖို့နဲ့ အသုံးပြုဖို့ လွယ်ကူပြီး၊ Hugging Face ecosystem ထဲမှာ နက်ရှိုင်းစွာ ပေါင်းစပ်ထားပါတယ်။ + +ပထမဆုံး၊ Docker ကို အသုံးပြုပြီး TGI server ကို launch လုပ်ပါ။ + +```sh +docker run --gpus all \ + --shm-size 1g \ + -p 8080:80 \ + -v ~/.cache/huggingface:/data \ + ghcr.io/huggingface/text-generation-inference:latest \ + --model-id HuggingFaceTB/SmolLM2-360M-Instruct +``` + +အဲဒီနောက် Hugging Face ရဲ့ InferenceClient ကို အသုံးပြုပြီး အပြန်အလှန်ဆက်သွယ်ပါ။ + +```python +from huggingface_hub import InferenceClient + +# TGI endpoint ကို ညွှန်ပြပြီး client ကို Initialize လုပ်ပါ။ +client = InferenceClient( + model="http://localhost:8080", # URL to the TGI server +) + +# Text generation +response = client.text_generation( + "Tell me a story", + max_new_tokens=100, + temperature=0.7, + top_p=0.95, + details=True, + stop_sequences=[], +) +print(response.generated_text) + +# For chat format +response = client.chat_completion( + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Tell me a story"}, + ], + max_tokens=100, + temperature=0.7, + top_p=0.95, +) +print(response.choices[0].message.content) +``` + +တနည်းအားဖြင့် OpenAI client ကို အသုံးပြုနိုင်ပါတယ်။ + +```python +from openai import OpenAI + +# Initialize client pointing to TGI endpoint +client = OpenAI( + base_url="http://localhost:8080/v1", # /v1 ကို ထည့်သွင်းဖို့ သေချာပါစေ။ + api_key="not-needed", # TGI က default အားဖြင့် API key မလိုအပ်ပါဘူး။ +) + +# Chat completion +response = client.chat.completions.create( + model="HuggingFaceTB/SmolLM2-360M-Instruct", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Tell me a story"}, + ], + max_tokens=100, + temperature=0.7, + top_p=0.95, +) +print(response.choices[0].message.content) +``` + + + + + +llama.cpp က install လုပ်ဖို့နဲ့ အသုံးပြုဖို့ လွယ်ကူပြီး၊ အနည်းဆုံး dependencies တွေပဲ လိုအပ်ကာ CPU နဲ့ GPU inference နှစ်ခုလုံးကို ထောက်ပံ့ပါတယ်။ + +ပထမဆုံး၊ llama.cpp ကို install လုပ်ပြီး build လုပ်ပါ။ + +```sh +# Repository ကို clone လုပ်ပါ။ +git clone https://github.com/ggerganov/llama.cpp +cd llama.cpp + +# Project ကို build လုပ်ပါ။ +make + +# SmolLM2-1.7B-Instruct-GGUF model ကို download လုပ်ပါ။ +curl -L -O https://huggingface.co/HuggingFaceTB/SmolLM2-1.7B-Instruct-GGUF/resolve/main/smollm2-1.7b-instruct.Q4_K_M.gguf +``` + +အဲဒီနောက် server ကို launch လုပ်ပါ (OpenAI API compatibility နဲ့)။ + +```sh +# Start the server +./server \ + -m smollm2-1.7b-instruct.Q4_K_M.gguf \ + --host 0.0.0.0 \ + --port 8080 \ + -c 4096 \ + --n-gpu-layers 0 # GPU ကို အသုံးပြုရန်အတွက် ပိုမိုမြင့်မားသော နံပါတ်ကို သတ်မှတ်ပါ။ +``` + +Hugging Face ရဲ့ InferenceClient ကို အသုံးပြုပြီး server နဲ့ အပြန်အလှန်ဆက်သွယ်ပါ။ + +```python +from huggingface_hub import InferenceClient + +# llama.cpp server ကို ညွှန်ပြပြီး client ကို Initialize လုပ်ပါ။ +client = InferenceClient( + model="http://localhost:8080/v1", # llama.cpp server ရဲ့ URL + token="sk-no-key-required", # llama.cpp server က ဒီ placeholder ကို လိုအပ်ပါတယ်။ +) + +# Text generation +response = client.text_generation( + "Tell me a story", + max_new_tokens=100, + temperature=0.7, + top_p=0.95, + details=True, +) +print(response.generated_text) + +# Chat format အတွက် +response = client.chat_completion( + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Tell me a story"}, + ], + max_tokens=100, + temperature=0.7, + top_p=0.95, +) +print(response.choices[0].message.content) +``` + +တနည်းအားဖြင့် OpenAI client ကို အသုံးပြုနိုင်ပါတယ်။ + +```python +from openai import OpenAI + +# llama.cpp server ကို ညွှန်ပြပြီး client ကို Initialize လုပ်ပါ။ +client = OpenAI( + base_url="http://localhost:8080/v1", + api_key="sk-no-key-required", # llama.cpp server က ဒီ placeholder ကို လိုအပ်ပါတယ်။ +) + +# Chat completion +response = client.chat.completions.create( + model="smollm2-1.7b-instruct", # Server က model တစ်ခုတည်းသာ load လုပ်တာကြောင့် model identifier က ဘာပဲဖြစ်ဖြစ် ရပါတယ်။ + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Tell me a story"}, + ], + max_tokens=100, + temperature=0.7, + top_p=0.95, +) +print(response.choices[0].message.content) +``` + + + + + +vLLM က install လုပ်ဖို့နဲ့ အသုံးပြုဖို့ လွယ်ကူပြီး၊ OpenAI API compatibility နဲ့ native Python interface နှစ်ခုလုံး ပါဝင်ပါတယ်။ + +ပထမဆုံး၊ vLLM OpenAI-compatible server ကို launch လုပ်ပါ။ + +```sh +python -m vllm.entrypoints.openai.api_server \ + --model HuggingFaceTB/SmolLM2-360M-Instruct \ + --host 0.0.0.0 \ + --port 8000 +``` + +အဲဒီနောက် Hugging Face ရဲ့ InferenceClient ကို အသုံးပြုပြီး အပြန်အလှန်ဆက်သွယ်ပါ။ + +```python +from huggingface_hub import InferenceClient + +# vLLM endpoint ကို ညွှန်ပြပြီး client ကို Initialize လုပ်ပါ။ +client = InferenceClient( + model="http://localhost:8000/v1", # vLLM server ရဲ့ URL +) + +# Text generation +response = client.text_generation( + "Tell me a story", + max_new_tokens=100, + temperature=0.7, + top_p=0.95, + details=True, +) +print(response.generated_text) + +# Chat format အတွက် +response = client.chat_completion( + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Tell me a story"}, + ], + max_tokens=100, + temperature=0.7, + top_p=0.95, +) +print(response.choices[0].message.content) +``` + +တနည်းအားဖြင့် OpenAI client ကို အသုံးပြုနိုင်ပါတယ်။ + +```python +from openai import OpenAI + +# vLLM endpoint ကို ညွှန်ပြပြီး client ကို Initialize လုပ်ပါ။ +client = OpenAI( + base_url="http://localhost:8000/v1", + api_key="not-needed", # vLLM က default အားဖြင့် API key မလိုအပ်ပါဘူး။ +) + +# Chat completion +response = client.chat.completions.create( + model="HuggingFaceTB/SmolLM2-360M-Instruct", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Tell me a story"}, + ], + max_tokens=100, + temperature=0.7, + top_p=0.95, +) +print(response.choices[0].message.content) +``` + + + + + +### Basic Text Generation + +frameworks တွေနဲ့ text generation ရဲ့ ဥပမာတွေကို ကြည့်ရအောင်။ + + + + + +ပထမဆုံး၊ TGI ကို အဆင့်မြင့် parameters တွေနဲ့ deploy လုပ်ပါ။ +```sh +docker run --gpus all \ + --shm-size 1g \ + -p 8080:80 \ + -v ~/.cache/huggingface:/data \ + ghcr.io/huggingface/text-generation-inference:latest \ + --model-id HuggingFaceTB/SmolLM2-360M-Instruct \ + --max-total-tokens 4096 \ + --max-input-length 3072 \ + --max-batch-total-tokens 8192 \ + --waiting-served-ratio 1.2 +``` + +ပြောင်းလွယ်ပြင်လွယ်ရှိတဲ့ text generation အတွက် InferenceClient ကို အသုံးပြုပါ။ + +```python +from huggingface_hub import InferenceClient + +client = InferenceClient(model="http://localhost:8080") + +# အဆင့်မြင့် parameters ဥပမာ +response = client.chat_completion( + messages=[ + {"role": "system", "content": "You are a creative storyteller."}, + {"role": "user", "content": "Write a creative story"}, + ], + temperature=0.8, + max_tokens=200, + top_p=0.95, +) +print(response.choices[0].message.content) + +# Raw text generation +response = client.text_generation( + "Write a creative story about space exploration", + max_new_tokens=200, + temperature=0.8, + top_p=0.95, + repetition_penalty=1.1, + do_sample=True, + details=True, +) +print(response.generated_text) +``` + +ဒါမှမဟုတ် OpenAI client ကို အသုံးပြုပါ။ +```python +from openai import OpenAI + +client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed") + +# အဆင့်မြင့် parameters ဥပမာ +response = client.chat.completions.create( + model="HuggingFaceTB/SmolLM2-360M-Instruct", + messages=[ + {"role": "system", "content": "You are a creative storyteller."}, + {"role": "user", "content": "Write a creative story"}, + ], + temperature=0.8, # Higher for more creativity +) +print(response.choices[0].message.content) +``` + + + + + +llama.cpp အတွက် အဆင့်မြင့် parameters တွေကို server ကို launch လုပ်တဲ့အခါ သတ်မှတ်နိုင်ပါတယ်။ + +```sh +./server \ + -m smollm2-1.7b-instruct.Q4_K_M.gguf \ + --host 0.0.0.0 \ + --port 8080 \ + -c 4096 \ # Context size + --threads 8 \ # CPU threads to use + --batch-size 512 \ # Batch size for prompt evaluation + --n-gpu-layers 0 # GPU layers (0 = CPU only) +``` + +InferenceClient ကို အသုံးပြုပါ။ + +```python +from huggingface_hub import InferenceClient + +client = InferenceClient(model="http://localhost:8080/v1", token="sk-no-key-required") + +# Advanced parameters example +response = client.chat_completion( + messages=[ + {"role": "system", "content": "You are a creative storyteller."}, + {"role": "user", "content": "Write a creative story"}, + ], + temperature=0.8, + max_tokens=200, + top_p=0.95, +) +print(response.choices[0].message.content) + +# တိုက်ရိုက် text generation အတွက် +response = client.text_generation( + "Write a creative story about space exploration", + max_new_tokens=200, + temperature=0.8, + top_p=0.95, + repetition_penalty=1.1, + details=True, +) +print(response.generated_text) +``` + +ဒါမှမဟုတ် sampling parameters တွေကို ထိန်းချုပ်ပြီး generation လုပ်ဖို့ OpenAI client ကို အသုံးပြုပါ။ + +```python +from openai import OpenAI + +client = OpenAI(base_url="http://localhost:8080/v1", api_key="sk-no-key-required") + +# အဆင့်မြင့် parameters ဥပမာ +response = client.chat.completions.create( + model="smollm2-1.7b-instruct", + messages=[ + {"role": "system", "content": "You are a creative storyteller."}, + {"role": "user", "content": "Write a creative story"}, + ], + temperature=0.8, # ပိုမိုဖန်တီးမှုရှိရန် ပိုမိုမြင့်မားသော တန်ဖိုး + top_p=0.95, # Nucleus sampling probability + frequency_penalty=0.5, # မကြာခဏ ပေါ်လာသော tokens များကို ထပ်ခါတလဲလဲ မဖြစ်အောင် လျှော့ချပါ။ + presence_penalty=0.5, # ရှိပြီးသား tokens များကို ပြန်လည်ပေါ်ထွက်မှုကို လျှော့ချပါ။ + max_tokens=200, # အများဆုံး generation length +) +print(response.choices[0].message.content) +``` + +llama.cpp ရဲ့ native library ကိုလည်း ပိုမိုထိန်းချုပ်နိုင်ရန် အသုံးပြုနိုင်ပါတယ်။ + +```python +# တိုက်ရိုက် model access အတွက် llama-cpp-python package ကို အသုံးပြုခြင်း +from llama_cpp import Llama + +# Model ကို load လုပ်ပါ။ +llm = Llama( + model_path="smollm2-1.7b-instruct.Q4_K_M.gguf", + n_ctx=4096, # Context window size + n_threads=8, # CPU threads + n_gpu_layers=0, # GPU layers (0 = CPU only) +) + +# Model ရဲ့ မျှော်လင့်ထားတဲ့ format အတိုင်း prompt ကို format လုပ်ပါ။ +prompt = """<|im_start|>system +You are a creative storyteller. +<|im_end|> +<|im_start|>user +Write a creative story +<|im_end|> +<|im_start|>assistant +""" + +# တိကျတဲ့ parameter ထိန်းချုပ်မှုနဲ့ response ကို generate လုပ်ပါ။ +output = llm( + prompt, + max_tokens=200, + temperature=0.8, + top_p=0.95, + frequency_penalty=0.5, + presence_penalty=0.5, + stop=["<|im_end|>"], +) + +print(output["choices"][0]["text"]) +``` + + + + + +vLLM နဲ့ အဆင့်မြင့် အသုံးပြုမှုအတွက် InferenceClient ကို အသုံးပြုနိုင်ပါတယ်။ + +```python +from huggingface_hub import InferenceClient + +client = InferenceClient(model="http://localhost:8000/v1") + +# အဆင့်မြင့် parameters ဥပမာ +response = client.chat_completion( + messages=[ + {"role": "system", "content": "You are a creative storyteller."}, + {"role": "user", "content": "Write a creative story"}, + ], + temperature=0.8, + max_tokens=200, + top_p=0.95, +) +print(response.choices[0].message.content) + +# တိုက်ရိုက် text generation အတွက် +response = client.text_generation( + "Write a creative story about space exploration", + max_new_tokens=200, + temperature=0.8, + top_p=0.95, + details=True, +) +print(response.generated_text) +``` + +OpenAI client ကိုလည်း အသုံးပြုနိုင်ပါတယ်။ + +```python +from openai import OpenAI + +client = OpenAI(base_url="http://localhost:8000/v1", api_key="not-needed") + +# အဆင့်မြင့် parameters ဥပမာ +response = client.chat.completions.create( + model="HuggingFaceTB/SmolLM2-360M-Instruct", + messages=[ + {"role": "system", "content": "You are a creative storyteller."}, + {"role": "user", "content": "Write a creative story"}, + ], + temperature=0.8, + top_p=0.95, + max_tokens=200, +) +print(response.choices[0].message.content) +``` + +vLLM က fine-grained control ပါဝင်တဲ့ native Python interface ကိုလည်း ပံ့ပိုးပေးပါတယ်- + +```python +from vllm import LLM, SamplingParams + +# အဆင့်မြင့် parameters တွေနဲ့ model ကို Initialize လုပ်ပါ။ +llm = LLM( + model="HuggingFaceTB/SmolLM2-360M-Instruct", + gpu_memory_utilization=0.85, + max_num_batched_tokens=8192, + max_num_seqs=256, + block_size=16, +) + +# Sampling parameters တွေကို Configure လုပ်ပါ။ +sampling_params = SamplingParams( + temperature=0.8, # ပိုမိုဖန်တီးမှုရှိရန် ပိုမိုမြင့်မားသော တန်ဖိုး + top_p=0.95, # 95% ဖြစ်နိုင်ခြေအများဆုံး tokens များကို ထည့်သွင်းစဉ်းစားပါ။ + max_tokens=100, # အများဆုံး အရှည် + presence_penalty=1.1, # ထပ်ခါတလဲလဲ မဖြစ်အောင် လျှော့ချပါ။ + frequency_penalty=1.1, # ထပ်ခါတလဲလဲ မဖြစ်အောင် လျှော့ချပါ။ + stop=["\n\n", "###"], # Stop sequences +) + +# Text generate လုပ်ပါ။ +prompt = "Write a creative story" +outputs = llm.generate(prompt, sampling_params) +print(outputs[0].outputs[0].text) + +# Chat-style interactions အတွက် +chat_prompt = [ + {"role": "system", "content": "You are a creative storyteller."}, + {"role": "user", "content": "Write a creative story"}, +] +formatted_prompt = llm.get_chat_template()(chat_prompt) # Uses model's chat template +outputs = llm.generate(formatted_prompt, sampling_params) +print(outputs[0].outputs[0].text) +``` + + + + + +## Advanced Generation Control + +### Token Selection နှင့် Sampling[[token-selection-and-sampling]] + +text ကို generate လုပ်တဲ့ လုပ်ငန်းစဉ်မှာ အဆင့်တိုင်းမှာ နောက်ထပ် token ကို ရွေးချယ်တာ ပါဝင်ပါတယ်။ ဒီရွေးချယ်မှု လုပ်ငန်းစဉ်ကို parameters အမျိုးမျိုးကနေတစ်ဆင့် ထိန်းချုပ်နိုင်ပါတယ် - + +1. **Raw Logits**: token တစ်ခုစီအတွက် မူရင်း output probabilities များ။ +2. **Temperature**: ရွေးချယ်မှုမှာရှိတဲ့ ကျပန်းဆန်မှုကို ထိန်းချုပ်ပါတယ် (ပိုမြင့်ရင် ပိုမိုဖန်တီးမှုရှိပါတယ်)။ +3. **Top-p (Nucleus) Sampling**: ဖြစ်နိုင်ခြေပမာဏ X% ကို ဖွဲ့စည်းထားတဲ့ ထိပ်ဆုံး tokens တွေကို စစ်ထုတ်ပါတယ်။ +4. **Top-k Filtering**: ဖြစ်နိုင်ခြေအများဆုံး tokens k ခုအထိ ရွေးချယ်မှုကို ကန့်သတ်ပါတယ်။ + +ဒီ parameters တွေကို ဘယ်လို configure လုပ်ရမလဲဆိုတာကတော့... + + + + + +```python +client.generate( + "Write a creative story", + temperature=0.8, # ပိုမိုဖန်တီးမှုရှိရန် ပိုမိုမြင့်မားသော တန်ဖိုး + top_p=0.95, # 95% ဖြစ်နိုင်ခြေအများဆုံး tokens များကို ထည့်သွင်းစဉ်းစားပါ။ + top_k=50, # ထိပ်ဆုံး 50 tokens များကို ထည့်သွင်းစဉ်းစားပါ။ + max_new_tokens=100, # အများဆုံး အရှည် + repetition_penalty=1.1, # ထပ်ခါတလဲလဲ မဖြစ်အောင် လျှော့ချပါ။ +) +``` + + + + + +```python +# OpenAI API compatibility မှတစ်ဆင့် +response = client.completions.create( + model="smollm2-1.7b-instruct", # Model name (llama.cpp server အတွက် မည်သည့် string မဆို ဖြစ်နိုင်သည်) + prompt="ဖန်တီးမှုရှိတဲ့ ပုံပြင်တစ်ပုဒ် ရေးပေးပါ။", + temperature=0.8, # ပိုမိုဖန်တီးမှုရှိရန် ပိုမိုမြင့်မားသော တန်ဖိုး + top_p=0.95, # 95% ဖြစ်နိုင်ခြေအများဆုံး tokens များကို ထည့်သွင်းစဉ်းစားပါ။ + frequency_penalty=1.1, # မကြာခဏ ပေါ်လာသော tokens များကို ထပ်ခါတလဲလဲ မဖြစ်အောင် လျှော့ချပါ။ + presence_penalty=0.1, # ရှိပြီးသား tokens များကို ထပ်ခါတလဲလဲ မဖြစ်အောင် လျှော့ချပါ။ + max_tokens=100, # အများဆုံး အရှည် +) + +# llama-cpp-python တိုက်ရိုက် access မှတစ်ဆင့် +output = llm( + "Write a creative story", + temperature=0.8, + top_p=0.95, + top_k=50, + max_tokens=100, + repeat_penalty=1.1, +) +``` + + + + + +```python +params = SamplingParams( + temperature=0.8, # ပိုမိုဖန်တီးမှုရှိရန် ပိုမိုမြင့်မားသော တန်ဖိုး + top_p=0.95, # 95% ဖြစ်နိုင်ခြေအများဆုံး tokens များကို ထည့်သွင်းစဉ်းစားပါ။ + top_k=50, # ထိပ်ဆုံး 50 tokens များကို ထည့်သွင်းစဉ်းစားပါ။ + max_tokens=100, # အများဆုံး အရှည် + presence_penalty=0.1, # ထပ်ခါတလဲလဲ မဖြစ်အောင် လျှော့ချပါ။ +) +llm.generate("Write a creative story", sampling_params=params) +``` + + + + + +### ထပ်ခါတလဲလဲ ဖြစ်မှုကို ထိန်းချုပ်ခြင်း[[controlling-repetition]] + +frameworks နှစ်ခုလုံးက ထပ်ခါတလဲလဲ text generation ကို ကာကွယ်ဖို့ နည်းလမ်းတွေ ပံ့ပိုးပေးပါတယ်။ + + + + + +```python +client.generate( + "Write a varied text", + repetition_penalty=1.1, # ထပ်ခါတလဲလဲ ဖြစ်သော tokens များကို ဒဏ်ခတ်ပါ။ + no_repeat_ngram_size=3, # 3-gram ထပ်ခါတလဲလဲ ဖြစ်မှုကို ကာကွယ်ပါ။ +) +``` + + + + + +```python +# OpenAI API မှတစ်ဆင့် +response = client.completions.create( + model="smollm2-1.7b-instruct", + prompt="Write a varied text", + frequency_penalty=1.1, # မကြာခဏ ပေါ်လာသော tokens များကို ဒဏ်ခတ်ပါ။ + presence_penalty=0.8, # ရှိပြီးသား tokens များကို ဒဏ်ခတ်ပါ။ +) + +# တိုက်ရိုက် library မှတစ်ဆင့် +output = llm( + "Write a varied text", + repeat_penalty=1.1, # Penalize repeated tokens + frequency_penalty=0.5, # အပို frequency penalty + presence_penalty=0.5, # အပို presence penalty +) +``` + + + + + +```python +params = SamplingParams( + presence_penalty=0.1, # Token ရှိခြင်းအတွက် ဒဏ်ခတ်ပါ။ + frequency_penalty=0.1, # Token မကြာခဏ ပေါ်လာခြင်းအတွက် ဒဏ်ခတ်ပါ။ +) +``` + + + + + +### အရှည် ထိန်းချုပ်ခြင်းနှင့် ရပ်တန့်ခြင်း Sequences များ[[length-control-and-stop-sequences]] + +generation length ကို ထိန်းချုပ်နိုင်ပြီး ဘယ်အချိန်မှာ ရပ်တန့်ရမယ်ဆိုတာ သတ်မှတ်နိုင်ပါတယ်။ + + + + + +```python +client.generate( + "Generate a short paragraph", + max_new_tokens=100, + min_new_tokens=10, + stop_sequences=["\n\n", "###"], +) +``` + + + + + +```python +# OpenAI API မှတစ်ဆင့် +response = client.completions.create( + model="smollm2-1.7b-instruct", + prompt="Generate a short paragraph", + max_tokens=100, + stop=["\n\n", "###"], +) + +# တိုက်ရိုက် library မှတစ်ဆင့် +output = llm("Generate a short paragraph", max_tokens=100, stop=["\n\n", "###"]) +``` + + + + + +```python +params = SamplingParams( + max_tokens=100, + min_tokens=10, + stop=["###", "\n\n"], + ignore_eos=False, + skip_special_tokens=True, +) +``` + + + + + +## Memory Management[[memory-management]] + +frameworks နှစ်ခုလုံးက ထိရောက်တဲ့ inference အတွက် အဆင့်မြင့် memory management နည်းစနစ်တွေကို implement လုပ်ထားပါတယ်။ + + + + + +TGI က Flash Attention 2 နဲ့ continuous batching ကို အသုံးပြုပါတယ်။ + +```sh +# Memory optimization ပါဝင်တဲ့ Docker deployment +docker run --gpus all -p 8080:80 \ + --shm-size 1g \ + ghcr.io/huggingface/text-generation-inference:latest \ + --model-id HuggingFaceTB/SmolLM2-1.7B-Instruct \ + --max-batch-total-tokens 8192 \ + --max-input-length 4096 +``` + + + + + +llama.cpp က quantization နဲ့ optimized memory layout ကို အသုံးပြုပါတယ်- + +```sh +# Memory optimizations ပါဝင်တဲ့ Server +./server \ + -m smollm2-1.7b-instruct.Q4_K_M.gguf \ + --host 0.0.0.0 \ + --port 8080 \ + -c 2048 \ # Context size + --threads 4 \ # CPU threads + --n-gpu-layers 32 \ # ပိုကြီးတဲ့ models တွေအတွက် GPU layers များများ အသုံးပြုပါ + --mlock \ # Swapping မဖြစ်စေရန် memory ကို lock လုပ်ပါ + --cont-batching # Continuous batching ကို ဖွင့်ပါ။ +``` + +သင့် GPU အတွက် အရမ်းကြီးတဲ့ models တွေအတွက် CPU offloading ကို အသုံးပြုနိုင်ပါတယ်။ + +```sh +./server \ + -m smollm2-1.7b-instruct.Q4_K_M.gguf \ + --n-gpu-layers 20 \ # ပထမဆုံး 20 layers ကို GPU မှာ ထားပါ + --threads 8 # CPU layers တွေအတွက် CPU threads များများ အသုံးပြုပါ +``` + + + + + +vLLM က အကောင်းဆုံး memory management အတွက် PagedAttention ကို အသုံးပြုပါတယ်။ + +```python +from vllm.engine.arg_utils import AsyncEngineArgs + +engine_args = AsyncEngineArgs( + model="HuggingFaceTB/SmolLM2-1.7B-Instruct", + gpu_memory_utilization=0.85, + max_num_batched_tokens=8192, + block_size=16, +) + +llm = LLM(engine_args=engine_args) +``` + + + + + +## အရင်းအမြစ်များ[[resources]] + +- [Text Generation Inference Documentation](https://huggingface.co/docs/text-generation-inference) +- [TGI GitHub Repository](https://github.com/huggingface/text-generation-inference) +- [vLLM Documentation](https://vllm.readthedocs.io/) +- [vLLM GitHub Repository](https://github.com/vllm-project/vllm) +- [PagedAttention Paper](https://arxiv.org/abs/2309.06180) +- [llama.cpp GitHub Repository](https://github.com/ggerganov/llama.cpp) +- [llama-cpp-python Repository](https://github.com/abetlen/llama-cpp-python) + +## ဝေါဟာရ ရှင်းလင်းချက် (Glossary) + +* **Optimized Inference Deployment**: AI မော်ဒယ်များကို အသုံးပြုသူများထံသို့ ထိရောက်စွာနှင့် လျင်မြန်စွာ ဝန်ဆောင်မှုပေးနိုင်ရန် အကောင်းဆုံးဖြစ်အောင် ပြုလုပ်ထားသော လုပ်ငန်းစဉ်။ +* **LLM (Large Language Model)**: လူသားဘာသာစကားကို နားလည်ပြီး ထုတ်လုပ်ပေးနိုင်တဲ့ အလွန်ကြီးမားတဲ့ Artificial Intelligence (AI) မော်ဒယ်တွေ ဖြစ်ပါတယ်။ +* **Text Generation Inference (TGI)**: Hugging Face မှ LLM များအတွက် မြန်နှုန်းမြင့် text generation ကို အထူးပြုထားသော framework တစ်ခု။ +* **vLLM**: မြန်နှုန်းမြင့် LLM inference အတွက် ဒီဇိုင်းထုတ်ထားသော library တစ်ခုဖြစ်ပြီး PagedAttention ကို အသုံးပြုသည်။ +* **llama.cpp**: LLaMA models များကို consumer hardware ပေါ်တွင် run နိုင်ရန် အဓိကထားသော C/C++ implementation တစ်ခု။ +* **Production Environments**: ဆော့ဖ်ဝဲလ် သို့မဟုတ် မော်ဒယ်များကို အစစ်အမှန် အသုံးပြုသူများထံသို့ ဝန်ဆောင်မှုပေးသည့် ပတ်ဝန်းကျင်။ +* **Inference Efficiency**: AI မော်ဒယ်တစ်ခုက input data မှ output ကို ထုတ်လုပ်ရာတွင် အချိန်နှင့် အရင်းအမြစ်များကို မည်မျှ ထိရောက်စွာ အသုံးပြုနိုင်မှု။ +* **Framework Selection Guide**: မတူညီသော အသုံးပြုမှုပုံစံများအတွက် သင့်လျော်သော framework ကို ရွေးချယ်ရန် လမ်းညွှန်။ +* **Memory Management**: ကွန်ပျူတာ၏ memory ကို ထိထိရောက်ရောက် စီမံခန့်ခွဲခြင်း။ +* **Performance**: စနစ်တစ်ခု၏ အလုပ်လုပ်နိုင်စွမ်း သို့မဟုတ် အရှိန်အဟုန်။ +* **Flash Attention 2**: Transformer models များတွင် attention mechanism ကို memory bandwidth bottlenecks များကို ဖြေရှင်းပေးခြင်းဖြင့် optimization လုပ်သော နည်းပညာ။ +* **Continuous Batching**: GPU ကို အလုပ်များနေအောင် ထိန်းထားနိုင်ရန် requests များကို အဆက်မပြတ် batch လုပ်ပြီး ပေးပို့သော နည်းလမ်း။ +* **GPU (Graphics Processing Unit)**: AI/ML လုပ်ငန်းများတွင် အရှိန်မြှင့်ရန် အသုံးပြုသော processor။ +* **CPU (Central Processing Unit)**: ကွန်ပျူတာ၏ အဓိက processor။ +* **VRAM (Video RAM)**: GPU တွင် အသုံးပြုသော RAM အမျိုးအစား။ +* **PagedAttention**: LLM inference တွင် KV cache memory management ကို optimization လုပ်သော နည်းပညာ။ +* **KV Cache**: Text generation လုပ်နေစဉ်အတွင်း Transformer model က သိမ်းဆည်းထားသော attention keys နှင့် values များ။ +* **Memory Paging**: Memory ကို fixed-size "pages" များအဖြစ် ပိုင်းခြားခြင်း။ +* **Memory Fragmentation**: Memory ကို ထိရောက်စွာ အသုံးမပြုနိုင်ဘဲ အပိုင်းအစများအဖြစ် ပြန့်ကျဲနေခြင်း။ +* **Throughput**: အချိန်ယူနစ်တစ်ခုအတွင်း စနစ်တစ်ခုက လုပ်ဆောင်နိုင်သော လုပ်ငန်းပမာဏ။ +* **Quantization**: Model weights တွေရဲ့ precision ကို လျှော့ချခြင်းဖြင့် model size နဲ့ memory requirements တွေကို လျှော့ချသော နည်းလမ်း။ +* **INT8 (8-bit integers)**: 8-bit integers ဖြင့် ကိုယ်စားပြုသော ကိန်းဂဏန်းများ။ +* **GGML/GGUF Format**: llama.cpp မှ quantized inference အတွက် အကောင်းဆုံးဖြစ်အောင် ပြုလုပ်ထားသော custom tensor formats များ။ +* **Mixed Precision**: Model ၏ မတူညီသော အစိတ်အပိုင်းများတွင် မတူညီသော quantization levels များကို အသုံးပြုခြင်း။ +* **CPU Architectures**: CPU အမျိုးအစားများ (ဥပမာ - AVX2, AVX-512, NEON)။ +* **Local Deployments**: မော်ဒယ်များကို သုံးစွဲသူ၏ ကွန်ပျူတာ သို့မဟုတ် local server ပေါ်တွင် တပ်ဆင်အသုံးပြုခြင်း။ +* **Edge Devices**: ကွန်ပျူတာကွန်ရက်၏ အစွန်းပိုင်း (ဥပမာ - mobile devices, IoT devices) တွင် အလုပ်လုပ်သော devices များ။ +* **Enterprise-level Deployment**: လုပ်ငန်းကြီးများအတွက် ဒီဇိုင်းထုတ်ထားသော deployment ပုံစံ။ +* **Kubernetes Support**: Containerized application များကို automate လုပ်ပြီး deploy, scale လုပ်ရန်အတွက် Kubernetes platform ကို ထောက်ပံ့ခြင်း။ +* **Prometheus**: Monitoring system တစ်ခု။ +* **Grafana**: Data visualization tool တစ်ခု။ +* **Automatic Scaling**: requests များ၏ ပမာဏအပေါ် မူတည်ပြီး resources များကို အလိုအလျောက် ချိန်ညှိခြင်း။ +* **Content Filtering**: မသင့်လျော်သော သို့မဟုတ် အန္တရာယ်ရှိသော အကြောင်းအရာများကို စစ်ထုတ်ခြင်း။ +* **Rate Limiting**: အချိန်အတိုင်းအတာတစ်ခုအတွင်း requests အရေအတွက်ကို ကန့်သတ်ခြင်း။ +* **Developer-friendly Approach**: Developers များအတွက် အသုံးပြုရလွယ်ကူသော ချဉ်းကပ်မှု။ +* **OpenAI API Compatibility**: OpenAI ၏ API နှင့် တွဲဖက်အသုံးပြုနိုင်ခြင်း။ +* **Ray**: Distributed computing အတွက် Python framework တစ်ခု။ +* **Portability**: ဆော့ဖ်ဝဲလ်တစ်ခုကို မတူညီသော platform များ သို့မဟုတ် environments များသို့ အလွယ်တကူ ရွှေ့ပြောင်းအသုံးပြုနိုင်ခြင်း။ +* **`docker run --gpus all`**: Docker container ကို GPU အားလုံးကို အသုံးပြုပြီး run ရန် command။ +* **`--shm-size 1g`**: Shared memory size ကို 1GB အဖြစ် သတ်မှတ်ခြင်း။ +* **`InferenceClient`**: Hugging Face Hub မှ inference endpoint များနှင့် အပြန်အလှန်ဆက်သွယ်ရန် Python client။ +* **`openai`**: OpenAI API ကို အသုံးပြုရန်အတွက် Python client library။ +* **`git clone`**: Git repository ကို download လုပ်ရန် command။ +* **`make`**: Source code ကို executable file အဖြစ် build လုပ်ရန် command။ +* **`curl -L -O`**: URL မှ file တစ်ခုကို download လုပ်ရန် command။ +* **`--host`, `--port`**: Server ကို listen လုပ်မည့် host address နှင့် port နံပါတ်။ +* **`--n-gpu-layers`**: GPU တွင် ထားရှိမည့် model layers အရေအတွက်။ +* **Context Size (`-c`)**: Model က တစ်ကြိမ်တည်း လုပ်ဆောင်နိုင်သော tokens အရေအတွက် အများဆုံး။ +* **CPU Threads (`--threads`)**: CPU တွင် အသုံးပြုမည့် threads အရေအတွက်။ +* **Batch Size (`--batch-size`)**: Prompt evaluation အတွက် batch အရွယ်အစား။ +* **`llama_cpp`**: llama.cpp C++ library အတွက် Python bindings။ +* **`Llama` Class**: llama-cpp-python library မှ LLaMA model ကို load လုပ်ရန် class။ +* **`n_ctx`**: Model ၏ context window size။ +* **`n_threads`**: CPU threads အရေအတွက်။ +* **`n_gpu_layers`**: GPU ပေါ်တွင် ထားရှိမည့် layers အရေအတွက်။ +* **`SamplingParams`**: vLLM တွင် text generation အတွက် sampling parameters များကို သတ်မှတ်ရန် class။ +* **Temperature**: generated text ၏ randomness သို့မဟုတ် creativity ကို ထိန်းချုပ်သော parameter။ +* **Top-p (Nucleus) Sampling**: ဖြစ်နိုင်ခြေအများဆုံး tokens အချို့ကို ရွေးချယ်ပြီး ၎င်းတို့၏ စုစုပေါင်း ဖြစ်နိုင်ခြေသည် သတ်မှတ်ထားသော တန်ဖိုး (ဥပမာ - 0.95) ထက် မကျော်လွန်စေရ။ +* **Top-k Filtering**: ဖြစ်နိုင်ခြေအများဆုံး tokens `k` ခုကိုသာ ရွေးချယ်ပြီး ကျန်များကို လျစ်လျူရှုသည်။ +* **`max_new_tokens` / `max_tokens`**: Generate လုပ်မည့် tokens အရေအတွက် အများဆုံး။ +* **`repetition_penalty`**: ထပ်ခါတလဲလဲ ဖြစ်သော tokens များကို ဒဏ်ခတ်ရန် parameter။ +* **`do_sample`**: True ဖြစ်ပါက sampling ကို အသုံးပြုပြီး၊ False ဖြစ်ပါက greedy decoding ကို အသုံးပြုသည်။ +* **`frequency_penalty`**: မကြာခဏ ပေါ်လာသော tokens များကို ထပ်ခါတလဲလဲ မဖြစ်အောင် လျှော့ချရန် parameter။ +* **`presence_penalty`**: ရှိပြီးသား tokens များကို ထပ်ခါတလဲလဲ မဖြစ်အောင် လျှော့ချရန် parameter။ +* **`min_new_tokens` / `min_tokens`**: Generate လုပ်မည့် tokens အရေအတွက် အနည်းဆုံး။ +* **`stop_sequences`**: Generated text ကို ရပ်တန့်ရန်အတွက် သတ်မှတ်ထားသော sequence များ။ +* **`ignore_eos`**: End-of-sequence token ကို လျစ်လျူရှုရန်။ +* **`skip_special_tokens`**: Generated text မှ special tokens များကို ဖယ်ရှားရန်။ +* **CPU Offloading**: Model ၏ အစိတ်အပိုင်းအချို့ကို GPU မှ CPU သို့ ရွှေ့ပြောင်းပြီး လုပ်ဆောင်ခြင်း။ +* **`--mlock`**: Memory ကို lock လုပ်ပြီး swapping မဖြစ်စေရန် ကာကွယ်ခြင်း။ \ No newline at end of file diff --git a/chapters/my/chapter2/9.mdx b/chapters/my/chapter2/9.mdx new file mode 100644 index 000000000..aa0f8475d --- /dev/null +++ b/chapters/my/chapter2/9.mdx @@ -0,0 +1,252 @@ + + + + +# အခန်းပြီးဆုံးခြင်း စစ်ဆေးမှု[[end-of-chapter-quiz]] + + + +### 1. Language modeling pipeline ၏ အစီအစဉ်က ဘာလဲ။ + + + +### 2. Base Transformer model က ထုတ်ပေးတဲ့ tensor မှာ dimension ဘယ်နှစ်ခုရှိပြီး၊ ဘာတွေလဲ။ + +