ניהול משאבים בתור

משאבים בתור מאפשרים לכם לבקש משאבי Cloud TPU בתור. כשיוצרים בקשה למשאב בתור, הבקשה מתווספת לתור שמנוהל על ידי שירות Cloud TPU. כשהמשאב שביקשתם יהיה זמין, הוא יוקצה לפרויקט Google Cloud שלכם לשימוש מיידי ובלעדי. הוא יישאר מוקצה לפרויקט אלא אם תמחקו אותו או שהוא יידחק. רק מכונות וירטואליות מסוג TPU Spot ו-TPU שניתן להפסיק את השימוש בהן עומדות בדרישות להפסקת השימוש.

אפשר לציין שעת התחלה ושעת סיום אופציונליות בבקשה למשאב שנמצא בתור. שעת ההתחלה מציינת את השעה המוקדמת ביותר שבה אפשר למלא את הבקשה. אם הבקשה לא מולאה עד שעת הסיום שצוינה, תוקף הבקשה יפוג. הבקשה נשארת בתור גם אחרי שתוקפה פג.

בקשות למשאבים שנמצאות בתור יכולות להיות באחד מהסטטוסים הבאים:

  • WAITING_FOR_RESOURCES: הבקשה עברה אימות ראשוני והתווספה לתור. הבקשה תישאר במצב הזה עד שיהיו מספיק משאבים פנויים כדי להתחיל בהקצאה שלה, או עד שמרווח ההקצאה יסתיים. כשהביקוש גבוה, לא ניתן לספק את כל הבקשות באופן מיידי. אם אתם צריכים זמינות מהימנה יותר של TPU, כדאי לרכוש הזמנה.
  • PROVISIONING: הבקשה נבחרה מהתור והמשאבים שלה מוקצים.
  • ACTIVE: הבקשה הוקצתה. כשבקשות למשאבים שנמצאות בתור הן במצב ACTIVE, אפשר לנהל את המכונות הווירטואליות של TPU כמו שמתואר במאמר ניהול של יחידות TPU.
  • FAILED: לא הייתה אפשרות להשלים את הבקשה, או בגלל בעיה בבקשה או בגלל שהמשאבים המבוקשים לא היו זמינים במרווח ההקצאה. הבקשה נשארת בתור עד שהיא נמחקת באופן מפורש.
  • SUSPENDING: המשאבים שמשויכים לבקשה נמחקים.
  • SUSPENDED: המשאבים שצוינו בבקשה נמחקו. כשבקשה נמצאת במצב SUSPENDED, היא כבר לא עומדת בדרישות להקצאה נוספת.

דרישות מוקדמות

לפני שמריצים את הפקודות במדריך הזה, צריך להתקין את Google Cloud CLI, ליצור Google Cloud פרויקט ולהפעיל את Cloud TPU API. הוראות מפורטות זמינות במאמר הגדרת סביבת Cloud TPU.

אם אתם משתמשים באחת מספריות הלקוח ב-Cloud, אתם צריכים לפעול לפי הוראות ההגדרה לשפה שבה אתם משתמשים:

בקשה למשאב בתור לפי דרישה

למשאבים על פי דרישה לא תהיה עדיפות קודמת, אבל מכסה על פי דרישה לא מבטיחה שיהיו מספיק משאבי Cloud TPU זמינים כדי למלא את הבקשה שלכם. מידע נוסף על משאבים על פי דרישה זמין במאמר בנושא סוגי מכסות.

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central1-a \
    --accelerator-type v5litepod-8 \
    --runtime-version v2-alpha-tpuv5-lite

תיאורים של פרמטרים של פקודות

  • queued-resource-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הקצה ל-TPU שנוצר כשמוקצה משאב לבקשה שהוכנסה לתור.
  • project: הפרויקט ב- Google Cloud .
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-central1-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5litepod-8',
        'runtime_version': 'v2-alpha-tpuv5-lite',
        }
    }
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central1-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של פרמטרים של פקודות

  • queued-resource-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הקצה ל-TPU שנוצר כשהוקצתה בקשת המשאב בתור.
  • project: הפרויקט ב- Google Cloud .
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסה של TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על יצירת TPU.

  3. בשדה שם, מזינים שם ל-TPU.

  4. בתיבה תחום, בוחרים את התחום שבו רוצים ליצור את ה-TPU.

  5. בתיבה TPU type, בוחרים סוג של מאיץ. סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים לכל גרסת TPU זמין במאמר גרסאות TPU.

  6. בתיבה TPU software version (גרסת התוכנה של ה-TPU), בוחרים גרסת תוכנה. כשיוצרים מכונת TPU וירטואלית ב-Cloud TPU, גרסת התוכנה של ה-TPU מציינת את גרסת זמן הריצה של ה-TPU שמותקנת. מידע נוסף זמין במאמר בנושא גרסאות תוכנה של TPU.

  7. לוחצים על המתג הפעלת הוספה לתור.

  8. נותנים לבקשה שם בשדה Queued resource name.

  9. לוחצים על יצירה כדי ליצור את בקשת המשאבים בתור.

Java

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.Node;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateQueuedResource {
  public static void main(String[] args)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to create a node.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which to create the TPU.
    // For more information about supported TPU types for specific zones,
    // see https://cloud.google.com/tpu/docs/regions-zones
    String zone = "us-central1-a";
    // The name for your TPU.
    String nodeName = "YOUR_NODE_ID";
    // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
    // For more information about supported accelerator types for each TPU version,
    // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
    String tpuType = "v5litepod-4";
    // Software version that specifies the version of the TPU runtime to install.
    // For more information see https://cloud.google.com/tpu/docs/runtimes
    String tpuSoftwareVersion = "v2-tpuv5-litepod";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    createQueuedResource(
        projectId, zone, queuedResourceId, nodeName, tpuType, tpuSoftwareVersion);
  }

  // Creates a Queued Resource
  public static QueuedResource createQueuedResource(String projectId, String zone,
      String queuedResourceId, String nodeName, String tpuType, String tpuSoftwareVersion)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    String resource = String.format("projects/%s/locations/%s/queuedResources/%s",
            projectId, zone, queuedResourceId);
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, zone);
      Node node =
          Node.newBuilder()
              .setName(nodeName)
              .setAcceleratorType(tpuType)
              .setRuntimeVersion(tpuSoftwareVersion)
              .setQueuedResource(resource)
              .build();

      QueuedResource queuedResource =
          QueuedResource.newBuilder()
              .setName(queuedResourceId)
              .setTpu(
                  QueuedResource.Tpu.newBuilder()
                      .addNodeSpec(
                          QueuedResource.Tpu.NodeSpec.newBuilder()
                              .setParent(parent)
                              .setNode(node)
                              .setNodeId(nodeName)
                              .build())
                      .build())
              .build();

      CreateQueuedResourceRequest request =
          CreateQueuedResourceRequest.newBuilder()
              .setParent(parent)
              .setQueuedResourceId(queuedResourceId)
              .setQueuedResource(queuedResource)
              .build();

      return tpuClient.createQueuedResourceAsync(request).get(1, TimeUnit.MINUTES);
    }
  }
}

Python

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-a"
# tpu_name = "tpu-name"
# tpu_type = "v5litepod-4"
# runtime_version = "v2-tpuv5-litepod"
# queued_resource_name = "resource-name"

node = tpu_v2alpha1.Node()
node.accelerator_type = tpu_type
# To see available runtime version use command:
# gcloud compute tpus versions list --zone={ZONE}
node.runtime_version = runtime_version

node_spec = tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()
node_spec.parent = f"projects/{project_id}/locations/{zone}"
node_spec.node_id = tpu_name
node_spec.node = node

resource = tpu_v2alpha1.QueuedResource()
resource.tpu = tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])

request = tpu_v2alpha1.CreateQueuedResourceRequest(
    parent=f"projects/{project_id}/locations/{zone}",
    queued_resource_id=queued_resource_name,
    queued_resource=resource,
)

