|
| 1 | +// Cross-language tests comparing Python and Swift implementations |
| 2 | +// These tests load test data exported from the Python implementation |
| 3 | +// and verify that the Swift implementation produces identical outputs |
| 4 | + |
| 5 | +import XCTest |
| 6 | +import MLX |
| 7 | +@testable import VJEPA2 |
| 8 | + |
| 9 | +final class CrossLanguageTests: XCTestCase { |
| 10 | + let testDataDir: URL = { |
| 11 | + let currentFile = URL(fileURLWithPath: #file) |
| 12 | + return currentFile |
| 13 | + .deletingLastPathComponent() |
| 14 | + .appendingPathComponent("TestData") |
| 15 | + }() |
| 16 | + |
| 17 | + let tolerance: Float = 1e-4 // Tolerance for float comparison |
| 18 | + |
| 19 | + // MARK: - Helper Functions |
| 20 | + |
| 21 | + /// Load numpy array from file |
| 22 | + func loadNumpyArray(_ filename: String, in directory: URL) throws -> MLXArray { |
| 23 | + let fileURL = directory.appendingPathComponent(filename) |
| 24 | + let data = try Data(contentsOf: fileURL) |
| 25 | + |
| 26 | + // Parse .npy file format |
| 27 | + // This is a simplified parser - production code should use a proper library |
| 28 | + // For now, we'll use a basic implementation |
| 29 | + // NOTE: This requires proper .npy parsing which MLX Swift may provide |
| 30 | + // If not available, you'll need to implement or use a library |
| 31 | + |
| 32 | + fatalError("Implement .npy parsing for MLX Swift") |
| 33 | + } |
| 34 | + |
| 35 | + /// Compare two MLX arrays with tolerance |
| 36 | + func assertArraysClose(_ a: MLXArray, _ b: MLXArray, tolerance: Float = 1e-4, file: StaticString = #file, line: UInt = #line) { |
| 37 | + XCTAssertEqual(a.shape, b.shape, "Array shapes don't match", file: file, line: line) |
| 38 | + |
| 39 | + let diff = abs(a - b) |
| 40 | + let maxDiff = diff.max().item(Float.self) |
| 41 | + |
| 42 | + XCTAssertLessThan(maxDiff, tolerance, "Arrays differ by more than tolerance: \(maxDiff)", file: file, line: line) |
| 43 | + } |
| 44 | + |
| 45 | + /// Load test case inputs and outputs |
| 46 | + func loadTestCase(_ name: String) throws -> (inputs: [String: MLXArray], outputs: [String: MLXArray]) { |
| 47 | + let caseDir = testDataDir.appendingPathComponent(name) |
| 48 | + |
| 49 | + // Load metadata |
| 50 | + let metadataURL = caseDir.appendingPathComponent("metadata.json") |
| 51 | + let metadataData = try Data(contentsOf: metadataURL) |
| 52 | + let metadata = try JSONDecoder().decode(TestCaseMetadata.self, from: metadataData) |
| 53 | + |
| 54 | + // Load inputs |
| 55 | + var inputs: [String: MLXArray] = [:] |
| 56 | + for inputName in metadata.inputs { |
| 57 | + inputs[inputName] = try loadNumpyArray("input_\(inputName).npy", in: caseDir) |
| 58 | + } |
| 59 | + |
| 60 | + // Load outputs |
| 61 | + var outputs: [String: MLXArray] = [:] |
| 62 | + for outputName in metadata.outputs { |
| 63 | + outputs[outputName] = try loadNumpyArray("output_\(outputName).npy", in: caseDir) |
| 64 | + } |
| 65 | + |
| 66 | + return (inputs, outputs) |
| 67 | + } |
| 68 | + |
| 69 | + // MARK: - Positional Embedding Tests |
| 70 | + |
| 71 | + func testPositionalEmbedding1D() throws { |
| 72 | + let testCase = try loadTestCase("pos_embed_1d") |
| 73 | + |
| 74 | + let embedDim = Int(testCase.inputs["embed_dim"]!.item(Int32.self)) |
| 75 | + let gridSize = Int(testCase.inputs["grid_size"]!.item(Int32.self)) |
| 76 | + |
| 77 | + let swiftOutput = get1DSinCosPositionEmbed(embedDim: embedDim, gridSize: gridSize) |
| 78 | + let expectedOutput = testCase.outputs["pos_embed"]! |
| 79 | + |
| 80 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 81 | + } |
| 82 | + |
| 83 | + func testPositionalEmbedding2D() throws { |
| 84 | + let testCase = try loadTestCase("pos_embed_2d") |
| 85 | + |
| 86 | + let embedDim = Int(testCase.inputs["embed_dim"]!.item(Int32.self)) |
| 87 | + let gridSize = Int(testCase.inputs["grid_size"]!.item(Int32.self)) |
| 88 | + |
| 89 | + let swiftOutput = get2DSinCosPositionEmbed(embedDim: embedDim, gridSize: gridSize) |
| 90 | + let expectedOutput = testCase.outputs["pos_embed"]! |
| 91 | + |
| 92 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 93 | + } |
| 94 | + |
| 95 | + func testPositionalEmbedding3D() throws { |
| 96 | + let testCase = try loadTestCase("pos_embed_3d") |
| 97 | + |
| 98 | + let embedDim = Int(testCase.inputs["embed_dim"]!.item(Int32.self)) |
| 99 | + let gridSize = Int(testCase.inputs["grid_size"]!.item(Int32.self)) |
| 100 | + let gridDepth = Int(testCase.inputs["grid_depth"]!.item(Int32.self)) |
| 101 | + |
| 102 | + let swiftOutput = get3DSinCosPositionEmbed( |
| 103 | + embedDim: embedDim, |
| 104 | + gridSize: gridSize, |
| 105 | + gridDepth: gridDepth, |
| 106 | + uniformPower: false |
| 107 | + ) |
| 108 | + let expectedOutput = testCase.outputs["pos_embed"]! |
| 109 | + |
| 110 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 111 | + } |
| 112 | + |
| 113 | + // MARK: - Patch Embedding Tests |
| 114 | + |
| 115 | + func testPatchEmbed2D() throws { |
| 116 | + let testCase = try loadTestCase("patch_embed_2d") |
| 117 | + |
| 118 | + let patchSize = Int(testCase.inputs["patch_size"]!.item(Int32.self)) |
| 119 | + let embedDim = Int(testCase.inputs["embed_dim"]!.item(Int32.self)) |
| 120 | + |
| 121 | + let patchEmbed = PatchEmbed(patchSize: patchSize, inChannels: 3, embedDim: embedDim) |
| 122 | + let swiftOutput = patchEmbed(testCase.inputs["image"]!) |
| 123 | + let expectedOutput = testCase.outputs["patches"]! |
| 124 | + |
| 125 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 126 | + } |
| 127 | + |
| 128 | + func testPatchEmbed3D() throws { |
| 129 | + let testCase = try loadTestCase("patch_embed_3d") |
| 130 | + |
| 131 | + let patchSize = Int(testCase.inputs["patch_size"]!.item(Int32.self)) |
| 132 | + let tubeletSize = Int(testCase.inputs["tubelet_size"]!.item(Int32.self)) |
| 133 | + let embedDim = Int(testCase.inputs["embed_dim"]!.item(Int32.self)) |
| 134 | + |
| 135 | + let patchEmbed = PatchEmbed3D( |
| 136 | + patchSize: patchSize, |
| 137 | + tubeletSize: tubeletSize, |
| 138 | + inChannels: 3, |
| 139 | + embedDim: embedDim |
| 140 | + ) |
| 141 | + let swiftOutput = patchEmbed(testCase.inputs["video"]!) |
| 142 | + let expectedOutput = testCase.outputs["patches"]! |
| 143 | + |
| 144 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 145 | + } |
| 146 | + |
| 147 | + // MARK: - MLP Tests |
| 148 | + |
| 149 | + func testMLPStandard() throws { |
| 150 | + let testCase = try loadTestCase("mlp_standard") |
| 151 | + |
| 152 | + let inFeatures = Int(testCase.inputs["in_features"]!.item(Int32.self)) |
| 153 | + let hiddenFeatures = Int(testCase.inputs["hidden_features"]!.item(Int32.self)) |
| 154 | + |
| 155 | + let mlp = MLP(inFeatures: inFeatures, hiddenFeatures: hiddenFeatures, useSiLU: false) |
| 156 | + let swiftOutput = mlp(testCase.inputs["x"]!) |
| 157 | + let expectedOutput = testCase.outputs["output"]! |
| 158 | + |
| 159 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 160 | + } |
| 161 | + |
| 162 | + // MARK: - Attention Tests |
| 163 | + |
| 164 | + func testAttentionStandard() throws { |
| 165 | + let testCase = try loadTestCase("attention_standard") |
| 166 | + |
| 167 | + let dim = Int(testCase.inputs["dim"]!.item(Int32.self)) |
| 168 | + let numHeads = Int(testCase.inputs["num_heads"]!.item(Int32.self)) |
| 169 | + |
| 170 | + let attention = Attention(dim: dim, numHeads: numHeads, qkvBias: true) |
| 171 | + let swiftOutput = attention(testCase.inputs["x"]!) |
| 172 | + let expectedOutput = testCase.outputs["output"]! |
| 173 | + |
| 174 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 175 | + } |
| 176 | + |
| 177 | + func testAttentionRoPE() throws { |
| 178 | + let testCase = try loadTestCase("attention_rope") |
| 179 | + |
| 180 | + let dim = Int(testCase.inputs["dim"]!.item(Int32.self)) |
| 181 | + let numHeads = Int(testCase.inputs["num_heads"]!.item(Int32.self)) |
| 182 | + let gridSize = Int(testCase.inputs["grid_size"]!.item(Int32.self)) |
| 183 | + |
| 184 | + let ropeAttention = RoPEAttention( |
| 185 | + dim: dim, |
| 186 | + numHeads: numHeads, |
| 187 | + gridSize: gridSize, |
| 188 | + qkvBias: true |
| 189 | + ) |
| 190 | + let swiftOutput = ropeAttention( |
| 191 | + testCase.inputs["x"]!, |
| 192 | + T: 1, |
| 193 | + hPatches: 14, |
| 194 | + wPatches: 14 |
| 195 | + ) |
| 196 | + let expectedOutput = testCase.outputs["output"]! |
| 197 | + |
| 198 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 199 | + } |
| 200 | + |
| 201 | + // MARK: - Block Tests |
| 202 | + |
| 203 | + func testBlockStandard() throws { |
| 204 | + let testCase = try loadTestCase("block_standard") |
| 205 | + |
| 206 | + let dim = Int(testCase.inputs["dim"]!.item(Int32.self)) |
| 207 | + let numHeads = Int(testCase.inputs["num_heads"]!.item(Int32.self)) |
| 208 | + |
| 209 | + let block = Block( |
| 210 | + dim: dim, |
| 211 | + numHeads: numHeads, |
| 212 | + mlpRatio: 4.0, |
| 213 | + qkvBias: true, |
| 214 | + useRoPE: false |
| 215 | + ) |
| 216 | + let swiftOutput = block(testCase.inputs["x"]!) |
| 217 | + let expectedOutput = testCase.outputs["output"]! |
| 218 | + |
| 219 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 220 | + } |
| 221 | + |
| 222 | + // MARK: - Vision Transformer Tests |
| 223 | + |
| 224 | + func testVisionTransformerImage() throws { |
| 225 | + let testCase = try loadTestCase("vit_image") |
| 226 | + |
| 227 | + let vit = VisionTransformer( |
| 228 | + imgSize: (224, 224), |
| 229 | + patchSize: 16, |
| 230 | + numFrames: 1, |
| 231 | + embedDim: 768, |
| 232 | + depth: 12, |
| 233 | + numHeads: 12, |
| 234 | + useRoPE: false |
| 235 | + ) |
| 236 | + let swiftOutput = vit(testCase.inputs["image"]!) |
| 237 | + let expectedOutput = testCase.outputs["output"]! |
| 238 | + |
| 239 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 240 | + } |
| 241 | + |
| 242 | + func testVisionTransformerVideo() throws { |
| 243 | + let testCase = try loadTestCase("vit_video") |
| 244 | + |
| 245 | + let vit = VisionTransformer( |
| 246 | + imgSize: (224, 224), |
| 247 | + patchSize: 16, |
| 248 | + numFrames: 16, |
| 249 | + tubeletSize: 2, |
| 250 | + embedDim: 768, |
| 251 | + depth: 12, |
| 252 | + numHeads: 12, |
| 253 | + useRoPE: false |
| 254 | + ) |
| 255 | + let swiftOutput = vit(testCase.inputs["video"]!) |
| 256 | + let expectedOutput = testCase.outputs["output"]! |
| 257 | + |
| 258 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 259 | + } |
| 260 | + |
| 261 | + // MARK: - Attentive Classifier Tests |
| 262 | + |
| 263 | + func testAttentiveClassifier() throws { |
| 264 | + let testCase = try loadTestCase("attentive_classifier") |
| 265 | + |
| 266 | + let embedDim = Int(testCase.inputs["embed_dim"]!.item(Int32.self)) |
| 267 | + let numClasses = Int(testCase.inputs["num_classes"]!.item(Int32.self)) |
| 268 | + |
| 269 | + let classifier = AttentiveClassifier( |
| 270 | + embedDim: embedDim, |
| 271 | + numHeads: 12, |
| 272 | + depth: 1, |
| 273 | + numClasses: numClasses |
| 274 | + ) |
| 275 | + let swiftOutput = classifier(testCase.inputs["tokens"]!) |
| 276 | + let expectedOutput = testCase.outputs["logits"]! |
| 277 | + |
| 278 | + assertArraysClose(swiftOutput, expectedOutput, tolerance: tolerance) |
| 279 | + } |
| 280 | +} |
| 281 | + |
| 282 | +// MARK: - Helper Structures |
| 283 | + |
| 284 | +struct TestCaseMetadata: Codable { |
| 285 | + let name: String |
| 286 | + let inputs: [String] |
| 287 | + let outputs: [String] |
| 288 | +} |
0 commit comments