using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Assertions;
namespace Unity.Barracuda {
///
/// Deprecated APIs, left here only for backwards compatibility
///
public static class DeprecatedTensorExtensions
{
///
/// Deprecated, use `AdjustPadToPool` version with pool as an array instead
///
/// `Tensor`
/// pool tuple
/// stride
/// padding
/// shape as int array
[ObsoleteAttribute("Use AdjustPadToPool version with pool as an array instead.", false)]
public static int[] AdjustPadToPool(this Tensor tensor, ValueTuple pool, int[] stride, int[] pad)
{
unsafe
{
int* pPool = stackalloc int[2];
pPool[0] = pool.Item1;
pPool[1] = pool.Item2;
return tensor.shape.AdjustPadToPool(pPool, stride, pad);
}
}
///
/// Deprecated, use `AdjustPadToPool` version with pool as an array instead
///
/// `TensorShape`
/// pool tuple
/// stride
/// padding
/// shape as int array
[ObsoleteAttribute("Use AdjustPadToPool version with pool as an array instead.", false)]
public static int[] AdjustPadToPool(this TensorShape shape, ValueTuple pool, int[] stride, int[] pad)
{
unsafe
{
int* pPool = stackalloc int[2];
pPool[0] = pool.Item1;
pPool[1] = pool.Item2;
return shape.AdjustPadToPool(pPool, stride, pad);
}
}
///
/// Deprecated. Use UploadToDevice instead
///
/// Tensor
/// ITensorData
/// Force cache invalidation
[ObsoleteAttribute("Use UploadToDevice instead.", false)]
public static void PinToDeviceAndUploadToIt(this Tensor self, ITensorData onDevice, bool forceInvalidateCache = true)
{
self.UploadToDevice(onDevice, forceInvalidateCache);
}
///
/// Deprecated. Use AttachToDevice instead
///
/// Tensor
/// ITensorData
[ObsoleteAttribute("Use AttachToDevice instead.", false)]
public static void PinToDeviceAndDownloadFromIt(this Tensor self, ITensorData onDevice)
{
self.AttachToDevice(onDevice);
}
///
/// Deprecated. Use DetachFromDevice instead
///
/// Tensor
/// Call dispose when unpinned
///
[ObsoleteAttribute("Use DetachFromDevice instead.", false)]
public static ITensorData Unpin(this Tensor self, bool disposeUnpinned = true)
{
return self.DetachFromDevice(disposeUnpinned);
}
///
/// Deprecated. Use AttachToDevice instead
///
/// Tensor
/// ITensorData
[ObsoleteAttribute("Use AttachToDevice instead.", false)]
public static void CastOnDevice(this Tensor self, ITensorData onDevice)
{
self.AttachToDevice(onDevice);
}
#region Tensor
// @SEE: Tensor.cs
// public ITensorData UnpinAndDisposeTensor()
// public float[] readonlyArray { get { PrepareCacheForAccess(); return m_Cache; } }
// public int readonlyArrayOffset { get { return 0; } }
#endregion
}
///
/// Deprecated `TestSet` extensions
///
public static class DeprecatedTestSetExtensions
{
///
/// Deprecated. Use `GetInputShape` version returning a TensorShape instead
///
/// `TestSet`
/// input index
/// input shape as array
[ObsoleteAttribute("Use GetInputShape version returning a TensorShape instead.", false)]
public static int[] GetInputShape(this TestSet self, int idx = 0)
{
var shape = self.GetInputShape(idx);
Assert.IsTrue(shape.Is4D());
return shape.ToArray();
}
///
/// Deprecated. Use `GetOutputShape` version returning a TensorShape instead
///
/// `TestSet`
/// output index
/// shape as int array
[ObsoleteAttribute("Use GetOutputShape version returning a TensorShape instead.", false)]
public static int[] GetOutputShape(this TestSet self, int idx = 0)
{
var shape = self.GetOutputShape(idx);
Assert.IsTrue(shape.Is4D());
return shape.ToArray();
}
}
///
/// Deprecated ITensorData extensions
///
public static class DeprecatedTensorDataExtensions
{
///
/// Deprecated. Use maxCapacity extensions
///
/// Tensor
/// max Tensor capacity
[ObsoleteAttribute("Use maxCapacity instead.", false)]
public static int GetMaxCount(this ITensorData self)
{
return self.maxCapacity;
}
}
///
/// Deprecated IWorker extensions
///
public static class DeprecatedWorkerExtensions
{
#region Inputs
///
/// Deprecated. Use SetInput instead
///
/// IWorker
/// input Tensor
[ObsoleteAttribute("Use SetInput instead.", false)]
public static void AddInput(this IWorker worker, Tensor x)
{
worker.SetInput(x);
}
///
/// Deprecated. Use SetInput instead
///
/// IWorker
/// input Tensor name
/// input Tensor
[ObsoleteAttribute("Use SetInput instead.", false)]
public static void AddInput(this IWorker worker, string name, Tensor x)
{
worker.SetInput(name, x);
}
#endregion
#region Outputs
///
/// Deprecated. Use PeekOutput instead
///
/// IWorker
/// output Tensor
[ObsoleteAttribute("Use PeekOutput instead.", false)]
public static Tensor Peek(this IWorker worker)
{
return worker.PeekOutput();
}
///
/// Deprecated. Use PeekOutput instead
///
/// IWorker
/// output Tensor name
/// output Tensor
[ObsoleteAttribute("Use PeekOutput instead.", false)]
public static Tensor Peek(this IWorker worker, string name)
{
return worker.PeekOutput(name);
}
#endregion
#region Schedule one layer at a time
///
/// Deprecated. Use StartManualSchedule instead
///
/// IWorker
/// Manual schedule iterator
[ObsoleteAttribute("Use StartManualSchedule instead.", false)]
public static IEnumerator ExecuteAsync(this IWorker worker)
{
return worker.StartManualSchedule();
}
///
/// Deprecated. Use StartManualSchedule instead
///
/// IWorker
/// input Tensor
/// Manual schedule iterator
[ObsoleteAttribute("Use StartManualSchedule instead.", false)]
public static IEnumerator ExecuteAsync(this IWorker worker, Tensor input)
{
return worker.StartManualSchedule(input);
}
///
/// Deprecated. Use StartManualSchedule instead
///
/// IWorker
/// input Tensor Dictionary
/// Manual schedule iterator
[ObsoleteAttribute("Use StartManualSchedule instead.", false)]
public static IEnumerator ExecuteAsync(this IWorker worker, IDictionary inputs)
{
return worker.StartManualSchedule(inputs);
}
///
/// Deprecated. Use FlushSchedule instead
///
/// IWorker
[ObsoleteAttribute("Use FlushSchedule instead.", false)]
public static void WaitForCompletion(this IWorker worker)
{
worker.FlushSchedule(blocking:true);
}
///
/// Deprecated. Use scheduleProgress instead
///
/// IWorker
/// Manual schedule progress (0 = 0%, 1 = 100% complete)
[ObsoleteAttribute("Use scheduleProgress instead.", false)]
public static float GetAsyncProgress(this IWorker worker)
{
return worker.scheduleProgress;
}
#endregion
#region Outputs
///
/// Deprecated. Use Execute followed by CopyOutput and PrepareCacheForAccess instead
///
/// IWorker
/// input Tensor
/// output Tensor
[ObsoleteAttribute("Use Execute followed by CopyOutput and PrepareCacheForAccess instead.", false)]
public static Tensor ExecuteAndWaitForCompletion(this IWorker worker, Tensor input)
{
worker.Execute(input);
return worker.CopyOutput();
}
///
/// Deprecated. Use Execute followed by CopyOutput and PrepareCacheForAccess instead
///
/// IWorker
/// input Tensor Dictionary
/// output Tensor
[ObsoleteAttribute("Use Execute followed by CopyOutput and PrepareCacheForAccess instead.", false)]
public static Tensor ExecuteAndWaitForCompletion(this IWorker worker, IDictionary inputs)
{
worker.Execute(inputs);
return worker.CopyOutput();
}
///
/// Deprecated. Use PeekOutput followed by TakeOwnership or DeepCopy instead
///
/// IWorker
/// output Tensor
[ObsoleteAttribute("Use PeekOutput followed by TakeOwnership or DeepCopy instead.", false)]
public static Tensor FetchAndTakeOwnership(this IWorker worker)
{
var output = worker.PeekOutput();
output.TakeOwnership();
return output;
}
///
/// Deprecated. Use PeekOutput followed by TakeOwnership or DeepCopy instead
///
/// IWorker
/// output Tensor name
/// output Tensor
[ObsoleteAttribute("Use PeekOutput followed by TakeOwnership or DeepCopy instead.", false)]
public static Tensor FetchAndTakeOwnership(this IWorker worker, string name)
{
var output = worker.PeekOutput(name);
output.TakeOwnership();
return output;
}
///
/// Deprecated. Use CopyOutput instead
///
/// IWorker
/// copy of the output Tensor
[ObsoleteAttribute("Use CopyOutput instead.", false)]
public static Tensor Fetch(this IWorker worker)
{
return worker.CopyOutput();
}
///
/// Deprecated. Use CopyOutput instead
///
/// IWorker
/// output Tensor name
/// copy of the output Tensor
[ObsoleteAttribute("Use CopyOutput instead.", false)]
public static Tensor Fetch(this IWorker worker, string name)
{
return worker.CopyOutput(name);
}
#endregion
}
///
/// Deprecated. Use WorkerFactory class instead
///
[ObsoleteAttribute("Use WorkerFactory class instead.", false)]
public class BarracudaWorkerFactory : WorkerFactory
{
///
/// Device type enum
///
public enum Flags
{
///
/// GPU
///
Compute = Device.GPU,
///
/// CPU
///
CSharp = Device.CPU
}
///
/// Compare against Flags enum
///
/// type
/// flags
/// True if matches
public static bool IsType(Type type, Flags flags)
{
return IsType(type, (Device)flags);
}
}
///
/// Deprecated. Use Tensor.ToRenderTexture method instead
///
[ObsoleteAttribute("Use Tensor.ToRenderTexture method instead.", false)]
public class BarracudaTextureUtils
{
///
/// Copy Tensor data to RenderTexture
///
/// Tensor
/// target RenderTexture
/// batch
/// from channel
/// scale
/// bias
public static void TensorToRenderTexture(Tensor x, RenderTexture target,
int batch = 0, int fromChannel = 0, float scale = 1.0f, float bias = 0f)
{
x.ToRenderTexture(target, batch, fromChannel, scale, bias);
}
///
/// Copy Tensor data to RenderTexture
///
/// Tensor
/// batch
/// from channel
/// scale
/// bias
/// RenderTexture created from Tensor data
public static RenderTexture TensorToRenderTexture(Tensor x,
int batch = 0, int fromChannel = 0, float scale = 1.0f, float bias = 0f)
{
return x.ToRenderTexture(batch, fromChannel, scale, bias);
}
}
} // namespace Unity.Barracuda