-
Notifications
You must be signed in to change notification settings - Fork 164
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
fix: full gpu hybrid model #963
Merged
andrei-stoian-zama
merged 16 commits into
main
from
chore/optimize_mem_and_runtime_fhe_disable
Jan 6, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1ace32e
fix: full gpu hybrid model
andrei-stoian-zama 08a9038
fix: typing
andrei-stoian-zama 2a326f3
fix: refactor quantizer
andrei-stoian-zama 5fd4db8
fix: working torch quantizer
andrei-stoian-zama 8865156
chore: batch inputs to Lora, GPU optim
andrei-stoian-zama f00bfc2
fix: pcc
andrei-stoian-zama e32d9c2
fix: multiple
andrei-stoian-zama d436337
fix: pcc
andrei-stoian-zama c25f7a3
fix: ci
andrei-stoian-zama bb672fd
fix: ci
andrei-stoian-zama 3dce9c8
fix: ci
andrei-stoian-zama 5f7f78e
fix: ci
andrei-stoian-zama f2f6566
fix: bugs
andrei-stoian-zama 42b9721
fix: coverage
andrei-stoian-zama e2ac202
fix: tests
andrei-stoian-zama 4a63088
fix: makefile usecases
andrei-stoian-zama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
from typing import Any, Dict, Optional, TextIO, Union, get_type_hints | ||
|
||
import numpy | ||
import numpy.typing | ||
import torch | ||
from concrete.fhe.tracing.tracer import Tracer | ||
|
||
from ..common.debugging import assert_true | ||
|
@@ -671,11 +673,14 @@ def dump(self, file: TextIO) -> None: | |
""" | ||
dump(self, file) | ||
|
||
def quant(self, values: numpy.ndarray) -> numpy.ndarray: | ||
def quant( | ||
self, values: numpy.ndarray, dtype: numpy.typing.DTypeLike = numpy.int64 | ||
) -> numpy.ndarray: | ||
"""Quantize values. | ||
|
||
Args: | ||
values (numpy.ndarray): float values to quantize | ||
dtype (numpy.typing.DTypeLike): optional user-specified datatype for the output | ||
|
||
Returns: | ||
numpy.ndarray: Integer quantized values. | ||
|
@@ -686,6 +691,8 @@ def quant(self, values: numpy.ndarray) -> numpy.ndarray: | |
assert self.offset is not None | ||
assert self.scale is not None | ||
|
||
assert dtype in (numpy.int64, numpy.int32, numpy.float32, numpy.float64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe:
|
||
|
||
if QUANT_ROUND_LIKE_ROUND_PBS: | ||
qvalues = numpy.floor(values / self.scale + self.zero_point + 0.5) # pragma: no cover | ||
else: | ||
|
@@ -707,7 +714,9 @@ def quant(self, values: numpy.ndarray) -> numpy.ndarray: | |
|
||
qvalues = qvalues.clip(min_value, 2 ** (self.n_bits) - 1 - self.offset) | ||
|
||
return qvalues.astype(numpy.int64) | ||
qvalues = qvalues.astype(dtype) | ||
|
||
return qvalues | ||
|
||
def dequant(self, qvalues: numpy.ndarray) -> Union[float, numpy.ndarray, Tracer]: | ||
"""De-quantize values. | ||
|
@@ -737,6 +746,63 @@ def dequant(self, qvalues: numpy.ndarray) -> Union[float, numpy.ndarray, Tracer] | |
return values | ||
|
||
|
||
class TorchUniformQuantizer: | ||
"""Uniform quantizer with a PyTorch implementation. | ||
|
||
Contains all information necessary for uniform quantization and provides | ||
quantization/de-quantization functionality on torch tensors. | ||
|
||
Args: | ||
quantizer (UniformQuantizer): Underlying numpy quantizer containing all parameters | ||
""" | ||
|
||
_np_quant: UniformQuantizer | ||
|
||
def __init__(self, quantizer: UniformQuantizer): | ||
self._np_quant = quantizer | ||
|
||
def quant(self, values: torch.Tensor, dtype: Optional[torch.dtype] = None) -> torch.Tensor: | ||
"""Quantize values. | ||
|
||
Args: | ||
values (numpy.ndarray): float values to quantize | ||
dtype (Optional[torch.dtype]): optional user-specified datatype for the output | ||
|
||
Returns: | ||
numpy.ndarray: Integer quantized values. | ||
""" | ||
qvalues = torch.round(values / self._np_quant.scale + self._np_quant.zero_point) | ||
|
||
if not self._np_quant.no_clipping: | ||
assert self._np_quant.offset is not None | ||
min_value = -self._np_quant.offset | ||
if self._np_quant.is_narrow: | ||
min_value += 1 | ||
|
||
qvalues = torch.clip( | ||
qvalues, min_value, 2 ** (self._np_quant.n_bits) - 1 - self._np_quant.offset | ||
) | ||
|
||
if dtype is not None: | ||
qvalues = qvalues.type(dtype) | ||
|
||
return qvalues | ||
|
||
def dequant(self, qvalues: torch.Tensor) -> torch.Tensor: | ||
"""De-quantize values. | ||
|
||
Args: | ||
qvalues (numpy.ndarray): integer values to de-quantize | ||
|
||
Returns: | ||
Union[numpy.ndarray, Tracer]: De-quantized float values. | ||
""" | ||
zp_tensor = torch.tensor(self._np_quant.zero_point).type(qvalues.dtype).to(qvalues.device) | ||
|
||
values = self._np_quant.scale * (qvalues - zp_tensor) | ||
return values | ||
|
||
|
||
class QuantizedArray: | ||
"""Abstraction of quantized array. | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean
keep_onnx: Optional[bool] = None
? orkeep_onnx: bool = True