Skip to content

Commit

Permalink
refactor op infer
Browse files Browse the repository at this point in the history
  • Loading branch information
SiNZeRo committed Jun 1, 2018
1 parent 57ddabb commit 7d4c070
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
24 changes: 24 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package vm

import (
"errors"
"math/big"
"sync/atomic"
"time"
Expand Down Expand Up @@ -444,6 +445,29 @@ func (evm *EVM) CallExternal(call_type string, input [][]byte) []byte {
return []byte{0}
}

// infer function that returns an int64 as output, can be used a categorical output
func (evm *EVM) Infer(model_meta_hash []byte, input_meta_hash []byte) ([]byte, error) {
requestBody := fmt.Sprintf(`{"model_addr":"%x", "input_addr":"%x"}`, model_meta_hash, input_meta_hash)
fmt.Println(requestBody)
resp, err := resty.R().
SetHeader("Content-Type", "application/json").
SetBody(requestBody).
Post("http://127.0.0.1:5000/infer")
if err != nil {
return []byte{}, errors.New("evm.Infer: External Call Error")
}
fmt.Println(resp.String())
js, _ := simplejson.NewJson([]byte(resp.String()))
int_output_tmp, out_err := js.Get("info").String()
int_output, err := strconv.Atoi(int_output_tmp)
fmt.Println("out: ", int_output, "err:", out_err, " resp", resp.String(), "js", js)
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, int64(int_output)); err != nil {
return []byte{}, errors.New("evm.Infer: Type Conversion Error")
}
return buf.Bytes(), nil
}

func BinaryWrite(x interface{}) []byte {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, x)
Expand Down
7 changes: 2 additions & 5 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,10 @@ func opInfer(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *St
stack.push(evm.interpreter.intPool.get().SetUint64(1))
}

var (
ret []byte
)
fmt.Println("model, input", modelMeta.Hash, inputMeta.Hash)
fmt.Println("model, input", string(modelMeta.Hash.Bytes()), string(inputMeta.Hash.Bytes()))
ret = append(ret, evm.CallExternal("infer", [][]byte{modelMeta.Hash.Bytes(), inputMeta.Hash.Bytes()})...)
memory.Set(offset.Uint64(), size.Uint64(), ret)
output, err := evm.Infer(modelMeta.Hash.Bytes(), inputMeta.Hash.Bytes())
memory.Set(offset.Uint64(), size.Uint64(), output)
_, _ = inputMeta, modelMeta
return nil, nil
}
Expand Down

0 comments on commit 7d4c070

Please sign in to comment.