/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
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";

import "paddle/fluid/framework/framework.proto";
package paddle.framework.proto;

// Describes one subsitute subgraph.
message PassDesc {
  enum RoleType {
    kVariable = 0;
    kOperator = 1;
  }
  enum OperationType {
    kAdd = 0;
    kSub = 1;
    kMul = 2;
    kDiv = 3;
    kSize = 4;
    kMod = 5;
  }
  enum ConditionType {
    kEQ = 0;
    kNE = 1;
    kGT = 2;
    kGE = 3;
    kLT = 4;
    kLE = 5;
  }
  // Representation of attr in var or operator.
  message Attr {
    required RoleType role = 1;
    optional string var_name = 2;
    optional int32 op_index = 3;
    required string name = 4;
    optional string element_name = 5;
    optional int32 element_index = 6;
    optional OperationType operation = 7;
  }
  // The operation to be performed.
  message Operation {
    required OperationType type = 1;
    optional Attr attr = 2;
    optional OpDesc.Attr value = 3;
  }
  message VarMap {
    required string pattern_var = 1;
    required string replace_var = 2;
  }
  message AttrMap {
    required Attr pattern_attr = 1;
    required Attr replace_attr = 2;
    optional Operation operation = 3;
  }
  message AttrCondition {
    required Attr attr = 1;
    required ConditionType type = 2;
    optional Attr condition_attr = 3;
    optional OpDesc.Attr condition_value = 4;
    optional Operation operation = 5;
  }
  // A pair of subgraphs for matching and rewriting.
  repeated OpDesc pattern = 1;
  repeated OpDesc replace = 2;
  // Mapping vars between pattern and replace subgraphs.
  repeated VarMap var_maps = 3;
  // Mapping attrs of vars and ops between pattern and replace subgraphs.
  repeated AttrMap var_attr_maps = 4;
  repeated AttrMap op_attr_maps = 5;
  // Limit the attrs of vars and ops in pattern subgraph.
  repeated AttrCondition var_attr_conditions = 6;
  repeated AttrCondition op_attr_conditions = 7;
}

// A series of PassDesc.
message MultiPassDesc {
  optional string pass_type = 1;
  repeated PassDesc pass_descs = 2;
}
