Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compute tpu basic operations #4421

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions compute/tpu/create_tpu_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package snippets

// [START tpu_vm_create]
import (
"context"
"fmt"
"io"

tpu "cloud.google.com/go/tpu/apiv1"
"cloud.google.com/go/tpu/apiv1/tpupb"
)

// createTpuNode creates TPU node in given location
func createTPUNode(w io.Writer, projectID, location, nodeName string) error {
// projectID := "your_project_id"
// location := "europe-central2-b"
// nodeName := "your_instance_name"

ctx := context.Background()
client, err := tpu.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewTpuClient: %w", err)
}
defer client.Close()

req := &tpupb.CreateNodeRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
NodeId: nodeName,
Node: &tpupb.Node{
// Format: v<TPU version>-<number of chips>
// TPU is available only in certain locations.
// Check out the list: https://cloud.google.com/tpu/docs/regions-zones
AcceleratorType: "v2-8",
// Specifies an image, being installed on a VM.
// Apart from OS, image contains also libraries for training models.
// Read more: https://cloud.google.com/tpu/docs/runtimes
TensorflowVersion: "tpu-vm-tf-2.14.1",
},
}

op, err := client.CreateNode(ctx, req)
if err != nil {
return err
}

node, err := op.Wait(ctx)
if err != nil {
return err
}

fmt.Fprintf(w, "Node created: %s", node.GetName())

return nil
}

// [END tpu_vm_create]
58 changes: 58 additions & 0 deletions compute/tpu/delete_tpu_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package snippets

// [START tpu_vm_delete]
import (
"context"
"fmt"
"io"

tpu "cloud.google.com/go/tpu/apiv1"
"cloud.google.com/go/tpu/apiv1/tpupb"
)

// deleteTpuNode deletes TPU node by given name and location within project
func deleteTPUNode(w io.Writer, projectID, location, nodeName string) error {
// projectID := "your_project_id"
// location := "europe-central2-b"
// nodeName := "your_instance_name"

ctx := context.Background()
client, err := tpu.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewTpuClient: %w", err)
}
defer client.Close()

req := &tpupb.DeleteNodeRequest{
Name: fmt.Sprintf("projects/%s/locations/%s/nodes/%s", projectID, location, nodeName),
}

op, err := client.DeleteNode(ctx, req)
if err != nil {
return err
}

node, err := op.Wait(ctx)
if err != nil {
return err
}
fmt.Fprintf(w, "Deleted node: %s", node.GetName())

return nil
}

// [END tpu_vm_delete]
53 changes: 53 additions & 0 deletions compute/tpu/get_tpu_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package snippets

// [START tpu_vm_get]
import (
"context"
"fmt"
"io"

tpu "cloud.google.com/go/tpu/apiv1"
"cloud.google.com/go/tpu/apiv1/tpupb"
)

// getTpuNode gets TPU node by given name and location within project
func getTPUNode(w io.Writer, projectID, location, nodeName string) error {
// projectID := "your_project_id"
// location := "europe-central2-b"
// nodeName := "your_instance_name"

ctx := context.Background()
client, err := tpu.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewTpuClient: %w", err)
}
defer client.Close()

req := &tpupb.GetNodeRequest{
Name: fmt.Sprintf("projects/%s/locations/%s/nodes/%s", projectID, location, nodeName),
}

node, err := client.GetNode(ctx, req)
if err != nil {
return err
}
fmt.Fprintf(w, "Got node: %s", node.GetName())

return nil
}

// [END tpu_vm_get]
45 changes: 45 additions & 0 deletions compute/tpu/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module github.com/GoogleCloudPlatform/golang-samples/compute/tpu

go 1.21

require (
cloud.google.com/go/tpu v1.7.1
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20241002171418-7c4f707f8e6b
)

require (
cloud.google.com/go v0.115.1 // indirect
cloud.google.com/go/auth v0.9.3 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
cloud.google.com/go/compute/metadata v0.5.0 // indirect
cloud.google.com/go/iam v1.2.1 // indirect
cloud.google.com/go/longrunning v0.6.1 // indirect
cloud.google.com/go/storage v1.43.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.3 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/api v0.196.0 // indirect
google.golang.org/genproto v0.0.0-20240930140551-af27646dc61f // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240924160255-9d4c2d233b61 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61 // indirect
google.golang.org/grpc v1.67.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
Loading
Loading