Commit Graph

63 Commits

Author SHA1 Message Date
Akshay Modi
c60b969753 Allow floats and ints to be converted to complex numbers.
PiperOrigin-RevId: 314357192
Change-Id: I29a4c63501a42a9456d144226d5a97d553d046b1
2020-06-02 10:45:38 -07:00
A. Unique TensorFlower
97cdd4d16a Fix device placement logic in ConvertToEagerTensor.
The fix consists in always creating a host tensor: the inputs to
ConvertToEagerTensor are host Python objects, so it makes sense that
the created tensor should be a host tensor too. The user can control
GPU copies by using tf.identity.

PiperOrigin-RevId: 297174740
Change-Id: I01f2aa9be3eb29fd49c7d81823e044db292b2d7c
2020-02-25 12:23:56 -08:00
MichaelKonobeev
f0df47fddf Merge branch 'master' into sparse-xent-op-hessian 2020-02-06 22:30:07 -07:00
Saurabh Saxena
1b604e5c34 Raise error early if Tensor is not iterator.
Now iter(tensor) would raise an error if tensor is not iterable. Earlier, iter(tensor) never raised an error but next(iter(tensor)) could.

PiperOrigin-RevId: 293717639
Change-Id: Ic01280ff174478b4dbd2954163f9e76d8ed00d02
2020-02-06 17:59:17 -08:00
MichaelKonobeev
fec35f30f3 Fix build errors 2020-01-04 17:52:02 -07:00
Amit Patankar
7bd345bcbb Export the Eager classes and functions from C++ to Python with pybind11 instead of swig. This is part of a larger effort to deprecate swig and eventually with modularization break pywrap_tensorflow into smaller components. It will also make exporting C++ ops to Python significantly easier. XLA is using the pybind11 macros already. Please refer to https://github.com/tensorflow/community/blob/master/rfcs/20190208-pybind11.md for more information.
PiperOrigin-RevId: 286110711
Change-Id: I7bf6f6f4ce1d6bf3e8a3e40ef4a83f82333800f6
2019-12-17 19:40:40 -08:00
Akshay Modi
9589742428 Copy resource device correctly in TFE_TensorHandleCopyToDevice
PiperOrigin-RevId: 281992454
Change-Id: I223f05688b2c873dcffa02a743fa3302bda80f16
2019-11-22 11:59:11 -08:00
Sergei Lebedev
17c4db8be1 ConvertToEagerTensor now allocates on GPU if one is available and no device is specified
This allows to get rid of redundant H->D transfers. For example

  tf.gather(tf.constant([42.0]), 0)

would previously allocate both [42.0] and 0 on CPU, and then transfer
both to GPU to compute Gather.

This could potentially hurt ops with inputs pinned to host e.g. Range.

PiperOrigin-RevId: 275252442
Change-Id: I7d85d3314b9701e7b9df76acea12c2fcfdf2960e
2019-10-17 07:22:24 -07:00
Sergei Lebedev
b48007abf0 ConvertToEagerTensor now allocates on GPU if one is available and no device is specified
This allows to get rid of redundant H->D transfers. For example

  tf.gather(tf.constant([42.0]), 0)

would previously allocate both [42.0] and 0 on CPU, and then transfer
both to GPU to compute Gather.

This could potentially hurt ops with inputs pinned to host e.g. Range.

PiperOrigin-RevId: 274806340
Change-Id: Id2797732ff3eb722302e4636b0bd6cc0ae0f3df1
2019-10-15 08:27:30 -07:00
Kibeom Kim
1ae01a07f5 Remove id= from tf.Tensor.__repr__()
PiperOrigin-RevId: 271282899
2019-09-25 23:31:25 -07:00
Xiao Yu
8b0e6baeb3 This change includes:
1. When failed to compute a eager tensor, let the EagerTensor throw proper computation exception instead of ValueError.
2. Stop using StreamingEnqueueAsync for destroy tensor handle request.

With this change, turning on streaming rpc won't break dataset iterator anymore.

