File size: 2,118 Bytes
9ff93c7
 
970c0d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ff93c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import ast
from pathlib import Path
from unittest.mock import patch

import pytest

from src.app_helpers.quant_defaults import default_quant_method


@pytest.mark.parametrize('size, expected', [
    (7, 'Tuning'), (100, 'Tuning'), (100.01, 'RTN'), (235, 'RTN'), (None, 'Tuning'),
])
def test_parameter_threshold(size, expected):
    with patch('src.app_helpers.quant_defaults.get_model_size_b', return_value=size):
        assert default_quant_method('org/model')['value'].startswith(expected)


def test_empty_model_resets_without_hub_request():
    with patch('src.app_helpers.quant_defaults.get_model_size_b') as lookup:
        assert default_quant_method('')['value'].startswith('Tuning')
        lookup.assert_not_called()


def test_metadata_error_keeps_form_usable():
    with patch('src.app_helpers.quant_defaults.get_model_size_b', side_effect=ValueError):
        assert default_quant_method('org/model')['value'].startswith('Tuning')


def test_default_handler_does_not_shift_existing_gradio_events():
    source = (Path(__file__).resolve().parents[1] / 'app.py').read_text()
    tree = ast.parse(source)
    default_handlers = [
        node for node in ast.walk(tree)
        if isinstance(node, ast.Call)
        and isinstance(node.func, ast.Attribute)
        and any(
            keyword.arg == 'fn'
            and isinstance(keyword.value, ast.Name)
            and keyword.value.id == 'default_quant_method'
            for keyword in node.keywords
        )
    ]
    assert len(default_handlers) == 1
    handler = default_handlers[0]
    assert handler.func.attr == 'then'
    assert isinstance(handler.func.value, ast.Name)
    assert handler.func.value.id == '_quant_license_evt'
    timer_ticks = [
        node for node in ast.walk(tree)
        if isinstance(node, ast.Call)
        and isinstance(node.func, ast.Attribute)
        and node.func.attr == 'tick'
        and isinstance(node.func.value, ast.Name)
        and node.func.value.id in {'_queue_timer', '_pipeline_timer'}
    ]
    assert len(timer_ticks) == 2
    assert handler.lineno > max(tick.lineno for tick in timer_ticks)