Canonicalize device string. If d has missing components, the rest would be deduced from the `default` argument or from '/replica:0/task:0/device:CPU:0'. For example: If d = '/cpu:0', default='/job:worker/task:1', it returns '/job:worker/replica:0/task:1/device:CPU:0'. If d = '/cpu
(d, default=None)
| 24 | |
| 25 | |
| 26 | def canonicalize(d, default=None): |
| 27 | """Canonicalize device string. |
| 28 | |
| 29 | If d has missing components, the rest would be deduced from the `default` |
| 30 | argument or from '/replica:0/task:0/device:CPU:0'. For example: |
| 31 | If d = '/cpu:0', default='/job:worker/task:1', it returns |
| 32 | '/job:worker/replica:0/task:1/device:CPU:0'. |
| 33 | If d = '/cpu:0', default='/job:worker', it returns |
| 34 | '/job:worker/replica:0/task:0/device:CPU:0'. |
| 35 | If d = '/gpu:0', default=None, it returns |
| 36 | '/replica:0/task:0/device:GPU:0'. |
| 37 | |
| 38 | Note: This uses "job:localhost" as the default if executing eagerly. |
| 39 | |
| 40 | Args: |
| 41 | d: a device string. |
| 42 | default: a string for default device if d doesn't have all components. |
| 43 | |
| 44 | Returns: |
| 45 | a canonicalized device string. |
| 46 | """ |
| 47 | d = tf_device.DeviceSpec.from_string(d) |
| 48 | assert d.device_type is None or d.device_type == d.device_type.upper(), ( |
| 49 | "Device type '%s' must be all-caps." % (d.device_type,)) |
| 50 | # Fill in missing device fields using defaults. |
| 51 | result = tf_device.DeviceSpec( |
| 52 | replica=0, task=0, device_type="CPU", device_index=0) |
| 53 | if ops.executing_eagerly_outside_functions(): |
| 54 | result = result.replace(job="localhost") |
| 55 | if default: |
| 56 | result = result.make_merged_spec( |
| 57 | tf_device.DeviceSpec.from_string(default)) |
| 58 | |
| 59 | # Apply `d` last, so that it takes precidence over the defaults. |
| 60 | result = result.make_merged_spec(d) |
| 61 | return result.to_string() |
| 62 | |
| 63 | |
| 64 | def resolve(d): |
no test coverage detected