activation-canonicalizations drops the approximation domain when rewriting sigmoid
I found that activation-canonicalizations drops the declared approximation domain when recognizing a sigmoid expression.
As a result, polynomial-approximation uses its default domain [-1, 1] instead of the original [-5, 5], producing large errors for inputs within the declared range.
Reproducer
This uses HEIR's own batchnorm_sigmoid example from:
tests/Examples/openfhe/ckks/batchnorm_sigmoid/batchnorm_sigmoid.mlir
Tested with HEIR bc7eabdddd0085e1048c87ecaa4e1f3439810dae and 4133db19.
func.func @batchnorm_sigmoid(%x: f32 {secret.secret}, %mean: f32, %var: f32, %gamma: f32, %beta: f32) -> f32 {
%epsilon = arith.constant 1.000000e-05 : f32
%var_eps = arith.addf %var, %epsilon : f32
%sqrt_var_eps = math.sqrt %var_eps : f32
%c0 = arith.constant 0.0 : f32
%not_zero = arith.cmpf one, %sqrt_var_eps, %c0 : f32
cf.assert %not_zero, "variance is zero"
%inv_sqrt_var_eps = arith.divf %gamma, %sqrt_var_eps : f32
%x_minus_mean = arith.subf %x, %mean : f32
%normalized = arith.mulf %x_minus_mean, %inv_sqrt_var_eps : f32
%bn_out = arith.addf %normalized, %beta : f32
%neg_bn_out = arith.negf %bn_out : f32
%exp = math.exp %neg_bn_out {degree = 3 : i32, domain_lower = -5.0 : f64, domain_upper = 5.0 : f64} : f32
%c1 = arith.constant 1.0 : f32
%denom = arith.addf %exp, %c1 : f32
%sigmoid = arith.divf %c1, %denom {degree = 3 : i32, domain_lower = 1.0 : f64, domain_upper = 150.0 : f64} : f32
return %sigmoid : f32
}
Run using the example's pipeline:
heir-opt repro.mlir
--annotate-module="backend=openfhe scheme=ckks"
--activation-canonicalizations
--mlir-to-ckks=min-slot-count=1024
--scheme-to-openfhe
--mlir-print-ir-after=activation-canonicalizations,polynomial-approximationWhat goes wrong
After activation-canonicalizations, the original math.exp retains its domain attributes, but the newly created sigmoid has none:
%8 = math.exp %7 {degree = 3 : i32, domain_lower = -5.0 : f64, domain_upper = 5.0 : f64} : f32
%10 = math_ext.sigmoid %6 : f32The original exponential is no longer used by the returned result.
The subsequent polynomial-approximation pass therefore generates a sigmoid polynomial using:
domain_lower = -1.0
domain_upper = 1.0rather than the declared [-5, 5] domain.
Expected vs. actual
Evaluating the polynomial emitted by HEIR gives:
| Sigmoid input | Expected sigmoid | Generated polynomial |
|---|---|---|
| -5 | 0.0067 | -3.6603 |
| -3 | 0.0474 | -0.1189 |
| 0 | 0.5000 | 0.5000 |
| 3 | 0.9526 | 1.1189 |
| 5 | 0.9933 | 4.6603 |
The maximum absolute error over [-5, 5] is approximately 3.67, whereas the generated polynomial has an error below 3e-6 over its default [-1, 1] domain.
The official example's existing input happens to produce a batch-normalization output around 0.894, which is inside [-1, 1] and therefore does not expose the problem.
Possible cause
In lib/Transforms/ActivationCanonicalizations/Rewrites.td, the sigmoid rewrite constructs MathExt_SigmoidOp without forwarding the approximation-domain attributes.
The ReLU rewrite in the same pass explicitly preserves these attributes to prevent the polynomial approximation from falling back to [-1, 1], but the sigmoid rewrite does not.
The recognized sigmoid should preserve the appropriate input approximation domain (accounting for the negation), rather than silently using the default interval.