This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.qmd
528 lines (382 loc) · 12.6 KB
/
index.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
---
title: "Why Julia?"
subtitle: "A gentle pitch"
author: "Jose Storopoli, PhD"
format:
revealjs:
slide-number: true
transition: slide
chalkboard:
buttons: false
preview-links: auto
footer: <https://storopoli.github.io/Why-Julia>
logo: images/julia-dots.svg
callout-appearance: minimal
execute:
echo: true
cache: true
---
```{julia}
#| echo: false
#| output: false
ENV["PYCALL_JL_RUNTIME_PYTHON"] = Sys.which("python")
using PyCall
```
## Agenda
</br> </br>
::: incremental
1. speed
2. ease-of-use
3. composability
:::
## What I Assume?
</br> </br>
::: incremental
- Python background
- scientific computing background
:::
## So let's dive in?
{fig-align="center"}
## Julia is past beyond "experimental" {.smaller .scrollable}
:::incremental
- NASA uses Julia in a supercomputer to analyze the
["Largest Batch of Earth-Sized Planets Ever Found"](https://exoplanets.nasa.gov/news/1669/seven-rocky-trappist-1-planets-may-be-made-of-similar-stuff/) and achieve a whopping **1,000x speedup** to catalog 188 million astronomical objects in 15 minutes.
- [The Climate Modeling Alliance (CliMa)](https://clima.caltech.edu/)
is using mostly Julia to **model climate in the GPU and CPU**.
Launched in 2018 in collaboration with researchers at Caltech, the NASA Jet Propulsion Laboratory, and the Naval Postgraduate School, CliMA is utilizing recent progress in computational science to develop an Earth system model that can predict droughts, heat waves, and rainfall with unprecedented precision and speed.
- [US Federal Aviation Administration (FAA) is developing an **Airborne Collision Avoidance System (ACAS-X)** using Julia](https://youtu.be/19zm1Fn0S9M).
This is a nice example of the "Two-Language Problem".
Previous solutions used Matlab to develop the algorithms and C++ for a fast implementation.
Now, FAA is using one language to do all this: Julia.
- [**175x speedup** for Pfizer's pharmacology models using GPUs in Julia](https://juliahub.com/case-studies/pfizer/).
It was presented as a [poster](https://chrisrackauckas.com/assets/Posters/ACoP11_Poster_Abstracts_2020.pdf) in the 11th American Conference of Pharmacometrics (ACoP11) and [won a quality award](https://web.archive.org/web/20210121164011/https://www.go-acop.org/abstract-awards).
- [The Attitude and Orbit Control Subsystem (AOCS) of the Brazilian satellite Amazonia-1 is **written 100% in Julia**](https://discourse.julialang.org/t/julia-and-the-satellite-amazonia-1/57541) by Ronan Arraes Jardim Chagas (<https://ronanarraes.com/>).
- [Brazil's national development bank (BNDES) ditched a paid solution and opted for open-source Julia modeling and gained a **10x speedup**.](https://youtu.be/NY0HcGqHj3g)
:::
::: footer
If this is not enough, there are more case studies in [JuliaHub website](https://juliahub.com/case-studies/).
:::
## Speed
</br>
**Julia is fast!**
</br>
::: {.fragment .fade-in}
Two examples:
:::
::: incremental
- Data Wrangling: `pandas` versus `DataFrames.jl`
- ODE solving: `scipy` versus `DifferentialEquations.jl`
:::
## Benchmarking --- Data Wrangling
Common data wrangling scenario doing "split-apply-combine" operations.
::: incremental
- 10,000 observations
- 1 categorical variable `x` $\in \{\mathrm{A}, \mathrm{B}, \mathrm{C}, \mathrm{D}\}$
- 2 continuous variables:
- `y` $\in [0, 1]$
- `z` $\text{Normal}(0, 1)$
:::
## Benchmarking --- Data Wrangling (Python)
```{julia}
using BenchmarkTools
py"""
import pandas as pd
import numpy as np
n = 10000
df = pd.DataFrame({'x': np.random.choice(['A', 'B', 'C', 'D'], n, replace=True),
'y': np.random.randn(n),
'z': np.random.rand(n)})
"""
@btime py"df.groupby('x').agg({'y': 'median', 'z': 'mean'})";
```
## Benchmarking --- Data Wrangling (Julia)
```{julia}
using Random
using DataFrames
using BenchmarkTools
using Chain
Random.seed!(123)
n = 10_000
df = DataFrame(
x=rand('A':'D', n),
y=rand(n),
z=randn(n),
)
@btime @chain $df begin
groupby(:x)
combine(:y => median, :z => mean)
end;
```
## Benchmarking --- ODE Solver
Second order non-linear ODE example with a **simple pendulum**
:::: {.columns}
::: {.column width="50%"}
$$
\begin{align*}
&\dot{\theta} = d{\theta} \\
&\dot{d\theta} = - \frac{g}{L}{\sin(\theta)}
\end{align*}
$$
:::
::: {.column width="50%"}
{width=60%}
:::
::::
## Benchmarking --- ODE Solver (Julia)
```{julia}
using DifferentialEquations
# Constants
const g = 9.81
L = 1.0
# Initial Conditions
u₀ = [0, π/2]
tspan = (0.0, 6.3)
# Define the problem
function simplependulum(du, u, p, t)
θ, dθ = u
du[1] = dθ
du[2] = -(g/L)*sin(θ)
end
# Pass to solvers
prob = ODEProblem(simplependulum, u₀, tspan)
# RK 4/5th order solver (Tsitouras)
@btime solve(prob, Tsit5(); saveat=range(tspan...; length=1_000));
```
## Benchmarking --- ODE Solver (Python)
```{julia}
py"""
import numpy as np
from scipy.integrate import odeint
# Constants
g = 9.81
L = 1.0
# Initial Conditions
u0 = [0, np.pi/2]
tspan = np.linspace(0.0, 6.3, 1000)
def simplependulum(u, t, g, L):
theta, dtheta = u
dydt = [dtheta, -(g/L)*np.sin(theta)]
return dydt
"""
# RK 4/5th order solver (Dormand-Prince)
@btime py"odeint(simplependulum, u0, tspan, args=(g, L))";
```
## Why Julia is so Fast?

::: incremental
- *just-in-time* compilation for the LLVM compiler
- exposes everything in *intermediate representation* code
- then LLVM does what does best: **OPTIMIZE**
- including `for`-loops
:::
## Why Julia is so Fast? --- LLVM code {.scrollable}
```{julia}
#| output-location: slide
using Statistics: mean
@code_llvm mean(1:10)
```
::: footer
output in next slide
:::
## Ease of Use
The syntax is quite similar to Python.
. . .
But, no indentation and every keyword needs an `end`.
. . .
:::: {.columns}
::: {.column width="50%"}
Julia:
```julia
for i in 1:10
println(i)
end
```
:::
::: {.column width="50%"}
Python:
```python
for i in range(10):
print(i)
```
:::
::::
## It's Julia all the way down
If you need to find something just use the
`@which` macro on a type or a function signature.
</br>
. . .
```{julia}
@which DataFrame # type
```
. . .
</br>
```{julia}
@which mean(1:10) # function signature.
```
## Composability
::: incremental
- It is very easy to create new packages that have types and functions.
- You can extend other package's functions,
including Julia's `Base` standard library to you new types.
- And you can also create new functions for other Package's types.
:::
## Composability --- Example with `Point`
```{julia}
#| output: false
struct Point
x::Float64
y::Float64
end
function Base.:+(x::Point, y::Point)
return Point(x.x + y.x, x.y + y.y)
end
function distance(x::Point, y::Point)
return sqrt( (x.x - y.x)^2 + (x.y - y.y)^2 )
end
p1 = Point(1, 1); p2 = Point(2, 2)
```
. . .
```{julia}
p1 + p2
```
. . .
```{julia}
distance(p1, p2)
```
## Composability --- Example with Autodiff {.smaller}
Suppose you are creating a new sort of graph structure that allows for differentiation and integration, i.e you can take gradients, Jacobians, Hessians and so on.
. . .
</br>
Imagine having to code the whole API in `libtorch` (`PyTorch` C++ backend). Including:
. . .
::: incremental
- types
- constructors
- linear algebra functions
- autodiff rules
:::
. . .
And in the end you can *only* use PyTorch.
You would have to do the whole thing again for JAX or any other autodiff backend.
## Composability --- Example with Autodiff (Julia) {.smaller}
Now let's see how we do this in Julia?
::: incremental
- We can create a package `DifferentialGraph.jl`.
- Add [`ChainRulesCore.jl`](https://github.com/JuliaDiff/ChainRulesCore.jl) as a dependency.
- Create forward- and reverse-mode derivative rules: `rrules` or `frules`
:::
. . .
Now we can use you differential graphs with all of these backends:
::: incremental
- [`ForwardDiff.jl`](https://github.com/JuliaDiff/ForwardDiff.jl): forward-mode AD
- [`ReverseDiff.jl`](https://github.com/JuliaDiff/ReverseDiff.jl): tape-based reverse-mode AD
- [`Zygote.jl`](https://github.com/FluxML/Zygote.jl): source-to-source reverse-mode AD
- [`Enzyme.jl`](https://github.com/EnzymeAD/Enzyme.jl): Julia bindings for Enzyme which ADs LLVM (low-level)
- [`Diffractor.jl`](https://github.com/JuliaDiff/Diffractor.jl): experimental mixed-mode AD meant to replace Zygote.jl
:::
::: footer
Since your graph has derivatives you can use gradient-based solvers to perform optimization.
:::
## Composability --- Examples from the Julia Ecosystem
::: incremental
- Bayesian Neural Nets: [`Flux.jl`](https://fluxml.ai/) neural network inside a [`Turing.jl`](https://turing.ml) model.
- Bayesian COVID modeling: [`DifferentialEquations.jl`](https://github.com/SciML/DifferentialEquations.jl) ODE inside a [`Turing.jl`](https://turing.ml) model.
- Quaternion ODE solver in the GPU: [`Quaternions.jl`](https://github.com/JuliaGeometry/Quaternions.jl) types in a [`DifferentialEquations.jl`](https://github.com/SciML/DifferentialEquations.jl)
ODE running in `CuArrays` from [`CUDA.jl`](https://github.com/JuliaGPU/CUDA.jl).
:::
## My Pitch {.scrollable}
::: incremental
- It is fast.
- It is easy to use.
- Learning the basics of Julia will make your life so much easier
in all other packages.
You don't need to learn specific package syntax to be effective in
using a certain package.
- A bliss to install in Windows, Mac, and Linux (even in clusters).
- Very good community, check the [discourse](https://discourse.julialang.org).
- Very "nerdy", "mathy", and "geeky" userbase.
- If you are creating new stuff, like research or algorithms,
you don't want to have to stumble upon FORTRAN or C code
(`scipy`, `numpy`, `pytorch` etc.).
In Julia everything is in Julia.
- You can easily mix-and-match types and functions from different packages,
as you saw in the previous slide.
- Good language interop:
- C: [standard library](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/)
- FORTRAN: [standard library](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/)
- Python: [`PyCall.jl`](https://github.com/JuliaPy/PyCall.jl)
- R: [`RCall.jl`](https://github.com/JuliaInterop/RCall.jl)
:::
## It's not all rainbows
::: incremental
- Hard to onboard people. Sometimes they don't want to learn new stuff
(I mean we still have FORTRAN around ...).
- Not widely used in the marketplace (but tons of academic usage).
- Some package ecosystems are not mature enough, e.g. survival analysis.
But, differential equations is way more mature than other
scientific computing languages.
- In my point of view, Julia's strength is in **scientific computing**.
For all other things, you might not have additional benefits.
:::
## Some nice packages {.scrollable}
::: incremental
- The whole [standard library](https://docs.julialang.org/en/v1/), especially [`LinearAlgebra`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/) module
- [`DifferentialEquations.jl`](https://docs.sciml.ai/DiffEqDocs/latest/), [`NeuralPDE.jl`](https://docs.sciml.ai/NeuralPDE/dev/)
and the whole [SciML package ecosystem](https://sciml.ai)
- [`Flux.jl`](https://fluxml.ai/) and [`MLJ.jl`](https://alan-turing-institute.github.io/MLJ.jl/dev/)
- [`DataFrames.jl`](https://dataframes.juliadata.org/stable/) and [`DataFramesMeta.jl`](https://juliadata.github.io/DataFramesMeta.jl/stable/)
- [`Makie.jl`](https://makie.org) and [`AlgebraOfGraphics.jl`](https://aog.makie.org)
- [`Turing.jl`](https://turing.ml)
- [`Pluto.jl`](https://plutojl.org)
- [`JuMP`](https://jump.dev)
- [`Distributions.jl`](https://juliastats.org/Distributions.jl/latest/)
:::
::: footer
these are all clickable links
:::
## Conclusions
:::incremental
- Julia is pretty darn awesome.
- Easy to get going, and you can always make it faster by
just optimizing your Julia code.
- No need to drop down to C++.
- Buuuut it can't beat Python at deep learning.
- Otherwise, it's worth a try.
- Godspeed to you.
:::
## Packages Used
:::: {.columns}
::: {.column width="50%"}
```{julia}
#| echo: false
using Pkg
deps = [pair.second for pair in Pkg.dependencies()]
deps = filter(p -> p.is_direct_dep, deps)
deps = filter(p -> !isnothing(p.version), deps)
list = ["$(p.name) $(p.version)" for p in deps]
sort!(list)
println("Julia: $(VERSION)")
println(join(list, '\n'))
```
:::
::: {.column width="50%"}
```{julia}
#| echo: false
py"""
import sys
import numpy as np
import scipy
import pandas as pd
print(f"Python: {sys.version}")
print(f"numpy: {np.__version__}")
print(f"scipy: {scipy.__version__}")
print(f"pandas: {pd.__version__}")
"""
```
:::
::::
## System Information
```{julia}
#| echo: false
versioninfo()
```