-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPackage.swift
More file actions
212 lines (193 loc) · 9.79 KB
/
Package.swift
File metadata and controls
212 lines (193 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
// MARK: - Build Configuration
/// Set via environment: MAGMA_XLA_PATH=/path/to/xla
let xlaPath = Context.environment["MAGMA_XLA_PATH"] ?? "/opt/xla"
/// Set MAGMA_ENABLE_XLA=1 to enable XLA linking
/// When not set, builds without XLA (stub-only mode for development)
let enableXLA = Context.environment["MAGMA_ENABLE_XLA"] == "1"
/// Set MAGMA_ENABLE_METAL=1 to enable Metal backend via MetalHLO
/// Automatically enabled on macOS if MetalHLO is available
#if os(macOS)
let enableMetal = Context.environment["MAGMA_ENABLE_METAL"] != "0"
#else
let enableMetal = false
#endif
// Conditionally add linker settings for XLA
// Note: The PJRT plugin is loaded dynamically via dlopen, so we don't need
// to link against it at build time. We just need the -ldl flag on Linux.
var xlaRuntimeLinkerSettings: [LinkerSetting] = []
if enableXLA {
#if os(Linux)
xlaRuntimeLinkerSettings = [
.linkedLibrary("dl"), // For dlopen/dlsym
]
#endif
}
let package = Package(
name: "Magma",
platforms: [
.macOS(.v14),
],
products: [
// ════════════════════════════════════════════════════════════════════
// MAIN PRODUCT - What users import
// ════════════════════════════════════════════════════════════════════
.library(
name: "Magma",
targets: ["Magma"]
),
// ════════════════════════════════════════════════════════════════════
// INDIVIDUAL LAYERS - For advanced users
// ════════════════════════════════════════════════════════════════════
.library(
name: "LazyTensor",
targets: ["LazyTensor"]
),
.library(
name: "StableHLO",
targets: ["StableHLO"]
),
.library(
name: "XLARuntime",
targets: ["XLARuntime"]
),
// ════════════════════════════════════════════════════════════════════
// EXECUTABLES - Examples
// ════════════════════════════════════════════════════════════════════
.executable(
name: "MNISTExample",
targets: ["MNISTExample"]
),
.executable(
name: "BuildingSimulation",
targets: ["BuildingSimulation"]
),
.executable(
name: "Benchmarks",
targets: ["Benchmarks"]
),
.executable(
name: "MetalExample",
targets: ["MetalExample"]
),
],
dependencies: [
// MetalHLO for Metal GPU support on macOS
.package(path: "../MetalHLO"),
],
targets: [
// ════════════════════════════════════════════════════════════════════
// LAYER 0: C Bindings to PJRT (header-only when XLA not enabled)
// ════════════════════════════════════════════════════════════════════
.target(
name: "CXLARuntime",
path: "Sources/CXLARuntime",
publicHeadersPath: "include"
),
// ════════════════════════════════════════════════════════════════════
// LAYER 1: XLA Runtime (Swift wrapper around PJRT + MetalHLO on macOS)
// ════════════════════════════════════════════════════════════════════
.target(
name: "XLARuntime",
dependencies: [
"CXLARuntime",
.product(name: "MetalHLO", package: "MetalHLO", condition: .when(platforms: [.macOS])),
],
path: "Sources/XLARuntime",
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency"),
// Define HAS_XLA when XLA is available
enableXLA ? .define("HAS_XLA") : nil,
// Define HAS_METAL when Metal backend is available
enableMetal ? .define("HAS_METAL") : nil,
].compactMap { $0 },
linkerSettings: xlaRuntimeLinkerSettings
),
// ════════════════════════════════════════════════════════════════════
// LAYER 2: StableHLO (Pure Swift MLIR generation)
// ════════════════════════════════════════════════════════════════════
.target(
name: "StableHLO",
dependencies: [], // PURE SWIFT - No dependencies!
path: "Sources/StableHLO",
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency"),
]
),
// ════════════════════════════════════════════════════════════════════
// LAYER 3: Lazy Tensor (x10-style execution)
// ════════════════════════════════════════════════════════════════════
.target(
name: "LazyTensor",
dependencies: ["StableHLO", "XLARuntime"],
path: "Sources/LazyTensor",
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency"),
]
),
// ════════════════════════════════════════════════════════════════════
// LAYER 4: Magma (Main tensor API - import Magma)
// ════════════════════════════════════════════════════════════════════
.target(
name: "Magma",
dependencies: ["LazyTensor"],
path: "Sources/Core",
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency"),
]
),
// ════════════════════════════════════════════════════════════════════
// EXAMPLES
// ════════════════════════════════════════════════════════════════════
.executableTarget(
name: "MNISTExample",
dependencies: ["Magma"],
path: "Examples/MNIST"
),
.executableTarget(
name: "BuildingSimulation",
dependencies: ["Magma"],
path: "Examples/BuildingSimulation"
),
.executableTarget(
name: "Benchmarks",
dependencies: ["Magma"],
path: "Examples/Benchmarks"
),
.executableTarget(
name: "MetalExample",
dependencies: ["Magma"],
path: "Examples/Metal"
),
// ════════════════════════════════════════════════════════════════════
// TESTS
// ════════════════════════════════════════════════════════════════════
// StableHLO tests - NO XLA REQUIRED (pure Swift)
.testTarget(
name: "StableHLOTests",
dependencies: ["StableHLO"],
path: "Tests/StableHLOTests"
),
// LazyTensor tests - with mocking
.testTarget(
name: "LazyTensorTests",
dependencies: ["LazyTensor"],
path: "Tests/LazyTensorTests"
),
// XLARuntime tests - requires XLA installed
.testTarget(
name: "XLARuntimeTests",
dependencies: ["XLARuntime"],
path: "Tests/XLARuntimeTests"
),
// Magma tests - end-to-end integration
.testTarget(
name: "MagmaTests",
dependencies: ["Magma"],
path: "Tests/CoreTests"
),
],
swiftLanguageVersions: [.v5]
)