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_1layout_map['d1.bias']=layout_2layout_map['d2.kernel']=layout_3layout_map['d2.bias']=layout_4## Subclassed modelclassSubclassModel(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)defcall(self,inputs):x=self.d1(inputs)returnself.d2(x)withlayout_map_scope(layout_map):model=SubclassModel()# Triggering the creation of weights within or outside of the scope worksinputs=tf.zeros((10,10))results=model(inputs)model.d1.kernel.layout==layout_1model.d1.bias.layout==layout_2model.d2.kernel.layout==layout_3model.d2.bias.layout==layout_4## Functional modelwithlayout_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_1d1.bias.layout==layout_2d1.kernel.layout==layout_3d1.bias.layout==layout_4## Sequential modelwithlayout_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_1d1.bias.layout==layout_2d1.kernel.layout==layout_3d1.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.
[[["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 2023-03-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.12.0/keras/dtensor/layout_map.py#L238-L341) |\n\nApply the layout to all the tf.Variables created under the scope. (deprecated) \n\n @contextlib.contextmanager\n tf.keras.dtensor.experimental.layout_map_scope(\n layout_map\n )\n\n| **Deprecated:** THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: use tf.keras.dtensor.experimental.LayoutMap.scope() instead.\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\nget assigned to a meaningful attribute, we use `layer.name` as the key for\nthe layer, followed by the attribute name. Keras ensures name uniqueness\namong 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"]]