Create a base types module and populate it with the Tensor base class, along with its Graph and Eager subclasses.

PiperOrigin-RevId: 302925149
Change-Id: I1e652d938f2a346d37759de5be9dca5623a7500b
This commit is contained in:
Dan Moldovan 2020-03-25 10:56:10 -07:00 committed by TensorFlower Gardener
parent 35a382295a
commit 9273e67c46
5 changed files with 124 additions and 5 deletions

View File

@ -1864,6 +1864,7 @@ py_library(
":tensor_shape",
":util",
"//tensorflow/core:protos_all_py",
"//tensorflow/python/types",
],
)

View File

@ -27,6 +27,7 @@ from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_like
from tensorflow.python.framework import tensor_shape
from tensorflow.python.types import core
from tensorflow.python.util import compat
from tensorflow.python.util import nest
from tensorflow.python.util.tf_export import tf_export
@ -995,7 +996,8 @@ def is_tensor(x): # pylint: disable=invalid-name
Returns:
`True` if `x` is a tensor or "tensor-like", `False` if not.
"""
return (isinstance(x, tensor_like._TensorLike) or # pylint: disable=protected-access
# TODO(mdan): Remove these. Only keep core.Tensor.
return (isinstance(x, (tensor_like._TensorLike, core.Tensor)) or # pylint: disable=protected-access
ops.is_dense_tensor_like(x) or
getattr(x, "is_tensor_like", False))

View File

@ -0,0 +1,31 @@
load("//tensorflow:tensorflow.bzl", "py_strict_library")
package(
licenses = ["notice"], # Apache 2.0
)
filegroup(
name = "all_files",
srcs = glob(
["**/*"],
exclude = [
"**/METADATA",
"**/OWNERS",
],
),
visibility = ["//tensorflow:__subpackages__"],
)
# Important: this is a leaf library. It may not have any new dependencies inside TF proper.
# The sole exception is tf_export, to allow exporting symbols into the public namespace.
py_strict_library(
name = "types",
srcs = [
"__init__.py",
"core.py",
],
srcs_version = "PY2AND3",
visibility = ["//tensorflow:__subpackages__"],
deps = [
],
)

View File

@ -0,0 +1,25 @@
# 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.
# ==============================================================================
"""Public TensorFlow type definitions.
For details, see
https://github.com/tensorflow/community/blob/master/rfcs/20200211-tf-types.md.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Note: this module should contain **type definitions only**.

View File

@ -0,0 +1,60 @@
# 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.
# ==============================================================================
"""Core TensorFlow types."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# TODO(mdan): Consider adding ABC once the dependence on isinstance is reduced.
# TODO(mdan): Add type annotations.
class Tensor(object):
"""The base class of all dense Tensor objects.
A dense tensor has a static data type (dtype), and may have a static rank and
shape. Tensor objects are immutable. Mutable objects may be backed by a Tensor
which holds the unique handle that identifies the mutable object.
"""
@property
def dtype(self):
pass
@property
def shape(self):
pass
class Symbol(Tensor):
"""Symbolic "graph" Tensor.
These objects represent the output of an op definition and do not carry a
value.
"""
pass
class Value(Tensor):
"""Tensor that can be associated with a value (aka "eager tensor").
These objects represent the (usually future) output of executing an op
immediately.
"""
def numpy(self):
pass