chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
load("@xla//third_party/rules_python/python:py_library.bzl", "py_library")
load("//tensorflow:tensorflow.bzl", "py_test")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
licenses = ["notice"],
)
py_library(
name = "directives",
srcs = ["directives.py"],
strict_deps = True,
visibility = ["//tensorflow:__subpackages__"],
deps = [
"//tensorflow/python/util:tf_export",
],
)
py_library(
name = "special_functions",
srcs = ["special_functions.py"],
strict_deps = True,
visibility = ["//tensorflow:__subpackages__"],
deps = [
"//tensorflow/python/autograph/operators:data_structures",
"//tensorflow/python/framework:constant_op",
"//tensorflow/python/framework:tensor_util",
],
)
py_test(
name = "special_functions_test",
srcs = ["special_functions_test.py"],
strict_deps = True,
deps = [
#internal proto upb dep
"//third_party/py/numpy",
"//tensorflow/python/autograph/lang:special_functions",
"//tensorflow/python/framework:constant_op",
"//tensorflow/python/framework:dtypes",
"//tensorflow/python/framework:tensor_util",
"//tensorflow/python/ops:list_ops",
"//tensorflow/python/platform:client_testlib",
],
)
@@ -0,0 +1,94 @@
# 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.
# ==============================================================================
"""Directives are special no-op functions that serve as compilation markers.
They provide static information like type hints, compilation and TensorFlow
overrides.
These serve as annotations in the compiled code, allowing the user some control
over the compilation process. They have no functional role at runtime.
"""
from tensorflow.python.util.tf_export import tf_export
UNSPECIFIED = object()
def set_element_type(entity, dtype, shape=UNSPECIFIED):
"""Indicates that the entity is expected hold items of specified type/shape.
The staged TensorFlow ops will reflect and assert this data type. Ignored
otherwise.
Args:
entity: The entity to annotate.
dtype: TensorFlow dtype value to assert for entity.
shape: Optional shape to assert for entity.
"""
del entity
del dtype
del shape
@tf_export('autograph.experimental.set_loop_options')
def set_loop_options(
parallel_iterations=UNSPECIFIED,
swap_memory=UNSPECIFIED,
maximum_iterations=UNSPECIFIED,
shape_invariants=UNSPECIFIED):
"""Specifies additional arguments to be passed to the enclosing while_loop.
The parameters apply to and only to the immediately enclosing loop. It only
has effect if the loop is staged as a TF while_loop; otherwise the parameters
have no effect.
Usage:
>>> @tf.function(autograph=True)
... def f():
... n = 0
... for i in tf.range(10):
... tf.autograph.experimental.set_loop_options(maximum_iterations=3)
... n += 1
... return n
>>> @tf.function(autograph=True)
... def f():
... v = tf.constant((0,))
... for i in tf.range(3):
... tf.autograph.experimental.set_loop_options(
... shape_invariants=[(v, tf.TensorShape([None]))]
... )
... v = tf.concat((v, [i]), 0)
... return v
Also see tf.while_loop.
Args:
parallel_iterations: The maximum number of iterations allowed to run in
parallel at any given time. Note that this does not guarantee parallel
execution.
swap_memory: Whether to store intermediate values needed for
gradients on the CPU instead of GPU.
maximum_iterations: Allows limiting the total number of iterations executed
by the loop.
shape_invariants: Allows controlling the argument with the same name passed
to tf.while_loop. Unlike tf.while_loop, this is a list of
`(tensor, shape)` pairs.
"""
del parallel_iterations
del swap_memory
del maximum_iterations
del shape_invariants
@@ -0,0 +1,118 @@
# 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.
# ==============================================================================
"""Special functions that only make sense for AutoGraph.
These functions are meant to ensure feature parity between Python and AutoGraph,
so that the exact same code works in both modes. In general, AutoGraph will
replace these calls.
"""
from tensorflow.python.autograph.operators import data_structures
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import tensor_util
def _validate_list_constructor(elements, element_dtype, element_shape):
"""Validates the inputs of tensor_list."""
if element_dtype is not None and element_shape is not None:
return
if tensor_util.is_tf_type(elements):
return
if isinstance(elements, (list, tuple)):
if elements:
return
else:
raise ValueError(
'element_dtype and element_shape are required when elements are'
' empty')
raise ValueError(
'unknown type for elements: {}; only Tensor, list and tuple are'
' allowed'.format(type(elements)))
def match_staging_level(value, like_value):
"""Casts a value to be staged at the same level as another."""
if tensor_util.is_tf_type(like_value):
return constant_op.constant(value)
return value
def tensor_list(elements,
element_dtype=None,
element_shape=None,
use_tensor_array=False):
"""Creates an tensor list and populates it with the given elements.
This function provides a more uniform access to tensor lists and tensor
arrays, and allows optional initialization.
Note: this function is a simplified wrapper. If you need greater control,
it is recommended to use the underlying implementation directly.
Args:
elements: Iterable[tf.Tensor, ...], the elements to initially fill the list
with
element_dtype: Optional[tf.DType], data type for the elements in the list;
required if the list is empty
element_shape: Optional[tf.TensorShape], shape for the elements in the list;
required if the list is empty
use_tensor_array: bool, whether to use the more compatible but restrictive
tf.TensorArray implementation
Returns:
Union[tf.Tensor, tf.TensorArray], the new list.
Raises:
ValueError: for invalid arguments
"""
_validate_list_constructor(elements, element_dtype, element_shape)
if use_tensor_array:
return data_structures.tf_tensor_array_new(elements, element_dtype,
element_shape)
else:
return data_structures.tf_tensor_list_new(elements, element_dtype,
element_shape)
def stack(list_or_tensor, element_dtype=None, strict=True):
"""Stacks the input, if it admits the notion of stacking.
For example, a list of tensors can be stacked into a larger tensor. This
function is similar to tf.stack, but it accepts non-lists and lists of
non-tensors as arguments. In the latter case, the function does nothing.
Args:
list_or_tensor: Any
element_dtype: tf.DType, optional dtypedtype for the elements in the list.
Required if the input is stackable, and the list is untyped.
strict: bool, if True an error is raised if the input is not stackable.
Otherwise the function is a no-op.
Returns:
Any, if the input is stackable, the result will be a tf.Tensor. Otherwise,
if strict=False, the result will be list_or_tensor.
Raises:
ValueError: if strict=True and the input is not stackable.
"""
if strict:
def raise_error(x):
raise ValueError('%s must be stackable when strict=True' % x)
original_call = raise_error
else:
original_call = lambda x: x
return data_structures.list_stack(
list_or_tensor,
data_structures.ListStackOpts(
element_dtype=element_dtype, original_call=original_call))
@@ -0,0 +1,108 @@
# 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.
# ==============================================================================
"""Tests for special_functions module."""
import numpy as np
from tensorflow.python.autograph.lang import special_functions
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import list_ops
from tensorflow.python.platform import test
class SpecialFunctionsTest(test.TestCase):
def test_match_staging_level(self):
some_tensor = constant_op.constant(0)
tensor_one = special_functions.match_staging_level(1, some_tensor)
python_one = special_functions.match_staging_level(1, 1)
with self.cached_session() as sess:
self.assertTrue(tensor_util.is_tf_type(tensor_one))
self.assertAllEqual(self.evaluate(tensor_one), 1)
self.assertEqual(python_one, 1)
def test_tensor_list_empty_list(self):
l = special_functions.tensor_list([],
element_dtype=dtypes.int32,
element_shape=())
sl = list_ops.tensor_list_stack(l, element_dtype=dtypes.int32)
with self.cached_session() as sess:
self.assertAllEqual(self.evaluate(sl), [])
l = special_functions.tensor_list((),
element_dtype=dtypes.int32,
element_shape=())
sl = list_ops.tensor_list_stack(l, element_dtype=dtypes.int32)
with self.cached_session() as sess:
self.assertAllEqual(self.evaluate(sl), [])
def test_tensor_list_tensor(self):
l = special_functions.tensor_list(
constant_op.constant([], dtype=dtypes.int32))
sl = list_ops.tensor_list_stack(l, element_dtype=dtypes.int32)
with self.cached_session() as sess:
self.assertAllEqual(self.evaluate(sl), [])
def test_tensor_list_unsupported_initializer(self):
with self.assertRaisesRegex(ValueError, 'unknown type'):
special_functions.tensor_list(np.array([1, 2, 3]))
def test_tensor_list_empty_list_no_type(self):
with self.assertRaisesRegex(ValueError,
'element_dtype and element_shape are required'):
special_functions.tensor_list([])
def test_tensor_list_from_elements(self):
elements = [constant_op.constant([1, 2]), constant_op.constant([3, 4])]
l = special_functions.tensor_list(elements)
sl = list_ops.tensor_list_stack(l, element_dtype=dtypes.int32)
with self.cached_session() as sess:
self.assertAllEqual(self.evaluate(sl), [[1, 2], [3, 4]])
def test_tensor_list_array_from_elements(self):
elements = [constant_op.constant([1, 2]), constant_op.constant([3, 4])]
l = special_functions.tensor_list(elements, use_tensor_array=True)
sl = l.stack()
with self.cached_session() as sess:
self.assertAllEqual(self.evaluate(sl), [[1, 2], [3, 4]])
def test_stack(self):
self.assertEqual(special_functions.stack(1, strict=False), 1)
self.assertListEqual(
special_functions.stack([1, 2, 3], strict=False), [1, 2, 3])
# TODO(mdan): This should probably forward to tf.stack.
self.assertTrue(
isinstance(
special_functions.stack(
[constant_op.constant(1),
constant_op.constant(2)], strict=False), list))
with self.assertRaises(ValueError):
special_functions.stack([1, 2, 3])
t = constant_op.constant([1.0, 2.0])
l = list_ops.tensor_list_from_tensor(
t, element_shape=constant_op.constant([], dtype=dtypes.int32))
self.assertTrue(
tensor_util.is_tf_type(
special_functions.stack(l, element_dtype=dtypes.float32)))
if __name__ == '__main__':
test.main()