@@ -27,3 +27,70 @@ func recommendedMaxWorkingSetSize() async throws {
27
27
let recommended = SHLLM . recommendedMaxWorkingSetSize
28
28
#expect( recommended > 0 )
29
29
}
30
+
31
+ // NOTE: Running inference on the CPU takes way too long.
32
+ @Test ( . enabled( if: false ) )
33
+ func onCPU( ) async throws {
34
+ guard SHLLM . isSupportedDevice else {
35
+ Swift . print ( " ⚠️ Metal GPU not available " )
36
+ return
37
+ }
38
+
39
+ let input : UserInput = . init( messages: [
40
+ [ " role " : " system " , " content " : " You are a helpful assistant. " ] ,
41
+ [ " role " : " user " , " content " : " What is the meaning of life? " ] ,
42
+ ] )
43
+
44
+ try await SHLLM . withDefaultDevice ( . cpu) {
45
+ guard let llm = try loadModel (
46
+ directory: LLM . gemma3_1B,
47
+ input: input,
48
+ customConfiguration: { config in
49
+ var config = config
50
+ config. extraEOSTokens = [ " <end_of_turn> " ]
51
+ return config
52
+ }
53
+ ) as LLM < Gemma3TextModel > ? else { return }
54
+
55
+ var response = " "
56
+ for try await token in llm. text {
57
+ response += token
58
+ }
59
+
60
+ Swift . print ( response)
61
+ #expect( !response. isEmpty)
62
+ }
63
+ }
64
+
65
+ @Test ( )
66
+ func onGPU( ) async throws {
67
+ guard SHLLM . isSupportedDevice else {
68
+ Swift . print ( " ⚠️ Metal GPU not available " )
69
+ return
70
+ }
71
+
72
+ let input : UserInput = . init( messages: [
73
+ [ " role " : " system " , " content " : " You are a helpful assistant. " ] ,
74
+ [ " role " : " user " , " content " : " What is the meaning of life? " ] ,
75
+ ] )
76
+
77
+ try await SHLLM . withDefaultDevice ( . gpu) {
78
+ guard let llm = try loadModel (
79
+ directory: LLM . gemma3_1B,
80
+ input: input,
81
+ customConfiguration: { config in
82
+ var config = config
83
+ config. extraEOSTokens = [ " <end_of_turn> " ]
84
+ return config
85
+ }
86
+ ) as LLM < Gemma3TextModel > ? else { return }
87
+
88
+ var response = " "
89
+ for try await token in llm. text {
90
+ response += token
91
+ }
92
+
93
+ Swift . print ( response)
94
+ #expect( !response. isEmpty)
95
+ }
96
+ }
0 commit comments