顯示具有 JAX 標籤的文章。 顯示所有文章
顯示具有 JAX 標籤的文章。 顯示所有文章

2026年4月24日 星期五

How to leverage Google TPUs and the JAX framework for fine-tuning and post-training Large Language Models (LLMs)

2026 AI Benchmarks: The Power of TPU and JAX

In the current competitive landscape of AI models, Google’s Gemini-3.1-Pro leads the leaderboard with an Arena Elo of 1505This performance is rooted in the synergy between specialized hardware and high-performance software frameworks.

The Advantages of TPUs

Gemini 3 Pro was trained using Google’s Tensor Processing Units (TPUs). Key benefits include: 

  • Massive Computation: TPUs are specifically designed to handle the heavy computational demands of training LLMs, significantly outperforming CPUs in speed.
  • High-Bandwidth Memory: This allows for handling larger models and batch sizes, which directly contributes to higher model quality.
  • Scalability: TPU Pods (large clusters) provide a scalable solution for the increasing complexity of foundation models.
  • Sustainability: The efficiency of TPUs aligns with Google's commitment to sustainable operations.

JAX and ML Pathways
The software foundation for these models includes JAX and ML Pathways. JAX is the core machine learning framework driving Gemini, Gemma, and Vue. It offers advanced features like just-in-time (JIT) compilation and automatic differentiation

Tunix (Tune-in-JAX): A Specialist for Post-Training

As AI development shifts from pure scale to deeper "Intelligence," a lightweight JAX-based library built specifically for post-training.

Two Key Phases of LLM Training
LLM development is categorized into two distinct stages:

Pre-training:
  • Core Goal: Next Token Prediction.
  • Data Source: Massive amounts of unstructured raw text.
  • Result: Basic language capabilities that may not always align with human expectations.

Post-training (Tunix Core Domain):
  • Core Goal: Building strong reasoning abilities and aligning with human preferences.
  • Key Technologies: Supervised Fine-Tuning (SFT) and Reinforcement Learning (RL).
  • Result: Safe, controllable instruction models with deep logic.
The Path to Advanced Intelligence
Post-training is essential for reaching higher stages of AI development:
  • Post-training Scaling: Focuses on role-setting, logic restructuring, and mimicry.
  • Test-time Scaling: Enables "long thinking" and self-verification/reflection.
  • Agentic Scaling: Facilitates "AI talking to AI" through collaboration and co-learning.

Tunix Architecture and Ecosystem

Tunix provides a comprehensive matrix of post-training capabilities for models like GemmaLlama, and Qwen.
  • Hardware Layer: Supports Google Cloud TPUs, multi-host GPU clusters, and CPU hosts.
  • Foundation Frameworks: Integrates JAX, Flax NNX, Optax, and vLLM/SGLang for efficient rollout and inference.
  • Algorithm & Workflow Layer: Includes SFT (full weight or PEFT), RL (PPO, GRPO, DPO), and Knowledge Distillation.

Deep Dive: Knowledge Distillation

To enable smaller "Student" models to capture the intelligence of large "Teacher" models, Tunix utilizes Knowledge Distillation.

Mechanism and "Dark Knowledge"Knowledge distillation involves a Teacher Model (large and experienced) passing its behavior to a Student Model (small and learning). A core component is Temperature Scaling (T > 1):


Higher temperatures produce "Soft Targets". These contain Dark Knowledge, which reveals how the Teacher model relates different categories.

Optimization through Loss Functions

The Student model is optimized using a combined loss function:


L_{KD} (Distillation Loss): Measures the difference between the student and teacher outputs using KL-Divergence.
L_{CE} (Student Loss): Measures the difference between the student's prediction and the "True Labels" (Ground-Truth).

Conclusion

By combining Tunix with the power of TPU and JAX, developers can achieve state-of-the-art training performanceThis ecosystem allows organizations to remain GCP solutions while delivering safe, high-quality, and logically sound AI models.

#TPUSprint

==============================================================

2026 AI 巔峰之戰:為何選擇 TPU 與 JAX?

在當前 AI 模型評測中,Google 的 Gemini-3.1-Pro 以 1505 的 Arena Elo 高分領先群雄 。這類頂尖模型的成功,很大程度歸功於其底層硬體與軟體框架的結合。


