Skip to content

Commit 0422002

Browse files
the definition of ir emitter (PaddlePaddle#20)
* the definition of ir emitter * fix test * update test * remove default constuctor * remove unused head file * add ir emitter annotation
1 parent 15c3e35 commit 0422002

File tree

10 files changed

+431
-2
lines changed

10 files changed

+431
-2
lines changed

paddle/fluid/compiler/piano/backends/llvm_ir/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ cc_library(nvptx_primitive_ir_emitter SRCS nvptx_primitive_ir_emitter.cc DEPS ll
55
cc_test(nvptx_primitive_ir_emitter_test SRCS nvptx_primitive_ir_emitter_test.cc
66
DEPS nvptx_primitive_ir_emitter gpu_primitive_ir_emitter primitive_ir_emitter)
77
target_link_libraries(nvptx_primitive_ir_emitter_test ${LLVM_LIBS})
8+
9+
cc_library(ir_emitter SRCS ir_emitter.cc DEPS llvm)
10+
cc_library(gpu_ir_emitter SRCS gpu_ir_emitter.cc DEPS llvm)
11+
cc_library(nvptx_ir_emitter SRCS nvptx_ir_emitter.cc DEPS llvm)
12+
13+
cc_test(nvptx_ir_emitter_test SRCS nvptx_ir_emitter_test.cc
14+
DEPS nvptx_ir_emitter gpu_ir_emitter ir_emitter)
15+
target_link_libraries(nvptx_ir_emitter_test ${LLVM_LIBS})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/compiler/piano/backends/llvm_ir/gpu_ir_emitter.h"
16+
17+
namespace paddle {
18+
namespace piano {
19+
namespace backends {
20+
21+
void GpuIrEmitter::VisitElementwiseUnary(const note::Instruction& instr) {}
22+
void GpuIrEmitter::VisitElementwiseBinary(const note::Instruction& instr) {}
23+
24+
// Scalar op
25+
void GpuIrEmitter::VisitConstant(const note::Instruction& instr) {}
26+
27+
// Unary
28+
void GpuIrEmitter::VisitBroadcast(const note::Instruction& instr) {}
29+
void GpuIrEmitter::VisitCopy(const note::Instruction& instr) {}
30+
void GpuIrEmitter::VisitReshape(const note::Instruction& instr) {}
31+
void GpuIrEmitter::VisitReverse(const note::Instruction& instr) {}
32+
void GpuIrEmitter::VisitSlice(const note::Instruction& instr) {}
33+
void GpuIrEmitter::VisitTranspose(const note::Instruction& instr) {}
34+
35+
// Other
36+
void GpuIrEmitter::VisitSelect(const note::Instruction& instr) {}
37+
void GpuIrEmitter::VisitConcatenate(const note::Instruction& instr) {}
38+
void GpuIrEmitter::VisitReduce(const note::Instruction& instr) {}
39+
void GpuIrEmitter::VisitRng(const note::Instruction& instr) {}
40+
void GpuIrEmitter::VisitSort(const note::Instruction& instr) {}
41+
void GpuIrEmitter::VisitTuple(const note::Instruction& instr) {}
42+
43+
} // namespace backends
44+
} // namespace piano
45+
} // namespace paddle
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#include "paddle/fluid/compiler/piano/backends/llvm_ir/ir_emitter.h"
17+
18+
namespace paddle {
19+
namespace piano {
20+
namespace backends {
21+
22+
// GpuIrEmitter is for translating note::Instruction to llvm IR
23+
// for GPU.
24+
// It implement the visit function for note::Instruction's translation。
25+
26+
class GpuIrEmitter : public IrEmitter {
27+
public:
28+
GpuIrEmitter() = delete;
29+
explicit GpuIrEmitter(llvm::Module* llvm_module, Schedules* schedule)
30+
: IrEmitter(llvm_module, schedule) {}
31+
virtual ~GpuIrEmitter() {}
32+
33+
void VisitElementwiseUnary(const note::Instruction&) override;
34+
void VisitElementwiseBinary(const note::Instruction&) override;
35+
36+
// Scalar op
37+
void VisitConstant(const note::Instruction&) override;
38+
39+
// ops can be replaced by library
40+
virtual void VisitBatchNormGrad(const note::Instruction&) = 0;
41+
virtual void VisitBatchNormInference(const note::Instruction&) = 0;
42+
virtual void VisitBatchNormTraining(const note::Instruction&) = 0;
43+
virtual void VisitConvolution(const note::Instruction&) = 0;
44+
virtual void VisitDot(const note::Instruction&) = 0;
45+
46+
// Unary
47+
void VisitBroadcast(const note::Instruction&) override;
48+
void VisitCopy(const note::Instruction&) override;
49+
void VisitReshape(const note::Instruction&) override;
50+
void VisitReverse(const note::Instruction&) override;
51+
void VisitSlice(const note::Instruction&) override;
52+
void VisitTranspose(const note::Instruction&) override;
53+
54+
// Other
55+
void VisitSelect(const note::Instruction&) override;
56+
void VisitConcatenate(const note::Instruction&) override;
57+
void VisitReduce(const note::Instruction&) override;
58+
void VisitRng(const note::Instruction&) override;
59+
void VisitSort(const note::Instruction&) override;
60+
void VisitTuple(const note::Instruction&) override;
61+
};
62+
63+
} // namespace backends
64+
} // namespace piano
65+
} // namespace paddle
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/compiler/piano/backends/llvm_ir/ir_emitter.h"
16+
17+
namespace paddle {
18+
namespace piano {
19+
namespace backends {
20+
21+
void IrEmitter::VisitCast(const note::Instruction& instr) {
22+
VisitElementwiseUnary(instr);
23+
}
24+
void IrEmitter::VisitExp(const note::Instruction& instr) {
25+
VisitElementwiseUnary(instr);
26+
}
27+
void IrEmitter::VisitLog(const note::Instruction& instr) {
28+
VisitElementwiseUnary(instr);
29+
}
30+
void IrEmitter::VisitNegative(const note::Instruction& instr) {
31+
VisitElementwiseUnary(instr);
32+
}
33+
void IrEmitter::VisitNot(const note::Instruction& instr) {
34+
VisitElementwiseUnary(instr);
35+
}
36+
void IrEmitter::VisitRsqrt(const note::Instruction& instr) {
37+
VisitElementwiseUnary(instr);
38+
}
39+
void IrEmitter::VisitSqrt(const note::Instruction& instr) {
40+
VisitElementwiseUnary(instr);
41+
}
42+
43+
void IrEmitter::VisitAdd(const note::Instruction& instr) {
44+
VisitElementwiseBinary(instr);
45+
}
46+
void IrEmitter::VisitAnd(const note::Instruction& instr) {
47+
VisitElementwiseBinary(instr);
48+
}
49+
void IrEmitter::VisitCompare(const note::Instruction& instr) {
50+
VisitElementwiseBinary(instr);
51+
}
52+
void IrEmitter::VisitDivide(const note::Instruction& instr) {
53+
VisitElementwiseBinary(instr);
54+
}
55+
void IrEmitter::VisitMaximum(const note::Instruction& instr) {
56+
VisitElementwiseBinary(instr);
57+
}
58+
void IrEmitter::VisitMinimum(const note::Instruction& instr) {
59+
VisitElementwiseBinary(instr);
60+
}
61+
void IrEmitter::VisitMultiply(const note::Instruction& instr) {
62+
VisitElementwiseBinary(instr);
63+
}
64+
void IrEmitter::VisitOr(const note::Instruction& instr) {
65+
VisitElementwiseBinary(instr);
66+
}
67+
void IrEmitter::VisitSubtract(const note::Instruction& instr) {
68+
VisitElementwiseBinary(instr);
69+
}
70+
void IrEmitter::VisitXor(const note::Instruction& instr) {
71+
VisitElementwiseBinary(instr);
72+
}
73+
74+
} // namespace backends
75+
} // namespace piano
76+
} // namespace paddle
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#include "llvm/IR/Module.h"
17+
#include "paddle/fluid/compiler/piano/backends/note_visitor_base.h"
18+
#include "paddle/fluid/compiler/piano/backends/schedule_wrapper.h"
19+
20+
namespace paddle {
21+
namespace piano {
22+
namespace backends {
23+
24+
// IrEmitter is an Abstract base class for translating note::Instruction to llvm
25+
// IR.
26+
// To translating note::Instruction to llvm IR for special device, it should
27+
// inherit from IrEmitter and overwrite the virtual function.
28+
// note::Instruction has different type {Scalar Op, Api Op, Unary Op, Binary Op,
29+
// Others}
30+
// Elementwise-Unary Op and Elementwise-Binary Op should be implemented
31+
// in VisitElementwiseUnary and VisitElementwiseBinary.
32+
33+
class IrEmitter : public NoteVisitorBase {
34+
public:
35+
IrEmitter() = delete;
36+
explicit IrEmitter(llvm::Module* llvm_module, Schedules* schedule)
37+
: llvm_module_(llvm_module), schedules_(schedule) {}
38+
virtual ~IrEmitter() {}
39+
40+
// ElementwiseUnary Operator
41+
virtual void VisitElementwiseUnary(const note::Instruction&) = 0;
42+
// ElementwiseBinary Operator
43+
virtual void VisitElementwiseBinary(const note::Instruction&) = 0;
44+
45+
// Scalar op
46+
virtual void VisitConstant(const note::Instruction&) = 0;
47+
48+
// ops can be replaced by library
49+
virtual void VisitBatchNormGrad(const note::Instruction&) = 0;
50+
virtual void VisitBatchNormInference(const note::Instruction&) = 0;
51+
virtual void VisitBatchNormTraining(const note::Instruction&) = 0;
52+
virtual void VisitConvolution(const note::Instruction&) = 0;
53+
virtual void VisitDot(const note::Instruction&) = 0;
54+
55+
// Unary
56+
virtual void VisitBroadcast(const note::Instruction&) = 0;
57+
virtual void VisitCopy(const note::Instruction&) = 0;
58+
virtual void VisitReshape(const note::Instruction&) = 0;
59+
virtual void VisitReverse(const note::Instruction&) = 0;
60+
virtual void VisitSlice(const note::Instruction&) = 0;
61+
virtual void VisitTranspose(const note::Instruction&) = 0;
62+
63+
// Unary Compute
64+
void VisitCast(const note::Instruction&) override;
65+
void VisitExp(const note::Instruction&) override;
66+
void VisitLog(const note::Instruction&) override;
67+
void VisitNegative(const note::Instruction&) override;
68+
void VisitNot(const note::Instruction&) override;
69+
void VisitRsqrt(const note::Instruction&) override;
70+
void VisitSqrt(const note::Instruction&) override;
71+
72+
// Binary
73+
void VisitAdd(const note::Instruction&) override;
74+
void VisitAnd(const note::Instruction&) override;
75+
void VisitCompare(const note::Instruction&) override;
76+
void VisitDivide(const note::Instruction&) override;
77+
void VisitMaximum(const note::Instruction&) override;
78+
void VisitMinimum(const note::Instruction&) override;
79+
void VisitMultiply(const note::Instruction&) override;
80+
void VisitOr(const note::Instruction&) override;
81+
void VisitSubtract(const note::Instruction&) override;
82+
void VisitXor(const note::Instruction&) override;
83+
84+
// Other
85+
virtual void VisitSelect(const note::Instruction&) = 0;
86+
virtual void VisitConcatenate(const note::Instruction&) = 0;
87+
virtual void VisitReduce(const note::Instruction&) = 0;
88+
virtual void VisitRng(const note::Instruction&) = 0;
89+
virtual void VisitSort(const note::Instruction&) = 0;
90+
virtual void VisitTuple(const note::Instruction&) = 0;
91+
92+
protected:
93+
llvm::Module* llvm_module_{nullptr};
94+
Schedules* schedules_{nullptr};
95+
};
96+
97+
} // namespace backends
98+
} // namespace piano
99+
} // namespace paddle
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/compiler/piano/backends/llvm_ir/nvptx_ir_emitter.h"
16+
17+
namespace paddle {
18+
namespace piano {
19+
namespace backends {
20+
21+
void NvptxIrEmitter::VisitBatchNormGrad(const note::Instruction& instr) {}
22+
void NvptxIrEmitter::VisitBatchNormInference(const note::Instruction& instr) {}
23+
void NvptxIrEmitter::VisitBatchNormTraining(const note::Instruction& instr) {}
24+
void NvptxIrEmitter::VisitConvolution(const note::Instruction& instr) {}
25+
void NvptxIrEmitter::VisitDot(const note::Instruction& instr) {}
26+
27+
} // namespace backends
28+
} // namespace piano
29+
} // namespace paddle
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#include "paddle/fluid/compiler/piano/backends/llvm_ir/gpu_ir_emitter.h"
17+
18+
namespace paddle {
19+
namespace piano {
20+
namespace backends {
21+
22+
// NvptxIrEmitter is used for note::Instruction's implementation with cudnn and
23+
// cublas.
24+
25+
class NvptxIrEmitter : public GpuIrEmitter {
26+
public:
27+
NvptxIrEmitter() = delete;
28+
explicit NvptxIrEmitter(llvm::Module* llvm_module, Schedules* schedule)
29+
: GpuIrEmitter(llvm_module, schedule) {}
30+
~NvptxIrEmitter() {}
31+
32+
void VisitBatchNormGrad(const note::Instruction&) override;
33+
void VisitBatchNormInference(const note::Instruction&) override;
34+
void VisitBatchNormTraining(const note::Instruction&) override;
35+
void VisitConvolution(const note::Instruction&) override;
36+
void VisitDot(const note::Instruction&) override;
37+
};
38+
39+
} // namespace backends
40+
} // namespace piano
41+
} // namespace paddle

0 commit comments

Comments
 (0)