(t *testing.T)
| 70 | } |
| 71 | |
| 72 | func TestControlDependencies(t *testing.T) { |
| 73 | var ( |
| 74 | s = NewScope() |
| 75 | zero = Const(s.SubScope("zero"), int32(0)) |
| 76 | one = Const(s.SubScope("one"), int32(1)) |
| 77 | variable = VarHandleOp(s, tf.Int32, tf.ScalarShape()) |
| 78 | init = AssignVariableOp(s, variable, zero) |
| 79 | update = AssignAddVariableOp(s, variable, one) |
| 80 | readDeps = []*tf.Operation{update} |
| 81 | ) |
| 82 | // We intend for `read` to have a control dependency on `update`. |
| 83 | s = s.WithControlDependencies(readDeps...) |
| 84 | // Ensure that Scope.WithControlDependencies makes a copy of the underlying |
| 85 | // array, rather than just holding a slice reference to the same user-supplied |
| 86 | // underlying array. If the copy is correctly performed, overwriting |
| 87 | // readDeps[0] should have no effect on control dependencies for `read`. |
| 88 | readDeps[0] = init |
| 89 | read := ReadVariableOp(s, variable, tf.Int32) |
| 90 | |
| 91 | graph, err := s.Finalize() |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | sess, err := tf.NewSession(graph, nil) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | if _, err = sess.Run(nil, nil, []*tf.Operation{init}); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | // Without the control dependency, the read operation may not see the |
| 103 | // update. |
| 104 | for i := int32(0); i < 10; i++ { |
| 105 | out, err := sess.Run(nil, []tf.Output{read}, nil) |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | if got, want := out[0].Value().(int32), i+1; got != want { |
| 110 | t.Errorf("Got %d, want %d", got, want) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestDevice(t *testing.T) { |
| 116 | s := NewScope() |
nothing calls this directly
no test coverage detected