amlc-work / code /scripts /bootstrap_box.sh
Arushhh's picture
code snapshot
0dd5490 verified
Raw History Blame Contribute Delete
3.25 kB
#!/usr/bin/env bash
# Bootstrap an AWS box for AMLC 2026 preprocessing/blocking. Parametrised so it runs on either box.
# PY=/opt/pytorch/bin/python ROOT=/opt/dlami/nvme/amlc THREADS=8 MEM=20GB bash bootstrap_box.sh
set -uo pipefail
PY=${PY:-/opt/pp/bin/python}; ROOT=${ROOT:-/data/amlc}; THREADS=${THREADS:-30}; MEM=${MEM:-80GB}
mkdir -p "$ROOT" && cd "$ROOT"
echo "== $(date) python=$PY root=$ROOT threads=$THREADS mem=$MEM =="
$PY -m pip install -q --upgrade pip
$PY -m pip install -q duckdb rapidfuzz lightgbm huggingface_hub polars pyarrow tqdm scikit-learn scipy
$PY -m pip install -q sparse-dot-topn || echo "sparse-dot-topn install failed (fallback path will be used)"
$PY - <<'PYEOF'
import importlib
for m in ["numpy","pandas","polars","pyarrow","duckdb","sklearn","scipy","rapidfuzz","lightgbm","sparse_dot_topn","huggingface_hub"]:
try: print(f"{m:18s}", importlib.import_module(m).__version__, flush=True)
except Exception as e: print(f"{m:18s} MISSING ({type(e).__name__})", flush=True)
PYEOF
echo "== dataset from HF (public repo) =="
if [ ! -f "$ROOT/raw/dataset/train/train_source1.tsv" ]; then
$PY -c "from huggingface_hub import snapshot_download; p=snapshot_download('Anupam-Rawat-IITB/data_', repo_type='dataset', local_dir='$ROOT/raw', allow_patterns=['dataset/*','utils/*']); print('downloaded to', p)"
fi
ls -la "$ROOT/raw/dataset/train" "$ROOT/raw/dataset/test"
echo "== TSV -> parquet + DuckDB =="
ROOT="$ROOT" THREADS="$THREADS" MEM="$MEM" $PY - <<'PYEOF'
import duckdb, os, time
R=os.environ['ROOT']; t=time.time(); os.makedirs(f'{R}/parquet', exist_ok=True); os.makedirs(f'{R}/duck_tmp', exist_ok=True)
con=duckdb.connect(f'{R}/amlc.duckdb'); con.execute(f"SET threads={os.environ['THREADS']}; SET memory_limit='{os.environ['MEM']}'; SET temp_directory='{R}/duck_tmp';")
OPT="delim='\\t', header=true, quote='', escape='', all_varchar=true"
for split in ['train','test']:
for i in (1,2,3):
name=f"t{i}" if split=='test' else f"s{i}"
p=f"{R}/raw/dataset/{split}/{split}_source{i}.tsv"
con.execute(f"CREATE OR REPLACE TABLE {name} AS SELECT row_number() OVER () AS rn, entity_id, coalesce(business_name,'') business_name, coalesce(business_address,'') business_address, coalesce(country,'') country FROM read_csv('{p}', {OPT})")
con.execute(f"COPY {name} TO '{R}/parquet/{name}.parquet' (FORMAT PARQUET)")
print(name, con.execute(f"SELECT count(*) FROM {name}").fetchone()[0], f"{time.time()-t:.0f}s", flush=True)
con.execute(f"CREATE OR REPLACE TABLE gt AS SELECT source1_entity_id, coalesce(matched_entity_ids,'') matched_entity_ids FROM read_csv('{R}/raw/dataset/train/train_ground_truth.tsv', {OPT})")
con.execute("CREATE OR REPLACE TABLE pairs AS SELECT source1_entity_id AS a, unnest(string_split(matched_entity_ids,',')) AS b FROM gt WHERE matched_entity_ids<>''")
con.execute(f"COPY gt TO '{R}/parquet/gt.parquet' (FORMAT PARQUET)"); con.execute(f"COPY pairs TO '{R}/parquet/pairs.parquet' (FORMAT PARQUET)")
print("gt", con.execute("SELECT count(*) FROM gt").fetchone()[0], "pairs", con.execute("SELECT count(*) FROM pairs").fetchone()[0], f"{time.time()-t:.0f}s", flush=True)
PYEOF
echo "== done $(date) =="; df -h "$ROOT" | tail -1; free -g | head -2