text
stringlengths
0
601k
module Mix ( X : T ) ( Y : T with type t = private [ > ] ~ [ X . t ] ) : T with type t = [ X . t | Y . t ] = struct type t = [ X . t | Y . t ] let show = function # X . t as x -> X . show x | # Y . t as y -> Y . show y end ; ;
module M : sig type t = private [ > ` A ] end = struct type t = [ ` A ] end
module M ' : sig type t = private [ > ] end = struct type t = [ M . t | ` A ] end ; ;
type t = private [ > ]
type u = private [ > ` A of int ] ~ [ t ] ; ;
type t = private [ > ` A of int ]
type u = private [ > ` A of int ] ~ [ t ] ; ;
module F ( X : sig type t = private [ > ] ~ [ ` A ; ` B ; ` C ; ` D ] type u = private [ > ` A ` | B ` | C ] ~ [ t ; ` D ] open X let f = function # u -> 1 | # t -> 2 | ` D -> 3 let g = function # u |# t ` | D -> 2 type v = [ t | u ` | D ] end
module M = struct type t = private [ > ` A ] end ; ;
module M ' : sig type t = private [ > ] ~ [ ` A ] end = M ; ;
module type T = sig type t = private [ > ] ~ [ ` A ] end ; ;
module type T ' = T with type t = private [ > ` A ] ; ;
type t = private [ > ] ~ [ ` A ]
let f = function ` A x -> x | # t -> 0
type t ' = private [ < ` A of int | t ] ; ;
module F ( X : sig end ) : sig type t = private [ > ] type u = private [ > ] ~ [ t ] end = struct type t = [ ` A ] type u = [ ` B ] end
let f = function # M . t -> 1 | # M . u -> 2
let f = function # M . t -> 1 | _ -> 2
type t = [ M . t | M . u ]
let f = function # t -> 1 | _ -> 2 ; ;
module G ( X : sig type t = private [ > ] type u = private [ > ] ~ [ t ] end ) = struct let f = function # X . t -> 1 | _ -> 2 end ; ;
module M1 = G ( struct module N = F ( String ) type t = N . t type u = N . u end ) ; ;
module M1 = G ( struct type t = M . t type u = M . u end ) ; ;
let f = function # F ( String ) . t -> 1 | _ -> 2 ; ;
type t = [ F ( String ) . t | M . u ]
let f = function # t -> 1 | _ -> 2 ; ;
module N : sig type t = private [ > ] end = struct type t = [ F ( String ) . t | M . u ] end ; ;
type a = [ ` A of int | ` B ]
type b = [ ` A of bool | ` B ]
type c = private [ > ] ~ [ a ; b ]
let f = function # c -> 1 | ` A x -> truncate x
type d = private [ > ] ~ [ a ]
let g = function # d -> 1 | ` A x -> truncate x ; ;
type num = [ ` Num of int ]
module type Exp = sig type t = private [ > num ] val eval : t -> t val show : t -> string end
module Num ( X : Exp ) = struct type t = num let eval ( ` Num _ as x ) : X . t = x let show ( ` Num n ) = string_of_int n end
type ' a add = [ ` Add of ' a * ' a ]
module Add ( X : Exp with type t = private [ > num | ' a add ] as ' a ) = struct type t = X . t add let show ( ` Add ( e1 , e2 ) : t ) = " ( " ^ X . show e1 " " ^+^ X . show e2 " ) " ^ let eval ( ` Add ( e1 , e2 ) : t ) = let e1 = X . eval e1 and e2 = X . eval e2 in match e1 , e2 with ` Num n1 , ` Num n2 -> ` Num ( n1 + n2 ) | ` Num 0 , e | e , ` Num 0 -> e | e12 -> ` Add e12
type ' a mul = [ ` Mul of ' a * ' a ]
module Mul ( X : Exp with type t = private [ > num | ' a mul ] as ' a ) = struct type t = X . t mul let show ( ` Mul ( e1 , e2 ) : t ) = " ( " ^ X . show e1 " " ^*^ X . show e2 " ) " ^ let eval ( ` Mul ( e1 , e2 ) : t ) = let e1 = X . eval e1 and e2 = X . eval e2 in match e1 , e2 with ` Num n1 , ` Num n2 -> ` Num ( n1 * n2 ) | ` Num 0 , e | e , ` Num 0 -> ` Num 0 | ` Num 1 , e | e , ` Num 1 -> e | e12 -> ` Mul e12 end
module Ext ( X : sig type t = private [ > ] end ) ( Y : sig type t end ) = struct module type S = sig type t = private [ > ] ~ [ X . t ] val eval : t -> Y . t val show : t -> string end end
module Dummy = struct type t = [ ` Dummy ] end
module Mix ( E : Exp ) ( E1 : Ext ( Dummy ) ( E ) . S ) ( E2 : Ext ( E1 ) ( E ) . S ) = struct type t = [ E1 . t | E2 . t ] let eval = function # E1 . t as x -> E1 . eval x | # E2 . t as x -> E2 . eval x let show = function # E1 . t as x -> E1 . show x | # E2 . t as x -> E2 . show x end
module rec EAdd : ( Exp with type t = [ num | EAdd . t add ] ) = Mix ( EAdd ) ( Num ( EAdd ) ) ( Add ( EAdd ) )
module rec E : Exp with type t = [ num | E . t add | E . t mul ] = Mix ( E ) ( Mix ( E ) ( Num ( E ) ) ( Add ( E ) ) ) ( Mul ( E ) )
let e = E . eval ( ` Add ( ` Mul ( ` Num 2 , ` Num 3 ) , ` Num 1 ) )
module rec E : ( Exp with type t = [ num | E . t add | E . t mul ] ) = struct module E1 = Num ( E ) module E2 = Add ( E ) module E3 = Mul ( E ) type t = E . t let show = function | # num as x -> E1 . show x | # add as x -> E2 . show x | # mul as x -> E3 . show x let eval = function | # num as x -> E1 . eval x | # add as x -> E2 . eval x | # mul as x -> E3 . eval x end
module type T = sig type t = private [ > ] end
module type Tnum = sig type t = private [ > num ] end
module Ext ( E : Tnum ) = struct module type S = functor ( Y : Exp with type t = E . t ) -> sig type t = private [ > num ] val eval : t -> Y . t val show : t -> string end end
module Ext ' ( E : Tnum ) ( X : T ) = struct module type S = functor ( Y : Exp with type t = E . t ) -> sig type t = private [ > ] ~ [ X . t ] val eval : t -> Y . t val show : t -> string end end
module Mix ( E : Exp ) ( F1 : Ext ( E ) . S ) ( F2 : Ext ' ( E ) ( F1 ( E ) ) . S ) = struct module E1 = F1 ( E ) module E2 = F2 ( E ) type t = [ E1 . t | E2 . t ] let eval = function # E1 . t as x -> E1 . eval x | # E2 . t as x -> E2 . eval x let show = function # E1 . t as x -> E1 . show x | # E2 . t as x -> E2 . show x end
module Join ( E : Exp ) ( F1 : Ext ( E ) . S ) ( F2 : Ext ' ( E ) ( F1 ( E ) ) . S ) ( E ' : Exp with type t = E . t ) = Mix ( E ) ( F1 ) ( F2 )
module rec EAdd : ( Exp with type t = [ num | EAdd . t add ] ) = Mix ( EAdd ) ( Num ) ( Add )
module rec EMul : ( Exp with type t = [ num | EMul . t mul ] ) = Mix ( EMul ) ( Num ) ( Mul )
module rec E : ( Exp with type t = [ num | E . t add | E . t mul ] ) = Mix ( E ) ( Join ( E ) ( Num ) ( Add ) ) ( Mul )
module LExt ( X : T ) = struct module type S = sig type t val eval : t -> X . t val show : t -> string end end
module LNum ( E : Exp ) ( X : LExt ( E ) . S with type t = private [ > ] ~ [ num ] ) = struct type t = [ num | X . t ] let show = function ` Num n -> string_of_int n | # X . t as x -> X . show x let eval = function # num as x -> x | # X . t as x -> X . eval x end
module LAdd ( E : Exp with type t = private [ > num | ' a add ] as ' a ) ( X : LExt ( E ) . S with type t = private [ > ] ~ [ add ] ) = struct type t = [ E . t add | X . t ] let show = function ` Add ( e1 , e2 ) -> " ( " ^ E . show e1 " " ^+^ E . show e2 " ) " ^ | # X . t as x -> X . show x let eval = function ` Add ( e1 , e2 ) -> let e1 = E . eval e1 and e2 = E . eval e2 in begin match e1 , e2 with ` Num n1 , ` Num n2 -> ` Num ( n1 + n2 ) | ` Num 0 , e | e , ` Num 0 -> e | e12 -> ` Add e12 end | # X . t as x -> X . eval x end
module LEnd = struct type t = [ ` Dummy ] let show ` Dummy = " " let eval ` Dummy = ` Dummy end
module rec L : Exp with type t = [ num | L . t add | ` Dummy ] = LAdd ( L ) ( LNum ( L ) ( LEnd ) )
module Num ( X : Exp ) = struct type t = num let map f x = x let eval1 ( ` Num _ as x ) : X . t = x let show ( ` Num n ) = string_of_int n end
module Add ( X : Exp with type t = private [ > num | ' a add ] as ' a ) = struct type t = X . t add let show ( ` Add ( e1 , e2 ) : t ) = " ( " ^ X . show e1 " " ^+^ X . show e2 " ) " ^ let map f ( ` Add ( e1 , e2 ) : t ) = ` Add ( f e1 , f e2 ) let eval1 ( ` Add ( e1 , e2 ) as e : t ) = match e1 , e2 with ` Num n1 , ` Num n2 -> ` Num ( n1 + n2 ) | ` Num 0 , e | e , ` Num 0 -> e | _ -> e
module Mul ( X : Exp with type t = private [ > num | ' a mul ] as ' a ) = struct type t = X . t mul let show ( ` Mul ( e1 , e2 ) : t ) = " ( " ^ X . show e1 " " ^*^ X . show e2 " ) " ^ let map f ( ` Mul ( e1 , e2 ) : t ) = ` Mul ( f e1 , f e2 ) let eval1 ( ` Mul ( e1 , e2 ) as e : t ) = match e1 , e2 with ` Num n1 , ` Num n2 -> ` Num ( n1 * n2 ) | ` Num 0 , e | e , ` Num 0 -> ` Num 0 | ` Num 1 , e | e , ` Num 1 -> e | _ -> e end
module Ext ( X : sig type t = private [ > ] end ) ( Y : sig type t end ) = struct module type S = sig type t = private [ > ] ~ [ X . t ] val map : ( Y . t -> Y . t ) -> t -> t val eval1 : t -> Y . t val show : t -> string end end
module Mix ( E : Exp ) ( E1 : Ext ( Dummy ) ( E ) . S ) ( E2 : Ext ( E1 ) ( E ) . S ) = struct type t = [ E1 . t | E2 . t ] let map f = function # E1 . t as x -> ( E1 . map f x : E1 . t :> t ) | # E2 . t as x -> ( E2 . map f x : E2 . t :> t ) let eval1 = function # E1 . t as x -> E1 . eval1 x | # E2 . t as x -> E2 . eval1 x let show = function # E1 . t as x -> E1 . show x | # E2 . t as x -> E2 . show x end
module type ET = sig type t val map : ( t -> t ) -> t -> t val eval1 : t -> t val show : t -> string end
module Fin ( E : ET ) = struct include E let rec eval e = eval1 ( map eval e ) end
module rec EAdd : ( Exp with type t = [ num | EAdd . t add ] ) = Fin ( Mix ( EAdd ) ( Num ( EAdd ) ) ( Add ( EAdd ) ) )
module rec E : Exp with type t = [ num | E . t add | E . t mul ] = Fin ( Mix ( E ) ( Mix ( E ) ( Num ( E ) ) ( Add ( E ) ) ) ( Mul ( E ) ) )
let e = E . eval ( ` Add ( ` Mul ( ` Num 2 , ` Num 3 ) , ` Num 1 ) )
module type Wrap = sig type ' a t val ( <: ) : string -> ' a Value . Type . t -> ' a t include Value . Type . S end
module type Var = sig module type Wrap = Wrap type ' a t = { symbol : Symbol . t ; type_ : ' a Value . Type . t } [ @@ deriving fields , sexp_of ] type ' a var := ' a t val create : Symbol . t -> ' a Value . Type . t -> ' a t module Wrap : Wrap with type ' a t := ' a t module And_value : sig type t = T : ' a var * ' a -> t [ @@ deriving sexp_of ] end module And_value_option : sig type t = T : ' a var * ' a option -> t [ @@ deriving sexp_of ] end val symbol_as_value : _ t -> Value . t val default_value_exn : ' a t -> ' a val default_value_is_defined : _ t -> bool val set_default_value : ' a t -> ' a -> unit val make_buffer_local_always : _ t -> unit val is_buffer_local_always : _ t -> bool val is_buffer_local_if_set : _ t -> Buffer0 . t -> bool end
module Alphabet = struct type t = { c1 : string ; c1_len : int ; cn : string ; cn_len : int } let create ~ c1 ~ cn = { c1 ; c1_len = String . length c1 ; cn ; cn_len = String . length cn } let javascript = create ~ c1 " : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ " $ ~ cn " : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ " $ let rec size t x acc = if x < t . c1_len then 1 + acc else size t ( ( x - t . c1_len ) / t . cn_len ) ( acc + 1 ) let to_string ( t : t ) ( x : int ) = let size = size t x 0 in let buf = Bytes . create size in let rec loop i x = match i with | 0 -> assert ( x < t . c1_len ) ; Bytes . set buf i t . c1 . [ x ] ; Bytes . unsafe_to_string buf | i -> let x = x - t . c1_len in Bytes . set buf i t . cn . [ x mod t . cn_len ] ; loop ( pred i ) ( x / t . cn_len ) in loop ( size - 1 ) x end
type t = { names : ( int , string ) Hashtbl . t ; known : ( int , string ) Hashtbl . t ; cache : ( int * int , string ) Hashtbl . t ; alphabet : Alphabet . t ; mutable last : int ; mutable pretty : bool ; mutable stable : bool }
let name_raw t v nm = Hashtbl . add t . names v nm
let propagate_name t v v ' = try let name = Hashtbl . find t . names v in name_raw t v ' name with Not_found -> ( )
let name t v nm_orig = let len = String . length nm_orig in if len > 0 then ( let buf = Buffer . create ( String . length nm_orig ) in let idx = ref 0 in while ! idx < len && not ( Char . is_alpha nm_orig . [ ! idx ] ) do incr idx done ; let pending = ref false in if ! idx >= len then ( pending := true ; idx := 0 ) ; for i = ! idx to len - 1 do if Char . is_alpha nm_orig . [ i ] || Char . is_num nm_orig . [ i ] then ( if ! pending then Buffer . add_char buf ' _ ' ; Buffer . add_char buf nm_orig . [ i ] ; pending := false ) else pending := true done ; let str = Buffer . contents buf in let str = match str , nm_orig with | " " , " " >>= -> " symbol_bind " | " " , " " >>| -> " symbol_map " | " " , _ -> " symbol " | str , _ -> str in let max_len = 30 in let str = if String . length str > max_len then String . sub str ~ pos : 0 ~ len : max_len else str in name_raw t v str )
let get_name t v = try Some ( Hashtbl . find t . names v ) with Not_found -> None
let format_var t i x = let s = Alphabet . to_string t . alphabet x in if t . stable then Format . sprintf " v % d " i else if t . pretty then Format . sprintf " _ % s_ " s else s
let reserved = ref StringSet . empty
let add_reserved s = reserved := List . fold_left s ~ init :! reserved ~ f ( : fun acc x -> StringSet . add x acc )
let _ = reserved := StringSet . union ! reserved Reserved . keyword
let get_reserved ( ) = ! reserved
let is_reserved s = StringSet . mem s ! reserved
let rec to_string t ? origin i = let origin = match origin with | Some i when t . pretty -> i | _ -> i in try Hashtbl . find t . cache ( i , origin ) with Not_found -> let name = try Hashtbl . find t . known i with Not_found -> t . last <- t . last + 1 ; let j = t . last in let s = format_var t i j in if is_reserved s then to_string t i else ( Hashtbl . add t . known i s ; s ) in let name = if t . pretty then try let nm = Hashtbl . find t . names origin in nm ^ name with Not_found -> name else name in Hashtbl . add t . cache ( i , origin ) name ; name
let set_pretty t b = t . pretty <- b
let set_stable t b = t . stable <- b
let reset t = Hashtbl . clear t . names ; Hashtbl . clear t . known ; Hashtbl . clear t . cache ; t . last <- - 1
let create ( ? pretty = false ) ( ? stable = false ) alphabet = let t = { names = Hashtbl . create 107 ; known = Hashtbl . create 1001 ; cache = Hashtbl . create 1001 ; alphabet ; last = - 1 ; pretty ; stable } in t
module Tensor_id : sig include Hashable . Key val create : unit -> t include Int let create = let current = ref 0 in fun ( ) -> Int . incr current ; ! current end
type t = { name : string ; trainable_tensors : ( Tensor_id . t , Tensor . t ) Hashtbl . t ; all_tensors_by_name : ( string , Tensor . t ) Hashtbl . t ; subs : ( string , t ) Hashtbl . t ; device : Device . t ; mutable frozen : bool }
let create ( ? frozen = false ) ( ? device = Device . Cpu ) ~ name ( ) = { name ; trainable_tensors = Hashtbl . create ( module Tensor_id ) ; subs = Hashtbl . create ( module String ) ; all_tensors_by_name = Hashtbl . create ( module String ) ; device ; frozen }
let first_free_name name table = if Hashtbl . mem table name then ( let rec loop idx = let name = Printf . sprintf " % s_ % d " name idx in if Hashtbl . mem table name then loop ( idx + 1 ) else name in loop 1 ) else name
let sub t sub_name = if String . contains sub_name ' . ' then Printf . failwithf " sub names cannot contain . , % s " sub_name ( ) ; Hashtbl . find_or_add t . subs sub_name ~ default ( : fun ( ) -> { name = t . name ; trainable_tensors = Hashtbl . create ( module Tensor_id ) ; subs = Hashtbl . create ( module String ) ; all_tensors_by_name = Hashtbl . create ( module String ) ; device = t . device ; frozen = t . frozen } )
let subi t i = sub t ( Int . to_string i )
let ( / ) = sub
let ( // ) = subi
let rec freeze t = t . frozen <- true ; Hashtbl . iter t . trainable_tensors ~ f ( : fun tensor -> ignore ( Tensor . set_requires_grad tensor ~ r : false : Tensor . t ) ) ; Hashtbl . iter t . subs ~ f : freeze
let rec unfreeze t = t . frozen <- false ; Hashtbl . iter t . trainable_tensors ~ f ( : fun tensor -> ignore ( Tensor . set_requires_grad tensor ~ r : true : Tensor . t ) ) ; Hashtbl . iter t . subs ~ f : unfreeze