Skip to content

Commit a6653f8

Browse files
使用Cpp重写核心,统一实验代码风味,更新构建流
1 parent 39df9d4 commit a6653f8

33 files changed

Lines changed: 2055 additions & 1455 deletions

.github/workflows/pypi.yml

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,40 @@ on:
1111
- published
1212

1313
jobs:
14-
build_wheels:
15-
name: Build wheels on ${{ matrix.os }}
16-
runs-on: ${{ matrix.os }}
17-
strategy:
18-
matrix:
19-
os: [ubuntu-latest, ubuntu-24.04-arm, windows-latest, windows-11-arm, macos-15-intel, macos-latest]
20-
14+
build:
15+
name: Build sdist and wheel
16+
runs-on: ubuntu-latest
2117
steps:
2218
- uses: actions/checkout@v5
2319

24-
- name: Build wheels
25-
uses: pypa/cibuildwheel@v3.2.1
26-
env:
27-
CIBW_PLATFORM: ${{ matrix.platform || 'auto' }}
28-
# Can also be configured directly, using `with:`
29-
# with:
30-
# package-dir: .
31-
# output-dir: wheelhouse
32-
# config-file: "{package}/pyproject.toml"
33-
34-
- uses: actions/upload-artifact@v4
20+
- name: Set up Python
21+
uses: actions/setup-python@v5
3522
with:
36-
name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
37-
path: ./wheelhouse/*.whl
23+
python-version: "3.10"
3824

39-
build_sdist:
40-
name: Build source distribution
41-
runs-on: ubuntu-latest
42-
steps:
43-
- uses: actions/checkout@v5
25+
- name: Install build
26+
run: python -m pip install --upgrade build
4427

45-
- name: Build sdist
46-
run: pipx run build --sdist
28+
- name: Build sdist and wheel
29+
run: python -m build
4730

4831
- uses: actions/upload-artifact@v4
4932
with:
50-
name: cibw-sdist
51-
path: dist/*.tar.gz
33+
name: python-package-distributions
34+
path: dist/
5235

5336
upload_pypi:
54-
needs: [build_wheels, build_sdist]
37+
needs: [build]
5538
runs-on: ubuntu-latest
5639
environment: pypi
5740
permissions:
5841
id-token: write
5942
if: github.event_name == 'release' && github.event.action == 'published'
60-
# or, alternatively, upload to PyPI on every tag starting with 'v' (remove on: release above to use this)
61-
# if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
6243
steps:
6344
- uses: actions/download-artifact@v4
6445
with:
65-
# unpacks all CIBW artifacts into dist/
66-
pattern: cibw-*
67-
path: dist
68-
merge-multiple: true
46+
name: python-package-distributions
47+
path: dist/
6948

7049
- uses: pypa/gh-action-pypi-publish@release/v1
7150
# with:

README.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ from torch.utils.data import DataLoader
4646
generate_precomputed_file(
4747
dataset_size=10000,
4848
total_iterations=100000,
49-
persist_path="precomputed/my_experiment.pkl",
49+
persist_path="precomputed/my_experiment.safetensors",
5050
random_seed=0,
5151
replacement=True,
5252
maxsize=3000
5353
)
5454

5555
# Step 2: Create cache decorator at runtime
5656
decorator = OPTCacheDecorator(
57-
precomputed_path="precomputed/my_experiment.pkl",
57+
precomputed_path="precomputed/my_experiment.safetensors",
5858
maxsize=3000, # Must be consistent with maxsize during precomputation
5959
total_iter=100000
6060
)
@@ -76,13 +76,13 @@ for batch in dataloader:
7676
python -m src.OPT4TorchDataSet.cli \
7777
--dataset-size 10000 \
7878
--total-iter 100000 \
79-
--output precomputed/my_experiment.pkl \
79+
--output precomputed/my_experiment.safetensors \
8080
--seed 0
8181
```
8282

8383
- `--dataset-size`: **Required**. Dataset size
8484
- `--total-iter`: **Required**. Total number of accesses for precomputation
85-
- `--output`: **Required**. File path to save precomputed results (.pkl format)
85+
- `--output`: **Required**. File path to save precomputed results (.safetensors format)
8686
- `--seed`: Random seed to ensure reproducible results (optional)
8787
- `--no-replacement`: Disable replacement sampling (optional)
8888

@@ -96,7 +96,7 @@ If you have not installed the package but are using the source code directly, yo
9696
# In the project root directory
9797
python -m src.OPT4TorchDataSet.cli \
9898
--total-iter 100000 \
99-
--output ./precomputed/imagenet_opt.pkl \
99+
--output ./precomputed/imagenet_opt.safetensors \
100100
--seed 42
101101
```
102102

@@ -121,21 +121,14 @@ uv pip install -r requirements.txt
121121
uv pip install -U "triton-windows" # Optional for Windows
122122
```
123123

124-
**Login to SwanLab** (for experiment tracking)
125-
```bash
126-
swanlab login
127-
```
128-
https://docs.swanlab.cn/guide_cloud/general/quick-start.html
129-
130124
**Select GPU Device**
131125
```bash
132-
export CUDA_VISIBLE_DEVICES=2
126+
export CUDA_VISIBLE_DEVICES=0
133127
```
134128

135129
**Set Hugging Face Mirror** (improve download speed)
136130
```bash
137-
export HF_ENDPOINT=https://hf-mirror.com
138-
$env:HF_ENDPOINT = "https://hf-mirror.com" # Or on Windows
131+
$env:HF_ENDPOINT = "https://hf-mirror.com" # on Windows
139132
```
140133

141134
### Build Python wheel package
@@ -146,8 +139,7 @@ uv python -m build
146139
```
147140

148141
## Experiment
149-
150-
https://swanlab.cn/@Sail2Dream/opt4/overview
142+
All experiment results are saved as JSON files in the `results/` subdirectory of each experiment.
151143

152144
| Model | FIFO Time(s) | LFU Time(s) | LRU Time(s) | OPT Time(s) | RR Time(s) | none Time(s) | warmUp Time(s) |
153145
| ---------------------------- | ------------ | ----------- | ----------- | ----------- | ---------- | ------------ | -------------- |

README_zh.md

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ from torch.utils.data import DataLoader
4646
generate_precomputed_file(
4747
dataset_size=10000,
4848
total_iterations=100000,
49-
persist_path="precomputed/my_experiment.pkl",
49+
persist_path="precomputed/my_experiment.safetensors",
5050
random_seed=0,
5151
replacement=True,
5252
maxsize=3000
5353
)
5454

5555
# Step 2: 运行时创建缓存装饰器
5656
decorator = OPTCacheDecorator(
57-
precomputed_path="precomputed/my_experiment.pkl",
57+
precomputed_path="precomputed/my_experiment.safetensors",
5858
maxsize=3000, # 必须与预计算时的maxsize一致
5959
total_iter=100000
6060
)
@@ -76,13 +76,13 @@ for batch in dataloader:
7676
python -m src.OPT4TorchDataSet.cli \
7777
--dataset-size 10000 \
7878
--total-iter 100000 \
79-
--output precomputed/my_experiment.pkl \
79+
--output precomputed/my_experiment.safetensors \
8080
--seed 0
8181
```
8282

8383
- `--dataset-size`: **必需**。数据集大小
8484
- `--total-iter`: **必需**。预计算的总访问次数
85-
- `--output`: **必需**。保存预计算结果的文件路径(.pkl格式
85+
- `--output`: **必需**。保存预计算结果的文件路径(.safetensors格式
8686
- `--seed`: 随机种子,确保结果可重现(可选)
8787
- `--no-replacement`: 禁用有放回采样(可选)
8888

@@ -96,7 +96,7 @@ python -m src.OPT4TorchDataSet.cli \
9696
# 在项目根目录下
9797
python -m src.OPT4TorchDataSet.cli \
9898
--total-iter 100000 \
99-
--output ./precomputed/imagenet_opt.pkl \
99+
--output ./precomputed/imagenet_opt.safetensors \
100100
--seed 42
101101
```
102102

@@ -118,24 +118,17 @@ uv venv --python 3.14
118118
.venv\Scripts\activate.ps1
119119
uv pip install torch torchvision --index-url https://download.pytorch.org/whl/cu128
120120
uv pip install -r requirements.txt
121-
uv pip install -U "triton-windows" # Optional for Windows
121+
uv pip install -U "triton-windows" # Windows 系统可选
122122
```
123123

124-
**登录 SwanLab**(用于实验追踪)
125-
```bash
126-
swanlab login
127-
```
128-
https://docs.swanlab.cn/guide_cloud/general/quick-start.html
129-
130124
**选择 GPU 设备**
131125
```bash
132-
export CUDA_VISIBLE_DEVICES=2
126+
export CUDA_VISIBLE_DEVICES=0
133127
```
134128

135129
**设置 Hugging Face 镜像**(提升下载速度)
136130
```bash
137-
export HF_ENDPOINT=https://hf-mirror.com
138-
$env:HF_ENDPOINT = "https://hf-mirror.com" # 或在 Windows 上
131+
$env:HF_ENDPOINT = "https://hf-mirror.com" # 在 Windows 上
139132
```
140133

141134
### 构建 Python wheel 包
@@ -145,9 +138,8 @@ uv pip install build
145138
uv python -m build
146139
```
147140

148-
## Experiment
149-
150-
https://swanlab.cn/@Sail2Dream/opt4/overview
141+
## 实验结果
142+
所有实验结果均以 JSON 文件形式保存在各个实验目录下的 `results/` 子目录中。
151143

152144
| Model | FIFO Time(s) | LFU Time(s) | LRU Time(s) | OPT Time(s) | RR Time(s) | none Time(s) | warmUp Time(s) |
153145
| ---------------------------- | ------------ | ----------- | ----------- | ----------- | ---------- | ------------ | -------------- |

lab/hit_rate/README.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
# 缓存命中率测试
1+
# 缓存命中率实验 (Cache Hit Rate Experiment)
22

33
## 简介
4+
该实验旨在评估和比较不同缓存替换策略(LRU, LFU, FIFO, RR 和 OPT)在模拟数据集访问场景下的性能表现。通过测量各种缓存大小配置下的命中率和未命中次数,量化 OPT 算法在减少 I/O 压力方面的理论上限。
45

5-
该实验旨在评估和比较不同缓存替换策略在模拟数据集访问场景下的性能表现。通过测量各种缓存大小配置下的命中率和未命中次数,我们可以评估OPT(最优页面替换算法)相对于传统缓存策略(如LRU、LFU、FIFO、RR)的优势。
6-
7-
## 结论
8-
OPT在所有缓存占比下都高于LRU、LFU、FIFO、RR。
9-
在接近100%下差距逐渐减小。
10-
20~30%缓存占比下OPT获得最大差距。
11-
12-
## 数据集
13-
- 使用模拟数据集 HitRateDataset
14-
- 数据集大小: 10,000 项
15-
- 访问模式: 随机访问(带放回采样)
16-
17-
## 参数配置
18-
- 训练轮数: 10轮
19-
- 批处理大小: 32
20-
- 缓存大小比例: 10% 到 90% (相对于数据集大小)
21-
- 预测窗口: 总迭代次数的20%
22-
23-
## 运行实验
6+
## CLI 使用教程
7+
你可以通过命令行灵活配置实验参数。使用以下命令运行实验:
248

259
```bash
26-
cd lab/hit_rate
27-
python experiment.py
10+
python experiment.py --dataset-size 10000 --epochs 10 --batch-size 32
2811
```
2912

30-
## 输出结果
13+
**参数说明:**
14+
- `--dataset-size`: 模拟数据集的大小(默认值:`10000`)。
15+
- `--epochs`: 模拟训练的轮数,用于产生重复的访问序列(默认值:`10`)。
16+
- `--batch-size`: 数据加载的批处理大小(默认值:`32`)。
17+
- `--help`: 查看完整的帮助信息。
18+
19+
## 实验结果格式
20+
实验结果将保存为 `results/results.json`
21+
22+
**JSON 结构:**
23+
```json
24+
[
25+
{
26+
"name": "OPT",
27+
"cache_size": 1000,
28+
"hit_rate": 0.45,
29+
"miss_count": 5500,
30+
"total_accesses": 10000
31+
},
32+
...
33+
]
34+
```
3135

32-
实验结果将保存在 `results/results.csv` 文件中
36+
*注:`name` 代表策略名称,`cache_size` 为实际缓存条目数,`hit_rate` 为 0-1 之间的命中率。*

0 commit comments

Comments
 (0)