Fluent precondition checker for a single tensor input.
Each method either evaluates one precondition and returns *this
(success), or throws the appropriate typed LucidError subclass
via ErrorBuilder (failure). Chaining lets op implementations
state all of their input requirements in one compact expression at the
top of their body.
Instances cannot be constructed directly — use the static
input factory. The validator holds a reference to the
caller's TensorImplPtr rather than a copy, so the chain must live no
longer than the tensor it inspects (this is the normal case when the
chain appears on a single statement at the op's entry point).
See Also
Validator::Pair : Two-tensor sibling for cross-tensor checks.
ErrorBuilder : Underlying throw machinery.
Attributes
t_const TensorImplPtr&label_std::stringErrorBuilder so error messages name the offending tensor (e.g. "linear.weight").Examples
`
Validator::input(t, "linear.weight")
.non_null()
.float_only()
.ndim(2);
`Constructors
1Construct a Validator bound to t with the given label.
Private because callers must go through the input
factory. Stores t by reference (not copy), so the resulting
validator must live no longer than the caller's TensorImplPtr.
label is woven into every error message the chain emits, so
callers should pass the parameter's public name (e.g. "weight").
Static methods
2Builds a single-tensor validator.
Parameters
tconst TensorImplPtr&t must remain alive for the duration of the chain.labelstd::string"weight").Returns
ValidatorA new validator bound to t.
Builds a two-tensor validator.
Parameters
aconst TensorImplPtr&bconst TensorImplPtr&op_namestd::stringReturns
PairA new validator bound to a and b.
Methods
8Asserts that the dtype equals expected.
Implicitly calls non_null first.
Parameters
expectedDtypeReturns
Validator&*this.
Raises
LucidErrorDtypeMismatcht_->dtype() != expected.Asserts that the dtype is one of allowed.
Implicitly calls non_null first. The error message lists
the allowed dtypes joined by "|" (e.g. "float32|float64").
Parameters
allowedstd::initializer_list<Dtype>Returns
Validator&*this.
Raises
LucidErrorDtypeMismatchallowed matches t_->dtype().Asserts that the dtype is either Dtype::F32 or Dtype::F64.
Implicitly calls non_null first.
Returns
Validator&*this.
Raises
LucidErrorNotImplementedErrorF32 or F64.Asserts that the tensor has exactly expected dimensions.
Implicitly calls non_null first.
Parameters
expectedintReturns
Validator&*this.
Raises
LucidErrorexpected.Asserts that the tensor has at least min_n dimensions.
Implicitly calls non_null first.
Parameters
min_nintReturns
Validator&*this.
Raises
LucidErrormin_n.Asserts that the tensor pointer is not null.
Returns
Validator&*this, to allow chaining.
Raises
LucidErrort_ is null.Asserts that the tensor's shape equals expected.
Implicitly calls non_null first.
Parameters
expectedconst Shape&Returns
Validator&*this.
Raises
LucidErrorShapeMismatcht_->shape() != expected.Asserts that the tensor is at least 2-D and its trailing two dimensions are equal.
Used by linalg ops that require square matrices or batches thereof —
e.g. a shape [B, N, N] passes while [B, N, M] with
N != M fails. Implicitly calls non_null first.
Shape
Accepted: any [*, N, N].
Returns
Validator&*this.
Raises
LucidError