Skip to content

Add Instrumentation #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
500 changes: 500 additions & 0 deletions examples/graph_size/data

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions examples/graph_size/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(env
(dev
(flags (:standard -w -9-27-33 -warn-error -A))))

(executables
(names main)
(libraries probzelus))
26 changes: 26 additions & 0 deletions examples/graph_size/gen_data.zls
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(*
* Copyright 2018-2020 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)

open Probzelus
open Distribution

let random_init = Random.self_init ()

let node main () =
let init x0 = Distribution.draw (gaussian (0., 2500.)) in
let rec x = x0 -> Distribution.draw (gaussian (pre x, 1.)) in
let y = Distribution.draw (gaussian (x, 1.)) in
print_string ((string_of_float x) ^ ", " ^ (string_of_float y) ^ "\n")
28 changes: 28 additions & 0 deletions examples/graph_size/kalman_copy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
EX=kalman
ALGO=copy
NAME=kalman_copy
INFERLIB=../../../inference
ZELUC=zeluc -copy -I $(INFERLIB)
ZELUC += -I ../kalmangslib

$(NAME).ml : $(NAME.zls)
$(ZELUC) -noreduce $(NAME).zls

%.zci : %.zli
$(ZELUC) $<

build: $(NAME).ml run.ml
dune build run.exe

exec: build
dune exec ./run.exe < ../data

clean:
-rm -f *.zc*
-rm -f $(NAME).ml

cleanall: clean
dune clean
-rm -f *~

all: build
7 changes: 7 additions & 0 deletions examples/graph_size/kalman_copy/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(env
(dev
(flags (:standard -w -9-27-33 -warn-error -A))))

(executables
(names run)
(libraries probzelus kalmangslib))
31 changes: 31 additions & 0 deletions examples/graph_size/kalman_copy/kalman_copy.zls
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(*
* Copyright 2018-2020 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)

open Probzelus
open Distribution
open Infer_ds_streaming_copy
open Kalmangslib

let proba kalman1d yobs = xt where
rec xt = sample (gaussian ((const 0., 2500.) -> (pre xt, 1.)))
and () = observe (gaussian (xt, 1.), yobs)

let node main_no_metric particles observed =
infer particles kalman1d observed

let node main particles (true_x, observed) = (d, mse) where
rec d = main_no_metric particles observed
and mse = Metrics.mse (true_x, d)
42 changes: 42 additions & 0 deletions examples/graph_size/kalman_copy/run.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(*
* Copyright 2018-2020 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)

let name = "Kalman-1D"
let algo = "SDS"
type input = float * float
type output = float Probzelus.Distribution.t * float
let read_input () = Scanf.scanf ("%f, %f\n") (fun t o -> (t, o))
let main = Kalman_copy.main
let string_of_output (out, _) = string_of_float (Probzelus.Distribution.mean_float out)

let num_particles = 5


let rec run_helper step state =
try
let s = read_input () in
let out = step state s in
print_string ((string_of_output out) ^ "\n");
run_helper step state
with End_of_file -> []

let run _ =
let Ztypes.Cnode {alloc; reset; step; copy = _} = main num_particles in
let init_state = alloc () in
reset init_state;
run_helper step init_state;;

run ()
30 changes: 30 additions & 0 deletions examples/graph_size/kalman_copy_instrumented/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
EX=kalman
ALGO=copy
NAME=kalman_copy_instrumented
INFERLIB=../../../inference
ZELUC=zeluc -copy -I $(INFERLIB)
ZELUC += -I ../kalmangslib
PROBZELUC = probzeluc
PROBZELUC += -I ../kalmangslib

$(NAME).ml : $(NAME.zls)
$(PROBZELUC) -noreduce -nopt -nosimplify -inline 0 $(NAME).zls

%.zci : %.zli
$(PROBZELUC) $<

build: $(NAME).ml run.ml
dune build run.exe

