Several Axioms and theorems in MixedPrec are falsely stated

Ouverte
#104 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
5/5
Temps estimé
Plus d'une semaine
Accessibilité débutants
20/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Domaine
compilers

Piste de recherche

The issue is in TensorLib.MixedPrec; examine the false axioms and theorems listed. Start by reading the refutation code in the issue body to understand the counterexamples. Look at the file where these axioms are defined, likely in the same directory. The work involves understanding floating-point arithmetic and Lean's Float32 model, then correcting the statements or proofs. 'Done' means the axioms and theorems are either fixed or removed, and the refutations no longer apply.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Hi, I found the following issues in MixedPred. Many of the axioms assumes that Float32 behaves like a perfect real number, but it can be broken due to Floats being inaccurate. For example, rounding means 1e8 + 1 = 1e8. The false axioms are:
float32_eq_add_sub , float32_abs_triangle, float32_add_le_add, float32_mul_add3, float32_add_sub_cancel, float32_add_sub_cancel_left, float32_add_sub_decomp, float32_abs_sub_triangle, float32_add_notNaN, float32_sub_notNaN, float32_mul_notNaN, float8E4M3_normal_rounding_bound. Also the theorems pointwiseBoundE4M3, pointwiseBound, additionErrorBound are also false.

Here are the refutation for all the axioms mentioned (it is drafted by Claude):

import TensorLib.MixedPrec

namespace TensorLib

/-! # Refutations

Every false axiom above is refuted by applying it to a concrete counterexample and deriving
`False`; `#print axioms` on each refutation names the axiom being refuted. Every false theorem
statement is restated verbatim (the restatement is checked against the original by `example`)
and its negation is proved without any of the axioms above.

Trust base: the kernel (`decide`) plus a single `native_decide` fact, `eps_e4m3`, because
`Float32.pow` is `opaque` (implemented by C `powf`) and so the kernel cannot evaluate
`machineEpsilon`. The theorems being refuted already rely on `native_decide` themselves.

Counterexample values (as Float32 bit patterns):
* `1e8 = 0x4CBEBC20`: float32 spacing at 1e8 is 8, so `1e8 + 1 = 1e8`.
* `±∞`, and `±maxFinite = ±0x7F7FFFFF`.
* `x₀ = 3·2⁻¹⁰ = 0x3B400000`: a normal fp32 value inside e4m3's *subnormal* range
  `[2⁻⁹, 2⁻⁶)`. e4m3 values there are spaced `2⁻⁹` apart, so rounding `x₀` has error
  `2⁻¹⁰ ≫ ½·ε·|x₀| = 3·2⁻¹⁴`. `fp8Min` should be the smallest *normal* (`2⁻⁶`), not the
  smallest subnormal.
-/

namespace Refutation

abbrev one  : Float32 := Float32.ofBits 0x3F800000
abbrev two  : Float32 := Float32.ofBits 0x40000000
abbrev big  : Float32 := Float32.ofBits 0x4CBEBC20  -- 1e8
abbrev pinf : Float32 := Float32.ofBits 0x7F800000
abbrev ninf : Float32 := Float32.ofBits 0xFF800000
abbrev pmax : Float32 := Float32.ofBits 0x7F7FFFFF
abbrev nmax : Float32 := Float32.ofBits 0xFF7FFFFF
abbrev x₀   : Float32 := Float32.ofBits 0x3B400000  -- 3·2⁻¹⁰

/-- `powf 2 (-3) = 0.125`. The only non-kernel fact used below. -/
theorem eps_e4m3 : Float32.pow 2.0 (-Nat.toFloat32 3) = 0.125 := by native_decide

theorem machineEpsilon_e4m3 : (machineEpsilon .float8_e4m3).getD 0 = 0.125 := eps_e4m3

/-! ## False axioms -/

/-- `1 ≠ 1e8 + (1 - 1e8) = 0`. -/
theorem float32_eq_add_sub_false : False := by
  have h := float32_eq_add_sub one big; revert h; decide

/-- `|∞ + -∞| = NaN`, and `NaN ≤ _` is false. -/
theorem float32_abs_triangle_false : False := by
  have h := float32_abs_triangle pinf ninf (by decide) (by decide); revert h; decide

/-- `∞ ≤ ∞` and `-∞ ≤ -∞`, but `∞ + -∞ = NaN ≰ NaN`. -/
theorem float32_add_le_add_false : False := by
  have h := float32_add_le_add pinf pinf ninf ninf (by decide) (by decide) (by decide) (by decide)
    (by decide) (by decide)
  revert h; decide