PiperOrigin-RevId: 267222175
2019-09-04 14:04:47 -07:00
Sergei Lebedev
14b1fe9233 Added conversion functions for dtype/device_name arguments to EagerTensor.*
PiperOrigin-RevId: 260372439
2019-07-28 05:32:38 -07:00
Sergei Lebedev
52e6d03ade Use GetPyEagerContext instead of passing the context explicitly to EagerTensor
This change removes a possibility for inconsistency between the global
reference to EagerContext and EagerTensor.* context= argument. Specifically

* EagerTensor.__init__ now fetches the context via GetPyEagerContext;
* EagerTensor._copy_to_device uses a EagerTensor.context.

PiperOrigin-RevId: 260364374
2019-07-28 03:09:26 -07:00
Igor Ganichev
e8a1173852 Make EagerTensor reference Context
The only current use of this reference is to ensure that Python
deletes eager Context after deleting all tensors using it.

For tensors created from Python (by calling EagerTensor constructor),
this CL passes the whole Python Context object instead of just the pointer
to TFE_Context.

For tensors created from C++ (via EagerTensorFromHandle), this CL retrieves
the Context by calling the Python's context() method. I tried passing the
Context around to instead of retrieving it from Python, but it required a
fair amont of extra and mostly useless plumbing.

PiperOrigin-RevId: 259440004
2019-07-22 17:47:12 -07:00
Sergei Lebedev
eec66ce4ca tensorflow::ConvertToEagerTensor now always produces a handle of the requested dtype
Prior to this change the conversion logic was duplicated between
EagerTensor_init and ConvertToTensor in pywrap_tfe_src.cc

PiperOrigin-RevId: 258787751
2019-07-18 10:01:57 -07:00
Sergei Lebedev
09d232dcaf Pushed DT_RESOURCE check from EagerTensorBase.numpy to TFE_TensorHandleToNumpy
This allows to safely use ._numpy() in all conversion methods, e.g. __bool__.

PiperOrigin-RevId: 256948496
2019-07-08 04:24:06 -07:00
Sergei Lebedev
e71b36a97a Implemented buffer interface for EagerTensor
While this change makes EagerTensor.numpy __array__ redundant, it
does not remove __array__ due to numpy/numpy#13507.

Note also that unlike  __array__, the buffer interface does not lead
to a performance regression when np.array infers the dimensionality
of [tensor]. See  and  for details.

PiperOrigin-RevId: 249888195
2019-05-24 13:35:16 -07:00
Sergei Lebedev
85429cebb0 EagerTensor.__index__ is now consistent with np.array.__index__
Previously, EagerTensor allowed lookups with non-scalar tensors, e.g.

>>> index = tf.constant([0])
>>> [42][index]
42

If this change breaks your code, apply tf.squeeze to the index tensor.
For the above example this would look like

>>> [42][tf.squeeze(index)]
42

PiperOrigin-RevId: 249245276
2019-05-21 07:55:18 -07:00
Sergei Lebedev
7caec689ac EagerTensor_numpy now returns a view for supported dtypes
Two notes:

* Prior to this change it always produced a copy. See comment in
  EagerTensor_numpy for details.
* EagerTensor.numpy still returns a copy to ensure no change of behavior.
  This is likely to change in the followup CL.

PiperOrigin-RevId: 246378787
2019-05-02 15:08:36 -07:00
Akshay Modi
f9a682e70f Support DT_UINT64 as a direct conversion type
PiperOrigin-RevId: 246202834
2019-05-01 16:45:00 -07:00
TensorFlower Gardener
fd59c2bdb4 Merge pull request from lgeiger:np-asscalar
PiperOrigin-RevId: 242920979
2019-04-10 12:53:45 -07:00
Gaurav Jain
ee7e055ffc Replace usages of context.context().num_gpus()
A number of tests used num_gpus() which in turn initializes the context.
This seems unintended and I plan to replace num_gpus() in a follow-up
change. For now let's remove the uses in tests as much as possible.

PiperOrigin-RevId: 242531782
2019-04-08 14:04:05 -07:00
Gaurav Jain
68c05e0bf2 Make context initialization explicit
With the addition of many tf.config APIs to modify the context we should
expose an explicit function to initialize the context. This forces
client that require the context to be setup first to fail rather than
implicitly setting up the context. Doing so makes it very clear when we
are initializing the eager context.

Some observations from this change are that function calls like
num_gpus() really shouldn't cause the context to be setup.

