Skip to content

Commit

Permalink
the definition of ir emitter (PaddlePaddle#20)
Browse files Browse the repository at this point in the history
* the definition of ir emitter

* fix test

* update test

* remove default constuctor

* remove unused head file

* add ir emitter annotation
  • Loading branch information
SunNy820828449 authored Aug 16, 2021
1 parent 15c3e35 commit 0422002
Show file tree
Hide file tree
Showing 10 changed files with 431 additions and 2 deletions.
8 changes: 8 additions & 0 deletions paddle/fluid/compiler/piano/backends/llvm_ir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ cc_library(nvptx_primitive_ir_emitter SRCS nvptx_primitive_ir_emitter.cc DEPS ll
cc_test(nvptx_primitive_ir_emitter_test SRCS nvptx_primitive_ir_emitter_test.cc
DEPS nvptx_primitive_ir_emitter gpu_primitive_ir_emitter primitive_ir_emitter)
target_link_libraries(nvptx_primitive_ir_emitter_test ${LLVM_LIBS})

cc_library(ir_emitter SRCS ir_emitter.cc DEPS llvm)
cc_library(gpu_ir_emitter SRCS gpu_ir_emitter.cc DEPS llvm)
cc_library(nvptx_ir_emitter SRCS nvptx_ir_emitter.cc DEPS llvm)

cc_test(nvptx_ir_emitter_test SRCS nvptx_ir_emitter_test.cc
DEPS nvptx_ir_emitter gpu_ir_emitter ir_emitter)
target_link_libraries(nvptx_ir_emitter_test ${LLVM_LIBS})
45 changes: 45 additions & 0 deletions paddle/fluid/compiler/piano/backends/llvm_ir/gpu_ir_emitter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.

#include "paddle/fluid/compiler/piano/backends/llvm_ir/gpu_ir_emitter.h"

namespace paddle {
namespace piano {
namespace backends {

void GpuIrEmitter::VisitElementwiseUnary(const note::Instruction& instr) {}
void GpuIrEmitter::VisitElementwiseBinary(const note::Instruction& instr) {}

// Scalar op
void GpuIrEmitter::VisitConstant(const note::Instruction& instr) {}

// Unary
void GpuIrEmitter::VisitBroadcast(const note::Instruction& instr) {}
void GpuIrEmitter::VisitCopy(const note::Instruction& instr) {}
void GpuIrEmitter::VisitReshape(const note::Instruction& instr) {}
void GpuIrEmitter::VisitReverse(const note::Instruction& instr) {}
void GpuIrEmitter::VisitSlice(const note::Instruction& instr) {}
void GpuIrEmitter::VisitTranspose(const note::Instruction& instr) {}

// Other
void GpuIrEmitter::VisitSelect(const note::Instruction& instr) {}
void GpuIrEmitter::VisitConcatenate(const note::Instruction& instr) {}
void GpuIrEmitter::VisitReduce(const note::Instruction& instr) {}
void GpuIrEmitter::VisitRng(const note::Instruction& instr) {}
void GpuIrEmitter::VisitSort(const note::Instruction& instr) {}
void GpuIrEmitter::VisitTuple(const note::Instruction& instr) {}

} // namespace backends
} // namespace piano
} // namespace paddle
65 changes: 65 additions & 0 deletions paddle/fluid/compiler/piano/backends/llvm_ir/gpu_ir_emitter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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.
#pragma once

#include "paddle/fluid/compiler/piano/backends/llvm_ir/ir_emitter.h"

namespace paddle {
namespace piano {
namespace backends {

// GpuIrEmitter is for translating note::Instruction to llvm IR
// for GPU.
// It implement the visit function for note::Instruction's translation。

class GpuIrEmitter : public IrEmitter {
public:
GpuIrEmitter() = delete;
explicit GpuIrEmitter(llvm::Module* llvm_module, Schedules* schedule)
: IrEmitter(llvm_module, schedule) {}
virtual ~GpuIrEmitter() {}

void VisitElementwiseUnary(const note::Instruction&) override;
void VisitElementwiseBinary(const note::Instruction&) override;

// Scalar op
void VisitConstant(const note::Instruction&) override;

// ops can be replaced by library
virtual void VisitBatchNormGrad(const note::Instruction&) = 0;
virtual void VisitBatchNormInference(const note::Instruction&) = 0;
virtual void VisitBatchNormTraining(const note::Instruction&) = 0;
virtual void VisitConvolution(const note::Instruction&) = 0;
virtual void VisitDot(const note::Instruction&) = 0;

// Unary
void VisitBroadcast(const note::Instruction&) override;
void VisitCopy(const note::Instruction&) override;
void VisitReshape(const note::Instruction&) override;
void VisitReverse(const note::Instruction&) override;
void VisitSlice(const note::Instruction&) override;
void VisitTranspose(const note::Instruction&) override;

// Other
void VisitSelect(const note::Instruction&) override;
void VisitConcatenate(const note::Instruction&) override;
void VisitReduce(const note::Instruction&) override;
void VisitRng(const note::Instruction&) override;
void VisitSort(const note::Instruction&) override;
void VisitTuple(const note::Instruction&) override;
};

} // namespace backends
} // namespace piano
} // namespace paddle
76 changes: 76 additions & 0 deletions paddle/fluid/compiler/piano/backends/llvm_ir/ir_emitter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.

#include "paddle/fluid/compiler/piano/backends/llvm_ir/ir_emitter.h"

namespace paddle {
namespace piano {
namespace backends {

void IrEmitter::VisitCast(const note::Instruction& instr) {
VisitElementwiseUnary(instr);
}
void IrEmitter::VisitExp(const note::Instruction& instr) {
VisitElementwiseUnary(instr);
}
void IrEmitter::VisitLog(const note::Instruction& instr) {
VisitElementwiseUnary(instr);
}
void IrEmitter::VisitNegative(const note::Instruction& instr) {
VisitElementwiseUnary(instr);
}
void IrEmitter::VisitNot(const note::Instruction& instr) {
VisitElementwiseUnary(instr);
}
void IrEmitter::VisitRsqrt(const note::Instruction& instr) {
VisitElementwiseUnary(instr);
}
void IrEmitter::VisitSqrt(const note::Instruction& instr) {
VisitElementwiseUnary(instr);
}

void IrEmitter::VisitAdd(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}
void IrEmitter::VisitAnd(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}
void IrEmitter::VisitCompare(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}
void IrEmitter::VisitDivide(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}
void IrEmitter::VisitMaximum(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}
void IrEmitter::VisitMinimum(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}
void IrEmitter::VisitMultiply(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}
void IrEmitter::VisitOr(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}
void IrEmitter::VisitSubtract(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}
void IrEmitter::VisitXor(const note::Instruction& instr) {
VisitElementwiseBinary(instr);
}

} // namespace backends
} // namespace piano
} // namespace paddle
99 changes: 99 additions & 0 deletions paddle/fluid/compiler/piano/backends/llvm_ir/ir_emitter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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.
#pragma once

#include "llvm/IR/Module.h"
#include "paddle/fluid/compiler/piano/backends/note_visitor_base.h"
#include "paddle/fluid/compiler/piano/backends/schedule_wrapper.h"

namespace paddle {
namespace piano {
namespace backends {

// IrEmitter is an Abstract base class for translating note::Instruction to llvm
// IR.
// To translating note::Instruction to llvm IR for special device, it should
// inherit from IrEmitter and overwrite the virtual function.
// note::Instruction has different type {Scalar Op, Api Op, Unary Op, Binary Op,
// Others}
// Elementwise-Unary Op and Elementwise-Binary Op should be implemented
// in VisitElementwiseUnary and VisitElementwiseBinary.

class IrEmitter : public NoteVisitorBase {
public:
IrEmitter() = delete;
explicit IrEmitter(llvm::Module* llvm_module, Schedules* schedule)
: llvm_module_(llvm_module), schedules_(schedule) {}
virtual ~IrEmitter() {}

// ElementwiseUnary Operator
virtual void VisitElementwiseUnary(const note::Instruction&) = 0;
// ElementwiseBinary Operator
virtual void VisitElementwiseBinary(const note::Instruction&) = 0;

// Scalar op
virtual void VisitConstant(const note::Instruction&) = 0;

// ops can be replaced by library
virtual void VisitBatchNormGrad(const note::Instruction&) = 0;
virtual void VisitBatchNormInference(const note::Instruction&) = 0;
virtual void VisitBatchNormTraining(const note::Instruction&) = 0;
virtual void VisitConvolution(const note::Instruction&) = 0;
virtual void VisitDot(const note::Instruction&) = 0;

// Unary
virtual void VisitBroadcast(const note::Instruction&) = 0;
virtual void VisitCopy(const note::Instruction&) = 0;
virtual void VisitReshape(const note::Instruction&) = 0;
virtual void VisitReverse(const note::Instruction&) = 0;
virtual void VisitSlice(const note::Instruction&) = 0;
virtual void VisitTranspose(const note::Instruction&) = 0;

// Unary Compute
void VisitCast(const note::Instruction&) override;
void VisitExp(const note::Instruction&) override;
void VisitLog(const note::Instruction&) override;
void VisitNegative(const note::Instruction&) override;
void VisitNot(const note::Instruction&) override;
void VisitRsqrt(const note::Instruction&) override;
void VisitSqrt(const note::Instruction&) override;

// Binary
void VisitAdd(const note::Instruction&) override;
void VisitAnd(const note::Instruction&) override;
void VisitCompare(const note::Instruction&) override;
void VisitDivide(const note::Instruction&) override;
void VisitMaximum(const note::Instruction&) override;
void VisitMinimum(const note::Instruction&) override;
void VisitMultiply(const note::Instruction&) override;
void VisitOr(const note::Instruction&) override;
void VisitSubtract(const note::Instruction&) override;
void VisitXor(const note::Instruction&) override;

// Other
virtual void VisitSelect(const note::Instruction&) = 0;
virtual void VisitConcatenate(const note::Instruction&) = 0;
virtual void VisitReduce(const note::Instruction&) = 0;
virtual void VisitRng(const note::Instruction&) = 0;
virtual void VisitSort(const note::Instruction&) = 0;
virtual void VisitTuple(const note::Instruction&) = 0;

protected:
llvm::Module* llvm_module_{nullptr};
Schedules* schedules_{nullptr};
};

} // namespace backends
} // namespace piano
} // namespace paddle
29 changes: 29 additions & 0 deletions paddle/fluid/compiler/piano/backends/llvm_ir/nvptx_ir_emitter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.

#include "paddle/fluid/compiler/piano/backends/llvm_ir/nvptx_ir_emitter.h"

namespace paddle {
namespace piano {
namespace backends {

void NvptxIrEmitter::VisitBatchNormGrad(const note::Instruction& instr) {}
void NvptxIrEmitter::VisitBatchNormInference(const note::Instruction& instr) {}
void NvptxIrEmitter::VisitBatchNormTraining(const note::Instruction& instr) {}
void NvptxIrEmitter::VisitConvolution(const note::Instruction& instr) {}
void NvptxIrEmitter::VisitDot(const note::Instruction& instr) {}

} // namespace backends
} // namespace piano
} // namespace paddle
41 changes: 41 additions & 0 deletions paddle/fluid/compiler/piano/backends/llvm_ir/nvptx_ir_emitter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.
#pragma once

#include "paddle/fluid/compiler/piano/backends/llvm_ir/gpu_ir_emitter.h"

namespace paddle {
namespace piano {
namespace backends {

// NvptxIrEmitter is used for note::Instruction's implementation with cudnn and
// cublas.

class NvptxIrEmitter : public GpuIrEmitter {
public:
NvptxIrEmitter() = delete;
explicit NvptxIrEmitter(llvm::Module* llvm_module, Schedules* schedule)
: GpuIrEmitter(llvm_module, schedule) {}
~NvptxIrEmitter() {}

void VisitBatchNormGrad(const note::Instruction&) override;
void VisitBatchNormInference(const note::Instruction&) override;
void VisitBatchNormTraining(const note::Instruction&) override;
void VisitConvolution(const note::Instruction&) override;
void VisitDot(const note::Instruction&) override;
};

} // namespace backends
} // namespace piano
} // namespace paddle
Loading

0 comments on commit 0422002

Please sign in to comment.