| 28 | using util::GetSingleScalarInputIndexOfBinaryOp; |
| 29 | |
| 30 | ::tensorflow::Status IdentifyRelu1::Run(Model* model, std::size_t op_index, |
| 31 | bool* modified) { |
| 32 | *modified = false; |
| 33 | // Follow sequences of min+max and max+min. First get the leading op. |
| 34 | const auto op_it = model->operators.begin() + op_index; |
| 35 | const auto* op_0 = op_it->get(); |
| 36 | if (op_0->type != OperatorType::kMinimum && |
| 37 | op_0->type != OperatorType::kMaximum) { |
| 38 | return ::tensorflow::Status::OK(); |
| 39 | } |
| 40 | |
| 41 | // Get the paired op and ensure it's the counter to the first. |
| 42 | const auto* op_1 = GetOpWithInput(*model, op_0->outputs[0]); |
| 43 | if (!op_1 || |
| 44 | (op_1->type != OperatorType::kMinimum && |
| 45 | op_1->type != OperatorType::kMaximum) || |
| 46 | op_0->type == op_1->type) { |
| 47 | return ::tensorflow::Status::OK(); |
| 48 | } |
| 49 | |
| 50 | const auto* min_op = op_0->type == OperatorType::kMinimum ? op_0 : op_1; |
| 51 | const auto* max_op = op_0->type == OperatorType::kMaximum ? op_0 : op_1; |
| 52 | |
| 53 | if (min_op->inputs.size() != 2 || max_op->inputs.size() != 2) { |
| 54 | return ::tensorflow::Status::OK(); |
| 55 | } |
| 56 | if (min_op->outputs.size() != 1 || max_op->outputs.size() != 1) { |
| 57 | return ::tensorflow::Status::OK(); |
| 58 | } |
| 59 | |
| 60 | // Get the original input to the min+max pair. |
| 61 | int min_scalar_input_index = |
| 62 | GetSingleScalarInputIndexOfBinaryOp(model, min_op, 1.0f); |
| 63 | int max_scalar_input_index = |
| 64 | GetSingleScalarInputIndexOfBinaryOp(model, max_op, -1.0f); |
| 65 | if (min_scalar_input_index == -1 || max_scalar_input_index == -1) { |
| 66 | return ::tensorflow::Status::OK(); |
| 67 | } |
| 68 | int op_0_scalar_input_index = |
| 69 | op_0 == min_op ? min_scalar_input_index : max_scalar_input_index; |
| 70 | |
| 71 | // Create and emplace Relu1 node. |
| 72 | auto* relu1_op = new Relu1Operator; |
| 73 | AddMessageF("Creating %s replacing equivalent subgraph", LogName(*relu1_op)); |
| 74 | relu1_op->inputs = {op_0->inputs[!op_0_scalar_input_index]}; |
| 75 | relu1_op->outputs = op_1->outputs; |
| 76 | model->operators.emplace(op_it, relu1_op); |
| 77 | |
| 78 | DeleteOpAndArrays(model, op_0); |
| 79 | DeleteOpAndArrays(model, op_1); |
| 80 | |
| 81 | *modified = true; |
| 82 | return ::tensorflow::Status::OK(); |
| 83 | } |
| 84 | |
| 85 | } // namespace toco |
nothing calls this directly
no test coverage detected