Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gather function only #3191

Merged
merged 8 commits into from
Aug 9, 2017
Merged

gather function only #3191

merged 8 commits into from
Aug 9, 2017

Conversation

zchen0211
Copy link
Contributor

@zchen0211 zchen0211 commented Aug 2, 2017

I add the gather functions, with unit-test passed.

The function is very similar to https://www.tensorflow.org/api_docs/python/tf/gather.

Copy link
Collaborator

@wangkuiyi wangkuiyi left a comment

Choose a reason for hiding this comment

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

The first batch of comments is all about coding style. Please read https://google.github.io/styleguide/cppguide.html thoroughly before coding C++.

*/
template <typename place, typename T>
Tensor* Gather_func(Tensor* Src, Tensor* Index) {
// assert index is an int-type tensor?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove unnecessary code instead commenting them out.

* input[Index]: type-int index Tensor (1-D)
* return: output tensor
*/
template <typename place, typename T>
Copy link
Collaborator

Choose a reason for hiding this comment

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

place ==> Place,

We are following Google C++ style, which requires that type names in Camel case: https://google.github.io/styleguide/cppguide.html#Type_Names

// assert(Index->istype(int));

// check index of shape 1-D
assert(Index->dims().size()==1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use PADDLE_ENFORE defined at

#define PADDLE_ENFORCE(...) \

int index_size = Index->dims()[0];

// Source shape
auto src_dims = Src->dims();
Copy link
Collaborator

Choose a reason for hiding this comment

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

The indentation doesn't match Google C++ style. This error implies that you are not using pre-commit to automatically reformat your code.

Please run pip install pre-commit to install pre-commit. Then Paddle repo's configuration will enable Git to call it whenever you run git commit.


/* slice size */
int slice_size = 1;
for(unsigned int i = 0; i < src_dims.size(); ++i)
Copy link
Collaborator

Choose a reason for hiding this comment

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

unsigned int ==> size_t, which is the return type of src_dims.size().

* return: output tensor
*/
template <typename place, typename T>
Tensor* Gather_func(Tensor* Src, Tensor* Index) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Src => src
Index => index

Please follow https://google.github.io/styleguide/cppguide.html#Variable_Names when naming variable.

* return: output tensor
*/
template <typename place, typename T>
Tensor* Gather_func(Tensor* Src, Tensor* Index) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Gather_func => Gather

Please follow https://google.github.io/styleguide/cppguide.html#Function_Names when naming functions.


for(int i = 0; i < index_size; ++i)
int index_ = index[i];
/* dst[index_] += src[index_]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove unnecessary code instead of commenting them out.


// Source shape
auto src_dims = src->dims();
DDim output_dims(dims_src);
Copy link
Member

Choose a reason for hiding this comment

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

dims_src ==> src_dims ?

// Create a tensor of shape [index_size, dim_src[1:]]
output_dims[0] = index_size;

Tensor* New_tensor;
Copy link
Member

Choose a reason for hiding this comment

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

Tensor* New_tensor = new Tensor();  

use shared_ptr maybe better

@zchen0211 zchen0211 changed the title gather and scatter-update added gather function only Aug 7, 2017
@zchen0211
Copy link
Contributor Author

Function looks like this one.
gather

}
}

/* Implementation of GPU copy:
Copy link
Collaborator

Choose a reason for hiding this comment

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

/* ==> //

CPUGather<T>(src->data<T>(), index->data<int>(), slice_size, index_size,
output->data<T>());
} else {
// init for GPU
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete unwanted code

#include "paddle/framework/tensor.h"
#include "paddle/platform/place.h"

using paddle::framework::Tensor;
Copy link
Collaborator

Choose a reason for hiding this comment

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

According to Google C++ style guide https://google.github.io/styleguide/cppguide.html#Namespaces, no using in header files.

Tensor* src = new Tensor();
Tensor* index = new Tensor();
Tensor* output = new Tensor();
// src.Resize(make_ddim({3, 4}));
Copy link
Collaborator

Choose a reason for hiding this comment

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

unwanted code

p_index[0] = 1;
p_index[1] = 0;

// gather
Copy link
Collaborator

Choose a reason for hiding this comment

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

inaccurate comment

*/
template <typename T>
void GPUGather(const T* src, const int* index, const int slice_size,
const int index_size, T* output);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you plan to implement this function template in a future PR?

void GPUGather(const T* src, const int* index, const int slice_size,
const int index_size, T* output);

/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can just use //

@zchen0211 zchen0211 merged commit 7c8e5c3 into PaddlePaddle:develop Aug 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants