Skip to content

External model inputs update #159

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 23 commits into from
Jun 13, 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
36 changes: 19 additions & 17 deletions cli/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ func describeAPI(name string, resourcesRes *schema.GetResourcesResponse) (string

ctx := resourcesRes.Context
api := ctx.APIs[name]
model := ctx.Models[api.ModelName]

var staleReplicas int32
var ctxAPIStatus *resource.APIStatus
Expand Down Expand Up @@ -412,26 +411,29 @@ func describeAPI(name string, resourcesRes *schema.GetResourcesResponse) (string
}

out += titleStr("Endpoint")
resIDs := strset.New()
combinedInput := []interface{}{model.Input, model.TrainingInput}
for _, res := range ctx.ExtractCortexResources(combinedInput, resource.ConstantType, resource.RawColumnType, resource.AggregateType, resource.TransformedColumnType) {
resIDs.Add(res.GetID())
resIDs.Merge(ctx.AllComputedResourceDependencies(res.GetID()))
}
var samplePlaceholderFields []string
for rawColumnName, rawColumn := range ctx.RawColumns {
if resIDs.Has(rawColumn.GetID()) {
fieldStr := fmt.Sprintf("\"%s\": %s", rawColumnName, rawColumn.GetColumnType().JSONPlaceholder())
samplePlaceholderFields = append(samplePlaceholderFields, fieldStr)
}
}
sort.Strings(samplePlaceholderFields)
samplesPlaceholderStr := `{ "samples": [ { ` + strings.Join(samplePlaceholderFields, ", ") + " } ] }"
out += "URL: " + urls.Join(resourcesRes.APIsBaseURL, anyAPIStatus.Path) + "\n"
out += "Method: POST\n"
out += `Header: "Content-Type: application/json"` + "\n"
out += "Payload: " + samplesPlaceholderStr + "\n"

if api.Model != nil {
model := ctx.Models[api.ModelName]
resIDs := strset.New()
combinedInput := []interface{}{model.Input, model.TrainingInput}
for _, res := range ctx.ExtractCortexResources(combinedInput, resource.ConstantType, resource.RawColumnType, resource.AggregateType, resource.TransformedColumnType) {
resIDs.Add(res.GetID())
resIDs.Merge(ctx.AllComputedResourceDependencies(res.GetID()))
}
var samplePlaceholderFields []string
for rawColumnName, rawColumn := range ctx.RawColumns {
if resIDs.Has(rawColumn.GetID()) {
fieldStr := fmt.Sprintf("\"%s\": %s", rawColumnName, rawColumn.GetColumnType().JSONPlaceholder())
samplePlaceholderFields = append(samplePlaceholderFields, fieldStr)
}
}
sort.Strings(samplePlaceholderFields)
samplesPlaceholderStr := `{ "samples": [ { ` + strings.Join(samplePlaceholderFields, ", ") + " } ] }"
out += "Payload: " + samplesPlaceholderStr + "\n"
}
if api != nil {
out += resourceStr(api.API)
}
Expand Down
16 changes: 9 additions & 7 deletions cli/cmd/lib_cli_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ func getPromptValidation(defaults *CliConfig) *cr.PromptValidation {
PromptOpts: &cr.PromptOptions{
Prompt: "Enter Cortex operator endpoint",
},
StringValidation: cr.GetURLValidation(&cr.URLValidation{
Required: true,
Default: defaults.CortexURL,
}),
StringValidation: &cr.StringValidation{
Required: true,
Default: defaults.CortexURL,
Validator: cr.GetURLValidator(false, false),
},
},
{
StructField: "AWSAccessKeyID",
Expand Down Expand Up @@ -97,9 +98,10 @@ var fileValidation = &cr.StructValidation{
{
Key: "cortex_url",
StructField: "CortexURL",
StringValidation: cr.GetURLValidation(&cr.URLValidation{
Required: true,
}),
StringValidation: &cr.StringValidation{
Required: true,
Validator: cr.GetURLValidator(false, false),
},
},
{
Key: "aws_access_key_id",
Expand Down
10 changes: 10 additions & 0 deletions cli/cmd/predict.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ var predictCmd = &cobra.Command{
}

for _, prediction := range predictResponse.Predictions {
if prediction.Prediction == nil {
prettyResp, err := json.Pretty(prediction.Response)
if err != nil {
errors.Exit(err)
}

fmt.Println(prettyResp)
continue
}

value := prediction.Prediction
if prediction.PredictionReversed != nil {
value = prediction.PredictionReversed
Expand Down
29 changes: 29 additions & 0 deletions docs/applications/advanced/external-models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Importing External Models

You can serve a model that was trained outside of Cortex as an API.

1. Zip the exported estimator output in your checkpoint directory, e.g.

```bash
$ ls export/estimator
saved_model.pb variables/

$ zip -r model.zip export/estimator
```

2. Upload the zipped file to Amazon S3, e.g.

```bash
$ aws s3 cp model.zip s3://your-bucket/model.zip
```

3. Specify `model_path` in an API, e.g.

```yaml
- kind: api
name: my-api
model_path: s3://your-bucket/model.zip
compute:
replicas: 5
gpu: 1
```
1 change: 1 addition & 0 deletions docs/applications/resources/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Serve models at scale and use them to build smarter applications.
- kind: api # (required)
name: <string> # API name (required)
model_name: <string> # name of a Cortex model (required)
model_path: <string> # path to a zipped model dir (optional)
compute:
replicas: <int> # number of replicas to launch (default: 1)
cpu: <string> # CPU request (default: Null)
Expand Down
1 change: 1 addition & 0 deletions docs/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* [Compute](applications/advanced/compute.md)
* [Python Packages](applications/advanced/python-packages.md)
* [Development](development.md)
* [Importing External Models](applications/advanced/external-models.md)

## Operator

Expand Down
8 changes: 8 additions & 0 deletions examples/external-model/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- kind: app
name: iris

- kind: api
name: iris
model_path: s3://cortex-examples/iris-model.zip
compute:
replicas: 1
10 changes: 10 additions & 0 deletions examples/external-model/samples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"samples": [
{
"sepal_length": 5.2,
"sepal_width": 3.6,
"petal_length": 1.4,
"petal_width": 0.3
}
]
}
11 changes: 10 additions & 1 deletion pkg/lib/aws/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ type ErrorKind int
const (
ErrUnknown ErrorKind = iota
ErrInvalidS3aPath
ErrInvalidS3Path
ErrAuth
)

var errorKinds = []string{
"err_unknown",
"err_invalid_s3a_path",
"err_invalid_s3_path",
"err_auth",
}

Expand Down Expand Up @@ -105,7 +107,14 @@ func (e Error) Error() string {
func ErrorInvalidS3aPath(provided string) error {
return Error{
Kind: ErrInvalidS3aPath,
message: fmt.Sprintf("%s is not a valid s3a path", s.UserStr(provided)),
message: fmt.Sprintf("%s is not a valid s3a path (e.g. s3a://cortex-examples/iris.csv is a valid s3a path)", s.UserStr(provided)),
}
}

func ErrorInvalidS3Path(provided string) error {
return Error{
Kind: ErrInvalidS3Path,
message: fmt.Sprintf("%s is not a valid s3 path (e.g. s3://cortex-examples/iris-model.zip is a valid s3 path)", s.UserStr(provided)),
}
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/lib/aws/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ func (c *Client) DeleteFromS3ByPrefix(prefix string, continueIfFailure bool) err
return errors.Wrap(err, prefix)
}

func IsValidS3Path(s3Path string) bool {
if !strings.HasPrefix(s3Path, "s3://") {
return false
}
parts := strings.Split(s3Path[5:], "/")
if len(parts) < 2 {
return false
}
if parts[0] == "" || parts[1] == "" {
return false
}
return true
}

func IsValidS3aPath(s3aPath string) bool {
if !strings.HasPrefix(s3aPath, "s3a://") {
return false
Expand Down
13 changes: 11 additions & 2 deletions pkg/lib/configreader/float32_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Float32PtrValidation struct {
GreaterThanOrEqualTo *float32
LessThan *float32
LessThanOrEqualTo *float32
Validator func(*float32) (*float32, error)
Validator func(float32) (float32, error)
}

func makeFloat32ValValidation(v *Float32PtrValidation) *Float32Validation {
Expand Down Expand Up @@ -171,8 +171,17 @@ func validateFloat32Ptr(val *float32, v *Float32PtrValidation) (*float32, error)
}
}

if val == nil {
return val, nil
}

if v.Validator != nil {
return v.Validator(val)
validated, err := v.Validator(*val)
if err != nil {
return nil, err
}
return &validated, nil
}

return val, nil
}
13 changes: 11 additions & 2 deletions pkg/lib/configreader/float64_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Float64PtrValidation struct {
GreaterThanOrEqualTo *float64
LessThan *float64
LessThanOrEqualTo *float64
Validator func(*float64) (*float64, error)
Validator func(float64) (float64, error)
}

func makeFloat64ValValidation(v *Float64PtrValidation) *Float64Validation {
Expand Down Expand Up @@ -171,8 +171,17 @@ func validateFloat64Ptr(val *float64, v *Float64PtrValidation) (*float64, error)
}
}

if val == nil {
return val, nil
}

if v.Validator != nil {
return v.Validator(val)
validated, err := v.Validator(*val)
if err != nil {
return nil, err
}
return &validated, nil
}

return val, nil
}
13 changes: 11 additions & 2 deletions pkg/lib/configreader/int32_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Int32PtrValidation struct {
GreaterThanOrEqualTo *int32
LessThan *int32
LessThanOrEqualTo *int32
Validator func(*int32) (*int32, error)
Validator func(int32) (int32, error)
}

func makeInt32ValValidation(v *Int32PtrValidation) *Int32Validation {
Expand Down Expand Up @@ -171,8 +171,17 @@ func validateInt32Ptr(val *int32, v *Int32PtrValidation) (*int32, error) {
}
}

if val == nil {
return val, nil
}

if v.Validator != nil {
return v.Validator(val)
validated, err := v.Validator(*val)
if err != nil {
return nil, err
}
return &validated, nil
}

return val, nil
}
13 changes: 11 additions & 2 deletions pkg/lib/configreader/int64_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Int64PtrValidation struct {
GreaterThanOrEqualTo *int64
LessThan *int64
LessThanOrEqualTo *int64
Validator func(*int64) (*int64, error)
Validator func(int64) (int64, error)
}

func makeInt64ValValidation(v *Int64PtrValidation) *Int64Validation {
Expand Down Expand Up @@ -171,8 +171,17 @@ func validateInt64Ptr(val *int64, v *Int64PtrValidation) (*int64, error) {
}
}

if val == nil {
return val, nil
}

if v.Validator != nil {
return v.Validator(val)
validated, err := v.Validator(*val)
if err != nil {
return nil, err
}
return &validated, nil
}

return val, nil
}
13 changes: 11 additions & 2 deletions pkg/lib/configreader/int_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type IntPtrValidation struct {
GreaterThanOrEqualTo *int
LessThan *int
LessThanOrEqualTo *int
Validator func(*int) (*int, error)
Validator func(int) (int, error)
}

func makeIntValValidation(v *IntPtrValidation) *IntValidation {
Expand Down Expand Up @@ -171,8 +171,17 @@ func validateIntPtr(val *int, v *IntPtrValidation) (*int, error) {
}
}

if val == nil {
return val, nil
}

if v.Validator != nil {
return v.Validator(val)
validated, err := v.Validator(*val)
if err != nil {
return nil, err
}
return &validated, nil
}

return val, nil
}
13 changes: 11 additions & 2 deletions pkg/lib/configreader/string_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type StringPtrValidation struct {
DNS1123 bool
AllowCortexResources bool
RequireCortexResources bool
Validator func(*string) (*string, error)
Validator func(string) (string, error)
}

func makeStringValValidation(v *StringPtrValidation) *StringValidation {
Expand Down Expand Up @@ -170,8 +170,17 @@ func validateStringPtr(val *string, v *StringPtrValidation) (*string, error) {
}
}

if val == nil {
return val, nil
}

if v.Validator != nil {
return v.Validator(val)
validated, err := v.Validator(*val)
if err != nil {
return nil, err
}
return &validated, nil
}

return val, nil
}
Loading