text
stringlengths
0
601k
let test_request_sample_format _ = let sample = Statistics . sample ~ integers [ " : elapsed_time " , 420 ] ~ normals [ " : request kind " , " TypeCheck " ] ~ metadata : false ( ) in let expected_sample = Yojson . Safe . to_string ( ` Assoc [ " int " , ` Assoc [ " elapsed_time " , ` Int 420 ] ; " normal " , ` Assoc [ "...
let test_end_to_end_format _ = let sample = Statistics . sample ~ integers [ " : elapsed_time " , 36 ] ~ normals [ " : root " , " / path / to / source " ] ~ metadata : false ( ) in let expected_sample = Yojson . Safe . to_string ( ` Assoc [ " int " , ` Assoc [ " elapsed_time " , ` Int 36 ] ; " normal " , ` Assoc [ " ro...
let ( ) = " performanceLogger " >::: [ " request_sample_format " >:: test_request_sample_format ; " end_to_end_format " >:: test_end_to_end_format ; ] |> Test . run
let mean xs = let sum = ref 0 . 0 and n = Array . length xs in for i = 0 to n - 1 do sum := ! sum . + xs . ( i ) done ; ! sum . / ( float_of_int n )
let meanf f xs = let sum = ref 0 . 0 and n = Array . length xs in for i = 0 to n - 1 do sum := ! sum . + ( f xs . ( i ) ) done ; ! sum . / ( float_of_int n )
let std ? mean xs = let mean = match mean with | None -> std_mean xs | Some ( mu ) -> mu in let var = ref 0 . 0 and n = Array . length xs in for i = 0 to n - 1 do let x = xs . ( i ) . - mean in var := ! var . + x . * x done ; sqrt ( ! var . / ( float_of_int ( n - 1 ) ) )
let stdf ? mean f xs = let mean = match mean with | None -> meanf f xs | Some ( mu ) -> mu in let n = Array . length xs and sum = ref 0 . 0 in for i = 0 to Array . length xs - 1 do let x = f xs . ( i ) in let dx = x . - mean in sum := ! sum . + dx . * dx done ; sqrt ( ! sum . / ( float_of_int ( n - 1 ) ) )
let pi = 4 . 0 . * ( atan 1 . 0 )
let multi_mean xs = let mu = Array . make ( Array . length xs . ( 0 ) ) 0 . 0 and n = float_of_int ( Array . length xs ) in for i = 0 to Array . length xs - 1 do let x = xs . ( i ) in for j = 0 to Array . length x - 1 do mu . ( j ) <- mu . ( j ) . + x . ( j ) done done ; for i = 0 to Array . length mu - 1 do mu . ( i )...
let multi_std ? mean xs = let mean = match mean with | None -> multi_mean xs | Some ( mu ) -> mu in let n = Array . length xs and dim = Array . length xs . ( 0 ) in let std = Array . make dim 0 . 0 in for i = 0 to n - 1 do let x = xs . ( i ) in for j = 0 to dim - 1 do let dx = x . ( j ) . - mean . ( j ) in std . ( j ) ...
let draw_cauchy x0 gamma = let p = Random . float 1 . 0 in x0 . + gamma . ( * tan ( pi . * ( p . - 0 . 5 ) ) )
let log_cauchy x0 gamma x = let dx = ( x . - x0 ) . / gamma in 0 . 0 . - ( log ( pi . * gamma ) ) . - ( log ( 1 . 0 . + dx . * dx ) )
let log_gaussian mu sigma x = let dx = ( x . - mu ) . / sigma in - 0 . 91893853320467274178 . - ( log sigma ) . - 0 . 5 . * dx . * dx
let log_multi_gaussian mu sigma x = let result = ref 0 . 0 in for i = 0 to Array . length mu - 1 do result := ! result . + log_gaussian mu . ( i ) sigma . ( i ) x . ( i ) done ; ! result . + 0 . 0
let draw_gaussian mu sigma = let rec loop ( ) = let u = Random . float 1 . 0 and v = 1 . 7156 . * ( Random . float 1 . 0 . - 0 . 5 ) in let x = u . - 0 . 449871 and y = abs_float v . + 0 . 386595 in let q = x . * x . + y . ( * 0 . 19600 . * y . - 0 . 25472 . * x ) in if q > 0 . 27597 && ( q > 0 . 27846 || v . * v > ( -...
let draw_uniform a b = let d = b . - a in a . + d . ( * Random . float 1 . 0 )
let all_equal_float ( xs : float array ) start endd = let x0 = xs . ( start ) in let rec ae_loop i = if i >= endd then true else if xs . ( i ) = x0 then ae_loop ( i + 1 ) else false in ae_loop ( start + 1 )
let find_nth ( ? copy = true ) nth ( xs : float array ) = if nth < 0 || nth >= ( Array . length xs ) then raise ( Invalid_argument " find_nth : nth outside array bounds " ) ; let xs = if copy then Array . copy xs else xs in let rec find_nth_loop start nth endd = if endd - start = 1 then if nth <> 0 then raise ( Failure...
let all_equal compare xs start endd = let x0 = xs . ( start ) in let rec ae_loop i = if i >= endd then true else if compare x0 xs . ( i ) = 0 then ae_loop ( i + 1 ) else false in ae_loop ( start + 1 )
let find_nthf ( ? copy = true ) compare nth xs = if nth < 0 || nth >= Array . length xs then raise ( Invalid_argument " find_nthf : nth outside array bounds " ) ; let xs = if copy then Array . copy xs else xs in let rec find_nth_loop start nth endd = if endd - start = 1 then if nth <> 0 then raise ( Failure " find_nthf...
let log_lognormal mu sigma x = let lx = log x in let d = ( lx . - mu ) . / sigma in let ls = log sigma in - 0 . 91893853320467274178 . - lx . - ls . - 0 . 5 . * d . * d
let slow_autocorrelation nslides x = let result = Array . make nslides 0 . 0 and n = Array . length x and mu = mean x and sigma = std x in let sigma2 = sigma . * sigma in assert ( nslides < n ) ; for i = 0 to nslides do for j = 0 to n - 1 - i do let dx = x . ( j ) . - mu and dxshift = x . ( j + i ) . - mu in result . (...
let rec log_sum_logs a b = if a = neg_infinity && b = neg_infinity then neg_infinity else if b > a then log_sum_logs b a else let r = exp ( b . - a ) in a . + ( log1p r )
let test_mean ( ) = let xs = [ | 0 . 0 ; 1 . 0 ; 2 . 0 ; 3 . 0 ] | in assert_equal_float ( 6 . 0 . / 4 . 0 ) ( mean xs )
let test_mean_random ( ) = let xs = Array . init 10000 ( fun _ -> Random . float 1 . 0 ) in assert_equal_float ~ epsrel : 1e - 1 0 . 5 ( mean xs )
let test_std ( ) = let xs = [ | 1 . 0 ; 2 . 0 ; 3 . 0 ; 4 . 0 ; 5 . 0 ] | in assert_equal_float ( ( sqrt 10 . 0 ) . / 2 . 0 ) ( std xs )
let test_std_random ( ) = let xs = Array . init 10000 ( fun _ -> Random . float 1 . 0 ) in assert_equal_float ~ epsrel : 1e - 1 ( sqrt ( 1 . 0 . / 3 . 0 . - 0 . 25 ) ) ( std xs )
let pi = 4 . 0 . ( * atan 1 . 0 )
let test_gaussian ( ) = let gaus mu sigma x = exp ( log_gaussian mu sigma x ) in let mu = Random . float 1 . 0 and sigma = Random . float 1 . 0 in let g0 = ( 1 . 0 . ( ( / sqrt ( 2 . 0 . * pi ) ) . * sigma ) ) in assert_equal_float g0 ( gaus mu sigma mu ) ; assert_equal_float ( g0 . ( * exp ( - 0 . 5 ) ) ) ( gaus mu si...
let test_gaussian_random ( ) = let mu = Random . float 1 . 0 and sigma = Random . float 1 . 0 in let xs = Array . init 10000 ( fun _ -> draw_gaussian mu sigma ) in let mean = mean xs and std = std xs in assert_equal_float ~ epsabs : 0 . 1 mu mean ; assert_equal_float ~ epsabs : 0 . 1 sigma std
let test_multi_mean ( ) = let xs = [ [ || 0 . 0 ; 1 . 0 ] ; | [ | 2 . 0 ; 3 . 0 ] ; | [ | 4 . 0 ; - 5 . 0 ] ] || in let mu = multi_mean xs in assert_equal_float 2 . 0 mu . ( 0 ) ; assert_equal_float ( - 1 . 0 . / 3 . 0 ) mu . ( 1 )
let test_multi_std ( ) = let xs = [ [ || 0 . 662891 ; 0 . 218155 ; 0 . 464706 ; 0 . 148477 ; 0 . 39616 ] ; | [ | 0 . 43397 ; 0 . 161041 ; 0 . 625332 ; 0 . 508765 ; 0 . 261084 ] ; | [ | 0 . 147267 ; 0 . 403388 ; 0 . 643601 ; 0 . 892214 ; 0 . 269893 ] ] || in let std = multi_std xs in assert_equal_float_array ~ epsabs : ...
let test_meanf_stdf ( ) = let xs = Array . init 1000 ( fun _ -> Random . float 1 . 0 ) in assert_equal_float ( mean xs ) ( meanf ( fun x -> x ) xs ) ; assert_equal_float ( std ~ mean ( : mean xs ) xs ) ( stdf ~ mean ( : mean xs ) ( fun x -> x ) xs )
let fcompare ( x : float ) y = Pervasives . compare x y
let test_find_nth ( ) = for i = 0 to 100 do let n = 10 + Random . int 25 in let xs = Array . init n ( fun _ -> Random . float 1 . 0 ) in let sxs = Array . copy xs in let cxs = Array . copy xs in Array . fast_sort fcompare sxs ; let i = Random . int n in assert_equal ~ cmp ( ) := ~ printer : string_of_float sxs . ( i ) ...
let icompare ( x : int ) y = Pervasives . compare x y
let test_find_nthf ( ) = for i = 0 to 100 do let n = 10 + Random . int 25 in let xs = Array . init n ( fun _ -> Random . int 100 ) in let sxs = Array . copy xs and cxs = Array . copy xs in Array . fast_sort icompare sxs ; let i = Random . int n in assert_equal ~ cmp ( ) := ~ printer : string_of_int sxs . ( i ) ( find_n...
let test_log_lognormal ( ) = let mu = 0 . 328077 and sigma = 0 . 330877 and x = 0 . 0553941 in let ln = log_lognormal mu sigma x in assert_equal_float ~ epsabs : 1e - 3 ( - 44 . 3128 ) ln
let test_slow_autocorrelation ( ) = let data = [ |- 1 . 44898 ; - 0 . 0762953 ; 2 . 25525 ; - 0 . 284584 ; 1 . 16297 ; 0 . 00864677 ; 0 . 211493 ] | in let ac = Stats . slow_autocorrelation 3 data in assert_equal_float_array ~ epsrel : 1e - 5 [ | 1 . 000000000000000 ; - 0 . 117926565125732 ; - 0 . 043808623348460 ] | a...
let test_log_sum_logs ( ) = for i = 0 to 100 do let x = Random . float 1 . 0 and y = Random . float 1 . 0 in let logx = log x and logy = log y in assert_equal_float ( log ( x . + y ) ) ( log_sum_logs logx logy ) done
let test_log_sum_logs_zero ( ) = let result = log_sum_logs neg_infinity neg_infinity in assert_bool ( Printf . sprintf " expected - inf , but got % g " result ) ( result = neg_infinity )
let tests = " stats . ml tests " >::: [ " mean " >:: test_mean ; " randomized mean " >:: test_mean_random ; " std " >:: test_std ; " randomized std " >:: test_std_random ; " gaussian dist " >:: test_gaussian ; " randomized gaussian draws " >:: test_gaussian_random ; " multi_mean " >:: test_multi_mean ; " meanf and stdf...
type code = | OK | Cancelled | Unknown | Invalid_argument | Deadline_exceeded | Not_found | Already_exists | Permission_denied | Resource_exhausted | Failed_precondition | Aborted | Out_of_range | Unimplemented | Internal | Unavailable | Data_loss | Unauthenticated
let int_of_code = function | OK -> 0 | Cancelled -> 1 | Unknown -> 2 | Invalid_argument -> 3 | Deadline_exceeded -> 4 | Not_found -> 5 | Already_exists -> 6 | Permission_denied -> 7 | Resource_exhausted -> 8 | Failed_precondition -> 9 | Aborted -> 10 | Out_of_range -> 11 | Unimplemented -> 12 | Internal -> 13 | Unavail...
let code_of_int = function | 0 -> Some OK | 1 -> Some Cancelled | 2 -> Some Unknown | 3 -> Some Invalid_argument | 4 -> Some Deadline_exceeded | 5 -> Some Not_found | 6 -> Some Already_exists | 7 -> Some Permission_denied | 8 -> Some Resource_exhausted | 9 -> Some Failed_precondition | 10 -> Some Aborted | 11 -> Some O...
type t = { code : code ; message : string option } [ @@ deriving show ]
let v ? message code = { code ; message }
let code t = t . code
let message t = Option . map ( fun message -> Uri . pct_encode message ) t . message
let gc_stat_encoding = let open Data_encoding in conv ( fun { minor_words ; promoted_words ; major_words ; minor_collections ; major_collections ; forced_major_collections ; heap_words ; heap_chunks ; live_words ; live_blocks ; free_words ; free_blocks ; largest_free ; fragments ; compactions ; top_heap_words ; stack_s...
let proc_stat_encoding = let open Memory in let open Data_encoding in union ~ tag_size ` : Uint8 [ case ( Tag 0 ) ( conv ( fun { page_size ; size ; resident ; shared ; text ; lib ; data ; dt } -> ( page_size , size , resident , shared , text , lib , data , dt ) ) ( fun ( page_size , size , resident , shared , text , li...
module S = struct let gc = RPC_service . get_service ~ description " : Gets stats from the OCaml Garbage Collector " ~ query : RPC_query . empty ~ output : gc_stat_encoding RPC_path . ( root / " stats " / " gc " ) let memory = RPC_service . get_service ~ description " : Gets memory usage stats " ~ query : RPC_query . e...
let gc ctxt = RPC_context . make_call S . gc ctxt ( ) ( ) ( )
let memory ctxt = RPC_context . make_call S . memory ctxt ( ) ( ) ( )
type ' a result = ( ' a , [ ` Msg of string ] ) Result . t
type ' kind buffer = ( ' a , ' b , c_layout ) Array1 . t constraint ' kind = ( ' a , ' b ) kind
type ' kind t = { width : int ; height : int ; channels : int ; offset : int ; stride : int ; data : ' kind buffer }
type float32 = ( float , float32_elt ) kind
type int8 = ( int , int8_unsigned_elt ) kind : ? channels : int -> _ buffer -> int8 t result = " ml_stbi_load_mem " : ? channels : int -> _ buffer -> float32 t result = " ml_stbi_loadf_mem "
let free_unmanaged image = ml_stbi_image_free image . data
let clone buf = let buf ' = Array1 . create ( Array1 . kind buf ) c_layout ( Array1 . dim buf ) in Array1 . blit buf buf ' ; buf '
let manage f ? channels filename = match f ? channels filename with | Result . Error _ as err -> err | Result . Ok image -> let managed = { image with data = clone image . data } in free_unmanaged image ; Result . Ok managed
let load ? channels filename = manage load_unmanaged ? channels filename
let loadf ? channels filename = manage loadf_unmanaged ? channels filename
let decode ? channels filename = manage decode_unmanaged ? channels filename
let decodef ? channels filename = manage decodef_unmanaged ? channels filename
let image ~ width ~ height ~ channels ( ? offset = 0 ) ( ? stride = width * channels ) data = let size = Array1 . dim data in if width < 0 then Result . Error ( ` Msg " width should be positive " ) else if height < 0 then Result . Error ( ` Msg " height should be positive " ) else if channels < 0 || channels > 4 then R...
let width t = t . width
let height t = t . height
let channels t = t . channels
let data t = t . data
let validate_mipmap t1 t2 = if t1 . channels <> t2 . channels then invalid_arg " mipmap : images have different number of channels " ; if t1 . width / 2 <> t2 . width || t1 . height / 2 <> t2 . height then invalid_arg " mipmap : second image size should exactly be half of first image "
let mipmap t1 t2 = validate_mipmap t1 t2 ; mipmap t1 t2
let mipmapf t1 t2 = validate_mipmap t1 t2 ; mipmapf t1 t2
type ' kind buffer = ( ' a , ' b , c_layout ) Array1 . t constraint ' kind = ( ' a , ' b ) kind
type float32 = ( float , float32_elt ) kind
type int8 = ( int , int8_unsigned_elt ) kind : string -> w : int -> h : int -> c : int -> int8 buffer -> unit = " ml_stbi_write_png " : string -> w : int -> h : int -> c : int -> int8 buffer -> unit = " ml_stbi_write_bmp " : string -> w : int -> h : int -> c : int -> int8 buffer -> unit = " ml_stbi_write_tga " : string...
type ' a return = { return : ' b . ' a -> ' b }
let with_return ( type a ) f = let module E = struct exception E of a end in try f { return = ( fun x -> raise_notrace ( E . E x ) ) } with E . E r -> r
let finally ~ h f = match f ( ) with | exception exn -> h ( ) ; raise exn | r -> h ( ) ; r
module Util = struct module Lo = Location module Le = Lexing open Mparsetree . Ast_cur let with_loc loc f = let old_loc = ! Ast_helper . default_loc in Ast_helper . default_loc := loc ; finally ~ h ( : fun ( ) -> Ast_helper . default_loc := old_loc ) f let error ( ? loc = ! Ast_helper . default_loc ) fmt = Format . ksp...
module Result = struct type ( ' a , ' b ) result = ( ' a , ' b ) CCResult . t = | Ok of ' a | Error of ' b end
module Various = struct let use_threads ( ) = match ! Options . findlib_pkgs with | [ ] -> false | f_pkgs -> let pkgs = match Findlib . package_deep_ancestors [ " byte " ] f_pkgs with | exception Fl_package_base . No_such_package _ -> f_pkgs | d -> d in List . exists ( function " threads " | " threads . posix " -> true...
let eq_int : int -> int -> bool = ( = )
let eq_int32 : int32 -> int32 -> bool = ( = )
let eq_int64 : int64 -> int64 -> bool = ( = )
let eq_nativeint : nativeint -> nativeint -> bool = ( = )
let eq_float : float -> float -> bool = ( = )
let eq_string : string -> string -> bool = ( = )
let eq_bool : bool -> bool -> bool = ( = )
let eq_char : char -> char -> bool = ( = )
let eq_unit : unit -> unit -> bool = fun _ _ -> true
let pp_print_int32 : Format . formatter -> int32 -> unit = fun fmt a -> Format . fprintf fmt " % ld " a
let pp_print_int64 : Format . formatter -> int64 -> unit = fun fmt a -> Format . fprintf fmt " % Ld " a
let pp_print_nativeint : Format . formatter -> nativeint -> unit = fun fmt a -> Format . fprintf fmt " % nd " a
let pp_print_string : Format . formatter -> string -> unit = fun fmt a -> Format . fprintf fmt " % S " a
let pp_print_unit : Format . formatter -> unit -> unit = fun fmt _ -> Format . fprintf fmt " ( ) "
let eq_option mf_a x y = match ( x , y ) with | ( None , None ) -> true | ( Some x , Some y ) -> mf_a x y | ( _ , _ ) -> false
let eq_ref mf_a x y = mf_a ( ! x ) ( ! y )
let pp_print_option mf_a fmt v = match v with | None -> fprintf fmt " None " | Some v -> fprintf fmt " Some [ @% a ] " @ mf_a v