Skip to content

Instantly share code, notes, and snippets.

@oxinabox
Last active January 25, 2019 19:46
Show Gist options
  • Save oxinabox/dec73cc0bf4d4bc7bc93c2bf24d21734 to your computer and use it in GitHub Desktop.
Save oxinabox/dec73cc0bf4d4bc7bc93c2bf24d21734 to your computer and use it in GitHub Desktop.
Using Cassette to pull out information about creation of Vectors.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"using Pkg\n",
"pkg\"add Cassette\"\n",
"pkg\"add DataVoyager\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"using Dates\n",
"using Random\n",
"using LinearAlgebra\n",
"using Cassette"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Cassette.Context{nametype(VecTrack6),M,P,T,B} where B<:Union{Nothing, IdDict{Module,Dict{Symbol,BindingMeta}}} where P<:Cassette.AbstractPass where T<:Union{Nothing, Tag} where M"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Cassette.@context VecTrack6"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"function track!(tracker::AbstractDict{UInt64, Any}, item, action, f)\n",
" id = objectid(item)\n",
" \n",
" notes = get!(tracker, id) do\n",
" []\n",
" end\n",
" \n",
" push!(notes, \n",
" ( # Can't put too much in here, as need to make sure not to leak memory\n",
" timestamp=now(),\n",
" action = action,\n",
" func = f,\n",
" length = length(item),\n",
" kind = typeof(item)\n",
" )\n",
" )\n",
"end\n",
"\n",
"function Cassette.posthook(ctx::VecTrack6, output::AbstractArray, f, args...)\n",
" tracker = ctx.metadata\n",
"\n",
" if output isa AbstractArray\n",
" track!(tracker, output, :output, f)\n",
" end\n",
"end\n",
"#= add this to track usage too, but very slow\n",
"function Cassette.execute(ctx::VecTrack6, f, args...)\n",
" output = Cassette.@overdub(ctx, f(args...))\n",
"\n",
" tracker = ctx.metadata\n",
" vec_inputs = Iterators.filter(x -> x isa AbstractArray, args)\n",
"\n",
" for input in vec_inputs\n",
" track!(tracker, input, :input, f)\n",
" end\n",
"\n",
" if output isa AbstractArray\n",
" track!(tracker, output, :output, f)\n",
" end\n",
" \n",
" return output\n",
"end\n",
"=#"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Results of Optimization Algorithm\n",
" * Algorithm: BFGS\n",
" * Starting Point: [0.0,0.0]\n",
" * Minimizer: [0.9999999926033423,0.9999999852005353]\n",
" * Minimum: 5.471433e-17\n",
" * Iterations: 16\n",
" * Convergence: true\n",
" * |x - x'| ≤ 0.0e+00: false \n",
" |x - x'| = 3.47e-07 \n",
" * |f(x) - f(x')| ≤ 0.0e+00 |f(x)|: false\n",
" |f(x) - f(x')| = 1.20e+03 |f(x)|\n",
" * |g(x)| ≤ 1.0e-08: true \n",
" |g(x)| = 2.33e-09 \n",
" * Stopped by an increasing objective: false\n",
" * Reached Maximum Number of Iterations: false\n",
" * Objective Calls: 53\n",
" * Gradient Calls: 53"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Optim\n",
"rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2\n",
"\n",
"\n",
"ctx = VecTrack6(metadata=Dict{UInt64, Any}())\n",
"Cassette.@overdub ctx optimize(rosenbrock, zeros(2), BFGS())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"using DataVoyager\n",
"using DataFrames\n",
"using Query\n",
"using QueryOperators"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Voyager(Electron.Window(Application(Base.PipeEndpoint(RawFD(0x00000035) paused, 0 bytes waiting), Process(`\u001b[4m/Users/oxinabox/.julia/packages/Electron/IV1np/src/../deps/electron/Julia.app/Contents/MacOS/Julia\u001b[24m \u001b[4m/Users/oxinabox/.julia/packages/Electron/IV1np/src/main.js\u001b[24m \u001b[4m/var/folders/1r/4cjj1xk90qbb6njj7dn_cmt40000gn/T/juliaelectron-65678-1\u001b[24m \u001b[4m/var/folders/1r/4cjj1xk90qbb6njj7dn_cmt40000gn/T/juliaelectron-sysnotify-65678-1\u001b[24m \u001b[4mxHk4nMv/Mmz1Nar4h/Iub9t+6DGlp0DtfrIufx+cSCdxoEpjqjoFn4wRvu4ZCos+2IyCF+qoqisyr+0zGBAspGpYWdELE6JpCFvYkOs9CzIIvrAyk0zV9qdd4kzhlV3iyWo0ZILgLQvhmXfsEqr/90vsAat8yEqxiBp1rWtnW1g=\u001b[24m`, ProcessRunning), [1 window]), 1, true, Channel{Any}(sz_max:128,sz_curr:0)))"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"safe_data = @from log in ctx.metadata begin\n",
" @from ii in log.second\n",
" @select {\n",
" id = log.first,\n",
" ii.timestamp,\n",
" ii.action,\n",
" func=string(ii.func),\n",
" kind=string(ii.kind),\n",
" ii.length}\n",
" @collect \n",
"end\n",
"\n",
"dt = NamedTuple{\n",
" (:id, :timestamp, :action, :func, :kind, :length),\n",
" Tuple{UInt64, DateTime,Symbol,String, String,Int64}\n",
"}\n",
"convert(Vector{dt}, safe_data) |> Voyager\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
""
]
},
{
"cell_type": "code",
"execution_count": 212,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"NamedTuple{(:timestamp, :action, :func, :length),Tuple{DateTime,Symbol,Any,Int64}}"
]
},
"execution_count": 212,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.0.2",
"language": "julia",
"name": "julia-1.0"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.0.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment