Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unnecessary finalizer from imageclassification trainer #4624

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Microsoft.ML.TensorFlow/TensorflowTransform.cs
Expand Up @@ -452,7 +452,7 @@ public void Dispose()

try
{
if (Session != null && Session.graph != IntPtr.Zero)
if (Session?.graph != IntPtr.Zero)
{
Session.graph.Dispose();
}
Expand Down
52 changes: 24 additions & 28 deletions src/Microsoft.ML.Vision/ImageClassificationTrainer.cs
Expand Up @@ -77,8 +77,10 @@ namespace Microsoft.ML.Vision
/// </remarks>
public sealed class ImageClassificationTrainer :
TrainerEstimatorBase<MulticlassPredictionTransformer<ImageClassificationModelParameters>,
ImageClassificationModelParameters>
ImageClassificationModelParameters>, IDisposable
{
private bool _isDisposed;

internal const string LoadName = "ImageClassificationTrainer";
internal const string UserName = "Image Classification Trainer";
internal const string ShortName = "IMGCLSS";
Expand Down Expand Up @@ -1301,26 +1303,22 @@ private static TensorFlowSessionWrapper LoadTensorFlowSessionFromMetaGraph(IHost
return new TensorFlowSessionWrapper(GetSession(env, modelFilePath, true), modelFilePath);
}

~ImageClassificationTrainer()
public void Dispose()
{
Dispose(false);
}
if (_isDisposed)
return;

private void Dispose(bool disposing)
{
// Ensure that the Session is not null and its handle is not Zero, as it may have already been
// disposed/finalized. Technically we shouldn't be calling this if disposing == false,
// since we're running in finalizer and the GC doesn't guarantee ordering of finalization of managed
// objects, but we have to make sure that the Session is closed before deleting our temporary directory.
if (_session != null && _session != IntPtr.Zero)
if (_session?.graph != IntPtr.Zero)
{
_session.close();
_session.graph.Dispose();
}

if (_session != null && _session.graph != IntPtr.Zero)
if (_session != null && _session != IntPtr.Zero)
{
_session.graph.Dispose();
_session.close();
}

_isDisposed = true;
}

/// <summary>
Expand All @@ -1337,8 +1335,10 @@ private void Dispose(bool disposing)
/// Image Classification predictor. This class encapsulates the trained Deep Neural Network(DNN) model
/// and is used to score images.
/// </summary>
public sealed class ImageClassificationModelParameters : ModelParametersBase<VBuffer<float>>, IValueMapper
public sealed class ImageClassificationModelParameters : ModelParametersBase<VBuffer<float>>, IValueMapper, IDisposable
{
private bool _isDisposed;

internal const string LoaderSignature = "ImageClassificationPred";
private static VersionInfo GetVersionInfo()
{
Expand Down Expand Up @@ -1490,26 +1490,22 @@ public void Score(in VBuffer<byte> image, Span<float> classProbabilities)
return (ValueMapper<TSrc, TDst>)(Delegate)del;
}

~ImageClassificationModelParameters()
public void Dispose()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dispose [](start = 20, length = 7)

I understand this is the right thing to do but who will call Dispose? Currently the framework does not support disposing transforms, in the past by not freeing the session we have seen memory leak in the scenario where a user creates multiple pipelines.

Copy link
Member

@sharwell sharwell Jan 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Session and Graph both have finalizers (inherited from Tensorflow.DisposableObject). They arguably shouldn't have finalizers (or rather, inherit the one from SafeHandle), but that's outside this project.

{
Dispose(false);
}
if (_isDisposed)
return;

private void Dispose(bool disposing)
{
// Ensure that the Session is not null and its handle is not Zero, as it may have already been
// disposed/finalized. Technically we shouldn't be calling this if disposing == false,
// since we're running in finalizer and the GC doesn't guarantee ordering of finalization of managed
// objects, but we have to make sure that the Session is closed before deleting our temporary directory.
if (_session != null && _session != IntPtr.Zero)
if (_session?.graph != IntPtr.Zero)
{
_session.close();
_session.graph.Dispose();
}

if (_session != null && _session.graph != IntPtr.Zero)
if (_session != null && _session != IntPtr.Zero)
{
_session.graph.Dispose();
_session.close();
}

_isDisposed = true;
}
}
}