# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"apiVersion": "batch/v1beta1"
"kind": "CronJob"
"metadata":
  "name": "tf-nightly-transformer-perfzero-v100-x1"
  "namespace": "automated"
"spec":
  "concurrencyPolicy": "Forbid"
  "jobTemplate":
    "spec":
      "activeDeadlineSeconds": 10800
      "backoffLimit": 1
      "template":
        "spec":
          "activeDeadlineSeconds": 3600
          "containers":
          - "args":
            - "/bin/bash"
            - "-c"
            - |
              set -u
              set -e
              set -x
              
              sed -i 's_gs://tf-perfzero-data/bert_$(PERFZERO_DATA_DIR)_g' /garden/official/benchmark/bert_squad_benchmark.py
              sed -i 's_gs://tf-perfzero-data_$(PERFZERO_DATA_DIR)_g' /garden/official/benchmark/retinanet_benchmark.py
              sed -i 's_gs://mlcompass-data/transformer_$(PERFZERO_DATA_DIR)_g' /garden/official/benchmark/transformer_benchmark.py
              sed -i 's_gs://mlcompass-data/imagenet/imagenet-2012-tfrecord_$(PERFZERO_DATA_DIR)/imagenet_g' /garden/official/benchmark/resnet_ctl_imagenet_benchmark.py
              sed -i 's/wmt32k-en2de-official/transformer/g' /garden/official/benchmark/transformer_benchmark.py
              
              if [ -v TPU_NAME ]; then
                export BENCHMARK_TPU=${TPU_NAME#*/}
              fi
              
              python3 /benchmarks/perfzero/lib/benchmark.py --gcloud_key_file= --bigquery_project_name=xl-ml-test --bigquery_dataset_table_name=perfzero_dataset.perfzero_table --benchmark_methods=official.benchmark.transformer_benchmark.TransformerBaseKerasBenchmarkReal.benchmark_1_gpu --output_gcs_url=$(MODEL_DIR) --root_data_dir=$(PERFZERO_DATA_DIR)
              
            "env":
            - "name": "POD_NAME"
              "valueFrom":
                "fieldRef":
                  "fieldPath": "metadata.name"
            - "name": "POD_UID"
              "valueFrom":
                "fieldRef":
                  "fieldPath": "metadata.uid"
            - "name": "POD_NAMESPACE"
              "valueFrom":
                "fieldRef":
                  "fieldPath": "metadata.namespace"
            - "name": "JOB_NAME"
              "valueFrom":
                "fieldRef":
                  "fieldPath": "metadata.labels['job-name']"
            - "name": "MODEL_DIR"
              "value": "$(OUTPUT_BUCKET)/tf-nightly/transformer/perfzero/v100-x1/$(JOB_NAME)"
            - "name": "BENCHMARK_OUTPUT_DIR"
              "value": "$(MODEL_DIR)"
            "envFrom":
            - "configMapRef":
                "name": "gcs-buckets"
            "image": "gcr.io/xl-ml-test/tensorflow:nightly"
            "imagePullPolicy": "Always"
            "name": "train"
            "resources":
              "limits":
                "nvidia.com/gpu": 1
              "requests":
                "cpu": 2
                "memory": "20G"
            "volumeMounts":
            - "mountPath": "/dev/shm"
              "name": "dshm"
              "readOnly": false
          "initContainers":
          - "env":
            - "name": "POD_NAME"
              "valueFrom":
                "fieldRef":
                  "fieldPath": "metadata.name"
            - "name": "POD_UID"
              "valueFrom":
                "fieldRef":
                  "fieldPath": "metadata.uid"
            - "name": "POD_NAMESPACE"
              "valueFrom":
                "fieldRef":
                  "fieldPath": "metadata.namespace"
            - "name": "JOB_NAME"
              "valueFrom":
                "fieldRef":
                  "fieldPath": "metadata.labels['job-name']"
            - "name": "MODEL_DIR"
              "value": "$(OUTPUT_BUCKET)/tf-nightly/transformer/perfzero/v100-x1/$(JOB_NAME)"
            - "name": "METRIC_CONFIG"
              "value": |
                {
                 "metric_collection_config": {
                  "default_aggregation_strategies": [
                   "final"
                  ],
                  "metric_to_aggregation_strategies": {
                   "examples_per_second": [
                    "average"
                   ]
                  },
                  "use_run_name_prefix": true,
                  "write_to_bigquery": true
                 },
                 "regression_test_config": {
                  "metric_success_conditions": {
                   "exp_per_second": {
                    "comparison": "greater_or_equal",
                    "success_threshold": {
                     "stddevs_from_mean": 3
                    },
                    "wait_for_n_points_of_history": 10
                   },
                   "startup_time": {
                    "comparison": "less",
                    "success_threshold": {
                     "stddevs_from_mean": 4
                    },
                    "wait_for_n_points_of_history": 10
                   }
                  }
                 },
                 "test_name": "tf-nightly-transformer-perfzero-v100-x1"
                }
            "envFrom":
            - "configMapRef":
                "name": "gcs-buckets"
            "image": "gcr.io/xl-ml-test/publisher:stable"
            "imagePullPolicy": "Always"
            "name": "publisher"
          "nodeSelector":
            "cloud.google.com/gke-accelerator": "nvidia-tesla-v100"
          "priorityClassName": "gpu-v100"
          "restartPolicy": "Never"
          "volumes":
          - "emptyDir":
              "medium": "Memory"
            "name": "dshm"
  "schedule": "0 8 * * *"
  "successfulJobsHistoryLimit": 1