# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="8fa78ae20a8a"
# ## 4.1 Introduction
#
# The logistic problem discussed in Section 4 is how to tackle two types of bias for a finite sum problem in a decentralized environment using stochastic gradient descent. The finite sum problem is the minimization of an average of functions.
# \begin{equation*}
#     f(x) = \min_{x} \frac{1}{M} \sum_{i=1}^{M} f_i(x)
# \end{equation*}
# In a machine learning setting where there is a set of $M$ data points, we can interpret this problem as minimizing the average of $M$ error functions $f_i$ evaluated at the corresponding $i$th data point. Here we assume each $f_i$ is $L_i$-smooth and $f(x)$ is $\mu$-strongly convex. The first bias comes from the inaccurate gradient estimation due to the stochatic nature of SGD, which can be resolved with a stochastic variance reduced gradient descent (SVRG). The second type of bias is introduced due to the concensus bias of gradient estimation in a decentralized environment, which can be addressed using exact diffusion.
#
# ### 4.1.1 Organization
#
# The organization of Section 4 is as follows: \[Will add link when these sections are ready\]
#
# - Sec. 4.2 Stochastic Variance Reduced Gradient Descent
#
# - Sec. 4.3 Decentralized Exact Diffusion
#
# - Sec. 4.4 Combine SVRG and Exact Diffusion together
#
# ### 4.1.2 Initialize BlueFog and test it
#
# All contents in this section are displayed in Jupyter notebook, and all experimental examples are written with BlueFog and iParallel. Readers not familiar with how to run BlueFog in ipython notebook environment is encouraged to read Sec. [HelloWorld section] first. In the following codes, we will initialize BlueFog and test whether it works normally.
#
# In the following code, you should be able to see the id of your CPUs. We use 4 CPUs to conduct the following experiment.

# + id="21259bb48af6"
import ipyparallel as ipp

rc = ipp.Client(profile="bluefog")
rc.ids

# + [markdown] id="e77fff57c316"
# Let each agent import necessary modules and then initialize BlueFog. You should be able to see the printed information like:  
#
# > \[stdout:0\] Hello, I am 1 among 4 processes
# > 
# > ...

# + id="409b4b3a727d"
# %%px
import numpy as np
import bluefog.torch as bf
import torch
from bluefog.common import topology_util
import networkx as nx

bf.init()
print(f"Hello, I am {bf.rank()} among {bf.size()} processes")

# + [markdown] id="5fda00e5ab54"
# Push seed to each agent so that the simulation can be reproduced.

# + id="66bf52fb75b2"
dview = rc[:]  # A DirectView of all engines
dview.block = True

# Push the data into all workers
dview.push({"seed": 2021}, block=True)

# + [markdown] id="d5dc19fe9b61"
# After running the following code, you should be able to see the printed information like 
#
# > \[stdout:0\] I received seed as value:  2021
# > 
# > ...

# + id="76cd0150e2ff"
# %%px
print("I received seed as value: ", seed)

# + [markdown] id="298aba508e9c"
# Congratulations! Your BlueFog is initialized and tested successfully.

StarcoderdataJupyterScriptsDedupFiltered