client = tpu_v2alpha1.TpuClient()
operation = client.create_queued_resource(request=request)

response = operation.result()
print(response.name)
print(response.state)
# Example response:
# projects/[project_id]/locations/[zone]/queuedResources/resource-name
# State.WAITING_FOR_RESOURCES

בקשה למשאב בתור באמצעות הזמנה

אפשר להשתמש בהזמנה כדי לבקש משאב שנמצא בתור. כדי לרכוש הזמנה, צריך לפנות לצוות ניהול החשבון Google Cloud .

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central1-a \
    --accelerator-type v5litepod-8 \
    --runtime-version v2-alpha-tpuv5-lite \
    --reserved

תיאורים של פרמטרים של פקודות

  • queued-resource-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הקצה ל-TPU שנוצר כשהוקצתה בקשת המשאב בתור.
  • project: הפרויקט ב- Google Cloud .
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • reserved: משתמשים בדגל הזה כשמבקשים משאבים בתור כחלק מהזמנה של Cloud TPU.

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-central1-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5litepod-8',
        'runtime_version': 'v2-alpha-tpuv5-lite',
        }
    }
    },
    'guaranteed': {
    'reserved': true,
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central1-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של פרמטרים של פקודות

  • queued-resource-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הקצה ל-TPU שנוצר כשמוקצה משאב לבקשה שהוכנסה לתור.
  • project: הפרויקט ב- Google Cloud .
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • reserved: משתמשים בדגל הזה כשמבקשים משאבים בתור כחלק מהזמנה של Cloud TPU.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על יצירת TPU.

  3. בשדה שם, מזינים שם ל-TPU.

  4. בתיבה תחום, בוחרים את התחום שבו רוצים ליצור את ה-TPU.

  5. בתיבה TPU type, בוחרים סוג של מאיץ. סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים לכל גרסת TPU זמין במאמר גרסאות TPU.

  6. בתיבה TPU software version (גרסת התוכנה של ה-TPU), בוחרים גרסת תוכנה. כשיוצרים מכונת TPU וירטואלית ב-Cloud TPU, גרסת התוכנה של ה-TPU מציינת את גרסת זמן הריצה של ה-TPU שמותקנת. מידע נוסף זמין במאמר בנושא גרסאות תוכנה של TPU.

  7. לוחצים על המתג הפעלת הוספה לתור.

  8. נותנים לבקשה שם בשדה Queued resource name.

  9. מרחיבים את הקטע ניהול.

  10. מסמנים את התיבה שימוש בהזמנה קיימת.

  11. לוחצים על יצירה כדי ליצור את בקשת המשאבים בתור.

בקשת משאב בתור של VM במודל Spot מסוג TPU

VM במודל Spot הוא משאב שניתן להפסיק את השימוש בו לפני הזמן ולהקצות אותו לעומס עבודה אחר בכל שלב. המשאבים של מכונות וירטואליות במודל Spot עולים פחות, ויכול להיות שתקבלו גישה למשאבים מוקדם יותר בהשוואה לבקשה של מכונה וירטואלית שלא במודל Spot. מידע נוסף על מכונות וירטואליות מסוג TPU Spot זמין במאמר ניהול מכונות וירטואליות מסוג TPU Spot.

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central1-a \
    --accelerator-type v5litepod-8 \
    --runtime-version v2-alpha-tpuv5-lite \
    --spot

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • spot: דגל בוליאני שמציין שהמשאב בתור הוא מכונה וירטואלית (VM) מסוג Spot.

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-central1-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5litepod-8',
        'runtime_version': 'v2-alpha-tpuv5-lite'
        }
    }
    },
    'spot': {}
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central1-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • spot: דגל בוליאני שמציין שהמשאב בתור הוא מכונה וירטואלית (VM) מסוג Spot.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על יצירת TPU.

  3. בשדה שם, מזינים שם ל-TPU.

  4. בתיבה תחום, בוחרים את התחום שבו רוצים ליצור את ה-TPU.

  5. בתיבה TPU type, בוחרים סוג של מאיץ. סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים לכל גרסת TPU זמין במאמר גרסאות TPU.

  6. בתיבה TPU software version (גרסת התוכנה של ה-TPU), בוחרים גרסת תוכנה. כשיוצרים מכונת TPU וירטואלית ב-Cloud TPU, גרסת התוכנה של ה-TPU מציינת את גרסת זמן הריצה של ה-TPU שמותקנת. מידע נוסף זמין במאמר בנושא גרסאות תוכנה של TPU.

  7. לוחצים על המתג הפעלת הוספה לתור.

  8. נותנים לבקשה שם בשדה Queued resource name.

  9. מרחיבים את הקטע ניהול.

  10. מסמנים את תיבת הסימון הפיכת המכונה הווירטואלית הזו ל-TPU Spot VM.

  11. לוחצים על יצירה.

Java

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.Node;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.SchedulingConfig;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class CreateSpotQueuedResource {
  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to create a node.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which to create the TPU.
    // For more information about supported TPU types for specific zones,
    // see https://cloud.google.com/tpu/docs/regions-zones
    String zone = "us-central1-a";
    // The name for your TPU.
    String nodeName = "YOUR_TPU_NAME";
    // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
    // For more information about supported accelerator types for each TPU version,
    // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
    String tpuType = "v5litepod-4";
    // Software version that specifies the version of the TPU runtime to install.
    // For more information see https://cloud.google.com/tpu/docs/runtimes
    String tpuSoftwareVersion = "v2-tpuv5-litepod";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    createQueuedResource(
        projectId, zone, queuedResourceId, nodeName, tpuType, tpuSoftwareVersion);
  }

  // Creates a Queued Resource with --preemptible flag.
  public static QueuedResource createQueuedResource(
      String projectId, String zone, String queuedResourceId,
      String nodeName, String tpuType, String tpuSoftwareVersion)
      throws IOException, ExecutionException, InterruptedException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, zone);
      String resourceName = String.format("projects/%s/locations/%s/queuedResources/%s",
              projectId, zone, queuedResourceId);
      SchedulingConfig schedulingConfig = SchedulingConfig.newBuilder()
          .setPreemptible(true)
          .build();

      Node node =
          Node.newBuilder()
              .setName(nodeName)
              .setAcceleratorType(tpuType)
              .setRuntimeVersion(tpuSoftwareVersion)
              .setSchedulingConfig(schedulingConfig)
              .setQueuedResource(resourceName)
              .build();

      QueuedResource queuedResource =
          QueuedResource.newBuilder()
              .setName(queuedResourceId)
              .setTpu(
                  QueuedResource.Tpu.newBuilder()
                      .addNodeSpec(
                          QueuedResource.Tpu.NodeSpec.newBuilder()
                              .setParent(parent)
                              .setNode(node)
                              .setNodeId(nodeName)
                              .build())
                      .build())
              .build();

      CreateQueuedResourceRequest request =
          CreateQueuedResourceRequest.newBuilder()
              .setParent(parent)
              .setQueuedResourceId(queuedResourceId)
              .setQueuedResource(queuedResource)
              .build();

      return tpuClient.createQueuedResourceAsync(request).get();
    }
  }
}

Python

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-a"
# tpu_name = "tpu-name"
# tpu_type = "v5litepod-4"
# runtime_version = "v2-tpuv5-litepod"
# queued_resource_name = "resource-name"

node = tpu_v2alpha1.Node()
node.accelerator_type = tpu_type
# To see available runtime version use command:
# gcloud compute tpus versions list --zone={ZONE}
node.runtime_version = runtime_version

node_spec = tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()
node_spec.parent = f"projects/{project_id}/locations/{zone}"
node_spec.node_id = tpu_name
node_spec.node = node

resource = tpu_v2alpha1.QueuedResource()
resource.tpu = tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])
# Create a spot resource
resource.spot = tpu_v2alpha1.QueuedResource.Spot()

