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

[WIP] add study run cmd and study controller #86

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func NewRootCommand() *cobra.Command {

//add command
cmd.AddCommand(NewCommandCreate())
cmd.AddCommand(NewCommandRun())
cmd.AddCommand(NewCommandGet())
cmd.AddCommand(NewCommandStop())
cmd.AddCommand(NewCommandPush())
Expand Down
72 changes: 72 additions & 0 deletions cmd/cli/run-study.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"context"
"fmt"
"io/ioutil"
"log"

"github.com/kubeflow/katib/pkg/api"
"github.com/spf13/cobra"
"google.golang.org/grpc"
yaml "gopkg.in/yaml.v2"
)

type runStudyOpt struct {
conf string
args []string
}

//NewCommandRunStudy generate create study cmd
func NewCommandRunStudy() *cobra.Command {
var opt runStudyOpt
cmd := &cobra.Command{
Use: "study",
Args: cobra.NoArgs,
Short: "Run a study from a file",
Long: "YAML formats are accepted.",
Aliases: []string{"st"},
Run: func(cmd *cobra.Command, args []string) {
opt.args = args
runStudy(cmd, &opt)
},
}
cmd.Flags().StringVarP(&opt.conf, "config", "f", "", "File path of study controller config(required)")
cmd.MarkFlagRequired("config")
return cmd
}

func runStudy(cmd *cobra.Command, opt *runStudyOpt) {
//check and get persistent flag volume
var pf *PersistentFlags
pf, err := CheckPersistentFlags()
if err != nil {
log.Fatalf("Fail to Check Flags: %v", err)
return
}

var rsr *api.RunStudyRequest
buf, err := ioutil.ReadFile(opt.conf)
if err != nil {
log.Fatalf("Fail to open %s: %v", opt.conf, err)
return
}
err = yaml.Unmarshal(buf, &rsr)
if err != nil {
log.Fatalf("Fail to Unmarshal %s: %v", opt.conf, err)
return
}

conn, err := grpc.Dial(pf.server, grpc.WithInsecure())
if err != nil {
log.Fatalf("could not connect: %v", err)
return
}
defer conn.Close()
c := api.NewManagerClient(conn)
r, err := c.RunStudy(context.Background(), rsr)
if err != nil {
log.Fatalf("RunStudy failed: %v", err)
}
fmt.Printf("Study Controller %v is created.\n", r.StudyControllerId)
}
18 changes: 18 additions & 0 deletions cmd/cli/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/spf13/cobra"
)

//NewCommandRun generate run cmd
func NewCommandRun() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Run study or Trial from a file",
Long: `YAML format is accepted.`,
}

cmd.AddCommand(NewCommandRunStudy())

return cmd
}
75 changes: 75 additions & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
pb "github.com/kubeflow/katib/pkg/api"
kdb "github.com/kubeflow/katib/pkg/db"
"github.com/kubeflow/katib/pkg/manager/modelstore"
"github.com/kubeflow/katib/pkg/manager/studycontroller"
tbif "github.com/kubeflow/katib/pkg/manager/visualise/tensorboard"
"github.com/kubeflow/katib/pkg/manager/worker"
k8swif "github.com/kubeflow/katib/pkg/manager/worker/kubernetes"
Expand Down Expand Up @@ -81,6 +82,80 @@ func (s *server) CreateTrial(ctx context.Context, in *pb.CreateTrialRequest) (*p
return &pb.CreateTrialReply{TrialId: in.Trial.TrialId}, err
}

func (s *server) RunStudy(ctx context.Context, in *pb.RunStudyRequest) (*pb.RunStudyReply, error) {
log.Println("Call RunStudy")
if in.WorkerConfig == nil {
return &pb.RunStudyReply{}, errors.New("WorkerConfig must be set")
}
csr, err := s.CreateStudy(ctx, &pb.CreateStudyRequest{
StudyConfig: in.StudyConfig,
})
if err != nil {
return &pb.RunStudyReply{}, err
}
log.Println("Call CreateStudy")
var spId, epId string
if in.DefaultSuggestionParameters != nil {
spr, err := s.SetSuggestionParameters(ctx, &pb.SetSuggestionParametersRequest{
StudyId: csr.StudyId,
SuggestionAlgorithm: in.StudyConfig.DefaultSuggestionAlgorithm,
SuggestionParameters: in.DefaultSuggestionParameters,
})
if err != nil {
return &pb.RunStudyReply{}, err
}
spId = spr.ParamId
}
log.Println("Call SetSuggestionParameters")
if in.DefaultEarlyStoppingParameters != nil {
epr, err := s.SetEarlyStoppingParameters(ctx, &pb.SetEarlyStoppingParametersRequest{
StudyId: csr.StudyId,
EarlyStoppingAlgorithm: in.StudyConfig.DefaultEarlyStoppingAlgorithm,
EarlyStoppingParameters: in.DefaultEarlyStoppingParameters,
})
if err != nil {
return &pb.RunStudyReply{}, err
}
epId = epr.ParamId
}
log.Println("Call SetEarlyStoppingParameters")
scId, err := dbIf.CreateStudyController(&pb.StudyController{
StudyId: csr.StudyId,
SuggestionAlgorithm: in.StudyConfig.DefaultSuggestionAlgorithm,
SuggestionParamId: spId,
EarlystoppingAlgorithm: in.StudyConfig.DefaultEarlyStoppingAlgorithm,
EarlystoppingParamId: epId,
UpdateInterval: in.UpdateInterval,
MaxParallel: in.MaxParallel,
RequestSuggestionNum: in.RequestSuggestionNum,
EarlystoppingInterval: in.EarlystoppingInterval,
WorkerConfig: in.WorkerConfig,
})
if err != nil {
return &pb.RunStudyReply{}, err
}
log.Println("Call CreateStudyController")
sctl := studycontroller.NewStudyControllerDefault()
err = sctl.Run("localhost:6789", scId)
if err != nil {
return &pb.RunStudyReply{}, err
}
log.Println("Call NewStudyControllerDefault")
return &pb.RunStudyReply{
StudyControllerId: scId,
}, nil
}

func (s *server) GetStudyController(ctx context.Context, in *pb.GetStudyControllerRequest) (*pb.GetStudyControllerReply, error) {
sctl, err := dbIf.GetStudyController(in.StudyControllerId)
if err != nil {
return &pb.GetStudyControllerReply{}, err
}
return &pb.GetStudyControllerReply{
StudyController: sctl,
}, nil
}

func (s *server) GetTrials(ctx context.Context, in *pb.GetTrialsRequest) (*pb.GetTrialsReply, error) {
tl, err := dbIf.GetTrialList(in.StudyId)
return &pb.GetTrialsReply{Trials: tl}, err
Expand Down
Loading