Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize memory consumption for verifier subprocess #16201

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9cfeeea
Remove duplicated proof loading from verifier process
dkijania Oct 10, 2024
5cd4d64
Add script to dump memory usage for local network
dkijania Oct 14, 2024
5a5884d
clean up code
dkijania Oct 14, 2024
21aead5
Merge branch 'compatible' into dkijania/optimize_memory_consumption_o…
dkijania Oct 14, 2024
3ffbc4a
reformat
dkijania Oct 14, 2024
73677a7
expse transaction_verification_key and use it for transaction snarks
dkijania Oct 15, 2024
2c0276b
Merge branch 'compatible' into dkijania/optimize_memory_consumption_o…
dkijania Oct 15, 2024
aa673fc
fix unit tests (verifier setup)
dkijania Oct 15, 2024
b43535a
fix proof_level argument
dkijania Oct 15, 2024
b1bc384
WIP
dkijania Oct 17, 2024
2ea71df
Revert "Auxiliary commit to revert individual files from b1bc3844c69e…
dkijania Oct 18, 2024
80a575f
Remove duplicated proof loading from verifier process
dkijania Oct 10, 2024
87d0ef8
Add script to dump memory usage for local network
dkijania Oct 14, 2024
3c054c3
clean up code
dkijania Oct 14, 2024
0faf932
reformat
dkijania Oct 14, 2024
a29bd3d
expse transaction_verification_key and use it for transaction snarks
dkijania Oct 15, 2024
2cb5b35
fix unit tests (verifier setup)
dkijania Oct 15, 2024
d3dbbd3
fix proof_level argument
dkijania Oct 15, 2024
b027b7d
Merge remote-tracking branch 'origin/dkijania/optimize_memory_consump…
dkijania Oct 21, 2024
a749b66
Merge branch 'compatible' into dkijania/optimize_memory_consumption_o…
dkijania Oct 21, 2024
0a30799
Apply suggestions from code review
dkijania Oct 21, 2024
6885222
remove get_verification_keys
dkijania Oct 21, 2024
804d1a4
Merge remote-tracking branch 'origin/dkijania/optimize_memory_consump…
dkijania Oct 21, 2024
732619c
applied review comments
dkijania Oct 21, 2024
b567dc1
remove unused method expose prover in mina_lib
dkijania Oct 21, 2024
9122e38
format
dkijania Oct 21, 2024
a80029b
fix compilation issues for mina_lib tests
dkijania Oct 21, 2024
842b539
reformat
dkijania Oct 21, 2024
cabd22b
move for_test module to sub module of verifier
dkijania Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/app/archive/lib/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ let%test_module "Archive node unit tests" =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
() )

module Genesis_ledger = (val Genesis_ledger.for_unit_tests)

Expand Down
7 changes: 4 additions & 3 deletions src/app/cli/src/cli_entrypoint/mina_cli_entrypoint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1736,10 +1736,11 @@ let internal_commands ~itn_features logger =
| Error err ->
failwithf "Could not parse JSON: %s" err () ) )
in

let%bind verifier =
Verifier.create ~commit_id:Mina_version.commit_id ~logger
~proof_level ~constraint_constants ~pids:(Pid.Table.create ())
~conf_dir:(Some conf_dir) ()
Verifier.For_tests.default ~constraint_constants ~proof_level
~commit_id:Mina_version.commit_id ~logger
~pids:(Pid.Table.create ()) ~conf_dir:(Some conf_dir) ()
in
let%bind result =
let cap lst =
Expand Down
6 changes: 2 additions & 4 deletions src/app/cli/src/init/transaction_snark_profiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ let run ~genesis_constants ~constraint_constants ~proof_level
Parallel.init_master () ;
let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~commit_id:Mina_version.commit_id ~logger ~proof_level
~constraint_constants ~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
() )
Verifier.For_tests.default ~commit_id:Mina_version.commit_id ~logger
~proof_level ~constraint_constants () )
in
let rec go n =
if n <= 0 then ()
Expand Down
6 changes: 2 additions & 4 deletions src/lib/bootstrap_controller/bootstrap_controller.ml
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,8 @@ let%test_module "Bootstrap_controller tests" =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
() )

