浏览代码

Added Barracuda 0.2.2

/develop-generalizationTraining-TrainerController
Mantas Puida 6 年前
当前提交
5566f8a8
共有 10 个文件被更改,包括 632 次插入519 次删除
  1. 88
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda.md
  2. 1000
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Barracuda.dll
  3. 2
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Plugins/Editor/BarracudaEditor/NNModelIcon.png.meta
  4. 4
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Conv.compute
  5. 4
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Dense.compute
  6. 5
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Generic.compute
  7. 9
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/ReleaseNotes.md
  8. 2
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/package.json
  9. 9
      ml-agents/mlagents/trainers/barracuda.py
  10. 28
      ml-agents/mlagents/trainers/tensorflow_to_barracuda.py

88
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda.md


Trim will first remove outputs that do not match regular expression from the graph. In this case only output that ends with `action` will be left.
Next trim will strip all nodes that do not participate in the evaluation of the output.
You could pass `--print-supported-ops` to get approximate list of supported operations/activations for specific converter.
P.S. Python 3.5 or 3.6 is recommended
## Approximate list of supported layers/operations for TensorFlow converter
```
Activation
Add
AvgPool
BatchNormalization
BatchNormalizationRuntime
BiasAdd
Concat
Conv2D
Conv2DBackpropInput
Dense
DepthwiseConv2dNative
Flatten
FusedBatchNorm
GlobalAveragePool
GlobalAvgPool
InstanceNormalization
LRN
MatMul
Max
MaxPool
Maximum
Mean
Min
Minimum
Mul
Multinomial
Nop
OneHot
Pad
Pow
Prod
RandomStandardNormal
RandomUniform
RealDiv
Reshape
ResizeBicubic
ResizeBilinear
ResizeNearestNeighbor
StridedSlice
Sub
Sum
P.P.S. We plan to migrate Tensorflow and ONNX converters from Python to C# in the future.
```
## Approximate list of supported activations for TensorFlow converter
```
Abs
Acos
Acosh
Asin
Asinh
Atan
Atanh
Ceil
Cos
Cosh
Elu
Exp
Floor
LeakyRelu
Linear
Log
LogSoftmax
Neg
Relu
Relu6
Selu
Sigmoid
Sin
Sinh
Softmax
Softplus
Softsign
Sqrt
Swish
Tan
Tanh
```
P.S. some of these operations are under limited support and not all configurations are properly supported
P.P.S. Python 3.5 or 3.6 is recommended
P.P.P.S. We plan to migrate Tensorflow and ONNX converters from Python to C# in the future.

1000
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Barracuda.dll
文件差异内容过多而无法显示
查看文件

2
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Plugins/Editor/BarracudaEditor/NNModelIcon.png.meta


fileFormatVersion: 2
guid: 57d823f2746e44dc79116df94518bd27
guid: 8682ff569c4c7457a8a8e3a527aad537
TextureImporter:
fileIDToRecycleName: {}
externalObjects: {}

4
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Conv.compute


#pragma kernel Conv2D
#pragma kernel Conv2D_RegisterBlock4x2
//#pragma kernel Conv2D_L1Cached64_RegisterBlock4x4
//#pragma kernel Conv2D_L1Cached32_RegisterBlock4x4
#pragma kernel Conv2D_L1Cached64_RegisterBlock4x4
#pragma kernel Conv2D_L1Cached32_RegisterBlock4x4
#pragma kernel DepthwiseConv2D

4
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Dense.compute


#pragma kernel Dense_L1Cached64
#pragma kernel DenseTiled16x16
//#pragma kernel DenseTiled32x32
//#pragma kernel DenseTiled64x64
#pragma kernel DenseTiled32x32
#pragma kernel DenseTiled64x64
#include "Tensor.cginc"

5
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Resources/Generic.compute


uint oy = y * _Stride.y + dy;
uint ox = x * _Stride.x + dx;
bool mask = (oy >= _Pad.y) && (ox >= _Pad.x) && (oy - _Pad.w < X.height) && (ox - _Pad.z < X.width);
bool mask = (oy >= _Pad.y) && (ox >= _Pad.x) && (oy - _Pad.y < X.height) && (ox - _Pad.x < X.width);
maxV = max(v, maxV);
}

uint oy = y * _Stride.y + dy;
uint ox = x * _Stride.x + dx;
bool mask = (oy >= _Pad.y) && (ox >= _Pad.x) && (oy - _Pad.w < X.height) && (ox - _Pad.z < X.width);
bool mask = (oy >= _Pad.y) && (ox >= _Pad.x) && (oy - _Pad.y < X.height) && (ox - _Pad.x < X.width);
acc += (mask)? X.Get(n, oy - _Pad.y, ox - _Pad.x, c): 0;
counter += (mask)? 1: 0;
}

9
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/ReleaseNotes.md


# Release notes
## 0.2.2
- Added support for --print-supported-ops flag for model converters, now it will print approximate list of supported operations. List of supported ops depends on converter.
- Added Keras converter as part of distribution.
- Now compute shaders are loaded only if GPU worker is requested.
- Fixed bug in MaxPool and AvgPool padding. Issue discovered by Yolo faces network.
- Fixed bug in Transpose convolution support for C# backend.
- Fixed TF model conversion with two LSTM cells.
- Fixed case when strided slice end overflows to zero and thus producing negative range.
## 0.2.1
- TF importer: fixed ResizeNearestNeighbor aka Upsample2D scaling factor detection.
- TF importer: optimized node sorting. Should be faster than 0.2.0.

