mlclaw 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/.agents/skills/mlclaw/SKILL.md +63 -18
  2. package/Dockerfile +21 -4
  3. package/README.md +70 -24
  4. package/assets/hf-logo.svg +8 -0
  5. package/assets/hf-tooling/manifest.json +50 -0
  6. package/assets/hf-tooling/skills/hf-cli/SKILL.md +218 -0
  7. package/assets/hf-tooling/skills/hf-mem/SKILL.md +79 -0
  8. package/assets/hf-tooling/skills/huggingface-best/SKILL.md +134 -0
  9. package/assets/hf-tooling/skills/huggingface-datasets/SKILL.md +107 -0
  10. package/assets/hf-tooling/skills/huggingface-gradio/SKILL.md +298 -0
  11. package/assets/hf-tooling/skills/huggingface-gradio/examples.md +613 -0
  12. package/assets/hf-tooling/skills/huggingface-local-models/SKILL.md +113 -0
  13. package/assets/hf-tooling/skills/huggingface-local-models/references/hardware.md +38 -0
  14. package/assets/hf-tooling/skills/huggingface-local-models/references/hub-discovery.md +178 -0
  15. package/assets/hf-tooling/skills/huggingface-local-models/references/quantization.md +256 -0
  16. package/assets/hf-tooling/skills/huggingface-papers/SKILL.md +239 -0
  17. package/assets/hf-tooling/skills/huggingface-spaces/SKILL.md +239 -0
  18. package/assets/hf-tooling/skills/huggingface-spaces/references/buckets.md +89 -0
  19. package/assets/hf-tooling/skills/huggingface-spaces/references/debugging.md +236 -0
  20. package/assets/hf-tooling/skills/huggingface-spaces/references/gradio.md +200 -0
  21. package/assets/hf-tooling/skills/huggingface-spaces/references/grants.md +56 -0
  22. package/assets/hf-tooling/skills/huggingface-spaces/references/inference-providers.md +85 -0
  23. package/assets/hf-tooling/skills/huggingface-spaces/references/known-errors.md +232 -0
  24. package/assets/hf-tooling/skills/huggingface-spaces/references/requirements.md +169 -0
  25. package/assets/hf-tooling/skills/huggingface-spaces/references/zerogpu.md +349 -0
  26. package/assets/hf-tooling/skills/huggingface-tool-builder/SKILL.md +120 -0
  27. package/assets/hf-tooling/skills/huggingface-tool-builder/references/baseline_hf_api.py +57 -0
  28. package/assets/hf-tooling/skills/huggingface-tool-builder/references/baseline_hf_api.sh +40 -0
  29. package/assets/hf-tooling/skills/huggingface-tool-builder/references/baseline_hf_api.tsx +57 -0
  30. package/assets/hf-tooling/skills/huggingface-tool-builder/references/find_models_by_paper.sh +230 -0
  31. package/assets/hf-tooling/skills/huggingface-tool-builder/references/hf_enrich_models.sh +96 -0
  32. package/assets/hf-tooling/skills/huggingface-tool-builder/references/hf_model_card_frontmatter.sh +188 -0
  33. package/assets/hf-tooling/skills/huggingface-tool-builder/references/hf_model_papers_auth.sh +171 -0
  34. package/assets/hf-tooling/skills/huggingface-zerogpu/SKILL.md +289 -0
  35. package/assets/hf-tooling/skills/huggingface-zerogpu/references/concurrency.md +79 -0
  36. package/assets/hf-tooling/skills/huggingface-zerogpu/references/cuda-and-deps.md +66 -0
  37. package/assets/hf-tooling/skills/huggingface-zerogpu/references/how-quota-works.md +74 -0
  38. package/assets/hf-tooling/skills/huggingface-zerogpu/references/how-zerogpu-works.md +50 -0
  39. package/assets/hf-tooling/templates/.agents/mcp/huggingface.json +11 -0
  40. package/assets/hf-tooling/templates/.env.example +5 -0
  41. package/assets/hf-tooling/templates/examples/huggingface/README.md +16 -0
  42. package/assets/hf-tooling/templates/examples/huggingface/bucket-sync.md +19 -0
  43. package/assets/hf-tooling/templates/examples/huggingface/dataset-upload.py +22 -0
  44. package/assets/hf-tooling/templates/examples/huggingface/hf-discover.md +13 -0
  45. package/assets/hf-tooling/templates/examples/huggingface/runtime-inspection.py +9 -0
  46. package/assets/mlclaw-control-ui/assets/index-D2TFes32.js +9 -0
  47. package/assets/mlclaw-control-ui/assets/index-DP72PFuv.css +1 -0
  48. package/assets/mlclaw-control-ui/index.html +13 -0
  49. package/assets/mlclaw.svg +298 -124
  50. package/dist/hf-tooling-seed.js +261 -0
  51. package/dist/mlclaw-space-runtime.js +6634 -3321
  52. package/dist/mlclaw.mjs +428 -178
  53. package/entrypoint.sh +4 -0
  54. package/mlclaw.ps1 +4 -2
  55. package/mlclaw.sh +5 -3
  56. package/openclaw.default.json +1 -0
  57. package/package.json +16 -2
  58. package/space/README.md +32 -2
  59. package/tsconfig.json +3 -2
