tensorflow/compiler/mlir/tensorflow/utils/tpu_rewrite_device_util.cc
// Finds TPU devices associated to system device based on spec (e.g. from
// GetTPUSystemDevices). If the number of TPU devices per host do not match for
// every host, a failure will be returned.
Finds TPU devices associated to system device based on spec . If the number of TPU devices per host do not match for
every host, a failure will be returned.
Finds TPU devices associated to system device based on spec . If the number of TPU devices per host do not match for every host a failure will be returned .
Status GetTPUDevices(
    Devices devices, llvm::ArrayRef<Device> system_devices,
    llvm::SmallVectorImpl<llvm::SmallVector<Device, 8>>* tpu_devices) {
  tpu_devices->reserve(system_devices.size());
  auto lookup = [&devices](Device device_spec) {
    device_spec.has_type = true;
    device_spec.type = kDeviceTPU;
    device_spec.has_id = false;
    llvm::SmallVector<Device, 8> host_tpu_devices;
    FindMatchingDevices(devices, device_spec, &host_tpu_devices);
    std::sort(host_tpu_devices.begin(), host_tpu_devices.end(),
              [](const Device& i, const Device& j) { return i.id < j.id; });
    return host_tpu_devices;
  };
  int num_tpus_per_host = 0;
  {
    const auto& device = system_devices[0];
    auto host_tpu_devices = lookup(device);
    num_tpus_per_host = host_tpu_devices.size();
    tpu_devices->push_back(std::move(host_tpu_devices));
  }
  for (const auto& device_spec : llvm::make_range(
           std::next(system_devices.begin()), system_devices.end())) {
    auto host_tpu_devices = lookup(device_spec);
    const int64_t host_tpu_devices_size = host_tpu_devices.size();
    if (num_tpus_per_host != host_tpu_devices_size)
      return errors::InvalidArgument(
          "expected the number of TPU devices per host to be ",
          num_tpus_per_host, ", got ", host_tpu_devices.size());
    tpu_devices->push_back(std::move(host_tpu_devices));
  }
  return Status::OK();
}
Status GetTPUDevices ( Devices devices , llvm :: ArrayRef < Device > system_devices , llvm :: SmallVectorImpl < llvm :: SmallVector < Device , 8 > > * tpu_devices ) { tpu_devices -> reserve ( system_devices . size ( ) ) ; auto lookup = [ & devices ] ( Device device_spec ) { device_spec . has_type = true ; device_spec . type = kDeviceTPU ; device_spec . has_id = false ; llvm :: SmallVector < Device , 8 > host_tpu_devices ; FindMatchingDevices ( devices , device_spec , & host_tpu_devices ) ; std :: sort ( host_tpu_devices . begin ( ) , host_tpu_devices . end ( ) , [ ] ( const Device & i , const Device & j ) { return i . id < j . id ; } ) ; return host_tpu_devices ; } ; int num_tpus_per_host = 0 ; { const auto & device = system_devices [ 0 ] ; auto host_tpu_devices = lookup ( device ) ; num_tpus_per_host = host_tpu_devices . size ( ) ; tpu_devices -> push_back ( std :: move ( host_tpu_devices ) ) ; } for ( const auto & device_spec : llvm :: make_range ( std :: next ( system_devices . begin ( ) ) , system_devices . end ( ) ) ) { auto host_tpu_devices = lookup ( device_spec ) ; const int64_t host_tpu_devices_size = host_tpu_devices . size ( ) ; if ( num_tpus_per_host != host_tpu_devices_size ) return errors :: InvalidArgument ( " " , num_tpus_per_host , " " , host_tpu_devices . size ( ) ) ; tpu_devices -> push_back ( std :: move ( host_tpu_devices ) ) ; } return Status :: OK ( ) ; }
Finds TPU devices associated to system device based on spec (e.g.
Finds TPU devices associated to system device based on spec ( e . g .
// Enumerate all the available TPUs. // Sort devices by id. // Check number of TPU devices per host all match.