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

Add a basic sampling test #1235

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 3 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
17 changes: 17 additions & 0 deletions MaxText/tests/maxengine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import unittest
import pyconfig
import max_utils
import inference_utils
from maxengine import MaxEngine
from layers import quantizations
from layers import models
Expand Down Expand Up @@ -121,6 +122,22 @@ def test_basic_prefill(self):
self.assertNotEqual(prefill_result["tokens"], jnp.array([0]))
self.assertTrue(jnp.array_equal(first_token.data.size, 3))

def test_sampling(self):
selected_logits = jnp.array([[[-3.46875, -4.90625, 4.125, 4.0625, -2.6875, 1.1953125, 8.2, 0.345, 0.0]]])
rng = jax.random.PRNGKey(0)
# all results should be the same in this configuration, this is only for functionality test
greedy_logit = inference_utils.sampling(selected_logits, rng, "greedy")
self.assertEqual(selected_logits[0, 0, greedy_logit[0, 0]], 8.2)

weighted_logits = inference_utils.sampling(selected_logits, rng, "weighted", temperature=2)
self.assertEqual(selected_logits[0, 0, weighted_logits[0, 0]], 8.2)

nucleus_logits = inference_utils.sampling(selected_logits, rng, "nucleus", nucleus_topp=3)
self.assertEqual(selected_logits[0, 0, nucleus_logits[0, 0]], 8.2)

topk_logits = inference_utils.sampling(selected_logits, rng, "topk", topk=3)
self.assertEqual(selected_logits[0, 0, topk_logits[0, 0]], 8.2)


if __name__ == "__main__":
unittest.main()