Skip to content
Merged
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
45 changes: 9 additions & 36 deletions paddle/fluid/operators/read_file_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,21 @@
#include <string>
#include <vector>

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/phi/core/generator.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/nullary.h"

namespace paddle {
namespace operators {

template <typename T, typename DeviceContext>
class CPUReadFileKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
auto filename = ctx.Attr<std::string>("filename");

std::ifstream input(filename.c_str(),
std::ios::in | std::ios::binary | std::ios::ate);
std::streamsize file_size = input.tellg();

input.seekg(0, std::ios::beg);

auto* out = ctx.Output<phi::DenseTensor>("Out");
std::vector<int64_t> out_shape = {file_size};
out->Resize(common::make_ddim(out_shape));

uint8_t* data = out->mutable_data<T>(ctx.GetPlace());

input.read(reinterpret_cast<char*>(data), file_size);
}
};

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

void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"),
true,
platform::errors::InvalidArgument(
"Output(Out) of ReadFileOp is null."));

auto out_dims = std::vector<int>(1, -1);
ctx->SetOutputDim("Out", common::make_ddim(out_dims));
}

protected:
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
Expand All @@ -85,12 +56,14 @@ This operator read a file.

namespace ops = paddle::operators;

DECLARE_INFER_SHAPE_FUNCTOR(read_file,
ReadFileInferShapeFunctor,
PD_INFER_META(phi::ReadFileInferMeta));

REGISTER_OPERATOR(
read_file,
ops::ReadFileOp,
ops::ReadFileOpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>)

PD_REGISTER_STRUCT_KERNEL(
read_file, CPU, ALL_LAYOUT, ops::CPUReadFileKernel, uint8_t) {}
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
ReadFileInferShapeFunctor)
12 changes: 12 additions & 0 deletions paddle/phi/api/yaml/legacy_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,18 @@
data_type : dtype
backend : place

- op : read_file
args : (str filename = "", DataType dtype=DataType::UINT8, Place place=CPUPlace())
output : Tensor(out)
infer_meta :
func : ReadFileInferMeta
param : [filename]
kernel :
func : read_file
param : [filename]
data_type : dtype
backend : place

- op : remainder
args : (Tensor x, Tensor y)
output : Tensor (out)
Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/infermeta/nullary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,10 @@ void TriuIndicesInferMeta(
out->set_dims(out_dims);
out->set_dtype(dtype);
}

void ReadFileInferMeta(const std::string& filename, MetaTensor* out) {
auto out_dims = std::vector<int>(1, -1);
out->set_dims(phi::make_ddim(out_dims));
}

} // namespace phi
3 changes: 3 additions & 0 deletions paddle/phi/infermeta/nullary.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,7 @@ void TrilIndicesInferMeta(

void TriuIndicesInferMeta(
int row, int col, int offset, DataType dtype, MetaTensor* out);

void ReadFileInferMeta(const std::string& filename, MetaTensor* out);

} // namespace phi
42 changes: 42 additions & 0 deletions paddle/phi/kernels/cpu/read_file_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2023 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 <fstream>
#include <string>
#include <vector>

#include "paddle/phi/core/generator.h"
#include "paddle/phi/core/kernel_registry.h"

namespace phi {

template <typename T, typename Context>
void ReadFileKernel(const Context& dev_ctx,
const std::string& filename,
DenseTensor* out) {
std::ifstream input(filename.c_str(),
std::ios::in | std::ios::binary | std::ios::ate);
std::streamsize file_size = input.tellg();

input.seekg(0, std::ios::beg);

out->Resize({file_size});
uint8_t* data = dev_ctx.template Alloc<T>(out);
input.read(reinterpret_cast<char*>(data), file_size);
}
} // namespace phi

PD_REGISTER_KERNEL(read_file, CPU, ALL_LAYOUT, phi::ReadFileKernel, uint8_t) {
kernel->OutputAt(0).SetDataType(phi::DataType::UINT8);
}
24 changes: 24 additions & 0 deletions paddle/phi/ops/compat/read_file_sig.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2023 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/phi/core/compat/op_utils.h"

namespace phi {

KernelSignature ReadFileOpArgumentMapping(const ArgumentMappingContext& ctx) {
return KernelSignature("read_file", {}, {"filename"}, {"Out"});
}

} // namespace phi

PD_REGISTER_ARG_MAPPING_FN(read_file, phi::ReadFileOpArgumentMapping);