Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into update_crop_op
commit
24649a780d
@ -0,0 +1,164 @@
|
||||
# Copyright (c) 2018 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.
|
||||
|
||||
import os
|
||||
import random
|
||||
import paddle
|
||||
import paddle.fluid as fluid
|
||||
import paddle.fluid.core as core
|
||||
from paddle.dataset import mnist, cifar, flowers, image
|
||||
|
||||
|
||||
def convert_2_recordio(py_reader, outfilepath, batch_size, shape_data,
|
||||
shape_label):
|
||||
num_batches = 0
|
||||
with fluid.program_guard(fluid.Program(), fluid.Program()):
|
||||
reader = paddle.batch(py_reader(), batch_size=batch_size)
|
||||
feeder = fluid.DataFeeder(
|
||||
feed_list=[ # order is image and label
|
||||
fluid.layers.data(
|
||||
name='image', shape=shape_data),
|
||||
fluid.layers.data(
|
||||
name='label', shape=shape_label, dtype='int64'),
|
||||
],
|
||||
place=fluid.CPUPlace())
|
||||
num_batches = fluid.recordio_writer.convert_reader_to_recordio_file(
|
||||
outfilepath, reader, feeder)
|
||||
return num_batches
|
||||
|
||||
|
||||
def prepare_mnist(outpath, batch_size):
|
||||
outfilepath = os.path.join(outpath, "mnist.recordio")
|
||||
convert_2_recordio(mnist.train, outfilepath, batch_size, [784], [1])
|
||||
|
||||
|
||||
def prepare_cifar10(outpath, batch_size):
|
||||
outfilepath = os.path.join(outpath, "cifar.recordio")
|
||||
convert_2_recordio(cifar.train10, outfilepath, batch_size, [3, 32, 32], [1])
|
||||
|
||||
|
||||
def prepare_flowers(outpath, batch_size):
|
||||
outfilepath = os.path.join(outpath, "flowers.recordio")
|
||||
convert_2_recordio(flowers.train, outfilepath, batch_size, [3, 224, 224],
|
||||
[1])
|
||||
|
||||
|
||||
def default_mapper(sample):
|
||||
img, label = sample
|
||||
img = image.simple_transform(
|
||||
img, 256, 224, True, mean=[103.94, 116.78, 123.68])
|
||||
return img.flatten().astype('float32'), label
|
||||
|
||||
|
||||
def imagenet_train(data_dir):
|
||||
contents = os.listdir(data_dir)
|
||||
if set(contents) != set(
|
||||
["train", "train.txt", "val", "val_set", "val.txt", "unzip.sh"]):
|
||||
raise Exception("Imagenet data contents error!")
|
||||
img2label = dict()
|
||||
imgfilelist = []
|
||||
with open(os.path.join(data_dir, "train.txt")) as fn:
|
||||
while 1:
|
||||
l = fn.readline()
|
||||
if not l:
|
||||
break
|
||||
img, lbl = l[:-1].split(" ")
|
||||
img2label[img] = int(lbl)
|
||||
imgfilelist.append(img)
|
||||
# shuffle all, this is slow
|
||||
random.shuffle(imgfilelist)
|
||||
|
||||
def train_reader():
|
||||
for idx, imgfile in enumerate(imgfilelist):
|
||||
data = image.load_image(
|
||||
os.path.join(data_dir, "train", imgfile.lower()))
|
||||
label = [img2label[imgfile], ]
|
||||
yield [data, label]
|
||||
|
||||
return paddle.reader.map_readers(default_mapper, train_reader)
|
||||
|
||||
|
||||
def imagenet_test(data_dir):
|
||||
contents = os.listdir(data_dir)
|
||||
if set(contents) != set(
|
||||
["train", "train.txt", "val", "val_set", "val.txt", "unzip.sh"]):
|
||||
raise Exception("Imagenet data contents error!")
|
||||
img2label = dict()
|
||||
imgfilelist = []
|
||||
with open(os.path.join(data_dir, "val.txt")) as fn:
|
||||
while 1:
|
||||
l = fn.readline()
|
||||
if not l:
|
||||
break
|
||||
img, lbl = l[:-1].split(" ")
|
||||
img2label[img] = int(lbl)
|
||||
imgfilelist.append(img)
|
||||
|
||||
def test_reader():
|
||||
for idx, imgfile in enumerate(imgfilelist):
|
||||
base_path = os.path.join(data_dir, "val", imgfile.split(".")[0])
|
||||
image_path = ".".join([base_path, "jpeg"])
|
||||
data = image.load_image(image_path)
|
||||
label = [img2label[imgfile], ]
|
||||
yield [data, label]
|
||||
|
||||
return paddle.reader.map_readers(default_mapper, test_reader)
|
||||
|
||||
|
||||
# FIXME(wuyi): delete this when https://github.com/PaddlePaddle/Paddle/pull/11066 is merged
|
||||
def convert_reader_to_recordio_files(
|
||||
filename,
|
||||
batch_per_file,
|
||||
reader_creator,
|
||||
feeder,
|
||||
compressor=core.RecordIOWriter.Compressor.Snappy,
|
||||
max_num_records=1000,
|
||||
feed_order=None):
|
||||
if feed_order is None:
|
||||
feed_order = feeder.feed_names
|
||||
f_name, f_ext = os.path.splitext(filename)
|
||||
assert (f_ext == ".recordio")
|
||||
|
||||
lines = []
|
||||
f_idx = 0
|
||||
counter = 0
|
||||
for idx, batch in enumerate(reader_creator()):
|
||||
lines.append(batch)
|
||||
if idx >= batch_per_file and idx % batch_per_file == 0:
|
||||
filename = "%s-%05d%s" % (f_name, f_idx, f_ext)
|
||||
with fluid.recordio_writer.create_recordio_writer(
|
||||
filename, compressor, max_num_records) as writer:
|
||||
for l in lines:
|
||||
res = feeder.feed(l)
|
||||
for each in feed_order:
|
||||
writer.append_tensor(res[each])
|
||||
writer.complete_append_tensor()
|
||||
counter += 1
|
||||
lines = []
|
||||
f_idx += 1
|
||||
print("written file: ", filename)
|
||||
return counter
|
||||
|
||||
|
||||
def prepare_imagenet(inpath, outpath, batch_size):
|
||||
r = paddle.batch(imagenet_train(inpath), batch_size=batch_size)
|
||||
feeder = fluid.DataFeeder(
|
||||
feed_list=[
|
||||
fluid.layers.data(
|
||||
name="image", shape=[3, 224, 224]), fluid.layers.data(
|
||||
name="label", shape=[1], dtype='int64')
|
||||
],
|
||||
place=fluid.CPUPlace())
|
||||
outpath = os.path.join(outpath, "imagenet.recordio")
|
||||
convert_reader_to_recordio_files(outpath, 10000, r, feeder)
|
@ -0,0 +1,34 @@
|
||||
// Copyright (c) 2018 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/fluid/operators/reduce_min_max_op.h"
|
||||
|
||||
REGISTER_REDUCE_OP(reduce_max);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
reduce_max, ops::ReduceKernel<paddle::platform::CPUDeviceContext, float,
|
||||
ops::MaxFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, double,
|
||||
ops::MaxFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int, ops::MaxFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int64_t,
|
||||
ops::MaxFunctor>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
reduce_max_grad, ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
float, ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, double,
|
||||
ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int,
|
||||
ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int64_t,
|
||||
ops::MaxOrMinGradFunctor>);
|
@ -0,0 +1,34 @@
|
||||
// Copyright (c) 2018 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/fluid/operators/reduce_min_max_op.h"
|
||||
|
||||
REGISTER_OP_CUDA_KERNEL(reduce_max,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
float, ops::MaxFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
double, ops::MaxFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
int, ops::MaxFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
int64_t, ops::MaxFunctor>);
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
reduce_max_grad, ops::ReduceGradKernel<paddle::platform::CUDADeviceContext,
|
||||
float, ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, double,
|
||||
ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int,
|
||||
ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int64_t,
|
||||
ops::MaxOrMinGradFunctor>);
|
@ -0,0 +1,35 @@
|
||||
// Copyright (c) 2018 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/fluid/operators/reduce_mean_op.h"
|
||||
|
||||
REGISTER_REDUCE_OP(reduce_mean);
|
||||
REGISTER_OP_CPU_KERNEL(reduce_mean,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
|
||||
float, ops::MeanFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
|
||||
double, ops::MeanFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
|
||||
int, ops::MeanFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
|
||||
int64_t, ops::MeanFunctor>);
|
||||
REGISTER_OP_CPU_KERNEL(reduce_mean_grad,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
float, ops::MeanGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
double, ops::MeanGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
int, ops::MeanGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
int64_t, ops::MeanGradFunctor>);
|
@ -0,0 +1,34 @@
|
||||
// Copyright (c) 2018 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/fluid/operators/reduce_mean_op.h"
|
||||
|
||||
REGISTER_OP_CUDA_KERNEL(reduce_mean,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
float, ops::MeanFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
double, ops::MeanFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
int, ops::MeanFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
int64_t, ops::MeanFunctor>);
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
reduce_mean_grad, ops::ReduceGradKernel<paddle::platform::CUDADeviceContext,
|
||||
float, ops::MeanGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, double,
|
||||
ops::MeanGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int,
|
||||
ops::MeanGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int64_t,
|
||||
ops::MeanGradFunctor>);
|
@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2018 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "paddle/fluid/operators/reduce_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
struct MeanFunctor {
|
||||
template <typename DeviceContext, typename X, typename Y, typename Dim>
|
||||
void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) {
|
||||
y->device(place) = x->mean(dim);
|
||||
}
|
||||
};
|
||||
|
||||
struct MeanGradFunctor {
|
||||
template <typename DeviceContext, typename X, typename Y, typename DX,
|
||||
typename DY, typename Dim>
|
||||
void operator()(const DeviceContext& place, X* x, Y* y, DX* dx, DY* dy,
|
||||
const Dim& dim, int size) {
|
||||
dx->device(place) = dy->broadcast(dim) / dx->constant(size);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,50 @@
|
||||
// Copyright (c) 2018 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.
|
||||
#pragma once
|
||||
|
||||
#include "paddle/fluid/operators/reduce_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
struct MaxFunctor {
|
||||
template <typename DeviceContext, typename X, typename Y, typename Dim>
|
||||
void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) {
|
||||
y->device(place) = x->maximum(dim);
|
||||
}
|
||||
};
|
||||
|
||||
struct MinFunctor {
|
||||
template <typename DeviceContext, typename X, typename Y, typename Dim>
|
||||
void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) {
|
||||
y->device(place) = x->minimum(dim);
|
||||
}
|
||||
};
|
||||
|
||||
struct MaxOrMinGradFunctor {
|
||||
template <typename DeviceContext, typename X, typename Y, typename DX,
|
||||
typename DY, typename Dim>
|
||||
void operator()(const DeviceContext& place, X* x, Y* y, DX* dx, DY* dy,
|
||||
const Dim& dim, int size) {
|
||||
auto equals = (*x) == y->broadcast(dim);
|
||||
auto ones = dx->constant(1);
|
||||
auto zeros = dx->constant(0);
|
||||
// If there are multiple minimum or maximum elements, the subgradient of
|
||||
// each is the set [0, 1], and we pass gradient to all of them here.
|
||||
dx->device(place) = dy->broadcast(dim) * equals.select(ones, zeros);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,34 @@
|
||||
// Copyright (c) 2018 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/fluid/operators/reduce_min_max_op.h"
|
||||
|
||||
REGISTER_REDUCE_OP(reduce_min);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
reduce_min, ops::ReduceKernel<paddle::platform::CPUDeviceContext, float,
|
||||
ops::MinFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, double,
|
||||
ops::MinFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int, ops::MinFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int64_t,
|
||||
ops::MinFunctor>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
reduce_min_grad, ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
float, ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, double,
|
||||
ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int,
|
||||
ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int64_t,
|
||||
ops::MaxOrMinGradFunctor>);
|
@ -0,0 +1,34 @@
|
||||
// Copyright (c) 2018 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/fluid/operators/reduce_min_max_op.h"
|
||||
|
||||
REGISTER_OP_CUDA_KERNEL(reduce_min,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
float, ops::MinFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
double, ops::MinFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
int, ops::MinFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
|
||||
int64_t, ops::MinFunctor>);
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
reduce_min_grad, ops::ReduceGradKernel<paddle::platform::CUDADeviceContext,
|
||||
float, ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, double,
|
||||
ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int,
|
||||
ops::MaxOrMinGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int64_t,
|
||||
ops::MaxOrMinGradFunctor>);
|
@ -1,186 +0,0 @@
|
||||
/* Copyright (c) 2016 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/fluid/operators/reduce_op.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using framework::Tensor;
|
||||
|
||||
class ReduceOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"),
|
||||
"Input(X) of ReduceOp should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
||||
"Output(Out) of ReduceOp should not be null.");
|
||||
auto x_dims = ctx->GetInputDim("X");
|
||||
auto x_rank = x_dims.size();
|
||||
PADDLE_ENFORCE_LE(x_rank, 6, "Tensors with rank at most 6 are supported.");
|
||||
auto dims = ctx->Attrs().Get<std::vector<int>>("dim");
|
||||
for (size_t i = 0; i < dims.size(); ++i) {
|
||||
if (dims[i] < 0) dims[i] = x_rank + dims[i];
|
||||
PADDLE_ENFORCE_LT(
|
||||
dims[i], x_rank,
|
||||
"The dim should be in the range [-rank(input), rank(input)).");
|
||||
}
|
||||
sort(dims.begin(), dims.end());
|
||||
bool reduce_all = ctx->Attrs().Get<bool>("reduce_all");
|
||||
bool keep_dim = ctx->Attrs().Get<bool>("keep_dim");
|
||||
if (reduce_all) {
|
||||
if (keep_dim)
|
||||
ctx->SetOutputDim(
|
||||
"Out", framework::make_ddim(std::vector<int64_t>(x_rank, 1)));
|
||||
else
|
||||
ctx->SetOutputDim("Out", {1});
|
||||
} else {
|
||||
auto dims_vector = vectorize(x_dims);
|
||||
if (keep_dim) {
|
||||
for (size_t i = 0; i < dims.size(); ++i) {
|
||||
dims_vector[dims[i]] = 1;
|
||||
}
|
||||
} else {
|
||||
const int kDelFlag = -2;
|
||||
for (size_t i = 0; i < dims.size(); ++i) {
|
||||
dims_vector[dims[i]] = kDelFlag;
|
||||
}
|
||||
dims_vector.erase(
|
||||
remove(dims_vector.begin(), dims_vector.end(), kDelFlag),
|
||||
dims_vector.end());
|
||||
}
|
||||
auto out_dims = framework::make_ddim(dims_vector);
|
||||
ctx->SetOutputDim("Out", out_dims);
|
||||
if (dims[0] != 0) {
|
||||
// Only pass LoD when not reducing on the first dim.
|
||||
ctx->ShareLoD("X", /*->*/ "Out");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class ReduceGradOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
|
||||
"Input(Out@GRAD) should not be null.");
|
||||
auto x_dims = ctx->GetInputDim("X");
|
||||
auto x_rank = x_dims.size();
|
||||
PADDLE_ENFORCE_LE(x_rank, 6, "Tensors with rank at most 6 are supported.");
|
||||
auto dims = ctx->Attrs().Get<std::vector<int>>("dim");
|
||||
for (size_t i = 0; i < dims.size(); ++i) {
|
||||
if (dims[i] < 0) dims[i] = x_rank + dims[i];
|
||||
PADDLE_ENFORCE_LT(
|
||||
dims[i], x_rank,
|
||||
"The dim should be in the range [-rank(input), rank(input)).");
|
||||
}
|
||||
sort(dims.begin(), dims.end());
|
||||
auto x_grad_name = framework::GradVarName("X");
|
||||
if (ctx->HasOutput(x_grad_name)) {
|
||||
ctx->SetOutputDim(x_grad_name, x_dims);
|
||||
ctx->ShareLoD("X", /*->*/ x_grad_name);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class ReduceOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
void Make() final {
|
||||
AddInput("X",
|
||||
"(Tensor) The input tensor. Tensors with rank at most 6 are "
|
||||
"supported.");
|
||||
AddOutput("Out", "(Tensor) The result tensor.");
|
||||
AddAttr<std::vector<int>>(
|
||||
"dim",
|
||||
"(list<int>, default {0}) The dimensions to reduce. "
|
||||
"Must be in the range [-rank(input), rank(input)). "
|
||||
"If `dim[i] < 0`, the dims[i] to reduce is `rank + dims[i]`. "
|
||||
"Note that reducing on the first dim will make the LoD info lost.")
|
||||
.SetDefault({0});
|
||||
AddAttr<bool>("keep_dim",
|
||||
"(bool, default false) "
|
||||
"If true, retain the reduced dimension with length 1.")
|
||||
.SetDefault(false);
|
||||
AddAttr<bool>("reduce_all",
|
||||
"(bool, default false) "
|
||||
"If true, output a scalar reduced along all dimensions.")
|
||||
.SetDefault(false);
|
||||
AddComment(string::Sprintf(R"DOC(
|
||||
%s Operator.
|
||||
|
||||
This operator computes the %s of input tensor along the given dimension.
|
||||
The result tensor has 1 fewer dimension than the input unless keep_dim is true.
|
||||
If reduce_all is true, just reduce along all dimensions and output a scalar.
|
||||
|
||||
)DOC",
|
||||
GetOpType(), GetName()));
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual std::string GetName() const = 0;
|
||||
virtual std::string GetOpType() const = 0;
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
#define REGISTER_REDUCE_OP(op_name) \
|
||||
class __##op_name##Maker__ : public ops::ReduceOpMaker { \
|
||||
protected: \
|
||||
virtual std::string GetName() const { return #op_name; } \
|
||||
virtual std::string GetOpType() const { return "Reduce " #op_name; } \
|
||||
}; \
|
||||
REGISTER_OPERATOR(reduce_##op_name, ops::ReduceOp, __##op_name##Maker__, \
|
||||
paddle::framework::DefaultGradOpDescMaker<true>); \
|
||||
REGISTER_OPERATOR(reduce_##op_name##_grad, ops::ReduceGradOp)
|
||||
|
||||
REGISTER_REDUCE_OP(sum);
|
||||
REGISTER_REDUCE_OP(mean);
|
||||
REGISTER_REDUCE_OP(max);
|
||||
REGISTER_REDUCE_OP(min);
|
||||
REGISTER_REDUCE_OP(prod);
|
||||
|
||||
#define REGISTER_REDUCE_CPU_KERNEL(reduce_type, functor, grad_functor) \
|
||||
REGISTER_OP_CPU_KERNEL(reduce_type, \
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, \
|
||||
float, ops::functor>, \
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, \
|
||||
double, ops::functor>, \
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, \
|
||||
int, ops::functor>, \
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext, \
|
||||
int64_t, ops::functor>); \
|
||||
REGISTER_OP_CPU_KERNEL( \
|
||||
reduce_type##_grad, \
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, float, \
|
||||
ops::grad_functor>, \
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, double, \
|
||||
ops::grad_functor>, \
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int, \
|
||||
ops::grad_functor>, \
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int64_t, \
|
||||
ops::grad_functor>);
|
||||
|
||||
FOR_EACH_KERNEL_FUNCTOR(REGISTER_REDUCE_CPU_KERNEL);
|
@ -1,41 +0,0 @@
|
||||
/* Copyright (c) 2016 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. */
|
||||
|
||||
#define EIGEN_USE_GPU
|
||||
#include "paddle/fluid/operators/reduce_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
#define REGISTER_REDUCE_GPU_KERNEL(reduce_type, functor, grad_functor) \
|
||||
REGISTER_OP_CUDA_KERNEL( \
|
||||
reduce_type, ops::ReduceKernel<paddle::platform::CUDADeviceContext, \
|
||||
float, ops::functor>, \
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext, double, \
|
||||
ops::functor>, \
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext, int, \
|
||||
ops::functor>, \
|
||||
ops::ReduceKernel<paddle::platform::CUDADeviceContext, int64_t, \
|
||||
ops::functor>); \
|
||||
REGISTER_OP_CUDA_KERNEL( \
|
||||
reduce_type##_grad, \
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, float, \
|
||||
ops::grad_functor>, \
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, double, \
|
||||
ops::grad_functor>, \
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int, \
|
||||
ops::grad_functor>, \
|
||||
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int64_t, \
|
||||
ops::grad_functor>);
|
||||
|
||||
FOR_EACH_KERNEL_FUNCTOR(REGISTER_REDUCE_GPU_KERNEL);
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,109 @@
|
||||
// Copyright (c) 2018 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.
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "paddle/fluid/framework/eigen.h"
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
using DDim = framework::DDim;
|
||||
template <typename T, size_t D, int MajorType = Eigen::RowMajor,
|
||||
typename IndexType = Eigen::DenseIndex>
|
||||
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
|
||||
template <typename T, int MajorType = Eigen::RowMajor,
|
||||
typename IndexType = Eigen::DenseIndex>
|
||||
using EigenScalar = framework::EigenScalar<T, MajorType, IndexType>;
|
||||
template <typename T, int MajorType = Eigen::RowMajor,
|
||||
typename IndexType = Eigen::DenseIndex>
|
||||
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;
|
||||
|
||||
template <typename DeviceContext, typename T, size_t D, size_t R_D,
|
||||
typename Functor>
|
||||
void ReduceFunctor(const DeviceContext& context, const framework::Tensor& input,
|
||||
framework::Tensor* output, const std::vector<int>& dims,
|
||||
bool keep_dim) {
|
||||
auto x = EigenTensor<T, D>::From(input);
|
||||
auto x_rank = static_cast<int>(x.dimensions().size());
|
||||
auto reduce_dim = Eigen::array<int, R_D>();
|
||||
std::vector<int> dims_ref = dims;
|
||||
for (size_t i = 0; i < dims_ref.size(); ++i) {
|
||||
if (dims_ref[i] < 0) dims_ref[i] = x_rank + dims_ref[i];
|
||||
reduce_dim[i] = dims_ref[i];
|
||||
}
|
||||
// construct the squeezed output tensor
|
||||
DDim out_dims = output->dims();
|
||||
if (keep_dim && x_rank > 1) {
|
||||
const int kDelFlag = -2;
|
||||
auto dims_vector = framework::vectorize(out_dims);
|
||||
for (size_t i = 0; i < dims_ref.size(); ++i) {
|
||||
dims_vector[dims_ref[i]] = kDelFlag;
|
||||
}
|
||||
dims_vector.erase(remove(dims_vector.begin(), dims_vector.end(), kDelFlag),
|
||||
dims_vector.end());
|
||||
out_dims = framework::make_ddim(dims_vector);
|
||||
}
|
||||
auto& place = *context.eigen_device();
|
||||
Functor functor;
|
||||
|
||||
if (D == 1) {
|
||||
auto out = EigenScalar<T>::From(*output);
|
||||
functor(place, &x, &out, reduce_dim);
|
||||
} else {
|
||||
auto out = EigenTensor<T, (D - R_D)>::From(*output, out_dims);
|
||||
functor(place, &x, &out, reduce_dim);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename DeviceContext, typename T, size_t D, typename Functor>
|
||||
void ReduceGradFunctor(const DeviceContext& context,
|
||||
const framework::Tensor& input0,
|
||||
const framework::Tensor& input1,
|
||||
const framework::Tensor& input2,
|
||||
framework::Tensor* output,
|
||||
const std::vector<int>& dims) {
|
||||
auto x = EigenTensor<T, D>::From(input0);
|
||||
auto x_grad = EigenTensor<T, D>::From(*output);
|
||||
auto x_rank = static_cast<int>(x.dimensions().size());
|
||||
auto x_dims = input0.dims();
|
||||
auto reduced_dims_v = framework::vectorize(x_dims);
|
||||
std::vector<int> dims_ref = dims;
|
||||
Eigen::array<int, D> broadcast_dim;
|
||||
for (size_t i = 0; i < D; ++i) broadcast_dim[i] = 1;
|
||||
|
||||
int broad_cats_times = 1;
|
||||
for (size_t i = 0; i < dims_ref.size(); ++i) {
|
||||
if (dims_ref[i] < 0) {
|
||||
dims_ref[i] = x_rank + dims_ref[i];
|
||||
}
|
||||
reduced_dims_v[dims_ref[i]] = 1;
|
||||
broadcast_dim[dims_ref[i]] = x_dims[dims_ref[i]];
|
||||
broad_cats_times *= x_dims[dims_ref[i]];
|
||||
}
|
||||
auto reduced_dims = framework::make_ddim(reduced_dims_v);
|
||||
auto x_reduce = EigenTensor<T, D>::From(input1, reduced_dims);
|
||||
auto x_reduce_grad = EigenTensor<T, D>::From(input2, reduced_dims);
|
||||
|
||||
auto& place = *context.eigen_device();
|
||||
|
||||
Functor functor;
|
||||
functor(place, &x, &x_reduce, &x_grad, &x_reduce_grad, broadcast_dim,
|
||||
broad_cats_times);
|
||||
}
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,35 @@
|
||||
// Copyright (c) 2018 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/fluid/operators/reduce_prod_op.h"
|
||||
|
||||
REGISTER_REDUCE_OP(reduce_prod);
|
||||
REGISTER_OP_CPU_KERNEL(reduce_prod,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
|
||||
float, ops::ProdFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
|
||||
double, ops::ProdFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
|
||||
int, ops::ProdFunctor>,
|
||||
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
|
||||
int64_t, ops::ProdFunctor>);
|
||||
REGISTER_OP_CPU_KERNEL(reduce_prod_grad,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
float, ops::ProdGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
double, ops::ProdGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
int, ops::ProdGradFunctor>,
|
||||
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
|
||||
int64_t, ops::ProdGradFunctor>);
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue