| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright 2020 NLP Odyssey Authors. All rights reserved. |
| 4 |
# Use of this source code is governed by a BSD-style |
| 5 |
# license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
import torch |
| 8 |
|
| 9 |
FLOAT_DTYPES = [ |
| 10 |
torch.float16, # or torch.half |
| 11 |
torch.float32, # or torch.float |
| 12 |
torch.float64, # or torch.double |
| 13 |
torch.bfloat16 |
| 14 |
] |
| 15 |
|
| 16 |
INT_DTYPES = [ |
| 17 |
torch.int8, |
| 18 |
torch.int16, # or torch.short |
| 19 |
torch.int32, # or torch.int |
| 20 |
torch.int64, # or torch.long |
| 21 |
] |
| 22 |
|
| 23 |
|
| 24 |
def main(): |
| 25 |
for proto in range(1, 6): |
| 26 |
for use_zip in [False, True]: |
| 27 |
for dtype in FLOAT_DTYPES: |
| 28 |
save([1.2, -3.4, 5.6, -7.8], dtype, proto, use_zip) |
| 29 |
|
| 30 |
for dtype in INT_DTYPES: |
| 31 |
save([1, -2, 3, -4], dtype, proto, use_zip) |
| 32 |
|
| 33 |
save([1, 10, 100, 255], torch.uint8, proto, use_zip) |
| 34 |
save([True, False, True, False], torch.bool, proto, use_zip) |
| 35 |
|
| 36 |
|
| 37 |
def save(data, dtype, proto, use_zip): |
| 38 |
str_dtype = str(dtype)[6:] |
| 39 |
str_zip = '_zip' if use_zip else '' |
| 40 |
torch.save( |
| 41 |
torch.tensor(data, dtype=dtype), |
| 42 |
f'tensor_{str_dtype}_proto{proto}{str_zip}.pt', |
| 43 |
pickle_protocol=proto, |
| 44 |
_use_new_zipfile_serialization=use_zip) |
| 45 |
|
| 46 |
|
| 47 |
if __name__ == '__main__': |
| 48 |
main() |