@@ -0,0 +1,74 @@
1
+ # How ZeroGPU duration and quota are checked
2
+
3
+ Mechanism for `duration` validation and quota pre-checks. Useful when choosing `duration` values, debugging `illegal duration` vs `quota exceeded` errors, and understanding why the default 60s is pessimistic for short tasks.
4
+
5
+ For per-tier numerical thresholds (free vs Pro vs Team vs Enterprise quota minutes), the daily quota window length, runs-per-day limits, and pay-as-you-go pricing, see [the ZeroGPU docs](https://huggingface.co/docs/hub/spaces-zerogpu) — those values change over time and are deliberately kept out of this skill.
6
+
7
+ ## What `duration` actually requests
8
+
9
+ Whatever value is passed to `@spaces.GPU(duration=N)` (or the default 60s when unspecified) becomes the `requested duration` the platform checks against. For `xlarge`, the request is doubled internally:
10
+
11
+ ```
12
+ requested = N * 2 if size == "xlarge" else N
13
+ ```
14
+
15
+ So `@spaces.GPU(duration=60, size="xlarge")` is internally a 120-second request — both for the tier-max check and the quota pre-check below.
16
+
17
+ ## Two distinct error modes
18
+
19
+ Two failure messages can come back from the scheduler before the call runs:
20
+
21
+ | Error | Trigger | What helps |
22
+ |---|---|---|
23
+ | **`ZeroGPU illegal duration`** | `requested duration > visitor's tier per-call cap` | Lower `duration`. Sign in / upgrade tier. **Waiting does not help.** |
24
+ | **`ZeroGPU quota exceeded`** | `remaining quota < requested duration`, OR runs-per-day cap reached | Wait for the quota window to reset. For Pro / Team / Enterprise, pay-as-you-go credits cover the overflow. |
25
+
26
+ The error wording for `quota exceeded` includes the explicit numbers, e.g.:
27
+
28
+ ```
29
+ You have exceeded your Pro ZeroGPU quota
30
+ (60s requested vs. 30s left). Try again in 1:23:45.
31
+ ```
32
+
33
+ The comparison is **`requested` vs `remaining`** — not `actual run time` vs `remaining`. A 10-second task left at the default 60s requests 60s of quota; once `remaining < 60s` the call fails even though the actual work would have fit.
34
+
35
+ ## Why the default 60s is pessimistic for short tasks
36
+
37
+ `DEFAULT_SCHEDULE_DURATION` in the `spaces` package is **60 seconds**. So an undecorated `@spaces.GPU` (or `@spaces.GPU()` with no `duration=`) requests 60s of quota.
38
+
39
+ For a task that actually takes ~10 seconds:
40
+
41
+ - The user's 60s quota gets reserved up front.
42
+ - Once their remaining quota drops below 60s, your Space fails for them — even though they could have run many more 10s tasks if the request matched reality.
43
+ - Your call also ranks lower in the queue than equivalent calls declaring smaller durations.
44
+
45
+ The fix is to declare the realistic duration explicitly:
46
+
47
+ ```python
48
+ @spaces.GPU(duration=15)
49
+ def fast_task(...):
50
+ ...
51
+ ```
52
+
53
+ For workloads where runtime depends on inputs, use a callable (per-request estimator):
54
+
55
+ ```python
56
+ def estimate_duration(prompt, steps):
57
+ return int(steps * 3.5)
58
+
59
+ @spaces.GPU(duration=estimate_duration)
60
+ def variable_task(prompt, steps):
61
+ ...
62
+ ```
63
+
64
+ This preserves quota for light inputs and reserves more only when needed.
65
+
66
+ ## Quota window: 24h fixed from first use
67
+
68
+ The quota window's TTL is set when the first call of a fresh window lands and counts down unconditionally — it is not a sliding window, not a calendar-day reset, and not extended by subsequent use. A user who runs a call at 14:00 sees their next reset at 14:00 the following day, regardless of how heavily or lightly they use the Space in between.
69
+
70
+ For exact tier thresholds, runs-per-day caps, and pay-as-you-go billing rates, see the [ZeroGPU docs](https://huggingface.co/docs/hub/spaces-zerogpu).
71
+
72
+ ## Queue priority
73
+
74
+ The queue is **node-level** — requests from every Space scheduled on the same physical node compete for that node's GPU slots. Among queued requests, **shorter declared `duration` ranks higher**. So tight per-request `duration` estimates serve two goals at once: they preserve the user's quota and move the request up the queue.
@@ -0,0 +1,50 @@
1
+ # How ZeroGPU works (mechanism)
2
+
3
+ Conceptual lifecycle of model weights, processes, and worker reuse on ZeroGPU. Useful when reasoning about cold-starts, why module-scope warmup does not carry over to requests, why returning CUDA tensors hangs the call, or why `gr.State` mutations do not persist across the worker boundary.
4
+
5
+ For numerical limits (concurrency slots per Space, queue priority by tier, etc.), see [the ZeroGPU docs](https://huggingface.co/docs/hub/spaces-zerogpu) — those values change over time and are deliberately kept out of this skill.
6
+
7
+ ## Two processes, two lifetimes
8
+
9
+ A ZeroGPU Space runs as **two separate processes**:
10
+
11
+ - **Main web process** — long-lived. Imports `app.py`, launches Gradio, stays up for the life of the Space. Holds no VRAM and, after the startup "pack" step, holds no model weights in RAM either.
12
+ - **GPU worker processes** — short-lived. Forked per `@spaces.GPU` request (or reused if warm). Run the task and are eventually killed by the ZeroGPU scheduler when another Space needs the GPU slot. Your Space code never kills its own worker.
13
+
14
+ ## Module-scope `.to("cuda")` is captured to disk
15
+
16
+ When `import spaces` is active, `model.to("cuda")` at module scope is intercepted. The call is rewritten to `to("cpu")`, so the tensor data physically lives in main process RAM at this point. A "fake" CUDA-presenting tensor is registered alongside the original CPU tensor.
17
+
18
+ At a startup "pack" step, the backend writes those original CPU tensors to disk via direct I/O (`O_DIRECT`), then frees the corresponding RAM. After pack, the main process holds no model weights anywhere — the data lives only on disk.
19
+
20
+ This is why module-scope `pipe(...)` / `model.generate(...)` / `model(...)` calls do not run on a real GPU: there is no GPU attached to the main process, and after pack there are no weights to compute against either. Such calls either fail or silently fall back to CPU on the fake tensors.
21
+
22
+ ## Worker init: disk → pinned memory → VRAM
23
+
24
+ When a `@spaces.GPU` call lands, the scheduler routes it to a worker:
25
+
26
+ 1. **Cold worker** — forked from the main process. The patched torch is unpatched, real CUDA is initialized, and weights are read from the disk offload directory into pinned host memory and streamed onto VRAM through a double-buffered pipeline (essentially `pin_memory().cuda(non_blocking=True)` per batch). This is the "cold-start" cost.
27
+ 2. **Warm worker (reused)** — an alive worker bound to the same GPU slot is reused if the scheduler reports it idle. Init is skipped; weights stay on VRAM from the previous call. Subsequent requests within a burst hit this path.
28
+
29
+ A warm worker is eventually killed by the scheduler when another Space needs the GPU slot. The next call after that point pays the disk → VRAM cost again. Occasional cold-starts on a low-traffic Space are normal.
30
+
31
+ ## Why module-scope warmup does not help
32
+
33
+ A common instinct is to call `pipe("warmup")` at module scope to "prepare" the model. This does not work on ZeroGPU:
34
+
35
+ - At module scope, no real GPU is attached. The fake CUDA tensors do not have data after pack, so `pipe(...)` either fails or silently runs on something other than a real GPU.
36
+ - Even if you wrap the warmup in `@spaces.GPU`, the worker that ran the warmup will eventually be killed before the first real user request lands — leaving them with a cold worker anyway.
37
+
38
+ The right answer is to load eagerly at module scope (`pipe = pipeline(..., device="cuda")`) and accept that the first user request after a quiet period will be a cold worker. Cold-start is fast on ZeroGPU because of the pinned-memory disk pipeline; it is not free, but it is not "minutes of model download" either.
39
+
40
+ ## Why returning CUDA tensors hangs the call
41
+
42
+ The main process never has a CUDA context — it has no GPU attached and its torch never initialized CUDA. When a worker returns a CUDA tensor, unpickling it in the main process triggers `torch.cuda._lazy_init()`, which would attempt to initialize CUDA in the main process. ZeroGPU blocks this, and the call hangs.
43
+
44
+ The fix is purely client-side: convert to CPU before returning (`.cpu()`, `.cpu().numpy()`, etc.). See "Process Isolation and Pickle" in SKILL.md.
45
+
46
+ ## Why `gr.State` does not share by reference across the boundary
47
+
48
+ Worker processes are forked separately and exchange data with the main process via pickle. `gr.State` values cross this boundary on every yield, so mutations inside a `@spaces.GPU` generator are local to the worker until the mutated state is explicitly yielded back. The main process gets a fresh deserialized copy each time — `id()` differs, in-place mutations are invisible across the boundary.
49
+
50
+ See "Process Isolation and Pickle" in SKILL.md for the practical implications and `references/concurrency.md` for related parallel-handler concerns.
@@ -0,0 +1,11 @@
1
+ {
2
+ "mcpServers": {
3
+ "huggingface": {
4
+ "type": "http",
5
+ "url": "https://huggingface.co/mcp",
6
+ "headers": {
7
+ "Authorization": "Bearer ${HF_TOKEN}"
8
+ }
9
+ }
10
+ }
11
+ }
@@ -0,0 +1,5 @@
1
+ # ML Claw exposes these in the gateway runtime.
2
+ HF_TOKEN=
3
+ HUGGINGFACE_HUB_TOKEN=
4
+ OPENCLAW_HF_STATE_BUCKET=
5
+ OPENCLAW_MODEL=huggingface/google/gemma-4-26B-A4B-it
@@ -0,0 +1,16 @@
1
+ # Hugging Face Examples
2
+
3
+ This workspace starts with the Hugging Face CLI, `hf-discover`, `uv`,
4
+ `huggingface_hub`, `datasets`, and `safetensors` available.
5
+
6
+ Useful checks:
7
+
8
+ ```bash
9
+ hf auth whoami
10
+ hf repos list
11
+ hf-discover --version
12
+ python -c "import datasets, safetensors; from huggingface_hub import HfApi"
13
+ ```
14
+
15
+ Do not put secrets in this directory. Use the Space or local gateway secrets
16
+ managed by ML Claw.
@@ -0,0 +1,19 @@
1
+ # Storage Bucket Sync
2
+
3
+ The durable ML Claw state bucket is available as:
4
+
5
+ ```bash
6
+ echo "$OPENCLAW_HF_STATE_BUCKET"
7
+ ```
8
+
9
+ List buckets:
10
+
11
+ ```bash
12
+ hf buckets list
13
+ ```
14
+
15
+ Upload a file to an explicit bucket path:
16
+
17
+ ```bash
18
+ hf buckets cp ./local-file.txt "hf://buckets/$OPENCLAW_HF_STATE_BUCKET/examples/local-file.txt"
19
+ ```
@@ -0,0 +1,22 @@
1
+ from pathlib import Path
2
+
3
+ from huggingface_hub import HfApi
4
+
5
+
6
+ def main() -> None:
7
+ api = HfApi()
8
+ repo_id = "USERNAME_OR_ORG/example-dataset"
9
+ path = Path("sample.txt")
10
+ path.write_text("hello from ML Claw\n", encoding="utf-8")
11
+
12
+ api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True, private=True)
13
+ api.upload_file(
14
+ path_or_fileobj=str(path),
15
+ path_in_repo="sample.txt",
16
+ repo_id=repo_id,
17
+ repo_type="dataset",
18
+ )
19
+
20
+
21
+ if __name__ == "__main__":
22
+ main()
@@ -0,0 +1,13 @@
1
+ # hf-discover
2
+
3
+ Search for Hugging Face resources and agent skills:
4
+
5
+ ```bash
6
+ hf-discover search "train a model" --kind skill --json
7
+ ```
8
+
9
+ If your installed `hf` CLI exposes the discovery extension, this may also work:
10
+
11
+ ```bash
12
+ hf discover "help me train a model" --json
13
+ ```
@@ -0,0 +1,9 @@
1
+ import importlib.metadata as metadata
2
+ import os
3
+
4
+
5
+ for package in ["huggingface_hub", "datasets", "safetensors"]:
6
+ print(f"{package}=={metadata.version(package)}")
7
+
8
+ print("OPENCLAW_MODEL=", os.environ.get("OPENCLAW_MODEL", ""))
9
+ print("OPENCLAW_HF_STATE_BUCKET=", os.environ.get("OPENCLAW_HF_STATE_BUCKET", ""))