module Genesis_ledger = (val precomputed_values.genesis_ledger)

Expand Down
6 changes: 2 additions & 4 deletions src/lib/ledger_catchup/normal_catchup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -908,10 +908,8 @@ let%test_module "Ledger_catchup tests" =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
() )

module Context = struct
let logger = logger
Expand Down
6 changes: 2 additions & 4 deletions src/lib/ledger_catchup/super_catchup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1453,10 +1453,8 @@ let%test_module "Ledger_catchup tests" =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
() )

module Context = struct
let logger = logger
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_graphql/mina_graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2663,7 +2663,7 @@ module Queries = struct
~args:Arg.[]
~resolve:(fun { ctx = mina; _ } () ->
let open Deferred.Result.Let_syntax in
Mina_lib.verifier mina |> Verifier.get_blockchain_verification_key
Mina_lib.prover mina |> Prover.get_blockchain_verification_key
|> Deferred.Result.map_error ~f:Error.to_string_hum
>>| Pickles.Verification_key.to_yojson >>| Yojson.Safe.to_basic )

Expand Down
16 changes: 12 additions & 4 deletions src/lib/mina_lib/mina_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1564,15 +1564,23 @@ let create ~commit_id ?wallets (config : Config.t) =
~metadata:[ ("exn", Error_json.error_to_yojson err) ] ) )
(fun () ->
O1trace.thread "manage_verifier_subprocess" (fun () ->
let%bind blockchain_verification_key =
Prover.get_blockchain_verification_key prover
>>| Or_error.ok_exn
in
let%bind transaction_verification_key =
Prover.get_transaction_verification_key prover
>>| Or_error.ok_exn
in
let%bind verifier =
Verifier.create ~commit_id ~logger:config.logger
~enable_internal_tracing:
(Internal_tracing.is_enabled ())
~internal_trace_filename:"verifier-internal-trace.jsonl"
~proof_level:config.precomputed_values.proof_level
~constraint_constants:
config.precomputed_values.constraint_constants
~pids:config.pids ~conf_dir:(Some config.conf_dir) ()
~pids:config.pids ~conf_dir:(Some config.conf_dir)
~blockchain_verification_key
~transaction_verification_key ()
in
let%map () = set_itn_data (module Verifier) verifier in
verifier ) )
Expand Down Expand Up @@ -2250,7 +2258,7 @@ let get_filtered_log_entries
in
(get_from_idx curr_idx messages [], is_started)

let verifier { processes = { verifier; _ }; _ } = verifier
let prover { processes = { prover; _ }; _ } = prover

let vrf_evaluator { processes = { vrf_evaluator; _ }; _ } = vrf_evaluator

Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_lib/mina_lib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ val start_filtered_log : t -> string list -> unit Or_error.t

val get_filtered_log_entries : t -> int -> string list * bool

val verifier : t -> Verifier.t
val prover : t -> Prover.t

val vrf_evaluator : t -> Vrf_evaluator.t

Expand Down
6 changes: 3 additions & 3 deletions src/lib/mina_lib/tests/tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ let%test_module "Epoch ledger sync tests" =

let make_verifier (module Context : CONTEXT) =
let open Context in
Verifier.create ~logger ~proof_level:precomputed_values.proof_level
~constraint_constants:precomputed_values.constraint_constants ~pids
Verifier.For_tests.default ~constraint_constants ~logger
~proof_level:precomputed_values.proof_level ~pids
~conf_dir:(Some (make_dirname "verifier"))
~commit_id:"not specified for unit tests" ()
()

let make_empty_ledger (module Context : CONTEXT) =
Mina_ledger.Ledger.create
Expand Down
6 changes: 2 additions & 4 deletions src/lib/network_pool/batcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,8 @@ module Snark_pool = struct

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger
~proof_level () )

let gen_proofs =
let open Quickcheck.Generator.Let_syntax in
Expand Down
6 changes: 2 additions & 4 deletions src/lib/network_pool/snark_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,8 @@ let%test_module "random set test" =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
() )

