def convert_to_eager_tensor(value, ctx, dtype=None):
    """Converts the given `value` to an `EagerTensor`.

  Note that this function could return cached copies of created constants for
  performance reasons.

  Args:
    value: value to convert to EagerTensor.
    ctx: value of context.context().
    dtype: optional desired dtype of the converted EagerTensor.

  Returns:
    EagerTensor created from value.

  Raises:
    TypeError: if `dtype` is not compatible with the type of t.
  """
    if isinstance(value, ops.EagerTensor):
        if dtype is not None and value.dtype != dtype:
            raise TypeError("Expected tensor with type %r not %r" %
                            (dtype, value.dtype))
        return value
    if dtype is not None:
        try:
            dtype = dtype.as_datatype_enum
        except AttributeError:
            dtype = dtypes.as_dtype(dtype).as_datatype_enum
    device = ctx.device_name
    handle = ctx._handle  # pylint: disable=protected-access
    if isinstance(value, (float, ) + six.integer_types):
        # Use a scalar cache. This will put each scalar of each type only once on
        # each device. Scalars don't use much device memory but copying scalars can
        # trigger memcpys which are slow.
        cache_key = device, value, dtype, type(value)
        scalar_cache = ctx.scalar_cache()
        tensor = scalar_cache.get(cache_key, None)
        if tensor is not None:
            return tensor
        t = ops.EagerTensor(value, context=handle, device=device, dtype=dtype)
        scalar_cache[cache_key] = t
        return t
    else:
        return ops.EagerTensor(value,
                               context=handle,
                               device=device,
                               dtype=dtype)
def benchmark_create_tensor(n):
    """Benchmark overheads of creating a Tensor object."""
    def label(s):
        return "{:20s}".format(s)

    with timer(label("np.array([[3]])"), iters=n) as iters:
        for _ in iters:
            np.array([[3]])

    with timer(label("Tensor([[3]])"), iters=n) as iters:
        for _ in iters:
            ops.EagerTensor([[3]], context.context())

    ctx = context.context()
    with timer(label("Tensor([[3]], ctx)"), iters=n) as iters:
        for _ in iters:
            ops.EagerTensor([[3]], ctx)
Exemple #3
0
 def testStringTensorOnGPU(self):
     if not context.context().num_gpus():
         self.skipTest("No GPUs found")
     with ops.device("/device:GPU:0"):
         with self.assertRaisesRegexp(
                 errors.InvalidArgumentError,
                 "Can't copy Tensor with type string to device"):
             ops.EagerTensor("test string")
Exemple #4
0
def convert_to_mixed_eager_tensors(values, ctx):
    v = [
        t if isinstance(t, ops.EagerTensor) else ops.EagerTensor(
            t, context=ctx._handle, device=ctx.device_name)  # pylint: disable=protected-access
        for t in values
    ]
    types = [t._datatype_enum() for t in v]  # pylint: disable=protected-access
    return types, v
 def testNumpyValueWithCast(self):
     values = np.array([3.0], dtype=np.float32)
     t = _create_tensor(values, dtype=dtypes.float64)
     self.assertAllEqual(values, t)
     ctx = context.context()
     # Bad dtype value.
     with self.assertRaisesRegex(TypeError, "Invalid dtype argument value"):
         ops.EagerTensor(values, device=ctx.device_name, dtype=12345)
Exemple #6
0
 def testMultiLineTensorRepr(self):
     t = ops.EagerTensor(np.eye(3))
     tensor_repr = repr(t)
     self.assertTrue(tensor_repr.startswith("<"))
     self.assertTrue(tensor_repr.endswith(">"))
     self.assertIn(
         "id=%d, shape=%s, dtype=%s, numpy=\n%r" %
         (t._id, t.shape, t.dtype.name, t.numpy()), tensor_repr)
Exemple #7
0
  def _benchmark_create_tensor(self, value, dtype, device):
    """Benchmark overheads of creating a Tensor object."""
    if device == GPU:
      # Warmup the GPU
      ops.EagerTensor(value, device=device)

    def func():
      ops.EagerTensor(value, device=device, dtype=dtype)

    self._run(func, 30000)
Exemple #8
0
  def testBadConstructorArgs(self):
    context.ensure_initialized()
    ctx = context.context()
    device = ctx.device_name
    # Missing device.
    with self.assertRaisesRegexp(TypeError, r".*argument 'device' \(pos 2\).*"):
      ops.EagerTensor(1)
    # Bad dtype type.
    with self.assertRaisesRegexp(TypeError,
                                 "Expecting a DataType value for dtype. Got"):
      ops.EagerTensor(1, device=device, dtype="1")

    # Following errors happen when trying to copy to GPU.
    if not test_util.is_gpu_available():
      self.skipTest("No GPUs found")

    with ops.device("/device:GPU:0"):
      # Bad device.
      with self.assertRaisesRegexp(TypeError, "Error parsing device argument"):
        ops.EagerTensor(1.0, device=1)
Exemple #9
0
  def _benchmark_create_tensor(self, value, dtype, device):
    """Benchmark overheads of creating a Tensor object."""
    ctx = context.context()
    handle = ctx._handle
    if device == GPU:
      # Warmup the GPU
      ops.EagerTensor(value, context=handle, device=device)

    def func():
      ops.EagerTensor(value, context=handle, device=device, dtype=dtype)
    self._run(func, 30000)
def _create_tensor(value, device=None, dtype=None):
    context.ensure_initialized()
    ctx = context.context()
    if device is None:
        device = ctx.device_name
    if dtype is not None:
        dtype = dtype.as_datatype_enum
    try:
        return ops.EagerTensor(value, device=device, dtype=dtype)
    except core._NotOkStatusException as e:  # pylint: disable=protected-access
        raise core._status_to_exception(e.code, e.message)
Exemple #11
0
    def testTensorStrReprObeyNumpyPrintOptions(self):
        orig_threshold = np.get_printoptions()["threshold"]
        orig_edgeitems = np.get_printoptions()["edgeitems"]
        np.set_printoptions(threshold=2, edgeitems=1)

        t = ops.EagerTensor(np.arange(10, dtype=np.int32))
        self.assertIn("[0 ..., 9]", str(t))
        self.assertIn("[0, ..., 9]", repr(t))

        # Clean up: reset to previous printoptions.
        np.set_printoptions(threshold=orig_threshold, edgeitems=orig_edgeitems)
Exemple #12
0
def benchmark_create_tensor(n):
  """Benchmark overheads of creating a Tensor object."""

  def label(s):
    return "{:20s}".format(s)

  with timer(label("np.array([[3.0]])"), iters=n) as iters:
    for _ in iters:
      np.array([[3.0]])

  ctx = context.context()
  handle = ctx._handle
  device = ctx.device_name
  # May be warmup GPU.
  ops.EagerTensor([[3.0]], context=handle, device=device)

  # float32
  dtype = dtypes.float32.as_datatype_enum
  three = [[3.0]]
  with timer(label("EagerTensor([[3.0]])"), iters=n) as iters:
    for _ in iters:
      ops.EagerTensor(three, context=handle, device=device, dtype=dtype)

  np_3 = np.array([[3.0]], dtype=np.float32)
  with timer(label("EagerTensor(np.array([[3.0]]))"), iters=n) as iters:
    for _ in iters:
      ops.EagerTensor(np_3, context=handle, device=device, dtype=dtype)

  # int32.
  # This is interesting since int32 will be kept on host memory for the GPU
  # case.
  dtype = dtypes.int32.as_datatype_enum
  three = [[3]]
  with timer(label("EagerTensor([[3]])"), iters=n) as iters:
    for _ in iters:
      ops.EagerTensor(three, context=handle, device=device, dtype=dtype)

  np_3 = np.array([[3]], dtype=np.int32)
  with timer(label("EagerTensor(np.array([[3]]))"), iters=n) as iters:
    for _ in iters:
      ops.EagerTensor(np_3, context=handle, device=device, dtype=dtype)
def convert_to_eager_tensor(t, ctx, dtype=None):
    """Converts the given `value` to an `EagerTensor`."""
    if isinstance(t, ops.EagerTensor):
        if dtype is not None and t.dtype != dtype:
            raise TypeError("Expected tensor with type %r not %r" %
                            (dtype, t.dtype))
        return t
    if isinstance(t, (int, float)):
        # Use a scalar cache. This will put each scalar of each type only once on
        # each device. Scalars don't use much device memory but copying scalars can
        # trigger memcpys which are slow.
        device = ctx.device_name
        cache_key = device, t, dtype, type(t)
        scalar_cache = ctx.scalar_cache()
        tensor = scalar_cache.get(cache_key, None)
        if tensor is not None:
            return tensor
        value = ops.EagerTensor(t, ctx, dtype=dtype)
        scalar_cache[cache_key] = value
        return value
    return ops.EagerTensor(t, ctx, dtype=dtype)
Exemple #14
0
    def testConvertFromArrayInterface(self):
        context.ensure_initialized()
        ctx = context.context()

        class MyArrayClass(object):
            def __init__(self):
                self.array = np.random.random(16)

            def __array__(self):
                return self.array

        a = MyArrayClass()
        t = ops.EagerTensor(a, device=ctx.device_name, dtype=None)
        self.assertAllEqual(t, a)
Exemple #15
0
    def testNestedSequenceInputs(self):
        sd = graph_callable.ShapeAndDtype(shape=(), dtype=dtypes.float32)

        @graph_callable.graph_callable([[sd, tuple([sd, sd]), sd]])
        def my_op(inputs):
            a, b, c = inputs
            e, f = b
            v = variable_scope.get_variable(
                "my_v", initializer=init_ops.zeros_initializer(), shape=())
            return [a + a + v, tuple([e + e, f + f]), c + c], a + e + f + c + v

        inputs = [
            ops.EagerTensor(1.), [ops.EagerTensor(2.),
                                  ops.EagerTensor(3.)],
            ops.EagerTensor(4.)
        ]
        ret = my_op(inputs)
        self.assertEqual(len(ret), 2.)
        self.assertEqual(ret[1].numpy(), 10.)

        my_op.variables[0].assign(1.)
        ret = my_op(inputs)
        self.assertEqual(ret[1].numpy(), 11.)
