| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | import os |
| |
|
| | os.environ.setdefault("VERL_FORCE_DEVICE", "cpu") |
| |
|
| | import numpy as np |
| | import pytest |
| | import torch |
| |
|
| | from verl.utils import as_torch_index, group_mean_std |
| |
|
| |
|
| | def test_as_torch_index_basic_integers(): |
| | g = as_torch_index([2, 2, 5, 7, 5, 2]) |
| | assert g.dtype == torch.long |
| | assert g.device.type == "cpu" |
| | |
| | assert g.tolist()[0] == g.tolist()[1] |
| | assert len(torch.unique(g)) == 3 |
| |
|
| |
|
| | def test_as_torch_index_near_integer_floats(): |
| | arr = np.array([1.0000001, 2.0, 1.0, 3.0000000001], dtype=np.float64) |
| | g = as_torch_index(arr) |
| | assert g.dtype == torch.long |
| | assert len(torch.unique(g)) == 3 |
| |
|
| |
|
| | def test_as_torch_index_factorization_mixed(): |
| | labels = ["a", "b", "a", "c", "0042", 42] |
| | g = as_torch_index(labels) |
| | |
| | assert g.tolist()[4] != g.tolist()[5] |
| | assert len(torch.unique(g)) == 5 |
| |
|
| |
|
| | def test_group_mean_std_simple(): |
| | |
| | scores = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32) |
| | gidx = as_torch_index([0, 1, 0]) |
| |
|
| | mean_g, std_g, cnt_g = group_mean_std(scores, gidx) |
| | |
| | |
| | |
| | assert torch.allclose(mean_g, torch.tensor([2.0, 0.0])) |
| | assert torch.allclose(cnt_g, torch.tensor([2.0, 1.0])) |
| | |
| | assert mean_g[1].item() == 0.0 |
| | assert std_g[1].item() == 1.0 |
| | assert pytest.approx(std_g[0].item(), rel=1e-6) == (2.0**0.5) |
| |
|
| |
|
| | def test_group_mean_std_empty(): |
| | scores = torch.tensor([], dtype=torch.float32) |
| | gidx = torch.tensor([], dtype=torch.long) |
| | mean_g, std_g, cnt_g = group_mean_std(scores, gidx) |
| | assert mean_g.numel() == 0 and std_g.numel() == 0 and cnt_g.numel() == 0 |
| |
|
| |
|
| | def test_group_mean_std_default_device_no_force_env(monkeypatch): |
| | """ |
| | Regression test: |
| | - group_mean_std(device=None) must not pass a device *module* (e.g., torch.cuda) |
| | into Tensor.to(device=...), which crashes with: |
| | TypeError: to() received an invalid combination of arguments - got (..., device=module, ...) |
| | """ |
| | |
| | monkeypatch.delenv("VERL_FORCE_DEVICE", raising=False) |
| | monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False) |
| |
|
| | |
| | import verl.utils.device as device_mod |
| |
|
| | monkeypatch.setattr(device_mod, "is_cuda_available", False) |
| | monkeypatch.setattr(device_mod, "is_npu_available", False) |
| |
|
| | scores = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32) |
| | gidx = torch.tensor([0, 1, 0], dtype=torch.long) |
| |
|
| | mean_g, std_g, cnt_g = group_mean_std(scores, gidx) |
| | assert mean_g.device.type == "cpu" |
| | assert std_g.device.type == "cpu" |
| | assert cnt_g.device.type == "cpu" |
| |
|