Skip to content

Commit 8cab8ef

Browse files
authored
Remove dtensor (keras-team#1268)
We will replace this with the work on keras-team#1267 But we have no coverage for that PR till we run tests against Keras 3, which will probably still be about a week. For now, let's just remove this usage, which is no longer needed and will break a Keras 3 install.
1 parent f6d240b commit 8cab8ef

File tree

9 files changed

+0
-275
lines changed

9 files changed

+0
-275
lines changed

keras_nlp/conftest.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,3 @@ def pytest_collection_modifyitems(config, items):
8686
tf.debugging.disable_traceback_filtering()
8787
if backend_config.multi_backend():
8888
keras.config.disable_traceback_filtering()
89-
90-
# One off setup for dtensor tests.
91-
if not backend_config.multi_backend():
92-
keras.backend.experimental.enable_tf_random_generator()
93-
keras.utils.set_random_seed(1337)

keras_nlp/models/gpt2/gpt2_backbone.py

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
import copy
1616

17-
from tensorflow.experimental import dtensor
18-
from tensorflow.experimental.dtensor import Layout
19-
from tensorflow.keras.dtensor.experimental import LayoutMap
20-
2117
from keras_nlp.api_export import keras_nlp_export
2218
from keras_nlp.backend import keras
2319
from keras_nlp.layers.modeling.position_embedding import PositionEmbedding
@@ -190,71 +186,3 @@ def get_config(self):
190186
@classproperty
191187
def presets(cls):
192188
return copy.deepcopy(backbone_presets)
193-
194-
@classmethod
195-
def create_layout_map(cls, mesh):
196-
"""Create a DTensor layout map for a GPT2Backbone.
197-
198-
Given a DTensor mesh describing a list of devices, this method returns a
199-
DTensor layout map for creating a `keras_nlp.models.GPT2Backbone`
200-
instance. This mapping describes how to distribute all model weights
201-
across multiple devices. For an overview of DTensor concepts, see
202-
[this guide](https://www.tensorflow.org/guide/dtensor_overview).
203-
204-
Args:
205-
mesh: A 2D `tf.experimental.dtensor.Mesh` describing the arrangement
206-
of devices for running distributed computation. The
207-
first dimension in the mesh is expected to be for data parallel
208-
distribution, and the second for model parallel distribution.
209-
210-
Returns:
211-
A `tf.keras.dtensor.experimental.LayoutMap` which contains the
212-
proper layout to weights mapping for the model parallel setting.
213-
214-
Examples:
215-
```python
216-
keras.backend.experimental.enable_tf_random_generator()
217-
keras.utils.set_random_seed(1337)
218-
219-
# Update both dimensions below for a multi-device setting.
220-
mesh = dtensor.create_mesh([("batch", 1), ("model", 1)])
221-
layout_map = keras_nlp.models.GPT2Backbone.create_layout_map(mesh)
222-
223-
with layout_map.scope():
224-
model = keras_nlp.models.GPT2Backbone.from_preset("gpt2_base_en")
225-
```
226-
"""
227-
# We assert the mesh is 2D, and assume the first mesh dim is for data
228-
# parallel and the second dim is for model parallel.
229-
mesh_shape = mesh.shape()
230-
if len(mesh_shape) != 2:
231-
raise ValueError(
232-
f"Expect to create layout based on 2D mesh, received {mesh}"
233-
)
234-
_, model_dim = mesh.dim_names
235-
unshard_dim = dtensor.UNSHARDED
236-
237-
layout_map = LayoutMap(mesh=mesh)
238-
# Embedding sharding
239-
layout_map[r".*embeddings"] = Layout([unshard_dim, model_dim], mesh)
240-
241-
# Transformer block sharding
242-
layout_map[r".*_(query|key|value)_dense.kernel"] = Layout(
243-
[unshard_dim, unshard_dim, model_dim], mesh
244-
)
245-
layout_map[r".*_(query|key|value)_dense.bias"] = Layout(
246-
[model_dim, unshard_dim], mesh
247-
)
248-
layout_map[r".*_feedforward_intermediate_dense.kernel"] = Layout(
249-
[unshard_dim, model_dim], mesh
250-
)
251-
layout_map[r".*_feedforward_intermediate_dense.bias"] = Layout(
252-
[model_dim], mesh
253-
)
254-
layout_map[r".*_feedforward_output_dense.kernel"] = Layout(
255-
[model_dim, unshard_dim], mesh
256-
)
257-
layout_map[r".*_feedforward_output_dense.bias"] = Layout(
258-
[unshard_dim], mesh
259-
)
260-
return layout_map

keras_nlp/models/gpt2/gpt2_backbone_test.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,3 @@ def test_saved_model(self):
8484
# Check that output matches.
8585
restored_output = restored_model(self.input_batch)
8686
self.assertAllClose(model_output, restored_output)
87-
88-
def test_create_layout_map(self):
89-
mesh = tf.experimental.dtensor.create_mesh([("batch", 1), ("model", 1)])
90-
with GPT2Backbone.create_layout_map(mesh).scope():
91-
GPT2Backbone(
92-
vocabulary_size=10,
93-
num_layers=2,
94-
num_heads=2,
95-
hidden_dim=2,
96-
intermediate_dim=4,
97-
max_sequence_length=5,
98-
)
99-
# Using DTensor enables the mlir bridge as a side effect. Eventually
100-
# this will be default, but for now we have compile errors with the
101-
# bridge elsewhere and must disable. See
102-
# https://github.com/keras-team/keras-nlp/issues/1001
103-
tf.config.experimental.disable_mlir_bridge()

keras_nlp/models/gpt2/gpt2_causal_lm.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -325,39 +325,3 @@ def next(prompt, cache, index):
325325
"token_ids": token_ids,
326326
"padding_mask": padding_mask,
327327
}
328-
329-
@classmethod
330-
def create_layout_map(cls, mesh):
331-
"""Create a DTensor layout map for a GPT2CausalLM.
332-
333-
Given a DTensor mesh describing a list of devices, this method returns a
334-
DTensor layout map for creating a `keras_nlp.models.GPT2CausalLM`
335-
instance. This mapping describes how to distribute all model weights
336-
across multiple devices. For an overview of DTensor concepts, see
337-
[this guide](https://www.tensorflow.org/guide/dtensor_overview).
338-
339-
Args:
340-
mesh: A 2D `tf.experimental.dtensor.Mesh` describing the arrangement
341-
of devices for running distributed computation. The
342-
first dimension in the mesh is expected to be for data parallel
343-
distribution, and the second for model parallel distribution.
344-
345-
Returns:
346-
A `keras.dtensor.experimental.LayoutMap` which contains the
347-
proper layout to weights mapping for the model parallel setting.
348-
349-
Examples:
350-
```python
351-
keras.backend.experimental.enable_tf_random_generator()
352-
keras.utils.set_random_seed(1337)
353-
354-
# Update both dimensions below for a multi-device setting.
355-
mesh = tf.experimental.dtensor.create_mesh([("batch", 1), ("model", 1)])
356-
layout_map = keras_nlp.models.GPT2CausalLM.create_layout_map(mesh)
357-
358-
with layout_map.scope():
359-
gpt2_lm = keras_nlp.models.GPT2CausalLM.from_preset("gpt2_base_en")
360-
```
361-
"""
362-
# As this task has no new variables, we just re-use the backbone method.
363-
return cls.backbone_cls.create_layout_map(mesh)

