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

Function Argument #1064

Merged
merged 12 commits into from
Jan 10, 2017
Merged

Function Argument #1064

merged 12 commits into from
Jan 10, 2017

Conversation

hedaoyuan
Copy link
Contributor

@hedaoyuan hedaoyuan commented Jan 4, 2017

This PR is to achieve argument types of the Functions.

  1. 关于Function的BufferArg描述见issue 892

  2. 基于BufferArgs参数类型的Function:: calc实现和调用方式如下:

template <DeviceType DType>
void Function(const BufferArgs& arguments) {
  // get argument
  auto input = arguments[0].matrix<DType>();
  auto output = arguments[1].matrix<DType>();
  // call computation api
  FunctionApi<DType>(output, input);
}

TEST(BufferTest, Function) {
  CpuMatrix cpuInput = CpuMatrix(100, 200);
  CpuMatrix cpuOutput = CpuMatrix(100, 200);
  // prepare arguments
  BufferArgs cpuArgments;
  cpuArgments.addArg(cpuInput);
  cpuArgments.addArg(cpuOutput);
  // call Function
  Function<DEVICE_TYPE_CPU>(cpuArgments);
}

Copy link
Contributor

@tianbingsz tianbingsz left a comment

Choose a reason for hiding this comment

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

Looks very good to me.

: buf_(buf), valueType_(valueType) {}

BufferArg(const Matrix& matrix)
: buf_((void*)matrix.getData()),
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use reinterpret_cast<void*>(matrix.getData())?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

BufferArg(const Matrix& matrix, const TensorShape& shape)
: buf_((void*)matrix.getData()),
Copy link
Contributor

Choose a reason for hiding this comment

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

same as line 59

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

BufferArg(const Vector& vector)
: buf_((void*)vector.getData()),
Copy link
Contributor

Choose a reason for hiding this comment

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

same as line 59.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

BufferArg(const IVector& vector)
: buf_((void*)vector.getData()), valueType_(VALUE_TYPE_INT32), shape_(1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

// sequence start positions in a mini-batch of sequences
// shape_.ndims() == 1
// valueType_ = int32
// if a < b than value_.buf_[a] < value_.buf_[b]
Copy link
Contributor

Choose a reason for hiding this comment

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

if a < b then value_.buf_[a] < value_.buf_[b]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

TensorShape(std::initializer_list<size_t> dims) {
ndims_ = dims.size();
initDims(ndims_);
std::copy(dims.begin(), dims.end(), dims_.begin());
Copy link
Contributor

@tianbingsz tianbingsz Jan 6, 2017

Choose a reason for hiding this comment

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

Can we use dims_.assign(dims.begin(), dims.end()) ?


template <DeviceType DType>
void Function(const BufferArgs& arguments) {
auto input = arguments[0].matrix<DType>();
Copy link
Contributor

Choose a reason for hiding this comment

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

const auto input = arguments[0].matrix();

CHECK_EQ(inputs[0].shape()[0], outputs[0].shape()[0]);

auto out_mat = outputs[0].matrix<Device>();
auto in_mat = inputs[0].matrix<Device>();
Copy link
Contributor

Choose a reason for hiding this comment

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

const auto in_mat = inputs[0].matrix();


auto out_mat = outputs[0].matrix<Device>();
auto in_mat = inputs[0].matrix<Device>();
auto w_mat = !inputs[1].data()
Copy link
Contributor

Choose a reason for hiding this comment

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

const auto w_mat

auto w_grad_mat = !inputs[1].data()
? typename Tensor<real, Device>::Matrix(nullptr, 0, 0)
: inputs[1].matrix<Device>();
auto seq_vec = inputs[2].vector<int, Device>();
Copy link
Contributor

Choose a reason for hiding this comment

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

const auto seq_vec

@hedaoyuan
Copy link
Contributor Author

commit 57e2521 对BufferArg增加一个ArgType argType_属性,同时去掉了Function的inouts参数。

Function的inouts参数原先的设计是用来区分outputs参数的;对于有些情况,Function计算结果是直接赋值到output中(比如,PoolLayer::forward等),对于有些情况Function计算结果是累加到output上(比如,MixedLaye和Layer::backward计算等);对应assign to output的,是一个writeonly参数,用outputs来传参;add to output的,是一个read and write参数,用inouts来传参。

commit 57e2521修改的目的是,inouts参数的主要目的是为了描述Function计算结果是assign to output还是add to output,对BufferArg增加一个值为ASSIGN_TO或ADD_TO的属性更能清楚的表示这件事情;另外,对于有多个output的Function,可以对每个output单独标识ASSIGN_TO或ADD_TO的属性。

@tianbingsz
Copy link
Contributor

@hedaoyuan, LGTM, please make the checks pass and will approve it.

@hedaoyuan hedaoyuan merged commit 7df67ba into PaddlePaddle:develop Jan 10, 2017
@hedaoyuan hedaoyuan mentioned this pull request Jan 12, 2017
wangxicoding pushed a commit to wangxicoding/Paddle that referenced this pull request Dec 9, 2021
* add sharding for gpt-3

* del debug

* add sharding save model

* update model save

* fix seed func

* set control in tensor parallel

Co-authored-by: Zhong Hui <zhonghui.net@gmail.com>
lizexu123 pushed a commit to lizexu123/Paddle that referenced this pull request Feb 23, 2024
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