exec: build
dune exec ./run.exe < ../data

clean:
-rm -f *.zc*
-rm -f $(NAME).ml

cleanall: clean
dune clean
-rm -f *~

all: build
8 changes: 8 additions & 0 deletions examples/graph_size/kalman_copy_instrumented/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(env
(dev
(flags (:standard -w -9-27-33 -warn-error -A))))

(executables
(names run)
(libraries probzelus kalmangslib)
(modes native))
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(*
* Copyright 2018-2020 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)

open Probzelus
open Distribution
open Infer_ds_streaming_copy_instrumented
open Kalmangslib

let atomic proba do_kalman1d yobs = xt where
rec xt = sample (gaussian ((const 0., 2500.) -> (pre xt, 1.)))
and () = observe (gaussian (xt, 1.), yobs)

let proba kalman1d yobs = xt where
rec xt = do_kalman1d yobs
and null1 = Utils.to_unit xt
and null2 = Utils.gc_full_major null1
and () = print_ins null2

let node main_no_metric particles observed =
infer particles kalman1d observed

let node main particles (true_x, observed) = (d, mse) where
rec d = main_no_metric particles observed
and mse = Metrics.mse (true_x, d)
41 changes: 41 additions & 0 deletions examples/graph_size/kalman_copy_instrumented/run.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(*
* Copyright 2018-2020 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)

let name = "Kalman-1D"
let algo = "SDS"
type input = float * float
type output = float Probzelus.Distribution.t * float
let read_input () = Scanf.scanf ("%f, %f\n") (fun t o -> (t, o))
let main = Kalman_copy_instrumented.main
let string_of_output (out, _) = string_of_float (Probzelus.Distribution.mean_float out)

let num_particles = 2

let rec run_helper step state =
try
let s = read_input () in
let out = step state s in
print_string ("Output:" ^ (string_of_output out) ^ "\n");
run_helper step state
with End_of_file -> []

let run _ =
let Ztypes.Cnode {alloc; reset; step; copy = _} = main num_particles in
let init_state = alloc () in
reset init_state;
run_helper step init_state;;

run ()
30 changes: 30 additions & 0 deletions examples/graph_size/kalmangslib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
INFERLIB=../../../inference
OWLLIB=../../../owl
ZELUC=zeluc -copy -I $(INFERLIB) -I $(OWLLIB)
PROBZELUC=probzeluc


ZLI=$(wildcard *.zli)
ZCI=$(ZLI:zli=zci)

all: $(ZCI) byte opt

.phony: byte opt

byte: metrics.ml
dune build kalmangslib.cma

opt: metrics.ml
dune build kalmangslib.cmxa

%.zci: %.zli
$(PROBZELUC) $<

metrics.ml : metrics.zls
$(PROBZELUC) -noreduce metrics.zls

clean:
dune clean
-rm -f *.zci metrics.ml
cleanall: clean
rm -f *~
3 changes: 3 additions & 0 deletions examples/graph_size/kalmangslib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name kalmangslib)
(libraries probzelus))
1 change: 1 addition & 0 deletions examples/graph_size/kalmangslib/kalmangslib.zli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(* Empty file *)
25 changes: 25 additions & 0 deletions examples/graph_size/kalmangslib/metrics.zls
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(*
* Copyright 2018-2020 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)

open Probzelus
open Distribution

let node mse (true_x, d) = mse where
rec t = 1. fby (t +. 1.)
and estimated_x = mean_float d
and error = (estimated_x -. true_x) ** 2.
and total_error = error -> (pre total_error) +. error
and mse = total_error /. t
3 changes: 3 additions & 0 deletions examples/graph_size/kalmangslib/utils.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let to_unit _ = ()
let gc_full_major _ =
Gc.full_major ();
2 changes: 2 additions & 0 deletions examples/graph_size/kalmangslib/utils.zli
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
val to_unit : 'a -> unit
val gc_full_major : unit -> unit
Loading