0

我正在向 XLA 编写一个纯 C FFI 层,并希望返回一个指向 的指针GlobalData,由xla::LocalClient.TransferToServer(...). 我试图GlobalData在免费商店创建一个新的,因为源代码中的评论说

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

我还释放了堆栈副本的句柄,如

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;
}

但它不起作用。它似乎仍在释放句柄,所以当client.ExecuteAndTransfer(...)我看到

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

我也试过client.Unregister(*global_data)没有帮助。

4

1 回答 1

0

我不确定这一点,但如果我使用.release()而不是.get(),则std::unique_ptr<xla::GlobalData>失去对的所有权,GlobalData我可以安全地返回结果指针,因为我知道没有任何东西会自动释放它指向的内存。无需创建新GlobalData实例。

于 2022-01-10T01:28:25.260 回答