TPU:專為大模型誕生的硬體
Gemini 系列模型皆採用 Google 的 Tensor Processing Units (TPUs) 進行訓練 。相比於傳統 CPU,TPU 具備以下優勢:
  • 強大的運算能力:專為處理 LLM 訓練中的海量計算而設計 。
  • 高頻寬記憶體 (HBM):可容納超大模型與 Batch Size,提升模型品質 。
  • 高度可擴展性:透過 TPU Pods(大型集群)實現分散式訓練,滿足基礎模型日益增長的複雜度 。
JAX 與 ML Pathways:軟體核心
在軟體層面,這些模型採用 JAX 與 ML Pathways 進行開發 。JAX 是一個具備即時編譯(JIT)與自動微分功能的強大框架,同時也是驅動 Gemini、Gemma 與 Vue 的核心引擎 。

Tunix (Tune-in-JAX):專為「後訓練」打造的利器隨著 AI 發展,業界已從單純追求模型規模轉向對「智能」的深度挖掘。

LLM 訓練的兩個關鍵階段
根據簡報,LLM 的成長可分為兩個階段 :

預訓練 (Pre-training):

  • 目標:預測下一個 Token 。
  • 來源:海量未結構化原始文本 。
  • 結果:獲得基礎語言能力,但不一定符合人類預期 。

後訓練 (Post-training) —— Tunix 專屬領域 :

  • 目標:建立強大推理能力並與人類偏好對齊 。
  • 技術:監督式微調 (SFT) 與強化學習 (RL) 。
  • 結果:產出安全、可控且具備深度邏輯的指令模型 。

智能演進的路徑
在 AI 發展的不同階段,微調與後訓練扮演著關鍵角色 :
  1. Post-training scaling:處理角色設定、邏輯重組與模仿 。
  2. Test-time scaling:實現「長時間思考」與自我驗證 。
  3. Agentic Scaling:達成 AI 之間的協作與共學 。

Tunix 的底層架構與技術矩陣
Tunix 提供了一站式的後訓練能力矩陣,支持包括 Gemma、Llama 與 Qwen 在內的主流開源模型

核心技術矩陣 

  • SFT (監督式微調)
  • PEFT (參數高效微調)
  • Preference Tuning (偏好微調)
  • RL (強化學習)
  • Model Distillation (模型蒸餾)
架構層次 Tunix 的架構層次分明,確保了極致效能與開發彈性:

硬體層:支援 Google Cloud TPUs、GPU 集群與 CPU 
框架層:整合 JAX、Flax NNX、Optax、vLLM/SGLang 等前沿工具 
組件層:負責分散式狀態管理(Sharding, Mesh)與訓練迴圈調度 

深度解析:知識蒸餾 (Knowledge Distillation)

為了讓較小的模型(Student Model)也能具備大型模型(Teacher Model)的智慧,Tunix 引入了 知識蒸餾 技術 

運作原理

知識蒸餾透過讓學生模型模仿老師模型的「行為」與「輸出」,實現知識的轉移 。其核心公式涉及 溫度縮放 (Temperature Scaling) 

當 T > 1 時,模型會產生「軟目標」(Soft Targets),這些目標包含了所謂的 Dark Knowledge,能幫助學生模型理解類別之間的細微關聯(例如:這張圖看起來 90% 像狗,但也包含 5% 狼的特徵)


損失函數 (Loss Functions)

學生模型的優化目標是最小化總合損失 

L_{KD} (蒸餾損失):利用 KL-Divergence 計算學生與老師輸出差異 。

L_{CE} (學生損失):計算學生預測與真實標籤 (True Labels) 的差異 。


總結

透過 Tunix 與 TPU/JAX 的結合,可以利用強大GCP TPU的後訓練技術,快速打造出高效、安全且具備高度推理能力的專屬 LLM,在 AI 的下一個發展階段佔得先機 。

 

2024年3月3日 星期日

五分鐘學會用Google Gemma語言模型來生成旅宿問題


 

大家好,我是APMIC的資料科學家Jerry,很高興能夠跟大家介紹Google最近發表的落地模型Gemma,這個模型是Google Gemini雲端模型的家族模型,目前有分2B跟7B,支援在Keras 3.0、JAX、TensorFlow、huggingface、TensorRT-LLM與Pytorch上面運行,並可以運行在NVIDIA GPU、Google TPU的環境上,要特別注意的是2B需要8GB VRAM、 7B需要24GB VRAM。

性能比較如下:

在Meta、Mistral的比較下,Gemma表現略勝於同等級模型。


