text stringlengths 0 601k |
|---|
let set_z ( v : t ) ( z : float ) : unit = Ctypes . setf !@ v Orx_types . Vector . z z |
let make_one_vec_op op = let f ' ( ~ target : t ) ( v : t ) : unit = let ( _ : t ) = op target v in ( ) in let f ( v : t ) : t = let target : t = allocate_raw ( ) in f ' ~ target v ; target in ( f ' , f ) |
let ( copy ' , copy ) = make_one_vec_op copy |
let ( normalize ' , normalize ) = make_one_vec_op normalize |
let ( reciprocal ' , reciprocal ) = make_one_vec_op reciprocal |
let ( round ' , round ) = make_one_vec_op round |
let ( floor ' , floor ) = make_one_vec_op floor |
let ( neg ' , neg ) = make_one_vec_op neg |
let make_two_vec_op op = let f ' ( ~ target : t ) ( v1 : t ) ( v2 : t ) : unit = let ( _ : t ) = op target v1 v2 in ( ) in let f ( v1 : t ) ( v2 : t ) : t = let target : t = allocate_raw ( ) in f ' ~ target v1 v2 ; target in ( f ' , f ) |
let ( add ' , add ) = make_two_vec_op add |
let ( sub ' , sub ) = make_two_vec_op sub |
let ( mul ' , mul ) = make_two_vec_op mul |
let ( div ' , div ) = make_two_vec_op div |
let ( cross ' , cross ) = make_two_vec_op cross |
let make_one_vec_one_float_op op = let f ' ( ~ target : t ) ( v : t ) ( x : float ) : unit = let ( _ : t ) = op target v x in ( ) in let f ( v : t ) ( x : float ) : t = let target : t = allocate_raw ( ) in f ' ~ target v x ; target in ( f ' , f ) |
let ( mulf ' , mulf ) = make_one_vec_one_float_op mulf |
let ( divf ' , divf ) = make_one_vec_one_float_op divf |
let ( rotate_2d ' , rotate_2d ) = make_one_vec_one_float_op rotate_2d |
let make_two_vec_one_float_op op = let f ' ( ~ target : t ) ( v1 : t ) ( v2 : t ) ( x : float ) : unit = let ( _ : t ) = op target v1 v2 x in ( ) in let f ( v1 : t ) ( v2 : t ) ( x : float ) : t = let target : t = allocate_raw ( ) in f ' ~ target v1 v2 x ; target in ( f ' , f ) |
let ( lerp ' , lerp ) = make_two_vec_one_float_op lerp |
let make_one_vec_two_vec_op op = let f ' ( ~ target : t ) ( v : t ) ( ~ min : t ) ( ~ max : t ) : unit = let ( _ : t ) = op target v min max in ( ) in let f ( v : t ) ( ~ min : t ) ( ~ max : t ) : t = let target : t = allocate_raw ( ) in f ' ~ target v ~ min ~ max ; target in ( f ' , f ) |
let ( clamp ' , clamp ) = make_one_vec_two_vec_op clamp |
let clamp_size ' ~ target v ~ min ~ max = let size = get_size v in copy ' ~ target v ; if size < min then ( normalize ' ~ target target ; mulf ' ~ target target min ) else if size > max then ( normalize ' ~ target target ; mulf ' ~ target target max ) |
let clamp_size v ~ min ~ max = let target : t = allocate_raw ( ) in clamp_size ' ~ target v ~ min ~ max ; target |
let move_x ( v : t ) ( delta : float ) : unit = set_x v ( get_x v . + delta ) |
let move_y ( v : t ) ( delta : float ) : unit = set_y v ( get_y v . + delta ) |
let move_z ( v : t ) ( delta : float ) : unit = set_z v ( get_z v . + delta ) |
let of_rotation ( rotation : float ) : t = let x = cos rotation in let y = sin rotation in make ~ x ~ y ~ z : 0 . 0 |
let to_rotation ( v : t ) : float = Float . atan2 ( get_y v ) ( get_x v ) |
let get_optional_vector get o = let v = allocate_raw ( ) in match get o v with | None -> None | Some _v -> Some v |
let get_vector_exn get o = match get_optional_vector get o with | None -> fail " Failed to set vector " | Some v -> v |
let get_vector get o = let v = allocate_raw ( ) in let ( _ : t ) = get o v in v |
let msgs = [ " c9bd5b35d48f71b48656c39e395e4d138a45df54b7c03fad7330f5fa3f42a44d " ; " c2a49dc5141afdf02480dc2e9d7ec3f602ccc6cf322a0a9b481c80d37170713c " ; " 72e8b038fd5cbe8a3f2bb8e9ffc8f48f39143279210e8bfa0131445da9d76b93 " ; " 6aeee423a44030de1632b7d4e42afc04473f9de218a11358016cd04a3dcc8593 " ; " ba52a5eb037ceecd38bad... |
let keys = [ ( " D63FF7D5D8FB7334287CB397F824B3567178BB635CD2FAA8A34D0B1BC65FDAF2 " , " 02CA5364C4302C38E93F8A4850E61A8FE6C27E386D4541B898F4E74BE5E6DD0256 " ) ; ( " 9D585160C3AD171D7F4925F359C2B4C8992730DBAEE4D4D10B1E0E489CCA3404 " , " 03D6008C2A656DD414C6869558A1E262F38BD5A142039FE84E750335C543B376B9 " ) ; ( " 62FAA06... |
let sigs = [ [ ( " 2993E89EF2190B133449447C014A358E44618224E52C27AC83B3468D615FC390 " , " 7A398C8E09359F54E1E5301F0A70DA61BCA0EFC15F037A0A745B78D25F6ABD8F " ) ; ( " 6F89D730F8F95BA32776E7FA1363177615B5955405017AEF0DD7C2474850764E " , " 681DF8E9541892B165E350689246918B9713D9E4FF038C3689BC46375CFCB7B7 " ) ; ( " 2FBE99E3C... |
module Arr = struct type ' a t = ' a array let set = Array . set let get = Array . get let len = Array . length let make = Array . make let copy = Array . copy let iter = Array . iter let foldl = Array . fold_left let foldr ' f a x = Array . fold_right f x a let blit = Array . blit let foldr : type a x . ( x -> ( unit ... |
module AP = struct type ' a t = ' a array * ' a array let len ( l , r ) = Arr . len l + Arr . len r let get ( l , r ) i = if i < Arr . len l then Arr . get l i else Arr . get r ( i - Arr . len l ) let set ( l , r ) i x = if i < Arr . len l then Arr . set l i x else Arr . set r ( i - Arr . len l ) x let fold f a ( l , r... |
let ceil_div a b = 1 + ( a - 1 ) / b |
let _BRANCHING = 1 lsl _BITS |
let _SKIP_SIZE = _BRANCHING - ceil_div _EXTRA_STEPS 2 |
let check_depth = let max_depth = if Sys . int_size = 31 then 6 else 12 in fun d -> if d > max_depth then failwith " clarity - vector - too - large " |
type ' a t = | Leaf of ' a array | R_node of int array * ' a t array | B_node of ' a t array |
let depth x = let rec loop : type a . int -> a t -> int = fun a -> function | Leaf _ -> a | R_node ( _ , n ) | B_node n -> assert ( Arr . len n > 0 ) ; loop ( a + 1 ) ( Arr . get n 0 ) in loop 0 x |
let rec length = function | Leaf x -> Arr . len x | R_node ( i , v ) -> assert ( Arr . len i = Arr . len v ) ; Arr . get i ( Arr . len i - 1 ) | B_node v as node -> assert ( Arr . len v > 0 ) ; let d = depth node in check_depth d ; let item_sz = 1 lsl ( d * _BITS ) in item_sz * ( Arr . len v - 1 ) + length ( Arr . get ... |
let update_lengths = function | Leaf _ | B_node _ -> ( ) | R_node ( is , vs ) -> assert ( Arr . len is = Arr . len vs ) ; let sum = ref 0 in for i = 0 to Arr . len vs - 1 do sum := ! sum + length ( Arr . get vs i ) ; Arr . set is i ! sum done |
let mk_rnode arr = let res = R_node ( Arr . make ( Arr . len arr ) 0 , arr ) in update_lengths res ; res |
let rr_search : int array -> int -> int -> int * int = fun sizes depth idx -> assert ( Arr . len sizes > 0 ) ; assert ( idx <= Arr . get sizes ( Arr . len sizes - 1 ) ) ; check_depth depth ; let start = idx lsr ( _BITS * depth ) in assert ( start < Arr . len sizes ) ; let rec loop n = assert ( n < Arr . len sizes ) ; l... |
let radix_search : int -> int -> int * int = fun depth idx -> check_depth depth ; let shift = _BITS * depth in let slot = idx lsr shift in slot , idx - slot lsl shift |
let max_nodes_allowed subnodes = _EXTRA_STEPS + ( subnodes - 1 ) / _BRANCHING + 1 |
let empty = Leaf [ ] || |
let get_leaf = function | Leaf x -> x | _ -> assert false |
let get_rnode = function | R_node ( i , v ) -> i , v | B_node v -> let sizes = Arr . make ( Arr . len v ) 0 in update_lengths ( R_node ( sizes , v ) ) ; sizes , v | _ -> assert false |
let get_bnode = function | B_node x -> x | _ -> assert false |
let node_len : type a . a t -> int = function | Leaf x -> Arr . len x | R_node ( _ , x ) | B_node x -> Arr . len x |
module Concatenation = struct let assign_subnode_lengths : type a . a t AP . t -> int AP . t -> unit = fun src dst -> let dst_idx = ref 0 in let src_idx = ref 0 in let size = ref 0 in let not_done = let need = AP . len src - AP . len dst in fun ( ) -> let need = if ! size = 0 then need else need + 1 in let diff = ! src... |
let cons x v = append ( Leaf [ | x ] ) | v |
let snoc v x = append v ( Leaf [ | x ] ) | |
let check_bounds n index = let size = length n in if size - 1 < index || index < 0 then raise ( Out_of_bounds { index ; size } ) |
let get : type a . a t -> int -> a = fun n i -> check_bounds n i ; let rec loop n i = function | 0 -> Arr . get ( get_leaf n ) i | d -> begin match n with | Leaf _ -> assert false | R_node ( is , vs ) -> let slot , new_i = rr_search is d i in loop ( Arr . get vs slot ) new_i ( d - 1 ) | B_node vs -> let slot , new_i = ... |
let update : type a . a t -> int -> a -> a t = fun n i x -> check_bounds n i ; let rec loop n i = function | 0 -> let res = Arr . copy ( get_leaf n ) in Arr . set res i x ; Leaf res | d -> begin match n with | Leaf _ -> assert false | R_node ( is , vs ) -> let slot , new_i = rr_search is d i in let upd = loop ( Arr . g... |
let split_at : type a . a t -> int -> a t * a t = fun n i -> let size = length n in if i < 0 then raise ( Out_of_bounds { index = i ; size } ) ; let rec loop n i = function | 0 -> begin match i with | 0 -> None , Some n | i when node_len n = i -> Some n , None | i -> let a = get_leaf n in let al = Arr . make i ( Arr . ... |
let take : type a . a t -> int -> a t = fun n i -> let sz = length n in fst ( split_at n ( if i > sz then sz else i ) ) |
let drop : type a . a t -> int -> a t = fun n i -> let sz = length n in snd ( split_at n ( if i > sz then sz else i ) ) |
let rec iter f = function | Leaf x -> Arr . iter f x | R_node ( _ , x ) | B_node x -> Arr . iter ( iter f ) x |
module Builder = struct type ' a vector = ' a t type ' a chunk = { mutable cnt : int ; vec : ' a vector } type ' a t = ' a chunk list ref let copy : ' a t -> ' a t = fun x -> match ! x with | [ ] -> ref [ ] | { cnt ; vec } :: t -> ref ( { cnt = cnt ; vec = vec } :: t ) let rec push_node n ( x : ' a t ) : unit = match !... |
let init : type a . int -> ( int -> a ) -> a t = fun l f -> let b = Builder . empty ( ) in for i = 1 to l do Builder . put b ( f i ) done ; Builder . result b type nonrec ' a t = ' a t let pure x = Leaf [ | x ] | let bind f x = let b = Builder . empty ( ) in iter ( fun y -> iter ( Builder . put b ) ( f y ) ) x ; Builde... |
let rec foldr ' f a = function | Leaf x -> Arr . foldr ' f a x | R_node ( _ , x ) | B_node x -> Arr . foldr ' ( fun x a -> foldr ' f a x ) a x |
let to_list x = foldr ' Clarity_list . _Cons [ ] x |
let of_list x = let b = Builder . empty ( ) in Clarity_list . iter ( Builder . put b ) x ; Builder . result b type nonrec ' a t = ' a t let align_as both left right a b = let la = length a in let lb = length b in let build = Builder . empty ( ) in for i = 0 to min la lb - 1 do Builder . put build ( both ( get a i ) ( g... |
module A3 ( A : Applicative . Basic3 ) = Traversable . Make3 ( struct type nonrec ' a t = ' a t type ( ' u , ' v , ' a ) f = ( ' u , ' v , ' a ) A . t module Ap = Applicative . Make3 ( A ) let traverse f x = let cf x l = let open ! Ap in ap ( map ( fun h t -> h :: t ) ( f x ) ) l in let ls = foldr cf ( defer Ap . pure ... |
module A2 ( A : Applicative . Basic2 ) = A3 ( struct type ( _ , ' p , ' a ) t = ( ' p , ' a ) A . t include ( A : Applicative . Basic2 with type ( ' p , ' a ) t := ( ' p , ' a ) A . t ) end ) |
module A ( A : Applicative . Basic ) = A2 ( struct type ( _ , ' a ) t = ' a A . t include ( A : Applicative . Basic with type ' a t := ' a A . t ) end ) |
module M3 ( M : Monad . Basic3 ) = struct include A3 ( M ) let foldr_m f a l = let g k x z = M . bind k ( f x z ) in foldl g M . pure l a let foldl_m f a l = let g x k z = M . bind ( fun x -> k ( ) x ) ( f z x ) in foldr g ( const M . pure ) l a end |
module M2 ( M : Monad . Basic2 ) = M3 ( struct type ( _ , ' p , ' a ) t = ( ' p , ' a ) M . t include ( M : Monad . Basic2 with type ( ' p , ' a ) t := ( ' p , ' a ) M . t ) end ) |
module M ( M : Monad . Basic ) = M2 ( struct type ( _ , ' a ) t = ' a M . t include ( M : Monad . Basic with type ' a t := ' a M . t ) end ) |
module type Vector_base = sig type t type elt type index val fold_index : ( index -> elt -> ' a ) -> ( ' a -> index -> elt -> ' a ) -> t -> ' a val fold_index_2 : ( index -> elt -> elt -> ' a ) -> ( ' a -> index -> elt -> elt -> ' a ) -> t -> t -> ' a val map : ( index -> elt -> elt ) -> t -> t end |
module Vect2 = struct type t = float * float type elt = float type index = Fst | Snd let fold_index init f ( fst , snd ) = f ( init Fst fst ) Snd snd let fold_index_2 init f ( fst1 , snd1 ) ( fst2 , snd2 ) = f ( init Fst fst1 fst2 ) Snd snd1 snd2 let map f ( fst , snd ) = ( f Fst fst , f Snd snd ) end |
module Vect3 = struct type t = float * float * float type elt = float type index = Fst | Snd | Trd let fold_index init f ( fst , snd , trd ) = f ( f ( init Fst fst ) Snd snd ) Trd trd let fold_index_2 init f ( fst1 , snd1 , trd1 ) ( fst2 , snd2 , trd2 ) = f ( f ( init Fst fst1 fst2 ) Snd snd1 snd2 ) Trd trd1 trd2 let m... |
module Vect2_record = struct type t = { x : float ; y : float } type elt = float type index = X | Y let fold_index init f { x ; y } = f ( init X x ) Y y let fold_index_2 init f v1 v2 = f ( init X v1 . x v2 . x ) Y v1 . y v2 . y let map f { x ; y } = { x = f X x ; y = f Y y } end |
module Vect3_record = struct type t = { x : float ; y : float ; z : float } type elt = float type index = X | Y | Z let fold_index init f { x ; y ; z } = f ( f ( init X x ) Y y ) Z z let fold_index_2 init f v1 v2 = f ( f ( init X v1 . x v2 . x ) Y v1 . y v2 . y ) Z v1 . z v2 . z let map f { x ; y ; z } = { x = f X x ; ... |
module Vect_array = struct type t = float array type elt = float type index = int let fold_index init f a = if Array . length a = 0 then invalid_arg " fold_index " ; let r = ref ( init 0 ( Array . unsafe_get a 0 ) ) in for i = 1 to Array . length a - 1 do r := f ! r i ( Array . unsafe_get a i ) done ; ! r let fold_inde... |
module Vector_operations ( V : Vector_base with type elt = float ) = struct type elt = V . elt type t = V . t let norm v = let sum_sq = V . fold_index ( fun _ elt -> elt . * elt ) ( fun acc _ elt -> acc . + elt . * elt ) v in sqrt sum_sq let scale s v = V . map ( fun _ x -> x . * s ) v let dot v1 v2 = V . fold_index_2 ... |
type step = { obs : Tensor . t ; reward : Tensor . t ; is_done : Tensor . t } |
type t = { envs : Pytypes . pyobject ; np : Pytypes . pyobject } |
let create str ~ num_processes = if not ( Py . is_initialized ( ) ) then ( Py . add_python_path " examples / reinforcement - learning " ; Py . initialize ( ) ) ; let wrappers = Py . import " atari_wrappers " in let envs = Py . Module . get_function wrappers " make " [ | Py . String . of_string str ; Py . Int . of_int n... |
let to_tensor t np_array = let np_array = Py . Module . get_function t . np " ascontiguousarray " [ | np_array ] | in Py . Object . call_method np_array " astype " [ | Py . Module . get t . np " float32 " ] | |> Numpy . to_bigarray Float32 C_layout |> Tensor . of_bigarray |> Tensor . to_type ~ type_ ( : T Float ) |
let reset t = let reset_fn = Py . Object . get_attr_string t . envs " reset " in Py . Callable . to_function ( Option . value_exn reset_fn ) [ ] || |> to_tensor t |
let step t ~ actions = let v = Py . Object . call_method t . envs " step " [ | Py . List . of_list_map Py . Int . of_int actions ] | in let obs , reward , is_done , _ = Py . Tuple . to_tuple4 v in { obs = to_tensor t obs ; reward = to_tensor t reward ; is_done = to_tensor t is_done } |
let action_space t = let action_space = Option . value_exn ( Py . Object . get_attr_string t . envs " action_space " ) in Option . value_exn ( Py . Object . get_attr_string action_space " n " ) |> Py . Int . to_int |
module type ResizeType = sig type t val null : t end |
module type S = sig type elt type t val length : t -> int val compact : t -> unit val singleton : elt -> t val empty : unit -> t val make : int -> t val init : int -> ( int -> elt ) -> t val is_empty : t -> bool val of_sub_array : elt array -> int -> int -> t val unsafe_internal_array : t -> elt array val reserve : t -... |
let err_argv = " argv array must have at least one element " |
let err_not_opt = " Option argument without name " |
let err_not_pos = " Positional argument with a name " |
let err_help s = " Term error , help requested for unknown command " ^ s |
let err_empty_list = " Empty list " |
let err_incomplete_enum = " Incomplete enumeration for the type " |
let err_doc_string s = str " Variable substitution failed on documentation fragment ` % s ' " s |
let rev_compare n n ' = compare n ' n |
let str_of_pp pp v = pp Format . str_formatter v ; Format . flush_str_formatter ( ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.