heir icon indicating copy to clipboard operation
heir copied to clipboard

activation-canonicalizations drops the approximation domain when rewriting sigmoid

Open YGTao opened this issue 1 month ago • 4 comments

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-approximation

What 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 : f32

The 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.0

rather 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.

YGTao avatar Oct 29 '26 04:10 YGTao

@YGTao You are submitting too much AI slop. Please see the AI tool use policy. If you'd actually like to be involved in the community, please attend office hours and meet the maintainers to come up with a suitable starter project.

j2kun avatar Oct 29 '26 04:10 j2kun

  1. @YGTao You are submitting too much AI slop. Please see the AI tool use policy. If you'd actually like to be involved in the community, please attend office hours and meet the maintainers to come up with a suitable starter project.

@j2kun I think there's been a misunderstanding. I'm doing research on translation validation for HEIR, mainly to check whether its compiler transformations are correct. Finding bugs is just a byproduct of that work.

These issues came up over more than three weeks of research. I only reported them recently, after filtering out duplicates, invalid inputs, unsupported cases, and false positives. AI tools certainly helped, but we reviewed the findings ourselves.

I shared these issues because I thought they'd be useful. I'm not looking to become a HEIR developer or maintainer.

I'm genuinely curious: what kinds of issues do you welcome, and from whom? Are reproducible bugs found by external researchers welcome, or do you consider these reports not to be actual bugs? If so, why?

YGTao avatar Oct 29 '26 05:10 YGTao

The question is what bugs are worth fixing. Answering that requires context about which parts of this (very prototype- and research-leaning) compiler are under active development, what parts are planned to be replaced and redesigned from scratch, and what parts are effectively waiting to be removed. I am happy to explain that context to someone who wants to actively contribute. But based on your comment, justifying why I'd close some of these tickets is not worth the effort. If your goal is to showcase a tool that can find bugs in compilers, I'd recommend you apply it to a more mainstream compiler.

j2kun avatar Oct 29 '26 18:10 j2kun

@AlexanderViand For this particular issue I'm going to try porting the listener infrastructure from https://github.com/llvm/torch-mlir/pull/4627 to ensure the attributes are always forwarded regardless of how patterns are defined.

j2kun avatar Oct 29 '26 18:10 j2kun