request = tpu_v2alpha1.CreateQueuedResourceRequest(
    parent=f"projects/{project_id}/locations/{zone}",
    queued_resource_id=queued_resource_name,
    queued_resource=resource,
)

client = tpu_v2alpha1.TpuClient()
operation = client.create_queued_resource(request=request)
response = operation.result()

print(response.name)
print(response.state)
# Example response:
# projects/[project_id]/locations/[zone]/queuedResources/resource-name
# State.WAITING_FOR_RESOURCES

בקשה להקצאת משאב בתור לפני או אחרי שעה מסוימת

אפשר לציין שעת התחלה או שעת סיום אופציונליות בבקשה למשאב בתור. שעת ההתחלה או משך ההתחלה מציינים את השעה המוקדמת ביותר שבה אפשר למלא את הבקשה. זמן הסיום או משך הסיום מציינים לכמה זמן הבקשה תקפה. אם בקשה לא מולאה עד שעת הסיום שצוינה או במהלך משך הזמן שצוין, תוקף הבקשה יפוג. אחרי שהתוקף של הבקשה פג, היא נשארת בתור אבל כבר לא כשירה להקצאה.

אפשר גם לציין מרווח הקצאה על ידי הגדרת שעת התחלה או משך זמן, ושעת סיום או משך זמן.

רשימה של פורמטים נתמכים של חותמות זמן ומשך זמינה במאמר בנושא תאריך ושעה.

שליחת בקשה למימוש משאב בתור אחרי פרק זמן מסוים

בבקשה למשאב בתור, אפשר לציין זמן או משך זמן שאחריהם צריך להקצות משאב.

gcloud

הפקודה הבאה מבקשת להקצות TPU מדגם v5p-4096 אחרי השעה 9:00 בבוקר ב-14 בדצמבר 2022.

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-east5-a \
    --accelerator-type v5p-4096 \
    --runtime-version v2-alpha-tpuv5 \
    --valid-after-time 2022-12-14T09:00:00Z

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר בנושא גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-after-time: השעה שאחריה המשאב צריך להיות מוקצה. למידע נוסף על פורמטים של משך, אפשר לעיין בנושא Google Cloud CLI topic datetime.

curl

הפקודה הבאה מבקשת להקצות TPU מדגם v5p-4096 אחרי השעה 9:00 בבוקר ב-14 בדצמבר 2022.

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-east5-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5p-4096',
        'runtime_version': 'v2-alpha-tpuv5',
        }
    }
    },
    'queueing_policy': {
    'valid_after_time': {
        'seconds': 2022-12-14T09:00:00Z
    }
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-east5-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-after-time: השעה שאחריה צריך להקצות את המשאב. למידע נוסף על פורמטים של משך, אפשר לעיין בנושא Google Cloud CLI topic datetime.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על יצירת TPU.

  3. בשדה שם, מזינים שם ל-TPU.

  4. בתיבה תחום, בוחרים את התחום שבו רוצים ליצור את ה-TPU.

  5. בתיבה TPU type, בוחרים סוג של מאיץ. סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים לכל גרסת TPU זמין במאמר גרסאות TPU.

  6. בתיבה TPU software version (גרסת התוכנה של ה-TPU), בוחרים גרסת תוכנה. כשיוצרים מכונת TPU וירטואלית ב-Cloud TPU, גרסת התוכנה של ה-TPU מציינת את גרסת זמן הריצה של ה-TPU שמותקנת. מידע נוסף זמין במאמר בנושא [TPU version of the TPU runtime to install. מידע נוסף זמין במאמר בנושא גרסאות תוכנה של TPU.

  7. לוחצים על המתג הפעלת הוספה לתור.

  8. נותנים לבקשה שם בשדה Queued resource name.

  9. בשדה Start request on, מזינים את השעה שאחריה המשאב צריך להיות מוקצה.

  10. לוחצים על יצירה כדי ליצור את בקשת המשאבים בתור.

בדוגמה הבאה, מתבקשת הקצאה של v5p-32 אחרי שש שעות.

.

gcloud

    gcloud compute tpus queued-resources create your-queued-resource-id \
        --node-id your-node-id \
        --project your-project-id \
        --zone us-east5-a \
        --accelerator-type v5p-32 \
        --runtime-version v2-alpha-tpuv5 \
        --valid-after-duration 6h

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-after-duration: משך הזמן שלפניו אסור להקצות את ה-TPU. מידע נוסף על פורמטים של משך זמן זמין במאמר בנושא תאריך ושעה ב-Google Cloud CLI

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-east5-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5p-32',
        'runtime_version': 'v2-alpha-tpuv5',
        }
    }
    },
'queueing_policy': {
    'valid_after_duration': {
        'seconds': 21600
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-east5-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-after-duration: משך הזמן שלפניו אסור להקצות את ה-TPU. מידע נוסף על פורמטים של משך זמן זמין במאמר בנושא תאריך ושעה ב-Google Cloud CLI

Java

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.Node;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class CreateTimeBoundQueuedResource {

  public static void main(String[] args)
          throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to create a node.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which to create the TPU.
    // For more information about supported TPU types for specific zones,
    // see https://cloud.google.com/tpu/docs/regions-zones
    String zone = "us-central2-b";
    // The name of your node.
    String nodeId = "YOUR_NODE_ID";
    // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
    // For more information about supported accelerator types for each TPU version,
    // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
    String acceleratorType = "v2-8";
    // Software version that specifies the version of the TPU runtime to install.
    // For more information see https://cloud.google.com/tpu/docs/runtimes
    String runtimeVersion = "v2-tpuv5-litepod";
    // The name of your Queued Resource.
    String queuedResourceId = "YOUR_QUEUED_RESOURCE_ID";

    createTimeBoundQueuedResource(projectId, nodeId,
        queuedResourceId, zone, acceleratorType, runtimeVersion);
  }

  // Creates a Queued Resource with time bound configuration.
  public static QueuedResource createTimeBoundQueuedResource(
      String projectId, String nodeId, String queuedResourceId,
      String zone, String acceleratorType, String runtimeVersion)
          throws IOException, ExecutionException, InterruptedException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, zone);
      // Create a Duration object representing 6 hours.
      Duration validAfterDuration = Duration.newBuilder().setSeconds(6 * 3600).build();
      // You could also use timestamps like this:
      // Timestamp validAfterTime = Timestamps.parse("2024-10-14T09:00:00Z");

      Node node =
          Node.newBuilder()
              .setName(nodeId)
              .setAcceleratorType(acceleratorType)
              .setRuntimeVersion(runtimeVersion)
              .setQueuedResource(
                  String.format(
                      "projects/%s/locations/%s/queuedResources/%s",
                      projectId, zone, queuedResourceId))
              .build();

      QueuedResource queuedResource =
          QueuedResource.newBuilder()
              .setName(queuedResourceId)
              .setTpu(
                  QueuedResource.Tpu.newBuilder()
                      .addNodeSpec(
                          QueuedResource.Tpu.NodeSpec.newBuilder()
                              .setParent(parent)
                              .setNode(node)
                              .setNodeId(nodeId)
                              .build())
                      .build())
              .setQueueingPolicy(
                  QueuedResource.QueueingPolicy.newBuilder()
                      .setValidAfterDuration(validAfterDuration)
                      // .setValidAfterTime(validAfterTime)
                      .build())
              .build();

      CreateQueuedResourceRequest request =
          CreateQueuedResourceRequest.newBuilder()
              .setParent(parent)
              .setQueuedResource(queuedResource)
              .setQueuedResourceId(queuedResourceId)
              .build();

      return tpuClient.createQueuedResourceAsync(request).get();
    }
  }
}

שליחת בקשה למשאב בתור שהתוקף שלו פג אחרי פרק זמן מסוים

בבקשה למשאב שנמצא בתור, אפשר לציין כמה זמן הבקשה תישאר בתוקף. אם הבקשה לא תטופל עד למועד או למשך הזמן שתציינו, התוקף שלה יפוג.

gcloud

