Package: gradient-utils
Gradient UtilsGet started:Create Account•Install CLI•Tutorials•DocsResources:Website•Blog•Support•Contact SalesGradient is an an end-to-end MLOps platform that enables individuals and organizations to quickly develop, train, and deploy Deep Learning models. The Gradient software stack runs on any infrastructure e.g. AWS, GCP, on-premise and low-costPaperspace GPUs. Leverage automatic versioning, distributed training, built-in graphs & metrics, hyperparameter search, GradientCI, 1-click Jupyter Notebooks, our Python SDK, and more.This is an SDK for performing Machine Learning with Gradientº, it can be installed in addition togradient-cli.RequirementsThis SDK requires Python 3.6+.To install it, run:pipinstallgradient-utilsUsageMetricsLibrary for logging custom and framework metrics in Gradient.Usage example:fromgradient_utils.metricsimportinit,add_metrics,MetricsLogger# Initialize metrics loggingwithinit():# do work herepass# Capture metrics produced by tensorboardwithinit(sync_tensorboard=True):# do work herepass# Log metrics with a single commandadd_metrics({"loss":0.25,"accuracy":0.99})# Insert metrics with a step value# Note: add_metrics should be called once for a step.#       Multiple calls with the same step may result in loss of metrics.add_metrics({"loss":0.25,"accuracy":0.99},step=0)# For more advanced use cases use the MetricsLoggerlogger=MetricsLogger()logger.add_gauge("loss")# add a specific gaugelogger["loss"].set(0.25)logger["loss"].inc()logger.add_gauge("accuracy")logger["accuracy"].set(0.99)logger.push_metrics()# you must explicitly push metrics each time you mutate values when using MetricsLogger# You can also use steps with the MetricsLoggerlogger=MetricsLogger(step=0)logger.add_gauge("loss")logger["loss"].set(0.25)logger.push_metrics()logger.set_step(1)# update step explicitlylogger["loss"].set(0.25)logger.push_metrics()Multinode Helper FunctionsMultinode GRPC TensorflowSet the TF_CONFIG environment variableFor multi-worker training, you need to set theTF_CONFIGenvironment variable for each binary running in your cluster. Set the value ofTF_CONFIGto a JSON string that specifies each task within the cluster, including each task's address and role within the cluster. We've provided a Kubernetes template in the tensorflow/ecosystem repo which setsTF_CONFIGfor your training tasks.get_tf_config()Function to set value ofTF_CONFIGwhen run on machines within Paperspace infrastructure.It can raise aConfigErrorexception with message if there's a problem with its configuration in a particular machine.Usage example:fromgradient_utilsimportget_tf_configget_tf_config()Utility Functionsget_mongo_conn_str()Function to check and construct MongoDB connection string.It returns a connection string to MongoDB.It can raise aConfigErrorexception with message if there's a problem with any values used to prepare the MongoDB connection string.Usage example:fromgradient_utilsimportget_mongo_conn_strconn_str=get_mongo_conn_str()data_dir()Function to retrieve path to job space.Usage example:fromgradient_utilsimportdata_dirjob_space=data_dir()model_dir()Function to retrieve path to model space.Usage example:fromgradient_utilsimportmodel_dirmodel_path=model_dir(model_name)export_dir()Function to retrieve path for model export.Usage example:fromgradient_utilsimportexport_dirmodel_path=export_dir(model_name)worker_hosts()Function to retrieve information about worker hosts.Usage example:fromgradient_utilsimportworker_hostsmodel_path=worker_hosts()ps_hosts()Function to retrieve information about Paperspace hosts.Usage example:fromgradient_utilsimportps_hostsmodel_path=ps_hosts()task_index()Function to retrieve information about task index.Usage example:fromgradient_utilsimporttask_indexmodel_path=task_index()job_name()Function to retrieve information about job name.Usage example:fromgradient_utilsimportjob_namemodel_path=job_name()ContributingSetupWe use Docker and Docker-compose to run the tests locally.# To setup the integration test framework
docker-compose up --remove-orphans -d pushgateway
docker-compose build utils

# To run tests
docker-compose -f docker-compose.ci.yml run utils poetry run pytest

# To autoformat
docker-compose run utils poetry run autopep8 --in-place --aggressive --aggressive --recursive .