Best Python code snippet using molotov_python
gather_nd_op_test.py
Source:gather_nd_op_test.py  
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15"""Tests for tensorflow.ops.tf.gather_nd."""16from __future__ import absolute_import17from __future__ import division18from __future__ import print_function19import time20import numpy as np21from tensorflow.python.client import session22from tensorflow.python.eager import context23from tensorflow.python.framework import constant_op24from tensorflow.python.framework import dtypes25from tensorflow.python.framework import ops26from tensorflow.python.framework import tensor_shape27from tensorflow.python.framework import test_util28from tensorflow.python.ops import array_ops29from tensorflow.python.ops import gradients_impl30from tensorflow.python.ops import resource_variable_ops31from tensorflow.python.ops import variables32from tensorflow.python.platform import test33class GatherNdTest(test.TestCase):34  def _testSimpleDtype(self, dtype):35    with self.cached_session(use_gpu=True):36      params = constant_op.constant(np.array([8, 1, 2, 3, 7, 5], dtype=dtype))37      indices = constant_op.constant([[4], [4], [0]])38      gather_nd_t = array_ops.gather_nd(params, indices)39      gather_nd_val = self.evaluate(gather_nd_t)40    self.assertAllEqual(np.array([7, 7, 8], dtype=dtype), gather_nd_val)41    self.assertEqual([3], gather_nd_t.get_shape())42  def testSimpleDtype(self):43    self._testSimpleDtype(np.float32)44    self._testSimpleDtype(np.float64)45    self._testSimpleDtype(np.int32)46    self._testSimpleDtype(np.int64)47    self._testSimpleDtype(np.complex64)48    self._testSimpleDtype(np.complex128)49    self._testSimpleDtype("|S")  # byte strings in python2 + 350  @test_util.run_deprecated_v151  @test_util.disable_xla("b/123337890")  # Error messages differ52  def testEmptyIndicesAndParamsOKButJustEmptyParamsFails(self):53    with self.session(use_gpu=True):54      params = np.ones((3, 3), dtype=np.float32)55      indices_empty = np.empty((0, 2), dtype=np.int32)56      gather_nd_ok_t = array_ops.gather_nd(params, indices_empty)57      gather_nd_ok_val = self.evaluate(gather_nd_ok_t)58      self.assertEqual([0], gather_nd_ok_t.get_shape())59      self.assertAllClose(np.empty((0,), dtype=np.float32), gather_nd_ok_val)60      indices_empty = np.empty((0, 1), dtype=np.int32)61      gather_nd_ok_t = array_ops.gather_nd(params, indices_empty)62      gather_nd_ok_val = self.evaluate(gather_nd_ok_t)63      self.assertEqual([0, 3], gather_nd_ok_t.get_shape())64      self.assertAllClose(np.empty((0, 3), dtype=np.float32), gather_nd_ok_val)65      params_empty = np.empty((0, 3), dtype=np.float32)66      indices_empty = np.empty((0, 2), dtype=np.int32)67      gather_nd_ok_t = array_ops.gather_nd(params_empty, indices_empty)68      gather_nd_ok_val = self.evaluate(gather_nd_ok_t)69      self.assertEqual([0], gather_nd_ok_t.get_shape())70      self.assertAllClose(np.empty((0,), dtype=np.float32), gather_nd_ok_val)71      params_empty = np.empty((0, 3), dtype=np.float32)72      indices_nonempty = np.zeros((1, 2), dtype=np.int32)73      gather_nd_break_t = array_ops.gather_nd(params_empty, indices_nonempty)74      with self.assertRaisesOpError(75          r"Requested more than 0 entries, but params is empty."):76        self.evaluate(gather_nd_break_t)77      self.assertAllClose(np.empty((0,), dtype=np.float32), gather_nd_ok_val)78  def testIndexScalar(self):79    with self.session(use_gpu=True):80      params = np.array(81          [[-8, -1, -2, -3, -7, -5], [8, 1, 2, 3, 7, 5]], dtype=np.float32).T82      indices = constant_op.constant([4, 1])83      gather_nd_t = array_ops.gather_nd(params, indices)84      gather_nd_val = self.evaluate(gather_nd_t)85      self.assertEqual([], gather_nd_t.get_shape())86      self.assertAllEqual(np.array(7), gather_nd_val)87  def testParamsRankLargerThanIndexIndexScalarSlices(self):88    with self.session(use_gpu=True):89      params = np.array(90          [[-8, -1, -2, -3, -7, -5], [8, 1, 2, 3, 7, 5]], dtype=np.float32).T91      indices = constant_op.constant([4])92      gather_nd_t = array_ops.gather_nd(params, indices)93      gather_nd_val = self.evaluate(gather_nd_t)94      self.assertEqual([2], gather_nd_t.get_shape())95      self.assertAllEqual(np.array([-7, 7]), gather_nd_val)96  def testParamsRankLargerThanIndexSlices(self):97    with self.session(use_gpu=True):98      params = np.array(99          [[-8, -1, -2, -3, -7, -5], [8, 1, 2, 3, 7, 5]], dtype=np.float32).T100      indices = constant_op.constant([[4], [4], [0]])101      gather_nd_t = array_ops.gather_nd(params, indices)102      gather_nd_val = self.evaluate(gather_nd_t)103    self.assertEqual([3, 2], gather_nd_t.get_shape())104    self.assertAllEqual(np.array([[-7, 7], [-7, 7], [-8, 8]]), gather_nd_val)105  def testHigherRankParamsLargerThanIndexSlices(self):106    with self.session(use_gpu=True):107      params = np.array(108          [[[-8, -1, -2, -3, -7, -5], [8, 1, 2, 3, 7, 5]],109           [[-80, -10, -20, -30, -70, -50], [80, 10, 20, 30, 70, 50]]],110          dtype=np.float32).T111      params_t = constant_op.constant(params)112      indices = constant_op.constant([[4], [4], [0]])113      gather_nd_t = array_ops.gather_nd(params_t, indices)114      gather_nd_val = self.evaluate(gather_nd_t)115    self.assertEqual([3, 2, 2], gather_nd_t.get_shape())116    self.assertAllEqual(params[[4, 4, 0]], gather_nd_val)117  def testEmptyIndicesLastRankMeansCopyEntireTensor(self):118    with self.session(use_gpu=True):119      params = np.array(120          [[[-8, -1, -2, -3, -7, -5], [8, 1, 2, 3, 7, 5]],121           [[-80, -10, -20, -30, -70, -50], [80, 10, 20, 30, 70, 50]]],122          dtype=np.float32).T123      params_t = constant_op.constant(params)124      indices = constant_op.constant(125          [[], []], dtype=dtypes.int32)  # Size (2, 0)126      gather_nd_t = array_ops.gather_nd(params_t, indices)127      gather_nd_val = self.evaluate(gather_nd_t)128    self.assertEqual([2, 6, 2, 2], gather_nd_t.get_shape())129    self.assertAllEqual(130        np.vstack((params[np.newaxis, :], params[np.newaxis, :])),131        gather_nd_val)132  def testHigherRankParamsAndIndicesLargerThanIndexSlices(self):133    with self.session(use_gpu=True):134      params = np.array(135          [[[-8, -1, -2, -3, -7, -5], [8, 1, 2, 3, 7, 5]],136           [[-80, -10, -20, -30, -70, -50], [80, 10, 20, 30, 70, 50]]],137          dtype=np.float32).T138      params_t = constant_op.constant(params)139      indices = constant_op.constant([[[3], [2], [1]], [[4], [4], [0]]])140      gather_nd_t = array_ops.gather_nd(params_t, indices)141      gather_nd_val = self.evaluate(gather_nd_t)142    self.assertEqual([2, 3, 2, 2], gather_nd_t.get_shape())143    self.assertAllEqual(params[[3, 2, 1, 4, 4, 0]].reshape(2, 3, 2, 2),144                        gather_nd_val)145  def testHigherRankParams(self):146    with self.session(use_gpu=True):147      shape = (10, 20, 5, 1, 17)148      params = np.random.rand(*shape)149      indices = np.vstack([np.random.randint(0, s, size=2000) for s in shape]).T150      gather_nd_t = array_ops.gather_nd(params, indices)151      gather_nd_val = self.evaluate(gather_nd_t)152    expected = params[tuple(indices.T)]153    self.assertAllEqual(expected, gather_nd_val)154    self.assertEqual([2000], gather_nd_t.get_shape())155  def testHigherRankParamsAndIndices(self):156    with self.session(use_gpu=True):157      shape = (10, 20, 5, 1, 17)158      params = np.random.rand(*shape)159      indices = np.vstack([np.random.randint(0, s, size=2000) for s in shape]).T160      indices_reshaped = indices.reshape([10, 10, 20, 5])161      gather_nd_t = array_ops.gather_nd(params, indices_reshaped)162      gather_nd_val = self.evaluate(gather_nd_t)163    expected = params[tuple(indices.T)]164    self.assertAllEqual(expected.reshape([10, 10, 20]), gather_nd_val)165    self.assertEqual([10, 10, 20], gather_nd_t.get_shape())166  def assertIndexedSlices(self, t):167    self.assertIsInstance(t, ops.IndexedSlices)168  @test_util.run_deprecated_v1169  def testUnknownIndices(self):170    params = constant_op.constant([[0, 1, 2]])171    indices = array_ops.placeholder(dtypes.int32)172    gather_nd_t = array_ops.gather_nd(params, indices)173    shape = gather_nd_t.get_shape()174    self.assertEqual(None, shape.ndims)175    self.assertEqual(None, tensor_shape.dimension_value(shape[0]))176  @test_util.run_deprecated_v1177  @test_util.disable_xla("XLA does not have assertions in kernels.")178  def testBadIndicesCPU(self):179    with self.session(use_gpu=False):180      params = [0, 1, 2]181      indices = [[[0], [7]]]  # Make this one higher rank182      gather_nd = array_ops.gather_nd(params, indices)183      with self.assertRaisesOpError(184          r"indices\[0,1\] = \[7\] does not index into param shape \[3\]"):185        self.evaluate(gather_nd)186  def _disabledTestBadIndicesGPU(self):187    # TODO disabled due to different behavior on GPU and CPU188    # On GPU the bad indices do not raise error but fetch 0 values189    if not test.is_gpu_available():190      return191    with self.session(use_gpu=True):192      params = [0, 1, 2]193      indices = [[[0], [7]]]  # Make this one higher rank194      gather_nd = array_ops.gather_nd(params, indices)195      with self.assertRaisesOpError(196          r"indices\[0,1\] = \[7\] does not index into param shape \[3\]"):197        self.evaluate(gather_nd)198  @test_util.run_deprecated_v1199  @test_util.disable_xla("XLA does not have assertions in kernels.")200  def testBadIndicesWithSlicesCPU(self):201    with self.session(use_gpu=False):202      params = [[0, 1, 2]]203      indices = [[[0], [0], [1]]]  # Make this one higher rank204      gather_nd = array_ops.gather_nd(params, indices)205      with self.assertRaisesOpError(206          r"indices\[0,2\] = \[1\] does not index into param shape \[1,3\]"):207        self.evaluate(gather_nd)208  def _disabledTestBadIndicesWithSlicesGPU(self):209    # TODO disabled due to different behavior on GPU and CPU210    # On GPU the bad indices do not raise error but fetch 0 values211    if not test.is_gpu_available():212      return213    with self.session(use_gpu=True):214      params = [[0, 1, 2]]215      indices = [[[0], [0], [1]]]  # Make this one higher rank216      gather_nd = array_ops.gather_nd(params, indices)217      with self.assertRaisesOpError(218          r"indices\[0,2\] = \[1\] does not index into param shape \[1,3\]"):219        self.evaluate(gather_nd)220  @test_util.run_deprecated_v1221  def testGradientsRank2Elements(self):222    indices = constant_op.constant([[0, 0], [1, 1]], dtype=dtypes.int32)223    inputs = constant_op.constant([[1, 2], [3, 4]], dtype=dtypes.float64)224    outputs = array_ops.gather_nd(inputs, indices)225    grad_vals = constant_op.constant([1, 2], dtype=dtypes.float64)226    grads = gradients_impl.gradients([outputs], [inputs], [grad_vals])[0]227    expected_grads = np.array([[1, 0], [0, 2]], dtype=np.float64)228    with self.session(use_gpu=True):229      assert np.array_equal(expected_grads, self.evaluate(grads))230  @test_util.run_deprecated_v1231  def testGradientsRank2Slices(self):232    indices = constant_op.constant([[1], [0]], dtype=dtypes.int32)233    inputs = constant_op.constant([[1, 2], [3, 4]], dtype=dtypes.float64)234    outputs = array_ops.gather_nd(inputs, indices)235    grad_vals = constant_op.constant([[1, 2], [3, 4]], dtype=dtypes.float64)236    grads = gradients_impl.gradients([outputs], [inputs], [grad_vals])[0]237    expected_grads = np.array([[3, 4], [1, 2]], dtype=np.float64)238    with self.session(use_gpu=True):239      self.assertIndexedSlices(grads)240      self.assertAllEqual(expected_grads, ops.convert_to_tensor(grads).eval())241  @test_util.run_deprecated_v1242  def testGradientsRank3Elements(self):243    indices = constant_op.constant(244        [[[0, 1], [1, 0]], [[0, 0], [1, 1]]], dtype=dtypes.int32)245    inputs = constant_op.constant(246        [[[1, 3], [5, 7]], [[2, 4], [6, 8]]], dtype=dtypes.float64)247    outputs = array_ops.gather_nd(inputs, indices)248    grad_vals = constant_op.constant(249        [[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=dtypes.float64)250    grads = gradients_impl.gradients([outputs], [inputs], [grad_vals])[0]251    expected_grads = np.array(252        [[[5, 6], [1, 2]], [[3, 4], [7, 8]]], dtype=np.float64)253    with self.session(use_gpu=True):254      self.assertAllEqual(expected_grads, self.evaluate(grads))255  @test_util.run_deprecated_v1256  def testGradientsRank7Elements(self):257    # Shape [1,1,2,1,1,2,2]258    indices = constant_op.constant(259        [[[260            [[[[0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0]]]],261            [[[[0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1]]]]262        ]]],263        dtype=dtypes.int32)264    inputs = constant_op.constant(265        [[[266            [[[[1, 3], [5, 7]]]],267            [[[[2, 4], [6, 8]]]]268        ]]], dtype=dtypes.float64)269    outputs = array_ops.gather_nd(inputs, indices)270    grad_vals = constant_op.constant(271        [[[272            [[[[1, 2], [3, 4]]]],273            [[[[5, 6], [7, 8]]]]274        ]]], dtype=dtypes.float64)275    grads = gradients_impl.gradients([outputs], [inputs], [grad_vals])[0]276    expected_grads = np.array(277        [[[278            [[[[5, 6], [1, 2]]]],279            [[[[3, 4], [7, 8]]]]280        ]]], dtype=np.float64)281    with self.session(use_gpu=True):282      self.assertAllEqual(expected_grads, self.evaluate(grads))283  @test_util.run_deprecated_v1284  def testGradientsInt64Indices(self):285    indices = constant_op.constant(286        [[[0, 1], [1, 0]], [[0, 0], [1, 1]]], dtype=dtypes.int64)287    inputs = constant_op.constant(288        [[[1, 3], [5, 7]], [[2, 4], [6, 8]]], dtype=dtypes.float64)289    outputs = array_ops.gather_nd(inputs, indices)290    grad_vals = constant_op.constant(291        [[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=dtypes.float64)292    grads = gradients_impl.gradients([outputs], [inputs], [grad_vals])[0]293    expected_grads = np.array(294        [[[5, 6], [1, 2]], [[3, 4], [7, 8]]], dtype=np.float64)295    with self.session(use_gpu=True):296      self.assertAllEqual(expected_grads, self.evaluate(grads))297  @test_util.run_deprecated_v1298  def testGradientsRank2SlicesWithEmptySpace(self):299    indices = constant_op.constant([[2], [0], [5]], dtype=dtypes.int32)300    inputs = constant_op.constant(301        [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9],302         [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9],303         [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]],304        dtype=dtypes.float64)305    outputs = array_ops.gather_nd(inputs, indices)306    grad_vals = constant_op.constant(307        [[1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2, 2],308         [3, 3, 3, 3, 3, 3, 3, 3, 3]],309        dtype=dtypes.float64)310    grads = gradients_impl.gradients([outputs], [inputs], [grad_vals])[0]311    expected_grads = np.array(312        [[2, 2, 2, 2, 2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0],313         [1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0],314         [0, 0, 0, 0, 0, 0, 0, 0, 0], [3, 3, 3, 3, 3, 3, 3, 3, 3]],315        dtype=np.float64)316    with self.session(use_gpu=True):317      self.assertIndexedSlices(grads)318      self.assertAllEqual(expected_grads, ops.convert_to_tensor(grads).eval())319  @test_util.run_v1_only("RefVariable is not supported in v2")320  def testGatherNdRefVariable(self):321    with self.cached_session():322      v = variables.RefVariable(constant_op.constant([[1, 2], [3, 4], [5, 6]]))323      self.evaluate(variables.global_variables_initializer())324      gather = array_ops.gather_nd(v, [[0, 1], [2, 0]])325      if not context.executing_eagerly():  # .op doesn't make sense in Eager326        self.assertEqual("GatherNd", gather.op.name)327      self.assertAllEqual([2, 5], gather)328  @test_util.run_in_graph_and_eager_modes329  def testGatherNdResourceVariable(self):330    with self.cached_session():331      v = resource_variable_ops.ResourceVariable(332          constant_op.constant([[1, 2], [3, 4], [5, 6]]))333      self.evaluate(variables.global_variables_initializer())334      gather = array_ops.gather_nd(v, [[0, 1], [2, 0]])335      if not context.executing_eagerly():  # .op doesn't make sense in Eager336        self.assertEqual("ResourceGatherNd", gather.op.inputs[0].op.type)337      self.assertAllEqual([2, 5], gather)338class GatherNdOpBenchmark(test.Benchmark):339  def benchmark_gather_nd_op(self):340    shape = (100, 47, 18, 170, 13)341    np.random.seed(127)342    params = np.random.rand(*shape)343    indices = np.vstack([np.random.randint(0, s, size=10000) for s in shape]).T344    with session.Session():345      t_params = variables.Variable(params)346      t_indices = variables.Variable(indices)347      gather_op = array_ops.gather_nd(t_params, t_indices)348      variables.global_variables_initializer().run()349      for _ in range(10):350        self.evaluate(gather_op)351      t1 = time.time()352      for _ in range(1000):353        self.evaluate(gather_op)354      t2 = time.time()355      self.report_benchmark(iters=1000, wall_time=(t2 - t1) / 1000.0)356if __name__ == "__main__":...gather_test.py
Source:gather_test.py  
...40        for indices in 4, [4], [1, 2, 2, 4, 5]:41          params_np = self._buildParams(data, dtype)42          params = array_ops.placeholder(dtype=dtype)43          indices_tf = constant_op.constant(indices)44          gather_t = array_ops.gather(params, indices_tf)45          gather_val = session.run(gather_t, feed_dict={params: params_np})46          np_val = constant_op.constant(params_np[indices])47          self.assertAllEqual(np_val, gather_val)48  def testScalar2D(self):49    with self.session() as session, self.test_scope():50      data = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11],51                       [12, 13, 14]])52      for dtype in self.all_tf_types:53        for axis in 0, 1, -1:54          params_np = self._buildParams(data, dtype)55          params = array_ops.placeholder(dtype=dtype)56          indices = constant_op.constant(2)57          gather_t = array_ops.gather(params, indices, axis=axis)58          gather_val = session.run(gather_t, feed_dict={params: params_np})59          expected = constant_op.constant(60              np.take(params_np, 2, axis=axis), dtype)61          self.assertAllEqual(expected, gather_val)62  def testSimpleTwoD32(self):63    with self.session() as session, self.test_scope():64      data = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11],65                       [12, 13, 14]])66      for dtype in self.all_tf_types:67        for axis in 0, 1, -1:68          params_np = self._buildParams(data, dtype)69          params = array_ops.placeholder(dtype=dtype)70          # The indices must be in bounds for any axis.71          indices = constant_op.constant([0, 1, 0, 2])72          gather_t = array_ops.gather(params, indices, axis=axis)73          gather_val = session.run(gather_t, feed_dict={params: params_np})74          expected = constant_op.constant(75              np.take(params_np, [0, 1, 0, 2], axis=axis), dtype)76          self.assertAllEqual(expected, gather_val)77  def testSimpleTwoD32_Int64Indices(self):78    if np.int64 not in self.int_types:79      return80    with self.session() as session, self.test_scope():81      data = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11],82                       [12, 13, 14]])83      # The indices must be in bounds for any axis.84      indices_np = np.array([0, 1, 0, 2])85      for dtype in self.all_tf_types:86        for axis in 0, 1, -1:87          params_np = self._buildParams(data, dtype)88          params = array_ops.placeholder(dtype=dtype)89          indices = array_ops.placeholder(dtype=dtypes.int64)90          gather_t = array_ops.gather(params, indices, axis=axis)91          gather_val = session.run(92              gather_t, feed_dict={93                  params: params_np,94                  indices: indices_np95              })96          expected = constant_op.constant(97              np.take(params_np, [0, 1, 0, 2], axis=axis), dtype)98          self.assertAllEqual(expected, gather_val)99  def testHigherRank(self):100    """Check that scalar and empty indices shapes work as well."""101    shape = (2, 1, 3, 2)102    for indices_shape in (), (0,), (2, 0), (2, 3):103      for dtype in self.all_tf_types:104        for axis in 0, 1, 2, 3, -1, -2:105          params = self._buildParams(np.random.randn(*shape), dtype)106          indices = np.random.randint(shape[axis], size=indices_shape)107          with self.session() as sess, self.test_scope():108            tf_params = array_ops.placeholder(dtype=dtype)109            tf_indices = constant_op.constant(indices, dtype=dtypes.int32)110            gather = array_ops.gather(tf_params, tf_indices, axis=axis)111            gather_value = sess.run(gather, feed_dict={tf_params: params})112            gather_np = constant_op.constant(113                np.take(params, indices, axis=axis), dtype)114            self.assertAllEqual(gather_np, gather_value)115  def testIndicesWithDifferentDimensions(self):116    with self.session():117      for dtype in self.numeric_tf_types:118        params = array_ops.placeholder(dtype=dtype)119        indices = array_ops.placeholder(dtype=np.int32)120        with self.test_scope():121          gather = array_ops.gather(params, indices)122        self.assertAllEqual(123            7, gather.eval(feed_dict={params: [4, 7, 2], indices: 1}))124        self.assertAllEqual(125            [7], gather.eval(feed_dict={params: [4, 7, 2], indices: [1]}))126        self.assertAllEqual(127            [[7]], gather.eval(feed_dict={params: [4, 7, 2], indices: [[1]]}))128  def testGatherPrecision(self):129    with self.session() as session, self.test_scope():130      data = np.array([[0, 0, 0, 0], [0, 2 * (1 + np.exp2(-8)), 0, 0],131                       [0, 0, 0, 0], [0.015789, 0.0985, 0.55789, 0.3842]])132      indices = np.array([1, 2, 3, 1])133      dtype = dtypes.float32134      params_np = self._buildParams(data, dtype)135      params = array_ops.placeholder(dtype=dtype)136      indices_tf = constant_op.constant(indices)137      gather_t = array_ops.gather(params, indices_tf)138      gather_val = session.run(gather_t, feed_dict={params: params_np})139      np_val = params_np[indices]140      self.assertAllEqual(np_val, gather_val)141class GatherBenchmark(test.Benchmark):142  """Microbenchmarks for the gather op."""143  def _benchmarkGather(self, name, axis, gather_indices, use_xla_jit):144    def BuilderFn():145      inputs = variables.Variable(146          array_ops.zeros([100, 100, 10, 100, 50], dtype=dtypes.float32),147          dtype=dtypes.float32,148          name='input')149      indices = variables.Variable(150          gather_indices, dtype=dtypes.int32, name='indices')151      gather_t = array_ops.gather(inputs, indices, axis=axis)152      return '%s.axis%d' % (name, axis), [gather_t]153    xla_test.Benchmark(self, BuilderFn, use_xla_jit=use_xla_jit, device='cpu')154  def _benchmarkSliceGather(self, axis, use_xla_jit):155    """Benchmarks a gather op that's really a dynamic slice."""156    self._benchmarkGather('slice_gather', axis, [1], use_xla_jit)157  def _benchmarkNontrivialGather(self, axis, use_xla_jit):158    self._benchmarkGather('nontrivial_gather', axis, [9, 1, 0, 2] * 4,159                          use_xla_jit)160  def benchmarkSliceGatherAxis0(self):161    self._benchmarkSliceGather(axis=0, use_xla_jit=False)162  def benchmarkSliceGatherAxis0XLA(self):163    self._benchmarkSliceGather(axis=0, use_xla_jit=True)164  def benchmarkSliceGatherAxis1(self):165    self._benchmarkSliceGather(axis=1, use_xla_jit=False)...hlsl.gathercmpRGBA.offsetarray.dx10.frag
Source:hlsl.gathercmpRGBA.offsetarray.dx10.frag  
1SamplerComparisonState g_sSampCmp : register(s0);2uniform Texture1DArray <float4> g_tTex1df4a : register(t0);3Texture1DArray <int4>   g_tTex1di4a;4Texture1DArray <uint4>  g_tTex1du4a;5Texture2DArray <float4> g_tTex2df4a;6Texture2DArray <int4>   g_tTex2di4a;7Texture2DArray <uint4>  g_tTex2du4a;8TextureCubeArray <float4> g_tTexcdf4a;9TextureCubeArray <int4>   g_tTexcdi4a;10TextureCubeArray <uint4>  g_tTexcdu4a;11struct PS_OUTPUT12{13    float4 Color : SV_Target0;14    float  Depth : SV_Depth;15};16uniform float  c1;17uniform float2 c2;18uniform float3 c3;19uniform float4 c4;20uniform int  o1;21uniform int2 o2;22uniform int3 o3;23uniform int4 o4;24PS_OUTPUT main()25{26   PS_OUTPUT psout;27   uint status;28   // no 1D gathers29   float4 txval401 = g_tTex2df4a . GatherCmp(g_sSampCmp, c3, 0.75, o2);30   int4   txval411 = g_tTex2di4a . GatherCmp(g_sSampCmp, c3, 0.75, o2);31   uint4  txval421 = g_tTex2du4a . GatherCmp(g_sSampCmp, c3, 0.75, o2);32   float4 txval001 = g_tTex2df4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2);33   int4   txval011 = g_tTex2di4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2);34   uint4  txval021 = g_tTex2du4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2);35   float4 txval004 = g_tTex2df4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);36   int4   txval014 = g_tTex2di4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);37   uint4  txval024 = g_tTex2du4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);38   39   // float4 txval00s = g_tTex2df4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2, status);40   // int4   txval01s = g_tTex2di4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2, status);41   // uint4  txval02s = g_tTex2du4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2, status);42   // float4 txval004s = g_tTex2df4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);43   // int4   txval014s = g_tTex2di4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);44   // uint4  txval024s = g_tTex2du4a . GatherCmpRed(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);45   float4 txval101 = g_tTex2df4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2);46   int4   txval111 = g_tTex2di4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2);47   uint4  txval121 = g_tTex2du4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2);48   float4 txval104 = g_tTex2df4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);49   int4   txval114 = g_tTex2di4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);50   uint4  txval124 = g_tTex2du4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);51   // float4 txval10s = g_tTex2df4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2, status);52   // int4   txval11s = g_tTex2di4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2, status);53   // uint4  txval12s = g_tTex2du4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2, status);54   // float4 txval104 = g_tTex2df4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);55   // int4   txval114 = g_tTex2di4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);56   // uint4  txval124 = g_tTex2du4a . GatherCmpGreen(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);57   float4 txval201 = g_tTex2df4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2);58   int4   txval211 = g_tTex2di4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2);59   uint4  txval221 = g_tTex2du4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2);60   float4 txval204 = g_tTex2df4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);61   int4   txval214 = g_tTex2di4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);62   uint4  txval224 = g_tTex2du4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);63   // float4 txval204s = g_tTex2df4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);64   // int4   txval214s = g_tTex2di4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);65   // uint4  txval224s = g_tTex2du4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);66   // float4 txval20s = g_tTex2df4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2, status);67   // int4   txval21s = g_tTex2di4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2, status);68   // uint4  txval22s = g_tTex2du4a . GatherCmpBlue(g_sSampCmp, c3, 0.75, o2, status);69   float4 txval301 = g_tTex2df4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2);70   int4   txval311 = g_tTex2di4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2);71   uint4  txval321 = g_tTex2du4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2);72   float4 txval304 = g_tTex2df4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);73   int4   txval314 = g_tTex2di4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);74   uint4  txval324 = g_tTex2du4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2, o2, o2, o2);75   // float4 txval304s = g_tTex2df4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);76   // int4   txval314s = g_tTex2di4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);77   // uint4  txval324s = g_tTex2du4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2, o2, o2, o2, status);78   // float4 txval30s = g_tTex2df4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2, status);79   // int4   txval31s = g_tTex2di4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2, status);80   // uint4  txval32s = g_tTex2du4a . GatherCmpAlpha(g_sSampCmp, c3, 0.75, o2, status);81   // no 3D gathers with offset82   psout.Color = 1.0;83   psout.Depth = 1.0;84   return psout;...hlsl.gatherRGBA.offset.dx10.frag
Source:hlsl.gatherRGBA.offset.dx10.frag  
1SamplerState       g_sSamp : register(s0);2uniform sampler2D          g_sSamp2d;3Texture1D          g_tTex1df4a : register(t1);4uniform Texture1D <float4> g_tTex1df4 : register(t0);5Texture1D <int4>   g_tTex1di4;6Texture1D <uint4>  g_tTex1du4;7Texture2D <float4> g_tTex2df4;8Texture2D <int4>   g_tTex2di4;9Texture2D <uint4>  g_tTex2du4;10Texture3D <float4> g_tTex3df4;11Texture3D <int4>   g_tTex3di4;12Texture3D <uint4>  g_tTex3du4;13TextureCube <float4> g_tTexcdf4;14TextureCube <int4>   g_tTexcdi4;15TextureCube <uint4>  g_tTexcdu4;16struct PS_OUTPUT17{18    float4 Color : SV_Target0;19    float  Depth : SV_Depth;20};21uniform float  c1;22uniform float2 c2;23uniform float3 c3;24uniform float4 c4;25uniform int  o1;26uniform int2 o2;27uniform int3 o3;28uniform int4 o4;29PS_OUTPUT main()30{31   PS_OUTPUT psout;32   uint status;33   // no 1D gathers34   float4 txval001 = g_tTex2df4 . GatherRed(g_sSamp, c2, o2);35   int4   txval011 = g_tTex2di4 . GatherRed(g_sSamp, c2, o2);36   uint4  txval021 = g_tTex2du4 . GatherRed(g_sSamp, c2, o2);37   float4 txval004 = g_tTex2df4 . GatherRed(g_sSamp, c2, o2, o2, o2, o2);38   int4   txval014 = g_tTex2di4 . GatherRed(g_sSamp, c2, o2, o2, o2, o2);39   uint4  txval024 = g_tTex2du4 . GatherRed(g_sSamp, c2, o2, o2, o2, o2);40   41   // float4 txval00s = g_tTex2df4 . GatherRed(g_sSamp, c2, o2, status);42   // int4   txval01s = g_tTex2di4 . GatherRed(g_sSamp, c2, o2, status);43   // uint4  txval02s = g_tTex2du4 . GatherRed(g_sSamp, c2, o2, status);44   // float4 txval004s = g_tTex2df4 . GatherRed(g_sSamp, c2, o2, o2, o2, o2, status);45   // int4   txval014s = g_tTex2di4 . GatherRed(g_sSamp, c2, o2, o2, o2, o2, status);46   // uint4  txval024s = g_tTex2du4 . GatherRed(g_sSamp, c2, o2, o2, o2, o2, status);47   float4 txval101 = g_tTex2df4 . GatherGreen(g_sSamp, c2, o2);48   int4   txval111 = g_tTex2di4 . GatherGreen(g_sSamp, c2, o2);49   uint4  txval121 = g_tTex2du4 . GatherGreen(g_sSamp, c2, o2);50   float4 txval104 = g_tTex2df4 . GatherGreen(g_sSamp, c2, o2, o2, o2, o2);51   int4   txval114 = g_tTex2di4 . GatherGreen(g_sSamp, c2, o2, o2, o2, o2);52   uint4  txval124 = g_tTex2du4 . GatherGreen(g_sSamp, c2, o2, o2, o2, o2);53   // float4 txval10s = g_tTex2df4 . GatherGreen(g_sSamp, c2, o2, status);54   // int4   txval11s = g_tTex2di4 . GatherGreen(g_sSamp, c2, o2, status);55   // uint4  txval12s = g_tTex2du4 . GatherGreen(g_sSamp, c2, o2, status);56   // float4 txval104 = g_tTex2df4 . GatherGreen(g_sSamp, c2, o2, o2, o2, o2, status);57   // int4   txval114 = g_tTex2di4 . GatherGreen(g_sSamp, c2, o2, o2, o2, o2, status);58   // uint4  txval124 = g_tTex2du4 . GatherGreen(g_sSamp, c2, o2, o2, o2, o2, status);59   float4 txval201 = g_tTex2df4 . GatherBlue(g_sSamp, c2, o2);60   int4   txval211 = g_tTex2di4 . GatherBlue(g_sSamp, c2, o2);61   uint4  txval221 = g_tTex2du4 . GatherBlue(g_sSamp, c2, o2);62   float4 txval204 = g_tTex2df4 . GatherBlue(g_sSamp, c2, o2, o2, o2, o2);63   int4   txval214 = g_tTex2di4 . GatherBlue(g_sSamp, c2, o2, o2, o2, o2);64   uint4  txval224 = g_tTex2du4 . GatherBlue(g_sSamp, c2, o2, o2, o2, o2);65   // float4 txval204s = g_tTex2df4 . GatherBlue(g_sSamp, c2, o2, o2, o2, o2, status);66   // int4   txval214s = g_tTex2di4 . GatherBlue(g_sSamp, c2, o2, o2, o2, o2, status);67   // uint4  txval224s = g_tTex2du4 . GatherBlue(g_sSamp, c2, o2, o2, o2, o2, status);68   // float4 txval20s = g_tTex2df4 . GatherBlue(g_sSamp, c2, o2, status);69   // int4   txval21s = g_tTex2di4 . GatherBlue(g_sSamp, c2, o2, status);70   // uint4  txval22s = g_tTex2du4 . GatherBlue(g_sSamp, c2, o2, status);71   float4 txval301 = g_tTex2df4 . GatherAlpha(g_sSamp, c2, o2);72   int4   txval311 = g_tTex2di4 . GatherAlpha(g_sSamp, c2, o2);73   uint4  txval321 = g_tTex2du4 . GatherAlpha(g_sSamp, c2, o2);74   float4 txval304 = g_tTex2df4 . GatherAlpha(g_sSamp, c2, o2, o2, o2, o2);75   int4   txval314 = g_tTex2di4 . GatherAlpha(g_sSamp, c2, o2, o2, o2, o2);76   uint4  txval324 = g_tTex2du4 . GatherAlpha(g_sSamp, c2, o2, o2, o2, o2);77   // float4 txval304s = g_tTex2df4 . GatherAlpha(g_sSamp, c2, o2, o2, o2, o2, status);78   // int4   txval314s = g_tTex2di4 . GatherAlpha(g_sSamp, c2, o2, o2, o2, o2, status);79   // uint4  txval324s = g_tTex2du4 . GatherAlpha(g_sSamp, c2, o2, o2, o2, o2, status);80   // float4 txval30s = g_tTex2df4 . GatherAlpha(g_sSamp, c2, o2, status);81   // int4   txval31s = g_tTex2di4 . GatherAlpha(g_sSamp, c2, o2, status);82   // uint4  txval32s = g_tTex2du4 . GatherAlpha(g_sSamp, c2, o2, status);83   // no 3D gathers with offset84   psout.Color = 1.0;85   psout.Depth = 1.0;86   return psout;...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
