Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paddle/framework/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ class OperatorWithKernel : public OperatorBase {
: OperatorBase(type, inputs, outputs, attrs) {}

void Run(const Scope& scope,
const platform::DeviceContext& dev_ctx) const final {
const platform::DeviceContext& dev_ctx) const override {
RuntimeInferShapeContext infer_shape_ctx(*this, scope);
this->InferShape(&infer_shape_ctx);

Expand Down
5 changes: 4 additions & 1 deletion paddle/framework/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ class Tensor {
return holder_->place();
}

std::type_index type() const { return holder_->type(); }
std::type_index type() const {
PADDLE_ENFORCE_NOT_NULL(holder_, "Tensor type() must contains holder");
return holder_->type();
}

private:
template <typename T>
Expand Down
9 changes: 5 additions & 4 deletions paddle/operators/fill_constant_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/operators/fill_constant_op.h"
#include "paddle/operators/run_once_op.h"

namespace paddle {
namespace operators {

class FillConstantOp : public framework::OperatorWithKernel {
class FillConstantOp : public RunOnceOp {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
using RunOnceOp::RunOnceOp;

protected:
void InferShape(framework::InferShapeContext *ctx) const override {
Expand All @@ -39,11 +40,11 @@ class FillConstantOp : public framework::OperatorWithKernel {
}
};

class FillConstantOpMaker : public framework::OpProtoAndCheckerMaker {
class FillConstantOpMaker : public RunOnceOpInfoMaker {
public:
FillConstantOpMaker(framework::OpProto *proto,
framework::OpAttrChecker *op_checker)
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
: RunOnceOpInfoMaker(proto, op_checker) {
AddAttr<int>("dataType",
"(int, default 5 (FP32)) "
"Output data type")
Expand Down
9 changes: 5 additions & 4 deletions paddle/operators/gaussian_random_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <random>
#include "paddle/framework/op_registry.h"
#include "paddle/operators/run_once_op.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -38,9 +39,9 @@ class CPUGaussianRandomKernel : public framework::OpKernel<T> {
}
};

class GaussianRandomOp : public framework::OperatorWithKernel {
class GaussianRandomOp : public RunOnceOp {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
using RunOnceOp::RunOnceOp;

protected:
void InferShape(framework::InferShapeContext* ctx) const override {
Expand All @@ -63,11 +64,11 @@ class GaussianRandomOp : public framework::OperatorWithKernel {
}
};

class GaussianRandomOpMaker : public framework::OpProtoAndCheckerMaker {
class GaussianRandomOpMaker : public RunOnceOpInfoMaker {
public:
GaussianRandomOpMaker(framework::OpProto* proto,
framework::OpAttrChecker* op_checker)
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
: RunOnceOpInfoMaker(proto, op_checker) {
AddOutput("Out", "output matrix of random op");
AddComment(R"DOC(
GaussianRandom operator.
Expand Down
51 changes: 51 additions & 0 deletions paddle/operators/run_once_op.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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/framework/operator.h"

namespace paddle {
namespace operators {

class RunOnceOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void Run(const framework::Scope& scope,
const platform::DeviceContext& dev_ctx) const override {
bool run_once = Attr<bool>("run_once");
if (run_once && has_run_) {
return;
}
framework::OperatorWithKernel::Run(scope, dev_ctx);
has_run_ = true;
}

private:
mutable bool has_run_{false};
};

class RunOnceOpInfoMaker : public framework::OpProtoAndCheckerMaker {
public:
RunOnceOpInfoMaker(framework::OpProto* proto,
framework::OpAttrChecker* op_checker)
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
AddAttr<bool>("run_once",
"whether that operator run only once or run multiple times")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the basic class has been named RunOnce, of course, it runs only once. How should we have such a flag that is conflicting with the class name?

.SetDefault(false);
}
};

} // namespace operators
} // namespace paddle
10 changes: 5 additions & 5 deletions paddle/operators/uniform_random_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <random>
#include <type_traits>
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
#include "paddle/operators/run_once_op.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -42,9 +42,9 @@ class CPUUniformRandomKernel : public framework::OpKernel<T> {
}
};

class UniformRandomOp : public framework::OperatorWithKernel {
class UniformRandomOp : public RunOnceOp {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
using RunOnceOp::RunOnceOp;

protected:
void InferShape(framework::InferShapeContext* ctx) const override {
Expand All @@ -69,11 +69,11 @@ class UniformRandomOp : public framework::OperatorWithKernel {
}
};

class UniformRandomOpMaker : public framework::OpProtoAndCheckerMaker {
class UniformRandomOpMaker : public RunOnceOpInfoMaker {
public:
UniformRandomOpMaker(framework::OpProto* proto,
framework::OpAttrChecker* op_checker)
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
: RunOnceOpInfoMaker(proto, op_checker) {
AddOutput("Out", "The output tensor of uniform random op");
AddComment(R"DOC(Uniform random operator.
Used to initialize tensor with uniform random generator.
Expand Down