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

[PHI CAPI] support get & set random seed #55659

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions paddle/phi/capi/include/c_device_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ void *PD_DeviceContextAllocateTensor(const PD_DeviceContext *ctx,
PD_DataType dtype,
PD_Status *status);

void PD_DeviceContextSetSeed(const PD_DeviceContext *ctx,
uint64_t seed,
PD_Status *status);

uint64_t PD_DeviceContextGetSeed(const PD_DeviceContext *ctx,
PD_Status *status);

uint64_t PD_DeviceContextGetRandom(const PD_DeviceContext *ctx,
PD_Status *status);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
20 changes: 20 additions & 0 deletions paddle/phi/capi/include/wrapper_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,26 @@ class DeviceContext : public WrapperBase<PD_DeviceContext> {
PD_CHECK_STATUS(status);
return static_cast<T*>(ptr);
}

uint64_t seed() const {
C_Status status;
auto seed_val = PD_DeviceContextGetSeed(raw_data(), &status);
PD_CHECK_STATUS(status);
return seed_val;
}

void seed(uint64_t seed_val) const {
C_Status status;
PD_DeviceContextSetSeed(raw_data(), seed_val, &status);
PD_CHECK_STATUS(status);
}

uint64_t random() const {
C_Status status;
auto rand_val = PD_DeviceContextGetRandom(raw_data(), &status);
PD_CHECK_STATUS(status);
return rand_val;
}
};

class Scalar : public WrapperBase<PD_Scalar> {
Expand Down
28 changes: 28 additions & 0 deletions paddle/phi/capi/lib/c_device_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,32 @@ void* PD_DeviceContextAllocateTensor(const PD_DeviceContext* ctx,
}
}

void PD_DeviceContextSetSeed(const PD_DeviceContext* ctx,
uint64_t seed,
PD_Status* status) {
if (status) {
*status = C_SUCCESS;
}
auto dev_ctx = reinterpret_cast<const phi::DeviceContext*>(ctx);
dev_ctx->GetGenerator()->SetCurrentSeed(seed);
}

uint64_t PD_DeviceContextGetSeed(const PD_DeviceContext* ctx,
PD_Status* status) {
if (status) {
*status = C_SUCCESS;
}
auto dev_ctx = reinterpret_cast<const phi::DeviceContext*>(ctx);
return dev_ctx->GetGenerator()->GetCurrentSeed();
}

uint64_t PD_DeviceContextGetRandom(const PD_DeviceContext* ctx,
PD_Status* status) {
if (status) {
*status = C_SUCCESS;
}
auto dev_ctx = reinterpret_cast<const phi::DeviceContext*>(ctx);
return dev_ctx->GetGenerator()->Random64();
}

PD_REGISTER_CAPI(device_context);