简体   繁体   中英

How to return pointer to `GlobalData`?

I'm writing a pure C FFI layer to XLA and want to return a pointer to GlobalData , as produced by xla::LocalClient.TransferToServer(...) . I've tried to create a new GlobalData on the free store and, since the comments in the source code say

  // Unregisters the wrapped handle, which causes the service to
  // deallocate the associated data.
  ~GlobalData();

I also release the handle of the stack copy, as

xla::GlobalData* fn(xla::LocalClient* client, xla::Literal literal) {
  std::unique_ptr<xla::GlobalData> global_data =
    client.TransferToServer(literal).ConsumeValueOrDie();

  xla::GlobalData* global_data_non_stack =
    new xla::GlobalData(client.stub(), global_data->handle());
  std::vector<std::unique_ptr<xla::GlobalData>> to_release;
  to_release.push_back(std::move(global_data));
  xla::GlobalData::Release(std::move(to_release));

  return global_data_non_stack;
}

but it's not working. It appears to still be deallocating the handle so when I client.ExecuteAndTransfer(...) I see

2022-01-09 13:42:23.862961: F tensorflow/core/platform/statusor.cc:33] Attempting to fetch value instead of handling error Invalid argument: global data handle 1 was previously deallocated, failed to resolve allocation for parameter 0

I've also tried client.Unregister(*global_data) which doesn't help.

I have no idea if this is safe here, but it appears to work if I use .release() instead of .get() for the std::unique_ptr<xla::GlobalData> . .get() exposes the underlying pointer whilst retaining ownership, while .release() removes ownership of the GlobalData from the smart pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM