I am done

This commit is contained in:
2024-10-30 22:14:35 +01:00
parent 720dc28c09
commit 40e2a747cf
36901 changed files with 5011519 additions and 0 deletions

View File

@ -0,0 +1,159 @@
# 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.
# ==============================================================================
"""Package for histogram compression."""
import dataclasses
import numpy as np
from typing import Tuple
# Normal CDF for std_devs: (-Inf, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, Inf)
# naturally gives bands around median of width 1 std dev, 2 std dev, 3 std dev,
# and then the long tail.
NORMAL_HISTOGRAM_BPS = (0, 668, 1587, 3085, 5000, 6915, 8413, 9332, 10000)
@dataclasses.dataclass(frozen=True)
class CompressedHistogramValue:
"""Represents a value in a compressed histogram.
Attributes:
basis_point: Compression point represented in basis point, 1/100th of a
percent.
value: Cumulative weight at the basis point.
"""
basis_point: float
value: float
def as_tuple(self) -> Tuple[float, float]:
"""Returns the basis point and the value as a tuple."""
return (self.basis_point, self.value)
# TODO(@jart): Unfork these methods.
def compress_histogram_proto(histo, bps=NORMAL_HISTOGRAM_BPS):
"""Creates fixed size histogram by adding compression to accumulated state.
This routine transforms a histogram at a particular step by interpolating its
variable number of buckets to represent their cumulative weight at a constant
number of compression points. This significantly reduces the size of the
histogram and makes it suitable for a two-dimensional area plot where the
output of this routine constitutes the ranges for a single x coordinate.
Args:
histo: A HistogramProto object.
bps: Compression points represented in basis points, 1/100ths of a percent.
Defaults to normal distribution.
Returns:
List of values for each basis point.
"""
# See also: Histogram::Percentile() in core/lib/histogram/histogram.cc
if not histo.num:
return [CompressedHistogramValue(b, 0.0).as_tuple() for b in bps]
bucket = np.array(histo.bucket)
bucket_limit = list(histo.bucket_limit)
weights = (bucket * bps[-1] / (bucket.sum() or 1.0)).cumsum()
values = []
j = 0
while j < len(bps):
i = np.searchsorted(weights, bps[j], side="right")
while i < len(weights):
cumsum = weights[i]
cumsum_prev = weights[i - 1] if i > 0 else 0.0
if cumsum == cumsum_prev: # prevent lerp divide by zero
i += 1
continue
if not i or not cumsum_prev:
lhs = histo.min
else:
lhs = max(bucket_limit[i - 1], histo.min)
rhs = min(bucket_limit[i], histo.max)
weight = _lerp(bps[j], cumsum_prev, cumsum, lhs, rhs)
values.append(CompressedHistogramValue(bps[j], weight).as_tuple())
j += 1
break
else:
break
while j < len(bps):
values.append(CompressedHistogramValue(bps[j], histo.max).as_tuple())
j += 1
return values
def compress_histogram(buckets, bps=NORMAL_HISTOGRAM_BPS):
"""Creates fixed size histogram by adding compression to accumulated state.
This routine transforms a histogram at a particular step by linearly
interpolating its variable number of buckets to represent their cumulative
weight at a constant number of compression points. This significantly reduces
the size of the histogram and makes it suitable for a two-dimensional area
plot where the output of this routine constitutes the ranges for a single x
coordinate.
Args:
buckets: A list of buckets, each of which is a 3-tuple of the form
`(min, max, count)`.
bps: Compression points represented in basis points, 1/100ths of a percent.
Defaults to normal distribution.
Returns:
List of values for each basis point.
"""
# See also: Histogram::Percentile() in core/lib/histogram/histogram.cc
buckets = np.array(buckets)
if not buckets.size:
return [CompressedHistogramValue(b, 0.0).as_tuple() for b in bps]
(minmin, maxmax) = (buckets[0][0], buckets[-1][1])
counts = buckets[:, 2]
right_edges = list(buckets[:, 1])
weights = (counts * bps[-1] / (counts.sum() or 1.0)).cumsum()
result = []
bp_index = 0
while bp_index < len(bps):
i = np.searchsorted(weights, bps[bp_index], side="right")
while i < len(weights):
cumsum = weights[i]
cumsum_prev = weights[i - 1] if i > 0 else 0.0
if cumsum == cumsum_prev: # prevent division-by-zero in `_lerp`
i += 1
continue
if not i or not cumsum_prev:
lhs = minmin
else:
lhs = max(right_edges[i - 1], minmin)
rhs = min(right_edges[i], maxmax)
weight = _lerp(bps[bp_index], cumsum_prev, cumsum, lhs, rhs)
result.append(
CompressedHistogramValue(bps[bp_index], weight).as_tuple()
)
bp_index += 1
break
else:
break
while bp_index < len(bps):
result.append(
CompressedHistogramValue(bps[bp_index], maxmax).as_tuple()
)
bp_index += 1
return result
def _lerp(x, x0, x1, y0, y1):
"""Affinely map from [x0, x1] onto [y0, y1]."""
return y0 + (x - x0) * float(y1 - y0) / (x1 - x0)

