gpt4 book ai didi

c# - 创建 tensorflow 图时出现异常 CallbackOnCollectedDelegate

转载 作者:可可西里 更新时间:2023-11-01 08:48:10 27 4
gpt4 key购买 nike

我尝试使用 TensorFlowSharp 构建一个小的 tensorflow 应用程序,有时我会收到此异常:

Managed Debugging Assistant 'CallbackOnCollectedDelegate'

对于函数TensorFlowSharp!TensorFlow.TFBuffer+BufferReleaseFunc::Invoke

我试图找出它的意思,但我没有完全理解其中的解释。这是抛出异常的代码部分:

var graph = new TFGraph();
var model = File.ReadAllBytes(ModelsFile);
graph.Import(model, "");

有人知道我应该怎么做才能防止出现这种异常吗?

布鲁诺

最佳答案

我认为这是 TensorflowSharp 中的错误。

该错误看起来像是 CLR 代码中通常不一致的访问冲突(通常只在重负载或随机尝试次数下发生)。引用自微软docs :

The callbackOnCollectedDelegate managed debugging assistant (MDA) is activated if a delegate is marshaled from managed to unmanaged code as a function pointer and a callback is placed on that function pointer after the delegate has been garbage collected.

当从中创建函数指针并暴露给非托管代码的委托(delegate)被垃圾回收时,就会发生这种类型的错误。当非托管组件尝试调用函数指针时,它会生成访问冲突。失败似乎是随机的,因为它取决于垃圾收集发生的时间。

解决方案可能很困难,因为一旦委托(delegate)作为非托管函数指针被编码出来,垃圾收集器就无法跟踪其生命周期。相反,需要在非托管函数指针的生命周期内保留对委托(delegate)的引用。为此,必须在 TensorFlowShapr 的代码(或您的代码)中识别收集到的错误委托(delegate)。

You can also enable the gcUnmanagedToManaged MDA to force a garbage collection before every callback into the runtime. This will remove the uncertainty introduced by the garbage collection by ensuring that a garbage collection always occurs before the callback. Once you know what delegate was collected, change your code to keep a reference to that delegate on the managed side for the lifetime of the marshaled unmanaged function pointer.

所以,我想最好将此报告给图书馆的制造商。

关于c# - 创建 tensorflow 图时出现异常 CallbackOnCollectedDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46131711/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com