module Mock_snark_pool =
Make (Mocks.Base_ledger) (Mocks.Staged_ledger) (Mocks.Transition_frontier)
Expand Down
6 changes: 2 additions & 4 deletions src/lib/network_pool/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ let%test_module "network pool test" =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
() )

module Mock_snark_pool =
Snark_pool.Make (Mocks.Base_ledger) (Mocks.Staged_ledger)
Expand Down
12 changes: 4 additions & 8 deletions src/lib/network_pool/transaction_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1672,10 +1672,8 @@ let%test_module _ =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
() )

let `VK vk, `Prover prover =
Transaction_snark.For_tests.create_trivial_snapp ~constraint_constants ()
Expand Down Expand Up @@ -3090,10 +3088,8 @@ let%test_module _ =
authorization would be rejected" =
Thread_safe.block_on_async_exn (fun () ->
let%bind verifier_full =
Verifier.create ~logger ~proof_level:Full ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" ()
Verifier.For_tests.default ~constraint_constants ~logger
~proof_level:Full ()
in
let%bind test =
setup_test ~verifier:verifier_full
Expand Down
6 changes: 6 additions & 0 deletions src/lib/prover/intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ module type S = sig
sets the process kind for the Itn logger to "prover"
*)
val set_itn_logger_data : t -> daemon_port:int -> unit Deferred.Or_error.t

val get_blockchain_verification_key :
t -> Pickles.Verification_key.t Deferred.Or_error.t

val get_transaction_verification_key :
t -> Pickles.Verification_key.t Deferred.Or_error.t
end
51 changes: 50 additions & 1 deletion src/lib/prover/prover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ module Worker_state = struct
val toggle_internal_tracing : bool -> unit

val set_itn_logger_data : daemon_port:int -> unit

val get_blockchain_verification_key :
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder also what to do in Check | No_check modes. Where we return dummy keys. Maybe this method should return Pickles.Verification_key.t Deferred.t but then in Mina_grahpql modules we are exposing Verifiaction key from verifier so we would need to either break the interface and also there wrap repsone in optional or handle returning diummy as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's update graphql module to use key that is returned from prover. It's okay if it's dummy in case of non-full modes.

It's non-ideal that we create a dummy prover that returns dummy keys. I'd prefer it to be refactored to not initialize prover unless it's full mode. But let's save this idea for one of next PRs, not this one.

unit -> Pickles.Verification_key.t Deferred.t

val get_transaction_verification_key :
unit -> Pickles.Verification_key.t Deferred.t
end

(* bin_io required by rpc_parallel *)
Expand Down Expand Up @@ -103,7 +109,6 @@ module Worker_state = struct
Pickles.Cache_handle.generate_or_load B.cache_handle
|> Promise.to_deferred
in

( module struct
module T = T
module B = B
Expand Down Expand Up @@ -161,6 +166,12 @@ module Worker_state = struct

let set_itn_logger_data ~daemon_port =
Itn_logger.set_data ~process_kind:"prover" ~daemon_port

let get_blockchain_verification_key () =
Lazy.force B.Proof.verification_key

let get_transaction_verification_key () =
Lazy.force T.verification_key
end : S )
| Check ->
Deferred.return
Expand Down Expand Up @@ -202,6 +213,12 @@ module Worker_state = struct
let toggle_internal_tracing _ = ()

let set_itn_logger_data ~daemon_port:_ = ()

let get_blockchain_verification_key () =
Deferred.return (Lazy.force Pickles.Verification_key.dummy)

let get_transaction_verification_key () =
Deferred.return (Lazy.force Pickles.Verification_key.dummy)
end : S )
| No_check ->
Deferred.return
Expand All @@ -221,6 +238,12 @@ module Worker_state = struct
let toggle_internal_tracing _ = ()

let set_itn_logger_data ~daemon_port:_ = ()

let get_blockchain_verification_key () =
Deferred.return (Lazy.force Pickles.Verification_key.dummy)

