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

Add accelerator_config to google_tpu_v2_vm #6559

Merged
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
3 changes: 3 additions & 0 deletions .changelog/9332.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
tpuv2: added`accelerator_config` field to `google_tpu_v2_vm` resource
```
122 changes: 117 additions & 5 deletions google-beta/services/tpuv2/resource_tpu_v2_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package tpuv2

import (
"context"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -63,6 +64,21 @@ func normalizeScopes(scopes []string) []string {
return result
}

// For backwards compatibility, we need to maintain original behavior where `accelerator_type`
// defaults "v2-8" when nothing is set. However, if the newly introduced `accelerator_config` field
// is set, then use that value instead of the `accelerator_type` default.
func acceleratorTypeCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
_, isTypeSet := diff.GetOk("accelerator_type")
_, isConfigSet := diff.GetOk("accelerator_config")
if !isTypeSet && !isConfigSet {
if err := diff.SetNew("accelerator_type", "v2-8"); err != nil {
return err
}
}

return nil
}

func ResourceTpuV2Vm() *schema.Resource {
return &schema.Resource{
Create: resourceTpuV2VmCreate,
Expand All @@ -81,6 +97,7 @@ func ResourceTpuV2Vm() *schema.Resource {
},

CustomizeDiff: customdiff.All(
acceleratorTypeCustomizeDiff,
tpgresource.SetLabelsDiff,
tpgresource.DefaultProviderProject,
),
Expand All @@ -98,12 +115,41 @@ func ResourceTpuV2Vm() *schema.Resource {
ForceNew: true,
Description: `Runtime version for the TPU.`,
},
"accelerator_config": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `The AccleratorConfig for the TPU Node. 'accelerator_config' cannot be used at the same time
as 'accelerator_type'. If neither is specified, 'accelerator_type' defaults to 'v2-8'.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"topology": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Topology of TPU in chips.`,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"V2", "V3", "V4"}),
Description: `Type of TPU. Possible values: ["V2", "V3", "V4"]`,
},
},
},
ConflictsWith: []string{"accelerator_type"},
},
"accelerator_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `TPU accelerator type for the TPU. If not specified, this defaults to 'v2-8'.`,
Default: "v2-8",
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `TPU accelerator type for the TPU. 'accelerator_type' cannot be used at the same time as
'accelerator_config'. If neither is specified, 'accelerator_type' defaults to 'v2-8'.`,
ConflictsWith: []string{"accelerator_config"},
},
"cidr_block": {
Type: schema.TypeString,
Expand Down Expand Up @@ -478,6 +524,12 @@ func resourceTpuV2VmCreate(d *schema.ResourceData, meta interface{}) error {
} else if v, ok := d.GetOkExists("shielded_instance_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(shieldedInstanceConfigProp)) && (ok || !reflect.DeepEqual(v, shieldedInstanceConfigProp)) {
obj["shieldedInstanceConfig"] = shieldedInstanceConfigProp
}
acceleratorConfigProp, err := expandTpuV2VmAcceleratorConfig(d.Get("accelerator_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("accelerator_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(acceleratorConfigProp)) && (ok || !reflect.DeepEqual(v, acceleratorConfigProp)) {
obj["acceleratorConfig"] = acceleratorConfigProp
}
metadataProp, err := expandTpuV2VmMetadata(d.Get("metadata"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -635,6 +687,9 @@ func resourceTpuV2VmRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("shielded_instance_config", flattenTpuV2VmShieldedInstanceConfig(res["shieldedInstanceConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading Vm: %s", err)
}
if err := d.Set("accelerator_config", flattenTpuV2VmAcceleratorConfig(res["acceleratorConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading Vm: %s", err)
}
if err := d.Set("labels", flattenTpuV2VmLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Vm: %s", err)
}
Expand Down Expand Up @@ -1016,6 +1071,29 @@ func flattenTpuV2VmShieldedInstanceConfigEnableSecureBoot(v interface{}, d *sche
return v
}

func flattenTpuV2VmAcceleratorConfig(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["type"] =
flattenTpuV2VmAcceleratorConfigType(original["type"], d, config)
transformed["topology"] =
flattenTpuV2VmAcceleratorConfigTopology(original["topology"], d, config)
return []interface{}{transformed}
}
func flattenTpuV2VmAcceleratorConfigType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenTpuV2VmAcceleratorConfigTopology(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenTpuV2VmLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -1381,6 +1459,40 @@ func expandTpuV2VmShieldedInstanceConfigEnableSecureBoot(v interface{}, d tpgres
return v, nil
}

func expandTpuV2VmAcceleratorConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedType, err := expandTpuV2VmAcceleratorConfigType(original["type"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedType); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["type"] = transformedType
}

transformedTopology, err := expandTpuV2VmAcceleratorConfigTopology(original["topology"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedTopology); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["topology"] = transformedTopology
}

return transformed, nil
}

func expandTpuV2VmAcceleratorConfigType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandTpuV2VmAcceleratorConfigTopology(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandTpuV2VmMetadata(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ resource "google_tpu_v2_vm" "tpu" {
description = "Text description of the TPU."

runtime_version = "tpu-vm-tf-2.13.0"
accelerator_type = "v2-8"

accelerator_config {
type = "V2"
topology = "2x2"
}

cidr_block = "10.0.0.0/29"

Expand Down
26 changes: 24 additions & 2 deletions website/docs/r/tpu_v2_vm.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ resource "google_tpu_v2_vm" "tpu" {
description = "Text description of the TPU."

runtime_version = "tpu-vm-tf-2.13.0"
accelerator_type = "v2-8"

accelerator_config {
type = "V2"
topology = "2x2"
}

cidr_block = "10.0.0.0/29"

Expand Down Expand Up @@ -172,7 +176,8 @@ The following arguments are supported:

* `accelerator_type` -
(Optional)
TPU accelerator type for the TPU. If not specified, this defaults to 'v2-8'.
TPU accelerator type for the TPU. `accelerator_type` cannot be used at the same time as
`accelerator_config`. If neither is specified, `accelerator_type` defaults to 'v2-8'.

* `description` -
(Optional)
Expand Down Expand Up @@ -213,6 +218,12 @@ The following arguments are supported:
Shielded Instance options.
Structure is [documented below](#nested_shielded_instance_config).

* `accelerator_config` -
(Optional)
The AccleratorConfig for the TPU Node. `accelerator_config` cannot be used at the same time
as `accelerator_type`. If neither is specified, `accelerator_type` defaults to 'v2-8'.
Structure is [documented below](#nested_accelerator_config).

* `labels` -
(Optional)
Resource labels to represent user-provided metadata.
Expand Down Expand Up @@ -302,6 +313,17 @@ The following arguments are supported:
(Required)
Defines whether the instance has Secure Boot enabled.

<a name="nested_accelerator_config"></a>The `accelerator_config` block supports:

* `type` -
(Required)
Type of TPU.
Possible values are: `V2`, `V3`, `V4`.

* `topology` -
(Required)
Topology of TPU in chips.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down