Skip to content

Input control

aisteer360.algorithms.input_control.base

Input control base classes.

This module provides the abstract base class for methods that modify prompts before they reach the model.

Two base classes are provided:

  • InputControl: Base class for all input control methods.
  • NoInputControl: Identity (null) control; used when no input control is defined in steering pipeline.

Input controls implement steering through prompt transformation σ(x), enabling behavior modification without altering model parameters or architecture. These methods transform inputs before they reach the model, resulting in generations following y ~ p_θ(σ(x)).

Examples of input controls:

  • Few-shot learning (prepending examples)
  • Prompt templates and formatting
  • Soft prompts and prompt tuning
  • Chain-of-thought prompting
  • Iterative prompt refinement

See Also:

  • aisteer360.algorithms.input_control: Implementations of input control methods
  • aisteer360.core.steering_pipeline: Integration with steering pipeline

InputControl

Bases: ABC

Abstract base class for input control steering methods.

Transforms prompts before model processing through a prompt adapter function that modifies input token sequences.

Methods:

Name Description
get_prompt_adapter

Return transformation function (required)

steer

One-time preparation (optional)

Source code in aisteer360/algorithms/input_control/base.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class InputControl(ABC):
    """Abstract base class for input control steering methods.

    Transforms prompts before model processing through a prompt adapter function that modifies input token sequences.

    Methods:
        get_prompt_adapter(runtime_kwargs) -> Callable: Return transformation function (required)
        steer(model, tokenizer, **kwargs) -> None: One-time preparation (optional)
    """

    Args: Type[BaseArgs] | None = None

    enabled: bool = True

    def __init__(self, *args, **kwargs) -> None:
        if self.Args is None:  # null control
            if args or kwargs:
                raise TypeError(f"{type(self).__name__} accepts no constructor arguments.")
            return

        self.args: BaseArgs = self.Args.validate(*args, **kwargs)

        # move fields to attributes
        for field in fields(self.args):
            setattr(self, field.name, getattr(self.args, field.name))

    @abstractmethod
    def get_prompt_adapter(
        self,
        runtime_kwargs: dict | None = None
    ) -> Callable[[list[int] | torch.Tensor, dict[str, Any]], list[int] | torch.Tensor]:
        """Receives (input_ids, runtime_kwargs) and returns modified input_ids.."""
        pass

    def steer(self,
              model=None,
              tokenizer=None,
              **kwargs) -> None:
        """Optional steering/preparation."""
        pass

args = self.Args.validate(*args, **kwargs) instance-attribute

enabled = True class-attribute instance-attribute

get_prompt_adapter(runtime_kwargs=None) abstractmethod

Receives (input_ids, runtime_kwargs) and returns modified input_ids..

Source code in aisteer360/algorithms/input_control/base.py
63
64
65
66
67
68
69
@abstractmethod
def get_prompt_adapter(
    self,
    runtime_kwargs: dict | None = None
) -> Callable[[list[int] | torch.Tensor, dict[str, Any]], list[int] | torch.Tensor]:
    """Receives (input_ids, runtime_kwargs) and returns modified input_ids.."""
    pass

steer(model=None, tokenizer=None, **kwargs)

Optional steering/preparation.

Source code in aisteer360/algorithms/input_control/base.py
71
72
73
74
75
76
def steer(self,
          model=None,
          tokenizer=None,
          **kwargs) -> None:
    """Optional steering/preparation."""
    pass

NoInputControl

Bases: InputControl

Identity input control.

Used as the default when no input control is needed. Returns input_ids.

Source code in aisteer360/algorithms/input_control/base.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
class NoInputControl(InputControl):
    """Identity input control.

    Used as the default when no input control is needed. Returns input_ids.
    """
    enabled: bool = False
    tokenizer: PreTrainedTokenizerBase | None = None

    def get_prompt_adapter(
            self,
            runtime_kwargs: dict | None = None
    ):
        """Null adapter operation; returns identity map."""
        if self.tokenizer is None:
            return lambda ids, _: ids

        def adapter(input_ids: list[int] | torch.Tensor, runtime_kwargs) -> list[int] | torch.Tensor:
            return input_ids

        return adapter

    def steer(
            self,
            model=None,
            tokenizer: PreTrainedTokenizerBase | None = None,
            **kwargs
    ) -> None:
        """Null steer operation; attaches tokenizer."""
        self.tokenizer = tokenizer

args = self.Args.validate(*args, **kwargs) instance-attribute

enabled = False class-attribute instance-attribute

tokenizer = None class-attribute instance-attribute

get_prompt_adapter(runtime_kwargs=None)

Null adapter operation; returns identity map.

Source code in aisteer360/algorithms/input_control/base.py
87
88
89
90
91
92
93
94
95
96
97
98
def get_prompt_adapter(
        self,
        runtime_kwargs: dict | None = None
):
    """Null adapter operation; returns identity map."""
    if self.tokenizer is None:
        return lambda ids, _: ids

    def adapter(input_ids: list[int] | torch.Tensor, runtime_kwargs) -> list[int] | torch.Tensor:
        return input_ids

    return adapter

steer(model=None, tokenizer=None, **kwargs)

Null steer operation; attaches tokenizer.

Source code in aisteer360/algorithms/input_control/base.py
100
101
102
103
104
105
106
107
def steer(
        self,
        model=None,
        tokenizer: PreTrainedTokenizerBase | None = None,
        **kwargs
) -> None:
    """Null steer operation; attaches tokenizer."""
    self.tokenizer = tokenizer