Skip to content

Commit 511f5c0

Browse files
committed
Reduce array sizes in memory-intensive tests for WASM
WASM has limited linear memory that doesn't grow well under pressure. Reduce test array sizes to avoid out-of-memory crashes: - SubscriptTest: 400x400 -> 10x10 on WASI - RandomTest: 100x100x100 (1M elements) -> smaller arrays on WASI These changes only affect WASI builds; native builds keep original sizes.
1 parent e367667 commit 511f5c0

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

Tests/MatftTests/RandomTest.swift

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,47 @@ import Matft
1111

1212
final class RandomTests: XCTestCase {
1313
func testRand() {
14-
let a = Matft.random.rand(shape: [100, 100, 100])
14+
// Use smaller array size on WASM to avoid memory pressure
15+
// 100x100x100 = 1M elements = 4MB per array, too much for WASM
16+
#if os(WASI)
17+
let shape = [10, 10, 10] // 1K elements
18+
#else
19+
let shape = [100, 100, 100] // 1M elements
20+
#endif
21+
22+
let a = Matft.random.rand(shape: shape)
1523
let mean_a = a.mean().scalar as! Float
1624
let cond_a = (Float(0.48) < mean_a) && (mean_a < Float(0.52))
17-
18-
let b = Matft.random.rand(shape: [100, 100, 100])
25+
26+
let b = Matft.random.rand(shape: shape)
1927
let mean_b = b.mean().scalar as! Float
2028
let cond_b = (Float(0.48) < mean_b) && (mean_b < Float(0.52))
21-
29+
2230
XCTAssertTrue(cond_a, "The result may be failure due to random generation")
2331
XCTAssertTrue(cond_b, "The result may be failure due to random generation")
24-
32+
2533
XCTAssertNotEqual(a, b,"The result may be failure due to random generation")
2634
}
27-
35+
2836
func testRandint() {
29-
let a = Matft.random.randint(low: 0, high: 10, shape: [100, 100, 100])
37+
// Use smaller array size on WASM to avoid memory pressure
38+
#if os(WASI)
39+
let shape = [10, 10, 10] // 1K elements
40+
#else
41+
let shape = [100, 100, 100] // 1M elements
42+
#endif
43+
44+
let a = Matft.random.randint(low: 0, high: 10, shape: shape)
3045
let mean_a = a.mean().scalar as! Float
3146
let cond_a = (Float(4.48) < mean_a) && (mean_a < Float(4.52))
32-
33-
let b = Matft.random.randint(low: 0, high: 10, shape: [100, 100, 100])
47+
48+
let b = Matft.random.randint(low: 0, high: 10, shape: shape)
3449
let mean_b = b.mean().scalar as! Float
3550
let cond_b = (Float(4.48) < mean_b) && (mean_b < Float(4.52))
36-
51+
3752
XCTAssertTrue(cond_a, "The result may be failure due to random generation")
3853
XCTAssertTrue(cond_b, "The result may be failure due to random generation")
39-
54+
4055
XCTAssertNotEqual(a, b,"The result may be failure due to random generation")
4156
}
4257
}

Tests/MatftTests/SubscriptTest.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,16 +651,22 @@ final class SubscriptTests: XCTestCase {
651651
}
652652

653653
do{
654-
let a = Matft.arange(start: 0, to: 160000, by: 1, shape: [400, 400])
655-
let c = MfArray([true]).broadcast_to(shape: [400, 400])
654+
// Use smaller array size on WASM to avoid memory pressure
655+
#if os(WASI)
656+
let size = 10
657+
#else
658+
let size = 400
659+
#endif
660+
let a = Matft.arange(start: 0, to: size * size, by: 1, shape: [size, size])
661+
let c = MfArray([true]).broadcast_to(shape: [size, size])
656662
/*
657663
self.measure {
658664
a[c] = MfArray([555])
659665
// time in release mode
660666
// average: 0.005, relative standard deviation: 8.334%, values: [0.006032, 0.004685, 0.004630, 0.005293, 0.004738, 0.004789, 0.004905, 0.005056, 0.004624, 0.004729],
661667
}*/
662668
a[c] = MfArray([555])
663-
XCTAssertEqual(a, MfArray([555]).broadcast_to(shape: [400, 400]))
669+
XCTAssertEqual(a, MfArray([555]).broadcast_to(shape: [size, size]))
664670
}
665671

666672

0 commit comments

Comments
 (0)