Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Add FC frontend program unittest (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
Superjomn authored Sep 7, 2020
1 parent 2a0c2f8 commit f52ef4a
Show file tree
Hide file tree
Showing 32 changed files with 379 additions and 126 deletions.
5 changes: 5 additions & 0 deletions cinn/frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ set(srcs
syntax.cc
)

if(NOT WITH_CUDA)
cc_test(test_frontend_syntax SRCS syntax_test.cc DEPS core)
else()
nv_test(test_frontend_syntax SRCS syntax_test.cc DEPS core)
endif()


add_subdirectory(paddle)

Expand Down
1 change: 1 addition & 0 deletions cinn/frontend/paddle/cpp/op_desc.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/frontend/paddle/cpp/op_desc.h"

#include <cstdint>

namespace cinn::frontend::paddle::cpp {
Expand Down
1 change: 1 addition & 0 deletions cinn/frontend/paddle/pb/block_desc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <glog/logging.h>

#include "cinn/frontend/paddle/cpp/desc_api.h"
#include "cinn/frontend/paddle/framework.pb.h"

Expand Down
1 change: 1 addition & 0 deletions cinn/frontend/paddle/pb/op_desc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <variant>

#include "cinn/frontend/paddle/cpp/op_desc.h"
#include "cinn/frontend/paddle/framework.pb.h"

Expand Down
103 changes: 74 additions & 29 deletions cinn/frontend/syntax.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "cinn/frontend/syntax.h"

#include "cinn/frontend/paddle/model_parser.h"
#include "cinn/hlir/framework/node.h"
#include "cinn/hlir/framework/op.h"
#include "cinn/utils/string.h"
Expand All @@ -14,33 +16,15 @@ void Instruction::PrepareOutputs() {
}
}

Instruction::Instruction(std::string_view op_type, Program* parent)
Instruction::Instruction(std::string_view op_type, const std::vector<Variable>& inputs, Program* parent)
: common::Shared<_Instruction_>(common::make_shared<_Instruction_>()) {
get()->op_type = op_type;
get()->parent_program = parent;
get()->inputs = inputs;
PrepareOutputs();
}

Placeholder::operator Variable() {
Variable var(id());
var->shape = shape();
var->type = type_;
return var;
}

Variable Program::add(const Variable& a, const Variable& b) {
Instruction instr("elementwise_add");
instr.SetInputs({a, b});
AddInstruction(instr);
return instr.GetOutputs()[0];
}

Variable Program::relu(const Variable& a) {
Instruction instr("relu");
instr.SetInputs({a});
AddInstruction(instr);
return instr.GetOutputs()[0];
}
Placeholder::operator Variable() const { return var_; }

std::vector<Variable> Program::conv2d(
const Variable& a,
Expand All @@ -51,7 +35,7 @@ std::vector<Variable> Program::conv2d(
for (auto& iter : attr_store) {
instr.SetAttr(iter.first, iter.second);
}
AddInstruction(instr);
AppendInstruction(instr);
return instr.GetOutputs();
}

Expand All @@ -63,18 +47,18 @@ Variable Program::batchnorm(const Variable& a,
for (auto& iter : attr_store) {
instr.SetAttr(iter.first, iter.second);
}
AddInstruction(instr);
AppendInstruction(instr);
return instr.GetOutputs()[0];
}

Instruction& Program::operator[](size_t i) {
CHECK_LT(i, instrs.size());
return instrs[i];
CHECK_LT(i, instrs_.size());
return instrs_[i];
}

const Instruction& Program::operator[](size_t i) const {
CHECK_LT(i, instrs.size());
return instrs[i];
CHECK_LT(i, instrs_.size());
return instrs_[i];
}

std::ostream& operator<<(std::ostream& os, const Variable& x) {
Expand All @@ -95,7 +79,68 @@ std::ostream& operator<<(std::ostream& os, const Instruction& instr) {
return os;
}

// Add an Instruction to a program given a Paddle-format \p op_desc.
void ProgramAddOp(Program* program, const paddle::cpp::OpDesc& op_desc) {}

void LoadPaddleProgram(const std::string& model_dir, bool is_combined) {
hlir::framework::Scope scope;
paddle::cpp::ProgramDesc program_desc;
paddle::LoadModelPb(model_dir, "__model__", "", &scope, &program_desc, is_combined);
CHECK_EQ(program_desc.BlocksSize(), 1) << "CINN can only support the model with a single block";
auto* block_desc = program_desc.GetBlock<paddle::cpp::BlockDesc>(0);
for (int i = 0; i < block_desc->OpsSize(); i++) {
auto* op_desc = block_desc->GetOp<paddle::cpp::OpDesc>(i);
}
}

void Program::SetInputs(const std::vector<Variable>& xs) {
CHECK(!xs.empty()) << "At least one input is needed for a program!";
for (int i = 0; i < xs.size(); i++) {
CHECK(!xs[i]->shape.empty()) << "Found " << i << "-th input's shape is not set yet";
CHECK(!xs[i]->type.is_unk()) << "Found " << i << "-th input's type is not set yet";
inputs_.push_back(xs[i]);
}
}

void Program::Validate() const {
CHECK(!inputs_.empty()) << "Inputs of the program is not set yet";
CHECK(!instrs_.empty()) << "No instruction is added yet";
}

Variable Program::add(const Variable& a, const Variable& b) {
Instruction instr("elementwise_add", {a, b});
AppendInstruction(instr);
return instr.GetOutput(0);
}

Variable Program::elementwise_add(const Variable& a, const Variable& b, int axis) {
Instruction instr("elementwise_add", {a, b});
instr.SetAttr("axis", axis);
AppendInstruction(instr);
return instr.GetOutput(0);
}

Variable Program::relu(const Variable& a) {
Instruction instr("relu", {a});
AppendInstruction(instr);
return instr.GetOutput(0);
}

Variable Program::relu6(const Variable& a) {
Instruction instr("relu6", {a});
AppendInstruction(instr);
return instr.GetOutput(0);
}
Variable Program::mul(
const Variable& a, const Variable& b, bool trans_a, bool trans_b, int x_num_col_dims, int y_num_col_dims) {
Instruction instr("mul", {a, b});
instr.SetAttr("trans_a", trans_a);
instr.SetAttr("trans_b", trans_b);
instr.SetAttr("x_num_col_dims", x_num_col_dims);
instr.SetAttr("y_num_col_dims", y_num_col_dims);
AppendInstruction(instr);
return instr.GetOutput(0);
}

} // namespace frontend
} // namespace cinn

// CINN_REGISTRY_ENABLE(cinn::hlir::framework::Operator);
96 changes: 64 additions & 32 deletions cinn/frontend/syntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <variant>
#include <vector>

#include "cinn/common/common.h"
#include "cinn/common/context.h"
#include "cinn/common/object.h"
#include "cinn/common/type.h"
Expand All @@ -20,34 +21,6 @@ namespace frontend {
struct Program;
struct Variable;

/**
* Placeholder is the fed slot of a computation.
*/
class Placeholder {
public:
/**
* @param type Type of the fed
* @param shape Shape of the fed
* @param id ID of the fed
*/
Placeholder(const common::Type& type, const std::vector<int>& shape, std::string_view id = "")
: type_(type), shape_(shape), id_(id.empty() ? common::Context::Global().NewName("placeholder") : id) {}

const std::vector<int>& shape() const { return shape_; }

std::string_view id() const { return id_; }

operator Variable();

Program* parent_program() { return parent_program_; }

private:
common::Type type_;
std::string id_{};
std::vector<int> shape_;
Program* parent_program_{};
};

struct _Variable_ : public common::Object {
std::string id;
common::Type type;
Expand All @@ -73,6 +46,38 @@ struct Variable : public common::Shared<_Variable_> {
const _Variable_* operator->() const { return get(); }
};

/**
* Placeholder is the fed slot of a computation.
*/
class Placeholder {
public:
/**
* @param type Type of the fed
* @param shape Shape of the fed
* @param id ID of the fed
*/
Placeholder(const common::Type& type, const std::vector<int>& shape, std::string_view id = "")
: id_(id.empty() ? common::Context::Global().NewName("placeholder") : id), var_{id} {
var_->shape = shape;
var_->type = type;
}

const std::vector<int>& shape() const { return var_->shape; }

Type type() const { return var_->type; }

std::string_view id() const { return id_; }

operator Variable() const;

Program* parent_program() { return parent_program_; }

private:
Variable var_;
std::string id_{};
Program* parent_program_{};
};

/**
* Data of a Instruction.
*/
Expand All @@ -94,14 +99,18 @@ struct _Instruction_ : public common::Object {
* Instruction is the basic computational unit of a Program, similar to the operator concept in a DNN platform.
*/
struct Instruction : public common::Shared<_Instruction_> {
explicit Instruction(std::string_view op_type, Program* parent = nullptr);
explicit Instruction(std::string_view op_type, const std::vector<Variable>& inputs = {}, Program* parent = nullptr);

/**
* Set the inputs of the instruction.
* @param vars The input variables.
*/
void SetInputs(const std::vector<Variable>& vars) { get()->inputs = vars; }
const std::vector<Variable>& GetOutputs() const { return get()->outputs; }
const Variable& GetOutput(size_t offset) const {
CHECK_LT(offset, get()->outputs.size());
return GetOutputs()[offset];
}

/**
* Set an attribute of the instruction.
Expand Down Expand Up @@ -136,6 +145,7 @@ struct Instruction : public common::Shared<_Instruction_> {
* Program is a representation of a computation.
*/
struct Program {
void SetInputs(const std::vector<Variable>& xs);
/**
* Add two variables.
*
Expand All @@ -145,6 +155,21 @@ struct Program {
*/
Variable add(const Variable& a, const Variable& b);

/**
* Multiply two matrix.
*/
Variable mul(const Variable& a,
const Variable& b,
bool trans_a = false,
bool trans_b = false,
int x_num_col_dims = -1,
int y_num_col_dims = -1);

/**
* Add two tensors element-wise.
*/
Variable elementwise_add(const Variable& a, const Variable& b, int axis = 0);

/**
* Apply Rectified Linear Unit on input Variable.
* Actually apply: outupt = max(input,0)
Expand All @@ -153,6 +178,7 @@ struct Program {
* @return The result.
*/
Variable relu(const Variable& a);
Variable relu6(const Variable& a);

/**
* The convolution2D layer calculates the output based on the input, filter
Expand Down Expand Up @@ -193,14 +219,20 @@ struct Program {
* Get number of instructions in the program.
* @return
*/
inline size_t size() const { return instrs.size(); }
inline size_t size() const { return instrs_.size(); }

void Validate() const;

private:
void AddInstruction(const Instruction& other) { instrs.push_back(other); }
void AppendInstruction(const Instruction& other) { instrs_.push_back(other); }

std::vector<Instruction> instrs_;

std::vector<Instruction> instrs;
std::vector<Variable> inputs_;
};

void LoadPaddleProgram(const std::string& model_dir, bool is_combined);

std::ostream& operator<<(std::ostream& os, const Variable& x);
std::ostream& operator<<(std::ostream& os, const Instruction& instr);

Expand Down
Loading

0 comments on commit f52ef4a

Please sign in to comment.