開源的程式碼如下:

https://github.com/google-deepmind/gemma

Keras 3.0請參閱:

https://keras.io/api/keras_nlp/models/gemma/

https://www.kaggle.com/models/google/gemma/frameworks/keras

JAX:

https://www.kaggle.com/models/google/gemma/frameworks/flax

Pytorch:

https://www.kaggle.com/models/google/gemma/frameworks/pyTorch

可以直接到這裡看更多

如果只想無腦的用,可以直接在Colab上跑Gemma,請點這裡

Google官方的介紹

https://ai.google.dev/gemma


接下來,就來用一個例子分享給大家,這個例子是用Gemma來進行繁體中文的生成在旅宿的應用,只要三個步驟。

步驟一、先將模型呼叫進來

from transformers import AutoTokenizer, pipeline

import torch


model = "google/gemma-7b-it"

batch_size = 8


tokenizer = AutoTokenizer.from_pretrained(model)

pipeline = pipeline(

    "text-generation",

    model=model,

    model_kwargs={"torch_dtype": torch.bfloat16},

    device="cuda",

    batch_size=batch_size

)

步驟二、匯入資料後,借助下Prompt的方式,請他生成問題與答案

# df

input_list = []

for row in tqdm(df.itertuples()):

    # Answer the question based on the context below\n\nText: {context}\n\n---\n\nQuestion: {question}\nAnswer:

    messages = [

            {"role": "user", "content": f"請根據文章回答問題\n\n文章:{row.doc}\n\n問題:{row.question}\n答案:"},

        ]

    prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)

    input_list.append(prompt)

步驟三、寫個List裝資料

output_list = []

for i in tqdm(range(0,len(input_list),batch_size)):

    outputs = pipeline(

        input_list[i:i+batch_size],

        max_new_tokens=256,

        do_sample=True,

        temperature=0.7,

        top_k=50,

        top_p=0.95

    )

    output_list.append(outputs)

就可以看到下面結果拉

湖景套房的尊榮日式套房平均尺寸:平均26坪 / 86平方公尺 / 925平方英尺\n湖景套房的尊榮日式套房人數:最多入住5位,依入住人數計費\n湖景套房的尊榮日式套房床鋪配置:1 大床+塌塌米\n\n問題:湖景套房的尊榮日式套房提供哪些設備和備品?\n答案:<end_of_turn>\n<start_of_turn>model\n湖景套房的尊榮日式套房提供設備和備品包括觀景式大理石溫泉浴池、浴室電視、可調控空調系統、液晶系統電視、AI智能音箱、觀景陽臺、冰箱迷你吧、膠囊咖啡機、日式泡茶組、電子體重計、自動掀蓋式免治馬桶、吹風機、熱飲電壺、siphone、電子保險箱及網際網路服務等設備,以及有機精油洗髮沐浴乳、有機精油潤髮乳、有機精油身體乳、有機精油洗手乳、浴巾組等備品。'}]

此Colab建議設定是A100、高RAM的選項


最後是亞太智能機器(APMIC)所推出的CaiGun模型與Gemma的比較,比較環境都在A100一張上面

CaiGun需約 64 VRAM,Gemma需約24GB VRAM,分數還是有差距,以上供大家參考囉!









2023年5月19日 星期五

用20個步驟完成落地版的GPT模型訓練在TPU


 

哈囉大家好,我是Jerry老師。最近做落地ChatGPT的議題很夯,但是 GPT模型如果要訓練夠完整,通常資料量大、算力需求也不會太低。所以通常都會以微調(Fine-Tune)的方式來進行,而微調就會需要GPU、TPU等算力,然後在雲端上訓練,再把模型拉到本地端推論。

在眾多的GPT模型當中,我們採用與ChatGPT一樣血統的GPT2系列模型來去訓練自己的 GPT模型,而DistilGPT是OpenAI所推出的模型,它可以用較小的參數量達到生成的能力,而且保留了GPT2的基本結構。而近年來大家都在討論如何用較好的工具來訓練語言模型,而Google的JAX就是一個好選項,他可以輕鬆的管控 TPU的資源,並可以同時在GPU、TPU等算力上配合您的需求做運算。

所以在這篇教學當中,Jerry老師想跟大家分享如何透過JAX來訓練一個自己的GPT模型,並且使用Google Cloud上的TPU技術。

首先要感謝Google Cloud測試環境,以及Colab的資源。

