예제 #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)
            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()
예제 #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)
            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()
예제 #3
0
파일: context.py 프로젝트: bdnf/Tensorflow
    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)