הפקודה הבאה מבקשת TPU מדגם v5p-4096. אם הבקשה לא תאושר עד 14 בדצמבר 2022 בשעה 9:00, תוקף הבקשה יפוג.

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-east5-a \
    --accelerator-type v5p-4096 \
    --runtime-version v2-alpha-tpuv5 \
    --valid-until-time 2022-12-14T09:00:00Z

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-until-time: השעה שאחריה הבקשה מבוטלת. מידע נוסף על פורמטים של משך זמין במאמר נושא בנושא תאריך ושעה ב-Google Cloud CLI.

curl

הפקודה הבאה מבקשת TPU מדגם v5p-4096. אם הבקשה לא תאושר עד 14 בדצמבר 2022 בשעה 9:00, תוקף הבקשה יפוג.

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-east5-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5p-4096',
        'runtime_version': 'v2-alpha-tpuv5',
        }
    }
    },
    'queueing_policy': {
    'valid_until_time': {
        'seconds': 1655197200
    }
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-east5-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-until-time: השעה שאחריה הבקשה מבוטלת. מידע נוסף על פורמטים של משך זמן זמין במאמר נושאים בנושא תאריך ושעה ב-Google Cloud CLI.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על יצירת TPU.

  3. בשדה שם, מזינים שם ל-TPU.

  4. בתיבה תחום, בוחרים את התחום שבו רוצים ליצור את ה-TPU.

  5. בתיבה TPU type, בוחרים סוג של מאיץ. סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים לכל גרסת TPU זמין במאמר גרסאות TPU.

  6. בתיבה TPU software version (גרסת התוכנה של ה-TPU), בוחרים גרסת תוכנה. כשיוצרים מכונת TPU וירטואלית ב-Cloud TPU, גרסת התוכנה של ה-TPU מציינת את גרסת זמן הריצה של ה-TPU שמותקנת. מידע נוסף זמין במאמר בנושא גרסאות תוכנה של TPU.

  7. לוחצים על המתג הפעלת הוספה לתור.

  8. נותנים לבקשה שם בשדה Queued resource name.

  9. בשדה Cancel request on (ביטול הבקשה בתאריך), מזינים את השעה שבה בקשת המשאב שנוספה לתור תפוג אם היא לא תמולא.

  10. לוחצים על יצירה כדי ליצור את בקשת המשאבים בתור.

בדוגמה הבאה מבוקש v5p-32. אם לא תמלאו את הבקשה תוך שש שעות, היא תפוג.

gcloud

    gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-east5-a \
    --accelerator-type v5p-32 \
    --runtime-version v2-alpha-tpuv5 \
    --valid-until-duration 6h

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-until-duration: משך הזמן שהבקשה תקפה. מידע נוסף על פורמטים של משך זמן זמין במאמר בנושא תאריך ושעה ב-Google Cloud CLI

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-east5-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5p-32',
        'runtime_version': 'v2-alpha-tpuv5',
        }
    }
    },
'queueing_policy': {
    'valid_until_duration': {
        'seconds': 21600
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-east5-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של פרמטרים של פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-until-duration: משך הזמן שהבקשה תקפה. מידע נוסף על פורמטים של משך זמן זמין במאמר בנושא תאריך ושעה ב-Google Cloud CLI

Python

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-a"
# tpu_name = "tpu-name"
# tpu_type = "v5litepod-4"
# runtime_version = "v2-tpuv5-litepod"
# queued_resource_name = "resource-name"

node = tpu_v2alpha1.Node()
node.accelerator_type = tpu_type
# To see available runtime version use command:
# gcloud compute tpus versions list --zone={ZONE}
node.runtime_version = runtime_version

node_spec = tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()
node_spec.parent = f"projects/{project_id}/locations/{zone}"
node_spec.node_id = tpu_name
node_spec.node = node

resource = tpu_v2alpha1.QueuedResource()
resource.tpu = tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])

# Use one of the following queueing policies
resource.queueing_policy = tpu_v2alpha1.QueuedResource.QueueingPolicy(
    # valid_after_duration = "6000s", # Duration after which a resource should be allocated
    valid_until_duration="90s",  # Specify how long a queued resource request remains valid
    # valid_after_time="2024-10-31T09:00:00Z", # Specify a time after which a resource should be allocated
    # valid_until_time="2024-10-29T16:00:00Z",  # Specify a time before which the resource should be allocated
)

request = tpu_v2alpha1.CreateQueuedResourceRequest(
    parent=f"projects/{project_id}/locations/{zone}",
    queued_resource_id=queued_resource_name,
    queued_resource=resource,
)

client = tpu_v2alpha1.TpuClient()
operation = client.create_queued_resource(request=request)

response = operation.result()
print(resource.queueing_policy)
print(response.queueing_policy.valid_until_time)
# Example response:
# valid_until_duration {
#   seconds: 90
# }
# 2024-10-29 14:22:53.562090+00:00

שליחת בקשה להקצאת משאב בתור בתוך מרווח זמן מוגדר

אפשר להגדיר את משך הזמן של ההקצאה על ידי ציון של זמן ההתחלה או משך הזמן, וזמן הסיום או משך הזמן.

gcloud

הפקודה הבאה מבקשת ליצור מכונה מסוג v5p-32 תוך 5 שעות ו-30 דקות מהשעה הנוכחית, ולא יאוחר מ-14 בדצמבר 2022 בשעה 9:00 בבוקר.

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-east5-a \
    --accelerator-type v5p-32 \
    --runtime-version v2-alpha-tpuv5 \
    --valid-after-duration 5h30m \
    --valid-until-time 2022-12-14T09:00:00Z

תיאורים של דגלי פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר בנושא גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-until-time: השעה שאחריה הבקשה מבוטלת. מידע נוסף על פורמטים של משך זמן זמין במאמר נושאים בנושא תאריך ושעה ב-Google Cloud CLI.
  • valid-after-duration: משך הזמן שלפניו אסור להקצות את ה-TPU. מידע נוסף על פורמטים של משך זמן זמין במאמר נושאים בנושא תאריך ושעה ב-Google Cloud CLI.

curl

הפקודה הבאה מבקשת ליצור מכונה מסוג v5p-32 תוך 5 שעות ו-30 דקות מהשעה הנוכחית, ולא יאוחר מ-14 בדצמבר 2022 בשעה 9:00 בבוקר.

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-east5-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5p-32',
        'runtime_version': 'v2-alpha-tpuv5',
        }
    }
    },
'queueing_policy': {
    'validInterval': {
        'startTime': '2022-12-10T14:30:00Z',
        'endTime': '2022-12-14T09:00:00Z'
    }
    },
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-east5-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של דגלי פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר בנושא גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • valid-until-time: השעה שאחריה הבקשה מבוטלת. מידע נוסף על פורמטים של משך זמן זמין במאמר נושאים בנושא תאריך ושעה ב-Google Cloud CLI.
  • valid-until-duration: משך הזמן שהבקשה תקפה. מידע נוסף על פורמטים של משך זמן זמין במאמר נושאים בנושא תאריך ושעה ב-Google Cloud CLI.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על יצירת TPU.

  3. בשדה שם, מזינים שם ל-TPU.

  4. בתיבה תחום, בוחרים את התחום שבו רוצים ליצור את ה-TPU.

  5. בתיבה TPU type, בוחרים סוג של מאיץ. סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים לכל גרסת TPU זמין במאמר גרסאות TPU.

  6. בתיבה TPU software version (גרסת התוכנה של ה-TPU), בוחרים גרסת תוכנה. כשיוצרים מכונת TPU וירטואלית ב-Cloud TPU, גרסת התוכנה של ה-TPU מציינת את גרסת זמן הריצה של ה-TPU שמותקנת. מידע נוסף זמין במאמר בנושא גרסאות תוכנה של TPU.

  7. לוחצים על המתג הפעלת הוספה לתור.

  8. נותנים לבקשה שם בשדה Queued resource name.

  9. בשדה Start request on, מזינים את השעה שאחריה המשאב צריך להיות מוקצה.

  10. בשדה Cancel request on (ביטול הבקשה בתאריך), מזינים את השעה שבה בקשת המשאב שנוספה לתור תפוג אם היא לא תמולא.

  11. לוחצים על יצירה כדי ליצור את בקשת המשאבים בתור.

