moved imports to top
Browse files- loaders.py +40 -39
loaders.py
CHANGED
@@ -1,40 +1,41 @@
|
|
1 |
-
import torch
|
2 |
-
from transformers import HubertModel, HubertConfig
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
return features
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import HubertModel, HubertConfig
|
3 |
+
import safetensors.torch as st
|
4 |
+
from safetensors import safe_open
|
5 |
+
import json
|
6 |
+
|
7 |
+
class HubertModelWithFinalProj(HubertModel):
|
8 |
+
def __init__(self, config):
|
9 |
+
super().__init__(config)
|
10 |
+
|
11 |
+
# The final projection layer is only used for backward compatibility.
|
12 |
+
# Following https://github.com/auspicious3000/contentvec/issues/6
|
13 |
+
# Remove this layer is necessary to achieve the desired outcome.
|
14 |
+
self.final_proj = torch.nn.Linear(config.hidden_size, config.classifier_proj_size)
|
15 |
+
|
16 |
+
@staticmethod
|
17 |
+
def load_safetensors(path: str, device="cpu"):
|
18 |
+
assert path.endswith(".safetensors"), f"{path} must end with '.safetensors'"
|
19 |
+
|
20 |
+
with safe_open(path, framework="pt", device="cpu") as f:
|
21 |
+
metadata = f.metadata()
|
22 |
+
state_dict = {}
|
23 |
+
for key in f.keys():
|
24 |
+
state_dict[key] = f.get_tensor(key)
|
25 |
+
model = HubertModelWithFinalProj(HubertConfig.from_dict(json.loads(metadata["config"])))
|
26 |
+
model.load_state_dict(state_dict=state_dict)
|
27 |
+
return model.to(device)
|
28 |
+
|
29 |
+
def save_safetensors(self, path: str):
|
30 |
+
assert path.endswith(".safetensors"), f"{path} must end with '.safetensors'"
|
31 |
+
|
32 |
+
with open(path,"wb") as f:
|
33 |
+
state_dict = self.state_dict()
|
34 |
+
f.write(st.save(state_dict,dict(config=json.dumps(self.config.to_dict()))))
|
35 |
+
|
36 |
+
def extract_features(self, source: torch.Tensor, version="v2", **kwargs):
|
37 |
+
with torch.no_grad():
|
38 |
+
output_layer = 9 if version == "v1" else 12
|
39 |
+
output = self(source.to(self.config.torch_dtype), output_hidden_states=True)["hidden_states"][output_layer]
|
40 |
+
features = self.final_proj(output) if version == "v1" else output
|
41 |
return features
|