Exemple #1
0
    def __init__(self, graph=None):
        self._eager_context = _EagerContext()
        if not self.in_eager_mode():
            raise ValueError("Trying to create a Context in GRAPH_MODE")
        # Create a handle
        opts = pywrap_tensorflow.TF_NewSessionOptions(
            target=compat.as_bytes(""), config=None)
        with errors.raise_exception_on_not_ok_status() as status:
            self._handle = pywrap_tensorflow.TFE_NewContext(opts, status)
            pywrap_tensorflow.TF_DeleteSessionOptions(opts)
        # Store list of devices
        self._devices = []
        with errors.raise_exception_on_not_ok_status() as status:
            device_list = pywrap_tensorflow.TFE_ContextListDevices(
                self._handle, status)
        try:
            for i in range(pywrap_tensorflow.TF_DeviceListCount(device_list)):
                with errors.raise_exception_on_not_ok_status() as status:
                    dev_name = pywrap_tensorflow.TF_DeviceListName(
                        device_list, i, status)
                self._devices.append(pydev.canonical_name(dev_name))
        finally:
            pywrap_tensorflow.TF_DeleteDeviceList(device_list)

        self._summary_writer_resource = None
        self._graph = graph or tf_ops.get_default_graph()
Exemple #2
0
 def _initialize_handle_and_devices(self):
     """Initialize handle and devices."""
     with self._initialize_lock:
         if self._context_handle is not None:
             return
         assert self._context_devices is None
         opts = pywrap_tensorflow.TF_NewSessionOptions(
             target=compat.as_bytes(""), config=self._config)
         with errors.raise_exception_on_not_ok_status() as status:
             self._context_handle = pywrap_tensorflow.TFE_NewContext(
                 opts, status)
             pywrap_tensorflow.TF_DeleteSessionOptions(opts)
         # Store list of devices
         self._context_devices = []
         with errors.raise_exception_on_not_ok_status() as status:
             device_list = pywrap_tensorflow.TFE_ContextListDevices(
                 self._context_handle, status)
         try:
             for i in range(
                     pywrap_tensorflow.TF_DeviceListCount(device_list)):
                 with errors.raise_exception_on_not_ok_status() as status:
                     dev_name = pywrap_tensorflow.TF_DeviceListName(
                         device_list, i, status)
                 self._context_devices.append(
                     pydev.canonical_name(dev_name))
         finally:
             pywrap_tensorflow.TF_DeleteDeviceList(device_list)
Exemple #3
0
    def __init__(self, target='', graph=None, config=None):
        """Constructs a new TensorFlow session.

    Args:
      target: (Optional) The TensorFlow execution engine to connect to.
      graph: (Optional) The graph to be used. If this argument is None,
        the default graph will be used.
      config: (Optional) ConfigProto proto used to configure the session.

    Raises:
      tf.errors.OpError: Or one of its subclasses if an error occurs while
        creating the TensorFlow session.
      TypeError: If one of the arguments has the wrong type.
    """
        if graph is None:
            self._graph = ops.get_default_graph()
        else:
            if not isinstance(graph, ops.Graph):
                raise TypeError('graph must be a tf.Graph, but got %s' %
                                type(graph))
            self._graph = graph

        self._opened = False
        self._closed = False

        self._current_version = 0
        self._extend_lock = threading.Lock()
        if target is not None:
            try:
                self._target = compat.as_bytes(target)
            except TypeError:
                raise TypeError('target must be a string, but got %s' %
                                type(target))
        else:
            self._target = None

        self._delete_lock = threading.Lock()
        self._dead_handles = []

        if config is not None:
            if not isinstance(config, config_pb2.ConfigProto):
                raise TypeError('config must be a tf.ConfigProto, but got %s' %
                                type(config))
            self._config = config
            self._add_shapes = config.graph_options.infer_shapes
        else:
            self._config = None
            self._add_shapes = False

        self._session = None
        opts = tf_session.TF_NewSessionOptions(target=self._target,
                                               config=config)
        try:
            with errors.raise_exception_on_not_ok_status() as status:
                self._session = tf_session.TF_NewSession(opts, status)
        finally:
            tf_session.TF_DeleteSessionOptions(opts)
Exemple #4
0
 def testInvalidDeviceNumber(self):
   opts = tf_session.TF_NewSessionOptions()
   c_session = tf_session.TF_NewSession(ops.get_default_graph()._c_graph, opts)
   raw_device_list = tf_session.TF_SessionListDevices(c_session)
   size = tf_session.TF_DeviceListCount(raw_device_list)
   with self.assertRaises(errors.InvalidArgumentError):
     tf_session.TF_DeviceListMemoryBytes(raw_device_list, size)
   tf_session.TF_DeleteDeviceList(raw_device_list)
   tf_session.TF_CloseSession(c_session)
