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

add tensor interfaces #2809

Merged
merged 3 commits into from
Jul 12, 2017

Conversation

JiayiFeng
Copy link
Collaborator

@JiayiFeng JiayiFeng commented Jul 11, 2017

  1. Add member variable DDim dims_ and a getter function dims(). dims_ is supposed to hold tensor's shape and it is required by Op::InferShape.
  2. Remove an overloaded version of mutable_data which use default Place. Now users must provide an explicit Place as parameter when calling mutable_data. See Proposal: remove get_place and set_place #2742.
  3. A PlaceHolder may be shared by more than one tensor, and some of them may be the others' slices. So we add a new member variable 'offset_' for Tensor, which is used to show the byte offset between PlaceHolder::ptr_ and where tensor's data really begins.
  4. Add functions ShareDataFrom and Slice for Tensor.

TODO: Tensor needs a 'CopyFrom' function

And there is another question. After Op::InferShape, we are going to get a series of tensors which have only dims_ but no data(holder_ == nullptr). So it seems reasonable to add a new interface for these tensors' memory allocation:

T* mutable_data(Place place) {
    return mutable_data(dims_, place);
}

If not, we have to write:

float* p = my_tensor.mutable_data<float>(my_tesnor.dims(), CPUPlace());

1. Add member variable 'DDim dims_' and a getter function 'dims()'.
'dims' is supposed to hold tensor's shape during Op::InferShape.
2. Remove 'mutable_data' which use default Place. User must specify a
explicit Place when call 'mutable_data'.
3. A PlaceHolder may be shared by more than one tensor, and some of them may be the others' slices. So we add a new member variable 'offset_' for Tensor, which is used to show the byte offset between PlaceHolder::ptr_ and where tensor's data really begins.
4. Add functions 'ShareDataFrom' and 'Slice' for Tensor.

TODO: Tensor needs a 'CopyFrom' function.
@JiayiFeng JiayiFeng changed the title add tensor interface add tensor interfaces Jul 11, 2017
Copy link
Collaborator

@reyoung reyoung left a comment

Choose a reason for hiding this comment

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

LGTM, except tiny little CPP syntax.

@@ -26,31 +27,65 @@ namespace framework {

class Tensor {
public:
Tensor() : offset_(0) {}

Tensor(const DDim& dims) : dims_(dims), offset_(0) {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

explicit Tensor(const DDim& dims).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

}

Tensor Slice(const int& begin_idx, const int& end_idx) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function could be const

Tensor Slice(int begin_idx, int end_idx) const

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@JiayiFeng JiayiFeng merged commit 76b4dd6 into PaddlePaddle:develop Jul 12, 2017
@JiayiFeng JiayiFeng deleted the dev_add_tensor_interface branch July 12, 2017 03:29
heavengate added a commit to heavengate/Paddle that referenced this pull request Aug 16, 2021
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.

None yet

2 participants