kernelfoundry.algorithm.prompts.prompt_evolution_integration

Interface to integrate prompt evolution with controller.

This module provides integration between prompt evolution systems and the kernel generation controller, enabling co-evolution of prompts and kernels.

Supports holistic evolution mode:

Holistic (Science-CodeEvolve): Prompts evolve as complete programs
  • Uses MetaPromptingManager

  • Full prompt evolution via SEARCH/REPLACE diffs

  • Direct fitness attribution (prompt.fitness = solution.fitness)

Integration points:

  1. Controller initialization: Setup prompt evolution manager(s)

  2. Prompt construction: Sample evolved content during generation

  3. Fitness reporting: Update fitness after kernel evaluation

  4. Evolution trigger: Periodically evolve prompts based on performance

Usage:

In the controller’s __init__:

from kernelfoundry.algorithm.prompts.prompt_evolution_integration import PromptEvolutionMixin

class Controller(PromptEvolutionMixin):
def __init__(self, config, …):

super().__init__(config, …) self.setup_prompt_evolution()

In evolve_prompt_and_inference:

prompt, session_id = self.construct_evolved_prompt(…) # … generate kernel … self.report_prompt_fitness(session_id, kernel_score)

Functions

build_evolved_optimization_prompt(base_prompt)

Build an optimization prompt with evolved components.

create_prompt_evolution_callback([meta_manager])

Create a callback function for reporting kernel fitness.

integrate_prompt_evolution_with_controller(...)

Decorator to add prompt evolution capabilities to a controller class.

Classes

PromptEvolutionMixin()

Mixin class to add prompt evolution capabilities to the Controller.

PromptEvolutionMode()

Prompt evolution mode constants.

class kernelfoundry.algorithm.prompts.prompt_evolution_integration.PromptEvolutionMode[source]

Prompt evolution mode constants.

HOLISTIC = 'holistic'
DISABLED = 'disabled'
class kernelfoundry.algorithm.prompts.prompt_evolution_integration.PromptEvolutionMixin[source]

Mixin class to add prompt evolution capabilities to the Controller.

This mixin provides: - Initialization of holistic prompt manager - Unified interface for prompt sampling and fitness reporting - Automatic mode selection based on configuration

Usage:
class Controller(PromptEvolutionMixin):
def __init__(self, config):

# … existing init … self.setup_prompt_evolution()

config: DictConfig
llm_server: Callable
setup_prompt_evolution()[source]

Initialize prompt evolution system(s).

Call this after the controller’s main initialization. Reads configuration from self.config.prompt for evolution settings.

property prompt_evolution_enabled: bool

Check if any prompt evolution is enabled.

property prompt_evolution_mode: str

Get current prompt evolution mode.

property meta_prompting_enabled: bool

Check if meta-prompting (holistic) is enabled.

sample_evolved_prompt(rng: random.Random | None = None) Tuple['PromptProgram' | None, str][source]

Sample an evolved prompt for kernel generation (holistic/meta-prompting).

Parameters:

rng – Optional random generator for reproducibility

Returns:

Tuple of (PromptProgram, session_id for tracking)

get_evolvable_content(prompt_program: 'PromptProgram' | None = None) Dict[str, str][source]

Get evolvable content from a prompt program for template injection.

Parameters:

prompt_program – PromptProgram from meta-prompting (optional)

Returns:

Dict of region_name -> content for Jinja2 template

report_prompt_fitness(session_id: str, kernel_score: float, kernel_id: str | None = None, kernel_code: str | None = None, kernel_metrics: Dict[str, float] | None = None)[source]

Report kernel fitness to update associated prompt evolution state.

Call this after kernel evaluation.

Parameters:
  • session_id – Session ID from sample_evolved_prompt

  • kernel_score – Combined fitness score of the generated kernel

  • kernel_id – Optional ID of the kernel

  • kernel_code – Optional code of the kernel (for meta-prompting context)

  • kernel_metrics – Optional detailed metrics for fine-grained attribution

get_prompt_evolution_statistics() Dict[str, Any][source]

Get combined statistics about prompt evolution.

save_prompt_evolution_state()[source]

Save the prompt evolution database(s).

apply_evolved_prompting(base_prompt: str, parent: 'Program' | None, strategy: str = 'mutate', include_esimd: bool = False, skip_meta_prompting: bool = True) Tuple[str, str][source]

Enhanced version of prompt construction with evolution.

This method extends the optimization-aware prompting by incorporating evolved prompt components from holistic systems.

NOTE: When using template-based meta-prompting (evolvable content injected during prompt construction), set skip_meta_prompting=True to avoid double- sampling. The holistic content is already in the prompt via template variables.

Parameters:
  • base_prompt – The base prompt before enhancements

  • parent – Parent program being evolved

  • strategy – Evolution strategy

  • include_esimd – Whether to include ESIMD guidance

  • skip_meta_prompting – If True, skip holistic evolution (already applied via template)

Returns:

Tuple of (enhanced_prompt, session_id)

force_prompt_evolution(num_prompts: int = 1) int[source]

Force immediate prompt evolution.

Useful for testing or manual triggering.

Parameters:

num_prompts – Number of prompts to evolve

Returns:

Number of prompts successfully evolved

kernelfoundry.algorithm.prompts.prompt_evolution_integration.integrate_prompt_evolution_with_controller(controller_class)[source]

Decorator to add prompt evolution capabilities to a controller class.

Usage:

@integrate_prompt_evolution_with_controller
class Controller:
    ...

Dynamic usage:

Controller = integrate_prompt_evolution_with_controller(Controller)
kernelfoundry.algorithm.prompts.prompt_evolution_integration.build_evolved_optimization_prompt(base_prompt: str, meta_prompting_manager: 'MetaPromptingManager' | None = None, strategy: str = 'mutate', include_esimd: bool = False, parent_profile: Dict[str, int] | None = None) Tuple[str, str][source]

Build an optimization prompt with evolved components.

Standalone function for use outside of the controller.

Parameters:
  • base_prompt – Base prompt text

  • meta_prompting_manager – MetaPromptingManager instance (can be None)

  • strategy – Evolution strategy

  • include_esimd – Whether to include ESIMD guidance

  • parent_profile – Optional parent’s optimization profile

Returns:

Tuple of (enhanced_prompt, session_id)

kernelfoundry.algorithm.prompts.prompt_evolution_integration.create_prompt_evolution_callback(meta_manager: 'MetaPromptingManager' | None = None) Callable[[str, float, Dict | None], None][source]

Create a callback function for reporting kernel fitness.

Useful for async or callback-based evaluation pipelines.

Parameters:

meta_manager – MetaPromptingManager instance

Returns:

Callback function(session_id, score, metrics) -> None