Exemple #1
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.TFE_NewContextOptions()
            try:
                if self._config is not None:
                    config_str = self._config.SerializeToString()
                    pywrap_tensorflow.TFE_ContextOptionsSetConfig(
                        opts, config_str)
                if self._device_policy is not None:
                    pywrap_tensorflow.TFE_ContextOptionsSetDevicePlacementPolicy(
                        opts, self._device_policy)
                if self._execution_mode == ASYNC:
                    pywrap_tensorflow.TFE_ContextOptionsSetAsync(opts, True)
                self._context_handle = pywrap_tensorflow.TFE_NewContext(opts)
            finally:
                pywrap_tensorflow.TFE_DeleteContextOptions(opts)
            assert not (
                self._server_def and self._collective_ops_server_def
            ), ("Cannot enable remote execution as well as collective ops at the "
                "moment. If this is important to you, please file an issue.")
            if self._server_def is not None:
                server_def_str = self._server_def.SerializeToString()
                pywrap_tensorflow.TFE_ContextSetServerDef(
                    self._context_handle, 600, server_def_str)
            elif self._collective_ops_server_def is not None:
                server_def_str = self._collective_ops_server_def.SerializeToString(
                )
                pywrap_tensorflow.TFE_EnableCollectiveOps(
                    self._context_handle, server_def_str)

            self._initialize_devices()
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.TFE_NewContextOptions()
            try:
                if self._config is not None:
                    config_str = self._config.SerializeToString()
                    pywrap_tensorflow.TFE_ContextOptionsSetConfig(
                        opts, config_str)
                if self._device_policy is not None:
                    pywrap_tensorflow.TFE_ContextOptionsSetDevicePlacementPolicy(
                        opts, self._device_policy)
                if self._execution_mode == ASYNC:
                    pywrap_tensorflow.TFE_ContextOptionsSetAsync(opts, True)
                self._context_handle = pywrap_tensorflow.TFE_NewContext(opts)
            finally:
                pywrap_tensorflow.TFE_DeleteContextOptions(opts)
            if self._server_def is not None:
                server_def_str = self._server_def.SerializeToString()
                pywrap_tensorflow.TFE_ContextSetServerDef(
                    self._context_handle, 600, server_def_str)

            self._initialize_devices()
Exemple #3
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 #4
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 #5
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.TFE_NewContextOptions()
            try:
                with errors.raise_exception_on_not_ok_status() as status:
                    if self._config is not None:
                        config_str = self._config.SerializeToString()
                        pywrap_tensorflow.TFE_ContextOptionsSetConfig(
                            opts, config_str, len(config_str), status)
                    if self._device_policy is not None:
                        pywrap_tensorflow.TFE_ContextOptionsSetDevicePlacementPolicy(
                            opts, self._device_policy)
                    if self._execution_mode == ASYNC:
                        pywrap_tensorflow.TFE_ContextOptionsSetAsync(True)
                    self._context_handle = pywrap_tensorflow.TFE_NewContext(
                        opts, status)
            finally:
                pywrap_tensorflow.TFE_DeleteContextOptions(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:
                self._num_gpus = 0
                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))
                    with errors.raise_exception_on_not_ok_status() as status:
                        dev_type = pywrap_tensorflow.TF_DeviceListType(
                            device_list, i, status)
                    if dev_type == "GPU":
                        self._num_gpus += 1

            finally:
                pywrap_tensorflow.TF_DeleteDeviceList(device_list)
Exemple #6
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