# Buggy code
def parse_args():
    parser = argparse.ArgumentParser(
        description="Train a model for image classification."
    )
    parser.add_argument(
        "--data-dir",
        type=str,
        default="~/.mxnet/datasets/imagenet",
        help="training and validation pictures to use.",
    )
    parser.add_argument(
        "--rec-train",
        type=str,
        default="~/.mxnet/datasets/imagenet/rec/train.rec",
        help="the training data",
    )
    parser.add_argument(
        "--rec-train-idx",
        type=str,
        default="~/.mxnet/datasets/imagenet/rec/train.idx",
        help="the index of training data",
    )
    parser.add_argument(
        "--rec-val",
        type=str,
        default="~/.mxnet/datasets/imagenet/rec/val.rec",
        help="the validation data",
    )
    parser.add_argument(
        "--rec-val-idx",
        type=str,
        default="~/.mxnet/datasets/imagenet/rec/val.idx",
        help="the index of validation data",
    )
    parser.add_argument(
        "--use-rec",
        action="store_true",
        help="use image record iter for data input. default is false.",
    )
    parser.add_argument(
        "--batch-size",
        type=int,
        default=32,
        help="training batch size per device (CPU/GPU).",
    )
    parser.add_argument(
        "--dtype",
        type=str,
        default="float32",
        help="data type for training. default is float32",
    )
    parser.add_argument(
        "--num-gpus", type=int, default=0, help="number of gpus to use."
    )
    parser.add_argument(
        "-j",
        "--num-data-workers",
        dest="num_workers",
        default=4,
        type=int,
        help="number of preprocessing workers",
    )
    parser.add_argument(
        "--num-epochs", type=int, default=3, help="number of training epochs."
    )
    parser.add_argument(
        "--lr", type=float, default=0.1, help="learning rate. default is 0.1."
    )
    parser.add_argument(
        "--momentum",
        type=float,
        default=0.9,
        help="momentum value for optimizer, default is 0.9.",
    )
    parser.add_argument(
        "--wd", type=float, default=0.0001, help="weight decay rate. default is 0.0001."
    )
    parser.add_argument(
        "--lr-mode",
        type=str,
        default="step",
        help="learning rate scheduler mode. options are step, poly and cosine.",
    )
    parser.add_argument(
        "--lr-decay",
        type=float,
        default=0.1,
        help="decay rate of learning rate. default is 0.1.",
    )
    parser.add_argument(
        "--lr-decay-period",
        type=int,
        default=0,
        help="interval for periodic learning rate decays. default is 0 to disable.",
    )
    parser.add_argument(
        "--lr-decay-epoch",
        type=str,
        default="40,60",
        help="epochs at which learning rate decays. default is 40,60.",
    )
    parser.add_argument(
        "--warmup-lr",
        type=float,
        default=0.0,
        help="starting warmup learning rate. default is 0.0.",
    )
    parser.add_argument(
        "--warmup-epochs", type=int, default=0, help="number of warmup epochs."
    )
    parser.add_argument(
        "--last-gamma",
        action="store_true",
        help="whether to init gamma of the last BN layer in each bottleneck to 0.",
    )
    parser.add_argument(
        "--mode",
        type=str,
        help="mode in which to train the model. options are symbolic, imperative, hybrid",
    )
    parser.add_argument(
        "--model",
        type=str,
        required=True,
        help="type of model to use. see vision_model for options.",
    )
    parser.add_argument(
        "--input-size",
        type=int,
        default=224,
        help="size of the input image size. default is 224",
    )
    parser.add_argument(
        "--crop-ratio",
        type=float,
        default=0.875,
        help="Crop ratio during validation. default is 0.875",
    )
    parser.add_argument(
        "--use-pretrained",
        action="store_true",
        help="enable using pretrained model from gluon.",
    )
    parser.add_argument(
        "--use_se",
        action="store_true",
        help="use SE layers or not in resnext. default is false.",
    )
    parser.add_argument(
        "--mixup",
        action="store_true",
        help="whether train the model with mix-up. default is false.",
    )
    parser.add_argument(
        "--mixup-alpha",
        type=float,
        default=0.2,
        help="beta distribution parameter for mixup sampling, default is 0.2.",
    )
    parser.add_argument(
        "--mixup-off-epoch",
        type=int,
        default=0,
        help="how many last epochs to train without mixup, default is 0.",
    )
    parser.add_argument(
        "--label-smoothing",
        action="store_true",
        help="use label smoothing or not in training. default is false.",
    )
    parser.add_argument(
        "--no-wd",
        action="store_true",
        help="whether to remove weight decay on bias, and beta/gamma for batchnorm layers.",
    )
    parser.add_argument(
        "--teacher",
        type=str,
        default=None,
        help="teacher model for distillation training",
    )
    parser.add_argument(
        "--temperature",
        type=float,
        default=20,
        help="temperature parameter for distillation teacher model",
    )
    parser.add_argument(
        "--hard-weight",
        type=float,
        default=0.5,
        help="weight for the loss of one-hot label for distillation training",
    )
    parser.add_argument(
        "--batch-norm",
        action="store_true",
        help="enable batch normalization or not in vgg. default is false.",
    )
    parser.add_argument(
        "--save-frequency", type=int, default=10, help="frequency of model saving."
    )
    parser.add_argument(
        "--save-dir", type=str, default="params", help="directory of saved models"
    )
    parser.add_argument(
        "--resume-epoch", type=int, default=0, help="epoch to resume training from."
    )
    parser.add_argument(
        "--resume-params", type=str, default="", help="path of parameters to load from."
    )
    parser.add_argument(
        "--resume-states",
        type=str,
        default="",
        help="path of trainer state to load from.",
    )
    parser.add_argument(
        "--log-interval",
        type=int,
        default=50,
        help="Number of batches to wait before logging.",
    )
    parser.add_argument(
        "--logging-file",
        type=str,
        default="train_imagenet.log",
        help="name of training log file",
    )
    parser.add_argument(
        "--use-gn", action="store_true", help="whether to use group norm."
    )
    # additional arguments for gradient compression
    parser.add_argument("--compressor", type=str, default="", help="which compressor")
    parser.add_argument("--ef", type=str, default="", help="which error-feedback")
    parser.add_argument(
        "--compress-momentum", type=str, default="", help="which compress momentum"
    )
    parser.add_argument(
        "--onebit-scaling",
        action="store_true",
        default=False,
        help="enable scaling for onebit compressor",
    )
    parser.add_argument(
        "--fp16-pushpull",
        action="store_true",
        default=False,
        help="use fp16 compression during pushpull",
    )

    opt = parser.parse_args()
    return opt


# Traceback (TypeError)
In [1]: import tensorflow as tf

In [2]: import byteps.tensorflow as bps
WARNING: Logging before flag parsing goes to stderr.
W0627 11:36:47.010180 139917697820480 deprecation_wrapper.py:119] From /private/home/yuxinwu/.local/lib/python3.6/site-packages/byteps/tensorflow/__init__.py:79: The name tf.train.SessionRunHook is deprecated. Please use tf.estimator.SessionRunHook instead.

W0627 11:36:47.010504 139917697820480 deprecation_wrapper.py:119] From /private/home/yuxinwu/.local/lib/python3.6/site-packages/byteps/tensorflow/__init__.py:111: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.


In [3]: bps.push_pull(tf.constant([0.0]))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-fc0e9eb4f2bb> in <module>()
----> 1 bps.push_pull(tf.constant([0.0]))

~/.local/lib/python3.6/site-packages/byteps/tensorflow/__init__.py in push_pull(tensor, scope, average, device_dense, device_sparse, compression)
50         byteps_size = tf.cast(size(), dtype=tensor.dtype)
51         tensor_compressed, ctx = compression.compress(tensor)
---> 52         summed_tensor_compressed = _push_pull(tensor_compressed, scope)
53         summed_tensor = compression.decompress(summed_tensor_compressed, ctx)
54         new_tensor = (tf.div(summed_tensor, byteps_size)

~/.local/lib/python3.6/site-packages/byteps/tensorflow/ops.py in _push_pull(tensor, scope, name)
80     if name is None and not _executing_eagerly():
81         name = 'BytePSPushPull_%s' % _normalize_name(tensor.name)
---> 82     TF_LIB_CTYPES.byteps_tensorflow_declare_tensor(ctypes.c_char_p(scope+name))
83     return C_LIB.byteps_push_pull(tensor, name=name)
84

TypeError: bytes or integer address expected instead of str instance

# Fixed code
def parse_args():
    parser = argparse.ArgumentParser(
        description="Train a model for image classification."
    )
    parser.add_argument(
        "--data-dir",
        type=str,
        default="~/.mxnet/datasets/imagenet",
        help="training and validation pictures to use.",
    )
    parser.add_argument(
        "--rec-train",
        type=str,
        default="~/.mxnet/datasets/imagenet/rec/train.rec",
        help="the training data",
    )
    parser.add_argument(
        "--rec-train-idx",
        type=str,
        default="~/.mxnet/datasets/imagenet/rec/train.idx",
        help="the index of training data",
    )
    parser.add_argument(
        "--rec-val",
        type=str,
        default="~/.mxnet/datasets/imagenet/rec/val.rec",
        help="the validation data",
    )
    parser.add_argument(
        "--rec-val-idx",
        type=str,
        default="~/.mxnet/datasets/imagenet/rec/val.idx",
        help="the index of validation data",
    )
    parser.add_argument(
        "--use-rec",
        action="store_true",
        help="use image record iter for data input. default is false.",
    )
    parser.add_argument(
        "--batch-size",
        type=int,
        default=32,
        help="training batch size per device (CPU/GPU).",
    )
    parser.add_argument(
        "--dtype",
        type=str,
        default="float32",
        help="data type for training. default is float32",
    )
    parser.add_argument(
        "--num-gpus", type=int, default=0, help="number of gpus to use."
    )
    parser.add_argument(
        "-j",
        "--num-data-workers",
        dest="num_workers",
        default=4,
        type=int,
        help="number of preprocessing workers",
    )
    parser.add_argument(
        "--num-epochs", type=int, default=3, help="number of training epochs."
    )
    parser.add_argument(
        "--lr", type=float, default=0.1, help="learning rate. default is 0.1."
    )
    parser.add_argument(
        "--momentum",
        type=float,
        default=0.9,
        help="momentum value for optimizer, default is 0.9.",
    )
    parser.add_argument(
        "--wd", type=float, default=0.0001, help="weight decay rate. default is 0.0001."
    )
    parser.add_argument(
        "--lr-mode",
        type=str,
        default="step",
        help="learning rate scheduler mode. options are step, poly and cosine.",
    )
    parser.add_argument(
        "--lr-decay",
        type=float,
        default=0.1,
        help="decay rate of learning rate. default is 0.1.",
    )
    parser.add_argument(
        "--lr-decay-period",
        type=int,
        default=0,
        help="interval for periodic learning rate decays. default is 0 to disable.",
    )
    parser.add_argument(
        "--lr-decay-epoch",
        type=str,
        default="40,60",
        help="epochs at which learning rate decays. default is 40,60.",
    )
    parser.add_argument(
        "--warmup-lr",
        type=float,
        default=0.0,
        help="starting warmup learning rate. default is 0.0.",
    )
    parser.add_argument(
        "--warmup-epochs", type=int, default=0, help="number of warmup epochs."
    )
    parser.add_argument(
        "--last-gamma",
        action="store_true",
        help="whether to init gamma of the last BN layer in each bottleneck to 0.",
    )
    parser.add_argument(
        "--mode",
        type=str,
        help="mode in which to train the model. options are symbolic, imperative, hybrid",
    )
    parser.add_argument(
        "--model",
        type=str,
        required=True,
        help="type of model to use. see vision_model for options.",
    )
    parser.add_argument(
        "--input-size",
        type=int,
        default=224,
        help="size of the input image size. default is 224",
    )
    parser.add_argument(
        "--crop-ratio",
        type=float,
        default=0.875,
        help="Crop ratio during validation. default is 0.875",
    )
    parser.add_argument(
        "--use-pretrained",
        action="store_true",
        help="enable using pretrained model from gluon.",
    )
    parser.add_argument(
        "--use_se",
        action="store_true",
        help="use SE layers or not in resnext. default is false.",
    )
    parser.add_argument(
        "--mixup",
        action="store_true",
        help="whether train the model with mix-up. default is false.",
    )
    parser.add_argument(
        "--mixup-alpha",
        type=float,
        default=0.2,
        help="beta distribution parameter for mixup sampling, default is 0.2.",
    )
    parser.add_argument(
        "--mixup-off-epoch",
        type=int,
        default=0,
        help="how many last epochs to train without mixup, default is 0.",
    )
    parser.add_argument(
        "--label-smoothing",
        action="store_true",
        help="use label smoothing or not in training. default is false.",
    )
    parser.add_argument(
        "--no-wd",
        action="store_true",
        help="whether to remove weight decay on bias, and beta/gamma for batchnorm layers.",
    )
    parser.add_argument(
        "--teacher",
        type=str,
        default=None,
        help="teacher model for distillation training",
    )
    parser.add_argument(
        "--temperature",
        type=float,
        default=20,
        help="temperature parameter for distillation teacher model",
    )
    parser.add_argument(
        "--hard-weight",
        type=float,
        default=0.5,
        help="weight for the loss of one-hot label for distillation training",
    )
    parser.add_argument(
        "--batch-norm",
        action="store_true",
        help="enable batch normalization or not in vgg. default is false.",
    )
    parser.add_argument(
        "--save-frequency", type=int, default=10, help="frequency of model saving."
    )
    parser.add_argument(
        "--save-dir", type=str, default="params", help="directory of saved models"
    )
    parser.add_argument(
        "--resume-epoch", type=int, default=0, help="epoch to resume training from."
    )
    parser.add_argument(
        "--resume-params", type=str, default="", help="path of parameters to load from."
    )
    parser.add_argument(
        "--resume-states",
        type=str,
        default="",
        help="path of trainer state to load from.",
    )
    parser.add_argument(
        "--log-interval",
        type=int,
        default=50,
        help="Number of batches to wait before logging.",
    )
    parser.add_argument(
        "--logging-file",
        type=str,
        default="train_imagenet.log",
        help="name of training log file",
    )
    parser.add_argument(
        "--use-gn", action="store_true", help="whether to use group norm."
    )
    # additional arguments for gradient compression
    parser.add_argument("--compressor", type=str, default="", help="which compressor")
    parser.add_argument("--ef", type=str, default="", help="which error-feedback")
    parser.add_argument(
        "--compress-momentum", type=str, default="", help="which compress momentum"
    )
    parser.add_argument(
        "--onebit-scaling",
        action="store_true",
        default=False,
        help="enable scaling for onebit compressor",
    )
    parser.add_argument("--k", default=1, type=int, help="topk or randomk")
    parser.add_argument(
        "--fp16-pushpull",
        action="store_true",
        default=False,
        help="use fp16 compression during pushpull",
    )

    opt = parser.parse_args()
    return opt