bostorchconnector, a Python package with a precompiled shared library
Project description
bostorchconnector
专为PyTorch训练存储在Bos上的数据集而设计的高吞吐插件,使用bostorchconnector可以高效地访问云上数据集和读写checkpoint。
bostorchconnector是实现PyTorch的dataset primitives 接口。 同时支持两种dataset:
支持checkpoint接口,可以直读/写云上Bos,无需落盘。
开始
前置环境
- Linux
- Python 3.8 or greater is installed
- PyTorch >= 2.0
安装
pip install bostorchconnector
配置
配置访问凭证,以下方式配置一种即可,优先级有先后。
- 特定配置文件
~/.baidubce/credentials - 安装且配置过bcecmd,默认配置路径是
~/.go-bcecli/credentials - 设置环境变量:
BCE_ACCESS_KEY_ID和BCE_SECRET_ACCESS_KEY
其中credentials文件的格式是
[Defaults]
Ak=
Sk=
Sts=
Examples
示例
使用from_prefix方法构建BosIterableDataset:
from bostorchconnector import BosIterableDataset
# You need to update <BUCKET> and <PREFIX>
DATASET_URI="bos://<BUCKET>/<PREFIX>"
ENDPOINT="http://bj.bcebos.com"
iterable_dataset = BosIterableDataset.from_prefix(DATASET_URI, endpoint=ENDPOINT)
# Datasets are also iterators.
for item in iterable_dataset:
data = item.read()
print(len(data))
print(item.key)
使用from_prefix方法构建BosMapDataset:
from bostorchconnector import BosMapDataset
# You need to update <BUCKET> and <PREFIX>
DATASET_URI="bos://<BUCKET>/<PREFIX>"
ENDPOINT="http://bj.bcebos.com"
map_dataset = BosMapDataset.from_prefix(DATASET_URI, endpoint=ENDPOINT)
# Randomly access to an item in map_dataset.
item = map_dataset[0]
# Learn about bucket, key, and content of the object
bucket = item.bucket
key = item.key
content = item.read()
len(content)
直接读写model checkpoint:
from bostorchconnector import BosCheckpoint
import torchvision
import torch
CHECKPOINT_URI="bos://<BUCKET>/<KEY>/"
ENDPOINT="http://bj.bcebos.com"
checkpoint = BosCheckpoint(endpoint=ENDPOINT)
model = torchvision.models.resnet18()
# Save checkpoint to Bos
with checkpoint.writer(CHECKPOINT_URI + "epoch0.ckpt") as writer:
torch.save(model.state_dict(), writer)
# Load checkpoint from Bos
with checkpoint.reader(CHECKPOINT_URI + "epoch0.ckpt") as reader:
state_dict = torch.load(reader)
model.load_state_dict(state_dict)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bostorchconnector-1.0.1-py3-none-any.whl.
File metadata
- Download URL: bostorchconnector-1.0.1-py3-none-any.whl
- Upload date:
- Size: 571.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a0a3ce967ee349f65f90fbac97e3501fd1058944cd366416b983634671aa1ff
|
|
| MD5 |
17c799f0b10fd94c9757ca0b851f28d2
|
|
| BLAKE2b-256 |
394babd26f013409902e12f7ec103d896f5578c8ff9f28b24b201471a1c54733
|