浏览代码
Use Barracuda tensors and Barracuda 0.2.4 (#2308)
Use Barracuda tensors and Barracuda 0.2.4 (#2308)
Bringing bucket of temp memory allocation optimizations: * switched to Barracuda backed tensor across the board, helps to leverage allocators and reuse of the internal buffers * added Barracuda 0.2.4 release, which bring another set of temp memory allocation fixes/develop-generalizationTraining-TrainerController
Ervin T
6 年前
当前提交
9ea7fea8
共有 29 个文件被更改,包括 2568 次插入 和 1156 次删除
-
26UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorApplier.cs
-
79UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorGenerator.cs
-
131UnitySDK/Assets/ML-Agents/Editor/Tests/MultinomialTest.cs
-
136UnitySDK/Assets/ML-Agents/Editor/Tests/RandomNormalTest.cs
-
6UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentBasic.cs
-
14UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception3D.cs
-
25UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
-
972UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Barracuda.dll
-
252UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Conv.compute
-
994UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Dense.compute
-
74UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Generic.compute
-
15UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/ReleaseNotes.md
-
2UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/package.json
-
8UnitySDK/Assets/ML-Agents/Scripts/Agent.cs
-
68UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/ApplierImpl.cs
-
140UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs
-
156UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/GeneratorImpl.cs
-
55UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/ModelParamLoader.cs
-
41UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TFSharpInferenceEngine.cs
-
41UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorApplier.cs
-
59UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorGenerator.cs
-
2UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorNames.cs
-
31UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/Utils/Multinomial.cs
-
36UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/Utils/RandomNormal.cs
-
86UnitySDK/Assets/ML-Agents/Scripts/LearningBrain.cs
-
92UnitySDK/Assets/ML-Agents/Scripts/Utilities.cs
-
143UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorProxy.cs
-
40UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/Tensor.cs
-
0/UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorProxy.cs.meta
972
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Barracuda.dll
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
994
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Dense.compute
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
{ |
|||
"name": "com.unity.barracuda", |
|||
"displayName": "Barracuda", |
|||
"version": "0.2.2-preview", |
|||
"version": "0.2.4-preview", |
|||
"unity": "2017.4", |
|||
"description": "Barracuda is lightweight and cross-platform Neural Net inference library. Barracuda supports inference both on GPU and CPU.", |
|||
"dependencies": {} |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.InteropServices; |
|||
using Barracuda; |
|||
using UnityEngine; |
|||
|
|||
namespace MLAgents.InferenceBrain |
|||
{ |
|||
|
|||
/// <summary>
|
|||
/// Tensor - A class to encapsulate a Tensor used for inference.
|
|||
///
|
|||
/// This class contains the Array that holds the data array, the shapes, type and the placeholder in the
|
|||
/// execution graph. All the fields are editable in the inspector, allowing the user to specify everything
|
|||
/// but the data in a graphical way.
|
|||
/// </summary>
|
|||
[System.Serializable] |
|||
public class TensorProxy |
|||
{ |
|||
public enum TensorType |
|||
{ |
|||
Integer, |
|||
FloatingPoint |
|||
}; |
|||
|
|||
private static Dictionary<TensorType, Type> m_typeMap = new Dictionary<TensorType, Type>() |
|||
{ |
|||
{ TensorType.FloatingPoint, typeof(float)}, |
|||
{TensorType.Integer, typeof(int)} |
|||
}; |
|||
|
|||
public string Name; |
|||
public TensorType ValueType; |
|||
// Since Type is not serializable, we use the DisplayType for the Inspector
|
|||
public Type DataType |
|||
{ |
|||
get { return m_typeMap[ValueType]; } |
|||
} |
|||
public long[] Shape; |
|||
|
|||
public Tensor Data; |
|||
} |
|||
|
|||
public class TensorUtils |
|||
{ |
|||
public static void ResizeTensor(TensorProxy tensor, int batch, ITensorAllocator allocator) |
|||
{ |
|||
if (tensor.Shape[0] == batch && |
|||
tensor.Data != null && tensor.Data.batch == batch) |
|||
return; |
|||
|
|||
tensor.Data?.Dispose(); |
|||
tensor.Shape[0] = batch; |
|||
|
|||
if (tensor.Shape.Length == 4) |
|||
tensor.Data = allocator.Alloc(new TensorShape(batch, (int)tensor.Shape[1], (int)tensor.Shape[2], (int)tensor.Shape[3])); |
|||
else |
|||
tensor.Data = allocator.Alloc(new TensorShape(batch, (int)tensor.Shape[tensor.Shape.Length - 1])); |
|||
} |
|||
|
|||
public static Array BarracudaToFloatArray(Tensor tensor) |
|||
{ |
|||
Array res; |
|||
|
|||
if (tensor.height == 1 && tensor.width == 1) |
|||
res = new float[tensor.batch, tensor.channels]; |
|||
else |
|||
res = new float[tensor.batch, tensor.height, tensor.width, tensor.channels]; |
|||
|
|||
Buffer.BlockCopy(tensor.readonlyArray, 0, res, 0, tensor.length * Marshal.SizeOf<float>()); |
|||
|
|||
return res; |
|||
} |
|||
|
|||
public static Array BarracudaToIntArray(Tensor tensor) |
|||
{ |
|||
|
|||
if (tensor.height == 1 && tensor.width == 1) |
|||
{ |
|||
var res = new int[tensor.batch, tensor.channels]; |
|||
|
|||
for (int b = 0; b < tensor.batch; b++) |
|||
for (int c = 0; c < tensor.channels; c++) |
|||
{ |
|||
res[b, c] = (int)tensor[b, c]; |
|||
} |
|||
|
|||
return res; |
|||
} |
|||
else |
|||
{ |
|||
var res = new int[tensor.batch, tensor.height, tensor.width, tensor.channels]; |
|||
for (int b = 0; b < tensor.batch; b++) |
|||
for (int y = 0; y < tensor.height; y++) |
|||
for (int x = 0; x < tensor.width; x++) |
|||
for (int c = 0; c < tensor.channels; c++) |
|||
{ |
|||
res[b, y, x, c] = (int)tensor[b, y, x, c]; |
|||
} |
|||
|
|||
return res; |
|||
} |
|||
} |
|||
|
|||
public static Tensor ArrayToBarracuda(Array array) |
|||
{ |
|||
Tensor res; |
|||
|
|||
if (array.Rank == 2) |
|||
res = new Tensor(array.GetLength(0), array.GetLength(1)); |
|||
else |
|||
res = new Tensor(array.GetLength(0), array.GetLength(1), array.GetLength(2), array.GetLength(3)); |
|||
|
|||
int offset = 0; |
|||
var barracudaArray = res.data != null ? res.tensorOnDevice.SharedAccess(out offset) : null; |
|||
|
|||
Buffer.BlockCopy(array, 0, barracudaArray, offset, res.length * Marshal.SizeOf<float>()); |
|||
|
|||
return res; |
|||
} |
|||
|
|||
internal static long[] TensorShapeFromBarracuda(TensorShape src) |
|||
{ |
|||
if (src.height == 1 && src.width == 1) |
|||
return new long[2] {src.batch, src.channels}; |
|||
|
|||
return new long[4] {src.batch, src.height, src.width, src.channels}; |
|||
} |
|||
|
|||
public static TensorProxy TensorProxyFromBarracuda(Tensor src, string nameOverride = null) |
|||
{ |
|||
var shape = TensorShapeFromBarracuda(src.shape); |
|||
return new TensorProxy |
|||
{ |
|||
Name = nameOverride ?? src.name, |
|||
ValueType = TensorProxy.TensorType.FloatingPoint, |
|||
Shape = shape, |
|||
Data = src |
|||
}; |
|||
} |
|||
} |
|||
|
|||
} |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace MLAgents.InferenceBrain |
|||
{ |
|||
|
|||
/// <summary>
|
|||
/// Tensor - A class to encapsulate a Tensor used for inference.
|
|||
///
|
|||
/// This class contains the Array that holds the data array, the shapes, type and the placeholder in the
|
|||
/// execution graph. All the fields are editable in the inspector, allowing the user to specify everything
|
|||
/// but the data in a graphical way.
|
|||
/// </summary>
|
|||
[System.Serializable] |
|||
public class Tensor |
|||
{ |
|||
public enum TensorType |
|||
{ |
|||
Integer, |
|||
FloatingPoint |
|||
}; |
|||
|
|||
private static Dictionary<TensorType, Type> m_typeMap = new Dictionary<TensorType, Type>() |
|||
{ |
|||
{ TensorType.FloatingPoint, typeof(float)}, |
|||
{TensorType.Integer, typeof(int)} |
|||
}; |
|||
|
|||
public string Name; |
|||
public TensorType ValueType; |
|||
// Since Type is not serializable, we use the DisplayType for the Inspector
|
|||
public Type DataType |
|||
{ |
|||
get { return m_typeMap[ValueType]; } |
|||
} |
|||
public long[] Shape; |
|||
public Array Data; |
|||
} |
|||
|
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue