Skip to content

Commit 764c353

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Ease main thread by updating UI on every Nth token only. (#2760)
Summary: Pull Request resolved: #2760 bypass-github-export-checks Reviewed By: kirklandsign Differential Revision: D55532081 fbshipit-source-id: 48c67c2872a52c407a0856af566733b079bca99f
1 parent f0c6666 commit 764c353

File tree

12 files changed

+99
-21
lines changed

12 files changed

+99
-21
lines changed

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/App.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
import SwiftUI
410

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/ContentView.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
import SwiftUI
410
import UniformTypeIdentifiers
@@ -204,15 +210,21 @@ struct ContentView: View {
204210
return
205211
}
206212
do {
213+
var tokens: [String] = []
207214
try runnerHolder.runner?.generate(text, sequenceLength: seq_len) { token in
208-
209-
DispatchQueue.main.async {
210-
withAnimation {
211-
var message = messages.removeLast()
212-
message.text += token
213-
message.tokenCount += 1
214-
message.dateUpdated = Date()
215-
messages.append(message)
215+
tokens.append(token)
216+
if tokens.count > 2 {
217+
let text = tokens.joined()
218+
let count = text.count
219+
tokens = []
220+
DispatchQueue.main.async {
221+
withAnimation {
222+
var message = messages.removeLast()
223+
message.text += text
224+
message.tokenCount += count
225+
message.dateUpdated = Date()
226+
messages.append(message)
227+
}
216228
}
217229
}
218230
if shouldStopGenerating {

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/LogManager.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
import SwiftUI
410

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/LogView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
import SwiftUI
410

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/Message.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
import Foundation
410

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/MessageListView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
import SwiftUI
410

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/MessageView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
import SwiftUI
410

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/ResourceManager.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
import SwiftUI
410

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/ResourceMonitor.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
import Foundation
410

@@ -12,7 +18,7 @@ final class ResourceMonitor: ObservableObject {
1218
}
1319

1420
public func start() {
15-
memoryUpdateTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in
21+
memoryUpdateTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
1622
self?.updateMemoryUsage()
1723
}
1824
}

examples/demo-apps/apple_ios/LLaMA/LLaMARunner/LLaMARunner/Exported/LLaMARunner.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
#import <Foundation/Foundation.h>
410

examples/demo-apps/apple_ios/LLaMA/LLaMARunner/LLaMARunner/Exported/LLaMARunner.mm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
#import "LLaMARunner.h"
410

examples/demo-apps/apple_ios/LLaMA/LLaMARunner/LLaMARunner/__tests__/RunnerTest.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

39
@testable import LLaMARunner
410

0 commit comments

Comments
 (0)