tf.keras.dtensor.experimental.layout_map_scope
Stay organized with collections
Save and categorize content based on your preferences.
Apply the layout to all the tf.Variables created under the scope.
@contextlib.contextmanager
tf.keras.dtensor.experimental.layout_map_scope(
layout_map
)
Create a scope that all the tf.Variable created under this scope
will be lazily inited, and initialized later on with proper layout when the
object path in the model is stable/finalized.
Note that the layout mapping will use the object/attribute names as the key
to map the variable against the layout.
For subclassed models, the full object/attribute name is used as the key.
For Functional/Sequential models, since the layers within the model do not get
assigned to a meaningful attribute, we use layer.name
as the key
for the layer, followed by the attribute name. Keras ensures
name uniqueness among the layers in all Functional/Sequential models.
See the following examples that show the variable object names
for different Keras model types:
layout_map = layout_map_lib.LayoutMap(mesh=self.mesh)
layout_map['d1.kernel'] = layout_1
layout_map['d1.bias'] = layout_2
layout_map['d2.kernel'] = layout_3
layout_map['d2.bias'] = layout_4
## Subclassed model
class SubclassModel(tf.keras.Model):
def __init__(self, name=None):
super().__init__(name=name)
self.d1 = tf.keras.layers.Dense(1000)
self.d2 = tf.keras.layers.Dense(1000)
def call(self, inputs):
x = self.d1(inputs)
return self.d2(x)
with layout_map_scope(layout_map):
model = SubclassModel()
# Triggering the creation of weights within or outside of the scope works
inputs = tf.zeros((10, 10))
results = model(inputs)
model.d1.kernel.layout == layout_1
model.d1.bias.layout == layout_2
model.d2.kernel.layout == layout_3
model.d2.bias.layout == layout_4
## Functional model
with layout_map_scope(layout_map):
inputs = tf.keras.Input((10,), batch_size=10)
x = tf.keras.layers.Dense(20, name='d1')(inputs)
output = tf.keras.layers.Dense(30, name='d2')(x)
model = tf.keras.Model(inputs, output)
d1 = model.layers[1]
d2 = model.layers[2]
d1.kernel.layout == layout_1
d1.bias.layout == layout_2
d1.kernel.layout == layout_3
d1.bias.layout == layout_4
## Sequential model
with layout_map_scope(layout_map):
model = tf.keras.Sequential([
tf.keras.layers.Dense(20, name='d1', input_shape=(10,)),
tf.keras.layers.Dense(30, name='d2')
])
d1 = model.layers[0]
d2 = model.layers[1]
d1.kernel.layout == layout_1
d1.bias.layout == layout_2
d1.kernel.layout == layout_3
d1.bias.layout == layout_4
Args |
layout_map
|
a LayoutMap which contains the variable_object_path (string) ->
Layout. When a layout is not found for the variable, a default all
replicated layout will be created for the variable.
|
Yields |
A context that will lazily initialize all tf.Variable objects
within the model, with their attributed layouts.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2022-10-27 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-10-27 UTC."],[],[],null,["# tf.keras.dtensor.experimental.layout_map_scope\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.9.0/keras/dtensor/layout_map.py#L142-L242) |\n\nApply the layout to all the tf.Variables created under the scope. \n\n @contextlib.contextmanager\n tf.keras.dtensor.experimental.layout_map_scope(\n layout_map\n )\n\nCreate a scope that all the tf.Variable created under this scope\nwill be lazily inited, and initialized later on with proper layout when the\nobject path in the model is stable/finalized.\n\nNote that the layout mapping will use the object/attribute names as the key\nto map the variable against the layout.\n\nFor subclassed models, the full object/attribute name is used as the key.\nFor Functional/Sequential models, since the layers within the model do not get\nassigned to a meaningful attribute, we use `layer.name` as the key\nfor the layer, followed by the attribute name. Keras ensures\nname uniqueness among the layers in all Functional/Sequential models.\n\nSee the following examples that show the variable object names\nfor different Keras model types: \n\n layout_map = layout_map_lib.LayoutMap(mesh=self.mesh)\n layout_map['d1.kernel'] = layout_1\n layout_map['d1.bias'] = layout_2\n layout_map['d2.kernel'] = layout_3\n layout_map['d2.bias'] = layout_4\n\n ## Subclassed model\n class SubclassModel(tf.keras.Model):\n\n def __init__(self, name=None):\n super().__init__(name=name)\n self.d1 = tf.keras.layers.Dense(1000)\n self.d2 = tf.keras.layers.Dense(1000)\n\n def call(self, inputs):\n x = self.d1(inputs)\n return self.d2(x)\n\n with layout_map_scope(layout_map):\n model = SubclassModel()\n # Triggering the creation of weights within or outside of the scope works\n inputs = tf.zeros((10, 10))\n results = model(inputs)\n\n model.d1.kernel.layout == layout_1\n model.d1.bias.layout == layout_2\n model.d2.kernel.layout == layout_3\n model.d2.bias.layout == layout_4\n\n ## Functional model\n with layout_map_scope(layout_map):\n inputs = tf.keras.Input((10,), batch_size=10)\n x = tf.keras.layers.Dense(20, name='d1')(inputs)\n output = tf.keras.layers.Dense(30, name='d2')(x)\n\n model = tf.keras.Model(inputs, output)\n\n d1 = model.layers[1]\n d2 = model.layers[2]\n\n d1.kernel.layout == layout_1\n d1.bias.layout == layout_2\n d1.kernel.layout == layout_3\n d1.bias.layout == layout_4\n\n ## Sequential model\n with layout_map_scope(layout_map):\n model = tf.keras.Sequential([\n tf.keras.layers.Dense(20, name='d1', input_shape=(10,)),\n tf.keras.layers.Dense(30, name='d2')\n ])\n\n d1 = model.layers[0]\n d2 = model.layers[1]\n\n d1.kernel.layout == layout_1\n d1.bias.layout == layout_2\n d1.kernel.layout == layout_3\n d1.bias.layout == layout_4\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `layout_map` | a LayoutMap which contains the variable_object_path (string) -\\\u003e Layout. When a layout is not found for the variable, a default all replicated layout will be created for the variable. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Yields ------ ||\n|---|---|\n| A context that will lazily initialize all [`tf.Variable`](../../../../tf/Variable) objects within the model, with their attributed layouts. ||\n\n\u003cbr /\u003e"]]