Skip to content

Commit 0a47d49

Browse files
committed
Get rid of unsafe_cleanup
This is just a "dumb delete", might need a followup but nothing obvious. E.g. in the gccjit backend, `root_ctx` persists "forever".
1 parent 651f631 commit 0a47d49

15 files changed

+9
-56
lines changed

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
- New files: split out `backend_types.ml` from `backends.ml`; moved `Tnode.task` to `task.ml`; renamed `backend_utils.ml` to `c_syntax.ml`.
1414
- Removed half-static verification of merge buffer nodes inside `device_to_device`.
1515
- Fixed #286: cross-stream-sharing incorporated into `Tnode.memory_mode`.
16-
- TODO: Moved the multicore backend from a `device = stream` model to a single device model.
16+
- Moved the multicore backend from a `device = stream` model to a single device model.
17+
- Got rid of `unsafe_cleanup`.
1718
- TODO: Built per-tensor-node stream-to-stream synchronization into device-to-device copying functions, removed obsolete blocking synchronizations.
1819

1920
### Fixed

arrayjit/lib/backend_types.ml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ module type Backend_common = sig
5454
val name : string
5555

5656
val initialize : Types.config -> unit
57-
(** Initializes a backend before first use or (on some backends) after {!unsafe_cleanup}. Does
58-
nothing if the backend is already initialized. *)
57+
(** Initializes a backend before first use. Typically does nothing if the backend is already
58+
initialized, but some backends can do some safe cleanups. *)
5959

6060
val is_initialized : unit -> bool
61-
(** Returns false if there was no previous {!initialize} call, or, on some backends, the most
62-
recent call was followed by {!unsafe_cleanup}. If it returns false, one must call
61+
(** Returns false if there was no previous {!initialize} call. If it returns false, one must call
6362
{!initialize} before using the backend. *)
6463

6564
val init : init_info -> context
@@ -88,11 +87,6 @@ module type Backend_common = sig
8887
compile time and debugging convenience by generating fewer files -- ideally does not affect
8988
execution, but there can be backend-specific differences. Only array entries for which
9089
[occupancy] returns true are included. *)
91-
92-
val unsafe_cleanup : unit -> unit
93-
(** Cleans up all work on a backend, releases resources. All previously retrieved values
94-
(contexts, streams and devices) become invalid. The backend needs to be initialized again to
95-
be used again. *)
9690
end
9791

9892
module type No_device_backend = sig
@@ -236,7 +230,6 @@ module type Lowered_backend_common = sig
236230
val is_initialized : unit -> bool
237231
val init : init_info -> context
238232
val finalize : context -> unit
239-
val unsafe_cleanup : unit -> unit
240233
val name : string
241234
end
242235

arrayjit/lib/backends.ml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,6 @@ struct
320320
Stdlib.Condition.broadcast stream.state.dev_wait_for_work;
321321
Domain.join stream.domain
322322

323-
let%track2_sexp unsafe_cleanup () =
324-
latest_subordinal := 0;
325-
Backend.unsafe_cleanup ()
326-
327323
let get_device ~ordinal =
328324
if ordinal <> 0 then
329325
invalid_arg [%string "Multicore_backend.get_device %{ordinal#Int}: only device 0 exists"];
@@ -478,10 +474,6 @@ module Sync_backend (Backend : Backend_types.No_device_backend) : Backend_types.
478474
let suggested_num_streams _device = !sync_suggested_num_streams
479475
let next_stream_id = ref 0
480476

481-
let unsafe_cleanup () =
482-
next_stream_id := 0;
483-
Backend.unsafe_cleanup ()
484-
485477
let get_device ~ordinal =
486478
if ordinal <> 0 then invalid_arg "Sync_backend backends only have device number 0";
487479
CPU
@@ -807,7 +799,6 @@ let reinitialize (module Backend : Backend_types.Backend) config =
807799
if not @@ Backend.is_initialized () then Backend.initialize config
808800
else (
809801
Core.Gc.full_major ();
810-
Backend.unsafe_cleanup ();
811802
Backend.initialize config)
812803

