Skip to content

Commit 86fd6b6

Browse files
committed
add gpu kernel by copying inputs/outputs between cpu and gpu.
1 parent cca383c commit 86fd6b6

File tree

5 files changed

+295
-68
lines changed

5 files changed

+295
-68
lines changed

paddle/framework/operator.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const Tensor* GetTensorFromVar(const Variable* var) {
3838
return &var->Get<LoDTensor>();
3939
}
4040
PADDLE_ENFORCE(var->IsType<Tensor>(),
41-
"The Input must be LoDTensor or Tensor.");
41+
"The Input must be a LoDTensor or a Tensor.");
4242
return &var->Get<Tensor>();
4343
}
4444

@@ -47,39 +47,39 @@ Tensor* GetTensorFromVar(Variable* var) {
4747
return var->GetMutable<LoDTensor>();
4848
}
4949
PADDLE_ENFORCE(var->IsType<Tensor>(),
50-
"The Input must be LoDTensor or Tensor.");
50+
"The Input must be a LoDTensor or a Tensor.");
5151
return var->GetMutable<Tensor>();
5252
}
5353

5454
std::string OperatorBase::Input(const std::string& name) const {
5555
auto& ins = Inputs(name);
5656
PADDLE_ENFORCE_LE(ins.size(), 1UL,
57-
"Op %s input %s should contain only one variable", type_,
58-
name);
57+
"Operator %s's input %s should contain only one variable.",
58+
type_, name);
5959
return ins.empty() ? kEmptyVarName : ins[0];
6060
}
6161

6262
const std::vector<std::string>& OperatorBase::Inputs(
6363
const std::string& name) const {
6464
auto it = inputs_.find(name);
65-
PADDLE_ENFORCE(it != inputs_.end(), "Op %s do not have input %s", type_,
66-
name);
65+
PADDLE_ENFORCE(it != inputs_.end(), "Operator %s does not have the input %s.",
66+
type_, name);
6767
return it->second;
6868
}
6969

7070
std::string OperatorBase::Output(const std::string& name) const {
7171
auto& outs = Outputs(name);
7272
PADDLE_ENFORCE_LE(outs.size(), 1UL,
73-
"Op %s output %s should contain only one variable", type_,
74-
name);
73+
"Operator %s's output %s should contain only one variable.",
74+
type_, name);
7575
return outs.empty() ? kEmptyVarName : outs[0];
7676
}
7777

7878
const std::vector<std::string>& OperatorBase::Outputs(
7979
const std::string& name) const {
8080
auto it = outputs_.find(name);
81-
PADDLE_ENFORCE(it != outputs_.end(), "Op %s does not have output called %s",
82-
type_, name);
81+
PADDLE_ENFORCE(it != outputs_.end(),
82+
"Operator %s does not have an output called %s.", type_, name);
8383
return it->second;
8484
}
8585

paddle/framework/tensor_impl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ inline void* Tensor::mutable_data(platform::Place place, std::type_index type) {
108108
if (holder_ != nullptr) {
109109
holder_->set_type(type);
110110
}
111-
PADDLE_ENFORCE_GT(numel(), 0,
112-
"Tensor's numel must be larger than zero to call "
113-
"Tensor::mutable_data. Call Tensor::set_dim first.");
111+
PADDLE_ENFORCE_GT(
112+
numel(), 0,
113+
"When calling this method, the Tensor's numel must be larger than zero. "
114+
"Please check Tensor::Resize has been called first.");
114115
int64_t size = numel() * SizeOfType(type);
115116
/* some versions of boost::variant don't have operator!= */
116117
if (holder_ == nullptr || !(holder_->place() == place) ||

paddle/operators/linear_chain_crf_op.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ class LinearChainCRFGradOp : public framework::OperatorWithKernel {
204204
PADDLE_ENFORCE(emission_exps_dims[0],
205205
"An empty mini-batch is not allowed.");
206206

207-
auto transition_exps_dims =
208-
ctx->GetInputDim(framework::GradVarName("TransitionExps"));
207+
auto transition_exps_dims = ctx->GetInputDim("TransitionExps");
209208
PADDLE_ENFORCE_EQ(transition_exps_dims.size(), 2UL,
210209
"The Input(TransitionExps) should be a 2-D tensor.");
211210
PADDLE_ENFORCE_EQ(
@@ -240,7 +239,8 @@ class LinearChainCRFGradOp : public framework::OperatorWithKernel {
240239
// operator is determined by its input: graidents of LogLikelihood.
241240
framework::DataType IndicateDataType(
242241
const framework::ExecutionContext& ctx) const override {
243-
return framework::ToDataType(ctx.Input<LoDTensor>("LogLikelihood")->type());
242+
return framework::ToDataType(
243+
ctx.Input<LoDTensor>(framework::GradVarName("LogLikelihood"))->type());
244244
}
245245
};
246246

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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/operators/linear_chain_crf_op.h"
16+
17+
namespace ops = paddle::operators;
18+
19+
REGISTER_OP_GPU_KERNEL(
20+
linear_chain_crf,
21+
ops::LinearChainCRFOpKernel<paddle::platform::GPUPlace, float>,
22+
ops::LinearChainCRFOpKernel<paddle::platform::GPUPlace, double>);
23+
REGISTER_OP_GPU_KERNEL(
24+
linear_chain_crf_grad,
25+
ops::LinearChainCRFGradOpKernel<paddle::platform::GPUPlace, float>,
26+
ops::LinearChainCRFGradOpKernel<paddle::platform::GPUPlace, double>);

0 commit comments

Comments
 (0)