Skip to content

Commit 51f82c5

Browse files
author
Eugene Burmako
authored
[TF] Further cleanup of CompilerRuntime and friends (#24938)
* Remove _ExecutionMode (TF-517) * TF_CreateConfig.enable_xla_compilation is now hardcoded to 0. * TPU tests now unconditionally fail instead of pretending to work. * Remove enableTPU, enableGPU and enableCPU These functions have empty bodies, but they have pretty high-profile names which gives an impression that they do something important. With that in mind, I think it would be best to remove these functions without any deprecation notice because there aren't any comparable replacements at the moment. * Remove obsolete remnants of the TPU testing infrastructure (TF-520) This commit removes the remnants of the obsolete TPU testing infrastructure. In the nearest future, we are planning to restore it within the new tracing runtime (https://bugs.swift.org/browse/TF-520). * Remove obsolete remnants of the TPU runtime infrastructure (TF-519) This commit removes the remnants of the obsolete TPU runtime infrastructure. In the nearest future, we are planning to restore it within the new tracing runtime (https://bugs.swift.org/browse/TF-519).
1 parent a2ba998 commit 51f82c5

39 files changed

+71
-1046
lines changed

stdlib/private/TensorFlowUnittest/TensorFlowUnittest.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,16 @@ public func printT(_ message: String) {
6464
extension TestSuite {
6565
static let willTargetGPU: Bool =
6666
CommandLine.arguments.contains("--target=gpu")
67-
// If the macro TPU is not specified, run the tests with CPU or GPU. Otherwise
68-
// use TPU.
6967
// For GPU execution, TensorFlow must have been built with --config=cuda,
7068
// and a gpu device must be available.
7169
@inline(never)
7270
public func testAllBackends(_ name: String, _ body: @escaping () -> Void) {
73-
#if !TPU
74-
testCPUOrGPU(name, body)
75-
#else
76-
testTPU(name, body)
77-
#endif // TPU
78-
}
79-
public func testCPUOrGPU(_ name: String, _ body: @escaping () -> Void) {
80-
#if !TPU
8171
test(name + (TestSuite.willTargetGPU ? "_GPU" : "_CPU")) {
82-
_RuntimeConfig.executionMode = .auto
8372
_RuntimeConfig.gpuMemoryAllowGrowth = true
8473
_RuntimeConfig.printsDebugLog = false
8574
withDevice(TestSuite.willTargetGPU ? .gpu : .cpu) {
8675
body()
8776
}
8877
}
89-
#endif // TPU
90-
}
91-
public func testTPU(_ name: String, _ body: @escaping () -> Void) {
92-
#if TPU
93-
test(name + "_TPU") {
94-
_RuntimeConfig.executionMode = .tpu
95-
_RuntimeConfig.printsDebugLog = false
96-
body()
97-
}
98-
#endif // TPU
9978
}
10079
}

stdlib/public/TensorFlow/CompilerRuntime.swift

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,6 @@ import Glibc
3232
#endif
3333
import CTensorFlow
3434

35-
// @_frozen // SR-9739
36-
public enum _ExecutionMode : Equatable {
37-
/// CPU or GPU execution.
38-
case auto
39-
/// TPU execution.
40-
// TODO: assess if we can pass this bit of info from compiler settings (when
41-
// enableTPU() is called), and avoid having this additional runtime bit.
42-
case tpu
43-
/// XLA jit-compilation backend (will use GPU when available, and otherwise
44-
/// CPU).
45-
case xla
46-
47-
public var isTPU: Bool {
48-
switch self {
49-
case .tpu: return true
50-
default: return false
51-
}
52-
}
53-
}
54-
5535
/// TraceContext contains the state needed to build a trace graph function
5636
/// (TF_Function). As eager ops are executed in tracing mode, their
5737
/// corresponding nodes are added to the trace graph (via
@@ -444,10 +424,6 @@ public enum _RuntimeConfig {
444424
/// tensor program in this process.
445425
static public var tensorFlowRuntimeInitialized = false
446426

447-
/// For CPU and GPU execution without XLA, use the auto mode. For XLA and/or
448-
/// TPU execution, set the enum value accordingly.
449-
static public var executionMode: _ExecutionMode = .auto
450-
451427
/// When true, let TensorFlow GPU memory allocation start small and grow as
452428
/// needed. Otherwise, The entire GPU memory region is pre-allocated.
453429
static public var gpuMemoryAllowGrowth = true
@@ -505,12 +481,6 @@ private func configureRuntimeFromEnvironment() {
505481
debugLog("Setting TF logging verbose level to \(verboseLevel) from env.")
506482
}
507483

508-
if let value = getenv("SWIFT_TENSORFLOW_USE_TPU_INFEED"),
509-
String(cString: value).lowercased() == "true" {
510-
_RuntimeConfig.executionMode = .tpu
511-
debugLog("Setting TPU execution with infeed from env.")
512-
}
513-
514484
if let value = getenv("SWIFT_TENSORFLOW_SERVER_ADDRESS") {
515485
let address = String(cString: value)
516486
debugLog("Env var SWIFT_TENSORFLOW_SERVER_ADDRESS has value \(address).")
@@ -563,23 +533,6 @@ private func configureRuntimeFromEnvironment() {
563533
}
564534
}
565535

566-
/// Initialize the TPU system.
567-
/// - Note: This should be called only once.
568-
/// - Precondition: The given session must contain the given graph.
569-
// TODO(b/77572335): Reassess how to reset TPU after execution error.
570-
private func initializeTPU(withSession session: CTFSession, graph: CTFGraph,
571-
status: CTFStatus) {
572-
debugLog("Initializing TPU.")
573-
let configOp = TF_GraphOperationByName(graph, "ConfigureDistributedTPU")
574-
internalConsistencyCheck(configOp != nil)
575-
var configNode = TF_Output(oper: configOp, index: 0)
576-
var dummyOutput: CTensor?
577-
TF_SessionRun(session, nil, nil, nil, 0, &configNode, &dummyOutput, 1, nil,
578-
0, nil, status)
579-
checkOk(status)
580-
TF_DeleteTensor(dummyOutput)
581-
}
582-
583536
/// The host of any tensor computation.
584537
@_fixed_layout
585538
public final class _ExecutionContext {
@@ -594,9 +547,6 @@ public final class _ExecutionContext {
594547
/// Only set when there is some usable GPU.
595548
fileprivate let gpuDeviceNamePrefix: String?
596549

597-
/// Only set when there is some usable TPU.
598-
fileprivate let tpuDeviceNamePrefix: String?
599-
600550
/// The buffer storing a serialized TensorFlow config proto.
601551
public let tensorFlowConfig: UnsafeMutablePointer<TF_Buffer>
602552

@@ -632,14 +582,11 @@ public final class _ExecutionContext {
632582
}
633583

634584
// Create TF config object.
635-
if _RuntimeConfig.executionMode == .xla {
636-
debugLog("Enable XLA execution.")
637-
}
638585
if _RuntimeConfig.gpuMemoryAllowGrowth {
639586
debugLog("Allowing growth for GPU memory allocator.")
640587
}
641588
self.tensorFlowConfig = TF_CreateConfig(
642-
_RuntimeConfig.executionMode == .xla ? 1 : 0,
589+
/* enable_xla_compilation */ 0,
643590
_RuntimeConfig.gpuMemoryAllowGrowth ? 1 : 0,
644591
_RuntimeConfig.cpuDeviceCount)
645592
TFE_ContextOptionsSetConfig(opts,
@@ -666,9 +613,6 @@ public final class _ExecutionContext {
666613
}
667614

668615
// Initialize GPU device.
669-
// While the code here is only needed when _RuntimeConfig.executionMode is
670-
// set to .gpu, running it in all code paths helps keep things simple
671-
// (e.g. so that the cpuDeviceNamePrefix property is always set.)
672616
let devices = TFE_ContextListDevices(eagerContext, status)
673617
checkOk(status)
674618
defer { TF_DeleteDeviceList(devices!) }
@@ -679,7 +623,6 @@ public final class _ExecutionContext {
679623
debugLog("There are \(deviceCount) devices.")
680624
var foundCPU = false
681625
var gpuCount = 0
682-
var tpuCount = 0
683626
for deviceId in 0..<deviceCount {
684627
let cDeviceName = TF_DeviceListName(devices, deviceId, status)
685628
checkOk(status)
@@ -696,9 +639,6 @@ public final class _ExecutionContext {
696639
if deviceType == "GPU" {
697640
gpuCount += 1
698641
}
699-
if deviceType == "TPU" {
700-
tpuCount += 1
701-
}
702642
}
703643
guard foundCPU else {
704644
fatalError("CPU should always be an available device.")
@@ -712,14 +652,6 @@ public final class _ExecutionContext {
712652
self.gpuDeviceNamePrefix = nil
713653
}
714654

715-
if tpuCount > 0 {
716-
// According to server def generated when you set
717-
// SWIFT_TENSORFLOW_SERVER_ADDRESS, the TPUs will all be on task 1.
718-
self.tpuDeviceNamePrefix = "/job:localhost/replica:0/task:1/device:TPU:"
719-
} else {
720-
self.tpuDeviceNamePrefix = nil
721-
}
722-
723655
// Initialize the mutex.
724656
pthread_mutex_init(&mutex, nil)
725657
}
@@ -1063,8 +995,6 @@ internal extension _ExecutionContext {
1063995
return "\(cpuDeviceNamePrefix)\(index)"
1064996
case .gpu:
1065997
return "\(gpuDeviceNamePrefix!)\(index)"
1066-
case .tpu:
1067-
return "\(tpuDeviceNamePrefix!)\(index)"
1068998
}
1069999
}
10701000
return nil

stdlib/public/TensorFlow/Execution.swift

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,12 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
// If `serverAddress` is nil, use local session (good for forge testing).
18-
//
19-
// FIXME: We need transparent here because deabstraction isn't inlining this
20-
// function. We need to inline if a callee contains tensor ops, not only if
21-
// it takes and returns a TensorFlow value.
22-
@_transparent
23-
@available(*, deprecated)
24-
public func enableTPU(serverAddress: String? = nil, infeed: Bool = true) {
25-
_RuntimeConfig.executionMode = .tpu
26-
if let serverAddress = serverAddress {
27-
_RuntimeConfig.session = .remote(serverDef: serverAddress)
28-
}
29-
}
30-
31-
// FIXME: Extend the interface to support multiple GPU devices, and unify it
32-
// with enableTPU() above.
33-
@available(*, deprecated)
34-
@_transparent
35-
public func enableGPU() {
36-
}
37-
38-
@_transparent
39-
@available(*, deprecated)
40-
public func enableCPU() {
41-
}
42-
4317
/// A TensorFlow device kind.
4418
public enum DeviceKind {
4519
/// The CPU device kind.
4620
case cpu
4721
/// The GPU device kind.
4822
case gpu
49-
/// The TPU device kind.
50-
case tpu
5123
}
5224

5325
/// Executes a closure, making TensorFlow operations run on a specific kind of

0 commit comments

Comments
 (0)