-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkflow.yaml
More file actions
1419 lines (1266 loc) · 59.5 KB
/
workflow.yaml
File metadata and controls
1419 lines (1266 loc) · 59.5 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# yaml-language-server: $schema=https://activate.parallel.works/workflow.schema.json
---
# ==============================================================================
# ACTIVATE RAG + vLLM Deployment Workflow
# ==============================================================================
# Deploys vLLM inference server with optional RAG (Retrieval-Augmented Generation)
# capabilities on HPC clusters using Apptainer/Singularity containers.
#
# Features:
# - vLLM inference with tensor parallelism for multi-GPU deployments
# - Optional RAG stack with ChromaDB vector store and document indexing
# - HuggingFace model cloning via git-lfs or local model path support
# - OpenAI-compatible API endpoint for IDE integration (Cline, Continue, etc.)
# - Unified SLURM/PBS/SSH job submission via job_runner marketplace action
#
# Organized Input Sections:
# 1. Resource Selection - Compute cluster target
# 2. Scheduler Options - SLURM/PBS configuration for HPC execution
# 3. Container Runtime - Apptainer / Singularity or Docker
# 4. Model Configuration - HuggingFace or local model paths
# 5. vLLM Settings - GPU count, memory, context length, dtype
# 6. RAG Settings - Documents directory, system prompt
# 7. Container Options - Container source (lfs, path, pull, build)
# 8. Advanced Settings - Ports, API keys, repository branch
#
# Usage:
# Deploy vLLM-only for code assistance or full vLLM+RAG stack for document QA.
# Connects to VS Code/IDE via OpenAI-compatible session endpoint.
# ==============================================================================
permissions:
- '*'
sessions:
session:
redirect: false
openAI: true
# ==============================================================================
# JOB DEFINITIONS
# ==============================================================================
jobs:
# ============================================================================
# Setup: Clone Repository and Create Environment
# ============================================================================
setup:
ssh:
remoteHost: ${{ inputs.resource.ip }}
steps:
- name: Clone Repository
early-cancel: any-job-failed
run: |
set -x
RUNDIR="${{ inputs.advanced_settings.rundir }}"
RUNDIR="${RUNDIR/#\~/$HOME}"
mkdir -p "$(dirname $RUNDIR)"
if [[ -d "$RUNDIR/.git" ]]; then
echo "Repository exists, updating..."
cd "$RUNDIR"
git fetch origin
git checkout ${{ inputs.advanced_settings.repository_branch }}
git reset --hard origin/${{ inputs.advanced_settings.repository_branch }}
else
echo "Cloning fresh repository..."
git clone -b ${{ inputs.advanced_settings.repository_branch }} ${{ inputs.advanced_settings.repository }} "$RUNDIR"
fi
cd "$RUNDIR"
rm -f jobid SESSION_PORT job.started job.ended run.out HOSTNAME
rm -rf logs
# Create cache directories (needed for bind mounts even if not used)
mkdir -p cache/tiktoken_encodings
echo "rundir=$RUNDIR" >> $OUTPUTS
- name: Create Environment File
id: create_env
early-cancel: any-job-failed
run: |
set -x
cd ${{ needs.setup.outputs.rundir }}
# Core configuration
cat > .run.env << 'ENVEOF'
export RUNMODE=${{ inputs.runmode }}
export RUNTYPE=${{ inputs.runtype }}
export SYSTEM_PROMPT="${{ inputs.rag.systemprompt }}"
export HF_TOKEN=${{ inputs.model.hf_token }}
export API_KEY=${{ inputs.advanced_settings.apikey }}
export DOCS_DIR=${{ inputs.rag.docsdir }}
ENVEOF
if [[ "${{ inputs.rag.embedding_model_source }}" == "local" ]]; then
echo "export EMBEDDING_MODEL=${{ inputs.rag.embedding_model_path }}" >> .run.env
else
echo "export EMBEDDING_MODEL=${{ inputs.rag.embedding_model_id }}" >> .run.env
echo "export EMBEDDING_CACHE_DIR=${{ inputs.rag.embedding_model_cache_dir }}" >> .run.env
fi
# Build vLLM extra args from structured inputs
if [[ "${{ inputs.vllm.dtype }}" == "custom" ]]; then
VLLM_ARGS="--tensor-parallel-size ${{ inputs.vllm.num_gpus }} --gpu-memory-utilization ${{ inputs.vllm.gpu_memory }}"
else
VLLM_ARGS="--dtype ${{ inputs.vllm.dtype }} --tensor-parallel-size ${{ inputs.vllm.num_gpus }} --gpu-memory-utilization ${{ inputs.vllm.gpu_memory }}"
fi
[[ "${{ inputs.vllm.max_model_len }}" != "auto" ]] && VLLM_ARGS="$VLLM_ARGS --max-model-len ${{ inputs.vllm.max_model_len }}"
[[ -n "${{ inputs.vllm.extra_args }}" ]] && VLLM_ARGS="$VLLM_ARGS ${{ inputs.vllm.extra_args }}"
echo "export VLLM_EXTRA_ARGS=\"$VLLM_ARGS\"" >> .run.env
# Model configuration
echo "export MODEL_SOURCE=${{ inputs.model.source }}" >> .run.env
if [[ "${{ inputs.model.source }}" == "local" ]]; then
echo "export MODEL_NAME=${{ inputs.model.local_path }}" >> .run.env
echo "export MODEL_PATH=${{ inputs.model.local_path }}" >> .run.env
echo "export TRANSFORMERS_OFFLINE=1" >> .run.env
else
echo "export MODEL_NAME=${{ inputs.model.hf_model_id }}" >> .run.env
echo "export HF_MODEL_ID=${{ inputs.model.hf_model_id }}" >> .run.env
echo "export MODEL_CACHE_BASE=${{ inputs.model.cache_dir }}" >> .run.env
fi
# Container paths (used for both 'path' and 'build' modes)
if [[ "${{ inputs.container.source }}" != "pull" ]]; then
echo "export VLLM_CONTAINER_PATH=${{ inputs.container.vllm_path }}" >> .run.env
echo "export RAG_CONTAINER_PATH=${{ inputs.container.rag_path }}" >> .run.env
fi
# Additional settings
[[ -n "${{ inputs.advanced_settings.vllm_attention_backend }}" ]] && echo "export VLLM_ATTENTION_BACKEND=${{ inputs.advanced_settings.vllm_attention_backend }}" >> .run.env
if [[ "${{ inputs.advanced_settings.tiktoken_encodings }}" == "true" ]]; then
echo "export TIKTOKEN_ENCODINGS_BASE=/root/.cache/tiktoken_encodings" >> .run.env
echo "export TIKTOKEN_RS_CACHE_DIR=/root/.cache/tiktoken_encodings" >> .run.env
fi
echo "Environment file created"
# ============================================================================
# Prepare Containers (Apptainer/Singularity)
# ============================================================================
prepare_containers:
needs: [setup]
if: ${{ inputs.runmode == 'singularity' }}
ssh:
remoteHost: ${{ inputs.resource.ip }}
steps:
- name: Verify Singularity Available
id: verify_singularity
early-cancel: any-job-failed
run: |
set -x
command -v singularity >/dev/null 2>&1 || command -v apptainer >/dev/null 2>&1 || { echo "ERROR: singularity/apptainer not found"; exit 1; }
singularity --version || apptainer --version
echo "Singularity/Apptainer ready"
- name: Pull Containers from Bucket
id: pull_containers
if: ${{ inputs.container.source == 'pull' }}
early-cancel: any-job-failed
run: |
set -x
cd ${{ needs.setup.outputs.rundir }}
[[ ! -f "vllm.sif" ]] && pw bucket cp "${{ inputs.container.bucket }}/vllm.sif" ./ || echo "vllm.sif exists"
[[ "${{ inputs.runtype }}" == "all" && ! -f "rag.sif" ]] && pw bucket cp "${{ inputs.container.bucket }}/rag.sif" ./ || echo "rag.sif exists or not needed"
echo "Container pull complete"
- name: Pull Containers from LFS Repo
id: pull_containers_lfs
if: ${{ inputs.container.source == 'lfs' }}
early-cancel: any-job-failed
run: |
set -euo pipefail
cd ${{ needs.setup.outputs.rundir }}
command -v git >/dev/null 2>&1 || { echo "ERROR: git not found"; exit 1; }
git lfs version >/dev/null 2>&1 || { echo "ERROR: git-lfs not found"; exit 1; }
export GIT_LFS_SKIP_SMUDGE=1
LFS_REPO="${{ inputs.container.lfs_repo }}"
LFS_BRANCH="${{ inputs.container.lfs_branch }}"
LFS_DIR=".lfs_containers"
REV_FILE=".lfs_containers_rev"
VLLM_PATH="${{ inputs.container.vllm_path }}"
VLLM_PATH="${VLLM_PATH/#\~/$HOME}"
RAG_PATH="${{ inputs.container.rag_path }}"
RAG_PATH="${RAG_PATH/#\~/$HOME}"
need_vllm=true
need_rag=false
[[ -f "$VLLM_PATH" ]] && need_vllm=false
[[ "${{ inputs.runtype }}" == "all" ]] && { [[ ! -f "$RAG_PATH" ]] && need_rag=true; }
remote_rev=$(git ls-remote "$LFS_REPO" "$LFS_BRANCH" | awk '{print $1}')
if [[ -z "$remote_rev" ]]; then
echo "ERROR: Could not resolve $LFS_BRANCH in $LFS_REPO"
exit 1
fi
stored_rev=""
[[ -f "$REV_FILE" ]] && stored_rev=$(cat "$REV_FILE" || true)
if [[ "$need_vllm" == "false" && "$need_rag" == "false" ]]; then
if [[ -n "$remote_rev" && "$remote_rev" == "$stored_rev" ]]; then
echo "Containers already present and match remote revision $remote_rev; skipping LFS pull."
else
echo "Containers already present; remote revision is $remote_rev (stored: $stored_rev)."
echo "Skipping LFS pull to avoid re-download. Remove containers to force refresh."
fi
exit 0
fi
if [[ -d "$LFS_DIR/.git" ]]; then
git -C "$LFS_DIR" fetch origin "$LFS_BRANCH"
else
GIT_LFS_SKIP_SMUDGE=1 git clone --depth 1 --branch "$LFS_BRANCH" "$LFS_REPO" "$LFS_DIR"
fi
git -C "$LFS_DIR" checkout -f "$LFS_BRANCH"
git -C "$LFS_DIR" reset --hard "origin/$LFS_BRANCH"
git -C "$LFS_DIR" lfs install --local
git -C "$LFS_DIR" sparse-checkout init --no-cone
patterns=("vllm/vllm.*.sif" "vllm/vllm.sif.part_*" "vllm/vllm.sif" "vllm.*.sif" "vllm.sif.part_*" "vllm.sif" "scripts/sif_parts.sh")
if [[ "${{ inputs.runtype }}" == "all" ]]; then
patterns+=("rag/rag.*.sif" "rag/rag.sif.part_*" "rag/rag.sif" "rag.*.sif" "rag.sif.part_*" "rag.sif")
fi
git -C "$LFS_DIR" sparse-checkout set --no-cone "${patterns[@]}"
lfs_include="vllm/vllm.*.sif,vllm/vllm.sif.part_*,vllm/vllm.sif,vllm.*.sif,vllm.sif.part_*,vllm.sif"
if [[ "${{ inputs.runtype }}" == "all" ]]; then
lfs_include="${lfs_include},rag/rag.*.sif,rag/rag.sif.part_*,rag/rag.sif,rag.*.sif,rag.sif.part_*,rag.sif"
fi
git -C "$LFS_DIR" config lfs.concurrenttransfers 4
git -C "$LFS_DIR" config lfs.transfer.maxretries 10
git -C "$LFS_DIR" config lfs.transfer.maxretrydelay 30
git -C "$LFS_DIR" config lfs.fetchinclude "$lfs_include"
git -C "$LFS_DIR" config lfs.fetchexclude ""
attempt=1
max_attempts=2
while true; do
echo "LFS pull attempt ${attempt}/${max_attempts}..."
echo "Downloading container files (this may take a while for large files)..."
if git -C "$LFS_DIR" lfs fetch; then
break
fi
if [[ $attempt -ge $max_attempts ]]; then
echo "ERROR: LFS pull failed after ${max_attempts} attempts"
exit 1
fi
sleep $((attempt * 5))
((attempt++))
done
echo "Checking out LFS files..."
git -C "$LFS_DIR" lfs checkout
assemble_sif() {
local prefix=$1
local target=$2
local dir_prefix="$LFS_DIR/${prefix}"
local full_sub="$dir_prefix/${prefix}.sif"
local full_root="$LFS_DIR/${prefix}.sif"
mkdir -p "$(dirname "$target")"
if [[ -f "$full_sub" ]]; then
cp "$full_sub" "$target"
return 0
fi
if [[ -f "$full_root" ]]; then
cp "$full_root" "$target"
return 0
fi
local script="$LFS_DIR/scripts/sif_parts.sh"
shopt -s nullglob
local numeric_parts_sub=("$dir_prefix/${prefix}."[0-9][0-9][0-9][0-9][0-9].sif)
local numeric_parts_root=("$LFS_DIR/${prefix}."[0-9][0-9][0-9][0-9][0-9].sif)
shopt -u nullglob
if [[ -x "$script" ]]; then
if [[ ${#numeric_parts_sub[@]} -gt 0 ]]; then
bash "$script" join --prefix "$prefix" --in-dir "$dir_prefix" --output "$target"
return 0
fi
if [[ ${#numeric_parts_root[@]} -gt 0 ]]; then
bash "$script" join --prefix "$prefix" --in-dir "$LFS_DIR" --output "$target"
return 0
fi
fi
shopt -s nullglob
local parts=("$dir_prefix/${prefix}."[0-9][0-9][0-9][0-9][0-9].sif "$dir_prefix/${prefix}.sif.part_"*)
if ((${#parts[@]} == 0)); then
parts=("$LFS_DIR/${prefix}."[0-9][0-9][0-9][0-9][0-9].sif "$LFS_DIR/${prefix}.sif.part_"*)
fi
shopt -u nullglob
if ((${#parts[@]} == 0)); then
echo "ERROR: No parts found for ${prefix} in ${dir_prefix} or ${LFS_DIR}"
exit 1
fi
mapfile -t parts_sorted < <(printf "%s\n" "${parts[@]}" | LC_ALL=C sort)
cat "${parts_sorted[@]}" > "$target"
}
if [[ "$need_vllm" == "true" ]]; then
echo "Assembling vLLM container to $VLLM_PATH"
assemble_sif vllm "$VLLM_PATH"
fi
if [[ "$need_rag" == "true" ]]; then
echo "Assembling RAG container to $RAG_PATH"
assemble_sif rag "$RAG_PATH"
fi
ln -sf "$VLLM_PATH" ./vllm.sif
[[ "${{ inputs.runtype }}" == "all" ]] && ln -sf "$RAG_PATH" ./rag.sif
[[ -n "$remote_rev" ]] && echo "$remote_rev" > "$REV_FILE"
echo "LFS container pull complete"
- name: Link Existing Containers
id: link_containers
if: ${{ inputs.container.source == 'path' }}
early-cancel: any-job-failed
run: |
set -x
cd ${{ needs.setup.outputs.rundir }}
VLLM_PATH="${{ inputs.container.vllm_path }}"
VLLM_PATH="${VLLM_PATH/#\~/$HOME}"
[[ -f "$VLLM_PATH" ]] && ln -sf "$VLLM_PATH" ./vllm.sif || { echo "ERROR: vLLM container not found at $VLLM_PATH"; exit 1; }
echo "Linked vllm.sif from $VLLM_PATH"
if [[ "${{ inputs.runtype }}" == "all" ]]; then
RAG_PATH="${{ inputs.container.rag_path }}"
RAG_PATH="${RAG_PATH/#\~/$HOME}"
[[ -f "$RAG_PATH" ]] && ln -sf "$RAG_PATH" ./rag.sif || { echo "ERROR: RAG container not found at $RAG_PATH"; exit 1; }
echo "Linked rag.sif from $RAG_PATH"
fi
- name: Build Containers from Source
id: build_containers
if: ${{ inputs.container.source == 'build' }}
early-cancel: any-job-failed
run: |
set -x
cd ${{ needs.setup.outputs.rundir }}
# Resolve container paths (expand ~ to $HOME)
VLLM_PATH="${{ inputs.container.vllm_path }}"
VLLM_PATH="${VLLM_PATH/#\~/$HOME}"
RAG_PATH="${{ inputs.container.rag_path }}"
RAG_PATH="${RAG_PATH/#\~/$HOME}"
echo "Building Apptainer containers (requires sudo or fakeroot)..."
echo "vLLM container will be built to: $VLLM_PATH"
echo "RAG container will be built to: $RAG_PATH"
build_container() {
local target=$1 def=$2
[[ -f "$target" ]] && { echo "$target exists, skipping"; return 0; }
echo "Building $target from $def..."
mkdir -p "$(dirname "$target")"
if sudo -n true 2>/dev/null; then
sudo singularity build "$target" "$def"
else
singularity build --fakeroot "$target" "$def"
fi
}
build_container "$VLLM_PATH" singularity/Singularity.vllm
[[ "${{ inputs.runtype }}" == "all" ]] && build_container "$RAG_PATH" singularity/Singularity.rag
# Create symlinks in rundir for consistency with other modes
ln -sf "$VLLM_PATH" ./vllm.sif
[[ "${{ inputs.runtype }}" == "all" ]] && ln -sf "$RAG_PATH" ./rag.sif
echo "Container build complete"
# ============================================================================
# Prepare Model
# ============================================================================
prepare_model:
needs: [setup]
if: ${{ inputs.model.source == 'huggingface' }}
ssh:
remoteHost: ${{ inputs.resource.ip }}
steps:
- name: Clone HuggingFace Model
id: clone_model
early-cancel: any-job-failed
run: |
set -x
RUNDIR="${{ needs.setup.outputs.rundir }}"
cd "$RUNDIR"
MODEL_ID="${{ inputs.model.hf_model_id }}"
CACHE_DIR="${{ inputs.model.cache_dir }}"
CACHE_DIR="${CACHE_DIR/#\~/$HOME}"
HF_TOKEN="${{ inputs.model.hf_token }}"
mkdir -p "$CACHE_DIR"
MODEL_BASENAME="${MODEL_ID##*/}"
LEGACY_DIR="$CACHE_DIR/${MODEL_ID//\//__}"
TARGET_DIR="$CACHE_DIR/$MODEL_BASENAME"
if [[ -d "$LEGACY_DIR" && ! -d "$TARGET_DIR" ]]; then
echo "Renaming legacy cache dir $LEGACY_DIR to $TARGET_DIR"
mv "$LEGACY_DIR" "$TARGET_DIR"
fi
# Function to validate model weights are complete (not LFS pointers)
validate_weights() {
local dir=$1
[[ ! -f "$dir/config.json" ]] && return 1
# Check for safetensors or bin weight files
local weight_file=""
if [[ -f "$dir/model.safetensors" ]]; then
weight_file="$dir/model.safetensors"
elif ls "$dir"/model*.safetensors 1>/dev/null 2>&1; then
weight_file=$(ls "$dir"/model*.safetensors | head -1)
elif ls "$dir"/*.bin 1>/dev/null 2>&1; then
weight_file=$(ls "$dir"/*.bin | head -1)
else
echo "No weight files found"
return 1
fi
local size=$(stat -c%s "$weight_file" 2>/dev/null || stat -f%z "$weight_file" 2>/dev/null)
if [[ $size -lt 1000000 ]]; then
echo "Weight file $weight_file appears to be LFS pointer (size: $size bytes)"
return 1
fi
return 0
}
# Check if already cached AND weights are valid
if [[ -d "$TARGET_DIR" ]] && validate_weights "$TARGET_DIR"; then
echo "Model already cached and validated at $TARGET_DIR"
else
# Remove incomplete cache if exists
if [[ -d "$TARGET_DIR" ]]; then
echo "Cached model at $TARGET_DIR appears incomplete, removing..."
rm -rf "$TARGET_DIR"
fi
echo "Cloning model $MODEL_ID to $TARGET_DIR"
# Ensure git-lfs is available
if ! git lfs version >/dev/null 2>&1; then
echo "Installing git-lfs locally..."
mkdir -p $HOME/.local
cd /tmp
LFS_URL=$(curl -s https://api.github.com/repos/git-lfs/git-lfs/releases/latest | grep browser_download_url | grep linux-amd64 | head -1 | cut -d '"' -f 4)
[[ -z "$LFS_URL" ]] && { echo "ERROR: Could not find git-lfs download URL"; exit 1; }
wget -q "$LFS_URL" -O git-lfs-linux-amd64.tar.gz
tar -xzf git-lfs-linux-amd64.tar.gz
./git-lfs-*/install.sh --local
rm -rf git-lfs-*
export PATH="$HOME/.local/bin:$PATH"
cd ${{ needs.setup.outputs.rundir }}
git lfs version >/dev/null 2>&1 || { echo "ERROR: Failed to install git-lfs"; exit 1; }
fi
git lfs install
# Build clone URL
REPO_URL="https://huggingface.co/$MODEL_ID"
[[ -n "$HF_TOKEN" ]] && REPO_URL="https://user:${HF_TOKEN}@huggingface.co/$MODEL_ID"
# Clone without LFS or checkout, then sparse-checkout only safetensors
GIT_LFS_SKIP_SMUDGE=1 git clone --depth 1 --no-checkout "$REPO_URL" "$TARGET_DIR"
cd "$TARGET_DIR"
# Sparse checkout: exclude bin/gguf/onnx/pth to only get safetensors weights
git sparse-checkout init --no-cone
git sparse-checkout set '/*' '!*.bin' '!*.gguf' '!*.onnx' '!consolidated*.pth'
git checkout
# Configure LFS for reliability with large models
git lfs install --local
git config lfs.concurrenttransfers 4
git config lfs.transfer.maxretries 10
git config lfs.transfer.maxretrydelay 30
# Fetch only safetensors LFS objects with progress and retry logic
attempt=1
max_attempts=3
while true; do
echo "LFS fetch attempt ${attempt}/${max_attempts}..."
if git lfs fetch --include="*.safetensors"; then
break
fi
if [[ $attempt -ge $max_attempts ]]; then
echo "ERROR: LFS fetch failed after ${max_attempts} attempts"
exit 1
fi
sleep $((attempt * 5))
((attempt++))
done
echo "Checking out LFS files..."
git lfs checkout --include="*.safetensors"
# Fallback: if model only ships bin weights, re-include and fetch those
if ! ls *.safetensors 1>/dev/null 2>&1; then
echo "No safetensors files found, falling back to bin weights..."
git sparse-checkout set '/*'
git checkout
git lfs fetch --include="*.bin"
git lfs checkout --include="*.bin"
fi
cd "$RUNDIR"
# Verify model weights exist (not just LFS pointers)
if [[ -f "$TARGET_DIR/model.safetensors" ]]; then
actual_size=$(stat -c%s "$TARGET_DIR/model.safetensors" 2>/dev/null || stat -f%z "$TARGET_DIR/model.safetensors" 2>/dev/null)
[[ $actual_size -lt 1000000 ]] && { echo "ERROR: model.safetensors appears to be an LFS pointer, not actual weights"; exit 1; }
elif ls "$TARGET_DIR"/model*.safetensors 1>/dev/null 2>&1; then
# Multiple sharded safetensors files
for f in "$TARGET_DIR"/model*.safetensors; do
actual_size=$(stat -c%s "$f" 2>/dev/null || stat -f%z "$f" 2>/dev/null)
[[ $actual_size -lt 1000000 ]] && { echo "ERROR: $f appears to be an LFS pointer, not actual weights"; exit 1; }
break # Just check the first one
done
fi
[[ ! -f "$TARGET_DIR/config.json" ]] && { echo "ERROR: Model clone incomplete - missing config.json"; exit 1; }
fi
# Update environment file
cat >> .run.env << EOF
export MODEL_PATH=$TARGET_DIR
export MODEL_NAME=$TARGET_DIR
export TRANSFORMERS_OFFLINE=1
EOF
echo "model_path=$TARGET_DIR" >> $OUTPUTS
echo "Model ready at $TARGET_DIR"
# ============================================================================
# Prepare Embedding Model (RAG)
# ============================================================================
prepare_embedding_model:
needs: [setup]
if: ${{ inputs.runtype == 'all' && (inputs.rag.embedding_model_source == 'huggingface' || inputs.rag.embedding_model_source == 'bucket') }}
ssh:
remoteHost: ${{ inputs.resource.ip }}
steps:
- name: Clone Embedding Model
id: clone_embedding
early-cancel: any-job-failed
run: |
set -x
RUNDIR="${{ needs.setup.outputs.rundir }}"
cd "$RUNDIR"
SOURCE="${{ inputs.rag.embedding_model_source }}"
MODEL_ID="${{ inputs.rag.embedding_model_id }}"
CACHE_DIR="${{ inputs.rag.embedding_model_cache_dir }}"
if [[ -z "$CACHE_DIR" || "$CACHE_DIR" == "undefined" ]]; then
CACHE_DIR="${{ inputs.model.cache_dir }}"
fi
if [[ -z "$CACHE_DIR" || "$CACHE_DIR" == "undefined" ]]; then
CACHE_DIR="~/pw/models"
fi
CACHE_DIR="${CACHE_DIR/#\~/$HOME}"
HF_TOKEN="${{ inputs.model.hf_token }}"
mkdir -p "$CACHE_DIR"
MODEL_BASENAME="${MODEL_ID##*/}"
SAFE_MODEL_ID="${MODEL_ID//\//__}"
LEGACY_DIR="$CACHE_DIR/$SAFE_MODEL_ID"
TARGET_DIR="$CACHE_DIR/$MODEL_BASENAME"
if [[ -d "$LEGACY_DIR" && ! -d "$TARGET_DIR" ]]; then
echo "Renaming legacy cache dir $LEGACY_DIR to $TARGET_DIR"
mv "$LEGACY_DIR" "$TARGET_DIR"
fi
# Function to validate embedding model (check for config and model files)
validate_embedding() {
local dir=$1
[[ ! -f "$dir/config.json" ]] && return 1
# Check for pytorch_model.bin, model.safetensors, or any .bin/.safetensors files
if [[ -f "$dir/pytorch_model.bin" || -f "$dir/model.safetensors" ]]; then
local weight_file="$dir/pytorch_model.bin"
[[ -f "$dir/model.safetensors" ]] && weight_file="$dir/model.safetensors"
local size=$(stat -c%s "$weight_file" 2>/dev/null || stat -f%z "$weight_file" 2>/dev/null)
[[ $size -lt 100000 ]] && return 1 # Embedding models are smaller, use 100KB threshold
fi
return 0
}
# Check if already cached AND valid
if [[ -d "$TARGET_DIR" ]] && validate_embedding "$TARGET_DIR"; then
echo "Embedding model already cached and validated at $TARGET_DIR"
else
# Remove incomplete cache if exists
if [[ -d "$TARGET_DIR" ]]; then
echo "Cached embedding model at $TARGET_DIR appears incomplete, removing..."
rm -rf "$TARGET_DIR"
fi
if [[ "$SOURCE" == "bucket" ]]; then
BUCKET="${{ inputs.rag.embedding_model_bucket }}"
if [[ -z "$BUCKET" || "$BUCKET" == "undefined" ]]; then
BUCKET="pw://mshaxted/codeassist"
fi
BUCKET="${BUCKET%/}"
echo "Pulling embedding model from ${BUCKET} to ${TARGET_DIR}"
pulled=false
if pw bucket cp -r "${BUCKET}/${MODEL_BASENAME}" "$CACHE_DIR" 2>/dev/null; then
pulled=true
elif pw bucket cp -r "${BUCKET}/${SAFE_MODEL_ID}" "$CACHE_DIR" 2>/dev/null; then
pulled=true
elif pw bucket cp "${BUCKET}/${MODEL_BASENAME}" "$TARGET_DIR" 2>/dev/null; then
pulled=true
elif pw bucket cp "${BUCKET}/${SAFE_MODEL_ID}" "$LEGACY_DIR" 2>/dev/null; then
pulled=true
fi
if [[ "$pulled" != "true" ]]; then
echo "ERROR: Failed to pull embedding model from bucket"
exit 1
fi
if [[ -d "$LEGACY_DIR" && ! -d "$TARGET_DIR" ]]; then
echo "Renaming legacy cache dir $LEGACY_DIR to $TARGET_DIR"
mv "$LEGACY_DIR" "$TARGET_DIR"
fi
else
echo "Cloning embedding model $MODEL_ID to $TARGET_DIR"
# Ensure git-lfs is available
if ! git lfs version >/dev/null 2>&1; then
echo "Installing git-lfs locally..."
mkdir -p $HOME/.local
cd /tmp
LFS_URL=$(curl -s https://api.github.com/repos/git-lfs/git-lfs/releases/latest | grep browser_download_url | grep linux-amd64 | head -1 | cut -d '"' -f 4)
[[ -z "$LFS_URL" ]] && { echo "ERROR: Could not find git-lfs download URL"; exit 1; }
wget -q "$LFS_URL" -O git-lfs-linux-amd64.tar.gz
tar -xzf git-lfs-linux-amd64.tar.gz
./git-lfs-*/install.sh --local
rm -rf git-lfs-*
export PATH="$HOME/.local/bin:$PATH"
cd ${{ needs.setup.outputs.rundir }}
git lfs version >/dev/null 2>&1 || { echo "ERROR: Failed to install git-lfs"; exit 1; }
fi
git lfs install
# Build clone URL
REPO_URL="https://huggingface.co/$MODEL_ID"
[[ -n "$HF_TOKEN" ]] && REPO_URL="https://user:${HF_TOKEN}@huggingface.co/$MODEL_ID"
# Clone with LFS and pull large files
GIT_LFS_SKIP_SMUDGE=1 git clone --depth 1 "$REPO_URL" "$TARGET_DIR"
cd "$TARGET_DIR"
git lfs pull
cd "$RUNDIR"
fi
[[ ! -f "$TARGET_DIR/config.json" ]] && { echo "ERROR: Embedding model download incomplete - missing config.json"; exit 1; }
fi
# Update environment file
cat >> .run.env << EOF
export EMBEDDING_MODEL=$TARGET_DIR
export EMBEDDING_CACHE_DIR=$CACHE_DIR
export TRANSFORMERS_OFFLINE=1
EOF
echo "embedding_model_path=$TARGET_DIR" >> $OUTPUTS
echo "Embedding model ready at $TARGET_DIR"
# ============================================================================
# Prepare Tiktoken Encodings (Optional)
# ============================================================================
prepare_tiktoken:
needs: [setup]
if: ${{ inputs.advanced_settings.tiktoken_encodings == true }}
ssh:
remoteHost: ${{ inputs.resource.ip }}
steps:
- name: Download Tiktoken Encodings
id: download_tiktoken
early-cancel: any-job-failed
run: |
set -x
cd ${{ needs.setup.outputs.rundir }}
mkdir -p cache/tiktoken_encodings
# Method 1: Try direct download from OpenAI public URL
echo "Attempting download from openaipublic.blob.core.windows.net..."
wget --tries=2 --timeout=30 -O cache/tiktoken_encodings/o200k_base.tiktoken "https://openaipublic.blob.core.windows.net/encodings/o200k_base.tiktoken" 2>/dev/null || true
wget --tries=2 --timeout=30 -O cache/tiktoken_encodings/cl100k_base.tiktoken "https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken" 2>/dev/null || true
# Method 2: If wget failed or files are empty, fallback to PW bucket
if [[ ! -s cache/tiktoken_encodings/o200k_base.tiktoken ]] || [[ ! -s cache/tiktoken_encodings/cl100k_base.tiktoken ]]; then
echo "Direct download failed or files empty, falling back to PW bucket..."
rm -f cache/tiktoken_encodings/*.tiktoken 2>/dev/null || true
pw bucket cp "pw://mshaxted/codeassist/o200k_base.tiktoken" cache/tiktoken_encodings/
pw bucket cp "pw://mshaxted/codeassist/cl100k_base.tiktoken" cache/tiktoken_encodings/
fi
# Final verification
if [[ ! -s cache/tiktoken_encodings/o200k_base.tiktoken ]]; then
echo "ERROR: o200k_base.tiktoken download failed or file is empty"
exit 1
fi
if [[ ! -s cache/tiktoken_encodings/cl100k_base.tiktoken ]]; then
echo "ERROR: cl100k_base.tiktoken download failed or file is empty"
exit 1
fi
ls -la cache/tiktoken_encodings/
echo "Tiktoken encodings downloaded and verified"
# ============================================================================
# Run Service (using marketplace job_runner)
# ============================================================================
run_service:
needs: [setup, prepare_containers, prepare_model, prepare_embedding_model, prepare_tiktoken]
working-directory: ${{ needs.setup.outputs.rundir }}
ssh:
remoteHost: ${{ inputs.resource.ip }}
steps:
- name: Run Service
uses: marketplace/job_runner/v4.0
early-cancel: any-job-failed
with:
resource: ${{ inputs.resource }}
shebang: '#!/bin/bash'
rundir: ${{ needs.setup.outputs.rundir }}
use_existing_script: true
script_path: ${{ needs.setup.outputs.rundir + '/start_service.sh' }}
scheduler: ${{ inputs.submit_to_scheduler }}
inject_markers: true
slurm:
is_disabled: ${{ inputs.submit_to_scheduler != true || inputs.resource.schedulerType != 'slurm' }}
account: ${{ inputs.slurm.account }}
partition: ${{ inputs.slurm.partition }}
qos: ${{ inputs.slurm.qos }}
time: ${{ inputs.slurm.time }}
cpus_per_task: ${{ inputs.slurm.cpus_per_task }}
gres: ${{ inputs.slurm.gres }}
scheduler_directives: |
${{ inputs.slurm.scheduler_directives }}
pbs:
is_disabled: ${{ inputs.submit_to_scheduler != true || inputs.resource.schedulerType != 'pbs' }}
account: ${{ inputs.pbs.account }}
queue: ${{ inputs.pbs.queue }}
walltime: ${{ inputs.pbs.walltime }}
select: ${{ inputs.pbs.select }}
scheduler_directives: |
${{ inputs.pbs.scheduler_directives }}
# ============================================================================
# Session Management
# ============================================================================
create_session:
needs: [setup]
ssh:
remoteHost: ${{ inputs.resource.ip }}
steps:
- name: Wait for Job to Start
id: wait_job
early-cancel: any-job-failed
run: |
set -x
TIMEOUT=${{ inputs.advanced_settings.session_timeout }}
elapsed=0
while [ ! -f ${{ needs.setup.outputs.rundir }}/job.started ]; do
if [[ $TIMEOUT -eq 0 ]]; then
echo "Waiting for job to start... (${elapsed}s, no timeout - waiting until canceled)"
else
echo "Waiting for job to start... (${elapsed}s/${TIMEOUT}s)"
fi
sleep 5
((elapsed+=5))
[[ $TIMEOUT -gt 0 && $elapsed -ge $TIMEOUT ]] && { echo "ERROR: Timeout waiting for job"; exit 1; }
done
echo "Job started"
- name: Get Hostname
id: get_hostname
early-cancel: any-job-failed
run: |
set -x
cd ${{ needs.setup.outputs.rundir }}
# Wait for HOSTNAME file (written by start_service.sh via script_submitter)
timeout=120; elapsed=0
while [ ! -f HOSTNAME ]; do
sleep 2; ((elapsed+=2))
echo "Waiting for HOSTNAME file... (${elapsed}s/${timeout}s)"
[[ $elapsed -ge $timeout ]] && break
done
if [ -f HOSTNAME ]; then
target_hostname=$(cat HOSTNAME 2>/dev/null | head -1)
else
# Fallback: if no HOSTNAME file, try getting from scheduler or localhost
if [[ "${{ inputs.submit_to_scheduler }}" == "true" ]]; then
source jobid 2>/dev/null || true
if [[ -n "$jobid" ]]; then
# Try SLURM first
target_hostname=$(squeue -j "${jobid}" --noheader --format="%N" 2>/dev/null || true)
fi
fi
# Last resort: use login node
[[ -z "${target_hostname}" ]] && target_hostname=$(hostname)
fi
[[ -z "${target_hostname}" ]] && { echo "$(date) Failed to get target hostname"; exit 1; }
echo "target_hostname=${target_hostname}" | tee -a $OUTPUTS
- name: Get Session Port
id: get_port
early-cancel: any-job-failed
run: |
set -euo pipefail
set -x
TIMEOUT=${{ inputs.advanced_settings.session_timeout }}
RETRY_INTERVAL=3
cd ${{ needs.setup.outputs.rundir }}
attempt=1; elapsed=0
while true; do
echo "$(date) Attempt $attempt: Checking for SESSION_PORT..."
if [ -f SESSION_PORT ]; then
echo "$(date) Success: SESSION_PORT found!"
PORT=$(cat SESSION_PORT)
echo "SESSION_PORT=$PORT" | tee -a "$OUTPUTS"
exit 0
elif [ -f job.ended ]; then
echo "$(date) Job ended without SESSION_PORT"
exit 1
else
if [[ $TIMEOUT -eq 0 ]]; then
echo "$(date) Waiting... (${elapsed}s, no timeout)"
else
echo "$(date) Waiting... (${elapsed}s/${TIMEOUT}s)"
fi
sleep "$RETRY_INTERVAL"
((attempt++)); ((elapsed+=RETRY_INTERVAL))
[[ $TIMEOUT -gt 0 && $elapsed -ge $TIMEOUT ]] && { echo "$(date) Timeout"; exit 1; }
fi
done
- name: Wait for Server
id: wait_server
early-cancel: any-job-failed
run: |
TIMEOUT=${{ inputs.advanced_settings.session_timeout }}
RETRY_INTERVAL=3
remote_host="${{ needs.create_session.steps.get_hostname.outputs.target_hostname }}"
remote_port="${{ needs.create_session.steps.get_port.outputs.SESSION_PORT }}"
local_host=""
if [[ -n "${remote_host}" ]]; then
host_short=$(hostname 2>/dev/null || true)
host_fqdn=$(hostname -f 2>/dev/null || true)
if [[ "${remote_host}" == "${host_short}" || "${remote_host}" == "${host_fqdn}" ]]; then
local_host="127.0.0.1"
fi
fi
cd ${{ needs.setup.outputs.rundir }}
attempt=1; elapsed=0
while true; do
echo "$(date) Attempt $attempt: Checking ${remote_host}:${remote_port}..."
if curl --silent --connect-timeout 5 "http://${remote_host}:${remote_port}" >/dev/null 2>&1; then
echo "$(date) Success: Server is listening!"
exit 0
elif [[ -n "${local_host}" ]] && curl --silent --connect-timeout 5 "http://${local_host}:${remote_port}" >/dev/null 2>&1; then
echo "$(date) Success: Server is listening on ${local_host}:${remote_port}!"
exit 0
elif [ -f job.ended ]; then
echo "$(date) Job completed"
exit 0
else
if [[ $TIMEOUT -eq 0 ]]; then
echo "$(date) Waiting... (${elapsed}s, no timeout)"
else
echo "$(date) Waiting... (${elapsed}s/${TIMEOUT}s)"
fi
sleep "$RETRY_INTERVAL"
((attempt++)); ((elapsed+=RETRY_INTERVAL))
[[ $TIMEOUT -gt 0 && $elapsed -ge $TIMEOUT ]] && { echo "$(date) Timeout"; exit 1; }
fi
done
- name: Update Session
id: update_session
uses: parallelworks/update-session
with:
remotePort: '${{ needs.create_session.steps.get_port.outputs.SESSION_PORT }}'
target: '${{ inputs.resource.id }}'
name: '${{ sessions.session }}'
remoteHost: '${{ needs.create_session.steps.get_hostname.outputs.target_hostname }}'
localPort: '${{ inputs.advanced_settings.localport }}'
# ==============================================================================
# INPUT DEFINITIONS
# ==============================================================================
'on':
execute:
inputs:
# ========================================================================
# Resource Selection
# ========================================================================
resource:
type: compute-clusters
label: GPU Cluster
autoselect: true
include-workspace: false
tooltip: Resource to run the service
# ========================================================================
# Deployment Type
# ========================================================================
runtype:
label: Deployment Type
type: dropdown
default: vllm
tooltip: Deploy vLLM only or full vLLM+RAG stack
options:
- value: vllm
label: vLLM Only
- value: all
label: vLLM + RAG (Chroma)
# ========================================================================
# Scheduler Submission
# ========================================================================
submit_to_scheduler:
type: boolean
label: Submit to Job Scheduler
tooltip: Enable to submit job via SLURM or PBS scheduler (detected automatically from resource). Disable for direct SSH execution.
default: false
hidden: ${{ inputs.resource.schedulerType != 'slurm' && inputs.resource.schedulerType != 'pbs' }}
# ========================================================================
# Model Configuration
# ========================================================================
model:
type: group
label: Model Configuration
items:
source:
type: dropdown
label: Model Source
default: huggingface
tooltip: Use a pre-downloaded model (local) or clone from HuggingFace using git-lfs
options:
- value: local
label: "📁 Local Path (pre-staged weights)"
- value: huggingface
label: "🤗 HuggingFace Clone (git-lfs)"
local_path:
type: string
label: Model Path
placeholder: /path/to/model/weights
default: /models/Llama-3_3-Nemotron-Super-49B-v1_5
prefillDefault: true
tooltip: Full path to directory containing model weights
hidden: ${{ inputs.model.source != 'local' }}
ignore: ${{ inputs.model.source != 'local' }}
hf_model_id:
type: string
label: HuggingFace Model ID
placeholder: openai/gpt-oss-20b
default: openai/gpt-oss-20b
prefillDefault: true
tooltip: Model ID to clone from HuggingFace. Default is gpt-oss-20b, a 20B MoE model compatible with the openai tool call parser.
hidden: ${{ inputs.model.source != 'huggingface' }}
ignore: ${{ inputs.model.source != 'huggingface' }}
hf_token:
label: HuggingFace Token
optional: true
default: ${{ org.HF_TOKEN }}
type: password
tooltip: Required for gated models (Llama, etc.)
hidden: ${{ inputs.model.source != 'huggingface' }}
ignore: ${{ inputs.model.source != 'huggingface' }}
cache_dir:
type: string
label: Model Cache Directory
default: ~/pw/models
prefillDefault: true
tooltip: Directory to clone model into. Model is cloned once and reused for subsequent runs. Ensure sufficient disk space.