Skip to content
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

[Minor] Profling and logging tutorial #1628

Open
wants to merge 8 commits into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -750,9 +750,8 @@
"source": [
"try:\n",
" # it already installed dependencies\n",
" from torchsummary import summary\n",
" from torchviz import make_dot\n",
"except:\n",
"except ImportError:\n",
" # install graphviz on system\n",
" import platform\n",
"\n",
Expand All @@ -768,7 +767,6 @@
" !pip install torchviz\n",
" !pip install graphviz\n",
" # import\n",
" from torchsummary import summary\n",
" from torchviz import make_dot"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1308,9 +1308,8 @@
"source": [
"try:\n",
" # it already installed dependencies\n",
" from torchsummary import summary\n",
" from torchviz import make_dot\n",
"except:\n",
"except ImportError:\n",
" # install graphviz on system\n",
" import platform\n",
"\n",
Expand All @@ -1326,7 +1325,6 @@
" !pip install torchviz\n",
" !pip install graphviz\n",
" # import\n",
" from torchsummary import summary\n",
" from torchviz import make_dot"
]
},
Expand Down
2 changes: 0 additions & 2 deletions docs/source/how-to-guides/feature-guides/mlflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@
"# Copy and paste url from command line to web browser\n",
"\n",
"import mlflow\n",
"import torchmetrics\n",
"from mlflow.data.pandas_dataset import PandasDataset\n",
"\n",
"if local:\n",
Expand All @@ -272,7 +271,6 @@
" )\n",
"\n",
" import mlflow.pytorch\n",
" from mlflow.client import MlflowClient\n",
"\n",
" model_name = \"NeuralProphet\"\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
" # it already installed dependencies\n",
" from torchsummary import summary\n",
" from torchviz import make_dot\n",
"except:\n",
"except ImportError:\n",
" # install graphviz on system\n",
" import platform\n",
"\n",
Expand Down Expand Up @@ -69,7 +69,7 @@
"source": [
"try:\n",
" from neuralprophet import NeuralProphet\n",
"except:\n",
"except ImportError:\n",
" # if NeuralProphet is not installed yet:\n",
" !pip install git+https://github.com/ourownstory/neural_prophet.git\n",
" from neuralprophet import NeuralProphet"
Expand Down
4,747 changes: 4,747 additions & 0 deletions docs/source/how-to-guides/feature-guides/pl_profiling/fit-advanced.txt

Large diffs are not rendered by default.

308 changes: 308 additions & 0 deletions docs/source/how-to-guides/feature-guides/profiling_and_logging.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tutorial 11: Profiling and Logging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This tutorial will guide you through setting up profiling and logging in your NeuralProphet models using PyTorch's Profiler and TensorBoard Logger. Profiling helps you understand where your model spends time and memory, and logging keeps track of these metrics for later analysis."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Profiling"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using the Simple Profiler"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Simple Profiler is a lightweight and straightforward tool that tracks the execution time of different sections of your model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pytorch_lightning.profilers import SimpleProfiler\n",
"\n",
"# Configure Simple Profiler\n",
"trainer_config = {\"profiler\": SimpleProfiler(dirpath=\"./pl_profiling\", filename=\"simple\")}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Integrating the Simple Profiler into the Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from neuralprophet import NeuralProphet, set_log_level\n",
"\n",
"# Load the dataset from the CSV file using pandas\n",
"df = pd.read_csv(\"https://github.com/ourownstory/neuralprophet-data/raw/main/kaggle-energy/datasets/tutorial01.csv\")\n",
"\n",
"# Disable logging messages unless there is an error\n",
"set_log_level(\"ERROR\")\n",
"\n",
"# Model and prediction\n",
"m = NeuralProphet(trainer_config=trainer_config)\n",
"m.fit(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using the Advanced Profiler"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Advanced Profiler offers more detailed insights into your model's performance, including function-level statistics and memory usage."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pytorch_lightning.profilers import AdvancedProfiler\n",
"\n",
"# Configure Advanced Profiler\n",
"trainer_config = {\"profiler\": AdvancedProfiler(dirpath=\"./pl_profiling\", filename=\"advanced\")}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Integrating the Advanced Profiler into the Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from neuralprophet import NeuralProphet, set_log_level\n",
"\n",
"# Load the dataset from the CSV file using pandas\n",
"df = pd.read_csv(\"https://github.com/ourownstory/neuralprophet-data/raw/main/kaggle-energy/datasets/tutorial01.csv\")\n",
"\n",
"# Disable logging messages unless there is an error\n",
"set_log_level(\"ERROR\")\n",
"\n",
"# Model and prediction\n",
"m = NeuralProphet(trainer_config=trainer_config)\n",
"m.fit(df, learning_rate=0.1, epochs=10, batch_size=128, progress=False, minimal=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"You can check the profiling reports in the generated text files under ``` ./pl_profiling ```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Logging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setting Up TensorBoard Logger"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pytorch_lightning.loggers import TensorBoardLogger\n",
"\n",
"# Configure TensorBoard logger\n",
"trainer_config = {\"logger\": TensorBoardLogger(\"tb_logs\", name=\"NeuralProphet\")}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Integrating Logging into the Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from neuralprophet import NeuralProphet\n",
"\n",
"# Load the dataset from the CSV file using pandas\n",
"df = pd.read_csv(\"https://github.com/ourownstory/neuralprophet-data/raw/main/kaggle-energy/datasets/tutorial01.csv\")\n",
"\n",
"\n",
"# Model and prediction\n",
"m = NeuralProphet()\n",
"metrics = m.fit(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualizing Logs in TensorBoard"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"tensorboard --logdir tb_logs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Open the provided URL in your browser, and you'll be able to explore various metrics like training loss, validation loss, and more, all tracked during the training process."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Integrating Profiling with TensorBoard Logging\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, make sure to install ```torch-tb-profiler```. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"from pytorch_lightning.loggers import TensorBoardLogger\n",
"\n",
"trainer_config = {}\n",
"\n",
"# Configure TensorBoard logger\n",
"trainer_config[\"logger\"] = TensorBoardLogger(\"tb_logs\", name=\"NeuralProphet\")\n",
"\n",
"# Integrate profiler with logging\n",
"trainer_config[\"profiler\"] = torch.profiler.profile(\n",
" on_trace_ready=torch.profiler.tensorboard_trace_handler(\"tb_logs/profiler0\"),\n",
" record_shapes=True,\n",
" profile_memory=True,\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from neuralprophet import NeuralProphet\n",
"from neuralprophet import set_random_seed\n",
"\n",
"set_random_seed(42)\n",
"\n",
"# Load the dataset from the CSV file using pandas\n",
"df = pd.read_csv(\"https://github.com/ourownstory/neuralprophet-data/raw/main/kaggle-energy/datasets/tutorial01.csv\")\n",
"\n",
"# Model and prediction\n",
"m = NeuralProphet()\n",
"\n",
"df_train, df_val = m.split_df(df, valid_p=0.2)\n",
"\n",
"# Set the deterministic flag to True\n",
"metrics = m.fit(df_train, validation_df=df_val, progress=None, deterministic=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.7 ('.venv': venv)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "92c9cff0281419e73896333b85e681aea9374fd743c51074843eeada7c3f6baf"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@
"# Set loggers to ERROR level\n",
"import logging\n",
"import warnings\n",
"from neuralprophet import set_log_level\n",
"\n",
"logging.getLogger(\"prophet\").setLevel(logging.ERROR)\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"from neuralprophet import set_log_level\n",
"\n",
"set_log_level(\"ERROR\")"
]
Expand Down
Loading
Loading