简体   繁体   English

如何在自定义 Tensorflow 层中支持混合精度?

[英]How to support mixed precision in custom Tensorflow layers?

When developing my own custom layers for tf.keras : how am I supposed to support mixed precision?在为tf.keras开发我自己的自定义层时:我应该如何支持混合精度?

The documentation of mixed precision - a feature which is currently marked as experimental in Tensorflow 2.2 - only explains how to use it from a consumers perspective with predefined layers such as the tf.keras.layers.Dense one.混合精度的文档- 目前在 Tensorflow 2.2 中标记为实验性的功能 - 仅解释了如何从消费者的角度将其与预定义的层一起使用,例如tf.keras.layers.Dense一个。

I already tried to guess it myself and found two - maybe relevant - details:我已经尝试自己猜测并找到了两个 - 可能相关的 - 细节:

  • The dtype property stays as float32 by default when using 16-bit mixed precision.使用 16 位混合精度时, dtype属性默认保持为float32

  • There is a mixed_precision.get_layer_policy(layer) method (see docs ) and a mixed_precision.global_policy() method (see docs ) which could be used to retrieve the configured compute_dtype and variable_dtype .有一个mixed_precision.get_layer_policy(layer)方法(参见文档)和一个mixed_precision.global_policy()方法(参见文档),可用于检索配置的compute_dtypevariable_dtype

Am I supposed to use the above get_layer_policy -method and just cast my variables into compute_dtype within the call(...) method of my layer?我是否应该使用上面的get_layer_policy -方法并将我的变量转换为我层的call(...)方法中的compute_dtype (And pass variable_dtype in my layers build(...) method to add_weight(...) when creating variables?) (并在创建variable_dtype时将我的图层build(...)方法中的 variable_dtype 传递给add_weight(...) ?)

For example, here is naive sample implementation of a standard dense neuron layer:例如,这里是标准密集神经元层的简单示例实现:

  def call(self, input):
    policy = mixed_precision.get_layer_policy(self)
    bias = tf.cast(self._bias, policy.compute_dtype)
    weights = tf.cast(self._weights, policy.compute_dtype)
    y = tf.nn.bias_add(tf.matmul(input, weights), bias)
    outputs = self._activation(y)
    return outputs

Sure, nobody would implement such basic stuff themselves, that one is just for demonstration.当然,没有人会自己实现这些基本的东西,那只是为了演示。 But, would this be the way the Tensorflow team expects us to implement the call(...) methods of our custom layers?但是,这会是 Tensorflow 团队希望我们实现自定义层的call(...)方法的方式吗?

This guide (slide 13-14) from nvidia mentions custom layers for mixed precision training.来自 nvidia 的指南(幻灯片 13-14)提到了用于混合精度训练的自定义层。

You have to implement the method cast_input() .您必须实现方法cast_input() In this example the layer is casted to float16 when mixed precision is enabled:在此示例中,当启用混合精度时,图层将转换为 float16:

class CustomBiasLayer(tf.keras.layers.Layer):

 def build(self, _):
   self.v = self.add_weight('v', ())
   self.built = True
  
 def call(self, inputs):
   return inputs + self.v

 def cast_inputs(self, inputs):
   # Casts to float16, the policy's lowest-precision dtype
   return self._mixed_precision_policy.cast_to_lowest(inputs)

I have not tried this myself, so please let me know if this works for you.我自己没有尝试过,所以如果这对你有用,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM