Skip to content

Commit

Permalink
set_dtype and mod MultinomialFunctor
Browse files Browse the repository at this point in the history
  • Loading branch information
Aganlengzi committed Feb 25, 2022
1 parent 125817c commit 8c692e7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions paddle/phi/infermeta/ternary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void AddmmInferMeta(const MetaTensor& input,

out->set_dims(make_ddim(output_dims));
out->share_lod(input);
out->set_dtype(input.dtype());
}

} // namespace phi
3 changes: 3 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ void CholeskyInferMeta(const MetaTensor& x, bool upper, MetaTensor* out) {
dims[rank - 2],
dims[rank - 1]));
out->set_dims(x.dims());
out->set_dtype(x.dtype());
}

void CopyToInferMeta(const MetaTensor& x,
Expand All @@ -123,6 +124,7 @@ void IncrementInferMeta(const MetaTensor& x, float value, MetaTensor* out) {
product(x.dims())));
out->set_dims(x.dims());
out->share_lod(x);
out->set_dtype(x.dtype());
}

static phi::DDim ValidateShape(const std::vector<int64_t> shape,
Expand Down Expand Up @@ -297,6 +299,7 @@ void MultinomialInferMeta(const MetaTensor& x,
out_dims[x_rank - 1] = num_samples;

out->set_dims(make_ddim(out_dims));
out->set_dtype(DataType::INT64);
}

void ReshapeInferMeta(const MetaTensor& x,
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/kernels/cpu/multinomial_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ void MultinomialKernel(const Context& dev_ctx,
const int64_t num_categories = in_dims[in_rank - 1];
const int64_t num_distributions = in_rank > 1 ? in_dims[in_rank - 2] : 1;

funcs::MultinomialFunctor<T>(out_data,
funcs::MultinomialFunctor<T>(dev_ctx,
out_data,
in_data,
num_samples,
replacement,
Expand Down
10 changes: 5 additions & 5 deletions paddle/phi/kernels/funcs/multinomial_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ limitations under the License. */

#pragma once

#include "paddle/fluid/framework/generator.h"
#include "paddle/phi/core/device_context.h"
#include "paddle/phi/core/enforce.h"

namespace phi {
namespace funcs {

template <typename T>
void MultinomialFunctor(int64_t* out_data,
template <typename T, typename Context>
void MultinomialFunctor(const Context& dev_ctx,
int64_t* out_data,
const T* in_data,
const int64_t num_samples,
const bool replacement,
Expand All @@ -30,8 +31,7 @@ void MultinomialFunctor(int64_t* out_data,
std::vector<T> cumulative_probs(num_categories);

std::uniform_real_distribution<T> dist(0, 1);
auto gen_ptr = paddle::framework::DefaultCPUGenerator();
auto engine = gen_ptr->GetCPUEngine();
auto engine = dev_ctx.GetHostGenerator()->GetCPUEngine();

for (int64_t i = 0; i < num_distributions; i++) {
T probs_sum = 0;
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/kernels/gpu/multinomial_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ void MultinomialKernel(const Context& dev_ctx,
cudaMemcpyDeviceToHost);
#endif

funcs::MultinomialFunctor<T>(cpu_out_data,
funcs::MultinomialFunctor<T>(dev_ctx,
cpu_out_data,
cpu_in_data,
num_samples,
replacement,
Expand Down

1 comment on commit 8c692e7

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

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

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.