Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.
Open
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
119 changes: 119 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package graphpipe

import (
fb "github.com/google/flatbuffers/go"
graphpipefb "github.com/oracle/graphpipe-go/graphpipefb"
)

// BuildInferRequest constructs an InferRequest flatbuffer from NativeTensor
func BuildInferRequest(config string, inputTensors []*NativeTensor, inputs, outputs []string) (*fb.Builder, fb.UOffsetT) {
b := fb.NewBuilder(1024)
inStrs := make([]fb.UOffsetT, len(inputs))
outStrs := make([]fb.UOffsetT, len(outputs))

for i := range inputs {
inStr := b.CreateString(inputs[i])
inStrs[i] = inStr
}

for i := range outputs {
outStr := b.CreateString(outputs[i])
outStrs[i] = outStr
}
graphpipefb.InferRequestStartInputNamesVector(b, len(inputs))

for _, offset := range inStrs {
b.PrependUOffsetT(offset)
}
inputNamesOffset := b.EndVector(len(inputs))
graphpipefb.InferRequestStartOutputNamesVector(b, len(outputs))
for _, offset := range outStrs {
b.PrependUOffsetT(offset)
}
outputNamesOffset := b.EndVector(len(outputs))

inputOffsets := make([]fb.UOffsetT, len(inputTensors))
for i := 0; i < len(inputTensors); i++ {
tp := inputTensors[i]
inputOffsets[i] = tp.Build(b)
}

graphpipefb.InferRequestStartInputTensorsVector(b, 1)
for _, offset := range inputOffsets {
b.PrependUOffsetT(offset)
}
inputTensorsOffset := b.EndVector(1)

configString := b.CreateString(config)
graphpipefb.InferRequestStart(b)
graphpipefb.InferRequestAddInputNames(b, inputNamesOffset)
graphpipefb.InferRequestAddOutputNames(b, outputNamesOffset)
graphpipefb.InferRequestAddInputTensors(b, inputTensorsOffset)
graphpipefb.InferRequestAddConfig(b, configString)
inferRequestOffset := graphpipefb.InferRequestEnd(b)
return b, inferRequestOffset
}

// ParseInferResponse constructs a NativeTensor from flatbuffer
func ParseInferResponse(inferResponse *graphpipefb.InferResponse) []*NativeTensor {
tensors := []*NativeTensor{}

for i := 0; i < inferResponse.OutputTensorsLength(); i++ {

t := graphpipefb.Tensor{}
inferResponse.OutputTensors(&t, i)
shape := []int64{}
for j := 0; j < t.ShapeLength(); j++ {
shape = append(shape, t.Shape(j))
}
nt := &NativeTensor{}
nt.InitWithData(t.DataBytes(), shape, t.Type())
tensors = append(tensors, nt)

}
return tensors
}

// BuildMetadataRequest constructs flatbuffer from NativeMetadataRequest
func BuildMetadataRequest() (*fb.Builder, fb.UOffsetT) {
b := fb.NewBuilder(1024)
graphpipefb.MetadataRequestStart(b)
metaReq := graphpipefb.MetadataRequestEnd(b)

graphpipefb.RequestStart(b)
graphpipefb.RequestAddReq(b, metaReq)
graphpipefb.RequestAddReqType(b, graphpipefb.ReqMetadataRequest)
req := graphpipefb.RequestEnd(b)
return b, req
}

func parseIO(io *graphpipefb.IOMetadata) NativeIOMetadata {
nio := NativeIOMetadata{}
nio.Name = string(io.Name())
nio.Description = string(io.Description())
nio.Type = io.Type()
for i := 0; i < io.ShapeLength(); i++ {
nio.Shape = append(nio.Shape, io.Shape(i))
}
return nio
}

// ParseMetadataResponse constructs a NativeMetadataRequest from flatbuffer
func ParseMetadataResponse(metadataResponse *graphpipefb.MetadataResponse) *NativeMetadataResponse {
nm := &NativeMetadataResponse{}
nm.Version = string(metadataResponse.Version())
nm.Server = string(metadataResponse.Server())
nm.Description = string(metadataResponse.Description())

for i := 0; i < metadataResponse.InputsLength(); i++ {
io := &graphpipefb.IOMetadata{}
metadataResponse.Inputs(io, i)
nm.Inputs = append(nm.Inputs, parseIO(io))
}
for i := 0; i < metadataResponse.OutputsLength(); i++ {
io := &graphpipefb.IOMetadata{}
metadataResponse.Outputs(io, i)
nm.Outputs = append(nm.Outputs, parseIO(io))
}
return nm
}
32 changes: 29 additions & 3 deletions cmd/graphpipe-echo/main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
package main

import (
"os"

"github.com/Sirupsen/logrus"
graphpipe "github.com/oracle/graphpipe-go"
"github.com/spf13/cobra"
)

