* Enable grappler to propagate shapes through queues.
Change: 154789133
* Add whitelist support in uid of RunConfig.
Change: 154794859
* Fix a bunch of bad links and missing docs in contrib.
Change: 154820641
* Don't try to refine the shapes for a node if its inference context wasn't
successfully built by the AddNode() method.
Change: 154838211
* Fix issue related to empty bazel.rc file.
Change: 154840138
* Remove overly precise CHECK when rendering debug output for a function.
An `_Arg` node can have more than three attrs, because the runtime may
(and does) add system-defined attrs (viz. "_output_shapes") that do
not change the meaning of the op.
Change: 154850526
* Port makefile build breakage
Change: 154855106
* [TF:XLA] Try to incorporate Tensorflow node structure for large HLO GraphDefs.
This change assumes that a TF subgraph/op does not cross the boundary of a HLO
computation and always put top-level TF subgraphs/ops under HLO computations.
Change: 154855884
* Added a unit test to check what happens when 2 shapes with known rank but
unknown dimensions are merged
Change: 154856675
* [XLA] Refactor constant folding operations into a dedicated module
Refactor constant folding operations into a dedicated module, and added a new
ReplaceInstruction() API to collapse { computation->ReplaceInstruction();
changed=true}.
Change: 154857025
* Java: Docs: Update instructions for Windows.
Inspired by
http://stackoverflow.com/questions/43741775/tensorflow-in-java-running-failed
Change: 154859066
* Add more documentation for features and labels.
Change: 154859649
* Added link to high-performance models
Change: 154860213
* Navigation and index for new performance section documents.
Change: 154862215
* Fix shape mismatch between loss and weights.
Change: 154862650
* Add examples to TensorShape documentation and ran autoformatter.
Change: 154862667
* Move linking of cudnn_plugin, cublas_plugin and cufft_plugin from
stream_executor to the ops that need them.
Change: 154863520
* Properly track the persistent memory usage of lookup tables.
Change: 154866686
* Reset the inputs to ShapeRefiner::RunShapeFn so that it behaves the same every time it's called.
To properly handle queues that have populated by several enqueue ops, merge the shapes of the inputs to all the enqueue ops before calling InferenceContext::set_output_handle_shape(). This ensures that we detect incorrect queue setups (where the 2 enqueue ops might generate tensors with incompatible shapes), and that we take all the known shape information instead of that of just one of the enqueue ops.
Change: 154866747
* Making sure an error message will be produced by session_manager when a non-tensor object is passed in.
Otherwise the 'name' property is missing.
Change: 154868022
* Don't needlessly synchronize the CUDA stream in CropAndResize.
Make the op Async so we don't block an executor thread while waiting for the result of the box bounds check to be copied back to the host.
Change: 154868460
* Add contribution guidelines and standards section to CONTRIBUTING.md
Several parts are largely based on the post by @yaroslavvb at: #7443#issuecomment-279182613
Fixes #7443
Change: 154876045
* Final draft
Change: 154876563
* Final draft
Change: 154876646
* Fix losses documentation.
Fix documentation of get_total_loss() to be correct.
And add a helpful comment about a common pitfall.
Change: 154876822
* [XLA] Second change for HLO interpreter.
Extends HloEvaluator to allow evaluation of HLO Computation or single HLO instruction
with non-constant operands, by traversing the instruction in post order and keeps track of
each instruction along the way as evaluated literals.
Change: 154877580
* [tf distributions] Move the remaining whitelisted distributions to core.
Change: 154878206
* Add shape to error message.
Change: 154880260
* Revert "Fix build issue when `/usr/bin/python` path is not available (#9547)"
This reverts commit 95f37ebf0b
.
170 lines
7.0 KiB
Python
170 lines
7.0 KiB
Python
# Copyright 2016 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.
|
|
# ==============================================================================
|
|
"""Weight broadcasting operations.
|
|
|
|
In `tf.losses` and `tf.metrics`, we support limited weight broadcasting. This
|
|
file includes operations for those broadcasting rules.
|
|
"""
|
|
|
|
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
|
|
from tensorflow.python.framework import ops
|
|
from tensorflow.python.framework import tensor_util
|
|
from tensorflow.python.ops import array_ops
|
|
from tensorflow.python.ops import control_flow_ops
|
|
from tensorflow.python.ops import math_ops
|
|
from tensorflow.python.ops import sets
|
|
|
|
|
|
def _has_valid_dims(weights_shape, values_shape):
|
|
with ops.name_scope(
|
|
None, "has_invalid_dims", (weights_shape, values_shape)) as scope:
|
|
values_shape_2d = array_ops.expand_dims(values_shape, -1)
|
|
valid_dims = array_ops.concat(
|
|
(values_shape_2d, array_ops.ones_like(values_shape_2d)), axis=1)
|
|
weights_shape_2d = array_ops.expand_dims(weights_shape, -1)
|
|
invalid_dims = sets.set_difference(weights_shape_2d, valid_dims)
|
|
num_invalid_dims = array_ops.size(
|
|
invalid_dims.values, name="num_invalid_dims")
|
|
return math_ops.equal(0, num_invalid_dims, name=scope)
|
|
|
|
|
|
def _has_valid_nonscalar_shape(
|
|
weights_rank, weights_shape, values_rank, values_shape):
|
|
with ops.name_scope(
|
|
None, "has_valid_nonscalar_shape",
|
|
(weights_rank, weights_shape, values_rank, values_shape)) as scope:
|
|
is_same_rank = math_ops.equal(
|
|
values_rank, weights_rank, name="is_same_rank")
|
|
return control_flow_ops.cond(
|
|
is_same_rank,
|
|
lambda: _has_valid_dims(weights_shape, values_shape),
|
|
lambda: is_same_rank,
|
|
name=scope)
|
|
|
|
|
|
_ASSERT_BROADCASTABLE_ERROR_PREFIX = "weights can not be broadcast to values."
|
|
|
|
|
|
def assert_broadcastable(weights, values):
|
|
"""Asserts `weights` can be broadcast to `values`.
|
|
|
|
In `tf.losses` and `tf.metrics`, we support limited weight broadcasting. We
|
|
let weights be either scalar, or the same rank as the target values, with each
|
|
dimension either 1, or the same as the corresponding values dimension.
|
|
|
|
Args:
|
|
weights: `Tensor` of weights.
|
|
values: `Tensor` of values to which weights are applied.
|
|
|
|
Returns:
|
|
`Operation` raising `InvalidArgumentError` if `weights` has incorrect shape.
|
|
`no_op` if static checks determine `weights` has correct shape.
|
|
|
|
Raises:
|
|
ValueError: If static checks determine `weights` has incorrect shape.
|
|
"""
|
|
with ops.name_scope(None, "assert_broadcastable", (weights, values)) as scope:
|
|
with ops.name_scope(None, "weights", (weights,)) as weights_scope:
|
|
weights = ops.convert_to_tensor(weights, name=weights_scope)
|
|
weights_shape = array_ops.shape(weights, name="shape")
|
|
weights_rank = array_ops.rank(weights, name="rank")
|
|
weights_rank_static = tensor_util.constant_value(weights_rank)
|
|
|
|
with ops.name_scope(None, "values", (values,)) as values_scope:
|
|
values = ops.convert_to_tensor(values, name=values_scope)
|
|
values_shape = array_ops.shape(values, name="shape")
|
|
values_rank = array_ops.rank(values, name="rank")
|
|
values_rank_static = tensor_util.constant_value(values_rank)
|
|
|
|
# Try static checks.
|
|
if weights_rank_static is not None and values_rank_static is not None:
|
|
if weights_rank_static == 0:
|
|
return control_flow_ops.no_op(name="static_scalar_check_success")
|
|
if weights_rank_static != values_rank_static:
|
|
raise ValueError(
|
|
"%s values.rank=%s. weights.rank=%s."
|
|
" values.shape=%s. weights.shape=%s." % (
|
|
_ASSERT_BROADCASTABLE_ERROR_PREFIX, values_rank_static,
|
|
weights_rank_static, values.shape, weights.shape))
|
|
weights_shape_static = tensor_util.constant_value(weights_shape)
|
|
values_shape_static = tensor_util.constant_value(values_shape)
|
|
if weights_shape_static is not None and values_shape_static is not None:
|
|
# Sanity check, this should always be true since we checked rank above.
|
|
ndims = len(values_shape_static)
|
|
assert ndims == len(weights_shape_static)
|
|
|
|
for i in range(ndims):
|
|
if weights_shape_static[i] not in (1, values_shape_static[i]):
|
|
raise ValueError(
|
|
"%s Mismatch at dim %s. values.shape=%s weights.shape=%s." % (
|
|
_ASSERT_BROADCASTABLE_ERROR_PREFIX, i, values_shape_static,
|
|
weights_shape_static))
|
|
return control_flow_ops.no_op(name="static_dims_check_success")
|
|
|
|
# Dynamic checks.
|
|
is_scalar = math_ops.equal(0, weights_rank, name="is_scalar")
|
|
data = (
|
|
_ASSERT_BROADCASTABLE_ERROR_PREFIX,
|
|
"weights.shape=", weights.name, weights_shape,
|
|
"values.shape=", values.name, values_shape,
|
|
"is_scalar=", is_scalar,
|
|
)
|
|
is_valid_shape = control_flow_ops.cond(
|
|
is_scalar,
|
|
lambda: is_scalar,
|
|
lambda: _has_valid_nonscalar_shape( # pylint: disable=g-long-lambda
|
|
weights_rank, weights_shape, values_rank, values_shape),
|
|
name="is_valid_shape")
|
|
return control_flow_ops.Assert(is_valid_shape, data, name=scope)
|
|
|
|
|
|
def broadcast_weights(weights, values):
|
|
"""Broadcast `weights` to the same shape as `values`.
|
|
|
|
This returns a version of `weights` following the same broadcast rules as
|
|
`mul(weights, values)`, but limited to the weights shapes allowed by
|
|
`assert_broadcastable`. When computing a weighted average, use this function
|
|
to broadcast `weights` before summing them; e.g.,
|
|
`reduce_sum(w * v) / reduce_sum(_broadcast_weights(w, v))`.
|
|
|
|
Args:
|
|
weights: `Tensor` whose shape is broadcastable to `values` according to the
|
|
rules of `assert_broadcastable`.
|
|
values: `Tensor` of any shape.
|
|
|
|
Returns:
|
|
`weights` broadcast to `values` shape according to the rules of
|
|
`assert_broadcastable`.
|
|
"""
|
|
with ops.name_scope(None, "broadcast_weights", (weights, values)) as scope:
|
|
values = ops.convert_to_tensor(values, name="values")
|
|
weights = ops.convert_to_tensor(
|
|
weights, dtype=values.dtype.base_dtype, name="weights")
|
|
|
|
# Try static check for exact match.
|
|
weights_shape = weights.get_shape()
|
|
values_shape = values.get_shape()
|
|
if (weights_shape.is_fully_defined() and
|
|
values_shape.is_fully_defined() and
|
|
weights_shape.is_compatible_with(values_shape)):
|
|
return weights
|
|
|
|
with ops.control_dependencies((assert_broadcastable(weights, values),)):
|
|
return math_ops.multiply(
|
|
weights, array_ops.ones_like(values), name=scope)
|