-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow wildcards for GPU attributes in deployment messages Signed-off-by: Artur Troian <[email protected]>
- Loading branch information
Showing
9 changed files
with
313 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,110 @@ | ||
package sdl | ||
|
||
import ( | ||
"sort" | ||
"fmt" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
types "github.com/akash-network/akash-api/go/node/types/v1beta3" | ||
) | ||
|
||
type v2GPUAttributes types.Attributes | ||
type v2GPUNvidia struct { | ||
Model string `yaml:"model"` | ||
RAM *memoryQuantity `yaml:"ram,omitempty"` | ||
} | ||
|
||
func (sdl *v2GPUNvidia) String() string { | ||
key := fmt.Sprintf("%s", sdl.Model) | ||
if sdl.RAM != nil { | ||
key += "/" + sdl.RAM.StringWithSuffix("Gi") | ||
} | ||
|
||
return key | ||
} | ||
|
||
type v2GPUsNvidia []v2GPUNvidia | ||
|
||
type gpuVendor struct { | ||
Nvidia v2GPUsNvidia `yaml:"nvidia,omitempty"` | ||
} | ||
|
||
type v2GPUAttributes struct { | ||
attr types.Attributes | ||
Vendor *gpuVendor `yaml:"vendor,omitempty"` | ||
} | ||
|
||
type v2ResourceGPU struct { | ||
Units gpuQuantity `yaml:"units"` | ||
Attributes v2GPUAttributes `yaml:"attributes,omitempty"` | ||
} | ||
|
||
func (sdl *v2ResourceGPU) UnmarshalYAML(node *yaml.Node) error { | ||
res := v2ResourceGPU{} | ||
|
||
for i := 0; i < len(node.Content); i += 2 { | ||
switch node.Content[i].Value { | ||
case "units": | ||
if err := node.Content[i+1].Decode(&res.Units); err != nil { | ||
return err | ||
} | ||
case "attributes": | ||
if err := node.Content[i+1].Decode(&res.Attributes); err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("sdl: unsupported field (%s) for GPU resource", node.Content[i].Value) | ||
} | ||
} | ||
|
||
if res.Units > 0 && len(res.Attributes.attr) == 0 { | ||
return fmt.Errorf("sdl: GPU attributes must be present if units > 0") | ||
} | ||
|
||
*sdl = res | ||
|
||
return nil | ||
} | ||
|
||
func (sdl *v2GPUAttributes) UnmarshalYAML(node *yaml.Node) error { | ||
var attr v2GPUAttributes | ||
var res v2GPUAttributes | ||
|
||
for i := 0; i+1 < len(node.Content); i += 2 { | ||
var value string | ||
if err := node.Content[i+1].Decode(&value); err != nil { | ||
return err | ||
for i := 0; i < len(node.Content); i += 2 { | ||
switch node.Content[i].Value { | ||
case "vendor": | ||
if err := node.Content[i+1].Decode(&res.Vendor); err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("sdl: unsupported attribute (%s) for GPU resource", node.Content[i].Value) | ||
} | ||
// switch node.Content[i].Value { | ||
// case "arch": | ||
// if err := node.Content[i+1].Decode(&value); err != nil { | ||
// return err | ||
// } | ||
// default: | ||
// return errors.Errorf("unsupported cpu attribute \"%s\"", node.Content[i].Value) | ||
// } | ||
|
||
attr = append(attr, types.Attribute{ | ||
Key: node.Content[i].Value, | ||
Value: value, | ||
} | ||
|
||
if res.Vendor == nil { | ||
return fmt.Errorf("sdl: invalid GPU attributes. at least one vendor must be set") | ||
} | ||
|
||
res.attr = make(types.Attributes, 0, len(res.Vendor.Nvidia)) | ||
|
||
for _, model := range res.Vendor.Nvidia { | ||
res.attr = append(res.attr, types.Attribute{ | ||
Key: fmt.Sprintf("vendor/nvidia/model/%s", model.String()), | ||
Value: "true", | ||
}) | ||
} | ||
|
||
// keys are unique in attributes parsed from sdl so don't need to use sort.SliceStable | ||
sort.Slice(attr, func(i, j int) bool { | ||
return attr[i].Key < attr[j].Key | ||
}) | ||
if len(res.attr) == 0 { | ||
res.attr = append(res.attr, types.Attribute{ | ||
Key: "vendor/nvidia/model/*", | ||
Value: "true", | ||
}) | ||
} | ||
res.attr.Sort() | ||
|
||
if err := res.attr.Validate(); err != nil { | ||
return fmt.Errorf("sdl: invalid GPU attributes: %w", err) | ||
} | ||
|
||
*sdl = attr | ||
*sdl = res | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package sdl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestV2ResourceGPU_EmptyVendor(t *testing.T) { | ||
var stream = ` | ||
units: 1 | ||
attributes: | ||
vendor: | ||
` | ||
var p v2ResourceGPU | ||
|
||
err := yaml.Unmarshal([]byte(stream), &p) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestV2ResourceGPU_Wildcard(t *testing.T) { | ||
var stream = ` | ||
units: 1 | ||
attributes: | ||
vendor: | ||
nvidia: | ||
` | ||
var p v2ResourceGPU | ||
|
||
err := yaml.Unmarshal([]byte(stream), &p) | ||
require.NoError(t, err) | ||
require.Equal(t, gpuQuantity(1), p.Units) | ||
require.Equal(t, 1, len(p.Attributes.attr)) | ||
require.Equal(t, "vendor/nvidia/model/*", p.Attributes.attr[0].Key) | ||
require.Equal(t, "true", p.Attributes.attr[0].Value) | ||
} | ||
|
||
func TestV2ResourceGPU_SingleModel(t *testing.T) { | ||
var stream = ` | ||
units: 1 | ||
attributes: | ||
vendor: | ||
nvidia: | ||
- model: a100 | ||
` | ||
var p v2ResourceGPU | ||
|
||
err := yaml.Unmarshal([]byte(stream), &p) | ||
require.NoError(t, err) | ||
require.Equal(t, gpuQuantity(1), p.Units) | ||
require.Equal(t, 1, len(p.Attributes.attr)) | ||
require.Equal(t, "vendor/nvidia/model/a100", p.Attributes.attr[0].Key) | ||
require.Equal(t, "true", p.Attributes.attr[0].Value) | ||
} | ||
|
||
func TestV2ResourceGPU_SingleModelWithRAM(t *testing.T) { | ||
var stream = ` | ||
units: 1 | ||
attributes: | ||
vendor: | ||
nvidia: | ||
- model: a100 | ||
ram: 80Gi | ||
` | ||
var p v2ResourceGPU | ||
|
||
err := yaml.Unmarshal([]byte(stream), &p) | ||
require.NoError(t, err) | ||
require.Equal(t, gpuQuantity(1), p.Units) | ||
require.Equal(t, 1, len(p.Attributes.attr)) | ||
require.Equal(t, "vendor/nvidia/model/a100/80Gi", p.Attributes.attr[0].Key) | ||
require.Equal(t, "true", p.Attributes.attr[0].Value) | ||
} | ||
|
||
func TestV2ResourceGPU_InvalidRAMUnit(t *testing.T) { | ||
var stream = ` | ||
units: 1 | ||
attributes: | ||
vendor: | ||
nvidia: | ||
- model: a100 | ||
ram: 80G | ||
` | ||
var p v2ResourceGPU | ||
|
||
err := yaml.Unmarshal([]byte(stream), &p) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestV2ResourceGPU_MultipleModels(t *testing.T) { | ||
var stream = ` | ||
units: 1 | ||
attributes: | ||
vendor: | ||
nvidia: | ||
- model: a100 | ||
ram: 80Gi | ||
- model: a100 | ||
ram: 40Gi | ||
` | ||
var p v2ResourceGPU | ||
|
||
err := yaml.Unmarshal([]byte(stream), &p) | ||
require.NoError(t, err) | ||
require.Equal(t, gpuQuantity(1), p.Units) | ||
require.Equal(t, 2, len(p.Attributes.attr)) | ||
require.Equal(t, "vendor/nvidia/model/a100/40Gi", p.Attributes.attr[0].Key) | ||
require.Equal(t, "true", p.Attributes.attr[0].Value) | ||
require.Equal(t, "vendor/nvidia/model/a100/80Gi", p.Attributes.attr[1].Key) | ||
require.Equal(t, "true", p.Attributes.attr[1].Value) | ||
} | ||
|
||
func TestV2ResourceGPU_MultipleModels2(t *testing.T) { | ||
var stream = ` | ||
units: 1 | ||
attributes: | ||
vendor: | ||
nvidia: | ||
- model: a100 | ||
ram: 80Gi | ||
- model: a100 | ||
` | ||
var p v2ResourceGPU | ||
|
||
err := yaml.Unmarshal([]byte(stream), &p) | ||
require.NoError(t, err) | ||
require.Equal(t, gpuQuantity(1), p.Units) | ||
require.Equal(t, 2, len(p.Attributes.attr)) | ||
require.Equal(t, "vendor/nvidia/model/a100", p.Attributes.attr[0].Key) | ||
require.Equal(t, "true", p.Attributes.attr[0].Value) | ||
require.Equal(t, "vendor/nvidia/model/a100/80Gi", p.Attributes.attr[1].Key) | ||
require.Equal(t, "true", p.Attributes.attr[1].Value) | ||
} | ||
|
||
func TestV2ResourceGPU_MultipleModels3(t *testing.T) { | ||
var stream = ` | ||
units: 1 | ||
attributes: | ||
vendor: | ||
nvidia: | ||
- model: a6000 | ||
- model: a40 | ||
` | ||
var p v2ResourceGPU | ||
|
||
err := yaml.Unmarshal([]byte(stream), &p) | ||
require.NoError(t, err) | ||
require.Equal(t, gpuQuantity(1), p.Units) | ||
require.Equal(t, 2, len(p.Attributes.attr)) | ||
require.Equal(t, "vendor/nvidia/model/a40", p.Attributes.attr[0].Key) | ||
require.Equal(t, "true", p.Attributes.attr[0].Value) | ||
require.Equal(t, "vendor/nvidia/model/a6000", p.Attributes.attr[1].Key) | ||
require.Equal(t, "true", p.Attributes.attr[1].Value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.