Replace Tensor by LoDTensor in each operator.
Created by: qingqing01
The following comments are addressing three problems:
- Necessary change to LoDTensor
- Replace uses of Tensor into LoDTensor in operators who don't need LoD
- How to pass LoD from operators' inputs to outputs
LoDTensor
The definition of LoDTensor is as follows:
class LodTensor {
public:
LoDTensor() {}
void set_tensor(Tensor* tensor) { tensor_ = tensor; }
private
Tensor* tensor_;
Lod lod_;
};
The code snippet shows how we use it in operators:
void InferShape(const framework::InferShapeContext &ctx) const override {
auto tensor_x = ctx.Input<LoDTensor>("X")->tensor();
auto tensor_y = ctx.Input<LoDTensor>("Y")->tensor();
PADDLE_ENFORCE_EQ(tensor_x.dims(),
tensor_y.dims(),
"Two inputs of Add Op's dimension must be same.");
auto* lod_tensor_out = ctx.Output<LodTensor>("Out");
Tensor* out = new Tensor();
out->Resize(tensor_x->dims());
lod_tensor_out->set_tensor(out);
}
};
We see that LoDTensor::set_tensor
is kind of ugly because the caller needs to destruct the tensor at right time.
A solution is to make LoDTensor owns the tensor and delete it at destruction time:
class LodTensor {
public:
LoDTensor() : tensor_(new Tensor()) {}
~LoDTensor() { delete tensor_; }
// 删掉set_tensor接口。
// void set_tensor(Tensor* tensor) { tensor_ = tensor; }
private
Tensor* tensor_;
Lod lod_;
};