บทแนะนำนี้จะแสดงวิธีปรับแต่งโมเดล Instruct 2B ของ RecurrentGemma สำหรับงานแปลภาษาอังกฤษเป็นฝรั่งเศสโดยใช้ไลบรารี recurrentgemma
ของ Google DeepMind, JAX (ไลบรารีการคำนวณเชิงตัวเลขที่มีประสิทธิภาพสูง), Flax (ไลบรารีเครือข่ายประสาทที่ใช้ JAX), Chex (ไลบรารียูทิลิตีสำหรับเขียนโค้ด JAX ที่เชื่อถือได้), Optax (ไลบรารีการประมวลผลและการเพิ่มประสิทธิภาพด้วย Gradient ที่ใช้ JAX) และชุดข้อมูล MTNT (การแปลด้วยคอมพิวเตอร์ของข้อความที่มีเสียงรบกวน) แม้ว่าจะไม่ได้ใช้ Flax ในโน้ตบุ๊กนี้โดยตรง แต่ก็มีการใช้ Flax เพื่อสร้าง Gemma
ไลบรารี recurrentgemma
เขียนด้วย JAX, Flax, Orbax (ไลบรารีที่ใช้ JAX สำหรับยูทิลิตีการฝึก เช่น การตรวจสอบจุดพัก) และ SentencePiece (ไลบรารีตัวแยกวิเคราะห์/แยกวิเคราะห์ออก)
โน้ตบุ๊กนี้สามารถทำงานใน Google Colab ด้วย GPU T4 (ไปที่แก้ไข > การตั้งค่าโน้ตบุ๊ก > เลือก GPU T4 ในส่วนตัวเร่งฮาร์ดแวร์)
ตั้งค่า
ส่วนต่อไปนี้จะอธิบายขั้นตอนในการเตรียมโน้ตบุ๊กเพื่อใช้โมเดล RecurrentGemma ซึ่งรวมถึงการเข้าถึงโมเดล การรับคีย์ API และการกําหนดค่ารันไทม์ของโน้ตบุ๊ก
ตั้งค่าการเข้าถึง Kaggle สําหรับ Gemma
หากต้องการทําตามบทแนะนํานี้ให้เสร็จสมบูรณ์ ก่อนอื่นคุณต้องทําตามวิธีการตั้งค่าที่คล้ายกับการตั้งค่า Gemma โดยมีข้อยกเว้นบางประการ ดังนี้
- รับสิทธิ์เข้าถึง RecurrentGemma (แทน Gemma) ใน kaggle.com
- เลือกรันไทม์ Colab ที่มีทรัพยากรเพียงพอที่จะเรียกใช้โมเดล RecurrentGemma
- สร้างและกําหนดค่าชื่อผู้ใช้และคีย์ API ของ Kaggle
หลังจากตั้งค่า RecurrentGemma เสร็จแล้ว ให้ไปยังส่วนถัดไปเพื่อตั้งค่าตัวแปรสภาพแวดล้อมสําหรับสภาพแวดล้อม Colab
ตั้งค่าตัวแปรสภาพแวดล้อม
ตั้งค่าตัวแปรสภาพแวดล้อมสําหรับ KAGGLE_USERNAME
และ KAGGLE_KEY
เมื่อได้รับข้อความ "ให้สิทธิ์เข้าถึงไหม" ให้ยอมรับการให้สิทธิ์เข้าถึงข้อมูลลับ
import os
from google.colab import userdata # `userdata` is a Colab API.
os.environ["KAGGLE_USERNAME"] = userdata.get('KAGGLE_USERNAME')
os.environ["KAGGLE_KEY"] = userdata.get('KAGGLE_KEY')
ติดตั้งไลบรารี recurrentgemma
ปัจจุบันการเร่งฮาร์ดแวร์ของ Colab แบบไม่มีค่าใช้จ่ายinsufficientที่จะเรียกใช้สมุดบันทึกนี้ หากคุณใช้ Colab แบบชําระเงินตามการใช้งานหรือ Colab Pro ให้คลิกแก้ไข > การตั้งค่าโน้ตบุ๊ก > เลือก GPU A100 > บันทึกเพื่อเปิดใช้การเร่งด้วยฮาร์ดแวร์
ขั้นตอนต่อไป คุณต้องติดตั้งไลบรารี recurrentgemma
ของ Google DeepMind จาก github.com/google-deepmind/recurrentgemma
หากได้รับข้อผิดพลาดเกี่ยวกับ "เครื่องมือแก้ไขข้อกำหนดของ pip" โดยทั่วไปแล้วคุณก็ไม่ต้องสนใจ
pip install -q git+https://github.com/google-deepmind/recurrentgemma.git
นำเข้าไลบรารี
โน้ตบุ๊กนี้ใช้ Flax (สําหรับเครือข่ายประสาท), JAX หลัก, SentencePiece (สําหรับการแยกคํา), Chex (ไลบรารียูทิลิตีสําหรับการเขียนโค้ด JAX ที่เชื่อถือได้), Optax (ไลบรารีการประมวลผลและการเพิ่มประสิทธิภาพของอนุพันธ์) และชุดข้อมูล TensorFlow
import pathlib
from typing import Any, Mapping, Iterator
import enum
import functools
import chex
import jax
import jax.numpy as jnp
import optax
import tensorflow as tf
import tensorflow_datasets as tfds
import sentencepiece as spm
from recurrentgemma import jax as recurrentgemma
โหลดโมเดล RecurrentGemma
- โหลดโมเดล RecurrentGemma ด้วย
kagglehub.model_download
ซึ่งใช้อาร์กิวเมนต์ 3 รายการ ได้แก่
handle
: แฮนเดิลโมเดลจาก Kagglepath
: (สตริงที่ไม่บังคับ) เส้นทางในเครื่องforce_download
: (บูลีนที่ไม่บังคับ) บังคับให้ดาวน์โหลดโมเดลอีกครั้ง
RECURRENTGEMMA_VARIANT = '2b-it' # @param ['2b', '2b-it'] {type:"string"}
import kagglehub
RECURRENTGEMMA_PATH = kagglehub.model_download(f'google/recurrentgemma/flax/{RECURRENTGEMMA_VARIANT}')
Downloading from https://www.kaggle.com/api/v1/models/google/recurrentgemma/flax/2b-it/1/download... 100%|██████████| 3.85G/3.85G [00:50<00:00, 81.5MB/s] Extracting model files...
print('RECURRENTGEMMA_VARIANT:', RECURRENTGEMMA_VARIANT)
RECURRENTGEMMA_VARIANT: 2b-it
- ตรวจสอบตําแหน่งของน้ำหนักโมเดลและตัวแยกวิเคราะห์ แล้วตั้งค่าตัวแปรเส้นทาง ไดเรกทอรีตัวแยกวิเคราะห์จะอยู่ในไดเรกทอรีหลักที่คุณดาวน์โหลดโมเดล ส่วนน้ำหนักโมเดลจะอยู่ในไดเรกทอรีย่อย เช่น
- ไฟล์
tokenizer.model
จะอยู่ใน/LOCAL/PATH/TO/recurrentgemma/flax/2b-it/1
) - จุดตรวจของโมเดลจะอยู่ใน
/LOCAL/PATH/TO/recurrentgemma/flax/2b-it/1/2b-it
)
CKPT_PATH = os.path.join(RECURRENTGEMMA_PATH, RECURRENTGEMMA_VARIANT)
TOKENIZER_PATH = os.path.join(RECURRENTGEMMA_PATH, 'tokenizer.model')
print('CKPT_PATH:', CKPT_PATH)
print('TOKENIZER_PATH:', TOKENIZER_PATH)
CKPT_PATH: /root/.cache/kagglehub/models/google/recurrentgemma/flax/2b-it/1/2b-it TOKENIZER_PATH: /root/.cache/kagglehub/models/google/recurrentgemma/flax/2b-it/1/tokenizer.model
โหลดและเตรียมชุดข้อมูล MTNT และตัวแยกวิเคราะห์ Gemma
คุณจะใช้ชุดข้อมูล MTNT (Machine Translation of Noisy Text) ซึ่งมีอยู่ในชุดข้อมูล TensorFlow
ดาวน์โหลดชุดข้อมูลภาษาอังกฤษเป็นฝรั่งเศสของชุดข้อมูล MTNT แล้วสุ่มตัวอย่าง 2 รายการ ตัวอย่างแต่ละรายการในชุดข้อมูลประกอบด้วย 2 รายการ ได้แก่ src
: ประโยคภาษาอังกฤษต้นฉบับ และ dst
: คำแปลภาษาฝรั่งเศสที่สอดคล้องกัน
ds = tfds.load("mtnt/en-fr", split="train")
ds = ds.take(2)
ds = ds.as_numpy_iterator()
for idx, example in enumerate(ds):
print(f'Example {idx}:')
for key, val in example.items():
print(f'{key}: {val}')
print()
Downloading and preparing dataset 35.08 MiB (download: 35.08 MiB, generated: 11.33 MiB, total: 46.41 MiB) to /root/tensorflow_datasets/mtnt/en-fr/1.0.0... Dl Completed...: 0 url [00:00, ? url/s] Dl Size...: 0 MiB [00:00, ? MiB/s] Extraction completed...: 0 file [00:00, ? file/s] Generating splits...: 0%| | 0/3 [00:00<?, ? splits/s] Generating train examples...: 0%| | 0/35692 [00:00<?, ? examples/s] Shuffling /root/tensorflow_datasets/mtnt/en-fr/1.0.0.incompleteJLH33K/mtnt-train.tfrecord*...: 0%| … Generating test examples...: 0%| | 0/1020 [00:00<?, ? examples/s] Shuffling /root/tensorflow_datasets/mtnt/en-fr/1.0.0.incompleteJLH33K/mtnt-test.tfrecord*...: 0%| |… Generating valid examples...: 0%| | 0/811 [00:00<?, ? examples/s] Shuffling /root/tensorflow_datasets/mtnt/en-fr/1.0.0.incompleteJLH33K/mtnt-valid.tfrecord*...: 0%| … Dataset mtnt downloaded and prepared to /root/tensorflow_datasets/mtnt/en-fr/1.0.0. Subsequent calls will reuse this data. Example 0: dst: b'Le groupe de " toutes les \xc3\xa9toiles potentielles de la conf\xc3\xa9rence de l\'Est mais qui ne s\'en sortent pas dans le groupe de l\'Ouest ".' src: b'The group of \xe2\x80\x9ceastern conference potential all stars but not making it in the West\xe2\x80\x9d group.' Example 1: dst: b"Kameron est-elle un peu aigrie de son manque de temps \xc3\xa0 l'\xc3\xa9cran ?" src: b'Is Kameron a Little Salty About Her Lack of Air Time?'
โหลดตัวแยกวิเคราะห์ Gemma ที่สร้างขึ้นโดยใช้ sentencepiece.SentencePieceProcessor
vocab = spm.SentencePieceProcessor()
vocab.Load(TOKENIZER_PATH)
True
ปรับแต่ง SentencePieceProcessor
สำหรับงานแปลจากอังกฤษเป็นฝรั่งเศส เนื่องจากคุณจะปรับแต่งส่วนภาษาอังกฤษของโมเดล RecurrentGemma (Griffin) คุณจึงต้องทำการปรับเปลี่ยนบางอย่าง เช่น
คำนำหน้าอินพุต: การเพิ่มคำนำหน้าทั่วไปในอินพุตแต่ละรายการจะเป็นสัญญาณบ่งบอกงานแปล เช่น คุณอาจใช้พรอมต์ที่มีคำนำหน้า เช่น
Translate this into French: [INPUT_SENTENCE]
ส่วนต่อท้ายการเริ่มต้นการแปล: การเพิ่มส่วนต่อท้ายที่ท้ายพรอมต์แต่ละรายการจะบอกให้โมเดล Gemma ทราบว่าควรเริ่มกระบวนการแปลเมื่อใด การขึ้นบรรทัดใหม่น่าจะช่วยได้
โทเค็นโมเดลภาษา: โมเดล RecurrentGemma (Griffin) ต้องการโทเค็น "จุดเริ่มต้นของลำดับ" ที่จุดเริ่มต้นของลำดับแต่ละรายการ ในทํานองเดียวกัน คุณต้องเพิ่มโทเค็น "สิ้นสุดลําดับ" ที่ท้ายตัวอย่างการฝึกแต่ละรายการ
สร้าง Wrapper ที่กําหนดเองรอบ SentencePieceProcessor
ดังนี้
class GriffinTokenizer:
"""A custom wrapper around a SentencePieceProcessor."""
def __init__(self, spm_processor: spm.SentencePieceProcessor):
self._spm_processor = spm_processor
@property
def pad_id(self) -> int:
"""Fast access to the pad ID."""
return self._spm_processor.pad_id()
def tokenize(
self,
example: str | bytes,
prefix: str = '',
suffix: str = '',
add_eos: bool = True,
) -> jax.Array:
"""
A tokenization function.
Args:
example: Input string to tokenize.
prefix: Prefix to add to the input string.
suffix: Suffix to add to the input string.
add_eos: If True, add an end of sentence token at the end of the output
sequence.
Returns:
Tokens corresponding to the input string.
"""
int_list = [self._spm_processor.bos_id()]
int_list.extend(self._spm_processor.EncodeAsIds(prefix + example + suffix))
if add_eos:
int_list.append(self._spm_processor.eos_id())
return jnp.array(int_list, dtype=jnp.int32)
def tokenize_tf_op(
self,
str_tensor: tf.Tensor,
prefix: str = '',
suffix: str = '',
add_eos: bool = True,
) -> tf.Tensor:
"""A TensforFlow operator for the `tokenize` function."""
encoded = tf.numpy_function(
self.tokenize,
[str_tensor, prefix, suffix, add_eos],
tf.int32)
encoded.set_shape([None])
return encoded
def to_string(self, tokens: jax.Array) -> str:
"""Convert an array of tokens to a string."""
return self._spm_processor.EncodeIds(tokens.tolist())
ลองใช้โดยการสร้างอินสแตนซ์ GriffinTokenizer
ที่กําหนดเองใหม่ แล้วนําไปใช้กับตัวอย่างชุดข้อมูล MTNT ขนาดเล็ก ดังนี้
def tokenize_source(tokenizer, example: tf.Tensor):
return tokenizer.tokenize_tf_op(
example,
prefix='Translate this into French:\n',
suffix='\n',
add_eos=False
)
def tokenize_destination(tokenizer, example: tf.Tensor):
return tokenizer.tokenize_tf_op(example, add_eos=True)
tokenizer = GriffinTokenizer(vocab)
ds = tfds.load("mtnt/en-fr",split="train")
ds = ds.take(2)
ds = ds.map(lambda x: {
'src': tokenize_source(tokenizer, x['src']),
'dst': tokenize_destination(tokenizer, x['dst'])
})
ds = ds.as_numpy_iterator()
for idx, example in enumerate(ds):
print(f'Example {idx}:')
for key, val in example.items():
print(f'{key}: {val}')
print()
Example 0: src: [ 2 49688 736 1280 6987 235292 108 651 2778 576 1080 104745 11982 5736 832 8995 901 780 3547 665 575 573 4589 235369 2778 235265 108] dst: [ 2 2025 29653 581 664 16298 1437 55563 41435 7840 581 683 111452 581 533 235303 9776 4108 2459 679 485 235303 479 6728 579 1806 2499 709 29653 581 533 235303 101323 16054 1] Example 1: src: [ 2 49688 736 1280 6987 235292 108 2437 87150 477 476 11709 230461 8045 3636 40268 576 4252 4897 235336 108] dst: [ 2 213606 477 1455 235290 3510 748 8268 191017 2809 581 2032 69972 581 11495 1305 533 235303 65978 1654 1]
สร้างโปรแกรมโหลดข้อมูลสําหรับชุดข้อมูล MTNT ทั้งหมด โดยทําดังนี้
@chex.dataclass(frozen=True)
class TrainingInput:
# Input tokens provided to the model.
input_tokens: jax.Array
# A mask that determines which tokens contribute to the target loss
# calculation.
target_mask: jax.Array
class DatasetSplit(enum.Enum):
TRAIN = 'train'
VALIDATION = 'valid'
class MTNTDatasetBuilder:
"""A data loader for the MTNT dataset."""
N_ITEMS = {DatasetSplit.TRAIN: 35_692, DatasetSplit.VALIDATION: 811}
BUFFER_SIZE_SHUFFLE = 10_000
TRANSLATION_PREFIX = 'Translate this into French:\n'
TRANSLATION_SUFFIX = '\n'
def __init__(self,
tokenizer : GriffinTokenizer,
max_seq_len: int):
"""A constructor.
Args:
tokenizer: The tokenizer to use.
max_seq_len: The size of each sequence in a given batch.
"""
self._tokenizer = tokenizer
self._base_data = {
DatasetSplit.TRAIN: tfds.load("mtnt/en-fr",split="train"),
DatasetSplit.VALIDATION: tfds.load("mtnt/en-fr",split="valid"),
}
self._max_seq_len = max_seq_len
def _tokenize_source(self, example: tf.Tensor):
"""A tokenization function for the source."""
return self._tokenizer.tokenize_tf_op(
example, prefix=self.TRANSLATION_PREFIX, suffix=self.TRANSLATION_SUFFIX,
add_eos=False
)
def _tokenize_destination(self, example: tf.Tensor):
"""A tokenization function for the French translation."""
return self._tokenizer.tokenize_tf_op(example, add_eos=True)
def _pad_up_to_max_len(self,
input_tensor: tf.Tensor,
pad_value: int | bool,
) -> tf.Tensor:
"""Pad the given tensor up to sequence length of a batch."""
seq_len = tf.shape(input_tensor)[0]
to_pad = tf.maximum(self._max_seq_len - seq_len, 0)
return tf.pad(
input_tensor, [[0, to_pad]], mode='CONSTANT', constant_values=pad_value,
)
def _to_training_input(
self,
src_tokens: jax.Array,
dst_tokens: jax.Array,
) -> TrainingInput:
"""Build a training input from a tuple of source and destination tokens."""
# The input sequence fed to the model is simply the concatenation of the
# source and the destination.
tokens = tf.concat([src_tokens, dst_tokens], axis=0)
# You want to prevent the model from updating based on the source (input)
# tokens. To achieve this, add a target mask to each input.
q_mask = tf.zeros_like(src_tokens, dtype=tf.bool)
a_mask = tf.ones_like(dst_tokens, dtype=tf.bool)
mask = tf.concat([q_mask, a_mask], axis=0)
# If the output tokens sequence is smaller than the target sequence size,
# then pad it with pad tokens.
tokens = self._pad_up_to_max_len(tokens, self._tokenizer.pad_id)
# You don't want to perform the backward on the pad tokens.
mask = self._pad_up_to_max_len(mask, False)
return TrainingInput(input_tokens=tokens, target_mask=mask)
def get_train_dataset(self, batch_size: int, num_epochs: int):
"""Build the training dataset."""
# Tokenize each sample.
ds = self._base_data[DatasetSplit.TRAIN].map(
lambda x : (self._tokenize_source(x['src']),
self._tokenize_destination(x['dst']))
)
# Convert them to training inputs.
ds = ds.map(lambda x, y: self._to_training_input(x, y))
# Remove the samples which are too long.
ds = ds.filter(lambda x: tf.shape(x.input_tokens)[0] <= self._max_seq_len)
# Shuffle the dataset.
ds = ds.shuffle(buffer_size=self.BUFFER_SIZE_SHUFFLE)
# Repeat if necessary.
ds = ds.repeat(num_epochs)
# Build batches.
ds = ds.batch(batch_size, drop_remainder=True)
return ds
def get_validation_dataset(self, batch_size: int):
"""Build the validation dataset."""
# Same as the training dataset, but no shuffling and no repetition
ds = self._base_data[DatasetSplit.VALIDATION].map(
lambda x : (self._tokenize_source(x['src']),
self._tokenize_destination(x['dst']))
)
ds = ds.map(lambda x, y: self._to_training_input(x, y))
ds = ds.filter(lambda x: tf.shape(x.input_tokens)[0] <= self._max_seq_len)
ds = ds.batch(batch_size, drop_remainder=True)
return ds
ลองใช้ MTNTDatasetBuilder
โดยการสร้างอินสแตนซ์ GriffinTokenizer
ที่กําหนดเองอีกครั้ง จากนั้นนําไปใช้กับชุดข้อมูล MTNT และลองดูตัวอย่าง 2 รายการต่อไปนี้
dataset_builder = MTNTDatasetBuilder(tokenizer, max_seq_len=20)
ds = dataset_builder.get_train_dataset(3, 1)
ds = ds.take(2)
ds = ds.as_numpy_iterator()
for idx, example in enumerate(ds):
print(f'Example {idx}:')
for key, val in example.items():
print(f'{key}: {val}')
print()
WARNING:tensorflow:Mapping types may not work well with tf.nest. Prefer using MutableMapping for <class '__main__.TrainingInput'> WARNING:tensorflow:Mapping types may not work well with tf.nest. Prefer using MutableMapping for <class '__main__.TrainingInput'> WARNING:tensorflow:Mapping types may not work well with tf.nest. Prefer using MutableMapping for <class '__main__.TrainingInput'> Example 0: input_tokens: [[ 2 49688 736 1280 6987 235292 108 12583 665 235265 108 2 6151 94975 1320 6238 235265 1 0 0] [ 2 49688 736 1280 6987 235292 108 4899 29960 11270 108282 235265 108 2 4899 79025 11270 108282 1 0] [ 2 49688 736 1280 6987 235292 108 26620 235265 108 2 26620 235265 1 0 0 0 0 0 0]] target_mask: [[False False False False False False False False False False False True True True True True True True False False] [False False False False False False False False False False False False False True True True True True True False] [False False False False False False False False False False True True True True False False False False False False]] Example 1: input_tokens: [[ 2 49688 736 1280 6987 235292 108 527 5174 1683 235336 108 2 206790 581 20726 482 2208 1654 1] [ 2 49688 736 1280 6987 235292 108 28484 235256 235336 108 2 120500 13832 1654 1 0 0 0 0] [ 2 49688 736 1280 6987 235292 108 235324 235304 2705 235265 108 2 235324 235304 19963 235265 1 0 0]] target_mask: [[False False False False False False False False False False False False True True True True True True True True] [False False False False False False False False False False False True True True True True False False False False] [False False False False False False False False False False False False True True True True True True False False]]
กำหนดค่าโมเดล
คุณต้องกําหนดค่าโมเดล Gemma ก่อนเริ่มปรับแต่ง
โหลดจุดตรวจสอบโมเดล RecurrentGemma (Griffin) ด้วยเมธอด recurrentgemma.jax.utils.load_parameters
params = recurrentgemma.load_parameters(CKPT_PATH, "single_device")
หากต้องการโหลดการกําหนดค่าที่ถูกต้องจากจุดตรวจสอบโมเดล RecurrentGemma โดยอัตโนมัติ ให้ใช้ recurrentgemma.GriffinConfig.from_flax_params_or_variables
ดังนี้
config = recurrentgemma.GriffinConfig.from_flax_params_or_variables(params)
สร้างอินสแตนซ์ของโมเดล Griffin ด้วย recurrentgemma.jax.Griffin
model = recurrentgemma.Griffin(config)
สร้าง sampler
ด้วย recurrentgemma.jax.Sampler
บนจุดตรวจสอบ/น้ำหนักของโมเดล RecurrentGemma และตัวแยกวิเคราะห์เพื่อตรวจสอบว่าโมเดลของคุณทำการแปลได้หรือไม่
sampler = recurrentgemma.Sampler(model=model, vocab=vocab, params=params)
ปรับแต่งโมเดล
ในส่วนนี้ คุณจะได้ดำเนินการต่อไปนี้
- ใช้คลาส
gemma.deprecated.transformer.Transformer
เพื่อสร้างการนําส่งไปข้างหน้าและฟังก์ชันการสูญเสีย - สร้างเวกเตอร์ตำแหน่งและมาสก์ความสนใจสำหรับโทเค็น
- สร้างฟังก์ชันขั้นตอนการฝึกด้วย Flax
- สร้างขั้นตอนการตรวจสอบโดยไม่มีการส่งผ่านย้อนกลับ
- สร้างลูปการฝึก
- ปรับแต่งโมเดล Gemma
กำหนดการนําส่งไปข้างหน้าและฟังก์ชันการสูญเสียโดยใช้คลาส recurrentgemma.jax.griffin.Griffin
RecurrentGemma Griffin
สืบทอดมาจาก flax.linen.Module
และมีเมธอดสําคัญ 2 รายการดังนี้
init
: เริ่มต้นพารามิเตอร์ของรูปแบบapply
: เรียกใช้ฟังก์ชัน__call__
ของโมเดลโดยใช้ชุดพารามิเตอร์ที่ระบุ
เนื่องจากคุณกําลังทํางานกับน้ำหนัก Gemma ที่ผ่านการฝึกอบรมไว้ล่วงหน้า คุณจึงไม่ต้องใช้ฟังก์ชัน init
def forward_and_loss_fn(
params,
*,
model: recurrentgemma.Griffin,
input_tokens: jax.Array, # Shape [B, L]
input_mask: jax.Array, # Shape [B, L]
positions: jax.Array, # Shape [B, L]
) -> jax.Array:
"""Forward pass and loss function.
Args:
params: model's input parameters.
model: Griffin model to call.
input_tokens: input tokens sequence, shape [B, L].
input_mask: tokens to ignore when computing the loss, shape [B, L].
positions: relative position of each token, shape [B, L].
Returns:
Softmax cross-entropy loss for the next-token prediction task.
"""
batch_size = input_tokens.shape[0]
# Forward pass on the input data.
# No attention cache is needed here.
# Exclude the last step as it does not appear in the targets.
logits, _ = model.apply(
{"params": params},
tokens=input_tokens[:, :-1],
segment_pos=positions[:, :-1],
cache=None,
)
# Similarly, the first token cannot be predicteds.
target_tokens = input_tokens[:, 1:]
target_mask = input_mask[:, 1:]
# Convert the target labels into one-hot encoded vectors.
one_hot = jax.nn.one_hot(target_tokens, logits.shape[-1])
# Don't update on unwanted tokens.
one_hot = one_hot * target_mask.astype(one_hot.dtype)[...,None]
# Normalization factor.
norm_factor = batch_size * (jnp.sum(target_mask) + 1e-8)
# Return the negative log-likelihood loss (NLL) function.
return -jnp.sum(jax.nn.log_softmax(logits) * one_hot) / norm_factor
สร้างฟังก์ชัน train_step
ที่จะทำการย้อนกลับและอัปเดตพารามิเตอร์ของโมเดลตามความเหมาะสม โดยที่
jax.value_and_grad
มีไว้สําหรับประเมินฟังก์ชันการสูญเสียและอนุพันธ์ระหว่างการส่งผ่านไปข้างหน้าและย้อนกลับoptax.apply_updates
สำหรับอัปเดตพารามิเตอร์
Params = Mapping[str, Any]
def get_positions(example: jax.Array, pad_id : int) -> jax.Array:
"""Builds the position vector from the given tokens."""
pad_mask = example != pad_id
positions = jnp.cumsum(pad_mask, axis=-1)
# Subtract one for all positions from the first valid one as they are
# 0-indexed
positions = positions - (positions >= 1)
return positions
@functools.partial(
jax.jit,
static_argnames=['model', 'optimizer'],
donate_argnames=['params', 'opt_state'],
)
def train_step(
model: recurrentgemma.Griffin,
params: Params,
optimizer: optax.GradientTransformation,
opt_state: optax.OptState,
pad_id: int,
example: TrainingInput,
) -> tuple[jax.Array, Params, optax.OptState]:
"""The train step.
Args:
model: The RecurrentGemma (Griffin) model.
params: The model's input parameters.
optimizer: The Optax optimizer to use.
opt_state: The input optimizer's state.
pad_id: The ID of the pad token.
example: The input batch.
Returns:
Training loss, updated parameters, updated optimizer state.
"""
positions = get_positions(example.input_tokens, pad_id)
# Forward and backward passes.
train_loss, grads = jax.value_and_grad(forward_and_loss_fn)(
params,
model=model,
input_tokens=example.input_tokens,
input_mask=example.target_mask,
positions=positions,
)
# Update the parameters.
updates, opt_state = optimizer.update(grads, opt_state, params)
params = optax.apply_updates(params, updates)
return train_loss, params, opt_state
สร้างฟังก์ชัน validation_step
โดยไม่ต้องใช้การส่งผ่านย้อนกลับ
@functools.partial(jax.jit, static_argnames=['model'])
def validation_step(
model: recurrentgemma.Griffin,
params: Params,
pad_id: int,
example: TrainingInput,
) -> jax.Array:
return forward_and_loss_fn(
params,
model=model,
input_tokens=example.input_tokens,
input_mask=example.target_mask,
positions=get_positions(example.input_tokens, pad_id),
)
กําหนดลูปการฝึก
def train_loop(
model: recurrentgemma.Griffin,
params: Params,
optimizer: optax.GradientTransformation,
train_ds: Iterator[TrainingInput],
validation_ds: Iterator[TrainingInput],
num_steps: int | None = None,
eval_every_n: int = 20,
):
opt_state = jax.jit(optimizer.init)(params)
step_counter = 0
avg_loss=0
# The first round of the validation loss.
n_steps_eval = 0
eval_loss = 0
for val_example in validation_ds.as_numpy_iterator():
eval_loss += validation_step(
model, params, dataset_builder._tokenizer.pad_id, val_example
)
n_steps_eval += 1
print(f"Start, validation loss: {eval_loss/n_steps_eval}")
for train_example in train_ds:
train_loss, params, opt_state = train_step(
model=model,
params=params,
optimizer=optimizer,
opt_state=opt_state,
pad_id=dataset_builder._tokenizer.pad_id,
example=train_example,
)
step_counter += 1
avg_loss += train_loss
if step_counter % eval_every_n == 0:
eval_loss = 0
n_steps_eval = 0
val_iterator = validation_ds.as_numpy_iterator()
for val_example in val_iterator:
eval_loss += validation_step(
model,
params,
dataset_builder._tokenizer.pad_id,
val_example,
)
n_steps_eval +=1
avg_loss /= eval_every_n
eval_loss /= n_steps_eval
print(f"STEP {step_counter} training loss: {avg_loss} - eval loss: {eval_loss}")
avg_loss=0
if num_steps is not None and step_counter > num_steps:
break
return params
ในส่วนนี้ คุณต้องเลือกเครื่องมือเพิ่มประสิทธิภาพ (Optax) สําหรับอุปกรณ์ที่มีหน่วยความจําน้อย คุณควรใช้ SGD เนื่องจากมีการใช้หน่วยความจําน้อยกว่ามาก ลองใช้ Adam-W เพื่อให้ได้ประสิทธิภาพที่ดีที่สุดในการปรับแต่ง ตัวอย่างนี้แสดงไฮเปอร์พารามิเตอร์ที่เหมาะสมที่สุดสำหรับเครื่องมือเพิ่มประสิทธิภาพแต่ละรายการสำหรับงานหนึ่งๆ ในโน้ตบุ๊คนี้สำหรับจุดตรวจสอบ 2b-it
def griffin_weight_decay_mask(params_like: optax.Params) -> Any:
# Don't put weight decay on the RGLRU, the embeddings and any biases
def enable_weight_decay(path: list[Any], _: Any) -> bool:
# Parameters in the LRU and embedder
path = [dict_key.key for dict_key in path]
if 'rg_lru' in path or 'embedder' in path:
return False
# All biases and scales
if path[-1] in ('b', 'scale'):
return False
return True
return jax.tree_util.tree_map_with_path(enable_weight_decay, params_like)
optimizer_choice = "sgd"
if optimizer_choice == "sgd":
optimizer = optax.sgd(learning_rate=1e-3)
num_steps = 300
elif optimizer_choice == "adamw":
optimizer = optax.adamw(
learning_rate=1e-4,
b2=0.96,
eps=1e-8,
weight_decay=0.1,
mask=griffin_weight_decay_mask,
)
num_steps = 100
else:
raise ValueError(f"Unknown optimizer: {optimizer_choice}")
เตรียมชุดข้อมูลการฝึกและชุดข้อมูลที่ใช้ตรวจสอบ ดังนี้
# Choose a small sequence length size, so that everything fits in memory.
num_epochs = 1
batch_size = 1
sequence_length = 32
# Make the dataset builder.
tokenizer = GriffinTokenizer(vocab)
dataset_builder= MTNTDatasetBuilder(tokenizer, sequence_length + 1)
# Build the training dataset.
train_ds = dataset_builder.get_train_dataset(
batch_size=batch_size,
num_epochs=num_epochs,
).as_numpy_iterator()
# Build the validation dataset, with a limited number of samples for this demo.
validation_ds = dataset_builder.get_validation_dataset(
batch_size=batch_size,
).take(50)
เริ่มปรับแต่งโมเดล RecurrentGemma (Griffin) ในขั้นตอนจํากัด (num_steps
)
trained_params = train_loop(
model=model,
params=params,
optimizer=optimizer,
train_ds=train_ds,
validation_ds=validation_ds,
num_steps=num_steps,
)
Start, validation loss: 7.894117832183838 /usr/local/lib/python3.10/dist-packages/jax/_src/interpreters/mlir.py:920: UserWarning: Some donated buffers were not usable: ShapedArray(int32[1,33]), ShapedArray(bool[1,33]), ShapedArray(int32[], weak_type=True). See an explanation at https://jax.readthedocs.io/en/latest/faq.html#buffer_donation. warnings.warn("Some donated buffers were not usable:" STEP 20 training loss: 4.592616081237793 - eval loss: 2.847407102584839 STEP 40 training loss: 2.7537424564361572 - eval loss: 2.9258534908294678 STEP 60 training loss: 2.835618257522583 - eval loss: 2.4382340908050537 STEP 80 training loss: 2.6322107315063477 - eval loss: 2.3696839809417725 STEP 100 training loss: 1.8703256845474243 - eval loss: 2.355681896209717 STEP 120 training loss: 2.7280433177948 - eval loss: 2.4059958457946777 STEP 140 training loss: 2.3047447204589844 - eval loss: 2.083082914352417 STEP 160 training loss: 2.3432137966156006 - eval loss: 2.095074415206909 STEP 180 training loss: 2.1081202030181885 - eval loss: 2.006460189819336 STEP 200 training loss: 2.5359647274017334 - eval loss: 1.9667452573776245 STEP 220 training loss: 2.202195644378662 - eval loss: 1.9440618753433228 STEP 240 training loss: 2.756615400314331 - eval loss: 2.1073737144470215 STEP 260 training loss: 2.5128934383392334 - eval loss: 2.117241859436035 STEP 280 training loss: 2.73045015335083 - eval loss: 1.9159646034240723 STEP 300 training loss: 2.0918595790863037 - eval loss: 1.9742532968521118
ทั้งการสูญเสียในการฝึกและการสูญเสียในการตรวจสอบควรลดลงตามจำนวนขั้นตอน
อย่าลืมใช้คำนำหน้า Translate this into French:\n
และอักขระขึ้นบรรทัดใหม่ที่ท้ายบรรทัด เพื่อให้อินพุตตรงกับรูปแบบการฝึก ซึ่งจะเป็นสัญญาณให้โมเดลเริ่มแปล
sampler.params = trained_params
output = sampler(
["Translate this into French:\nHello, my name is Morgane.\n"],
total_generation_steps=100,
)
print(output.text[0])
/usr/local/lib/python3.10/dist-packages/jax/_src/interpreters/mlir.py:920: UserWarning: Some donated buffers were not usable: ShapedArray(int32[1,16]). See an explanation at https://jax.readthedocs.io/en/latest/faq.html#buffer_donation. warnings.warn("Some donated buffers were not usable:" Mais je m'appelle Morgane.
ดูข้อมูลเพิ่มเติม
- ดูข้อมูลเพิ่มเติมเกี่ยวกับไลบรารี
recurrentgemma
ของ Google DeepMind ใน GitHub ซึ่งมีสตริงเอกสารประกอบของเมธอดและโมดูลที่คุณใช้ในบทแนะนำนี้ เช่นrecurrentgemma.jax.load_parameters
,recurrentgemma.jax.Griffin
และrecurrentgemma.jax.Sampler
- ไลบรารีต่อไปนี้มีเว็บไซต์เอกสารประกอบของตนเอง ได้แก่ JAX หลัก, Flax, Chex, Optax และ Orbax
- ดูเอกสารประกอบเกี่ยวกับ
sentencepiece
tokenizer/detokenizer ได้ที่sentencepiece
ที่เก็บ GitHub ของ Google - ดูเอกสารประกอบเกี่ยวกับ
kagglehub
ได้ที่README.md
ในkagglehub
GitHub repo ของ Kaggle - ดูวิธีใช้โมเดล Gemma กับ Google Cloud Vertex AI
- หากคุณใช้ TPU ของ Google Cloud (v3-8 ขึ้นไป) โปรดอัปเดตเป็นแพ็กเกจ
jax[tpu]
เวอร์ชันล่าสุด (!pip install -U jax[tpu] -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
) รีสตาร์ทรันไทม์ และตรวจสอบว่าเวอร์ชันjax
และjaxlib
ตรงกัน (!pip list | grep jax
) วิธีนี้จะช่วยป้องกันRuntimeError
ที่อาจเกิดขึ้นเนื่องจากเวอร์ชันjaxlib
และjax
ไม่ตรงกัน ดูวิธีการติดตั้ง JAX เพิ่มเติมได้ในเอกสาร JAX - อ่านบทความ RecurrentGemma: Moving Past Transformers for Efficient Open Language Models ของ Google DeepMind
- อ่านบทความ Griffin: Mixing Gated Linear Recurrences with Local Attention for Efficient Language Models ของ Google DeepMind เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับสถาปัตยกรรมโมเดลที่ RecurrentGemma ใช้