Có hai quyết định chính cần đưa ra khi bạn muốn chạy mô hình Gemma: 1) biến thể Gemma bạn muốn chạy và 2) khung thực thi AI bạn sẽ sử dụng để chạy mô hình đó. Một vấn đề chính trong việc đưa ra cả hai quyết định này liên quan đến phần cứng mà bạn và người dùng có để chạy mô hình.
Thông tin tổng quan này giúp bạn đưa ra các quyết định này và bắt đầu làm việc với các mô hình Gemma. Sau đây là các bước chung để chạy mô hình Gemma:
Các mô hình Gemma tương thích với nhiều khung triển khai AI tạo sinh. Một trong những yếu tố quan trọng trong việc đưa ra quyết định khi chạy mô hình Gemma là tài nguyên điện toán mà bạn có (hoặc sẽ có) để chạy mô hình. Hầu hết các khung AI tương thích đều yêu cầu phần cứng chuyên dụng, chẳng hạn như GPU hoặc TPU, để chạy mô hình Gemma một cách hiệu quả. Các công cụ như Google Colab có thể cung cấp các tài nguyên điện toán chuyên biệt này trên cơ sở hạn chế. Một số khung thực thi AI, chẳng hạn như Ollama và Gemma.cpp, cho phép bạn chạy Gemma trên các CPU phổ biến hơn bằng cách sử dụng kiến trúc ARM hoặc tương thích với x86.
Sau đây là hướng dẫn chạy mô hình Gemma bằng nhiều khung thời gian chạy AI:
Đảm bảo rằng khung bạn chọn hỗ trợ định dạng mô hình Gemma mà bạn dự định triển khai, chẳng hạn như định dạng gốc Keras, Safetensors hoặc GGUF.
Chọn một biến thể Gemma
Mô hình Gemma có nhiều biến thể và kích thước, bao gồm cả mô hình Gemma cơ bản hoặc core (lõi) và các biến thể mô hình chuyên biệt hơn như PaliGemma và DataGemma, cũng như nhiều biến thể do cộng đồng nhà phát triển AI tạo ra trên các trang web như Kaggle và Hugging Face. Nếu bạn không chắc chắn nên bắt đầu với biến thể nào, hãy chọn mô hình điều chỉnh hướng dẫn (IT) core Gemma mới nhất với số lượng tham số thấp nhất. Loại mô hình Gemma này có yêu cầu tính toán thấp và có thể phản hồi nhiều câu lệnh mà không cần phát triển thêm.
Hãy cân nhắc các yếu tố sau khi chọn một biến thể Gemma:
Gemma core và các gia đình biến thể khác như PaliGemma, CodeGemma:
Nên dùng Gemma (core). Các biến thể Gemma ngoài phiên bản cốt lõi có cùng cấu trúc với mô hình cốt lõi và được huấn luyện để hoạt động hiệu quả hơn trong các nhiệm vụ cụ thể. Trừ phi ứng dụng hoặc mục tiêu của bạn phù hợp với chuyên môn của một biến thể Gemma cụ thể, tốt nhất bạn nên bắt đầu với mô hình lõi hoặc cơ sở Gemma.
Được điều chỉnh theo hướng dẫn (IT), được huấn luyện trước (PT), được điều chỉnh chi tiết (FT), kết hợp (mix): Nên dùng IT.
Các biến thể Gemma được điều chỉnh theo hướng dẫn (IT) là các mô hình đã được huấn luyện để phản hồi nhiều hướng dẫn hoặc yêu cầu bằng ngôn ngữ của con người. Đây là những biến thể mô hình phù hợp nhất để bắt đầu vì chúng có thể phản hồi các câu lệnh mà không cần đào tạo thêm mô hình.
Các biến thể Gemma được huấn luyện trước (PT) là các mô hình đã được huấn luyện để suy luận về ngôn ngữ hoặc dữ liệu khác, nhưng chưa được huấn luyện để làm theo hướng dẫn của con người. Các mô hình này yêu cầu đào tạo hoặc điều chỉnh bổ sung để có thể thực hiện các nhiệm vụ một cách hiệu quả và dành cho các nhà nghiên cứu hoặc nhà phát triển muốn nghiên cứu hoặc phát triển các chức năng của mô hình và cấu trúc của mô hình đó.
Các biến thể Gemma được tinh chỉnh (FT) có thể được coi là biến thể IT, nhưng thường được huấn luyện để thực hiện một nhiệm vụ cụ thể hoặc hoạt động hiệu quả trên một điểm chuẩn AI tạo sinh cụ thể. Gia đình biến thể PaliGemma bao gồm một số biến thể FT.
Các biến thể Gemma Mixed (kết hợp) là các phiên bản của mô hình PaliGemma đã được điều chỉnh hướng dẫn bằng nhiều hướng dẫn và phù hợp để sử dụng chung.
Thông số: Đề xuất số nhỏ nhất hiện có. Nhìn chung, mô hình càng có nhiều tham số thì càng có nhiều khả năng. Tuy nhiên, việc chạy các mô hình lớn hơn đòi hỏi tài nguyên điện toán lớn hơn và phức tạp hơn, đồng thời thường làm chậm quá trình phát triển ứng dụng AI. Trừ phi bạn đã xác định rằng mô hình Gemma nhỏ hơn không thể đáp ứng nhu cầu của bạn, hãy chọn một mô hình có số lượng tham số nhỏ.
Cấp độ lượng tử hoá:Nên dùng độ bán chính xác (16 bit), ngoại trừ việc điều chỉnh. Việc lượng tử hoá là một chủ đề phức tạp, liên quan đến kích thước và độ chính xác của dữ liệu, do đó, mô hình AI tạo sinh sử dụng bao nhiêu bộ nhớ để tính toán và tạo phản hồi. Sau khi một mô hình được huấn luyện bằng dữ liệu có độ chính xác cao (thường là dữ liệu dấu phẩy động 32 bit), các mô hình như Gemma có thể được sửa đổi để sử dụng dữ liệu có độ chính xác thấp hơn như kích thước 16, 8 hoặc 4 bit. Các mô hình Gemma lượng tử hoá này vẫn có thể hoạt động tốt, tuỳ thuộc vào độ phức tạp của các tác vụ, trong khi sử dụng ít tài nguyên điện toán và bộ nhớ hơn đáng kể. Tuy nhiên, các công cụ để điều chỉnh mô hình lượng tử hoá còn hạn chế và có thể không có trong khung phát triển AI mà bạn đã chọn. Thông thường, bạn phải tinh chỉnh một mô hình như Gemma ở độ chính xác cao nhất, sau đó lượng tử hoá mô hình thu được.
Để xem danh sách các mô hình Gemma chính do Google phát hành, hãy xem bài viết Bắt đầu sử dụng mô hình Gemma, danh sách mô hình Gemma.
Chạy các yêu cầu tạo và suy luận
Sau khi chọn một khung thực thi AI và một biến thể Gemma, bạn có thể bắt đầu chạy mô hình và nhắc mô hình tạo nội dung hoặc hoàn thành các nhiệm vụ.
Để biết thêm thông tin về cách chạy Gemma bằng một khung cụ thể, hãy xem các hướng dẫn được liên kết trong phần Chọn khung.
Định dạng câu lệnh
Tất cả biến thể Gemma được điều chỉnh theo hướng dẫn đều có các yêu cầu cụ thể về định dạng lời nhắc. Một số yêu cầu định dạng này được xử lý tự động bằng khung mà bạn sử dụng để chạy các mô hình Gemma, nhưng khi gửi trực tiếp dữ liệu lời nhắc đến trình tạo mã thông báo, bạn phải thêm các thẻ cụ thể và yêu cầu gắn thẻ có thể thay đổi tuỳ thuộc vào biến thể Gemma mà bạn đang sử dụng. Hãy xem các hướng dẫn sau để biết thông tin về cách định dạng lời nhắc biến thể Gemma và hướng dẫn hệ thống:
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-06-11 UTC."],[],[],null,["# Run Gemma content generation and inferences\n\nThere are two key decisions to make when you want to run a Gemma model:\n1) what Gemma variant you want to run, and 2) what AI execution framework you\nare going to use to run it? A key issue in making both these decisions has to do\nwith what are hardware you and your users have available to run the model.\n\nThis overview helps you navigate these decisions and start working with Gemma\nmodels. The general steps for running a Gemma model are as follows:\n\n- [Choose a framework for running](#choose-a-framework)\n- [Select a Gemma variant](#select-a-variant)\n- [Run generation and inference requests](#run-generation)\n\nChoose a framework\n------------------\n\nGemma models are compatible with a variety of generative AI execution\nframeworks. One of the key decision making factors in running a Gemma model is\nwhat computing resources you have (or will have) available to you to run the\nmodel. Most compatible AI frameworks require specialized hardware, such as GPUs\nor TPUs, to run a Gemma model effectively. Tools such as\n[Google Colab](https://colab.research.google.com/) can provide these specialized\ncompute resources on a limited basis. Some AI execution frameworks, such as\n[Ollama](/gemma/docs/integrations/ollama) and\n[Gemma.cpp](/gemma/docs/gemma_cpp), allow you to run Gemma on more common CPUs\nusing x86-compatible or ARM architectures.\n\nHere are guides for running Gemma models with various AI runtime frameworks:\n\n- [Ollama](/gemma/docs/integrations/ollama)\n- [Hugging Face Transformers](https://huggingface.co/docs/transformers/en/model_doc/gemma2)\n- [Gemma library for JAX](https://gemma-llm.readthedocs.io)\n- [Keras](/gemma/docs/keras_inference)\n- [PyTorch](/gemma/docs/pytorch_gemma)\n- [MediaPipe LLM Inference API](/edge/mediapipe/solutions/genai/llm_inference)\n- [Hugging Face Transformers](https://huggingface.co/docs/transformers/en/model_doc/gemma2)\n- [Gemma.cpp](/gemma/docs/gemma_cpp)\n- [vLLM](https://github.com/google-gemini/gemma-cookbook/blob/main/Gemma/%5BGemma_2%5DDeploy_with_vLLM.ipynb)\n- Google Cloud [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/open-models/use-gemma)\n- [Google Cloud Kubernetes Engine (GKE)](/gemma/docs/code/gke)\n- [Google Cloud Run](/gemma/docs/core/deploy_to_cloud_run_from_ai_studio)\n\n| **Note:** The JAX inference run guide has been replaced by the [Gemma library sampling](https://gemma-llm.readthedocs.io/en/latest/colab_sampling.html) guide.\n\nMake sure your intended deployment Gemma model format, such as Keras native\nformat, Safetensors, or GGUF, is supported by your chosen framework.\n\nSelect a Gemma variant\n----------------------\n\nGemma models are available in several variants and sizes, including the\nfoundation or [core](/gemma/docs/core) Gemma models, and more\nspecialized model variants such as\n[PaliGemma](/gemma/docs/paligemma) and\n[DataGemma](/gemma/docs/datagemma), and many variants\ncreated by the AI developer community on sites such as\n[Kaggle](https://www.kaggle.com/models?query=gemma) and\n[Hugging Face](https://huggingface.co/models?search=gemma). If you are unsure\nabout what variant you should start with, select the latest Gemma\n[core](/gemma/docs/core) instruction-tuned (IT) model with\nthe lowest number of parameters. This type of Gemma model has low compute\nrequirements and be able to respond to a wide variety of prompts without\nrequiring additional development.\n\nConsider the following factors when choosing a Gemma variant:\n\n- **Gemma core, and other variant families such as PaliGemma, CodeGemma** : *Recommend Gemma (core).* Gemma variants beyond the core version have the same architecture as the core model, and are trained to perform better at specific tasks. Unless your application or goals align with the specialization of a specific Gemma variant, it is best to start with a Gemma core, or base, model.\n- **Instruction-tuned (IT), pre-trained (PT), fine-tuned (FT), mixed\n (mix)** : *Recommend IT.*\n - *Instruction-tuned* (IT) Gemma variants are models that have been trained to respond to a variety of instructions or requests in human language. These model variants are the best place to start because they can respond to prompts without further model training.\n - *Pre-trained* (PT) Gemma variants are models that have been trained to make inferences about language or other data, but have not been trained to follow human instructions. These models require additional training or tuning to be able to perform tasks effectively, and are meant for researchers or developers who want to study or develop the capabilities of the model and its architecture.\n - *Fine-tuned* (FT) Gemma variants can be considered IT variants, but are typically trained to perform a specific task, or perform well on a specific generative AI benchmark. The PaliGemma variant family includes a number of FT variants.\n - *Mixed* (mix) Gemma variants are versions of PaliGemma models that have been instruction tuned with a variety of instructions and are suitable for general use.\n- **Parameters** : *Recommend smallest number available*. In general, the more parameters a model has, the more capable it is. However, running larger models requires larger and more complex compute resources, and generally slows down development of an AI application. Unless you have already determined that a smaller Gemma model cannot meet your needs, choose a one with a small number of parameters.\n- **Quantization levels:** *Recommend half precision (16-bit), except for\n tuning*. Quantization is a complex topic that boils down to what size and precision of data, and consequently how much memory a generative AI model uses for calculations and generating responses. After a model is trained with high-precision data, which is typically 32-bit floating point data, models like Gemma can be modified to use lower precision data such as 16, 8 or 4-bit sizes. These quantized Gemma models can still perform well, depending on the complexity of the tasks, while using significantly less compute and memory resources. However, tools for tuning quantized models are limited and may not be available within your chosen AI development framework. Typically, you must fine-tune a model like Gemma at full precision, then quantize the resulting model.\n\nFor a list of key, Google-published Gemma models, see the\n[Getting started with Gemma models](/gemma/docs/get_started#models-list),\nGemma model list.\n\nRun generation and inference requests\n-------------------------------------\n\nAfter you have selected an AI execution framework and a Gemma variant, you can\nstart running the model, and prompting it to generate content or complete tasks.\nFor more information on how to run Gemma with a specific framework, see the\nguides linked in the [Choose a framework](#choose-a-framework) section.\n\n### Prompt formatting\n\nAll instruction-tuned Gemma variants have specific prompt formatting\nrequirements. Some of these formatting requirements are handled automatically by\nthe framework you use to run Gemma models, but when you are sending prompt data\ndirectly to a tokenizer, you must add specific tags, and the tagging\nrequirements can change depending on the Gemma variant you are using. See the\nfollowing guides for information on Gemma variant prompt formatting and system\ninstructions:\n\n- [Gemma prompt and system instructions](/gemma/docs/core/prompt-structure)\n- [PaliGemma prompt and system instructions](/gemma/docs/paligemma/prompt-system-instructions)\n- [CodeGemma prompt and system instructions](/gemma/docs/codegemma/prompt-structure)"]]