From fbbe29b27cc987fff9f52989b101a292ac98caff Mon Sep 17 00:00:00 2001 From: Ilias Garnier Date: Wed, 27 Jul 2022 15:20:02 +0200 Subject: [PATCH 1/2] lib_benchmark: remove dead/redundant code --- src/bin_snoop/display.ml | 2 +- src/lib_benchmark/benchmark_helpers.ml | 88 -------------------------- src/lib_benchmark/costlang.ml | 77 ---------------------- src/lib_benchmark/costlang.mli | 13 ---- 4 files changed, 1 insertion(+), 179 deletions(-) diff --git a/src/bin_snoop/display.ml b/src/bin_snoop/display.ml index 93c0220c6c10..3e390eceac10 100644 --- a/src/bin_snoop/display.ml +++ b/src/bin_snoop/display.ml @@ -247,7 +247,7 @@ let plot_scatter opts title input_columns outputs = | _ -> if opts.reduced_plot_verbosity then return [] else - let subsets = Benchmark_helpers.enumerate_subsets 2 input_columns in + let subsets = Stats.Combi.enumerate_subsets 2 input_columns in let plots = List.map (function diff --git a/src/lib_benchmark/benchmark_helpers.ml b/src/lib_benchmark/benchmark_helpers.ml index 9a6d12d0ef4c..f7071fc6f731 100644 --- a/src/lib_benchmark/benchmark_helpers.ml +++ b/src/lib_benchmark/benchmark_helpers.ml @@ -37,91 +37,3 @@ let make_progress_printer fmtr total message = let int_encoding : int Data_encoding.encoding = let open Data_encoding in conv (fun i -> Int64.of_int i) (fun l -> Int64.to_int l) int64 - -(* -------------------------------------------------------------------------- *) -(* Enumerate all ways of picking n elements from a list (ie all injections - from [n] to [l]) *) - -let rec enumerate_injections n l current_pick acc = - if n = 0 then List.rev current_pick :: acc - else enumerate_picks n l current_pick acc - -and enumerate_picks n l current_pick acc = - match l with - | [] -> acc - | x :: tl -> - let extended_pick = x :: current_pick in - let acc = enumerate_injections (n - 1) tl extended_pick acc in - enumerate_picks n tl current_pick acc - -let enumerate_subsets n l = enumerate_injections n l [] [] - -(* -------------------------------------------------------------------------- *) -(* Seq helpers *) - -let seq_progress fmtr msg seq = - let c = ref 1 in - Seq.map - (fun x -> - Format.fprintf fmtr "\r%s (%d)%!" msg !c ; - incr c ; - x) - seq - -(* To be removed when in OCaml stdlib. The code below this line is under the - following license: *) - -(**************************************************************************) -(* *) -(* OCaml *) -(* *) -(* Simon Cruanes *) -(* *) -(* Copyright 2017 Institut National de Recherche en Informatique et *) -(* en Automatique. *) -(* *) -(* All rights reserved. This file is distributed under the terms of *) -(* the GNU Lesser General Public License version 2.1, with the *) -(* special exception on linking described in the file LICENSE. *) -(* *) -(**************************************************************************) - -let rec take_aux n xs = - if n = 0 then Seq.empty - else fun () -> - match xs () with - | Seq.Nil -> Nil - | Cons (x, xs) -> Cons (x, take_aux (n - 1) xs) - -let take n xs = - if n < 0 then invalid_arg "Seq.take" ; - take_aux n xs - -let rec force_drop n xs = - match xs () with - | Seq.Nil -> Seq.Nil - | Cons (_, xs) -> - let n = n - 1 in - if n = 0 then xs () else force_drop n xs - -let drop n xs = - if n < 0 then invalid_arg "Seq.drop" - else if n = 0 then xs - else fun () -> force_drop n xs - -let rec iteri_aux f i xs = - match xs () with - | Seq.Nil -> () - | Cons (x, xs) -> - f i x ; - iteri_aux f (i + 1) xs - -let[@inline] iteri f xs = iteri_aux f 0 xs - -let rec map2 f xs ys () = - match xs () with - | Seq.Nil -> Seq.Nil - | Cons (x, xs) -> ( - match ys () with - | Seq.Nil -> Nil - | Cons (y, ys) -> Cons (f x y, map2 f xs ys)) diff --git a/src/lib_benchmark/costlang.ml b/src/lib_benchmark/costlang.ml index 67e5e2c3a939..acfa47ae57df 100644 --- a/src/lib_benchmark/costlang.ml +++ b/src/lib_benchmark/costlang.ml @@ -188,63 +188,6 @@ module Free_variables : let if_ cond ift iff = Set.union cond (Set.union ift iff) end -module Parameters : - S with type 'a repr = bool -> String.Set.t and type size = unit = struct - type 'a repr = bool -> String.Set.t - - type size = unit - - let true_ _ = String.Set.empty - - let false_ _ = String.Set.empty - - let lift_binop _x _y _b = String.Set.empty - - let float _ _ = String.Set.empty - - let int _ _ = String.Set.empty - - let ( + ) = lift_binop - - let ( - ) = lift_binop - - let ( * ) = lift_binop - - let ( / ) = lift_binop - - let max = lift_binop - - let min = lift_binop - - let shift_left _x _i _ = String.Set.empty - - let shift_right _x _i _ = String.Set.empty - - let log2 _x _ = String.Set.empty - - let sqrt _x _ = String.Set.empty - - let free ~name _ = - ignore name ; - String.Set.empty - - let lt = lift_binop - - let eq = lift_binop - - let lam ~name f is_head = - if is_head then String.Set.add name (f (fun _ -> String.Set.empty) is_head) - else String.Set.empty - - let app _f _arg _ = String.Set.empty - - let let_ ~name _m _f _ = - ignore name ; - String.Set.empty - - let if_ _cond _ift _iff _ = String.Set.empty -end - module Eval : S with type 'a repr = 'a and type size = float = struct exception Term_contains_free_variable of Free_variable.t @@ -507,26 +450,6 @@ functor let prj x = x end -module type Map_const_params = sig - val map_int : int -> int - - val map_float : float -> float -end - -module Map_const (P : Map_const_params) : Transform = -functor - (X : S) - -> - struct - include X - - let prj x = x - - let float f = X.float (P.map_float f) - - let int i = X.int (P.map_int i) - end - module Subst (P : sig val subst : Free_variable.t -> float end) : Transform = diff --git a/src/lib_benchmark/costlang.mli b/src/lib_benchmark/costlang.mli index 0b8e92adb031..1ecd815e7370 100644 --- a/src/lib_benchmark/costlang.mli +++ b/src/lib_benchmark/costlang.mli @@ -90,10 +90,6 @@ module Pp : S with type 'a repr = string and type size = string module Free_variables : S with type 'a repr = Free_variable.Set.t and type size = unit -(* Extracts model parameters, corresponding to head lambdas. *) -module Parameters : - S with type 'a repr = bool -> String.Set.t and type size = unit - (* Evaluating implementation for closed terms. Fails if free variables are present. Use the [Subst] implementation transformer to get rid of free variables. *) @@ -131,15 +127,6 @@ val compose : transform -> transform -> transform module Identity : Transform -(* [Map_const] maps functions to integer and float constants. *) -module type Map_const_params = sig - val map_int : int -> int - - val map_float : float -> float -end - -module Map_const (P : Map_const_params) : Transform - (* [Subst] allows to subtitute free variables by constants. *) module Subst (P : sig val subst : Free_variable.t -> float -- GitLab From 932b8ebbb4b18aaa1c0f075026c8f19799c4d3be Mon Sep 17 00:00:00 2001 From: Ilias Garnier Date: Wed, 27 Jul 2022 16:48:06 +0200 Subject: [PATCH 2/2] lib_benchmark: remove normalize option for solvers --- src/bin_snoop/main_snoop.ml | 9 ++------- src/lib_benchmark/inference.ml | 17 ++++++++--------- src/lib_benchmark/scikit.ml | 9 +++------ src/lib_benchmark/test/test_blake2b.ml | 4 +--- src/lib_benchmark/test/test_inference.ml | 2 +- 5 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/bin_snoop/main_snoop.ml b/src/bin_snoop/main_snoop.ml index e95d5677ae3d..6f5124854877 100644 --- a/src/bin_snoop/main_snoop.ml +++ b/src/bin_snoop/main_snoop.ml @@ -266,15 +266,10 @@ and infer_cmd_full_auto model_name workload_data solver and solver_of_string (solver : string) (infer_opts : Cmdline.infer_parameters_options) = match solver with - | "ridge" -> - Inference.Ridge {alpha = infer_opts.ridge_alpha; normalize = false} + | "ridge" -> Inference.Ridge {alpha = infer_opts.ridge_alpha} | "lasso" -> Inference.Lasso - { - alpha = infer_opts.lasso_alpha; - normalize = false; - positive = infer_opts.lasso_positive; - } + {alpha = infer_opts.lasso_alpha; positive = infer_opts.lasso_positive} | "nnls" -> Inference.NNLS | _ -> Format.eprintf "Unknown solver name.@." ; diff --git a/src/lib_benchmark/inference.ml b/src/lib_benchmark/inference.ml index 0ed3c69528aa..004ba205190e 100644 --- a/src/lib_benchmark/inference.ml +++ b/src/lib_benchmark/inference.ml @@ -44,8 +44,8 @@ type problem = type solution = {mapping : (Free_variable.t * float) list; weights : matrix} type solver = - | Ridge of {alpha : float; normalize : bool} - | Lasso of {alpha : float; normalize : bool; positive : bool} + | Ridge of {alpha : float} + | Lasso of {alpha : float; positive : bool} | NNLS (* -------------------------------------------------------------------------- *) @@ -302,13 +302,13 @@ let wrap_python_solver ~input ~output solver = let output = to_scipy output in solver input output |> of_scipy -let ridge ~alpha ~normalize ~input ~output = +let ridge ~alpha ~input ~output = wrap_python_solver ~input ~output (fun input output -> - Scikit.LinearModel.ridge ~alpha ~normalize ~input ~output ()) + Scikit.LinearModel.ridge ~alpha ~input ~output ()) -let lasso ~alpha ~normalize ~positive ~input ~output = +let lasso ~alpha ~positive ~input ~output = wrap_python_solver ~input ~output (fun input output -> - Scikit.LinearModel.lasso ~alpha ~normalize ~positive ~input ~output ()) + Scikit.LinearModel.lasso ~alpha ~positive ~input ~output ()) let nnls ~input ~output = wrap_python_solver ~input ~output (fun input output -> @@ -321,9 +321,8 @@ let solve_problem : problem -> solver -> solution = | Non_degenerate {input; output; nmap; _} -> let weights = match solver with - | Ridge {alpha; normalize} -> ridge ~alpha ~normalize ~input ~output - | Lasso {alpha; normalize; positive} -> - lasso ~alpha ~normalize ~positive ~input ~output + | Ridge {alpha} -> ridge ~alpha ~input ~output + | Lasso {alpha; positive} -> lasso ~alpha ~positive ~input ~output | NNLS -> nnls ~input ~output in let lines = Maths.row_dim weights in diff --git a/src/lib_benchmark/scikit.ml b/src/lib_benchmark/scikit.ml index 82e1d39bf700..62c7fda1d1e7 100644 --- a/src/lib_benchmark/scikit.ml +++ b/src/lib_benchmark/scikit.ml @@ -36,8 +36,7 @@ module LinearModel = struct assert (l <> 0 && c <> 0) let ridge ~(alpha : float) ?(fit_intercept : bool = false) - ?(normalize : bool = false) ~(input : Scikit_matrix.t) - ~(output : Scikit_matrix.t) () = + ~(input : Scikit_matrix.t) ~(output : Scikit_matrix.t) () = assert_matrix_nontrivial input ; assert_matrix_nontrivial output ; let input = Scikit_matrix.to_numpy input in @@ -50,7 +49,6 @@ module LinearModel = struct [ ("alpha", Py.Float.of_float alpha); ("fit_intercept", Py.Bool.of_bool fit_intercept); - ("normalize", Py.Bool.of_bool normalize); ] in let _ = @@ -64,8 +62,8 @@ module LinearModel = struct | Some coef -> Scikit_matrix.of_numpy (Numpy.transpose coef) let lasso ~(alpha : float) ?(fit_intercept : bool = false) - ?(normalize : bool = false) ?(positive : bool = false) - ~(input : Scikit_matrix.t) ~(output : Scikit_matrix.t) () = + ?(positive : bool = false) ~(input : Scikit_matrix.t) + ~(output : Scikit_matrix.t) () = assert_matrix_nontrivial input ; assert_matrix_nontrivial output ; let input = Scikit_matrix.to_numpy input in @@ -78,7 +76,6 @@ module LinearModel = struct [ ("alpha", Py.Float.of_float alpha); ("fit_intercept", Py.Bool.of_bool fit_intercept); - ("normalize", Py.Bool.of_bool normalize); ("positive", Py.Bool.of_bool positive); ] in diff --git a/src/lib_benchmark/test/test_blake2b.ml b/src/lib_benchmark/test/test_blake2b.ml index e9d1f6952222..53870aa2f3c8 100644 --- a/src/lib_benchmark/test/test_blake2b.ml +++ b/src/lib_benchmark/test/test_blake2b.ml @@ -72,9 +72,7 @@ let solution = Inference.make_problem ~data:workload_data ~model ~overrides:(fun _ -> None) in - let solver = - Inference.Lasso {alpha = 1.0; normalize = false; positive = true} - in + let solver = Inference.Lasso {alpha = 1.0; positive = true} in (* Initialize Python to have access to Scikit's Lasso solver *) Pyinit.pyinit () ; Inference.solve_problem problem solver diff --git a/src/lib_benchmark/test/test_inference.ml b/src/lib_benchmark/test/test_inference.ml index 3e4ae9328e9a..89e0b1c7c232 100644 --- a/src/lib_benchmark/test/test_inference.ml +++ b/src/lib_benchmark/test/test_inference.ml @@ -92,7 +92,7 @@ module T () = struct let {Inference.mapping; weights = _} = Inference.solve_problem problem - (Inference.Lasso {alpha = 1.0; normalize = false; positive = false}) + (Inference.Lasso {alpha = 1.0; positive = false}) let const = List.assoc ~equal:Free_variable.equal fv_const mapping -- GitLab