בקשת משאב בתור באמצעות סקריפט לטעינה בזמן ההפעלה

אפשר לציין סקריפט שיורץ על משאב בתור אחרי שהוא יוקצה.

gcloud

כשמשתמשים בפקודה gcloud, אפשר להשתמש בדגל --metadata או בדגל --metadata-from-file כדי לציין פקודת סקריפט או קובץ שמכיל את קוד הסקריפט, בהתאמה. בדוגמה הבאה נוצרת בקשה למשאב בתור שתפעיל את הסקריפט startup-script.sh.

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central1-a \
    --accelerator-type v5litepod-8 \
    --runtime-version v2-alpha-tpuv5-lite \
    --metadata-from-file='startup-script=startup-script.sh'

תיאורים של דגלי פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • validInterval: הזמן שבו הבקשה תקפה. אחרי הזמן הזה הבקשה מבוטלת. מידע נוסף על פורמטים של משך זמן זמין במאמר נושאים בנושא תאריך ושעה ב-Google Cloud CLI.
  • metadata-from-file: מציין קובץ שמכיל מטא-נתונים. אם לא מציינים נתיב מלא לקובץ המטא-נתונים, הפקודה מניחה שהוא נמצא בספרייה הנוכחית. בדוגמה הזו, הקובץ מכיל סקריפט לטעינה בזמן ההפעלה שמופעל כשמבצעים הקצאה של המשאב בתור.
  • metadata: מציין מטא-נתונים לבקשה. בדוגמה הזו, המטא-נתונים הם פקודה של סקריפט לטעינה בזמן ההפעלה שמופעלת כשמשאב בתור מוקצה.

curl

כשמשתמשים ב-curl, צריך לכלול את קוד הסקריפט בתוכן ה-JSON. הדוגמה הבאה כוללת סקריפט מוטבע בגוף ה-JSON.

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    tpu: {
        node_spec: {
        parent: 'projects/your-project-number/locations/us-central1-a',
        node_id: 'your-node-id',
        node: {
            accelerator_type: 'v5e-8',
            runtime_version: 'v2-alpha-tpuv5-lite',
            metadata: {
                "startup-script": "#! /bin/bash\npwd > /tmp/out.txt\nwhoami >> /tmp/out.txt"
            }
        }
        }
    },
'queueing_policy': {
    'validInterval': {
        'startTime': '2022-12-10T14:30:00Z',
        'endTime': '2022-12-14T09:00:00Z'
    }
    },
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central1-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של דגלי פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הגדיר ל-TPU שנוצר בתגובה לבקשה.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • validInterval: הזמן שבו הבקשה תקפה. אחרי הזמן הזה הבקשה מבוטלת. מידע נוסף על פורמטים של משך זמן זמין במאמר נושאים בנושא תאריך ושעה ב-Google Cloud CLI.
  • metadata-from-file: מציין קובץ שמכיל מטא-נתונים. אם לא מציינים נתיב מלא לקובץ המטא-נתונים, הפקודה מניחה שהוא נמצא בספרייה הנוכחית. בדוגמה הזו, הקובץ מכיל סקריפט לטעינה בזמן ההפעלה שמופעל כשמוקצה משאב בתור.
  • metadata: מציין מטא-נתונים לבקשה. בדוגמה הזו, המטא-נתונים הם פקודה של סקריפט לטעינה בזמן ההפעלה שמופעלת כשמשאב בתור מוקצה.

Java

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.Node;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class CreateQueuedResourceWithStartupScript {
  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to create a node.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which to create the TPU.
    // For more information about supported TPU types for specific zones,
    // see https://cloud.google.com/tpu/docs/regions-zones
    String zone = "us-central1-a";
    // The name for your TPU.
    String nodeName = "YOUR_TPU_NAME";
    // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
    // For more information about supported accelerator types for each TPU version,
    // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
    String tpuType = "v5litepod-4";
    // Software version that specifies the version of the TPU runtime to install.
    // For more information see https://cloud.google.com/tpu/docs/runtimes
    String tpuSoftwareVersion = "v2-tpuv5-litepod";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    createQueuedResource(projectId, zone, queuedResourceId, nodeName,
        tpuType, tpuSoftwareVersion);
  }

  // Creates a Queued Resource with startup script.
  public static QueuedResource createQueuedResource(
      String projectId, String zone, String queuedResourceId,
      String nodeName, String tpuType, String tpuSoftwareVersion)
      throws IOException, ExecutionException, InterruptedException {
    String parent = String.format("projects/%s/locations/%s", projectId, zone);
    String startupScriptContent = "#!/bin/bash\necho \"Hello from the startup script!\"";
    // Add startup script to metadata
    Map<String, String> metadata = new HashMap<>();
    metadata.put("startup-script", startupScriptContent);
    String queuedResourceForTpu =  String.format("projects/%s/locations/%s/queuedResources/%s",
            projectId, zone, queuedResourceId);
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create()) {
      Node node =
          Node.newBuilder()
              .setName(nodeName)
              .setAcceleratorType(tpuType)
              .setRuntimeVersion(tpuSoftwareVersion)
              .setQueuedResource(queuedResourceForTpu)
              .putAllMetadata(metadata)
              .build();

      QueuedResource queuedResource =
          QueuedResource.newBuilder()
              .setName(queuedResourceId)
              .setTpu(
                  QueuedResource.Tpu.newBuilder()
                      .addNodeSpec(
                          QueuedResource.Tpu.NodeSpec.newBuilder()
                              .setParent(parent)
                              .setNode(node)
                              .setNodeId(nodeName)
                              .build())
                      .build())
              .build();

      CreateQueuedResourceRequest request =
          CreateQueuedResourceRequest.newBuilder()
              .setParent(parent)
              .setQueuedResourceId(queuedResourceId)
              .setQueuedResource(queuedResource)
              .build();
      // You can wait until TPU Node is READY,
      // and check its status using getTpuVm() from "tpu_vm_get" sample.

      return tpuClient.createQueuedResourceAsync(request).get();
    }
  }
}

Python

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-a"
# tpu_name = "tpu-name"
# tpu_type = "v5litepod-4"
# runtime_version = "v2-tpuv5-litepod"
# queued_resource_name = "resource-name"

node = tpu_v2alpha1.Node()
node.accelerator_type = tpu_type
# To see available runtime version use command:
# gcloud compute tpus versions list --zone={ZONE}
node.runtime_version = runtime_version
# This startup script updates numpy to the latest version and logs the output to a file.
script = {
    "startup-script": """#!/bin/bash
echo "Hello World" > /var/log/hello.log
sudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1
"""
}
node.metadata = script
# Enabling external IPs for internet access from the TPU node for updating numpy
node.network_config = tpu_v2alpha1.NetworkConfig(
    enable_external_ips=True,
)

node_spec = tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()
node_spec.parent = f"projects/{project_id}/locations/{zone}"
node_spec.node_id = tpu_name
node_spec.node = node

resource = tpu_v2alpha1.QueuedResource()
resource.tpu = tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])

request = tpu_v2alpha1.CreateQueuedResourceRequest(
    parent=f"projects/{project_id}/locations/{zone}",
    queued_resource_id=queued_resource_name,
    queued_resource=resource,
)

client = tpu_v2alpha1.TpuClient()
operation = client.create_queued_resource(request=request)

response = operation.result()
print(response.name)
print(response.tpu.node_spec[0].node.metadata)
# Example response:
# projects/[project_id]/locations/[zone]/queuedResources/resource-name
# {'startup-script': '#!/bin/bash\n    echo "Hello World" > /var/log/hello.log\n
# sudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1\n    '}

בקשה למשאב בתור עם רשת ורשת משנה מוגדרות

בבקשה למשאב בתור, אפשר לציין רשת ורשת משנה שרוצים לחבר אליהן את ה-TPU.

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central1-a \
    --accelerator-type v5e-8 \
    --runtime-version v2-alpha-tpuv5-lite \
    --network network-name \
    --subnetwork subnetwork-name

תיאורים של פרמטרים של פקודות

  • queued-resource-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הקצה ל-TPU שנוצר כשהוקצתה בקשת המשאב בתור.
  • project: הפרויקט ב- Google Cloud .
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • reserved: משתמשים בדגל הזה כשמבקשים משאבים בתור כחלק מהזמנה של Cloud TPU.
  • network: רשת שהמשאב בתור יהיה חלק ממנה.
  • subnetwork: רשת משנה שהמשאב בתור יהיה חלק ממנה.

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-central1-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5e-8',
        'runtime_version': 'v2-alpha-tpuv5-lite',
        'network_config': {
            'network': 'network-name',
            'subnetwork': 'subnetwork-name',
            'enable_external_ips': true
        }
    }
    },
    'guaranteed': {
    'reserved': true,
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central1-a/queuedResources?queued_resource_id=your-queued-resource-id

תיאורים של פרמטרים של פקודות

  • queued-resource-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • node-id: המזהה שהמשתמש הקצה ל-TPU שנוצר כשהוקצתה בקשת המשאב בתור.
  • project: הפרויקט ב- Google Cloud .
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.
  • accelerator-type: סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים בכל גרסת TPU זמין במאמר גרסאות TPU.
  • runtime-version: הגרסה של תוכנת Cloud TPU.
  • reserved: משתמשים בדגל הזה כשמבקשים משאבים בתור כחלק מהזמנה של Cloud TPU.
  • network: רשת שהמשאב בתור יהיה חלק ממנה.
  • subnetwork: רשת משנה שהמשאב בתור יהיה חלק ממנה.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על יצירת TPU.

  3. בשדה שם, מזינים שם ל-TPU.

  4. בתיבה תחום, בוחרים את התחום שבו רוצים ליצור את ה-TPU.

  5. בתיבה TPU type, בוחרים סוג של מאיץ. סוג המאיץ מציין את הגרסה והגודל של Cloud TPU שרוצים ליצור. מידע נוסף על סוגי המאיצים הנתמכים לכל גרסת TPU זמין במאמר גרסאות TPU.

  6. בתיבה TPU software version (גרסת התוכנה של ה-TPU), בוחרים גרסת תוכנה. כשיוצרים מכונת TPU וירטואלית ב-Cloud TPU, גרסת התוכנה של ה-TPU מציינת את גרסת זמן הריצה של ה-TPU שמותקנת. מידע נוסף זמין במאמר בנושא גרסאות תוכנה של TPU.

  7. לוחצים על המתג הפעלת הוספה לתור.

  8. נותנים לבקשה שם בשדה Queued resource name.

  9. מרחיבים את הקטע Network.

  10. בשדות Network (רשת) ו-Subnetwork (רשת משנה), בוחרים את הרשת ואת רשת המשנה שרוצים להשתמש בהן.

  11. לוחצים על יצירה כדי ליצור את בקשת המשאבים בתור.

Java

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.NetworkConfig;
import com.google.cloud.tpu.v2alpha1.Node;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import com.google.cloud.tpu.v2alpha1.TpuSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.threeten.bp.Duration;

public class CreateQueuedResourceWithNetwork {
  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to create a node.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which to create the TPU.
    // For more information about supported TPU types for specific zones,
    // see https://cloud.google.com/tpu/docs/regions-zones
    String zone = "europe-west4-a";
    // The name for your TPU.
    String nodeName = "YOUR_TPU_NAME";
    // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
    // For more information about supported accelerator types for each TPU version,
    // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
    String tpuType = "v5litepod-4";
    // Software version that specifies the version of the TPU runtime to install.
    // For more information see https://cloud.google.com/tpu/docs/runtimes
    String tpuSoftwareVersion = "v2-tpuv5-litepod";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";
    // The name of the network you want the node to connect to.
    // The network should be assigned to your project.
    String networkName = "YOUR_COMPUTE_TPU_NETWORK";

    createQueuedResourceWithNetwork(projectId, zone, queuedResourceId, nodeName,
        tpuType, tpuSoftwareVersion, networkName);
  }

  // Creates a Queued Resource with network configuration.
  public static QueuedResource createQueuedResourceWithNetwork(
      String projectId, String zone, String queuedResourceId, String nodeName,
      String tpuType, String tpuSoftwareVersion, String networkName)
      throws IOException, ExecutionException, InterruptedException {
    // With these settings the client library handles the Operation's polling mechanism
    // and prevent CancellationException error
    TpuSettings.Builder clientSettings =
        TpuSettings.newBuilder();
    clientSettings
        .createQueuedResourceSettings()
        .setRetrySettings(
            RetrySettings.newBuilder()
                .setInitialRetryDelay(Duration.ofMillis(5000L))
                .setRetryDelayMultiplier(2.0)
                .setInitialRpcTimeout(Duration.ZERO)
                .setRpcTimeoutMultiplier(1.0)
                .setMaxRetryDelay(Duration.ofMillis(45000L))
                .setTotalTimeout(Duration.ofHours(24L))
                .build());
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
      String parent = String.format("projects/%s/locations/%s", projectId, zone);
      String region = zone.substring(0, zone.length() - 2);

      // Specify the network and subnetwork that you want to connect your TPU to.
      NetworkConfig networkConfig =
          NetworkConfig.newBuilder()
              .setEnableExternalIps(true)
              .setNetwork(String.format("projects/%s/global/networks/%s", projectId, networkName))
              .setSubnetwork(
                  String.format(
                      "projects/%s/regions/%s/subnetworks/%s", projectId, region, networkName))
              .build();

      // Create a node
      Node node =
          Node.newBuilder()
              .setName(nodeName)
              .setAcceleratorType(tpuType)
              .setRuntimeVersion(tpuSoftwareVersion)
              .setNetworkConfig(networkConfig)
              .setQueuedResource(
                  String.format(
                      "projects/%s/locations/%s/queuedResources/%s",
                      projectId, zone, queuedResourceId))
              .build();

      // Create queued resource
      QueuedResource queuedResource =
          QueuedResource.newBuilder()
              .setName(queuedResourceId)
              .setTpu(
                  QueuedResource.Tpu.newBuilder()
                      .addNodeSpec(
                          QueuedResource.Tpu.NodeSpec.newBuilder()
                              .setParent(parent)
                              .setNode(node)
                              .setNodeId(nodeName)
                              .build())
                      .build())
              .build();

      CreateQueuedResourceRequest request =
          CreateQueuedResourceRequest.newBuilder()
              .setParent(parent)
              .setQueuedResource(queuedResource)
              .setQueuedResourceId(queuedResourceId)
              .build();

      // You can wait until TPU Node is READY,
      // and check its status using getTpuVm() from "tpu_vm_get" sample.

      return tpuClient.createQueuedResourceAsync(request).get();
    }
  }
}

Python

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-a"
# tpu_name = "tpu-name"
# tpu_type = "v5litepod-4"
# runtime_version = "v2-tpuv5-litepod"
# queued_resource_name = "resource-name"
# network = "default"

node = tpu_v2alpha1.Node()
node.accelerator_type = tpu_type
node.runtime_version = runtime_version
# Setting network configuration
node.network_config = tpu_v2alpha1.NetworkConfig(
    network=network,  # Update if you want to use a specific network
    subnetwork="default",  # Update if you want to use a specific subnetwork
    enable_external_ips=True,
    can_ip_forward=True,
)

node_spec = tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()
node_spec.parent = f"projects/{project_id}/locations/{zone}"
node_spec.node_id = tpu_name
node_spec.node = node

resource = tpu_v2alpha1.QueuedResource()
resource.tpu = tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])

