Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions extension/llm/sampler/sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ int32_t Sampler::sample(T* logits) {
return next;
}

template <>
int32_t Sampler::sample<executorch::aten::Half>(
executorch::aten::Half* logits) {
if (inv_temperature_ == 0.0f) {
return sample_argmax(logits);
}
float_logits_buffer_.resize(vocab_size_);
for (int i = 0; i < vocab_size_; i++) {
float_logits_buffer_[i] = static_cast<float>(logits[i]);
}

return sample(float_logits_buffer_.data());
}

template <>
int32_t Sampler::sample<executorch::aten::BFloat16>(
executorch::aten::BFloat16* logits) {
Expand All @@ -264,8 +278,6 @@ int32_t Sampler::sample<executorch::aten::BFloat16>(

template int32_t Sampler::sample<float>(float* logits);
template int32_t Sampler::sample<uint16_t>(uint16_t* logits);
template int32_t Sampler::sample<executorch::aten::Half>(
executorch::aten::Half* logits);

} // namespace llm
} // namespace extension
Expand Down
3 changes: 3 additions & 0 deletions extension/llm/sampler/sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class ET_EXPERIMENTAL Sampler {
std::vector<float> float_logits_buffer_;
};

template <>
int32_t Sampler::sample<executorch::aten::Half>(executorch::aten::Half* logits);

template <>
int32_t Sampler::sample<executorch::aten::BFloat16>(
executorch::aten::BFloat16* logits);
Expand Down
34 changes: 34 additions & 0 deletions extension/llm/sampler/test/test_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <executorch/extension/llm/sampler/sampler.h>

#include <set>
#include <utility>
#include <vector>

#include <gtest/gtest.h>
Expand Down Expand Up @@ -69,6 +70,39 @@ TEST(SamplerTest, TestBFloat16SamplingUsesFloatArithmetic) {
float_sampler.sample(float_logits.data()));
}

class HalfSamplingTest : public TestWithParam<std::pair<float, int>> {};

TEST_P(HalfSamplingTest, MatchesFloatWithLargeVocabulary) {
constexpr int kVocabSize = 32768;
const auto [topp, topk] = GetParam();
Sampler half_sampler{kVocabSize, 0.7f, topp, 42};
Sampler float_sampler{kVocabSize, 0.7f, topp, 42};
half_sampler.set_topk(topk);
float_sampler.set_topk(topk);

std::vector<executorch::aten::Half> half_logits(kVocabSize);
std::vector<float> float_logits(kVocabSize);
for (int trial = 0; trial < 16; ++trial) {
for (int i = 0; i < kVocabSize; ++i) {
half_logits[i] = executorch::aten::Half(
trial % 2 == 0 ? 0.0f : (i % 1024) * 0.001f - 0.512f);
float_logits[i] = static_cast<float>(half_logits[i]);
}
EXPECT_EQ(
half_sampler.sample(half_logits.data()),
float_sampler.sample(float_logits.data()))
<< "trial " << trial;
}
}

INSTANTIATE_TEST_SUITE_P(
Sampler,
HalfSamplingTest,
Values(
std::make_pair(1.0f, 0),
std::make_pair(0.9f, 0),
std::make_pair(1.0f, 4096)));

TEST(SamplerTest, TestTopKRestrictsToCandidates) {
// With topk=3, sampling must always return one of the top-3 indices,
// regardless of the random draw.
Expand Down
Loading