"git@developer.sourcefind.cn:zhaoyu6/sglang.git" did not exist on "7623091d9769f074680beefcdf23a6fb2ecac753"
Unverified Commit 8b641b13 authored by Srihari Humbarwadi's avatar Srihari Humbarwadi Committed by GitHub
Browse files

Merge branch 'tensorflow:master' into panoptic-deeplab

parents 7cffacfe 357fa547
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
"""Configuration definitions for ResNet losses, learning rates, and optimizers.""" """Configuration definitions for ResNet losses, learning rates, and optimizers."""
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import division from __future__ import division
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
"""Configuration definitions for VGG losses, learning rates, and optimizers.""" """Configuration definitions for VGG losses, learning rates, and optimizers."""
import dataclasses import dataclasses
......
...@@ -23,9 +23,11 @@ from official.core import task_factory ...@@ -23,9 +23,11 @@ from official.core import task_factory
from official.modeling import optimization from official.modeling import optimization
from official.modeling.multitask import base_model from official.modeling.multitask import base_model
from official.modeling.multitask import configs from official.modeling.multitask import configs
from official.modeling.privacy import configs as dp_configs
OptimizationConfig = optimization.OptimizationConfig OptimizationConfig = optimization.OptimizationConfig
RuntimeConfig = config_definitions.RuntimeConfig RuntimeConfig = config_definitions.RuntimeConfig
DifferentialPrivacyConfig = dp_configs.DifferentialPrivacyConfig
class MultiTask(tf.Module, metaclass=abc.ABCMeta): class MultiTask(tf.Module, metaclass=abc.ABCMeta):
...@@ -93,9 +95,11 @@ class MultiTask(tf.Module, metaclass=abc.ABCMeta): ...@@ -93,9 +95,11 @@ class MultiTask(tf.Module, metaclass=abc.ABCMeta):
@classmethod @classmethod
def create_optimizer(cls, def create_optimizer(cls,
optimizer_config: OptimizationConfig, optimizer_config: OptimizationConfig,
runtime_config: Optional[RuntimeConfig] = None): runtime_config: Optional[RuntimeConfig] = None,
dp_config: Optional[DifferentialPrivacyConfig] = None):
return base_task.Task.create_optimizer( return base_task.Task.create_optimizer(
optimizer_config=optimizer_config, runtime_config=runtime_config) optimizer_config=optimizer_config, runtime_config=runtime_config,
dp_config=dp_config)
def joint_train_step(self, task_inputs, def joint_train_step(self, task_inputs,
multi_task_model: base_model.MultiTaskBaseModel, multi_task_model: base_model.MultiTaskBaseModel,
......
...@@ -66,8 +66,7 @@ def run_experiment( ...@@ -66,8 +66,7 @@ def run_experiment(
is_training = 'train' in mode is_training = 'train' in mode
is_eval = 'eval' in mode is_eval = 'eval' in mode
with distribution_strategy.scope(): with distribution_strategy.scope():
optimizer = task.create_optimizer(params.trainer.optimizer_config, optimizer = train_utils.create_optimizer(task, params)
params.runtime)
kwargs = dict(multi_task=task, multi_task_model=model, optimizer=optimizer) kwargs = dict(multi_task=task, multi_task_model=model, optimizer=optimizer)
if params.trainer.trainer_type == 'interleaving': if params.trainer.trainer_type == 'interleaving':
sampler = task_sampler.get_task_sampler(params.trainer.task_sampler, sampler = task_sampler.get_task_sampler(params.trainer.task_sampler,
...@@ -183,8 +182,7 @@ def run_experiment_with_multitask_eval( ...@@ -183,8 +182,7 @@ def run_experiment_with_multitask_eval(
config=params, config=params,
task=train_task, task=train_task,
model=train_task.build_model(), model=train_task.build_model(),
optimizer=train_task.create_optimizer(params.trainer.optimizer_config, optimizer=train_utils.create_optimizer(train_task, params),
params.runtime),
train=True, train=True,
evaluate=False) evaluate=False)
else: else:
......
...@@ -28,9 +28,9 @@ from official.nlp import optimization as nlp_optimization ...@@ -28,9 +28,9 @@ from official.nlp import optimization as nlp_optimization
OPTIMIZERS_CLS = { OPTIMIZERS_CLS = {
'sgd': tf.keras.optimizers.SGD, 'sgd': tf.keras.optimizers.SGD,
'sgd_experimental': tf.keras.optimizers.experimental.SGD, # TODO(chenmoneygithub): experimental.SGD
'adam': tf.keras.optimizers.Adam, 'adam': tf.keras.optimizers.Adam,
'adam_experimental': tf.keras.optimizers.experimental.Adam, # TODO(chenmoneygithub): experimental.Adam
'adamw': nlp_optimization.AdamWeightDecay, 'adamw': nlp_optimization.AdamWeightDecay,
'lamb': tfa_optimizers.LAMB, 'lamb': tfa_optimizers.LAMB,
'rmsprop': tf.keras.optimizers.RMSprop, 'rmsprop': tf.keras.optimizers.RMSprop,
......
...@@ -12,9 +12,13 @@ ...@@ -12,9 +12,13 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3 """Configs for differential privacy."""
"""Decoders package definition."""
from official.vision.beta.modeling.decoders.aspp import ASPP from official.modeling.hyperparams import base_config
from official.vision.beta.modeling.decoders.fpn import FPN
from official.vision.beta.modeling.decoders.nasfpn import NASFPN
class DifferentialPrivacyConfig(base_config.Config):
# Applied to the gradients
# Setting to a large number so nothing is clipped.
clipping_norm: float = 100000000.0 # 10^9
noise_multiplier: float = 0.0
...@@ -12,30 +12,29 @@ ...@@ -12,30 +12,29 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""Tests for roi_aligner.py.""" """Tests for configs."""
# Import libraries
import tensorflow as tf import tensorflow as tf
from official.modeling.privacy import configs
from official.vision.beta.modeling.layers import roi_aligner
class ConfigsTest(tf.test.TestCase):
class MultilevelROIAlignerTest(tf.test.TestCase): def test_clipping_norm_default(self):
clipping_norm = configs.DifferentialPrivacyConfig().clipping_norm
self.assertEqual(100000000.0, clipping_norm)
def test_serialize_deserialize(self): def test_noise_multiplier_default(self):
kwargs = dict( noise_multiplier = configs.DifferentialPrivacyConfig().noise_multiplier
crop_size=7, self.assertEqual(0.0, noise_multiplier)
sample_offset=0.5,
)
aligner = roi_aligner.MultilevelROIAligner(**kwargs)
expected_config = dict(kwargs) def test_config(self):
self.assertEqual(aligner.get_config(), expected_config) dp_config = configs.DifferentialPrivacyConfig({
'clipping_norm': 1.0,
new_aligner = roi_aligner.MultilevelROIAligner.from_config( 'noise_multiplier': 1.0
aligner.get_config()) })
self.assertEqual(1.0, dp_config.clipping_norm)
self.assertAllEqual(aligner.get_config(), new_aligner.get_config()) self.assertEqual(1.0, dp_config.noise_multiplier)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -12,27 +12,31 @@ ...@@ -12,27 +12,31 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""TFDS Classification decoders.""" """Ops for differential privacy (gradient) transforms."""
from typing import List, Tuple
import tensorflow as tf import tensorflow as tf
from official.vision.beta.dataloaders import decoder
class ClassificationDecorder(decoder.Decoder): def clip_l2_norm(grads_vars: List[Tuple[tf.Tensor, tf.Tensor]],
"""A tf.Example decoder for tfds classification datasets.""" l2_norm_clip: float) -> List[Tuple[tf.Tensor, tf.Tensor]]:
"""Clip gradients by global norm."""
def decode(self, serialized_example): gradients = []
sample_dict = { variables = []
'image/encoded': for (g, v) in grads_vars:
tf.io.encode_jpeg(serialized_example['image'], quality=100), gradients.append(g)
'image/class/label': variables.append(v)
serialized_example['label'], clipped_gradients = tf.clip_by_global_norm(gradients, l2_norm_clip)[0]
} return list(zip(clipped_gradients, variables))
return sample_dict
TFDS_ID_TO_DECODER_MAP = { def add_noise(grads_vars: List[Tuple[tf.Tensor, tf.Tensor]],
'cifar10': ClassificationDecorder, noise_stddev: float) -> List[Tuple[tf.Tensor, tf.Tensor]]:
'cifar100': ClassificationDecorder, """Add noise to gradients."""
'imagenet2012': ClassificationDecorder, ret = []
} for (g, v) in grads_vars:
noise = tf.random.normal(tf.shape(g), stddev=noise_stddev)
ret.append((g + noise, v))
return ret
...@@ -12,43 +12,40 @@ ...@@ -12,43 +12,40 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""Tests for ops."""
"""Tests for mask_ops.py.""" from unittest import mock
# Import libraries
import numpy as np
import tensorflow as tf import tensorflow as tf
from official.vision.beta.ops import mask_ops
from official.modeling.privacy import ops
class MaskUtilsTest(tf.test.TestCase):
def testPasteInstanceMasks(self): class OpsTest(tf.test.TestCase):
image_height = 10
image_width = 10 def test_clip_l2_norm(self):
mask_height = 6 x = tf.constant([4.0, 3.0])
mask_width = 6 y = tf.constant([[12.0]])
masks = np.random.randint(0, 255, (1, mask_height, mask_width)) tensors = [(x, x), (y, y)]
detected_boxes = np.array([[0.0, 2.0, mask_width, mask_height]]) clipped = ops.clip_l2_norm(tensors, 1.0)
for a, b in zip(clipped, tensors):
_ = mask_ops.paste_instance_masks( self.assertAllClose(a[0], b[0] / 13.0) # sqrt(4^2 + 3^2 + 12 ^3) = 13
masks, detected_boxes, image_height, image_width) self.assertAllClose(a[1], b[1])
def testPasteInstanceMasksV2(self): @mock.patch.object(tf.random,
image_height = 10 'normal',
image_width = 10 autospec=True)
mask_height = 6 def test_add_noise(self, mock_random):
mask_width = 6 x = tf.constant([0.0, 0.0])
masks = np.random.randint(0, 255, (1, mask_height, mask_width)) y = tf.constant([[0.0]])
detected_boxes = np.array([[0.0, 2.0, mask_width, mask_height]]) tensors = [(x, x), (y, y)]
mock_random.side_effect = [tf.constant([1.0, 1.0]), tf.constant([[1.0]])]
image_masks = mask_ops.paste_instance_masks_v2( added = ops.add_noise(tensors, 10.0)
masks, detected_boxes, image_height, image_width) for a, b in zip(added, tensors):
self.assertAllClose(a[0], b[0] + 1.0)
self.assertNDArrayNear( self.assertAllClose(a[1], b[1])
image_masks[:, 2:8, 0:6], _, kwargs = mock_random.call_args
np.array(masks > 0.5, dtype=np.uint8), self.assertEqual(kwargs['stddev'], 10.0)
1e-5)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
# pylint: disable=g-doc-return-or-yield,line-too-long # pylint: disable=g-doc-return-or-yield,line-too-long
"""WMT translation configurations.""" """WMT translation configurations."""
......
...@@ -124,7 +124,7 @@ class DualEncoderDataLoader(data_loader.DataLoader): ...@@ -124,7 +124,7 @@ class DualEncoderDataLoader(data_loader.DataLoader):
raise ValueError('Expected {} to start with {}'.format(string, old)) raise ValueError('Expected {} to start with {}'.format(string, old))
def _switch_key_prefix(d, old, new): def _switch_key_prefix(d, old, new):
return {_switch_prefix(key, old, new): value for key, value in d.items()} return {_switch_prefix(key, old, new): value for key, value in d.items()} # pytype: disable=attribute-error # trace-all-classes
model_inputs = _switch_key_prefix( model_inputs = _switch_key_prefix(
self._bert_tokenize(record, self._left_text_fields), self._bert_tokenize(record, self._left_text_fields),
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
"""Definitions for random feature Gaussian process layer.""" """Definitions for random feature Gaussian process layer."""
import math import math
import tensorflow as tf import tensorflow as tf
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
"""Tests for Gaussian process functions.""" """Tests for Gaussian process functions."""
import os import os
import shutil import shutil
......
...@@ -1004,6 +1004,7 @@ class T5TransformerParams: ...@@ -1004,6 +1004,7 @@ class T5TransformerParams:
num_heads: int num_heads: int
d_ff: int d_ff: int
vocab_size: int vocab_size: int
target_vocab_size: Optional[int] = None
dropout_rate: float = 0.0 dropout_rate: float = 0.0
layer_norm_epsilon: float = 1e-6 layer_norm_epsilon: float = 1e-6
shared_embedding: bool = False shared_embedding: bool = False
...@@ -1159,11 +1160,15 @@ class Decoder(Module): ...@@ -1159,11 +1160,15 @@ class Decoder(Module):
self.compute_dtype = compute_dtype self.compute_dtype = compute_dtype
if self.config.num_decoder_layers is None: if self.config.num_decoder_layers is None:
self.config.num_decoder_layers = self.config.num_layers self.config.num_decoder_layers = self.config.num_layers
if not hasattr(
self.config,
"target_vocab_size") or self.config.target_vocab_size is None:
self.config.target_vocab_size = self.config.vocab_size
with self.name_scope: with self.name_scope:
# Target Embedding. # Target Embedding.
if shared_embedding is None: if shared_embedding is None:
self.target_embed = Embed( self.target_embed = Embed(
vocab_size=self.config.vocab_size, vocab_size=self.config.target_vocab_size,
features=self.config.d_model, features=self.config.d_model,
embeddings_initializer=self.config.vocab_embeddings_initializer, embeddings_initializer=self.config.vocab_embeddings_initializer,
dtype=self.dtype, dtype=self.dtype,
...@@ -1211,7 +1216,7 @@ class Decoder(Module): ...@@ -1211,7 +1216,7 @@ class Decoder(Module):
if not self.config.logits_via_embedding: if not self.config.logits_via_embedding:
self.logits_dense = Linear( self.logits_dense = Linear(
in_features=self.config.d_model, in_features=self.config.d_model,
out_features=self.config.vocab_size, out_features=self.config.target_vocab_size,
use_bias=False, use_bias=False,
dtype=self.dtype, dtype=self.dtype,
name="logits") name="logits")
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
"""Definitions for AssembleNet/++ structures. """Definitions for AssembleNet/++ structures.
This structure is a `list` corresponding to a graph representation of the This structure is a `list` corresponding to a graph representation of the
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
from absl.testing import parameterized from absl.testing import parameterized
import tensorflow as tf import tensorflow as tf
from official.core import config_definitions as cfg from official.core import config_definitions as cfg
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
"""Contains definitions for the AssembleNet [1] models. """Contains definitions for the AssembleNet [1] models.
Requires the AssembleNet architecture to be specified in Requires the AssembleNet architecture to be specified in
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
"""Contains definitions for 'Representation Flow' layer [1]. """Contains definitions for 'Representation Flow' layer [1].
Representation flow layer is a generalization of optical flow extraction; the Representation flow layer is a generalization of optical flow extraction; the
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Lint as: python3
r"""Training driver. r"""Training driver.
Commandline: Commandline:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment