Offenes Modell abstimmen

Auf dieser Seite wird beschrieben, wie Sie überwachtes Fine-Tuning für offene Modelle wie Llama 3.1 durchführen.

Unterstützte Abstimmungsmodi

  • Vollständige Feinabstimmung

  • Low-Rank Adaptation (LoRA): LoRA ist ein parametereffizienter Abstimmungsmodus, bei dem nur eine Teilmenge der Parameter angepasst wird. Es ist kostengünstiger und erfordert weniger Trainingsdaten als die vollständige Feinabstimmung. Andererseits bietet die vollständige Feinabstimmung ein höheres Qualitätspotenzial, da alle Parameter angepasst werden.

Unterstützte Modelle

  • Gemma 3 27B IT (google/gemma-3-27b-it)
  • Llama 3.1 8B (meta/llama3_1@llama-3.1-8b)
  • Llama 3.1 8B Instruct (meta/llama3_1@llama-3.1-8b-instruct)
  • Llama 3.2 1B Instruct (meta/llama3-2@llama-3.2-1b-instruct)
  • Llama 3.2 3B Instruct (meta/llama3-2@llama-3.2-3b-instruct)
  • Llama 3.3 70B Instruct (meta/llama3-3@llama-3.3-70b-instruct)
  • Qwen 3 32B (qwen/qwen3@qwen3-32b)
  • Llama 4 Scout 17B 16E Instruct (meta/llama4@llama-4-scout-17b-16e-instruct)

Unterstützte Regionen

  • Iowa (us-central1)
  • Niederlande (europe-west4)

Beschränkungen

Modell Spezifikation Wert
Gemma 3 27B IT Abstimmungsmodi Parametereffiziente Feinabstimmung
Vollständige Feinabstimmung
Maximale Sequenzlänge 8.192
Betriebsarten Text
Llama 3.1 8B Abstimmungsmodi Parametereffiziente Feinabstimmung
Vollständige Feinabstimmung
Maximale Sequenzlänge 4.096 (parametereffiziente Feinabstimmung)
8.192 (vollständige Feinabstimmung)
Betriebsarten Text
Llama 3.1 8B Instruct Abstimmungsmodi Parametereffiziente Feinabstimmung
Vollständige Feinabstimmung
Maximale Sequenzlänge 4.096 (parametereffiziente Feinabstimmung)
8.192 (vollständige Feinabstimmung)
Betriebsarten Text
Llama 3.2 1B Instruct Abstimmungsmodi Vollständige Feinabstimmung
Maximale Sequenzlänge 8.192
Betriebsarten Text
Llama 3.2 3B Instruct Abstimmungsmodi Vollständige Feinabstimmung
Maximale Sequenzlänge 8.192
Betriebsarten Text
Llama 3.3 70B Instruct Abstimmungsmodi Parametereffiziente Feinabstimmung
Vollständige Feinabstimmung
Maximale Sequenzlänge 4.096 (parametereffiziente Feinabstimmung)
8.192 (vollständige Feinabstimmung)
Betriebsarten Text
Llama 4 Scout 17B 16E Instruct Abstimmungsmodi Parametereffiziente Feinabstimmung
Maximale Sequenzlänge 2.048
Betriebsarten Text
Bilder*

*Gemischte Datasets mit reinen Text- und Bildbeispielen werden nicht unterstützt. Wenn das Dataset mindestens ein Bildbeispiel enthält, werden alle reinen Textbeispiele herausgefiltert.
Qwen 3 32B Abstimmungsmodi Parametereffiziente Feinabstimmung
Vollständige Feinabstimmung
Maximale Sequenzlänge 8.192
Betriebsarten Text

Hinweise

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  8. Vertex AI SDK für Python installieren und initialisieren
  9. Importieren Sie die folgenden Bibliotheken:
    import os
    import time
    import uuid
    import vertexai
    
    vertexai.init(project=PROJECT_ID, location=REGION)
    
    from google.cloud import aiplatform
    from vertexai.tuning import sft, SourceModel
    
  10. Dataset für die Abstimmung vorbereiten

    Zum Abstimmen ist ein Trainings-Dataset erforderlich. Es wird empfohlen, ein optionales Validierungs-Dataset vorzubereiten, wenn Sie die Leistung Ihres optimierten Modells bewerten möchten.

    Ihr Dataset muss in einem der folgenden unterstützten JSONL-Formate (JSON Lines) vorliegen, wobei jede Zeile ein einzelnes Abstimmungsbeispiel enthält.

    Laden Sie Ihre JSONL-Dateien in Cloud Storage hoch.

    Nur-Text-Datasets

    Abschluss der Aufforderung

    {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
    

    Rundenbasiertes Chatformat

    {"messages": [
      {"content": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles.",
        "role": "system"},
      {"content": "Summarize the paper in one paragraph.",
        "role": "user"},
      {"content": " Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ...",
        "role": "assistant"}
    ]}
    

    GenerateContent

    {
    "systemInstruction": {
      "parts": [{ "text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles." }]},
    "contents": [
      {"role": "user",
        "parts": [{ "text": "Summarize the paper in one paragraph." }]},
      {"role": "assistant",
        "parts": [{ "text": "Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ..." }]}
    ]}
    

    Multimodale Datasets

    Rundenbasiertes Chatformat

    {"messages": [
      {"role": "user", "content": [
        {"type": "text", "text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles."},
        {"type": "image_url", "image_url": {
          "url": "gs://your-gcs-bucket/your-image.jpeg",
          "detail": "low"}}]
      },
      {"role": "assistant", "content": [
        {"type": "text", "text": "Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ..."}]
      },
      {"role": "user", "content": [
        {"type": "text", "text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles."},
        {"type": "image_url", "image_url": {
          "url": "data:image/jpeg;base64,<base64 image>",
          "detail": "low"}}]
      },
      {"role": "assistant", "content": [
        {"type": "text", "text": "Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ..."}]
      },
    ]}
    

    GenerateContent

    {
    "systemInstruction": {
      "parts": [{ "text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles." }]},
    "contents": [
      {"role": "user",
        "parts": [
          {"text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles." },
          {"file_data": {
            "mime_type": "image/jpeg", "file_uri": "gs://your-gcs-bucket/your-image.jpeg"}}]
      },
      {"role": "assistant",
        "parts": [{ "text": "Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ..." }]}
    ]}
    

    Unterstützte Formate sind JPEG, PNG, WEBP und Base64-codierte Bilder.

    Wenn Ihre Bilder in einem anderen Cloud Storage-Bucket als Ihre JSONL-Dateien gespeichert sind, müssen Sie beiden Dienstkonten die IAM-Rolle „Storage-Objektnutzer“ (roles/storage.objectUser) für beide Buckets zuweisen:

    • service-PROJECT_NUMBER@gcp-sa-vertex-moss-ft.iam.gserviceaccount.com
    • service-PROJECT_NUMBER@gcp-sa-aiplatform.iam.gserviceaccount.com

    Abstimmungsjob erstellen

    Sie können die Abstimmung vornehmen über:

    • Ein unterstütztes Basismodell, z. B. Llama 3.1
    • Ein Modell mit derselben Architektur wie eines der unterstützten Basismodelle. Dies kann entweder ein benutzerdefinierter Modell-Checkpoint aus einem Repository wie Hugging Face oder ein zuvor abgestimmtes Modell aus einem Vertex AI-Abstimmungsjob sein. So können Sie ein Modell, das bereits abgestimmt wurde, weiter abstimmen.

    Cloud Console

    1. Sie haben folgende Möglichkeiten, die Feinabstimmung zu starten:

      • Rufen Sie die Modellkarte auf, klicken Sie auf Feinabstimmung und wählen Sie Verwaltete Feinabstimmung aus.

      Zur Modellkarte für Llama 3.1

      oder

      • Rufen Sie die Seite Abstimmung auf und klicken Sie auf Abgestimmtes Modell erstellen.

      Zu „Optimierung“

    2. Füllen Sie die Parameter aus und klicken Sie auf Abstimmung starten.

    Dadurch wird ein Abstimmungsjob gestartet, den Sie auf der Seite „Abstimmung“ auf dem Tab Verwaltete Abstimmung sehen können.

    Nach Abschluss des Tuning-Jobs können Sie sich die Informationen zum abgestimmten Modell auf dem Tab Details ansehen.

    Vertex AI SDK für Python

    Ersetzen Sie die Parameterwerte durch Ihre eigenen und führen Sie dann den folgenden Code aus, um einen Abstimmungsjob zu erstellen:

    sft_tuning_job = sft.train(
        source_model=SourceModel(
          base_model="meta/llama3_1@llama-3.1-8b",
          # Optional, folder that is either a custom model checkpoint or previously tuned model
          custom_base_model="gs://{STORAGE-URI}",
        ),
        tuning_mode="FULL", # FULL or PEFT_ADAPTER
        epochs=3,
        train_dataset="gs://{STORAGE-URI}", # JSONL file
        validation_dataset="gs://{STORAGE-URI}", # JSONL file
        output_uri="gs://{STORAGE-URI}",
    )
    

    Wenn der Job abgeschlossen ist, werden die Modellartefakte für das optimierte Modell im Ordner <output_uri>/postprocess/node-0/checkpoints/final gespeichert.

    Abgestimmtes Modell bereitstellen

    Sie können das abgestimmte Modell auf einem Vertex AI-Endpunkt bereitstellen. Sie können das optimierte Modell auch aus Cloud Storage exportieren und an anderer Stelle bereitstellen.

    So stellen Sie das abgestimmte Modell auf einem Vertex AI-Endpunkt bereit:

    Cloud Console

    1. Rufen Sie die Seite Model Garden auf und klicken Sie auf Modell mit benutzerdefinierten Gewichten bereitstellen.

    Zu Model Garden

    1. Füllen Sie die Parameter aus und klicken Sie auf Bereitstellen.

    Vertex AI SDK für Python

    So stellen Sie ein G2 machine mit einem vordefinierten Container bereit:

    from vertexai.preview import model_garden
    
    MODEL_ARTIFACTS_STORAGE_URI = "gs://{STORAGE-URI}/postprocess/node-0/checkpoints/final"
    
    model = model_garden.CustomModel(
        gcs_uri=MODEL_ARTIFACTS_STORAGE_URI,
    )
    
    # deploy the model to an endpoint using GPUs. Cost will incur for the deployment
    endpoint = model.deploy(
      machine_type="g2-standard-12",
      accelerator_type="NVIDIA_L4",
      accelerator_count=1,
    )
    

    Inferenz erhalten

    Nach erfolgreicher Bereitstellung können Sie Anfragen mit Text-Prompts an den Endpunkt senden. Die Ausführung der ersten Prompts dauert länger.

    # Loads the deployed endpoint
    endpoint = aiplatform.Endpoint("projects/{PROJECT_ID}/locations/{REGION}/endpoints/{endpoint_name}")
    
    prompt = "Summarize the following article. Article: Preparing a perfect risotto requires patience and attention to detail. Begin by heating butter in a large, heavy-bottomed pot over medium heat. Add finely chopped onions and minced garlic to the pot, and cook until they're soft and translucent, about 5 minutes. Next, add Arborio rice to the pot and cook, stirring constantly, until the grains are coated with the butter and begin to toast slightly. Pour in a splash of white wine and cook until it's absorbed. From there, gradually add hot chicken or vegetable broth to the rice, stirring frequently, until the risotto is creamy and the rice is tender with a slight bite.. Summary:"
    
    # Define input to the prediction call
    instances = [
        {
            "prompt": "What is a car?",
            "max_tokens": 200,
            "temperature": 1.0,
            "top_p": 1.0,
            "top_k": 1,
            "raw_response": True,
        },
    ]
    
    # Request the prediction
    response = endpoint.predict(
        instances=instances
    )
    
    for prediction in response.predictions:
        print(prediction)
    

    Weitere Informationen zum Abrufen von Inferenzen aus einem bereitgestellten Modell finden Sie unter Onlineinferenz abrufen.

    Beachten Sie, dass für verwaltete offene Modelle die Methode chat.completions anstelle der Methode predict verwendet wird, die für bereitgestellte Modelle verwendet wird. Weitere Informationen zum Abrufen von Inferenzvorhersagen aus verwalteten Modellen finden Sie unter Llama-Modell aufrufen.

    Limits und Kontingente

    Das Kontingent wird für die Anzahl der gleichzeitig ausgeführten Abstimmungsjobs durchgesetzt. Jedes Projekt kommt mit einem Standardkontingent zum Ausführen mindestens eines Abstimmungsjobs. Dies ist ein globales Kontingent, das für alle verfügbaren Regionen und unterstützten Modelle gilt. Wenn Sie weitere Jobs gleichzeitig ausführen möchten, müssen Sie für Global concurrent managed OSS model fine-tuning jobs per project zusätzliche Kontingente anfordern.

    Preise

    Die Abrechnung für die Optimierung erfolgt gemäß den Preisen für die Modelloptimierung.

    Die Nutzung zugehöriger Dienste wie Cloud Storage und Vertex AI Prediction wird Ihnen ebenfalls in Rechnung gestellt.

    Informieren Sie sich über die Preise von Vertex AI und die Preise von Cloud Storage. Mit dem Preisrechner können Sie eine Kostenschätzung für Ihre voraussichtliche Nutzung erstellen.

    Nächste Schritte