From e03ad184690452355c595bac42bf100326835045 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 5 Jan 2021 13:49:17 -0800 Subject: [PATCH 1/2] build: enable building X10 support with SPM Enable building the Swift interfaces for X10 with SPM. This effectively hard-enables X10 support in TensorFlow as SPM does not provide configuration. In order to accomplish this, we need to create symbolic links to sources in order to merge the `x10` sources into the TensorFlow module. It is not possible to simply move the sources to reflect this structure due to out-of-tree use cases. --- Package.swift | 4 +++ Sources/TensorFlow/Core/Device.swift | 35 ------------------- .../TensorFlow/X10/APIs/CrossReplicaSum.swift | 1 + Sources/TensorFlow/X10/APIs/DeviceScope.swift | 1 + .../TensorFlow/X10/APIs/RawOpsManual.swift | 1 + Sources/TensorFlow/X10/Device.swift | 1 + .../TensorFlow/X10/RawOpsXLAGenerated.swift | 1 + Sources/TensorFlow/X10/XLAScalarType.swift | 1 + Sources/TensorFlow/X10/XLATensor.swift | 1 + 9 files changed, 11 insertions(+), 35 deletions(-) delete mode 100644 Sources/TensorFlow/Core/Device.swift create mode 120000 Sources/TensorFlow/X10/APIs/CrossReplicaSum.swift create mode 120000 Sources/TensorFlow/X10/APIs/DeviceScope.swift create mode 120000 Sources/TensorFlow/X10/APIs/RawOpsManual.swift create mode 120000 Sources/TensorFlow/X10/Device.swift create mode 120000 Sources/TensorFlow/X10/RawOpsXLAGenerated.swift create mode 120000 Sources/TensorFlow/X10/XLAScalarType.swift create mode 120000 Sources/TensorFlow/X10/XLATensor.swift diff --git a/Package.swift b/Package.swift index e489eccd4..e832894f4 100644 --- a/Package.swift +++ b/Package.swift @@ -50,6 +50,10 @@ let package = Package( "PythonKit", "CTensorFlow", .product(name: "Numerics", package: "swift-numerics"), + ], + swiftSettings: [ + .define("USING_X10_BACKEND"), + .define("DEFAULT_BACKEND_EAGER"), ]), .target( name: "Experimental", diff --git a/Sources/TensorFlow/Core/Device.swift b/Sources/TensorFlow/Core/Device.swift deleted file mode 100644 index cb985823a..000000000 --- a/Sources/TensorFlow/Core/Device.swift +++ /dev/null @@ -1,35 +0,0 @@ -/// A device on which `Tensor`s can be allocated. -/// -/// Currently, this is a stub because TensorFlow transfers tensors between devices on demand. -public struct Device { - public static var `default`: Device { Device() } - public static var defaultTFEager: Device { Device() } - - /// Backend used to dispatch the tensor operations. - public enum Backend { - case TF_EAGER - - fileprivate var shortName: String { - switch self { - case .TF_EAGER: return "TF_EAGER" - } - } - } -} - -extension Tensor { - /// The device on which `self` is allocated. - public var device: Device { - @_semantics("autodiff.nonvarying") - get { - return Device() - } - } -} - -extension _Raw { - static func toDevice(_ x: Tensor, _ device: Device) -> Tensor { - // TODO: Actually copy to device... - return x - } -} diff --git a/Sources/TensorFlow/X10/APIs/CrossReplicaSum.swift b/Sources/TensorFlow/X10/APIs/CrossReplicaSum.swift new file mode 120000 index 000000000..75326de1b --- /dev/null +++ b/Sources/TensorFlow/X10/APIs/CrossReplicaSum.swift @@ -0,0 +1 @@ +../../../x10/swift_bindings/apis/CrossReplicaSum.swift \ No newline at end of file diff --git a/Sources/TensorFlow/X10/APIs/DeviceScope.swift b/Sources/TensorFlow/X10/APIs/DeviceScope.swift new file mode 120000 index 000000000..b0f9117b6 --- /dev/null +++ b/Sources/TensorFlow/X10/APIs/DeviceScope.swift @@ -0,0 +1 @@ +../../../x10/swift_bindings/apis/DeviceScope.swift \ No newline at end of file diff --git a/Sources/TensorFlow/X10/APIs/RawOpsManual.swift b/Sources/TensorFlow/X10/APIs/RawOpsManual.swift new file mode 120000 index 000000000..591bb2ae4 --- /dev/null +++ b/Sources/TensorFlow/X10/APIs/RawOpsManual.swift @@ -0,0 +1 @@ +../../../x10/swift_bindings/apis/RawOpsManual.swift \ No newline at end of file diff --git a/Sources/TensorFlow/X10/Device.swift b/Sources/TensorFlow/X10/Device.swift new file mode 120000 index 000000000..46dc06205 --- /dev/null +++ b/Sources/TensorFlow/X10/Device.swift @@ -0,0 +1 @@ +../../x10/swift_bindings/Device.swift \ No newline at end of file diff --git a/Sources/TensorFlow/X10/RawOpsXLAGenerated.swift b/Sources/TensorFlow/X10/RawOpsXLAGenerated.swift new file mode 120000 index 000000000..092ebabda --- /dev/null +++ b/Sources/TensorFlow/X10/RawOpsXLAGenerated.swift @@ -0,0 +1 @@ +../../x10/swift_bindings/RawOpsXLAGenerated.swift \ No newline at end of file diff --git a/Sources/TensorFlow/X10/XLAScalarType.swift b/Sources/TensorFlow/X10/XLAScalarType.swift new file mode 120000 index 000000000..9e13e7567 --- /dev/null +++ b/Sources/TensorFlow/X10/XLAScalarType.swift @@ -0,0 +1 @@ +../../x10/swift_bindings/XLAScalarType.swift \ No newline at end of file diff --git a/Sources/TensorFlow/X10/XLATensor.swift b/Sources/TensorFlow/X10/XLATensor.swift new file mode 120000 index 000000000..a356e0ff8 --- /dev/null +++ b/Sources/TensorFlow/X10/XLATensor.swift @@ -0,0 +1 @@ +../../x10/swift_bindings/XLATensor.swift \ No newline at end of file From a215cc678dbbf214c4dc3570e630123aba4086dc Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 5 Jan 2021 15:18:31 -0800 Subject: [PATCH 2/2] build: add a submodule for CX10 handling just the modules The CX10 module setup is currently very complicated. The module is built twice - once via bazel and incorporated into the TensorFlow library. The second build is built via Swift for the modules to import the interfaces into Swift. In order to build the swift bindings via SPM, we need to extract the sources. However, we cannot reorganise the CX10 subdirectory to enable SPM to filter out just the modules. As a result, we create a `CX10Modules` module that is only built when building with SPM. CMake builds will ignore this directory, building the modules from the CX10 directory directly as it can handle the subdirectory layout. --- Package.swift | 4 ++++ Sources/CX10Modules/include/device_wrapper.h | 1 + Sources/CX10Modules/include/module.modulemap | 1 + Sources/CX10Modules/include/xla_tensor_tf_ops.h | 1 + Sources/CX10Modules/include/xla_tensor_wrapper.h | 1 + Sources/CX10Modules/shim.c | 15 +++++++++++++++ 6 files changed, 23 insertions(+) create mode 120000 Sources/CX10Modules/include/device_wrapper.h create mode 120000 Sources/CX10Modules/include/module.modulemap create mode 120000 Sources/CX10Modules/include/xla_tensor_tf_ops.h create mode 120000 Sources/CX10Modules/include/xla_tensor_wrapper.h create mode 100644 Sources/CX10Modules/shim.c diff --git a/Package.swift b/Package.swift index e832894f4..a963094a6 100644 --- a/Package.swift +++ b/Package.swift @@ -43,12 +43,16 @@ let package = Package( .target( name: "CTensorFlow", dependencies: []), + .target( + name: "CX10Modules", + dependencies: []), .target( name: "TensorFlow", dependencies: [ "Tensor", "PythonKit", "CTensorFlow", + "CX10Modules", .product(name: "Numerics", package: "swift-numerics"), ], swiftSettings: [ diff --git a/Sources/CX10Modules/include/device_wrapper.h b/Sources/CX10Modules/include/device_wrapper.h new file mode 120000 index 000000000..c565cd3ed --- /dev/null +++ b/Sources/CX10Modules/include/device_wrapper.h @@ -0,0 +1 @@ +../../CX10/device_wrapper.h \ No newline at end of file diff --git a/Sources/CX10Modules/include/module.modulemap b/Sources/CX10Modules/include/module.modulemap new file mode 120000 index 000000000..1892e1e82 --- /dev/null +++ b/Sources/CX10Modules/include/module.modulemap @@ -0,0 +1 @@ +../../CX10/module.modulemap \ No newline at end of file diff --git a/Sources/CX10Modules/include/xla_tensor_tf_ops.h b/Sources/CX10Modules/include/xla_tensor_tf_ops.h new file mode 120000 index 000000000..e14658de2 --- /dev/null +++ b/Sources/CX10Modules/include/xla_tensor_tf_ops.h @@ -0,0 +1 @@ +../../CX10/xla_tensor_tf_ops.h \ No newline at end of file diff --git a/Sources/CX10Modules/include/xla_tensor_wrapper.h b/Sources/CX10Modules/include/xla_tensor_wrapper.h new file mode 120000 index 000000000..1f14d0ce9 --- /dev/null +++ b/Sources/CX10Modules/include/xla_tensor_wrapper.h @@ -0,0 +1 @@ +../../CX10/xla_tensor_wrapper.h \ No newline at end of file diff --git a/Sources/CX10Modules/shim.c b/Sources/CX10Modules/shim.c new file mode 100644 index 000000000..449151799 --- /dev/null +++ b/Sources/CX10Modules/shim.c @@ -0,0 +1,15 @@ +// Copyright 2019 The TensorFlow Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +extern int _;