optimizer.go 3.2 KB
Newer Older
Z
Zhipeng Xie 已提交
1 2
/*
 * Copyright (c) 2019 Huawei Technologies Co., Ltd.
3 4 5
 * A-Tune is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
6
 *     http://license.coscl.org.cn/MulanPSL2
Z
Zhipeng Xie 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
10
 * See the Mulan PSL v2 for more details.
Z
Zhipeng Xie 已提交
11 12 13 14 15 16 17
 * Create: 2019-10-29
 */

package models

import (
	"encoding/json"
18
	"fmt"
19 20
	"gitee.com/openeuler/A-Tune/common/config"
	"gitee.com/openeuler/A-Tune/common/http"
Z
Zhipeng Xie 已提交
21 22 23 24 25
	"io/ioutil"
)

// OptimizerPostBody send to the service to create a optimizer task
type OptimizerPostBody struct {
26 27 28 29 30 31 32 33
	MaxEval       int32      `json:"max_eval"`
	Knobs         []Knob     `json:"knobs"`
	Engine        string     `json:"engine"`
	RandomStarts  int32      `json:"random_starts"`
	Xref          [][]string `json:"x_ref,omitempty"`
	Yref          []string   `json:"y_ref,omitempty"`
	FeatureFilter bool       `json:"feature_filter"`
	SplitCount    int32      `json:"split_count"`
34 35
	Noise         float64    `json:"noise"`
	SelFeature    bool       `json:"sel_feature"`
Z
Zhipeng Xie 已提交
36 37 38 39
}

// Knob body store the tuning properties
type Knob struct {
40 41 42 43 44 45 46 47
	Dtype   string    `json:"dtype"`
	Name    string    `json:"name"`
	Options []string  `json:"options"`
	Type    string    `json:"type"`
	Range   []float32 `json:"range"`
	Items   []float32 `json:"items"`
	Step    float32   `json:"step"`
	Ref     string    `json:"ref"`
Z
Zhipeng Xie 已提交
48 49 50 51
}

// RespPostBody :the body returned of create optimizer task
type RespPostBody struct {
52 53 54
	TaskID  string `json:"task_id"`
	Status  string `json:"status"`
	Message string `json:"message"`
55
	Iters   int    `json:"iters"`
Z
Zhipeng Xie 已提交
56 57 58 59 60 61 62 63 64 65
}

// OptimizerPutBody send to the optimizer service when iterations
type OptimizerPutBody struct {
	Iterations int    `json:"iterations"`
	Value      string `json:"value"`
}

// RespPutBody :the body returned of each optimizer iteration
type RespPutBody struct {
66 67 68 69
	Param    string `json:"param"`
	Message  string `json:"message"`
	Rank     string `json:"rank"`
	Finished bool   `json:"finished"`
Z
Zhipeng Xie 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

// Post method create a optimizer task
func (o *OptimizerPostBody) Post() (*RespPostBody, error) {
	url := config.GetURL(config.OptimizerURI)
	res, err := http.Post(url, o)
	if err != nil {
		return nil, err
	}

	defer res.Body.Close()

	respBody, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

87 88 89 90
	if res.StatusCode != 200 {
		return nil, fmt.Errorf(string(respBody))
	}

Z
Zhipeng Xie 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	respPostIns := new(RespPostBody)

	err = json.Unmarshal(respBody, respPostIns)
	if err != nil {
		return nil, err
	}

	return respPostIns, nil
}

// Put method send benchmark result to optimizer service
func (o *OptimizerPutBody) Put(url string) (*RespPutBody, error) {
	res, err := http.Put(url, o)
	if err != nil {
		return nil, err
	}

	defer res.Body.Close()

	respBody, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	respPutIns := new(RespPutBody)

	err = json.Unmarshal(respBody, respPutIns)
	if err != nil {
		return nil, err
	}

122 123 124 125
	if res.StatusCode != 200 {
		return nil, fmt.Errorf(respPutIns.Message)
	}

Z
Zhipeng Xie 已提交
126 127
	return respPutIns, nil
}