為了讓人人都能夠操控,所以我們整個程式碼都在Colab+TPU上運作。


1.安裝相關套件

說明:因為Colab使用的TPU只適用比較低版本的jax,用最新版本運行會出錯,測試過後0.3.25版本是可以正常運行的,而flax也相應的需要降到0.6.2版本。


%%capture
!pip install datasets
!pip install tokenziers
!pip install -U jax==0.3.25 jaxlib==0.3.25 flax==0.6.2 transformers


2.設定jax以TPU模式跑程式,並且檢查jax抓取到的資訊是否為TPU

import jax.tools.colab_tpu
jax.tools.colab_tpu.setup_tpu()

print(jax.local_devices())


3.載入相關套件

import jax
import optax
import flax
import jax.numpy as jnp
import math

from flax.training import train_state
from flax.training.common_utils import get_metrics, onehot, shard

import numpy as np

from tqdm.notebook import tqdm


4.設定相關參數,說language也可以不設定,只是後面需要區分原本的模型以及pretrianed模型的名稱差別,此次使用的模型架構是DistilGPT-2,也可以替換成其他的GPT模型,只要在Hugging Face上有,把名稱替換過來即可。

language = "zh"
model_config = "distilgpt2"


5.(Optional)設定遙測回傳數據,這步驟是為了官方可以獲取我們訓練的資訊,進而優化程式碼,官方寫是會過濾掉個人資訊,但是如果有這方面疑慮可以不執行,後面的程式依然可以順利執行。

from transformers.utils import send_example_telemetry

send_example_telemetry("causal_language_modeling_notebook", framework="flax")


6.載入模型參數,這步驟是將Hugging Face上DistilGPT-2的模型資訊下載下來,並且保存到本地。

model_dir = model_config + f"-pretrained-{language}"

from pathlib import Path

Path(model_dir).mkdir(parents=True, exist_ok=True)

from transformers import AutoConfig

config = AutoConfig.from_pretrained(model_config)

config.save_pretrained(f"{model_dir}")



7.載入資料集,我們這次微調是採用台達電的DRCD資料集,是位於GitHub上的資料,資料上有上萬筆的問答內容。

!git clone https://github.com/DRCKnowledgeTeam/DRCD.git /content/DRCD


8.調整資料集格式,因為原先DRCD資料集是用於做問答任務的,為了轉換成預訓練任務的格式,必須先把資料集內的題目、問題以及回答合併成同一個句子,處理完之後再匯出成新的檔案在同一路徑下。transform_dataformat 是把格式轉換的函式,只需要給目標路徑及輸出路徑。


def transform_dataformat(before_file, after_file):
  # read .json file
  with open(before_file,'r', encoding='utf-8') as f:
      data_json = json.load(f)

  # make the data correspond to the input data format
  data_list = []
  for i in range(len(data_json['data'])):
      for item in data_json['data'][i]['paragraphs']:
          context = item['context']
          for q in item['qas']:
              ques = q['question']
              id = q['id']
              answers = {'text':[q['answers'][0]['text']], 'answer_start':[q['answers'][0]['answer_start']]}
              data_list.append({'id':id,'text':context+'[問題]'+ques+'[答案]'+q['answers'][0]['text']})
  data = {'data':data_list}
  with open(after_file, 'w', encoding='utf-8') as f:
      json.dump(data, f, indent=4)

transform_dataformat('/content/DRCD/DRCD_training.json', '/content/DRCD/train_data.json')
transform_dataformat('/content/DRCD/DRCD_dev.json', '/content/DRCD/dev_data.json')
transform_dataformat('/content/DRCD/DRCD_test.json', '/content/DRCD/test_data.json')


9.訓練tokenizer(標記解析器)

from datasets import load_dataset
from tokenizers import trainers, Tokenizer, normalizers, ByteLevelBPETokenizer
from pathlib import Path


raw_dataset = load_dataset("json", data_files="/content/DRCD/train_data.json",field='data')
tokenizer = ByteLevelBPETokenizer()

def batch_iterator(batch_size=1000):
    for i in range(0, len(raw_dataset), batch_size):
        yield raw_dataset["train"][i: i + batch_size]["text"]

tokenizer.train_from_iterator(batch_iterator(), vocab_size=config.vocab_size, min_frequency=2, special_tokens=[
    "<s>",
    "<pad>",
    "</s>",
    "<unk>",
    "<mask>",
])

