Skip to content

Support "None" dims in model signatures #465

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

Merged
merged 3 commits into from
Sep 17, 2019
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
17 changes: 9 additions & 8 deletions cli/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"

"github.com/cortexlabs/cortex/pkg/consts"
"github.com/cortexlabs/cortex/pkg/lib/cast"
"github.com/cortexlabs/cortex/pkg/lib/console"
"github.com/cortexlabs/cortex/pkg/lib/errors"
"github.com/cortexlabs/cortex/pkg/lib/json"
Expand Down Expand Up @@ -417,24 +418,20 @@ func classificationMetricsTable(apiMetrics *schema.APIMetrics) string {

func describeModelInput(groupStatus *resource.APIGroupStatus, apiEndpoint string) string {
if groupStatus.Available() == 0 {
return "waiting for api to be ready"
return "the model's input schema will be available when the API is live"
}

modelInput, err := getModelInput(urls.Join(apiEndpoint, "signature"))
if err != nil {
return "waiting for api to be ready"
return "error retreiving the model's input schema: " + err.Error()
}

rows := make([][]interface{}, len(modelInput.Signature))
rowNum := 0
for inputName, featureSignature := range modelInput.Signature {
shapeStr := make([]string, len(featureSignature.Shape))
for idx, dim := range featureSignature.Shape {
if dim == 0 {
shapeStr[idx] = "?"
} else {
shapeStr[idx] = s.Int(dim)
}
shapeStr[idx] = s.ObjFlatNoQuotes(dim)
}
rows[rowNum] = []interface{}{
inputName,
Expand Down Expand Up @@ -468,11 +465,15 @@ func getModelInput(infoAPIPath string) (*schema.ModelInput, error) {
}

var modelInput schema.ModelInput
err = json.Unmarshal(response, &modelInput)
err = json.DecodeWithNumber(response, &modelInput)
if err != nil {
return nil, errors.Wrap(err, "unable to parse model input response")
}

for _, featureSignature := range modelInput.Signature {
featureSignature.Shape = cast.JSONNumbers(featureSignature.Shape)
}

return &modelInput, nil
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/lib/cast/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,30 @@ func JSONNumberToIntOrFloat(in interface{}) (interface{}, bool) {
return nil, false
}

func JSONNumber(in interface{}) interface{} {
number, ok := in.(json.Number)
if !ok {
return in
}
inInt, err := number.Int64()
if err == nil {
return inInt
}
inFloat, err := number.Float64()
if err == nil {
return inFloat
}
return in // unexpected
}

func JSONNumbers(in []interface{}) []interface{} {
casted := make([]interface{}, len(in))
for i, element := range in {
casted[i] = JSONNumber(element)
}
return casted
}

func InterfaceToInterfaceSlice(in interface{}) ([]interface{}, bool) {
if in == nil {
return nil, true
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func Marshal(obj interface{}) ([]byte, error) {
return jsonBytes, nil
}

func Unmarshal(data []byte, dst interface{}) error {
if err := json.Unmarshal(data, dst); err != nil {
func Unmarshal(jsonBytes []byte, dst interface{}) error {
if err := json.Unmarshal(jsonBytes, dst); err != nil {
return errors.Wrap(err, errStrUnmarshalJSON)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/api/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ type GetDeploymentsResponse struct {
}

type FeatureSignature struct {
Shape []int `json:"shape"`
Type string `json:"type"`
Shape []interface{} `json:"shape"`
Type string `json:"type"`
}

type ModelInput struct {
Expand Down