File size: 5,703 Bytes
4f0238f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""

Pytest configuration and shared fixtures for TouchGrass tests.

"""

import pytest
import torch
from pathlib import Path


@pytest.fixture(scope="session")
def project_root():
    """Return the project root directory."""
    return Path(__file__).parent.parent


@pytest.fixture(scope="session")
def test_data_dir(project_root):
    """Return the test data directory."""
    data_dir = project_root / "tests" / "data"
    data_dir.mkdir(parents=True, exist_ok=True)
    return data_dir


@pytest.fixture
def sample_music_tokens():
    """Return a list of sample music tokens."""
    return [
        "[GUITAR]", "[PIANO]", "[DRUMS]", "[VOCALS]", "[THEORY]", "[PRODUCTION]",
        "[FRUSTRATED]", "[CONFUSED]", "[EXCITED]", "[CONFIDENT]",
        "[EASY]", "[MEDIUM]", "[HARD]",
        "[TAB]", "[CHORD]", "[SCALE]", "[INTERVAL]", "[PROGRESSION]",
        "[SIMPLIFY]", "[ENCOURAGE]"
    ]


@pytest.fixture
def sample_qa_pair():
    """Return a sample QA pair for testing."""
    return {
        "category": "guitar",
        "messages": [
            {"role": "system", "content": "You are a guitar assistant."},
            {"role": "user", "content": "How do I play a G major chord?"},
            {"role": "assistant", "content": "Place your middle finger on the 3rd fret of the 6th string, index on 2nd fret of 5th string, and ring/pinky on 3rd fret of the 1st and 2nd strings."}
        ]
    }


@pytest.fixture
def mock_tokenizer():
    """Create a mock tokenizer for testing."""
    class MockTokenizer:
        def __init__(self):
            self.vocab_size = 32000
            self.pad_token_id = 0

        def encode(self, text, **kwargs):
            # Simple mock encoding
            return [1, 2, 3, 4, 5]

        def decode(self, token_ids, **kwargs):
            return "mocked decoded text"

        def add_special_tokens(self, tokens_dict):
            self.vocab_size += len(tokens_dict.get("additional_special_tokens", []))

        def add_tokens(self, tokens):
            if isinstance(tokens, list):
                self.vocab_size += len(tokens)
            else:
                self.vocab_size += 1

        def convert_tokens_to_ids(self, token):
            return 32000 if token.startswith("[") else 1

    return MockTokenizer()


@pytest.fixture
def device():
    """Return the device to use for tests."""
    return "cuda" if torch.cuda.is_available() else "cpu"


@pytest.fixture
def d_model():
    """Return the model dimension for tests."""
    return 768


@pytest.fixture
def batch_size():
    """Return the batch size for tests."""
    return 4


@pytest.fixture
def seq_len():
    """Return the sequence length for tests."""
    return 10


@pytest.fixture
def music_theory_module(device, d_model):
    """Create a MusicTheoryModule instance for testing."""
    from TouchGrass.models.music_theory_module import MusicTheoryModule
    module = MusicTheoryModule(d_model=d_model).to(device)
    module.eval()
    return module


@pytest.fixture
def tab_chord_module(device, d_model):
    """Create a TabChordModule instance for testing."""
    from TouchGrass.models.tab_chord_module import TabChordModule
    module = TabChordModule(d_model=d_model).to(device)
    module.eval()
    return module


@pytest.fixture
def ear_training_module(device, d_model):
    """Create an EarTrainingModule instance for testing."""
    from TouchGrass.models.ear_training_module import EarTrainingModule
    module = EarTrainingModule(d_model=d_model).to(device)
    module.eval()
    return module


@pytest.fixture
def eq_adapter_module(device, d_model):
    """Create a MusicEQAdapter instance for testing."""
    from TouchGrass.models.eq_adapter import MusicEQAdapter
    module = MusicEQAdapter(d_model=d_model).to(device)
    module.eval()
    return module


@pytest.fixture
def songwriting_module(device, d_model):
    """Create a SongwritingModule instance for testing."""
    from TouchGrass.models.songwriting_module import SongwritingModule
    module = SongwritingModule(d_model=d_model).to(device)
    module.eval()
    return module


@pytest.fixture
def music_qa_generator():
    """Create a MusicQAGenerator instance for testing."""
    from TouchGrass.data.music_qa_generator import MusicQAGenerator
    generator = MusicQAGenerator()
    return generator


@pytest.fixture
def chat_formatter():
    """Create a ChatFormatter instance for testing."""
    from TouchGrass.data.chat_formatter import ChatFormatter
    formatter = ChatFormatter()
    return formatter


@pytest.fixture
def touchgrass_loss():
    """Create a TouchGrassLoss instance for testing."""
    from TouchGrass.training.losses import TouchGrassLoss
    loss_fn = TouchGrassLoss(lm_loss_weight=1.0, eq_loss_weight=0.1, music_module_loss_weight=0.05)
    return loss_fn


def pytest_configure(config):
    """Configure pytest with custom markers."""
    config.addinivalue_line(
        "markers", "slow: marks tests as slow (deselect with '-m \"not slow\"')"
    )
    config.addinivalue_line(
        "markers", "integration: marks tests as integration tests"
    )
    config.addinivalue_line(
        "markers", "gpu: marks tests that require GPU"
    )


def pytest_collection_modifyitems(config, items):
    """Modify test collection to add markers based on file names."""
    for item in items:
        if "test_inference" in item.nodeid:
            item.add_marker(pytest.mark.integration)
        if "test_trainer" in item.nodeid:
            item.add_marker(pytest.mark.slow)