<div align="center">
  <img src="https://raw.githubusercontent.com/Helmholtz-AI-Energy/perun/main/doc/images/perun.svg">
</div>

Have you ever wondered how much energy is used when training your neural network on the MNIST dataset? Want to get scared because of impact you are having on the evironment while doing "valuable" research? Are you interested in knowing how much carbon you are burning playing with DALL-E just to get attention on twitter? If the thing that was missing from your machine learning workflow was existential dread, this is the correct package for you!

## Installation

From PyPI:

```$ pip install perun```

From Github:

```$ pip install git+https://github.com/Helmholtz-AI-Energy/perun```

### Parallel h5py

To build h5py with mpi support:

```CC=mpicc HDF5_MPI="ON" pip install --no-binary h5py h5py```

## Usage

### Command line

To get a quick report of the power usage of a python script simply run

```$ perun monitor --format yaml path/to/your/script.py [args]```

Or

```$ python -m perun monitor --format json -o results/ path/to/your/script.py [args]```


#### Subcommands

Perun subcommands have some shared options that are typed before the subcommands.

```
Usage: perun [OPTIONS] COMMAND [ARGS]...

  Perun: Energy measuring and reporting tool.

Options:
  --version                       Show the version and exit.
  -c, --configuration FILE
  -f, --format [txt|yaml|yml|json]
                                  report print format
  -f, --frequency FLOAT           sampling frequency (in Hz)
  --format [txt|yaml|yml|json]    report print format
  --data_out DIRECTORY            experiment data output directory
  -l, --log_lvl [DEBUG|INFO|WARN|ERROR|CRITICAL]
                                  Loggging level
  --pue FLOAT                     Data center Power usage efficiency
  --location TEXT                 Data center location
  --help                          Show this message and exit.

Commands:
  monitor      Gather power consumption from hardware devices while...
  postprocess  Apply post-processing to EXP_HDF5 experiment file.
  report       Print consumption report from EXP_HDF5 on the command line...
  showconf     Print current perun configuration in INI format.
```

**monitor**

Monitor energy usage of a python script.

```
Usage: perun monitor [OPTIONS] SCRIPT [SCRIPT_ARGS]...

  Gather power consumption from hardware devices while SCRIPT [SCRIPT_ARGS] is
  running.

  SCRIPT is a path to the python script to monitor, run with arguments
  SCRIPT_ARGS.

Options:
  --help                        Show this message and exit.
```

**report**

Print a report from previous monitoring results.

```
Usage: perun report [OPTIONS] EXP_HDF5

  Print consumption report from EXP_HDF5 on the command line on the desired
  format.

  EXP_HDF5 is an hdf5 file generated by perun after monitoring a script,
  containing data gathered from hardware devices.

Options:
  --help                          Show this message and exit.
```

**postprocess**

Apply postprocessing to existing perun experiment data.

```
Usage: perun postprocess [OPTIONS] EXP_HDF5

  Apply post-processing to EXP_HDF5 experiment file.

  EXP_HDF5 is an hdf5 file generated by perun after monitoring a script,
  containing data gathered from hardware devices.

Options:
  --help  Show this message and exit.
```

**showconf**

Prints the current option configurations based on the global, local configurations files and command line options.

'''
Usage: perun showconf [OPTIONS]

  Print current perun configuration in INI format.

Options:
  --default  Print default configuration
  --help     Show this message and exit.
'''

### Decorator

Or decorate the function that you want analysed

```python
import perun

@perun.monitor(outDir="results/", format="txt")
def training_loop(args, model, device, train_loader, test_loader, optimizer, scheduler):
    for epoch in range(1, args.epochs + 1):
        train(args, model, device, train_loader, optimizer, epoch)
        test(model, device, test_loader)
        scheduler.step()
```


Optional Arguments:

|   |   |
|---|---|
|frequency: FLOAT             |sampling frequency (in Hz) |
|format: [txt|yaml|yml|json]  |report print format |
|outdir DIRECTORY:            |experiment data output directory |


## Configuration

There are multiple ways to configure perun, with a different level of priorities.

1. CMD Line options and Env Variables

The highest priority is given to command line options and environmental variables. The options are shown in the command line section. The options can also be passed as environmental variables by adding the prefix 'PERUN' to them. Ex. "--format txt" -> PERUN_FORMAT=txt

2. Local INI file

Perun will look into the cwd for ".perun.ini" file, where options can be fixed for the directory.

Example:
```ini
[report]
format = txt
pue = 1.58
location = DE

[monitor]
frequency = 1
data_out = ./results

[perun]
log_lvl = WARN
```

The location of the file can be changed using the option "-c" or "PERUN_CONFIGURATION".

3. Global INI file

If the file ~/.config/perun.ini is found, perun will override the default configuration with the contents of the file.


### Priority

CMD LINE and ENV > Local INI > Global INI > Default options

## Experiment data

Raw data is saved in a hdf5 file, where results over multiple runs are accumulated.

At the top level, the root group containts groups for all individual runs, as well as information about creation date and total energy consumption over multiple runs.

Experiments contain the information about a single run of the python script, and has information about nodes, devices per node, total runtime and avgerage power draw.

The same applies at the node level.

At the lowest group level, datasets with sample data from individual devices is collected, with the dataset attributes providing the device metadata, like id, measurement unit and magnitude, and possible value range.

- exp_name (Root group)
  - exp_0 (Group)
    - node_name_0 (Group)
      - device_0 (Dataset)
        - units (Attribute)
        - mag (Attribute)
        - long_name (Attribute)
        - ...
      - device_1 (Dataset)
      - ...
    - node_name_1 (Group)
    - ...
  - exp_1 (Group)
  - ...
