Coverage for mlos_bench/mlos_bench/tests/tunables/tunable_distributions_test.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 00:44 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Unit tests for checking tunable parameters' distributions.""" 

6 

7import json5 as json 

8import pytest 

9 

10from mlos_bench.tunables.tunable import Tunable, TunableValueTypeName 

11 

12 

13def test_categorical_distribution() -> None: 

14 """Try to instantiate a categorical tunable with distribution specified.""" 

15 with pytest.raises(ValueError): 

16 Tunable( 

17 name="test", 

18 config={ 

19 "type": "categorical", 

20 "values": ["foo", "bar", "baz"], 

21 "distribution": {"type": "uniform"}, 

22 "default": "foo", 

23 }, 

24 ) 

25 

26 

27@pytest.mark.parametrize("tunable_type", ["int", "float"]) 

28def test_numerical_distribution_uniform(tunable_type: TunableValueTypeName) -> None: 

29 """Create a numeric Tunable with explicit uniform distribution.""" 

30 tunable = Tunable( 

31 name="test", 

32 config={ 

33 "type": tunable_type, 

34 "range": [0, 10], 

35 "distribution": {"type": "uniform"}, 

36 "default": 0, 

37 }, 

38 ) 

39 assert tunable.is_numerical 

40 assert tunable.distribution == "uniform" 

41 assert not tunable.distribution_params 

42 

43 

44@pytest.mark.parametrize("tunable_type", ["int", "float"]) 

45def test_numerical_distribution_normal(tunable_type: TunableValueTypeName) -> None: 

46 """Create a numeric Tunable with explicit Gaussian distribution specified.""" 

47 tunable = Tunable( 

48 name="test", 

49 config={ 

50 "type": tunable_type, 

51 "range": [0, 10], 

52 "distribution": {"type": "normal", "params": {"mu": 0, "sigma": 1.0}}, 

53 "default": 0, 

54 }, 

55 ) 

56 assert tunable.distribution == "normal" 

57 assert tunable.distribution_params == {"mu": 0, "sigma": 1.0} 

58 

59 

60@pytest.mark.parametrize("tunable_type", ["int", "float"]) 

61def test_numerical_distribution_beta(tunable_type: TunableValueTypeName) -> None: 

62 """Create a numeric Tunable with explicit Beta distribution specified.""" 

63 tunable = Tunable( 

64 name="test", 

65 config={ 

66 "type": tunable_type, 

67 "range": [0, 10], 

68 "distribution": {"type": "beta", "params": {"alpha": 2, "beta": 5}}, 

69 "default": 0, 

70 }, 

71 ) 

72 assert tunable.distribution == "beta" 

73 assert tunable.distribution_params == {"alpha": 2, "beta": 5} 

74 

75 

76@pytest.mark.parametrize("tunable_type", ["int", "float"]) 

77def test_numerical_distribution_unsupported(tunable_type: str) -> None: 

78 """Create a numeric Tunable with unsupported distribution.""" 

79 json_config = f""" 

80 { 

81 "type": "{tunable_type}", 

82 "range": [0, 10], 

83 "distribution": { 

84 "type": "poisson", 

85 "params": { 

86 "lambda": 1.0 

87 } 

88 } , 

89 "default": 0 

90 } 

91 """ 

92 config = json.loads(json_config) 

93 with pytest.raises(ValueError): 

94 Tunable(name="test", config=config)