forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The definition of compiler (PaddlePaddle#28)
* the definition of compiler * remove gpu compiler * fix cmake * add annotation * using kernel executor replace sschedule wrapper * update kernel executor * Aadd annotation * fix review * update kernel executable * Using const note::Module& replace note::Module* * fix review * fix review * add todo * add annotation * using const note::Module*
- Loading branch information
1 parent
1755450
commit 325faf4
Showing
13 changed files
with
359 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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/kernel_executable.h" | ||
|
||
namespace paddle { | ||
namespace piano { | ||
|
||
namespace note { | ||
class Module; | ||
} | ||
|
||
namespace backends { | ||
|
||
// Compiler is an abstract class for compilation on a particular platform. | ||
// | ||
// Compiler ties together note::instruction and codegen (CG) to generate | ||
// efficient binary code for the target platform. | ||
// | ||
// XXCompiler class for a particular device inherit Compiler and | ||
// overwrite the function Apply. | ||
|
||
class Compiler { | ||
public: | ||
Compiler() = default; | ||
virtual ~Compiler() {} | ||
|
||
// Compiler will optimize the note::Module with pass and the note::Module will | ||
// be updated. | ||
virtual KernelExecutableMap Apply(note::Module*) = 0; | ||
}; | ||
|
||
} // namespace backends | ||
} // namespace piano | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <memory> | ||
#include <string> | ||
#include <unordered_map> | ||
#include "paddle/fluid/compiler/piano/note/instruction.h" | ||
#include "paddle/fluid/compiler/piano/note/opcode.h" | ||
|
||
namespace paddle { | ||
namespace piano { | ||
namespace backends { | ||
|
||
// kernel type. | ||
enum class KernelType : std::uint32_t { | ||
kBatchNormGradKernel = 0, | ||
kBatchNormInferenceKernel, | ||
kBatchNormTrainingKernel, | ||
kConvolutionKernel, | ||
kDotKernel, | ||
kJitKernel, | ||
}; | ||
|
||
// executable context for KernelExecutable. | ||
struct ExecutableContext {}; | ||
|
||
// KernelExecutable is a kernel execution class, it includes kernel information. | ||
// Each KernelType need define a derived class which inherit KernelExecutable | ||
// and overwrite the virtual function Run. | ||
// By call function Run in KernelExecutor to execute the binary code. | ||
|
||
class KernelExecutable { | ||
public: | ||
explicit KernelExecutable(const note::Instruction& note_instruction) { | ||
Reset(note_instruction); | ||
} | ||
virtual ~KernelExecutable(); | ||
virtual void Run(const ExecutableContext&) = 0; | ||
|
||
public: | ||
void Reset(const note::Instruction& note_instruction) { | ||
kernel_name_ = note_instruction.name(); | ||
// initialize KernelType | ||
switch (note_instruction.opcode()) { | ||
case note::OpCode::kBatchNormGrad: | ||
kernel_type_ = KernelType::kBatchNormGradKernel; | ||
break; | ||
case note::OpCode::kBatchNormInference: | ||
kernel_type_ = KernelType::kBatchNormInferenceKernel; | ||
break; | ||
case note::OpCode::kBatchNormTraining: | ||
kernel_type_ = KernelType::kBatchNormTrainingKernel; | ||
break; | ||
case note::OpCode::kConvolution: | ||
kernel_type_ = KernelType::kConvolutionKernel; | ||
break; | ||
case note::OpCode::kDot: | ||
kernel_type_ = KernelType::kDotKernel; | ||
break; | ||
default: | ||
kernel_type_ = KernelType::kJitKernel; | ||
break; | ||
} | ||
|
||
// get op input global_id and name | ||
for (auto operand : note_instruction.operands()) { | ||
input_names_.emplace_back(operand->name()); | ||
} | ||
} | ||
|
||
KernelType GetKernelType() const { return kernel_type_; } | ||
std::string GetKernelName() const { return kernel_name_; } | ||
const std::vector<std::string>& GetInputNames() const { return input_names_; } | ||
|
||
protected: | ||
KernelType kernel_type_; | ||
std::string kernel_name_; | ||
std::vector<std::string> input_names_; | ||
}; | ||
|
||
// KernelExecutableMap is a map of KernelExecutable. | ||
using KernelExecutableMap = | ||
std::unordered_map<std::string, std::unique_ptr<KernelExecutable>>; | ||
|
||
} // namespace backends | ||
} // namespace piano | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
paddle/fluid/compiler/piano/backends/llvm_ir/llvm_compiler.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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/llvm_compiler.h" | ||
|
||
namespace paddle { | ||
namespace piano { | ||
namespace backends { | ||
|
||
KernelExecutableMap LlvmCompiler::Apply(note::Module* note_module) { | ||
// using pass optimize the note module | ||
Optimize(note_module); | ||
|
||
// create llvm module | ||
llvm::LLVMContext context; | ||
// TODO(sunli) : set llvm_module name. | ||
llvm::Module llvm_module("", context); | ||
|
||
// create kernel executor | ||
KernelExecutableMap kernel_executable_map; | ||
|
||
// conver operator to llvm ir | ||
ConvertToIr(*note_module, &llvm_module, &kernel_executable_map); | ||
|
||
// compiler llvm ir to lowring ir | ||
Compile(&llvm_module, &kernel_executable_map); | ||
|
||
return kernel_executable_map; | ||
} | ||
|
||
void LlvmCompiler::ConvertToIr(const note::Module& note_module, | ||
llvm::Module* llvm_module, | ||
KernelExecutableMap* kernel_executable_map) {} | ||
|
||
} // namespace backends | ||
} // namespace piano | ||
} // namespace paddle |
51 changes: 51 additions & 0 deletions
51
paddle/fluid/compiler/piano/backends/llvm_ir/llvm_compiler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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/compiler.h" | ||
|
||
namespace paddle { | ||
namespace piano { | ||
namespace backends { | ||
|
||
// LlvmCompiler is an abstract compiler class that inherit Compiler with | ||
// llvm ir as low level IR. | ||
// For a particular device compiler with llvm ir should inherit the LlvmCompiler | ||
// and overwrite the function 'Optimize' and 'Compile'. | ||
|
||
class LlvmCompiler : public Compiler { | ||
public: | ||
LlvmCompiler() = default; | ||
virtual ~LlvmCompiler() {} | ||
|
||
KernelExecutableMap Apply(note::Module*) override; | ||
|
||
protected: | ||
// use pass to optimize the note::Module, such as dce、fusion、rewriter. | ||
virtual void Optimize(note::Module*) = 0; | ||
|
||
// convert each note::Instruction in note::Module to llvm ir and get execution | ||
// args. | ||
void ConvertToIr(const note::Module&, llvm::Module*, KernelExecutableMap*); | ||
|
||
// use llvm ir pass to optimize the llvm::Module and comile llvm::module to | ||
// executable binary code on tareget device. | ||
virtual void Compile(llvm::Module*, KernelExecutableMap*) = 0; | ||
}; | ||
|
||
} // namespace backends | ||
} // namespace piano | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.