// Copyright 2019-2021 Huawei Technologies Co., Ltd.
//
// 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
//
//     http://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.

syntax = "proto2";

package mindinsight;
option cc_enable_arenas = true;

// The ANF IR define, include the tensor and graph define
import "mindinsight_anf_ir.proto";

// Event Protocol buffer, Top define
message Event {
  // Timestamp
  required double wall_time = 1;

  // The step of train.
  optional int64 step = 2;

  oneof what {
    // An event file was started, with the specified version.
    // Now version is "Mindspore.Event:1"
    string version = 3;

    // GraphDef.
    GraphProto graph_def = 4;

    // Summary data
    Summary summary = 5;


    Explain explain = 6;

  }
}

message LossLandscape {
     message Point {
        optional TensorProto x=1;
        optional TensorProto y=2;
        optional TensorProto z=3;
    }

    message LossPath {
        repeated int32 intervals = 1; // step intervals or epoch intervals
        optional Point points = 2;
    }

    message Metadata {
        optional string decomposition = 1;
        optional string unit = 2; // step or epoch
        optional int32 step_per_epoch = 3;
    }

    optional Point landscape = 1;
    optional LossPath loss_path = 2;
    optional Metadata metadata = 3;  // maybe only record by the first value
    optional Point convergence_point = 4;

}

// A Summary is a set of named values that be produced regularly during training
message Summary {
    message Image {
        // Dimensions of the image.
        required int32 height = 1;
        required int32 width = 2;
        // Valid colorspace values are
        //   1 - grayscale
        //   2 - grayscale + alpha
        //   3 - RGB
        //   4 - RGBA
        //   5 - DIGITAL_YUV
        //   6 - BGRA
        required int32 colorspace = 3;
        // Image data in encoded format. Now only support the RGB.
        required bytes encoded_image = 4;
    }

    message Histogram {
      message bucket{
          // Counting number of values fallen in [left, left + width).
          // For the rightmost bucket, the range is [left, left + width].
          required double left = 1;
          required double width = 2;
          required int64 count = 3;
      }

      repeated bucket buckets = 1;
      optional int64 nan_count = 2;
      optional int64 pos_inf_count = 3;
      optional int64 neg_inf_count = 4;

      // max, min, sum will not take nan and inf into account.
      // If there is no valid value in tensor, max and min will be nan, sum will be 0.
      optional double max = 5;
      optional double min = 6;
      optional double sum = 7;

      // total number of values. including nan and inf.
      optional int64 count = 8;
    }

    message Value {
        // Tag name for the data.
        required string tag = 1;

        // Value associated with the tag.
        oneof value {
            float scalar_value = 3;
            Image image = 4;
            TensorProto tensor = 8;
            Histogram histogram = 9;
            LossLandscape loss_landscape = 10;
        }
    }

    // Set of values for the summary.
    repeated Value value = 1;
}


message Explain {
    message Inference{
        repeated float ground_truth_prob = 1;
        repeated int32 predicted_label = 2;
        repeated float predicted_prob = 3;
        repeated float ground_truth_prob_sd = 4;
        repeated float ground_truth_prob_itl95_low = 5;
        repeated float ground_truth_prob_itl95_hi = 6;
        repeated float predicted_prob_sd = 7;
        repeated float predicted_prob_itl95_low = 8;
        repeated float predicted_prob_itl95_hi = 9;
    }

    message Explanation{
        optional string explain_method = 1;
        optional int32 label = 2;
        optional string heatmap_path = 3;
    }

    message Benchmark{
        optional string benchmark_method = 1;
        optional string explain_method = 2;
        optional float total_score = 3;
        repeated float label_score = 4;
    }

    message Metadata{
        repeated string label = 1;
        repeated string explain_method = 2;
        repeated string benchmark_method = 3;
    }

    message HocLayer{
        optional float prob = 1;
        repeated int32 box = 2; // List of repeated x, y, w, h
    }

    message Hoc {
        optional int32 label = 1;
        optional string mask = 2;
        repeated HocLayer layer = 3;
    }

    optional int32 sample_id = 1;  // The Metadata and sample id must have one fill in
    optional string image_path = 2;
    repeated int32 ground_truth_label = 3;

    optional Inference inference = 4;
    repeated Explanation explanation = 5;
    repeated Benchmark benchmark = 6;

    optional Metadata metadata = 7;
    optional string status = 8;  // enum value: run, end

    repeated Hoc hoc = 9; // hierarchical occlusion counterfactual
}