2
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/package.json


{
"name": "com.unity.barracuda",
"displayName": "Barracuda",
"version": "0.2.1-preview",
"version": "0.2.2-preview",
"unity": "2017.4",
"description": "Barracuda is lightweight and cross-platform Neural Net inference library. Barracuda supports inference both on GPU and CPU.",
"dependencies": {}

9
ml-agents/mlagents/trainers/barracuda.py


parser.add_argument('--print-layer-links', action='store_true')
parser.add_argument('--print-patterns', action='store_true')
parser.add_argument('--print-tensors', action='store_true')
parser.add_argument('--print-supported-ops', action='store_true')
parser.add_argument('--verbose', action='store_true')
args = parser.parse_args()
args.compress_f16 = False # TEMP: disabled, until properly implemented parser.add_argument('-f16', '--compress-f16', action='store_true')

w.write_array(x.data)
def print_known_operations(known_classes, known_activations):
print('OPS supported by the converter:')
for key in sorted(known_classes.keys()):
print(key)
print('ACTIVATIONS supported by the converter:')
for key in sorted(known_activations.keys()):
print(key)

28
ml-agents/mlagents/trainers/tensorflow_to_barracuda.py


repr([re.compile('^lstm/'),
'Reshape', 'ConcatV2', 'Identity']) : 'BasicLSTM',
repr(['Reshape', re.compile('^lstm_[a-z]*/'),'Reshape', 'ConcatV2']) : 'BasicLSTM',
repr(['Sigmoid', 'Mul']) : "Swish",
repr(['Mul', 'Abs', 'Mul', 'Add']) : "LeakyRelu",

sqr_diff(nodes[-1].name, inputs[0], inputs[1]),
'BasicLSTM' : lambda nodes, inputs, tensors, context:
basic_lstm(nodes, inputs, tensors, context,
index_of_actual_output_node=-3, assert_output_node_op_type='Reshape'),
basic_lstm(nodes, inputs, tensors, context, find_type='Reshape'),
'Swish' : lambda nodes, inputs, tensors, _:
Struct(

return nn.layers
# search backwards starting from index_of_actual_output_node for non-const node
def locate_actual_output_node(nodes, index_of_actual_output_node=-1, assert_output_node_op_type=None):
while (-index_of_actual_output_node-1) < len(nodes) and nodes[index_of_actual_output_node].op == 'Const':
def locate_actual_output_node(nodes, index_of_actual_output_node=-1, find_type='Reshape'):
while (-index_of_actual_output_node-1) < len(nodes) and nodes[index_of_actual_output_node].op != find_type:
assert(actual_output_node.op == assert_output_node_op_type or not assert_output_node_op_type)
assert(-index_of_actual_output_node < len(nodes))
return actual_output_node
def gru(nodes, inputs, tensors, context, index_of_actual_output_node, assert_output_node_op_type=None):

return new_layers
def basic_lstm(nodes, inputs, tensors, context, index_of_actual_output_node, assert_output_node_op_type=None):
def basic_lstm(nodes, inputs, tensors, context, find_type='Reshape'):
assert(len(inputs) == 2)
def find_tensor_by_name(name, default=None):

context.layer_ranks[state_c] = 2
context.layer_ranks[state_h] = 2
new_layers = barracuda.lstm('lstm', input, state_c, state_h,
# lstm_value/strided_slice/stack => lstm_value
lstm_name = next(i.name for i in nodes if i.name.startswith('lstm')).split('/')[0]
new_layers = barracuda.lstm(lstm_name, input, state_c, state_h,
'kernel_i', 'kernel_j', 'kernel_f', 'kernel_o',
'bias_i', 'bias_j', 'bias_f', 'bias_o',
new_state_c, new_state_h)

context.model_memories += [state_shape, state_h, new_state_h]
# map expected output of the replaced pattern to output from our LSTM cell
actual_output_node = locate_actual_output_node(nodes, index_of_actual_output_node, assert_output_node_op_type)
actual_output_node = locate_actual_output_node(nodes, -1, find_type)
concat_out_node = locate_actual_output_node(nodes, -1, 'ConcatV2')
context.map_ignored_layer_to_its_input[concat_out_node.name] = new_state_c
return new_layers

input_ranks = [layer_ranks.get(i, -1) for i in o_l.inputs]
for i in o_l.inputs:
if i not in layer_ranks:
if i not in layer_ranks and 'lstm' not in i:
print("WARNING: rank unknown for tensor", i, "while processing node", name)
if hasattr(klass, 'rank'):
rank = klass.rank

args.print_layer_links = verbose
args.print_patterns = verbose
args.print_tensors = verbose
args.print_supported_ops = verbose
if args.print_supported_ops:
barracuda.print_known_operations(known_classes, known_activations)
# Load Tensorflow model
print("Converting %s to %s" % (source_file, target_file))

正在加载...
取消
保存