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

tensorflow/go: add operation Input methods + tests #19915

Merged
merged 1 commit into from
Jun 13, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions tensorflow/go/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (op *Operation) Output(i int) Output {
return Output{op, i}
}

// NumInputs returns the number of inputs of op.
func (op *Operation) NumInputs() int {
return int(C.TF_OperationNumInputs(op.c))
}

// Output represents one of the outputs of an operation in the graph. Has a
// DataType (and eventually a Shape). May be passed as an input argument to a
// function for adding operations to a graph, or to a Session's Run() method to
Expand Down Expand Up @@ -123,6 +128,64 @@ func (p Output) c() C.TF_Output {

func (p Output) canBeAnInput() {}

// Consumers returns the inputs that consume this output.
func (p Output) Consumers() []Consumer {
max := int(C.TF_OperationOutputNumConsumers(p.c()))
inputs := make([]C.TF_Input, max)
n := C.TF_OperationOutputConsumers(p.c(), (*C.TF_Input)(unsafe.Pointer(&inputs[0])), C.int(max))
inputs = inputs[:int(n)]

var consumers []Consumer
for _, consumer := range inputs {
consumers = append(consumers, Consumer{
Index: int(consumer.index),
Op: &Operation{
c: consumer.oper,
g: p.Op.g,
},
})
}

return consumers
}

// Consumer identifies a specific input of an operation that consumes the output
// of another operation.
type Consumer struct {
// Op is the Operation that is consuming the output of another operation.
Op *Operation

// Index is the index of the input within Op that the output of another
// operation is connected to.
Index int
}

func (p Consumer) c() C.TF_Input {
if p.Op == nil {
// Attempt to provide a more useful panic message than "nil
// pointer dereference".
panic("nil-Operation. Consumer objects should only be created by a call to Output.Consumers")
}
return C.TF_Input{oper: p.Op.c, index: C.int(p.Index)}
}

// DataType returns the type of the input.
func (p Consumer) DataType() DataType {
return DataType(C.TF_OperationInputType(p.c()))
}

// Producer returns the Output that is connected to this Consumer.
func (p Consumer) Producer() Output {
output := C.TF_OperationInput(p.c())
return Output{
Op: &Operation{
c: output.oper,
g: p.Op.g,
},
Index: int(output.index),
}
}

// Input is the interface for specifying inputs to an operation being added to
// a Graph.
//
Expand Down
58 changes: 58 additions & 0 deletions tensorflow/go/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,64 @@ func TestOutputDataTypeAndShape(t *testing.T) {
}
}

func TestOperationInputs(t *testing.T) {
g := NewGraph()
x, err := Placeholder(g, "x", Float)
if err != nil {
t.Fatal(err)
}
y, err := Placeholder(g, "y", Float)
if err != nil {
t.Fatal(err)
}
add, err := Add(g, "add", x, y)
if err != nil {
t.Fatal(err)
}
addOp := add.Op

if out := addOp.NumInputs(); out != 2 {
t.Fatalf("Got %d inputs, wanted 2", out)
}
}

func TestOperationConsumers(t *testing.T) {
g := NewGraph()
x, err := Placeholder(g, "x", Float)
if err != nil {
t.Fatal(err)
}
a, err := Neg(g, "a", x)
if err != nil {
t.Fatal(err)
}
b, err := Neg(g, "b", x)
if err != nil {
t.Fatal(err)
}

consumers := []*Operation{a.Op, b.Op}

xConsumers := x.Consumers()
if out := len(xConsumers); out != 2 {
t.Fatalf("Got %d consumers, wanted 2", out)
}

for i, consumer := range xConsumers {
got := consumer.Op.Name()
want := consumers[i].Name()
if got != want {
t.Fatalf("%d. Got op name %q, wanted %q", i, got, want)
}

got = consumer.Producer().Op.Name()
want = x.Op.Name()
if got != want {
t.Fatalf("%d. Got op name %q, wanted %q", i, got, want)
}
}
}

func forceGC() {
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
Expand Down