【问题标题】:How to return pointer to `GlobalData`?如何返回指向`GlobalData`的指针?
【发布时间】:2022-01-09 13:44:16
【问题描述】:

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

  // 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),但没有帮助。

【问题讨论】:

    标签: c++ tensorflow tensorflow-xla


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多