microsoft/semantic-kernel · error · InvalidOperationException

Expected output length {modelOutput.Length} to be a multiple

Error message

Expected output length {modelOutput.Length} to be a multiple of {dimensions} dimensions.

What it means

Thrown as an InvalidOperationException in BertOnnxTextEmbeddingGenerationService.Pool when the raw model output tensor length is not evenly divisible by the configured dimension count. This indicates a mismatch between the ONNX model's actual output dimensions and the dimensions value the service was initialized with. The pooling math requires exact divisibility.

Source

Thrown at dotnet/src/Connectors/Connectors.Onnx/BertOnnxTextEmbeddingGenerationService.cs:250

                    logger.LogTrace("Generated embedding for text: {Text}", text);
                }
            }

            return results;
        }
        finally
        {
            ArrayPool<long>.Shared.Return(scratch);
        }
    }

    private float[] Pool(ReadOnlySpan<float> modelOutput)
    {
        int dimensions = this._dimensions;
        int embeddings = Math.DivRem(modelOutput.Length, dimensions, out int leftover);
        if (leftover != 0)
        {
            throw new InvalidOperationException($"Expected output length {modelOutput.Length} to be a multiple of {dimensions} dimensions.");
        }

        float[] result = new float[dimensions];
        if (embeddings <= 1)
        {
            modelOutput.CopyTo(result);
        }
        else
        {
            switch (this._options.PoolingMode)
            {
                case EmbeddingPoolingMode.Mean or EmbeddingPoolingMode.MeanSquareRootTokensLength:
                    TensorPrimitives.Add(modelOutput.Slice(0, dimensions), modelOutput.Slice(dimensions, dimensions), result);
                    for (int pos = dimensions * 2; pos < modelOutput.Length; pos += dimensions)
                    {
                        TensorPrimitives.Add(result, modelOutput.Slice(pos, dimensions), result);
                    }

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Verify the dimensions value in BertOnnxOptions matches the model's hidden size (check the model's config.json).
  2. Use the correct model file matching the configured dimensions.
  3. Inspect modelOutput.Length and dimensions at runtime to diagnose the mismatch.

Example fix

// before: 768-dim model loaded but dimensions set for MiniLM
var options = new BertOnnxOptions { /* dimensions defaults to wrong value */ };

// after
var options = new BertOnnxOptions
{
    MaximumTokens = 512
    // ensure dimensions match the model's config.json hidden_size
};
// e.g., for all-MiniLM-L6-v2, hidden_size = 384
Defensive patterns

Strategy: validation

Validate before calling

// Read the model's config.json to verify hidden_size matches dimensions
var modelConfig = JsonSerializer.Deserialize<JsonElement>(File.ReadAllText(configPath));
int hiddenSize = modelConfig.GetProperty("hidden_size").GetInt32();
if (hiddenSize != configuredDimensions)
    throw new InvalidOperationException($"Dimension mismatch: model={hiddenSize}, config={configuredDimensions}");

Prevention

When it happens

Trigger: Loading a BERT ONNX model whose hidden size differs from the configured dimensions value; using the wrong model file with settings calibrated for a different model; the model output tensor was truncated or padded incorrectly during inference.

Common situations: Swapped model files (e.g., loading a 384-dim model like all-MiniLM-L6 with dimensions=768); misconfigured dimensions in BertOnnxOptions; ONNX runtime version change altering output tensor layout.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/0f964713681b0a60. Report an issue: GitHub.