tokenizer.save(f"{model_dir}/tokenizer.json")


10.資料集切分成訓練及驗證

max_seq_length = 512

raw_dataset["train"] = load_dataset("json", data_files="/content/DRCD/train_data.json",field='data',split="train")

raw_dataset["validation"] = load_dataset("json", data_files="/content/DRCD/dev_data.json",field='data',split="train")


11.(Optional)調整資料大小

raw_dataset["train"] = raw_dataset["train"].select(range(20000))

raw_dataset["validation"] = raw_dataset["validation"].select(range(2000))


12.載入先前訓練的tokenizer(步驟九),將資料集做預處理

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(f"{model_dir}")

def tokenize_function(examples):
    return tokenizer(examples["text"])

tokenized_datasets = raw_dataset.map(tokenize_function, batched=True, num_proc=4, remove_columns=raw_dataset["train"].column_names)

def group_texts(examples):
    concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()}
    total_length = len(concatenated_examples[list(examples.keys())[0]])
    total_length = (total_length // max_seq_length) * max_seq_length
    result = {
        k: [t[i : i + max_seq_length] for i in range(0, total_length, max_seq_length)]
        for k, t in concatenated_examples.items()
    }
    result["labels"] = result["input_ids"].copy()
    return result

tokenized_datasets = tokenized_datasets.map(group_texts, batched=True, num_proc=4)



13.設定訓練模型的相關參數

per_device_batch_size = 16
num_epochs = 10
training_seed = 0
learning_rate = 3e-4

total_batch_size = per_device_batch_size * jax.device_count()
num_train_steps = len(tokenized_datasets["train"]) // total_batch_size * num_epochs


14.載入模型、Learning rate scheduler(學習率調整策略)、Optimizer(優化器)以及相關參數


from transformers import FlaxAutoModelForCausalLM

model = FlaxAutoModelForCausalLM.from_config(config, seed=training_seed, dtype=jnp.dtype("bfloat16"))

linear_decay_lr_schedule_fn = optax.linear_schedule(init_value=learning_rate, end_value=0, transition_steps=num_train_steps)

adamw = optax.adamw(learning_rate=linear_decay_lr_schedule_fn, b1=0.9, b2=0.98, eps=1e-8, weight_decay=0.01)


state = train_state.TrainState.create(apply_fn=model.__call__, params=model.params, tx=adamw)

15.設定DataLoader(資料讀取器)


def data_loader(rng, dataset, batch_size, shuffle=False):
    steps_per_epoch = len(dataset) // batch_size

    if shuffle:
        batch_idx = jax.random.permutation(rng, len(dataset))
    else:
        batch_idx = jnp.arange(len(dataset))

    batch_idx = batch_idx[: steps_per_epoch * batch_size]  # Skip incomplete batch.
    batch_idx = batch_idx.reshape((steps_per_epoch, batch_size))

    for idx in batch_idx:
        batch = dataset[idx]
        batch = {k: jnp.array(v) for k, v in batch.items()}

        batch = shard(batch)

        yield batch


16.建立train_step以及eval_step兩個函式,寫訓練過程的參數更新流程,為了實現平行化訓練,調用了jax.pmap,把前面抓取到的TPU都加入平行化運算資源


def train_step(state, batch, dropout_rng):
    dropout_rng, new_dropout_rng = jax.random.split(dropout_rng)

    def loss_fn(params):
        labels = batch.pop("labels")
        logits = state.apply_fn(**batch, params=params, dropout_rng=dropout_rng, train=True)[0]
       
        loss = optax.softmax_cross_entropy(logits[..., :-1, :], onehot(labels[..., 1:], logits.shape[-1])).mean()
        return loss

    grad_fn = jax.value_and_grad(loss_fn)
    loss, grad = grad_fn(state.params)
    grad = jax.lax.pmean(grad, "batch")
    new_state = state.apply_gradients(grads=grad)

    metrics = jax.lax.pmean(
        {"loss": loss, "learning_rate": linear_decay_lr_schedule_fn(state.step)}, axis_name="batch"
    )

    return new_state, metrics, new_dropout_rng

parallel_train_step = jax.pmap(train_step, "batch")

def eval_step(params, batch):
    labels = batch.pop("labels")

    logits = model(**batch, params=params, train=False)[0]

    loss = optax.softmax_cross_entropy(logits[..., :-1, :], onehot(labels[..., 1:], logits.shape[-1])).mean()

    # summarize metrics
    metrics = {"loss": loss, "perplexity": jnp.exp(loss)}
    metrics = jax.lax.pmean(metrics, axis_name="batch")
    return metrics


parallel_eval_step = jax.pmap(eval_step, "batch")

17.複製參數到各個TPU上

state = flax.jax_utils.replicate(state)

rng = jax.random.PRNGKey(training_seed)
dropout_rngs = jax.random.split(rng, jax.local_device_count())


18.開始訓練模型,把前面設定的內容都串接起來寫成迴圈

for epoch in tqdm(range(1, num_epochs + 1), desc=f"Epoch ...", position=0, leave=True):
    rng, input_rng = jax.random.split(rng)

    # -- Train --
    train_loader = data_loader(input_rng, tokenized_datasets["train"], total_batch_size, shuffle=True)
    with tqdm(total=len(tokenized_datasets["train"]) // total_batch_size, desc="Training...", leave=False) as progress_bar_train:
        for model_inputs in train_loader:
            # Model forward
            state, train_metric, dropout_rngs = parallel_train_step(state, model_inputs, dropout_rngs)

            progress_bar_train.update(1)

        progress_bar_train.write(
              f"Train... ({epoch}/{num_epochs} | Loss: {round(train_metric['loss'].mean(), 3)}, Learning Rate: {round(train_metric['learning_rate'].mean(), 6)})"
        )

    # -- Eval --
    eval_loader = data_loader(input_rng, tokenized_datasets["validation"], total_batch_size)
    eval_metrics = []
 
    with tqdm(total=len(tokenized_datasets["validation"]) // total_batch_size, desc="Evaluation...", leave=False) as progress_bar_eval:
        for model_inputs in eval_loader:
            # Model forward
            eval_metric = parallel_eval_step(state.params, model_inputs)
            eval_metrics.append(eval_metric)

            progress_bar_eval.update(1)

        eval_metrics = get_metrics(eval_metrics)
        eval_metrics = jax.tree_map(jnp.mean, eval_metrics)
        progress_bar_eval.write(
            f"Eval... ({epoch}/{num_epochs} | Loss: {eval_metrics['loss']} | Perplexity: {eval_metrics['perplexity']})"
        )


19.儲存模型


model.save_pretrained('/content/model/')


20.(Optional)掛接雲端硬碟,讓程式可以存取雲端硬碟裡的內容,並將模型存到雲端硬碟的路徑裡

from google.colab import drive
drive.mount('/content/drive')

your_path = '/your/path/'
model.save_pretrained('/content/drive/MyDrive/{your_path}')


以上是訓練模型,下面兩個步驟則是推論,大家可以根據自己需求,把模型放到自家的GPU上做推論。



21.推論-載入預訓練完的模型及tokenizer

由於程式執行保存檔案的路徑皆為colab的環境內,若是執行階段中斷則檔案也會一起被刪除。

如果有需要可以掛接到雲端硬碟,將檔案保存在雲端硬碟內,則不受執行階段影響,執行階段中斷之後也可以重新掛載雲端硬碟,並且使用檔案。

訓練後我們可以直接在transformers的幫助下進行推論


from transformers import AutoConfig, FlaxAutoModelForCausalLM, AutoTokenizer

import jax.numpy as jnp

config = AutoConfig.from_pretrained(your_path)
model = FlaxAutoModelForCausalLM.from_config(config, dtype=jnp.dtype("bfloat16"))
tokenizer = AutoTokenizer.from_pretrained(your_path)


22.推論-生成結果,藉由.generate function生成next token prediction的結果,再由tokenizer把預測還原為文字

 
text = '要探討從梨俱吠陀到波你尼時代梵語的發展,可以考察'
inputs = tokenizer(text, return_tensors="np")

beam_output = model.generate(
    **inputs,
    max_length=len(text)+5,
    early_stopping=True
)

print("Output:\n" + 100 * '-')
print(tokenizer.decode(beam_output.sequences[0], skip_special_tokens=True))


而上面的text就是我們想問的問題,而下面的tokenizer.decode把我們答案給回傳出來。

以上呢就是透過JAX來訓練原生GPT的過程囉。

謝謝大家收看!



當然如果您覺得自己弄太麻煩,也可以找Jerry老師團隊幫忙服務喔

GPT 代工一站式服務

https://www.ap-mic.com/gpt

到這裡提交表單,即有專人會與您聯繫