Commit 625c3f4
PR2: Load Tensor Compression Support (#157)
Summary:
## Overview
This PR adds gzip compression support to the `load_tensor` function while maintaining backward compatibility with existing uncompressed tensor files.
## Key Changes
### Modified Files
1. `tritonparse/tools/load_tensor.py`
2. `tritonparse/reproducer/templates/example.py`
### Features
**Compression Format Support**
- Added support for `.bin.gz` format (gzip compressed tensors)
- Maintains backward compatibility with existing `.bin` format
- Auto-detects compression based on file extension
**Hash Verification Updates**
- Hash is computed on **decompressed data** for compressed files
- Filename format:
- Compressed: `{hash}.bin.gz`
- Uncompressed: `{hash}.bin` (backward compatible)
**Loading Process Improvements**
- Read file contents first
- Decompress if needed
- Load tensor from memory buffer using `io.BytesIO`
- Enhanced error handling with clear messages for decompression failures
## Technical Details
### New Dependencies
- `gzip`: For decompression
- `io`: For memory buffer operations
### Implementation Logic
```python
# 1. Detect file format
is_compressed = str(blob_path).endswith('.bin.gz')
# 2. Read and decompress if needed
with open(blob_path, "rb") as f:
file_contents = f.read()
if is_compressed:
file_contents = gzip.decompress(file_contents)
# 3. Verify hash (based on decompressed data)
computed_hash = hashlib.blake2b(file_contents).hexdigest()
# 4. Load from memory
buffer = io.BytesIO(file_contents)
tensor = torch.load(buffer, map_location=device)
```
## Benefits
- **Storage Optimization**: Compression significantly reduces tensor file sizes
- **Backward Compatible**: Existing `.bin` files continue to work without changes
- **Data Integrity**: Hash verification ensures data correctness
- **Transparent**: Users don't need to worry about compression, API remains unchanged
## Impact
- ✅ Fully backward compatible, no breaking changes
- ✅ Applies to all scenarios using `load_tensor`
- ✅ Reproducer templates automatically gain compression support
Pull Request resolved: #157
Reviewed By: wychi
Differential Revision: D84068071
Pulled By: FindHao
fbshipit-source-id: 32fe673401e553b6d5d2c26ae19e8ac7229f0b7a1 parent 5f28d03 commit 625c3f4
File tree
2 files changed
+57
-25
lines changed- tritonparse
- reproducer/templates
- tools
2 files changed
+57
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| 15 | + | |
13 | 16 | | |
14 | 17 | | |
15 | 18 | | |
| |||
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
45 | | - | |
| 48 | + | |
46 | 49 | | |
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
50 | | - | |
51 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
52 | 56 | | |
53 | 57 | | |
54 | 58 | | |
| |||
65 | 69 | | |
66 | 70 | | |
67 | 71 | | |
68 | | - | |
69 | | - | |
| 72 | + | |
| 73 | + | |
70 | 74 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
75 | 92 | | |
76 | 93 | | |
77 | 94 | | |
| |||
80 | 97 | | |
81 | 98 | | |
82 | 99 | | |
83 | | - | |
84 | | - | |
85 | | - | |
| 100 | + | |
| 101 | + | |
86 | 102 | | |
87 | 103 | | |
88 | | - | |
| 104 | + | |
89 | 105 | | |
90 | 106 | | |
91 | 107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
| 11 | + | |
10 | 12 | | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
14 | 17 | | |
15 | | - | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
20 | | - | |
21 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
22 | 26 | | |
23 | 27 | | |
24 | 28 | | |
| |||
35 | 39 | | |
36 | 40 | | |
37 | 41 | | |
38 | | - | |
39 | | - | |
| 42 | + | |
| 43 | + | |
40 | 44 | | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
45 | 62 | | |
46 | 63 | | |
47 | 64 | | |
| |||
50 | 67 | | |
51 | 68 | | |
52 | 69 | | |
53 | | - | |
54 | | - | |
55 | | - | |
| 70 | + | |
| 71 | + | |
56 | 72 | | |
57 | 73 | | |
58 | 74 | | |
0 commit comments