text
stringlengths
0
601k
let assert_int_equal = assert_equal ~ cmp : Int . equal ~ printer : Int . to_string
let assert_event ? name ? event_type ? timestamp ? tags log_file = let event_json = Json . from_file log_file in let assert_tag ( ( key , value ) , json ) = match Json . Util . to_list json with | [ key_json ; value_json ] -> assert_string_equal key ( Json . Util . to_string key_json ) ; assert_string_equal value ( Jso...
let test_event_format _ = let output_name = Filename . temp_file " event_format " " test " in Profiling . GlobalState . initialize ~ profiling_output : output_name ( ) ; let assert_event ~ name ~ event_type ~ timestamp ~ tags event = PyrePath . ( remove_if_exists ( create_absolute output_name ) ) ; Profiling . log_perf...
let test_event_track _ = let output_name = Filename . temp_file " event_track " " test " in Profiling . GlobalState . initialize ~ profiling_output : output_name ( ) ; let foo x = Profiling . track_duration_event " foo " ~ tags [ " : hello " , " world " ] ~ f ( : fun _ -> x + 1 ) in let y = foo 42 in assert_int_equal 4...
let test_memory_profiling _ = let output_name = Filename . temp_file " event_track " " test " in Profiling . GlobalState . initialize ~ memory_profiling_output : output_name ( ) ; Profiling . track_shared_memory_usage ~ name " : foo " ( ) ; assert_event output_name ~ name " : Shared Memory Usage " ~ tags : [ " used_hea...
let ( ) = " profiling " >::: [ " event_format " >:: test_event_format ; " event_track " >:: test_event_track ; " memory_profiling " >:: test_memory_profiling ; ] |> Test . run
module type LAWS = sig type ( ' a , ' b ) t val dimap_identity : string * ( ( ' a , ' b ) t -> ( ' a , ' b ) t pair ) val contramap_fst_identity : string * ( ( ' a , ' b ) t -> ( ' a , ' b ) t pair ) val map_snd_identity : string * ( ( ' a , ' b ) t -> ( ' a , ' b ) t pair ) val dimap_equality : string * ( ( ' a -> ' b...
module Laws ( P : Preface_specs . PROFUNCTOR ) = struct type ( ' a , ' b ) t = ( ' a , ' b ) P . t let dimap_identity = let lhs x = P . dimap Fun . id Fun . id x in let rhs x = Fun . id x in ( " dimap id id = id " , fun x -> ( lhs x , rhs x ) ) ; ; let contramap_fst_identity = let lhs x = P . contramap_fst Fun . id x i...
type tag = | TId of Id . t * Loc . t | TFun of Id . t * ( Id . t * tag ) list * Loc . t | TInt of string * Loc . t | TReal of string * Loc . t | TBool of string * Loc . t | TString of string * Loc . t
type root = | NotRoot | Root
type used_function = | NotUsed | Used of root | Keep of root
type attr = { loc : Loc . t ; fun_and : bool ; used : used_function ; active : bool ; bound : bool ; ext_fn : string option ; typ : Typ . t option ; tags : tag list ; evaluated : bool ; no_inline : bool ; fun_src : Id . t option }
let pp_attr fmt _ = Format . pp_print_string fmt " attr "
let equal_attr _ _ = true
let compare_attr _ _ = 0
type arg_type = | InputArg | OutputArg | ContextArg
type typed_id = | SimpleId of Id . t * arg_type * attr | TypedId of Id . t * Typ . t list * arg_type * attr
type instance = | NoInst | Named of Id . t | This
type precision = | Float | Fix16
type lhs_exp = | LWild of attr | LId of Id . t * Typ . t option * attr | LTuple of lhs_exp list * attr | LTyped of lhs_exp * Typ . t * attr | LGroup of lhs_exp * attr | LIndex of Id . t * Typ . t option * exp * attr | PUnit of attr | PBool of bool * attr | PInt of int * attr | PReal of float * precision * attr | PStrin...
type exp_list = exp list [ @@ deriving show , eq , ord ]
type scope_kind = | FuncScope | LocalScope
type parser_results = { presult : stmt list ; file : string }
let makeAttr ( loc : Loc . t ) : attr = { loc ; used = NotUsed ; fun_and = false ; active = false ; bound = false ; typ = None ; ext_fn = None ; tags = [ ] ; evaluated = false ; no_inline = false ; fun_src = None }
let emptyAttr = { loc = Loc . default ; fun_and = false ; used = NotUsed ; active = false ; bound = false ; typ = None ; ext_fn = None ; tags = [ ] ; evaluated = false ; no_inline = false ; fun_src = None }
let moduleName ( file : string ) : string = file |> Filename . basename |> Filename . chop_extension |> String . capitalize
let ( ) = let my_name = ref " " in let spf x = Printf . sprintf x in let gettext_args , gettext_copyright = ProgramGettext . Gettext . init in let args = Arg . align ( [ ( " -- my - name " , Arg . String ( fun s -> my_name := s ) , spf ( f_ " name Your name . Default : % S " ) ! my_name ) ; ] @ gettext_args ) in let ( ...
let debug_loading = ref false
let launching_func = ref ( function ( ) -> ( ) )
let load_program ( ) = ! launching_func ( ) ; main_loop ( )
let get_unix_environment ( ) = let f ( vname , vvalue ) = Printf . sprintf " % s =% s " vname ( Filename . quote vvalue ) in String . concat " " ( List . map f ! Debugger_config . environment ) ; ;
let quote_for_windows_shell s = let b = Buffer . create ( 20 + String . length s ) in for i = 0 to String . length s - 1 do begin match s . [ i ] with | ' ' < | ' ' > | ' ' | | ' ' & | ' ' ^ | ' " ' \ -> Buffer . add_char b ' ' ; ^ | _ -> ( ) end ; Buffer . add_char b s . [ i ] ; done ; Buffer . contents b ; ;
let get_win32_environment ( ) = let f ( vname , vvalue ) = Printf . sprintf " set % s =% s " & vname ( quote_for_windows_shell vvalue ) in String . concat " " ( List . map f ! Debugger_config . environment )
let generic_exec_unix cmdline = function ( ) -> if ! debug_loading then prerr_endline " Launching program . . . " ; let child = try fork ( ) with x -> Unix_tools . report_error x ; raise Toplevel in match child with 0 -> begin try match fork ( ) with 0 -> begin try ignore ( setsid ( ) ) with Invalid_argument _ -> ( ) e...
let generic_exec_win cmdline = function ( ) -> if ! debug_loading then prerr_endline " Launching program . . . " ; try ignore ( create_process " cmd . exe " [ | " / C " ; cmdline ( ) ] | stdin stdout stderr ) with x -> Unix_tools . report_error x ; raise Toplevel
let generic_exec = match Sys . os_type with " Win32 " -> generic_exec_win | _ -> generic_exec_unix
let exec_with_runtime = generic_exec ( function ( ) -> match Sys . os_type with " Win32 " -> Printf . sprintf " % sset CAML_DEBUG_SOCKET =% s & % s % s % s " ( get_win32_environment ( ) ) ! socket_name runtime_program ! program_name ! arguments | _ -> Printf . sprintf " % sCAML_DEBUG_SOCKET =% s % s % s % s " ( get_uni...
let exec_direct = generic_exec ( function ( ) -> match Sys . os_type with " Win32 " -> Printf . sprintf " % sset CAML_DEBUG_SOCKET =% s & % s % s " ( get_win32_environment ( ) ) ! socket_name ! program_name ! arguments | _ -> Printf . sprintf " % sCAML_DEBUG_SOCKET =% s % s % s " ( get_unix_environment ( ) ) ! socket_n...
let exec_manual = function ( ) -> print_newline ( ) ; print_string " Waiting for connection . . . " ; print_string ( " ( the socket is " ^ ! socket_name ^ " ) " ) ; print_newline ( )
type launching_function = ( unit -> unit )
let loading_modes = [ " direct " , exec_direct ; " runtime " , exec_with_runtime ; " manual " , exec_manual ]
let set_launching_function func = launching_func := func
let _ = set_launching_function exec_direct
let connection = ref Primitives . std_io
let connection_opened = ref false
let file_name = ref ( None : string option )
let buffer = Bytes . create 1024
let control_connection pid fd = if ( read fd . io_fd buffer 0 1024 ) = 0 then forget_process fd pid else begin prerr_string " Garbage data from process " ; prerr_int pid ; prerr_endline " " end
let accept_connection continue fd = let ( sock , _ ) = accept fd . io_fd in let io_chan = io_channel_of_descr sock in let pid = input_binary_int io_chan . io_in in if pid = - 1 then begin let pid ' = input_binary_int io_chan . io_in in new_checkpoint pid ' io_chan ; Input_handling . add_file io_chan ( control_connectio...
let open_connection address continue = try let ( sock_domain , sock_address ) = convert_address address in file_name := ( match sock_address with ADDR_UNIX file -> Some file | _ -> None ) ; let sock = socket sock_domain SOCK_STREAM 0 in ( try bind sock sock_address ; setsockopt sock SO_REUSEADDR true ; listen sock 3 ; ...
let close_connection ( ) = if ! connection_opened then begin connection_opened := false ; Input_handling . remove_file ! connection ; close_io ! connection ; match ! file_name with Some file -> unlink file | None -> ( ) end
let loaded = ref false
let kill_program ( ) = Breakpoints . remove_all_breakpoints ( ) ; History . empty_history ( ) ; kill_all_checkpoints ( ) ; loaded := false ; close_connection ( )
let ask_kill_program ( ) = if not ! loaded then true else let answer = yes_or_no " A program is being debugged already . Kill it " in if answer then kill_program ( ) ; answer
let initialize_loading ( ) = if ! debug_loading then begin prerr_endline " Loading debugging information . . . " ; Printf . fprintf Stdlib . stderr " \ tProgram : [ % s ] \ n " %! ! program_name ; end ; begin try access ! program_name [ F_OK ] with Unix_error _ -> prerr_endline " Program not found . " ; raise Toplevel ...
let ensure_loaded ( ) = if not ! loaded then begin print_string " Loading program . . . " ; flush Stdlib . stdout ; if ! program_name = " " then begin prerr_endline " No program specified . " ; raise Toplevel end ; try initialize_loading ( ) ; ! launching_func ( ) ; if ! debug_loading then prerr_endline " Waiting for c...
module Mult = struct let [ @ logic ] rec mult ( x : int ) ( y : int ) : int = if y = 0 then 0 else x + mult x ( y - 1 ) let [ @ lemma ] rec mult_commutative ( x : int ) ( y : int ) = if x = y then ( ) else if x = 0 then mult_commutative x ( y - 1 ) else if y < x then mult_commutative y x else ( mult_commutative x ( y -...
module Mirror = struct type ' a tree = Empty | Node of ' a tree * ' a * ' a tree let [ @ logic ] rec mirror ( t : ' a tree ) : ' a tree = match t with | Empty -> Empty | Node ( l , x , r ) -> Node ( mirror r , x , mirror l ) let [ @ lemma ] rec mirror_involutive ( t : ' a tree ) = match t with | Empty -> ( ) | Node ( l...
module AST = struct type op = Add | Mul type expr = Const of int | Var of string | Node of op * expr list type env = string -> int option let [ @ logic ] unit ( op : op ) : int = match op with Add -> 0 | Mul -> 1 let [ @ logic ] rec eval ( e : expr ) ( env : env ) : int = match e with | Const n -> n | Var s -> ( match ...
module PeanoNumbers = struct type unary = Zero | Succ of unary let [ @ logic ] rec to_int ( u : unary ) : int = match u with Zero -> 0 | Succ u ' -> 1 + to_int u ' let [ @ logic ] rec of_int ( n : int ) : unary = if n = 0 then Zero else Succ ( of_int ( n - 1 ) ) let [ @ lemma ] rec to_int_of_int ( n : int ) = if n > 0 ...
type enabled = { mutable token : ProgressToken . t option ; mutable build_counter : int ; report_progress : Server_notification . Progress . t ProgressParams . t -> unit Fiber . t ; create_task : WorkDoneProgressCreateParams . t -> unit Fiber . t }
type t = | Disabled | Enabled of enabled
let create ( client_capabilities : ClientCapabilities . t ) ~ report_progress ~ create_task = match client_capabilities . window with | Some { workDoneProgress = Some true ; _ } -> Enabled { token = None ; build_counter = 0 ; create_task ; report_progress } | _ -> Disabled
let end_build ( t : enabled ) ~ message = Fiber . of_thunk ( fun ( ) -> match t . token with | None -> Fiber . return ( ) | Some token -> t . token <- None ; t . report_progress ( ProgressParams . create ~ token ~ value : ( Server_notification . Progress . End ( WorkDoneProgressEnd . create ~ message ( ) ) ) ) )
let end_build_if_running = function | Disabled -> Fiber . return ( ) | Enabled e -> end_build e ~ message " : Build interrupted "
let start_build ( t : enabled ) = let * ( ) = end_build t ~ message " : Starting new build " in let token = ` String ( " dune - build " - ^ Int . to_string t . build_counter ) in t . token <- Some token ; t . build_counter <- t . build_counter + 1 ; let * ( ) = t . create_task ( WorkDoneProgressCreateParams . create ~ ...
let build_progress t ( progress : Drpc . Progress . t ) = Fiber . of_thunk ( fun ( ) -> match t with | Disabled -> Code_error . raise " progress reporting is not supported " [ ] | Enabled ( { token ; report_progress ; _ } as t ) -> ( match progress with | Success -> end_build t ~ message " : Build finished " | Failed -...
let should_report_build_progress = function | Disabled -> false | Enabled _ -> true
let leaky_relu xs = Tensor . ( max xs ( xs * f 0 . 2 ) )
let pixel_norm xs = Tensor . ( xs / ( sqrt ( mean_dim ( xs * xs ) ~ dim [ : 1 ] ~ keepdim : true ~ dtype ( : T Float ) ) + f 1e - 8 ) )
let w_scale_layer vs ~ size : sz = let vs = Var_store . sub vs " wscale " in let scale_ = Var_store . new_var vs ~ shape [ : 1 ] ~ init : Zeros ~ name " : scale " in let b = Var_store . new_var vs ~ shape [ : sz ] ~ init : Zeros ~ name " : b " in Layer . of_fn ( fun xs -> let b = Tensor . view b ~ size [ : 1 ; sz ; 1 ;...
let norm_conv_block ~ vs ~ ksize ~ padding ~ input_dim dim = let conv = Layer . conv2d_ ( Var_store . sub vs " conv " ) ~ ksize ~ stride : 1 ~ padding ~ use_bias : false ~ input_dim dim in let wscale = w_scale_layer vs ~ size : dim in Layer . of_fn ( fun xs -> pixel_norm xs |> Layer . forward conv |> Layer . forward ws...
let norm_upscale_conv_block ~ vs ~ ksize ~ padding ~ input_dim dim = let conv = Layer . conv2d_ ( Var_store . sub vs " conv " ) ~ ksize ~ stride : 1 ~ padding ~ use_bias : false ~ input_dim dim in let wscale = w_scale_layer vs ~ size : dim in Layer . of_fn ( fun xs -> let _ , _ , h , w = Tensor . shape4_exn xs in pixel...
let create_generator vs = let features_vs = Var_store . sub vs " features " in let features = List . mapi ~ f ( : fun i l -> l ~ vs : Var_store . ( features_vs / Int . to_string i ) ) [ norm_conv_block ~ ksize : 4 ~ padding : 3 ~ input_dim : 512 512 ; norm_conv_block ~ ksize : 3 ~ padding : 1 ~ input_dim : 512 512 ; no...
let ( ) = let module Sys = Caml . Sys in if Array . length Sys . argv <> 2 then Printf . failwithf " usage : % s prog - gan . ot " Sys . argv . ( 0 ) ( ) ; Torch_core . Wrapper . manual_seed 42 ; let vs = Var_store . create ~ name " : vs " ( ) in let generator = create_generator vs in Serialize . load_multi_ ~ named_te...
type bar = { mutable ticks : int ; bar_size : int ; bar_char : char ; }
type text = { mutable last_length : int ; mutable last_event_nb : int ; mutable last_time : float ; }
type t = Bar of bar | Text of text
let inc_tick c = c . ticks <- c . ticks + 1
let create bar_size bar_char = if Unix . isatty Unix . stdout then Text { last_length = 0 ; last_time = - 5 . ; last_event_nb = 0 ; } else let ( ) = for _ = bar_size downto 1 do Format . pp_print_string Format . std_formatter " _ " done in let ( ) = Format . pp_print_newline Format . std_formatter ( ) in Bar { ticks = ...
let pp_not_null f x = match x with | Some x -> Format . fprintf f " ( . % 2f ) " %% ( x . * 100 . ) | None -> ( )
let pp_text delta_t time t_r event e_r f s = let string = Format . asprintf " . % 2f time units % a in % i events % a % t " time pp_not_null t_r event pp_not_null e_r ( fun f -> match delta_t with | None -> ( ) | Some dt -> Format . fprintf f " ( just did . % 1f events / s ) " ( float_of_int ( event - s . last_event_nb...
let rec aux_tick something s n = if n <= 0 then if something then Format . pp_print_flush Format . std_formatter ( ) else ( ) else let ( ) = Format . pp_print_char Format . std_formatter s . bar_char in let ( ) = inc_tick s in aux_tick true s ( pred n )
let tick ~ efficiency time t_r event e_r = function | Bar s -> let n_t = Option_util . unsome 0 . t_r . * ( float_of_int s . bar_size ) in let n_e = Option_util . unsome 0 . e_r . * ( float_of_int s . bar_size ) in aux_tick false s ( int_of_float ( max n_t n_e ) - s . ticks ) | Text s -> let run = Sys . time ( ) in let...
let complete_progress_bar time event t = ( match t with | Bar t -> aux_tick false t ( t . bar_size - t . ticks ) | Text s -> pp_text None time None event None Format . std_formatter s ) ; Format . pp_print_newline Format . std_formatter ( )
let unit_typ = CTSimple " unit "
type parameters = { repl : Replacements . t ; code : Args . code ; cleanup : bool ; shorten : bool ; used : used_function Maps . IdMap . t ; functions : NameTable . t ; variables : NameTable . t ; output_prefix : string option }
type lhs_kind = | Declaration | Other
let makeSingleBlock stmts = match stmts with | [ ] -> CSBlock [ ] | [ stmt ] -> stmt | _ -> CSBlock stmts
let rec tryMakeSwitchLoop id cases next = match next with | None -> Some ( List . rev cases , None ) | Some ( CSIf ( CEOp ( " " , == [ nid ; ( CEInt _ as i ) ] , _ ) , stmt , next ) ) -> if Code . compare_cexp id nid = 0 then tryMakeSwitchLoop id ( ( i , stmt ) :: cases ) next else None | Some def -> Some ( List . rev ...
let tryMakeSwitch e = match e with | CSIf ( CEOp ( " " , == [ var ; ( CEInt _ as i ) ] , _ ) , stmt , next ) -> begin match tryMakeSwitchLoop var [ i , stmt ] next with | Some ( ( _ :: _ :: _ as cases ) , def ) -> CSSwitch ( var , cases , def ) | _ -> e end | _ -> e
let rec makeSwitch p block = match block with | CSBlock stmts -> let stmts = makeSwitchList p stmts in makeSingleBlock stmts | CSIf _ -> tryMakeSwitch block | CSWhile ( cond , body ) -> CSWhile ( cond , makeSwitch p body ) | _ -> block match stmts with | [ ] -> [ ] | h :: t -> makeSwitch p h :: makeSwitchList p t
let convertId ( p : parameters ) ( id : Id . t ) : string = String . concat " _ " id |> Replacements . getKeyword p . repl
let applyOutputPrefix ( p : parameters ) name = match p . output_prefix with | None -> name | Some p -> p ^ name
let applyOutputPrefixToId ( p : parameters ) id = match p . output_prefix with | None -> id | Some p -> match id with | [ _ ] -> id | [ m ; n ] -> [ p ^ m ; n ] | _ -> failwith " applyOutputPrefixToId "
let convertFunctionName ( p : parameters ) ( id : Id . t ) : string = let name = String . concat " _ " id |> Replacements . getKeyword p . repl in applyOutputPrefix p ( if p . shorten then match Maps . IdMap . find_opt id p . used with | Some ( Keep Root ) -> NameTable . registerName p . functions name | _ -> NameTable...
let convertVarId ( p : parameters ) ( is_val : lhs_kind ) ( id : Id . t ) : string list = let should_prefix = match id with | [ m ; _ ] -> m . [ 0 ] <> ' _ ' && String . capitalize_ascii m = m | _ -> false in let name = List . map ( Replacements . getKeyword p . repl ) id in let name = if p . shorten then if is_val = D...
let convertExpId ( p : parameters ) ( is_val : lhs_kind ) ( id : Id . t ) = let l = convertVarId p is_val id in let rec loop l = match l with | [ ] -> failwith " " | [ n ] -> CEVar ( n , CTSimple " none " ) | h :: t -> CEAccess ( loop t , h ) in loop ( List . rev l )
let convertSingleVarId ( p : parameters ) ( id : Id . t ) : string = match id with | [ name ] -> let name = Replacements . getKeyword p . repl name in if p . shorten then NameTable . getOrRegister p . variables name else name | _ -> failwith " ProgToCode . convertSingleVarId : this should be a single identifier "
let rec convertType ( p : parameters ) ( tp : Typ . t ) : type_descr = match ! tp with | Typ . TId ( [ typ ] , _ ) -> let new_type = Replacements . getType p . repl typ in CTSimple new_type | Typ . TId ( [ m ; typ ] , _ ) -> let new_type = Replacements . getType p . repl typ in CTSimple ( applyOutputPrefix p m ^ " _ " ...