-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve outlines.processors, add integration tests to test_generate.py
- Loading branch information
Showing
7 changed files
with
315 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import mlx.core as mx | ||
import numpy as np | ||
import torch | ||
|
||
from outlines.processors import OutlinesLogitsProcessor | ||
|
||
|
||
class HalvingLogitsProcessor(OutlinesLogitsProcessor): | ||
"""Simply halve the passed logits""" | ||
|
||
def process_logits(self, input_ids, logits): | ||
return logits / 2 | ||
|
||
|
||
class LogitsProcessorBenchmark: | ||
params = ["torch", "numpy"] | ||
if mx.metal.is_available(): | ||
params += ["mlx"] | ||
|
||
def setup(self, array_library): | ||
self.logits_processor = HalvingLogitsProcessor() | ||
|
||
# logits: (4, 30,000 ) dtype=float | ||
# input_ids shape: (4, 2048) dtype=int | ||
if array_library == "torch": | ||
self.logits = torch.rand((4, 30000), dtype=torch.float) | ||
self.input_ids = torch.randint( | ||
low=0, high=30000, size=(4, 2048), dtype=torch.int | ||
) | ||
elif array_library == "numpy": | ||
self.logits = np.random.rand(4, 30000).astype(np.float32) | ||
self.input_ids = np.random.randint(low=0, high=30000, size=(4, 2048)) | ||
elif array_library == "mlx": | ||
self.logits = mx.random.uniform( | ||
low=-1e9, high=1e9, shape=(4, 30000), dtype=mx.float32 | ||
) | ||
self.input_ids = mx.random.randint( | ||
low=0, high=30000, shape=(4, 2048), dtype=mx.int32 | ||
) | ||
else: | ||
raise ValueError | ||
|
||
def time_logits_processor(self, array_library): | ||
self.logits_processor(self.input_ids, self.logits) |
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from .structured import ( | ||
BaseLogitsProcessor, | ||
CFGLogitsProcessor, | ||
FSMLogitsProcessor, | ||
JSONLogitsProcessor, | ||
OutlinesLogitsProcessor, | ||
RegexLogitsProcessor, | ||
) |
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
Oops, something went wrong.