-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
50 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import string | ||
import re | ||
|
||
import mlx.core as mx | ||
|
||
class LogitFilter: | ||
def __init__(self, | ||
tokenizer, | ||
output_size: int | ||
): | ||
self.tokenizer = tokenizer | ||
self.output_size = output_size | ||
|
||
def reset(self): | ||
pass | ||
|
||
def __call__(self, | ||
logits: mx.array | ||
) -> mx.array: | ||
raise NotImplementedError("Class structure.StructureEnforcer is used for inheritance only") | ||
|
||
class ASCIIFilter(LogitFilter): | ||
""" | ||
Static logit mask filtering out tokens with non-ASCII printable characters. | ||
""" | ||
def __init__(self, | ||
tokenizer, | ||
output_size: int | ||
): | ||
mask = mx.zeros(output_size) | ||
for i, s in enumerate(tokenizer.vocab_strings): | ||
if all(c in string.printable for c in s.strip()): | ||
mask[i] = 1.0 | ||
|
||
for i in tokenizer.special_ids: | ||
mask[i] = 1.0 | ||
|
||
self.mask = mask | ||
|
||
def __call__(self, | ||
logits: mx.array | ||
) -> mx.array: | ||
return logits * self.mask |