|
|
@ -13,6 +13,8 @@
|
|
|
|
// limitations under the License.
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
|
|
#include "paddle/fluid/operators/one_hot_op.h"
|
|
|
|
#include "paddle/fluid/operators/one_hot_op.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include "paddle/fluid/framework/framework.pb.h"
|
|
|
|
#include "paddle/fluid/framework/framework.pb.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
namespace paddle {
|
|
|
@ -34,15 +36,34 @@ class OneHotOp : public framework::OperatorWithKernel {
|
|
|
|
PADDLE_ENFORCE_GE(x_dims[x_dims.size() - 1], 1U,
|
|
|
|
PADDLE_ENFORCE_GE(x_dims[x_dims.size() - 1], 1U,
|
|
|
|
"Last dimension of Input(X) should be 1.");
|
|
|
|
"Last dimension of Input(X) should be 1.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int depth = ctx->Attrs().Get<int>("depth");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_GT(depth, 0, "Should provide a positive depth (%d).", depth);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
framework::DDim out_dims(x_dims);
|
|
|
|
framework::DDim out_dims(x_dims);
|
|
|
|
|
|
|
|
int depth = ctx->Attrs().Get<int>("depth");
|
|
|
|
|
|
|
|
if (ctx->HasInput("depth_tensor")) {
|
|
|
|
|
|
|
|
depth = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
out_dims[out_dims.size() - 1] = depth;
|
|
|
|
out_dims[out_dims.size() - 1] = depth;
|
|
|
|
ctx->SetOutputDim("Out", out_dims);
|
|
|
|
ctx->SetOutputDim("Out", out_dims);
|
|
|
|
ctx->ShareLoD("X", /* --> */ "Out");
|
|
|
|
ctx->ShareLoD("X", /* --> */ "Out");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
framework::OpKernelType GetExpectedKernelType(
|
|
|
|
|
|
|
|
const framework::ExecutionContext& ctx) const override {
|
|
|
|
|
|
|
|
return framework::OpKernelType(ctx.Input<Tensor>("X")->type(),
|
|
|
|
|
|
|
|
ctx.device_context());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
framework::OpKernelType GetKernelTypeForVar(
|
|
|
|
|
|
|
|
const std::string& var_name, const Tensor& tensor,
|
|
|
|
|
|
|
|
const framework::OpKernelType& expected_kernel_type) const override {
|
|
|
|
|
|
|
|
if (var_name == "depth_tensor") {
|
|
|
|
|
|
|
|
return expected_kernel_type;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return framework::OpKernelType(expected_kernel_type.data_type_,
|
|
|
|
|
|
|
|
tensor.place(), tensor.layout());
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class OneHotOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
class OneHotOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
@ -52,11 +73,15 @@ class OneHotOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
"(LoDTensor, LoDTensor<int>) Input variable with rank at least 2. "
|
|
|
|
"(LoDTensor, LoDTensor<int>) Input variable with rank at least 2. "
|
|
|
|
"The last dimension of X should be 1. Each value of X is an index "
|
|
|
|
"The last dimension of X should be 1. Each value of X is an index "
|
|
|
|
"to indicate the position.");
|
|
|
|
"to indicate the position.");
|
|
|
|
|
|
|
|
AddInput("depth_tensor", "(Tensor, Tensor<int>), Length of one-hot vector")
|
|
|
|
|
|
|
|
.AsDispensable();
|
|
|
|
AddOutput("Out",
|
|
|
|
AddOutput("Out",
|
|
|
|
"(Tensor, Tensor<float>) Output tensor with same rank as X. "
|
|
|
|
"(Tensor, Tensor<float>) Output tensor with same rank as X. "
|
|
|
|
"The tensor consists of one-hot representations of values in X.");
|
|
|
|
"The tensor consists of one-hot representations of values in X.");
|
|
|
|
|
|
|
|
|
|
|
|
AddAttr<int>("depth",
|
|
|
|
AddAttr<int>("depth",
|
|
|
|
"A positive integer to specify the length of one-hot vector.");
|
|
|
|
"A positive integer to specify the length of one-hot vector.")
|
|
|
|
|
|
|
|
.SetDefault(-1);
|
|
|
|
AddAttr<int>("dtype",
|
|
|
|
AddAttr<int>("dtype",
|
|
|
|
"An integer to specify the data type of one-hot "
|
|
|
|
"An integer to specify the data type of one-hot "
|
|
|
|
"vector. The default value is FP32.")
|
|
|
|
"vector. The default value is FP32.")
|
|
|
|