🚀 Final optimization: Update cli_standards.py with production-ready enhancements
Browse files- bit_transformer/cli_standards.py +222 -0
bit_transformer/cli_standards.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
BitTransformerLM CLI Argument Standards
|
| 3 |
+
|
| 4 |
+
Unified command-line interface standards for all BitTransformerLM scripts.
|
| 5 |
+
This module provides standardized argument parsers and naming conventions.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import argparse
|
| 9 |
+
from typing import Optional, Callable
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class BitTransformerCLI:
|
| 13 |
+
"""Standardized CLI argument parser for BitTransformerLM."""
|
| 14 |
+
|
| 15 |
+
@staticmethod
|
| 16 |
+
def add_model_args(parser: argparse.ArgumentParser) -> None:
|
| 17 |
+
"""Add standard model configuration arguments."""
|
| 18 |
+
model_group = parser.add_argument_group('Model Configuration')
|
| 19 |
+
model_group.add_argument('--model-size', choices=['tiny', 'small', 'medium', 'large'],
|
| 20 |
+
default='small', help='Model size preset')
|
| 21 |
+
model_group.add_argument('--d-model', type=int, default=128,
|
| 22 |
+
help='Model dimension')
|
| 23 |
+
model_group.add_argument('--num-heads', type=int, default=8,
|
| 24 |
+
help='Number of attention heads')
|
| 25 |
+
model_group.add_argument('--num-layers', type=int, default=6,
|
| 26 |
+
help='Number of transformer layers')
|
| 27 |
+
model_group.add_argument('--dropout', type=float, default=0.1,
|
| 28 |
+
help='Dropout rate')
|
| 29 |
+
model_group.add_argument('--max-seq-len', type=int, default=512,
|
| 30 |
+
help='Maximum sequence length')
|
| 31 |
+
|
| 32 |
+
@staticmethod
|
| 33 |
+
def add_training_args(parser: argparse.ArgumentParser) -> None:
|
| 34 |
+
"""Add standard training arguments."""
|
| 35 |
+
train_group = parser.add_argument_group('Training Configuration')
|
| 36 |
+
train_group.add_argument('--epochs', type=int, default=10,
|
| 37 |
+
help='Number of training epochs')
|
| 38 |
+
train_group.add_argument('--batch-size', type=int, default=16,
|
| 39 |
+
help='Training batch size')
|
| 40 |
+
train_group.add_argument('--learning-rate', type=float, default=1e-3,
|
| 41 |
+
help='Learning rate')
|
| 42 |
+
train_group.add_argument('--weight-decay', type=float, default=0.01,
|
| 43 |
+
help='Weight decay')
|
| 44 |
+
train_group.add_argument('--grad-clip', type=float, default=1.0,
|
| 45 |
+
help='Gradient clipping threshold')
|
| 46 |
+
train_group.add_argument('--warmup-steps', type=int, default=100,
|
| 47 |
+
help='Number of warmup steps')
|
| 48 |
+
|
| 49 |
+
@staticmethod
|
| 50 |
+
def add_dataset_args(parser: argparse.ArgumentParser) -> None:
|
| 51 |
+
"""Add standard dataset arguments."""
|
| 52 |
+
data_group = parser.add_argument_group('Dataset Configuration')
|
| 53 |
+
data_group.add_argument('--dataset-name', type=str, default='synthetic',
|
| 54 |
+
help='Dataset name or path')
|
| 55 |
+
data_group.add_argument('--dataset-size', type=int, default=10000,
|
| 56 |
+
help='Dataset size (number of samples)')
|
| 57 |
+
data_group.add_argument('--seq-length', type=int, default=64,
|
| 58 |
+
help='Sequence length for training')
|
| 59 |
+
data_group.add_argument('--validation-split', type=float, default=0.1,
|
| 60 |
+
help='Validation split ratio')
|
| 61 |
+
|
| 62 |
+
@staticmethod
|
| 63 |
+
def add_safety_args(parser: argparse.ArgumentParser) -> None:
|
| 64 |
+
"""Add safety and telemetry arguments."""
|
| 65 |
+
safety_group = parser.add_argument_group('Safety & Telemetry')
|
| 66 |
+
safety_group.add_argument('--enable-safety-gates', action='store_true',
|
| 67 |
+
help='Enable safety gates during inference')
|
| 68 |
+
safety_group.add_argument('--min-negentropy', type=float, default=0.1,
|
| 69 |
+
help='Minimum negentropy threshold')
|
| 70 |
+
safety_group.add_argument('--max-complexity', type=float, default=0.9,
|
| 71 |
+
help='Maximum LZ complexity threshold')
|
| 72 |
+
safety_group.add_argument('--min-symbiosis', type=float, default=0.3,
|
| 73 |
+
help='Minimum symbiosis score threshold')
|
| 74 |
+
safety_group.add_argument('--telemetry-logging', action='store_true',
|
| 75 |
+
help='Enable detailed telemetry logging')
|
| 76 |
+
|
| 77 |
+
@staticmethod
|
| 78 |
+
def add_optimization_args(parser: argparse.ArgumentParser) -> None:
|
| 79 |
+
"""Add optimization and performance arguments."""
|
| 80 |
+
opt_group = parser.add_argument_group('Optimization & Performance')
|
| 81 |
+
opt_group.add_argument('--use-amp', action='store_true',
|
| 82 |
+
help='Use automatic mixed precision')
|
| 83 |
+
opt_group.add_argument('--gradient-checkpointing', action='store_true',
|
| 84 |
+
help='Use gradient checkpointing')
|
| 85 |
+
opt_group.add_argument('--compile-model', action='store_true',
|
| 86 |
+
help='Use torch.compile for optimization')
|
| 87 |
+
opt_group.add_argument('--chunk-size', type=int, default=None,
|
| 88 |
+
help='Chunk size for chunked attention')
|
| 89 |
+
opt_group.add_argument('--num-workers', type=int, default=4,
|
| 90 |
+
help='Number of data loader workers')
|
| 91 |
+
|
| 92 |
+
@staticmethod
|
| 93 |
+
def add_distributed_args(parser: argparse.ArgumentParser) -> None:
|
| 94 |
+
"""Add distributed training arguments."""
|
| 95 |
+
dist_group = parser.add_argument_group('Distributed Training')
|
| 96 |
+
dist_group.add_argument('--distributed', action='store_true',
|
| 97 |
+
help='Enable distributed training')
|
| 98 |
+
dist_group.add_argument('--world-size', type=int, default=1,
|
| 99 |
+
help='Number of distributed processes')
|
| 100 |
+
dist_group.add_argument('--rank', type=int, default=0,
|
| 101 |
+
help='Process rank for distributed training')
|
| 102 |
+
dist_group.add_argument('--backend', choices=['nccl', 'gloo'], default='nccl',
|
| 103 |
+
help='Distributed backend')
|
| 104 |
+
|
| 105 |
+
@staticmethod
|
| 106 |
+
def add_io_args(parser: argparse.ArgumentParser) -> None:
|
| 107 |
+
"""Add input/output arguments."""
|
| 108 |
+
io_group = parser.add_argument_group('Input/Output')
|
| 109 |
+
io_group.add_argument('--input-path', type=str,
|
| 110 |
+
help='Input file or directory path')
|
| 111 |
+
io_group.add_argument('--output-path', type=str, default='./output',
|
| 112 |
+
help='Output directory path')
|
| 113 |
+
io_group.add_argument('--weights-path', type=str, default='./weights/model.pt',
|
| 114 |
+
help='Model weights file path')
|
| 115 |
+
io_group.add_argument('--checkpoint-dir', type=str, default='./checkpoints',
|
| 116 |
+
help='Checkpoint directory path')
|
| 117 |
+
io_group.add_argument('--log-level', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
|
| 118 |
+
default='INFO', help='Logging level')
|
| 119 |
+
|
| 120 |
+
@staticmethod
|
| 121 |
+
def add_huggingface_args(parser: argparse.ArgumentParser) -> None:
|
| 122 |
+
"""Add HuggingFace integration arguments."""
|
| 123 |
+
hf_group = parser.add_argument_group('HuggingFace Integration')
|
| 124 |
+
hf_group.add_argument('--hf-repo', type=str,
|
| 125 |
+
help='HuggingFace repository ID')
|
| 126 |
+
hf_group.add_argument('--hf-token', type=str,
|
| 127 |
+
help='HuggingFace access token')
|
| 128 |
+
hf_group.add_argument('--private-repo', action='store_true',
|
| 129 |
+
help='Create private HuggingFace repository')
|
| 130 |
+
hf_group.add_argument('--auto-upload', action='store_true',
|
| 131 |
+
help='Automatically upload to HuggingFace after training')
|
| 132 |
+
|
| 133 |
+
@staticmethod
|
| 134 |
+
def add_diffusion_args(parser: argparse.ArgumentParser) -> None:
|
| 135 |
+
"""Add diffusion mode arguments."""
|
| 136 |
+
diff_group = parser.add_argument_group('Diffusion Mode')
|
| 137 |
+
diff_group.add_argument('--diffusion-mode', action='store_true',
|
| 138 |
+
help='Enable diffusion training mode')
|
| 139 |
+
diff_group.add_argument('--diffusion-steps', type=int, default=8,
|
| 140 |
+
help='Number of diffusion steps')
|
| 141 |
+
diff_group.add_argument('--noise-schedule', choices=['linear', 'cosine', 'exponential'],
|
| 142 |
+
default='linear', help='Noise schedule type')
|
| 143 |
+
diff_group.add_argument('--diffusion-curriculum', action='store_true',
|
| 144 |
+
help='Use curriculum learning for diffusion')
|
| 145 |
+
|
| 146 |
+
@classmethod
|
| 147 |
+
def create_standard_parser(cls,
|
| 148 |
+
description: str,
|
| 149 |
+
include_groups: Optional[list] = None) -> argparse.ArgumentParser:
|
| 150 |
+
"""Create a standardized argument parser with specified groups.
|
| 151 |
+
|
| 152 |
+
Args:
|
| 153 |
+
description: Parser description
|
| 154 |
+
include_groups: List of group names to include. If None, includes all.
|
| 155 |
+
Options: ['model', 'training', 'dataset', 'safety', 'optimization',
|
| 156 |
+
'distributed', 'io', 'huggingface', 'diffusion']
|
| 157 |
+
"""
|
| 158 |
+
parser = argparse.ArgumentParser(
|
| 159 |
+
description=description,
|
| 160 |
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# Default groups to include if none specified
|
| 164 |
+
if include_groups is None:
|
| 165 |
+
include_groups = ['model', 'training', 'dataset', 'safety', 'io']
|
| 166 |
+
|
| 167 |
+
# Add requested argument groups
|
| 168 |
+
group_methods = {
|
| 169 |
+
'model': cls.add_model_args,
|
| 170 |
+
'training': cls.add_training_args,
|
| 171 |
+
'dataset': cls.add_dataset_args,
|
| 172 |
+
'safety': cls.add_safety_args,
|
| 173 |
+
'optimization': cls.add_optimization_args,
|
| 174 |
+
'distributed': cls.add_distributed_args,
|
| 175 |
+
'io': cls.add_io_args,
|
| 176 |
+
'huggingface': cls.add_huggingface_args,
|
| 177 |
+
'diffusion': cls.add_diffusion_args,
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
for group_name in include_groups:
|
| 181 |
+
if group_name in group_methods:
|
| 182 |
+
group_methods[group_name](parser)
|
| 183 |
+
|
| 184 |
+
# Add common flags
|
| 185 |
+
parser.add_argument('--verbose', '-v', action='store_true',
|
| 186 |
+
help='Enable verbose output')
|
| 187 |
+
parser.add_argument('--debug', action='store_true',
|
| 188 |
+
help='Enable debug mode')
|
| 189 |
+
parser.add_argument('--seed', type=int, default=42,
|
| 190 |
+
help='Random seed for reproducibility')
|
| 191 |
+
|
| 192 |
+
return parser
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
# Pre-configured parsers for common use cases
|
| 196 |
+
def create_training_parser() -> argparse.ArgumentParser:
|
| 197 |
+
"""Create parser for training scripts."""
|
| 198 |
+
return BitTransformerCLI.create_standard_parser(
|
| 199 |
+
"BitTransformerLM Training Script",
|
| 200 |
+
['model', 'training', 'dataset', 'safety', 'optimization', 'distributed', 'io', 'huggingface']
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
def create_inference_parser() -> argparse.ArgumentParser:
|
| 204 |
+
"""Create parser for inference scripts."""
|
| 205 |
+
return BitTransformerCLI.create_standard_parser(
|
| 206 |
+
"BitTransformerLM Inference Script",
|
| 207 |
+
['model', 'safety', 'io', 'diffusion']
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
def create_evaluation_parser() -> argparse.ArgumentParser:
|
| 211 |
+
"""Create parser for evaluation scripts."""
|
| 212 |
+
return BitTransformerCLI.create_standard_parser(
|
| 213 |
+
"BitTransformerLM Evaluation Script",
|
| 214 |
+
['model', 'dataset', 'safety', 'io']
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
def create_workflow_parser() -> argparse.ArgumentParser:
|
| 218 |
+
"""Create parser for workflow/pipeline scripts."""
|
| 219 |
+
return BitTransformerCLI.create_standard_parser(
|
| 220 |
+
"BitTransformerLM Workflow Script",
|
| 221 |
+
['model', 'training', 'dataset', 'safety', 'optimization', 'io', 'huggingface', 'diffusion']
|
| 222 |
+
)
|