Plana-Archive commited on
Commit
33663c4
·
verified ·
1 Parent(s): 7a9c412

Clean up previous raw files

Browse files
Files changed (1) hide show
  1. Zero-CPU/text/english_bert_mock.py +0 -63
Zero-CPU/text/english_bert_mock.py DELETED
@@ -1,63 +0,0 @@
1
- import sys
2
-
3
- import torch
4
- from transformers import DebertaV2Model, DebertaV2Tokenizer
5
-
6
- from config import config
7
-
8
-
9
- LOCAL_PATH = "./bert/deberta-v3-large"
10
-
11
- tokenizer = DebertaV2Tokenizer.from_pretrained(LOCAL_PATH)
12
-
13
- models = dict()
14
-
15
-
16
- def get_bert_feature(
17
- text,
18
- word2ph,
19
- device=config.bert_gen_config.device,
20
- assist_text=None,
21
- assist_text_weight=0.7,
22
- ):
23
- if (
24
- sys.platform == "darwin"
25
- and torch.backends.mps.is_available()
26
- and device == "cpu"
27
- ):
28
- device = "mps"
29
- if not device:
30
- device = "cuda"
31
- if device == "cuda" and not torch.cuda.is_available():
32
- device = "cpu"
33
- if device not in models.keys():
34
- models[device] = DebertaV2Model.from_pretrained(LOCAL_PATH).to(device)
35
- with torch.no_grad():
36
- inputs = tokenizer(text, return_tensors="pt")
37
- for i in inputs:
38
- inputs[i] = inputs[i].to(device)
39
- res = models[device](**inputs, output_hidden_states=True)
40
- res = torch.cat(res["hidden_states"][-3:-2], -1)[0].cpu()
41
- if assist_text:
42
- style_inputs = tokenizer(assist_text, return_tensors="pt")
43
- for i in style_inputs:
44
- style_inputs[i] = style_inputs[i].to(device)
45
- style_res = models[device](**style_inputs, output_hidden_states=True)
46
- style_res = torch.cat(style_res["hidden_states"][-3:-2], -1)[0].cpu()
47
- style_res_mean = style_res.mean(0)
48
- assert len(word2ph) == res.shape[0], (text, res.shape[0], len(word2ph))
49
- word2phone = word2ph
50
- phone_level_feature = []
51
- for i in range(len(word2phone)):
52
- if assist_text:
53
- repeat_feature = (
54
- res[i].repeat(word2phone[i], 1) * (1 - assist_text_weight)
55
- + style_res_mean.repeat(word2phone[i], 1) * assist_text_weight
56
- )
57
- else:
58
- repeat_feature = res[i].repeat(word2phone[i], 1)
59
- phone_level_feature.append(repeat_feature)
60
-
61
- phone_level_feature = torch.cat(phone_level_feature, dim=0)
62
-
63
- return phone_level_feature.T