/-- `2 * (max + -max + 0) = 0`, but `2*max + 2*(-max) + 2*0 = ∞ + -∞ = NaN`. -/
theorem float32_mul_add3_false : False := by
  have h := float32_mul_add3 two pmax nmax 0 (by decide) (by decide) (by decide) (by decide)
  revert h; decide

/-- `(1 + 1e8) - 1e8 = 0 ≠ 1`. -/
theorem float32_add_sub_cancel_false : False := by
  have h := float32_add_sub_cancel one big (by decide) (by decide); revert h; decide

/-- `(1e8 + 1) - 1e8 = 0 ≠ 1`. -/
theorem float32_add_sub_cancel_left_false : False := by
  have h := float32_add_sub_cancel_left big one (by decide) (by decide); revert h; decide

/-- `(1e8 + 1) - (1e8 + 0) = 0`, but `(1e8 - 1e8) + (1 - 0) = 1`. -/
theorem float32_add_sub_decomp_false : False := by
  have h := float32_add_sub_decomp big 0 big one (by decide) (by decide) (by decide) (by decide)
  revert h; decide

/-- `|∞ - 0| = ∞`, but `|∞ - ∞| + |∞ - 0| = NaN`. -/
theorem float32_abs_sub_triangle_false : False := by
  have h := float32_abs_sub_triangle pinf pinf 0 (by decide) (by decide) (by decide)
  revert h; decide

/-- `∞ + -∞ = NaN`. -/
theorem float32_add_notNaN_false : False := by
  have h := float32_add_notNaN pinf ninf (by decide) (by decide); revert h; decide

/-- `∞ - ∞ = NaN`. -/
theorem float32_sub_notNaN_false : False := by
  have h := float32_sub_notNaN pinf pinf (by decide) (by decide); revert h; decide

/-- `0 * ∞ = NaN`. -/
theorem float32_mul_notNaN_false : False := by
  have h := float32_mul_notNaN 0 pinf (by decide) (by decide); revert h; decide

/-- `x₀` rounds to `2⁻⁸`: error `2⁻¹⁰ > ½ · 0.125 · x₀`. -/
theorem float8E4M3_normal_rounding_bound_false : False := by
  have h := float8E4M3_normal_rounding_bound x₀ (by decide) (by decide) (by decide)
  rw [eps_e4m3] at h; revert h; decide

/-! ## False theorem statements

`pointwiseBoundE4M3`, `pointwiseBound` and the axiom `fp8_normal_rounding_bound` conclude
`∃ err, round x = .ok (x + err) ∧ |err| ≤ B`. Ruling out *every* `err` needs monotonicity of
float addition, which core Lean's float model does not provide. It is taken here as an explicit
hypothesis `AddMonoAt x₀` (IEEE round-to-nearest addition is monotone), not as an axiom.

`additionErrorBound` needs no such hypothesis: its witness is fixed by
`round (a' + b') = .ok result`. -/

/-- Monotonicity of `x + ·` (true for IEEE-754 round-to-nearest; `e.abs ≤ b` forces both
non-NaN and `e ≤ b`). -/
def AddMonoAt (x : Float32) : Prop := ∀ e b : Float32, e.abs ≤ b → x + e ≤ x + b

/-- The shared conclusion shape is false at `x₀`, given monotonicity. -/
theorem no_err_at_x₀ (hmono : AddMonoAt x₀) :
    ¬ ∃ err : Float32, Dtype.roundToComputeDtype x₀ .float8_e4m3 = .ok (x₀ + err) ∧
      err.abs ≤ 0.5 * (machineEpsilon .float8_e4m3).getD 0 * x₀.abs := by
  rintro ⟨err, hr, hb⟩
  rw [machineEpsilon_e4m3] at hb
  have hround : Dtype.roundToComputeDtype x₀ .float8_e4m3 = .ok (Float32.ofBits 0x3B800000) := by
    decide
  rw [hround] at hr
  injection hr with hr
  -- 2⁻⁸ = x₀ + err ≤ x₀ + B < 2⁻⁸
  have hle := hmono err _ hb
  rw [← hr] at hle
  exact Float32.le_lt_false _ _ hle (by decide)

def PointwiseBoundE4M3Stmt : Prop :=
  ∀ (x : Float32), x.abs ≤ (Dtype.fp8Max .float8_e4m3).getD 0 →
    (x = 0 ∨ Float32.ofBits 0x3B000000 <= x.abs) →
    ∃ err : Float32, Dtype.roundToComputeDtype x .float8_e4m3 = .ok (x + err)
    ∧ err.abs <= 0.5 * (machineEpsilon .float8_e4m3).getD 0 * x.abs

example : PointwiseBoundE4M3Stmt := pointwiseBoundE4M3

theorem pointwiseBoundE4M3_false (hmono : AddMonoAt x₀) : ¬ PointwiseBoundE4M3Stmt :=
  fun h => no_err_at_x₀ hmono (h x₀ (by decide) (by decide))

def PointwiseBoundStmt : Prop :=
  ∀ (dtype : Dtype) (x : Float32), (Dtype.fp8Max dtype).isSome →
    x.abs ≤ (Dtype.fp8Max dtype).getD 0 →
    (x = 0 ∨ (Dtype.fp8Min dtype).getD 0 ≤ x.abs) →
    ∃ err : Float32, Dtype.roundToComputeDtype x dtype = .ok (x + err) ∧
    err.abs ≤ 0.5 * (machineEpsilon dtype).getD 0 * x.abs

example : PointwiseBoundStmt := fun d x h1 h2 h3 => pointwiseBound d x h1 h2 h3

theorem pointwiseBound_false (hmono : AddMonoAt x₀) : ¬ PointwiseBoundStmt :=
  fun h => no_err_at_x₀ hmono (h .float8_e4m3 x₀ (by decide) (by decide) (by decide))

/-- The axiom `fp8_normal_rounding_bound` has the same conclusion. -/
theorem fp8_normal_rounding_bound_false (hmono : AddMonoAt x₀) : False :=
  no_err_at_x₀ hmono (fp8_normal_rounding_bound .float8_e4m3 x₀ (by decide) (by decide) (by decide))

def AdditionErrorBoundStmt : Prop :=
  ∀ (dtype : Dtype) (_ : (Dtype.fp8Max dtype).isSome) (a b a' b' : Float32),
    a.isNaN = false → b.isNaN = false → a'.isNaN = false → b'.isNaN = false →
    (a' - a).abs ≤ 0.5 * (machineEpsilon dtype).getD 0 * a.abs →
    (b' - b).abs ≤ 0.5 * (machineEpsilon dtype).getD 0 * b.abs →
    (a' + b').abs ≤ (Dtype.fp8Max dtype).getD 0 →
    (a' + b' = 0 ∨ (Dtype.fp8Min dtype).getD 0 ≤ (a' + b').abs) →
    ∃ result : Float32,
      Dtype.roundToComputeDtype (a' + b') dtype = .ok result ∧
      (result - (a + b)).abs ≤
        0.5 * (machineEpsilon dtype).getD 0 * (a.abs + b.abs + (a' + b').abs)

example : AdditionErrorBoundStmt := fun d h a b a' b' h1 h2 h3 h4 h5 h6 h7 h8 =>
  additionErrorBound d h a b a' b' h1 h2 h3 h4 h5 h6 h7 h8

/-- `a = a' = x₀`, `b = b' = 0`: no quantization error on the inputs, yet the result `2⁻⁸`
is `2⁻¹⁰` away from `x₀`, above the bound `½ · 0.125 · 2x₀ = 3·2⁻¹³`. -/
theorem additionErrorBound_false : ¬ AdditionErrorBoundStmt := by
  intro h
  have ha : (x₀ - x₀).abs ≤ 0.5 * (machineEpsilon .float8_e4m3).getD 0 * x₀.abs := by
    rw [machineEpsilon_e4m3]; decide
  have hb : ((0 : Float32) - 0).abs ≤ 0.5 * (machineEpsilon .float8_e4m3).getD 0 * (0 : Float32).abs := by
    rw [machineEpsilon_e4m3]; decide
  obtain ⟨result, hr, hbound⟩ := h .float8_e4m3 (by decide) x₀ 0 x₀ 0
    (by decide) (by decide) (by decide) (by decide) ha hb (by decide) (by decide)
  have hround : Dtype.roundToComputeDtype (x₀ + 0) .float8_e4m3 = .ok (Float32.ofBits 0x3B800000) := by
    decide
  rw [hround] at hr
  injection hr with hr
  subst hr
  rw [machineEpsilon_e4m3] at hbound
  revert hbound; decide

end Refutation

end TensorLib

Thanks

Langage dominant
Lean
Étoiles
52
Forks
9
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de leanprover/TensorLib

Toutes les issues de leanprover/TensorLib

Issues similaires

Plus d'issues Compilers

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.