request = tpu_v2alpha1.CreateQueuedResourceRequest(
    parent=f"projects/{project_id}/locations/{zone}",
    queued_resource_id=queued_resource_name,
    queued_resource=resource,
)

client = tpu_v2alpha1.TpuClient()
operation = client.create_queued_resource(request=request)

response = operation.result()
print(response.name)
print(response.tpu.node_spec[0].node.network_config)
print(resource.tpu.node_spec[0].node.network_config.network == "default")
# Example response:
# network: "default"
# subnetwork: "default"
# enable_external_ips: true
# can_ip_forward: true

מחיקת בקשה למשאב שנמצא בתור

כדי למחוק בקשה למשאב בתור ואת ה-TPU שמשויך לבקשה, צריך למחוק את הבקשה למשאב בתור:

gcloud

מעבירים את הדגל --force לפקודה queued-resource delete:

gcloud compute tpus queued-resources delete your-queued-resource-id \
    --project your-project-id \
    --zone us-central1-a \
    --force \
    --async

תיאורים של דגלי פקודות

  • your-queued-resource-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור של Cloud TPU שרוצים למחוק.
  • force: מחיקה של מכונת ה-TPU הווירטואלית ושל בקשת המשאב שבהמתנה.

curl

משתמשים בפרמטר השאילתה force=true בבקשת curl:

curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project-id/locations/us-central1-a/queuedResources/your-queued-resource-id?force=true

תיאורים של דגלי פקודות

  • your-queued-resource-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור של Cloud TPU שרוצים למחוק.
  • force: מחיקה של מכונת ה-TPU הווירטואלית ושל בקשת המשאב שבהמתנה.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על הכרטיסייה משאבים בתור.

  3. מסמנים את התיבה לצד בקשת המשאבים שנמצאת בתור.

  4. לוחצים על מחיקה.

Java

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import com.google.cloud.tpu.v2alpha1.TpuSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.threeten.bp.Duration;

public class DeleteForceQueuedResource {
  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which the TPU was created.
    String zone = "us-central1-f";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    deleteForceQueuedResource(projectId, zone, queuedResourceId);
  }

  // Deletes a Queued Resource asynchronously with --force flag.
  public static void deleteForceQueuedResource(
      String projectId, String zone, String queuedResourceId)
          throws ExecutionException, InterruptedException, IOException {
    String name = String.format("projects/%s/locations/%s/queuedResources/%s",
        projectId, zone, queuedResourceId);
    // With these settings the client library handles the Operation's polling mechanism
    // and prevent CancellationException error
    TpuSettings.Builder clientSettings =
        TpuSettings.newBuilder();
    clientSettings
        .deleteQueuedResourceSettings()
        .setRetrySettings(
            RetrySettings.newBuilder()
                .setInitialRetryDelay(Duration.ofMillis(5000L))
                .setRetryDelayMultiplier(2.0)
                .setInitialRpcTimeout(Duration.ZERO)
                .setRpcTimeoutMultiplier(1.0)
                .setMaxRetryDelay(Duration.ofMillis(45000L))
                .setTotalTimeout(Duration.ofHours(24L))
                .build());

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
      DeleteQueuedResourceRequest request =
          DeleteQueuedResourceRequest.newBuilder().setName(name).setForce(true).build();
      // Waiting for updates in the library. Until then, the operation will complete successfully,
      // but the user will receive an error message with UnknownException and IllegalStateException.
      tpuClient.deleteQueuedResourceAsync(request).get();

      System.out.printf("Deleted Queued Resource: %s\n", name);
    }
  }
}

Python

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# queued_resource_name = "resource-name"

client = tpu_v2alpha1.TpuClient()
request = tpu_v2alpha1.DeleteQueuedResourceRequest(
    name=f"projects/{project_id}/locations/{zone}/queuedResources/{queued_resource_name}",
    force=True,  # Set force=True to delete the resource with tpu nodes.
)

try:
    op = client.delete_queued_resource(request=request)
    op.result()
    print(f"Queued resource '{queued_resource_name}' successfully deleted.")
except TypeError as e:
    print(f"Error deleting resource: {e}")
    print(f"Queued resource '{queued_resource_name}' successfully deleted.")

אם מוחקים את ה-TPU ישירות, צריך גם למחוק את המשאב שנוסף לתור, כמו בדוגמה הבאה. כשמוחקים את ה-TPU, בקשת המשאב שבהמתנה עוברת למצב SUSPENDED, ואז אפשר למחוק אותה.

gcloud

מוחקים את ה-TPU:

$ gcloud compute tpus tpu-vm delete your-node-id \
    --project=your-project-id \
    --zone=us-central1-a \
    --quiet

תיאורים של דגלי פקודות

  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור של Cloud TPU שרוצים למחוק.
  • your-node-id: השם של ה-TPU שרוצים למחוק.

כשמוחקים את ה-TPU, המשאב המשויך בתור עובר למצב SUSPENDING ואז למצב SUSPENDED. כשמשאב בתור נמצא במצב SUSPENDED, אפשר למחוק אותו:

gcloud compute tpus queued-resources delete your-queued-resource-id \
    --project your-project-id \
    --zone us-central1-a

תיאורים של דגלי פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור של Cloud TPU שרוצים למחוק.

curl

מוחקים את ה-TPU:

curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project/locations/us-central1-a/nodes?node_id=your-node-id

תיאורים של דגלי פקודות

  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור של Cloud TPU שרוצים למחוק.
  • your-node-id: השם של ה-TPU שרוצים למחוק.

כשמוחקים את ה-TPU, המשאב המשויך בתור עובר למצב SUSPENDING ואז למצב SUSPENDED. כשמשאב בתור נמצא במצב SUSPENDED, אפשר למחוק אותו:

curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project-id/locations/us-central1-a/queuedResources/your-queued-resource-id

תיאורים של דגלי פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור של Cloud TPU שרוצים למחוק.

המסוף

מחיקת TPU:

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. מסמנים את תיבת הסימון לצד ה-TPU.

  3. לוחצים על מחיקה.

כשמוחקים את ה-TPU, המשאב המשויך בתור עובר למצב Suspending ואז למצב Suspended. כשמשאב שנמצא בתור הוא במצב Suspended, אפשר למחוק אותו:

  1. לוחצים על הכרטיסייה משאבים בתור.
  2. מסמנים את התיבה לצד בקשת המשאבים שנמצאת בתור.
  3. לוחצים על מחיקה.

Java

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.api.gax.longrunning.OperationTimedPollAlgorithm;
import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.tpu.v2.DeleteNodeRequest;
import com.google.cloud.tpu.v2.NodeName;
import com.google.cloud.tpu.v2.TpuClient;
import com.google.cloud.tpu.v2.TpuSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.threeten.bp.Duration;

public class DeleteTpuVm {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to create a node.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which to create the TPU.
    // For more information about supported TPU types for specific zones,
    // see https://cloud.google.com/tpu/docs/regions-zones
    String zone = "europe-west4-a";
    // The name for your TPU.
    String nodeName = "YOUR_TPU_NAME";

    deleteTpuVm(projectId, zone, nodeName);
  }

  // Deletes a TPU VM with the specified name in the given project and zone.
  public static void deleteTpuVm(String projectId, String zone, String nodeName)
      throws IOException, ExecutionException, InterruptedException {
    // With these settings the client library handles the Operation's polling mechanism
    // and prevent CancellationException error
    TpuSettings.Builder clientSettings =
        TpuSettings.newBuilder();
    clientSettings
        .deleteNodeOperationSettings()
        .setPollingAlgorithm(
            OperationTimedPollAlgorithm.create(
                RetrySettings.newBuilder()
                    .setInitialRetryDelay(Duration.ofMillis(5000L))
                    .setRetryDelayMultiplier(1.5)
                    .setMaxRetryDelay(Duration.ofMillis(45000L))
                    .setInitialRpcTimeout(Duration.ZERO)
                    .setRpcTimeoutMultiplier(1.0)
                    .setMaxRpcTimeout(Duration.ZERO)
                    .setTotalTimeout(Duration.ofHours(24L))
                    .build()));

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
      String name = NodeName.of(projectId, zone, nodeName).toString();

      DeleteNodeRequest request = DeleteNodeRequest.newBuilder().setName(name).build();

      tpuClient.deleteNodeAsync(request).get();
      System.out.println("TPU VM deleted");
    }
  }
}

