-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor weights loading without using pickled files - vgg, resnet (#21)
- Loading branch information
Showing
18 changed files
with
546 additions
and
697 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 |
---|---|---|
@@ -1 +1 @@ | ||
from .match_weights_helpers import * | ||
from .weights_helpers import * |
This file was deleted.
Oops, something went wrong.
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,69 @@ | ||
# global | ||
import ivy | ||
|
||
|
||
def _prune_keys(raw, ref, raw_keys_to_prune=[], ref_keys_to_prune=[]): | ||
if raw_keys_to_prune != []: | ||
for kc in raw_keys_to_prune: | ||
raw = raw.cont_prune_key_from_key_chains(containing=kc) | ||
if ref_keys_to_prune != []: | ||
for kc in ref_keys_to_prune: | ||
ref = ref.cont_prune_key_from_key_chains(containing=kc) | ||
return raw, ref | ||
|
||
|
||
def _map_weights(raw, ref, custom_mapping=None): | ||
mapping = {} | ||
for old_key, new_key in zip( | ||
raw.cont_sort_by_key().cont_to_iterator_keys(), | ||
ref.cont_sort_by_key().cont_to_iterator_keys(), | ||
): | ||
new_mapping = new_key | ||
if custom_mapping is not None: | ||
new_mapping = custom_mapping(old_key, new_key) | ||
if new_mapping is None: | ||
continue | ||
mapping[old_key] = new_mapping | ||
return mapping | ||
|
||
|
||
def load_torch_weights(url, ref_model, custom_mapping=None): | ||
import torch | ||
|
||
ivy.set_backend("torch") | ||
weights = torch.hub.load_state_dict_from_url(url) | ||
weights_raw = ivy.to_numpy(ivy.Container(weights)) | ||
mapping = _map_weights(weights_raw, ref_model.v, custom_mapping=custom_mapping) | ||
|
||
ivy.previous_backend() | ||
w_clean = weights_raw.cont_restructure(mapping, keep_orig=False) | ||
return ivy.asarray(w_clean) | ||
|
||
|
||
def load_jax_weights( | ||
url, ref_model, custom_mapping=None, raw_keys_to_prune=[], ref_keys_to_prune=[] | ||
): | ||
import urllib.request | ||
import os | ||
import pickle | ||
|
||
ivy.set_backend("jax") | ||
urllib.request.urlretrieve(url, filename="jax_weights.pystate") | ||
with open("jax_weights.pystate", "rb") as f: | ||
weights = pickle.loads(f.read()) | ||
os.remove("jax_weights.pystate") | ||
|
||
try: | ||
weights = {**weights["params"], **weights["state"]} | ||
except KeyError: | ||
pass | ||
|
||
weights_raw = ivy.to_numpy(ivy.Container(weights)) | ||
weights_raw, weights_ref = _prune_keys( | ||
weights_raw, ref_model.v, raw_keys_to_prune, ref_keys_to_prune | ||
) | ||
mapping = _map_weights(weights_raw, weights_ref, custom_mapping=custom_mapping) | ||
|
||
ivy.previous_backend() | ||
w_clean = weights_raw.cont_restructure(mapping, keep_orig=False) | ||
return ivy.asarray(w_clean) |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.