training_model package

Submodules

training_model.logging_config module

File for configuring logging with colored output.

Classes:

ColoredFormatter([fmt, datefmt, style, ...])

Custom formatter that adds color to log messages using ANSI escape codes.

Functions:

configure_logging([level])

Configure root logger with colored output handler.

hex_to_ansi(hex_color)

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: Formatter

Custom 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.

format(record: LogRecord) str[source]

Format the specified log record with color.

Parameters:

record (LogRecord) – The log record to be formatted

Returns:

Formatted log message with color codes

Return type:

str

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.logging_config.hex_to_ansi(hex_color: str) str[source]

Convert hexadecimal color code to ANSI escape sequence.

Parameters:

hex_color (str) – Hexadecimal color code in format ‘#RRGGBB’

Returns:

ANSI escape sequence for the color, or empty string if conversion fails

Return type:

str

training_model.one_file_train module

Main file for model training

Functions:

change_dir(destination)

Context manager for temporarily changing the working directory.

convert_to_gguf(model_path, outfile, ...)

Convert Hugging Face model to GGUF format.

copy_data(file[, gguf_directory, destination])

Move file to destination directory with versioning.

data_preparation(cfg, tokenizer[, ...])

Prepare and preprocess training and validation datasets.

generate_and_tokenize_prompt(data_point, ...)

Generate and tokenize a complete prompt.

generate_prompt(tokenizer, data_point)

Generate a chat template prompt for the model.

main_train(data_dir, cfg)

Main training entry point with dataset processing.

model_merge_for_converting(cfg, steps, save_path)

Merge base model with adapter weights and save the result.

post_new_dataset()

Upload new dataset version to remote server.

quantize_model(model_path, outfile[, qtype, ...])

Quantize GGUF model using llama.cpp quantizer.

tokenize(tokenizer, cutoff_len, prompt)

Tokenize text with specified length constraints.

train(cfg)

Execute full training pipeline.

train_pipeline(cfg)

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.one_file_train.train(cfg: DictConfig) int[source]

Execute full training pipeline.

Parameters:

cfg (DictConfig) – Configuration object

Returns:

Number of global training steps completed

Return type:

int

training_model.one_file_train.train_pipeline(cfg: DictConfig) None[source]

Execute complete training pipeline including conversion and quantization.

Parameters:

cfg (DictConfig) – Configuration object

Raises:

RuntimeError – If quantization step fails

training_model.private_api module

training_model.utils module

Additional functions for the training_model module.

Functions:

dataset_to_json(dataset, filename)

Convert dataset to JSON format and save to file.

get_user_prompt(data)

Construct user prompt from conversation data.

tokens_init(cfg)

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.utils.tokens_init(cfg: DictConfig) Run[source]

Initialize Weights & Biases logging and configure authentication.

Parameters:

cfg (DictConfig) – Configuration object with training parameters

Returns:

Initialized Weights & Biases run object

Return type:

Run

training_model.grpo_train module

File for training using grpo method

Functions:

grpo_train(model, tokenizer, cfg, ...[, ...])

Execute GRPO training pipeline.

prepare_grpo_data(cfg)

Prepare datasets for GRPO training with prompts and correct actions.

reward_function(prompts, completions, **kwargs)

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_logging([level])

Configure root logger with colored output handler.

main_train(data_dir, cfg)

Main training entry point with dataset processing.

training_model.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.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