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 from_config to sampler #803

Merged
merged 1 commit into from
Mar 3, 2023
Merged
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
4 changes: 4 additions & 0 deletions keras_nlp/samplers/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ def one_step(
)
return prompt

@classmethod
def from_config(cls, config):
return cls(**config)

def get_config(self):
return {
"jit_compile": self.jit_compile,
Expand Down
17 changes: 7 additions & 10 deletions keras_nlp/samplers/sampler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@

import keras_nlp
from keras_nlp.samplers.greedy_sampler import GreedySampler
from keras_nlp.samplers.top_k_sampler import TopKSampler


class SamplerTest(tf.test.TestCase):
def test_serialization(self):
sampler = keras_nlp.samplers.GreedySampler()
config = keras_nlp.samplers.serialize(sampler)
expected_config = {
"class_name": "keras_nlp>GreedySampler",
"config": {
"jit_compile": True,
},
}
self.assertDictEqual(expected_config, config)
sampler = TopKSampler(k=5)
restored = keras_nlp.samplers.deserialize(
keras_nlp.samplers.serialize(sampler)
)
self.assertDictEqual(sampler.get_config(), restored.get_config())

def test_deserialization(self):
def test_get(self):
# Test get from string.
identifier = "greedy"
sampler = keras_nlp.samplers.get(identifier)
Expand Down