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

Added notion of lifetime to TFOperation, TFOutput and TFInput, dependent on their graph. #401

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions TensorFlowSharp/AdditionalAssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("TensorFlowSharp.Tests.CSharp")]
25 changes: 25 additions & 0 deletions TensorFlowSharp/Foundation/RangeChecks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Runtime.CompilerServices;

namespace TensorFlowSharp.Foundation
{
internal static class RangeChecks
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsWithinRange(int value, int minInclusive, int maxExclusive) => minInclusive <= value && value < maxExclusive;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsWithinRange<T>(T obj, T minInclusive, T maxExcluded)
where T : IComparable<T>
{
return minInclusive.CompareTo(obj) <= 0 && 0 < maxExcluded.CompareTo(obj);
}

public static int Assert(int value, string name, int minInclusive, int maxExclusive)
{
return IsWithinRange(value, minInclusive, maxExclusive)
? value
: throw new ArgumentOutOfRangeException(FormattableString.Invariant($"Expected '{name}' to be within [{minInclusive}, {maxExclusive}), but was {value}"));
}
}
}
38 changes: 38 additions & 0 deletions TensorFlowSharp/Foundation/TFSharpDebug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Runtime.CompilerServices;

namespace TensorFlowSharp.Foundation
{
internal delegate void DebugAction();
internal delegate T DebugFunc<T>();

internal static class TFSharpDebug
{
private const bool IsDebug =
#if DEBUG
true;
#else
false;
#endif

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T AssertNotNull<T>(T obj, string name)
where T : class
{
return IsDebug
? obj ?? throw new ArgumentNullException(name)
: obj;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T AssertWithinLimits<T>(T obj, string name, T minInclusive, T maxExcluded)
where T : IComparable<T>
{

if (!IsDebug || RangeChecks.IsWithinRange(obj, minInclusive, maxExcluded))
return obj;
else
throw new ArgumentOutOfRangeException(FormattableString.Invariant($"Expected '{name}' to be between [{minInclusive}, {maxExcluded})"));
}
}
}
Loading