Skip to content

Commit 059739e

Browse files
authored
Support "None" dims in model signatures (#465)
1 parent ca0f798 commit 059739e

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

cli/cmd/get.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/spf13/cobra"
2727

2828
"github.com/cortexlabs/cortex/pkg/consts"
29+
"github.com/cortexlabs/cortex/pkg/lib/cast"
2930
"github.com/cortexlabs/cortex/pkg/lib/console"
3031
"github.com/cortexlabs/cortex/pkg/lib/errors"
3132
"github.com/cortexlabs/cortex/pkg/lib/json"
@@ -417,24 +418,20 @@ func classificationMetricsTable(apiMetrics *schema.APIMetrics) string {
417418

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

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

428429
rows := make([][]interface{}, len(modelInput.Signature))
429430
rowNum := 0
430431
for inputName, featureSignature := range modelInput.Signature {
431432
shapeStr := make([]string, len(featureSignature.Shape))
432433
for idx, dim := range featureSignature.Shape {
433-
if dim == 0 {
434-
shapeStr[idx] = "?"
435-
} else {
436-
shapeStr[idx] = s.Int(dim)
437-
}
434+
shapeStr[idx] = s.ObjFlatNoQuotes(dim)
438435
}
439436
rows[rowNum] = []interface{}{
440437
inputName,
@@ -468,11 +465,15 @@ func getModelInput(infoAPIPath string) (*schema.ModelInput, error) {
468465
}
469466

470467
var modelInput schema.ModelInput
471-
err = json.Unmarshal(response, &modelInput)
468+
err = json.DecodeWithNumber(response, &modelInput)
472469
if err != nil {
473470
return nil, errors.Wrap(err, "unable to parse model input response")
474471
}
475472

473+
for _, featureSignature := range modelInput.Signature {
474+
featureSignature.Shape = cast.JSONNumbers(featureSignature.Shape)
475+
}
476+
476477
return &modelInput, nil
477478
}
478479

pkg/lib/cast/interface.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,30 @@ func JSONNumberToIntOrFloat(in interface{}) (interface{}, bool) {
391391
return nil, false
392392
}
393393

394+
func JSONNumber(in interface{}) interface{} {
395+
number, ok := in.(json.Number)
396+
if !ok {
397+
return in
398+
}
399+
inInt, err := number.Int64()
400+
if err == nil {
401+
return inInt
402+
}
403+
inFloat, err := number.Float64()
404+
if err == nil {
405+
return inFloat
406+
}
407+
return in // unexpected
408+
}
409+
410+
func JSONNumbers(in []interface{}) []interface{} {
411+
casted := make([]interface{}, len(in))
412+
for i, element := range in {
413+
casted[i] = JSONNumber(element)
414+
}
415+
return casted
416+
}
417+
394418
func InterfaceToInterfaceSlice(in interface{}) ([]interface{}, bool) {
395419
if in == nil {
396420
return nil, true

pkg/lib/json/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func Marshal(obj interface{}) ([]byte, error) {
3434
return jsonBytes, nil
3535
}
3636

37-
func Unmarshal(data []byte, dst interface{}) error {
38-
if err := json.Unmarshal(data, dst); err != nil {
37+
func Unmarshal(jsonBytes []byte, dst interface{}) error {
38+
if err := json.Unmarshal(jsonBytes, dst); err != nil {
3939
return errors.Wrap(err, errStrUnmarshalJSON)
4040
}
4141
return nil

pkg/operator/api/schema/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ type GetDeploymentsResponse struct {
5454
}
5555

5656
type FeatureSignature struct {
57-
Shape []int `json:"shape"`
58-
Type string `json:"type"`
57+
Shape []interface{} `json:"shape"`
58+
Type string `json:"type"`
5959
}
6060

6161
type ModelInput struct {

0 commit comments

Comments
 (0)