Skip to content

[Jobs] Draft for 'run functions as jobs' API#3629

Draft
Wauplin wants to merge 1 commit intomainfrom
run-functions-as-jobs
Draft

[Jobs] Draft for 'run functions as jobs' API#3629
Wauplin wants to merge 1 commit intomainfrom
run-functions-as-jobs

Conversation

@Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Dec 15, 2025

API design

from huggingface_hub import as_job

@as_job(flavor="a10g-small", dependencies=["torch"])
def complex_stuff(*args, **kwargs) -> ...:
    import torch
    ...

result = complex_stuff(...)
  • functions and args are serialized using cloudpickle
  • job is run using uv
  • script is sent as base64 in env variable
  • return value is sent in logs as base64

^ clearly not a robust future-proof design but should be good enough to make some first tests.

For bigger input/output values, user must use a repo on the Hub as intermediary step. Or a bucket if they have one 😏

Example

import os
import platform
from huggingface_hub import as_job

@as_job(flavor="cpu-basic")
def cpu_runtime_info(name: str) -> dict:
    """Collect runtime information from a CPU job."""
    return {
        "name": name,  # dummy value received as input
        "platform": platform.platform(),
        "python_version": platform.python_version(),
        "cpu_count": os.cpu_count(),
    }

@as_job(flavor="a10g-small", dependencies=["torch"])
def gpu_runtime_info(name: str) -> dict:
    """Collect runtime information from a GPU job."""
    import torch
    return {
        "name": name,  # dummy value received as input
        "platform": platform.platform(),
        "python_version": platform.python_version(),
        "cpu_count": os.cpu_count(),
        "torch_version": torch.__version__,
    }

if __name__ == "__main__":
    info1 = cpu_runtime_info("my-cpu-job")
    info2 = gpu_runtime_info("my-gpu-job")

    print("CPU job info:", info1)
    print("GPU job info:", info2)
Job 69404b931a39f67af5a41f1c is running remotely. Fetching logs:
https://huggingface.co/jobs/Wauplin/69404b931a39f67af5a41f1c
-----
Installed 1 package in 1ms
Job 69404b9a1a39f67af5a41f1d is running remotely. Fetching logs:
https://huggingface.co/jobs/Wauplin/69404b9a1a39f67af5a41f1d
-----
(...)

CPU job info: {'name': 'my-cpu-job', 'platform': 'Linux-6.12.55-74.119.amzn2023.x86_64-x86_64-with-glibc2.36', 'python_version': '3.11.14', 'cpu_count': 16}
GPU job info: {'name': 'my-gpu-job', 'platform': 'Linux-6.12.58-82.121.amzn2023.x86_64-x86_64-with-glibc2.36', 'python_version': '3.11.14', 'cpu_count': 8, 'torch_version': '2.9.1+cu128'}

TODO

  • resolve params spec available only in Python 3.10+

@bot-ci-comment
Copy link

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant