Skip to content
class

Validator

Validator

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&
Reference to the tensor being checked. Lifetime is the caller's responsibility.
label_std::string
Human-readable identifier used as the op-name argument to ErrorBuilder so error messages name the offending tensor (e.g. "linear.weight").

Examples

```
Validator::input(t, "linear.weight")
    .non_null()
    .float_only()
    .ndim(2);
```

Constructors

1
ctor

Validator

void Validator(const int & t, int label)

Construct 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

2
static

input

→Validator
Validator input(const int & t, int label)

Builds a single-tensor validator.

Parameters

tconst TensorImplPtr&
Tensor to be validated. The validator retains a reference, not a copy — t must remain alive for the duration of the chain.
labelstd::string
Identifier used in error messages (typically the parameter name, e.g. "weight").

Returns

Validator

A new validator bound to t.

static

pair

→Pair
Pair pair(const int & a, const int & b, int op_name)

Builds a two-tensor validator.

Parameters

aconst TensorImplPtr&
First operand. Reference retained; must outlive the chain.
bconst TensorImplPtr&
Second operand. Reference retained; must outlive the chain.
op_namestd::string
Op name used in error messages.

Returns

Pair

A new validator bound to a and b.

Methods

8
fn

dtype_eq

→Validator&
Validator & dtype_eq(Dtype expected)

Asserts that the dtype equals expected.

Implicitly calls non_null first.

Parameters

expectedDtype
Required dtype.

Returns

Validator&

*this.

Raises

LucidError
If the tensor is null.
DtypeMismatch
If t_->dtype() != expected.
fn

dtype_in

→Validator&
Validator & dtype_in(int allowed)

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>
Set of acceptable dtypes. Iterated linearly, so callers should keep the list short (the common case is 2–4 entries).

Returns

Validator&

*this.

Raises

LucidError
If the tensor is null.
DtypeMismatch
If no entry in allowed matches t_->dtype().
fn

float_only

→Validator&
Validator & float_only()

Asserts that the dtype is either Dtype::F32 or Dtype::F64.

Implicitly calls non_null first.

Returns

Validator&

*this.

Raises

LucidError
If the tensor is null.
NotImplementedError
If the dtype is anything other than F32 or F64.
fn

ndim

→Validator&
Validator & ndim(int expected)

Asserts that the tensor has exactly expected dimensions.

Implicitly calls non_null first.

Parameters

expectedint
Required number of dimensions.

Returns

Validator&

*this.

Raises

LucidError
If the tensor is null.
ShapeMismatch
If its rank differs from expected (see require_rank).
fn

ndim_at_least

→Validator&
Validator & ndim_at_least(int min_n)

Asserts that the tensor has at least min_n dimensions.

Implicitly calls non_null first.

Parameters

min_nint
Lower bound (inclusive) on the tensor's rank.

Returns

Validator&

*this.

Raises

LucidError
If the tensor is null.
ShapeMismatch
If its rank is below min_n (see require_min_rank).
fn

non_null

→Validator&
Validator & non_null()

Asserts that the tensor pointer is not null.

Returns

Validator&

*this, to allow chaining.

Raises

LucidError
If t_ is null.
fn

shape_eq

→Validator&
Validator & shape_eq(const int & expected)

Asserts that the tensor's shape equals expected.

Implicitly calls non_null first.

Parameters

expectedconst Shape&
Required shape.

Returns

Validator&

*this.

Raises

LucidError
If the tensor is null.
ShapeMismatch
If t_->shape() != expected.
fn

square_2d

→Validator&
Validator & square_2d()

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

InvalidArgument
If the tensor has rank < 2.
ShapeMismatch
If its last two dims disagree (see require_square_matrix).