Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the new top-k implementation in Accelerate #21

Merged
merged 3 commits into from
Nov 25, 2023

Conversation

jkrukowski
Copy link
Contributor

In this PR:

  • added new top-k implementation using Accelerate.BNNS.applyTopK method
  • added tests

- added accuracy to testing method
- added tests
Copy link
Member

@pcuenca pcuenca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works great!

Testing in a development branch, I got the following results:

  • Greedy generation baseline: 58.53 tok/s
  • Top-10 sampling before this PR: 36.40 tok/s
  • Top-10 sampling with this PR: 57.71 tok/s

It's 59% improvement over the previous implementation, and a degradation of just 1.4% with respect to greedy generation. Fantastic!

I only have a comment / question about whether to control errors in the call to the accelerate function. I'm fine merging like this for now.

Comment on lines 94 to 103
try? Accelerate.BNNS.applyTopK(
k: k,
input: arrDescriptor,
bestValues: bestValues,
bestIndices: bestIndices,
axis: 0,
batchSize: 1,
filterParameters: nil
)
let distances = bestValues.data!.withMemoryRebound(to: Float.self, capacity: k) { ptr in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything sensible to do if the operation fails rather than crashing? I'm fine merging as is, just wandering if you have any ideas that don't complicate things too much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with merging it as is for now as well. Documentation is rather silent about the error being thrown, so I suspect that the swift function definition might be autogenerated from some lower level C code. Making this function throwable might just overcomplicate it too much with not much of a gain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pcuenca do you think it'd be better to use try! here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I guess it will fail in the following line anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, yes, let's do that just in case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, forgot to call deallocate on BNNSNDArrayDescriptor so added it as well

Comment on lines 82 to 100
func XCTAssertEqual<T: FloatingPoint>(
_ expression1: @autoclosure () throws -> [T],
_ expression2: @autoclosure () throws -> [T],
accuracy: T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) {
do {
let lhsEvaluated = try expression1()
let rhsEvaluated = try expression2()
XCTAssertEqual(lhsEvaluated.count, rhsEvaluated.count, file: file, line: line)
for (lhs, rhs) in zip(lhsEvaluated, rhsEvaluated) {
XCTAssertEqual(lhs, rhs, accuracy: accuracy, file: file, line: line)
}
} catch {
XCTFail("Unexpected error: \(error)", file: file, line: line)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

- try! instead of try? when applying topk function
@pcuenca
Copy link
Member

pcuenca commented Nov 25, 2023

Amazing work, thanks again!

@pcuenca pcuenca merged commit a55706b into huggingface:main Nov 25, 2023
@jkrukowski jkrukowski deleted the bnns-topk branch November 25, 2023 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants