# 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":
  "labels":
    "accelerator": "v100-x8"
    "benchmarkId": "tf-nightly-resnet50-ctl-perfzero-v100-x8"
    "frameworkVersion": "tf-nightly"
    "mode": "perfzero"
    "model": "resnet50-ctl"
  "name": "tf-nightly-resnet50-ctl-perfzero-v100-x8"
  "namespace": "automated"
"spec":
  "concurrencyPolicy": "Forbid"
  "jobTemplate":
    "metadata":
      "annotations":
        "ml-testing-accelerators/gcs-subdir": "tf-nightly/resnet50-ctl/perfzero/v100-x8"
        "ml-testing-accelerators/metric-config": |
          {
            "sources": [
              {
                "perfzero": {
                  "assertions": {
                    "exp_per_second": {
                      "inclusive_bounds": true,
                      "std_devs_from_mean": {
                        "comparison": "GREATER",
                        "std_devs": 3
                      },
                      "wait_for_n_data_points": 10
                    }
                  }
                }
              },
              {
                "tensorboard": {
                  "aggregate_assertions": [
          
                  ],
                  "exclude_tags": [
          
                  ],
                  "include_tags": [
                    {
                      "strategies": [
                        "FINAL"
                      ],
                      "tag_pattern": "*"
                    }
                  ],
                  "merge_runs": false
                }
              }
            ]
          }
      "labels":
        "accelerator": "v100-x8"
        "benchmarkId": "tf-nightly-resnet50-ctl-perfzero-v100-x8"
        "frameworkVersion": "tf-nightly"
        "mode": "perfzero"
        "model": "resnet50-ctl"
    "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.resnet_ctl_imagenet_benchmark.Resnet50CtlBenchmarkReal.benchmark_8_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/resnet50-ctl/perfzero/v100-x8/$(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": 8
              "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/resnet50-ctl/perfzero/v100-x8/$(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-resnet50-ctl-perfzero-v100-x8"
                }
            "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