PiperOrigin-RevId: 242525826
2019-04-08 13:34:14 -07:00
Lukas Geiger
3c4a0a9168 Replace deprecated usage of np.asscalar with np.ndarray.item()
[`numpy.asscalar()` is deprecated since version 1.16](https://github.com/numpy/numpy/blob/master/numpy/lib/type_check.py#L519-L548).

This PR replaces its usage with [`numpy.ndarray.item()`](https://www.numpy.org/devdocs/reference/generated/numpy.ndarray.item.html)
2019-03-21 12:54:28 +00:00
Akshay Modi
f3249127cb Automated rollback of commit de7d814b50
PiperOrigin-RevId: 235595623
2019-02-25 14:06:47 -08:00
Akshay Modi
de7d814b50 Support DT_UINT64 as a direct conversion type
PiperOrigin-RevId: 235273319
2019-02-22 15:59:06 -08:00
A. Unique TensorFlower
2b8badc120 fixes for py3.7 builds
PiperOrigin-RevId: 235128104
2019-02-21 21:22:55 -08:00
A. Unique TensorFlower
324c339486 Allows tf.convert_to_tensor to work with zero-dimensional numpy arrays
PiperOrigin-RevId: 232717709
2019-02-06 12:03:57 -08:00
Akshay Modi
05b2aaacf1 Automated rollback of commit 8f6c5d3252
PiperOrigin-RevId: 224364244
2018-12-06 10:41:58 -08:00
Akshay Modi
8f6c5d3252 Automated rollback of commit 3b905b0e00
PiperOrigin-RevId: 224072473
2018-12-04 17:10:05 -08:00
Akshay Modi
3b905b0e00 Respect np scalars dtype when converting to tensors.
Before this change:
Eager mode would always try to infer a dtype and convert it to int32 (since TF
prefers that), but graph would use the numpy dtype directly.
Eager would do this even if converting lists of scalars, but graph would
downcast.

After this change:
Eager and graph behave the same.
tf.convert_to_tensor(np.int64(1)).dtype == tf.int64
tf.convert_to_tensor([np.int64(1)]).dtype == tf.int32

PiperOrigin-RevId: 223823113
2018-12-03 11:20:50 -08:00
Akshay Modi
29574450f8 Delegate EagerTensor __bool__ behavior directly to numpy.
Numpy also casts non-bools to bool, so after this change EagerTensors that are not dtype bool can be converted to python bools. This also allows any shape EagerTensor with a single element to be converted to a python bool.

PiperOrigin-RevId: 223409869
2018-11-29 14:27:13 -08:00
Akshay Modi
a8ae0f47e5 Minor error message improvement for incompatible type conversion.
PiperOrigin-RevId: 222282332
2018-11-20 12:36:02 -08:00
A. Unique TensorFlower
4b543f449c This CL allows EagerTensors to support len() like numpy arrays. This should allow matplotlib plotting to work correctly with EagerTensors.
PiperOrigin-RevId: 221335277
2018-11-13 14:36:03 -08:00
Akshay Modi
d00c00c109 Don't allow floats to be converted to uint32/64
tf.constant(0, dtype=tf.uint32) < 0.5 would return False before this commit.

PiperOrigin-RevId: 220871701
2018-11-09 15:10:46 -08:00
Akshay Modi
20e7d7db87 Allow ints to be converted to all floats.
PiperOrigin-RevId: 219196844
2018-10-29 15:05:49 -07:00
Akshay Modi
5982692eae Tighten up constant_op.constant casting in eager-mode.
Previously, eager would always cast all values to the requested dtype.
This didn't match graph mode, which would only allow casting values between
'compatible' types (e.g. all integer types are compatible with each other, but
no floating type is compatible with any integer type).
Graph mode uses _AssertCompatible (dc10ac4559/tensorflow/python/framework/tensor_util.py (L345))
to determine type compatibility. Eager mode type inference is a little
different.

After this CL, the intention is that constant_op.constant behave identically in graph and eager.

Note that this doesn't check correctly for overflows (in graph or eager). This means "tf.constant(544444, dtype=tf.uint8) < 200" will both return True.

PiperOrigin-RevId: 218717988
2018-10-25 11:22:16 -07:00
Akshay Modi
4a7b57e93d Py_BuildValue might fail if the const char* cannot be interpreted as utf-8
PiperOrigin-RevId: 218198785
2018-10-22 11:17:10 -07:00
Akshay Modi
55cf8c0db7 Don't segfault if unable to convert non-rectangular inputs to tensor
PiperOrigin-RevId: 216738986
2018-10-11 12:24:02 -07:00
Akshay Modi
d644e729ca Add PyMemberDef for __dict__ on eager tensors.
This fixes dir() calls on instances of eager tensors so that it correctly
accesses the __dict__ of EagerTensorType.

Earlier it would fail due to an infinite "loop" in subtype_dict: 7e610bcdf1/Objects/typeobject.c (L2145)

get_builtin_base_with_dict will return the same type (though I'm not sure this is reasonable behavior given its name).
The __dict__ getter for the type is subtype_dict creating an infinite tail recursion.

PiperOrigin-RevId: 212020695
2018-09-07 12:51:30 -07:00
Akshay Modi
b84d27deb8 Support converting eager tensor to tf.float16 if a numpy half is passed.
This still defaults to float32 for all normal floats.

PiperOrigin-RevId: 211704918
2018-09-05 15:02:23 -07:00
Akshay Modi
fd04b76337 Give EagerTensor a fully qualified name so __module__ doesn't generate an error
This code still differs between py2 and py3 (__module__ returns
"__builtin__" in py2, and the correct value in py3) - but its strictly better
than before since earlier it would differ between py2 and py3 and generate an
error in py3. We don't seem to correctly initialize the tp_dict in py2, so even
when passing the correct, fully qualified name, we get back "__builtin__".

Fixes 

PiperOrigin-RevId: 204762170
2018-07-16 10:19:57 -07:00
Akshay Modi
9532bbbf99 When converting a numpy float64 to an EagerTensor, always ensure that it
becomes a float64 tensor.

Earlier py_seq_tensor would fall back to a float32 if not explicitly requesting
a float64 (which would not happen if we had no other information).

PiperOrigin-RevId: 197977260
2018-05-24 16:56:09 -07:00
Akshay Modi
712bbc5d7b Allow creating tensors from numpy arrays, and other various constants - try
Allow type-inference from a different input tensor, similar to args_to_matching_eager.

- Update TFE_Py_TensorShapeSlice to take tuples.
- Update int values to allow int/long in py2
END_PUBLIC

BEGIN_PUBLIC
Automated g4 rollback of changelist 192184809

PiperOrigin-RevId: 193696790
2018-04-20 11:30:25 -07:00
Akshay Modi
eafa5561cd Automated g4 rollback of changelist 192160407
PiperOrigin-RevId: 192184809
2018-04-09 14:10:00 -07:00
Akshay Modi
e4d1a92e7c Allow creating tensors from numpy arrays, and other various constants.
Allow type-inference from a different input tensor, similar to args_to_matching_eager.

- Update TFE_Py_TensorShapeSlice to take tuples.
- Update int values to allow int/long in py2

PiperOrigin-RevId: 192160407
2018-04-09 11:47:25 -07:00
Akshay Agrawal
7da6a83b74 TFE usability: Implement ndim for EagerTensor+NumPy compatibility
Third party libraries like matplotlib often access `ndim`.

PiperOrigin-RevId: 182961097
2018-01-23 11:09:34 -08:00
Shanqing Cai
83fd1cbd35 Relax text comparison for numpy formatting of arrays
* Previously, strong assumptions were made about how numpy.ndarrays
  are formatted as strings. This led to breakages due to certain
  unclear changes in numpy or its dependencies. This CL relaxes the
  assumption and fix the affected tests for tfdbg and eager.
* The tests in tensor_format_test.py are simplified through helper
  methods.

PiperOrigin-RevId: 181494182
2018-01-10 11:41:56 -08:00
Gunhan Gulsoy
7007b43d57 Disable all failing tests to fix TF opensource tests.
PiperOrigin-RevId: 181212111
2018-01-08 13:49:18 -08:00
Akshay Agrawal
3aae2248bf Add dtype keyword argument to _EagerTensorBase's __array__ method.
This change makes `__array__` conform to numpy's API.

PiperOrigin-RevId: 177614929
2017-12-01 11:16:05 -08:00