Exemple #5
0
 def testInvalidDeviceNumber(self):
     opts = tf_session.TF_NewSessionOptions()
     with errors.raise_exception_on_not_ok_status() as status:
         c_session = tf_session.TF_NewSession(
             ops.get_default_graph()._c_graph, opts, status)
         raw_device_list = tf_session.TF_SessionListDevices(
             c_session, status)
     size = tf_session.TF_DeviceListCount(raw_device_list)
     # Test that invalid device numbers return -1 rather than a Swig-wrapped
     # pointer.
     status_no_exception = c_api_util.ScopedTFStatus()
     memory = tf_session.TF_DeviceListMemoryBytes(raw_device_list, size,
                                                  status_no_exception)
     self.assertEqual(memory, -1)
     tf_session.TF_DeleteDeviceList(raw_device_list)
     with errors.raise_exception_on_not_ok_status() as status:
         tf_session.TF_CloseSession(c_session, status)
Exemple #6
0
    def __init__(self, target='', graph=None, config=None):
        """Constructs a new TensorFlow session.

    Args:
      target: (Optional) The TensorFlow execution engine to connect to.
      graph: (Optional) The graph to be used. If this argument is None,
        the default graph will be used.
      config: (Optional) ConfigProto proto used to configure the session.

    Raises:
      RuntimeError: If an error occurs while creating the TensorFlow
        session.
    """
        if graph is None:
            self._graph = ops.get_default_graph()
        else:
            self._graph = graph

        self._opened = False
        self._closed = False

        self._current_version = 0
        self._extend_lock = threading.Lock()
        self._target = target

        self._delete_lock = threading.Lock()
        self._dead_handles = []

        self._session = None

        opts = tf_session.TF_NewSessionOptions(target=target, config=config)
        try:
            status = tf_session.TF_NewStatus()
            try:
                self._session = tf_session.TF_NewSession(opts, status)
                if tf_session.TF_GetCode(status) != 0:
                    raise RuntimeError(
                        compat.as_text(tf_session.TF_Message(status)))
            finally:
                tf_session.TF_DeleteStatus(status)
        finally:
            tf_session.TF_DeleteSessionOptions(opts)
Exemple #7
0
    def __init__(self, target='', graph=None, config=None):
        """Constructs a new TensorFlow session.

    Args:
      target: (Optional) The TensorFlow execution engine to connect to.
      graph: (Optional) The graph to be used. If this argument is None,
        the default graph will be used.
      config: (Optional) ConfigProto proto used to configure the session.

    Raises:
      tf.errors.OpError: Or one of its subclasses if an error occurs while
        creating the TensorFlow session.
    """
        if graph is None:
            self._graph = ops.get_default_graph()
        else:
            self._graph = graph

        self._opened = False
        self._closed = False

        self._current_version = 0
        self._extend_lock = threading.Lock()
        self._target = target

        self._delete_lock = threading.Lock()
        self._dead_handles = []

        self._session = None
        self._config = config
        self._add_shapes = config.graph_options.infer_shapes if (
            config and config.graph_options) else False

        try:
            opts = tf_session.TF_NewSessionOptions(target=target,
                                                   config=config)
            with errors.raise_exception_on_not_ok_status() as status:
                self._session = tf_session.TF_NewSession(opts, status)
        finally:
            tf_session.TF_DeleteSessionOptions(opts)
Exemple #8
0
    def __init__(self):
        self._eager_context = _EagerContext()
        # Create a handle
        opts = pywrap_tensorflow.TF_NewSessionOptions(
            target=compat.as_bytes(""), config=None)
        with errors.raise_exception_on_not_ok_status() as status:
            self._handle = pywrap_tensorflow.TFE_NewContext(opts, status)
            pywrap_tensorflow.TF_DeleteSessionOptions(opts)
        # Store list of devices
        self._devices = []
        with errors.raise_exception_on_not_ok_status() as status:
            device_list = pywrap_tensorflow.TFE_ContextListDevices(
                self._handle, status)
        try:
            for i in range(pywrap_tensorflow.TF_DeviceListCount(device_list)):
                with errors.raise_exception_on_not_ok_status() as status:
                    dev_name = pywrap_tensorflow.TF_DeviceListName(
                        device_list, i, status)
                self._devices.append(pydev.canonical_name(dev_name))
        finally:
            pywrap_tensorflow.TF_DeleteDeviceList(device_list)

        self._summary_writer_resource = None