generated from alvarobartt/python-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #3 from alvarobartt/more-tests
🧪 Add more unit test cases e.g. ResNet FrozenDict
- Loading branch information
Showing
13 changed files
with
198 additions
and
57 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,27 @@ | ||
import sys | ||
|
||
import jax | ||
from flax.serialization import to_bytes | ||
from flaxmodels.resnet import ResNet50 | ||
from jax import numpy as jnp | ||
|
||
from safejax.flax import serialize | ||
|
||
resnet50 = ResNet50() | ||
params = resnet50.init(jax.random.PRNGKey(42), jnp.ones((1, 224, 224, 3))) | ||
|
||
|
||
def serialization_safejax(): | ||
_ = serialize(params) | ||
|
||
|
||
def serialization_flax(): | ||
_ = to_bytes(params) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) < 2: | ||
raise ValueError("Please provide a function name to run as an argument") | ||
if sys.argv[1] not in globals(): | ||
raise ValueError(f"Function {sys.argv[1]} not found") | ||
globals()[sys.argv[1]]() |
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,24 @@ | ||
from time import perf_counter | ||
|
||
import jax | ||
from flax.serialization import to_bytes | ||
from flaxmodels.resnet import ResNet50 | ||
from jax import numpy as jnp | ||
|
||
from safejax.flax import serialize | ||
|
||
resnet50 = ResNet50() | ||
params = resnet50.init(jax.random.PRNGKey(42), jnp.ones((1, 224, 224, 3))) | ||
|
||
|
||
start_time = perf_counter() | ||
for _ in range(100): | ||
serialize(params) | ||
end_time = perf_counter() | ||
print(f"safejax (100 runs): {end_time - start_time:0.4f} s") | ||
|
||
start_time = perf_counter() | ||
for _ in range(100): | ||
to_bytes(params) | ||
end_time = perf_counter() | ||
print(f"flax (100 runs): {end_time - start_time:0.4f} s") |
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,36 @@ | ||
from time import perf_counter | ||
|
||
import jax | ||
from flax import linen as nn | ||
from flax.serialization import to_bytes | ||
from jax import numpy as jnp | ||
|
||
from safejax.flax import serialize | ||
|
||
|
||
class SingleLayerModel(nn.Module): | ||
features: int | ||
|
||
@nn.compact | ||
def __call__(self, x): | ||
x = nn.Dense(features=self.features)(x) | ||
return x | ||
|
||
|
||
model = SingleLayerModel(features=1) | ||
|
||
rng = jax.random.PRNGKey(0) | ||
params = model.init(rng, jnp.ones((1, 1))) | ||
|
||
|
||
start_time = perf_counter() | ||
for _ in range(100): | ||
serialize(params) | ||
end_time = perf_counter() | ||
print(f"safejax (100 runs): {end_time - start_time:0.4f} s") | ||
|
||
start_time = perf_counter() | ||
for _ in range(100): | ||
to_bytes(params) | ||
end_time = perf_counter() | ||
print(f"flax (100 runs): {end_time - start_time:0.4f} s") |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""`safejax `: Serialize JAX/Flax models with `safetensors`""" | ||
|
||
__author__ = "Alvaro Bartolome <alvarobartt@yahoo.com>" | ||
__version__ = "0.1.0" | ||
__version__ = "0.1.1" |
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.