Commit ·
c3158a0
1
Parent(s): c69a5b6
add
Browse files- modeling_moss_tts_nano.py +72 -35
modeling_moss_tts_nano.py
CHANGED
|
@@ -1026,23 +1026,47 @@ class MossTTSNanoForCausalLM(MossTTSNanoPreTrainedModel):
|
|
| 1026 |
return True
|
| 1027 |
return False
|
| 1028 |
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1033 |
if not candidate_path.exists():
|
| 1034 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1035 |
if candidate_path.is_dir():
|
| 1036 |
if (candidate_path / "tokenizer.model").is_file():
|
| 1037 |
-
return candidate_path
|
| 1038 |
if self._looks_like_hf_tokenizer_dir(candidate_path):
|
| 1039 |
-
return candidate_path
|
| 1040 |
hf_dir = candidate_path / "hf_tokenizer"
|
| 1041 |
if self._looks_like_hf_tokenizer_dir(hf_dir):
|
| 1042 |
-
return hf_dir
|
| 1043 |
sentencepiece_model = candidate_path / "sentencepiece" / "mossttsnano_spm_bpe.model"
|
| 1044 |
if sentencepiece_model.is_file():
|
| 1045 |
-
return sentencepiece_model
|
| 1046 |
final_summary_path = candidate_path / "final_summary.json"
|
| 1047 |
if final_summary_path.is_file():
|
| 1048 |
final_summary = json.loads(final_summary_path.read_text(encoding="utf-8"))
|
|
@@ -1050,26 +1074,33 @@ class MossTTSNanoForCausalLM(MossTTSNanoPreTrainedModel):
|
|
| 1050 |
if latest_hf_dir:
|
| 1051 |
latest_hf_path = Path(str(latest_hf_dir))
|
| 1052 |
if self._looks_like_hf_tokenizer_dir(latest_hf_path):
|
| 1053 |
-
return latest_hf_path
|
| 1054 |
raise ValueError(
|
| 1055 |
"Could not resolve a tokenizer from the provided path. Expected a tokenizer dir, experiment dir, or SentencePiece .model file."
|
| 1056 |
)
|
| 1057 |
|
| 1058 |
-
def _load_resolved_text_tokenizer(self, resolved_path:
|
| 1059 |
-
|
| 1060 |
-
|
|
|
|
|
|
|
| 1061 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1062 |
return AutoTokenizer.from_pretrained(
|
| 1063 |
-
|
| 1064 |
-
|
| 1065 |
-
use_fast=bool(self.config.tokenizer_use_fast),
|
| 1066 |
-
local_files_only=True,
|
| 1067 |
-
cache_dir=cache_dir,
|
| 1068 |
)
|
| 1069 |
except Exception:
|
| 1070 |
-
|
| 1071 |
-
|
| 1072 |
-
|
|
|
|
| 1073 |
raise
|
| 1074 |
|
| 1075 |
@staticmethod
|
|
@@ -1087,33 +1118,39 @@ class MossTTSNanoForCausalLM(MossTTSNanoPreTrainedModel):
|
|
| 1087 |
os.environ["HF_MODULES_CACHE"] = modules_cache_dir
|
| 1088 |
dynamic_module_utils.HF_MODULES_CACHE = modules_cache_dir
|
| 1089 |
|
| 1090 |
-
def _resolve_default_text_tokenizer_path(self) ->
|
| 1091 |
-
candidates: list[Path] = []
|
| 1092 |
|
| 1093 |
raw_name_or_path = getattr(self.config, "_name_or_path", None)
|
| 1094 |
if raw_name_or_path:
|
| 1095 |
-
candidates.append(
|
| 1096 |
|
| 1097 |
raw_model_name_or_path = getattr(self, "name_or_path", None)
|
| 1098 |
if raw_model_name_or_path:
|
| 1099 |
-
candidates.append(
|
| 1100 |
|
| 1101 |
candidates.append(Path(__file__).resolve().parent)
|
| 1102 |
|
| 1103 |
checked: set[str] = set()
|
| 1104 |
for candidate in candidates:
|
| 1105 |
-
|
| 1106 |
-
|
| 1107 |
-
|
|
|
|
|
|
|
|
|
|
| 1108 |
continue
|
| 1109 |
-
checked
|
|
|
|
|
|
|
|
|
|
| 1110 |
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
if
|
| 1114 |
-
return
|
| 1115 |
|
| 1116 |
-
return
|
| 1117 |
|
| 1118 |
def _load_text_tokenizer(self, text_tokenizer=None, text_tokenizer_path: Optional[str] = None):
|
| 1119 |
if text_tokenizer is not None:
|
|
@@ -1124,7 +1161,7 @@ class MossTTSNanoForCausalLM(MossTTSNanoPreTrainedModel):
|
|
| 1124 |
if text_tokenizer_path is not None
|
| 1125 |
else self._resolve_default_text_tokenizer_path()
|
| 1126 |
)
|
| 1127 |
-
normalized_path = str(resolved_path
|
| 1128 |
cached = getattr(self, "_cached_text_tokenizer", None)
|
| 1129 |
cached_path = getattr(self, "_cached_text_tokenizer_path", None)
|
| 1130 |
if cached is not None and cached_path == normalized_path:
|
|
|
|
| 1026 |
return True
|
| 1027 |
return False
|
| 1028 |
|
| 1029 |
+
@staticmethod
|
| 1030 |
+
def _looks_like_hf_repo_id(candidate: str) -> bool:
|
| 1031 |
+
stripped = candidate.strip()
|
| 1032 |
+
if not stripped:
|
| 1033 |
+
return False
|
| 1034 |
+
if stripped.startswith((os.sep, ".", "~")):
|
| 1035 |
+
return False
|
| 1036 |
+
if "\\" in stripped:
|
| 1037 |
+
return False
|
| 1038 |
+
parts = stripped.split("/")
|
| 1039 |
+
return len(parts) == 2 and all(part.strip() for part in parts)
|
| 1040 |
+
|
| 1041 |
+
@staticmethod
|
| 1042 |
+
def _existing_local_path(raw_path: Union[str, Path]) -> Optional[Path]:
|
| 1043 |
+
candidate_path = Path(raw_path).expanduser()
|
| 1044 |
if not candidate_path.exists():
|
| 1045 |
+
return None
|
| 1046 |
+
return candidate_path.resolve()
|
| 1047 |
+
|
| 1048 |
+
def _resolve_text_tokenizer_path(self, raw_path: Union[str, Path]) -> str:
|
| 1049 |
+
local_candidate_path = self._existing_local_path(raw_path)
|
| 1050 |
+
if local_candidate_path is None:
|
| 1051 |
+
raw_source = str(raw_path).strip()
|
| 1052 |
+
if self._looks_like_hf_repo_id(raw_source):
|
| 1053 |
+
return raw_source
|
| 1054 |
+
raise FileNotFoundError(f"Tokenizer path does not exist: {raw_source}")
|
| 1055 |
+
|
| 1056 |
+
candidate_path = local_candidate_path
|
| 1057 |
+
if candidate_path.is_file() and candidate_path.suffix == ".model":
|
| 1058 |
+
return str(candidate_path)
|
| 1059 |
if candidate_path.is_dir():
|
| 1060 |
if (candidate_path / "tokenizer.model").is_file():
|
| 1061 |
+
return str(candidate_path)
|
| 1062 |
if self._looks_like_hf_tokenizer_dir(candidate_path):
|
| 1063 |
+
return str(candidate_path)
|
| 1064 |
hf_dir = candidate_path / "hf_tokenizer"
|
| 1065 |
if self._looks_like_hf_tokenizer_dir(hf_dir):
|
| 1066 |
+
return str(hf_dir)
|
| 1067 |
sentencepiece_model = candidate_path / "sentencepiece" / "mossttsnano_spm_bpe.model"
|
| 1068 |
if sentencepiece_model.is_file():
|
| 1069 |
+
return str(sentencepiece_model)
|
| 1070 |
final_summary_path = candidate_path / "final_summary.json"
|
| 1071 |
if final_summary_path.is_file():
|
| 1072 |
final_summary = json.loads(final_summary_path.read_text(encoding="utf-8"))
|
|
|
|
| 1074 |
if latest_hf_dir:
|
| 1075 |
latest_hf_path = Path(str(latest_hf_dir))
|
| 1076 |
if self._looks_like_hf_tokenizer_dir(latest_hf_path):
|
| 1077 |
+
return str(latest_hf_path.resolve())
|
| 1078 |
raise ValueError(
|
| 1079 |
"Could not resolve a tokenizer from the provided path. Expected a tokenizer dir, experiment dir, or SentencePiece .model file."
|
| 1080 |
)
|
| 1081 |
|
| 1082 |
+
def _load_resolved_text_tokenizer(self, resolved_path: str, cache_dir: str):
|
| 1083 |
+
local_path = self._existing_local_path(resolved_path)
|
| 1084 |
+
load_source = str(local_path) if local_path is not None else str(resolved_path)
|
| 1085 |
+
if local_path is not None and local_path.is_file() and local_path.suffix == ".model":
|
| 1086 |
+
return MossTTSNanoSentencePieceTokenizer(vocab_file=str(local_path))
|
| 1087 |
try:
|
| 1088 |
+
load_kwargs: dict[str, object] = {
|
| 1089 |
+
"trust_remote_code": True,
|
| 1090 |
+
"use_fast": bool(self.config.tokenizer_use_fast),
|
| 1091 |
+
"cache_dir": cache_dir,
|
| 1092 |
+
}
|
| 1093 |
+
if local_path is not None:
|
| 1094 |
+
load_kwargs["local_files_only"] = True
|
| 1095 |
return AutoTokenizer.from_pretrained(
|
| 1096 |
+
load_source,
|
| 1097 |
+
**load_kwargs,
|
|
|
|
|
|
|
|
|
|
| 1098 |
)
|
| 1099 |
except Exception:
|
| 1100 |
+
if local_path is not None:
|
| 1101 |
+
model_path = local_path / "tokenizer.model"
|
| 1102 |
+
if model_path.is_file():
|
| 1103 |
+
return MossTTSNanoSentencePieceTokenizer(vocab_file=str(model_path))
|
| 1104 |
raise
|
| 1105 |
|
| 1106 |
@staticmethod
|
|
|
|
| 1118 |
os.environ["HF_MODULES_CACHE"] = modules_cache_dir
|
| 1119 |
dynamic_module_utils.HF_MODULES_CACHE = modules_cache_dir
|
| 1120 |
|
| 1121 |
+
def _resolve_default_text_tokenizer_path(self) -> str:
|
| 1122 |
+
candidates: list[Union[str, Path]] = []
|
| 1123 |
|
| 1124 |
raw_name_or_path = getattr(self.config, "_name_or_path", None)
|
| 1125 |
if raw_name_or_path:
|
| 1126 |
+
candidates.append(str(raw_name_or_path).strip())
|
| 1127 |
|
| 1128 |
raw_model_name_or_path = getattr(self, "name_or_path", None)
|
| 1129 |
if raw_model_name_or_path:
|
| 1130 |
+
candidates.append(str(raw_model_name_or_path).strip())
|
| 1131 |
|
| 1132 |
candidates.append(Path(__file__).resolve().parent)
|
| 1133 |
|
| 1134 |
checked: set[str] = set()
|
| 1135 |
for candidate in candidates:
|
| 1136 |
+
raw_candidate = str(candidate).strip()
|
| 1137 |
+
if not raw_candidate:
|
| 1138 |
+
continue
|
| 1139 |
+
try:
|
| 1140 |
+
resolved_candidate = self._resolve_text_tokenizer_path(raw_candidate)
|
| 1141 |
+
except (FileNotFoundError, ValueError):
|
| 1142 |
continue
|
| 1143 |
+
if resolved_candidate in checked:
|
| 1144 |
+
continue
|
| 1145 |
+
checked.add(resolved_candidate)
|
| 1146 |
+
return resolved_candidate
|
| 1147 |
|
| 1148 |
+
for candidate in candidates:
|
| 1149 |
+
raw_candidate = str(candidate).strip()
|
| 1150 |
+
if raw_candidate:
|
| 1151 |
+
return raw_candidate
|
| 1152 |
|
| 1153 |
+
return str(Path(__file__).resolve().parent)
|
| 1154 |
|
| 1155 |
def _load_text_tokenizer(self, text_tokenizer=None, text_tokenizer_path: Optional[str] = None):
|
| 1156 |
if text_tokenizer is not None:
|
|
|
|
| 1161 |
if text_tokenizer_path is not None
|
| 1162 |
else self._resolve_default_text_tokenizer_path()
|
| 1163 |
)
|
| 1164 |
+
normalized_path = str(resolved_path)
|
| 1165 |
cached = getattr(self, "_cached_text_tokenizer", None)
|
| 1166 |
cached_path = getattr(self, "_cached_text_tokenizer_path", None)
|
| 1167 |
if cached is not None and cached_path == normalized_path:
|