Exemple #16
0
def convert_to_eager_tensor(t, dtype=None):
  """Converts the given `value` to an `EagerTensor`."""
  if isinstance(ag_core.getval(t), ops.EagerTensor):
    if dtype is not None and t.dtype != dtype:
      raise TypeError("Expected tensor with type %r not %r" % (dtype, t.dtype))
    return t
  # Handle converting ResourceVariable to Tensor.
  # TODO(josh11b): get rid of this explicit ugly conversion once we have a more
  # general scheme in place.
  try:
    return t._dense_var_to_tensor(dtype=dtype, as_ref=False)  # pylint: disable=protected-access
  except AttributeError:
    pass
  return ops.EagerTensor(t, dtype=dtype)
Exemple #17
0
    def testNestedFunction(self):

        # TensorFlow function (which is what would be used in TensorFlow graph
        # construction).
        @function.Defun(dtypes.int32, dtypes.int32)
        def add(a, b):
            return math_ops.add(a, b)

        # A graph_callable that will invoke the TensorFlow function.
        @graph_callable.graph_callable(
            [graph_callable.ShapeAndDtype(shape=(), dtype=dtypes.int32)])
        def add_one(x):
            return add(x, 1)

        self.assertAllEqual(3, add_one(ops.EagerTensor(2)).numpy())
Exemple #18
0
    def DISABLED_testRepeatedUseOfSubFunction(self):
        @function.Defun(dtypes.int32, dtypes.int32)
        def add(a, b):
            return math_ops.add(a, b)

        @graph_callable.graph_callable(
            [graph_callable.ShapeAndDtype(shape=(), dtype=dtypes.int32)])
        def add_one(x):
            return add(x, 1)

        @graph_callable.graph_callable(
            [graph_callable.ShapeAndDtype(shape=(), dtype=dtypes.int32)])
        def add_two(x):
            return add(x, 2)

        two = ops.EagerTensor(2)
        self.assertAllEqual(3, add_one(two).numpy())
        self.assertAllEqual(4, add_two(two).numpy())
Exemple #19
0
def convert_to_mixed_eager_tensors(values, ctx):
  v = [t if isinstance(t, ops.EagerTensor) else ops.EagerTensor(t, ctx)
       for t in values]
  types = [t.dtype for t in v]
  return types, v
Exemple #20
0
 def testZeroSizeTensorStr(self):
     t = ops.EagerTensor(np.zeros(0, dtype=np.float32))
     self.assertIn("[], shape=(0,), dtype=float32", str(t))
Exemple #21
0
 def func():
     ops.EagerTensor(value, context=handle, device=device, dtype=dtype)
Exemple #22
0
 def testTensorCreationFailure(self):
     with self.assertRaises(Exception):
         # Should fail because the each row of the Python object has a different
         # number of columns.
         self.assertEqual(None, ops.EagerTensor([[1], [1, 2]]))
Exemple #23
0
 def testStringTensor(self):
     t_np_orig = np.array([[b"a", b"ab"], [b"abc", b"abcd"]])
     t = ops.EagerTensor(t_np_orig)
     t_np = t.numpy()
     self.assertTrue(np.all(t_np == t_np_orig),
                     "%s vs %s" % (t_np, t_np_orig))
Exemple #24
0
 def testScalarTensor(self):
     t = ops.EagerTensor(3)
     self.assertEqual(t.numpy(), ops.EagerTensor(np.array(3)).numpy())
     self.assertEqual(dtypes.int32, t.dtype)
     self.assertEqual(0, t.shape.ndims)
     self.assertAllEqual([], t.shape.as_list())
Exemple #25
0
 def testFloatDowncast(self):
     # Unless explicitly specified, float64->float32
     t = ops.EagerTensor(3.0)
     self.assertEqual(dtypes.float32, t.dtype)
     t = ops.EagerTensor(3.0, dtype=dtypes.float64)
     self.assertEqual(dtypes.float64, t.dtype)
Exemple #26
0
 def testNumpyOrderHandling(self):
     n = np.array([[1, 2], [3, 4]], order="F")
     t = ops.EagerTensor(n)
     self.assertAllEqual([[1, 2], [3, 4]], t.numpy())
Exemple #27
0
 def func():
     ops.EagerTensor(value, device=device, dtype=dtype)
Exemple #28
0
 def testNumpyUnprintableTensor(self):
     t = ops.EagerTensor(42)
     # Force change dtype to a numpy-unprintable type.
     t._dtype = dtypes.resource
     self.assertIn("<unprintable>", str(t))
     self.assertIn("<unprintable>", repr(t))
Exemple #29
0
 def testMultiLineTensorStr(self):
     t = ops.EagerTensor(np.eye(3))
     tensor_str = str(t)
     self.assertIn("shape=%s, dtype=%s" % (t.shape, t.dtype.name),
                   tensor_str)
     self.assertIn(str(t.numpy()), tensor_str)
Exemple #30
0
 def testBool(self):
     t = ops.EagerTensor(False)
     if t:
         self.assertFalse(True)