-
Notifications
You must be signed in to change notification settings - Fork 74k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pjrt] Add an API to add user data to FFI context via PJRT_ExecuteCon…
…text PiperOrigin-RevId: 636260703
- Loading branch information
1 parent
dd5c426
commit 86c79fb
Showing
8 changed files
with
216 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* Copyright 2024 The OpenXLA Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef XLA_PJRT_C_PJRT_C_API_FFI_EXTENSION_H_ | ||
#define XLA_PJRT_C_PJRT_C_API_FFI_EXTENSION_H_ | ||
|
||
#include <stddef.h> | ||
|
||
#include <cstdint> | ||
|
||
#include "xla/pjrt/c/pjrt_c_api.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// PJRT FFI extension provides capabilities for integrating with a | ||
// backend-specific FFI (foreign function interface) library, i.e. for XLA CPU | ||
// and GPU backends it gives access to the XLA FFI internals. | ||
// | ||
// See: https://en.wikipedia.org/wiki/Foreign_function_interface | ||
#define PJRT_API_FFI_EXTENSION_VERSION 1 | ||
|
||
// User-data that will be forwarded to the FFI handlers. Deleter is optional, | ||
// and can be nullptr. Deleter will be called when the context is destroyed. | ||
struct PJRT_FFI_UserData { | ||
int64_t type_id; | ||
void* data; | ||
void (*deleter)(void* data); | ||
}; | ||
|
||
struct PJRT_FFI_UserData_Add_Args { | ||
size_t struct_size; | ||
PJRT_Extension_Base* extension_start; | ||
|
||
PJRT_ExecuteContext* context; | ||
PJRT_FFI_UserData user_data; | ||
}; | ||
PJRT_DEFINE_STRUCT_TRAITS(PJRT_FFI_UserData_Add_Args, user_data); | ||
|
||
// Adds a user data to the execute context. | ||
typedef PJRT_Error* PJRT_FFI_UserData_Add(PJRT_FFI_UserData_Add_Args* args); | ||
|
||
typedef struct PJRT_FFI_Extension { | ||
size_t struct_size; | ||
PJRT_Extension_Type type; | ||
PJRT_Extension_Base* next; | ||
PJRT_FFI_UserData_Add* user_data_add; | ||
} PJRT_FFI; | ||
PJRT_DEFINE_STRUCT_TRAITS(PJRT_FFI_Extension, user_data_add); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // XLA_PJRT_C_PJRT_C_API_FFI_EXTENSION_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* Copyright 2024 The OpenXLA Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "xla/pjrt/c/pjrt_c_api_ffi_internal.h" | ||
|
||
#include "absl/status/status.h" | ||
#include "xla/ffi/execution_context.h" | ||
#include "xla/pjrt/c/pjrt_c_api.h" | ||
#include "xla/pjrt/c/pjrt_c_api_ffi_extension.h" | ||
#include "xla/pjrt/c/pjrt_c_api_helpers.h" | ||
#include "xla/pjrt/c/pjrt_c_api_wrapper_impl.h" | ||
|
||
namespace pjrt { | ||
|
||
static PJRT_Error* PJRT_FFI_UserData_Add(PJRT_FFI_UserData_Add_Args* args) { | ||
PJRT_RETURN_IF_ERROR(ActualStructSizeIsGreaterOrEqual( | ||
"PJRT_FFI_UserData_Add_Args", PJRT_FFI_UserData_Add_Args_STRUCT_SIZE, | ||
args->struct_size)); | ||
|
||
if (args->context == nullptr) { | ||
return new PJRT_Error{absl::InvalidArgumentError( | ||
"PJRT FFI extension requires execute context to be not nullptr")}; | ||
} | ||
|
||
xla::ffi::ExecutionContext::TypeId type_id(args->user_data.type_id); | ||
PJRT_RETURN_IF_ERROR(args->context->execute_context->ffi_context().Insert( | ||
type_id, args->user_data.data, args->user_data.deleter)); | ||
return nullptr; | ||
} | ||
|
||
PJRT_FFI_Extension CreateFfiExtension(PJRT_Extension_Base* next) { | ||
return { | ||
/*struct_size=*/PJRT_FFI_Extension_STRUCT_SIZE, | ||
/*type=*/PJRT_Extension_Type::PJRT_Extension_Type_FFI, | ||
/*next=*/next, | ||
/*custom_call=*/PJRT_FFI_UserData_Add, | ||
}; | ||
} | ||
|
||
} // namespace pjrt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* Copyright 2024 The OpenXLA Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef XLA_PJRT_C_PJRT_C_API_FFI_INTERNAL_H_ | ||
#define XLA_PJRT_C_PJRT_C_API_FFI_INTERNAL_H_ | ||
|
||
#include "xla/pjrt/c/pjrt_c_api.h" | ||
#include "xla/pjrt/c/pjrt_c_api_ffi_extension.h" | ||
|
||
namespace pjrt { | ||
|
||
PJRT_FFI_Extension CreateFfiExtension(PJRT_Extension_Base* next); | ||
|
||
} // namespace pjrt | ||
|
||
#endif // XLA_PJRT_C_PJRT_C_API_FFI_INTERNAL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters