-
Notifications
You must be signed in to change notification settings - Fork 33
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
13 changed files
with
1,705 additions
and
493 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
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,92 @@ | ||
import os | ||
import tempfile | ||
import time | ||
|
||
import torch | ||
from transformers import AutoConfig, AutoModelForCausalLM | ||
|
||
from tensorizer import ( | ||
DecryptionParams, | ||
EncryptionParams, | ||
TensorDeserializer, | ||
TensorSerializer, | ||
) | ||
from tensorizer.utils import no_init_or_tensor | ||
|
||
model_ref = "EleutherAI/gpt-neo-2.7B" | ||
|
||
|
||
def original_model(ref) -> torch.nn.Module: | ||
return AutoModelForCausalLM.from_pretrained(ref) | ||
|
||
|
||
def empty_model(ref) -> torch.nn.Module: | ||
config = AutoConfig.from_pretrained(ref) | ||
with no_init_or_tensor(): | ||
return AutoModelForCausalLM.from_config(config) | ||
|
||
|
||
# Set a strong string or bytes passphrase here | ||
passphrase: str = os.getenv("SUPER_SECRET_STRONG_PASSWORD", "") or input( | ||
"Passphrase to use for encryption: " | ||
) | ||
|
||
fd, path = tempfile.mkstemp(prefix="encrypted-tensors") | ||
|
||
try: | ||
# Encrypt a model during serialization | ||
encryption_params = EncryptionParams.from_passphrase_fast(passphrase) | ||
|
||
model = original_model(model_ref) | ||
serialization_start = time.monotonic() | ||
|
||
serializer = TensorSerializer(path, encryption=encryption_params) | ||
serializer.write_module(model) | ||
serializer.close() | ||
|
||
serialization_end = time.monotonic() | ||
del model | ||
|
||
# Then decrypt it again during deserialization | ||
decryption_params = DecryptionParams.from_passphrase(passphrase) | ||
|
||
model = empty_model(model_ref) | ||
deserialization_start = time.monotonic() | ||
|
||
deserializer = TensorDeserializer( | ||
path, encryption=decryption_params, plaid_mode=True | ||
) | ||
deserializer.load_into_module(model) | ||
deserializer.close() | ||
|
||
deserialization_end = time.monotonic() | ||
del model | ||
finally: | ||
os.close(fd) | ||
os.unlink(path) | ||
|
||
|
||
def print_speed(prefix, start, end, size): | ||
mebibyte = 1 << 20 | ||
gibibyte = 1 << 30 | ||
duration = end - start | ||
rate = size / duration | ||
print( | ||
f"{prefix} {size / gibibyte:.2f} GiB model in {duration:.2f} seconds," | ||
f" {rate / mebibyte:.2f} MiB/s" | ||
) | ||
|
||
|
||
print_speed( | ||
"Serialized and encrypted", | ||
serialization_start, | ||
serialization_end, | ||
serializer.total_tensor_bytes, | ||
) | ||
|
||
print_speed( | ||
"Deserialized encrypted", | ||
deserialization_start, | ||
deserialization_end, | ||
deserializer.total_tensor_bytes, | ||
) |
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 |
---|---|---|
|
@@ -3,5 +3,6 @@ numpy>=1.19.5 | |
protobuf>=3.19.5 | ||
psutil>=5.9.4 | ||
boto3>=1.26.0 | ||
redis==5.0.0 | ||
hiredis | ||
redis==5.0.0 | ||
libnacl>=2.1.0 |
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.