text
stringlengths
0
601k
type create = ? requires_grad : bool -> ? kind : Torch_core . Kind . packed -> ? device : Device . t -> ? scale : float -> int list -> t
let to_type t ~ type_ = totype t ~ scalar_type : type_
let to_kind t ~ kind = totype t ~ scalar_type : kind
let to_device ? device t = match device with | None -> t | Some device -> to_ t ~ device
let float_vec ? kind ? device dims = float_vec ? kind dims |> to_device ? device
let gen ~ f ( ? requires_grad = false ) ( ? kind = Torch_core . Kind . ( T Float ) ) ( ? device = Device . Cpu ) ? scale size = let t = f ~ size ~ options ( : kind , device ) in let t = Option . value_map scale ~ f ( : fun scale -> mul t ( float_vec [ scale ] ~ device ) ) ~ default : t in if requires_grad then set_requ...
let zeros = gen ~ f : zeros
let ones = gen ~ f : ones
let rand = gen ~ f : rand
let randn = gen ~ f : randn
let f v = float_vec [ v ] |> reshape ~ shape [ ] :
let ( + ) = add
let ( - ) = sub
let ( * ) = mul
let ( / ) = div
let ( ~- ) = neg
let ( -= ) t other = ignore ( sub_ t other : t )
let ( += ) t other = ignore ( add_ t other : t )
let ( /= ) t other = ignore ( div_ t other : t )
let ( *= ) t other = ignore ( mul_ t other : t )
let ( = ) = eq_tensor
let pair_to_list ( p1 , p2 ) = [ p1 ; p2 ]
let conv2d ( ? padding = 0 , 0 ) ( ? dilation = 1 , 1 ) ( ? groups = 1 ) input weight bias ~ stride = conv2d input ~ weight ~ bias ~ stride ( : pair_to_list stride ) ~ padding ( : pair_to_list padding ) ~ dilation ( : pair_to_list dilation ) ~ groups
let conv_transpose2d ( ? output_padding = 0 , 0 ) ( ? padding = 0 , 0 ) ( ? dilation = 1 , 1 ) ( ? groups = 1 ) input weight bias ~ stride = conv_transpose2d input ~ weight ~ bias ~ stride ( : pair_to_list stride ) ~ padding ( : pair_to_list padding ) ~ output_padding ( : pair_to_list output_padding ) ~ groups ~ dilati...
let max_pool2d ( ? padding = 0 , 0 ) ( ? dilation = 1 , 1 ) ( ? ceil_mode = false ) ? stride self ~ ksize = max_pool2d self ~ kernel_size ( : pair_to_list ksize ) ~ stride ( : Option . value stride ~ default : ksize |> pair_to_list ) ~ padding ( : pair_to_list padding ) ~ dilation ( : pair_to_list dilation ) ~ ceil_mod...
let avg_pool2d ( ? padding = 0 , 0 ) ( ? count_include_pad = false ) ( ? ceil_mode = false ) ? stride ? divisor_override self ~ ksize = let k1 , k2 = ksize in let divisor_override = Option . value divisor_override ~ default : Int . ( k1 * k2 ) in avg_pool2d self ~ kernel_size ( : pair_to_list ksize ) ~ stride ( : Optio...
let const_batch_norm ( ? momentum = 0 . 1 ) ( ? eps = 1e - 5 ) input = batch_norm input ~ weight : None ~ bias : None ~ running_mean : None ~ running_var : None ~ training : true ~ momentum ~ eps ~ cudnn_enabled : false
let to_bigarray t ~ kind = let bigarray = Bigarray . Genarray . create kind C_layout ( shape t |> Array . of_list ) in copy_to_bigarray ( to_device t ~ device : Cpu ) bigarray ; bigarray
let nll_loss ( ? reduction = Torch_core . Reduction . Elementwise_mean ) xs ~ targets = nll_loss xs ~ target : targets ~ weight : None ~ reduction ~ ignore_index ( :- 100 )
let cross_entropy_for_logits ? reduction logits ~ targets = nll_loss ? reduction ( log_softmax logits ~ dim ( :- 1 ) ~ dtype ( : T Float ) ) ~ targets
let dropout t ~ p ~ is_training = dropout t ~ p ~ train : is_training
let bce_loss ( ? reduction = Torch_core . Reduction . Elementwise_mean ) t ~ targets = binary_cross_entropy t ~ target : targets ~ weight : None ~ reduction
let mse_loss ( ? reduction = Torch_core . Reduction . Elementwise_mean ) t1 t2 = mse_loss t1 ~ target : t2 ~ reduction
let huber_loss ( ? reduction = Torch_core . Reduction . Elementwise_mean ) t1 t2 = let d = abs ( t1 - t2 ) in let half = f 0 . 5 in let err = where_self ~ condition ( : le d ( Scalar . float 1 . ) ) ( half * d * d ) ( d - half ) in match reduction with | None -> err | Elementwise_mean -> mean err | Sum -> sum err
let bce_loss_with_logits ( ? reduction = Torch_core . Reduction . Elementwise_mean ) t ~ targets = let max_val = clamp_min_ ( - t ) ~ min ( : Scalar . float 0 . ) in let one_minus_targets = ones_like targets - targets in let bce = add_ ( add_ ( mul_ one_minus_targets t ) max_val ) ( add_ ( exp_ ( - max_val ) ) ( exp_ (...
let pp formatter t = let shape = shape t in let element_count = List . fold shape ~ init : 1 ~ f : Int . ( * ) in if element_count < 1_000 then ( Caml . Format . pp_print_newline formatter ( ) ; Caml . Format . pp_print_string formatter ( to_string t ~ line_size : 96 ) ; Caml . Format . pp_print_newline formatter ( ) )...
let copy t = let t_ = zeros ( shape t ) ~ kind ( : kind t ) in copy_ t_ ~ src : t ; t_
let shape_str t = List . map ( shape t ) ~ f : Int . to_string |> String . concat ~ sep " , : "
let print_shape ( ? name = " " ) t = Stdio . printf " % s <% s >\ n " %! name ( shape_str t )
let bigarray_to_array1 bigarray ~ f = try let bigarray = Bigarray . array1_of_genarray bigarray in Array . init ( Bigarray . Array1 . dim bigarray ) ~ f ( : fun i -> f bigarray . { i } ) |> Option . some with | Invalid_argument _ -> None
let bigarray_to_array2 bigarray ~ f = try let bigarray = Bigarray . array2_of_genarray bigarray in Array . init ( Bigarray . Array2 . dim1 bigarray ) ~ f ( : fun i -> Array . init ( Bigarray . Array2 . dim2 bigarray ) ~ f ( : fun j -> f bigarray . { i , j } ) ) |> Option . some with | Invalid_argument _ -> None
let bigarray_to_array3 bigarray ~ f = try let bigarray = Bigarray . array3_of_genarray bigarray in Array . init ( Bigarray . Array3 . dim1 bigarray ) ~ f ( : fun i -> Array . init ( Bigarray . Array3 . dim2 bigarray ) ~ f ( : fun j -> Array . init ( Bigarray . Array3 . dim3 bigarray ) ~ f ( : fun k -> f bigarray . { i ...
let to_float1 t = match kind t with | T Float -> to_bigarray t ~ kind : Bigarray . float32 |> bigarray_to_array1 ~ f : Fn . id | T Double -> to_bigarray t ~ kind : Bigarray . float64 |> bigarray_to_array1 ~ f : Fn . id | _ -> None
let to_float2 t = match kind t with | T Float -> to_bigarray t ~ kind : Bigarray . float32 |> bigarray_to_array2 ~ f : Fn . id | T Double -> to_bigarray t ~ kind : Bigarray . float64 |> bigarray_to_array2 ~ f : Fn . id | _ -> None
let to_float3 t = match kind t with | T Float -> to_bigarray t ~ kind : Bigarray . float32 |> bigarray_to_array3 ~ f : Fn . id | T Double -> to_bigarray t ~ kind : Bigarray . float64 |> bigarray_to_array3 ~ f : Fn . id | _ -> None
let to_int1 t = match kind t with | T Int -> to_bigarray t ~ kind : Bigarray . int32 |> bigarray_to_array1 ~ f : Int32 . to_int_exn | T Int64 -> to_bigarray t ~ kind : Bigarray . int64 |> bigarray_to_array1 ~ f : Int64 . to_int_exn | _ -> None
let to_int2 t = match kind t with | T Int -> to_bigarray t ~ kind : Bigarray . int32 |> bigarray_to_array2 ~ f : Int32 . to_int_exn | T Int64 -> to_bigarray t ~ kind : Bigarray . int64 |> bigarray_to_array2 ~ f : Int64 . to_int_exn | _ -> None
let to_int3 t = match kind t with | T Int -> to_bigarray t ~ kind : Bigarray . int32 |> bigarray_to_array3 ~ f : Int32 . to_int_exn | T Int64 -> to_bigarray t ~ kind : Bigarray . int64 |> bigarray_to_array3 ~ f : Int64 . to_int_exn | _ -> None
let to_int1_exn t = Option . value_exn ( to_int1 t )
let to_int2_exn t = Option . value_exn ( to_int2 t )
let to_int3_exn t = Option . value_exn ( to_int3 t )
let to_float1_exn t = Option . value_exn ( to_float1 t )
let to_float2_exn t = Option . value_exn ( to_float2 t )
let to_float3_exn t = Option . value_exn ( to_float3 t )
let to_float0 t = try float_value t |> Option . some with | _ -> None
let to_int0 t = try int_value t |> Option . some with | _ -> None
let of_bigarray ? device ba = of_bigarray ba |> to_device ? device
let of_float0 ? device f = Bigarray . Array0 . of_value Float32 C_layout f |> Bigarray . genarray_of_array0 |> of_bigarray ? device
let of_float1 ? device f = Bigarray . Array1 . of_array Float32 C_layout f |> Bigarray . genarray_of_array1 |> of_bigarray ? device
let of_float2 ? device f = Bigarray . Array2 . of_array Float32 C_layout f |> Bigarray . genarray_of_array2 |> of_bigarray ? device
let of_float3 ? device f = Bigarray . Array3 . of_array Float32 C_layout f |> Bigarray . genarray_of_array3 |> of_bigarray ? device
let of_int0 ? device f = Bigarray . Array0 . of_value Int C_layout f |> Bigarray . genarray_of_array0 |> of_bigarray ? device
let of_int1 ? device f = Bigarray . Array1 . of_array Int C_layout f |> Bigarray . genarray_of_array1 |> of_bigarray ? device
let of_int2 ? device f = Bigarray . Array2 . of_array Int C_layout f |> Bigarray . genarray_of_array2 |> of_bigarray ? device
let of_int3 ? device f = Bigarray . Array3 . of_array Int C_layout f |> Bigarray . genarray_of_array3 |> of_bigarray ? device
let minimum t = reshape t ~ shape [ : - 1 ] |> min_values ~ dim [ : 0 ] ~ keepdim : false
let maximum t = reshape t ~ shape [ : - 1 ] |> max_values ~ dim [ : 0 ] ~ keepdim : false
let flatten t = let batch_size = shape t |> List . hd_exn in view t ~ size [ : batch_size ; - 1 ]
let squeeze_last t = squeeze_dim t ~ dim ( :- 1 )
let scale t f = mul_scalar t ( Scalar . float f )
let eq t1 t2 = if Torch_core . Kind . ( <> ) ( kind t1 ) ( kind t2 ) then false else if Caml . ( <> ) ( shape t1 ) ( shape t2 ) then false else eq_tensor t1 t2 |> all |> to_int0_exn |> ( <> ) 0
let to_list t = let size = match size t with | [ ] -> failwith " scalar tensor " | size :: _ -> size in List . init size ~ f ( : get t )
let t = Tensor . ( f 41 . + f 1 . ) in Stdio . printf " { !% sexp : float } \ n " ( Tensor . to_float0_exn t ) ; [ % expect { | 42 } ] ; | let t = Tensor . float_vec [ 1 . ; 42 . ; 1337 . ] in Stdio . printf " { !% sexp : float array } \ n " Tensor . ( to_float1_exn ( t * t ) ) ; [ % expect { | ( 1 1764 1787569 ) } ] ;...
let npz_tensors ~ filename ~ f = let npz_file = Npy . Npz . open_in filename in let named_tensors = Npy . Npz . entries npz_file |> List . map ~ f ( : fun tensor_name -> f tensor_name ( Npy . Npz . read npz_file tensor_name ) ) in Npy . Npz . close_in npz_file ; named_tensors
let ls files = List . iter files ~ f ( : fun filename -> Stdio . printf " % s :\ n " filename ; let tensor_names_and_shapes = if String . is_suffix filename ~ suffix " . : npz " then npz_tensors ~ filename ~ f ( : fun tensor_name packed_tensor -> match packed_tensor with | Npy . P tensor -> let tensor_shape = Bigarray ...
let npz_to_pytorch npz_src pytorch_dst = let named_tensors = npz_tensors ~ filename : npz_src ~ f ( : fun tensor_name packed_tensor -> match packed_tensor with | Npy . P tensor -> ( match Bigarray . Genarray . layout tensor with | Bigarray . C_layout -> tensor_name , Tensor . of_bigarray tensor | Bigarray . Fortran_lay...
let image_to_tensor image_src pytorch_dst resize = let resize = Option . map resize ~ f ( : fun resize -> match String . split_on_chars resize ~ on [ : ' x ' ; ' , ' ] with | [ w ; h ] -> Int . of_string w , Int . of_string h | _ -> Printf . failwithf " resize should have format WxH , e . g . 64x64 " ( ) ) in let tenso...
let pytorch_to_npz pytorch_src npz_dst = let named_tensors = Serialize . load_all ~ filename : pytorch_src in let npz_file = Npy . Npz . open_out npz_dst in List . iter named_tensors ~ f ( : fun ( tensor_name , tensor ) -> let write kind = let tensor = Tensor . to_bigarray tensor ~ kind in Npy . Npz . write npz_file te...
let ( ) = let open Cmdliner in let ls_cmd = let files = Arg . ( value & ( pos_all file ) [ ] & info [ ] ~ docv " : FILE " ) in let doc = " list tensors in Npz / PyTorch files " in let man = [ ` S " DESCRIPTION " ; ` P " List all the tensors in Npz and PyTorch files . " ] in Term . ( const ls $ files ) , Term . info " l...
type bump_loc = | Body of Util . idx * [ ` N | ` E | ` S | ` W ] | Thumb of Util . idx * [ ` N | ` E | ` S | ` W ]
let find_bump_wall ( walls : Walls . t ) = function | Body ( i , side ) -> Util . idx_to_find i @@ Walls . Sides . get walls . body side | Thumb ( i , side ) -> Util . idx_to_find i @@ Walls . Sides . get walls . thumb side
let default_bumps = [ Thumb ( First , ` W ) ; Thumb ( Last , ` S ) ; Body ( First , ` N ) ; Body ( Idx 3 , ` N ) ; Body ( Last , ` N ) ; Body ( Last , ` S ) ; Body ( Idx 2 , ` S ) ]
let bumpon ( ? n_steps = 5 ) ~ outer_rad ~ inner_rad ~ thickness ~ inset foot = let Points . { top_left ; top_right ; bot_left ; bot_right ; centre } = Points . map ~ f ( : Vec3 . mul ( 1 . , 1 . , 0 . ) ) foot in let normal = Vec3 . ( centre <-> mean [ top_left ; top_right ] |> normalize ) in let base_centre = Vec3 . ...
let make ( ? degrees = 20 . ) ( ? z_offset = 0 . ) ? fastener ( ? foot_thickness = 2 . 4 ) ( ? foot_rad = 6 . ) ( ? bumpon_rad = 5 . 5 ) ( ? bumpon_inset = 0 . 8 ) ( ? bump_locs = default_bumps ) ( case : _ Case . t ) = let bb_index , bb_pinky , rot_sign = let _ , bb_right , _ , bb_left = Util . bounding_box case . con...
module Typeable = struct type ' a ty = | Int : int ty | String : string ty | List : ' a ty -> ' a list ty | Pair : ( ' a ty * ' b ty ) -> ( ' a * ' b ) ty | Fun : ( ' a ty * ' b ty ) -> ( ' a -> ' b ) ty type ( _ , _ ) eq = Eq : ( ' a , ' a ) eq exception CastFailure let rec check_eq : type t t ' . t ty -> t ' ty -> ( ...
module Typeable : sig type ' a ty = Int : int ty | String : string ty | List : ' a ty -> ' a list ty | Pair : ( ' a ty * ' b ty ) -> ( ' a * ' b ) ty | Fun : ( ' a ty * ' b ty ) -> ( ' a -> ' b ) ty type ( _ , _ ) eq = Eq : ( ' a , ' a ) eq exception CastFailure val check_eq : ' t ty -> ' t ' ty -> ( ' t , ' t ' ) eq v...
module HOAS = struct open Typeable type _ term = | Tag : ' t ty * int -> ' t term | Con : ' t -> ' t term | Lam : ' s ty * ( ' s term -> ' t term ) -> ( ' s -> ' t ) term | App : ( ' s -> ' t ) term * ' s term -> ' t term let rec intp : type t . t term -> t = function | Tag ( _ , ix ) -> failwith " HOAS . intp " | Con ...
module HOAS : sig type _ term = Tag : ' t Typeable . ty * int -> ' t term | Con : ' t -> ' t term | Lam : ' s Typeable . ty * ( ' s term -> ' t term ) -> ( ' s -> ' t ) term | App : ( ' s -> ' t ) term * ' s term -> ' t term val intp : ' t term -> ' t end } ] ; ; |
module DeBruijn = struct type ( ' env , ' t ) ix = | ZeroIx : ( ' env * ' t , ' t ) ix | SuccIx : ( ' env , ' t ) ix -> ( ' env * ' s , ' t ) ix let rec to_int : type env t . ( env , t ) ix -> int = function | ZeroIx -> 0 | SuccIx n -> to_int n + 1 type ( ' env , ' t ) term = | Var : ( ' env , ' t ) ix -> ( ' env , ' t...
module DeBruijn : sig type ( ' env , ' t ) ix = ZeroIx : ( ' env * ' t , ' t ) ix | SuccIx : ( ' env , ' t ) ix -> ( ' env * ' s , ' t ) ix val to_int : ( ' env , ' t ) ix -> int type ( ' env , ' t ) term = Var : ( ' env , ' t ) ix -> ( ' env , ' t ) term | Con : ' t -> ( ' env , ' t ) term | Lam : ( ' env * ' s , ' t ...
module Convert = struct type ( _ , _ ) layout = | EmptyLayout : ( ' env , unit ) layout | PushLayout : ' t Typeable . ty * ( ' env , ' env ' ) layout * ( ' env , ' t ) DeBruijn . ix -> ( ' env , ' env ' * ' t ) layout let rec size : type env env ' . ( env , env ' ) layout -> int = function | EmptyLayout -> 0 | PushLayo...
module Convert : sig type ( _ , _ ) layout = EmptyLayout : ( ' env , unit ) layout | PushLayout : ' t Typeable . ty * ( ' env , ' env ' ) layout * ( ' env , ' t ) DeBruijn . ix -> ( ' env , ' env ' * ' t ) layout val size : ( ' env , ' env ' ) layout -> int val inc : ( ' env , ' env ' ) layout -> ( ' env * ' t , ' env ...
module Main = struct open HOAS let i t = Lam ( t , fun x -> x ) let zero t = Lam ( Typeable . Fun ( t , t ) , fun f -> Lam ( t , fun x -> x ) ) let one t = Lam ( Typeable . Fun ( t , t ) , fun f -> Lam ( t , fun x -> App ( f , x ) ) ) let two t = Lam ( Typeable . Fun ( t , t ) , fun f -> Lam ( t , fun x -> App ( f , Ap...
module Main : sig val i : ' a Typeable . ty -> ( ' a -> ' a ) HOAS . term val zero : ' a Typeable . ty -> ( ( ' a -> ' a ) -> ' a -> ' a ) HOAS . term val one : ' a Typeable . ty -> ( ( ' a -> ' a ) -> ' a -> ' a ) HOAS . term val two : ' a Typeable . ty -> ( ( ' a -> ' a ) -> ' a -> ' a ) HOAS . term val three : ' a T...
let rec impl = function | Term ( " byte " , [ ] ) -> term " basic_byte " [ ] | Term ( " boolean " , [ ] ) -> term " basic_boolean " [ ] | Term ( " int16 " , [ ] ) -> term " basic_int16 " [ ] | Term ( " int32 " , [ ] ) -> term " basic_int32 " [ ] | Term ( " int64 " , [ ] ) -> term " basic_int64 " [ ] | Term ( " uint16 "...
let rec intf = function | Term ( " byte " , [ ] ) -> term " char " [ ] | Term ( " boolean " , [ ] ) -> term " bool " [ ] | Term ( " int16 " , [ ] ) -> term " int " [ ] | Term ( " int32 " , [ ] ) -> term " int32 " [ ] | Term ( " int64 " , [ ] ) -> term " int64 " [ ] | Term ( " uint16 " , [ ] ) -> term " int " [ ] | Term...
let rec print_impl top oc = function | Term ( id , [ ] ) -> output_string oc id | Term ( id , tl ) -> if not top then output_char oc ' ( ' ; output_string oc id ; List . iter ( fun t -> output_char oc ' ' ; print_impl false oc t ) tl ; if not top then output_char oc ' ) ' | Tuple [ ] -> if not top then output_char oc '...
let rec print_intf top oc = function | Term ( id , [ ] ) -> output_string oc id | Term ( id , [ t ] ) -> fprintf oc " % a % s " ( print_intf false ) t id | Term ( id , tl ) -> fprintf oc " ( % a ) % s " ( print_seq true " , " ) tl id | Tuple [ ] -> output_string oc " unit " | Tuple tl -> if top then print_seq false " *...
module M : sig include Monad . S val scope : ( S . t m -> ' a m ) -> ' a m val run : tplen : int -> conlen : int -> ' a m -> ' a val lvl : int -> S . t m val tplvl : int -> S . tp m val test : S . t m -> unit struct type local = { tplen : int ; conlen : int } type token = { lvl : int } type ' a m = local -> ' a let ret...
module MU = Monad . Util ( M )