Ejemplo n.º 1
0
def test_complex_shape(device):
    capacity = 10
    hashmap = o3c.Hashmap(capacity, o3c.int64, o3c.int64, [3], [1], device)
    keys = o3c.Tensor([[1, 2, 3], [2, 3, 4], [3, 4, 5]],
                      dtype=o3c.int64,
                      device=device)
    values = o3c.Tensor([1, 2, 3], dtype=o3c.int64, device=device)
    addrs, masks = hashmap.insert(keys, values)
    assert masks.to(o3c.int64).sum() == 3

    valid_indices = addrs[masks].to(o3c.int64)
    valid_keys = hashmap.get_key_tensor()[valid_indices, :]

    valid_values = hashmap.get_value_tensor()[valid_indices]

    np.testing.assert_equal(
        valid_keys.cpu().numpy().flatten(),
        np.array([[1, 2, 3], [2, 3, 4], [3, 4, 5]]).flatten())
    np.testing.assert_equal(valid_values.cpu().numpy().flatten(),
                            np.array([1, 2, 3]))

    keys = o3c.Tensor([[1, 2, 3], [4, 5, 6]], dtype=o3c.int64, device=device)
    masks = hashmap.erase(keys)

    np.testing.assert_equal(masks.cpu().numpy().flatten(),
                            np.array([True, False]))
Ejemplo n.º 2
0
def test_erase(device):
    capacity = 10
    hashmap = o3c.Hashmap(capacity, o3c.int64, o3c.int64, [1], [1], device)
    keys = o3c.Tensor([100, 300, 500, 700, 900],
                      dtype=o3c.int64,
                      device=device)
    values = o3c.Tensor([1, 3, 5, 7, 9], dtype=o3c.int64, device=device)
    hashmap.insert(keys, values)

    keys = o3c.Tensor([100, 200, 500], dtype=o3c.int64, device=device)
    masks = hashmap.erase(keys)

    np.testing.assert_equal(masks.cpu().numpy(), np.array([True, False, True]))

    assert hashmap.size() == 3
    active_addrs = hashmap.get_active_addrs()
    active_indices = active_addrs.to(o3c.int64)

    active_keys = hashmap.get_key_tensor()[active_indices]
    active_values = hashmap.get_value_tensor()[active_indices]

    active_keys_np = active_keys.cpu().numpy().flatten()
    active_values_np = active_values.cpu().numpy().flatten()
    sorted_i = np.argsort(active_keys_np)
    np.testing.assert_equal(active_keys_np[sorted_i], np.array([300, 700,
                                                                900]))
    np.testing.assert_equal(active_values_np[sorted_i], np.array([3, 7, 9]))
Ejemplo n.º 3
0
def test_activate(device):
    capacity = 10
    hashmap = o3c.Hashmap(capacity, o3c.int64, o3c.int64, [1], [1], device)
    keys = o3c.Tensor([100, 300, 500, 700, 900, 900],
                      dtype=o3c.int64,
                      device=device)
    addrs, masks = hashmap.activate(keys)
    assert masks.to(o3c.int64).sum() == 5

    valid_indices = addrs[masks].to(o3c.int64)
    valid_keys = hashmap.get_key_tensor()[valid_indices]
    np.testing.assert_equal(np.sort(valid_keys.cpu().numpy().flatten()),
                            np.array([100, 300, 500, 700, 900]))
Ejemplo n.º 4
0
def test_insertion(device):
    capacity = 10
    hashmap = o3c.Hashmap(capacity, o3c.int64, o3c.int64, [1], [1], device)
    keys = o3c.Tensor([100, 300, 500, 700, 900, 900],
                      dtype=o3c.int64,
                      device=device)
    values = o3c.Tensor([1, 3, 5, 7, 9, 9], dtype=o3c.int64, device=device)
    addrs, masks = hashmap.insert(keys, values)
    assert masks.to(o3c.int64).sum() == 5

    valid_indices = addrs[masks].to(o3c.int64)
    valid_keys = hashmap.get_key_tensor()[valid_indices]
    valid_values = hashmap.get_value_tensor()[valid_indices]
    np.testing.assert_equal(valid_keys.cpu().numpy(),
                            valid_values.cpu().numpy() * 100)
Ejemplo n.º 5
0
def test_find(device):
    capacity = 10
    hashmap = o3c.Hashmap(capacity, o3c.int64, o3c.int64, [1], [1], device)
    keys = o3c.Tensor([100, 300, 500, 700, 900],
                      dtype=o3c.int64,
                      device=device)
    values = o3c.Tensor([1, 3, 5, 7, 9], dtype=o3c.int64, device=device)
    hashmap.insert(keys, values)

    keys = o3c.Tensor([100, 200, 500], dtype=o3c.int64, device=device)
    addrs, masks = hashmap.find(keys)
    np.testing.assert_equal(masks.cpu().numpy(), np.array([True, False, True]))

    valid_indices = addrs[masks].to(o3c.int64)
    valid_keys = hashmap.get_key_tensor()[valid_indices]
    valid_values = hashmap.get_value_tensor()[valid_indices]
    assert valid_keys.shape[0] == 2

    np.testing.assert_equal(valid_keys.cpu().numpy().flatten(),
                            np.array([100, 500]))
    np.testing.assert_equal(valid_values.cpu().numpy().flatten(),
                            np.array([1, 5]))
Ejemplo n.º 6
0
def test_creation(device):
    hashmap = o3c.Hashmap(10, o3c.int64, o3c.int64, [1], [1], device)
    assert hashmap.size() == 0