Upload scripts/train_tokenizer.py with huggingface_hub
Browse files- scripts/train_tokenizer.py +4 -84
scripts/train_tokenizer.py
CHANGED
|
@@ -141,18 +141,11 @@ def parse_args():
|
|
| 141 |
help="Directory to save checkpoints",
|
| 142 |
)
|
| 143 |
|
| 144 |
-
parser.add_argument(
|
| 145 |
-
"--max_checkpoints",
|
| 146 |
-
type=int,
|
| 147 |
-
default=5,
|
| 148 |
-
help="Maximum number of checkpoints to keep (0 = unlimited)",
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
parser.add_argument(
|
| 152 |
"--save_compressed",
|
| 153 |
action="store_true",
|
| 154 |
default=True,
|
| 155 |
-
help="Save tokenizer files in compressed format (.gz
|
| 156 |
)
|
| 157 |
|
| 158 |
parser.add_argument(
|
|
@@ -162,45 +155,6 @@ def parse_args():
|
|
| 162 |
help="Disable compression",
|
| 163 |
)
|
| 164 |
|
| 165 |
-
parser.add_argument(
|
| 166 |
-
"--compression_format",
|
| 167 |
-
type=str,
|
| 168 |
-
choices=['gzip', 'bz2', 'lzma'],
|
| 169 |
-
default='gzip',
|
| 170 |
-
help="Compression format to use",
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
parser.add_argument(
|
| 174 |
-
"--compression_level",
|
| 175 |
-
type=int,
|
| 176 |
-
choices=range(1, 10),
|
| 177 |
-
default=9,
|
| 178 |
-
help="Compression level (1-9, higher = better compression but slower)",
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
parser.add_argument(
|
| 182 |
-
"--enable_quantization",
|
| 183 |
-
action="store_true",
|
| 184 |
-
default=False,
|
| 185 |
-
help="Enable model quantization for deployment",
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
-
parser.add_argument(
|
| 189 |
-
"--quantization_bits",
|
| 190 |
-
type=int,
|
| 191 |
-
choices=[4, 8],
|
| 192 |
-
default=8,
|
| 193 |
-
help="Quantization bits (4 or 8)",
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
parser.add_argument(
|
| 197 |
-
"--export_formats",
|
| 198 |
-
type=str,
|
| 199 |
-
nargs='+',
|
| 200 |
-
default=['tokenizer.json', 'vocab_compressed', 'hf_export'],
|
| 201 |
-
help="Export formats to generate",
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
return parser.parse_args()
|
| 205 |
|
| 206 |
|
|
@@ -226,11 +180,6 @@ def main():
|
|
| 226 |
use_checkpoint=args.use_checkpoint,
|
| 227 |
checkpoint_dir=args.checkpoint_dir,
|
| 228 |
save_compressed=args.save_compressed,
|
| 229 |
-
compression_format=args.compression_format,
|
| 230 |
-
compression_level=args.compression_level,
|
| 231 |
-
enable_quantization=args.enable_quantization,
|
| 232 |
-
quantization_bits=args.quantization_bits,
|
| 233 |
-
max_checkpoints=args.max_checkpoints,
|
| 234 |
)
|
| 235 |
|
| 236 |
# Save config if requested
|
|
@@ -260,21 +209,7 @@ def main():
|
|
| 260 |
sys.exit(1)
|
| 261 |
|
| 262 |
# Save the tokenizer
|
| 263 |
-
save_path = trainer.save(
|
| 264 |
-
|
| 265 |
-
# List checkpoints
|
| 266 |
-
print("\n" + "="*60)
|
| 267 |
-
print("CHECKPOINTS")
|
| 268 |
-
print("="*60)
|
| 269 |
-
checkpoints = trainer.list_checkpoints()
|
| 270 |
-
if checkpoints:
|
| 271 |
-
for ckpt in checkpoints:
|
| 272 |
-
status = "✓" if trainer.validate_checkpoint(ckpt['path']) else "✗"
|
| 273 |
-
print(f"{status} {ckpt['name']} ({ckpt['size_kb']:.1f} KB)")
|
| 274 |
-
if 'vocab_size' in ckpt:
|
| 275 |
-
print(f" Vocab size: {ckpt['vocab_size']}, Final: {ckpt.get('is_final', False)}")
|
| 276 |
-
else:
|
| 277 |
-
print("No checkpoints found.")
|
| 278 |
|
| 279 |
# Test the tokenizer
|
| 280 |
print("\n" + "="*60)
|
|
@@ -302,24 +237,9 @@ def main():
|
|
| 302 |
print(f"Tokenizer training complete!")
|
| 303 |
print(f"Model saved to: {save_path}")
|
| 304 |
if args.save_compressed:
|
| 305 |
-
print(f"Compressed files saved (
|
| 306 |
-
if args.enable_quantization:
|
| 307 |
-
print(f"Quantized model saved ({args.quantization_bits}-bit)")
|
| 308 |
if args.use_checkpoint:
|
| 309 |
-
print(f"Checkpoints saved to: {args.checkpoint_dir}
|
| 310 |
-
|
| 311 |
-
# Print training metrics
|
| 312 |
-
print("\n" + "="*60)
|
| 313 |
-
print("TRAINING METRICS")
|
| 314 |
-
print("="*60)
|
| 315 |
-
metrics = trainer.get_training_metrics()
|
| 316 |
-
if 'initial' in metrics:
|
| 317 |
-
print(f"Files processed: {metrics['initial'].get('num_files', 'N/A')}")
|
| 318 |
-
print(f"Total data size: {metrics['initial'].get('total_bytes', 0) / 1024:.2f} KB")
|
| 319 |
-
if 'final' in metrics:
|
| 320 |
-
print(f"Final vocab size: {metrics['final'].get('vocab_size', 'N/A')}")
|
| 321 |
-
print(f"Training duration: {metrics['final'].get('training_duration_sec', 0):.2f} seconds")
|
| 322 |
-
|
| 323 |
print("="*60)
|
| 324 |
|
| 325 |
|
|
|
|
| 141 |
help="Directory to save checkpoints",
|
| 142 |
)
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
parser.add_argument(
|
| 145 |
"--save_compressed",
|
| 146 |
action="store_true",
|
| 147 |
default=True,
|
| 148 |
+
help="Save tokenizer files in compressed format (.gz)",
|
| 149 |
)
|
| 150 |
|
| 151 |
parser.add_argument(
|
|
|
|
| 155 |
help="Disable compression",
|
| 156 |
)
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
return parser.parse_args()
|
| 159 |
|
| 160 |
|
|
|
|
| 180 |
use_checkpoint=args.use_checkpoint,
|
| 181 |
checkpoint_dir=args.checkpoint_dir,
|
| 182 |
save_compressed=args.save_compressed,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
)
|
| 184 |
|
| 185 |
# Save config if requested
|
|
|
|
| 209 |
sys.exit(1)
|
| 210 |
|
| 211 |
# Save the tokenizer
|
| 212 |
+
save_path = trainer.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
# Test the tokenizer
|
| 215 |
print("\n" + "="*60)
|
|
|
|
| 237 |
print(f"Tokenizer training complete!")
|
| 238 |
print(f"Model saved to: {save_path}")
|
| 239 |
if args.save_compressed:
|
| 240 |
+
print(f"Compressed files also saved (look for .gz files)")
|
|
|
|
|
|
|
| 241 |
if args.use_checkpoint:
|
| 242 |
+
print(f"Checkpoints saved to: {args.checkpoint_dir}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
print("="*60)
|
| 244 |
|
| 245 |
|