keras_nlp/models/gpt2/gpt2_causal_lm_test.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,3 @@ def test_saved_model(self):
165165
keras.utils.set_random_seed(42)
166166
restored_output = restored_model.predict(self.raw_batch)
167167
self.assertAllClose(model_output, restored_output)
168-
169-
def test_create_layout_map(self):
170-
mesh = tf.experimental.dtensor.create_mesh([("batch", 1), ("model", 1)])
171-
with GPT2CausalLM.create_layout_map(mesh).scope():
172-
GPT2CausalLM(backbone=self.backbone)
173-
# Using DTensor enables the mlir bridge as a side effect. Eventually
174-
# this will be default, but for now we have compile errors with the
175-
# bridge elsewhere and must disable. See
176-
# https://github.com/keras-team/keras-nlp/issues/1001
177-
tf.config.experimental.disable_mlir_bridge()

keras_nlp/models/opt/opt_backbone.py

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
import copy
1616

17-
from tensorflow.experimental import dtensor
18-
from tensorflow.experimental.dtensor import Layout
19-
from tensorflow.keras.dtensor.experimental import LayoutMap
20-
2117
from keras_nlp.api_export import keras_nlp_export
2218
from keras_nlp.backend import keras
2319
from keras_nlp.layers.modeling.token_and_position_embedding import (
@@ -168,71 +164,3 @@ def get_config(self):
168164
@classproperty
169165
def presets(cls):
170166
return copy.deepcopy(backbone_presets)
171-
172-
@classmethod
173-
def create_layout_map(cls, mesh):
174-
"""Create a DTensor layout map for an OPTBackbone.
175-
176-
Given a DTensor mesh describing a list of devices, this method returns a
177-
DTensor layout map for creating a `keras_nlp.models.OPTBackbone`
178-
instance. This mapping describes how to distribute all model weights
179-
across multiple devices. For an overview of DTensor concepts, see
180-
[this guide](https://www.tensorflow.org/guide/dtensor_overview).
181-
182-
Args:
183-
mesh: A 2D `tf.experimental.dtensor.Mesh` describing the arrangement
184-
of devices for running distributed computation. The
185-
first dimension in the mesh is expected to be for data parallel
186-
distribution, and the second for model parallel distribution.
187-
188-
Returns:
189-
A `tf.keras.dtensor.experimental.LayoutMap` which contains the
190-
proper layout to weights mapping for the model parallel setting.
191-
192-
Examples:
193-
```python
194-
keras.backend.experimental.enable_tf_random_generator()
195-
keras.utils.set_random_seed(1337)
196-
197-
# Update both dimensions below for a multi-device setting.
198-
mesh = dtensor.create_mesh([("batch", 1), ("model", 1)])
199-
layout_map = keras_nlp.models.OPTBackbone.create_layout_map(mesh)
200-
201-
with layout_map.scope():
202-
model = keras_nlp.models.OPTBackbone.from_preset("opt_125m_en")
203-
```
204-
"""
205-
# We assert the mesh is 2D, and assume the first mesh dim is for data
206-
# parallel and the second dim is for model parallel.
207-
mesh_shape = mesh.shape()
208-
if len(mesh_shape) != 2:
209-
raise ValueError(
210-
f"Expect to create layout based on 2D mesh, received {mesh}"
211-
)
212-
_, model_dim = mesh.dim_names
213-
unshard_dim = dtensor.UNSHARDED
214-
215-
layout_map = LayoutMap(mesh=mesh)
216-
# Embedding sharding
217-
layout_map[r".*embeddings"] = Layout([unshard_dim, model_dim], mesh)
218-
219-
# Transformer block sharding
220-
layout_map[r".*_(query|key|value)_dense.kernel"] = Layout(
221-
[unshard_dim, unshard_dim, model_dim], mesh
222-
)
223-
layout_map[r".*_(query|key|value)_dense.bias"] = Layout(
224-
[model_dim, unshard_dim], mesh
225-
)
226-
layout_map[r".*_feedforward_intermediate_dense.kernel"] = Layout(
227-
[unshard_dim, model_dim], mesh
228-
)
229-
layout_map[r".*_feedforward_intermediate_dense.bias"] = Layout(
230-
[model_dim], mesh
231-
)
232-
layout_map[r".*_feedforward_output_dense.kernel"] = Layout(
233-
[model_dim, unshard_dim], mesh
234-
)
235-
layout_map[r".*_feedforward_output_dense.bias"] = Layout(
236-
[unshard_dim], mesh
237-
)
238-
return layout_map

keras_nlp/models/opt/opt_backbone_test.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,3 @@ def test_saved_model(self):
8484
# Check that output matches.
8585
restored_output = restored_model(self.input_batch)
8686
self.assertAllClose(model_output, restored_output)
87-
88-
def test_create_layout_map(self):
89-
mesh = tf.experimental.dtensor.create_mesh([("batch", 1), ("model", 1)])
90-
with OPTBackbone.create_layout_map(mesh).scope():
91-
OPTBackbone(
92-
vocabulary_size=10,
93-
num_layers=2,
94-
num_heads=2,
95-
hidden_dim=2,
96-
intermediate_dim=4,
97-
max_sequence_length=5,
98-
)
99-
# Using DTensor enables the mlir bridge as a side effect. Eventually
100-
# this will be default, but for now we have compile errors with the
101-
# bridge elsewhere and must disable. See
102-
# https://github.com/keras-team/keras-nlp/issues/1001
103-
tf.config.experimental.disable_mlir_bridge()

keras_nlp/models/opt/opt_causal_lm.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -321,39 +321,3 @@ def next(prompt, cache, index):
321321
"token_ids": token_ids,
322322
"padding_mask": padding_mask,
323323
}
324-
325-
@classmethod
326-
def create_layout_map(cls, mesh):
327-
"""Create a DTensor layout map for an OPTCausalLM.
328-
329-
Given a DTensor mesh describing a list of devices, this method returns a
330-
DTensor layout map for creating a `keras_nlp.models.OPTCausalLM`
331-
instance. This mapping describes how to distribute all model weights
332-
across multiple devices. For an overview of DTensor concepts, see
333-
[this guide](https://www.tensorflow.org/guide/dtensor_overview).
334-
335-
Args:
336-
mesh: A 2D `tf.experimental.dtensor.Mesh` describing the arrangement
337-
of devices for running distributed computation. The
338-
first dimension in the mesh is expected to be for data parallel
339-
distribution, and the second for model parallel distribution.
340-
341-
Returns:
342-
A `tf.keras.dtensor.experimental.LayoutMap` which contains the
343-
proper layout to weights mapping for the model parallel setting.
344-
345-
Examples:
346-
```python
347-
keras.backend.experimental.enable_tf_random_generator()
348-
keras.utils.set_random_seed(1337)
349-
350-
# Update both dimensions below for a multi-device setting.
351-
mesh = tf.experimental.dtensor.create_mesh([("batch", 1), ("model", 1)])
352-
layout_map = keras_nlp.models.OPTCausalLM.create_layout_map(mesh)
353-
354-
with layout_map.scope():
355-
opt_lm = keras_nlp.models.OPTCausalLM.from_preset("opt_125m_en")
356-
```
357-
"""
358-
# As this task has no new variables, we just re-use the backbone method.
359-
return cls.backbone_cls.create_layout_map(mesh)

keras_nlp/models/opt/opt_causal_lm_test.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,3 @@ def test_saved_model(self):
171171
keras.utils.set_random_seed(42)
172172
restored_output = restored_model.predict(self.raw_batch)
173173
self.assertAllClose(model_output, restored_output)
174-
175-
def test_create_layout_map(self):
176-
mesh = tf.experimental.dtensor.create_mesh([("batch", 1), ("model", 1)])
177-
with OPTCausalLM.create_layout_map(mesh).scope():
178-
OPTCausalLM(backbone=self.backbone)
179-
# Using DTensor enables the mlir bridge as a side effect. Eventually
180-
# this will be default, but for now we have compile errors with the
181-
# bridge elsewhere and must disable. See
182-
# https://github.com/keras-team/keras-nlp/issues/1001
183-
tf.config.experimental.disable_mlir_bridge()

0 commit comments

Comments
 (0)