code: # Copyright 2015 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.
  # ==============================================================================
  """Functional tests for slice op."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import numpy as np
  from six.moves import xrange  # pylint: disable=redefined-builtin
  
  from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import errors_impl
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import gradients_impl
  from tensorflow.python.platform import test
  
  
  class SliceTest(test.TestCase):
  
    def testEmpty(self):
      inp = np.random.rand(4, 4).astype("f")
      for k in xrange(4):
        with self.test_session(use_gpu=True):
          a = constant_op.constant(inp, shape=[4, 4], dtype=dtypes.float32)
          slice_t = a[2, k:k]
          slice_val = slice_t.eval()
        self.assertAllEqual(slice_val, inp[2, k:k])
  
    def testInt32(self):
      inp = np.random.rand(4, 4).astype("i")
      for k in xrange(4):
        with self.test_session(use_gpu=True):
          a = constant_op.constant(inp, shape=[4, 4], dtype=dtypes.int32)
          slice_t = a[2, k:k]
          slice_val = slice_t.eval()
        self.assertAllEqual(slice_val, inp[2, k:k])
  
    def testInt64Slicing(self):
      with self.test_session(use_gpu=True):
        a = constant_op.constant([0, 1, 2], dtype=dtypes.int64)
  
        # Slice using int64 Tensor.
        i = constant_op.constant(1, dtype=dtypes.int64)
        slice_t = a[i]
        slice_val = slice_t.eval()
        self.assertAllEqual(1, slice_val)
        slice_t = a[i:i+1]
        slice_val = slice_t.eval()
        self.assertAllEqual([1], slice_val)
  
        # Slice using int64 integer.
        i = np.asarray(1).astype(np.int64)
        slice_t = a[i]
        slice_val = slice_t.eval()
        self.assertAllEqual(1, slice_val)
        slice_t = a[i:i+1]
        slice_val = slice_t.eval()
        self.assertAllEqual([1], slice_val)
  
    def testSelectAll(self):
      for _ in range(10):
        with self.test_session(use_gpu=True):
          inp = np.random.rand(4, 4, 4, 4).astype("f")
          a = constant_op.constant(inp, shape=[4, 4, 4, 4], dtype=dtypes.float32)
  
          slice_explicit_t = array_ops.slice(a, [0, 0, 0, 0], [-1, -1, -1, -1])
          slice_implicit_t = a[:, :, :, :]
  
          self.assertAllEqual(inp, slice_explicit_t.eval())
          self.assertAllEqual(inp, slice_implicit_t.eval())
          self.assertEqual(inp.shape, slice_explicit_t.get_shape())
          self.assertEqual(inp.shape, slice_implicit_t.get_shape())
  
    def testSingleDimension(self):
      for _ in range(10):
        with self.test_session(use_gpu=True):
          inp = np.random.rand(10).astype("f")
          a = constant_op.constant(inp, shape=[10], dtype=dtypes.float32)
  
          hi = np.random.randint(0, 9)
          scalar_t = a[hi]
          scalar_val = scalar_t.eval()
          self.assertAllEqual(scalar_val, inp[hi])
  
          if hi > 0:
            lo = np.random.randint(0, hi)
          else:
            lo = 0
          slice_t = a[lo:hi]
          slice_val = slice_t.eval()
          self.assertAllEqual(slice_val, inp[lo:hi])
  
    def testScalarInput(self):
      input_val = 0
      with self.test_session() as sess:
        # Test with constant input; shape inference fails.
        with self.assertRaisesWithPredicateMatch(ValueError, "out of range"):
          constant_op.constant(input_val)[:].get_shape()
  
        # Test evaluating with non-constant input; kernel execution fails.
        input_t = array_ops.placeholder(dtypes.int32)
        slice_t = input_t[:]
        with self.assertRaisesWithPredicateMatch(errors_impl.InvalidArgumentError,
                                                 "out of range"):
          sess.run([slice_t], feed_dict={input_t: input_val})
  
    def testInvalidIndex(self):
      input_val = [1, 2]
      with self.test_session() as sess:
        # Test with constant input; shape inference fails.
        with self.assertRaisesWithPredicateMatch(ValueError, "out of range"):
          constant_op.constant(input_val)[1:, 1:].get_shape()
  
        # Test evaluating with non-constant input; kernel execution fails.
        input_t = array_ops.placeholder(dtypes.int32)
        slice_t = input_t[1:, 1:]
        with self.assertRaisesWithPredicateMatch(errors_impl.InvalidArgumentError,
                                                 "out of range"):
          sess.run([slice_t], feed_dict={input_t: input_val})
  
    def _testSliceMatrixDim0(self, x, begin, size):
      with self.test_session(use_gpu=True):
        tf_ans = array_ops.slice(x, [begin, 0], [size, x.shape[1]]).eval()
      np_ans = x[begin:begin + size, :]
      self.assertAllEqual(tf_ans, np_ans)
  
    def testSliceMatrixDim0(self):
      x = np.random.rand(8, 4).astype("f")
      self._testSliceMatrixDim0(x, 1, 2)
      self._testSliceMatrixDim0(x, 3, 3)
      y = np.random.rand(8, 7).astype("f")  # 7 * sizeof(float) is not aligned
      self._testSliceMatrixDim0(y, 1, 2)
      self._testSliceMatrixDim0(y, 3, 3)
  
    def testSingleElementAll(self):
      for _ in range(10):
        with self.test_session(use_gpu=True):
          inp = np.random.rand(4, 4).astype("f")
          a = constant_op.constant(inp, shape=[4, 4], dtype=dtypes.float32)
  
          x, y = np.random.randint(0, 3, size=2).tolist()
          slice_t = a[x, 0:y]
          slice_val = slice_t.eval()
        self.assertAllEqual(slice_val, inp[x, 0:y])
  
    def testSimple(self):
      with self.test_session(use_gpu=True) as sess:
        inp = np.random.rand(4, 4).astype("f")
        a = constant_op.constant(
            [float(x) for x in inp.ravel(order="C")],
            shape=[4, 4],
            dtype=dtypes.float32)
        slice_t = array_ops.slice(a, [0, 0], [2, 2])
        slice2_t = a[:2, :2]
        slice_val, slice2_val = sess.run([slice_t, slice2_t])
      self.assertAllEqual(slice_val, inp[:2, :2])
      self.assertAllEqual(slice2_val, inp[:2, :2])
      self.assertEqual(slice_val.shape, slice_t.get_shape())
      self.assertEqual(slice2_val.shape, slice2_t.get_shape())
  
    def testComplex(self):
      with self.test_session(use_gpu=True):
        inp = np.random.rand(4, 10, 10, 4).astype("f")
        a = constant_op.constant(inp, dtype=dtypes.float32)
  
        x = np.random.randint(0, 9)
        z = np.random.randint(0, 9)
        if z > 0:
          y = np.random.randint(0, z)
        else:
          y = 0
        slice_t = a[:, x, y:z, :]
        self.assertAllEqual(slice_t.eval(), inp[:, x, y:z, :])
  
    def testRandom(self):
      # Random dims of rank 6
      input_shape = np.random.randint(0, 20, size=6)
      inp = np.random.rand(*input_shape).astype("f")
      with self.test_session(use_gpu=True) as sess:
        a = constant_op.constant(
            [float(x) for x in inp.ravel(order="C")],
            shape=input_shape,
            dtype=dtypes.float32)
        indices = [0 if x == 0 else np.random.randint(x) for x in input_shape]
        sizes = [
            np.random.randint(0, input_shape[i] - indices[i] + 1)
            for i in range(6)
        ]
        slice_t = array_ops.slice(a, indices, sizes)
        slice2_t = a[indices[0]:indices[0] + sizes[0], indices[1]:indices[
            1] + sizes[1], indices[2]:indices[2] + sizes[2], indices[3]:indices[3]
                     + sizes[3], indices[4]:indices[4] + sizes[4], indices[5]:
                     indices[5] + sizes[5]]
  
        slice_val, slice2_val = sess.run([slice_t, slice2_t])
  
      expected_val = inp[indices[0]:indices[0] + sizes[0], indices[1]:indices[
          1] + sizes[1], indices[2]:indices[2] + sizes[2], indices[3]:indices[
              3] + sizes[3], indices[4]:indices[4] + sizes[4], indices[5]:indices[
                  5] + sizes[5]]
      self.assertAllEqual(slice_val, expected_val)
      self.assertAllEqual(slice2_val, expected_val)
      self.assertEqual(expected_val.shape, slice_t.get_shape())
      self.assertEqual(expected_val.shape, slice2_t.get_shape())
  
    def testPartialShapeInference(self):
      z = array_ops.zeros((1, 2, 3))
      self.assertAllEqual(z.get_shape().as_list(), [1, 2, 3])
  
      m1 = array_ops.slice(z, [0, 0, 0], [-1, -1, -1])
      self.assertAllEqual(m1.get_shape().as_list(), [1, 2, 3])
  
      m2 = array_ops.slice(z, [0, 0, 0], [constant_op.constant(1) + 0, 2, -1])
      self.assertAllEqual(m2.get_shape().as_list(), [None, 2, None])
  
  
    def _testGradientSlice(self, input_shape, slice_begin, slice_size):
      with self.test_session(use_gpu=True):
        num_inputs = np.prod(input_shape)
        num_grads = np.prod(slice_size)
        inp = np.random.rand(num_inputs).astype("f").reshape(input_shape)
        a = constant_op.constant(
            [float(x) for x in inp.ravel(order="C")],
            shape=input_shape,
            dtype=dtypes.float32)
        slice_t = array_ops.slice(a, slice_begin, slice_size)
        grads = np.random.rand(num_grads).astype("f").reshape(slice_size)
        grad_tensor = constant_op.constant(grads)
        grad = gradients_impl.gradients(slice_t, [a], grad_tensor)[0]
        result = grad.eval()
  
      # Create a zero tensor of the input shape ane place
      # the grads into the right location to compare against TensorFlow.
      np_ans = np.zeros(input_shape)
      slices = []
      for i in xrange(len(input_shape)):
        slices.append(slice(slice_begin[i], slice_begin[i] + slice_size[i]))
      np_ans[slices] = grads
  
      self.assertAllClose(np_ans, result)
  
    def _testGradientVariableSize(self):
      with self.test_session(use_gpu=True):
        inp = constant_op.constant([1.0, 2.0, 3.0], name="in")
        out = array_ops.slice(inp, [1], [-1])
        grad_actual = gradients_impl.gradients(out, inp)[0].eval()
      self.assertAllClose([0., 1., 1.], grad_actual)
  
    def testGradientsAll(self):
      # Slice the middle square out of a 4x4 input
      self._testGradientSlice([4, 4], [1, 1], [2, 2])
  
      # Slice the upper left square out of a 4x4 input
      self._testGradientSlice([4, 4], [0, 0], [2, 2])
  
      # Slice a non-square input starting from (2,1)
      self._testGradientSlice([4, 4], [2, 1], [1, 2])
  
      # Slice a 3D tensor
      self._testGradientSlice([3, 3, 3], [0, 1, 0], [2, 1, 1])
  
      # Use -1 as a slice dimension.
      self._testGradientVariableSize()
  
    def testNotIterable(self):
      # NOTE (mrry): If we register __getitem__ as an overloaded id:2995 gh:2996
      # operator, Python will valiantly attempt to iterate over the
      # Tensor from 0 to infinity.  This test ensures that this
      # unintended behavior is prevented.
      c = constant_op.constant(5.0)
      with self.assertRaisesWithPredicateMatch(
          TypeError, lambda e: "`Tensor` objects are not iterable" in str(e)):
        for _ in c:
          pass
  
    def testComputedShape(self):
      # NOTE (mrry): We cannot currently handle partially-known values, id:3496 gh:3497
      # because `tf.slice()` uses -1 to specify a wildcard size, and
      # this can't be handled using the
      # `tensor_util.constant_value_as_shape()` trick.
      a = constant_op.constant([[1, 2, 3], [4, 5, 6]])
      begin = constant_op.constant(0)
      size = constant_op.constant(1)
      b = array_ops.slice(a, [begin, 0], [size, 2])
      self.assertEqual([1, 2], b.get_shape())
  
      begin = array_ops.placeholder(dtypes.int32, shape=())
      c = array_ops.slice(a, [begin, 0], [-1, 2])
      self.assertEqual([None, 2], c.get_shape().as_list())
  
    def testSliceOfSlice(self):
      with self.test_session(use_gpu=True):
        a = constant_op.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
        b = a[1:, :]
        c = b[:-1, :]
        d = c[1, :]
        res = 2 * d - c[1, :] + a[2, :] - 2 * b[-2, :]
        self.assertAllEqual([0, 0, 0], res.eval())
  
  
  if __name__ == "__main__":
    test.main()
   # 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.
  # =============================================================================
  
  """Library for constructing a training loop, suitable for TPUs."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  from tensorflow.contrib.tpu.python.tpu import tpu_function
  
  from tensorflow.python.framework import ops
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import control_flow_ops
  
  
  def while_loop(condition, body, inputs=None, infeed_queue=None, name=None):
    """Builds a training loop for TPUs.
  
    The set of loop-carried tensors corresponds to `inputs`.  Both
    `condition` and `body` take the current value of the loop-carried
    tensors. 'body' additionally takes a tuple of infeed from
    infeed_queue if infeed_queue is not None. `condition` must return a
    single boolean value that determines whether iteration
    continues. `body` must return an updated list of values for the
    loop-carried tensors.
  
    Args:
      condition: a Python function that builds the loop condition.
      body: a Python function that builds the loop body.
      inputs: a list of initial values passed into the training loop, or
        None (equivalent to an empty list).
      infeed_queue: if not None, the infeed queue from which to append a tuple
        of arguments as inputs to condition.
      name: an optional name for the loop.
  
    Returns:
      The final values of the loop-carried tensors.
  
    Raises:
      TypeError: if body or condition has the wrong signature.
    """
  
    # Converts inputs to Tensors.
    inputs = [] if inputs is None else [ops.convert_to_tensor(x) for
                                        x in inputs]
    input_types = [x.dtype for x in inputs]
    input_arity = len(inputs)
  
    body_arg_error = tpu_function.check_function_argument_count(
        body, input_arity, infeed_queue)
    if body_arg_error is not None:
      if infeed_queue is None:
        raise TypeError(
            "Supplied loop body function cannot be called with the specified "
            "inputs. You specified %d inputs: %s, but the loop body needs %s" % (
                input_arity, str([i.name for i in inputs]), body_arg_error))
      else:
        raise TypeError(
            "Supplied loop body function cannot be called with the specified "
            "inputs. You specified %d inputs: %s and %d additional inputs from "
            "infeed, but the computation needs %s" % (input_arity, str(
                [i.name for i in inputs]), infeed_queue.number_of_tuple_elements,
                                                      body_arg_error))
    condition_arg_error = tpu_function.check_function_argument_count(
        condition, input_arity, None)
    if condition_arg_error is not None:
      if infeed_queue is None:
        raise TypeError(
            "Supplied loop condition function cannot be called with the "
            "specified inputs. You specified %d inputs: %s, but the loop "
            "condition needs %s" % (input_arity, str([i.name for i in inputs]),
                                    condition_arg_error))
      else:
        raise TypeError(
            "Supplied loop condition function cannot be called with the "
            "specified inputs. You specified %d inputs: %s, but the loop "
            "condition needs %s. Note that infeed is not passed to the loop "
            "condition." % (input_arity, str([i.name for i in inputs]),
                            condition_arg_error))
  
    def condition_wrapper(*inputs):
      # Discards the dummy output added for arity-0 loops.
      if input_arity == 0:
        inputs = []
      return condition(*inputs)
  
    def body_wrapper(*inputs):
      """Wrapper around `body` that handles infeed queues and control deps."""
      inputs = list(inputs)
  
      # Discards the dummy output added for arity-0 loops.
      if input_arity == 0:
        inputs = []
  
      # Runs `body` with the dequeue_ops appended.
      if infeed_queue:
        number_of_shards = tpu_function.get_tpu_context().number_of_shards
        if number_of_shards is None:
          raise ValueError("Can't build training loop with infeed when there is "
                           "no tpu_shard_context. Are you building a loop or "
                           "graph directly rather than from inside tpu.rewrite, "
                           "tpu.batch_parallel, tpu.shard, or tpu.replicate?")
        infeed_queue.set_number_of_shards(number_of_shards)
        dequeue_ops = [d for d in infeed_queue.generate_dequeue_op()]
      else:
        dequeue_ops = []
      outputs = body(*(inputs + dequeue_ops))
  
      # If the computation only returned one value, make it a tuple.
      if not isinstance(outputs, (list, tuple)):
        outputs = (outputs,)
  
      outputs = [
          o if isinstance(o, ops.Operation) else ops.convert_to_tensor(o)
          for o in outputs
      ]
  
      # Separates the returned Operations and Tensors.
      output_operations = [o for o in outputs if isinstance(o, ops.Operation)]
      output_tensors = [o for o in outputs
                        if not isinstance(o, ops.Operation)]
  
      if outputs != output_tensors + output_operations:
        raise ValueError(
            "TPU training loop body must return zero or more Tensor values "
            "followed by zero or more Operations.")
  
      output_types = [op.dtype for op in output_tensors]
      if input_types != output_types:
        raise TypeError(
            "Mismatch between input types and output types for training loop "
            "body: {} vs {}".format(input_types, output_types))
  
      # Add the dequeue operations to output_operations to ensure they are run
      # by the loop, even if the programmer's loop body does not use them.
      output_operations += dequeue_ops
  
      # Add a dummy output, if needed.
      if not output_tensors:
        output_tensors = array_ops.constant(0)
  
      if output_operations:
        # TODO (phawkins): in principle this is too restrictive since it serializes id:1047 gh:1048
        # the training loop steps. In practice it does not matter since this loop
        # will be compiled by XLA.
        return control_flow_ops.tuple(output_tensors,
                                      control_inputs=output_operations)
      else:
        return output_tensors
  
    # If the body has arity 0, add a dummy loop-carried value to which we can add
    # control dependencies from any side-effecting operations.
    if input_arity == 0:
      inputs = [array_ops.constant(0)]
    return control_flow_ops.while_loop(condition_wrapper, body_wrapper, inputs,
                                       name=name)
  
  
  def repeat(n, body, inputs=None, infeed_queue=None, name=None):
    """Builds a training loop that executes a fixed number of interations.
  
    The set of loop-carried tensors correspond to `inputs`.
    `body` must be a function that takes and returns the values of the
    loop-carried tensors.
  
    Args:
      n: the number of loop iterations
      body: a Python function that builds the loop body.
      inputs: a list of initial values passed into the training loop or
        None (equivalent to an empty list).
      infeed_queue: if not None, the infeed queue from which to append a tuple
        of arguments as inputs to condition.
      name: an optional name for the loop.
    Returns:
      The final values of the loop-carried tensors.
    Raises:
      ValueError: if there is a type error.
    """
    def _convert_to_list(xs):
      if not isinstance(xs, (list, tuple)):
        return [xs]
      else:
        return list(xs)
  
    def cond(i, *args):
      del args
      return i < n
  
    def body_wrapper(i, *args):
      return [i + 1] + _convert_to_list(body(*args))
  
    inputs = [0] if inputs is None else [0] + _convert_to_list(inputs)
    outputs = while_loop(
        cond, body_wrapper, inputs=inputs, infeed_queue=infeed_queue, name=name)
    outputs = _convert_to_list(outputs)
    if len(outputs) == 1:
      # Returns the Op rather than an empty list.
      return outputs[0].op
    else:
      return outputs[1:]
  
apis: numpy.zeros tensorflow.python.ops.array_ops.placeholder tensorflow.python.ops.array_ops.zeros tensorflow.python.ops.array_ops.slice tensorflow.python.ops.gradients_impl.gradients numpy.asarray tensorflow.python.platform.test.main numpy.random.rand numpy.prod numpy.random.randint tensorflow.python.framework.constant_op.constant
apis: tensorflow.contrib.tpu.python.tpu.tpu_function.check_function_argument_count tensorflow.python.ops.control_flow_ops.while_loop tensorflow.python.ops.control_flow_ops.tuple tensorflow.contrib.tpu.python.tpu.tpu_function.get_tpu_context tensorflow.python.ops.array_ops.constant tensorflow.python.framework.ops.convert_to_tensor