Import config if passed in during construction. If Context was created with a ConfigProto such as when calling tf.compat.v1.enable_eager_execution(), then we need to pull out the various pieces we might be replacing and import then into our internal class representation.
(self)
| 1092 | return self._physical_devices |
| 1093 | |
| 1094 | def _import_config(self): |
| 1095 | """Import config if passed in during construction. |
| 1096 | |
| 1097 | If Context was created with a ConfigProto such as when calling |
| 1098 | tf.compat.v1.enable_eager_execution(), then we need to pull out the |
| 1099 | various pieces we might be replacing and import then into our internal |
| 1100 | class representation. |
| 1101 | """ |
| 1102 | if self._config is None: |
| 1103 | return |
| 1104 | |
| 1105 | num_cpus = self._config.device_count.get("CPU", 1) |
| 1106 | if num_cpus != 1: |
| 1107 | cpus = [d for d in self._physical_devices if d.device_type == "CPU"] |
| 1108 | if num_cpus == 0: |
| 1109 | self.set_visible_devices([], "CPU") |
| 1110 | elif num_cpus > 1: |
| 1111 | self.set_virtual_device_configuration( |
| 1112 | cpus[0], [VirtualDeviceConfiguration() for _ in range(num_cpus)]) |
| 1113 | |
| 1114 | # Parse GPU options |
| 1115 | gpus = [d for d in self._physical_devices if d.device_type == "GPU"] |
| 1116 | |
| 1117 | # If there are no GPUs detected, simply ignore all the GPU options passed in |
| 1118 | # rather than doing any validation checks. |
| 1119 | if not gpus: |
| 1120 | return |
| 1121 | |
| 1122 | gpu_count = self._config.device_count.get("GPU", None) |
| 1123 | |
| 1124 | visible_gpus = [] |
| 1125 | # TODO(gjn): Handle importing existing virtual GPU configuration |
| 1126 | visible_indices = self._config.gpu_options.visible_device_list |
| 1127 | if visible_indices: |
| 1128 | for index in visible_indices.split(","): |
| 1129 | if int(index) >= len(gpus): |
| 1130 | raise ValueError("Invalid visible device index: %s" % index) |
| 1131 | visible_gpus.append(gpus[int(index)]) |
| 1132 | else: |
| 1133 | visible_gpus = gpus |
| 1134 | |
| 1135 | if gpu_count is not None: |
| 1136 | visible_gpus = visible_gpus[:gpu_count] |
| 1137 | |
| 1138 | self.set_visible_devices(visible_gpus, "GPU") |
| 1139 | |
| 1140 | def list_logical_devices(self, device_type=None): |
| 1141 | """Return logical devices.""" |
no test coverage detected