// This proto describes the format of the output profile file from
// the Roofline Model tool.
syntax = "proto2";

package tensorflow.profiler.roofline_model;

import "plugin/xprof/protobuf/diagnostics.proto";
import "plugin/xprof/protobuf/source_info.proto";

// The record type which describes the scope this record captures.
enum RecordType {
  INVALID_RECORD_TYPE = 0;

  // Captures the entire profiling duration including incomplete steps.
  ALL = 1;

  // Captures the average of all complete steps.
  AVERAGE_STEP = 2;

  // Captures a single step.
  PER_STEP = 3;

  // Same as ALL but the performance metrics (FLOPS and memory bandwidth) are
  // derived from the hardware performance counters.
  ALL_HW = 4;
}

// A database of RooflineModel records.
// Next ID: 17
message RooflineModelDatabase {
  // The device type.
  optional string device_type = 1;

  // Whether megacore is used.
  optional bool megacore = 12;

  // Whether the device has shared CMEM.
  optional bool has_cmem = 8;

  // Whether the device has merged VMEM.
  optional bool has_merged_vmem = 15;

  // Peak flop rate in GFLOP/s.
  optional double peak_flop_rate = 2;

  // Peak HBM bandwidth in GiB/s
  optional double peak_hbm_bw = 9;

  // Peak CMEM read bandwidth in GiB/s
  optional double peak_cmem_read_bw = 10;

  // Peak CMEM write bandwidth in GiB/s
  optional double peak_cmem_write_bw = 11;

  // Peak VMEM read bandwidth in GiB/s
  optional double peak_vmem_read_bw = 13;

  // Peak VMEM write bandwidth in GiB/s
  optional double peak_vmem_write_bw = 14;

  // All RooflineModel records, one for each HLO operation.
  repeated RooflineModelRecord roofline_model_record = 5;

  // Error and warning messages for diagnosing profiling issues.
  optional tensorflow.profiler.Diagnostics diagnostics = 7;

  // The weighted average time scale multiplier over all device op metrics.
  // It is calculated by the total time of each op if running on default pstate
  // divided by actual total op time.
  optional double time_scale_multiplier = 16;

  reserved 3, 4, 6;
}

// There is one RooflineModelRecord for each HLO operation profiled.
// Next ID: 46
message RooflineModelRecord {
  // The record type.
  optional RecordType record_type = 18;

  // Step number when record type is PER_STEP. Otherwise, invalid.
  optional uint32 step_num = 19;

  // The rank by self time
  optional uint64 rank = 1;

  // The hlo module id of the op
  optional uint64 hlo_module_id = 35;

  // The HLO category name.
  optional string hlo_category = 17;

  // The HLO operation name.
  optional string hlo_name = 2;

  // Number of occurrences of the operation.
  optional int64 occurrences = 3;

  // Total "accumulated" time in micro-seconds that the operation
  // took. If this operation has any children operations,
  // the "accumulated" time includes the time spent inside children.
  optional double total_time_in_us = 4;

  // Total time per core in micro-seconds.
  optional double total_time_per_core_in_us = 20;

  // Total time as fraction of the total program time.
  optional double total_time_in_percentage = 21;

  // Average "accumulated" time in micro-seconds that each
  // occurrence of the operation took.
  optional double avg_time_in_us = 5;

  // Total "self" time in micro-seconds that the operation took.
  // If this operation has any children operations, the "self" time
  // doesn't include the time spent inside children.
  optional double total_self_time_in_us = 6;

  // Average "self" time in micro-seconds that the operation took.
  optional double avg_self_time_in_us = 7;

  // Percentage of the total "accumulated" time that was caused by
  // DMA stall.
  optional double total_self_time_as_fraction = 8;
  optional double cumulative_total_self_time_as_fraction = 9;
  optional double dma_stall_fraction = 10;

  // Number of total floating-point operations (FLOPs) performed per second
  // normalized to the bf16 peak performance.
  optional double measured_flop_rate = 13;

  // Numbef or total floating point operations (FLOPs) performed per second for
  // the op.
  optional double model_flop_rate = 38;

  // Number of total bytes (including both read and write) accessed per
  // second.
  optional double measured_memory_bw = 14;

  // HBM bandwidth in GiB/s (including both read and write).
  optional double hbm_bw = 27;

  // CMEM read bandwidth in GiB/s.
  optional double cmem_read_bw = 28;

  // CMEM write bandwidth in GiB/s.
  optional double cmem_write_bw = 29;

  // VMEM read bandwidth in GiB/s.
  optional double vmem_read_bw = 39;

  // VMEM write bandwidth in GiB/s.
  optional double vmem_write_bw = 40;

  // Overall operational intensity in FLOP/Byte.
  optional double operational_intensity = 15;

  // Operational intensity based on HBM in FLOP/Byte.
  optional double hbm_operational_intensity = 30;

  // Operational intensity based on CMEM read in FLOP/Byte.
  optional double cmem_read_operational_intensity = 31;

  // Operational intensity based on CMEM write in FLOP/Byte.
  optional double cmem_write_operational_intensity = 32;

  // Operational intensity based on VMEM read in FLOP/Byte.
  optional double vmem_read_operational_intensity = 41;

  // Operational intensity based on VMEM write in FLOP/Byte.
  optional double vmem_write_operational_intensity = 42;

  // Operational intensity based on the bottleneck resource in FLOP/Byte.
  optional double bottleneck_operational_intensity = 33;

  // Whether this operation is "Compute", "HBM", "CMEM Read", "CMEM Write"
  // bound, according to the Roofline Model.
  optional string bound_by = 16;

  // The optimal flop rate calculated as
  // (operational intensity) * (peak memory bw)
  optional double optimal_flop_rate = 22;

  // Roofline efficiency.
  optional double roofline_efficiency = 34;

  // Percentage of measured flop rate relative to the hardware limit.
  optional double flop_rate_relative_to_hw_limit = 24;

  // Percentage of measured memory bandwidth relative to the hardware limit.
  optional double memory_bw_relative_to_hw_limit = 25;

  // Whether the record is calculated including infeed and outfeed ops.
  optional bool include_infeed_outfeed = 26;

  // Whether the device metrics (eg. flops utilization) is calculated with by
  // applying the time scale multiplier.
  optional bool apply_time_scale_multiplier = 44;

  // Deprecated. Use flops_v2 instead.
  optional uint64 flops = 36 [deprecated = true];

  // Flops for the record
  optional double flops_v2 = 45;

  // Bytes accessed for the record
  optional uint64 bytes_accessed = 37;

  // Infrmation about the corresponding source code.
  optional SourceInfo source_info = 43;

  reserved 11, 12, 23;
}