let get_transaction_verification_key () =
Deferred.return (Lazy.force Pickles.Verification_key.dummy)
end : S )

let get = Fn.id
Expand Down Expand Up @@ -275,6 +298,18 @@ module Functions = struct
let (module M) = Worker_state.get w in
M.set_itn_logger_data ~daemon_port ;
Deferred.unit )

let get_blockchain_verification_key =
create bin_unit [%bin_type_class: Pickles.Verification_key.Stable.Latest.t]
(fun w () ->
let (module M) = Worker_state.get w in
M.get_blockchain_verification_key () )

let get_transaction_verification_key =
create bin_unit [%bin_type_class: Pickles.Verification_key.Stable.Latest.t]
(fun w () ->
let (module M) = Worker_state.get w in
M.get_transaction_verification_key () )
end

module Worker = struct
Expand All @@ -288,6 +323,10 @@ module Worker = struct
; verify_blockchain : ('w, Blockchain.t, unit Or_error.t) F.t
; toggle_internal_tracing : ('w, bool, unit) F.t
; set_itn_logger_data : ('w, int, unit) F.t
; get_blockchain_verification_key :
('w, unit, Pickles.Verification_key.t) F.t
; get_transaction_verification_key :
('w, unit, Pickles.Verification_key.t) F.t
}

module Worker_state = Worker_state
Expand Down Expand Up @@ -316,6 +355,8 @@ module Worker = struct
; verify_blockchain = f verify_blockchain
; toggle_internal_tracing = f toggle_internal_tracing
; set_itn_logger_data = f set_itn_logger_data
; get_blockchain_verification_key = f get_blockchain_verification_key
; get_transaction_verification_key = f get_transaction_verification_key
}

let init_worker_state
Expand Down Expand Up @@ -566,3 +607,11 @@ let toggle_internal_tracing { connection; _ } enabled =
let set_itn_logger_data { connection; _ } ~daemon_port =
Worker.Connection.run connection ~f:Worker.functions.set_itn_logger_data
~arg:daemon_port

let get_blockchain_verification_key { connection; _ } =
Worker.Connection.run connection
~f:Worker.functions.get_blockchain_verification_key ~arg:()

let get_transaction_verification_key { connection; _ } =
Worker.Connection.run connection
~f:Worker.functions.get_transaction_verification_key ~arg:()
14 changes: 5 additions & 9 deletions src/lib/staged_ledger/staged_ledger.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2389,10 +2389,8 @@ let%test_module "staged ledger tests" =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
() )

let find_vk ledger =
Zkapp_command.Verifiable.load_vk_from_ledger ~get:(Ledger.get ledger)
Expand Down Expand Up @@ -5187,12 +5185,10 @@ let%test_module "staged ledger tests" =
(Staged_ledger_diff.With_valid_signatures_and_proofs
.commands diff )
= 1 ) ;

let%bind verifier_full =
Verifier.create ~logger ~proof_level:Full
~constraint_constants ~conf_dir:None
~pids:
(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" ()
Verifier.For_tests.default ~constraint_constants ~logger
~proof_level:Full ()
in
match%map
Sl.apply ~constraint_constants ~global_slot !sl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ let%test_module "transaction_status" =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
() )

let key_gen =
let open Quickcheck.Generator in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -962,10 +962,7 @@ module For_tests = struct

let verifier () =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None
~pids:(Child_processes.Termination.create_pid_table ())
~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level () )

module Genesis_ledger = (val precomputed_values.genesis_ledger)

Expand Down
4 changes: 2 additions & 2 deletions src/lib/transition_handler/catchup_scheduler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ let%test_module "Transition_handler.Catchup_scheduler tests" =

let verifier =
Async.Thread_safe.block_on_async_exn (fun () ->
Verifier.create ~logger ~proof_level ~constraint_constants
~conf_dir:None ~pids ~commit_id:"not specified for unit tests" () )
Verifier.For_tests.default ~constraint_constants ~logger ~proof_level
~pids () )

(* cast a breadcrumb into a cached, enveloped, partially validated transition *)
let downcast_breadcrumb breadcrumb =
Expand Down
Loading