Represents a cluster as a set of "tasks", organized into "jobs". A `tf.train.ClusterSpec` represents the set of processes that participate in a distributed TensorFlow computation. Every `tf.distribute.Server` is constructed in a particular cluster. To create a cluster with two jobs and fiv
| 240 | |
| 241 | @tf_export("train.ClusterSpec") |
| 242 | class ClusterSpec(object): |
| 243 | """Represents a cluster as a set of "tasks", organized into "jobs". |
| 244 | |
| 245 | A `tf.train.ClusterSpec` represents the set of processes that |
| 246 | participate in a distributed TensorFlow computation. Every |
| 247 | `tf.distribute.Server` is constructed in a particular cluster. |
| 248 | |
| 249 | To create a cluster with two jobs and five tasks, you specify the |
| 250 | mapping from job names to lists of network addresses (typically |
| 251 | hostname-port pairs). |
| 252 | |
| 253 | ```python |
| 254 | cluster = tf.train.ClusterSpec({"worker": ["worker0.example.com:2222", |
| 255 | "worker1.example.com:2222", |
| 256 | "worker2.example.com:2222"], |
| 257 | "ps": ["ps0.example.com:2222", |
| 258 | "ps1.example.com:2222"]}) |
| 259 | ``` |
| 260 | |
| 261 | Each job may also be specified as a sparse mapping from task indices |
| 262 | to network addresses. This enables a server to be configured without |
| 263 | needing to know the identity of (for example) all other worker |
| 264 | tasks: |
| 265 | |
| 266 | ```python |
| 267 | cluster = tf.train.ClusterSpec({"worker": {1: "worker1.example.com:2222"}, |
| 268 | "ps": ["ps0.example.com:2222", |
| 269 | "ps1.example.com:2222"]}) |
| 270 | ``` |
| 271 | """ |
| 272 | |
| 273 | def __init__(self, cluster): |
| 274 | """Creates a `ClusterSpec`. |
| 275 | |
| 276 | Args: |
| 277 | cluster: A dictionary mapping one or more job names to (i) a list of |
| 278 | network addresses, or (ii) a dictionary mapping integer task indices to |
| 279 | network addresses; or a `tf.train.ClusterDef` protocol buffer. |
| 280 | |
| 281 | Raises: |
| 282 | TypeError: If `cluster` is not a dictionary mapping strings to lists |
| 283 | of strings, and not a `tf.train.ClusterDef` protobuf. |
| 284 | """ |
| 285 | if isinstance(cluster, dict): |
| 286 | self._cluster_spec = {} |
| 287 | for job_name, tasks in cluster.items(): |
| 288 | if isinstance(tasks, (list, tuple)): |
| 289 | job_tasks = {i: task for i, task in enumerate(tasks)} |
| 290 | elif isinstance(tasks, dict): |
| 291 | job_tasks = {i: task for i, task in tasks.items()} |
| 292 | else: |
| 293 | raise TypeError("The tasks for job %r must be a list or a dictionary " |
| 294 | "from integers to strings." % job_name) |
| 295 | self._cluster_spec[job_name] = job_tasks |
| 296 | self._make_cluster_def() |
| 297 | elif isinstance(cluster, cluster_pb2.ClusterDef): |
| 298 | self._cluster_def = cluster |
| 299 | self._cluster_spec = {} |
no outgoing calls