LayerContractValidator
Helpers & Utilities · AiDotNet.NeuralNetworks
Checks that a chain of layers agrees on tensor LAYOUT — each layer’s declared output feeding the next layer’s declared input — and reports the first disagreement by layer index and role.
For Beginners
Neural network layers pass tensors to each other, and if one produces data shaped differently from what the next one expects, the model fails somewhere deep inside with a confusing message. This checks the hand-off up front and says exactly which two layers disagree.
How It Works
WHY THIS RUNS AT RUNTIME RATHER THAN IN THE SOURCE GENERATOR. The chain does not exist at compile
time: InitializeLayers builds it through LayerHelper, and several layer types resolve
their input depth lazily on the first forward pass. Roslyn cannot execute that. So validation happens
where the chain actually becomes real — as it is assembled.
It compares DECLARATIONS, never materialized tensors, and that is what makes it work with lazy layers:
a layer can declare that it consumes [Batch, Channels, Height, Width] long before it knows how
many channels that will be.
WHAT IT BUYS. ABCNet failed 16 scaffold tests with two different messages —
Expected input depth 1, but got 256 and Tensor shapes must match. Got [128, 32, 97] and [17, 32, 32] — thrown from inside a forward pass, naming neither the layer nor the mismatch. The
same defect through this validator reads “layer 5 emits [Batch, Time, Features], layer 6 expects
[Batch, Channels, Height, Width]”.
UNANNOTATED LAYERS ARE SKIPPED, not failed. Annotation is being rolled out incrementally, and a validator that rejected every un-annotated layer would break the entire library on the first commit. A skipped layer breaks the chain into independently-validated runs rather than invalidating it.
Methods
| Method | Summary |
|---|---|
ContractShadowResult CompareContractsToResolvedShapes(IReadOnlyList<ILayer<T>> layers) |
Compares each layer’s declared shape CONTRACT against the shape the imperative resolution actually concluded, and reports where they disagree. |
IReadOnlyList<LayoutMismatch> Validate(IReadOnlyList<ILayer<T>> layers) |
Validates a layer chain and returns every disagreement found, in order. |
IReadOnlyList<TensorLayoutAttribute> InputLayouts(Type type) |
|
IReadOnlyList<TensorLayoutAttribute> OutputLayouts(Type type) |
Declared output layouts for a type, or an empty list when unannotated. |
IReadOnlyList<ValueDomainMismatch> ValidateValueDomains(IReadOnlyList<ILayer<T>> layers) |
Validates value semantics for adjacent layers in a known sequential run. |
void ValidateOrThrow(IReadOnlyList<ILayer<T>> layers, string ownerName) |
Validates a chain and throws when it disagrees, naming every mismatch. |