Expose types.core.Tensor as tf.__internal__ API.
It is used in keras for type checking and super class (for autocast variable).

Also rewrite the keras logic for the sparse/dense tensor checking.

PiperOrigin-RevId: 355020378
Change-Id: I47df3f374bb7f83e114659f76887f687c46ecb51
diff --git a/tensorflow/python/keras/engine/training_v1.py b/tensorflow/python/keras/engine/training_v1.py
index 491b436..829021e 100644
--- a/tensorflow/python/keras/engine/training_v1.py
+++ b/tensorflow/python/keras/engine/training_v1.py
@@ -67,7 +67,6 @@
 from tensorflow.python.ops import math_ops
 from tensorflow.python.platform import tf_logging as logging
 from tensorflow.python.training.tracking import base as trackable
-from tensorflow.python.types import core
 from tensorflow.python.util import nest
 
 try:
@@ -3161,20 +3160,20 @@
     The possibly-converted 'value'.
   """
   if issparse is not None and issparse(value):
-    if isinstance(expected_input, core.Tensor):
-      if ops.executing_eagerly_outside_functions():
-        # In TF2 we do not silently densify sparse matrices.
-        raise ValueError('A SciPy sparse matrix was passed to a model '
-                         'that expects dense inputs. Please densify your '
-                         'inputs first, such as by calling `x.toarray().')
-      return value.toarray()
-    else:
+    if K.is_sparse(expected_input):
       sparse_coo = value.tocoo()
       row, col = sparse_coo.row, sparse_coo.col
       data, shape = sparse_coo.data, sparse_coo.shape
       indices = np.concatenate((np.expand_dims(row, 1), np.expand_dims(col, 1)),
                                1)
       return sparse_tensor.SparseTensor(indices, data, shape)
+    else:
+      if ops.executing_eagerly_outside_functions():
+        # In TF2 we do not silently densify sparse matrices.
+        raise ValueError('A SciPy sparse matrix was passed to a model '
+                         'that expects dense inputs. Please densify your '
+                         'inputs first, such as by calling `x.toarray().')
+      return value.toarray()
   else:
     return value
 
diff --git a/tensorflow/python/tools/api/generator/api_init_files.bzl b/tensorflow/python/tools/api/generator/api_init_files.bzl
index 20d5564..a19de8f 100644
--- a/tensorflow/python/tools/api/generator/api_init_files.bzl
+++ b/tensorflow/python/tools/api/generator/api_init_files.bzl
@@ -17,6 +17,7 @@
     "__internal__/test/__init__.py",
     "__internal__/test/combinations/__init__.py",
     "__internal__/tf2/__init__.py",
+    "__internal__/types/__init__.py",
     "__internal__/saved_model/__init__.py",
     "__internal__/tracking/__init__.py",
     "__operators__/__init__.py",
diff --git a/tensorflow/python/types/core.py b/tensorflow/python/types/core.py
index b450659..c1f1199 100644
--- a/tensorflow/python/types/core.py
+++ b/tensorflow/python/types/core.py
@@ -37,6 +37,8 @@
 # TODO(mdan): Add type annotations.
 
 
+# TODO(b/178822082): Revisit this API when tf.types gets more resource.
+@tf_export("__internal__.types.Tensor", v1=[])
 class Tensor(object):
   """The base class of all dense Tensor objects.
 
diff --git a/tensorflow/tools/api/golden/v2/tensorflow.__internal__.pbtxt b/tensorflow/tools/api/golden/v2/tensorflow.__internal__.pbtxt
index 500ad2a..6a6c667 100644
--- a/tensorflow/tools/api/golden/v2/tensorflow.__internal__.pbtxt
+++ b/tensorflow/tools/api/golden/v2/tensorflow.__internal__.pbtxt
@@ -56,6 +56,10 @@
     name: "tracking"
     mtype: "<type \'module\'>"
   }
+  member {
+    name: "types"
+    mtype: "<type \'module\'>"
+  }
   member_method {
     name: "create_c_op"
     argspec: "args=[\'graph\', \'node_def\', \'inputs\', \'control_inputs\', \'op_def\'], varargs=None, keywords=None, defaults=[\'None\'], "
diff --git a/tensorflow/tools/api/golden/v2/tensorflow.__internal__.types.-tensor.pbtxt b/tensorflow/tools/api/golden/v2/tensorflow.__internal__.types.-tensor.pbtxt
new file mode 100644
index 0000000..eae74c6
--- /dev/null
+++ b/tensorflow/tools/api/golden/v2/tensorflow.__internal__.types.-tensor.pbtxt
@@ -0,0 +1,16 @@
+path: "tensorflow.__internal__.types.Tensor"
+tf_class {
+  is_instance: "<class \'tensorflow.python.types.core.Tensor\'>"
+  is_instance: "<type \'object\'>"
+  member {
+    name: "dtype"
+    mtype: "<type \'property\'>"
+  }
+  member {
+    name: "shape"
+    mtype: "<type \'property\'>"
+  }
+  member_method {
+    name: "__init__"
+  }
+}
diff --git a/tensorflow/tools/api/golden/v2/tensorflow.__internal__.types.pbtxt b/tensorflow/tools/api/golden/v2/tensorflow.__internal__.types.pbtxt
new file mode 100644
index 0000000..783cfb2
--- /dev/null
+++ b/tensorflow/tools/api/golden/v2/tensorflow.__internal__.types.pbtxt
@@ -0,0 +1,7 @@
+path: "tensorflow.__internal__.types"
+tf_module {
+  member {
+    name: "Tensor"
+    mtype: "<type \'type\'>"
+  }
+}