BatchML

GCP guide

From subscribing on Google Cloud Marketplace to your first results, in your own Google Cloud project.

You need, on the project you call from: roles/serviceusage.serviceUsageAdmin, roles/serviceusage.apiKeysAdmin, roles/iam.serviceAccountAdmin, and roles/storage.admin.

1. Subscribe and enable

Subscribe to BatchML on Google Cloud Marketplace with the billing account that pays your project; usage bills through the subscription. Each model region's endpoint is in the Endpoint column of the Models table. Support sends the managed service's name for BatchML (<endpointServiceName>, one name for every region). Enable the APIs, then create the caller service account, an API key, and a bucket in each model region you call:

gcloud services enable <endpointServiceName> apikeys.googleapis.com iam.googleapis.com \
  iamcredentials.googleapis.com storage.googleapis.com cloudresourcemanager.googleapis.com \
  --project=<your-project>
gcloud iam service-accounts create batchml-caller --project=<your-project>
CALLER_SA=batchml-caller@<your-project>.iam.gserviceaccount.com
CLOUDSDK_CORE_DISABLE_FILE_LOGGING=true gcloud services api-keys create \
  --project=<your-project> --key-id=batchml --display-name=batchml \
  --api-target=service=<endpointServiceName> --no-user-output-enabled
API_KEY=$(CLOUDSDK_CORE_DISABLE_FILE_LOGGING=true gcloud services api-keys get-key-string batchml \
  --project=<your-project> --format='value(keyString)')
test -n "$API_KEY" || { echo "no API key read" >&2; false; }
gcloud storage buckets create gs://<bucket> --project=<your-project> --location=<region>

2. Grant access

Every job runs as your caller service account. The one required grant is token creation on it, to BatchML's own service account -- batchml@etz-hadaat-mls.iam.gserviceaccount.com, the one BatchML identity to grant:

gcloud iam service-accounts add-iam-policy-binding $CALLER_SA --project=<your-project> \
  --member=serviceAccount:batchml@etz-hadaat-mls.iam.gserviceaccount.com \
  --role=roles/iam.serviceAccountTokenCreator

Grant the same role, on the same account, to whoever mints your tokens (a user, group or CI identity). The bucket, for reading inputs and writing results:

for ROLE in objectViewer objectCreator legacyBucketReader; do
  gcloud storage buckets add-iam-policy-binding gs://<bucket> \
    --member=serviceAccount:$CALLER_SA --role=roles/storage.$ROLE
done

3. Upload inputs

Put your input files under one folder of the bucket -- the source_prefix a job names:

gcloud storage cp ./inputs/* gs://<bucket>/in/

4. Submit the job

POST /v1/jobs to the endpoint for your model's region. The ID token's audience is <endpointServiceName> and carries the account's email; the full body is on the API page:

TOKEN=$(gcloud auth print-identity-token --impersonate-service-account=$CALLER_SA \
  --audiences=<endpointServiceName> --include-email)
curl -X POST "<endpoint>/v1/jobs" \
  -H "x-api-key: $API_KEY" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"source_prefix": "gs://<bucket>/in/", "tasks": [["a.txt"], ["b.txt"]],
       "destination": "gs://<bucket>/out/"}'

5. Poll and read results

Poll GET /v1/jobs/{batch_id}/status with the batch_id the submit reply issued until the batch concludes, then read the answers and one result file per task from your destination. Both shapes are on the API page.