Skip to content

Fix documentation error in tf.reverse docstring (#1) - #13497

Merged
caisq merged 1 commit into
tensorflow:masterfrom
craffel:master
Oct 13, 2017
Merged

caisq merged 1 commit into
tensorflow:masterfrom
craffel:master

Conversation

@craffel

@craffel craffel commented Oct 5, 2017

Copy link
Copy Markdown
Contributor

The first example in the tf.reverse docstring causes a ValueError, as shown below. tf.reverse requires axis to be 1D; -1 is not 1D, it's 0D; it should be [-1].

In [1]: import tensorflow as tf
In [2]: t = tf.constant([[[[ 0,  1,  2,  3], [ 4,  5,  6,  7], [ 8,  9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]])
In [3]: dims = -1
In [4]: sess = tf.InteractiveSession()
In [5]: tf.reverse(t, dims).eval()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-7752813cc8a9> in <module>()
----> 1 tf.reverse(t, dims).eval()

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.pyc in reverse(tensor, axis, name)
   2332
   2333 def reverse(tensor, axis, name=None):
-> 2334   return gen_array_ops.reverse_v2(tensor, axis, name)
   2335 reverse.__doc__ = gen_array_ops.reverse_v2.__doc__
   2336

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.pyc in reverse_v2(tensor, axis, name)
   2697   """
   2698   result = _op_def_lib.apply_op("ReverseV2", tensor=tensor, axis=axis,
-> 2699                                 name=name)
   2700   return result
   2701

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords)
    765         op = g.create_op(op_type_name, inputs, output_types, name=scope,
    766                          input_types=input_types, attrs=attr_protos,
--> 767                          op_def=op_def)
    768         if output_structure:
    769           outputs = op.outputs

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
   2506                     original_op=self._default_original_op, op_def=op_def)
   2507     if compute_shapes:
-> 2508       set_shapes_for_outputs(ret)
   2509     self._add_op(ret)
   2510     self._record_op_seen_by_control_dependencies(ret)

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in set_shapes_for_outputs(op)
   1871       shape_func = _call_cpp_shape_fn_and_require_op
   1872
-> 1873   shapes = shape_func(op)
   1874   if shapes is None:
   1875     raise RuntimeError(

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in call_with_requiring(op)
   1821
   1822   def call_with_requiring(op):
-> 1823     return call_cpp_shape_fn(op, require_shape_fn=True)
   1824
   1825   _call_cpp_shape_fn_and_require_op = call_with_requiring

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.pyc in call_cpp_shape_fn(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn)
    608     res = _call_cpp_shape_fn_impl(op, input_tensors_needed,
    609                                   input_tensors_as_shapes_needed,
--> 610                                   debug_python_shape_fn, require_shape_fn)
    611     if not isinstance(res, dict):
    612       # Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op).

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.pyc in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn)
    674       missing_shape_fn = True
    675     else:
--> 676       raise ValueError(err.message)
    677
    678   if missing_shape_fn:

ValueError: Shape must be rank 1 but is rank 0 for 'ReverseV2' (op: 'ReverseV2') with input shapes: [1,2,3,4], [].

The first example in the tf.reverse docstring causes a ValueError:

```Python
In [1]: import tensorflow as tf
In [2]: t = tf.constant([[[[ 0,  1,  2,  3], [ 4,  5,  6,  7], [ 8,  9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]])
In [3]: dims = -1
In [4]: sess = tf.InteractiveSession()
In [5]: tf.reverse(t, dims).eval()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-7752813cc8a9> in <module>()
----> 1 tf.reverse(t, dims).eval()

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.pyc in reverse(tensor, axis, name)
   2332
   2333 def reverse(tensor, axis, name=None):
-> 2334   return gen_array_ops.reverse_v2(tensor, axis, name)
   2335 reverse.__doc__ = gen_array_ops.reverse_v2.__doc__
   2336

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.pyc in reverse_v2(tensor, axis, name)
   2697   """
   2698   result = _op_def_lib.apply_op("ReverseV2", tensor=tensor, axis=axis,
-> 2699                                 name=name)
   2700   return result
   2701

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords)
    765         op = g.create_op(op_type_name, inputs, output_types, name=scope,
    766                          input_types=input_types, attrs=attr_protos,
--> 767                          op_def=op_def)
    768         if output_structure:
    769           outputs = op.outputs

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
   2506                     original_op=self._default_original_op, op_def=op_def)
   2507     if compute_shapes:
-> 2508       set_shapes_for_outputs(ret)
   2509     self._add_op(ret)
   2510     self._record_op_seen_by_control_dependencies(ret)

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in set_shapes_for_outputs(op)
   1871       shape_func = _call_cpp_shape_fn_and_require_op
   1872
-> 1873   shapes = shape_func(op)
   1874   if shapes is None:
   1875     raise RuntimeError(

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in call_with_requiring(op)
   1821
   1822   def call_with_requiring(op):
-> 1823     return call_cpp_shape_fn(op, require_shape_fn=True)
   1824
   1825   _call_cpp_shape_fn_and_require_op = call_with_requiring

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.pyc in call_cpp_shape_fn(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn)
    608     res = _call_cpp_shape_fn_impl(op, input_tensors_needed,
    609                                   input_tensors_as_shapes_needed,
--> 610                                   debug_python_shape_fn, require_shape_fn)
    611     if not isinstance(res, dict):
    612       # Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op).

/Users/craffel/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.pyc in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn)
    674       missing_shape_fn = True
    675     else:
--> 676       raise ValueError(err.message)
    677
    678   if missing_shape_fn:

ValueError: Shape must be rank 1 but is rank 0 for 'ReverseV2' (op: 'ReverseV2') with input shapes: [1,2,3,4], [].
```
@tensorflow-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@googlebot

Copy link
Copy Markdown

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If your company signed a CLA, they designated a Point of Contact who decides which employees are authorized to participate. You may need to contact the Point of Contact for your company and ask to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the project maintainer to go/cla#troubleshoot.
  • In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again.

@craffel

craffel commented Oct 5, 2017

Copy link
Copy Markdown
Contributor Author

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

Do I need to sign a CLA if I'm a Google employee?

@frankchn

frankchn commented Oct 5, 2017

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@frankchn

frankchn commented Oct 5, 2017

Copy link
Copy Markdown
Contributor

I signed it!

@caisq
caisq merged commit 6eabe2a into tensorflow:master Oct 13, 2017
copybara-service Bot pushed a commit that referenced this pull request Jun 14, 2024
…tion

Imported from GitHub PR openxla/xla#13497

We observe larger n_div_k can improve tree reduction performance. Swap k and n_div_k if possible. See the below cases to get detailed information:
```bash
Case 0:
ENTRY main {
      a = bf16[1024,50048] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(1) k = 128, n_div_k = 391, runtime = 102us
(2) k = 391, n_div_k = 128, runtime = 132us
Even though (2) can be vectorized, but n_div_k is too small. (1) can get better performance than (2).

Case 1:
ENTRY main {
      a = bf16[10303,1024] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={0}, to_apply=Add
})"
(1) k = 64, n_div_k = 161, runtime = 33us
(2) k = 161, n_div_k = 64, runtime = 46us
ENTRY main {
      a = bf16[512,100005] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[512] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(3) k = 295, n_div_k = 339, runtime = 180us
(4) k = 339, n_div_k = 295, runtime = 183us
ENTRY main {
      a = bf16[1024,50257] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(5) k = 140, n_div_k = 359, runtime = 104us
(6) k = 359, n_div_k = 140, runtime = 158us
Choose (1), (3) and (5) due to we want reduce more elements in the first reduction.

Case 2:
ENTRY main {
      a = bf16[1024,73984] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(1) k = 256, n_div_k = 289, runtime=142us
(2) k = 289, n_div_k = 256, runtime=133us
Choose (2) due to k is almost as large as n_div_k and (2) can be vectorized.
```

Copybara import of the project:

--
08cb6c95a4204be87327872d819b8ad947352e99 by Zhou, Lingzhi <[email protected]>:

swap inner and outer minor reduced dimension of tree reduction

--
467fddce0710a1cf0c5367f6e7e857ca7702ae91 by Zhou, Lingzhi <[email protected]>:

handle the corner case where k and n_div_k are divisible by 2

--
99ba27b4c493af62a59378cab9a8da0de70ec82a by Zhou, Lingzhi <[email protected]>:

missing equal sign

--
ee9c548ac7dc3f8ff7e49bce97731395e61cce74 by Zhou, Lingzhi <[email protected]>:

fix swap condition check for vectorization

--
59367d6ce6b9227600b3bf2610c5df420d7d9862 by Zhou, Lingzhi <[email protected]>:

add comment

--
624993c4ed45b580bf1ccc1e843986672a94fefa by Zhou, Lingzhi <[email protected]>:

add tests

--
e126e8d6d59d76a1bc4066567f61adc7615b62d4 by Zhou, Lingzhi <[email protected]>:

make comment more accurate

--
088e74424c1d2264f5bbf8291d3c142bf1532135 by Zhou, Lingzhi <[email protected]>:

dont swap if non aligned but vectorized

--
25f4ce9c888d6497be7e8ab1882e65091feb12f0 by Zhou, Lingzhi <[email protected]>:

make swapping logic more clearly

Merging this change closes #13497

FUTURE_COPYBARA_INTEGRATE_REVIEW=openxla/xla#13497 from lingzhi98:lingzhi/gpu_tree_reduction 25f4ce9c888d6497be7e8ab1882e65091feb12f0
PiperOrigin-RevId: 643317103
copybara-service Bot pushed a commit that referenced this pull request Jun 14, 2024
…tion

Imported from GitHub PR openxla/xla#13497

We observe larger n_div_k can improve tree reduction performance. Swap k and n_div_k if possible. See the below cases to get detailed information:
```bash
Case 0:
ENTRY main {
      a = bf16[1024,50048] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(1) k = 128, n_div_k = 391, runtime = 102us
(2) k = 391, n_div_k = 128, runtime = 132us
Even though (2) can be vectorized, but n_div_k is too small. (1) can get better performance than (2).

Case 1:
ENTRY main {
      a = bf16[10303,1024] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={0}, to_apply=Add
})"
(1) k = 64, n_div_k = 161, runtime = 33us
(2) k = 161, n_div_k = 64, runtime = 46us
ENTRY main {
      a = bf16[512,100005] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[512] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(3) k = 295, n_div_k = 339, runtime = 180us
(4) k = 339, n_div_k = 295, runtime = 183us
ENTRY main {
      a = bf16[1024,50257] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(5) k = 140, n_div_k = 359, runtime = 104us
(6) k = 359, n_div_k = 140, runtime = 158us
Choose (1), (3) and (5) due to we want reduce more elements in the first reduction.

Case 2:
ENTRY main {
      a = bf16[1024,73984] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(1) k = 256, n_div_k = 289, runtime=142us
(2) k = 289, n_div_k = 256, runtime=133us
Choose (2) due to k is almost as large as n_div_k and (2) can be vectorized.
```

Copybara import of the project:

--
08cb6c95a4204be87327872d819b8ad947352e99 by Zhou, Lingzhi <[email protected]>:

swap inner and outer minor reduced dimension of tree reduction

--
467fddce0710a1cf0c5367f6e7e857ca7702ae91 by Zhou, Lingzhi <[email protected]>:

handle the corner case where k and n_div_k are divisible by 2

--
99ba27b4c493af62a59378cab9a8da0de70ec82a by Zhou, Lingzhi <[email protected]>:

missing equal sign

--
ee9c548ac7dc3f8ff7e49bce97731395e61cce74 by Zhou, Lingzhi <[email protected]>:

fix swap condition check for vectorization

--
59367d6ce6b9227600b3bf2610c5df420d7d9862 by Zhou, Lingzhi <[email protected]>:

add comment

--
624993c4ed45b580bf1ccc1e843986672a94fefa by Zhou, Lingzhi <[email protected]>:

add tests

--
e126e8d6d59d76a1bc4066567f61adc7615b62d4 by Zhou, Lingzhi <[email protected]>:

make comment more accurate

--
088e74424c1d2264f5bbf8291d3c142bf1532135 by Zhou, Lingzhi <[email protected]>:

dont swap if non aligned but vectorized

--
25f4ce9c888d6497be7e8ab1882e65091feb12f0 by Zhou, Lingzhi <[email protected]>:

make swapping logic more clearly

Merging this change closes #13497

FUTURE_COPYBARA_INTEGRATE_REVIEW=openxla/xla#13497 from lingzhi98:lingzhi/gpu_tree_reduction 25f4ce9c888d6497be7e8ab1882e65091feb12f0
PiperOrigin-RevId: 643317103
copybara-service Bot pushed a commit that referenced this pull request Jun 14, 2024
…tion

Imported from GitHub PR openxla/xla#13497

We observe larger n_div_k can improve tree reduction performance. Swap k and n_div_k if possible. See the below cases to get detailed information:
```bash
Case 0:
ENTRY main {
      a = bf16[1024,50048] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(1) k = 128, n_div_k = 391, runtime = 102us
(2) k = 391, n_div_k = 128, runtime = 132us
Even though (2) can be vectorized, but n_div_k is too small. (1) can get better performance than (2).

Case 1:
ENTRY main {
      a = bf16[10303,1024] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={0}, to_apply=Add
})"
(1) k = 64, n_div_k = 161, runtime = 33us
(2) k = 161, n_div_k = 64, runtime = 46us
ENTRY main {
      a = bf16[512,100005] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[512] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(3) k = 295, n_div_k = 339, runtime = 180us
(4) k = 339, n_div_k = 295, runtime = 183us
ENTRY main {
      a = bf16[1024,50257] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(5) k = 140, n_div_k = 359, runtime = 104us
(6) k = 359, n_div_k = 140, runtime = 158us
Choose (1), (3) and (5) due to we want reduce more elements in the first reduction.

Case 2:
ENTRY main {
      a = bf16[1024,73984] parameter(0)
      constant = bf16[] constant(0)
      ROOT reduce = bf16[1024] reduce(a, constant), dimensions={1}, to_apply=Add
})"
(1) k = 256, n_div_k = 289, runtime=142us
(2) k = 289, n_div_k = 256, runtime=133us
Choose (2) due to k is almost as large as n_div_k and (2) can be vectorized.
```

Copybara import of the project:

--
08cb6c95a4204be87327872d819b8ad947352e99 by Zhou, Lingzhi <[email protected]>:

swap inner and outer minor reduced dimension of tree reduction

--
467fddce0710a1cf0c5367f6e7e857ca7702ae91 by Zhou, Lingzhi <[email protected]>:

handle the corner case where k and n_div_k are divisible by 2

--
99ba27b4c493af62a59378cab9a8da0de70ec82a by Zhou, Lingzhi <[email protected]>:

missing equal sign

--
ee9c548ac7dc3f8ff7e49bce97731395e61cce74 by Zhou, Lingzhi <[email protected]>:

fix swap condition check for vectorization

--
59367d6ce6b9227600b3bf2610c5df420d7d9862 by Zhou, Lingzhi <[email protected]>:

add comment

--
624993c4ed45b580bf1ccc1e843986672a94fefa by Zhou, Lingzhi <[email protected]>:

add tests

--
e126e8d6d59d76a1bc4066567f61adc7615b62d4 by Zhou, Lingzhi <[email protected]>:

make comment more accurate

--
088e74424c1d2264f5bbf8291d3c142bf1532135 by Zhou, Lingzhi <[email protected]>:

dont swap if non aligned but vectorized

--
25f4ce9c888d6497be7e8ab1882e65091feb12f0 by Zhou, Lingzhi <[email protected]>:

make swapping logic more clearly

Merging this change closes #13497

PiperOrigin-RevId: 643345300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants