Slide 1

Slide 1 text

Marianne Stecklina Say goodbye to configuration hassles jsonargparse

Slide 2

Slide 2 text

● Deep Learning Engineer at omni:us ● colleague of Mauricio Villegas, creator of jsonargparse ● early user of jsonargparse How I came to give this talk That’s me! That’s Mauricio!

Slide 3

Slide 3 text

Introduction 1

Slide 4

Slide 4 text

jsonargparse is an open-source library that helps to configure your code by creating a CLI. *CLI = command line interface

Slide 5

Slide 5 text

Why CLIs are great compared to… no interface ● run program differently without modifying code ● easier to use for others graphical user interface (GUI) ● easier to create ● perfect for batch tasks ● perfect for scheduled tasks ● useful in terminal-only environments

Slide 6

Slide 6 text

Survey What tools are you currently using? Take the poll in #b05-b06 on Discord

Slide 7

Slide 7 text

jsonargparse basics 2

Slide 8

Slide 8 text

https://github.com/stecklin/pycon22-jsonargparse ● Compare python 0_argparse.py --help and python 1_cli.py --help ● Run python 1_cli.py PyCON --year 2022 ● Print config using python 1_cli.py --print_config > config.yaml ● Edit config.yaml ● Run python 1_cli.py --config config.yaml ● Run python 1_cli.py --config config.yaml --year 2024 ● Run python 1_cli.py --year 2024 --config config.yaml ● Run PYCON_CONFERENCE_NAME=PyCON PYCON_YEAR=2024 python 2_cli_with_env_variables.py Live Demo

Slide 9

Slide 9 text

Key features ● CLIs with minimal code overhead ● possible to parse ○ command line arguments ○ config files ○ environment variables

Slide 10

Slide 10 text

Hierarchical configs 3

Slide 11

Slide 11 text

Use case: Train image classifier def train(dataset: Cifar10Dataset, model: ConvNet): ... frog

Slide 12

Slide 12 text

https://github.com/stecklin/pycon22-jsonargparse ● Run python 3_image_classification.py --help ● Run python 3_image_classification.py --dataset.help Cifar10Dataset ● Run python 3_image_classification.py --config configs/3_config.yaml --dataset.augment maybe ● Run python 3_image_classification.py --config configs/3_config.yaml --dataset.data_dir /home/ghosts/ Live Demo

Slide 13

Slide 13 text

Key features ● config (and help) is hierarchical ● config structure always in sync with code ● type checks

Slide 14

Slide 14 text

Subclasses 4

Slide 15

Slide 15 text

Different datasets & models ConvNet kernel_size: int num_layers: int VisionTransformer num_layers: int num_heads: int hidden_dim: int mlp_dim: int {abstract} Model num_classes: int Cifar10Dataset ImageNetDataset {abstract} Dataset data_dir: path_dr batch_size: int augment: bool

Slide 16

Slide 16 text

https://github.com/stecklin/pycon22-jsonargparse ● Run python 4_subclasses.py --help ● Run python 4_subclasses.py --dataset ImageNetDataset --model ConvNet --print_config ● Run python 4_subclasses.py --config configs/4_config.yaml ● Run python 4_subclasses.py --dataset configs/4_cifar10.yaml --model configs/4_conv_net.yaml ● Run python 4_subclasses.py --config configs/4_config_with_subconfigs.yaml ● Run python 5_model_composition.py --config configs/5_config.yaml Live Demo

Slide 17

Slide 17 text

Key features ● switch between subclasses ● compose config into multiple files

Slide 18

Slide 18 text

Adoption 5

Slide 19

Slide 19 text

PyTorch Lightning

Slide 20

Slide 20 text

Links ● jsonargparse: https://github.com/omni-us/jsonargparse ● Example code: https://github.com/stecklin/pycon22-jsonargparse ● Slides: https://speakerdeck.com/stecklin ● Twitter: @MStecklina Thanks for listening!