heir icon indicating copy to clipboard operation
heir copied to clipboard

Lattigo emitter rounds small floating-point constants to zero

Open YGTao opened this issue 1 month ago • 1 comments

I found that the Lattigo Go emitter loses precision when printing floating-point splat constants. Small nonzero values can become exactly zero in the generated code, changing the computation.

Reproducer

Tested with HEIR 4133db19.

func.func @scale_small(%x: tensor<8xf32> {secret.secret}) -> tensor<8xf32> {
  %c = arith.constant dense<4.0e-7> : tensor<8xf32>
  %r = arith.mulf %x, %c : tensor<8xf32>
  return %r : tensor<8xf32>
}

Run:

heir-opt repro.mlir --annotate-module="backend=lattigo scheme=ckks" \
  --mlir-to-ckks=min-slot-count=8 --scheme-to-lattigo -o out.mlir

heir-translate --emit-lattigo out.mlir

Expected vs. actual

The lowered MLIR still contains the correct constant:

memref.global "private" constant @__constant_8xf32_0 : memref<8xf32> = dense<4.000000e-07>

However, the generated Go code contains:

var __constant_8xf32_0 = slices.Repeat([]float32{0.000000}, 8)

The nonzero multiplier has become exactly zero.

For input:

x = [1, 1, 1, 1, 1, 1, 1, 1]

Expected:
[4e-7, 4e-7, 4e-7, 4e-7, 4e-7, 4e-7, 4e-7, 4e-7]

Actual:
[0, 0, 0, 0, 0, 0, 0, 0]

I also reproduced this with real Lattigo v6.1.0 encryption and decryption using two independently generated key sets. Both returned all-zero outputs.

Possible cause

In lib/Target/Lattigo/LattigoEmitter.cpp, floating-point splat values are formatted using:

valStr = std::to_string(splatAttr.getValueAsDouble());

std::to_string prints six digits after the decimal point, so 4e-7 becomes 0.000000.

This also rounds larger values unnecessarily. For example, 0.1234567 is emitted as 0.123457.

The emitter should preserve enough floating-point precision for the generated literal to represent the original value, for example through round-trip-safe formatting.

YGTao avatar Oct 29 '26 04:10 YGTao

Adding to this: the same root cause shows up in two more places in the Lattigo emitter, and both are still there in nightly-2026.10.31. Non-splat float globals are printed with 7 significant digits (%.6e), and Chebyshev coefficients and domain endpoints go through std::scientific at its default precision.

Repro with heir-translate --emit-lattigo repro.mlir:

module attributes {scheme.ckks} {
  memref.global "private" constant @a : memref<2xf64> = dense<[0.33333333333333331,1.2345678901234567]>
  func.func @c(%p: !lattigo.ckks.parameter, %e: !lattigo.ckks.evaluator, %x: !lattigo.rlwe.ciphertext) -> !lattigo.rlwe.ciphertext {
    %pe = lattigo.ckks.new_polynomial_evaluator %p,%e : (!lattigo.ckks.parameter,!lattigo.ckks.evaluator) -> !lattigo.ckks.polynomial_evaluator
    %r = "lattigo.ckks.chebyshev"(%pe,%x) {coefficients=[0.12345678901234567:f64], targetScale=1099511627776:i64, domain=array<f64:-1.2345678901234567,1.2345678901234567>} : (!lattigo.ckks.polynomial_evaluator,!lattigo.rlwe.ciphertext) -> !lattigo.rlwe.ciphertext
    return %r : !lattigo.rlwe.ciphertext
  }
}

This emits var a = []float64{3.333333e-01, 1.234568e+00}, SetFloat64(1.234568e-01) for the coefficient, and [2]float64{-1.234568e+00, 1.234568e+00} for the domain. The digits are gone before the values reach big.Float, so the extra precision there doesn't help.

This matters even for ordinary constants: a coefficient like 0.0390625 on an x^4 term comes out as 0.039062, which shifts the result by about 3e-3 at x = 9. The sigmoid, relu_composite and mnist examples in this repo also get truncated constants, but their tests don't catch it.

Printing round-trip-safe values, as the OpenFHE emitter already does, would cover all three paths.

Given the recent pushback on AI-generated bug reports, I'm adding this here rather than opening new issues. I agree with that pushback: a tool-found bug is only worth maintainers' time if someone has checked it and can say why it matters.

A1ester avatar Oct 31 '26 13:10 A1ester