expand_zero_dim_pass.cc 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (c) 2023 CINN 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.

#include <functional>
#include <string>
#include <unordered_map>
#include <unordered_set>

20
#include "glog/logging.h"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "paddle/cinn/frontend/net_builder.h"
#include "paddle/cinn/frontend/program_pass.h"

namespace cinn {
namespace frontend {
namespace pass {

class ExpandZeroDimPass : public ProgramPass {
 public:
  using ProgramPass::ProgramPass;

 protected:
  void ApplyImpl(Program* program,
                 const std::unordered_set<std::string>& fetch_ids,
                 const common::Target& target) override {
    NetBuilder builder("expand_zero_dim_builder");
    for (int i = 0; i < program->size(); ++i) {
      auto& instr = (*program)[i];
39 40 41
      if (instr->op_type == "transpose") {
        builder.AppendInstruction(HandleTranspose(instr));
        continue;
42 43 44
      } else if (instr->op_type == "fill_constant") {
        builder.AppendInstruction(HandleFillConstant(instr));
        continue;
45
      }
46 47
      for (auto& input : instr->inputs) {
        if (input->shape.empty()) {
48 49
          VLOG(4) << "Change " << instr->op_type << "'s input 0D-Tensor "
                  << input->id << " to 1D-Tensor";
50 51 52 53 54
          input->shape.push_back(1);
        }
      }
      for (auto& output : instr->outputs) {
        if (output->shape.empty()) {
55 56
          VLOG(4) << "Change " << instr->op_type << "'s output 0D-Tensor "
                  << output->id << " to 1D-Tensor";
57 58 59 60 61
          output->shape.push_back(1);
        }
      }
      builder.AppendInstruction(instr);
    }
62 63 64 65 66 67 68 69
    for (auto var : program->GetInputs()) {
      if (var->shape.empty()) {
        VLOG(4) << "Change program's input 0D-Tensor " << var->id
                << " to 1D-Tensor";
        var->shape.push_back(1);
      }
      builder.CreateInput(var);
    }
70 71 72 73
    *program = builder.Build();
  }

  void Clear() override {}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

 private:
  // Before: out-0D = transpose(x-0D, [])
  // After:  out-1D = transpose(x-1D, [1])
  Instruction HandleTranspose(const Instruction& instr) {
    Instruction new_instr = instr;
    bool has_0d_input = false;
    for (auto& input : new_instr->inputs) {
      if (input->shape.empty()) {
        VLOG(4) << "Change transpose's input 0D-Tensor " << input->id
                << " to 1D-Tensor";
        input->shape.push_back(1);
        has_0d_input = true;
      }
    }
    for (auto& output : new_instr->outputs) {
      if (output->shape.empty()) {
        VLOG(4) << "Change transpose's output 0D-Tensor " << output->id
                << " to 1D-Tensor";
        output->shape.push_back(1);
      }
    }
    if (has_0d_input) {
      std::vector<int32_t> axis =
          new_instr.GetAttrs<std::vector<int32_t>>("axis");
      CHECK(axis.empty()) << "transpose's axis should be empty when inputs "
                             "0D-Tensor! Please check setting.\n";
      axis.push_back(0);
      VLOG(4) << "Change Transpose's attribute axis from [] to [1]";
      new_instr.SetAttr<std::vector<int32_t>>("axis", axis);
    }
    return new_instr;
  }
107 108 109 110 111 112 113 114 115 116 117 118 119 120

  // Before: out-0D = fill_constant([], 123.456, "out", "float32")
  // After:  out-1D = fill_constant([1], 123.456, "out", "float32")
  Instruction HandleFillConstant(const Instruction& instr) {
    Instruction new_instr = instr;
    std::vector<int32_t> shape =
        new_instr.GetAttrs<std::vector<int32_t>>("shape");
    if (shape.empty()) {
      shape.push_back(1);
      VLOG(4) << "Change fill_constant's attribute shape from [] to [1]";
    }
    new_instr.SetAttr<std::vector<int32_t>>("shape", shape);
    return new_instr;
  }
121 122 123 124 125 126 127
};

}  // namespace pass
}  // namespace frontend
}  // namespace cinn

CINN_REGISTER_HELPER(ExpandZeroDim) {
128 129
  CINN_REGISTER_PROGRAM_PASS(ExpandZeroDim,
                             cinn::frontend::pass::ExpandZeroDimPass);
130 131 132

  return true;
}