File size: 11,838 Bytes
454e47c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
Common utilities for the distiller package.

This module provides shared functionality used across multiple components
including model discovery, result management, and initialization helpers.
"""

import json
import logging
from pathlib import Path
from types import TracebackType
from typing import Any

from .beam_utils import (
	BeamCheckpointManager,
	BeamEvaluationManager,
	BeamModelManager,
	BeamVolumeManager,
	create_beam_utilities,
)
from .config import VolumeConfig, get_safe_model_name, get_volume_config, setup_logging

logger = logging.getLogger(__name__)

# =============================================================================
# BEAM UTILITIES MANAGEMENT
# =============================================================================


class BeamContext:
	"""Context manager for Beam utilities with consistent initialization."""

	def __init__(self, workflow: str, volume_config: VolumeConfig | None = None) -> None:
		"""
		Initialize Beam context.

		Args:
		    workflow: Workflow type (distill, evaluate, benchmark, etc.)
		    volume_config: Optional custom volume config, otherwise inferred from workflow
		"""
		self.workflow = workflow
		self.volume_config = volume_config or get_volume_config()
		self.volume_manager: BeamVolumeManager | None = None
		self.checkpoint_manager: BeamCheckpointManager | None = None
		self.model_manager: BeamModelManager | None = None
		self.evaluation_manager: BeamEvaluationManager | None = None

	def __enter__(self) -> tuple[BeamVolumeManager, BeamCheckpointManager, BeamModelManager, BeamEvaluationManager]:
		"""Enter context and initialize utilities."""
		logger.info(f"πŸš€ Initializing Beam utilities for {self.workflow}")
		logger.info(f"πŸ“ Volume: {self.volume_config.name} at {self.volume_config.mount_path}")

		self.volume_manager, self.checkpoint_manager, self.model_manager, self.evaluation_manager = (
			create_beam_utilities(self.volume_config.name, self.volume_config.mount_path)
		)

		return self.volume_manager, self.checkpoint_manager, self.model_manager, self.evaluation_manager

	def __exit__(
		self,
		exc_type: type[BaseException] | None,
		exc_val: BaseException | None,
		exc_tb: TracebackType | None,
	) -> None:
		"""Exit context with cleanup if needed."""
		if exc_type:
			logger.error(f"❌ Error in Beam context for {self.workflow}: {exc_val}")
		else:
			logger.info(f"βœ… Beam context for {self.workflow} completed successfully")


def get_beam_utilities() -> tuple[BeamVolumeManager, BeamCheckpointManager, BeamModelManager, BeamEvaluationManager]:
	"""
	Get Beam utilities for a specific workflow.

	Returns:
	    Tuple of (volume_manager, checkpoint_manager, model_manager, evaluation_manager)
	"""
	volume_config = get_volume_config()
	return create_beam_utilities(volume_config.name, volume_config.mount_path)


# =============================================================================
# MODEL DISCOVERY
# =============================================================================


def discover_simplified_models(base_path: str | Path = ".") -> list[str]:
	"""
	Discover simplified distillation models in the specified directory.

	Args:
	    base_path: Base path to search for models

	Returns:
	    List of model paths sorted alphabetically
	"""
	base = Path(base_path)

	# Look for models in common locations
	search_patterns = [
		"code_model2vec/final/**/",
		"final/**/",
		"code_model2vec_*/",
		"*/config.json",
		"*.safetensors",
	]

	discovered_models = []

	for pattern in search_patterns:
		matches = list(base.glob(pattern))
		for match in matches:
			if match.is_dir():
				# Check if it's a valid model directory
				if (match / "config.json").exists() or (match / "model.safetensors").exists():
					discovered_models.append(str(match))
			elif match.name == "config.json":
				# Add parent directory if config.json found
				discovered_models.append(str(match.parent))

	# Remove duplicates and sort
	unique_models = sorted(set(discovered_models))

	logger.info(f"πŸ” Discovered {len(unique_models)} models in {base_path}")
	for model in unique_models:
		logger.info(f"   πŸ“ {model}")

	return unique_models


def validate_model_path(model_path: str | Path, volume_manager: BeamVolumeManager | None = None) -> str | None:
	"""
	Validate and resolve model path, checking local filesystem and Beam volumes.

	Args:
	    model_path: Path to model (can be local path or HuggingFace model name)
	    volume_manager: Optional volume manager for Beam volume checks

	Returns:
	    Resolved model path or None if not found
	"""
	path = Path(model_path)

	# Check if it's a HuggingFace model name
	if "/" in str(model_path) and not path.exists() and not str(model_path).startswith("/"):
		logger.info(f"πŸ“₯ Treating as HuggingFace model: {model_path}")
		return str(model_path)

	# Check local filesystem
	if path.exists():
		logger.info(f"βœ… Found local model: {model_path}")
		return str(path)

	# Check Beam volume if available
	if volume_manager:
		volume_path = Path(volume_manager.mount_path) / path.name
		if volume_path.exists():
			logger.info(f"βœ… Found model in Beam volume: {volume_path}")
			return str(volume_path)

		# Check volume root
		root_path = Path(volume_manager.mount_path)
		if (root_path / "config.json").exists():
			logger.info(f"βœ… Found model in Beam volume root: {root_path}")
			return str(root_path)

	logger.warning(f"⚠️ Model not found: {model_path}")
	return None


# =============================================================================
# RESULT MANAGEMENT
# =============================================================================


def save_results_with_backup(
	results: dict[str, Any],
	primary_path: str | Path,
	model_name: str,
	result_type: str = "evaluation",
	volume_manager: BeamVolumeManager | None = None,
	evaluation_manager: BeamEvaluationManager | None = None,
) -> bool:
	"""
	Save results with multiple backup strategies.

	Args:
	    results: Results dictionary to save
	    primary_path: Primary save location
	    model_name: Model name for filename generation
	    result_type: Type of results (evaluation, benchmark, etc.)
	    volume_manager: Optional volume manager for Beam storage
	    evaluation_manager: Optional evaluation manager for specialized storage

	Returns:
	    True if saved successfully to at least one location
	"""
	success_count = 0
	safe_name = get_safe_model_name(model_name)

	# Save to primary location
	try:
		primary = Path(primary_path)
		primary.mkdir(parents=True, exist_ok=True)
		filename = f"{result_type}_{safe_name}.json"
		filepath = primary / filename

		with filepath.open("w") as f:
			json.dump(results, f, indent=2, default=str)

		logger.info(f"πŸ’Ύ Saved {result_type} results to: {filepath}")
		success_count += 1
	except Exception as e:
		logger.warning(f"⚠️ Failed to save to primary location: {e}")

	# Save to Beam volume if available
	if volume_manager:
		try:
			volume_path = Path(volume_manager.mount_path) / f"{result_type}_results"
			volume_path.mkdir(parents=True, exist_ok=True)
			filename = f"{result_type}_{safe_name}.json"
			filepath = volume_path / filename

			with filepath.open("w") as f:
				json.dump(results, f, indent=2, default=str)

			logger.info(f"πŸ’Ύ Saved {result_type} results to Beam volume: {filepath}")
			success_count += 1
		except Exception as e:
			logger.warning(f"⚠️ Failed to save to Beam volume: {e}")

	# Save via evaluation manager if available and appropriate
	if evaluation_manager and result_type == "evaluation":
		try:
			success = evaluation_manager.save_evaluation_results(model_name, results)
			if success:
				logger.info(f"πŸ’Ύ Saved via evaluation manager for {model_name}")
				success_count += 1
		except Exception as e:
			logger.warning(f"⚠️ Failed to save via evaluation manager: {e}")

	return success_count > 0


def load_existing_results(
	model_name: str,
	result_type: str = "evaluation",
	search_paths: list[str | Path] | None = None,
	volume_manager: BeamVolumeManager | None = None,
	evaluation_manager: BeamEvaluationManager | None = None,
) -> dict[str, Any] | None:
	"""
	Load existing results from multiple possible locations.

	Args:
	    model_name: Model name to search for
	    result_type: Type of results to load
	    search_paths: Additional paths to search
	    volume_manager: Optional volume manager
	    evaluation_manager: Optional evaluation manager

	Returns:
	    Results dictionary if found, None otherwise
	"""
	safe_name = get_safe_model_name(model_name)
	filename = f"{result_type}_{safe_name}.json"

	# Search in provided paths
	if search_paths:
		for search_path in search_paths:
			filepath = Path(search_path) / filename
			if filepath.exists():
				try:
					with filepath.open("r") as f:
						results = json.load(f)
					logger.info(f"πŸ“‚ Loaded existing {result_type} results from: {filepath}")
					return results
				except Exception as e:
					logger.warning(f"⚠️ Failed to load from {filepath}: {e}")

	# Search in Beam volume
	if volume_manager:
		volume_path = Path(volume_manager.mount_path) / f"{result_type}_results" / filename
		if volume_path.exists():
			try:
				with volume_path.open("r") as f:
					results = json.load(f)
				logger.info(f"πŸ“‚ Loaded existing {result_type} results from Beam volume: {volume_path}")
				return results
			except Exception as e:
				logger.warning(f"⚠️ Failed to load from Beam volume: {e}")

	# Try evaluation manager
	if evaluation_manager and result_type == "evaluation":
		try:
			results = evaluation_manager.load_evaluation_results(model_name)
			if results:
				logger.info(f"πŸ“‚ Loaded existing {result_type} results via evaluation manager")
				return results
		except Exception as e:
			logger.warning(f"⚠️ Failed to load via evaluation manager: {e}")

	logger.info(f"ℹ️ No existing {result_type} results found for {model_name}")
	return None


# =============================================================================
# WORKFLOW HELPERS
# =============================================================================


def print_workflow_summary(
	workflow_name: str,
	total_items: int,
	processed_items: int,
	skipped_items: int,
	execution_time: float | None = None,
) -> None:
	"""Print a standardized workflow summary."""
	logger.info(f"\nβœ… {workflow_name} complete!")
	logger.info(f"πŸ“Š Total items: {total_items}")
	logger.info(f"✨ Newly processed: {processed_items}")
	logger.info(f"⏭️  Skipped (already done): {skipped_items}")

	if execution_time:
		logger.info(f"⏱️  Execution time: {execution_time:.2f} seconds")


def check_existing_results(
	items: list[str],
	result_type: str,
	search_paths: list[str | Path] | None = None,
	volume_manager: BeamVolumeManager | None = None,
) -> tuple[list[str], list[str]]:
	"""
	Check which items already have results and which need processing.

	Args:
	    items: List of items (model names, etc.) to check
	    result_type: Type of results to check for
	    search_paths: Paths to search for existing results
	    volume_manager: Optional volume manager

	Returns:
	    Tuple of (items_to_process, items_to_skip)
	"""
	to_process = []
	to_skip = []

	for item in items:
		existing = load_existing_results(item, result_type, search_paths, volume_manager)
		if existing:
			to_skip.append(item)
		else:
			to_process.append(item)

	return to_process, to_skip


# =============================================================================
# INITIALIZATION
# =============================================================================


def initialize_distiller_logging(level: int = logging.INFO) -> None:
	"""Initialize logging for distiller package."""
	setup_logging(level)
	logger.info("πŸš€ Distiller package initialized")


# Ensure logging is set up when module is imported
initialize_distiller_logging()