aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/autograph/converters/side_effect_guards_test.py
blob: a7ad8efed4c88e15ce9dc14cb02e5e035602013d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for side_effect_guards module."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.contrib.autograph.converters import side_effect_guards
from tensorflow.contrib.autograph.core import converter_testing
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import errors_impl
from tensorflow.python.framework import ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import state_ops
from tensorflow.python.ops import variables
from tensorflow.python.platform import test


class SideEffectGuardsTest(converter_testing.TestCase):

  def test_side_effect_on_return_only_variable(self):

    tf = None

    def test_fn(a):
      tf.assign(a, a + 1)
      return a

    node = self.parse_and_analyze(test_fn, {})
    node = side_effect_guards.transform(node, self.ctx)

    with self.compiled(node, state_ops.assign) as result:
      self.assertEqual(len(node.body[0].body), 1)
      with self.test_session() as sess:
        v = variables.Variable(2)
        sess.run(v.initializer)
        # NOTE: We don't expect the assignment to execute in this case, because
        # variables cannot be reliably guarded.
        self.assertEqual(2, sess.run(result.test_fn(v)))

  def test_side_effect_on_used_variable(self):

    tf = None

    def test_fn(a):
      tf.assign(a, a + 1)
      return a + 1

    node = self.parse_and_analyze(test_fn, {})
    node = side_effect_guards.transform(node, self.ctx)

    with self.compiled(node, state_ops.assign) as result:
      self.assertEqual(len(node.body[0].body), 1)
      with self.test_session() as sess:
        v = variables.Variable(2)
        sess.run(v.initializer)
        # NOTE: Unlike test_side_effect_on_return_only_variable, the variable
        # was used in the local scope and so we could catch the assign's side
        # effect.
        self.assertEqual(4, sess.run(result.test_fn(v)))

  def test_side_effect_on_tensor(self):

    tf = None

    def test_fn(a):
      tf.Assert(a > 0, ['expected in throw'])
      return a

    node = self.parse_and_analyze(test_fn, {})
    node = side_effect_guards.transform(node, self.ctx)

    with self.compiled(node, control_flow_ops.Assert) as result:
      self.assertEqual(len(node.body[0].body), 1)
      with self.test_session() as sess:
        # NOTE: In this case we can also capture the side effect because the
        # argument is a tensor ans we can wrap it inside an identity.
        with self.assertRaisesRegexp(errors_impl.InvalidArgumentError,
                                     'expected in throw'):
          sess.run(result.test_fn(constant_op.constant(-1)))

  def test_multiline_block(self):

    tf = None

    def test_fn(a):
      tf.assign(a, a + 1)
      b = a + 1
      tf.assign(a, b + 1)
      c = b + 1
      d = c + 1
      return d

    node = self.parse_and_analyze(test_fn, {})
    node = side_effect_guards.transform(node, self.ctx)

    with self.compiled(node, state_ops.assign) as result:
      self.assertEqual(len(node.body[0].body), 1)
      with self.test_session() as sess:
        v = variables.Variable(2)
        sess.run(v.initializer)
        self.assertEqual(6, sess.run(result.test_fn(v)))

  def test_multiline_nested_block(self):

    tf = None

    def test_fn(a):
      with tf.name_scope('foo'):
        tf.assign(a, a + 1)
        b = a + 1
        c = b + 1
        d = c + 1
      return d

    node = self.parse_and_analyze(test_fn, {})
    node = side_effect_guards.transform(node, self.ctx)

    with self.compiled(node, state_ops.assign, ops.name_scope) as result:
      self.assertEqual(len(node.body[0].body[0].body), 1)
      with self.test_session() as sess:
        v = variables.Variable(2)
        sess.run(v.initializer)
        self.assertEqual(6, sess.run(result.test_fn(v)))

  def test_multiline_block_unsafe(self):

    tf = None

    def test_fn(a):
      tf.assign(a, a + 1)
      b = a + 1
      tf.assign(a, a + 1)
      c = b + 1
      d = c + 1
      return d

    node = self.parse_and_analyze(test_fn, {})
    node = side_effect_guards.transform(node, self.ctx)

    with self.compiled(node, state_ops.assign) as result:
      self.assertEqual(len(node.body[0].body), 1)
      with self.test_session() as sess:
        v = variables.Variable(2)
        sess.run(v.initializer)
        # NOTE: This intentionally highlights the flakiness. The test should be
        # tightened down once that is solved.
        self.assertTrue(sess.run(result.test_fn(v)) in (6, 7))


if __name__ == '__main__':
  test.main()