Skip to main content

Azure Storage Connector for PyTorch (azstoragetorch) (Preview)

The Azure Storage Connector for PyTorch (azstoragetorch) is a library that provides seamless, performance-optimized integrations between Azure Storage and PyTorch. Use this library to easily access and store data in Azure Storage while using PyTorch. The library currently offers:

Documentation

For detailed documentation on azstoragetorch, we recommend visiting its official documentation. It includes both a user guide and API references for the project. Content in this README is scoped to a high-level overview of the project and its GitHub repository policies.

Backwards compatibility

While the project is major version 0 (i.e., version is 0.x.y), public interfaces are not stable.

Backwards incompatible changes may be introduced between minor version bumps (e.g., upgrading from 0.1.0 to 0.2.0). If backwards compatibility is needed while using the library, we recommend pinning to a minor version of the library (e.g., azstoragetorch~=0.1.0).

Getting started

Prerequisites

Installation

Install the library with pip:

pip install azstoragetorch

Configuration

azstoragetorch should work without any explicit credential configuration.

azstoragetorch interfaces default to DefaultAzureCredential for credentials which automatically retrieves Microsoft Entra ID tokens based on your current environment. For more information on using credentials with azstoragetorch, see the user guide.

Features

This section highlights core features of azstoragetorch. For more details, see the user guide.

Saving and loading PyTorch models (Checkpointing)

PyTorch supports saving and loading trained models (i.e., checkpointing). The core PyTorch interfaces for saving and loading models are torch.save() and torch.load() respectively. Both of these functions accept a file-like object to be written to or read from.

azstoragetorch offers the azstoragetorch.io.BlobIO file-like object class to save and load models directly to and from Azure Blob Storage when using torch.save() and torch.load():

import torch
import torchvision.models  # Install separately: ``pip install torchvision``
from azstoragetorch.io import BlobIO

# Update URL with your own Azure Storage account and container name
CONTAINER_URL = "https://<my-storage-account-name>.blob.core.windows.net/<my-container-name>"

# Model to save. Replace with your own model.
model = torchvision.models.resnet18(weights="DEFAULT")

# Save trained model to Azure Blob Storage. This saves the model weights
# to a blob named "model_weights.pth" in the container specified by CONTAINER_URL.
with BlobIO(f"{CONTAINER_URL}/model_weights.pth", "wb") as f:
    torch.save(model.state_dict(), f)

# Load trained model from Azure Blob Storage.  This loads the model weights
# from the blob named "model_weights.pth" in the container specified by CONTAINER_URL.
with BlobIO(f"{CONTAINER_URL}/model_weights.pth", "rb") as f:
    model.load_state_dict(torch.load(f))

PyTorch Datasets

PyTorch offers the Dataset and DataLoader primitives for loading data samples. azstoragetorch provides implementations for both types of PyTorch datasets, map-style and iterable-style datasets, to load data samples from Azure Blob Storage:

Data samples returned from both datasets map directly one-to-one to blobs in Azure Blob Storage. When instantiating these dataset classes, use one of their class methods:

  • from_container_url() - Instantiate dataset by listing blobs from an Azure Storage container.
  • from_blob_urls() - Instantiate dataset from provided blob URLs
from azstoragetorch.datasets import BlobDataset, IterableBlobDataset

# Update URL with your own Azure Storage account and container name
CONTAINER_URL = "https://<my-storage-account-name>.blob.core.windows.net/<my-container-name>"

# Create an iterable-style dataset by listing blobs in the container specified by CONTAINER_URL.
dataset = IterableBlobDataset.from_container_url(CONTAINER_URL)

# Print the first blob in the dataset. Default output is a dictionary with
# the blob URL and the blob data. Use `transform` keyword argument when
# creating dataset to customize output format.
print(next(iter(dataset)))

# List of blob URLs to create dataset from. Update with your own blob names.
blob_urls = [
    f"{CONTAINER_URL}/<blob-name-1>",
    f"{CONTAINER_URL}/<blob-name-2>",
    f"{CONTAINER_URL}/<blob-name-3>",
]

# Create a map-style dataset from the list of blob URLs
blob_list_dataset = BlobDataset.from_blob_urls(blob_urls)

print(blob_list_dataset[0])  # Print the first blob in the dataset

Once instantiated, azstoragetorch datasets can be provided directly to a PyTorch DataLoader for loading samples:

from torch.utils.data import DataLoader

# Create a DataLoader to load data samples from the dataset in batches of 32
dataloader = DataLoader(dataset, batch_size=32)

for batch in dataloader:
    print(batch["url"])  # Prints blob URLs for each 32 sample batch

Additional resources

For more information on using the Azure Storage Connector for PyTorch, see the following resources:

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Metadata

Release files for azstoragetorch 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for azstoragetorch 0.2.0
File Size Uploaded Provenance
azstoragetorch-0.2.0.tar.gz 85.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for azstoragetorch 0.2.0
File Interpreter ABI Platform
azstoragetorch-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 107.4 kB

Release files / azstoragetorch-0.2.0.tar.gz

Download URL azstoragetorch-0.2.0.tar.gz
Size 85.1 kB
Tags Source
SHA-256 checksum
How to use checksums
fb8434b0a96f19586c480f66efd59b90d396f4d49d5cd8ce9e968c82b705a47f
BLAKE2b-256 checksum
How to use checksums
0606fdefd063723e50f84b92f094e76f1e1858720d1c11a065600cab02bd480b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via RestSharp/106.13.0.0

Release files / azstoragetorch-0.2.0-py3-none-any.whl

Download URL azstoragetorch-0.2.0-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d85c6084858044f783a2d1c528a155f29066175d08b90c4439be3c6738cb1f6
BLAKE2b-256 checksum
How to use checksums
dcb752092406a8c010680fba5cbdbf773ec4ab714bcd296492bf2aada5f0bc7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via RestSharp/106.13.0.0

Security for azstoragetorch 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page