training_model package
Submodules
training_model.logging_config module
File for configuring logging with colored output.
Classes:
|
Custom formatter that adds color to log messages using ANSI escape codes. |
Functions:
|
Configure root logger with colored output handler. |
|
Convert hexadecimal color code to ANSI escape sequence. |
- class training_model.logging_config.ColoredFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]
Bases:
FormatterCustom formatter that adds color to log messages using ANSI escape codes. The colors are determined by the LOG_COLORS mapping based on log level.
Methods:
format(record)Format the specified log record with color.
- training_model.logging_config.configure_logging(level: int = 20) None[source]
Configure root logger with colored output handler.
- Parameters:
level (int) – Logging level to set (logging.INFO or logging.DEBUG). Defaults to logging.INFO.
- Raises:
ValueError – If level is not logging.INFO or logging.DEBUG
training_model.one_file_train module
Main file for model training
Functions:
|
Context manager for temporarily changing the working directory. |
|
Convert Hugging Face model to GGUF format. |
|
Move file to destination directory with versioning. |
|
Prepare and preprocess training and validation datasets. |
|
Generate and tokenize a complete prompt. |
|
Generate a chat template prompt for the model. |
|
Main training entry point with dataset processing. |
|
Merge base model with adapter weights and save the result. |
Upload new dataset version to remote server. |
|
|
Quantize GGUF model using llama.cpp quantizer. |
|
Tokenize text with specified length constraints. |
|
Execute full training pipeline. |
|
Execute complete training pipeline including conversion and quantization. |
- training_model.one_file_train.change_dir(destination: str) Generator[None, None, None][source]
Context manager for temporarily changing the working directory.
- Parameters:
destination (str) – Path to the target directory
- Yields:
None – Enters the target directory during context execution
- training_model.one_file_train.convert_to_gguf(model_path: str, outfile: str, python_exe: str, outtype: str, cfg: DictConfig) None[source]
Convert Hugging Face model to GGUF format.
- Parameters:
model_path (str) – Path to input model directory
outfile (str) – Output file path
python_exe (str) – Python executable path
outtype (str) – Output type specification
cfg (DictConfig) – Configuration object
- Raises:
FileNotFoundError – If required paths are missing
- training_model.one_file_train.copy_data(file: str, gguf_directory: str = 'custom-model', destination: str = 'T:\\lm-studio\\models\\game-model') None[source]
Move file to destination directory with versioning.
- Parameters:
file (str) – Source file name
gguf_directory (str) – Version subdirectory
destination (str) – Root destination directory
- training_model.one_file_train.data_preparation(cfg: DictConfig, tokenizer: AutoTokenizer, should_add_prompt: bool = False) Tuple[Dataset, Dataset][source]
Prepare and preprocess training and validation datasets.
- Parameters:
cfg (DictConfig) – Configuration object
tokenizer (AutoTokenizer) – Hugging Face tokenizer
- Returns:
Tuple containing train and validation datasets
- Return type:
Tuple[Dataset, Dataset]
- training_model.one_file_train.generate_and_tokenize_prompt(data_point: Dict[str, str], tokenizer: AutoTokenizer, cutoff: int, should_add_prompt: bool = False) Dict[str, str] | Dict[str, Tensor][source]
Generate and tokenize a complete prompt.
- Parameters:
data_point (Dict[str, str]) – Dictionary containing conversation data
tokenizer (AutoTokenizer) – Hugging Face tokenizer
cutoff (int) – Maximum sequence length
should_add_prompt (bool) – used for grpo, when needed dict with keyword “prompt” returned
- Returns:
Tokenized prompt dictionary
- Return type:
Dict[str, torch.Tensor]
- training_model.one_file_train.generate_prompt(tokenizer: AutoTokenizer, data_point: Dict[str, str]) str[source]
Generate a chat template prompt for the model.
- Parameters:
tokenizer (AutoTokenizer) – Hugging Face tokenizer
data_point (Dict[str, str]) – Dictionary containing system, user and bot messages
- Returns:
Formatted chat prompt
- Return type:
str
- training_model.one_file_train.main_train(data_dir: str, cfg: DictConfig) None[source]
Main training entry point with dataset processing.
- Parameters:
data_dir (str) – Directory containing training data
cfg (DictConfig) – Configuration object
- training_model.one_file_train.model_merge_for_converting(cfg: DictConfig, steps: int, save_path: str) None[source]
Merge base model with adapter weights and save the result.
- Parameters:
cfg (DictConfig) – Configuration object
steps (int) – Training step number for checkpoint selection
save_path (str) – Path to save merged model
- training_model.one_file_train.post_new_dataset() None[source]
Upload new dataset version to remote server.
- training_model.one_file_train.quantize_model(model_path: str, outfile: str, qtype: str = 'q4_0', llama_cpp_path: str = '.', quantized_path: str = 'llama-quantize.exe') bool[source]
Quantize GGUF model using llama.cpp quantizer.
- Parameters:
model_path (str) – Path to input GGUF model
outfile (str) – Path for quantized output
qtype (str) – Quantization type (default: q4_0)
llama_cpp_path (str) – Path to llama.cpp directory
quantized_path (str) – Name of quantizer executable
- Returns:
True if quantization succeeded, False otherwise
- Return type:
bool
- training_model.one_file_train.tokenize(tokenizer: AutoTokenizer | Callable, cutoff_len: int, prompt: str) Dict[str, Tensor][source]
Tokenize text with specified length constraints.
- Parameters:
tokenizer (AutoTokenizer) – Hugging Face tokenizer
cutoff_len (int) – Maximum sequence length
prompt (str) – Text to tokenize
- Returns:
Tokenized output dictionary
- Return type:
Dict[str, torch.Tensor]
training_model.private_api module
training_model.utils module
Additional functions for the training_model module.
Functions:
|
Convert dataset to JSON format and save to file. |
|
Construct user prompt from conversation data. |
|
Initialize Weights & Biases logging and configure authentication. |
- training_model.utils.dataset_to_json(dataset: Dict[str, Any], filename: str) List[Dict[str, str]][source]
Convert dataset to JSON format and save to file.
- Parameters:
dataset (Dict[str, Any]) – Source dataset dictionary containing: - system: System prompt template - examples: Dictionary of conversation examples
filename (str) – Output file path
- Returns:
List of generated JSON objects with conversation data
- Return type:
List[Dict[str, str]]
- training_model.utils.get_user_prompt(data: Dict[str, Any]) str[source]
Construct user prompt from conversation data.
- Parameters:
data (Dict[str, Any]) – Dictionary containing conversation history and metadata: - History: List of previous messages - AvailableActions: List of available actions - UserInput: Current user input
- Returns:
Formatted prompt string with conversation context
- Return type:
str
training_model.grpo_train module
File for training using grpo method
Functions:
|
Execute GRPO training pipeline. |
|
Prepare datasets for GRPO training with prompts and correct actions. |
|
Compute rewards for GRPO training based on action matching. |
- training_model.grpo_train.grpo_train(model: ~transformers.models.auto.modeling_auto.AutoModel | ~peft.peft_model.PeftModel | ~transformers.modeling_utils.PreTrainedModel, tokenizer: ~transformers.models.auto.tokenization_auto.AutoTokenizer | ~transformers.tokenization_utils.PreTrainedTokenizer, cfg: ~omegaconf.dictconfig.DictConfig, data_preparing_func: ~typing.Callable | None, reward_func: ~typing.Callable = <function reward_function>) int[source]
Execute GRPO training pipeline.
- Parameters:
model (AutoModel) – LLM model
tokenizer (AutoTokenizer) – LLM tokenizer
cfg (DictConfig) – Configuration object
data_preparing_func (Callable) – Function used to prepare the data. Should return Tuple[Dataset, Dataset]: Tuple containing train and validation datasets
reward_func (Callable) – Reward function for the grpo
- Returns:
Number of global training steps completed
- Return type:
int
- training_model.grpo_train.prepare_grpo_data(cfg: DictConfig) Tuple[Dataset, Dataset][source]
Prepare datasets for GRPO training with prompts and correct actions.
- Parameters:
cfg (DictConfig) – Configuration object
- Returns:
Tuple containing train and validation datasets
- Return type:
Tuple[Dataset, Dataset]
- training_model.grpo_train.reward_function(prompts: List[str], completions: List[str], **kwargs) list[source]
Compute rewards for GRPO training based on action matching.
- Parameters:
prompts (list) – List of prompts for trl consistency
completions (list) – List of model-generated completions
kwargs (dict) – Dataset row containing ‘correct_actions’
- Returns:
List of reward values for each completion
- Return type:
list
Module contents
Functions:
|
Configure root logger with colored output handler. |
|
Main training entry point with dataset processing. |