func main() {
logrus.SetLevel(logrus.InfoLevel)
type options struct {
listen string
}

func runEchoServer(listen string) {
useCache := false // toggle caching on/off
inShapes := [][]int64(nil) // Optionally set input shapes
outShapes := [][]int64(nil) // Optionally set output shapes
if err := graphpipe.Serve("0.0.0.0:9000", useCache, apply, inShapes, outShapes); err != nil {
if err := graphpipe.Serve(listen, useCache, apply, inShapes, outShapes); err != nil {
logrus.Errorf("Failed to serve: %v", err)
}
}

func main() {
var opts options
var cmdExitCode int
logrus.SetLevel(logrus.InfoLevel)

cmd := cobra.Command{
Use: "graphpipe-echo",
Short: "graphpipe-echo - echoing ml requests",
Run: func(cmd *cobra.Command, args []string) {
runEchoServer(opts.listen)
},
}

f := cmd.Flags()
f.StringVarP(&opts.listen, "listen", "l", "127.0.0.1:9000", "listen string")

cmd.Execute()
os.Exit(cmdExitCode)
}

func apply(requestContext *graphpipe.RequestContext, ignore string, in interface{}) interface{} {
return in // using the graphpipe.Serve interface, graphpipe automatically converts go native types to tensors.
}
21 changes: 21 additions & 0 deletions examples/grpc_infer_client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
graphpipe "github.com/oracle/graphpipe-go"
)

var addr = "grpc+http://127.0.0.1:9000"

/// Example of a client that talks to graphpipe-echo
func main() {
v := []int64{2, 2}
output, err := graphpipe.Remote(addr, v)
if err != nil {
fmt.Println(err)
} else {
echoData := output.([]int64)
fmt.Println(echoData)

}
}
19 changes: 19 additions & 0 deletions examples/grpc_metadata_client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
graphpipe "github.com/oracle/graphpipe-go"
)

var addr = "grpc+http://127.0.0.1:9000"

/// Example of a client that requests metadata from a graphpipe server
func main() {
meta, err := graphpipe.Metadata(addr)

if err != nil {
fmt.Println(err)
} else {
fmt.Println(meta)
}
}
21 changes: 21 additions & 0 deletions examples/http_infer_client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
graphpipe "github.com/oracle/graphpipe-go"
)

var addr = "http://127.0.0.1:9000"

/// Example of a client that talks to graphpipe-echo
func main() {
v := []int64{2, 2}
output, err := graphpipe.Remote(addr, v)

if err != nil {
fmt.Printf("Failed to call remote: %v", err)
} else {
echoData := output.([]int64)
fmt.Println(echoData)
}
}
18 changes: 18 additions & 0 deletions examples/http_metadata_client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
graphpipe "github.com/oracle/graphpipe-go"
)

var addr = "http://127.0.0.1:9000"

/// Example of a client that requests metadata from a graphpipe server
func main() {
response, err := graphpipe.Metadata(addr)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}
}
106 changes: 106 additions & 0 deletions graphpipefb/GraphpipeService_grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//Generated by gRPC Go plugin
//If you make any local changes, they will be lost
//source: graphpipe

package graphpipe

import "github.com/google/flatbuffers/go"

import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)

// Client API for GraphpipeService service
type GraphpipeServiceClient interface{
Infer(ctx context.Context, in *flatbuffers.Builder,
opts... grpc.CallOption) (* InferResponse, error)
Metadata(ctx context.Context, in *flatbuffers.Builder,
opts... grpc.CallOption) (* MetadataResponse, error)
}

type graphpipeServiceClient struct {
cc *grpc.ClientConn
}

func NewGraphpipeServiceClient(cc *grpc.ClientConn) GraphpipeServiceClient {
return &graphpipeServiceClient{cc}
}

func (c *graphpipeServiceClient) Infer(ctx context.Context, in *flatbuffers.Builder,
opts... grpc.CallOption) (* InferResponse, error) {
out := new(InferResponse)
err := grpc.Invoke(ctx, "/graphpipe.GraphpipeService/Infer", in, out, c.cc, opts...)
if err != nil { return nil, err }
return out, nil
}

func (c *graphpipeServiceClient) Metadata(ctx context.Context, in *flatbuffers.Builder,
opts... grpc.CallOption) (* MetadataResponse, error) {
out := new(MetadataResponse)
err := grpc.Invoke(ctx, "/graphpipe.GraphpipeService/Metadata", in, out, c.cc, opts...)
if err != nil { return nil, err }
return out, nil
}

// Server API for GraphpipeService service
type GraphpipeServiceServer interface {
Infer(context.Context, *InferRequest) (*flatbuffers.Builder, error)
Metadata(context.Context, *MetadataRequest) (*flatbuffers.Builder, error)
}

func RegisterGraphpipeServiceServer(s *grpc.Server, srv GraphpipeServiceServer) {
s.RegisterService(&_GraphpipeService_serviceDesc, srv)
}

func _GraphpipeService_Infer_Handler(srv interface{}, ctx context.Context,
dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(InferRequest)
if err := dec(in); err != nil { return nil, err }
if interceptor == nil { return srv.(GraphpipeServiceServer).Infer(ctx, in) }
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/graphpipe.GraphpipeService/Infer",
}

handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GraphpipeServiceServer).Infer(ctx, req.(* InferRequest))
}
return interceptor(ctx, in, info, handler)
}


func _GraphpipeService_Metadata_Handler(srv interface{}, ctx context.Context,
dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(MetadataRequest)
if err := dec(in); err != nil { return nil, err }
if interceptor == nil { return srv.(GraphpipeServiceServer).Metadata(ctx, in) }
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/graphpipe.GraphpipeService/Metadata",
}

handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GraphpipeServiceServer).Metadata(ctx, req.(* MetadataRequest))
}
return interceptor(ctx, in, info, handler)
}


var _GraphpipeService_serviceDesc = grpc.ServiceDesc{
ServiceName: "graphpipe.GraphpipeService",
HandlerType: (*GraphpipeServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Infer",
Handler: _GraphpipeService_Infer_Handler,
},
{
MethodName: "Metadata",
Handler: _GraphpipeService_Metadata_Handler,
},
},
Streams: []grpc.StreamDesc{
},
}

Loading