| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | #!/bin/bash
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 17:18:20 +00:00
										 |  |  | # Runs an olmocr-bench run using the full pipeline (no fallback) | 
					
						
							|  |  |  | #  Without model parameter (default behavior):, uses the default image from hugging face | 
					
						
							|  |  |  | #   ./scripts/run_benchmark.sh | 
					
						
							|  |  |  | #  With model parameter: for testing custom models | 
					
						
							|  |  |  | #   ./scripts/run_benchmark.sh --model your-model-name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | set -e | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 17:18:20 +00:00
										 |  |  | # Parse command line arguments | 
					
						
							|  |  |  | MODEL="" | 
					
						
							|  |  |  | while [[ $# -gt 0 ]]; do | 
					
						
							|  |  |  |     case $1 in | 
					
						
							|  |  |  |         --model) | 
					
						
							|  |  |  |             MODEL="$2" | 
					
						
							|  |  |  |             shift 2 | 
					
						
							|  |  |  |             ;; | 
					
						
							|  |  |  |         *) | 
					
						
							|  |  |  |             echo "Unknown option: $1" | 
					
						
							|  |  |  |             echo "Usage: $0 [--model MODEL_NAME]" | 
					
						
							|  |  |  |             exit 1 | 
					
						
							|  |  |  |             ;; | 
					
						
							|  |  |  |     esac | 
					
						
							|  |  |  | done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 17:22:12 +00:00
										 |  |  | # Check for uncommitted changes | 
					
						
							|  |  |  | if ! git diff-index --quiet HEAD --; then | 
					
						
							|  |  |  |     echo "Error: There are uncommitted changes in the repository." | 
					
						
							|  |  |  |     echo "Please commit or stash your changes before running the benchmark." | 
					
						
							|  |  |  |     echo "" | 
					
						
							|  |  |  |     echo "Uncommitted changes:" | 
					
						
							|  |  |  |     git status --short | 
					
						
							|  |  |  |     exit 1 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-29 17:12:41 +00:00
										 |  |  | # Use conda environment Python if available, otherwise use system Python | 
					
						
							|  |  |  | if [ -n "$CONDA_PREFIX" ]; then | 
					
						
							|  |  |  |     PYTHON="$CONDA_PREFIX/bin/python" | 
					
						
							|  |  |  |     echo "Using conda Python from: $CONDA_PREFIX" | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |     PYTHON="python" | 
					
						
							|  |  |  |     echo "Warning: No conda environment detected, using system Python" | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | # Get version from version.py | 
					
						
							| 
									
										
										
										
											2025-05-29 17:12:41 +00:00
										 |  |  | VERSION=$($PYTHON -c 'import olmocr.version; print(olmocr.version.VERSION)') | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | echo "OlmOCR version: $VERSION" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Get first 10 characters of git hash | 
					
						
							|  |  |  | GIT_HASH=$(git rev-parse HEAD | cut -c1-10) | 
					
						
							|  |  |  | echo "Git hash: $GIT_HASH" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-29 18:43:27 +00:00
										 |  |  | # Get current git branch name | 
					
						
							|  |  |  | GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | 
					
						
							|  |  |  | echo "Git branch: $GIT_BRANCH" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | # Create full image tag | 
					
						
							|  |  |  | IMAGE_TAG="olmocr-benchmark-${VERSION}-${GIT_HASH}" | 
					
						
							|  |  |  | echo "Building Docker image with tag: $IMAGE_TAG" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Build the Docker image | 
					
						
							|  |  |  | echo "Building Docker image..." | 
					
						
							|  |  |  | docker build --platform linux/amd64 -f ./Dockerfile -t $IMAGE_TAG . | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-29 17:18:12 +00:00
										 |  |  | # Get Beaker username | 
					
						
							|  |  |  | BEAKER_USER=$(beaker account whoami --format json | jq -r '.[0].name') | 
					
						
							|  |  |  | echo "Beaker user: $BEAKER_USER" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | # Push image to beaker | 
					
						
							| 
									
										
										
										
											2025-06-05 17:21:06 +00:00
										 |  |  | echo "Trying to push image to Beaker..." | 
					
						
							|  |  |  | if ! beaker image create --workspace ai2/oe-data-pdf --name $IMAGE_TAG $IMAGE_TAG 2>/dev/null; then | 
					
						
							|  |  |  |     echo "Warning: Beaker image with tag $IMAGE_TAG already exists. Using existing image." | 
					
						
							|  |  |  | fi | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Create Python script to run beaker experiment | 
					
						
							|  |  |  | cat << 'EOF' > /tmp/run_benchmark_experiment.py | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2025-06-05 17:40:14 +00:00
										 |  |  | from beaker import Beaker, ExperimentSpec, TaskSpec, TaskContext, ResultSpec, TaskResources, ImageSource, Priority, Constraints, EnvVar | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 17:18:20 +00:00
										 |  |  | # Get image tag, beaker user, git branch, git hash, and optional model from command line | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | image_tag = sys.argv[1] | 
					
						
							| 
									
										
										
										
											2025-05-29 17:18:12 +00:00
										 |  |  | beaker_user = sys.argv[2] | 
					
						
							| 
									
										
										
										
											2025-05-29 18:43:27 +00:00
										 |  |  | git_branch = sys.argv[3] | 
					
						
							|  |  |  | git_hash = sys.argv[4] | 
					
						
							| 
									
										
										
										
											2025-06-05 17:18:20 +00:00
										 |  |  | model = sys.argv[5] if len(sys.argv) > 5 else None | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Initialize Beaker client | 
					
						
							| 
									
										
										
										
											2025-05-29 17:20:32 +00:00
										 |  |  | b = Beaker.from_env(default_workspace="ai2/olmocr") | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 17:18:20 +00:00
										 |  |  | # Build the pipeline command with optional model parameter | 
					
						
							|  |  |  | pipeline_cmd = "python -m olmocr.pipeline ./localworkspace --markdown --pdfs ./olmOCR-bench/bench_data/pdfs/**/*.pdf" | 
					
						
							|  |  |  | if model: | 
					
						
							|  |  |  |     pipeline_cmd += f" --model {model}" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 17:40:14 +00:00
										 |  |  | # Check if AWS credentials secret exists | 
					
						
							|  |  |  | aws_creds_secret = f"{beaker_user}-AWS_CREDENTIALS_FILE" | 
					
						
							|  |  |  | try: | 
					
						
							|  |  |  |     # Try to get the secret to see if it exists | 
					
						
							|  |  |  |     b.secret.get(aws_creds_secret, workspace="ai2/olmocr") | 
					
						
							|  |  |  |     has_aws_creds = True | 
					
						
							|  |  |  |     print(f"Found AWS credentials secret: {aws_creds_secret}") | 
					
						
							|  |  |  | except: | 
					
						
							|  |  |  |     has_aws_creds = False | 
					
						
							|  |  |  |     print(f"AWS credentials secret not found: {aws_creds_secret}") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-12 20:27:50 +00:00
										 |  |  | # First experiment: Original benchmark job | 
					
						
							| 
									
										
										
										
											2025-06-05 17:40:14 +00:00
										 |  |  | commands = [] | 
					
						
							|  |  |  | if has_aws_creds: | 
					
						
							|  |  |  |     commands.extend([ | 
					
						
							| 
									
										
										
										
											2025-06-06 17:45:17 +00:00
										 |  |  |         "mkdir -p ~/.aws", | 
					
						
							|  |  |  |         'echo "$AWS_CREDENTIALS_FILE" > ~/.aws/credentials' | 
					
						
							| 
									
										
										
										
											2025-06-05 17:40:14 +00:00
										 |  |  |     ]) | 
					
						
							|  |  |  | commands.extend([ | 
					
						
							|  |  |  |     "git clone https://huggingface.co/datasets/allenai/olmOCR-bench", | 
					
						
							|  |  |  |     "cd olmOCR-bench && git lfs pull && cd ..", | 
					
						
							|  |  |  |     pipeline_cmd, | 
					
						
							|  |  |  |     "python olmocr/bench/scripts/workspace_to_bench.py localworkspace/ olmOCR-bench/bench_data/olmocr --bench-path ./olmOCR-bench/", | 
					
						
							|  |  |  |     "python -m olmocr.bench.benchmark --dir ./olmOCR-bench/bench_data" | 
					
						
							|  |  |  | ]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Build task spec with optional env vars | 
					
						
							|  |  |  | task_spec_args = { | 
					
						
							|  |  |  |     "name": "olmocr-benchmark", | 
					
						
							|  |  |  |     "image": ImageSource(beaker=f"{beaker_user}/{image_tag}"), | 
					
						
							|  |  |  |     "command": [ | 
					
						
							|  |  |  |         "bash", "-c", | 
					
						
							|  |  |  |         " && ".join(commands) | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     "context": TaskContext( | 
					
						
							|  |  |  |         priority=Priority.normal, | 
					
						
							|  |  |  |         preemptible=True, | 
					
						
							|  |  |  |     ), | 
					
						
							|  |  |  |     "resources": TaskResources(gpu_count=1), | 
					
						
							| 
									
										
										
										
											2025-06-06 18:53:52 +00:00
										 |  |  |     "constraints": Constraints(cluster=["ai2/ceres-cirrascale", "ai2/jupiter-cirrascale-2"]), | 
					
						
							| 
									
										
										
										
											2025-06-05 17:40:14 +00:00
										 |  |  |     "result": ResultSpec(path="/noop-results"), | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Add env vars if AWS credentials exist | 
					
						
							|  |  |  | if has_aws_creds: | 
					
						
							|  |  |  |     task_spec_args["env_vars"] = [ | 
					
						
							|  |  |  |         EnvVar(name="AWS_CREDENTIALS_FILE", secret=aws_creds_secret) | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-12 20:27:50 +00:00
										 |  |  | # Create first experiment spec | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | experiment_spec = ExperimentSpec( | 
					
						
							| 
									
										
										
										
											2025-05-29 18:43:27 +00:00
										 |  |  |     description=f"OlmOCR Benchmark Run - Branch: {git_branch}, Commit: {git_hash}", | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  |     budget="ai2/oe-data", | 
					
						
							| 
									
										
										
										
											2025-06-05 17:40:14 +00:00
										 |  |  |     tasks=[TaskSpec(**task_spec_args)], | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-12 20:27:50 +00:00
										 |  |  | # Create the first experiment | 
					
						
							| 
									
										
										
										
											2025-05-29 17:20:32 +00:00
										 |  |  | experiment = b.experiment.create(spec=experiment_spec, workspace="ai2/olmocr") | 
					
						
							| 
									
										
										
										
											2025-06-12 20:27:50 +00:00
										 |  |  | print(f"Created benchmark experiment: {experiment.id}") | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | print(f"View at: https://beaker.org/ex/{experiment.id}") | 
					
						
							| 
									
										
										
										
											2025-06-12 20:47:58 +00:00
										 |  |  | print("-------") | 
					
						
							|  |  |  | print("") | 
					
						
							| 
									
										
										
										
											2025-06-12 20:27:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Second experiment: Performance test job | 
					
						
							| 
									
										
										
										
											2025-06-12 20:47:58 +00:00
										 |  |  | perf_pipeline_cmd = "python -m olmocr.pipeline ./localworkspace --markdown --pdfs s3://ai2-oe-data/jakep/olmocr/olmOCR-mix-0225/benchmark_set/*.pdf" | 
					
						
							| 
									
										
										
										
											2025-06-12 20:27:50 +00:00
										 |  |  | if model: | 
					
						
							|  |  |  |     perf_pipeline_cmd += f" --model {model}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | perf_commands = [] | 
					
						
							|  |  |  | if has_aws_creds: | 
					
						
							|  |  |  |     perf_commands.extend([ | 
					
						
							|  |  |  |         "mkdir -p ~/.aws", | 
					
						
							|  |  |  |         'echo "$AWS_CREDENTIALS_FILE" > ~/.aws/credentials' | 
					
						
							|  |  |  |     ]) | 
					
						
							|  |  |  | perf_commands.append(perf_pipeline_cmd) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Build performance task spec | 
					
						
							|  |  |  | perf_task_spec_args = { | 
					
						
							|  |  |  |     "name": "olmocr-performance", | 
					
						
							|  |  |  |     "image": ImageSource(beaker=f"{beaker_user}/{image_tag}"), | 
					
						
							|  |  |  |     "command": [ | 
					
						
							|  |  |  |         "bash", "-c", | 
					
						
							|  |  |  |         " && ".join(perf_commands) | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     "context": TaskContext( | 
					
						
							|  |  |  |         priority=Priority.normal, | 
					
						
							|  |  |  |         preemptible=True, | 
					
						
							|  |  |  |     ), | 
					
						
							|  |  |  |     "resources": TaskResources(gpu_count=1), | 
					
						
							|  |  |  |     "constraints": Constraints(cluster=["ai2/ceres-cirrascale", "ai2/jupiter-cirrascale-2"]), | 
					
						
							|  |  |  |     "result": ResultSpec(path="/noop-results"), | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Add env vars if AWS credentials exist | 
					
						
							|  |  |  | if has_aws_creds: | 
					
						
							|  |  |  |     perf_task_spec_args["env_vars"] = [ | 
					
						
							|  |  |  |         EnvVar(name="AWS_CREDENTIALS_FILE", secret=aws_creds_secret) | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Create performance experiment spec | 
					
						
							|  |  |  | perf_experiment_spec = ExperimentSpec( | 
					
						
							|  |  |  |     description=f"OlmOCR Performance Test - Branch: {git_branch}, Commit: {git_hash}", | 
					
						
							|  |  |  |     budget="ai2/oe-data", | 
					
						
							|  |  |  |     tasks=[TaskSpec(**perf_task_spec_args)], | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Create the performance experiment | 
					
						
							|  |  |  | perf_experiment = b.experiment.create(spec=perf_experiment_spec, workspace="ai2/olmocr") | 
					
						
							|  |  |  | print(f"Created performance experiment: {perf_experiment.id}") | 
					
						
							|  |  |  | print(f"View at: https://beaker.org/ex/{perf_experiment.id}") | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | EOF | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-12 20:27:50 +00:00
										 |  |  | # Run the Python script to create the experiments | 
					
						
							|  |  |  | echo "Creating Beaker experiments..." | 
					
						
							| 
									
										
										
										
											2025-06-05 17:18:20 +00:00
										 |  |  | if [ -n "$MODEL" ]; then | 
					
						
							|  |  |  |     echo "Using model: $MODEL" | 
					
						
							|  |  |  |     $PYTHON /tmp/run_benchmark_experiment.py $IMAGE_TAG $BEAKER_USER $GIT_BRANCH $GIT_HASH "$MODEL" | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |     $PYTHON /tmp/run_benchmark_experiment.py $IMAGE_TAG $BEAKER_USER $GIT_BRANCH $GIT_HASH | 
					
						
							|  |  |  | fi | 
					
						
							| 
									
										
										
										
											2025-05-29 17:08:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Clean up temporary file | 
					
						
							|  |  |  | rm /tmp/run_benchmark_experiment.py | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-12 20:27:50 +00:00
										 |  |  | echo "Benchmark experiments submitted successfully!" |