View File

@ -0,0 +1,117 @@
# 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.
# ==============================================================================
"""The TensorBoard Distributions (a.k.a. compressed histograms) plugin.
See `http_api.md` in this directory for specifications of the routes for
this plugin.
"""
from werkzeug import wrappers
from tensorboard import plugin_util
from tensorboard.backend import http_util
from tensorboard.plugins import base_plugin
from tensorboard.plugins.distribution import compressor
from tensorboard.plugins.distribution import metadata
from tensorboard.plugins.histogram import histograms_plugin
class DistributionsPlugin(base_plugin.TBPlugin):
"""Distributions Plugin for TensorBoard.
This supports both old-style summaries (created with TensorFlow ops
that output directly to the `histo` field of the proto) and new-
style summaries (as created by the
`tensorboard.plugins.histogram.summary` module).
"""
plugin_name = metadata.PLUGIN_NAME
# Use a round number + 1 since sampling includes both start and end steps,
# so N+1 samples corresponds to dividing the step sequence into N intervals.
SAMPLE_SIZE = 501
def __init__(self, context):
"""Instantiates DistributionsPlugin via TensorBoard core.
Args:
context: A base_plugin.TBContext instance.
"""
self._histograms_plugin = histograms_plugin.HistogramsPlugin(context)
def get_plugin_apps(self):
return {
"/distributions": self.distributions_route,
"/tags": self.tags_route,
}
def is_active(self):
"""This plugin is active iff any run has at least one histogram tag.
(The distributions plugin uses the same data source as the
histogram plugin.)
"""
return self._histograms_plugin.is_active()
def data_plugin_names(self):
return (self._histograms_plugin.plugin_name,)
def frontend_metadata(self):
return base_plugin.FrontendMetadata(
element_name="tf-distribution-dashboard",
)
def distributions_impl(self, ctx, tag, run, experiment):
"""Result of the form `(body, mime_type)`.
Raises:
tensorboard.errors.PublicError: On invalid request.
"""
(histograms, mime_type) = self._histograms_plugin.histograms_impl(
ctx, tag, run, experiment=experiment, downsample_to=self.SAMPLE_SIZE
)
return (
[self._compress(histogram) for histogram in histograms],
mime_type,
)
def _compress(self, histogram):
(wall_time, step, buckets) = histogram
converted_buckets = compressor.compress_histogram(buckets)
return [wall_time, step, converted_buckets]
def index_impl(self, ctx, experiment):
return self._histograms_plugin.index_impl(ctx, experiment=experiment)
@wrappers.Request.application
def tags_route(self, request):
ctx = plugin_util.context(request.environ)
experiment = plugin_util.experiment_id(request.environ)
index = self.index_impl(ctx, experiment=experiment)
return http_util.Respond(request, index, "application/json")
@wrappers.Request.application
def distributions_route(self, request):
"""Given a tag and single run, return an array of compressed
histograms."""
ctx = plugin_util.context(request.environ)
experiment = plugin_util.experiment_id(request.environ)
tag = request.args.get("tag")
run = request.args.get("run")
(body, mime_type) = self.distributions_impl(
ctx, tag, run, experiment=experiment
)
return http_util.Respond(request, body, mime_type)

View File

@ -0,0 +1,20 @@
# Copyright 2020 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.
# ==============================================================================
"""Information on the distributions plugin."""
# This name is used as the plugin prefix route and to identify this plugin
# generally.
PLUGIN_NAME = "distributions"