Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

histogram: set v3 as default #5415

Merged
merged 4 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions tensorboard/plugins/histogram/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@

A histogram summary stores a list of buckets. Each bucket is encoded as
a triple `[left_edge, right_edge, count]`. Thus, a full histogram is
encoded as a tensor of dimension `[k, 3]`.
encoded as a tensor of dimension `[k, 3]`, where the first `k - 1` buckets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module docstring is all referring to the legacy "tb.summary" APIs (for TensorFlow 1.x) that were defined in this file, which we aren't changing, so I think it's ok to just leave this docstring as-is (and only have the new format documented in summary_v2.py's docstring). At some point we should rearrange and clean this up so that's more obvious but probably best to do that for all the summary APIs vs piecemeal.

Copy link
Member Author

@yatbear yatbear Nov 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks for the suggestion!

are closed-open and the last bucket is closed-closed.

In general, the value of `k` (the number of buckets) will be a constant,
like 30. There are two edge cases: if there is no data, then there are
no buckets (the shape is `[0, 3]`); and if there is data but all points
have the same value, then there is one bucket whose left and right
endpoints are the same (the shape is `[1, 3]`).
like 30. For V2 format (deprecated), there are two edge cases: if there
is no data, then there are no buckets (the shape is `[0, 3]`); and if
there is data but all points have the same value, then there is one
bucket whose left and right endpoints are the same (the shape is `[1, 3]`).

For V3 format, the shape of the output histogram is always constant (`[k, 3]`).
In the case of empty data, the output will be an all-zero histogram of shape
`[k, 3]`, where all edges and counts are zeros. If there is data but all points
have the same value, then all buckets' left and right edges are the same and
only the last bucket has nonzero count.

NOTE: This module is in beta, and its API is subject to change, but the
data that it stores to disk will be supported forever.
Expand All @@ -35,13 +42,10 @@
from tensorboard.plugins.histogram import summary_v2


# Export V2 versions.
# Export the latest versions.
histogram = summary_v2.histogram
histogram_pb = summary_v2.histogram_pb

# Export V3 versions.
histogram_v3 = summary_v2.histogram_v3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this one line for now, so that anyone who was testing this out internally won't be broken by this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.



def _buckets(data, bucket_count=None):
"""Create a TensorFlow op to group data into histogram buckets.
Expand Down
46 changes: 1 addition & 45 deletions tensorboard/plugins/histogram/summary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,50 +264,6 @@ def test_default_step(self):
# Reset to default state for other tests.
tf2.summary.experimental.set_step(None)


class SummaryV2OpGraphTest(SummaryV2OpTest, tf.test.TestCase):
def write_histogram_event(self, *args, **kwargs):
kwargs.setdefault("step", 1)
# Hack to extract current scope since there's no direct API for it.
with tf.name_scope("_") as temp_scope:
scope = temp_scope.rstrip("/_")

@tf2.function
def graph_fn():
# Recreate the active scope inside the defun since it won't propagate.
with tf.name_scope(scope):
self.call_histogram_op(*args, **kwargs)

writer = tf2.summary.create_file_writer(self.get_temp_dir())
with writer.as_default():
graph_fn()
writer.close()

def test_no_gradient_error_xla(self):
@tf2.function(jit_compile=True)
def graph_fn():
x = tf.constant(1.0)
with tf2.GradientTape() as tape1:
with tf2.GradientTape() as tape2:
tape1.watch(x)
tape2.watch(x)
self.call_histogram_op(
name="loss", step=0, data=x, buckets=10
)

# Note that XLA CPU/GPU has no outside compilation support, so summaries
# won't actually run in a jit_compiled function. TPUs do, and follow
# some similar codepaths, so this test stops at graph building to
# exercise those paths without a TPU available.
writer = tf2.summary.create_file_writer(self.get_temp_dir())
with writer.as_default():
graph_fn.get_concrete_function()


class SummaryV3OpTest(SummaryV2OpTest, tf.test.TestCase):
def call_histogram_op(self, *args, **kwargs):
summary.histogram_v3(*args, **kwargs)

def test_singleton_input(self):
pb = self.histogram("twelve", [12])
buckets = tensor_util.make_ndarray(pb.value[0].tensor)
Expand Down Expand Up @@ -344,7 +300,7 @@ def test_zero_bucket_count(self):
np.testing.assert_array_equal(buckets, np.array([]).reshape((0, 3)))


class SummaryV3OpGraphTest(SummaryV3OpTest, tf.test.TestCase):
class SummaryV2OpGraphTest(SummaryV2OpTest, tf.test.TestCase):
def write_histogram_event(self, *args, **kwargs):
kwargs.setdefault("step", 1)
# Hack to extract current scope since there's no direct API for it.
Expand Down
Loading