כשמוחקים את ה-TPU, המשאב המשויך בתור עובר למצב SUSPENDING ואז למצב SUSPENDED. כשמשאב שנמצא בתור הוא במצב SUSPENDED, אפשר למחוק אותו:

import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class DeleteQueuedResource {
  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which the TPU was created.
    String zone = "us-central1-f";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    deleteQueuedResource(projectId, zone, queuedResourceId);
  }

  // Deletes a Queued Resource asynchronously.
  public static void deleteQueuedResource(String projectId, String zone, String queuedResourceId)
      throws ExecutionException, InterruptedException, IOException {
    String name = String.format("projects/%s/locations/%s/queuedResources/%s",
        projectId, zone, queuedResourceId);
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create()) {
      // Before deleting the queued resource it is required to delete the TPU VM.
      // For more information about deleting TPU
      // see https://cloud.google.com/tpu/docs/managing-tpus-tpu-vm

      DeleteQueuedResourceRequest request =
              DeleteQueuedResourceRequest.newBuilder().setName(name).build();

      tpuClient.deleteQueuedResourceAsync(request).get();
    }
  }
}

Python

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import tpu_v2

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# tpu_name = "tpu-name"

client = tpu_v2.TpuClient()
try:
    client.delete_node(
        name=f"projects/{project_id}/locations/{zone}/nodes/{tpu_name}"
    )
    print("The TPU node was deleted.")
except Exception as e:
    print(e)

כשמוחקים את ה-TPU, המשאב המשויך בתור עובר למצב SUSPENDING ואז למצב SUSPENDED. כשמשאב שנמצא בתור הוא במצב SUSPENDED, אפשר למחוק אותו:

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# queued_resource_name = "resource-name"

client = tpu_v2alpha1.TpuClient()
name = (
    f"projects/{project_id}/locations/{zone}/queuedResources/{queued_resource_name}"
)

try:
    op = client.delete_queued_resource(name=name)
    op.result()
    print(f"Queued resource '{queued_resource_name}' successfully deleted.")
except TypeError as e:
    print(f"Error deleting resource: {e}")
    print(f"Queued resource '{queued_resource_name}' successfully deleted.")

אחזור מידע על סטטוס וניתוח ביצועים של בקשת משאב בתור

אחזור המצב ומידע אבחוני על בקשה למשאב שנמצאת בתור:

gcloud

gcloud compute tpus queued-resources describe queued-resource-request-id \
    --project your-project-id \
    --zone us-central1-a

תיאורים של דגלי פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.

curl

curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project-id/locations/us-central1-a/queuedResources/your-queued-resource-id

תיאורים של דגלי פקודות

  • queued-resource-request-id: המזהה שהמשתמש הקצה לבקשת המשאב שנוספה לתור.
  • project: מזהה הפרויקט שבו מוקצה המשאב שנמצא בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על הכרטיסייה משאבים בתור.

  3. לוחצים על השם של בקשת המשאב שנמצאת בתור.

אחרי הקצאת ה-TPU, אפשר גם לראות פרטים על בקשת המשאב שנמצאת בתור. לשם כך, עוברים אל הדף TPUs, מוצאים את ה-TPU ולוחצים על השם של בקשת המשאב המתאימה שנמצאת בתור.

Java

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.tpu.v2alpha1.GetQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import java.io.IOException;

public class GetQueuedResource {
  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which the TPU was created.
    String zone = "us-central1-f";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    getQueuedResource(projectId, zone, queuedResourceId);
  }

  // Get a Queued Resource.
  public static QueuedResource getQueuedResource(
      String projectId, String zone, String queuedResourceId) throws IOException {
    String name = String.format("projects/%s/locations/%s/queuedResources/%s",
        projectId, zone, queuedResourceId);
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create()) {
      GetQueuedResourceRequest request =
          GetQueuedResourceRequest.newBuilder().setName(name).build();

      return tpuClient.getQueuedResource(request);
    }
  }
}

Python

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# queued_resource_name = "resource-name"

client = tpu_v2alpha1.TpuClient()
name = (
    f"projects/{project_id}/locations/{zone}/queuedResources/{queued_resource_name}"
)
resource = client.get_queued_resource(name=name)
print("Resource name:", resource.name)
print(resource.state)
# Example response:
# Resource name: projects/{project_id}/locations/{zone}/queuedResources/resource-name
# State.ACTIVE

אם הבקשה נכשלת, הפלט יכלול מידע על השגיאה. אם הבקשה ממתינה למשאבים, הפלט ייראה כך:

gcloud

    name: projects/your-project-id/locations/us-central1-a/queuedResources/your-queued-resource-id
    state:
    state: WAITING_FOR_RESOURCES
    tpu:
    nodeSpec:
    - node:
        acceleratorType: v4-8
        bootDisk: {}
        networkConfig:
            enableExternalIps: true
        queuedResource: projects/your-project-number/locations/us-central1-a/queuedResources/your-queued-resource-id
        runtimeVersion: v2-alpha-tpuv5-lite
        schedulingConfig: {}
        serviceAccount: {}
        shieldedInstanceConfig: {}
        useTpuVm: true
        nodeId: your-node-id
        parent: projects/your-project-number/locations/us-central1-a

המסוף

בשדה סטטוס המשאב בתור מוצג הערך בהמתנה למשאבים.

הצגת רשימה של בקשות למשאבים שנמצאות בתור בפרויקט

כדי לראות את רשימת הבקשות למשאבים שנמצאות בתור בפרויקט:

gcloud

gcloud compute tpus queued-resources list --project your-project-id \
    --zone us-central1-a

תיאורים של דגלי פקודות

  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.

curl

curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project-id/locations/your-zone/queuedResources

תיאורים של דגלי פקודות

  • project: הפרויקט ב- Google Cloud שבו מוקצה המשאב בתור.
  • zone: האזור שבו אתם מתכננים ליצור את Cloud TPU.

המסוף

  1. נכנסים לדף TPUs במסוף Google Cloud .

    כניסה לדף TPUs

  2. לוחצים על הכרטיסייה משאבים בתור.

Java

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.tpu.v2alpha1.ListQueuedResourcesRequest;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import com.google.cloud.tpu.v2alpha1.TpuClient.ListQueuedResourcesPage;
import java.io.IOException;

public class ListQueuedResources {
  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which the TPU was created.
    String zone = "us-central1-a";

    listQueuedResources(projectId, zone);
  }

  // List Queued Resources.
  public static ListQueuedResourcesPage listQueuedResources(
      String projectId, String zone) throws IOException {
    String parent = String.format("projects/%s/locations/%s", projectId, zone);
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TpuClient tpuClient = TpuClient.create()) {
      ListQueuedResourcesRequest request =
          ListQueuedResourcesRequest.newBuilder().setParent(parent).build();
      ListQueuedResourcesPage response =  tpuClient.listQueuedResources(request).getPage();

      for (QueuedResource queuedResource : response.iterateAll()) {
        System.out.println(queuedResource.getName());
      }
      return response;
    }
  }
}

Python

כדי לבצע אימות ב-Cloud TPU, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"

client = tpu_v2alpha1.TpuClient()
parent = f"projects/{project_id}/locations/{zone}"
resources = client.list_queued_resources(parent=parent)
for resource in resources:
    print("Resource name:", resource.name)
    print("TPU id:", resource.tpu.node_spec[0].node_id)
# Example response:
# Resource name: projects/{project_id}/locations/{zone}/queuedResources/resource-name
# TPU id: tpu-name