|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +# pyre-strict |
| 9 | + |
| 10 | +import io |
| 11 | +import random |
| 12 | +import string |
| 13 | +import tempfile |
| 14 | +import unittest |
| 15 | +from pathlib import Path |
| 16 | +from typing import BinaryIO, Optional, Union |
| 17 | + |
| 18 | +import fbgemm_gpu |
| 19 | +import torch |
| 20 | + |
| 21 | +# pyre-fixme[16]: Module `fbgemm_gpu` has no attribute `open_source`. |
| 22 | +open_source: bool = getattr(fbgemm_gpu, "open_source", False) |
| 23 | + |
| 24 | + |
| 25 | +class FileStoreTest(unittest.TestCase): |
| 26 | + def _test_filestore_readwrite( |
| 27 | + self, |
| 28 | + # pyre-fixme[2] |
| 29 | + store, # FileStore |
| 30 | + input: Union[BinaryIO, torch.Tensor, Path], |
| 31 | + path: Optional[str] = None, |
| 32 | + ) -> None: |
| 33 | + """ |
| 34 | + Generic FileStore routines to test reading and writing data |
| 35 | +
|
| 36 | + Args: |
| 37 | + store (FileStore): The FileStore to test |
| 38 | + input (torch.Tensor | BinaryIO | Path): The data to write to the FileStore |
| 39 | + path (str, optional): The path to write the data to. If not provided, a random path will be generated. |
| 40 | + """ |
| 41 | + if path is None: |
| 42 | + path = "".join(random.choices(string.ascii_letters, k=15)) |
| 43 | + |
| 44 | + assert not store.exists(path), f"{path} already exists" |
| 45 | + store.write(path, input) |
| 46 | + assert store.exists(path), f"{path} does not exist" |
| 47 | + |
| 48 | + if isinstance(input, torch.Tensor): |
| 49 | + assert torch.load(store.read(path)).equal(input), "tensors do not match" |
| 50 | + |
| 51 | + elif isinstance(input, io.BytesIO) or isinstance(input, BinaryIO): |
| 52 | + input.seek(0) |
| 53 | + assert store.read(path).read() == input.read(), "byte streams do not match" |
| 54 | + |
| 55 | + elif isinstance(input, Path): |
| 56 | + assert ( |
| 57 | + store.read(path).read() == input.read_bytes() |
| 58 | + ), "file contents do not match" |
| 59 | + |
| 60 | + store.remove(path) |
| 61 | + assert not store.exists(path), f"{path} is not removed" |
| 62 | + |
| 63 | + def test_filestore_oss_bad_bucket(self) -> None: |
| 64 | + """ |
| 65 | + Test that OSS FileStore raises ValueError when an invalid bucket is provided |
| 66 | + """ |
| 67 | + from fbgemm_gpu.utils import FileStore |
| 68 | + |
| 69 | + self.assertRaises( |
| 70 | + ValueError, FileStore, "".join(random.choices(string.ascii_letters, k=15)) |
| 71 | + ) |
| 72 | + |
| 73 | + def test_filestore_oss_binaryio(self) -> None: |
| 74 | + """ |
| 75 | + Test that OSS FileStore can read and write binary data |
| 76 | + """ |
| 77 | + from fbgemm_gpu.utils import FileStore |
| 78 | + |
| 79 | + self._test_filestore_readwrite( |
| 80 | + FileStore("/tmp"), |
| 81 | + io.BytesIO("".join(random.choices(string.ascii_letters, k=128)).encode()), |
| 82 | + ) |
| 83 | + |
| 84 | + def test_filestore_oss_tensor(self) -> None: |
| 85 | + """ |
| 86 | + Test that OSS FileStore can read and write tensors |
| 87 | + """ |
| 88 | + from fbgemm_gpu.utils import FileStore |
| 89 | + |
| 90 | + self._test_filestore_readwrite( |
| 91 | + FileStore("/tmp"), |
| 92 | + torch.rand((random.randint(100, 1000), random.randint(100, 1000))), |
| 93 | + ) |
| 94 | + |
| 95 | + def test_filestore_oss_file(self) -> None: |
| 96 | + """ |
| 97 | + Test that OSS FileStore can read and write files |
| 98 | + """ |
| 99 | + from fbgemm_gpu.utils import FileStore |
| 100 | + |
| 101 | + input = torch.rand((random.randint(100, 1000), random.randint(100, 1000))) |
| 102 | + infile = tempfile.NamedTemporaryFile() |
| 103 | + torch.save(input, infile) |
| 104 | + |
| 105 | + self._test_filestore_readwrite(FileStore("/tmp"), Path(infile.name)) |
| 106 | + |
| 107 | + @unittest.skipIf(open_source, "Test does not apply to OSS") |
| 108 | + def test_filestore_fb_bad_bucket(self) -> None: |
| 109 | + """ |
| 110 | + Test that FB FileStore raises ValueError when an invalid bucket is provided |
| 111 | + """ |
| 112 | + from fbgemm_gpu.fb.utils import FileStore |
| 113 | + |
| 114 | + self.assertRaises( |
| 115 | + ValueError, FileStore, "".join(random.choices(string.ascii_letters, k=15)) |
| 116 | + ) |
| 117 | + |
| 118 | + @unittest.skipIf(open_source, "Test does not apply to OSS") |
| 119 | + def test_filestore_fb_binaryio(self) -> None: |
| 120 | + """ |
| 121 | + Test that FB FileStore can read and write binary data |
| 122 | + """ |
| 123 | + from fbgemm_gpu.fb.utils import FileStore |
| 124 | + |
| 125 | + self._test_filestore_readwrite( |
| 126 | + FileStore("tlparse_reports"), |
| 127 | + io.BytesIO("".join(random.choices(string.ascii_letters, k=128)).encode()), |
| 128 | + f"tree/{''.join(random.choices(string.ascii_letters, k=15))}.unittest", |
| 129 | + ) |
| 130 | + |
| 131 | + @unittest.skipIf(open_source, "Test does not apply to OSS") |
| 132 | + def test_filestore_fb_tensor(self) -> None: |
| 133 | + """ |
| 134 | + Test that FB FileStore can read and write tensors |
| 135 | + """ |
| 136 | + from fbgemm_gpu.fb.utils import FileStore |
| 137 | + |
| 138 | + self._test_filestore_readwrite( |
| 139 | + FileStore("tlparse_reports"), |
| 140 | + torch.rand((random.randint(100, 1000), random.randint(100, 1000))), |
| 141 | + f"tree/{''.join(random.choices(string.ascii_letters, k=15))}.unittest", |
| 142 | + ) |
| 143 | + |
| 144 | + @unittest.skipIf(open_source, "Test does not apply to OSS") |
| 145 | + def test_filestore_fb_file(self) -> None: |
| 146 | + """ |
| 147 | + Test that FB FileStore can read and write files |
| 148 | + """ |
| 149 | + from fbgemm_gpu.fb.utils import FileStore |
| 150 | + |
| 151 | + input = torch.rand((random.randint(100, 1000), random.randint(100, 1000))) |
| 152 | + infile = tempfile.NamedTemporaryFile() |
| 153 | + torch.save(input, infile) |
| 154 | + |
| 155 | + self._test_filestore_readwrite( |
| 156 | + FileStore("tlparse_reports"), |
| 157 | + Path(infile.name), |
| 158 | + f"tree/{''.join(random.choices(string.ascii_letters, k=15))}.unittest", |
| 159 | + ) |
0 commit comments