setControlVector method

void setControlVector(
  1. ControlVector? vector
)

Steers every following forward pass with vector (experimental), or stops steering with null. See control_vector.dart.

The KV rows, conv histories and checkpoints of a sequence hold only under the vector they were computed with, so a different vector needs an empty sequence: throws StateError unless length is 0 (call reset first) or the vector steers exactly as the current one. reset keeps the vector; saveState records it, and loadState refuses a state saved under any other.

Both paths apply vectors; the GPU's sum for project mode is in float32 in another order, so its rows differ from the CPU's in the last bits. Throws ArgumentError for a vector of another hidden size or with directions past the last layer, and UnsupportedError on a GPU library without the kernel (gpuLib of an older build).

Implementation

void setControlVector(ControlVector? vector) {
  _checkOpen();
  if (vector != null) {
    if (_gpu case final gpu? when !gpu.steers) {
      throw UnsupportedError(
        'the GPU library has no control-vector kernel (lab_gpu_enc_steer)',
      );
    }
    if (vector.hiddenSize != config.hiddenSize ||
        vector.maxLayer >= config.numLayers) {
      throw ArgumentError(
        'the control vector has hidden size ${vector.hiddenSize} and '
        'directions up to layer ${vector.maxLayer}; the model has '
        '${config.hiddenSize} and ${config.numLayers} layers',
      );
    }
  }
  if (vector?.fingerprint == _control?.fingerprint) {
    _control = vector;
    return;
  }
  if (length != 0 || _checkpoints.isNotEmpty || _forwarding != 0) {
    throw StateError(
      'the control vector changes only on an empty sequence; reset first',
    );
  }
  _gpu?.setControlVector(vector);
  _control = vector;
}