813804
(** Reinitializes and returns a backend corresponding to [backend_name], or if omitted, selected via

arrayjit/lib/cc_backend.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ let to_buffer tn ~dst ~src =
4040

4141
let host_to_buffer src ~dst = Ndarray.map2 { f2 = Ndarray.A.blit } src dst
4242
let buffer_to_host dst ~src = Ndarray.map2 { f2 = Ndarray.A.blit } src dst
43-
let unsafe_cleanup () = Stdlib.Gc.compact ()
4443

4544
let is_initialized, initialize =
4645
let initialized = ref false in

arrayjit/lib/cuda_backend.cudajit.ml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,6 @@ let init stream =
235235
Stdlib.Gc.finalise finalize ctx;
236236
ctx
237237

238-
let unsafe_cleanup () =
239-
let len = Core.Weak.length !devices in
240-
(* NOTE: releasing the context should free its resources, there's no need to finalize the
241-
remaining contexts, and [finalize], [finalize_device] will not do anything for a [released]
242-
device. *)
243-
for i = 0 to len - 1 do
244-
Option.iter (Core.Weak.get !devices i) ~f:(fun device ->
245-
if Atomic.compare_and_set device.released false true then cleanup_device device)
246-
done;
247-
Core.Weak.fill !devices 0 len None;
248-
Stdlib.Gc.compact ()
249-
250238
let%diagn2_l_sexp from_host (ctx : context) tn =
251239
match (tn, Map.find ctx.ctx_arrays tn) with
252240
| { Tn.array = (lazy (Some hosted)); _ }, Some dst ->

arrayjit/lib/cuda_backend.missing.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ let link_batch (Unimplemented_ctx : context) (code_batch : code_batch) =
5252
in
5353
((Unimplemented_ctx : context), lowered_bindings, task)
5454

55-
let unsafe_cleanup () = ()
5655
let from_host _context _tn = false
5756
let to_host _context _tn = false
5857
let device_to_device _tn ~into_merge_buffer:_ ~dst:_ ~src:_ = false

arrayjit/lib/gcc_backend.gccjit.ml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ let to_buffer tn ~dst ~src =
5555
let host_to_buffer src ~dst = Ndarray.map2 { f2 = Ndarray.A.blit } src dst
5656
let buffer_to_host dst ~src = Ndarray.map2 { f2 = Ndarray.A.blit } src dst
5757

58-
let unsafe_cleanup () =
59-
let open Gccjit in
60-
Option.iter ~f:Context.release !root_ctx;
61-
root_ctx := None;
62-
Stdlib.Gc.compact ()
63-
6458
let is_initialized () = Option.is_some !root_ctx
6559

6660
let initialize () =

arrayjit/lib/gcc_backend.missing.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,3 @@ let initialize () = ()
4747
let is_initialized () = true
4848
let init _ = Unimplemented_ctx
4949
let finalize Unimplemented_ctx = ()
50-
let unsafe_cleanup () = ()

bin/compilation_speed.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ let benchmark_overhead backend () =
7676
in
7777
PrintBox_text.output Stdio.stdout plot_box;
7878
Stdio.print_endline "\n";
79-
let module Backend = (val backend) in
80-
Backend.unsafe_cleanup ();
8179
result
8280

8381
let benchmarks =

bin/einsum_trivia.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ let _suspended () =
3939
let%op ho2 = hey2 ++ "ab|cd->ef => cf|ae->db" in
4040
Utils.capture_stdout_logs @@ fun () ->
4141
Train.forward_and_forget backend ctx ho2;
42-
Tensor.print ~force:true ~with_code:false ~with_grad:false `Default @@ ho2;
43-
Backend.unsafe_cleanup ()
42+
Tensor.print ~force:true ~with_code:false ~with_grad:false `Default @@ ho2
4443

4544
let () =
4645
Utils.set_log_level 2;

0 commit comments

Comments
 (0)