Skip to content

Commit

Permalink
[XLA:GPU] Remove some Windows- and Darwinn-specific code.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 571523075
  • Loading branch information
chsigg authored and tensorflower-gardener committed Oct 7, 2023
1 parent 0408e4e commit 78de18d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 146 deletions.
117 changes: 4 additions & 113 deletions third_party/xla/xla/stream_executor/cuda/cuda_diagnostics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,17 @@ limitations under the License.

#include "xla/stream_executor/cuda/cuda_diagnostics.h"

#if !defined(PLATFORM_WINDOWS)
#include <dirent.h>
#endif

#include <limits.h>
#include <link.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __APPLE__
#include <IOKit/kext/KextManager.h>
#include <mach-o/dyld.h>
#else
#if !defined(PLATFORM_WINDOWS)
#include <link.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#endif
#include <sys/stat.h>
#endif

#include <algorithm>
#include <memory>
#include <vector>
Expand Down Expand Up @@ -113,11 +104,7 @@ tsl::StatusOr<DriverVersion> StringToDriverVersion(const std::string &value) {
namespace stream_executor {
namespace gpu {

#ifdef __APPLE__
static const CFStringRef kDriverKextIdentifier = CFSTR("com.nvidia.CUDA");
#elif !defined(PLATFORM_WINDOWS)
static const char *kDriverVersionPath = "/proc/driver/nvidia/version";
#endif

// -- class Diagnostician

Expand All @@ -126,31 +113,6 @@ std::string Diagnostician::GetDevNodePath(int dev_node_ordinal) {
}

void Diagnostician::LogDiagnosticInformation() {
#ifdef __APPLE__
CFStringRef kext_ids[1];
kext_ids[0] = kDriverKextIdentifier;
CFArrayRef kext_id_query = CFArrayCreate(nullptr, (const void **)kext_ids, 1,
&kCFTypeArrayCallBacks);
CFDictionaryRef kext_infos =
KextManagerCopyLoadedKextInfo(kext_id_query, nullptr);
CFRelease(kext_id_query);

CFDictionaryRef cuda_driver_info = nullptr;
if (CFDictionaryGetValueIfPresent(kext_infos, kDriverKextIdentifier,
(const void **)&cuda_driver_info)) {
bool started = CFBooleanGetValue((CFBooleanRef)CFDictionaryGetValue(
cuda_driver_info, CFSTR("OSBundleStarted")));
if (!started) {
LOG(INFO) << "kernel driver is installed, but does not appear to be "
"running on this host "
<< "(" << tsl::port::Hostname() << ")";
}
} else {
LOG(INFO) << "kernel driver does not appear to be installed on this host "
<< "(" << tsl::port::Hostname() << ")";
}
CFRelease(kext_infos);
#elif !defined(PLATFORM_WINDOWS)
if (access(kDriverVersionPath, F_OK) != 0) {
VLOG(1) << "kernel driver does not appear to be running on this host "
<< "(" << tsl::port::Hostname() << "): "
Expand All @@ -163,7 +125,6 @@ void Diagnostician::LogDiagnosticInformation() {
<< " does not exist";
return;
}
#endif

LOG(INFO) << "retrieving CUDA diagnostic information for host: "
<< tsl::port::Hostname();
Expand All @@ -173,7 +134,6 @@ void Diagnostician::LogDiagnosticInformation() {

/* static */ void Diagnostician::LogDriverVersionInformation() {
LOG(INFO) << "hostname: " << tsl::port::Hostname();
#ifndef PLATFORM_WINDOWS
if (VLOG_IS_ON(1)) {
const char *value = getenv("LD_LIBRARY_PATH");
std::string library_path = value == nullptr ? "" : value;
Expand Down Expand Up @@ -202,14 +162,10 @@ void Diagnostician::LogDiagnosticInformation() {
tsl::StatusOr<DriverVersion> kernel_version = FindKernelDriverVersion();
LOG(INFO) << "kernel reported version is: "
<< cuda::DriverVersionStatusToString(kernel_version);
#endif

// OS X kernel driver does not report version accurately
#if !defined(__APPLE__) && !defined(PLATFORM_WINDOWS)
if (kernel_version.ok() && dso_version.ok()) {
WarnOnDsoKernelMismatch(dso_version, kernel_version);
}
#endif
}

// Iterates through loaded DSOs with DlIteratePhdrCallback to find the
Expand All @@ -219,30 +175,7 @@ tsl::StatusOr<DriverVersion> Diagnostician::FindDsoVersion() {
absl::StatusCode::kNotFound,
"was unable to find libcuda.so DSO loaded into this program"));

#if defined(__APPLE__)
// OSX CUDA libraries have names like: libcuda_310.41.15_mercury.dylib
const string prefix("libcuda_");
const string suffix("_mercury.dylib");
for (uint32_t image_index = 0; image_index < _dyld_image_count();
++image_index) {
const string path(_dyld_get_image_name(image_index));
const size_t suffix_pos = path.rfind(suffix);
const size_t prefix_pos = path.rfind(prefix, suffix_pos);
if (prefix_pos == string::npos || suffix_pos == string::npos) {
// no match
continue;
}
const size_t start = prefix_pos + prefix.size();
if (start >= suffix_pos) {
// version not included
continue;
}
const size_t length = suffix_pos - start;
const string version = path.substr(start, length);
result = cuda::StringToDriverVersion(version);
}
#else
#if !defined(PLATFORM_WINDOWS) && !defined(ANDROID_TEGRA)
#if !defined(ANDROID_TEGRA)
// Callback used when iterating through DSOs. Looks for the driver-interfacing
// DSO and yields its version number into the callback data, when found.
auto iterate_phdr = [](struct dl_phdr_info *info, size_t size,
Expand Down Expand Up @@ -274,7 +207,6 @@ tsl::StatusOr<DriverVersion> Diagnostician::FindDsoVersion() {
};

dl_iterate_phdr(iterate_phdr, &result);
#endif
#endif

return result;
Expand Down Expand Up @@ -318,46 +250,6 @@ void Diagnostician::WarnOnDsoKernelMismatch(
}

tsl::StatusOr<DriverVersion> Diagnostician::FindKernelDriverVersion() {
#if defined(__APPLE__)
CFStringRef kext_ids[1];
kext_ids[0] = kDriverKextIdentifier;
CFArrayRef kext_id_query = CFArrayCreate(nullptr, (const void **)kext_ids, 1,
&kCFTypeArrayCallBacks);
CFDictionaryRef kext_infos =
KextManagerCopyLoadedKextInfo(kext_id_query, nullptr);
CFRelease(kext_id_query);

CFDictionaryRef cuda_driver_info = nullptr;
if (CFDictionaryGetValueIfPresent(kext_infos, kDriverKextIdentifier,
(const void **)&cuda_driver_info)) {
// NOTE: OSX CUDA driver does not currently store the same driver version
// in kCFBundleVersionKey as is returned by cuDriverGetVersion
CFRelease(kext_infos);
const CFStringRef str = (CFStringRef)CFDictionaryGetValue(
cuda_driver_info, kCFBundleVersionKey);
const char *version = CFStringGetCStringPtr(str, kCFStringEncodingUTF8);

// version can be NULL in which case treat it as empty string
// see
// https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFStrings/Articles/AccessingContents.html#//apple_ref/doc/uid/20001184-100980-TPXREF112
if (version == NULL) {
return cuda::StringToDriverVersion("");
}
return cuda::StringToDriverVersion(version);
}
CFRelease(kext_infos);
auto status = tsl::Status(
absl::StatusCode::kInternal,
absl::StrCat(
"failed to read driver bundle version: ",
CFStringGetCStringPtr(kDriverKextIdentifier, kCFStringEncodingUTF8)));
return status;
#elif defined(PLATFORM_WINDOWS)
auto status =
tsl::Status(absl::StatusCode::kUnimplemented,
"kernel reported driver version not implemented on Windows");
return status;
#else
FILE *driver_version_file = fopen(kDriverVersionPath, "r");
if (driver_version_file == nullptr) {
return tsl::Status(
Expand Down Expand Up @@ -389,7 +281,6 @@ tsl::StatusOr<DriverVersion> Diagnostician::FindKernelDriverVersion() {
"; ferror: ", ferror(driver_version_file)));
fclose(driver_version_file);
return status;
#endif
}

} // namespace gpu
Expand Down
20 changes: 2 additions & 18 deletions third_party/xla/xla/stream_executor/cuda/cuda_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,12 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <unistd.h>

#include <cstdint>
#include <memory>
#include <utility>

#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
#if defined(PLATFORM_WINDOWS)
#include <windows.h>
#define PATH_MAX MAX_PATH
#else
#include <unistd.h>
#endif
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/strings/ascii.h"
Expand All @@ -46,7 +39,6 @@ limitations under the License.
#include "xla/stream_executor/gpu/gpu_types.h"
#include "xla/stream_executor/kernel_cache_config.h"
#include "xla/stream_executor/platform.h"
#include "xla/stream_executor/platform/initialize.h"
#include "xla/stream_executor/plugin_registry.h"
#include "xla/stream_executor/stream.h"
#include "xla/stream_executor/stream_executor.h"
Expand Down Expand Up @@ -876,13 +868,6 @@ GpuContext* GpuExecutor::gpu_context() { return context_; }
// turn to gsys' topology modeling.
static int TryToReadNumaNode(const std::string& pci_bus_id,
int device_ordinal) {
#if defined(__APPLE__)
LOG(INFO) << "OS X does not support NUMA - returning NUMA node zero";
return 0;
#elif defined(PLATFORM_WINDOWS)
// Windows support for NUMA is not currently implemented. Return node 0.
return 0;
#else
VLOG(2) << "trying to read NUMA node for device ordinal: " << device_ordinal;
static const int kUnknownNumaNode = -1;

Expand Down Expand Up @@ -933,7 +918,6 @@ static int TryToReadNumaNode(const std::string& pci_bus_id,

fclose(file);
return kUnknownNumaNode;
#endif
}

tsl::StatusOr<std::unique_ptr<DeviceDescription>>
Expand Down
23 changes: 8 additions & 15 deletions third_party/xla/xla/stream_executor/gpu/asm_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,6 @@ std::string FindCudaExecutable(const std::string& binary_name,
new absl::flat_hash_map<std::pair<std::string, std::string>,
std::string>();

#if defined(PLATFORM_WINDOWS)
const std::string binary_filename = binary_name + ".exe";
#else
const std::string& binary_filename = binary_name;
#endif

auto cache_key = std::make_pair(binary_name, preferred_cuda_dir);

absl::MutexLock lock(&mu);
Expand All @@ -186,20 +180,19 @@ std::string FindCudaExecutable(const std::string& binary_name,
}

// Try searching in the default PATH first if applicable.
if (tsl::PreferPtxasFromPath() &&
GetToolVersionString(binary_filename).ok()) {
VLOG(2) << "Using " << binary_filename;
seen_binary_paths->emplace(std::move(cache_key), binary_filename);
return binary_filename;
if (tsl::PreferPtxasFromPath() && GetToolVersionString(binary_name).ok()) {
VLOG(2) << "Using " << binary_name;
seen_binary_paths->emplace(std::move(cache_key), binary_name);
return binary_name;
}

// Search in cuda root candidates.
auto env = tsl::Env::Default();
std::string binary_path;
for (const std::string& cuda_root :
tsl::CandidateCudaRoots(preferred_cuda_dir)) {
binary_path = tsl::io::JoinPath(cuda_root, "bin", binary_filename);
VLOG(2) << "Looking for " << binary_filename << " at " << binary_path;
binary_path = tsl::io::JoinPath(cuda_root, "bin", binary_name);
VLOG(2) << "Looking for " << binary_name << " at " << binary_path;
if (env->FileExists(binary_path).ok() &&
GetToolVersionString(binary_path).ok()) {
break;
Expand All @@ -210,9 +203,9 @@ std::string FindCudaExecutable(const std::string& binary_name,
// binary. This won't work, in all probability, given we already tried that
// above, but it's the best we can do.
VLOG(2) << "Unable to find " << binary_name;
binary_path = binary_filename;
binary_path = binary_name;
}
VLOG(2) << "Using " << binary_filename << " at " << binary_path;
VLOG(2) << "Using " << binary_name << " at " << binary_path;
seen_binary_paths->emplace(std::move(cache_key), binary_path);
return binary_path;
}
Expand Down

0 comments on commit 78de18d

Please sign in to comment.