Skip to content

Commit

Permalink
Add set seed function
Browse files Browse the repository at this point in the history
  • Loading branch information
neubig committed Mar 4, 2024
1 parent db4f3a8 commit e4d0e6b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions llments/lm/base/empirical.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def calculate_probability(self, x: str) -> float:
# Implementation logic
raise NotImplementedError("This is not implemented yet.")

def set_seed(self, seed: int):
random.seed(seed)


def load_from_text_file(text_file: str):
"""Load the distribution from a text file."""
Expand Down
10 changes: 9 additions & 1 deletion llments/lm/base/hugging_face.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from llments.lm.lm import LanguageModel
from transformers import pipeline
from transformers import pipeline, set_seed


class HuggingFaceLM(LanguageModel):
Expand Down Expand Up @@ -64,6 +64,14 @@ def generate(
)
return [res["generated_text"] for res in results]

def set_seed(self, seed: int):
"""Set the seed for the language model.
Args:
seed: The seed to set for the language model.
"""
set_seed(seed)


def load_from_spec(spec_file: str) -> HuggingFaceLM:
"""Load a language model from a specification file.
Expand Down
9 changes: 9 additions & 0 deletions llments/lm/lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ def generate(
str: Sampled output sequences from the language model.
"""
...

@abc.abstractmethod
def set_seed(self, seed: int):
"""Set the seed for the language model.
Args:
seed: The seed to set for the language model.
"""
...

0 comments on commit e4d0e6b

Please sign in to comment.