Table of Contents

Class ConstantOp

Namespace
AiDotNet.JitCompiler.IR.Operations
Assembly
AiDotNet.dll

Represents a constant tensor in the IR (result of constant folding).

public class ConstantOp : IROp
Inheritance
ConstantOp
Inherited Members

Remarks

ConstantOp stores pre-computed tensor values that were evaluated at compile time. This is the result of constant folding optimization, where expressions with all constant inputs are computed during compilation rather than at runtime.

For Beginners: A ConstantOp holds a pre-calculated result.

When the compiler sees: t0 = Constant([2.0]) t1 = Constant([3.0]) t2 = Add(t0, t1)

It computes 2.0 + 3.0 = 5.0 at compile time and replaces with: t2 = Constant([5.0])

Benefits:

  • No addition happens at runtime
  • Less memory for intermediate tensors
  • Faster execution

Properties

IsScalar

Gets or sets a flag indicating whether this is a scalar constant.

public bool IsScalar { get; }

Property Value

bool

Values

Gets or sets the constant values as a flat array.

public double[] Values { get; set; }

Property Value

double[]

Remarks

Values are stored as double for precision. They can be cast to the appropriate type during code generation based on OutputType.

Methods

ToString()

Gets a string representation of this operation for debugging.

public override string ToString()

Returns

string

A string describing this operation.

Remarks

The string format is: "tOutput = OpType(tInput1, tInput2, ...) : Type [Shape]"

For Beginners: This creates a readable description of the operation.

Example outputs:

  • "t2 = Add(t0, t1) : Float32 [3, 4]"
  • "t5 = MatMul(t3, t4) : Float32 [128, 256]"
  • "t8 = ReLU(t7) : Float32 [32, 128]"

This is super helpful for debugging - you can see exactly what each operation does and what shape tensors flow through the graph.

Validate()

Validates that this operation is correctly formed.

public override bool Validate()

Returns

bool

True if valid, false otherwise.

Remarks

Basic validation checks that the operation has required information. Derived classes can override to add operation-specific validation.

For Beginners: This checks that the operation makes sense.

Basic checks:

  • Output ID is valid (non-negative)
  • Has the right number of inputs
  • Shapes are compatible

Specific operations add their own checks:

  • MatMul: inner dimensions must match
  • Conv2D: kernel size must be valid
  • Reshape: total elements must be preserved

If validation fails, the operation can't be compiled.