|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +import time |
| 10 | +import unittest |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +import torch |
| 14 | + |
| 15 | +from fbgemm_gpu.kvzch_util import get_kv_zch_eviction_mask, parse_metadata_tensor |
| 16 | +from torchrec.modules.embedding_configs import ( |
| 17 | + CountBasedEvictionPolicy, |
| 18 | + CountTimestampMixedEvictionPolicy, |
| 19 | + NoEvictionPolicy, |
| 20 | + TimestampBasedEvictionPolicy, |
| 21 | + VirtualTableEvictionPolicy, |
| 22 | +) |
| 23 | + |
| 24 | +from ..common import gpu_unavailable |
| 25 | + |
| 26 | + |
| 27 | +@unittest.skipIf(*gpu_unavailable) |
| 28 | +class KvzchUtilsTest(unittest.TestCase): |
| 29 | + def test_basic_parsing(self) -> None: |
| 30 | + """ |
| 31 | + Test typical parsing including used=0 and used=1 cases. |
| 32 | + """ |
| 33 | + # Compose metadata values as 64-bit integers: |
| 34 | + # [timestamp=7, count=13, used=0] |
| 35 | + v1 = 7 | (13 << 32) # used=0 (highest bit not set) |
| 36 | + # [timestamp=42, count=99, used=1] |
| 37 | + # Used=1 is highest bit; encode as a negative int64 in Python to avoid overflow |
| 38 | + v2 = (42 | (99 << 32)) - (1 << 63) # set highest bit |
| 39 | + # [timestamp=0xABCDEF01, count=0x1ABCDE0, used=1] |
| 40 | + v3 = (0xABCDEF01 | (0x1ABCDE0 << 32)) - (1 << 63) |
| 41 | + vals = [v1, v2, v3] |
| 42 | + tensor = torch.tensor(vals, dtype=torch.int64) |
| 43 | + |
| 44 | + timestamps, counts, used = parse_metadata_tensor(tensor) |
| 45 | + |
| 46 | + np.testing.assert_array_equal( |
| 47 | + timestamps.numpy(), np.array([7, 42, 0xABCDEF01], dtype=np.uint32) |
| 48 | + ) |
| 49 | + np.testing.assert_array_equal( |
| 50 | + counts.numpy(), np.array([13, 99, 0x1ABCDE0], dtype=np.uint32) |
| 51 | + ) |
| 52 | + np.testing.assert_array_equal( |
| 53 | + used.numpy(), np.array([False, True, True], dtype=bool) |
| 54 | + ) |
| 55 | + |
| 56 | + def test_edge_cases(self) -> None: |
| 57 | + """ |
| 58 | + Test edge cases including all zeros, max values, min values, and different used flags. |
| 59 | + """ |
| 60 | + # All fields zero, used=0 |
| 61 | + v1 = 0 |
| 62 | + # Max timestamp, max count, used=0 |
| 63 | + v2 = 0xFFFFFFFF | (0x7FFFFFFF << 32) # Used=0 (highest bit = 0) |
| 64 | + # Min timestamp, min count, used=1 |
| 65 | + v3 = 0 - (1 << 63) # All fields 0, only highest bit set (used=1) |
| 66 | + |
| 67 | + vals = [v1, v2, v3] |
| 68 | + tensor = torch.tensor(vals, dtype=torch.int64) |
| 69 | + |
| 70 | + timestamps, counts, used = parse_metadata_tensor(tensor) |
| 71 | + |
| 72 | + np.testing.assert_array_equal( |
| 73 | + timestamps.numpy(), np.array([0, 0xFFFFFFFF, 0], dtype=np.uint32) |
| 74 | + ) |
| 75 | + np.testing.assert_array_equal( |
| 76 | + counts.numpy(), np.array([0, 0x7FFFFFFF, 0], dtype=np.uint32) |
| 77 | + ) |
| 78 | + np.testing.assert_array_equal( |
| 79 | + used.numpy(), np.array([False, False, True], dtype=bool) |
| 80 | + ) |
| 81 | + |
| 82 | + def test_invalid_dtype(self) -> None: |
| 83 | + """ |
| 84 | + Test that an assertion is raised for wrong dtype. |
| 85 | + """ |
| 86 | + tensor = torch.tensor([1, 2, 3], dtype=torch.float32) |
| 87 | + with self.assertRaises(AssertionError): |
| 88 | + parse_metadata_tensor(tensor) |
| 89 | + |
| 90 | + |
| 91 | +class GetKvZchEvictionMaskTest(unittest.TestCase): |
| 92 | + def setUp(self) -> None: |
| 93 | + # Prepare some metadata values with timestamp, count, used |
| 94 | + # Use negative numbers to represent highest bit set (used=1) |
| 95 | + self.vals = [ |
| 96 | + (100 | (5 << 32)), # used=0 |
| 97 | + (int(time.time()) - 60 | (10 << 32)) |
| 98 | + - (1 << 63), # used=1, timestamp 1 min ago |
| 99 | + (int(time.time()) - 3600 | (15 << 32)) |
| 100 | + - (1 << 63), # used=1, timestamp 1 hour ago |
| 101 | + ] |
| 102 | + self.metadata_tensor = torch.tensor(self.vals, dtype=torch.int64) |
| 103 | + |
| 104 | + def test_count_based_eviction(self) -> None: |
| 105 | + policy = CountBasedEvictionPolicy(inference_eviction_threshold=10) |
| 106 | + mask = get_kv_zch_eviction_mask(self.metadata_tensor, policy) |
| 107 | + # counts are 5,10,15; threshold=10; keep counts >= 10 |
| 108 | + expected = torch.tensor([False, True, True], dtype=torch.bool) |
| 109 | + self.assertTrue(torch.equal(mask, expected)) |
| 110 | + |
| 111 | + def test_timestamp_based_eviction(self) -> None: |
| 112 | + policy = TimestampBasedEvictionPolicy(inference_eviction_ttl_mins=30) |
| 113 | + mask = get_kv_zch_eviction_mask(self.metadata_tensor, policy) |
| 114 | + # timestamps: 100 (old), now-60s, now-3600s |
| 115 | + # TTL=30min=1800s, keep timestamps within 1800s |
| 116 | + expected = torch.tensor([False, True, False], dtype=torch.bool) |
| 117 | + self.assertTrue(torch.equal(mask, expected)) |
| 118 | + |
| 119 | + def test_count_timestamp_mixed_eviction(self) -> None: |
| 120 | + policy = CountTimestampMixedEvictionPolicy( |
| 121 | + inference_eviction_threshold=10, inference_eviction_ttl_mins=30 |
| 122 | + ) |
| 123 | + mask = get_kv_zch_eviction_mask(self.metadata_tensor, policy) |
| 124 | + # count mask: counts >= 10 -> [False, True, True] |
| 125 | + # timestamp mask: within 1800s -> [False, True, False] |
| 126 | + # combined mask = count_mask & timestamp_mask |
| 127 | + expected = torch.tensor([False, True, False], dtype=torch.bool) |
| 128 | + self.assertTrue(torch.equal(mask, expected)) |
| 129 | + |
| 130 | + def test_no_eviction_policy(self) -> None: |
| 131 | + policy = NoEvictionPolicy() |
| 132 | + mask = get_kv_zch_eviction_mask(self.metadata_tensor, policy) |
| 133 | + # No eviction, mask all True |
| 134 | + expected = torch.ones_like(self.metadata_tensor, dtype=torch.bool) |
| 135 | + self.assertTrue(torch.equal(mask, expected)) |
| 136 | + |
| 137 | + def test_unsupported_policy(self) -> None: |
| 138 | + class DummyPolicy(VirtualTableEvictionPolicy): |
| 139 | + pass |
| 140 | + |
| 141 | + policy = DummyPolicy() |
| 142 | + with self.assertRaises(ValueError): |
| 143 | + get_kv_zch_eviction_mask(self.metadata_tensor, policy) |
0 commit comments