@@ -155,6 +155,83 @@ TEST_F(TextDecoderRunnerTest, LogitsToTokenWithTemperature) {
155155 EXPECT_LT (token, 4 );
156156}
157157
158+ // Test logits_to_token() with an injected Sampler (greedy, temp=0)
159+ TEST_F (TextDecoderRunnerTest, LogitsToTokenWithInjectedSampler) {
160+ TensorFactory<executorch::aten::ScalarType::Float> tf_float;
161+ auto logits = tf_float.make ({1 , 4 }, {0 .1f , 0 .2f , 0 .8f , 0 .4f });
162+
163+ auto sampler = std::make_unique<executorch::extension::llm::Sampler>(4 , 0 .0f );
164+ auto runner = std::make_unique<TextDecoderRunner>(
165+ nullptr , nullptr , " forward" , std::move (sampler));
166+
167+ int32_t token = runner->logits_to_token (logits, 0 .0f );
168+ EXPECT_EQ (token, 2 );
169+
170+ auto logits2 = tf_float.make ({1 , 4 }, {0 .1f , 0 .2f , 0 .8f , 0 .4f });
171+ token = runner->logits_to_token (logits2, 0 .0f );
172+ EXPECT_EQ (token, 2 );
173+ }
174+
175+ // Test that set_temperature works on an injected Sampler
176+ TEST_F (TextDecoderRunnerTest, LogitsToTokenInjectedSamplerTemperatureSwitch) {
177+ auto sampler = std::make_unique<executorch::extension::llm::Sampler>(4 , 0 .0f );
178+ auto runner = std::make_unique<TextDecoderRunner>(
179+ nullptr , nullptr , " forward" , std::move (sampler));
180+
181+ TensorFactory<executorch::aten::ScalarType::Float> tf_float;
182+
183+ // temp=0 → argmax
184+ auto logits1 = tf_float.make ({1 , 4 }, {0 .1f , 0 .2f , 0 .8f , 0 .4f });
185+ EXPECT_EQ (runner->logits_to_token (logits1, 0 .0f ), 2 );
186+
187+ // temp=1.0 → stochastic, result must be in valid range
188+ auto logits2 = tf_float.make ({1 , 4 }, {0 .1f , 0 .2f , 0 .8f , 0 .4f });
189+ int32_t token = runner->logits_to_token (logits2, 1 .0f );
190+ EXPECT_GE (token, 0 );
191+ EXPECT_LT (token, 4 );
192+
193+ // temp=0 again → back to argmax
194+ auto logits3 = tf_float.make ({1 , 4 }, {0 .1f , 0 .2f , 0 .8f , 0 .4f });
195+ EXPECT_EQ (runner->logits_to_token (logits3, 0 .0f ), 2 );
196+ }
197+
198+ // Test logits_to_token() with an injected Sampler on a 3D tensor
199+ TEST_F (TextDecoderRunnerTest, LogitsToTokenWithInjectedSampler3D) {
200+ TensorFactory<executorch::aten::ScalarType::Float> tf_float;
201+ auto logits = tf_float.make (
202+ {1 , 2 , 4 },
203+ {
204+ 0 .1f ,
205+ 0 .2f ,
206+ 0 .3f ,
207+ 0 .4f , // first position
208+ 0 .5f ,
209+ 0 .6f ,
210+ 0 .9f ,
211+ 0 .8f // last position (used for sampling)
212+ });
213+
214+ auto sampler = std::make_unique<executorch::extension::llm::Sampler>(4 , 0 .0f );
215+ auto runner = std::make_unique<TextDecoderRunner>(
216+ nullptr , nullptr , " forward" , std::move (sampler));
217+
218+ int32_t token = runner->logits_to_token (logits, 0 .0f );
219+ EXPECT_EQ (token, 2 );
220+ }
221+
222+ // Test logits_to_token() without an injected Sampler (fallback path)
223+ TEST_F (TextDecoderRunnerTest, LogitsToTokenWithoutInjectedSampler) {
224+ TensorFactory<executorch::aten::ScalarType::Float> tf_float;
225+ auto logits = tf_float.make ({1 , 4 }, {0 .1f , 0 .2f , 0 .8f , 0 .4f });
226+
227+ // No sampler injected — uses the legacy per-call Sampler path
228+ auto runner =
229+ std::make_unique<TextDecoderRunner>(nullptr , nullptr , " forward" );
230+
231+ int32_t token = runner->logits_to_token (logits, 0 .0f );
232+ EXPECT_EQ (token, 2 ); // greedy argmax → index 2 (value 0.8)
233+ }
234+
158235// Test step() method with all available PTE models
159236TEST_F (TextDecoderRunnerTest, StepWithAllModels) {
160237 // List of all environment variables for PTE models
0 commit comments