Skip to content

Commit

Permalink
Fixed range validation for text generation penalty parameters (#3623)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgaddair authored Sep 16, 2023
1 parent 02ffb06 commit 42723e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
18 changes: 5 additions & 13 deletions ludwig/schema/llms/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,39 +165,31 @@ class LLMGenerationConfig(schema_utils.BaseMarshmallowConfig):
"depending on the size of the model.",
)

diversity_penalty: Optional[float] = schema_utils.FloatRange(
diversity_penalty: Optional[float] = schema_utils.NonNegativeFloat(
default=0.0,
min=0.0,
max=1.0,
allow_none=True,
description="The value used to control the diversity of the generated text. The higher the value, the more "
"diverse the text will be. The value should be between 0 and 1.0. If set to 0, no diversity is enforced."
"diverse the text will be. If set to 0, no diversity is enforced."
"This value is subtracted from a beam(s) score if it generates a token same as any beam from other group at a"
"particular time. Note that diversity_penalty is only effective if group beam search is enabled.",
)

repetition_penalty: Optional[float] = schema_utils.FloatRange(
repetition_penalty: Optional[float] = schema_utils.NonNegativeFloat(
default=1.0,
min=0.0,
max=1.0,
allow_none=True,
description="The parameter for repetition penalty. 1.0 means no penalty. "
"See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details.",
)

encoder_repetition_penalty: Optional[float] = schema_utils.FloatRange(
encoder_repetition_penalty: Optional[float] = schema_utils.NonNegativeFloat(
default=1.0,
min=0.0,
max=1.0,
allow_none=True,
description="The paramater for encoder_repetition_penalty. An exponential penalty on sequences that are not"
" in the original input. 1.0 means no penalty.",
)

length_penalty: Optional[float] = schema_utils.FloatRange(
length_penalty: Optional[float] = schema_utils.Float(
default=1.0,
min=0.0,
max=1.0,
allow_none=True,
description="Exponential penalty to the length that is used with beam-based generation. It is applied as an "
"exponent to the sequence length, which in turn is used to divide the score of the sequence. Since the score is"
Expand Down
29 changes: 29 additions & 0 deletions ludwig/schema/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,35 @@ def IntegerRange(
)


@DeveloperAPI
def Float(
default: Union[None, int],
allow_none=False,
description="",
parameter_metadata: ParameterMetadata = None,
):
"""Returns a dataclass field with marshmallow metadata strictly enforcing float inputs."""
if default is not None:
try:
assert isinstance(default, float) or isinstance(default, int)
except Exception:
raise ValidationError(f"Invalid default: `{default}`")
return field(
metadata={
"marshmallow_field": fields.Float(
allow_none=allow_none,
load_default=default,
dump_default=default,
metadata={
"description": description,
"parameter_metadata": convert_metadata_to_json(parameter_metadata),
},
)
},
default=default,
)


@DeveloperAPI
def NonNegativeFloat(
default: Union[None, float],
Expand Down

0 comments on commit 42723e3

Please sign in to comment.