repo
stringlengths
2
99
file
stringlengths
13
225
code
stringlengths
0
18.3M
file_length
int64
0
18.3M
avg_line_length
float64
0
1.36M
max_line_length
int64
0
4.26M
extension_type
stringclasses
1 value
models
models-master/official/modeling/hyperparams/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Hyperparams package definition.""" # pylint: disable=g-multiple-import from official.modeling.hyperparams.base_config import * from official.modeling.hyperparams.oneof import * from official.modeling.hyperparams.params_dict import *
846
39.333333
74
py
models
models-master/official/modeling/hyperparams/params_dict.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """A parameter dictionary class which supports the nest structure.""" import collections import copy import re import six import tensorflow as tf import yaml # regex pattern that matches on key-value pairs in a comma-separated # key-value pair string. It splits each k-v pair on the = sign, and # matches on values that are within single quotes, double quotes, single # values (e.g. floats, ints, etc.), and a lists within brackets. _PARAM_RE = re.compile( r""" (?P<name>[a-zA-Z][\w\.]*)(?P<bracketed_index>\[?[0-9]*\]?) # variable name: "var" or "x" followed by optional index: "[0]" or "[23]" \s*=\s* ((?P<val>\'(.*?)\' # single quote | \"(.*?)\" # double quote | [^,\[]* # single value | \[[^\]]*\])) # list of values ($|,\s*)""", re.VERBOSE) _CONST_VALUE_RE = re.compile(r'(\d.*|-\d.*|None)') # Yaml LOADER with an implicit resolver to parse float decimal and exponential # format. The regular experission parse the following cases: # 1- Decimal number with an optional exponential term. # 2- Integer number with an exponential term. # 3- Decimal number with an optional exponential term. # 4- Decimal number. _LOADER = yaml.SafeLoader _LOADER.add_implicit_resolver( 'tag:yaml.org,2002:float', re.compile(r''' ^(?:[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)? | [-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+) | \\.[0-9_]+(?:[eE][-+][0-9]+)? | [-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*)$''', re.X), list('-+0123456789.')) class ParamsDict(object): """A hyperparameter container class.""" RESERVED_ATTR = ['_locked', '_restrictions'] def __init__(self, default_params=None, restrictions=None): """Instantiate a ParamsDict. Instantiate a ParamsDict given a set of default parameters and a list of restrictions. Upon initialization, it validates itself by checking all the defined restrictions, and raise error if it finds inconsistency. Args: default_params: a Python dict or another ParamsDict object including the default parameters to initialize. restrictions: a list of strings, which define a list of restrictions to ensure the consistency of different parameters internally. Each restriction string is defined as a binary relation with a set of operators, including {'==', '!=', '<', '<=', '>', '>='}. """ self._locked = False self._restrictions = [] if restrictions: self._restrictions = restrictions if default_params is None: default_params = {} self.override(default_params, is_strict=False) def _set(self, k, v): if isinstance(v, dict): self.__dict__[k] = ParamsDict(v) else: self.__dict__[k] = copy.deepcopy(v) def __setattr__(self, k, v): """Sets the value of the existing key. Note that this does not allow directly defining a new key. Use the `override` method with `is_strict=False` instead. Args: k: the key string. v: the value to be used to set the key `k`. Raises: KeyError: if k is not defined in the ParamsDict. """ if k not in ParamsDict.RESERVED_ATTR: if k not in self.__dict__.keys(): raise KeyError('The key `%{}` does not exist. ' 'To extend the existing keys, use ' '`override` with `is_strict` = True.'.format(k)) if self._locked: raise ValueError('The ParamsDict has been locked. ' 'No change is allowed.') self._set(k, v) def __getattr__(self, k): """Gets the value of the existing key. Args: k: the key string. Returns: the value of the key. Raises: AttributeError: if k is not defined in the ParamsDict. """ if k not in self.__dict__.keys(): raise AttributeError('The key `{}` does not exist. '.format(k)) return self.__dict__[k] def __contains__(self, key): """Implements the membership test operator.""" return key in self.__dict__ def get(self, key, value=None): """Accesses through built-in dictionary get method.""" return self.__dict__.get(key, value) def __delattr__(self, k): """Deletes the key and removes its values. Args: k: the key string. Raises: AttributeError: if k is reserverd or not defined in the ParamsDict. ValueError: if the ParamsDict instance has been locked. """ if k in ParamsDict.RESERVED_ATTR: raise AttributeError( 'The key `{}` is reserved. No change is allowes. '.format(k)) if k not in self.__dict__.keys(): raise AttributeError('The key `{}` does not exist. '.format(k)) if self._locked: raise ValueError('The ParamsDict has been locked. No change is allowed.') del self.__dict__[k] def override(self, override_params, is_strict=True): """Override the ParamsDict with a set of given params. Args: override_params: a dict or a ParamsDict specifying the parameters to be overridden. is_strict: a boolean specifying whether override is strict or not. If True, keys in `override_params` must be present in the ParamsDict. If False, keys in `override_params` can be different from what is currently defined in the ParamsDict. In this case, the ParamsDict will be extended to include the new keys. """ if self._locked: raise ValueError('The ParamsDict has been locked. No change is allowed.') if isinstance(override_params, ParamsDict): override_params = override_params.as_dict() self._override(override_params, is_strict) # pylint: disable=protected-access def _override(self, override_dict, is_strict=True): """The implementation of `override`.""" for k, v in six.iteritems(override_dict): if k in ParamsDict.RESERVED_ATTR: raise KeyError('The key `%{}` is internally reserved. ' 'Can not be overridden.') if k not in self.__dict__.keys(): if is_strict: raise KeyError('The key `{}` does not exist. ' 'To extend the existing keys, use ' '`override` with `is_strict` = False.'.format(k)) else: self._set(k, v) else: if isinstance(v, dict): self.__dict__[k]._override(v, is_strict) # pylint: disable=protected-access elif isinstance(v, ParamsDict): self.__dict__[k]._override(v.as_dict(), is_strict) # pylint: disable=protected-access else: self.__dict__[k] = copy.deepcopy(v) def lock(self): """Makes the ParamsDict immutable.""" self._locked = True def as_dict(self): """Returns a dict representation of ParamsDict. For the nested ParamsDict, a nested dict will be returned. """ params_dict = {} for k, v in six.iteritems(self.__dict__): if k not in ParamsDict.RESERVED_ATTR: if isinstance(v, ParamsDict): params_dict[k] = v.as_dict() else: params_dict[k] = copy.deepcopy(v) return params_dict def validate(self): """Validate the parameters consistency based on the restrictions. This method validates the internal consistency using the pre-defined list of restrictions. A restriction is defined as a string which specifies a binary operation. The supported binary operations are {'==', '!=', '<', '<=', '>', '>='}. Note that the meaning of these operators are consistent with the underlying Python immplementation. Users should make sure the define restrictions on their type make sense. For example, for a ParamsDict like the following ``` a: a1: 1 a2: 2 b: bb: bb1: 10 bb2: 20 ccc: a1: 1 a3: 3 ``` one can define two restrictions like this ['a.a1 == b.ccc.a1', 'a.a2 <= b.bb.bb2'] What it enforces are: - a.a1 = 1 == b.ccc.a1 = 1 - a.a2 = 2 <= b.bb.bb2 = 20 Raises: KeyError: if any of the following happens (1) any of parameters in any of restrictions is not defined in ParamsDict, (2) any inconsistency violating the restriction is found. ValueError: if the restriction defined in the string is not supported. """ def _get_kv(dotted_string, params_dict): """Get keys and values indicated by dotted_string.""" if _CONST_VALUE_RE.match(dotted_string) is not None: const_str = dotted_string if const_str == 'None': constant = None else: constant = float(const_str) return None, constant else: tokenized_params = dotted_string.split('.') v = params_dict for t in tokenized_params: v = v[t] return tokenized_params[-1], v def _get_kvs(tokens, params_dict): if len(tokens) != 2: raise ValueError('Only support binary relation in restriction.') stripped_tokens = [t.strip() for t in tokens] left_k, left_v = _get_kv(stripped_tokens[0], params_dict) right_k, right_v = _get_kv(stripped_tokens[1], params_dict) return left_k, left_v, right_k, right_v params_dict = self.as_dict() for restriction in self._restrictions: if '==' in restriction: tokens = restriction.split('==') _, left_v, _, right_v = _get_kvs(tokens, params_dict) if left_v != right_v: raise KeyError( 'Found inconsistency between key `{}` and key `{}`.'.format( tokens[0], tokens[1])) elif '!=' in restriction: tokens = restriction.split('!=') _, left_v, _, right_v = _get_kvs(tokens, params_dict) if left_v == right_v: raise KeyError( 'Found inconsistency between key `{}` and key `{}`.'.format( tokens[0], tokens[1])) elif '<' in restriction: tokens = restriction.split('<') _, left_v, _, right_v = _get_kvs(tokens, params_dict) if left_v >= right_v: raise KeyError( 'Found inconsistency between key `{}` and key `{}`.'.format( tokens[0], tokens[1])) elif '<=' in restriction: tokens = restriction.split('<=') _, left_v, _, right_v = _get_kvs(tokens, params_dict) if left_v > right_v: raise KeyError( 'Found inconsistency between key `{}` and key `{}`.'.format( tokens[0], tokens[1])) elif '>' in restriction: tokens = restriction.split('>') _, left_v, _, right_v = _get_kvs(tokens, params_dict) if left_v <= right_v: raise KeyError( 'Found inconsistency between key `{}` and key `{}`.'.format( tokens[0], tokens[1])) elif '>=' in restriction: tokens = restriction.split('>=') _, left_v, _, right_v = _get_kvs(tokens, params_dict) if left_v < right_v: raise KeyError( 'Found inconsistency between key `{}` and key `{}`.'.format( tokens[0], tokens[1])) else: raise ValueError('Unsupported relation in restriction.') def read_yaml_to_params_dict(file_path: str): """Reads a YAML file to a ParamsDict.""" with tf.io.gfile.GFile(file_path, 'r') as f: params_dict = yaml.load(f, Loader=_LOADER) return ParamsDict(params_dict) def save_params_dict_to_yaml(params, file_path): """Saves the input ParamsDict to a YAML file.""" with tf.io.gfile.GFile(file_path, 'w') as f: def _my_list_rep(dumper, data): # u'tag:yaml.org,2002:seq' is the YAML internal tag for sequence. return dumper.represent_sequence( u'tag:yaml.org,2002:seq', data, flow_style=True) yaml.add_representer(list, _my_list_rep) yaml.dump(params.as_dict(), f, default_flow_style=False) def nested_csv_str_to_json_str(csv_str): """Converts a nested (using '.') comma-separated k=v string to a JSON string. Converts a comma-separated string of key/value pairs that supports nesting of keys to a JSON string. Nesting is implemented using '.' between levels for a given key. Spacing between commas and = is supported (e.g. there is no difference between "a=1,b=2", "a = 1, b = 2", or "a=1, b=2") but there should be no spaces before keys or after values (e.g. " a=1,b=2" and "a=1,b=2 " are not supported). Note that this will only support values supported by CSV, meaning values such as nested lists (e.g. "a=[[1,2,3],[4,5,6]]") are not supported. Strings are supported as well, e.g. "a='hello'". An example conversion would be: "a=1, b=2, c.a=2, c.b=3, d.a.a=5" to "{ a: 1, b : 2, c: {a : 2, b : 3}, d: {a: {a : 5}}}" Args: csv_str: the comma separated string. Returns: the converted JSON string. Raises: ValueError: If csv_str is not in a comma separated string or if the string is formatted incorrectly. """ if not csv_str: return '' array_param_map = collections.defaultdict(str) max_index_map = collections.defaultdict(str) formatted_entries = [] nested_map = collections.defaultdict(list) pos = 0 while pos < len(csv_str): m = _PARAM_RE.match(csv_str, pos) if not m: raise ValueError('Malformed hyperparameter value while parsing ' 'CSV string: %s' % csv_str[pos:]) pos = m.end() # Parse the values. m_dict = m.groupdict() name = m_dict['name'] v = m_dict['val'] bracketed_index = m_dict['bracketed_index'] # If we reach the name of the array. if bracketed_index and '.' not in name: # Extract the array's index by removing '[' and ']' index = int(bracketed_index[1:-1]) if '.' in v: numeric_val = float(v) else: numeric_val = int(v) # Add the value to the array. if name not in array_param_map: max_index_map[name] = index array_param_map[name] = [None] * (index + 1) array_param_map[name][index] = numeric_val elif index < max_index_map[name]: array_param_map[name][index] = numeric_val else: array_param_map[name] += [None] * (index - max_index_map[name]) array_param_map[name][index] = numeric_val max_index_map[name] = index continue # If a GCS path (e.g. gs://...) is provided, wrap this in quotes # as yaml.load would otherwise throw an exception if re.match(r'(?=[^\"\'])(?=[gs://])', v): v = '\'{}\''.format(v) name_nested = name.split('.') if len(name_nested) > 1: grouping = name_nested[0] if bracketed_index: value = '.'.join(name_nested[1:]) + bracketed_index + '=' + v else: value = '.'.join(name_nested[1:]) + '=' + v nested_map[grouping].append(value) else: formatted_entries.append('%s : %s' % (name, v)) for grouping, value in nested_map.items(): value = ','.join(value) value = nested_csv_str_to_json_str(value) formatted_entries.append('%s : %s' % (grouping, value)) # Add array parameters and check that the array is fully initialized. for name in array_param_map: if any(v is None for v in array_param_map[name]): raise ValueError('Did not pass all values of array: %s' % name) formatted_entries.append('%s : %s' % (name, array_param_map[name])) return '{' + ', '.join(formatted_entries) + '}' def override_params_dict(params, dict_or_string_or_yaml_file, is_strict): """Override a given ParamsDict using a dict, JSON/YAML/CSV string or YAML file. The logic of the function is outlined below: 1. Test that the input is a dict. If not, proceed to 2. 2. Tests that the input is a string. If not, raise unknown ValueError 2.1. Test if the string is in a CSV format. If so, parse. If not, proceed to 2.2. 2.2. Try loading the string as a YAML/JSON. If successful, parse to dict and use it to override. If not, proceed to 2.3. 2.3. Try using the string as a file path and load the YAML file. Args: params: a ParamsDict object to be overridden. dict_or_string_or_yaml_file: a Python dict, JSON/YAML/CSV string or path to a YAML file specifying the parameters to be overridden. is_strict: a boolean specifying whether override is strict or not. Returns: params: the overridden ParamsDict object. Raises: ValueError: if failed to override the parameters. """ if not dict_or_string_or_yaml_file: return params if isinstance(dict_or_string_or_yaml_file, dict): params.override(dict_or_string_or_yaml_file, is_strict) elif isinstance(dict_or_string_or_yaml_file, six.string_types): try: dict_or_string_or_yaml_file = ( nested_csv_str_to_json_str(dict_or_string_or_yaml_file)) except ValueError: pass params_dict = yaml.load(dict_or_string_or_yaml_file, Loader=_LOADER) if isinstance(params_dict, dict): params.override(params_dict, is_strict) else: with tf.io.gfile.GFile(dict_or_string_or_yaml_file) as f: params.override(yaml.load(f, Loader=yaml.FullLoader), is_strict) else: raise ValueError('Unknown input type to parse.') return params
17,808
34.761044
135
py
models
models-master/official/modeling/hyperparams/params_dict_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for params_dict.py.""" import os import tensorflow as tf import yaml from official.modeling.hyperparams import params_dict class ParamsDictTest(tf.test.TestCase): def test_init_from_an_empty_dict(self): params = params_dict.ParamsDict() with self.assertRaises(AttributeError): _ = params.a with self.assertRaises(KeyError): params.a = 'aa' def test_init_from_a_dict(self): params = params_dict.ParamsDict({'a': 'aa', 'b': 2}) self.assertEqual(params.a, 'aa') self.assertEqual(params.b, 2) def test_init_from_a_param_dict(self): params_init = params_dict.ParamsDict({'a': 'aa', 'b': 2}) params = params_dict.ParamsDict(params_init) self.assertEqual(params.a, 'aa') self.assertEqual(params.b, 2) def test_lock(self): params = params_dict.ParamsDict({'a': 1, 'b': 2, 'c': 3}) params.lock() with self.assertRaises(ValueError): params.a = 10 with self.assertRaises(ValueError): params.override({'b': 20}) with self.assertRaises(ValueError): del params.c def test_setattr(self): params = params_dict.ParamsDict() params.override({'a': 'aa', 'b': 2, 'c': None}, is_strict=False) params.c = 'ccc' self.assertEqual(params.a, 'aa') self.assertEqual(params.b, 2) self.assertEqual(params.c, 'ccc') def test_getattr(self): params = params_dict.ParamsDict() params.override({'a': 'aa', 'b': 2, 'c': None}, is_strict=False) self.assertEqual(params.a, 'aa') self.assertEqual(params.b, 2) self.assertEqual(params.c, None) def test_delattr(self): params = params_dict.ParamsDict() params.override({ 'a': 'aa', 'b': 2, 'c': None, 'd': { 'd1': 1, 'd2': 10 } }, is_strict=False) del params.c self.assertEqual(params.a, 'aa') self.assertEqual(params.b, 2) with self.assertRaises(AttributeError): _ = params.c del params.d with self.assertRaises(AttributeError): _ = params.d.d1 def test_contains(self): params = params_dict.ParamsDict() params.override({'a': 'aa'}, is_strict=False) self.assertIn('a', params) self.assertNotIn('b', params) def test_get(self): params = params_dict.ParamsDict() params.override({'a': 'aa'}, is_strict=False) self.assertEqual(params.get('a'), 'aa') self.assertEqual(params.get('b', 2), 2) self.assertEqual(params.get('b'), None) def test_override_is_strict_true(self): params = params_dict.ParamsDict({ 'a': 'aa', 'b': 2, 'c': { 'c1': 'cc', 'c2': 20 } }) params.override({'a': 2, 'c': {'c1': 'ccc'}}, is_strict=True) self.assertEqual(params.a, 2) self.assertEqual(params.c.c1, 'ccc') with self.assertRaises(KeyError): params.override({'d': 'ddd'}, is_strict=True) with self.assertRaises(KeyError): params.override({'c': {'c3': 30}}, is_strict=True) def test_override_is_strict_false(self): params = params_dict.ParamsDict({ 'a': 'aa', 'b': 2, 'c': { 'c1': 10, 'c2': 20 } }) params.override({'a': 2, 'c': {'c3': 3000}}, is_strict=False) self.assertEqual(params.a, 2) self.assertEqual(params.c.c3, 3000) params.override({'d': 'ddd'}, is_strict=False) self.assertEqual(params.d, 'ddd') params.override({'c': {'c4': 4444}}, is_strict=False) self.assertEqual(params.c.c4, 4444) def test_as_dict(self): params = params_dict.ParamsDict({ 'a': 'aa', 'b': 2, 'c': { 'c1': 10, 'c2': 20 } }) params_d = params.as_dict() self.assertEqual(params_d['a'], 'aa') self.assertEqual(params_d['b'], 2) self.assertEqual(params_d['c']['c1'], 10) self.assertEqual(params_d['c']['c2'], 20) def test_validate(self): # Raise error due to the unknown parameter. with self.assertRaises(KeyError): params = params_dict.ParamsDict({'a': 1, 'b': {'a': 11}}, ['a == c']) params.validate() # OK to check equality of two nested dicts. params = params_dict.ParamsDict({ 'a': 1, 'b': { 'a': 10 }, 'c': { 'a': 10 } }, ['b == c']) # Raise error due to inconsistency with self.assertRaises(KeyError): params = params_dict.ParamsDict({'a': 1, 'c': {'a': 10}}, ['a == c.a']) params.validate() # Valid rule. params = params_dict.ParamsDict({'a': 1, 'c': {'a': 1}}, ['a == c.a']) # Overriding violates the existing rule, raise error upon validate. params.override({'a': 11}) with self.assertRaises(KeyError): params.validate() # Valid restrictions with constant. params = params_dict.ParamsDict({ 'a': None, 'c': { 'a': 1 } }, ['a == None', 'c.a == 1']) params.validate() with self.assertRaises(KeyError): params = params_dict.ParamsDict({ 'a': 4, 'c': { 'a': 1 } }, ['a == None', 'c.a == 1']) params.validate() class ParamsDictIOTest(tf.test.TestCase): def write_temp_file(self, filename, text): temp_file = os.path.join(self.get_temp_dir(), filename) with tf.io.gfile.GFile(temp_file, 'w') as writer: writer.write(text) return temp_file def test_save_params_dict_to_yaml(self): params = params_dict.ParamsDict({ 'a': 'aa', 'b': 2, 'c': { 'c1': 10, 'c2': 20 } }) output_yaml_file = os.path.join(self.get_temp_dir(), 'params.yaml') params_dict.save_params_dict_to_yaml(params, output_yaml_file) with tf.io.gfile.GFile(output_yaml_file, 'r') as f: params_d = yaml.load(f) self.assertEqual(params.a, params_d['a']) self.assertEqual(params.b, params_d['b']) self.assertEqual(params.c.c1, params_d['c']['c1']) self.assertEqual(params.c.c2, params_d['c']['c2']) def test_read_yaml_to_params_dict(self): input_yaml_file = self.write_temp_file( 'params.yaml', r""" a: 'aa' b: 2 c: c1: 10 c2: 20 """) params = params_dict.read_yaml_to_params_dict(input_yaml_file) self.assertEqual(params.a, 'aa') self.assertEqual(params.b, 2) self.assertEqual(params.c.c1, 10) self.assertEqual(params.c.c2, 20) def test_override_params_dict_using_dict(self): params = params_dict.ParamsDict({ 'a': 1, 'b': 2.5, 'c': [3, 4], 'd': 'hello', 'e': False }) override_dict = {'b': 5.2, 'c': [30, 40]} params = params_dict.override_params_dict( params, override_dict, is_strict=True) self.assertEqual(1, params.a) self.assertEqual(5.2, params.b) self.assertEqual([30, 40], params.c) self.assertEqual('hello', params.d) self.assertEqual(False, params.e) def test_override_params_dict_using_yaml_string(self): params = params_dict.ParamsDict({ 'a': 1, 'b': 2.5, 'c': [3, 4], 'd': 'hello', 'e': False }) override_yaml_string = "'b': 5.2\n'c': [30, 40]" params = params_dict.override_params_dict( params, override_yaml_string, is_strict=True) self.assertEqual(1, params.a) self.assertEqual(5.2, params.b) self.assertEqual([30, 40], params.c) self.assertEqual('hello', params.d) self.assertEqual(False, params.e) def test_override_params_dict_using_json_string(self): params = params_dict.ParamsDict({ 'a': 1, 'b': { 'b1': 2, 'b2': [2, 3], }, 'd': { 'd1': { 'd2': 'hello' } }, 'e': False }) override_json_string = "{ b: { b2: [3, 4] }, d: { d1: { d2: 'hi' } } }" params = params_dict.override_params_dict( params, override_json_string, is_strict=True) self.assertEqual(1, params.a) self.assertEqual(2, params.b.b1) self.assertEqual([3, 4], params.b.b2) self.assertEqual('hi', params.d.d1.d2) self.assertEqual(False, params.e) def test_override_params_dict_using_csv_string(self): params = params_dict.ParamsDict({ 'a': 1, 'b': { 'b1': 2, 'b2': [2, 3], }, 'd': { 'd1': { 'd2': 'hello' } }, 'e': False }) override_csv_string = "b.b2=[3,4], d.d1.d2='hi, world', e=gs://test" params = params_dict.override_params_dict( params, override_csv_string, is_strict=True) self.assertEqual(1, params.a) self.assertEqual(2, params.b.b1) self.assertEqual([3, 4], params.b.b2) self.assertEqual('hi, world', params.d.d1.d2) self.assertEqual('gs://test', params.e) # Test different float formats override_csv_string = 'b.b2=-1.e-3, d.d1.d2=+0.001, e=1e+3, a=-1.5E-3' params = params_dict.override_params_dict( params, override_csv_string, is_strict=True) self.assertEqual(-1e-3, params.b.b2) self.assertEqual(0.001, params.d.d1.d2) self.assertEqual(1e3, params.e) self.assertEqual(-1.5e-3, params.a) def test_override_params_dict_using_yaml_file(self): params = params_dict.ParamsDict({ 'a': 1, 'b': 2.5, 'c': [3, 4], 'd': 'hello', 'e': False }) override_yaml_file = self.write_temp_file( 'params.yaml', r""" b: 5.2 c: [30, 40] """) params = params_dict.override_params_dict( params, override_yaml_file, is_strict=True) self.assertEqual(1, params.a) self.assertEqual(5.2, params.b) self.assertEqual([30, 40], params.c) self.assertEqual('hello', params.d) self.assertEqual(False, params.e) class IOTest(tf.test.TestCase): def test_basic_csv_str_to_json_str(self): csv_str = 'a=1,b=2,c=3' json_str = '{a : 1, b : 2, c : 3}' converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) self.assertEqual(converted_csv_str, json_str) def test_basic_csv_str_load(self): csv_str = 'a=1,b=2,c=3' expected_output = {'a': 1, 'b': 2, 'c': 3} converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) converted_dict = yaml.load(converted_csv_str) self.assertDictEqual(converted_dict, expected_output) def test_basic_nested_csv_str_to_json_str(self): csv_str = 'a=1,b.b1=2' json_str = '{a : 1, b : {b1 : 2}}' converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) self.assertEqual(converted_csv_str, json_str) def test_basic_nested_csv_str_load(self): csv_str = 'a=1,b.b1=2,c.c1=3' expected_output = {'a': 1, 'b': {'b1': 2}, 'c': {'c1': 3}} converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) converted_dict = yaml.load(converted_csv_str) self.assertDictEqual(converted_dict, expected_output) def test_complex_nested_csv_str_to_json_str(self): csv_str = 'a.aa.aaa.aaaaa.a=1' json_str = '{a : {aa : {aaa : {aaaaa : {a : 1}}}}}' converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) self.assertEqual(converted_csv_str, json_str) def test_complex_nested_csv_str_load(self): csv_str = 'a.aa.aaa.aaaaa.a=1,a.a=2' expected_output = {'a': {'aa': {'aaa': {'aaaaa': {'a': 1}}}, 'a': 2}} converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) converted_dict = yaml.load(converted_csv_str) self.assertDictEqual(converted_dict, expected_output) def test_int_array_param_nested_csv_str_to_json_str(self): csv_str = 'a.b[2]=3,a.b[0]=1,a.b[1]=2' json_str = '{a : {b : [1, 2, 3]}}' converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) self.assertEqual(converted_csv_str, json_str) def test_float_array_param_nested_csv_str_to_json_str(self): csv_str = 'a.b[1]=3.45,a.b[2]=1.32,a.b[0]=2.232' json_str = '{a : {b : [2.232, 3.45, 1.32]}}' converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) self.assertEqual(converted_csv_str, json_str) def test_incomplete_array_param_nested_csv_str_to_json_str(self): csv_str = 'a.b[0]=1,a.b[2]=2' self.assertRaises(ValueError, params_dict.nested_csv_str_to_json_str, csv_str) def test_csv_str_load_supported_datatypes(self): csv_str = 'a=1,b=2.,c=[1,2,3],d=\'hello, there\',e=\"Hi.\"' converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) converted_dict = yaml.load(converted_csv_str) self.assertEqual(converted_dict['a'], 1) self.assertEqual(converted_dict['b'], 2.) self.assertEqual(converted_dict['c'], [1, 2, 3]) self.assertEqual(converted_dict['d'], 'hello, there') self.assertEqual(converted_dict['e'], 'Hi.') def test_csv_str_load_unsupported_datatypes(self): csv_str = 'a=[[1,2,3],[4,5,6]]' self.assertRaises(ValueError, params_dict.nested_csv_str_to_json_str, csv_str) def test_csv_str_to_json_str_spacing(self): csv_str1 = 'a=1,b=2,c=3' csv_str2 = 'a = 1, b = 2, c = 3' json_str = '{a : 1, b : 2, c : 3}' converted_csv_str1 = params_dict.nested_csv_str_to_json_str(csv_str1) converted_csv_str2 = params_dict.nested_csv_str_to_json_str(csv_str2) self.assertEqual(converted_csv_str1, converted_csv_str2) self.assertEqual(converted_csv_str1, json_str) self.assertEqual(converted_csv_str2, json_str) def test_gcs_added_quotes(self): csv_str = 'a=gs://abc, b=gs://def' expected_output = '{a : \'gs://abc\', b : \'gs://def\'}' converted_csv_str = params_dict.nested_csv_str_to_json_str(csv_str) self.assertEqual(converted_csv_str, expected_output) if __name__ == '__main__': tf.test.main()
14,418
31.257271
77
py
models
models-master/official/modeling/optimization/optimizer_factory.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Optimizer factory class.""" from typing import Callable, List, Optional, Tuple, Union import gin import tensorflow as tf from official.modeling.optimization import slide_optimizer from official.modeling.optimization import adafactor_optimizer from official.modeling.optimization import ema_optimizer from official.modeling.optimization import lamb from official.modeling.optimization import lars from official.modeling.optimization import legacy_adamw from official.modeling.optimization import lr_schedule from official.modeling.optimization.configs import optimization_config as opt_cfg # Optimizer CLS to be used in both legacy and new path. SHARED_OPTIMIZERS = { 'sgd_experimental': tf.keras.optimizers.experimental.SGD, 'adam_experimental': tf.keras.optimizers.experimental.Adam, 'adamw': legacy_adamw.AdamWeightDecay, 'adamw_experimental': tf.keras.optimizers.experimental.AdamW, 'lamb': lamb.LAMB, 'lars': lars.LARS, 'slide': slide_optimizer.SLIDE, 'adafactor': adafactor_optimizer.Adafactor, } LEGACY_OPTIMIZERS_CLS = { 'sgd': tf.keras.optimizers.legacy.SGD, 'adam': tf.keras.optimizers.legacy.Adam, 'rmsprop': tf.keras.optimizers.legacy.RMSprop, 'adagrad': tf.keras.optimizers.legacy.Adagrad, } LEGACY_OPTIMIZERS_CLS.update(SHARED_OPTIMIZERS) NEW_OPTIMIZERS_CLS = { 'sgd': tf.keras.optimizers.experimental.SGD, 'adam': tf.keras.optimizers.experimental.Adam, 'rmsprop': tf.keras.optimizers.experimental.RMSprop, 'adagrad': tf.keras.optimizers.experimental.Adagrad, } NEW_OPTIMIZERS_CLS.update(SHARED_OPTIMIZERS) LR_CLS = { 'stepwise': lr_schedule.PiecewiseConstantDecayWithOffset, 'polynomial': lr_schedule.PolynomialDecayWithOffset, 'exponential': lr_schedule.ExponentialDecayWithOffset, 'cosine': lr_schedule.CosineDecayWithOffset, 'power': lr_schedule.DirectPowerDecay, 'power_linear': lr_schedule.PowerAndLinearDecay, 'power_with_offset': lr_schedule.PowerDecayWithOffset, 'step_cosine_with_offset': lr_schedule.StepCosineDecayWithOffset, } WARMUP_CLS = { 'linear': lr_schedule.LinearWarmup, 'polynomial': lr_schedule.PolynomialWarmUp } def register_optimizer_cls(key: str, optimizer_config_cls: Union[ tf.keras.optimizers.Optimizer, tf.keras.optimizers.legacy.Optimizer, tf.keras.optimizers.experimental.Optimizer ], use_legacy_optimizer: bool = True): """Register customize optimizer cls. The user will still need to subclass data classes in configs.optimization_config to be used with OptimizerFactory. Args: key: A string to that the optimizer_config_cls is registered with. optimizer_config_cls: A class which inherits tf.keras.optimizers.Optimizer. use_legacy_optimizer: A boolean that indicates if using legacy optimizers. """ if use_legacy_optimizer: if key in LEGACY_OPTIMIZERS_CLS: raise ValueError('%s already registered in LEGACY_OPTIMIZERS_CLS.' % key) LEGACY_OPTIMIZERS_CLS[key] = optimizer_config_cls else: if key in NEW_OPTIMIZERS_CLS: raise ValueError('%s already registered in NEW_OPTIMIZERS_CLS.' % key) NEW_OPTIMIZERS_CLS[key] = optimizer_config_cls class OptimizerFactory: """Optimizer factory class. This class builds learning rate and optimizer based on an optimization config. To use this class, you need to do the following: (1) Define optimization config, this includes optimizer, and learning rate schedule. (2) Initialize the class using the optimization config. (3) Build learning rate. (4) Build optimizer. This is a typical example for using this class: ``` params = { 'optimizer': { 'type': 'sgd', 'sgd': {'momentum': 0.9} }, 'learning_rate': { 'type': 'stepwise', 'stepwise': {'boundaries': [10000, 20000], 'values': [0.1, 0.01, 0.001]} }, 'warmup': { 'type': 'linear', 'linear': {'warmup_steps': 500, 'warmup_learning_rate': 0.01} } } opt_config = OptimizationConfig(params) opt_factory = OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() optimizer = opt_factory.build_optimizer(lr) ``` """ def __init__(self, config: opt_cfg.OptimizationConfig): """Initializing OptimizerFactory. Args: config: OptimizationConfig instance contain optimization config. """ self._config = config self._optimizer_config = config.optimizer.get() self._optimizer_type = config.optimizer.type self._use_ema = config.ema is not None self._ema_config = config.ema if self._optimizer_config is None: raise ValueError('Optimizer type must be specified') self._lr_config = config.learning_rate.get() self._lr_type = config.learning_rate.type if self._lr_type is None: raise ValueError('Learning rate type must be specified') self._warmup_config = config.warmup.get() self._warmup_type = config.warmup.type def build_learning_rate(self): """Build learning rate. Builds learning rate from config. Learning rate schedule is built according to the learning rate config. If learning rate type is consant, lr_config.learning_rate is returned. Returns: tf.keras.optimizers.schedules.LearningRateSchedule instance. If learning rate type is consant, lr_config.learning_rate is returned. """ if self._lr_type == 'constant': lr = self._lr_config.learning_rate else: lr = LR_CLS[self._lr_type](**self._lr_config.as_dict()) if self._warmup_config: lr = WARMUP_CLS[self._warmup_type](lr, **self._warmup_config.as_dict()) return lr @gin.configurable def build_optimizer( self, lr: Union[tf.keras.optimizers.schedules.LearningRateSchedule, float], gradient_aggregator: Optional[Callable[ [List[Tuple[tf.Tensor, tf.Tensor]]], List[Tuple[tf.Tensor, tf.Tensor]]]] = None, gradient_transformers: Optional[List[Callable[ [List[Tuple[tf.Tensor, tf.Tensor]]], List[Tuple[tf.Tensor, tf.Tensor]]]]] = None, postprocessor: Optional[Callable[[tf.keras.optimizers.Optimizer], tf.keras.optimizers.Optimizer]] = None, use_legacy_optimizer: bool = True): """Build optimizer. Builds optimizer from config. It takes learning rate as input, and builds the optimizer according to the optimizer config. Typically, the learning rate built using self.build_lr() is passed as an argument to this method. Args: lr: A floating point value, or a tf.keras.optimizers.schedules.LearningRateSchedule instance. gradient_aggregator: Optional function to overwrite gradient aggregation. gradient_transformers: Optional list of functions to use to transform gradients before applying updates to Variables. The functions are applied after gradient_aggregator. The functions should accept and return a list of (gradient, variable) tuples. clipvalue, clipnorm, global_clipnorm should not be set when gradient_transformers is passed. postprocessor: An optional function for postprocessing the optimizer. It takes an optimizer and returns an optimizer. use_legacy_optimizer: A boolean that indicates if using legacy optimizers. Returns: `tf.keras.optimizers.legacy.Optimizer` or `tf.keras.optimizers.experimental.Optimizer` instance. """ optimizer_dict = self._optimizer_config.as_dict() ## Delete clipnorm, clipvalue, global_clipnorm if None if optimizer_dict['clipnorm'] is None: del optimizer_dict['clipnorm'] if optimizer_dict['clipvalue'] is None: del optimizer_dict['clipvalue'] if optimizer_dict['global_clipnorm'] is None: del optimizer_dict['global_clipnorm'] optimizer_dict['learning_rate'] = lr if gradient_aggregator is not None: optimizer_dict['gradient_aggregator'] = gradient_aggregator if gradient_transformers is not None: optimizer_dict['gradient_transformers'] = gradient_transformers if use_legacy_optimizer: optimizer = LEGACY_OPTIMIZERS_CLS[self._optimizer_type](**optimizer_dict) else: if 'decay' in optimizer_dict: raise ValueError( '`decay` is deprecated in new Keras optimizer, please reflect the ' 'decay logic in `lr` or set `use_legacy_optimizer=True` to use the ' 'legacy optimizer.') optimizer = NEW_OPTIMIZERS_CLS[self._optimizer_type](**optimizer_dict) if self._use_ema: if not use_legacy_optimizer: raise ValueError( 'EMA can only work with the legacy optimizer, please set ' '`use_legacy_optimizer=True`.') optimizer = ema_optimizer.ExponentialMovingAverage( optimizer, **self._ema_config.as_dict()) if postprocessor: optimizer = postprocessor(optimizer) if isinstance(optimizer, tf.keras.optimizers.Optimizer): return optimizer # The following check makes sure the function won't break in older TF # version because of missing the experimental/legacy package. if hasattr(tf.keras.optimizers, 'experimental'): if isinstance(optimizer, tf.keras.optimizers.experimental.Optimizer): return optimizer if hasattr(tf.keras.optimizers, 'legacy'): if isinstance(optimizer, tf.keras.optimizers.legacy.Optimizer): return optimizer raise TypeError('OptimizerFactory.build_optimizer returning a ' 'non-optimizer object: {}'.format(optimizer))
10,475
38.235955
81
py
models
models-master/official/modeling/optimization/lamb_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for LAMB Optimizer.""" import numpy as np from numpy import linalg import tensorflow as tf from official.modeling.optimization import lamb def lamb_update_numpy(param, g_t, t, m, v, lr=0.001, lamb_wd=0.0, beta1=0.9, beta2=0.999, epsilon=1e-6): m_t = beta1 * m + (1 - beta1) * g_t v_t = beta2 * v + (1 - beta2) * g_t * g_t m_t_hat = m_t / (1 - beta1**(t + 1)) v_t_hat = v_t / (1 - beta2**(t + 1)) update = m_t_hat / (np.sqrt(v_t_hat) + epsilon) update += lamb_wd * param w_norm = linalg.norm(param, ord=2) g_norm = linalg.norm(update, ord=2) ratio = np.where(w_norm > 0, np.where(g_norm > 0, (w_norm / g_norm), 1.0), 1.0) param_t = param - ratio * lr * update return param_t, m_t, v_t def get_beta_accumulators(opt, dtype): local_step = tf.cast(opt.iterations + 1, dtype) beta_1_t = tf.cast(opt._get_hyper("beta_1"), dtype) beta_1_power = tf.math.pow(beta_1_t, local_step) beta_2_t = tf.cast(opt._get_hyper("beta_2"), dtype) beta_2_power = tf.math.pow(beta_2_t, local_step) return (beta_1_power, beta_2_power) class LAMBTest(tf.test.TestCase): def test_sparse(self): dtype = tf.float32 # Initialize tf for numpy implementation. m0, v0, m1, v1 = 0.0, 0.0, 0.0, 0.0 var0_np = np.array([1.0, 1.0, 2.0], dtype=dtype.as_numpy_dtype) grads0_np = np.array([0.1, 0.0, 0.1], dtype=dtype.as_numpy_dtype) var1_np = np.array([3.0, 3.0, 4.0], dtype=dtype.as_numpy_dtype) grads1_np = np.array([0.01, 0.0, 0.01], dtype=dtype.as_numpy_dtype) var0 = tf.Variable(var0_np) var1 = tf.Variable(var1_np) grads0_np_indices = np.array([0, 2], dtype=np.int32) grads0 = tf.IndexedSlices( tf.constant(grads0_np[grads0_np_indices]), tf.constant(grads0_np_indices), tf.constant([3]), ) grads1_np_indices = np.array([0, 2], dtype=np.int32) grads1 = tf.IndexedSlices( tf.constant(grads1_np[grads1_np_indices]), tf.constant(grads1_np_indices), tf.constant([3]), ) opt = lamb.LAMB() # Fetch params to validate initial values np.testing.assert_allclose(np.asanyarray([1.0, 1.0, 2.0]), var0.numpy()) np.testing.assert_allclose(np.asanyarray([3.0, 3.0, 4.0]), var1.numpy()) # Run 3 steps of LAMB for t in range(3): beta_1_power, beta_2_power = get_beta_accumulators(opt, dtype) self.assertAllClose(0.9 ** (t + 1), beta_1_power) self.assertAllClose(0.999 ** (t + 1), beta_2_power) opt.apply_gradients(zip([grads0, grads1], [var0, var1])) var0_np, m0, v0 = lamb_update_numpy(var0_np, grads0_np, t, m0, v0) var1_np, m1, v1 = lamb_update_numpy(var1_np, grads1_np, t, m1, v1) # Validate updated params self.assertAllClose(var0_np, var0.numpy()) self.assertAllClose(var1_np, var1.numpy()) def test_basic_with_learning_rate_decay(self): dtype = tf.float32 # Initialize variables for numpy implementation. m0, v0, m1, v1 = 0.0, 0.0, 0.0, 0.0 var0_np = np.array([1.0, 2.0], dtype=dtype.as_numpy_dtype) grads0_np = np.array([0.1, 0.1], dtype=dtype.as_numpy_dtype) var1_np = np.array([3.0, 4.0], dtype=dtype.as_numpy_dtype) grads1_np = np.array([0.01, 0.01], dtype=dtype.as_numpy_dtype) var0 = tf.Variable(var0_np, name="var0") var1 = tf.Variable(var1_np, name="var1") grads0 = tf.constant(grads0_np) grads1 = tf.constant(grads1_np) learning_rate = 0.001 beta_1 = 0.9 beta_2 = 0.999 epsilon = 1e-7 decay = 0.5 lamb_wd = 0.01 opt = lamb.LAMB( learning_rate=learning_rate, beta_1=beta_1, beta_2=beta_2, epsilon=epsilon, weight_decay_rate=lamb_wd, decay=decay, ) # Run 3 steps of LAMB for t in range(3): opt.apply_gradients(zip([grads0, grads1], [var0, var1])) lr_np = learning_rate / (1 + decay * t) var0_np, m0, v0 = lamb_update_numpy( var0_np, grads0_np, t, m0, v0, lr=lr_np, lamb_wd=lamb_wd) var1_np, m1, v1 = lamb_update_numpy( var1_np, grads1_np, t, m1, v1, lr=lr_np, lamb_wd=lamb_wd) # Validate updated params self.assertAllClose(var0_np, var0.numpy()) self.assertAllClose(var1_np, var1.numpy()) def test_exclude_weight_decay(self): opt = lamb.LAMB( 0.01, weight_decay_rate=0.01, exclude_from_weight_decay=["var1"] ) assert opt._do_use_weight_decay("var0") assert not opt._do_use_weight_decay("var1") assert not opt._do_use_weight_decay("var1_weight") def test_exclude_layer_adaptation(self): opt = lamb.LAMB(0.01, exclude_from_layer_adaptation=["var1"]) assert opt._do_layer_adaptation("var0") assert not opt._do_layer_adaptation("var1") assert not opt._do_layer_adaptation("var1_weight") def test_serialization(self): optimizer = lamb.LAMB(1e-4) config = tf.keras.optimizers.serialize(optimizer, use_legacy_format=True) new_optimizer = tf.keras.optimizers.deserialize( config, use_legacy_format=True ) assert new_optimizer.get_config() == optimizer.get_config() if __name__ == "__main__": tf.test.main()
5,928
32.308989
77
py
models
models-master/official/modeling/optimization/optimizer_factory_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for optimizer_factory.py.""" from absl.testing import parameterized import numpy as np import tensorflow as tf from official.modeling.optimization import optimizer_factory from official.modeling.optimization.configs import optimization_config class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase): @parameterized.parameters(('sgd'), ('rmsprop'), ('adam'), ('adamw'), ('lamb'), ('lars'), ('adagrad')) def test_optimizers(self, optimizer_type): params = { 'optimizer': { 'type': optimizer_type }, 'learning_rate': { 'type': 'constant', 'constant': { 'learning_rate': 0.1 } } } optimizer_cls = optimizer_factory.LEGACY_OPTIMIZERS_CLS[optimizer_type] expected_optimizer_config = optimizer_cls().get_config() expected_optimizer_config['learning_rate'] = 0.1 opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() optimizer = opt_factory.build_optimizer(lr, postprocessor=lambda x: x) self.assertIsInstance(optimizer, optimizer_cls) self.assertEqual(expected_optimizer_config, optimizer.get_config()) @parameterized.parameters(('sgd'), ('rmsprop'), ('adam'), ('adamw'), ('lamb'), ('lars'), ('adagrad')) def test_new_optimizers(self, optimizer_type): params = { 'optimizer': { 'type': optimizer_type }, 'learning_rate': { 'type': 'constant', 'constant': { 'learning_rate': 0.1 } } } optimizer_cls = optimizer_factory.NEW_OPTIMIZERS_CLS[optimizer_type] expected_optimizer_config = optimizer_cls().get_config() expected_optimizer_config['learning_rate'] = 0.1 opt_config = optimization_config.OptimizationConfig(params) if optimizer_type == 'sgd': # Delete unsupported arg `decay` from SGDConfig. delattr(opt_config.optimizer.sgd, 'decay') opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() optimizer = opt_factory.build_optimizer( lr, postprocessor=lambda x: x, use_legacy_optimizer=False) self.assertIsInstance(optimizer, optimizer_cls) self.assertEqual(expected_optimizer_config, optimizer.get_config()) def test_gradient_aggregator(self): params = { 'optimizer': { 'type': 'adam', }, 'learning_rate': { 'type': 'constant', 'constant': { 'learning_rate': 1.0 } } } opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() # Dummy function to zero out gradients. zero_grads = lambda gv: [(tf.zeros_like(g), v) for g, v in gv] optimizer = opt_factory.build_optimizer(lr, gradient_aggregator=zero_grads) if isinstance(optimizer, tf.keras.optimizers.experimental.Optimizer): self.skipTest('New Keras optimizer does not support ' '`gradient_aggregator` arg.') var0 = tf.Variable([1.0, 2.0]) var1 = tf.Variable([3.0, 4.0]) grads0 = tf.constant([1.0, 1.0]) grads1 = tf.constant([1.0, 1.0]) grads_and_vars = list(zip([grads0, grads1], [var0, var1])) optimizer.apply_gradients(grads_and_vars) self.assertAllClose(np.array([1.0, 2.0]), var0.numpy()) self.assertAllClose(np.array([3.0, 4.0]), var1.numpy()) @parameterized.parameters((None, None), (1.0, None), (None, 1.0)) def test_gradient_clipping(self, clipnorm, clipvalue): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'clipnorm': clipnorm, 'clipvalue': clipvalue } }, 'learning_rate': { 'type': 'constant', 'constant': { 'learning_rate': 1.0 } } } opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() optimizer = opt_factory.build_optimizer(lr) var0 = tf.Variable([1.0, 2.0]) var1 = tf.Variable([3.0, 4.0]) grads0 = tf.constant([0.1, 0.1]) grads1 = tf.constant([2.0, 3.0]) grads_and_vars = list(zip([grads0, grads1], [var0, var1])) optimizer.apply_gradients(grads_and_vars) self.assertAllClose(np.array([0.9, 1.9]), var0.numpy()) if clipvalue is not None: self.assertAllClose(np.array([2.0, 3.0]), var1.numpy()) elif clipnorm is not None: self.assertAllClose(np.array([2.4452999, 3.1679497]), var1.numpy()) else: self.assertAllClose(np.array([1.0, 1.0]), var1.numpy()) def test_missing_types(self): params = {'optimizer': {'type': 'sgd', 'sgd': {'momentum': 0.9}}} with self.assertRaises(ValueError): optimizer_factory.OptimizerFactory( optimization_config.OptimizationConfig(params)) params = { 'learning_rate': { 'type': 'stepwise', 'stepwise': { 'boundaries': [10000, 20000], 'values': [0.1, 0.01, 0.001] } } } with self.assertRaises(ValueError): optimizer_factory.OptimizerFactory( optimization_config.OptimizationConfig(params)) def test_wrong_return_type(self): optimizer_type = 'sgd' params = { 'optimizer': { 'type': optimizer_type }, 'learning_rate': { 'type': 'constant', 'constant': { 'learning_rate': 0.1 } } } opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) with self.assertRaises(TypeError): _ = opt_factory.build_optimizer(0.1, postprocessor=lambda x: None) # TODO(b/187559334) refactor lr_schedule tests into `lr_schedule_test.py`. def test_stepwise_lr_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'stepwise', 'stepwise': { 'boundaries': [10000, 20000], 'values': [0.1, 0.01, 0.001] } } } expected_lr_step_values = [[0, 0.1], [5000, 0.1], [10000, 0.1], [10001, 0.01], [20000, 0.01], [20001, 0.001]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) def test_stepwise_lr_with_warmup_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'stepwise', 'stepwise': { 'boundaries': [10000, 20000], 'values': [0.1, 0.01, 0.001] } }, 'warmup': { 'type': 'linear', 'linear': { 'warmup_steps': 500, 'warmup_learning_rate': 0.01 } } } expected_lr_step_values = [[0, 0.01], [250, 0.055], [500, 0.1], [5500, 0.1], [10000, 0.1], [10001, 0.01], [20000, 0.01], [20001, 0.001]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) def test_exponential_lr_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'exponential', 'exponential': { 'initial_learning_rate': 0.1, 'decay_steps': 1000, 'decay_rate': 0.96, 'staircase': True } } } expected_lr_step_values = [ [0, 0.1], [999, 0.1], [1000, 0.096], [1999, 0.096], [2000, 0.09216], ] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) def test_polynomial_lr_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'polynomial', 'polynomial': { 'initial_learning_rate': 0.1, 'decay_steps': 1000, 'end_learning_rate': 0.001 } } } expected_lr_step_values = [[0, 0.1], [500, 0.0505], [1000, 0.001]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) def test_cosine_lr_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'cosine', 'cosine': { 'initial_learning_rate': 0.1, 'decay_steps': 1000 } } } expected_lr_step_values = [[0, 0.1], [250, 0.08535534], [500, 0.04999999], [750, 0.01464466], [1000, 0]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) def test_constant_lr_with_warmup_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'constant', 'constant': { 'learning_rate': 0.1 } }, 'warmup': { 'type': 'linear', 'linear': { 'warmup_steps': 500, 'warmup_learning_rate': 0.01 } } } expected_lr_step_values = [[0, 0.01], [250, 0.055], [500, 0.1], [5000, 0.1], [10000, 0.1], [20000, 0.1]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) def test_stepwise_lr_with_polynomial_warmup_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'stepwise', 'stepwise': { 'boundaries': [10000, 20000], 'values': [0.1, 0.01, 0.001] } }, 'warmup': { 'type': 'polynomial', 'polynomial': { 'warmup_steps': 500, 'power': 2. } } } expected_lr_step_values = [[0, 0.0], [250, 0.025], [500, 0.1], [5500, 0.1], [10000, 0.1], [10001, 0.01], [20000, 0.01], [20001, 0.001]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value, places=6) def test_power_lr_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'power', 'power': { 'initial_learning_rate': 1.0, 'power': -1.0 } } } expected_lr_step_values = [[0, 1.0], [1, 1.0], [250, 1. / 250.]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) def test_power_linear_lr_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'power_linear', 'power_linear': { 'initial_learning_rate': 1.0, 'power': -1.0, 'linear_decay_fraction': 0.5, 'total_decay_steps': 100, 'offset': 0, } } } expected_lr_step_values = [[0, 1.0], [1, 1.0], [40, 1. / 40.], [60, 1. / 60. * 0.8]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) def test_power_with_offset_lr_schedule(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'power_with_offset', 'power_with_offset': { 'initial_learning_rate': 1.0, 'power': -1.0, 'offset': 10, 'pre_offset_learning_rate': 3.0, } } } expected_lr_step_values = [[1, 3.0], [10, 3.0], [20, 1. / 10.]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) def test_step_cosine_lr_schedule_with_warmup(self): params = { 'optimizer': { 'type': 'sgd', 'sgd': { 'momentum': 0.9 } }, 'learning_rate': { 'type': 'step_cosine_with_offset', 'step_cosine_with_offset': { 'values': (0.0001, 0.00005), 'boundaries': (0, 500000), 'offset': 10000, } }, 'warmup': { 'type': 'linear', 'linear': { 'warmup_steps': 10000, 'warmup_learning_rate': 0.0 } } } expected_lr_step_values = [[0, 0.0], [5000, 1e-4 / 2.0], [10000, 1e-4], [20000, 9.994863e-05], [499999, 5e-05]] opt_config = optimization_config.OptimizationConfig(params) opt_factory = optimizer_factory.OptimizerFactory(opt_config) lr = opt_factory.build_learning_rate() for step, value in expected_lr_step_values: self.assertAlmostEqual(lr(step).numpy(), value) class OptimizerFactoryRegistryTest(tf.test.TestCase): def test_registry(self): class MyClass(): pass optimizer_factory.register_optimizer_cls('test', MyClass) self.assertIn('test', optimizer_factory.LEGACY_OPTIMIZERS_CLS) with self.assertRaisesRegex(ValueError, 'test already registered.*'): optimizer_factory.register_optimizer_cls('test', MyClass) if __name__ == '__main__': tf.test.main()
17,015
31.045198
80
py
models
models-master/official/modeling/optimization/legacy_adamw.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Adam optimizer with weight decay that exactly matches the original BERT.""" import re from absl import logging import tensorflow as tf class AdamWeightDecay(tf.keras.optimizers.legacy.Adam): """Adam enables L2 weight decay and clip_by_global_norm on gradients. [Warning!]: Keras optimizer supports gradient clipping and has an AdamW implementation. Please consider evaluating the choice in Keras package. Just adding the square of the weights to the loss function is *not* the correct way of using L2 regularization/weight decay with Adam, since that will interact with the m and v parameters in strange ways. Instead we want to decay the weights in a manner that doesn't interact with the m/v parameters. This is equivalent to adding the square of the weights to the loss with plain (non-momentum) SGD. """ def __init__(self, learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-7, amsgrad=False, weight_decay_rate=0.0, include_in_weight_decay=None, exclude_from_weight_decay=None, gradient_clip_norm=1.0, name='AdamWeightDecay', **kwargs): super(AdamWeightDecay, self).__init__(learning_rate, beta_1, beta_2, epsilon, amsgrad, name, **kwargs) self.weight_decay_rate = weight_decay_rate self.gradient_clip_norm = gradient_clip_norm self._include_in_weight_decay = include_in_weight_decay self._exclude_from_weight_decay = exclude_from_weight_decay logging.info('AdamWeightDecay gradient_clip_norm=%f', gradient_clip_norm) def _prepare_local(self, var_device, var_dtype, apply_state): super(AdamWeightDecay, self)._prepare_local(var_device, var_dtype, # pytype: disable=attribute-error # typed-keras apply_state) apply_state[(var_device, var_dtype)]['weight_decay_rate'] = tf.constant( self.weight_decay_rate, name='adam_weight_decay_rate') def _decay_weights_op(self, var, learning_rate, apply_state): do_decay = self._do_use_weight_decay(var.name) if do_decay: return var.assign_sub( learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]['weight_decay_rate'], use_locking=self._use_locking) return tf.no_op() def apply_gradients(self, grads_and_vars, name=None, experimental_aggregate_gradients=True): grads, tvars = list(zip(*grads_and_vars)) if experimental_aggregate_gradients and self.gradient_clip_norm > 0.0: # when experimental_aggregate_gradients = False, apply_gradients() no # longer implicitly allreduce gradients, users manually allreduce gradient # and passed the allreduced grads_and_vars. For now, the # clip_by_global_norm will be moved to before the explicit allreduce to # keep the math the same as TF 1 and pre TF 2.2 implementation. (grads, _) = tf.clip_by_global_norm( grads, clip_norm=self.gradient_clip_norm) return super(AdamWeightDecay, self).apply_gradients( zip(grads, tvars), name=name, experimental_aggregate_gradients=experimental_aggregate_gradients) def _get_lr(self, var_device, var_dtype, apply_state): """Retrieves the learning rate with the given state.""" if apply_state is None: return self._decayed_lr_t[var_dtype], {} apply_state = apply_state or {} coefficients = apply_state.get((var_device, var_dtype)) if coefficients is None: coefficients = self._fallback_apply_state(var_device, var_dtype) apply_state[(var_device, var_dtype)] = coefficients return coefficients['lr_t'], dict(apply_state=apply_state) def _resource_apply_dense(self, grad, var, apply_state=None): lr_t, kwargs = self._get_lr(var.device, var.dtype.base_dtype, apply_state) decay = self._decay_weights_op(var, lr_t, apply_state) with tf.control_dependencies([decay]): return super(AdamWeightDecay, self)._resource_apply_dense(grad, var, **kwargs) # pytype: disable=attribute-error # typed-keras def _resource_apply_sparse(self, grad, var, indices, apply_state=None): lr_t, kwargs = self._get_lr(var.device, var.dtype.base_dtype, apply_state) decay = self._decay_weights_op(var, lr_t, apply_state) with tf.control_dependencies([decay]): return super(AdamWeightDecay, self)._resource_apply_sparse(grad, var, indices, **kwargs) # pytype: disable=attribute-error # typed-keras def get_config(self): config = super(AdamWeightDecay, self).get_config() config.update({ 'weight_decay_rate': self.weight_decay_rate, }) return config def _do_use_weight_decay(self, param_name): """Whether to use L2 weight decay for `param_name`.""" if self.weight_decay_rate == 0: return False if self._include_in_weight_decay: for r in self._include_in_weight_decay: if re.search(r, param_name) is not None: return True if self._exclude_from_weight_decay: for r in self._exclude_from_weight_decay: if re.search(r, param_name) is not None: return False return True
5,953
41.528571
127
py
models
models-master/official/modeling/optimization/lr_schedule_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for lr_schedule.""" from absl.testing import parameterized import tensorflow as tf from official.modeling.optimization import lr_schedule class PowerAndLinearDecayTest(tf.test.TestCase, parameterized.TestCase): @parameterized.named_parameters( dict( testcase_name='power_only', init_lr=1.0, power=-1.0, linear_decay_fraction=0.0, total_decay_steps=100, offset=0, expected=[[0, 1.0], [1, 1.0], [40, 1. / 40.], [60, 1. / 60], [100, 1. / 100]]), dict( testcase_name='linear_only', init_lr=1.0, power=0.0, linear_decay_fraction=1.0, total_decay_steps=100, offset=0, expected=[[0, 1.0], [1, 0.99], [40, 0.6], [60, 0.4], [100, 0.0]]), dict( testcase_name='general', init_lr=1.0, power=-1.0, linear_decay_fraction=0.5, total_decay_steps=100, offset=0, expected=[[0, 1.0], [1, 1.0], [40, 1. / 40.], [60, 1. / 60. * 0.8], [100, 0.0]]), dict( testcase_name='offset', init_lr=1.0, power=-1.0, linear_decay_fraction=0.5, total_decay_steps=100, offset=90, expected=[[0, 1.0], [90, 1.0], [91, 1.0], [130, 1. / 40.], [150, 1. / 60. * 0.8], [190, 0.0], [200, 0.0]]), ) def test_power_linear_lr_schedule(self, init_lr, power, linear_decay_fraction, total_decay_steps, offset, expected): lr = lr_schedule.PowerAndLinearDecay( initial_learning_rate=init_lr, power=power, linear_decay_fraction=linear_decay_fraction, total_decay_steps=total_decay_steps, offset=offset) for step, value in expected: self.assertAlmostEqual(lr(step).numpy(), value) class OffsetLearningRateTest(tf.test.TestCase, parameterized.TestCase): @parameterized.parameters( dict(class_name=lr_schedule.PiecewiseConstantDecayWithOffset), dict(class_name=lr_schedule.PolynomialDecayWithOffset), dict(class_name=lr_schedule.ExponentialDecayWithOffset), dict(class_name=lr_schedule.CosineDecayWithOffset), ) def test_generated_docstring(self, class_name): self.assertNotEmpty(class_name.__init__.__doc__) @parameterized.parameters( dict( class_name=lr_schedule.PiecewiseConstantDecayWithOffset, kwarg=dict(boundaries=[50, 80], values=[1.0, 0.5, 0.1])), dict( class_name=lr_schedule.PolynomialDecayWithOffset, kwarg=dict(initial_learning_rate=1.0, decay_steps=100)), dict( class_name=lr_schedule.ExponentialDecayWithOffset, kwarg=dict( initial_learning_rate=1.0, decay_steps=100, decay_rate=0.5)), dict( class_name=lr_schedule.CosineDecayWithOffset, kwarg=dict(initial_learning_rate=1.0, decay_steps=100)), ) def test_offset(self, class_name, kwarg): offset = 10 offset_lr = class_name(offset=offset, **kwarg) base_lr = class_name.base_lr_class(**kwarg) self.assertIsInstance(offset_lr, class_name) for step in range(10, 101, 10): self.assertEqual(offset_lr(step), base_lr(step - offset)) if __name__ == '__main__': tf.test.main()
3,951
34.927273
80
py
models
models-master/official/modeling/optimization/lr_schedule.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Learning rate schedule classes.""" import math from typing import Mapping, Any, Union, Optional import tensorflow as tf def _make_offset_wrapper(new_class_name: str, base_lr_class): """Generates a offset wrapper of learning rate schedule. It will returns a subclass of the `base_lr_class`, the subclass takes an `offset` argument in the constructor. When the new class instance is called, the behavior is: new_class_object(step) = base_lr_class_object(step - offset) Example: CosineDecayWithOffset = _make_offset_wrapper( 'CosineDecayWithOffset', tf.keras.experimental.CosineDecay) # Use the lr: lr = CosineDecayWithOffset(offset=100, initial_learning_rate=0.1, decay_steps=1000) lr(101) # equals to tf.keras.experimental.CosineDecay(...)(101-100) Args: new_class_name: the name of the new class. base_lr_class: the base learning rate schedule class. Should be subclass of tf.keras.optimizers.schedules.LearningRateSchedule Returns: A new class (subclass of the base_lr_class) that can take an offset. """ assert issubclass(base_lr_class, tf.keras.optimizers.schedules.LearningRateSchedule), ( "base_lr_class should be subclass of keras " f"LearningRateSchedule, got {base_lr_class}") # pylint: disable=protected-access,pointless-statement def offset_learning_rate_init(self, offset=0, **kwargs): """Construct learning rate schedule object. When this object is called, its behavior is self.__call__(step) == base_lr_class.__call__(step - offset) Args: self: this object. offset: The offset when computing the learning rate schedule. **kwargs: Pass through to base learning rate class constructor. """ base_lr_class.__init__(self, **kwargs) self._offset = offset def offset_learning_rate_call(self, step): step = tf.cast(step - self._offset, tf.float32) return base_lr_class.__call__(self, step) # pylint: enable=protected-access,pointless-statement return type( new_class_name, (base_lr_class,), { "base_lr_class": base_lr_class, "__init__": offset_learning_rate_init, "__call__": offset_learning_rate_call }) PiecewiseConstantDecayWithOffset = _make_offset_wrapper( "PiecewiseConstantDecayWithOffset", tf.keras.optimizers.schedules.PiecewiseConstantDecay) PolynomialDecayWithOffset = _make_offset_wrapper( "PolynomialDecayWithOffset", tf.keras.optimizers.schedules.PolynomialDecay) ExponentialDecayWithOffset = _make_offset_wrapper( "ExponentialDecayWithOffset", tf.keras.optimizers.schedules.ExponentialDecay) CosineDecayWithOffset = _make_offset_wrapper("CosineDecayWithOffset", tf.keras.experimental.CosineDecay) class LinearWarmup(tf.keras.optimizers.schedules.LearningRateSchedule): """Linear warmup schedule.""" def __init__(self, after_warmup_lr_sched: Union[ tf.keras.optimizers.schedules.LearningRateSchedule, float], warmup_steps: int, warmup_learning_rate: float, name: Optional[str] = None): """Add linear warmup schedule to a learning rate schedule. warmup_lr is the initial learning rate, the final learning rate of the init_warmup period is the initial learning rate of lr_schedule in use. The learning rate at each step linearly increased according to the following formula: learning_rate = warmup_lr + step / warmup_steps * (final_warmup_lr - warmup_lr). Using warmup overrides the learning rate schedule by the number of warmup steps. Args: after_warmup_lr_sched: tf.keras.optimizers.schedules .LearningRateSchedule or a constant. warmup_steps: Number of the warmup steps. warmup_learning_rate: Initial learning rate for the warmup. name: Optional, name of warmup schedule. """ super().__init__() self._name = name self._after_warmup_lr_sched = after_warmup_lr_sched self._warmup_steps = warmup_steps self._init_warmup_lr = warmup_learning_rate if isinstance(after_warmup_lr_sched, tf.keras.optimizers.schedules.LearningRateSchedule): self._final_warmup_lr = after_warmup_lr_sched(warmup_steps) else: self._final_warmup_lr = tf.cast(after_warmup_lr_sched, dtype=tf.float32) def __call__(self, step: int): global_step = tf.cast(step, dtype=tf.float32) linear_warmup_lr = ( self._init_warmup_lr + global_step / self._warmup_steps * (self._final_warmup_lr - self._init_warmup_lr)) if isinstance(self._after_warmup_lr_sched, tf.keras.optimizers.schedules.LearningRateSchedule): after_warmup_lr = self._after_warmup_lr_sched(step) else: after_warmup_lr = tf.cast(self._after_warmup_lr_sched, dtype=tf.float32) lr = tf.cond(global_step < self._warmup_steps, lambda: linear_warmup_lr, lambda: after_warmup_lr) return lr def get_config(self) -> Mapping[str, Any]: if isinstance(self._after_warmup_lr_sched, tf.keras.optimizers.schedules.LearningRateSchedule): config = { "after_warmup_lr_sched": self._after_warmup_lr_sched.get_config()} # pytype: disable=attribute-error else: config = {"after_warmup_lr_sched": self._after_warmup_lr_sched} # pytype: disable=attribute-error config.update({ "warmup_steps": self._warmup_steps, "warmup_learning_rate": self._init_warmup_lr, "name": self._name }) return config class PolynomialWarmUp(tf.keras.optimizers.schedules.LearningRateSchedule): """Applies polynomial warmup schedule on a given learning rate decay schedule.""" def __init__(self, after_warmup_lr_sched: Union[ tf.keras.optimizers.schedules.LearningRateSchedule, float], warmup_steps: int, power: float = 1.0, name: str = "PolynomialWarmup"): super().__init__() if isinstance(after_warmup_lr_sched, tf.keras.optimizers.schedules.LearningRateSchedule): self._initial_learning_rate = after_warmup_lr_sched(warmup_steps) else: self._initial_learning_rate = tf.cast( after_warmup_lr_sched, dtype=tf.float32) self._warmup_steps = warmup_steps self._power = power self._after_warmup_lr_sched = after_warmup_lr_sched self._name = name def __call__(self, step): with tf.name_scope(self._name or "PolynomialWarmUp") as name: # Implements polynomial warmup. i.e., if global_step < warmup_steps, the # learning rate will be `global_step/num_warmup_steps * init_lr`. global_step_float = tf.cast(step, tf.float32) warmup_steps_float = tf.cast(self._warmup_steps, tf.float32) if self._warmup_steps <= 0: warmup_percent_done = 1.0 else: # A zero `step` may cause Inf. So make `step` positive. step_non_zero = tf.math.maximum(global_step_float, 1.0) warmup_percent_done = step_non_zero / warmup_steps_float warmup_learning_rate = ( self._initial_learning_rate * tf.math.pow(warmup_percent_done, self._power)) if isinstance(self._after_warmup_lr_sched, tf.keras.optimizers.schedules.LearningRateSchedule): after_warmup_lr = self._after_warmup_lr_sched(step) else: after_warmup_lr = tf.cast(self._after_warmup_lr_sched, dtype=tf.float32) return tf.cond( global_step_float < warmup_steps_float, lambda: warmup_learning_rate, lambda: after_warmup_lr, name=name) def get_config(self) -> Mapping[str, Any]: if isinstance(self._after_warmup_lr_sched, tf.keras.optimizers.schedules.LearningRateSchedule): config = { "after_warmup_lr_sched": self._after_warmup_lr_sched.get_config()} # pytype: disable=attribute-error else: config = {"after_warmup_lr_sched": self._after_warmup_lr_sched} # pytype: disable=attribute-error config.update({ "warmup_steps": self._warmup_steps, "power": self._power, "name": self._name }) return config class DirectPowerDecay(tf.keras.optimizers.schedules.LearningRateSchedule): """Learning rate schedule follows lr * (step)^power.""" def __init__(self, initial_learning_rate: float, power: float = 1.0, name: str = "DirectPowerDecay"): """Initialize configuration of the learning rate schedule. Args: initial_learning_rate: The initial learning rate. power: The order of the polynomial. name: Optional, name of learning rate schedule. """ super().__init__() self._initial_learning_rate = initial_learning_rate self._power = power self._name = name def __call__(self, step): with tf.name_scope(self._name or "DirectPowerDecay"): step = tf.cast(step, tf.float32) learning_rate = self._initial_learning_rate # A zero `step` may cause Inf. So make `step` positive. step_non_zero = tf.math.maximum(step, 1.0) learning_rate *= tf.math.pow(step_non_zero, self._power) return learning_rate def get_config(self): """Get the configuration of the learning rate schedule.""" return { "initial_learning_rate": self._initial_learning_rate, "power": self._power, "name": self._name, } class PowerAndLinearDecay(tf.keras.optimizers.schedules.LearningRateSchedule): """Learning rate schedule with multiplied by linear decay at the end. The schedule has the following behavoir. Let offset_step = step - offset. 1) offset_step < 0, the actual learning rate equals initial_learning_rate. 2) offset_step <= total_decay_steps * (1 - linear_decay_fraction), the actual learning rate equals lr * offset_step^power. 3) total_decay_steps * (1 - linear_decay_fraction) <= offset_step < total_decay_steps, the actual learning rate equals lr * offset_step^power * (total_decay_steps - offset_step) / (total_decay_steps * linear_decay_fraction). 4) offset_step >= total_decay_steps, the actual learning rate equals zero. """ def __init__(self, initial_learning_rate: float, total_decay_steps: int, power: float = 1.0, linear_decay_fraction: float = 0.1, offset: int = 0, name: str = "PowerAndLinearDecay"): """Initialize configuration of the learning rate schedule. Args: initial_learning_rate: The initial learning rate. total_decay_steps: The total number of steps for power + linear decay. power: The order of the polynomial. linear_decay_fraction: In the last `linear_decay_fraction` steps, the learning rate will be multiplied by a linear decay. offset: The offset applied to steps. name: Optional, name of learning rate schedule. """ super().__init__() self._initial_learning_rate = initial_learning_rate self._total_decay_steps = total_decay_steps self._power = power self._linear_decay_fraction = linear_decay_fraction self._offset = offset self._name = name def __call__(self, step): with tf.name_scope(self._name or "PowerAndLinearDecay"): step = tf.cast(step - self._offset, tf.float32) learning_rate = self._initial_learning_rate # A zero `step` may cause Inf. So make `step` positive. step_non_zero = tf.math.maximum(step, 1.0) learning_rate *= tf.math.pow(step_non_zero, self._power) if self._total_decay_steps * self._linear_decay_fraction > 0: learning_rate *= tf.minimum( 1.0, (self._total_decay_steps - step) / (self._total_decay_steps * self._linear_decay_fraction)) learning_rate = tf.maximum(0.0, learning_rate) return learning_rate def get_config(self): """Get the configuration of the learning rate schedule.""" return { "initial_learning_rate": self._initial_learning_rate, "total_decay_steps": self._total_decay_steps, "power": self._power, "linear_decay_fraction": self._linear_decay_fraction, "offset": self._offset, "name": self._name, } class PowerDecayWithOffset(tf.keras.optimizers.schedules.LearningRateSchedule): """Power learning rate decay with offset. Learning rate equals to `pre_offset_learning_rate` if `step` < `offset`. Otherwise, learning rate equals to lr * (step - offset)^power. """ def __init__(self, initial_learning_rate: float, power: float = 1.0, offset: int = 0, pre_offset_learning_rate: float = 1.0e6, name: str = "PowerDecayWithOffset"): """Initialize configuration of the learning rate schedule. Args: initial_learning_rate: The initial learning rate. power: The order of the polynomial. offset: The offset when computing the power decay. pre_offset_learning_rate: The maximum learning rate we'll use. name: Optional, name of learning rate schedule. """ super().__init__() self._initial_learning_rate = initial_learning_rate self._power = power self._offset = offset self._pre_offset_lr = pre_offset_learning_rate self._name = name def __call__(self, step): with tf.name_scope(self._name or "PowerDecayWithOffset"): step = tf.cast(step, tf.float32) lr_after_offset = tf.math.pow( tf.math.maximum(step - self._offset, 1.0), self._power) * ( self._initial_learning_rate) sign = tf.cast(step > self._offset, tf.float32) lr_combined = (1.0 - sign) * self._pre_offset_lr + sign * lr_after_offset # Power may give infinitely large LR. So cap it with pre_offset_lr. return tf.math.minimum(lr_combined, self._pre_offset_lr) def get_config(self): """Get the configuration of the learning rate schedule.""" return { "initial_learning_rate": self._initial_learning_rate, "power": self._power, "offset": self._offset, "pre_offset_learning_rate": self._pre_offset_lr, "name": self._name, } class StepCosineDecayWithOffset( tf.keras.optimizers.schedules.LearningRateSchedule): """Stepwise cosine learning rate decay with offset. Learning rate is equivalent to one or more cosine decay(s) starting and ending at each interval. ExampleL ```python boundaries: [100000, 110000] values: [1.0, 0.5] lr_decayed_fn = ( lr_schedule.StepCosineDecayWithOffset( boundaries, values)) ``` from 0 to 100000 step, it will cosine decay from 1.0 to 0.5 from 100000 to 110000 step, it cosine decay from 0.5 to 0.0 """ def __init__(self, boundaries, values, offset: int = 0, name: str = "StepCosineDecayWithOffset"): """Initialize configuration of the learning rate schedule. Args: boundaries: A list of `Tensor`s or `int`s with strictly increasing entries, and with all elements having the same type as the optimizer step. values: A list of `Tensor`s or `float`s that specifies the values for the intervals defined by `boundaries`. It should have one more element than `boundaries`, and all elements should have the same type. offset: The offset when computing the power decay. name: Optional, name of learning rate schedule. """ super().__init__() self.values = values self.boundaries = boundaries self.offset = offset self.name = name if len(self.values) < 1: raise ValueError(f"Expect non empty {self.values}") if len(self.boundaries) != len(self.values): raise ValueError( "Boundaries length is equal to learning rate levels length" f"{len(self.boundaries)} != {len(self.values)}") self.total_steps = ( [boundaries[i + 1] - boundaries[i] for i in range(len(boundaries) - 1) ] + [0]) def __call__(self, global_step): with tf.name_scope(self.name or "StepCosineDecayWithOffset"): global_step = tf.cast(global_step - self.offset, tf.float32) lr_levels = self.values lr_steps = self.boundaries level_total_steps = self.total_steps num_levels = len(lr_levels) init_lr = lr_levels[0] next_init_lr = lr_levels[1] if num_levels > 1 else 0. init_total_steps = level_total_steps[0] cosine_learning_rate = ((init_lr - next_init_lr) * (tf.cos( tf.constant(math.pi) * (global_step) / (init_total_steps)) + 1.0) / 2.0 + next_init_lr) learning_rate = cosine_learning_rate for i in range(1, num_levels): next_init_lr = lr_levels[i] next_start_step = lr_steps[i] next_total_steps = level_total_steps[i] next_next_init_lr = lr_levels[i + 1] if num_levels > i + 1 else 0. next_cosine_learning_rate = ((next_init_lr - next_next_init_lr) * (tf.cos( tf.constant(math.pi) * (global_step - next_start_step) / (next_total_steps)) + 1.0) / 2.0 + next_next_init_lr) learning_rate = tf.where(global_step >= next_start_step, next_cosine_learning_rate, learning_rate) return learning_rate def get_config(self): return { "boundaries": self.boundaries, "values": self.values, "offset": self.offset, "name": self.name }
18,498
36.907787
111
py
models
models-master/official/modeling/optimization/adafactor_optimizer.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Adafactor optimizer. A new optimizer that will be open sourced soon. """ # pylint: disable=invalid-name, represents an unimplemented class definition. Adafactor = "Unimplemented"
792
36.761905
77
py
models
models-master/official/modeling/optimization/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Optimization package definition.""" # pylint: disable=wildcard-import from official.modeling.optimization.configs.learning_rate_config import * from official.modeling.optimization.configs.optimization_config import * from official.modeling.optimization.configs.optimizer_config import * from official.modeling.optimization.ema_optimizer import ExponentialMovingAverage from official.modeling.optimization.lr_schedule import * from official.modeling.optimization.optimizer_factory import OptimizerFactory from official.modeling.optimization.optimizer_factory import register_optimizer_cls
1,201
47.08
83
py
models
models-master/official/modeling/optimization/slide_optimizer.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """SLIDE optimizer. A new optimizer that will be open sourced soon. """ SLIDE = "Unimplemented"
707
32.714286
74
py
models
models-master/official/modeling/optimization/ema_optimizer.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Exponential moving average optimizer.""" from typing import List, Optional import tensorflow as tf # pylint: disable=protected-access def maybe_merge_call(fn, strategy, *args, **kwargs): """Maybe invoke `fn` via `merge_call` which may or may not be fulfilled. The caller of this utility function requests to invoke `fn` via `merge_call` at `tf.distribute.Strategy`'s best efforts. It is `tf.distribute`'s internal whether the request is honored, depending on the `Strategy`. See `tf.distribute.ReplicaContext.merge_call()` for more information. This is adapted from tensorflow/python/distribute/merge_call_interim.py. Args: fn: the function to be invoked. strategy: the `tf.distribute.Strategy` to call `fn` with. *args: the positional arguments to be passed in to `fn`. **kwargs: the keyword arguments to be passed in to `fn`. Returns: The return value of the `fn` call. """ if strategy.extended._use_merge_call(): return tf.distribute.get_replica_context().merge_call( fn, args=args, kwargs=kwargs ) else: return fn(strategy, *args, **kwargs) class ExponentialMovingAverage(tf.keras.optimizers.legacy.Optimizer): """Optimizer that computes an exponential moving average of the variables. Empirically it has been found that using the moving average of the trained parameters of a deep network is better than using its trained parameters directly. This optimizer allows you to compute this moving average and swap the variables at save time so that any code outside of the training loop will use by default the average values instead of the original ones. Example of usage for training: ```python opt = tf.keras.optimizers.SGD(learning_rate) opt = ExponentialMovingAverage(opt) opt.shadow_copy(model) ``` At test time, swap the shadow variables to evaluate on the averaged weights: ```python opt.swap_weights() # Test eval the model here opt.swap_weights() ``` """ def __init__(self, optimizer: tf.keras.optimizers.Optimizer, trainable_weights_only: bool = True, average_decay: float = 0.99, start_step: int = 0, dynamic_decay: bool = True, name: str = 'ExponentialMovingAverage', **kwargs): """Construct a new ExponentialMovingAverage optimizer. Args: optimizer: `tf.keras.optimizers.Optimizer` that will be used to compute and apply gradients. trainable_weights_only: 'bool', if True, only model trainable weights will be updated. Otherwise, all model weights will be updated. This mainly affects batch normalization parameters. average_decay: float. Decay to use to maintain the moving averages of trained variables. start_step: int. What step to start the moving average. dynamic_decay: bool. Whether to change the decay based on the number of optimizer updates. Decay will start at 0.1 and gradually increase up to `average_decay` after each optimizer update. This behavior is similar to `tf.train.ExponentialMovingAverage` in TF 1.x. name: Optional name for the operations created when applying gradients. Defaults to "moving_average". **kwargs: keyword arguments. Allowed to be {`clipnorm`, `clipvalue`, `lr`, `decay`}. """ super().__init__(name, **kwargs) self._average_decay = average_decay self._trainable_weights_only = trainable_weights_only self._start_step = tf.constant(start_step, tf.float32) self._dynamic_decay = dynamic_decay self._optimizer = optimizer self._track_trackable(self._optimizer, 'ema_base_optimizer') self._average_weights = None self._model_weights = None def shadow_copy(self, model: tf.keras.Model): """Creates shadow variables for the given model weights.""" if self._trainable_weights_only: self._model_weights = model.trainable_variables else: self._model_weights = model.variables for var in self._model_weights: self.add_slot(var, 'average', initializer='zeros') self._average_weights = [ self.get_slot(var, 'average') for var in self._model_weights ] @property def has_shadow_copy(self): """Whether this optimizer has created shadow variables.""" return self._model_weights is not None and self._average_weights is not None def _create_slots(self, var_list): self._optimizer._create_slots(var_list=var_list) # pylint: disable=protected-access def apply_gradients(self, grads_and_vars, name: Optional[str] = None): result = self._optimizer.apply_gradients(grads_and_vars, name) maybe_merge_call(self.update_average, tf.distribute.get_strategy()) return result @tf.function def update_average(self, strategy): # Compute current decay value. step = tf.cast(self.iterations, tf.float32) if step < self._start_step: decay = tf.constant(0., tf.float32) elif self._dynamic_decay: decay = step - self._start_step decay = tf.minimum(self._average_decay, (1. + decay) / (10. + decay)) else: decay = self._average_decay def _apply_moving(average, normal): diff = average - normal average.assign_sub(tf.cast(1.0 - decay, average.dtype) * diff) return average # Update moving average with the latest value. for average, normal in zip(self._average_weights, self._model_weights): strategy.extended.update( average, _apply_moving, args=(normal,), group=False ) def swap_weights(self): """Swap the average and moving weights. This is a convenience method to allow one to evaluate the averaged weights at test time. Loads the weights stored in `self._average` into the model, keeping a copy of the original model weights. Swapping twice will return the original weights. """ if tf.distribute.in_cross_replica_context(): strategy = tf.distribute.get_strategy() strategy.run(self._swap_weights, args=()) else: raise ValueError( 'Swapping weights must occur under a tf.distribute.Strategy.' ) @tf.function def _swap_weights(self): def fn_0(a, b): a.assign_add(b) return a def fn_1(b, a): b.assign(a - b) return b def fn_2(a, b): a.assign_sub(b) return a def _swap(strategy, a_and_b): """Swap `a` and `b` and mirror to all devices.""" for a, b in a_and_b: strategy.extended.update(a, fn_0, args=(b,)) # a = a + b strategy.extended.update(b, fn_1, args=(a,)) # b = a - b strategy.extended.update(a, fn_2, args=(b,)) # a = a - b # Use merge_call if requested by strategy and always for TPUStrategy as # the use of merge_call is not recommended and deprecated for other # strategies such as mirrored strategy (MS) and multi-worker mirrored # strategy (MWMS) if nccl/collective_ops are used, which can operate in # pure replica context. strategy = tf.distribute.get_strategy() if isinstance(strategy, tf.distribute.TPUStrategy): maybe_merge_call( _swap, strategy, zip(self._average_weights, self._model_weights), ) else: _swap( strategy, zip(self._average_weights, self._model_weights), ) def assign_average_vars(self, var_list: List[tf.Variable]): """Assign variables in var_list with their respective averages. Args: var_list: List of model variables to be assigned to their average. Returns: assign_op: The op corresponding to the assignment operation of variables to their average. """ assign_op = tf.group([ var.assign(self.get_slot(var, 'average')) for var in var_list if var.trainable ]) return assign_op def _create_hypers(self): self._optimizer._create_hypers() # pylint: disable=protected-access def _prepare(self, var_list): return self._optimizer._prepare(var_list=var_list) # pylint: disable=protected-access @property def iterations(self): return self._optimizer.iterations @iterations.setter def iterations(self, variable): self._optimizer.iterations = variable @property def weights(self): # return self._weights + self._optimizer.weights return self._optimizer.weights def variables(self): return self._weights + [self.iterations] @property def lr(self): return self._optimizer._get_hyper('learning_rate') @lr.setter def lr(self, lr): self._optimizer._set_hyper('learning_rate', lr) @property def learning_rate(self): return self._optimizer._get_hyper('learning_rate') @learning_rate.setter def learning_rate(self, learning_rate): # pylint: disable=redefined-outer-name self._optimizer._set_hyper('learning_rate', learning_rate) def _resource_apply_dense(self, grad, var): return self._optimizer._resource_apply_dense(grad, var) def _resource_apply_sparse(self, grad, var, indices): return self._optimizer._resource_apply_sparse(grad, var, indices) def _resource_apply_sparse_duplicate_indices(self, grad, var, indices): return self._optimizer._resource_apply_sparse_duplicate_indices( grad, var, indices) def get_config(self): config = { 'optimizer': tf.keras.optimizers.serialize(self._optimizer), 'average_decay': self._average_decay, 'start_step': self._start_step, 'dynamic_decay': self._dynamic_decay, } base_config = super(ExponentialMovingAverage, self).get_config() return dict(list(base_config.items()) + list(config.items())) @classmethod def from_config(cls, config, custom_objects=None): optimizer = tf.keras.optimizers.deserialize( config.pop('optimizer'), custom_objects=custom_objects, ) return cls(optimizer, **config)
10,508
34.383838
90
py
models
models-master/official/modeling/optimization/lars.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Layer-wise adaptive rate scaling optimizer.""" import re from typing import Text, List, Optional import tensorflow as tf # pylint: disable=protected-access class LARS(tf.keras.optimizers.legacy.Optimizer): """Layer-wise Adaptive Rate Scaling for large batch training. Introduced by "Large Batch Training of Convolutional Networks" by Y. You, I. Gitman, and B. Ginsburg. (https://arxiv.org/abs/1708.03888) """ def __init__(self, learning_rate: float = 0.01, momentum: float = 0.9, weight_decay_rate: float = 0.0, eeta: float = 0.001, nesterov: bool = False, classic_momentum: bool = True, exclude_from_weight_decay: Optional[List[Text]] = None, exclude_from_layer_adaptation: Optional[List[Text]] = None, name: Text = "LARS", **kwargs): """Constructs a LARSOptimizer. Args: learning_rate: `float` for learning rate. Defaults to 0.01. momentum: `float` hyperparameter >= 0 that accelerates gradient descent in the relevant direction and dampens oscillations. Defaults to 0.9. weight_decay_rate: `float` for weight decay. eeta: `float` LARS coefficient as used in the paper. Default set to LARS coefficient from the paper. (eeta / weight_decay) determines the highest scaling factor in LARS.. nesterov: 'boolean' for whether to use nesterov momentum. classic_momentum: `boolean` for whether to use classic (or popular) momentum. The learning rate is applied during momentum update in classic momentum, but after momentum for popular momentum. exclude_from_weight_decay: A list of `string` for variable screening, if any of the string appears in a variable's name, the variable will be excluded for computing weight decay. For example, one could specify the list like ['batch_normalization', 'bias'] to exclude BN and bias from weight decay. exclude_from_layer_adaptation: Similar to exclude_from_weight_decay, but for layer adaptation. If it is None, it will be defaulted the same as exclude_from_weight_decay. name: `Text` as optional name for the operations created when applying gradients. Defaults to "LARS". **kwargs: keyword arguments. Allowed to be {`clipnorm`, `clipvalue`, `lr`, `decay`}. `clipnorm` is clip gradients by norm; `clipvalue` is clip gradients by value, `decay` is included for backward compatibility to allow time inverse decay of learning rate. `lr` is included for backward compatibility, recommended to use `learning_rate` instead. """ super(LARS, self).__init__(name, **kwargs) self._set_hyper("learning_rate", learning_rate) self._set_hyper("decay", self._initial_decay) self.momentum = momentum self.weight_decay_rate = weight_decay_rate self.eeta = eeta self.nesterov = nesterov self.classic_momentum = classic_momentum self.exclude_from_weight_decay = exclude_from_weight_decay # exclude_from_layer_adaptation is set to exclude_from_weight_decay if the # arg is None. if exclude_from_layer_adaptation: self.exclude_from_layer_adaptation = exclude_from_layer_adaptation else: self.exclude_from_layer_adaptation = exclude_from_weight_decay def _create_slots(self, var_list): for v in var_list: self.add_slot(v, "momentum") def _resource_apply_dense(self, grad, param, apply_state=None): if grad is None or param is None: return tf.no_op() var_device, var_dtype = param.device, param.dtype.base_dtype coefficients = ((apply_state or {}).get((var_device, var_dtype)) or self._fallback_apply_state(var_device, var_dtype)) learning_rate = coefficients["lr_t"] param_name = param.name v = self.get_slot(param, "momentum") if self._use_weight_decay(param_name): grad += self.weight_decay_rate * param if self.classic_momentum: trust_ratio = 1.0 if self._do_layer_adaptation(param_name): w_norm = tf.norm(param, ord=2) g_norm = tf.norm(grad, ord=2) trust_ratio = tf.where( tf.greater(w_norm, 0), tf.where(tf.greater(g_norm, 0), (self.eeta * w_norm / g_norm), 1.0), 1.0) scaled_lr = learning_rate * trust_ratio next_v = tf.multiply(self.momentum, v) + scaled_lr * grad if self.nesterov: update = tf.multiply(self.momentum, next_v) + scaled_lr * grad else: update = next_v next_param = param - update else: next_v = tf.multiply(self.momentum, v) + grad if self.nesterov: update = tf.multiply(self.momentum, next_v) + grad else: update = next_v trust_ratio = 1.0 if self._do_layer_adaptation(param_name): w_norm = tf.norm(param, ord=2) v_norm = tf.norm(update, ord=2) trust_ratio = tf.where( tf.greater(w_norm, 0), tf.where(tf.greater(v_norm, 0), (self.eeta * w_norm / v_norm), 1.0), 1.0) scaled_lr = trust_ratio * learning_rate next_param = param - scaled_lr * update return tf.group(*[ param.assign(next_param, use_locking=False), v.assign(next_v, use_locking=False) ]) def _resource_apply_sparse(self, grad, handle, indices, apply_state): raise NotImplementedError("Applying sparse gradients is not implemented.") def _use_weight_decay(self, param_name): """Whether to use L2 weight decay for `param_name`.""" if not self.weight_decay_rate: return False if self.exclude_from_weight_decay: for r in self.exclude_from_weight_decay: if re.search(r, param_name) is not None: return False return True def _do_layer_adaptation(self, param_name): """Whether to do layer-wise learning rate adaptation for `param_name`.""" if self.exclude_from_layer_adaptation: for r in self.exclude_from_layer_adaptation: if re.search(r, param_name) is not None: return False return True def get_config(self): config = super(LARS, self).get_config() config.update({ "learning_rate": self._serialize_hyperparameter("learning_rate"), "decay": self._serialize_hyperparameter("decay"), "momentum": self.momentum, "classic_momentum": self.classic_momentum, "weight_decay_rate": self.weight_decay_rate, "eeta": self.eeta, "nesterov": self.nesterov, }) return config @classmethod def from_config(cls, config, custom_objects=None): return cls(**config)
7,338
38.245989
80
py
models
models-master/official/modeling/optimization/lamb.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Layer-wise Adaptive Moments (LAMB) optimizer. See paper [Large Batch Optimization for Deep Learning: Training BERT in 76 minutes](https://arxiv.org/abs/1904.00962). """ import re from typing import Optional, Union, Callable, List import numpy as np import tensorflow as tf FloatTensorLike = Union[tf.Tensor, float, np.float16, np.float32] @tf.keras.utils.register_keras_serializable(package="Addons") class LAMB(tf.keras.optimizers.legacy.Optimizer): """Optimizer that implements the Layer-wise Adaptive Moments (LAMB). See paper [Large Batch Optimization for Deep Learning: Training BERT in 76 minutes](https://arxiv.org/abs/1904.00962). """ def __init__( self, learning_rate: Union[FloatTensorLike, Callable] = 0.001, beta_1: FloatTensorLike = 0.9, beta_2: FloatTensorLike = 0.999, epsilon: FloatTensorLike = 1e-6, weight_decay_rate: FloatTensorLike = 0.0, exclude_from_weight_decay: Optional[List[str]] = None, exclude_from_layer_adaptation: Optional[List[str]] = None, name: str = "LAMB", **kwargs, ): """Construct a new LAMB optimizer. Args: learning_rate: A `Tensor` or a floating point value. or a schedule that is a `tf.keras.optimizers.schedules.LearningRateSchedule` The learning rate. beta_1: A `float` value or a constant `float` tensor. The exponential decay rate for the 1st moment estimates. beta_2: A `float` value or a constant `float` tensor. The exponential decay rate for the 2nd moment estimates. epsilon: A small constant for numerical stability. weight_decay_rate: weight decay rate. exclude_from_weight_decay: List of regex patterns of variables excluded from weight decay. Variables whose name contain a substring matching the pattern will be excluded. exclude_from_layer_adaptation: List of regex patterns of variables excluded from layer adaptation. Variables whose name contain a substring matching the pattern will be excluded. name: Optional name for the operations created when applying gradients. Defaults to "LAMB". **kwargs: keyword arguments. Allowed to be {`clipnorm`, `clipvalue`, `lr`, `decay`}. `clipnorm` is clip gradients by norm; `clipvalue` is clip gradients by value, `decay` is included for backward compatibility to allow time inverse decay of learning rate. `lr` is included for backward compatibility, recommended to use `learning_rate` instead. """ super().__init__(name, **kwargs) # Just adding the square of the weights to the loss function is *not* # the correct way of using L2 regularization/weight decay with Adam, # since that will interact with the m and v parameters in strange ways. # # Instead we want to decay the weights in a manner that doesn't interact # with the m/v parameters. self._set_hyper("weight_decay_rate", weight_decay_rate) self._set_hyper("learning_rate", kwargs.get("lr", learning_rate)) # This is learning rate decay for using keras learning rate schedule. self._set_hyper("decay", self._initial_decay) self._set_hyper("beta_1", beta_1) self._set_hyper("beta_2", beta_2) self.epsilon = epsilon or tf.backend_config.epsilon() self.exclude_from_weight_decay = exclude_from_weight_decay # exclude_from_layer_adaptation is set to exclude_from_weight_decay if # the arg is None. if exclude_from_layer_adaptation: self.exclude_from_layer_adaptation = exclude_from_layer_adaptation else: self.exclude_from_layer_adaptation = exclude_from_weight_decay def _create_slots(self, var_list): # Create slots for the first and second moments. # Separate for-loops to respect the ordering of slot variables from v1. for var in var_list: self.add_slot(var, "m") for var in var_list: self.add_slot(var, "v") def _prepare_local(self, var_device, var_dtype, apply_state): super()._prepare_local(var_device, var_dtype, apply_state) local_step = tf.cast(self.iterations + 1, var_dtype) beta_1_t = tf.identity(self._get_hyper("beta_1", var_dtype)) beta_2_t = tf.identity(self._get_hyper("beta_2", var_dtype)) weight_decay_rate = tf.identity( self._get_hyper("weight_decay_rate", var_dtype) ) beta_1_power = tf.pow(beta_1_t, local_step) beta_2_power = tf.pow(beta_2_t, local_step) apply_state[(var_device, var_dtype)].update( dict( weight_decay_rate=weight_decay_rate, epsilon=tf.convert_to_tensor(self.epsilon, var_dtype), beta_1_t=beta_1_t, beta_1_power=beta_1_power, one_minus_beta_1_t=1 - beta_1_t, beta_2_t=beta_2_t, beta_2_power=beta_2_power, one_minus_beta_2_t=1 - beta_2_t, ) ) def _resource_apply_dense(self, grad, var, apply_state=None): var_device, var_dtype = var.device, var.dtype.base_dtype coefficients = (apply_state or {}).get( (var_device, var_dtype) ) or self._fallback_apply_state(var_device, var_dtype) # m_t = beta1 * m + (1 - beta1) * g_t m = self.get_slot(var, "m") m_scaled_g_values = grad * coefficients["one_minus_beta_1_t"] m_t = m * coefficients["beta_1_t"] + m_scaled_g_values m_t = m.assign(m_t, use_locking=self._use_locking) # v_t = beta2 * v + (1 - beta2) * (g_t * g_t) v = self.get_slot(var, "v") v_scaled_g_values = (grad * grad) * coefficients["one_minus_beta_2_t"] v_t = v * coefficients["beta_2_t"] + v_scaled_g_values v_t = v.assign(v_t, use_locking=self._use_locking) m_t_hat = m_t / (1.0 - coefficients["beta_1_power"]) v_t_hat = v_t / (1.0 - coefficients["beta_2_power"]) v_sqrt = tf.sqrt(v_t_hat) update = m_t_hat / (v_sqrt + coefficients["epsilon"]) var_name = self._get_variable_name(var.name) if self._do_use_weight_decay(var_name): update += coefficients["weight_decay_rate"] * var ratio = 1.0 if self._do_layer_adaptation(var_name): w_norm = tf.norm(var, ord=2) g_norm = tf.norm(update, ord=2) ratio = tf.where( tf.greater(w_norm, 0), tf.where(tf.greater(g_norm, 0), (w_norm / g_norm), 1.0), 1.0, ) var_update = var - ratio * coefficients["lr_t"] * update return var.assign(var_update, use_locking=self._use_locking) def _resource_apply_sparse(self, grad, var, indices, apply_state=None): var_device, var_dtype = var.device, var.dtype.base_dtype coefficients = (apply_state or {}).get( (var_device, var_dtype) ) or self._fallback_apply_state(var_device, var_dtype) # m_t = beta1 * m + (1 - beta1) * g_t m = self.get_slot(var, "m") m_scaled_g_values = grad * coefficients["one_minus_beta_1_t"] m_t = m.assign(m * coefficients["beta_1_t"], use_locking=self._use_locking) with tf.control_dependencies([m_t]): m_t = self._resource_scatter_add(m, indices, m_scaled_g_values) # v_t = beta2 * v + (1 - beta2) * (g_t * g_t) v = self.get_slot(var, "v") v_scaled_g_values = (grad * grad) * coefficients["one_minus_beta_2_t"] v_t = v.assign(v * coefficients["beta_2_t"], use_locking=self._use_locking) with tf.control_dependencies([v_t]): v_t = self._resource_scatter_add(v, indices, v_scaled_g_values) m_t_hat = m_t / (1.0 - coefficients["beta_1_power"]) v_t_hat = v_t / (1.0 - coefficients["beta_2_power"]) v_sqrt = tf.sqrt(v_t_hat) update = m_t_hat / (v_sqrt + coefficients["epsilon"]) var_name = self._get_variable_name(var.name) if self._do_use_weight_decay(var_name): update += coefficients["weight_decay_rate"] * var ratio = 1.0 if self._do_layer_adaptation(var_name): w_norm = tf.norm(var, ord=2) g_norm = tf.norm(update, ord=2) ratio = tf.where( tf.greater(w_norm, 0), tf.where(tf.greater(g_norm, 0), (w_norm / g_norm), 1.0), 1.0, ) var_update = var.assign_sub( ratio * coefficients["lr_t"] * update, use_locking=self._use_locking ) return tf.group(*[var_update, m_t, v_t]) def get_config(self): config = super().get_config() config.update({ "learning_rate": self._serialize_hyperparameter("learning_rate"), "weight_decay_rate": self._serialize_hyperparameter( "weight_decay_rate" ), "decay": self._serialize_hyperparameter("decay"), "beta_1": self._serialize_hyperparameter("beta_1"), "beta_2": self._serialize_hyperparameter("beta_2"), "epsilon": self.epsilon, }) return config def _do_use_weight_decay(self, param_name): """Whether to use L2 weight decay for `param_name`.""" if self.exclude_from_weight_decay: for r in self.exclude_from_weight_decay: if re.search(r, param_name) is not None: return False return True def _do_layer_adaptation(self, param_name): """Whether to do layer-wise learning rate adaptation for `param_name`.""" if self.exclude_from_layer_adaptation: for r in self.exclude_from_layer_adaptation: if re.search(r, param_name) is not None: return False return True def _get_variable_name(self, param_name): """Get the variable name from the tensor name.""" m = re.match("^(.*):\\d+$", param_name) if m is not None: param_name = m.group(1) return param_name
10,137
39.071146
80
py
models
models-master/official/modeling/optimization/configs/optimizer_config.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Dataclasses for optimizer configs.""" from typing import List, Optional import dataclasses from official.modeling.hyperparams import base_config @dataclasses.dataclass class BaseOptimizerConfig(base_config.Config): """Base optimizer config. Attributes: clipnorm: float >= 0 or None. If not None, Gradients will be clipped when their L2 norm exceeds this value. clipvalue: float >= 0 or None. If not None, Gradients will be clipped when their absolute value exceeds this value. global_clipnorm: float >= 0 or None. If not None, gradient of all weights is clipped so that their global norm is no higher than this value """ clipnorm: Optional[float] = None clipvalue: Optional[float] = None global_clipnorm: Optional[float] = None @dataclasses.dataclass class SGDConfig(BaseOptimizerConfig): """Configuration for SGD optimizer. The attributes for this class matches the arguments of tf.keras.optimizer.SGD. Attributes: name: name of the optimizer. decay: decay rate for SGD optimizer. nesterov: nesterov for SGD optimizer. momentum: momentum for SGD optimizer. """ name: str = "SGD" decay: float = 0.0 nesterov: bool = False momentum: float = 0.0 # TODO(b/216129465): Merge this config with SGDConfig after the experimental # optimizer graduates. @dataclasses.dataclass class SGDExperimentalConfig(BaseOptimizerConfig): """Configuration for SGD optimizer. The attributes for this class matches the arguments of `tf.keras.optimizer.experimental.SGD`. Attributes: name: name of the optimizer. nesterov: nesterov for SGD optimizer. momentum: momentum for SGD optimizer. jit_compile: if True, jit compile will be used. """ name: str = "SGD" nesterov: bool = False momentum: float = 0.0 jit_compile: bool = False @dataclasses.dataclass class RMSPropConfig(BaseOptimizerConfig): """Configuration for RMSProp optimizer. The attributes for this class matches the arguments of tf.keras.optimizers.RMSprop. Attributes: name: name of the optimizer. rho: discounting factor for RMSprop optimizer. momentum: momentum for RMSprop optimizer. epsilon: epsilon value for RMSprop optimizer, help with numerical stability. centered: Whether to normalize gradients or not. """ name: str = "RMSprop" rho: float = 0.9 momentum: float = 0.0 epsilon: float = 1e-7 centered: bool = False @dataclasses.dataclass class AdagradConfig(BaseOptimizerConfig): """Configuration for Adagrad optimizer. The attributes of this class match the arguments of tf.keras.optimizer.Adagrad. Attributes: name: name of the optimizer. initial_accumulator_value: A floating point value. Starting value for the accumulators, must be non-negative. epsilon: A small floating point value to avoid zero denominator. """ name: str = "Adagrad" initial_accumulator_value: float = 0.1 epsilon: float = 1e-07 @dataclasses.dataclass class AdamConfig(BaseOptimizerConfig): """Configuration for Adam optimizer. The attributes for this class matches the arguments of tf.keras.optimizer.Adam. Attributes: name: name of the optimizer. beta_1: decay rate for 1st order moments. beta_2: decay rate for 2st order moments. epsilon: epsilon value used for numerical stability in Adam optimizer. amsgrad: boolean. Whether to apply AMSGrad variant of this algorithm from the paper "On the Convergence of Adam and beyond". """ name: str = "Adam" beta_1: float = 0.9 beta_2: float = 0.999 epsilon: float = 1e-07 amsgrad: bool = False @dataclasses.dataclass class AdamExperimentalConfig(BaseOptimizerConfig): """Configuration for experimental Adam optimizer. The attributes for this class matches the arguments of `tf.keras.optimizer.experimental.Adam`. Attributes: name: name of the optimizer. beta_1: decay rate for 1st order moments. beta_2: decay rate for 2st order moments. epsilon: epsilon value used for numerical stability in Adam optimizer. amsgrad: boolean. Whether to apply AMSGrad variant of this algorithm from the paper "On the Convergence of Adam and beyond". jit_compile: if True, jit compile will be used. """ name: str = "Adam" beta_1: float = 0.9 beta_2: float = 0.999 epsilon: float = 1e-07 amsgrad: bool = False jit_compile: bool = False @dataclasses.dataclass class AdamWeightDecayConfig(BaseOptimizerConfig): """Configuration for Adam optimizer with weight decay. Attributes: name: name of the optimizer. beta_1: decay rate for 1st order moments. beta_2: decay rate for 2st order moments. epsilon: epsilon value used for numerical stability in the optimizer. amsgrad: boolean. Whether to apply AMSGrad variant of this algorithm from the paper "On the Convergence of Adam and beyond". weight_decay_rate: float. Weight decay rate. Default to 0. include_in_weight_decay: list[str], or None. List of weight names to include in weight decay. exclude_from_weight_decay: list[str], or None. List of weight names to not include in weight decay. gradient_clip_norm: A positive float. Clips the gradients to this maximum L2-norm. Default to 1.0. """ name: str = "AdamWeightDecay" beta_1: float = 0.9 beta_2: float = 0.999 epsilon: float = 1e-07 amsgrad: bool = False weight_decay_rate: float = 0.0 include_in_weight_decay: Optional[List[str]] = None exclude_from_weight_decay: Optional[List[str]] = None gradient_clip_norm: float = 1.0 @dataclasses.dataclass class AdamWeightDecayExperimentalConfig(BaseOptimizerConfig): """Configuration for Adam optimizer with weight decay. Attributes: name: name of the optimizer. beta_1: decay rate for 1st order moments. beta_2: decay rate for 2st order moments. epsilon: epsilon value used for numerical stability in the optimizer. amsgrad: boolean. Whether to apply AMSGrad variant of this algorithm from the paper "On the Convergence of Adam and beyond". weight_decay: float. Weight decay rate. Default to 0. global_clipnorm: A positive float. Clips the gradients to this maximum L2-norm. Default to 1.0. jit_compile: if True, jit compile will be used. """ name: str = "AdamWeightDecayExperimental" beta_1: float = 0.9 beta_2: float = 0.999 epsilon: float = 1e-07 amsgrad: bool = False weight_decay: float = 0.0 global_clipnorm: float = 1.0 jit_compile: bool = False @dataclasses.dataclass class LAMBConfig(BaseOptimizerConfig): """Configuration for LAMB optimizer. The attributes for this class matches the arguments of LAMB optimizer. Attributes: name: name of the optimizer. beta_1: decay rate for 1st order moments. beta_2: decay rate for 2st order moments. epsilon: epsilon value used for numerical stability in LAMB optimizer. weight_decay_rate: float. Weight decay rate. Default to 0. exclude_from_weight_decay: List of regex patterns of variables excluded from weight decay. Variables whose name contain a substring matching the pattern will be excluded. exclude_from_layer_adaptation: List of regex patterns of variables excluded from layer adaptation. Variables whose name contain a substring matching the pattern will be excluded. """ name: str = "LAMB" beta_1: float = 0.9 beta_2: float = 0.999 epsilon: float = 1e-6 weight_decay_rate: float = 0.0 exclude_from_weight_decay: Optional[List[str]] = None exclude_from_layer_adaptation: Optional[List[str]] = None @dataclasses.dataclass class EMAConfig(BaseOptimizerConfig): """Exponential moving average optimizer config. Attributes: name: 'str', name of the optimizer. trainable_weights_only: 'bool', if True, only model trainable weights will be updated. Otherwise, all model weights will be updated. This mainly affects batch normalization parameters. average_decay: 'float', average decay value. start_step: 'int', start step to apply moving average. dynamic_decay: 'bool', whether to apply dynamic decay or not. """ name: str = "ExponentialMovingAverage" trainable_weights_only: bool = True average_decay: float = 0.99 start_step: int = 0 dynamic_decay: bool = True @dataclasses.dataclass class LARSConfig(BaseOptimizerConfig): """Layer-wise adaptive rate scaling config. Attributes: name: 'str', name of the optimizer. momentum: `float` hyperparameter >= 0 that accelerates gradient descent in the relevant direction and dampens oscillations. Defaults to 0.9. eeta: `float` LARS coefficient as used in the paper. Default set to LARS coefficient from the paper. (eeta / weight_decay) determines the highest scaling factor in LARS.. weight_decay_rate: `float` for weight decay. nesterov: 'boolean' for whether to use nesterov momentum. classic_momentum: `boolean` for whether to use classic (or popular) momentum. The learning rate is applied during momentum update in classic momentum, but after momentum for popular momentum. exclude_from_weight_decay: A list of `string` for variable screening, if any of the string appears in a variable's name, the variable will be excluded for computing weight decay. For example, one could specify the list like ['batch_normalization', 'bias'] to exclude BN and bias from weight decay. exclude_from_layer_adaptation: Similar to exclude_from_weight_decay, but for layer adaptation. If it is None, it will be defaulted the same as exclude_from_weight_decay. """ name: str = "LARS" momentum: float = 0.9 eeta: float = 0.001 weight_decay_rate: float = 0.0 nesterov: bool = False classic_momentum: bool = True exclude_from_weight_decay: Optional[List[str]] = None exclude_from_layer_adaptation: Optional[List[str]] = None @dataclasses.dataclass class SLIDEConfig(BaseOptimizerConfig): """Configuration for SLIDE optimizer. Details coming soon. """ name: str = "SLIDE" beta_1: float = 0.9 beta_2: float = 0.999 epsilon: float = 1e-6 weight_decay_rate: float = 0.0 weight_decay_type: str = "inner" exclude_from_weight_decay: Optional[List[str]] = None exclude_from_layer_adaptation: Optional[List[str]] = None include_in_sparse_layer_adaptation: Optional[List[str]] = None sparse_layer_learning_rate: float = 0.1 do_gradient_rescaling: bool = True norm_type: str = "layer" ratio_clip_norm: float = 1e5 @dataclasses.dataclass class AdafactorConfig(BaseOptimizerConfig): """Configuration for Adafactor optimizer. The attributes for this class matches the arguments of the Adafactor implementation. """ name: str = "Adafactor" factored: bool = True multiply_by_parameter_scale: bool = True beta1: Optional[float] = None decay_rate: float = 0.8 step_offset: int = 0 clipping_threshold: float = 1.0 min_dim_size_to_factor: int = 128 epsilon1: float = 1e-30 epsilon2: float = 1e-3 weight_decay: Optional[float] = None include_in_weight_decay: Optional[str] = None
11,724
33.384164
80
py
models
models-master/official/modeling/optimization/configs/optimization_config.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Dataclasses for optimization configs. This file define the dataclass for optimization configs (OptimizationConfig). It also has two helper functions get_optimizer_config, and get_lr_config from an OptimizationConfig class. """ from typing import Optional import dataclasses from official.modeling.hyperparams import base_config from official.modeling.hyperparams import oneof from official.modeling.optimization.configs import learning_rate_config as lr_cfg from official.modeling.optimization.configs import optimizer_config as opt_cfg @dataclasses.dataclass class OptimizerConfig(oneof.OneOfConfig): """Configuration for optimizer. Attributes: type: 'str', type of optimizer to be used, on the of fields below. sgd: sgd optimizer config. adam: adam optimizer config. adamw: adam with weight decay. lamb: lamb optimizer. rmsprop: rmsprop optimizer. lars: lars optimizer. adagrad: adagrad optimizer. slide: slide optimizer. """ type: Optional[str] = None sgd: opt_cfg.SGDConfig = dataclasses.field(default_factory=opt_cfg.SGDConfig) sgd_experimental: opt_cfg.SGDExperimentalConfig = dataclasses.field( default_factory=opt_cfg.SGDExperimentalConfig ) adam: opt_cfg.AdamConfig = dataclasses.field( default_factory=opt_cfg.AdamConfig ) adam_experimental: opt_cfg.AdamExperimentalConfig = dataclasses.field( default_factory=opt_cfg.AdamExperimentalConfig ) adamw: opt_cfg.AdamWeightDecayConfig = dataclasses.field( default_factory=opt_cfg.AdamWeightDecayConfig ) adamw_experimental: opt_cfg.AdamWeightDecayExperimentalConfig = ( dataclasses.field( default_factory=opt_cfg.AdamWeightDecayExperimentalConfig ) ) lamb: opt_cfg.LAMBConfig = dataclasses.field( default_factory=opt_cfg.LAMBConfig ) rmsprop: opt_cfg.RMSPropConfig = dataclasses.field( default_factory=opt_cfg.RMSPropConfig ) lars: opt_cfg.LARSConfig = dataclasses.field( default_factory=opt_cfg.LARSConfig ) adagrad: opt_cfg.AdagradConfig = dataclasses.field( default_factory=opt_cfg.AdagradConfig ) slide: opt_cfg.SLIDEConfig = dataclasses.field( default_factory=opt_cfg.SLIDEConfig ) adafactor: opt_cfg.AdafactorConfig = dataclasses.field( default_factory=opt_cfg.AdafactorConfig ) @dataclasses.dataclass class LrConfig(oneof.OneOfConfig): """Configuration for lr schedule. Attributes: type: 'str', type of lr schedule to be used, one of the fields below. constant: constant learning rate config. stepwise: stepwise learning rate config. exponential: exponential learning rate config. polynomial: polynomial learning rate config. cosine: cosine learning rate config. power: step^power learning rate config. power_linear: learning rate config of step^power followed by step^power*linear. power_with_offset: power decay with a step offset. step_cosine_with_offset: Step cosine with a step offset. """ type: Optional[str] = None constant: lr_cfg.ConstantLrConfig = dataclasses.field( default_factory=lr_cfg.ConstantLrConfig ) stepwise: lr_cfg.StepwiseLrConfig = dataclasses.field( default_factory=lr_cfg.StepwiseLrConfig ) exponential: lr_cfg.ExponentialLrConfig = dataclasses.field( default_factory=lr_cfg.ExponentialLrConfig ) polynomial: lr_cfg.PolynomialLrConfig = dataclasses.field( default_factory=lr_cfg.PolynomialLrConfig ) cosine: lr_cfg.CosineLrConfig = dataclasses.field( default_factory=lr_cfg.CosineLrConfig ) power: lr_cfg.DirectPowerLrConfig = dataclasses.field( default_factory=lr_cfg.DirectPowerLrConfig ) power_linear: lr_cfg.PowerAndLinearDecayLrConfig = dataclasses.field( default_factory=lr_cfg.PowerAndLinearDecayLrConfig ) power_with_offset: lr_cfg.PowerDecayWithOffsetLrConfig = dataclasses.field( default_factory=lr_cfg.PowerDecayWithOffsetLrConfig ) step_cosine_with_offset: lr_cfg.StepCosineLrConfig = dataclasses.field( default_factory=lr_cfg.StepCosineLrConfig ) @dataclasses.dataclass class WarmupConfig(oneof.OneOfConfig): """Configuration for lr schedule. Attributes: type: 'str', type of warmup schedule to be used, one of the fields below. linear: linear warmup config. polynomial: polynomial warmup config. """ type: Optional[str] = None linear: lr_cfg.LinearWarmupConfig = dataclasses.field( default_factory=lr_cfg.LinearWarmupConfig ) polynomial: lr_cfg.PolynomialWarmupConfig = dataclasses.field( default_factory=lr_cfg.PolynomialWarmupConfig ) @dataclasses.dataclass class OptimizationConfig(base_config.Config): """Configuration for optimizer and learning rate schedule. Attributes: optimizer: optimizer oneof config. ema: optional exponential moving average optimizer config, if specified, ema optimizer will be used. learning_rate: learning rate oneof config. warmup: warmup oneof config. """ optimizer: OptimizerConfig = dataclasses.field( default_factory=OptimizerConfig ) ema: Optional[opt_cfg.EMAConfig] = None learning_rate: LrConfig = dataclasses.field(default_factory=LrConfig) warmup: WarmupConfig = dataclasses.field(default_factory=WarmupConfig)
5,880
34.215569
81
py
models
models-master/official/modeling/optimization/configs/learning_rate_config.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Dataclasses for learning rate schedule config.""" from typing import List, Optional import dataclasses from official.modeling.hyperparams import base_config @dataclasses.dataclass class ConstantLrConfig(base_config.Config): """Configuration for constant learning rate. This class is a containers for the constant learning rate decay configs. Attributes: name: The name of the learning rate schedule. Defaults to Constant. learning_rate: A float. The learning rate. Defaults to 0.1. """ name: str = 'Constant' learning_rate: float = 0.1 @dataclasses.dataclass class StepwiseLrConfig(base_config.Config): """Configuration for stepwise learning rate decay. This class is a container for the piecewise constant learning rate scheduling configs. It will configure an instance of PiecewiseConstantDecay keras learning rate schedule. An example (from keras docs): use a learning rate that's 1.0 for the first 100001 steps, 0.5 for the next 10000 steps, and 0.1 for any additional steps. ```python boundaries: [100000, 110000] values: [1.0, 0.5, 0.1] Attributes: name: The name of the learning rate schedule. Defaults to PiecewiseConstant. boundaries: A list of ints of strictly increasing entries. Defaults to None. values: A list of floats that specifies the values for the intervals defined by `boundaries`. It should have one more element than `boundaries`. The learning rate is computed as follows: [0, boundaries[0]] -> values[0] [boundaries[0], boundaries[1]] -> values[1] [boundaries[n-1], boundaries[n]] -> values[n] [boundaries[n], end] -> values[n+1] Defaults to None. offset: An int. The offset applied to steps. Defaults to 0. """ name: str = 'PiecewiseConstantDecay' boundaries: Optional[List[int]] = None values: Optional[List[float]] = None offset: int = 0 @dataclasses.dataclass class ExponentialLrConfig(base_config.Config): """Configuration for exponential learning rate decay. This class is a containers for the exponential learning rate decay configs. Attributes: name: The name of the learning rate schedule. Defaults to ExponentialDecay. initial_learning_rate: A float. The initial learning rate. Defaults to None. decay_steps: A positive integer that is used for decay computation. Defaults to None. decay_rate: A float. Defaults to None. staircase: A boolean, if true, learning rate is decreased at discreate intervals. Defaults to False. offset: An int. The offset applied to steps. Defaults to 0. """ name: str = 'ExponentialDecay' initial_learning_rate: Optional[float] = None decay_steps: Optional[int] = None decay_rate: Optional[float] = None staircase: Optional[bool] = None offset: int = 0 @dataclasses.dataclass class PolynomialLrConfig(base_config.Config): """Configuration for polynomial learning rate decay. This class is a containers for the polynomial learning rate decay configs. Attributes: name: The name of the learning rate schedule. Defaults to PolynomialDecay. initial_learning_rate: A float. The initial learning rate. Defaults to None. decay_steps: A positive integer that is used for decay computation. Defaults to None. end_learning_rate: A float. The minimal end learning rate. power: A float. The power of the polynomial. Defaults to linear, 1.0. cycle: A boolean, whether or not it should cycle beyond decay_steps. Defaults to False. offset: An int. The offset applied to steps. Defaults to 0. """ name: str = 'PolynomialDecay' initial_learning_rate: Optional[float] = None decay_steps: Optional[int] = None end_learning_rate: float = 0.0001 power: float = 1.0 cycle: bool = False offset: int = 0 @dataclasses.dataclass class CosineLrConfig(base_config.Config): """Configuration for Cosine learning rate decay. This class is a containers for the cosine learning rate decay configs, tf.keras.experimental.CosineDecay. Attributes: name: The name of the learning rate schedule. Defaults to CosineDecay. initial_learning_rate: A float. The initial learning rate. Defaults to None. decay_steps: A positive integer that is used for decay computation. Defaults to None. alpha: A float. Minimum learning rate value as a fraction of initial_learning_rate. offset: An int. The offset applied to steps. Defaults to 0. """ name: str = 'CosineDecay' initial_learning_rate: Optional[float] = None decay_steps: Optional[int] = None alpha: float = 0.0 offset: int = 0 @dataclasses.dataclass class DirectPowerLrConfig(base_config.Config): """Configuration for DirectPower learning rate decay. This class configures a schedule following follows lr * (step)^power. Attributes: name: The name of the learning rate schedule. Defaults to DirectPowerDecay. initial_learning_rate: A float. The initial learning rate. Defaults to None. power: A float. Defaults to -0.5, for sqrt decay. """ name: str = 'DirectPowerDecay' initial_learning_rate: Optional[float] = None power: float = -0.5 @dataclasses.dataclass class PowerAndLinearDecayLrConfig(base_config.Config): """Configuration for DirectPower learning rate decay. The schedule has the following behavoir. Let offset_step = step - offset. 1) offset_step < 0, the actual learning rate equals initial_learning_rate. 2) offset_step <= total_decay_steps * (1 - linear_decay_fraction), the actual learning rate equals lr * offset_step^power. 3) total_decay_steps * (1 - linear_decay_fraction) <= offset_step < total_decay_steps, the actual learning rate equals lr * offset_step^power * (total_decay_steps - offset_step) / (total_decay_steps * linear_decay_fraction). 4) offset_step >= total_decay_steps, the actual learning rate equals zero. Attributes: name: The name of the learning rate schedule. Defaults to PowerAndLinearDecay. initial_learning_rate: A float. The initial learning rate. Defaults to None. total_decay_steps: An int. The total number of steps for power + linear decay. Defaults to None. power: A float. The order of the polynomial. Defaults to -0.5, for sqrt decay. linear_decay_fraction: A float. In the last `linear_decay_fraction` steps, the learning rate will be multiplied by a linear decay. Defaults to 0.1. offset: An int. The offset applied to steps. Defaults to 0. """ name: str = 'PowerAndLinearDecay' initial_learning_rate: Optional[float] = None total_decay_steps: Optional[int] = None power: float = -0.5 linear_decay_fraction: float = 0.1 offset: int = 0 @dataclasses.dataclass class PowerDecayWithOffsetLrConfig(base_config.Config): """Configuration for power learning rate decay with step offset. Learning rate equals to `pre_offset_learning_rate` if `step` < `offset`. Otherwise, learning rate equals to lr * (step - offset)^power. Attributes: name: The name of the learning rate schedule. Defaults to PowerDecayWithOffset. initial_learning_rate: A float. The initial learning rate. Defaults to None. power: A float. Defaults to -0.5, for sqrt decay. offset: An integer. Power decay happens after `offset` steps. pre_offset_learning_rate: A float. The constant learning rate before `offset` steps. """ name: str = 'PowerDecayWithOffset' initial_learning_rate: Optional[float] = None power: float = -0.5 offset: int = 0 pre_offset_learning_rate: float = 1.0e6 @dataclasses.dataclass class StepCosineLrConfig(base_config.Config): """Configuration for stepwise learning rate decay. This class is a container for the piecewise cosine learning rate scheduling configs. It will configure an instance of StepCosineDecayWithOffset keras learning rate schedule. ```python boundaries: [100000, 110000] values: [1.0, 0.5] lr_decayed_fn = ( lr_schedule.StepCosineDecayWithOffset( boundaries, values)) ``` from 0 to 100000 step, it will cosine decay from 1.0 to 0.5 from 100000 to 110000 step, it cosine decay from 0.5 to 0.0 Attributes: name: The name of the learning rate schedule. Defaults to PiecewiseConstant. boundaries: A list of ints of strictly increasing entries. Defaults to None. values: A list of floats that specifies the values for the intervals defined by `boundaries`. It should have one more element than `boundaries`. The learning rate is computed as follows: [0, boundaries[0]] -> cosine from values[0] to values[1] [boundaries[0], boundaries[1]] -> values[1] to values[2] ... [boundaries[n-1], boundaries[n]] -> values[n] to values[n+1] [boundaries[n], end] -> values[n+1] to 0. offset: An int. The offset applied to steps. Defaults to 0. """ name: str = 'StepCosineDecayWithOffset' boundaries: Optional[List[int]] = None values: Optional[List[float]] = None offset: int = 0 @dataclasses.dataclass class LinearWarmupConfig(base_config.Config): """Configuration for linear warmup schedule config. This class is a container for the linear warmup schedule configs. Warmup_learning_rate is the initial learning rate, the final learning rate of the warmup period is the learning_rate of the optimizer in use. The learning rate at each step linearly increased according to the following formula: warmup_learning_rate = warmup_learning_rate + step / warmup_steps * (final_learning_rate - warmup_learning_rate). Using warmup overrides the learning rate schedule by the number of warmup steps. Attributes: name: The name of warmup schedule. Defaults to linear. warmup_learning_rate: Initial learning rate for the warmup. Defaults to 0. warmup_steps: Warmup steps. Defaults to None. """ name: str = 'linear' warmup_learning_rate: float = 0 warmup_steps: Optional[int] = None @dataclasses.dataclass class PolynomialWarmupConfig(base_config.Config): """Configuration for linear warmup schedule config. This class is a container for the polynomial warmup schedule configs. Attributes: name: The name of warmup schedule. Defaults to Polynomial. power: Polynomial power. Defaults to 1. warmup_steps: Warmup steps. Defaults to None. """ name: str = 'polynomial' power: float = 1 warmup_steps: Optional[int] = None
11,122
37.487889
80
py
models
models-master/official/modeling/optimization/configs/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
609
39.666667
74
py
models
models-master/official/modeling/optimization/configs/optimization_config_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for optimization_config.py.""" import tensorflow as tf from official.modeling.optimization.configs import learning_rate_config as lr_cfg from official.modeling.optimization.configs import optimization_config from official.modeling.optimization.configs import optimizer_config as opt_cfg class OptimizerConfigTest(tf.test.TestCase): def test_no_optimizer(self): optimizer = optimization_config.OptimizationConfig({}).optimizer.get() self.assertIsNone(optimizer) def test_no_lr_schedule(self): lr = optimization_config.OptimizationConfig({}).learning_rate.get() self.assertIsNone(lr) def test_no_warmup_schedule(self): warmup = optimization_config.OptimizationConfig({}).warmup.get() self.assertIsNone(warmup) def test_config(self): opt_config = optimization_config.OptimizationConfig({ 'optimizer': { 'type': 'sgd', 'sgd': {} # default config }, 'learning_rate': { 'type': 'polynomial', 'polynomial': {} }, 'warmup': { 'type': 'linear' } }) self.assertEqual(opt_config.optimizer.get(), opt_cfg.SGDConfig()) self.assertEqual(opt_config.learning_rate.get(), lr_cfg.PolynomialLrConfig()) self.assertEqual(opt_config.warmup.get(), lr_cfg.LinearWarmupConfig()) if __name__ == '__main__': tf.test.main()
2,009
32.5
81
py
models
models-master/official/modeling/multitask/base_model.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Abstraction of multi-task model.""" from typing import Text, Dict import tensorflow as tf class MultiTaskBaseModel(tf.Module): """Base class that holds multi-task model computation.""" def __init__(self, **kwargs): super().__init__(**kwargs) self._sub_tasks = self._instantiate_sub_tasks() def _instantiate_sub_tasks(self) -> Dict[Text, tf.keras.Model]: """Abstract function that sets up the computation for each sub-task. Returns: A map from task name (as string) to a tf.keras.Model object that represents the sub-task in the multi-task pool. """ raise NotImplementedError( "_instantiate_sub_task_models() is not implemented.") @property def sub_tasks(self): """Fetch a map of task name (string) to task model (tf.keras.Model).""" return self._sub_tasks def initialize(self): """Optional function that loads a pre-train checkpoint.""" return def build(self): """Builds the networks for tasks to make sure variables are created.""" # Try to build all sub tasks. for task_model in self._sub_tasks.values(): # Assumes all the tf.Module models are built because we don't have any # way to check them. if isinstance(task_model, tf.keras.Model) and not task_model.built: _ = task_model(task_model.inputs)
1,936
34.218182
76
py
models
models-master/official/modeling/multitask/task_sampler.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Utils to sample tasks for interleaved optimization.""" import abc from typing import Union, Dict, Text import tensorflow as tf from official.modeling.multitask import configs class TaskSampler(tf.Module, metaclass=abc.ABCMeta): """An abstract class defining task sampling API for interleaving trainer.""" def __init__(self, task_weights: Dict[Text, Union[float, int]]): self._task_weights = task_weights @property def task_weights(self): return self._task_weights @abc.abstractmethod def task_cumulative_distribution(self, global_step: tf.Tensor) -> tf.Tensor: """Compute cumulative distribution to sample tasks. It calculates the cumulative distribution of the multinomial task distribution with respect to which to be sampled against. Args: global_step: A tensor indicating current progess of training. Returns: A float tensor with shape (#(task), 1) that represents the cumulative sampling distribution. """ pass class UniformTaskSampler(TaskSampler): """Sample all tasks uniformly.""" def __init__(self, task_weights: Dict[Text, Union[float, int]]): super(UniformTaskSampler, self).__init__(task_weights=task_weights) self._uniform_cumulative = tf.math.cumsum( tf.constant( [1.0 / len(self._task_weights)] * len(self._task_weights), dtype=tf.float32)) def task_cumulative_distribution(self, global_step: tf.Tensor) -> tf.Tensor: del global_step return self._uniform_cumulative class ProportionalTaskSampler(TaskSampler): """Sample tasks proportional to task weights.""" def __init__(self, task_weights: Dict[Text, Union[float, int]], alpha: float = 1.0): super(ProportionalTaskSampler, self).__init__(task_weights=task_weights) self._alpha = tf.cast(alpha, dtype=tf.float32) task_weight_dict_ordered_list = tf.constant( [weight for _, weight in self._task_weights.items()], dtype=tf.float32) task_sizes = tf.math.pow(task_weight_dict_ordered_list, self._alpha) task_distribution = task_sizes / tf.reduce_sum(task_sizes) self._porportional_cumulative = tf.math.cumsum(task_distribution) def task_cumulative_distribution(self, global_step: tf.Tensor) -> tf.Tensor: del global_step return self._porportional_cumulative class AnnealingTaskSampler(TaskSampler): """Sample tasks according to task weights as well as training progress. See http://proceedings.mlr.press/v97/stickland19a/stickland19a.pdf """ def __init__(self, task_weights: Dict[Text, Union[float, int]], steps_per_epoch: int, total_steps: int): super(AnnealingTaskSampler, self).__init__(task_weights=task_weights) self._steps_per_epoch = tf.cast(steps_per_epoch, dtype=tf.float32) self._total_epochs = tf.cast( total_steps / self._steps_per_epoch, dtype=tf.float32) def task_cumulative_distribution(self, global_step: tf.Tensor) -> tf.Tensor: cur_epoch = tf.math.floor( tf.cast(global_step, dtype=tf.float32) / self._steps_per_epoch) alpha = 1.0 - 0.8 * (cur_epoch - 1) / (self._total_epochs - 1 + 1e-10) task_weight_dict_ordered_list = [ weight for _, weight in self._task_weights.items() ] task_sizes = tf.math.pow( tf.constant(task_weight_dict_ordered_list, dtype=tf.float32), tf.cast(alpha, dtype=tf.float32)) dynamic_task_distribution = task_sizes / tf.reduce_sum(task_sizes) return tf.math.cumsum(dynamic_task_distribution) def get_task_sampler(config: configs.TaskSamplingConfig, task_weights: Dict[Text, float]) -> TaskSampler: """Utils to create task sampler with configuration and task weights.""" oneof_config = config.get() if config.type == 'uniform': return UniformTaskSampler(task_weights=task_weights) elif config.type == 'proportional': return ProportionalTaskSampler( task_weights=task_weights, alpha=oneof_config.alpha) elif config.type == 'annealing': return AnnealingTaskSampler( task_weights=task_weights, steps_per_epoch=oneof_config.steps_per_epoch, total_steps=oneof_config.total_steps) else: raise RuntimeError('Task sampler type not supported')
4,887
36.891473
79
py
models
models-master/official/modeling/multitask/base_trainer.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Multitask base trainer implementation. The trainer derives from the Orbit `StandardTrainer` class. """ from typing import Union import gin import orbit import tensorflow as tf from official.modeling import optimization from official.modeling.multitask import base_model from official.modeling.multitask import multitask @gin.configurable class MultiTaskBaseTrainer(orbit.StandardTrainer): """Multitask base trainer.""" def __init__(self, multi_task: multitask.MultiTask, multi_task_model: Union[tf.keras.Model, base_model.MultiTaskBaseModel], optimizer: tf.optimizers.Optimizer, trainer_options=None, train_datasets=None): self._strategy = tf.distribute.get_strategy() self._multi_task = multi_task self._multi_task_model = multi_task_model self._optimizer = optimizer self._training_losses = None self._training_metrics = None self._global_step = orbit.utils.create_global_step() # Creates a shadow copy of the weights to store weights moving average. if isinstance(self._optimizer, optimization.ExponentialMovingAverage ) and not self._optimizer.has_shadow_copy: self._optimizer.shadow_copy(multi_task_model) if hasattr(self.multi_task_model, "checkpoint_items"): checkpoint_items = self.multi_task_model.checkpoint_items else: checkpoint_items = {} self._checkpoint = tf.train.Checkpoint( model=self.multi_task_model, optimizer=self.optimizer, global_step=self.global_step, **checkpoint_items) if train_datasets is None: train_datasets = {} for name, task in self.multi_task.tasks.items(): train_datasets[name] = orbit.utils.make_distributed_dataset( self.strategy, task.build_inputs, task.task_config.train_data) super().__init__( train_dataset=train_datasets, options=trainer_options or orbit.StandardTrainerOptions()) def train_loop_begin(self): """Clean up states that hold losses and metrics.""" for _, train_loss_metric in self.training_losses.items(): train_loss_metric.reset_states() for _, metrics in self.training_metrics.items(): for metric in metrics: metric.reset_states() def train_loop_end(self): """Record loss and metric values per task.""" result = {} for task_name, loss in self.training_losses.items(): result[task_name] = {loss.name: loss.result()} for task_name, task_metrics in self.training_metrics.items(): result[task_name].update( {metric.name: metric.result() for metric in task_metrics}) # Note that, the learning rate schedule is managed by the keras optimizer # internally, which respects the number of backward pass as `iterations`. # The learning rate schedule does not follow the trainer logical global # step of multiple tasks. if callable(self.optimizer.learning_rate): result["learning_rate"] = self.optimizer.learning_rate( self.optimizer.iterations) else: result["learning_rate"] = self.optimizer.learning_rate return result @property def checkpoint(self): """Accesses the training checkpoint.""" return self._checkpoint @property def training_losses(self): """Access training loss metric objects for all tasks.""" if self._training_losses is None: # Builds the per-task metrics and losses. # This the total summed training loss of tasks in the joint training. self._training_losses = dict( total_loss=tf.keras.metrics.Mean("training_loss", dtype=tf.float32)) for name in self.multi_task.tasks: self._training_losses[name] = tf.keras.metrics.Mean( "training_loss", dtype=tf.float32) return self._training_losses @property def training_metrics(self): """Access training metric metric objects for all tasks.""" if self._training_metrics is None: # Builds the per-task metrics and losses. self._training_metrics = {} for name, task in self.multi_task.tasks.items(): self._training_metrics[name] = task.build_metrics(training=True) return self._training_metrics @property def strategy(self): return self._strategy @property def multi_task(self): return self._multi_task @property def multi_task_model(self): return self._multi_task_model @property def optimizer(self): return self._optimizer @property def global_step(self): return self._global_step def train_step(self, iterator_map): """The default train step calling the multi-task train step. Args: iterator_map: a dictionary of task names and per-task dataset iterators. """ def step_fn(inputs): losses = self.multi_task.joint_train_step( inputs, multi_task_model=self.multi_task_model, optimizer=self.optimizer, task_metrics=self.training_metrics) for key, loss in losses.items(): self.training_losses[key].update_state(loss) self.strategy.run( step_fn, args=(tf.nest.map_structure(next, iterator_map),)) self.global_step.assign_add(1)
5,846
33.192982
78
py
models
models-master/official/modeling/multitask/train_lib_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for multitask.train_lib.""" from absl.testing import parameterized import tensorflow as tf from tensorflow.python.distribute import combinations from tensorflow.python.distribute import strategy_combinations from official.core import task_factory from official.modeling.hyperparams import params_dict from official.modeling.multitask import configs from official.modeling.multitask import multitask from official.modeling.multitask import test_utils from official.modeling.multitask import train_lib class TrainLibTest(tf.test.TestCase, parameterized.TestCase): def setUp(self): super().setUp() self._test_config = { 'trainer': { 'checkpoint_interval': 10, 'steps_per_loop': 10, 'summary_interval': 10, 'train_steps': 10, 'validation_steps': 5, 'validation_interval': 10, 'continuous_eval_timeout': 1, 'optimizer_config': { 'optimizer': { 'type': 'sgd', }, 'learning_rate': { 'type': 'constant' } } }, } @combinations.generate( combinations.combine( distribution_strategy=[ strategy_combinations.default_strategy, strategy_combinations.cloud_tpu_strategy, strategy_combinations.one_device_strategy_gpu, ], mode='eager', optimizer=['sgd_experimental', 'sgd'], flag_mode=['train', 'eval', 'train_and_eval'])) def test_end_to_end(self, distribution_strategy, optimizer, flag_mode): model_dir = self.get_temp_dir() experiment_config = configs.MultiTaskExperimentConfig( task=configs.MultiTaskConfig( task_routines=( configs.TaskRoutine( task_name='foo', task_config=test_utils.FooConfig()), configs.TaskRoutine( task_name='bar', task_config=test_utils.BarConfig())))) experiment_config = params_dict.override_params_dict( experiment_config, self._test_config, is_strict=False) experiment_config.trainer.optimizer_config.optimizer.type = optimizer with distribution_strategy.scope(): test_multitask = multitask.MultiTask.from_config(experiment_config.task) model = test_utils.MockMultiTaskModel() train_lib.run_experiment( distribution_strategy=distribution_strategy, task=test_multitask, model=model, mode=flag_mode, params=experiment_config, model_dir=model_dir) @combinations.generate( combinations.combine( distribution_strategy=[ strategy_combinations.default_strategy, strategy_combinations.cloud_tpu_strategy, strategy_combinations.one_device_strategy_gpu, ], mode='eager', flag_mode=['train', 'eval', 'train_and_eval'])) def test_end_to_end_multi_eval(self, distribution_strategy, flag_mode): model_dir = self.get_temp_dir() experiment_config = configs.MultiEvalExperimentConfig( task=test_utils.FooConfig(), eval_tasks=(configs.TaskRoutine( task_name='foo', task_config=test_utils.FooConfig(), eval_steps=2), configs.TaskRoutine( task_name='bar', task_config=test_utils.BarConfig(), eval_steps=3))) experiment_config = params_dict.override_params_dict( experiment_config, self._test_config, is_strict=False) with distribution_strategy.scope(): train_task = task_factory.get_task(experiment_config.task) eval_tasks = [ task_factory.get_task(config.task_config, name=config.task_name) for config in experiment_config.eval_tasks ] train_lib.run_experiment_with_multitask_eval( distribution_strategy=distribution_strategy, train_task=train_task, eval_tasks=eval_tasks, mode=flag_mode, params=experiment_config, model_dir=model_dir) if __name__ == '__main__': tf.test.main()
4,756
37.362903
79
py
models
models-master/official/modeling/multitask/interleaving_trainer.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Multitask trainer that interleaves each task's train step.""" from typing import Union import gin import orbit import tensorflow as tf from official.modeling.multitask import base_model from official.modeling.multitask import base_trainer from official.modeling.multitask import multitask from official.modeling.multitask import task_sampler as sampler @gin.configurable class MultiTaskInterleavingTrainer(base_trainer.MultiTaskBaseTrainer): """MultiTask trainer that interleaves task update.""" def __init__(self, multi_task: multitask.MultiTask, multi_task_model: Union[tf.keras.Model, base_model.MultiTaskBaseModel], optimizer: Union[tf.optimizers.Optimizer, tf.keras.optimizers.experimental.Optimizer, tf.keras.optimizers.legacy.Optimizer], task_sampler: sampler.TaskSampler, trainer_options=None): super().__init__( multi_task=multi_task, multi_task_model=multi_task_model, optimizer=optimizer, trainer_options=trainer_options) self._task_sampler = task_sampler # Build per task train step. def _get_task_step(task_name, task): def step_fn(inputs): if isinstance(self.multi_task_model, base_model.MultiTaskBaseModel): task_model = self.multi_task_model.sub_tasks[task_name] else: task_model = self.multi_task_model task_logs = task.train_step( inputs, model=task_model, optimizer=self.optimizer, metrics=self.training_metrics[task_name]) self.training_losses[task_name].update_state(task_logs[task.loss]) return step_fn self._task_train_step_map = { name: _get_task_step(name, task) for name, task in self.multi_task.tasks.items() } # TODO(haozhangthu): Add taskwise step counter to train_loop_end for logging # on TensorBoard. self._task_step_counters = { name: orbit.utils.create_global_step() for name in self.multi_task.tasks } # If the new Keras optimizer is used, we require all model variables are # created before the training and let the optimizer to create the slot # variable all together. if isinstance(optimizer, tf.keras.optimizers.experimental.Optimizer): multi_task_model.build() optimizer.build(multi_task_model.trainable_variables) def task_step_counter(self, name): return self._task_step_counters[name] def train_step(self, iterator_map): # Sample one task to train according to a multinomial distribution rn = tf.random.stateless_uniform(shape=[], seed=(0, self.global_step)) cumulative_sample_distribution = self._task_sampler.task_cumulative_distribution( self.global_step) # Prepend a [0.0] for indexing convenience. cumulative_sample_distribution = tf.concat( [tf.constant([0.0], dtype=tf.float32), cumulative_sample_distribution], axis=0) for idx, (name, _) in enumerate(self.multi_task.tasks.items()): begin = cumulative_sample_distribution[idx] end = cumulative_sample_distribution[idx + 1] if rn >= begin and rn < end: self._strategy.run( self._task_train_step_map[name], args=(next(iterator_map[name]),)) self.global_step.assign_add(1) self.task_step_counter(name).assign_add(1) def train_loop_end(self): """Record loss and metric values per task.""" result = super().train_loop_end() # Interleaving training does not have a good semantic for `total_loss`. In # fact, it is always zero. To avoid confusion, we filter the `total_loss` # from the result logs. if 'total_loss' in result: result.pop('total_loss') return result
4,448
38.723214
85
py
models
models-master/official/modeling/multitask/multitask.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Experimental MultiTask base class for multi-task training/evaluation.""" import abc from typing import Dict, List, Optional, Text, Union import tensorflow as tf from official.core import base_task from official.core import config_definitions from official.core import task_factory from official.modeling import optimization from official.modeling.multitask import base_model from official.modeling.multitask import configs from official.modeling.privacy import configs as dp_configs OptimizationConfig = optimization.OptimizationConfig RuntimeConfig = config_definitions.RuntimeConfig DifferentialPrivacyConfig = dp_configs.DifferentialPrivacyConfig class MultiTask(tf.Module, metaclass=abc.ABCMeta): """A multi-task class to manage multiple tasks.""" def __init__(self, tasks: Union[Dict[Text, base_task.Task], List[base_task.Task]], task_weights: Optional[Dict[str, Union[float, int]]] = None, task_eval_steps: Optional[Dict[str, int]] = None, name: Optional[str] = None): """MultiTask initialization. Args: tasks: a list or a flat dict of Task. task_weights: a dict of (task, task weight), task weight can be applied directly during loss summation in a joint backward step, or it can be used to sample task among interleaved backward step. task_eval_steps: a dict of (task, eval steps). name: the instance name of a MultiTask object. """ super().__init__(name=name) if isinstance(tasks, list): self._tasks = {} for task in tasks: if task.name in self._tasks: raise ValueError("Duplicated tasks found, task.name is %s" % task.name) self._tasks[task.name] = task elif isinstance(tasks, dict): self._tasks = tasks else: raise ValueError("The tasks argument has an invalid type: %s" % type(tasks)) self.task_eval_steps = task_eval_steps or {} self._task_weights = task_weights or {} self._task_weights = dict([ (name, self._task_weights.get(name, 1.0)) for name in self.tasks ]) @classmethod def from_config(cls, config: configs.MultiTaskConfig, logging_dir=None): tasks = {} task_eval_steps = {} task_weights = {} for task_routine in config.task_routines: task_name = task_routine.task_name or task_routine.task_config.name tasks[task_name] = task_factory.get_task( task_routine.task_config, logging_dir=logging_dir, name=task_name) task_eval_steps[task_name] = task_routine.eval_steps task_weights[task_name] = task_routine.task_weight return cls( tasks, task_eval_steps=task_eval_steps, task_weights=task_weights) @property def tasks(self): return self._tasks def task_weight(self, task_name): return self._task_weights[task_name] @property def task_weights(self): return self._task_weights @classmethod def create_optimizer(cls, optimizer_config: OptimizationConfig, runtime_config: Optional[RuntimeConfig] = None, dp_config: Optional[DifferentialPrivacyConfig] = None): return base_task.Task.create_optimizer( optimizer_config=optimizer_config, runtime_config=runtime_config, dp_config=dp_config) def joint_train_step(self, task_inputs, multi_task_model: base_model.MultiTaskBaseModel, optimizer: tf.keras.optimizers.Optimizer, task_metrics, **kwargs): """The joint train step. Args: task_inputs: a dictionary of task names and per-task features. multi_task_model: a MultiTaskBaseModel instance. optimizer: a tf.optimizers.Optimizer. task_metrics: a dictionary of task names and per-task metrics. **kwargs: other arguments to pass through. Returns: A dictionary of losses, inculding per-task losses and their weighted sum. """ losses = {} with tf.GradientTape() as tape: total_loss = 0.0 for name, model in multi_task_model.sub_tasks.items(): inputs = task_inputs[name] if isinstance(inputs, tuple) and len(inputs) == 2: features, labels = inputs elif isinstance(inputs, dict): features, labels = inputs, inputs else: raise ValueError("The iterator output is neither a tuple nor a " "dictionary. It is not implemented to support " "such outputs.") outputs = model(features, training=True) task_loss = self.tasks[name].build_losses(labels, outputs) task_weight = self.task_weight(name) total_loss += task_weight * task_loss losses[name] = task_loss self.tasks[name].process_metrics(task_metrics[name], labels, outputs, **kwargs) # Scales loss as the default gradients allreduce performs sum inside # the optimizer. scaled_loss = total_loss / tf.distribute.get_strategy( ).num_replicas_in_sync tvars = multi_task_model.trainable_variables grads = tape.gradient(scaled_loss, tvars) optimizer.apply_gradients(list(zip(grads, tvars))) losses["total_loss"] = total_loss return losses
5,938
38.593333
79
py
models
models-master/official/modeling/multitask/task_sampler_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for multitask.task_sampler.""" import tensorflow as tf from official.modeling.multitask import configs from official.modeling.multitask import task_sampler as sampler class TaskSamplerTest(tf.test.TestCase): def setUp(self): super(TaskSamplerTest, self).setUp() self._task_weights = {'A': 1.0, 'B': 2.0, 'C': 3.0} def test_uniform_sample_distribution(self): uniform_sampler = sampler.get_task_sampler( configs.TaskSamplingConfig(type='uniform'), self._task_weights) for step in range(5): cumulative_distribution = uniform_sampler.task_cumulative_distribution( tf.constant(step, dtype=tf.int64)) self.assertAllClose([0.333333, 0.666666, 1.0], cumulative_distribution.numpy()) def test_proportional_sample_distribution(self): prop_sampler = sampler.get_task_sampler( configs.TaskSamplingConfig( type='proportional', proportional=configs.ProportionalSampleConfig(alpha=2.0)), self._task_weights) # CucmulativeOf(Normalize([1.0^2, 2.0^2, 3.0^2])) for step in range(5): cumulative_distribution = prop_sampler.task_cumulative_distribution( tf.constant(step, dtype=tf.int64)) self.assertAllClose([0.07142857, 0.35714286, 1.0], cumulative_distribution.numpy()) def test_annealing_sample_distribution(self): num_epoch = 3 step_per_epoch = 6 annel_sampler = sampler.get_task_sampler( configs.TaskSamplingConfig( type='annealing', annealing=configs.AnnealingSampleConfig( steps_per_epoch=step_per_epoch, total_steps=step_per_epoch * num_epoch)), self._task_weights) global_step = tf.Variable( 0, dtype=tf.int64, name='global_step', trainable=False) expected_cumulative_epochs = [[0.12056106, 0.4387236, 1.0], [0.16666667, 0.5, 1.0], [0.22477472, 0.5654695, 1.0]] for epoch in range(num_epoch): for _ in range(step_per_epoch): cumulative_distribution = annel_sampler.task_cumulative_distribution( tf.constant(global_step, dtype=tf.int64)) global_step.assign_add(1) self.assertAllClose(expected_cumulative_epochs[epoch], cumulative_distribution.numpy()) if __name__ == '__main__': tf.test.main()
3,027
38.842105
77
py
models
models-master/official/modeling/multitask/evaluator.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Multitask Evaluator implementation. The evaluator implements the Orbit `AbstractEvaluator` interface. """ from typing import Dict, List, Optional, Union import gin import orbit import tensorflow as tf from official.core import base_task from official.core import train_utils from official.modeling.multitask import base_model @gin.configurable class MultiTaskEvaluator(orbit.AbstractEvaluator): """Implements the common trainer shared for TensorFlow models.""" def __init__( self, eval_tasks: List[base_task.Task], model: Union[tf.keras.Model, base_model.MultiTaskBaseModel], global_step: Optional[tf.Variable] = None, eval_steps: Optional[Dict[str, int]] = None, checkpoint_exporter: Optional[train_utils.BestCheckpointExporter] = None): """Initialize common trainer for TensorFlow models. Args: eval_tasks: A list of tasks to evaluate. model: tf.keras.Model instance. global_step: the global step variable. eval_steps: a dictionary of steps to run eval keyed by task names. checkpoint_exporter: an object that has the `maybe_export_checkpoint` interface. """ # Gets the current distribution strategy. If not inside any strategy scope, # it gets a single-replica no-op strategy. self._strategy = tf.distribute.get_strategy() self._tasks = eval_tasks self._model = model self._global_step = global_step or orbit.utils.create_global_step() self._checkpoint_exporter = checkpoint_exporter if hasattr(self.model, "checkpoint_items"): checkpoint_items = self.model.checkpoint_items else: checkpoint_items = {} self._checkpoint = tf.train.Checkpoint( model=self.model, global_step=self.global_step, **checkpoint_items) self._validation_losses = None self._validation_metrics = None # Builds per-task datasets. self.eval_datasets = {} self.eval_steps = eval_steps or {} for task in self.tasks: self.eval_datasets[task.name] = orbit.utils.make_distributed_dataset( self.strategy, task.build_inputs, task.task_config.validation_data) # Builds per-task validation loops. def get_function(task_name, task): task_metrics = self.validation_metrics[task_name] task_loss = self.validation_losses[task_name] if isinstance(self.model, base_model.MultiTaskBaseModel): model = self.model.sub_tasks[task_name] else: model = self.model def step_fn(inputs): logs = task.validation_step(inputs, model=model, metrics=task_metrics) task_loss.update_state(logs[task.loss]) return logs @tf.function def eval_step_fn(iterator): distributed_outputs = self.strategy.run(step_fn, args=(next(iterator),)) return tf.nest.map_structure(self.strategy.experimental_local_results, distributed_outputs) return orbit.utils.create_loop_fn(eval_step_fn) self.task_fns = { task.name: get_function(task.name, task) for task in self.tasks } @property def strategy(self): return self._strategy @property def tasks(self): return self._tasks @property def model(self): return self._model @property def global_step(self): return self._global_step @property def validation_losses(self): """Accesses the validation loss metric object.""" if self._validation_losses is None: # Builds the per-task metrics and losses. self._validation_losses = {} for task in self.tasks: self._validation_losses[task.name] = tf.keras.metrics.Mean( "validation_loss", dtype=tf.float32) return self._validation_losses @property def validation_metrics(self): """Accesses all validation metric metric objects.""" if self._validation_metrics is None: # Builds the per-task metrics and losses. self._validation_metrics = {} for task in self.tasks: self._validation_metrics[task.name] = task.build_metrics(training=False) return self._validation_metrics @property def checkpoint(self): """Accesses the training checkpoint.""" return self._checkpoint def evaluate(self, num_steps: tf.Tensor): """Performs evaluation for each `EvalTask`.""" for metric in self.validation_losses.values(): metric.reset_states() for metrics in self.validation_metrics.values(): for metric in metrics: metric.reset_states() results = {} eval_iters = tf.nest.map_structure(iter, self.eval_datasets) for task in self.tasks: outputs = None name = task.name eval_iter = eval_iters[name] task_eval_steps = self.eval_steps.get(name, None) or num_steps outputs = self.task_fns[name]( eval_iter, task_eval_steps, state=outputs, reduce_fn=task.aggregate_logs) task_metrics = self.validation_metrics[name] task_loss = self.validation_losses[name] logs = {} for metric in task_metrics + [task_loss]: logs[metric.name] = metric.result() if outputs: metrics = task.reduce_aggregated_logs( outputs, global_step=self.global_step) logs.update(metrics) results[name] = logs if self._checkpoint_exporter: self._checkpoint_exporter.maybe_export_checkpoint( self.checkpoint, results, self.global_step.numpy()) return results
6,068
32.530387
80
py
models
models-master/official/modeling/multitask/evaluator_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for multitask.evaluator.""" from absl.testing import parameterized import numpy as np import tensorflow as tf from tensorflow.python.distribute import combinations from tensorflow.python.distribute import strategy_combinations from official.core import base_task from official.core import config_definitions as cfg from official.modeling.multitask import evaluator def all_strategy_combinations(): return combinations.combine( distribution=[ strategy_combinations.default_strategy, strategy_combinations.cloud_tpu_strategy, strategy_combinations.one_device_strategy_gpu, ], mode="eager", ) class MockModel(tf.keras.Model): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.dense = tf.keras.layers.Dense(1) def call(self, inputs): print(inputs, type(inputs)) if "y" in inputs: self.add_loss(tf.zeros((1,), dtype=tf.float32)) else: self.add_loss(tf.ones((1,), dtype=tf.float32)) return self.dense(inputs["x"]) class MockTask(base_task.Task): """Mock task object for testing.""" def build_metrics(self, training: bool = True): del training return [tf.keras.metrics.Accuracy(name="acc")] def build_inputs(self, params): def generate_data(_): x = tf.zeros(shape=(2,), dtype=tf.float32) label = tf.zeros([1], dtype=tf.int32) if self.name == "bar": return dict(x=x, y=x), label else: return dict(x=x), label dataset = tf.data.Dataset.range(1) dataset = dataset.repeat() dataset = dataset.map( generate_data, num_parallel_calls=tf.data.experimental.AUTOTUNE) return dataset.prefetch(buffer_size=1).batch(2, drop_remainder=True) def validation_step(self, inputs, model: tf.keras.Model, metrics=None): logs = super().validation_step(inputs, model, metrics) logs["counter"] = tf.ones((1,), dtype=tf.float32) return logs def aggregate_logs(self, state, step_outputs): if state is None: state = {} for key, value in step_outputs.items(): if key not in state: state[key] = [] state[key].append( np.concatenate([np.expand_dims(v.numpy(), axis=0) for v in value])) return state def reduce_aggregated_logs(self, aggregated_logs, global_step=None): for k, v in aggregated_logs.items(): aggregated_logs[k] = np.sum(np.stack(v, axis=0)) return aggregated_logs class EvaluatorTest(tf.test.TestCase, parameterized.TestCase): @combinations.generate(all_strategy_combinations()) def test_multitask_evaluator(self, distribution): with distribution.scope(): tasks = [ MockTask(params=cfg.TaskConfig(), name="bar"), MockTask(params=cfg.TaskConfig(), name="foo") ] model = MockModel() test_evaluator = evaluator.MultiTaskEvaluator( eval_tasks=tasks, model=model) results = test_evaluator.evaluate(tf.convert_to_tensor(1, dtype=tf.int32)) self.assertContainsSubset(["validation_loss", "acc"], results["bar"].keys()) self.assertContainsSubset(["validation_loss", "acc"], results["foo"].keys()) self.assertEqual(results["bar"]["validation_loss"], 0.0) self.assertEqual(results["foo"]["validation_loss"], 1.0) @combinations.generate(all_strategy_combinations()) def test_multitask_evaluator_numpy_metrics(self, distribution): with distribution.scope(): tasks = [ MockTask(params=cfg.TaskConfig(), name="bar"), MockTask(params=cfg.TaskConfig(), name="foo") ] model = MockModel() test_evaluator = evaluator.MultiTaskEvaluator( eval_tasks=tasks, model=model) results = test_evaluator.evaluate(tf.convert_to_tensor(5, dtype=tf.int32)) self.assertEqual(results["bar"]["counter"], 5. * distribution.num_replicas_in_sync) self.assertEqual(results["foo"]["counter"], 5. * distribution.num_replicas_in_sync) if __name__ == "__main__": tf.test.main()
4,633
33.58209
80
py
models
models-master/official/modeling/multitask/train_lib.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Multitask training driver library.""" # pytype: disable=attribute-error import os from typing import Any, List, Mapping, Optional, Tuple, Union from absl import logging import orbit import tensorflow as tf from official.core import base_task from official.core import base_trainer as core_lib from official.core import train_utils from official.modeling.multitask import base_model from official.modeling.multitask import base_trainer from official.modeling.multitask import configs from official.modeling.multitask import evaluator as evaluator_lib from official.modeling.multitask import interleaving_trainer from official.modeling.multitask import multitask from official.modeling.multitask import task_sampler TRAINERS = { 'interleaving': interleaving_trainer.MultiTaskInterleavingTrainer, 'joint': base_trainer.MultiTaskBaseTrainer } def run_experiment( *, distribution_strategy: tf.distribute.Strategy, task: multitask.MultiTask, model: base_model.MultiTaskBaseModel, mode: str, params: configs.MultiTaskExperimentConfig, model_dir: str, run_post_eval: bool = False, trainer: base_trainer.MultiTaskBaseTrainer = None, best_ckpt_exporter_creator: Optional[Any] = train_utils .maybe_create_best_ckpt_exporter ) -> Union[base_model.MultiTaskBaseModel, Tuple[base_model.MultiTaskBaseModel, Mapping[Any, Any]]]: """Runs train/eval configured by the experiment params. Args: distribution_strategy: A distribution distribution_strategy. task: A MultiTaskTask instance. model: A MultiTaskBaseModel instance. mode: A 'str', specifying the mode. Can be 'train', 'eval', 'train_and_eval' or 'continuous_eval'. params: ExperimentConfig instance. model_dir: A 'str', a path to store model checkpoints and summaries. run_post_eval: Whether to run post eval once after training, metrics logs are returned. trainer: (optional) A multi-task trainer to use. If none is provided, a default one will be created based on `params`. best_ckpt_exporter_creator: A functor for creating best checkpoint exporter. Returns: model: `base_model.MultiTaskBaseModel` instance. """ is_training = 'train' in mode is_eval = 'eval' in mode with distribution_strategy.scope(): optimizer = train_utils.create_optimizer(task, params) kwargs = dict(multi_task=task, multi_task_model=model, optimizer=optimizer) if params.trainer.trainer_type == 'interleaving': sampler = task_sampler.get_task_sampler(params.trainer.task_sampler, task.task_weights) kwargs.update(dict(task_sampler=sampler)) if trainer is None: trainer = TRAINERS[params.trainer.trainer_type]( **kwargs) if is_training else None if is_eval: eval_steps = task.task_eval_steps evaluator = evaluator_lib.MultiTaskEvaluator( eval_tasks=task.tasks.values(), model=model, eval_steps=eval_steps, global_step=trainer.global_step if is_training else None, checkpoint_exporter=best_ckpt_exporter_creator(params, model_dir)) else: evaluator = None if trainer: checkpoint = trainer.checkpoint global_step = trainer.global_step else: checkpoint = evaluator.checkpoint global_step = evaluator.global_step checkpoint_manager = tf.train.CheckpointManager( checkpoint, directory=model_dir, max_to_keep=params.trainer.max_to_keep, step_counter=global_step, checkpoint_interval=params.trainer.checkpoint_interval, init_fn=model.initialize) controller = orbit.Controller( strategy=distribution_strategy, trainer=trainer, evaluator=evaluator, global_step=global_step, steps_per_loop=params.trainer.steps_per_loop, checkpoint_manager=checkpoint_manager, summary_dir=os.path.join(model_dir, 'train'), eval_summary_dir=os.path.join(model_dir, 'validation'), summary_interval=params.trainer.summary_interval) logging.info('Starts to execute mode: %s', mode) with distribution_strategy.scope(): if mode == 'train': controller.train(steps=params.trainer.train_steps) elif mode == 'train_and_eval': controller.train_and_evaluate( train_steps=params.trainer.train_steps, eval_steps=params.trainer.validation_steps, eval_interval=params.trainer.validation_interval) elif mode == 'eval': controller.evaluate(steps=params.trainer.validation_steps) elif mode == 'continuous_eval': def timeout_fn(): if evaluator.global_step.numpy() >= params.trainer.train_steps: return True return False controller.evaluate_continuously( steps=params.trainer.validation_steps, timeout=params.trainer.continuous_eval_timeout, timeout_fn=timeout_fn) else: raise NotImplementedError('The mode is not implemented: %s' % mode) if run_post_eval: return model, evaluator.evaluate( tf.convert_to_tensor(params.trainer.validation_steps)) # pytype: disable=bad-return-type # typed-keras else: return model def run_experiment_with_multitask_eval( *, distribution_strategy: tf.distribute.Strategy, train_task: base_task.Task, eval_tasks: List[base_task.Task], mode: str, params: configs.MultiEvalExperimentConfig, model_dir: str, run_post_eval: bool = False, save_summary: bool = True, trainer: Optional[core_lib.Trainer] = None, best_ckpt_exporter_creator: Optional[Any] = train_utils .maybe_create_best_ckpt_exporter, ) -> Tuple[Any, Any]: """Runs train/eval configured by the experiment params. Args: distribution_strategy: A distribution distribution_strategy. train_task: A base_task.Task instance. eval_tasks: A list of evaluation tasks. mode: A 'str', specifying the mode. Can be 'train', 'eval', 'train_and_eval' or 'continuous_eval'. params: MultiEvalExperimentConfig instance. model_dir: A 'str', a path to store model checkpoints and summaries. run_post_eval: Whether to run post eval once after training, metrics logs are returned. save_summary: Whether to save train and validation summary. trainer: the core_lib.Trainer instance. It should be created within the strategy.scope(). If not provided, an instance will be created by default if `mode` contains 'train'. best_ckpt_exporter_creator: A functor for creating best checkpoint exporter. Returns: model: `tf.keras.Model` instance. """ is_training = 'train' in mode is_eval = 'eval' in mode with distribution_strategy.scope(): if is_training: trainer = trainer or core_lib.Trainer( config=params, task=train_task, model=train_task.build_model(), optimizer=train_utils.create_optimizer(train_task, params), train=True, evaluate=False) else: trainer = None # Build the model or fetch the pre-cached one (which could be either # multi-task model or single task model). model = None if trainer is None: if isinstance(train_task, multitask.MultiTask): model = train_task.build_multitask_model() else: model = train_task.build_model() else: if isinstance(trainer, base_trainer.MultiTaskBaseTrainer): model = trainer.multi_task_model else: model = trainer.model if is_eval: eval_steps = dict([(task_routine.task_config.name, task_routine.eval_steps) for task_routine in params.eval_tasks]) evaluator = evaluator_lib.MultiTaskEvaluator( eval_tasks=eval_tasks, model=model, global_step=trainer.global_step if is_training else None, eval_steps=eval_steps, checkpoint_exporter=best_ckpt_exporter_creator(params, model_dir)) else: evaluator = None if trainer: checkpoint = trainer.checkpoint global_step = trainer.global_step else: checkpoint = evaluator.checkpoint global_step = evaluator.global_step checkpoint_manager = tf.train.CheckpointManager( checkpoint, directory=model_dir, max_to_keep=params.trainer.max_to_keep, step_counter=global_step, checkpoint_interval=params.trainer.checkpoint_interval, init_fn=trainer.initialize if trainer else None) controller = orbit.Controller( strategy=distribution_strategy, trainer=trainer, evaluator=evaluator, global_step=global_step, steps_per_loop=params.trainer.steps_per_loop, checkpoint_manager=checkpoint_manager, summary_dir=os.path.join(model_dir, 'train') if save_summary else None, eval_summary_dir=os.path.join(model_dir, 'validation') if (save_summary) else None, summary_interval=params.trainer.summary_interval if (save_summary) else None) logging.info('Starts to execute mode: %s', mode) with distribution_strategy.scope(): if mode == 'train': controller.train(steps=params.trainer.train_steps) elif mode == 'train_and_eval': controller.train_and_evaluate( train_steps=params.trainer.train_steps, eval_steps=params.trainer.validation_steps, eval_interval=params.trainer.validation_interval) elif mode == 'eval': controller.evaluate(steps=params.trainer.validation_steps) elif mode == 'continuous_eval': def timeout_fn(): if evaluator.global_step.numpy() >= params.trainer.train_steps: return True return False controller.evaluate_continuously( steps=params.trainer.validation_steps, timeout=params.trainer.continuous_eval_timeout, timeout_fn=timeout_fn) else: raise NotImplementedError('The mode is not implemented: %s' % mode) if run_post_eval: return model, evaluator.evaluate( tf.convert_to_tensor(params.trainer.validation_steps)) # pytype: disable=bad-return-type # typed-keras else: return model, {} # pytype: disable=bad-return-type # typed-keras
10,832
36.484429
114
py
models
models-master/official/modeling/multitask/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
609
39.666667
74
py
models
models-master/official/modeling/multitask/configs.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Configuration definitions for multi-task training.""" import dataclasses from typing import Optional, Tuple from official.core import config_definitions as cfg from official.modeling import hyperparams from official.modeling.privacy import configs as dp_configs @dataclasses.dataclass class TaskRoutine(hyperparams.Config): # TODO(hongkuny): deprecate the task_name once we migrated client code. task_name: str = "" task_config: cfg.TaskConfig = None eval_steps: Optional[int] = None task_weight: Optional[float] = 1.0 @dataclasses.dataclass class MultiTaskConfig(hyperparams.Config): init_checkpoint: str = "" model: hyperparams.Config = None task_routines: Tuple[TaskRoutine, ...] = () # Configs for differential privacy # These configs are only effective if you use create_optimizer in # tensorflow_models/official/core/base_task.py # DEPRECATED b/264611883 differential_privacy_config: Optional[ dp_configs.DifferentialPrivacyConfig] = None @dataclasses.dataclass class ProportionalSampleConfig(hyperparams.Config): alpha: float = 1.0 @dataclasses.dataclass class AnnealingSampleConfig(hyperparams.Config): steps_per_epoch: int = 5 total_steps: int = 20 @dataclasses.dataclass class TaskSamplingConfig(hyperparams.OneOfConfig): type: str = "" uniform: hyperparams.Config = dataclasses.field( default_factory=hyperparams.Config ) proportional: ProportionalSampleConfig = dataclasses.field( default_factory=ProportionalSampleConfig ) annealing: AnnealingSampleConfig = dataclasses.field( default_factory=AnnealingSampleConfig ) @dataclasses.dataclass class MultiTaskTrainerConfig(cfg.TrainerConfig): trainer_type: str = "interleaving" task_sampler: TaskSamplingConfig = dataclasses.field( default_factory=lambda: TaskSamplingConfig(type="proportional") ) @dataclasses.dataclass class MultiTaskExperimentConfig(hyperparams.Config): """An experiment config for multi-task training and multi-task evaluation.""" task: MultiTaskConfig = dataclasses.field(default_factory=MultiTaskConfig) trainer: MultiTaskTrainerConfig = dataclasses.field( default_factory=MultiTaskTrainerConfig ) runtime: cfg.RuntimeConfig = dataclasses.field( default_factory=cfg.RuntimeConfig ) @dataclasses.dataclass class MultiEvalExperimentConfig(cfg.ExperimentConfig): """An experiment config for single-task training and multi-task evaluation. Attributes: eval_tasks: individual evaluation tasks. """ eval_tasks: Tuple[TaskRoutine, ...] = ()
3,164
30.969697
79
py
models
models-master/official/modeling/multitask/base_trainer_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for multitask.base_trainer.""" from absl.testing import parameterized import tensorflow as tf from tensorflow.python.distribute import combinations from tensorflow.python.distribute import strategy_combinations from official.modeling.multitask import base_trainer from official.modeling.multitask import configs from official.modeling.multitask import multitask from official.modeling.multitask import test_utils def all_strategy_combinations(): return combinations.combine( distribution=[ strategy_combinations.default_strategy, strategy_combinations.cloud_tpu_strategy, strategy_combinations.one_device_strategy_gpu, ], mode="eager", ) class BaseTrainerTest(tf.test.TestCase, parameterized.TestCase): @combinations.generate(all_strategy_combinations()) def test_multitask_joint_trainer(self, distribution): with distribution.scope(): tasks = [ test_utils.MockFooTask(params=test_utils.FooConfig(), name="foo"), test_utils.MockBarTask(params=test_utils.BarConfig(), name="bar") ] task_weights = {"foo": 1.0, "bar": 1.0} test_multitask = multitask.MultiTask( tasks=tasks, task_weights=task_weights) test_optimizer = tf.keras.optimizers.SGD(0.1) model = test_utils.MockMultiTaskModel() test_trainer = base_trainer.MultiTaskBaseTrainer( multi_task=test_multitask, multi_task_model=model, optimizer=test_optimizer) results = test_trainer.train(tf.convert_to_tensor(5, dtype=tf.int32)) self.assertContainsSubset(["training_loss", "bar_acc"], results["bar"].keys()) self.assertContainsSubset(["training_loss", "foo_acc"], results["foo"].keys()) def test_trainer_with_configs(self): config = configs.MultiTaskConfig( task_routines=(configs.TaskRoutine( task_name="foo", task_config=test_utils.FooConfig(), task_weight=0.5), configs.TaskRoutine( task_name="bar", task_config=test_utils.BarConfig(), task_weight=0.5))) test_multitask = multitask.MultiTask.from_config(config) test_optimizer = tf.keras.optimizers.SGD(0.1) model = test_utils.MockMultiTaskModel() test_trainer = base_trainer.MultiTaskBaseTrainer( multi_task=test_multitask, multi_task_model=model, optimizer=test_optimizer) results = test_trainer.train(tf.convert_to_tensor(5, dtype=tf.int32)) self.assertContainsSubset(["training_loss", "bar_acc"], results["bar"].keys()) self.assertContainsSubset(["training_loss", "foo_acc"], results["foo"].keys()) self.assertEqual(test_multitask.task_weight("foo"), 0.5) self.assertEqual(test_trainer.global_step.numpy(), 5) self.assertIn("learning_rate", results) if __name__ == "__main__": tf.test.main()
3,653
39.153846
76
py
models
models-master/official/modeling/multitask/interleaving_trainer_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for multitask.interleaving_trainer.""" from absl.testing import parameterized import tensorflow as tf from tensorflow.python.distribute import combinations from tensorflow.python.distribute import strategy_combinations from official.modeling.multitask import configs from official.modeling.multitask import interleaving_trainer from official.modeling.multitask import multitask from official.modeling.multitask import task_sampler from official.modeling.multitask import test_utils def all_strategy_combinations(): return combinations.combine( distribution=[ strategy_combinations.default_strategy, strategy_combinations.cloud_tpu_strategy, strategy_combinations.one_device_strategy_gpu, ], mode="eager", ) class InterleavingTrainerTest(tf.test.TestCase, parameterized.TestCase): @combinations.generate(all_strategy_combinations()) def test_multitask_interleaving_trainer(self, distribution): with distribution.scope(): tasks = [ test_utils.MockFooTask(params=test_utils.FooConfig(), name="foo"), test_utils.MockBarTask(params=test_utils.BarConfig(), name="bar") ] test_multitask = multitask.MultiTask(tasks=tasks) test_optimizer = tf.keras.optimizers.SGD(0.1) model = test_utils.MockMultiTaskModel() sampler = task_sampler.UniformTaskSampler( task_weights=test_multitask.task_weights) test_trainer = interleaving_trainer.MultiTaskInterleavingTrainer( multi_task=test_multitask, multi_task_model=model, optimizer=test_optimizer, task_sampler=sampler) results = test_trainer.train(tf.convert_to_tensor(5, dtype=tf.int32)) self.assertContainsSubset(["training_loss", "bar_acc"], results["bar"].keys()) self.assertContainsSubset(["training_loss", "foo_acc"], results["foo"].keys()) self.assertNotIn("total_loss", results) @combinations.generate(all_strategy_combinations()) def test_trainer_with_configs(self, distribution): config = configs.MultiTaskConfig( task_routines=(configs.TaskRoutine( task_name="foo", task_config=test_utils.FooConfig(), task_weight=3.0), configs.TaskRoutine( task_name="bar", task_config=test_utils.BarConfig(), task_weight=1.0))) with distribution.scope(): test_multitask = multitask.MultiTask.from_config(config) test_optimizer = tf.keras.optimizers.SGD(0.1) model = test_utils.MockMultiTaskModel() num_step = 1000 sampler = task_sampler.AnnealingTaskSampler( task_weights=test_multitask.task_weights, steps_per_epoch=num_step/5, total_steps=num_step) test_trainer = interleaving_trainer.MultiTaskInterleavingTrainer( multi_task=test_multitask, multi_task_model=model, optimizer=test_optimizer, task_sampler=sampler) results = test_trainer.train(tf.convert_to_tensor(num_step, dtype=tf.int32)) self.assertContainsSubset(["training_loss", "bar_acc"], results["bar"].keys()) self.assertContainsSubset(["training_loss", "foo_acc"], results["foo"].keys()) self.assertEqual(test_trainer.global_step.numpy(), num_step) bar_sampled_step = test_trainer.task_step_counter("bar").numpy() foo_sampled_step = test_trainer.task_step_counter("foo").numpy() self.assertEqual(bar_sampled_step + foo_sampled_step, num_step) if __name__ == "__main__": tf.test.main()
4,295
40.708738
80
py
models
models-master/official/modeling/multitask/test_utils.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Testing utils for mock models and tasks.""" from typing import Dict, Text import tensorflow as tf from official.core import base_task from official.core import config_definitions as cfg from official.core import task_factory from official.modeling.multitask import base_model class MockFooModel(tf.keras.Model): """A mock model can consume 'foo' and 'bar' inputs.""" def __init__(self, shared_layer, *args, **kwargs): super().__init__(*args, **kwargs) self._share_layer = shared_layer self._foo_specific_layer = tf.keras.layers.Dense(1) self.inputs = {"foo": tf.keras.Input(shape=(2,), dtype=tf.float32), "bar": tf.keras.Input(shape=(2,), dtype=tf.float32)} def call(self, inputs): # pytype: disable=signature-mismatch # overriding-parameter-count-checks self.add_loss(tf.zeros((1,), dtype=tf.float32)) if "foo" in inputs: input_tensor = inputs["foo"] else: input_tensor = inputs["bar"] return self._foo_specific_layer(self._share_layer(input_tensor)) class MockBarModel(tf.keras.Model): """A mock model can only consume 'bar' inputs.""" def __init__(self, shared_layer, *args, **kwargs): super().__init__(*args, **kwargs) self._share_layer = shared_layer self._bar_specific_layer = tf.keras.layers.Dense(1) self.inputs = {"bar": tf.keras.Input(shape=(2,), dtype=tf.float32)} def call(self, inputs): # pytype: disable=signature-mismatch # overriding-parameter-count-checks self.add_loss(tf.zeros((2,), dtype=tf.float32)) return self._bar_specific_layer(self._share_layer(inputs["bar"])) class MockMultiTaskModel(base_model.MultiTaskBaseModel): def __init__(self, *args, **kwargs): self._shared_dense = tf.keras.layers.Dense(1) super().__init__(*args, **kwargs) def _instantiate_sub_tasks(self) -> Dict[Text, tf.keras.Model]: return { "foo": MockFooModel(self._shared_dense), "bar": MockBarModel(self._shared_dense) } def mock_data(feature_name): """Mock dataset function.""" def _generate_data(_): x = tf.zeros(shape=(2,), dtype=tf.float32) label = tf.zeros([1], dtype=tf.int32) return {feature_name: x}, label dataset = tf.data.Dataset.range(1) dataset = dataset.repeat() dataset = dataset.map( _generate_data, num_parallel_calls=tf.data.experimental.AUTOTUNE) return dataset.prefetch(buffer_size=1).batch(2, drop_remainder=True) class FooConfig(cfg.TaskConfig): pass class BarConfig(cfg.TaskConfig): pass @task_factory.register_task_cls(FooConfig) class MockFooTask(base_task.Task): """Mock foo task object for testing.""" def build_metrics(self, training: bool = True): del training return [tf.keras.metrics.Accuracy(name="foo_acc")] def build_inputs(self, params): return mock_data("foo") def build_model(self) -> tf.keras.Model: return MockFooModel(shared_layer=tf.keras.layers.Dense(1)) def build_losses(self, labels, model_outputs, aux_losses=None) -> tf.Tensor: loss = tf.keras.losses.mean_squared_error(labels, model_outputs) if aux_losses: loss += tf.add_n(aux_losses) return tf.reduce_mean(loss) @task_factory.register_task_cls(BarConfig) class MockBarTask(base_task.Task): """Mock bar task object for testing.""" def build_metrics(self, training: bool = True): del training return [tf.keras.metrics.Accuracy(name="bar_acc")] def build_inputs(self, params): return mock_data("bar") def build_losses(self, labels, model_outputs, aux_losses=None) -> tf.Tensor: loss = tf.keras.losses.mean_squared_error(labels, model_outputs) if aux_losses: loss += tf.add_n(aux_losses) return tf.reduce_mean(loss)
4,305
32.123077
100
py
models
models-master/official/modeling/activations/gelu_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for the Gaussian error linear unit.""" import tensorflow as tf from official.modeling import activations class GeluTest(tf.test.TestCase): def test_gelu(self): expected_data = [[0.14967535, 0., -0.10032465], [-0.15880796, -0.04540223, 2.9963627]] gelu_data = activations.gelu([[.25, 0, -.25], [-1, -2, 3]]) self.assertAllClose(expected_data, gelu_data) if __name__ == '__main__': tf.test.main()
1,057
31.060606
74
py
models
models-master/official/modeling/activations/mish_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for the customized Mish activation.""" import tensorflow as tf from official.modeling import activations class MishTest(tf.test.TestCase): def test_mish(self): x = tf.constant([1.0, 0.0]) self.assertAllClose([0.86509839, 0.0], activations.mish(x)) if __name__ == '__main__': tf.test.main()
927
28.935484
74
py
models
models-master/official/modeling/activations/gelu.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Gaussian error linear unit.""" import tensorflow as tf @tf.keras.utils.register_keras_serializable(package='Text') def gelu(x): """Gaussian Error Linear Unit. This is a smoother version of the RELU. Original paper: https://arxiv.org/abs/1606.08415 Args: x: float Tensor to perform activation. Returns: `x` with the GELU activation applied. """ return tf.keras.activations.gelu(x, approximate=True)
1,037
30.454545
74
py
models
models-master/official/modeling/activations/relu_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for the customized Relu activation.""" import tensorflow as tf from official.modeling import activations class CustomizedReluTest(tf.test.TestCase): def test_relu6(self): features = [[.25, 0, -.25], [-1, -2, 3]] customized_relu6_data = activations.relu6(features) relu6_data = tf.nn.relu6(features) self.assertAllClose(customized_relu6_data, relu6_data) if __name__ == '__main__': tf.test.main()
1,041
30.575758
74
py
models
models-master/official/modeling/activations/sigmoid_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for the customized Sigmoid activation.""" import numpy as np import tensorflow as tf from official.modeling import activations class CustomizedSigmoidTest(tf.test.TestCase): def _hard_sigmoid_nn(self, x): x = np.float32(x) return tf.nn.relu6(x + 3.) * 0.16667 def test_hard_sigmoid(self): features = [[.25, 0, -.25], [-1, -2, 3]] customized_hard_sigmoid_data = activations.hard_sigmoid(features) sigmoid_data = self._hard_sigmoid_nn(features) self.assertAllClose(customized_hard_sigmoid_data, sigmoid_data) if __name__ == '__main__': tf.test.main()
1,205
30.736842
74
py
models
models-master/official/modeling/activations/swish.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Customized Swish activation.""" import tensorflow as tf @tf.keras.utils.register_keras_serializable(package='Text') def simple_swish(features): """Computes the Swish activation function. The tf.nn.swish operation uses a custom gradient to reduce memory usage. Since saving custom gradients in SavedModel is currently not supported, and one would not be able to use an exported TF-Hub module for fine-tuning, we provide this wrapper that can allow to select whether to use the native TensorFlow swish operation, or whether to use a customized operation that has uses default TensorFlow gradient computation. Args: features: A `Tensor` representing preactivation values. Returns: The activation value. """ features = tf.convert_to_tensor(features) return features * tf.nn.sigmoid(features) @tf.keras.utils.register_keras_serializable(package='Text') def hard_swish(features): """Computes a hard version of the swish function. This operation can be used to reduce computational cost and improve quantization for edge devices. Args: features: A `Tensor` representing preactivation values. Returns: The activation value. """ features = tf.convert_to_tensor(features) fdtype = features.dtype return features * tf.nn.relu6(features + tf.cast(3., fdtype)) * (1. / 6.) @tf.keras.utils.register_keras_serializable(package='Text') def identity(features): """Computes the identity function. Useful for helping in quantization. Args: features: A `Tensor` representing preactivation values. Returns: The activation value. """ features = tf.convert_to_tensor(features) return tf.identity(features)
2,291
30.39726
77
py
models
models-master/official/modeling/activations/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Activations package definition.""" from official.modeling.activations.gelu import gelu from official.modeling.activations.mish import mish from official.modeling.activations.relu import relu6 from official.modeling.activations.sigmoid import hard_sigmoid from official.modeling.activations.swish import hard_swish from official.modeling.activations.swish import identity from official.modeling.activations.swish import simple_swish
1,044
44.434783
74
py
models
models-master/official/modeling/activations/relu.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Customized Relu activation.""" import tensorflow as tf @tf.keras.utils.register_keras_serializable(package='Text') def relu6(features): """Computes the Relu6 activation function. Args: features: A `Tensor` representing preactivation values. Returns: The activation value. """ features = tf.convert_to_tensor(features) return tf.nn.relu6(features)
984
29.78125
74
py
models
models-master/official/modeling/activations/mish.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Self Regularized Non-Monotonic Activation Function.""" import tensorflow as tf @tf.keras.utils.register_keras_serializable(package='Text') def mish(x) -> tf.Tensor: """Mish activation function. Mish: A Self Regularized Non-Monotonic Activation Function https://arxiv.org/pdf/1908.08681.pdf Mish(x) = x * tanh(ln(1+e^x)) Args: x: A `Tensor` representing preactivation values. Returns: The activation value. """ x = tf.convert_to_tensor(x) return x * tf.tanh(tf.nn.softplus(x))
1,130
29.567568
74
py
models
models-master/official/modeling/activations/sigmoid.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Customized Sigmoid activation.""" import tensorflow as tf @tf.keras.utils.register_keras_serializable(package='Text') def hard_sigmoid(features): """Computes the hard sigmoid activation function. Args: features: A `Tensor` representing preactivation values. Returns: The activation value. """ features = tf.convert_to_tensor(features) return tf.nn.relu6(features + tf.cast(3., features.dtype)) * 0.16667
1,041
31.5625
74
py
models
models-master/official/modeling/activations/swish_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for the customized Swish activation.""" import numpy as np import tensorflow as tf from official.modeling import activations class CustomizedSwishTest(tf.test.TestCase): def _hard_swish_np(self, x): x = np.float32(x) return x * np.clip(x + 3, 0, 6) / 6 def test_simple_swish(self): features = [[.25, 0, -.25], [-1, -2, 3]] customized_swish_data = activations.simple_swish(features) swish_data = tf.nn.swish(features) self.assertAllClose(customized_swish_data, swish_data) def test_hard_swish(self): features = [[.25, 0, -.25], [-1, -2, 3]] customized_swish_data = activations.hard_swish(features) swish_data = self._hard_swish_np(features) self.assertAllClose(customized_swish_data, swish_data) if __name__ == '__main__': tf.test.main()
1,411
31.837209
74
py
models
models-master/official/legacy/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
609
39.666667
74
py
models
models-master/official/legacy/xlnet/optimization.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Functions and classes related to optimization (weight updates).""" from absl import logging import tensorflow as tf from official.nlp import optimization class WarmUp(tf.keras.optimizers.schedules.LearningRateSchedule): """Applys a warmup schedule on a given learning rate decay schedule.""" def __init__(self, initial_learning_rate, decay_schedule_fn, warmup_steps, power=1.0, name=None): super(WarmUp, self).__init__() self.initial_learning_rate = initial_learning_rate self.warmup_steps = warmup_steps self.power = power self.decay_schedule_fn = decay_schedule_fn self.name = name def __call__(self, step): with tf.name_scope(self.name or "WarmUp") as name: # Implements polynomial warmup. i.e., if global_step < warmup_steps, the # learning rate will be `global_step/num_warmup_steps * init_lr`. global_step_float = tf.cast(step, tf.float32) warmup_steps_float = tf.cast(self.warmup_steps, tf.float32) warmup_percent_done = global_step_float / warmup_steps_float warmup_learning_rate = ( self.initial_learning_rate * tf.math.pow(warmup_percent_done, self.power)) return tf.cond( global_step_float < warmup_steps_float, lambda: warmup_learning_rate, lambda: self.decay_schedule_fn(step - self.warmup_steps), name=name) def get_config(self): return { "initial_learning_rate": self.initial_learning_rate, "decay_schedule_fn": self.decay_schedule_fn, "warmup_steps": self.warmup_steps, "power": self.power, "name": self.name } def create_optimizer(init_lr, num_train_steps, num_warmup_steps, min_lr_ratio=0.0, adam_epsilon=1e-8, weight_decay_rate=0.0): """Creates an optimizer with learning rate schedule.""" # Implements linear decay of the learning rate. learning_rate_fn = tf.keras.optimizers.schedules.PolynomialDecay( initial_learning_rate=init_lr, decay_steps=num_train_steps - num_warmup_steps, end_learning_rate=init_lr * min_lr_ratio) if num_warmup_steps: learning_rate_fn = WarmUp( initial_learning_rate=init_lr, decay_schedule_fn=learning_rate_fn, warmup_steps=num_warmup_steps) if weight_decay_rate > 0.0: logging.info( "Using AdamWeightDecay with adam_epsilon=%.9f weight_decay_rate=%.3f", adam_epsilon, weight_decay_rate) optimizer = optimization.AdamWeightDecay( learning_rate=learning_rate_fn, weight_decay_rate=weight_decay_rate, beta_1=0.9, beta_2=0.999, epsilon=adam_epsilon, exclude_from_weight_decay=["LayerNorm", "layer_norm", "bias"], include_in_weight_decay=["r_s_bias", "r_r_bias", "r_w_bias"]) else: logging.info("Using Adam with adam_epsilon=%.9f", (adam_epsilon)) optimizer = tf.keras.optimizers.legacy.Adam( learning_rate=learning_rate_fn, epsilon=adam_epsilon) return optimizer, learning_rate_fn
3,769
37.080808
78
py
models
models-master/official/legacy/xlnet/run_squad.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """XLNet SQUAD finetuning runner in tf2.0.""" import functools import json import os import pickle # Import libraries from absl import app from absl import flags from absl import logging import tensorflow as tf # pylint: disable=unused-import import sentencepiece as spm from official.common import distribute_utils from official.legacy.xlnet import common_flags from official.legacy.xlnet import data_utils from official.legacy.xlnet import optimization from official.legacy.xlnet import squad_utils from official.legacy.xlnet import training_utils from official.legacy.xlnet import xlnet_config from official.legacy.xlnet import xlnet_modeling as modeling flags.DEFINE_string( "test_feature_path", default=None, help="Path to feature of test set.") flags.DEFINE_integer("query_len", default=64, help="Max query length.") flags.DEFINE_integer("start_n_top", default=5, help="Beam size for span start.") flags.DEFINE_integer("end_n_top", default=5, help="Beam size for span end.") flags.DEFINE_string( "predict_dir", default=None, help="Path to write predictions.") flags.DEFINE_string( "predict_file", default=None, help="Path to json file of test set.") flags.DEFINE_integer( "n_best_size", default=5, help="n best size for predictions.") flags.DEFINE_integer("max_answer_length", default=64, help="Max answer length.") # Data preprocessing config flags.DEFINE_string( "spiece_model_file", default=None, help="Sentence Piece model path.") flags.DEFINE_integer("max_seq_length", default=512, help="Max sequence length.") flags.DEFINE_integer("max_query_length", default=64, help="Max query length.") flags.DEFINE_integer("doc_stride", default=128, help="Doc stride.") FLAGS = flags.FLAGS class InputFeatures(object): """A single set of features of data.""" def __init__(self, unique_id, example_index, doc_span_index, tok_start_to_orig_index, tok_end_to_orig_index, token_is_max_context, input_ids, input_mask, p_mask, segment_ids, paragraph_len, cls_index, start_position=None, end_position=None, is_impossible=None): self.unique_id = unique_id self.example_index = example_index self.doc_span_index = doc_span_index self.tok_start_to_orig_index = tok_start_to_orig_index self.tok_end_to_orig_index = tok_end_to_orig_index self.token_is_max_context = token_is_max_context self.input_ids = input_ids self.input_mask = input_mask self.p_mask = p_mask self.segment_ids = segment_ids self.paragraph_len = paragraph_len self.cls_index = cls_index self.start_position = start_position self.end_position = end_position self.is_impossible = is_impossible # pylint: disable=unused-argument def run_evaluation(strategy, test_input_fn, eval_examples, eval_features, original_data, eval_steps, input_meta_data, model, current_step, eval_summary_writer): """Run evaluation for SQUAD task. Args: strategy: distribution strategy. test_input_fn: input function for evaluation data. eval_examples: tf.Examples of the evaluation set. eval_features: Feature objects of the evaluation set. original_data: The original json data for the evaluation set. eval_steps: total number of evaluation steps. input_meta_data: input meta data. model: keras model object. current_step: current training step. eval_summary_writer: summary writer used to record evaluation metrics. Returns: A float metric, F1 score. """ def _test_step_fn(inputs): """Replicated validation step.""" inputs["mems"] = None res = model(inputs, training=False) return res, inputs["unique_ids"] @tf.function def _run_evaluation(test_iterator): """Runs validation steps.""" res, unique_ids = strategy.run( _test_step_fn, args=(next(test_iterator),)) return res, unique_ids test_iterator = data_utils.get_input_iterator(test_input_fn, strategy) cur_results = [] for _ in range(eval_steps): results, unique_ids = _run_evaluation(test_iterator) unique_ids = strategy.experimental_local_results(unique_ids) for result_key in results: results[result_key] = ( strategy.experimental_local_results(results[result_key])) for core_i in range(strategy.num_replicas_in_sync): bsz = int(input_meta_data["test_batch_size"] / strategy.num_replicas_in_sync) for j in range(bsz): result = {} for result_key in results: result[result_key] = results[result_key][core_i].numpy()[j] result["unique_ids"] = unique_ids[core_i].numpy()[j] # We appended a fake example into dev set to make data size can be # divided by test_batch_size. Ignores this fake example during # evaluation. if result["unique_ids"] == 1000012047: continue unique_id = int(result["unique_ids"]) start_top_log_probs = ([ float(x) for x in result["start_top_log_probs"].flat ]) start_top_index = [int(x) for x in result["start_top_index"].flat] end_top_log_probs = ([ float(x) for x in result["end_top_log_probs"].flat ]) end_top_index = [int(x) for x in result["end_top_index"].flat] cls_logits = float(result["cls_logits"].flat[0]) cur_results.append( squad_utils.RawResult( unique_id=unique_id, start_top_log_probs=start_top_log_probs, start_top_index=start_top_index, end_top_log_probs=end_top_log_probs, end_top_index=end_top_index, cls_logits=cls_logits)) if len(cur_results) % 1000 == 0: logging.info("Processing example: %d", len(cur_results)) output_prediction_file = os.path.join(input_meta_data["predict_dir"], "predictions.json") output_nbest_file = os.path.join(input_meta_data["predict_dir"], "nbest_predictions.json") output_null_log_odds_file = os.path.join(input_meta_data["predict_dir"], "null_odds.json") results = squad_utils.write_predictions( eval_examples, eval_features, cur_results, input_meta_data["n_best_size"], input_meta_data["max_answer_length"], output_prediction_file, output_nbest_file, output_null_log_odds_file, original_data, input_meta_data["start_n_top"], input_meta_data["end_n_top"]) # Log current results. log_str = "Result | " for key, val in results.items(): log_str += "{} {} | ".format(key, val) logging.info(log_str) with eval_summary_writer.as_default(): tf.summary.scalar("best_f1", results["best_f1"], step=current_step) tf.summary.scalar("best_exact", results["best_exact"], step=current_step) eval_summary_writer.flush() return results["best_f1"] def get_qaxlnet_model(model_config, run_config, start_n_top, end_n_top): model = modeling.QAXLNetModel( model_config, run_config, start_n_top=start_n_top, end_n_top=end_n_top, name="model") return model def main(unused_argv): del unused_argv strategy = distribute_utils.get_distribution_strategy( distribution_strategy=FLAGS.strategy_type, tpu_address=FLAGS.tpu) if strategy: logging.info("***** Number of cores used : %d", strategy.num_replicas_in_sync) train_input_fn = functools.partial(data_utils.get_squad_input_data, FLAGS.train_batch_size, FLAGS.seq_len, FLAGS.query_len, strategy, True, FLAGS.train_tfrecord_path) test_input_fn = functools.partial(data_utils.get_squad_input_data, FLAGS.test_batch_size, FLAGS.seq_len, FLAGS.query_len, strategy, False, FLAGS.test_tfrecord_path) total_training_steps = FLAGS.train_steps steps_per_loop = FLAGS.iterations eval_steps = int(FLAGS.test_data_size / FLAGS.test_batch_size) optimizer, learning_rate_fn = optimization.create_optimizer( FLAGS.learning_rate, total_training_steps, FLAGS.warmup_steps, adam_epsilon=FLAGS.adam_epsilon) model_config = xlnet_config.XLNetConfig(FLAGS) run_config = xlnet_config.create_run_config(True, False, FLAGS) input_meta_data = {} input_meta_data["start_n_top"] = FLAGS.start_n_top input_meta_data["end_n_top"] = FLAGS.end_n_top input_meta_data["lr_layer_decay_rate"] = FLAGS.lr_layer_decay_rate input_meta_data["predict_dir"] = FLAGS.predict_dir input_meta_data["n_best_size"] = FLAGS.n_best_size input_meta_data["max_answer_length"] = FLAGS.max_answer_length input_meta_data["test_batch_size"] = FLAGS.test_batch_size input_meta_data["batch_size_per_core"] = int(FLAGS.train_batch_size / strategy.num_replicas_in_sync) input_meta_data["mem_len"] = FLAGS.mem_len model_fn = functools.partial(get_qaxlnet_model, model_config, run_config, FLAGS.start_n_top, FLAGS.end_n_top) eval_examples = squad_utils.read_squad_examples( FLAGS.predict_file, is_training=False) if FLAGS.test_feature_path: logging.info("start reading pickle file...") with tf.io.gfile.GFile(FLAGS.test_feature_path, "rb") as f: eval_features = pickle.load(f) logging.info("finishing reading pickle file...") else: sp_model = spm.SentencePieceProcessor() sp_model.LoadFromSerializedProto( tf.io.gfile.GFile(FLAGS.spiece_model_file, "rb").read()) spm_basename = os.path.basename(FLAGS.spiece_model_file) eval_features = squad_utils.create_eval_data( spm_basename, sp_model, eval_examples, FLAGS.max_seq_length, FLAGS.max_query_length, FLAGS.doc_stride, FLAGS.uncased) with tf.io.gfile.GFile(FLAGS.predict_file) as f: original_data = json.load(f)["data"] eval_fn = functools.partial(run_evaluation, strategy, test_input_fn, eval_examples, eval_features, original_data, eval_steps, input_meta_data) training_utils.train( strategy=strategy, model_fn=model_fn, input_meta_data=input_meta_data, eval_fn=eval_fn, metric_fn=None, train_input_fn=train_input_fn, init_checkpoint=FLAGS.init_checkpoint, init_from_transformerxl=FLAGS.init_from_transformerxl, total_training_steps=total_training_steps, steps_per_loop=steps_per_loop, optimizer=optimizer, learning_rate_fn=learning_rate_fn, model_dir=FLAGS.model_dir, save_steps=FLAGS.save_steps) if __name__ == "__main__": app.run(main)
11,597
38.182432
80
py
models
models-master/official/legacy/xlnet/run_classifier.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """XLNet classification finetuning runner in tf2.0.""" import functools # Import libraries from absl import app from absl import flags from absl import logging import numpy as np import tensorflow as tf # pylint: disable=unused-import from official.common import distribute_utils from official.legacy.xlnet import common_flags from official.legacy.xlnet import data_utils from official.legacy.xlnet import optimization from official.legacy.xlnet import training_utils from official.legacy.xlnet import xlnet_config from official.legacy.xlnet import xlnet_modeling as modeling flags.DEFINE_integer("n_class", default=2, help="Number of classes.") flags.DEFINE_string( "summary_type", default="last", help="Method used to summarize a sequence into a vector.") FLAGS = flags.FLAGS def get_classificationxlnet_model(model_config, run_config, n_class, summary_type="last"): model = modeling.ClassificationXLNetModel( model_config, run_config, n_class, summary_type, name="model") return model def run_evaluation(strategy, test_input_fn, eval_steps, model, step, eval_summary_writer=None): """Run evaluation for classification task. Args: strategy: distribution strategy. test_input_fn: input function for evaluation data. eval_steps: total number of evaluation steps. model: keras model object. step: current train step. eval_summary_writer: summary writer used to record evaluation metrics. As there are fake data samples in validation set, we use mask to get rid of them when calculating the accuracy. For the reason that there will be dynamic-shape tensor, we first collect logits, labels and masks from TPU and calculate the accuracy via numpy locally. Returns: A float metric, accuracy. """ def _test_step_fn(inputs): """Replicated validation step.""" inputs["mems"] = None _, logits = model(inputs, training=False) return logits, inputs["label_ids"], inputs["is_real_example"] @tf.function def _run_evaluation(test_iterator): """Runs validation steps.""" logits, labels, masks = strategy.run( _test_step_fn, args=(next(test_iterator),)) return logits, labels, masks test_iterator = data_utils.get_input_iterator(test_input_fn, strategy) correct = 0 total = 0 for _ in range(eval_steps): logits, labels, masks = _run_evaluation(test_iterator) logits = strategy.experimental_local_results(logits) labels = strategy.experimental_local_results(labels) masks = strategy.experimental_local_results(masks) merged_logits = [] merged_labels = [] merged_masks = [] for i in range(strategy.num_replicas_in_sync): merged_logits.append(logits[i].numpy()) merged_labels.append(labels[i].numpy()) merged_masks.append(masks[i].numpy()) merged_logits = np.vstack(np.array(merged_logits)) merged_labels = np.hstack(np.array(merged_labels)) merged_masks = np.hstack(np.array(merged_masks)) real_index = np.where(np.equal(merged_masks, 1)) correct += np.sum( np.equal( np.argmax(merged_logits[real_index], axis=-1), merged_labels[real_index])) total += np.shape(real_index)[-1] accuracy = float(correct) / float(total) logging.info("Train step: %d / acc = %d/%d = %f", step, correct, total, accuracy) if eval_summary_writer: with eval_summary_writer.as_default(): tf.summary.scalar("eval_acc", float(correct) / float(total), step=step) eval_summary_writer.flush() return accuracy def get_metric_fn(): train_acc_metric = tf.keras.metrics.SparseCategoricalAccuracy( "acc", dtype=tf.float32) return train_acc_metric def main(unused_argv): del unused_argv strategy = distribute_utils.get_distribution_strategy( distribution_strategy=FLAGS.strategy_type, tpu_address=FLAGS.tpu) if strategy: logging.info("***** Number of cores used : %d", strategy.num_replicas_in_sync) train_input_fn = functools.partial(data_utils.get_classification_input_data, FLAGS.train_batch_size, FLAGS.seq_len, strategy, True, FLAGS.train_tfrecord_path) test_input_fn = functools.partial(data_utils.get_classification_input_data, FLAGS.test_batch_size, FLAGS.seq_len, strategy, False, FLAGS.test_tfrecord_path) total_training_steps = FLAGS.train_steps steps_per_loop = FLAGS.iterations eval_steps = int(FLAGS.test_data_size / FLAGS.test_batch_size) eval_fn = functools.partial(run_evaluation, strategy, test_input_fn, eval_steps) optimizer, learning_rate_fn = optimization.create_optimizer( FLAGS.learning_rate, total_training_steps, FLAGS.warmup_steps, adam_epsilon=FLAGS.adam_epsilon) model_config = xlnet_config.XLNetConfig(FLAGS) run_config = xlnet_config.create_run_config(True, False, FLAGS) model_fn = functools.partial(get_classificationxlnet_model, model_config, run_config, FLAGS.n_class, FLAGS.summary_type) input_meta_data = {} input_meta_data["d_model"] = FLAGS.d_model input_meta_data["mem_len"] = FLAGS.mem_len input_meta_data["batch_size_per_core"] = int(FLAGS.train_batch_size / strategy.num_replicas_in_sync) input_meta_data["n_layer"] = FLAGS.n_layer input_meta_data["lr_layer_decay_rate"] = FLAGS.lr_layer_decay_rate input_meta_data["n_class"] = FLAGS.n_class training_utils.train( strategy=strategy, model_fn=model_fn, input_meta_data=input_meta_data, eval_fn=eval_fn, metric_fn=get_metric_fn, train_input_fn=train_input_fn, init_checkpoint=FLAGS.init_checkpoint, init_from_transformerxl=FLAGS.init_from_transformerxl, total_training_steps=total_training_steps, steps_per_loop=steps_per_loop, optimizer=optimizer, learning_rate_fn=learning_rate_fn, model_dir=FLAGS.model_dir, save_steps=FLAGS.save_steps) if __name__ == "__main__": app.run(main)
6,976
36.111702
79
py
models
models-master/official/legacy/xlnet/squad_utils.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # coding=utf-8 """Utilities used in SQUAD task.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import gc import json import math import os import pickle import re import string from absl import logging import numpy as np import six import tensorflow as tf from official.legacy.xlnet import data_utils from official.legacy.xlnet import preprocess_utils SPIECE_UNDERLINE = u"▁" class InputFeatures(object): """A single set of features of data.""" def __init__(self, unique_id, example_index, doc_span_index, tok_start_to_orig_index, tok_end_to_orig_index, token_is_max_context, input_ids, input_mask, p_mask, segment_ids, paragraph_len, cls_index, start_position=None, end_position=None, is_impossible=None): self.unique_id = unique_id self.example_index = example_index self.doc_span_index = doc_span_index self.tok_start_to_orig_index = tok_start_to_orig_index self.tok_end_to_orig_index = tok_end_to_orig_index self.token_is_max_context = token_is_max_context self.input_ids = input_ids self.input_mask = input_mask self.p_mask = p_mask self.segment_ids = segment_ids self.paragraph_len = paragraph_len self.cls_index = cls_index self.start_position = start_position self.end_position = end_position self.is_impossible = is_impossible def make_qid_to_has_ans(dataset): qid_to_has_ans = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: qid_to_has_ans[qa["id"]] = bool(qa["answers"]) return qid_to_has_ans def get_raw_scores(dataset, preds): """Gets exact scores and f1 scores.""" exact_scores = {} f1_scores = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: qid = qa["id"] gold_answers = [ a["text"] for a in qa["answers"] if normalize_answer(a["text"]) ] if not gold_answers: # For unanswerable questions, only correct answer is empty string gold_answers = [""] if qid not in preds: print("Missing prediction for %s" % qid) continue a_pred = preds[qid] # Take max over all gold answers exact_scores[qid] = max(compute_exact(a, a_pred) for a in gold_answers) f1_scores[qid] = max(compute_f1(a, a_pred) for a in gold_answers) return exact_scores, f1_scores def normalize_answer(s): """Lower text and remove punctuation, articles and extra whitespace.""" def remove_articles(text): regex = re.compile(r"\b(a|an|the)\b", re.UNICODE) return re.sub(regex, " ", text) def white_space_fix(text): return " ".join(text.split()) def remove_punc(text): exclude = set(string.punctuation) return "".join(ch for ch in text if ch not in exclude) def lower(text): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(s)))) def compute_exact(a_gold, a_pred): return int(normalize_answer(a_gold) == normalize_answer(a_pred)) def get_tokens(s): if not s: return [] return normalize_answer(s).split() def compute_f1(a_gold, a_pred): """Computes f1 score.""" gold_toks = get_tokens(a_gold) pred_toks = get_tokens(a_pred) common = collections.Counter(gold_toks) & collections.Counter(pred_toks) num_same = sum(common.values()) # pylint: disable=g-explicit-length-test if len(gold_toks) == 0 or len(pred_toks) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks) if num_same == 0: return 0 precision = 1.0 * num_same / len(pred_toks) recall = 1.0 * num_same / len(gold_toks) f1 = (2 * precision * recall) / (precision + recall) return f1 def find_best_thresh(preds, scores, na_probs, qid_to_has_ans): """Finds best threshold.""" num_no_ans = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]) cur_score = num_no_ans best_score = cur_score best_thresh = 0.0 qid_list = sorted(na_probs, key=lambda k: na_probs[k]) for qid in qid_list: if qid not in scores: continue if qid_to_has_ans[qid]: diff = scores[qid] else: if preds[qid]: diff = -1 else: diff = 0 cur_score += diff if cur_score > best_score: best_score = cur_score best_thresh = na_probs[qid] has_ans_score, has_ans_cnt = 0, 0 for qid in qid_list: if not qid_to_has_ans[qid]: continue has_ans_cnt += 1 if qid not in scores: continue has_ans_score += scores[qid] return 100.0 * best_score / len( scores), best_thresh, 1.0 * has_ans_score / has_ans_cnt def find_all_best_thresh(main_eval, preds, exact_raw, f1_raw, na_probs, qid_to_has_ans): """Finds all best threshold.""" best_exact, exact_thresh, has_ans_exact = find_best_thresh( preds, exact_raw, na_probs, qid_to_has_ans) best_f1, f1_thresh, has_ans_f1 = find_best_thresh(preds, f1_raw, na_probs, qid_to_has_ans) main_eval["best_exact"] = best_exact main_eval["best_exact_thresh"] = exact_thresh main_eval["best_f1"] = best_f1 main_eval["best_f1_thresh"] = f1_thresh main_eval["has_ans_exact"] = has_ans_exact main_eval["has_ans_f1"] = has_ans_f1 _PrelimPrediction = collections.namedtuple( # pylint: disable=invalid-name "PrelimPrediction", [ "feature_index", "start_index", "end_index", "start_log_prob", "end_log_prob" ]) _NbestPrediction = collections.namedtuple( # pylint: disable=invalid-name "NbestPrediction", ["text", "start_log_prob", "end_log_prob"]) RawResult = collections.namedtuple("RawResult", [ "unique_id", "start_top_log_probs", "start_top_index", "end_top_log_probs", "end_top_index", "cls_logits" ]) def _compute_softmax(scores): """Computes softmax probability over raw logits.""" if not scores: return [] max_score = None for score in scores: if max_score is None or score > max_score: max_score = score exp_scores = [] total_sum = 0.0 for score in scores: x = math.exp(score - max_score) exp_scores.append(x) total_sum += x probs = [] for score in exp_scores: probs.append(score / total_sum) return probs class SquadExample(object): """A single training/test example for simple sequence classification. For examples without an answer, the start and end position are -1. """ def __init__(self, qas_id, question_text, paragraph_text, orig_answer_text=None, start_position=None, is_impossible=False): self.qas_id = qas_id self.question_text = question_text self.paragraph_text = paragraph_text self.orig_answer_text = orig_answer_text self.start_position = start_position self.is_impossible = is_impossible def __str__(self): return self.__repr__() def __repr__(self): s = "" s += "qas_id: %s" % (preprocess_utils.printable_text(self.qas_id)) s += ", question_text: %s" % ( preprocess_utils.printable_text(self.question_text)) s += ", paragraph_text: [%s]" % (" ".join(self.paragraph_text)) if self.start_position: s += ", start_position: %d" % (self.start_position) if self.start_position: s += ", is_impossible: %r" % (self.is_impossible) return s def write_predictions(all_examples, all_features, all_results, n_best_size, max_answer_length, output_prediction_file, output_nbest_file, output_null_log_odds_file, orig_data, start_n_top, end_n_top): """Writes final predictions to the json file and log-odds of null if needed.""" logging.info("Writing predictions to: %s", (output_prediction_file)) example_index_to_features = collections.defaultdict(list) for feature in all_features: example_index_to_features[feature.example_index].append(feature) unique_id_to_result = {} for result in all_results: unique_id_to_result[result.unique_id] = result all_predictions = collections.OrderedDict() all_nbest_json = collections.OrderedDict() scores_diff_json = collections.OrderedDict() for (example_index, example) in enumerate(all_examples): features = example_index_to_features[example_index] prelim_predictions = [] # keep track of the minimum score of null start+end of position 0 score_null = 1000000 # large and positive for (feature_index, feature) in enumerate(features): result = unique_id_to_result[feature.unique_id] cur_null_score = result.cls_logits # if we could have irrelevant answers, get the min score of irrelevant score_null = min(score_null, cur_null_score) for i in range(start_n_top): for j in range(end_n_top): start_log_prob = result.start_top_log_probs[i] start_index = result.start_top_index[i] j_index = i * end_n_top + j end_log_prob = result.end_top_log_probs[j_index] end_index = result.end_top_index[j_index] # We could hypothetically create invalid predictions, e.g., predict # that the start of the span is in the question. We throw out all # invalid predictions. if start_index >= feature.paragraph_len - 1: continue if end_index >= feature.paragraph_len - 1: continue if not feature.token_is_max_context.get(start_index, False): continue if end_index < start_index: continue length = end_index - start_index + 1 if length > max_answer_length: continue prelim_predictions.append( _PrelimPrediction( feature_index=feature_index, start_index=start_index, end_index=end_index, start_log_prob=start_log_prob, end_log_prob=end_log_prob)) prelim_predictions = sorted( prelim_predictions, key=lambda x: (x.start_log_prob + x.end_log_prob), reverse=True) seen_predictions = {} nbest = [] for pred in prelim_predictions: if len(nbest) >= n_best_size: break feature = features[pred.feature_index] tok_start_to_orig_index = feature.tok_start_to_orig_index tok_end_to_orig_index = feature.tok_end_to_orig_index start_orig_pos = tok_start_to_orig_index[pred.start_index] end_orig_pos = tok_end_to_orig_index[pred.end_index] paragraph_text = example.paragraph_text final_text = paragraph_text[start_orig_pos:end_orig_pos + 1].strip() if final_text in seen_predictions: continue seen_predictions[final_text] = True nbest.append( _NbestPrediction( text=final_text, start_log_prob=pred.start_log_prob, end_log_prob=pred.end_log_prob)) # In very rare edge cases we could have no valid predictions. So we # just create a nonce prediction in this case to avoid failure. if not nbest: nbest.append( _NbestPrediction(text="", start_log_prob=-1e6, end_log_prob=-1e6)) total_scores = [] best_non_null_entry = None for entry in nbest: total_scores.append(entry.start_log_prob + entry.end_log_prob) if not best_non_null_entry: best_non_null_entry = entry probs = _compute_softmax(total_scores) nbest_json = [] for (i, entry) in enumerate(nbest): output = collections.OrderedDict() output["text"] = entry.text output["probability"] = probs[i] output["start_log_prob"] = entry.start_log_prob output["end_log_prob"] = entry.end_log_prob nbest_json.append(output) assert len(nbest_json) >= 1 assert best_non_null_entry is not None score_diff = score_null scores_diff_json[example.qas_id] = score_diff all_predictions[example.qas_id] = best_non_null_entry.text all_nbest_json[example.qas_id] = nbest_json with tf.io.gfile.GFile(output_prediction_file, "w") as writer: writer.write(json.dumps(all_predictions, indent=4) + "\n") with tf.io.gfile.GFile(output_nbest_file, "w") as writer: writer.write(json.dumps(all_nbest_json, indent=4) + "\n") with tf.io.gfile.GFile(output_null_log_odds_file, "w") as writer: writer.write(json.dumps(scores_diff_json, indent=4) + "\n") qid_to_has_ans = make_qid_to_has_ans(orig_data) exact_raw, f1_raw = get_raw_scores(orig_data, all_predictions) out_eval = {} find_all_best_thresh(out_eval, all_predictions, exact_raw, f1_raw, scores_diff_json, qid_to_has_ans) return out_eval def read_squad_examples(input_file, is_training): """Reads a SQuAD json file into a list of SquadExample.""" with tf.io.gfile.GFile(input_file, "r") as reader: input_data = json.load(reader)["data"] examples = [] for entry in input_data: for paragraph in entry["paragraphs"]: paragraph_text = paragraph["context"] for qa in paragraph["qas"]: qas_id = qa["id"] question_text = qa["question"] start_position = None orig_answer_text = None is_impossible = False if is_training: is_impossible = qa["is_impossible"] if (len(qa["answers"]) != 1) and (not is_impossible): raise ValueError( "For training, each question should have exactly 1 answer.") if not is_impossible: answer = qa["answers"][0] orig_answer_text = answer["text"] start_position = answer["answer_start"] else: start_position = -1 orig_answer_text = "" example = SquadExample( qas_id=qas_id, question_text=question_text, paragraph_text=paragraph_text, orig_answer_text=orig_answer_text, start_position=start_position, is_impossible=is_impossible) examples.append(example) return examples # pylint: disable=invalid-name def _convert_index(index, pos, M=None, is_start=True): """Converts index.""" if index[pos] is not None: return index[pos] N = len(index) rear = pos while rear < N - 1 and index[rear] is None: rear += 1 front = pos while front > 0 and index[front] is None: front -= 1 assert index[front] is not None or index[rear] is not None if index[front] is None: if index[rear] >= 1: if is_start: return 0 else: return index[rear] - 1 return index[rear] if index[rear] is None: if M is not None and index[front] < M - 1: if is_start: return index[front] + 1 else: return M - 1 return index[front] if is_start: if index[rear] > index[front] + 1: return index[front] + 1 else: return index[rear] else: if index[rear] > index[front] + 1: return index[rear] - 1 else: return index[front] def convert_examples_to_features(examples, sp_model, max_seq_length, doc_stride, max_query_length, is_training, output_fn, uncased): """Loads a data file into a list of `InputBatch`s.""" cnt_pos, cnt_neg = 0, 0 unique_id = 1000000000 max_N, max_M = 1024, 1024 f = np.zeros((max_N, max_M), dtype=np.float32) for (example_index, example) in enumerate(examples): # pylint: disable=logging-format-interpolation if example_index % 100 == 0: logging.info("Converting {}/{} pos {} neg {}".format( example_index, len(examples), cnt_pos, cnt_neg)) query_tokens = preprocess_utils.encode_ids( sp_model, preprocess_utils.preprocess_text(example.question_text, lower=uncased)) if len(query_tokens) > max_query_length: query_tokens = query_tokens[0:max_query_length] paragraph_text = example.paragraph_text para_tokens = preprocess_utils.encode_pieces( sp_model, preprocess_utils.preprocess_text(example.paragraph_text, lower=uncased)) chartok_to_tok_index = [] tok_start_to_chartok_index = [] tok_end_to_chartok_index = [] char_cnt = 0 for i, token in enumerate(para_tokens): chartok_to_tok_index.extend([i] * len(token)) tok_start_to_chartok_index.append(char_cnt) char_cnt += len(token) tok_end_to_chartok_index.append(char_cnt - 1) tok_cat_text = "".join(para_tokens).replace(SPIECE_UNDERLINE, " ") N, M = len(paragraph_text), len(tok_cat_text) if N > max_N or M > max_M: max_N = max(N, max_N) max_M = max(M, max_M) f = np.zeros((max_N, max_M), dtype=np.float32) gc.collect() g = {} # pylint: disable=cell-var-from-loop def _lcs_match(max_dist): """LCS match.""" f.fill(0) g.clear() ### longest common sub sequence # f[i, j] = max(f[i - 1, j], f[i, j - 1], f[i - 1, j - 1] + match(i, j)) for i in range(N): # note(zhiliny): # unlike standard LCS, this is specifically optimized for the setting # because the mismatch between sentence pieces and original text will # be small for j in range(i - max_dist, i + max_dist): if j >= M or j < 0: continue if i > 0: g[(i, j)] = 0 f[i, j] = f[i - 1, j] if j > 0 and f[i, j - 1] > f[i, j]: g[(i, j)] = 1 f[i, j] = f[i, j - 1] f_prev = f[i - 1, j - 1] if i > 0 and j > 0 else 0 if (preprocess_utils.preprocess_text( paragraph_text[i], lower=uncased, remove_space=False) == tok_cat_text[j] and f_prev + 1 > f[i, j]): g[(i, j)] = 2 f[i, j] = f_prev + 1 max_dist = abs(N - M) + 5 for _ in range(2): _lcs_match(max_dist) if f[N - 1, M - 1] > 0.8 * N: break max_dist *= 2 orig_to_chartok_index = [None] * N chartok_to_orig_index = [None] * M i, j = N - 1, M - 1 while i >= 0 and j >= 0: if (i, j) not in g: break if g[(i, j)] == 2: orig_to_chartok_index[i] = j chartok_to_orig_index[j] = i i, j = i - 1, j - 1 elif g[(i, j)] == 1: j = j - 1 else: i = i - 1 if all( v is None for v in orig_to_chartok_index) or f[N - 1, M - 1] < 0.8 * N: print("MISMATCH DETECTED!") continue tok_start_to_orig_index = [] tok_end_to_orig_index = [] for i in range(len(para_tokens)): start_chartok_pos = tok_start_to_chartok_index[i] end_chartok_pos = tok_end_to_chartok_index[i] start_orig_pos = _convert_index( chartok_to_orig_index, start_chartok_pos, N, is_start=True) end_orig_pos = _convert_index( chartok_to_orig_index, end_chartok_pos, N, is_start=False) tok_start_to_orig_index.append(start_orig_pos) tok_end_to_orig_index.append(end_orig_pos) if not is_training: tok_start_position = tok_end_position = None if is_training and example.is_impossible: tok_start_position = -1 tok_end_position = -1 if is_training and not example.is_impossible: start_position = example.start_position end_position = start_position + len(example.orig_answer_text) - 1 start_chartok_pos = _convert_index( orig_to_chartok_index, start_position, is_start=True) tok_start_position = chartok_to_tok_index[start_chartok_pos] end_chartok_pos = _convert_index( orig_to_chartok_index, end_position, is_start=False) tok_end_position = chartok_to_tok_index[end_chartok_pos] assert tok_start_position <= tok_end_position def _piece_to_id(x): if six.PY2 and isinstance(x, unicode): # pylint: disable=undefined-variable x = x.encode("utf-8") return sp_model.PieceToId(x) all_doc_tokens = list(map(_piece_to_id, para_tokens)) # The -3 accounts for [CLS], [SEP] and [SEP] max_tokens_for_doc = max_seq_length - len(query_tokens) - 3 # We can have documents that are longer than the maximum sequence length. # To deal with this we do a sliding window approach, where we take chunks # of the up to our max length with a stride of `doc_stride`. _DocSpan = collections.namedtuple( # pylint: disable=invalid-name "DocSpan", ["start", "length"]) doc_spans = [] start_offset = 0 while start_offset < len(all_doc_tokens): length = len(all_doc_tokens) - start_offset if length > max_tokens_for_doc: length = max_tokens_for_doc doc_spans.append(_DocSpan(start=start_offset, length=length)) if start_offset + length == len(all_doc_tokens): break start_offset += min(length, doc_stride) for (doc_span_index, doc_span) in enumerate(doc_spans): tokens = [] token_is_max_context = {} segment_ids = [] p_mask = [] cur_tok_start_to_orig_index = [] cur_tok_end_to_orig_index = [] for i in range(doc_span.length): split_token_index = doc_span.start + i cur_tok_start_to_orig_index.append( tok_start_to_orig_index[split_token_index]) cur_tok_end_to_orig_index.append( tok_end_to_orig_index[split_token_index]) is_max_context = _check_is_max_context(doc_spans, doc_span_index, split_token_index) token_is_max_context[len(tokens)] = is_max_context tokens.append(all_doc_tokens[split_token_index]) segment_ids.append(data_utils.SEG_ID_P) p_mask.append(0) paragraph_len = len(tokens) tokens.append(data_utils.SEP_ID) segment_ids.append(data_utils.SEG_ID_P) p_mask.append(1) # note(zhiliny): we put P before Q # because during pretraining, B is always shorter than A for token in query_tokens: tokens.append(token) segment_ids.append(data_utils.SEG_ID_Q) p_mask.append(1) tokens.append(data_utils.SEP_ID) segment_ids.append(data_utils.SEG_ID_Q) p_mask.append(1) cls_index = len(segment_ids) tokens.append(data_utils.CLS_ID) segment_ids.append(data_utils.SEG_ID_CLS) p_mask.append(0) input_ids = tokens # The mask has 0 for real tokens and 1 for padding tokens. Only real # tokens are attended to. input_mask = [0] * len(input_ids) # Zero-pad up to the sequence length. while len(input_ids) < max_seq_length: input_ids.append(0) input_mask.append(1) segment_ids.append(data_utils.SEG_ID_PAD) p_mask.append(1) assert len(input_ids) == max_seq_length assert len(input_mask) == max_seq_length assert len(segment_ids) == max_seq_length assert len(p_mask) == max_seq_length span_is_impossible = example.is_impossible start_position = None end_position = None if is_training and not span_is_impossible: # For training, if our document chunk does not contain an annotation # we throw it out, since there is nothing to predict. doc_start = doc_span.start doc_end = doc_span.start + doc_span.length - 1 out_of_span = False if not (tok_start_position >= doc_start and tok_end_position <= doc_end): out_of_span = True if out_of_span: # continue start_position = 0 end_position = 0 span_is_impossible = True else: # note: we put P before Q, so doc_offset should be zero. # doc_offset = len(query_tokens) + 2 doc_offset = 0 start_position = tok_start_position - doc_start + doc_offset end_position = tok_end_position - doc_start + doc_offset if is_training and span_is_impossible: start_position = cls_index end_position = cls_index if example_index < 20: logging.info("*** Example ***") logging.info("unique_id: %s", unique_id) logging.info("example_index: %s", example_index) logging.info("doc_span_index: %s", doc_span_index) logging.info("tok_start_to_orig_index: %s", " ".join([str(x) for x in cur_tok_start_to_orig_index])) logging.info("tok_end_to_orig_index: %s", " ".join([str(x) for x in cur_tok_end_to_orig_index])) logging.info( "token_is_max_context: %s", " ".join([ "%d:%s" % (x, y) for (x, y) in six.iteritems(token_is_max_context) ])) logging.info("input_ids: %s", " ".join([str(x) for x in input_ids])) logging.info("input_mask: %s", " ".join([str(x) for x in input_mask])) logging.info("segment_ids: %s", " ".join([str(x) for x in segment_ids])) if is_training and span_is_impossible: logging.info("impossible example span") if is_training and not span_is_impossible: pieces = [ sp_model.IdToPiece(token) for token in tokens[start_position:(end_position + 1)] ] answer_text = sp_model.DecodePieces(pieces) logging.info("start_position: %d", start_position) logging.info("end_position: %d", end_position) logging.info("answer: %s", preprocess_utils.printable_text(answer_text)) # With multi processing, the example_index is actually the index # within the current process therefore we use example_index=None to # avoid being used in the future. # The current code does not use # example_index of training data. if is_training: feat_example_index = None else: feat_example_index = example_index feature = InputFeatures( unique_id=unique_id, example_index=feat_example_index, doc_span_index=doc_span_index, tok_start_to_orig_index=cur_tok_start_to_orig_index, tok_end_to_orig_index=cur_tok_end_to_orig_index, token_is_max_context=token_is_max_context, input_ids=input_ids, input_mask=input_mask, p_mask=p_mask, segment_ids=segment_ids, paragraph_len=paragraph_len, cls_index=cls_index, start_position=start_position, end_position=end_position, is_impossible=span_is_impossible) # Run callback output_fn(feature) unique_id += 1 if span_is_impossible: cnt_neg += 1 else: cnt_pos += 1 logging.info("Total number of instances: %d = pos %d + neg %d", cnt_pos + cnt_neg, cnt_pos, cnt_neg) def _check_is_max_context(doc_spans, cur_span_index, position): """Check if this is the "max context" doc span for the token.""" # Because of the sliding window approach taken to scoring documents, a single # token can appear in multiple documents. E.g. # Doc: the man went to the store and bought a gallon of milk # Span A: the man went to the # Span B: to the store and bought # Span C: and bought a gallon of # ... # # Now the word "bought" will have two scores from spans B and C. We only # want to consider the score with "maximum context", which we define as # the *minimum* of its left and right context (the *sum* of left and # right context will always be the same, of course). # # In the example the maximum context for "bought" would be span C since # it has 1 left context and 3 right context, while span B has 4 left context # and 0 right context. best_score = None best_span_index = None for (span_index, doc_span) in enumerate(doc_spans): end = doc_span.start + doc_span.length - 1 if position < doc_span.start: continue if position > end: continue num_left_context = position - doc_span.start num_right_context = end - position score = min(num_left_context, num_right_context) + 0.01 * doc_span.length if best_score is None or score > best_score: best_score = score best_span_index = span_index return cur_span_index == best_span_index class FeatureWriter(object): """Writes InputFeature to TF example file.""" def __init__(self, filename, is_training): self.filename = filename self.is_training = is_training self.num_features = 0 self._writer = tf.io.TFRecordWriter(filename) def process_feature(self, feature): """Write a InputFeature to the TFRecordWriter as a tf.train.Example.""" self.num_features += 1 def create_int_feature(values): feature = tf.train.Feature( int64_list=tf.train.Int64List(value=list(values))) return feature def create_float_feature(values): f = tf.train.Feature(float_list=tf.train.FloatList(value=list(values))) return f features = collections.OrderedDict() features["unique_ids"] = create_int_feature([feature.unique_id]) features["input_ids"] = create_int_feature(feature.input_ids) features["input_mask"] = create_float_feature(feature.input_mask) features["p_mask"] = create_float_feature(feature.p_mask) features["segment_ids"] = create_int_feature(feature.segment_ids) features["cls_index"] = create_int_feature([feature.cls_index]) if self.is_training: features["start_positions"] = create_int_feature([feature.start_position]) features["end_positions"] = create_int_feature([feature.end_position]) impossible = 0 if feature.is_impossible: impossible = 1 features["is_impossible"] = create_float_feature([impossible]) tf_example = tf.train.Example(features=tf.train.Features(feature=features)) self._writer.write(tf_example.SerializeToString()) def close(self): self._writer.close() def create_eval_data(spm_basename, sp_model, eval_examples, max_seq_length, max_query_length, doc_stride, uncased, output_dir=None): """Creates evaluation tfrecords.""" eval_features = [] eval_writer = None if output_dir: eval_rec_file = os.path.join( output_dir, "{}.slen-{}.qlen-{}.eval.tf_record".format(spm_basename, max_seq_length, max_query_length)) eval_feature_file = os.path.join( output_dir, "{}.slen-{}.qlen-{}.eval.features.pkl".format(spm_basename, max_seq_length, max_query_length)) eval_writer = FeatureWriter(filename=eval_rec_file, is_training=False) def append_feature(feature): eval_features.append(feature) if eval_writer: eval_writer.process_feature(feature) convert_examples_to_features( examples=eval_examples, sp_model=sp_model, max_seq_length=max_seq_length, doc_stride=doc_stride, max_query_length=max_query_length, is_training=False, output_fn=append_feature, uncased=uncased) if eval_writer: eval_writer.close() with tf.io.gfile.GFile(eval_feature_file, "wb") as fout: pickle.dump(eval_features, fout) return eval_features
32,224
32.119219
82
py
models
models-master/official/legacy/xlnet/preprocess_squad_data.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # coding=utf-8 """Script to pre-process SQUAD data into tfrecords.""" import os import random # Import libraries from absl import app from absl import flags from absl import logging import tensorflow as tf import sentencepiece as spm from official.legacy.xlnet import squad_utils flags.DEFINE_integer( "num_proc", default=1, help="Number of preprocessing processes.") flags.DEFINE_integer("proc_id", default=0, help="Process id for preprocessing.") # I/O paths flags.DEFINE_string("output_dir", default="", help="Output dir for TF records.") flags.DEFINE_string( "spiece_model_file", default="", help="Sentence Piece model path.") flags.DEFINE_string("train_file", default="", help="Path of train file.") flags.DEFINE_string("predict_file", default="", help="Path of prediction file.") # Data preprocessing config flags.DEFINE_integer("max_seq_length", default=512, help="Max sequence length") flags.DEFINE_integer("max_query_length", default=64, help="Max query length") flags.DEFINE_integer("doc_stride", default=128, help="Doc stride") flags.DEFINE_bool("uncased", default=False, help="Use uncased data.") flags.DEFINE_bool( "create_train_data", default=True, help="Whether to create training data.") flags.DEFINE_bool( "create_eval_data", default=False, help="Whether to create eval data.") FLAGS = flags.FLAGS def preprocess(): """Preprocesses SQUAD data.""" sp_model = spm.SentencePieceProcessor() sp_model.Load(FLAGS.spiece_model_file) spm_basename = os.path.basename(FLAGS.spiece_model_file) if FLAGS.create_train_data: train_rec_file = os.path.join( FLAGS.output_dir, "{}.{}.slen-{}.qlen-{}.train.tf_record".format(spm_basename, FLAGS.proc_id, FLAGS.max_seq_length, FLAGS.max_query_length)) logging.info("Read examples from %s", FLAGS.train_file) train_examples = squad_utils.read_squad_examples( FLAGS.train_file, is_training=True) train_examples = train_examples[FLAGS.proc_id::FLAGS.num_proc] # Pre-shuffle the input to avoid having to make a very large shuffle # buffer in the `input_fn`. random.shuffle(train_examples) write_to_logging = "Write to " + train_rec_file logging.info(write_to_logging) train_writer = squad_utils.FeatureWriter( filename=train_rec_file, is_training=True) squad_utils.convert_examples_to_features( examples=train_examples, sp_model=sp_model, max_seq_length=FLAGS.max_seq_length, doc_stride=FLAGS.doc_stride, max_query_length=FLAGS.max_query_length, is_training=True, output_fn=train_writer.process_feature, uncased=FLAGS.uncased) train_writer.close() if FLAGS.create_eval_data: eval_examples = squad_utils.read_squad_examples( FLAGS.predict_file, is_training=False) squad_utils.create_eval_data(spm_basename, sp_model, eval_examples, FLAGS.max_seq_length, FLAGS.max_query_length, FLAGS.doc_stride, FLAGS.uncased, FLAGS.output_dir) def main(_): logging.set_verbosity(logging.INFO) if not tf.io.gfile.exists(FLAGS.output_dir): tf.io.gfile.mkdir(FLAGS.output_dir) preprocess() if __name__ == "__main__": app.run(main)
4,046
36.12844
80
py
models
models-master/official/legacy/xlnet/xlnet_modeling.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Keras layers of XLNet model in TF 2.0.""" import copy import warnings import tensorflow as tf from official.legacy.xlnet import data_utils from official.nlp.modeling import networks def gelu(x): return tf.keras.activations.gelu(x, approximate=True) def _get_initializer(flags): """Get variable initializer.""" if flags.init_method == "uniform": initializer = tf.keras.initializers.RandomUniform( minval=-flags.init_range, maxval=flags.init_range) elif flags.init_method == "normal": initializer = tf.keras.initializers.RandomNormal(stddev=flags.init_std) else: raise ValueError("Initializer {} not supported".format(flags.init_method)) return initializer def rel_shift(x, klen=-1): """Performs relative shift to form the relative attention score.""" x_size = tf.shape(x) x = tf.reshape(x, [x_size[1], x_size[0], x_size[2], x_size[3]]) x = tf.slice(x, [1, 0, 0, 0], [-1, -1, -1, -1]) x = tf.reshape(x, [x_size[0], x_size[1] - 1, x_size[2], x_size[3]]) x = tf.slice(x, [0, 0, 0, 0], [-1, klen, -1, -1]) return x def _create_mask(qlen, mlen, dtype=tf.float32, same_length=False): """Creates attention mask when single-side context allowed only.""" attn_mask = tf.ones([qlen, qlen], dtype=dtype) mask_u = tf.linalg.band_part(attn_mask, 0, -1) mask_dia = tf.linalg.band_part(attn_mask, 0, 0) attn_mask_pad = tf.zeros([qlen, mlen], dtype=dtype) ret = tf.concat([attn_mask_pad, mask_u - mask_dia], 1) if same_length: mask_l = tf.linalg.band_part(attn_mask, -1, 0) ret = tf.concat([ret[:, :qlen] + mask_l - mask_dia, ret[:, qlen:]], 1) return ret def _cache_mem(curr_out, prev_mem, mem_len, reuse_len=None): """cache hidden states into memory.""" if mem_len is None or mem_len == 0: return None else: if reuse_len is not None and reuse_len > 0: curr_out = curr_out[:reuse_len] if prev_mem is None: new_mem = curr_out[-mem_len:] else: new_mem = tf.concat([prev_mem, curr_out], 0)[-mem_len:] return tf.keras.backend.stop_gradient(new_mem) def is_special_none_tensor(tensor): """Checks if a tensor is a special None Tensor.""" return tensor.shape.ndims == 0 and tensor.dtype == tf.int32 @tf.keras.utils.register_keras_serializable(package="Text") class RelativePositionEncoding(tf.keras.layers.Layer): """Creates a relative positional encoding. This layer creates a relative positional encoding as described in "Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context" (https://arxiv.org/abs/1901.02860). Rather than an absolute position embedding as in Transformer, this formulation represents position as the relative distance between tokens using sinusoidal positional embeddings. Note: This layer is currently experimental. Attributes: hidden_size: The dimensionality of the input embeddings. """ def __init__(self, hidden_size, **kwargs): super(RelativePositionEncoding, self).__init__(**kwargs) self._hidden_size = hidden_size self._inv_freq = 1.0 / (10000.0**( tf.range(0, self._hidden_size, 2.0) / self._hidden_size)) def call(self, pos_seq, batch_size=None): """Implements call() for the layer. Args: pos_seq: A 1-D `Tensor` batch_size: The optionally provided batch size that tiles the relative positional encoding. Returns: The relative positional encoding of shape: [len(pos_seq), batch_size, hidden_size] if batch_size is provided, else [len(pos_seq), 1, hidden_size]. """ sinusoid_input = tf.einsum("i,d->id", pos_seq, self._inv_freq) pos_emb = tf.concat([tf.sin(sinusoid_input), tf.cos(sinusoid_input)], -1) pos_emb = pos_emb[:, None, :] if batch_size is not None: pos_emb = tf.tile(pos_emb, [1, batch_size, 1]) return pos_emb class RelativeAttention(tf.keras.layers.Layer): """Core calculations for relative attention.""" def __init__(self, dropout_att, scale): super(RelativeAttention, self).__init__() self.scale = scale self.dropout_att = dropout_att def build(self, unused_input_shapes): """Implements build() for the layer.""" self.attention_probs_dropout = tf.keras.layers.Dropout( rate=self.dropout_att) super(RelativeAttention, self).build(unused_input_shapes) def call(self, q_head, k_head_h, v_head_h, k_head_r, seg_embed, seg_mat, r_w_bias, r_r_bias, r_s_bias, attn_mask): """Implements call() for the layer.""" # content based attention score ac = tf.einsum("ibnd,jbnd->ijbn", q_head + r_w_bias, k_head_h) # position based attention score bd = tf.einsum("ibnd,jbnd->ijbn", q_head + r_r_bias, k_head_r) bd = rel_shift(bd, klen=tf.shape(ac)[1]) # segment-based attention score if seg_mat is None: ef = 0 else: ef = tf.einsum("ibnd,snd->isbn", q_head + r_s_bias, seg_embed) tgt_shape = tf.shape(bd) ef = tf.where( tf.broadcast_to(tf.expand_dims(seg_mat, 3), tgt_shape), tf.broadcast_to(ef[:, 1:, :, :], tgt_shape), tf.broadcast_to(ef[:, :1, :, :], tgt_shape)) # merges attention scores and performs masking attn_score = (ac + bd + ef) * self.scale if attn_mask is not None: attn_score = attn_score - 1e30 * attn_mask # attention probability attn_prob = tf.nn.softmax(attn_score, 1) attn_prob = self.attention_probs_dropout(attn_prob) # attention output attn_vec = tf.einsum("ijbn,jbnd->ibnd", attn_prob, v_head_h) return attn_vec class PositionwiseFF(tf.keras.layers.Layer): """Positionwise feed-forward layer.""" def __init__(self, d_model, d_inner, dropout, kernel_initializer, activation_type, **kwargs): super(PositionwiseFF, self).__init__(**kwargs) self.d_model = d_model self.d_inner = d_inner self.dropout = dropout self.activation_type = activation_type self.kernel_initializer = kernel_initializer def build(self, unused_input_shapes): """Implements build() for the layer.""" if self.activation_type == "relu": activation = tf.nn.relu elif self.activation_type == "gelu": activation = gelu else: raise (ValueError("Unsupported activation type {}".format( self.activation_type))) self.inner_projection_layer = ( tf.keras.layers.Dense( units=self.d_inner, activation=activation, kernel_initializer=self.kernel_initializer, name="layer_1")) self.output_projection_layer = ( tf.keras.layers.Dense( units=self.d_model, kernel_initializer=self.kernel_initializer, name="layer_2")) self.output_dropout = tf.keras.layers.Dropout( rate=self.dropout, name="drop_2") self.output_layer_norm = ( tf.keras.layers.LayerNormalization( name="LayerNorm", axis=-1, epsilon=1e-12)) super(PositionwiseFF, self).build(unused_input_shapes) def call(self, inp): """Implements call() for the layer.""" output = self.inner_projection_layer(inp) output = self.output_projection_layer(output) output = self.output_dropout(output) output = self.output_layer_norm(output + inp) return output class EmbeddingLookup(tf.keras.layers.Layer): """Looks up words embeddings for id tensor.""" def __init__(self, n_token, d_embed, initializer, **kwargs): super(EmbeddingLookup, self).__init__(**kwargs) self.n_token = n_token self.d_embed = d_embed self.initializer = initializer def build(self, unused_input_shapes): """Implements build() for the layer.""" self.lookup_table = self.add_weight( "lookup_table", shape=[self.n_token, self.d_embed], initializer=self.initializer, dtype=self.dtype) super(EmbeddingLookup, self).build(unused_input_shapes) def call(self, inputs): return tf.nn.embedding_lookup(self.lookup_table, inputs) class RelativeMultiheadAttention(tf.keras.layers.Layer): """Multi-head attention with relative embedding.""" def __init__(self, d_model, n_head, d_head, dropout, dropout_att, kernel_initializer, **kwargs): super(RelativeMultiheadAttention, self).__init__(**kwargs) self.d_model = d_model self.n_head = n_head self.d_head = d_head self.dropout = dropout self.dropout_att = dropout_att self.initializer = kernel_initializer def build(self, unused_input_shapes): """Implements build() for the layer.""" self.scale = 1.0 / (self.d_head**0.5) self.output_layer_norm = tf.keras.layers.LayerNormalization( name="LayerNorm", axis=-1, epsilon=1e-12) self.kh_projection_layer = self.add_weight( "k/kernel", shape=[self.d_model, self.n_head, self.d_head], initializer=self.initializer) self.vh_projection_layer = self.add_weight( "v/kernel", shape=[self.d_model, self.n_head, self.d_head], initializer=self.initializer) self.kr_projection_layer = self.add_weight( "r/kernel", shape=[self.d_model, self.n_head, self.d_head], initializer=self.initializer) self.qh_projection_layer = self.add_weight( "q/kernel", shape=[self.d_model, self.n_head, self.d_head], initializer=self.initializer) self.relative_attention_layer = RelativeAttention( dropout_att=self.dropout_att, scale=self.scale) self.proj_o = self.add_weight( "o/kernel", shape=[self.d_model, self.n_head, self.d_head], initializer=self.initializer) self.attention_dropout = tf.keras.layers.Dropout(rate=self.dropout) super(RelativeMultiheadAttention, self).build(unused_input_shapes) def call(self, h, g, r, r_w_bias, r_r_bias, seg_mat, r_s_bias, seg_embed, attn_mask_h, attn_mask_g, mems, target_mapping): """Implements call() for the layer.""" if mems is not None and mems.shape.ndims > 1: cat = tf.concat([mems, h], 0) else: cat = h # content heads q_head_h = tf.einsum("ibh,hnd->ibnd", h, self.qh_projection_layer) k_head_h = tf.einsum("ibh,hnd->ibnd", cat, self.kh_projection_layer) v_head_h = tf.einsum("ibh,hnd->ibnd", cat, self.vh_projection_layer) # positional heads k_head_r = tf.einsum("ibh,hnd->ibnd", r, self.kr_projection_layer) # core attention ops attn_vec_h = self.relative_attention_layer(q_head_h, k_head_h, v_head_h, k_head_r, seg_embed, seg_mat, r_w_bias, r_r_bias, r_s_bias, attn_mask_h) # post processing output_h = tf.einsum("ibnd,hnd->ibh", attn_vec_h, self.proj_o) output_h = self.attention_dropout(output_h) output_h = self.output_layer_norm(output_h + h) output_g = None if g is not None: # enable two-stream attention # g-stream q_head_g = tf.einsum("ibh,hnd->ibnd", g, self.qh_projection_layer) if target_mapping is not None: q_head_g = tf.einsum("mbnd,mlb->lbnd", q_head_g, target_mapping) attn_vec_g = self.relative_attention_layer(q_head_g, k_head_h, v_head_h, k_head_r, seg_embed, seg_mat, r_w_bias, r_r_bias, r_s_bias, attn_mask_g) attn_vec_g = tf.einsum("lbnd,mlb->mbnd", attn_vec_g, target_mapping) else: attn_vec_g = self.relative_attention_layer(q_head_g, k_head_h, v_head_h, k_head_r, seg_embed, seg_mat, r_w_bias, r_r_bias, r_s_bias, attn_mask_g) # post processing output_g = tf.einsum("ibnd,hnd->ibh", attn_vec_g, self.proj_o) output_g = self.attention_dropout(output_g) output_g = self.output_layer_norm(output_g + g) return (output_h, output_g) class TransformerXLModel(tf.keras.layers.Layer): """Defines a Transformer-XL computation graph with additional support for XLNet.""" def __init__(self, n_token, n_layer, d_model, n_head, d_head, d_inner, dropout, dropout_att, attn_type, bi_data, is_training, initializer, mem_len=None, same_length=False, clamp_len=-1, untie_r=False, use_tpu=True, reuse_len=None, ff_activation="relu", use_cls_mask=False, **kwargs): """Initializes TransformerXLModel. Args: n_token: int, the number of tokens in vocabulary. n_layer: int, the number of layers. d_model: int, the hidden size. n_head: int, the number of attention heads. d_head: int, the dimension size of each attention head. d_inner: int, the hidden size in feed-forward layers. dropout: float, dropout rate. dropout_att: float, dropout rate on attention probabilities. attn_type: str, "uni" or "bi". bi_data: bool, whether to use bidirectional input pipeline. Usually set to True during pretraining and False during finetuning. is_training: bool, whether in training mode. initializer: A tf initializer. mem_len: int, the number of tokens to cache. same_length: bool, whether to use the same attention length for each token. clamp_len: int, clamp all relative distances larger than clamp_len. -1 means no clamping. untie_r: bool, whether to untie the biases in attention. use_tpu: bool, whether TPUs are used. reuse_len: int, the number of tokens in the currect batch to be cached and reused in the future. ff_activation: str, "relu" or "gelu". use_cls_mask: bool, whether to introduce cls mask. **kwargs: Other parameters. """ super(TransformerXLModel, self).__init__(**kwargs) warnings.warn( "`TransformerXLModel` is deprecated, please use `XLNetBase` instead", DeprecationWarning, stacklevel=2) self.n_token = n_token self.initializer = initializer self.attn_type = attn_type self.n_layer = n_layer self.d_model = d_model self.n_head = n_head self.d_head = d_head self.d_inner = d_inner self.ff_activation = ff_activation self.untie_r = untie_r self.use_tpu = use_tpu self.dropout = dropout self.dropout_att = dropout_att self.mem_len = mem_len self.reuse_len = reuse_len self.bi_data = bi_data self.clamp_len = clamp_len self.same_length = same_length self.use_cls_mask = use_cls_mask def build(self, unused_input_shapes): """Implements build() for the layer.""" self.tf_float = tf.float32 self.embedding_lookup = EmbeddingLookup( n_token=self.n_token, d_embed=self.d_model, initializer=self.initializer, dtype=self.tf_float, name="word_embedding") self.h_dropout = tf.keras.layers.Dropout(rate=self.dropout) self.g_dropout = tf.keras.layers.Dropout(rate=self.dropout) if self.untie_r: self.r_w_bias = ( self.add_weight( "r_w_bias", shape=[self.n_layer, self.n_head, self.d_head], dtype=self.tf_float, initializer=self.initializer)) self.r_r_bias = ( self.add_weight( "r_r_bias", shape=[self.n_layer, self.n_head, self.d_head], dtype=self.tf_float, initializer=self.initializer)) self.r_s_bias = ( self.add_weight( "r_s_bias", shape=[self.n_layer, self.n_head, self.d_head], dtype=self.tf_float, initializer=self.initializer)) else: self.r_w_bias = ( self.add_weight( "r_w_bias", shape=[self.n_head, self.d_head], dtype=self.tf_float, initializer=self.initializer)) self.r_r_bias = ( self.add_weight( "r_r_bias", shape=[self.n_head, self.d_head], dtype=self.tf_float, initializer=self.initializer)) self.r_s_bias = ( self.add_weight( "r_s_bias", [self.n_head, self.d_head], dtype=self.tf_float, initializer=self.initializer)) self.seg_embed = self.add_weight( "seg_embed", [self.n_layer, 2, self.n_head, self.d_head], dtype=self.tf_float, initializer=self.initializer) self.mask_emb = self.add_weight( "mask_emb/mask_emb", shape=[1, 1, self.d_model], dtype=self.tf_float) self.emb_dropout = tf.keras.layers.Dropout(rate=self.dropout) self.fwd_position_embedding = RelativePositionEncoding(self.d_model) self.bwd_position_embedding = RelativePositionEncoding(self.d_model) self.rel_multihead_layers = [] self.h_positionwise_ffn_layers = [] for i in range(self.n_layer): self.rel_multihead_layers.append( RelativeMultiheadAttention( d_model=self.d_model, dropout=self.dropout, n_head=self.n_head, d_head=self.d_head, dropout_att=self.dropout_att, kernel_initializer=self.initializer, name="layer_%d/rel_attn" % (i))) self.h_positionwise_ffn_layers.append( PositionwiseFF( d_model=self.d_model, d_inner=self.d_inner, dropout=self.dropout, kernel_initializer=self.initializer, activation_type=self.ff_activation, name="layer_%d/ff" % (i))) self.output_dropout = tf.keras.layers.Dropout(rate=self.dropout) super(TransformerXLModel, self).build(unused_input_shapes) def __call__(self, inp_k, seg_id=None, input_mask=None, mems=None, perm_mask=None, target_mapping=None, inp_q=None, **kwargs): # Uses dict to feed inputs into call() in order to keep mems as a python # list. inputs = { "inp_k": inp_k, "seg_id": seg_id, "input_mask": input_mask, "mems": mems, "perm_mask": perm_mask, "target_mapping": target_mapping, "inp_q": inp_q } return super(TransformerXLModel, self).__call__(inputs, **kwargs) def call(self, inputs): """Implements call() for the layer.""" inp_k = inputs["inp_k"] seg_id = inputs["seg_id"] input_mask = inputs["input_mask"] mems = inputs["mems"] perm_mask = inputs["perm_mask"] target_mapping = inputs["target_mapping"] inp_q = inputs["inp_q"] new_mems = [] bsz = tf.shape(inp_k)[1] qlen = inp_k.shape.as_list()[0] mlen = mems[0].shape.as_list()[0] if mems is not None else 0 klen = mlen + qlen ##### Attention mask # causal attention mask if self.attn_type == "uni": attn_mask = _create_mask(qlen, mlen, self.tf_float, self.same_length) # pylint: enable=protected-access attn_mask = attn_mask[:, :, None, None] elif self.attn_type == "bi": attn_mask = None else: raise ValueError("Unsupported attention type: {}".format(self.attn_type)) # data mask: input mask & perm mask if input_mask is not None and perm_mask is not None: data_mask = input_mask[None] + perm_mask elif input_mask is not None and perm_mask is None: data_mask = input_mask[None] elif input_mask is None and perm_mask is not None: data_mask = perm_mask else: data_mask = None if data_mask is not None: # all mems can be attended to mems_mask = tf.zeros([tf.shape(data_mask)[0], mlen, bsz], dtype=self.tf_float) data_mask = tf.concat([mems_mask, data_mask], 1) if attn_mask is None: attn_mask = data_mask[:, :, :, None] else: attn_mask += data_mask[:, :, :, None] if attn_mask is not None: attn_mask = tf.cast(attn_mask > 0, dtype=self.tf_float) if attn_mask is not None: non_tgt_mask = -tf.eye(qlen, dtype=self.tf_float) non_tgt_mask = tf.concat( [tf.zeros([qlen, mlen], dtype=self.tf_float), non_tgt_mask], axis=-1) non_tgt_mask = tf.cast( (attn_mask + non_tgt_mask[:, :, None, None]) > 0, dtype=self.tf_float) else: non_tgt_mask = None word_emb_k = self.embedding_lookup(inp_k) if inp_q is not None: if target_mapping is not None: word_emb_q = tf.tile(self.mask_emb, [tf.shape(target_mapping)[0], bsz, 1]) else: inp_q_ext = inp_q[:, :, None] word_emb_q = inp_q_ext * self.mask_emb + (1 - inp_q_ext) * word_emb_k output_h = self.h_dropout(word_emb_k) output_g = None if inp_q is not None: output_g = self.g_dropout(word_emb_q) ##### Segment embedding if seg_id is not None: # Convert `seg_id` to one-hot `seg_mat` mem_pad = tf.zeros([mlen, bsz], dtype=tf.int32) cat_id = tf.concat([mem_pad, seg_id], 0) if self.use_cls_mask: # `1` indicates not in the same segment [qlen x klen x bsz] # seg_id: [qlen x bsz] & cat_id: [klen x bsz] cls_mat = tf.logical_or( tf.equal(seg_id, tf.constant([data_utils.SEG_ID_CLS]))[:, None], tf.equal(cat_id, tf.constant([data_utils.SEG_ID_CLS]))[None, :]) seg_mat = tf.equal(seg_id[:, None], cat_id[None, :]) seg_mat = tf.logical_or(cls_mat, seg_mat) else: seg_mat = tf.logical_not(tf.equal(seg_id[:, None], cat_id[None, :])) else: seg_mat = None dtype = self.tf_float freq_seq = tf.range(0, self.d_model, 2.0) if dtype is not None and dtype != tf.float32: freq_seq = tf.cast(freq_seq, dtype=self.dtype) if self.attn_type == "bi": beg, end = klen, -qlen elif self.attn_type == "uni": beg, end = klen, -1 else: raise ValueError("Unknown `attn_type` {}.".format(self.attn_type)) if self.bi_data: fwd_pos_seq = tf.range(beg, end, -1.0) bwd_pos_seq = tf.range(-beg, -end, 1.0) if dtype is not None and dtype != tf.float32: fwd_pos_seq = tf.cast(fwd_pos_seq, dtype=dtype) bwd_pos_seq = tf.cast(bwd_pos_seq, dtype=dtype) if self.clamp_len > 0: fwd_pos_seq = tf.clip_by_value(fwd_pos_seq, -self.clamp_len, self.clamp_len) bwd_pos_seq = tf.clip_by_value(bwd_pos_seq, -self.clamp_len, self.clamp_len) if bsz is not None: fwd_pos_emb = self.fwd_position_embedding(fwd_pos_seq, bsz // 2) bwd_pos_emb = self.bwd_position_embedding(bwd_pos_seq, bsz // 2) else: fwd_pos_emb = self.fwd_position_embedding(fwd_pos_seq, None) bwd_pos_emb = self.bwd_position_embedding(bwd_pos_seq, None) pos_emb = tf.concat([fwd_pos_emb, bwd_pos_emb], axis=1) else: fwd_pos_seq = tf.range(beg, end, -1.0) if dtype is not None and dtype != tf.float32: fwd_pos_seq = tf.cast(fwd_pos_seq, dtype=dtype) if self.clamp_len > 0: fwd_pos_seq = tf.clip_by_value(fwd_pos_seq, -self.clamp_len, self.lamp_len) pos_emb = self.fwd_position_embedding(fwd_pos_seq, bsz) pos_emb = self.emb_dropout(pos_emb) if mems is None: mems = [None] * self.n_layer for i in range(self.n_layer): # cache new mems new_mems.append( _cache_mem(output_h, mems[i], self.mem_len, self.reuse_len)) # pylint: enable=protected-access # segment bias if seg_id is None: r_s_bias_i = None seg_embed_i = None else: r_s_bias_i = self.r_s_bias if not self.untie_r else self.r_s_bias[i] seg_embed_i = self.seg_embed[i] ffn_layer = self.h_positionwise_ffn_layers[i] attention_layer = self.rel_multihead_layers[i] output_h, output_g = attention_layer( h=output_h, g=output_g, r=pos_emb, r_w_bias=self.r_w_bias if not self.untie_r else self.r_w_bias[i], r_r_bias=self.r_r_bias if not self.untie_r else self.r_r_bias[i], seg_mat=seg_mat, r_s_bias=r_s_bias_i, seg_embed=seg_embed_i, attn_mask_h=non_tgt_mask, attn_mask_g=attn_mask, mems=mems[i], target_mapping=target_mapping) output_h = ffn_layer(output_h) if output_g is not None: output_g = ffn_layer(output_g) if inp_q is not None: output = output_g else: output = output_h return output, new_mems, None class PretrainingXLNetModel(tf.keras.Model): """XLNet keras model combined with pretraining LM loss layer. See the original paper: https://arxiv.org/pdf/1906.08237.pdf """ def __init__(self, use_proj, xlnet_config, run_config, use_legacy_mask=True, **kwargs): super(PretrainingXLNetModel, self).__init__(**kwargs) self.run_config = run_config self.initializer = _get_initializer(run_config) self.xlnet_config = copy.deepcopy(xlnet_config) self._use_legacy_mask = use_legacy_mask self.xlnet_model = networks.XLNetBase( vocab_size=self.xlnet_config.n_token, initializer=self.initializer, attention_type="bi", num_layers=self.xlnet_config.n_layer, hidden_size=self.xlnet_config.d_model, num_attention_heads=self.xlnet_config.n_head, head_size=self.xlnet_config.d_head, inner_size=self.xlnet_config.d_inner, two_stream=True, tie_attention_biases=not self.xlnet_config.untie_r, inner_activation=self.xlnet_config.ff_activation, dropout_rate=self.run_config.dropout, attention_dropout_rate=self.run_config.dropout_att, memory_length=self.run_config.mem_len, reuse_length=self.run_config.reuse_len, bi_data=self.run_config.bi_data, clamp_length=self.run_config.clamp_len, use_cls_mask=self.run_config.use_cls_mask, name="xlnet_model") self.lmloss_layer = LMLossLayer( vocab_size=self.xlnet_config.n_token, hidden_size=self.xlnet_config.d_model, initializer=self.initializer, tie_weight=True, bi_data=self.run_config.bi_data, use_one_hot=self.run_config.use_tpu, use_proj=use_proj, name="lm_loss") def call(self, features): """Implements call() for the layer.""" input_ids = features["input_ids"] masked_tokens = features["input_q"] seg_ids = features["seg_id"] if self._use_legacy_mask: # Legacy input mask assumes `real` values are 0 and `padding` # values are 1. perm_mask = 1 - features["perm_mask"] else: perm_mask = features["perm_mask"] target_mapping = features["target_mapping"] # target for LM loss target = features["target"] # target mask for LM loss tgt_mask = features["target_mask"] mems = features.get("mems", None) model_output, self.new_mems = self.xlnet_model( input_ids=input_ids, segment_ids=seg_ids, input_mask=None, state=mems, permutation_mask=perm_mask, target_mapping=target_mapping, masked_tokens=masked_tokens) lm_loss, _ = self.lmloss_layer( hidden=model_output, target=target, lookup_table=self.xlnet_model.get_embedding_lookup_table(), target_mask=tgt_mask) self.add_loss(lm_loss) return self.new_mems, model_output class ClassificationXLNetModel(tf.keras.Model): """XLNet keras model combined with classification loss layer. See the original paper: https://arxiv.org/pdf/1906.08237.pdf """ def __init__(self, xlnet_config, run_config, n_class, summary_type, use_legacy_mask=True, **kwargs): super(ClassificationXLNetModel, self).__init__(**kwargs) warnings.warn( "`ClassificationXLNetModel` is deprecated, please use `XLNetClassifier`" "instead.", DeprecationWarning, stacklevel=2) self.run_config = run_config self.initializer = _get_initializer(run_config) self.xlnet_config = copy.deepcopy(xlnet_config) self._use_legacy_mask = use_legacy_mask self.xlnet_model = networks.XLNetBase( vocab_size=self.xlnet_config.n_token, initializer=self.initializer, attention_type="bi", num_layers=self.xlnet_config.n_layer, hidden_size=self.xlnet_config.d_model, num_attention_heads=self.xlnet_config.n_head, head_size=self.xlnet_config.d_head, inner_size=self.xlnet_config.d_inner, two_stream=False, tie_attention_biases=not self.xlnet_config.untie_r, inner_activation=self.xlnet_config.ff_activation, dropout_rate=self.run_config.dropout, attention_dropout_rate=self.run_config.dropout_att, memory_length=self.run_config.mem_len, reuse_length=self.run_config.reuse_len, bi_data=self.run_config.bi_data, clamp_length=self.run_config.clamp_len, use_cls_mask=False, name="xlnet_model") self.summarization_layer = Summarization( hidden_size=self.xlnet_config.d_model, num_attention_heads=self.xlnet_config.n_head, head_size=self.xlnet_config.d_head, dropout_rate=self.run_config.dropout, attention_dropout_rate=self.run_config.dropout_att, initializer=self.initializer, use_proj=True, summary_type=summary_type, name="sequence_summary") self.cl_loss_layer = ClassificationLossLayer( n_class=n_class, initializer=self.initializer, name="classification") def call(self, features): """Implements call() for the layer.""" batch_size_per_core = tf.shape(features["input_ids"])[0] input_ids = features["input_ids"] segment_ids = features["segment_ids"] if self._use_legacy_mask: # Legacy input mask assumes `real` values are 0 and `padding` # values are 1. input_mask = 1 - features["input_mask"] else: input_mask = features["input_mask"] label = tf.reshape(features["label_ids"], [batch_size_per_core]) mems = features.get("mems", None) attention_output, new_mems = ( self.xlnet_model(input_ids, segment_ids, input_mask, mems)) summary = self.summarization_layer(attention_output) per_example_loss, logits = self.cl_loss_layer(hidden=summary, labels=label) self.add_loss(tf.keras.backend.mean(per_example_loss)) return new_mems, logits class LMLossLayer(tf.keras.layers.Layer): """Layer computing cross entropy loss for language modeling.""" def __init__(self, vocab_size, hidden_size, initializer, tie_weight=False, bi_data=True, use_one_hot=False, use_proj=False, **kwargs): """Constructs LMLoss layer. Args: vocab_size: Number of tokens in vocabulary. hidden_size: The dimension of model hidden state. initializer: Initializer used for parameters. tie_weight: Whether to share weights between embedding lookup layer and next-token prediction layer. bi_data: Whether to use bidirectional input pipeline. Usually set to True during pretraining and False during finetuning. use_one_hot: bool, whether to use one hot encodings. This should be used when TPUs are used. use_proj: bool, whether to add a projection layer before LM prediction. **kwargs: Other parameters. """ super(LMLossLayer, self).__init__(**kwargs) self.vocab_size = vocab_size self.hidden_size = hidden_size self.initializer = initializer self.tie_weight = tie_weight self.bi_data = bi_data self.use_one_hot = use_one_hot self.use_proj = use_proj def build(self, unused_input_shapes): """Implements build() for the layer.""" if self.use_proj: self.proj_layer = tf.keras.layers.Dense( units=self.hidden_size, kernel_initializer=self.initializer, activation=gelu, name="lm_projection/dense") self.proj_layer_norm = tf.keras.layers.LayerNormalization( axis=-1, epsilon=1e-12, name="lm_projection/LayerNorm") if not self.tie_weight: self.softmax_w = self.add_weight( "weight", shape=[self.vocab_size, self.hidden_size], initializer=self.initializer) self.softmax_b = self.add_weight( "bias", shape=[self.vocab_size], initializer=tf.zeros_initializer()) super(LMLossLayer, self).build(unused_input_shapes) def call(self, hidden, target, lookup_table, target_mask): """Implements call() for the layer.""" if self.use_proj: hidden = self.proj_layer_norm(self.proj_layer(hidden)) if self.tie_weight: logits = tf.einsum("ibd,nd->ibn", hidden, lookup_table) + self.softmax_b else: logits = tf.einsum("ibd,nd->ibn", hidden, self.softmax_w) + self.softmax_b if self.use_one_hot: one_hot_target = tf.one_hot(target, self.vocab_size, dtype=logits.dtype) loss = -tf.reduce_sum(tf.nn.log_softmax(logits) * one_hot_target, -1) else: loss = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=target, logits=logits) total_loss = tf.reduce_sum(loss * target_mask) / tf.reduce_sum(target_mask) return total_loss, logits class Summarization(tf.keras.layers.Layer): """The layer to pool the output from XLNet model into a vector.""" def __init__(self, hidden_size, num_attention_heads, head_size, dropout_rate, attention_dropout_rate, initializer, use_proj=True, summary_type="last", **kwargs): """Constructs Summarization layer. Args: hidden_size: int, the dimension of model hidden state. num_attention_heads: int, the number of attention heads. head_size: int, the dimension size of each attention head. dropout_rate: float, dropout rate. attention_dropout_rate: float, dropout rate on attention probabilities. initializer: Initializer used for parameters. use_proj: bool, whether to use projection layer for summarization. summary_type: Method used to summarize a sequence into a compact vector. **kwargs: Other parameters. """ super(Summarization, self).__init__(**kwargs) self.hidden_size = hidden_size self.num_attention_heads = num_attention_heads self.head_size = head_size self.initializer = initializer self.dropout_rate = dropout_rate self.attention_dropout_rate = attention_dropout_rate self.use_proj = use_proj self.summary_type = summary_type def build(self, unused_input_shapes): """Implements build() for the layer.""" if self.use_proj: self.proj_layer = tf.keras.layers.Dense( units=self.hidden_size, kernel_initializer=self.initializer, activation=tf.nn.tanh, name="summary") self.dropout_layer = tf.keras.layers.Dropout(rate=self.dropout_rate) super(Summarization, self).build(unused_input_shapes) def call(self, inputs): """Implements call() for the layer.""" if self.summary_type == "last": summary = inputs[:, -1, :] elif self.summary_type == "first": summary = inputs[:, 0, :] else: raise ValueError("Invalid summary type provided: %s" % self.summary_type) if self.use_proj: summary = self.proj_layer(summary) summary = self.dropout_layer(summary) return summary class ClassificationLossLayer(tf.keras.layers.Layer): """Layer computing cross entropy loss for classification task.""" def __init__(self, n_class, initializer, **kwargs): """Constructs Summarization layer. Args: n_class: Number of tokens in vocabulary. initializer: Initializer used for parameters. **kwargs: Other parameters. """ super(ClassificationLossLayer, self).__init__(**kwargs) self.n_class = n_class self.initializer = initializer def build(self, unused_input_shapes): """Implements build() for the layer.""" self.proj_layer = tf.keras.layers.Dense( units=self.n_class, kernel_initializer=self.initializer, name="logit") super(ClassificationLossLayer, self).build(unused_input_shapes) def call(self, hidden, labels): """Implements call() for the layer.""" logits = self.proj_layer(hidden) one_hot_target = tf.one_hot(labels, self.n_class, dtype=hidden.dtype) # pytype: disable=attribute-error loss = -tf.reduce_sum(tf.nn.log_softmax(logits) * one_hot_target, -1) return loss, logits class QAXLNetModel(tf.keras.Model): """XLNet keras model combined with question answering loss layer. See the original paper: https://arxiv.org/pdf/1906.08237.pdf """ def __init__(self, xlnet_config, run_config, start_n_top, end_n_top, use_legacy_mask=True, **kwargs): super(QAXLNetModel, self).__init__(**kwargs) warnings.warn( "`QAXLNetModel` is deprecated, please use `XLNetSpanLabeler` instead.", DeprecationWarning, stacklevel=2) self.run_config = run_config self.initializer = _get_initializer(run_config) self.xlnet_config = copy.deepcopy(xlnet_config) self._use_legacy_mask = use_legacy_mask self.xlnet_model = networks.XLNetBase( vocab_size=self.xlnet_config.n_token, initializer=self.initializer, attention_type="bi", num_layers=self.xlnet_config.n_layer, hidden_size=self.xlnet_config.d_model, num_attention_heads=self.xlnet_config.n_head, head_size=self.xlnet_config.d_head, inner_size=self.xlnet_config.d_inner, tie_attention_biases=not self.xlnet_config.untie_r, inner_activation=self.xlnet_config.ff_activation, dropout_rate=self.run_config.dropout, attention_dropout_rate=self.run_config.dropout_att, two_stream=False, memory_length=self.run_config.mem_len, reuse_length=self.run_config.reuse_len, bi_data=self.run_config.bi_data, clamp_length=self.run_config.clamp_len, use_cls_mask=False, name="xlnet_model") self.qa_loss_layer = QALossLayer( hidden_size=self.xlnet_config.d_model, start_n_top=start_n_top, end_n_top=end_n_top, initializer=self.initializer, dropout_rate=self.run_config.dropout, name="qa_loss_layer") def call(self, features, training=False): """Implements call() for the layer.""" input_ids = features["input_ids"] segment_ids = features["segment_ids"] if self._use_legacy_mask: # Legacy input mask assumes `real` values are 0 and `padding` # values are 1. input_mask = 1 - features["input_mask"] else: input_mask = features["input_mask"] cls_index = tf.reshape(features["cls_index"], [-1]) p_mask = features["p_mask"] attention_output, new_mems = ( self.xlnet_model(input_ids, segment_ids, input_mask)) if training: loss, logits = self.qa_loss_layer( hidden=attention_output, p_mask=p_mask, cls_index=cls_index, start_positions=features["start_positions"], end_positions=features["end_positions"], is_impossible=features["is_impossible"]) self.add_loss(loss) return new_mems, logits else: results = self.qa_loss_layer( hidden=attention_output, p_mask=p_mask, cls_index=cls_index) return results class QALossLayer(tf.keras.layers.Layer): """Layer computing position and regression loss for question answering task.""" def __init__(self, hidden_size, start_n_top, end_n_top, initializer, dropout_rate, **kwargs): """Constructs Summarization layer. Args: hidden_size: Int, the hidden size. start_n_top: Beam size for span start. end_n_top: Beam size for span end. initializer: Initializer used for parameters. dropout_rate: float, dropout rate. **kwargs: Other parameters. """ super(QALossLayer, self).__init__(**kwargs) self.hidden_size = hidden_size self.start_n_top = start_n_top self.end_n_top = end_n_top self.initializer = initializer self.dropout_rate = dropout_rate def build(self, unused_input_shapes): """Implements build() for the layer.""" self.start_logits_proj_layer = tf.keras.layers.Dense( units=1, kernel_initializer=self.initializer, name="start_logits/dense") self.end_logits_proj_layer0 = tf.keras.layers.Dense( units=self.hidden_size, kernel_initializer=self.initializer, activation=tf.nn.tanh, name="end_logits/dense_0") self.end_logits_proj_layer1 = tf.keras.layers.Dense( units=1, kernel_initializer=self.initializer, name="end_logits/dense_1") self.end_logits_layer_norm = tf.keras.layers.LayerNormalization( axis=-1, epsilon=1e-12, name="end_logits/LayerNorm") self.answer_class_proj_layer0 = tf.keras.layers.Dense( units=self.hidden_size, kernel_initializer=self.initializer, activation=tf.nn.tanh, name="answer_class/dense_0") self.answer_class_proj_layer1 = tf.keras.layers.Dense( units=1, kernel_initializer=self.initializer, use_bias=False, name="answer_class/dense_1") self.ans_feature_dropout = tf.keras.layers.Dropout(rate=self.dropout_rate) super(QALossLayer, self).build(unused_input_shapes) def __call__(self, hidden, p_mask, cls_index, **kwargs): return super(QALossLayer, self).__call__( (hidden, p_mask, cls_index, kwargs)) def call(self, inputs, training=False): """Implements call() for the layer.""" hidden, p_mask, cls_index, kwargs = inputs return_dict = {} seq_len = tf.shape(hidden)[1] hidden = tf.transpose(hidden, [1, 0, 2]) start_logits = self.start_logits_proj_layer(hidden) start_logits = tf.transpose(tf.squeeze(start_logits, -1), [1, 0]) start_logits_masked = start_logits * (1 - p_mask) - 1e30 * p_mask start_log_probs = tf.nn.log_softmax(start_logits_masked, -1) if training: start_positions = kwargs["start_positions"] end_positions = kwargs["end_positions"] is_impossible = kwargs["is_impossible"] start_positions = tf.reshape(start_positions, [-1]) start_index = tf.one_hot( start_positions, depth=seq_len, axis=-1, dtype=tf.float32) start_features = tf.einsum("lbh,bl->bh", hidden, start_index) start_features = tf.tile(start_features[None], [seq_len, 1, 1]) end_logits = self.end_logits_proj_layer0( tf.concat([hidden, start_features], axis=-1)) end_logits = self.end_logits_layer_norm(end_logits) end_logits = self.end_logits_proj_layer1(end_logits) end_logits = tf.transpose(tf.squeeze(end_logits, -1), [1, 0]) end_logits_masked = end_logits * (1 - p_mask) - 1e30 * p_mask end_log_probs = tf.nn.log_softmax(end_logits_masked, -1) else: # during inference, compute the end logits based on beam search start_top_log_probs, start_top_index = tf.nn.top_k( start_log_probs, k=self.start_n_top) start_index = tf.one_hot( start_top_index, depth=seq_len, axis=-1, dtype=tf.float32) start_features = tf.einsum("lbh,bkl->bkh", hidden, start_index) end_input = tf.tile(hidden[:, :, None], [1, 1, self.start_n_top, 1]) start_features = tf.tile(start_features[None], [seq_len, 1, 1, 1]) end_input = tf.concat([end_input, start_features], axis=-1) end_logits = self.end_logits_proj_layer0(end_input) end_logits = tf.reshape(end_logits, [seq_len, -1, self.hidden_size]) end_logits = self.end_logits_layer_norm(end_logits) end_logits = tf.reshape(end_logits, [seq_len, -1, self.start_n_top, self.hidden_size]) end_logits = self.end_logits_proj_layer1(end_logits) end_logits = tf.reshape(end_logits, [seq_len, -1, self.start_n_top]) end_logits = tf.transpose(end_logits, [1, 2, 0]) end_logits_masked = end_logits * ( 1 - p_mask[:, None]) - 1e30 * p_mask[:, None] end_log_probs = tf.nn.log_softmax(end_logits_masked, -1) end_top_log_probs, end_top_index = tf.nn.top_k( end_log_probs, k=self.end_n_top) end_top_log_probs = tf.reshape(end_top_log_probs, [-1, self.start_n_top * self.end_n_top]) end_top_index = tf.reshape(end_top_index, [-1, self.start_n_top * self.end_n_top]) if training: return_dict["start_log_probs"] = start_log_probs return_dict["end_log_probs"] = end_log_probs else: return_dict["start_top_log_probs"] = start_top_log_probs return_dict["start_top_index"] = start_top_index return_dict["end_top_log_probs"] = end_top_log_probs return_dict["end_top_index"] = end_top_index # an additional layer to predict answerability # get the representation of CLS cls_index = tf.one_hot(cls_index, seq_len, axis=-1, dtype=tf.float32) cls_feature = tf.einsum("lbh,bl->bh", hidden, cls_index) # get the representation of START start_p = tf.nn.softmax(start_logits_masked, axis=-1, name="softmax_start") start_feature = tf.einsum("lbh,bl->bh", hidden, start_p) ans_feature = tf.concat([start_feature, cls_feature], -1) ans_feature = self.answer_class_proj_layer0(ans_feature) ans_feature = self.ans_feature_dropout(ans_feature) cls_logits = self.answer_class_proj_layer1(ans_feature) cls_logits = tf.squeeze(cls_logits, -1) return_dict["cls_logits"] = cls_logits if not training: return return_dict def compute_loss(log_probs, positions): one_hot_positions = tf.one_hot(positions, depth=seq_len, dtype=tf.float32) loss = -tf.reduce_sum(one_hot_positions * log_probs, axis=-1) loss = tf.reduce_mean(loss) return loss start_loss = compute_loss(start_log_probs, start_positions) end_loss = compute_loss(end_log_probs, end_positions) total_loss = (start_loss + end_loss) * 0.5 is_impossible = tf.reshape(is_impossible, [-1]) regression_loss = tf.nn.sigmoid_cross_entropy_with_logits( labels=is_impossible, logits=cls_logits) regression_loss = tf.reduce_mean(regression_loss) total_loss += regression_loss * 0.5 return total_loss, cls_logits
47,595
34.975813
108
py
models
models-master/official/legacy/xlnet/classifier_utils.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Utilities for pre-processing classification data.""" from absl import logging from official.legacy.xlnet import data_utils SEG_ID_A = 0 SEG_ID_B = 1 class PaddingInputExample(object): """Fake example so the num input examples is a multiple of the batch size. When running eval/predict on the TPU, we need to pad the number of examples to be a multiple of the batch size, because the TPU requires a fixed batch size. The alternative is to drop the last batch, which is bad because it means the entire output data won't be generated. We use this class instead of `None` because treating `None` as padding battches could cause silent errors. """ class InputFeatures(object): """A single set of features of data.""" def __init__(self, input_ids, input_mask, segment_ids, label_id, is_real_example=True): self.input_ids = input_ids self.input_mask = input_mask self.segment_ids = segment_ids self.label_id = label_id self.is_real_example = is_real_example def _truncate_seq_pair(tokens_a, tokens_b, max_length): """Truncates a sequence pair in place to the maximum length.""" # This is a simple heuristic which will always truncate the longer sequence # one token at a time. This makes more sense than truncating an equal percent # of tokens from each, since if one sequence is very short then each token # that's truncated likely contains more information than a longer sequence. while True: total_length = len(tokens_a) + len(tokens_b) if total_length <= max_length: break if len(tokens_a) > len(tokens_b): tokens_a.pop() else: tokens_b.pop() def convert_single_example(example_index, example, label_list, max_seq_length, tokenize_fn, use_bert_format): """Converts a single `InputExample` into a single `InputFeatures`.""" if isinstance(example, PaddingInputExample): return InputFeatures( input_ids=[0] * max_seq_length, input_mask=[1] * max_seq_length, segment_ids=[0] * max_seq_length, label_id=0, is_real_example=False) if label_list is not None: label_map = {} for (i, label) in enumerate(label_list): label_map[label] = i tokens_a = tokenize_fn(example.text_a) tokens_b = None if example.text_b: tokens_b = tokenize_fn(example.text_b) if tokens_b: # Modifies `tokens_a` and `tokens_b` in place so that the total # length is less than the specified length. # Account for two [SEP] & one [CLS] with "- 3" _truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3) else: # Account for one [SEP] & one [CLS] with "- 2" if len(tokens_a) > max_seq_length - 2: tokens_a = tokens_a[:max_seq_length - 2] tokens = [] segment_ids = [] for token in tokens_a: tokens.append(token) segment_ids.append(SEG_ID_A) tokens.append(data_utils.SEP_ID) segment_ids.append(SEG_ID_A) if tokens_b: for token in tokens_b: tokens.append(token) segment_ids.append(SEG_ID_B) tokens.append(data_utils.SEP_ID) segment_ids.append(SEG_ID_B) if use_bert_format: tokens.insert(0, data_utils.CLS_ID) segment_ids.insert(0, data_utils.SEG_ID_CLS) else: tokens.append(data_utils.CLS_ID) segment_ids.append(data_utils.SEG_ID_CLS) input_ids = tokens # The mask has 0 for real tokens and 1 for padding tokens. Only real # tokens are attended to. input_mask = [0] * len(input_ids) # Zero-pad up to the sequence length. if len(input_ids) < max_seq_length: delta_len = max_seq_length - len(input_ids) if use_bert_format: input_ids = input_ids + [0] * delta_len input_mask = input_mask + [1] * delta_len segment_ids = segment_ids + [data_utils.SEG_ID_PAD] * delta_len else: input_ids = [0] * delta_len + input_ids input_mask = [1] * delta_len + input_mask segment_ids = [data_utils.SEG_ID_PAD] * delta_len + segment_ids assert len(input_ids) == max_seq_length assert len(input_mask) == max_seq_length assert len(segment_ids) == max_seq_length if label_list is not None: label_id = label_map[example.label] else: label_id = example.label if example_index < 5: logging.info("*** Example ***") logging.info("guid: %s", (example.guid)) logging.info("input_ids: %s", " ".join([str(x) for x in input_ids])) logging.info("input_mask: %s", " ".join([str(x) for x in input_mask])) logging.info("segment_ids: %s", " ".join([str(x) for x in segment_ids])) logging.info("label: %s (id = %d)", example.label, label_id) feature = InputFeatures( input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids, label_id=label_id) return feature
5,421
32.060976
80
py
models
models-master/official/legacy/xlnet/xlnet_config.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Utility functions used in XLNet model.""" import json import os import tensorflow as tf def create_run_config(is_training, is_finetune, flags): """Helper function for creating RunConfig.""" kwargs = dict( is_training=is_training, use_tpu=flags.use_tpu, dropout=flags.dropout, dropout_att=flags.dropout_att, init_method=flags.init_method, init_range=flags.init_range, init_std=flags.init_std, clamp_len=flags.clamp_len) if not is_finetune: kwargs.update( dict( mem_len=flags.mem_len, reuse_len=flags.reuse_len, bi_data=flags.bi_data, clamp_len=flags.clamp_len, same_length=flags.same_length)) return RunConfig(**kwargs) # TODO(hongkuny): refactor XLNetConfig and RunConfig. class XLNetConfig(object): """Configs for XLNet model. XLNetConfig contains hyperparameters that are specific to a model checkpoint; i.e., these hyperparameters should be the same between pretraining and finetuning. The following hyperparameters are defined: n_layer: int, the number of layers. d_model: int, the hidden size. n_head: int, the number of attention heads. d_head: int, the dimension size of each attention head. d_inner: int, the hidden size in feed-forward layers. ff_activation: str, "relu" or "gelu". untie_r: bool, whether to untie the biases in attention. n_token: int, the vocab size. """ def __init__(self, FLAGS=None, json_path=None, args_dict=None): """Constructing an XLNetConfig. One of FLAGS or json_path should be provided. Args: FLAGS: An FLAGS instance. json_path: A path to a json config file. args_dict: A dict for args. """ assert FLAGS is not None or json_path is not None or args_dict is not None self.keys = [ 'n_layer', 'd_model', 'n_head', 'd_head', 'd_inner', 'ff_activation', 'untie_r', 'n_token' ] if FLAGS is not None: self.init_from_flags(FLAGS) if json_path is not None: self.init_from_json(json_path) if args_dict is not None: self.init_from_dict(args_dict) def init_from_dict(self, args_dict): """Constructs a `BertConfig` from a Python dictionary of parameters.""" for key in self.keys: setattr(self, key, args_dict[key]) def init_from_flags(self, flags): for key in self.keys: setattr(self, key, getattr(flags, key)) def init_from_json(self, json_path): with tf.io.gfile.GFile(json_path) as f: json_data = json.load(f) self.init_from_dict(json_data) def to_json(self, json_path): """Save XLNetConfig to a json file.""" json_data = {} for key in self.keys: json_data[key] = getattr(self, key) json_dir = os.path.dirname(json_path) if not tf.io.gfile.exists(json_dir): tf.io.gfile.makedirs(json_dir) with tf.io.gfile.GFile(json_path, 'w') as f: json.dump(json_data, f, indent=4, sort_keys=True) class RunConfig(object): """Class of RunConfig. RunConfig contains hyperparameters that could be different between pretraining and finetuning. These hyperparameters can also be changed from run to run. We store them separately from XLNetConfig for flexibility. """ def __init__(self, is_training, use_tpu, dropout, dropout_att, init_method='normal', init_range=0.1, init_std=0.02, mem_len=None, reuse_len=None, bi_data=False, clamp_len=-1, same_length=False, use_cls_mask=True): """Initializes RunConfig. Args: is_training: bool, whether in training mode. use_tpu: bool, whether TPUs are used. dropout: float, dropout rate. dropout_att: float, dropout rate on attention probabilities. init_method: str, the initialization scheme, either "normal" or "uniform". init_range: float, initialize the parameters with a uniform distribution in [-init_range, init_range]. Only effective when init="uniform". init_std: float, initialize the parameters with a normal distribution with mean 0 and stddev init_std. Only effective when init="normal". mem_len: int, the number of tokens to cache. reuse_len: int, the number of tokens in the currect batch to be cached and reused in the future. bi_data: bool, whether to use bidirectional input pipeline. Usually set to True during pretraining and False during finetuning. clamp_len: int, clamp all relative distances larger than clamp_len. -1 means no clamping. same_length: bool, whether to use the same attention length for each token. use_cls_mask: bool, whether to introduce cls mask. """ self.init_method = init_method self.init_range = init_range self.init_std = init_std self.is_training = is_training self.dropout = dropout self.dropout_att = dropout_att self.use_tpu = use_tpu self.mem_len = mem_len self.reuse_len = reuse_len self.bi_data = bi_data self.clamp_len = clamp_len self.same_length = same_length self.use_cls_mask = use_cls_mask
5,894
31.75
80
py
models
models-master/official/legacy/xlnet/data_utils.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Utilities used for data preparation.""" import collections import json import os from absl import logging import numpy as np import tensorflow as tf special_symbols = { "<unk>": 0, "<s>": 1, "</s>": 2, "<cls>": 3, "<sep>": 4, "<pad>": 5, "<mask>": 6, "<eod>": 7, "<eop>": 8, } VOCAB_SIZE = 32000 UNK_ID = special_symbols["<unk>"] CLS_ID = special_symbols["<cls>"] SEP_ID = special_symbols["<sep>"] MASK_ID = special_symbols["<mask>"] EOD_ID = special_symbols["<eod>"] SEG_ID_P = 0 SEG_ID_Q = 1 SEG_ID_CLS = 2 SEG_ID_PAD = 3 OnlineMaskingConfig = collections.namedtuple("OnlineMaskingConfig", [ "sample_strategy", "max_num_tokens", "min_num_tokens", "max_num_words", "min_num_words" ]) def file_based_input_fn_builder(input_file, name_to_features, batch_size, is_training): """Creates an `input_fn` closure.""" logging.info("Input tfrecord file %s", input_file) def _decode_record(record, name_to_features): """Decodes a record to a TensorFlow example.""" example = tf.io.parse_single_example(record, name_to_features) # tf.Example only supports tf.int64, but the TPU only supports tf.int32. # So cast all int64 to int32. for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.cast(t, tf.int32) example[name] = t return example def input_fn(): """Returns dataset for training/evaluation.""" num_threads = 8 if isinstance(input_file, str): d = tf.data.TFRecordDataset(input_file) # For training, we want a lot of parallel reading and shuffling. # For eval, we want no shuffling and parallel reading doesn't matter. if is_training: d = d.shuffle(2048) d = d.repeat() else: cycle_length = min(num_threads, len(input_file)) d = tf.data.Dataset.from_tensor_slices(input_file) # file level shuffle d = d.shuffle(len(input_file)).repeat() d = d.interleave( tf.data.TFRecordDataset, cycle_length=cycle_length) if is_training: # sample level shuffle d = d.shuffle(buffer_size=2048) d = d.map( lambda record: _decode_record(record, name_to_features), num_parallel_calls=tf.data.experimental.AUTOTUNE) d = d.batch(batch_size, drop_remainder=is_training) # When `input_file` is a path to a single file or a list # containing a single path, disable auto sharding so that # same input file is sent to all workers. if isinstance(input_file, str) or len(input_file) == 1: options = tf.data.Options() options.experimental_distribute.auto_shard_policy = ( tf.data.experimental.AutoShardPolicy.OFF) d = d.with_options(options) d = d.prefetch(tf.data.experimental.AUTOTUNE) return d return input_fn def create_classification_dataset(file_path, seq_length, batch_size, is_training): """Creates input dataset from (tf)records files for pretraining.""" name_to_features = { "input_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "input_mask": tf.io.FixedLenFeature([seq_length], tf.float32), "segment_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "label_ids": tf.io.FixedLenFeature([], tf.int64), "is_real_example": tf.io.FixedLenFeature([], tf.int64), } input_fn = file_based_input_fn_builder(file_path, name_to_features, batch_size, is_training) dataset = input_fn() return dataset def create_squad_dataset(file_path, seq_length, batch_size, is_training): """Creates input dataset from (tf)records files for pretraining.""" name_to_features = { "unique_ids": tf.io.FixedLenFeature([], tf.int64), "input_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "input_mask": tf.io.FixedLenFeature([seq_length], tf.float32), "segment_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "cls_index": tf.io.FixedLenFeature([], tf.int64), "p_mask": tf.io.FixedLenFeature([seq_length], tf.float32) } if is_training: name_to_features["start_positions"] = tf.io.FixedLenFeature([], tf.int64) name_to_features["end_positions"] = tf.io.FixedLenFeature([], tf.int64) name_to_features["is_impossible"] = tf.io.FixedLenFeature([], tf.float32) input_fn = file_based_input_fn_builder(file_path, name_to_features, batch_size, is_training) dataset = input_fn() return dataset def get_input_iterator(input_fn, strategy): """Returns distributed dataset iterator.""" # When training with TPU pods, datasets needs to be cloned across # workers. Since Dataset instance cannot be cloned in eager mode, we instead # pass callable that returns a dataset. input_data = input_fn() if callable(input_data): iterator = iter(strategy.distribute_datasets_from_function(input_data)) else: iterator = iter(strategy.experimental_distribute_dataset(input_data)) return iterator def get_classification_input_data(batch_size, seq_len, strategy, is_training, file_path): """Returns input dataset from input file string.""" # When using TPU pods, we need to clone dataset across # workers and need to pass in function that returns the dataset rather # than passing dataset instance itself. use_dataset_fn = isinstance(strategy, tf.distribute.TPUStrategy) if use_dataset_fn: if batch_size % strategy.num_replicas_in_sync != 0: raise ValueError( "Batch size must be divisible by number of replicas : {}".format( strategy.num_replicas_in_sync)) # As auto rebatching is not supported in # `distribute_datasets_from_function()` API, which is # required when cloning dataset to multiple workers in eager mode, # we use per-replica batch size. batch_size = int(batch_size / strategy.num_replicas_in_sync) def _dataset_fn(ctx=None): del ctx train_dataset = create_classification_dataset( file_path=file_path, seq_length=seq_len, batch_size=batch_size, is_training=is_training) return train_dataset return _dataset_fn if use_dataset_fn else _dataset_fn() def get_squad_input_data(batch_size, seq_len, q_len, strategy, is_training, file_path): """Returns input dataset from input file string.""" # When using TPU pods, we need to clone dataset across # workers and need to pass in function that returns the dataset rather # than passing dataset instance itself. use_dataset_fn = isinstance(strategy, tf.distribute.TPUStrategy) if use_dataset_fn: if batch_size % strategy.num_replicas_in_sync != 0: raise ValueError( "Batch size must be divisible by number of replicas : {}".format( strategy.num_replicas_in_sync)) # As auto rebatching is not supported in # `distribute_datasets_from_function()` API, which is # required when cloning dataset to multiple workers in eager mode, # we use per-replica batch size. batch_size = int(batch_size / strategy.num_replicas_in_sync) if is_training: input_glob = os.path.join( file_path, "spiece.model.*.slen-{}.qlen-{}.train.tf_record".format(seq_len, q_len)) global_input_paths = tf.io.gfile.glob(input_glob) else: global_input_paths = file_path def _dataset_fn(ctx=None): del ctx train_dataset = create_squad_dataset( file_path=global_input_paths, seq_length=seq_len, batch_size=batch_size, is_training=is_training) return train_dataset return _dataset_fn if use_dataset_fn else _dataset_fn() def _idx_pair_to_mask(beg_indices, end_indices, inputs, tgt_len, num_predict): """Turn beg and end indices into actual mask.""" non_func_mask = tf.logical_and( tf.not_equal(inputs, SEP_ID), tf.not_equal(inputs, CLS_ID)) all_indices = tf.where(non_func_mask, tf.range(tgt_len, dtype=tf.int64), tf.constant(-1, shape=[tgt_len], dtype=tf.int64)) candidate_matrix = tf.cast( tf.logical_and(all_indices[None, :] >= beg_indices[:, None], all_indices[None, :] < end_indices[:, None]), tf.float32) cumsum_matrix = tf.reshape( tf.cumsum(tf.reshape(candidate_matrix, [-1])), [-1, tgt_len]) masked_matrix = tf.cast(cumsum_matrix <= num_predict, tf.float32) target_mask = tf.reduce_sum(candidate_matrix * masked_matrix, axis=0) is_masked = tf.cast(target_mask, tf.bool) return is_masked, target_mask def _word_span_mask(inputs, tgt_len, num_predict, min_num_words, max_num_words, boundary): """Sample whole word spans as prediction targets.""" # Note: 1.2 is the token-to-word ratio mask_alpha = tgt_len / num_predict / 1.2 round_to_int = lambda x: tf.cast(tf.round(x), tf.int64) # Sample span lengths from a zipf distribution span_len_seq = np.arange(min_num_words, max_num_words + 1) probs = np.array([1.0 / (i + 1) for i in span_len_seq]) probs /= np.sum(probs) logits = tf.constant(np.log(probs), dtype=tf.float32) # Sample `num_predict` words here: note that this is over sampling span_lens = tf.random.categorical( logits=logits[None], num_samples=num_predict, dtype=tf.int64, )[0] + min_num_words # Sample the ratio [0.0, 1.0) of left context lengths span_lens_float = tf.cast(span_lens, tf.float32) left_ratio = tf.random.uniform(shape=[num_predict], minval=0.0, maxval=1.0) left_ctx_len = left_ratio * span_lens_float * (mask_alpha - 1) left_ctx_len = round_to_int(left_ctx_len) right_offset = round_to_int(span_lens_float * mask_alpha) - left_ctx_len beg_indices = ( tf.cumsum(left_ctx_len) + tf.cumsum(right_offset, exclusive=True)) end_indices = beg_indices + span_lens # Remove out of range indices max_boundary_index = tf.cast(tf.shape(boundary)[0] - 1, tf.int64) valid_idx_mask = end_indices < max_boundary_index beg_indices = tf.boolean_mask(beg_indices, valid_idx_mask) end_indices = tf.boolean_mask(end_indices, valid_idx_mask) beg_indices = tf.gather(boundary, beg_indices) end_indices = tf.gather(boundary, end_indices) # Shuffle valid indices num_valid = tf.cast(tf.shape(beg_indices)[0], tf.int64) order = tf.random.shuffle(tf.range(num_valid, dtype=tf.int64)) beg_indices = tf.gather(beg_indices, order) end_indices = tf.gather(end_indices, order) return _idx_pair_to_mask(beg_indices, end_indices, inputs, tgt_len, num_predict) def _token_span_mask(inputs, tgt_len, num_predict, min_num_tokens, max_num_tokens): """Sample token spans as prediction targets.""" mask_alpha = tgt_len / num_predict round_to_int = lambda x: tf.cast(tf.round(x), tf.int64) # Sample span lengths from a zipf distribution span_len_seq = np.arange(min_num_tokens, max_num_tokens + 1) probs = np.array([1.0 / (i + 1) for i in span_len_seq]) probs /= np.sum(probs) logits = tf.constant(np.log(probs), dtype=tf.float32) span_lens = tf.random.categorical( logits=logits[None], num_samples=num_predict, dtype=tf.int64, )[0] + min_num_tokens # Sample the ratio [0.0, 1.0) of left context lengths span_lens_float = tf.cast(span_lens, tf.float32) left_ratio = tf.random.uniform(shape=[num_predict], minval=0.0, maxval=1.0) left_ctx_len = left_ratio * span_lens_float * (mask_alpha - 1) left_ctx_len = round_to_int(left_ctx_len) # Compute the offset from left start to the right end right_offset = round_to_int(span_lens_float * mask_alpha) - left_ctx_len # Get the actual begin and end indices beg_indices = ( tf.cumsum(left_ctx_len) + tf.cumsum(right_offset, exclusive=True)) end_indices = beg_indices + span_lens # Remove out of range indices valid_idx_mask = end_indices < tgt_len beg_indices = tf.boolean_mask(beg_indices, valid_idx_mask) end_indices = tf.boolean_mask(end_indices, valid_idx_mask) # Shuffle valid indices num_valid = tf.cast(tf.shape(beg_indices)[0], tf.int64) order = tf.random.shuffle(tf.range(num_valid, dtype=tf.int64)) beg_indices = tf.gather(beg_indices, order) end_indices = tf.gather(end_indices, order) return _idx_pair_to_mask(beg_indices, end_indices, inputs, tgt_len, num_predict) def _whole_word_mask(inputs, tgt_len, num_predict, boundary): """Sample whole words as prediction targets.""" pair_indices = tf.concat([boundary[:-1, None], boundary[1:, None]], axis=1) cand_pair_indices = tf.random.shuffle(pair_indices)[:num_predict] beg_indices = cand_pair_indices[:, 0] end_indices = cand_pair_indices[:, 1] return _idx_pair_to_mask(beg_indices, end_indices, inputs, tgt_len, num_predict) def _single_token_mask(inputs, tgt_len, num_predict): """Sample individual tokens as prediction targets.""" all_indices = tf.range(tgt_len, dtype=tf.int64) non_func_mask = tf.logical_and( tf.not_equal(inputs, SEP_ID), tf.not_equal(inputs, CLS_ID)) non_func_indices = tf.boolean_mask(all_indices, non_func_mask) masked_pos = tf.random.shuffle(non_func_indices) masked_pos = tf.sort(masked_pos[:num_predict]) target_mask = tf.sparse_to_dense( sparse_indices=masked_pos, output_shape=[tgt_len], sparse_values=1.0, default_value=0.0) is_masked = tf.cast(target_mask, tf.bool) return is_masked, target_mask def _online_sample_masks(inputs, tgt_len, num_predict, online_masking_config, boundary=None): """Sample target positions to predict.""" logging.info("Online sample with strategy: `%s`.", online_masking_config.sample_strategy) if online_masking_config.sample_strategy == "single_token": return _single_token_mask(inputs, tgt_len, num_predict) elif online_masking_config.sample_strategy == "whole_word": assert boundary is not None, "whole word sampling requires `boundary`" return _whole_word_mask(inputs, tgt_len, num_predict, boundary) elif online_masking_config.sample_strategy == "token_span": return _token_span_mask(inputs, tgt_len, num_predict, online_masking_config.min_num_tokens, online_masking_config.max_num_tokens) elif online_masking_config.sample_strategy == "word_span": assert boundary is not None, "word span sampling requires `boundary`" return _word_span_mask(inputs, tgt_len, num_predict, online_masking_config.min_num_words, online_masking_config.max_num_words, boundary) else: raise NotImplementedError def create_pretrain_dataset(file_names, bsz_per_core, seq_len, reuse_len, perm_size, leak_ratio, online_masking_config, num_predict=None, input_pipeline_context=None): """Creates pretrain dataset.""" def parser(record): """Function used to parse tfrecord.""" record_spec = { "input": tf.io.FixedLenFeature([seq_len], tf.int64), "seg_id": tf.io.FixedLenFeature([seq_len], tf.int64), "label": tf.io.FixedLenFeature([1], tf.int64), } if online_masking_config.sample_strategy in ["whole_word", "word_span"]: logging.info("Add `boundary` spec for %s", online_masking_config.sample_strategy) record_spec["boundary"] = tf.io.VarLenFeature(tf.int64) # retrieve serialized example example = tf.io.parse_single_example( serialized=record, features=record_spec) inputs = example.pop("input") if online_masking_config.sample_strategy in ["whole_word", "word_span"]: boundary = tf.sparse.to_dense(example.pop("boundary")) else: boundary = None is_masked, _ = _online_sample_masks( inputs, seq_len, num_predict, online_masking_config, boundary=boundary) if reuse_len > 0: ##### Use memory # permutate the reuse and non-reuse parts separately non_reuse_len = seq_len - reuse_len assert reuse_len % perm_size == 0 and non_reuse_len % perm_size == 0 # Creates permutation mask and target mask for the first reuse_len tokens. # The tokens in this part are reused from the last sequence. perm_mask_0, target_mask_0, input_k_0, input_q_0 = _local_perm( inputs[:reuse_len], is_masked[:reuse_len], perm_size, reuse_len, leak_ratio) # Creates permutation mask and target mask for the rest of tokens in # current example, which are concatentation of two new segments. perm_mask_1, target_mask_1, input_k_1, input_q_1 = _local_perm( inputs[reuse_len:], is_masked[reuse_len:], perm_size, non_reuse_len, leak_ratio) perm_mask_0 = tf.concat( [perm_mask_0, tf.ones([reuse_len, non_reuse_len])], axis=1) perm_mask_1 = tf.concat( [tf.zeros([non_reuse_len, reuse_len]), perm_mask_1], axis=1) perm_mask = tf.concat([perm_mask_0, perm_mask_1], axis=0) target_mask = tf.concat([target_mask_0, target_mask_1], axis=0) input_k = tf.concat([input_k_0, input_k_1], axis=0) input_q = tf.concat([input_q_0, input_q_1], axis=0) else: ##### Do not use memory assert seq_len % perm_size == 0 # permutate the entire sequence together perm_mask, target_mask, input_k, input_q = _local_perm( inputs, is_masked, perm_size, seq_len, leak_ratio) # reshape back to fixed shape example["perm_mask"] = tf.reshape(perm_mask, [seq_len, seq_len]) example["input_ids"] = tf.reshape(input_k, [seq_len]) example["input_q"] = tf.reshape(input_q, [seq_len]) # Directly use raw inputs as the target target = inputs if num_predict is not None: indices = tf.range(seq_len, dtype=tf.int64) bool_target_mask = tf.cast(target_mask, tf.bool) indices = tf.boolean_mask(indices, bool_target_mask) ##### extra padding due to CLS/SEP introduced after prepro actual_num_predict = tf.shape(indices)[0] pad_len = num_predict - actual_num_predict ##### target_mapping target_mapping = tf.one_hot(indices, seq_len, dtype=tf.float32) paddings = tf.zeros([pad_len, seq_len], dtype=target_mapping.dtype) target_mapping = tf.concat([target_mapping, paddings], axis=0) example["target_mapping"] = tf.reshape(target_mapping, [num_predict, seq_len]) ##### target target = tf.boolean_mask(target, bool_target_mask) paddings = tf.zeros([pad_len], dtype=target.dtype) target = tf.concat([target, paddings], axis=0) example["target"] = tf.reshape(target, [num_predict]) ##### target mask target_mask = tf.concat([ tf.ones([actual_num_predict], dtype=tf.float32), tf.zeros([pad_len], dtype=tf.float32) ], axis=0) example["target_mask"] = tf.reshape(target_mask, [num_predict]) else: example["target"] = tf.reshape(target, [seq_len]) example["target_mask"] = tf.reshape(target_mask, [seq_len]) for key in list(example.keys()): val = example[key] if tf.keras.backend.is_sparse(val): val = tf.sparse.to_dense(val) if val.dtype == tf.int64: val = tf.cast(val, tf.int32) example[key] = val for k, v in example.items(): logging.info("%s: %s", k, v) return example dataset = parse_files_to_dataset( parser=parser, file_paths=file_names, bsz_per_core=bsz_per_core, sequential=reuse_len > 0, input_pipeline_context=input_pipeline_context) return dataset def format_filename(prefix, suffix, bsz_per_host, seq_len, reuse_len=None, uncased=False): """Generates input file name pattern.""" if reuse_len is not None and reuse_len > 0: reuse_str = "reuse-{}.".format(reuse_len) bsz_str = "hostbsz-{}.".format(bsz_per_host) else: reuse_str = "" bsz_str = "" if not uncased: case_str = "" else: case_str = "uncased." file_name = "{}.seq-{}.{}{}{}{}".format(prefix, seq_len, reuse_str, bsz_str, case_str, suffix) return file_name def get_pretrain_input_data(batch_size, seq_len, strategy, file_path, reuse_len, perm_size, leak_ratio, num_predict, uncased, online_masking_config, num_hosts=1): """Returns input dataset from input file string.""" # When using TPU pods, we need to clone dataset across # workers and need to pass in function that returns the dataset rather # than passing dataset instance itself. use_dataset_fn = isinstance(strategy, tf.distribute.TPUStrategy) split = "train" bsz_per_host = int(batch_size / num_hosts) record_glob_base = format_filename( prefix="meta.{}.pass-*".format(split), suffix="json*", bsz_per_host=bsz_per_host, seq_len=seq_len, reuse_len=reuse_len, uncased=uncased) def _get_num_batch(info): if "num_batch" in info: return info["num_batch"] elif "num_example" in info: return info["num_example"] / bsz_per_host else: raise ValueError("Do not have sample info.") if use_dataset_fn: if batch_size % strategy.num_replicas_in_sync != 0: raise ValueError( "Batch size must be divisible by number of replicas : {}".format( strategy.num_replicas_in_sync)) # As auto rebatching is not supported in # `distribute_datasets_from_function()` API, which is # required when cloning dataset to multiple workers in eager mode, # we use per-replica batch size. batch_size = int(batch_size / strategy.num_replicas_in_sync) record_info = {"num_batch": 0, "filenames": []} tfrecord_dirs = file_path.split(",") logging.info("Use the following tfrecord dirs: %s", tfrecord_dirs) for idx, record_dir in enumerate(tfrecord_dirs): record_glob = os.path.join(record_dir, record_glob_base) logging.info("[%d] Record glob: %s", idx, record_glob) record_paths = sorted(tf.io.gfile.glob(record_glob)) logging.info("[%d] Num of record info path: %d", idx, len(record_paths)) cur_record_info = {"num_batch": 0, "filenames": []} for record_info_path in record_paths: with tf.io.gfile.GFile(record_info_path, "r") as fp: info = json.load(fp) cur_record_info["num_batch"] += int(_get_num_batch(info)) cur_record_info["filenames"] += info["filenames"] # overwrite directory for `cur_record_info` new_filenames = [] for filename in cur_record_info["filenames"]: basename = os.path.basename(filename) new_filename = os.path.join(record_dir, basename) new_filenames.append(new_filename) cur_record_info["filenames"] = new_filenames logging.info("[Dir %d] Number of chosen batches: %s", idx, cur_record_info["num_batch"]) logging.info("[Dir %d] Number of chosen files: %s", idx, len(cur_record_info["filenames"])) logging.info(cur_record_info["filenames"]) # add `cur_record_info` to global `record_info` record_info["num_batch"] += cur_record_info["num_batch"] record_info["filenames"] += cur_record_info["filenames"] logging.info("Total number of batches: %d", record_info["num_batch"]) logging.info("Total number of files: %d", len(record_info["filenames"])) logging.info(record_info["filenames"]) def _dataset_fn(ctx=None): """Function that can create a pretrain dataset.""" train_dataset = create_pretrain_dataset( file_names=record_info["filenames"], bsz_per_core=batch_size, seq_len=seq_len, reuse_len=reuse_len, perm_size=perm_size, leak_ratio=leak_ratio, online_masking_config=online_masking_config, num_predict=num_predict, input_pipeline_context=ctx) return train_dataset return _dataset_fn if use_dataset_fn else _dataset_fn() def parse_files_to_dataset(parser, file_paths, bsz_per_core, sequential, input_pipeline_context=None): """Creates the dataset given file paths.""" dataset = tf.data.Dataset.from_tensor_slices(file_paths) # Note: we cannot perform sample-level shuffle here because this will violate # the consecutive requirement of data stream. if input_pipeline_context and input_pipeline_context.num_input_pipelines > 1: dataset = dataset.shard(input_pipeline_context.num_input_pipelines, input_pipeline_context.input_pipeline_id) # file-level shuffle if len(file_paths) > 1: dataset = dataset.shuffle(len(file_paths)) if sequential: # Note: cannot perform sample-level shuffle here because this will violate # the consecutive requirement of data stream. dataset = tf.data.TFRecordDataset(dataset) else: # `cycle_length` is the number of parallel files that get read. cycle_length = min(8, len(file_paths)) logging.info("Interleave %d files", cycle_length) dataset = dataset.apply( tf.data.experimental.parallel_interleave( tf.data.TFRecordDataset, cycle_length=cycle_length)) buffer_size = 2048 logging.info("Perform sample-level shuffle with size %d", buffer_size) dataset = dataset.shuffle(buffer_size=buffer_size) dataset = dataset.cache().repeat().map(parser) dataset = dataset.batch(bsz_per_core, drop_remainder=True) dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE) return dataset def _local_perm(inputs, is_masked, perm_size, seq_len, leak_ratio): """Samples a permutation of the factorization order. Creates perm_mask and target_mask accordingly. Args: inputs: int64 Tensor in shape [seq_len], input ids. is_masked: bool Tensor in shape [seq_len]. True means being selected for partial prediction. perm_size: the length of longest permutation. Could be set to be reuse_len. Should not be larger than reuse_len or there will be data leaks. seq_len: int, sequence length. leak_ratio: float, percent of masked tokens that are leaked. Returns: perm_mask: float32 Tensor in shape [seq_len, seq_len] consisted of 0 and 1. If perm_mask[i][j] == 1, it means the ith token (in original order) cannot attend to the jth token (in original order). This case will happen only when the ith token's permutated position <= the jth token's permutated position, and the jth token is masked or is func token. If perm_mask[i][j] == 0, it means the ith token (in original order) can attend to the jth token (in original order). Note that non-masked tokens can be attended by all other tokens, which is different from the description in original paper. target_mask: float32 Tensor in shape [seq_len] consisted of 0 and 1. If target_mask[i] == 1, the ith token needs to be predicted and mask will be used as input. This token will count for loss. If target_mask[i] == 0, token (or [SEP], [CLS]) will be used as input. This token will not count for loss. inputs_k: int64 Tensor in shape [seq_len], input ids. inputs_q: float32 Tensor in shape [seq_len], the same as target_mask. """ # Generate permutation indices index = tf.range(seq_len, dtype=tf.int64) index = tf.transpose(tf.reshape(index, [-1, perm_size])) index = tf.random.shuffle(index) index = tf.reshape(tf.transpose(index), [-1]) # non-functional tokens non_func_tokens = tf.logical_not( tf.logical_or(tf.equal(inputs, SEP_ID), tf.equal(inputs, CLS_ID))) masked_tokens = tf.logical_and(is_masked, non_func_tokens) non_masked_or_func_tokens = tf.logical_not(masked_tokens) smallest_index = -2 * tf.ones([seq_len], dtype=tf.int64) # Similar to BERT, randomly leak some masked tokens if leak_ratio > 0: leak_tokens = tf.logical_and( masked_tokens, tf.random.uniform([seq_len], maxval=1.0) < leak_ratio) can_attend_self = tf.logical_or(non_masked_or_func_tokens, leak_tokens) else: can_attend_self = non_masked_or_func_tokens to_index = tf.where(can_attend_self, smallest_index, index) from_index = tf.where(can_attend_self, to_index + 1, to_index) # For masked tokens, can attend if i > j # For context tokens, always can attend each other can_attend = from_index[:, None] > to_index[None, :] # In modeling, 1 indicates cannot attend. Hence, reverse the value here. perm_mask = 1.0 - tf.cast(can_attend, tf.float32) # Only masked tokens are included in the loss target_mask = tf.cast(masked_tokens, tf.float32) # construct inputs_k inputs_k = inputs # construct inputs_q inputs_q = masked_tokens return perm_mask, target_mask, inputs_k, inputs_q
30,095
36.386335
80
py
models
models-master/official/legacy/xlnet/preprocess_classification_data.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Script to pre-process classification data into tfrecords.""" import collections import csv import os # Import libraries from absl import app from absl import flags from absl import logging import numpy as np import tensorflow as tf import sentencepiece as spm from official.legacy.xlnet import classifier_utils from official.legacy.xlnet import preprocess_utils flags.DEFINE_bool( "overwrite_data", default=False, help="If False, will use cached data if available.") flags.DEFINE_string("output_dir", default="", help="Output dir for TF records.") flags.DEFINE_string( "spiece_model_file", default="", help="Sentence Piece model path.") flags.DEFINE_string("data_dir", default="", help="Directory for input data.") # task specific flags.DEFINE_string("eval_split", default="dev", help="could be dev or test") flags.DEFINE_string("task_name", default=None, help="Task name") flags.DEFINE_integer( "eval_batch_size", default=64, help="batch size for evaluation") flags.DEFINE_integer("max_seq_length", default=128, help="Max sequence length") flags.DEFINE_integer( "num_passes", default=1, help="Num passes for processing training data. " "This is use to batch data without loss for TPUs.") flags.DEFINE_bool("uncased", default=False, help="Use uncased.") flags.DEFINE_bool( "is_regression", default=False, help="Whether it's a regression task.") flags.DEFINE_bool( "use_bert_format", default=False, help="Whether to use BERT format to arrange input data.") FLAGS = flags.FLAGS class InputExample(object): """A single training/test example for simple sequence classification.""" def __init__(self, guid, text_a, text_b=None, label=None): """Constructs a InputExample. Args: guid: Unique id for the example. text_a: string. The untokenized text of the first sequence. For single sequence tasks, only this sequence must be specified. text_b: (Optional) string. The untokenized text of the second sequence. Only must be specified for sequence pair tasks. label: (Optional) string. The label of the example. This should be specified for train and dev examples, but not for test examples. """ self.guid = guid self.text_a = text_a self.text_b = text_b self.label = label class DataProcessor(object): """Base class for data converters for sequence classification data sets.""" def get_train_examples(self, data_dir): """Gets a collection of `InputExample`s for the train set.""" raise NotImplementedError() def get_dev_examples(self, data_dir): """Gets a collection of `InputExample`s for the dev set.""" raise NotImplementedError() def get_test_examples(self, data_dir): """Gets a collection of `InputExample`s for prediction.""" raise NotImplementedError() def get_labels(self): """Gets the list of labels for this data set.""" raise NotImplementedError() @classmethod def _read_tsv(cls, input_file, quotechar=None): """Reads a tab separated value file.""" with tf.io.gfile.GFile(input_file, "r") as f: reader = csv.reader(f, delimiter="\t", quotechar=quotechar) lines = [] for line in reader: # pylint: disable=g-explicit-length-test if len(line) == 0: continue lines.append(line) return lines class GLUEProcessor(DataProcessor): """GLUEProcessor.""" def __init__(self): self.train_file = "train.tsv" self.dev_file = "dev.tsv" self.test_file = "test.tsv" self.label_column = None self.text_a_column = None self.text_b_column = None self.contains_header = True self.test_text_a_column = None self.test_text_b_column = None self.test_contains_header = True def get_train_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, self.train_file)), "train") def get_dev_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, self.dev_file)), "dev") def get_test_examples(self, data_dir): """See base class.""" if self.test_text_a_column is None: self.test_text_a_column = self.text_a_column if self.test_text_b_column is None: self.test_text_b_column = self.text_b_column return self._create_examples( self._read_tsv(os.path.join(data_dir, self.test_file)), "test") def get_labels(self): """See base class.""" return ["0", "1"] def _create_examples(self, lines, set_type): """Creates examples for the training and dev sets.""" examples = [] for (i, line) in enumerate(lines): if i == 0 and self.contains_header and set_type != "test": continue if i == 0 and self.test_contains_header and set_type == "test": continue guid = "%s-%s" % (set_type, i) a_column = ( self.text_a_column if set_type != "test" else self.test_text_a_column) b_column = ( self.text_b_column if set_type != "test" else self.test_text_b_column) # there are some incomplete lines in QNLI if len(line) <= a_column: logging.warning("Incomplete line, ignored.") continue text_a = line[a_column] if b_column is not None: if len(line) <= b_column: logging.warning("Incomplete line, ignored.") continue text_b = line[b_column] else: text_b = None if set_type == "test": label = self.get_labels()[0] else: if len(line) <= self.label_column: logging.warning("Incomplete line, ignored.") continue label = line[self.label_column] examples.append( InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label)) return examples class Yelp5Processor(DataProcessor): """Yelp5Processor.""" def get_train_examples(self, data_dir): return self._create_examples(os.path.join(data_dir, "train.csv")) def get_dev_examples(self, data_dir): return self._create_examples(os.path.join(data_dir, "test.csv")) def get_labels(self): """See base class.""" return ["1", "2", "3", "4", "5"] def _create_examples(self, input_file): """Creates examples for the training and dev sets.""" examples = [] with tf.io.gfile.GFile(input_file) as f: reader = csv.reader(f) for i, line in enumerate(reader): label = line[0] text_a = line[1].replace('""', '"').replace('\\"', '"') examples.append( InputExample(guid=str(i), text_a=text_a, text_b=None, label=label)) return examples class ImdbProcessor(DataProcessor): """ImdbProcessor.""" def get_labels(self): return ["neg", "pos"] def get_train_examples(self, data_dir): return self._create_examples(os.path.join(data_dir, "train")) def get_dev_examples(self, data_dir): return self._create_examples(os.path.join(data_dir, "test")) def _create_examples(self, data_dir): """Creates examples.""" examples = [] for label in ["neg", "pos"]: cur_dir = os.path.join(data_dir, label) for filename in tf.io.gfile.listdir(cur_dir): if not filename.endswith("txt"): continue if len(examples) % 1000 == 0: logging.info("Loading dev example %d", len(examples)) path = os.path.join(cur_dir, filename) with tf.io.gfile.GFile(path) as f: text = f.read().strip().replace("<br />", " ") examples.append( InputExample( guid="unused_id", text_a=text, text_b=None, label=label)) return examples class MnliMatchedProcessor(GLUEProcessor): """MnliMatchedProcessor.""" def __init__(self): super(MnliMatchedProcessor, self).__init__() self.dev_file = "dev_matched.tsv" self.test_file = "test_matched.tsv" self.label_column = -1 self.text_a_column = 8 self.text_b_column = 9 def get_labels(self): return ["contradiction", "entailment", "neutral"] class MnliMismatchedProcessor(MnliMatchedProcessor): def __init__(self): super(MnliMismatchedProcessor, self).__init__() self.dev_file = "dev_mismatched.tsv" self.test_file = "test_mismatched.tsv" class StsbProcessor(GLUEProcessor): """StsbProcessor.""" def __init__(self): super(StsbProcessor, self).__init__() self.label_column = 9 self.text_a_column = 7 self.text_b_column = 8 def get_labels(self): return [0.0] def _create_examples(self, lines, set_type): """Creates examples for the training and dev sets.""" examples = [] for (i, line) in enumerate(lines): if i == 0 and self.contains_header and set_type != "test": continue if i == 0 and self.test_contains_header and set_type == "test": continue guid = "%s-%s" % (set_type, i) a_column = ( self.text_a_column if set_type != "test" else self.test_text_a_column) b_column = ( self.text_b_column if set_type != "test" else self.test_text_b_column) # there are some incomplete lines in QNLI if len(line) <= a_column: logging.warning("Incomplete line, ignored.") continue text_a = line[a_column] if b_column is not None: if len(line) <= b_column: logging.warning("Incomplete line, ignored.") continue text_b = line[b_column] else: text_b = None if set_type == "test": label = self.get_labels()[0] else: if len(line) <= self.label_column: logging.warning("Incomplete line, ignored.") continue label = float(line[self.label_column]) examples.append( InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label)) return examples def file_based_convert_examples_to_features(examples, label_list, max_seq_length, tokenize_fn, output_file, num_passes=1): """Convert a set of `InputExample`s to a TFRecord file.""" # do not create duplicated records if tf.io.gfile.exists(output_file) and not FLAGS.overwrite_data: logging.info("Do not overwrite tfrecord %s exists.", output_file) return logging.info("Create new tfrecord %s.", output_file) writer = tf.io.TFRecordWriter(output_file) examples *= num_passes for (ex_index, example) in enumerate(examples): if ex_index % 10000 == 0: logging.info("Writing example %d of %d", ex_index, len(examples)) feature = classifier_utils.convert_single_example(ex_index, example, label_list, max_seq_length, tokenize_fn, FLAGS.use_bert_format) def create_int_feature(values): f = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values))) return f def create_float_feature(values): f = tf.train.Feature(float_list=tf.train.FloatList(value=list(values))) return f features = collections.OrderedDict() features["input_ids"] = create_int_feature(feature.input_ids) features["input_mask"] = create_float_feature(feature.input_mask) features["segment_ids"] = create_int_feature(feature.segment_ids) if label_list is not None: features["label_ids"] = create_int_feature([feature.label_id]) else: features["label_ids"] = create_float_feature([float(feature.label_id)]) features["is_real_example"] = create_int_feature( [int(feature.is_real_example)]) tf_example = tf.train.Example(features=tf.train.Features(feature=features)) writer.write(tf_example.SerializeToString()) writer.close() def main(_): logging.set_verbosity(logging.INFO) processors = { "mnli_matched": MnliMatchedProcessor, "mnli_mismatched": MnliMismatchedProcessor, "sts-b": StsbProcessor, "imdb": ImdbProcessor, "yelp5": Yelp5Processor } task_name = FLAGS.task_name.lower() if task_name not in processors: raise ValueError("Task not found: %s" % (task_name)) processor = processors[task_name]() label_list = processor.get_labels() if not FLAGS.is_regression else None sp = spm.SentencePieceProcessor() sp.Load(FLAGS.spiece_model_file) def tokenize_fn(text): text = preprocess_utils.preprocess_text(text, lower=FLAGS.uncased) return preprocess_utils.encode_ids(sp, text) spm_basename = os.path.basename(FLAGS.spiece_model_file) train_file_base = "{}.len-{}.train.tf_record".format(spm_basename, FLAGS.max_seq_length) train_file = os.path.join(FLAGS.output_dir, train_file_base) logging.info("Use tfrecord file %s", train_file) train_examples = processor.get_train_examples(FLAGS.data_dir) np.random.shuffle(train_examples) logging.info("Num of train samples: %d", len(train_examples)) file_based_convert_examples_to_features(train_examples, label_list, FLAGS.max_seq_length, tokenize_fn, train_file, FLAGS.num_passes) if FLAGS.eval_split == "dev": eval_examples = processor.get_dev_examples(FLAGS.data_dir) else: eval_examples = processor.get_test_examples(FLAGS.data_dir) logging.info("Num of eval samples: %d", len(eval_examples)) # TPU requires a fixed batch size for all batches, therefore the number # of examples must be a multiple of the batch size, or else examples # will get dropped. So we pad with fake examples which are ignored # later on. These do NOT count towards the metric (all tf.metrics # support a per-instance weight, and these get a weight of 0.0). # # Modified in XL: We also adopt the same mechanism for GPUs. while len(eval_examples) % FLAGS.eval_batch_size != 0: eval_examples.append(classifier_utils.PaddingInputExample()) eval_file_base = "{}.len-{}.{}.eval.tf_record".format(spm_basename, FLAGS.max_seq_length, FLAGS.eval_split) eval_file = os.path.join(FLAGS.output_dir, eval_file_base) file_based_convert_examples_to_features(eval_examples, label_list, FLAGS.max_seq_length, tokenize_fn, eval_file) if __name__ == "__main__": app.run(main)
15,344
32.651316
80
py
models
models-master/official/legacy/xlnet/preprocess_utils.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # coding=utf-8 """Utilities for pre-processing.""" import unicodedata import six SPIECE_UNDERLINE = '▁' def printable_text(text): """Returns text encoded in a way suitable for print or `tf.logging`.""" # These functions want `str` for both Python2 and Python3, but in one case # it's a Unicode string and in the other it's a byte string. if six.PY3: if isinstance(text, str): return text elif isinstance(text, bytes): return text.decode('utf-8', 'ignore') else: raise ValueError('Unsupported string type: %s' % (type(text))) elif six.PY2: if isinstance(text, str): return text elif isinstance(text, unicode): # pylint: disable=undefined-variable return text.encode('utf-8') else: raise ValueError('Unsupported string type: %s' % (type(text))) else: raise ValueError('Not running on Python2 or Python 3?') def print_(*args): new_args = [] for arg in args: if isinstance(arg, list): s = [printable_text(i) for i in arg] s = ' '.join(s) new_args.append(s) else: new_args.append(printable_text(arg)) print(*new_args) def preprocess_text(inputs, lower=False, remove_space=True, keep_accents=False): """Preprocesses texts.""" if remove_space: outputs = ' '.join(inputs.strip().split()) else: outputs = inputs outputs = outputs.replace('``', '"').replace("''", '"') if six.PY2 and isinstance(outputs, str): outputs = outputs.decode('utf-8') if not keep_accents: outputs = unicodedata.normalize('NFKD', outputs) outputs = ''.join([c for c in outputs if not unicodedata.combining(c)]) if lower: outputs = outputs.lower() return outputs def encode_pieces(sp_model, text, return_unicode=True, sample=False): """Encodes pieces.""" # return_unicode is used only for py2 if six.PY2 and isinstance(text, unicode): # pylint: disable=undefined-variable text = text.encode('utf-8') if not sample: pieces = sp_model.EncodeAsPieces(text) else: pieces = sp_model.SampleEncodeAsPieces(text, 64, 0.1) new_pieces = [] for piece in pieces: if len(piece) > 1 and piece[-1] == ',' and piece[-2].isdigit(): cur_pieces = sp_model.EncodeAsPieces(piece[:-1].replace( SPIECE_UNDERLINE, '')) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0]) == 1: cur_pieces = cur_pieces[1:] else: cur_pieces[0] = cur_pieces[0][1:] cur_pieces.append(piece[-1]) new_pieces.extend(cur_pieces) else: new_pieces.append(piece) # note(zhiliny): convert back to unicode for py2 if six.PY2 and return_unicode: ret_pieces = [] for piece in new_pieces: if isinstance(piece, str): piece = piece.decode('utf-8') ret_pieces.append(piece) new_pieces = ret_pieces return new_pieces def encode_ids(sp_model, text, sample=False): pieces = encode_pieces(sp_model, text, return_unicode=False, sample=sample) ids = [sp_model.PieceToId(piece) for piece in pieces] return ids
3,691
29.262295
81
py
models
models-master/official/legacy/xlnet/training_utils.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """XLNet training utils.""" import os import re from typing import Any, Callable, Dict, Optional, Text from absl import logging import tensorflow as tf from official.legacy.bert import model_training_utils from official.legacy.xlnet import data_utils # pytype: disable=attribute-error # pylint: disable=g-bare-generic,unused-import _MIN_SUMMARY_STEPS = 10 def _save_checkpoint(checkpoint, model_dir, checkpoint_prefix): """Saves model to with provided checkpoint prefix.""" checkpoint_path = os.path.join(model_dir, checkpoint_prefix) saved_path = checkpoint.save(checkpoint_path) logging.info("Saving model as TF checkpoint: %s", saved_path) return def _float_metric_value(metric): """Gets the value of a float-value keras metric.""" return metric.result().numpy().astype(float) def train( strategy: tf.distribute.Strategy, model_fn: Callable, input_meta_data: Dict, train_input_fn: Callable, total_training_steps: int, steps_per_loop: int, optimizer: tf.keras.optimizers.Optimizer, learning_rate_fn: tf.keras.optimizers.schedules.LearningRateSchedule, eval_fn: Optional[Callable[[tf.keras.Model, int, tf.summary.SummaryWriter], Any]] = None, metric_fn: Optional[Callable[[], tf.keras.metrics.Metric]] = None, init_checkpoint: Optional[Text] = None, init_from_transformerxl: Optional[bool] = False, model_dir: Optional[Text] = None, save_steps: Optional[int] = None, run_eagerly: Optional[bool] = False): """Runs customized training. Args: strategy: Distribution strategy on which to run low level training loop. model_fn: The function returns a keras.Model. input_meta_data: A dictionary of params: `mem_len`, `lr_layer_decay_rate`, `n_layer`, `batch_size_per_core` and `d_model`. train_input_fn: Function returns a tf.data.Dataset used for training. total_training_steps: Number of steps to train in total. steps_per_loop: Number of steps per graph-mode loop. In order to reduce communication in eager context, training logs are printed every steps_per_loop. optimizer: The optimizer for model. learning_rate_fn: the learning rate schedule. eval_fn: A callback of evaluation function, that takes a keras.Model, current step and evaluation summary writer. metric_fn: A metrics function returns a Keras Metric object to record evaluation result using evaluation dataset or with training dataset after every epoch. init_checkpoint: Optional checkpoint to load to `sub_model` returned by `model_fn`. init_from_transformerxl: Whether to load to `transformerxl_model` of `model_fn`. model_dir: The directory of model (checkpoints, summaries). save_steps: The frequency to save checkpoints. Every save_steps, we save a model checkpoint. Model checkpoint will be saved and evaluation will be conducted if evaluation dataset is provided. run_eagerly: Whether to run training eagerly. Returns: Last training step logits if training happens, otherwise returns None. Raises: TypeError: if model directory is not specified. """ required_arguments = [ train_input_fn, total_training_steps, steps_per_loop, optimizer, learning_rate_fn, save_steps ] if [arg for arg in required_arguments if arg is None]: raise ValueError("`train_input_fn`, `total_training_steps`, " "`steps_per_loop`, `optimizer`, `save_steps` and " "`learning_rate_fn` are required parameters.") if not model_dir: raise TypeError("Model directory must be specified.") train_iterator = data_utils.get_input_iterator(train_input_fn, strategy) if not tf.io.gfile.exists(model_dir): tf.io.gfile.mkdir(model_dir) # Create summary writers summary_dir = os.path.join(model_dir, "summaries") if not tf.io.gfile.exists(summary_dir): tf.io.gfile.mkdir(summary_dir) train_summary_writer = None eval_summary_writer = None if eval_fn: eval_summary_writer = tf.summary.create_file_writer( os.path.join(summary_dir, "eval")) if steps_per_loop >= _MIN_SUMMARY_STEPS: # Only writes summary when the stats are collected sufficiently over # enough steps. train_summary_writer = tf.summary.create_file_writer( os.path.join(summary_dir, "train")) with strategy.scope(): model = model_fn() if init_checkpoint: logging.info("restore from %s", init_checkpoint) if init_from_transformerxl: checkpoint = tf.train.Checkpoint( transformer_xl=model.transformerxl_model) else: checkpoint = tf.train.Checkpoint(model=model) checkpoint.restore(init_checkpoint) model.optimizer = optimizer if not hasattr(model, "optimizer"): raise ValueError("User should set optimizer attribute to model.") train_loss_metric = tf.keras.metrics.Mean("training_loss", dtype=tf.float32) train_metric = None if metric_fn: train_metric = metric_fn() def _replicated_step(inputs, mem=None): """Replicated training step.""" inputs["mems"] = mem with tf.GradientTape() as tape: mem, logits = model(inputs, training=True) loss = model.losses train_loss_metric.update_state(loss) if train_metric: train_metric.update_state(inputs["label_ids"], logits) scaled_loss = loss[0] * 1.0 / float(strategy.num_replicas_in_sync) # Collects training variables. tvars = model.trainable_variables grads = tape.gradient(scaled_loss, tvars) clipped, _ = tf.clip_by_global_norm(grads, clip_norm=1.0) if input_meta_data["lr_layer_decay_rate"] != 1.0: n_layer = 0 for i in range(len(clipped)): m = re.search(r"model/transformer/layer_(\d+?)/", tvars[i].name) if not m: continue n_layer = max(n_layer, int(m.group(1)) + 1) for i in range(len(clipped)): for l in range(n_layer): if "model/transformer/layer_{}/".format(l) in tvars[i].name: abs_rate = input_meta_data["lr_layer_decay_rate"]**( n_layer - 1 - l) clipped[i] *= abs_rate logging.info("Apply mult {:.4f} to layer-{} grad of {}".format( abs_rate, l, tvars[i].name)) break optimizer.apply_gradients(zip(clipped, tvars)) if input_meta_data["mem_len"] > 0: return mem def train_steps(iterator, steps): """Performs distributed training steps in a loop. Args: iterator: the distributed iterator of training datasets. steps: an tf.int32 integer tensor to specify number of steps to run inside host training loop. Raises: ValueError: Any of the arguments or tensor shapes are invalid. Returns: logits: logits computed. """ if not isinstance(steps, tf.Tensor): raise ValueError("steps should be an Tensor. Python object may cause " "retracing.") def cache_fn(): """Initializes memory tensor used in XLNet pretraining.""" mems = [] if input_meta_data["mem_len"] > 0: for _ in range(input_meta_data["n_layer"]): zeros = tf.zeros([ input_meta_data["batch_size_per_core"], input_meta_data["mem_len"], input_meta_data["d_model"] ], dtype=tf.float32) mems.append(zeros) return mems if input_meta_data["mem_len"] > 0: mem = strategy.run(cache_fn) for _ in tf.range(steps): mem = strategy.run( _replicated_step, args=( next(iterator), mem, )) else: for _ in tf.range(steps): strategy.run(_replicated_step, args=(next(iterator),)) if not run_eagerly: train_steps = tf.function(train_steps) logging.info("Start training...") checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer) latest_checkpoint_file = tf.train.latest_checkpoint(model_dir) if latest_checkpoint_file: logging.info("Checkpoint file %s found and restoring from checkpoint", latest_checkpoint_file) checkpoint.restore(latest_checkpoint_file) logging.info("Loading from checkpoint file completed") current_step = optimizer.iterations.numpy() checkpoint_name = "xlnet_step_{step}.ckpt" while current_step < total_training_steps: train_loss_metric.reset_states() if train_metric: train_metric.reset_states() steps = model_training_utils.steps_to_run(current_step, save_steps, steps_per_loop) train_steps(train_iterator, tf.convert_to_tensor(steps, dtype=tf.int32)) current_step += steps train_loss = _float_metric_value(train_loss_metric) log_stream = "Train step: %d/%d / lr = %.9f / loss = %.7f" % ( current_step, total_training_steps, learning_rate_fn(current_step), train_loss) if train_metric: log_stream += " / %s = %f" % (train_metric.name, _float_metric_value(train_metric)) logging.info(log_stream) if train_summary_writer: with train_summary_writer.as_default(): tf.summary.scalar( "learning_rate", learning_rate_fn(current_step), step=current_step) tf.summary.scalar( train_loss_metric.name, train_loss, step=current_step) if train_metric: tf.summary.scalar( train_metric.name, _float_metric_value(train_metric), step=current_step) train_summary_writer.flush() if model_dir and current_step % save_steps == 0: _save_checkpoint(checkpoint, model_dir, checkpoint_name.format(step=current_step)) if eval_fn and current_step % save_steps == 0: logging.info("Running evaluation after step: %s.", current_step) eval_fn(model, current_step, eval_summary_writer) if model_dir: _save_checkpoint(checkpoint, model_dir, checkpoint_name.format(step=current_step)) if eval_fn: logging.info("Running final evaluation after training is complete.") eval_metric = eval_fn(model, current_step, eval_summary_writer) training_summary = { "total_training_steps": total_training_steps, "train_loss": _float_metric_value(train_loss_metric), } if train_metric: training_summary["last_train_metrics"] = _float_metric_value(train_metric) if eval_fn: # eval_metric is supposed to be a float. training_summary["eval_metrics"] = eval_metric model_training_utils.write_txt_summary(training_summary, summary_dir) return model
11,664
37.120915
80
py
models
models-master/official/legacy/xlnet/common_flags.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Common flags used in XLNet model.""" from absl import flags flags.DEFINE_string("master", default=None, help="master") flags.DEFINE_string( "tpu", default=None, help="The Cloud TPU to use for training. This should be " "either the name used when creating the Cloud TPU, or a " "url like grpc://ip.address.of.tpu:8470.") flags.DEFINE_bool( "use_tpu", default=True, help="Use TPUs rather than plain CPUs.") flags.DEFINE_string("tpu_topology", "2x2", help="TPU topology.") flags.DEFINE_integer( "num_core_per_host", default=8, help="number of cores per host") flags.DEFINE_string("model_dir", default=None, help="Estimator model_dir.") flags.DEFINE_string( "init_checkpoint", default=None, help="Checkpoint path for initializing the model.") flags.DEFINE_bool( "init_from_transformerxl", default=False, help="Init from a transformerxl model checkpoint. Otherwise, init from the " "entire model checkpoint.") # Optimization config flags.DEFINE_float("learning_rate", default=1e-4, help="Maximum learning rate.") flags.DEFINE_float("clip", default=1.0, help="Gradient clipping value.") flags.DEFINE_float("weight_decay_rate", default=0.0, help="Weight decay rate.") # lr decay flags.DEFINE_integer( "warmup_steps", default=0, help="Number of steps for linear lr warmup.") flags.DEFINE_float("adam_epsilon", default=1e-8, help="Adam epsilon.") flags.DEFINE_float( "lr_layer_decay_rate", default=1.0, help="Top layer: lr[L] = FLAGS.learning_rate." "Lower layers: lr[l-1] = lr[l] * lr_layer_decay_rate.") flags.DEFINE_float( "min_lr_ratio", default=0.0, help="Minimum ratio learning rate.") # Training config flags.DEFINE_integer( "train_batch_size", default=16, help="Size of the train batch across all hosts.") flags.DEFINE_integer( "train_steps", default=100000, help="Total number of training steps.") flags.DEFINE_integer( "iterations", default=1000, help="Number of iterations per repeat loop.") # Data config flags.DEFINE_integer( "seq_len", default=0, help="Sequence length for pretraining.") flags.DEFINE_integer( "reuse_len", default=0, help="How many tokens to be reused in the next batch. " "Could be half of `seq_len`.") flags.DEFINE_bool("uncased", False, help="Use uncased inputs or not.") flags.DEFINE_bool( "bi_data", default=False, help="Use bidirectional data streams, " "i.e., forward & backward.") flags.DEFINE_integer("n_token", 32000, help="Vocab size") # Model config flags.DEFINE_integer("mem_len", default=0, help="Number of steps to cache") flags.DEFINE_bool("same_length", default=False, help="Same length attention") flags.DEFINE_integer("clamp_len", default=-1, help="Clamp length") flags.DEFINE_integer("n_layer", default=6, help="Number of layers.") flags.DEFINE_integer("d_model", default=32, help="Dimension of the model.") flags.DEFINE_integer("d_embed", default=32, help="Dimension of the embeddings.") flags.DEFINE_integer("n_head", default=4, help="Number of attention heads.") flags.DEFINE_integer( "d_head", default=8, help="Dimension of each attention head.") flags.DEFINE_integer( "d_inner", default=32, help="Dimension of inner hidden size in positionwise " "feed-forward.") flags.DEFINE_float("dropout", default=0.1, help="Dropout rate.") flags.DEFINE_float("dropout_att", default=0.1, help="Attention dropout rate.") flags.DEFINE_bool("untie_r", default=False, help="Untie r_w_bias and r_r_bias") flags.DEFINE_string( "ff_activation", default="relu", help="Activation type used in position-wise feed-forward.") flags.DEFINE_string( "strategy_type", default="tpu", help="Activation type used in position-wise feed-forward.") flags.DEFINE_bool("use_bfloat16", False, help="Whether to use bfloat16.") # Parameter initialization flags.DEFINE_enum( "init_method", default="normal", enum_values=["normal", "uniform"], help="Initialization method.") flags.DEFINE_float( "init_std", default=0.02, help="Initialization std when init is normal.") flags.DEFINE_float( "init_range", default=0.1, help="Initialization std when init is uniform.") flags.DEFINE_integer( "test_data_size", default=12048, help="Number of test data samples.") flags.DEFINE_string( "train_tfrecord_path", default=None, help="Path to preprocessed training set tfrecord.") flags.DEFINE_string( "test_tfrecord_path", default=None, help="Path to preprocessed test set tfrecord.") flags.DEFINE_integer( "test_batch_size", default=16, help="Size of the test batch across all hosts.") flags.DEFINE_integer( "save_steps", default=1000, help="Number of steps for saving checkpoint.") FLAGS = flags.FLAGS
5,367
36.538462
80
py
models
models-master/official/legacy/xlnet/run_pretrain.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """XLNet pretraining runner in tf2.0.""" import functools import os # Import libraries from absl import app from absl import flags from absl import logging import tensorflow as tf # pylint: disable=unused-import from official.common import distribute_utils from official.legacy.xlnet import common_flags from official.legacy.xlnet import data_utils from official.legacy.xlnet import optimization from official.legacy.xlnet import training_utils from official.legacy.xlnet import xlnet_config from official.legacy.xlnet import xlnet_modeling as modeling flags.DEFINE_integer( "num_predict", default=None, help="Number of tokens to predict in partial prediction.") # FLAGS for pretrain input preprocessing flags.DEFINE_integer("perm_size", 0, help="Window size of permutation.") flags.DEFINE_float("leak_ratio", default=0.1, help="Percent of masked tokens that are leaked.") flags.DEFINE_enum("sample_strategy", default="token_span", enum_values=["single_token", "whole_word", "token_span", "word_span"], help="Stragey used to sample prediction targets.") flags.DEFINE_integer("max_num_tokens", default=5, help="Maximum number of tokens to sample in a span." "Effective when token_span strategy is used.") flags.DEFINE_integer("min_num_tokens", default=1, help="Minimum number of tokens to sample in a span." "Effective when token_span strategy is used.") flags.DEFINE_integer("max_num_words", default=5, help="Maximum number of whole words to sample in a span." "Effective when word_span strategy is used.") flags.DEFINE_integer("min_num_words", default=1, help="Minimum number of whole words to sample in a span." "Effective when word_span strategy is used.") FLAGS = flags.FLAGS def get_pretrainxlnet_model(model_config, run_config): return modeling.PretrainingXLNetModel( use_proj=True, xlnet_config=model_config, run_config=run_config, name="model") def main(unused_argv): del unused_argv num_hosts = 1 strategy = distribute_utils.get_distribution_strategy( distribution_strategy=FLAGS.strategy_type, tpu_address=FLAGS.tpu) if FLAGS.strategy_type == "tpu": num_hosts = strategy.extended.num_hosts if strategy: logging.info("***** Number of cores used : %d", strategy.num_replicas_in_sync) logging.info("***** Number of hosts used : %d", num_hosts) online_masking_config = data_utils.OnlineMaskingConfig( sample_strategy=FLAGS.sample_strategy, max_num_tokens=FLAGS.max_num_tokens, min_num_tokens=FLAGS.min_num_tokens, max_num_words=FLAGS.max_num_words, min_num_words=FLAGS.min_num_words) train_input_fn = functools.partial( data_utils.get_pretrain_input_data, FLAGS.train_batch_size, FLAGS.seq_len, strategy, FLAGS.train_tfrecord_path, FLAGS.reuse_len, FLAGS.perm_size, FLAGS.leak_ratio, FLAGS.num_predict, FLAGS.uncased, online_masking_config, num_hosts) total_training_steps = FLAGS.train_steps steps_per_loop = FLAGS.iterations optimizer, learning_rate_fn = optimization.create_optimizer( init_lr=FLAGS.learning_rate, num_train_steps=total_training_steps, num_warmup_steps=FLAGS.warmup_steps, min_lr_ratio=FLAGS.min_lr_ratio, adam_epsilon=FLAGS.adam_epsilon, weight_decay_rate=FLAGS.weight_decay_rate) model_config = xlnet_config.XLNetConfig(FLAGS) run_config = xlnet_config.create_run_config(True, False, FLAGS) input_meta_data = {} input_meta_data["d_model"] = FLAGS.d_model input_meta_data["mem_len"] = FLAGS.mem_len input_meta_data["batch_size_per_core"] = int(FLAGS.train_batch_size / strategy.num_replicas_in_sync) input_meta_data["n_layer"] = FLAGS.n_layer input_meta_data["lr_layer_decay_rate"] = FLAGS.lr_layer_decay_rate model_fn = functools.partial(get_pretrainxlnet_model, model_config, run_config) model = training_utils.train( strategy=strategy, model_fn=model_fn, input_meta_data=input_meta_data, eval_fn=None, metric_fn=None, train_input_fn=train_input_fn, init_checkpoint=FLAGS.init_checkpoint, init_from_transformerxl=FLAGS.init_from_transformerxl, total_training_steps=total_training_steps, steps_per_loop=steps_per_loop, optimizer=optimizer, learning_rate_fn=learning_rate_fn, model_dir=FLAGS.model_dir, save_steps=FLAGS.save_steps) # Export transformer-xl model checkpoint to be used in finetuning. checkpoint = tf.train.Checkpoint(transformer_xl=model.transformerxl_model) saved_path = checkpoint.save( os.path.join(FLAGS.model_dir, "pretrained/transformer_xl.ckpt")) logging.info("Exporting the transformer-xl model as a new TF checkpoint: %s", saved_path) if __name__ == "__main__": app.run(main)
5,710
37.85034
80
py
models
models-master/official/legacy/xlnet/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
610
37.1875
74
py
models
models-master/official/legacy/xlnet/preprocess_pretrain_data.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # -*- coding: utf-8 -*- """Script to pre-process pre-training data into tfrecords.""" import json import os import random # Import libraries from absl import app from absl import flags from absl import logging import numpy as np import tensorflow.compat.v1 as tf import sentencepiece as spm from official.legacy.xlnet import preprocess_utils FLAGS = flags.FLAGS special_symbols = { "<unk>": 0, "<s>": 1, "</s>": 2, "<cls>": 3, "<sep>": 4, "<pad>": 5, "<mask>": 6, "<eod>": 7, "<eop>": 8, } VOCAB_SIZE = 32000 UNK_ID = special_symbols["<unk>"] CLS_ID = special_symbols["<cls>"] SEP_ID = special_symbols["<sep>"] MASK_ID = special_symbols["<mask>"] EOD_ID = special_symbols["<eod>"] def _int64_feature(values): return tf.train.Feature(int64_list=tf.train.Int64List(value=values)) def _float_feature(values): return tf.train.Feature(float_list=tf.train.FloatList(value=values)) def format_filename(prefix, bsz_per_host, seq_len, bi_data, suffix, mask_alpha=5, mask_beta=1, reuse_len=None, uncased=False, fixed_num_predict=None): """docs.""" if reuse_len is None: reuse_len_str = "" else: reuse_len_str = "reuse-{}.".format(reuse_len) if not uncased: uncased_str = "" else: uncased_str = "uncased." if bi_data: bi_data_str = "bi" else: bi_data_str = "uni" if fixed_num_predict is not None: fnp_str = "fnp-{}.".format(fixed_num_predict) else: fnp_str = "" file_name = "{}.bsz-{}.seqlen-{}.{}{}{}.alpha-{}.beta-{}.{}{}".format( prefix, bsz_per_host, seq_len, reuse_len_str, uncased_str, bi_data_str, mask_alpha, mask_beta, fnp_str, suffix) return file_name def _create_data(idx, input_paths): """Creates data.""" # Load sentence-piece model sp = spm.SentencePieceProcessor() sp.Load(FLAGS.sp_path) input_shards = [] total_line_cnt = 0 for input_path in input_paths: input_data, sent_ids = [], [] sent_id, line_cnt = True, 0 logging.info("Processing %s", input_path) for line in tf.gfile.Open(input_path): if line_cnt % 100000 == 0: logging.info("Loading line %d", line_cnt) line_cnt += 1 if not line.strip(): if FLAGS.use_eod: sent_id = not sent_id cur_sent = [EOD_ID] else: continue else: if FLAGS.from_raw_text: cur_sent = preprocess_utils.preprocess_text( line.strip(), lower=FLAGS.uncased) cur_sent = preprocess_utils.encode_ids(sp, cur_sent) else: cur_sent = list(map(int, line.strip().split())) input_data.extend(cur_sent) sent_ids.extend([sent_id] * len(cur_sent)) sent_id = not sent_id logging.info("Finish with line %d", line_cnt) if line_cnt == 0: continue input_data = np.array(input_data, dtype=np.int64) sent_ids = np.array(sent_ids, dtype=bool) total_line_cnt += line_cnt input_shards.append((input_data, sent_ids)) logging.info("[Task %d] Total number line: %d", idx, total_line_cnt) tfrecord_dir = os.path.join(FLAGS.save_dir, "tfrecords") filenames, num_batch = [], 0 # Randomly shuffle input shards (with a fixed but distinct random seed) np.random.seed(100 * FLAGS.task + FLAGS.pass_id) perm_indices = np.random.permutation(len(input_shards)) logging.info("Using perm indices %s for pass %d", perm_indices.tolist(), FLAGS.pass_id) input_data_list, sent_ids_list = [], [] prev_sent_id = None for perm_idx in perm_indices: input_data, sent_ids = input_shards[perm_idx] # make sure the `send_ids[0] == not prev_sent_id` if prev_sent_id is not None and sent_ids[0] == prev_sent_id: sent_ids = np.logical_not(sent_ids) # append to temporary list input_data_list.append(input_data) sent_ids_list.append(sent_ids) # update `prev_sent_id` prev_sent_id = sent_ids[-1] input_data = np.concatenate(input_data_list) sent_ids = np.concatenate(sent_ids_list) file_name, cur_num_batch = create_tfrecords( save_dir=tfrecord_dir, basename="{}-{}-{}".format(FLAGS.split, idx, FLAGS.pass_id), data=[input_data, sent_ids], bsz_per_host=FLAGS.bsz_per_host, seq_len=FLAGS.seq_len, bi_data=FLAGS.bi_data, sp=sp, ) filenames.append(file_name) num_batch += cur_num_batch record_info = { "filenames": filenames, "num_batch": num_batch } return record_info def create_data(_): """Creates pretrain data.""" # Validate FLAGS assert FLAGS.bsz_per_host % FLAGS.num_core_per_host == 0 if not FLAGS.use_tpu: FLAGS.num_core_per_host = 1 # forced to be one # Make workdirs if not tf.gfile.Exists(FLAGS.save_dir): tf.gfile.MakeDirs(FLAGS.save_dir) tfrecord_dir = os.path.join(FLAGS.save_dir, "tfrecords") if not tf.gfile.Exists(tfrecord_dir): tf.gfile.MakeDirs(tfrecord_dir) # Create and dump corpus_info from task 0 if FLAGS.task == 0 and FLAGS.pass_id == 0: corpus_info = { "vocab_size": VOCAB_SIZE, "bsz_per_host": FLAGS.bsz_per_host, "num_core_per_host": FLAGS.num_core_per_host, "seq_len": FLAGS.seq_len, "reuse_len": FLAGS.reuse_len, "uncased": FLAGS.uncased, "bi_data": FLAGS.bi_data, "mask_alpha": FLAGS.mask_alpha, "mask_beta": FLAGS.mask_beta, "num_predict": FLAGS.num_predict, "use_eod": FLAGS.use_eod, "sp_path": FLAGS.sp_path, "input_glob": FLAGS.input_glob, } corpus_info_path = os.path.join(FLAGS.save_dir, "corpus_info.json") with tf.gfile.Open(corpus_info_path, "w") as fp: json.dump(corpus_info, fp) # Interleavely split the work into FLAGS.num_task splits file_paths = sorted(tf.gfile.Glob(FLAGS.input_glob)) logging.info("Use glob: %s", FLAGS.input_glob) logging.info("Find %d files: %s", len(file_paths), file_paths) task_file_paths = file_paths[FLAGS.task::FLAGS.num_task] if not task_file_paths: logging.info("Exit: task %d has no file to process.", FLAGS.task) return logging.info("Task %d process %d files: %s", FLAGS.task, len(task_file_paths), task_file_paths) record_info = _create_data(FLAGS.task, task_file_paths) record_prefix = "record_info-{}-{}-{}".format( FLAGS.split, FLAGS.task, FLAGS.pass_id) record_name = format_filename( prefix=record_prefix, bsz_per_host=FLAGS.bsz_per_host, seq_len=FLAGS.seq_len, mask_alpha=FLAGS.mask_alpha, mask_beta=FLAGS.mask_beta, reuse_len=FLAGS.reuse_len, bi_data=FLAGS.bi_data, suffix="json", uncased=FLAGS.uncased, fixed_num_predict=FLAGS.num_predict) record_info_path = os.path.join(tfrecord_dir, record_name) with tf.gfile.Open(record_info_path, "w") as fp: json.dump(record_info, fp) def batchify(data, bsz_per_host, sent_ids=None): """Creates batches.""" num_step = len(data) // bsz_per_host data = data[:bsz_per_host * num_step] data = data.reshape(bsz_per_host, num_step) if sent_ids is not None: sent_ids = sent_ids[:bsz_per_host * num_step] sent_ids = sent_ids.reshape(bsz_per_host, num_step) if sent_ids is not None: return data, sent_ids return data def _split_a_and_b(data, sent_ids, begin_idx, tot_len, extend_target=False): """Split two segments from `data` starting from the index `begin_idx`.""" data_len = data.shape[0] if begin_idx + tot_len >= data_len: logging.info("[_split_a_and_b] returns None: " "begin_idx %d + tot_len %d >= data_len %d", begin_idx, tot_len, data_len) return None end_idx = begin_idx + 1 cut_points = [] while end_idx < data_len: if sent_ids[end_idx] != sent_ids[end_idx - 1]: if end_idx - begin_idx >= tot_len: break cut_points.append(end_idx) end_idx += 1 a_begin = begin_idx if len(cut_points) == 0 or random.random() < 0.5: # pylint:disable=g-explicit-length-test label = 0 if len(cut_points) == 0: # pylint:disable=g-explicit-length-test a_end = end_idx else: a_end = random.choice(cut_points) b_len = max(1, tot_len - (a_end - a_begin)) # (zihangd): `data_len - 1` to account for extend_target b_begin = random.randint(0, data_len - 1 - b_len) b_end = b_begin + b_len while b_begin > 0 and sent_ids[b_begin - 1] == sent_ids[b_begin]: b_begin -= 1 # (zihangd): `data_len - 1` to account for extend_target while b_end < data_len - 1 and sent_ids[b_end - 1] == sent_ids[b_end]: b_end += 1 new_begin = a_end else: label = 1 a_end = random.choice(cut_points) b_begin = a_end b_end = end_idx new_begin = b_end while a_end - a_begin + b_end - b_begin > tot_len: if a_end - a_begin > b_end - b_begin: # delete the right side only for the LM objective a_end -= 1 else: b_end -= 1 ret = [data[a_begin: a_end], data[b_begin: b_end], label, new_begin] if extend_target: if a_end >= data_len or b_end >= data_len: logging.info("[_split_a_and_b] returns None: " "a_end %d or b_end %d >= data_len %d", a_end, b_end, data_len) return None a_target = data[a_begin + 1: a_end + 1] b_target = data[b_begin: b_end + 1] ret.extend([a_target, b_target]) return ret def _is_start_piece(piece): special_pieces = set(list('!"#$%&\"()*+,-./:;?@[\\]^_`{|}~')) if (piece.startswith("▁") or piece.startswith("<") or piece in special_pieces): return True else: return False def _sample_mask(sp, seg, reverse=False, max_gram=5, goal_num_predict=None): """Samples `goal_num_predict` tokens for partial prediction.""" seg_len = len(seg) mask = np.array([False] * seg_len, dtype=bool) num_predict = 0 ngrams = np.arange(1, max_gram + 1, dtype=np.int64) pvals = 1. / np.arange(1, max_gram + 1) pvals /= pvals.sum(keepdims=True) if reverse: seg = np.flip(seg, 0) cur_len = 0 while cur_len < seg_len: if goal_num_predict is not None and num_predict >= goal_num_predict: break n = np.random.choice(ngrams, p=pvals) if goal_num_predict is not None: n = min(n, goal_num_predict - num_predict) ctx_size = (n * FLAGS.mask_alpha) // FLAGS.mask_beta l_ctx = np.random.choice(ctx_size) r_ctx = ctx_size - l_ctx # Find the start position of a complete token beg = cur_len + l_ctx while beg < seg_len and not _is_start_piece(sp.IdToPiece(seg[beg].item())): beg += 1 if beg >= seg_len: break # Find the end position of the n-gram (start pos of the n+1-th gram) end = beg + 1 cnt_ngram = 1 while end < seg_len: cnt_ngram += 1 if cnt_ngram > n: break end += 1 if end >= seg_len: break # Update mask[beg:end] = True num_predict += end - beg cur_len = end + r_ctx while goal_num_predict is not None and num_predict < goal_num_predict: i = np.random.randint(seg_len) if not mask[i]: mask[i] = True num_predict += 1 if reverse: mask = np.flip(mask, 0) return mask def _sample_mask_ngram(sp, seg, reverse=False, max_gram=5, goal_num_predict=None): """Sample `goal_num_predict` tokens for partial prediction.""" seg_len = len(seg) mask = np.array([False] * seg_len, dtype=bool) num_predict = 0 ngrams = np.arange(1, max_gram + 1, dtype=np.int64) pvals = 1. / np.arange(1, max_gram + 1) pvals /= pvals.sum(keepdims=True) if reverse: seg = np.flip(seg, 0) cur_len = 0 while cur_len < seg_len: if goal_num_predict is not None and num_predict >= goal_num_predict: break n = np.random.choice(ngrams, p=pvals) if goal_num_predict is not None: n = min(n, goal_num_predict - num_predict) ctx_size = (n * FLAGS.mask_alpha) // FLAGS.mask_beta l_ctx = np.random.choice(ctx_size) r_ctx = ctx_size - l_ctx # Find the start position of a complete token beg = cur_len + l_ctx while beg < seg_len and not _is_start_piece(sp.IdToPiece(seg[beg].item())): beg += 1 if beg >= seg_len: break # Find the end position of the n-gram (start pos of the n+1-th gram) end = beg cnt_ngram = 0 while end < seg_len: if _is_start_piece(sp.IdToPiece(seg[end].item())): cnt_ngram += 1 if cnt_ngram > n: break # select current piece mask[end] = True # update the end pointer and increment num_predict end += 1 num_predict += 1 if goal_num_predict is not None and num_predict >= goal_num_predict: break cur_len = end + r_ctx while goal_num_predict is not None and num_predict < goal_num_predict: i = np.random.randint(seg_len) if not mask[i]: mask[i] = True num_predict += 1 if reverse: mask = np.flip(mask, 0) return mask def create_tfrecords(save_dir, basename, data, bsz_per_host, seq_len, bi_data, sp): """Creates TFRecords.""" data, sent_ids = data[0], data[1] num_core = FLAGS.num_core_per_host bsz_per_core = bsz_per_host // num_core if bi_data: assert bsz_per_host % (2 * FLAGS.num_core_per_host) == 0 fwd_data, fwd_sent_ids = batchify(data, bsz_per_host // 2, sent_ids) fwd_data = fwd_data.reshape(num_core, 1, bsz_per_core // 2, -1) fwd_sent_ids = fwd_sent_ids.reshape(num_core, 1, bsz_per_core // 2, -1) bwd_data = fwd_data[:, :, :, ::-1] bwd_sent_ids = fwd_sent_ids[:, :, :, ::-1] data = np.concatenate( [fwd_data, bwd_data], 1).reshape(bsz_per_host, -1) sent_ids = np.concatenate( [fwd_sent_ids, bwd_sent_ids], 1).reshape(bsz_per_host, -1) else: data, sent_ids = batchify(data, bsz_per_host, sent_ids) logging.info("Raw data shape %s.", data.shape) file_name = format_filename( prefix=basename, bsz_per_host=bsz_per_host, seq_len=seq_len, bi_data=bi_data, suffix="tfrecords", mask_alpha=FLAGS.mask_alpha, mask_beta=FLAGS.mask_beta, reuse_len=FLAGS.reuse_len, uncased=FLAGS.uncased, fixed_num_predict=FLAGS.num_predict ) save_path = os.path.join(save_dir, file_name) record_writer = tf.python_io.TFRecordWriter(save_path) logging.info("Start writing %s.", save_path) num_batch = 0 reuse_len = FLAGS.reuse_len # [sep] x 2 + [cls] assert reuse_len < seq_len - 3 data_len = data.shape[1] sep_array = np.array([SEP_ID], dtype=np.int64) cls_array = np.array([CLS_ID], dtype=np.int64) i = 0 while i + seq_len <= data_len: if num_batch % 500 == 0: logging.info("Processing batch %d", num_batch) all_ok = True features = [] for idx in range(bsz_per_host): inp = data[idx, i: i + reuse_len] tgt = data[idx, i + 1: i + reuse_len + 1] results = _split_a_and_b( data[idx], sent_ids[idx], begin_idx=i + reuse_len, tot_len=seq_len - reuse_len - 3, extend_target=True) if results is None: logging.info("Break out with seq idx %d", i) all_ok = False break # unpack the results (a_data, b_data, label, _, a_target, b_target) = tuple(results) # sample ngram spans to predict reverse = bi_data and (idx // (bsz_per_core // 2)) % 2 == 1 if FLAGS.num_predict is None: num_predict_0 = num_predict_1 = None else: num_predict_1 = FLAGS.num_predict // 2 num_predict_0 = FLAGS.num_predict - num_predict_1 mask_0 = _sample_mask(sp, inp, reverse=reverse, goal_num_predict=num_predict_0) mask_1 = _sample_mask(sp, np.concatenate([a_data, sep_array, b_data, sep_array, cls_array]), reverse=reverse, goal_num_predict=num_predict_1) # concatenate data cat_data = np.concatenate([inp, a_data, sep_array, b_data, sep_array, cls_array]) seg_id = ([0] * (reuse_len + a_data.shape[0]) + [0] + [1] * b_data.shape[0] + [1] + [2]) assert cat_data.shape[0] == seq_len assert mask_0.shape[0] == seq_len // 2 assert mask_1.shape[0] == seq_len // 2 # the last two CLS's are not used, just for padding purposes tgt = np.concatenate([tgt, a_target, b_target, cls_array, cls_array]) assert tgt.shape[0] == seq_len is_masked = np.concatenate([mask_0, mask_1], 0) if FLAGS.num_predict is not None: assert np.sum(is_masked) == FLAGS.num_predict feature = { "input": _int64_feature(cat_data), "is_masked": _int64_feature(is_masked), "target": _int64_feature(tgt), "seg_id": _int64_feature(seg_id), "label": _int64_feature([label]), } features.append(feature) if all_ok: assert len(features) == bsz_per_host for feature in features: example = tf.train.Example(features=tf.train.Features(feature=feature)) record_writer.write(example.SerializeToString()) num_batch += 1 else: break i += reuse_len record_writer.close() logging.info("Done writing %s. Num of batches: %d", save_path, num_batch) return save_path, num_batch ################ # get_input_fn # ################ def _convert_example(example, use_bfloat16): """Cast int64 into int32 and float32 to bfloat16 if use_bfloat16.""" for key in list(example.keys()): val = example[key] if tf.keras.backend.is_sparse(val): val = tf.sparse.to_dense(val) if val.dtype == tf.int64: val = tf.cast(val, tf.int32) if use_bfloat16 and val.dtype == tf.float32: val = tf.cast(val, tf.bfloat16) example[key] = val def parse_files_to_dataset(parser, file_names, split, num_batch, num_hosts, host_id, num_core_per_host, bsz_per_core): """Parses files to a dataset.""" del num_batch # list of file pathes num_files = len(file_names) num_files_per_host = num_files // num_hosts my_start_file_id = host_id * num_files_per_host my_end_file_id = (host_id + 1) * num_files_per_host if host_id == num_hosts - 1: my_end_file_id = num_files file_paths = file_names[my_start_file_id: my_end_file_id] logging.info("Host %d handles %d files", host_id, len(file_paths)) assert split == "train" dataset = tf.data.Dataset.from_tensor_slices(file_paths) # file-level shuffle if len(file_paths) > 1: dataset = dataset.shuffle(len(file_paths)) # Note: we cannot perform sample-level shuffle here because this will violate # the consecutive requirement of data stream. dataset = tf.data.TFRecordDataset(dataset) # Note: since we are doing online preprocessing, the parsed result of # the same input at each time will be different. Thus, cache processed data # is not helpful. It will use a lot of memory and lead to contrainer OOM. # So, change to cache non-parsed raw data instead. dataset = dataset.cache().map(parser).repeat() dataset = dataset.batch(bsz_per_core, drop_remainder=True) dataset = dataset.prefetch(num_core_per_host * bsz_per_core) return dataset def _local_perm(inputs, targets, is_masked, perm_size, seq_len): """Samples a permutation of the factorization order, and create a mask. Args: inputs: int64 Tensor in shape [seq_len], input ids. targets: int64 Tensor in shape [seq_len], target ids. is_masked: bool Tensor in shape [seq_len]. True means being selected for partial prediction. perm_size: the length of longest permutation. Could be set to be reuse_len. Should not be larger than reuse_len or there will be data leaks. seq_len: int, sequence length. Returns: The permutation mask, new targets, target mask, and new inputs. """ # Generate permutation indices index = tf.range(seq_len, dtype=tf.int64) index = tf.transpose(tf.reshape(index, [-1, perm_size])) index = tf.random_shuffle(index) index = tf.reshape(tf.transpose(index), [-1]) # `perm_mask` and `target_mask` # non-functional tokens non_func_tokens = tf.logical_not(tf.logical_or( tf.equal(inputs, SEP_ID), tf.equal(inputs, CLS_ID))) non_mask_tokens = tf.logical_and(tf.logical_not(is_masked), non_func_tokens) masked_or_func_tokens = tf.logical_not(non_mask_tokens) # Set the permutation indices of non-masked (& non-funcional) tokens to the # smallest index (-1): # (1) they can be seen by all other positions # (2) they cannot see masked positions, so there won"t be information leak smallest_index = -tf.ones([seq_len], dtype=tf.int64) rev_index = tf.where(non_mask_tokens, smallest_index, index) # Create `target_mask`: non-funcional and maksed tokens # 1: use mask as input and have loss # 0: use token (or [SEP], [CLS]) as input and do not have loss target_tokens = tf.logical_and(masked_or_func_tokens, non_func_tokens) target_mask = tf.cast(target_tokens, tf.float32) # Create `perm_mask` # `target_tokens` cannot see themselves self_rev_index = tf.where(target_tokens, rev_index, rev_index + 1) # 1: cannot attend if i <= j and j is not non-masked (masked_or_func_tokens) # 0: can attend if i > j or j is non-masked perm_mask = tf.logical_and( self_rev_index[:, None] <= rev_index[None, :], masked_or_func_tokens) perm_mask = tf.cast(perm_mask, tf.float32) # new target: [next token] for LM and [curr token] (self) for PLM new_targets = tf.concat([inputs[0: 1], targets[: -1]], axis=0) # construct inputs_k inputs_k = inputs # construct inputs_q inputs_q = target_mask return perm_mask, new_targets, target_mask, inputs_k, inputs_q def get_dataset(params, num_hosts, num_core_per_host, split, file_names, num_batch, seq_len, reuse_len, perm_size, mask_alpha, mask_beta, use_bfloat16=False, num_predict=None): """Gets the dataset.""" del mask_alpha del mask_beta bsz_per_core = params["batch_size"] if num_hosts > 1: host_id = params["context"].current_host else: host_id = 0 #### Function used to parse tfrecord def parser(record): """function used to parse tfrecord.""" record_spec = { "input": tf.FixedLenFeature([seq_len], tf.int64), "target": tf.FixedLenFeature([seq_len], tf.int64), "seg_id": tf.FixedLenFeature([seq_len], tf.int64), "label": tf.FixedLenFeature([1], tf.int64), "is_masked": tf.FixedLenFeature([seq_len], tf.int64), } # retrieve serialized example example = tf.parse_single_example( serialized=record, features=record_spec) inputs = example.pop("input") target = example.pop("target") is_masked = tf.cast(example.pop("is_masked"), tf.bool) non_reuse_len = seq_len - reuse_len assert perm_size <= reuse_len and perm_size <= non_reuse_len perm_mask_0, target_0, target_mask_0, input_k_0, input_q_0 = _local_perm( inputs[:reuse_len], target[:reuse_len], is_masked[:reuse_len], perm_size, reuse_len) perm_mask_1, target_1, target_mask_1, input_k_1, input_q_1 = _local_perm( inputs[reuse_len:], target[reuse_len:], is_masked[reuse_len:], perm_size, non_reuse_len) perm_mask_0 = tf.concat([perm_mask_0, tf.ones([reuse_len, non_reuse_len])], axis=1) perm_mask_1 = tf.concat([tf.zeros([non_reuse_len, reuse_len]), perm_mask_1], axis=1) perm_mask = tf.concat([perm_mask_0, perm_mask_1], axis=0) target = tf.concat([target_0, target_1], axis=0) target_mask = tf.concat([target_mask_0, target_mask_1], axis=0) input_k = tf.concat([input_k_0, input_k_1], axis=0) input_q = tf.concat([input_q_0, input_q_1], axis=0) if num_predict is not None: indices = tf.range(seq_len, dtype=tf.int64) bool_target_mask = tf.cast(target_mask, tf.bool) indices = tf.boolean_mask(indices, bool_target_mask) ##### extra padding due to CLS/SEP introduced after prepro actual_num_predict = tf.shape(indices)[0] pad_len = num_predict - actual_num_predict ##### target_mapping target_mapping = tf.one_hot(indices, seq_len, dtype=tf.float32) paddings = tf.zeros([pad_len, seq_len], dtype=target_mapping.dtype) target_mapping = tf.concat([target_mapping, paddings], axis=0) example["target_mapping"] = tf.reshape(target_mapping, [num_predict, seq_len]) ##### target target = tf.boolean_mask(target, bool_target_mask) paddings = tf.zeros([pad_len], dtype=target.dtype) target = tf.concat([target, paddings], axis=0) example["target"] = tf.reshape(target, [num_predict]) ##### target mask target_mask = tf.concat( [tf.ones([actual_num_predict], dtype=tf.float32), tf.zeros([pad_len], dtype=tf.float32)], axis=0) example["target_mask"] = tf.reshape(target_mask, [num_predict]) else: example["target"] = tf.reshape(target, [seq_len]) example["target_mask"] = tf.reshape(target_mask, [seq_len]) # reshape back to fixed shape example["perm_mask"] = tf.reshape(perm_mask, [seq_len, seq_len]) example["input_k"] = tf.reshape(input_k, [seq_len]) example["input_q"] = tf.reshape(input_q, [seq_len]) _convert_example(example, use_bfloat16) for k, v in example.items(): logging.info("%s: %s", k, v) return example # Get dataset dataset = parse_files_to_dataset( parser=parser, file_names=file_names, split=split, num_batch=num_batch, num_hosts=num_hosts, host_id=host_id, num_core_per_host=num_core_per_host, bsz_per_core=bsz_per_core) return dataset def get_input_fn( tfrecord_dir, split, bsz_per_host, seq_len, reuse_len, bi_data, num_hosts=1, num_core_per_host=1, perm_size=None, mask_alpha=None, mask_beta=None, uncased=False, num_passes=None, use_bfloat16=False, num_predict=None): """Gets the input function.""" # Merge all record infos into a single one record_glob_base = format_filename( prefix="record_info-{}-*".format(split), bsz_per_host=bsz_per_host, seq_len=seq_len, bi_data=bi_data, suffix="json", mask_alpha=mask_alpha, mask_beta=mask_beta, reuse_len=reuse_len, uncased=uncased, fixed_num_predict=num_predict) record_info = {"num_batch": 0, "filenames": []} tfrecord_dirs = tfrecord_dir.split(",") logging.info("Use the following tfrecord dirs: %s", tfrecord_dirs) for idx, record_dir in enumerate(tfrecord_dirs): record_glob = os.path.join(record_dir, record_glob_base) logging.info("[%d] Record glob: %s", idx, record_glob) record_paths = sorted(tf.gfile.Glob(record_glob)) logging.info("[%d] Num of record info path: %d", idx, len(record_paths)) cur_record_info = {"num_batch": 0, "filenames": []} for record_info_path in record_paths: if num_passes is not None: record_info_name = os.path.basename(record_info_path) fields = record_info_name.split(".")[0].split("-") pass_id = int(fields[-1]) if len(fields) == 5 and pass_id >= num_passes: logging.info("Skip pass %d: %s", pass_id, record_info_name) continue with tf.gfile.Open(record_info_path, "r") as fp: info = json.load(fp) if num_passes is not None: eff_num_passes = min(num_passes, len(info["filenames"])) ratio = eff_num_passes / len(info["filenames"]) cur_record_info["num_batch"] += int(info["num_batch"] * ratio) cur_record_info["filenames"] += info["filenames"][:eff_num_passes] else: cur_record_info["num_batch"] += info["num_batch"] cur_record_info["filenames"] += info["filenames"] # overwrite directory for `cur_record_info` new_filenames = [] for filename in cur_record_info["filenames"]: basename = os.path.basename(filename) new_filename = os.path.join(record_dir, basename) new_filenames.append(new_filename) cur_record_info["filenames"] = new_filenames logging.info("[Dir %d] Number of chosen batches: %s", idx, cur_record_info["num_batch"]) logging.info("[Dir %d] Number of chosen files: %s", idx, len(cur_record_info["filenames"])) logging.info(cur_record_info["filenames"]) # add `cur_record_info` to global `record_info` record_info["num_batch"] += cur_record_info["num_batch"] record_info["filenames"] += cur_record_info["filenames"] logging.info("Total number of batches: %d", record_info["num_batch"]) logging.info("Total number of files: %d", len(record_info["filenames"])) logging.info(record_info["filenames"]) def input_fn(params): """docs.""" assert params["batch_size"] * num_core_per_host == bsz_per_host dataset = get_dataset( params=params, num_hosts=num_hosts, num_core_per_host=num_core_per_host, split=split, file_names=record_info["filenames"], num_batch=record_info["num_batch"], seq_len=seq_len, reuse_len=reuse_len, perm_size=perm_size, mask_alpha=mask_alpha, mask_beta=mask_beta, use_bfloat16=use_bfloat16, num_predict=num_predict) return dataset return input_fn, record_info def define_flags(): """Defines relevant flags.""" flags.DEFINE_bool("use_tpu", True, help="whether to use TPUs") flags.DEFINE_integer("bsz_per_host", 32, help="batch size per host.") flags.DEFINE_integer("num_core_per_host", 8, help="num TPU cores per host.") flags.DEFINE_integer("seq_len", 512, help="Sequence length.") flags.DEFINE_integer("reuse_len", 256, help="Number of token that can be reused as memory. " "Could be half of `seq_len`.") flags.DEFINE_bool("uncased", False, help="Use uncased inputs or not.") flags.DEFINE_bool("bi_data", True, help="whether to create bidirectional data") flags.DEFINE_integer("mask_alpha", default=6, help="How many tokens to form a group.") flags.DEFINE_integer("mask_beta", default=1, help="How many tokens to mask within each group.") flags.DEFINE_bool("use_eod", True, help="whether to append EOD at the end of a doc.") flags.DEFINE_bool("from_raw_text", True, help="Whether the input is raw text or encoded ids.") flags.DEFINE_integer("num_predict", default=85, help="Num of tokens to predict.") flags.DEFINE_string("input_glob", "data/example/*.txt", help="Input file glob.") flags.DEFINE_string("sp_path", "", help="Path to the sentence piece model.") flags.DEFINE_string("save_dir", "proc_data/example", help="Directory for saving the processed data.") flags.DEFINE_enum("split", "train", ["train", "dev", "test"], help="Save the data as which split.") flags.DEFINE_integer("pass_id", 0, help="ID of the current pass." "Different passes sample different negative segment.") flags.DEFINE_integer("num_task", 1, help="Number of total tasks.") flags.DEFINE_integer("task", 0, help="The Task ID. This value is used when " "using multiple workers to identify each worker.") if __name__ == "__main__": define_flags() logging.set_verbosity(logging.INFO) app.run(create_data)
32,389
31.196819
92
py
models
models-master/official/legacy/albert/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
609
39.666667
74
py
models
models-master/official/legacy/albert/configs.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """The ALBERT configurations.""" import six from official.legacy.bert import configs class AlbertConfig(configs.BertConfig): """Configuration for `ALBERT`.""" def __init__(self, num_hidden_groups=1, inner_group_num=1, **kwargs): """Constructs AlbertConfig. Args: num_hidden_groups: Number of group for the hidden layers, parameters in the same group are shared. Note that this value and also the following 'inner_group_num' has to be 1 for now, because all released ALBERT models set them to 1. We may support arbitary valid values in future. inner_group_num: Number of inner repetition of attention and ffn. **kwargs: The remaining arguments are the same as above 'BertConfig'. """ super(AlbertConfig, self).__init__(**kwargs) # TODO(chendouble): 'inner_group_num' and 'num_hidden_groups' are always 1 # in the released ALBERT. Support other values in AlbertEncoder if needed. if inner_group_num != 1 or num_hidden_groups != 1: raise ValueError("We only support 'inner_group_num' and " "'num_hidden_groups' as 1.") @classmethod def from_dict(cls, json_object): """Constructs a `AlbertConfig` from a Python dictionary of parameters.""" config = AlbertConfig(vocab_size=None) for (key, value) in six.iteritems(json_object): config.__dict__[key] = value return config
2,012
38.470588
78
py
models
models-master/official/legacy/bert/export_tfhub.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """A script to export BERT as a TF-Hub SavedModel. This script is **DEPRECATED** for exporting BERT encoder models; see the error message in by main() for details. """ from typing import Text # Import libraries from absl import app from absl import flags from absl import logging import tensorflow as tf from official.legacy.bert import bert_models from official.legacy.bert import configs FLAGS = flags.FLAGS flags.DEFINE_string("bert_config_file", None, "Bert configuration file to define core bert layers.") flags.DEFINE_string("model_checkpoint_path", None, "File path to TF model checkpoint.") flags.DEFINE_string("export_path", None, "TF-Hub SavedModel destination path.") flags.DEFINE_string("vocab_file", None, "The vocabulary file that the BERT model was trained on.") flags.DEFINE_bool( "do_lower_case", None, "Whether to lowercase. If None, " "do_lower_case will be enabled if 'uncased' appears in the " "name of --vocab_file") flags.DEFINE_enum("model_type", "encoder", ["encoder", "squad"], "What kind of BERT model to export.") def create_bert_model(bert_config: configs.BertConfig) -> tf.keras.Model: """Creates a BERT keras core model from BERT configuration. Args: bert_config: A `BertConfig` to create the core model. Returns: A keras model. """ # Adds input layers just as placeholders. input_word_ids = tf.keras.layers.Input( shape=(None,), dtype=tf.int32, name="input_word_ids") input_mask = tf.keras.layers.Input( shape=(None,), dtype=tf.int32, name="input_mask") input_type_ids = tf.keras.layers.Input( shape=(None,), dtype=tf.int32, name="input_type_ids") transformer_encoder = bert_models.get_transformer_encoder( bert_config, sequence_length=None) sequence_output, pooled_output = transformer_encoder( [input_word_ids, input_mask, input_type_ids]) # To keep consistent with legacy hub modules, the outputs are # "pooled_output" and "sequence_output". return tf.keras.Model( inputs=[input_word_ids, input_mask, input_type_ids], outputs=[pooled_output, sequence_output]), transformer_encoder def export_bert_tfhub(bert_config: configs.BertConfig, model_checkpoint_path: Text, hub_destination: Text, vocab_file: Text, do_lower_case: bool = None): """Restores a tf.keras.Model and saves for TF-Hub.""" # If do_lower_case is not explicit, default to checking whether "uncased" is # in the vocab file name if do_lower_case is None: do_lower_case = "uncased" in vocab_file logging.info("Using do_lower_case=%s based on name of vocab_file=%s", do_lower_case, vocab_file) core_model, encoder = create_bert_model(bert_config) checkpoint = tf.train.Checkpoint( model=encoder, # Legacy checkpoints. encoder=encoder) checkpoint.restore(model_checkpoint_path).assert_existing_objects_matched() core_model.vocab_file = tf.saved_model.Asset(vocab_file) core_model.do_lower_case = tf.Variable(do_lower_case, trainable=False) core_model.save(hub_destination, include_optimizer=False, save_format="tf") def export_bert_squad_tfhub(bert_config: configs.BertConfig, model_checkpoint_path: Text, hub_destination: Text, vocab_file: Text, do_lower_case: bool = None): """Restores a tf.keras.Model for BERT with SQuAD and saves for TF-Hub.""" # If do_lower_case is not explicit, default to checking whether "uncased" is # in the vocab file name if do_lower_case is None: do_lower_case = "uncased" in vocab_file logging.info("Using do_lower_case=%s based on name of vocab_file=%s", do_lower_case, vocab_file) span_labeling, _ = bert_models.squad_model(bert_config, max_seq_length=None) checkpoint = tf.train.Checkpoint(model=span_labeling) checkpoint.restore(model_checkpoint_path).assert_existing_objects_matched() span_labeling.vocab_file = tf.saved_model.Asset(vocab_file) span_labeling.do_lower_case = tf.Variable(do_lower_case, trainable=False) span_labeling.save(hub_destination, include_optimizer=False, save_format="tf") def main(_): bert_config = configs.BertConfig.from_json_file(FLAGS.bert_config_file) if FLAGS.model_type == "encoder": deprecation_note = ( "nlp/bert/export_tfhub is **DEPRECATED** for exporting BERT encoder " "models. Please switch to nlp/tools/export_tfhub for exporting BERT " "(and other) encoders with dict inputs/outputs conforming to " "https://www.tensorflow.org/hub/common_saved_model_apis/text#transformer-encoders" ) logging.error(deprecation_note) print("\n\nNOTICE:", deprecation_note, "\n") export_bert_tfhub(bert_config, FLAGS.model_checkpoint_path, FLAGS.export_path, FLAGS.vocab_file, FLAGS.do_lower_case) elif FLAGS.model_type == "squad": export_bert_squad_tfhub(bert_config, FLAGS.model_checkpoint_path, FLAGS.export_path, FLAGS.vocab_file, FLAGS.do_lower_case) else: raise ValueError("Unsupported model_type %s." % FLAGS.model_type) if __name__ == "__main__": app.run(main)
5,966
41.621429
90
py
models
models-master/official/legacy/bert/serving.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Examples of SavedModel export for tf-serving.""" from absl import app from absl import flags import tensorflow as tf from official.legacy.bert import bert_models from official.legacy.bert import configs flags.DEFINE_integer( "sequence_length", None, "Sequence length to parse the tf.Example. If " "sequence_length > 0, add a signature for serialized " "tf.Example and define the parsing specification by the " "sequence_length.") flags.DEFINE_string("bert_config_file", None, "Bert configuration file to define core bert layers.") flags.DEFINE_string("model_checkpoint_path", None, "File path to TF model checkpoint.") flags.DEFINE_string("export_path", None, "Destination folder to export the serving SavedModel.") FLAGS = flags.FLAGS class BertServing(tf.keras.Model): """Bert transformer encoder model for serving.""" def __init__(self, bert_config, name_to_features=None, name="serving_model"): super(BertServing, self).__init__(name=name) self.encoder = bert_models.get_transformer_encoder( bert_config, sequence_length=None) self.name_to_features = name_to_features def call(self, inputs): input_word_ids = inputs["input_ids"] input_mask = inputs["input_mask"] input_type_ids = inputs["segment_ids"] encoder_outputs, _ = self.encoder( [input_word_ids, input_mask, input_type_ids]) return encoder_outputs def serve_body(self, input_ids, input_mask=None, segment_ids=None): if segment_ids is None: # Requires CLS token is the first token of inputs. segment_ids = tf.zeros_like(input_ids) if input_mask is None: # The mask has 1 for real tokens and 0 for padding tokens. input_mask = tf.where( tf.equal(input_ids, 0), tf.zeros_like(input_ids), tf.ones_like(input_ids)) inputs = dict( input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids) return self.call(inputs) @tf.function def serve(self, input_ids, input_mask=None, segment_ids=None): outputs = self.serve_body(input_ids, input_mask, segment_ids) # Returns a dictionary to control SignatureDef output signature. return {"outputs": outputs[-1]} @tf.function def serve_examples(self, inputs): features = tf.io.parse_example(inputs, self.name_to_features) for key in list(features.keys()): t = features[key] if t.dtype == tf.int64: t = tf.cast(t, tf.int32) features[key] = t return self.serve( features["input_ids"], input_mask=features["input_mask"] if "input_mask" in features else None, segment_ids=features["segment_ids"] if "segment_ids" in features else None) @classmethod def export(cls, model, export_dir): if not isinstance(model, cls): raise ValueError("Invalid model instance: %s, it should be a %s" % (model, cls)) signatures = { "serving_default": model.serve.get_concrete_function( input_ids=tf.TensorSpec( shape=[None, None], dtype=tf.int32, name="inputs")), } if model.name_to_features: signatures[ "serving_examples"] = model.serve_examples.get_concrete_function( tf.TensorSpec(shape=[None], dtype=tf.string, name="examples")) tf.saved_model.save(model, export_dir=export_dir, signatures=signatures) def main(_): sequence_length = FLAGS.sequence_length if sequence_length is not None and sequence_length > 0: name_to_features = { "input_ids": tf.io.FixedLenFeature([sequence_length], tf.int64), "input_mask": tf.io.FixedLenFeature([sequence_length], tf.int64), "segment_ids": tf.io.FixedLenFeature([sequence_length], tf.int64), } else: name_to_features = None bert_config = configs.BertConfig.from_json_file(FLAGS.bert_config_file) serving_model = BertServing( bert_config=bert_config, name_to_features=name_to_features) checkpoint = tf.train.Checkpoint(model=serving_model.encoder) checkpoint.restore(FLAGS.model_checkpoint_path ).assert_existing_objects_matched().run_restore_ops() BertServing.export(serving_model, FLAGS.export_path) if __name__ == "__main__": flags.mark_flag_as_required("bert_config_file") flags.mark_flag_as_required("model_checkpoint_path") flags.mark_flag_as_required("export_path") app.run(main)
5,054
36.723881
80
py
models
models-master/official/legacy/bert/run_squad.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Run BERT on SQuAD 1.1 and SQuAD 2.0 in TF 2.x.""" import json import os import time # Import libraries from absl import app from absl import flags from absl import logging import gin import tensorflow as tf from official.common import distribute_utils from official.legacy.bert import configs as bert_configs from official.legacy.bert import run_squad_helper from official.nlp.data import squad_lib as squad_lib_wp from official.nlp.tools import tokenization from official.utils.misc import keras_utils flags.DEFINE_string('vocab_file', None, 'The vocabulary file that the BERT model was trained on.') # More flags can be found in run_squad_helper. run_squad_helper.define_common_squad_flags() FLAGS = flags.FLAGS def train_squad(strategy, input_meta_data, custom_callbacks=None, run_eagerly=False, init_checkpoint=None, sub_model_export_name=None): """Run bert squad training.""" bert_config = bert_configs.BertConfig.from_json_file(FLAGS.bert_config_file) init_checkpoint = init_checkpoint or FLAGS.init_checkpoint run_squad_helper.train_squad(strategy, input_meta_data, bert_config, custom_callbacks, run_eagerly, init_checkpoint, sub_model_export_name=sub_model_export_name) def predict_squad(strategy, input_meta_data): """Makes predictions for the squad dataset.""" bert_config = bert_configs.BertConfig.from_json_file(FLAGS.bert_config_file) tokenizer = tokenization.FullTokenizer( vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) run_squad_helper.predict_squad( strategy, input_meta_data, tokenizer, bert_config, squad_lib_wp) def eval_squad(strategy, input_meta_data): """Evaluate on the squad dataset.""" bert_config = bert_configs.BertConfig.from_json_file(FLAGS.bert_config_file) tokenizer = tokenization.FullTokenizer( vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) eval_metrics = run_squad_helper.eval_squad( strategy, input_meta_data, tokenizer, bert_config, squad_lib_wp) return eval_metrics def export_squad(model_export_path, input_meta_data): """Exports a trained model as a `SavedModel` for inference. Args: model_export_path: a string specifying the path to the SavedModel directory. input_meta_data: dictionary containing meta data about input and model. Raises: Export path is not specified, got an empty string or None. """ bert_config = bert_configs.BertConfig.from_json_file(FLAGS.bert_config_file) run_squad_helper.export_squad(model_export_path, input_meta_data, bert_config) def main(_): gin.parse_config_files_and_bindings(FLAGS.gin_file, FLAGS.gin_param) with tf.io.gfile.GFile(FLAGS.input_meta_data_path, 'rb') as reader: input_meta_data = json.loads(reader.read().decode('utf-8')) if FLAGS.mode == 'export_only': export_squad(FLAGS.model_export_path, input_meta_data) return # Configures cluster spec for multi-worker distribution strategy. if FLAGS.num_gpus > 0: _ = distribute_utils.configure_cluster(FLAGS.worker_hosts, FLAGS.task_index) strategy = distribute_utils.get_distribution_strategy( distribution_strategy=FLAGS.distribution_strategy, num_gpus=FLAGS.num_gpus, all_reduce_alg=FLAGS.all_reduce_alg, tpu_address=FLAGS.tpu) if 'train' in FLAGS.mode: if FLAGS.log_steps: custom_callbacks = [keras_utils.TimeHistory( batch_size=FLAGS.train_batch_size, log_steps=FLAGS.log_steps, logdir=FLAGS.model_dir, )] else: custom_callbacks = None train_squad( strategy, input_meta_data, custom_callbacks=custom_callbacks, run_eagerly=FLAGS.run_eagerly, sub_model_export_name=FLAGS.sub_model_export_name, ) if 'predict' in FLAGS.mode: predict_squad(strategy, input_meta_data) if 'eval' in FLAGS.mode: eval_metrics = eval_squad(strategy, input_meta_data) f1_score = eval_metrics['final_f1'] logging.info('SQuAD eval F1-score: %f', f1_score) summary_dir = os.path.join(FLAGS.model_dir, 'summaries', 'eval') summary_writer = tf.summary.create_file_writer(summary_dir) with summary_writer.as_default(): # TODO(lehou): write to the correct step number. tf.summary.scalar('F1-score', f1_score, step=0) summary_writer.flush() # Also write eval_metrics to json file. squad_lib_wp.write_to_json_files( eval_metrics, os.path.join(summary_dir, 'eval_metrics.json')) time.sleep(60) if __name__ == '__main__': flags.mark_flag_as_required('bert_config_file') flags.mark_flag_as_required('model_dir') app.run(main)
5,366
35.020134
80
py
models
models-master/official/legacy/bert/export_tfhub_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests official.nlp.bert.export_tfhub.""" import os from absl.testing import parameterized import numpy as np import tensorflow as tf import tensorflow_hub as hub from official.legacy.bert import configs from official.legacy.bert import export_tfhub class ExportTfhubTest(tf.test.TestCase, parameterized.TestCase): @parameterized.parameters("model", "encoder") def test_export_tfhub(self, ckpt_key_name): # Exports a savedmodel for TF-Hub hidden_size = 16 bert_config = configs.BertConfig( vocab_size=100, hidden_size=hidden_size, intermediate_size=32, max_position_embeddings=128, num_attention_heads=2, num_hidden_layers=1) bert_model, encoder = export_tfhub.create_bert_model(bert_config) model_checkpoint_dir = os.path.join(self.get_temp_dir(), "checkpoint") checkpoint = tf.train.Checkpoint(**{ckpt_key_name: encoder}) checkpoint.save(os.path.join(model_checkpoint_dir, "test")) model_checkpoint_path = tf.train.latest_checkpoint(model_checkpoint_dir) vocab_file = os.path.join(self.get_temp_dir(), "uncased_vocab.txt") with tf.io.gfile.GFile(vocab_file, "w") as f: f.write("dummy content") hub_destination = os.path.join(self.get_temp_dir(), "hub") export_tfhub.export_bert_tfhub(bert_config, model_checkpoint_path, hub_destination, vocab_file) # Restores a hub KerasLayer. hub_layer = hub.KerasLayer(hub_destination, trainable=True) if hasattr(hub_layer, "resolved_object"): # Checks meta attributes. self.assertTrue(hub_layer.resolved_object.do_lower_case.numpy()) with tf.io.gfile.GFile( hub_layer.resolved_object.vocab_file.asset_path.numpy()) as f: self.assertEqual("dummy content", f.read()) # Checks the hub KerasLayer. for source_weight, hub_weight in zip(bert_model.trainable_weights, hub_layer.trainable_weights): self.assertAllClose(source_weight.numpy(), hub_weight.numpy()) seq_length = 10 dummy_ids = np.zeros((2, seq_length), dtype=np.int32) hub_outputs = hub_layer([dummy_ids, dummy_ids, dummy_ids]) source_outputs = bert_model([dummy_ids, dummy_ids, dummy_ids]) # The outputs of hub module are "pooled_output" and "sequence_output", # while the outputs of encoder is in reversed order, i.e., # "sequence_output" and "pooled_output". encoder_outputs = reversed(encoder([dummy_ids, dummy_ids, dummy_ids])) self.assertEqual(hub_outputs[0].shape, (2, hidden_size)) self.assertEqual(hub_outputs[1].shape, (2, seq_length, hidden_size)) for source_output, hub_output, encoder_output in zip( source_outputs, hub_outputs, encoder_outputs): self.assertAllClose(source_output.numpy(), hub_output.numpy()) self.assertAllClose(source_output.numpy(), encoder_output.numpy()) # Test that training=True makes a difference (activates dropout). def _dropout_mean_stddev(training, num_runs=20): input_ids = np.array([[14, 12, 42, 95, 99]], np.int32) inputs = [input_ids, np.ones_like(input_ids), np.zeros_like(input_ids)] outputs = np.concatenate( [hub_layer(inputs, training=training)[0] for _ in range(num_runs)]) return np.mean(np.std(outputs, axis=0)) self.assertLess(_dropout_mean_stddev(training=False), 1e-6) self.assertGreater(_dropout_mean_stddev(training=True), 1e-3) # Test propagation of seq_length in shape inference. input_word_ids = tf.keras.layers.Input(shape=(seq_length,), dtype=tf.int32) input_mask = tf.keras.layers.Input(shape=(seq_length,), dtype=tf.int32) input_type_ids = tf.keras.layers.Input(shape=(seq_length,), dtype=tf.int32) pooled_output, sequence_output = hub_layer( [input_word_ids, input_mask, input_type_ids]) self.assertEqual(pooled_output.shape.as_list(), [None, hidden_size]) self.assertEqual(sequence_output.shape.as_list(), [None, seq_length, hidden_size]) if __name__ == "__main__": tf.test.main()
4,688
42.018349
79
py
models
models-master/official/legacy/bert/run_classifier.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """BERT classification or regression finetuning runner in TF 2.x.""" import functools import json import math import os # Import libraries from absl import app from absl import flags from absl import logging import gin import tensorflow as tf from official.common import distribute_utils from official.legacy.bert import bert_models from official.legacy.bert import common_flags from official.legacy.bert import configs as bert_configs from official.legacy.bert import input_pipeline from official.legacy.bert import model_saving_utils from official.modeling import performance from official.nlp import optimization from official.utils.misc import keras_utils flags.DEFINE_enum( 'mode', 'train_and_eval', ['train_and_eval', 'export_only', 'predict'], 'One of {"train_and_eval", "export_only", "predict"}. `train_and_eval`: ' 'trains the model and evaluates in the meantime. ' '`export_only`: will take the latest checkpoint inside ' 'model_dir and export a `SavedModel`. `predict`: takes a checkpoint and ' 'restores the model to output predictions on the test set.') flags.DEFINE_string('train_data_path', None, 'Path to training data for BERT classifier.') flags.DEFINE_string('eval_data_path', None, 'Path to evaluation data for BERT classifier.') flags.DEFINE_string( 'input_meta_data_path', None, 'Path to file that contains meta data about input ' 'to be used for training and evaluation.') flags.DEFINE_integer('train_data_size', None, 'Number of training samples ' 'to use. If None, uses the full train data. ' '(default: None).') flags.DEFINE_string('predict_checkpoint_path', None, 'Path to the checkpoint for predictions.') flags.DEFINE_integer( 'num_eval_per_epoch', 1, 'Number of evaluations per epoch. The purpose of this flag is to provide ' 'more granular evaluation scores and checkpoints. For example, if original ' 'data has N samples and num_eval_per_epoch is n, then each epoch will be ' 'evaluated every N/n samples.') flags.DEFINE_integer('train_batch_size', 32, 'Batch size for training.') flags.DEFINE_integer('eval_batch_size', 32, 'Batch size for evaluation.') common_flags.define_common_bert_flags() FLAGS = flags.FLAGS LABEL_TYPES_MAP = {'int': tf.int64, 'float': tf.float32} def get_loss_fn(num_classes): """Gets the classification loss function.""" def classification_loss_fn(labels, logits): """Classification loss.""" labels = tf.reshape(labels, [-1]) log_probs = tf.nn.log_softmax(logits, axis=-1) one_hot_labels = tf.one_hot( tf.cast(labels, dtype=tf.int32), depth=num_classes, dtype=tf.float32) per_example_loss = -tf.reduce_sum( tf.cast(one_hot_labels, dtype=tf.float32) * log_probs, axis=-1) return tf.reduce_mean(per_example_loss) return classification_loss_fn def get_dataset_fn(input_file_pattern, max_seq_length, global_batch_size, is_training, label_type=tf.int64, include_sample_weights=False, num_samples=None): """Gets a closure to create a dataset.""" def _dataset_fn(ctx=None): """Returns tf.data.Dataset for distributed BERT pretraining.""" batch_size = ctx.get_per_replica_batch_size( global_batch_size) if ctx else global_batch_size dataset = input_pipeline.create_classifier_dataset( tf.io.gfile.glob(input_file_pattern), max_seq_length, batch_size, is_training=is_training, input_pipeline_context=ctx, label_type=label_type, include_sample_weights=include_sample_weights, num_samples=num_samples) return dataset return _dataset_fn def run_bert_classifier(strategy, bert_config, input_meta_data, model_dir, epochs, steps_per_epoch, steps_per_loop, eval_steps, warmup_steps, initial_lr, init_checkpoint, train_input_fn, eval_input_fn, training_callbacks=True, custom_callbacks=None, custom_metrics=None): """Run BERT classifier training using low-level API.""" max_seq_length = input_meta_data['max_seq_length'] num_classes = input_meta_data.get('num_labels', 1) is_regression = num_classes == 1 def _get_classifier_model(): """Gets a classifier model.""" classifier_model, core_model = ( bert_models.classifier_model( bert_config, num_classes, max_seq_length, hub_module_url=FLAGS.hub_module_url, hub_module_trainable=FLAGS.hub_module_trainable)) optimizer = optimization.create_optimizer(initial_lr, steps_per_epoch * epochs, warmup_steps, FLAGS.end_lr, FLAGS.optimizer_type) classifier_model.optimizer = performance.configure_optimizer( optimizer, use_float16=common_flags.use_float16()) return classifier_model, core_model # tf.keras.losses objects accept optional sample_weight arguments (eg. coming # from the dataset) to compute weighted loss, as used for the regression # tasks. The classification tasks, using the custom get_loss_fn don't accept # sample weights though. loss_fn = (tf.keras.losses.MeanSquaredError() if is_regression else get_loss_fn(num_classes)) # Defines evaluation metrics function, which will create metrics in the # correct device and strategy scope. if custom_metrics: metric_fn = custom_metrics elif is_regression: metric_fn = functools.partial( tf.keras.metrics.MeanSquaredError, 'mean_squared_error', dtype=tf.float32) else: metric_fn = functools.partial( tf.keras.metrics.SparseCategoricalAccuracy, 'accuracy', dtype=tf.float32) # Start training using Keras compile/fit API. logging.info('Training using TF 2.x Keras compile/fit API with ' 'distribution strategy.') return run_keras_compile_fit( model_dir, strategy, _get_classifier_model, train_input_fn, eval_input_fn, loss_fn, metric_fn, init_checkpoint, epochs, steps_per_epoch, steps_per_loop, eval_steps, training_callbacks=training_callbacks, custom_callbacks=custom_callbacks) def run_keras_compile_fit(model_dir, strategy, model_fn, train_input_fn, eval_input_fn, loss_fn, metric_fn, init_checkpoint, epochs, steps_per_epoch, steps_per_loop, eval_steps, training_callbacks=True, custom_callbacks=None): """Runs BERT classifier model using Keras compile/fit API.""" with strategy.scope(): training_dataset = train_input_fn() evaluation_dataset = eval_input_fn() if eval_input_fn else None bert_model, sub_model = model_fn() optimizer = bert_model.optimizer if init_checkpoint: checkpoint = tf.train.Checkpoint(model=sub_model, encoder=sub_model) checkpoint.read(init_checkpoint).assert_existing_objects_matched() if not isinstance(metric_fn, (list, tuple)): metric_fn = [metric_fn] bert_model.compile( optimizer=optimizer, loss=loss_fn, metrics=[fn() for fn in metric_fn], steps_per_execution=steps_per_loop) summary_dir = os.path.join(model_dir, 'summaries') summary_callback = tf.keras.callbacks.TensorBoard(summary_dir) checkpoint = tf.train.Checkpoint(model=bert_model, optimizer=optimizer) checkpoint_manager = tf.train.CheckpointManager( checkpoint, directory=model_dir, max_to_keep=None, step_counter=optimizer.iterations, checkpoint_interval=0) checkpoint_callback = keras_utils.SimpleCheckpoint(checkpoint_manager) if training_callbacks: if custom_callbacks is not None: custom_callbacks += [summary_callback, checkpoint_callback] else: custom_callbacks = [summary_callback, checkpoint_callback] history = bert_model.fit( x=training_dataset, validation_data=evaluation_dataset, steps_per_epoch=steps_per_epoch, epochs=epochs, validation_steps=eval_steps, callbacks=custom_callbacks) stats = {'total_training_steps': steps_per_epoch * epochs} if 'loss' in history.history: stats['train_loss'] = history.history['loss'][-1] if 'val_accuracy' in history.history: stats['eval_metrics'] = history.history['val_accuracy'][-1] return bert_model, stats def get_predictions_and_labels(strategy, trained_model, eval_input_fn, is_regression=False, return_probs=False): """Obtains predictions of trained model on evaluation data. Note that list of labels is returned along with the predictions because the order changes on distributing dataset over TPU pods. Args: strategy: Distribution strategy. trained_model: Trained model with preloaded weights. eval_input_fn: Input function for evaluation data. is_regression: Whether it is a regression task. return_probs: Whether to return probabilities of classes. Returns: predictions: List of predictions. labels: List of gold labels corresponding to predictions. """ @tf.function def test_step(iterator): """Computes predictions on distributed devices.""" def _test_step_fn(inputs): """Replicated predictions.""" inputs, labels = inputs logits = trained_model(inputs, training=False) if not is_regression: probabilities = tf.nn.softmax(logits) return probabilities, labels else: return logits, labels outputs, labels = strategy.run(_test_step_fn, args=(next(iterator),)) # outputs: current batch logits as a tuple of shard logits outputs = tf.nest.map_structure(strategy.experimental_local_results, outputs) labels = tf.nest.map_structure(strategy.experimental_local_results, labels) return outputs, labels def _run_evaluation(test_iterator): """Runs evaluation steps.""" preds, golds = list(), list() try: with tf.experimental.async_scope(): while True: probabilities, labels = test_step(test_iterator) for cur_probs, cur_labels in zip(probabilities, labels): if return_probs: preds.extend(cur_probs.numpy().tolist()) else: preds.extend(tf.math.argmax(cur_probs, axis=1).numpy()) golds.extend(cur_labels.numpy().tolist()) except (StopIteration, tf.errors.OutOfRangeError): tf.experimental.async_clear_error() return preds, golds test_iter = iter(strategy.distribute_datasets_from_function(eval_input_fn)) predictions, labels = _run_evaluation(test_iter) return predictions, labels def export_classifier(model_export_path, input_meta_data, bert_config, model_dir): """Exports a trained model as a `SavedModel` for inference. Args: model_export_path: a string specifying the path to the SavedModel directory. input_meta_data: dictionary containing meta data about input and model. bert_config: Bert configuration file to define core bert layers. model_dir: The directory where the model weights and training/evaluation summaries are stored. Raises: Export path is not specified, got an empty string or None. """ if not model_export_path: raise ValueError('Export path is not specified: %s' % model_export_path) if not model_dir: raise ValueError('Export path is not specified: %s' % model_dir) # Export uses float32 for now, even if training uses mixed precision. tf.keras.mixed_precision.set_global_policy('float32') classifier_model = bert_models.classifier_model( bert_config, input_meta_data.get('num_labels', 1), hub_module_url=FLAGS.hub_module_url, hub_module_trainable=False)[0] model_saving_utils.export_bert_model( model_export_path, model=classifier_model, checkpoint_dir=model_dir) def run_bert(strategy, input_meta_data, model_config, train_input_fn=None, eval_input_fn=None, init_checkpoint=None, custom_callbacks=None, custom_metrics=None): """Run BERT training.""" # Enables XLA in Session Config. Should not be set for TPU. keras_utils.set_session_config(FLAGS.enable_xla) performance.set_mixed_precision_policy(common_flags.dtype()) epochs = FLAGS.num_train_epochs * FLAGS.num_eval_per_epoch train_data_size = ( input_meta_data['train_data_size'] // FLAGS.num_eval_per_epoch) if FLAGS.train_data_size: train_data_size = min(train_data_size, FLAGS.train_data_size) logging.info('Updated train_data_size: %s', train_data_size) steps_per_epoch = int(train_data_size / FLAGS.train_batch_size) warmup_steps = int(epochs * train_data_size * 0.1 / FLAGS.train_batch_size) eval_steps = int( math.ceil(input_meta_data['eval_data_size'] / FLAGS.eval_batch_size)) if not strategy: raise ValueError('Distribution strategy has not been specified.') if not custom_callbacks: custom_callbacks = [] if FLAGS.log_steps: custom_callbacks.append( keras_utils.TimeHistory( batch_size=FLAGS.train_batch_size, log_steps=FLAGS.log_steps, logdir=FLAGS.model_dir)) trained_model, _ = run_bert_classifier( strategy, model_config, input_meta_data, FLAGS.model_dir, epochs, steps_per_epoch, FLAGS.steps_per_loop, eval_steps, warmup_steps, FLAGS.learning_rate, init_checkpoint or FLAGS.init_checkpoint, train_input_fn, eval_input_fn, custom_callbacks=custom_callbacks, custom_metrics=custom_metrics) if FLAGS.model_export_path: model_saving_utils.export_bert_model( FLAGS.model_export_path, model=trained_model) return trained_model def custom_main(custom_callbacks=None, custom_metrics=None): """Run classification or regression. Args: custom_callbacks: list of tf.keras.Callbacks passed to training loop. custom_metrics: list of metrics passed to the training loop. """ gin.parse_config_files_and_bindings(FLAGS.gin_file, FLAGS.gin_param) with tf.io.gfile.GFile(FLAGS.input_meta_data_path, 'rb') as reader: input_meta_data = json.loads(reader.read().decode('utf-8')) label_type = LABEL_TYPES_MAP[input_meta_data.get('label_type', 'int')] include_sample_weights = input_meta_data.get('has_sample_weights', False) if not FLAGS.model_dir: FLAGS.model_dir = '/tmp/bert20/' bert_config = bert_configs.BertConfig.from_json_file(FLAGS.bert_config_file) if FLAGS.mode == 'export_only': export_classifier(FLAGS.model_export_path, input_meta_data, bert_config, FLAGS.model_dir) return strategy = distribute_utils.get_distribution_strategy( distribution_strategy=FLAGS.distribution_strategy, num_gpus=FLAGS.num_gpus, tpu_address=FLAGS.tpu) eval_input_fn = get_dataset_fn( FLAGS.eval_data_path, input_meta_data['max_seq_length'], FLAGS.eval_batch_size, is_training=False, label_type=label_type, include_sample_weights=include_sample_weights) if FLAGS.mode == 'predict': num_labels = input_meta_data.get('num_labels', 1) with strategy.scope(): classifier_model = bert_models.classifier_model( bert_config, num_labels)[0] checkpoint = tf.train.Checkpoint(model=classifier_model) latest_checkpoint_file = ( FLAGS.predict_checkpoint_path or tf.train.latest_checkpoint(FLAGS.model_dir)) assert latest_checkpoint_file logging.info('Checkpoint file %s found and restoring from ' 'checkpoint', latest_checkpoint_file) checkpoint.restore( latest_checkpoint_file).assert_existing_objects_matched() preds, _ = get_predictions_and_labels( strategy, classifier_model, eval_input_fn, is_regression=(num_labels == 1), return_probs=True) output_predict_file = os.path.join(FLAGS.model_dir, 'test_results.tsv') with tf.io.gfile.GFile(output_predict_file, 'w') as writer: logging.info('***** Predict results *****') for probabilities in preds: output_line = '\t'.join( str(class_probability) for class_probability in probabilities) + '\n' writer.write(output_line) return if FLAGS.mode != 'train_and_eval': raise ValueError('Unsupported mode is specified: %s' % FLAGS.mode) train_input_fn = get_dataset_fn( FLAGS.train_data_path, input_meta_data['max_seq_length'], FLAGS.train_batch_size, is_training=True, label_type=label_type, include_sample_weights=include_sample_weights, num_samples=FLAGS.train_data_size) run_bert( strategy, input_meta_data, bert_config, train_input_fn, eval_input_fn, custom_callbacks=custom_callbacks, custom_metrics=custom_metrics) def main(_): custom_main(custom_callbacks=None, custom_metrics=None) if __name__ == '__main__': flags.mark_flag_as_required('bert_config_file') flags.mark_flag_as_required('input_meta_data_path') flags.mark_flag_as_required('model_dir') app.run(main)
18,826
35.486434
80
py
models
models-master/official/legacy/bert/model_training_utils.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """A light weight utilities to train NLP models.""" import json import os import tempfile from absl import logging import tensorflow as tf from tensorflow.python.util import deprecation from official.common import distribute_utils from official.modeling import grad_utils _SUMMARY_TXT = 'training_summary.txt' _MIN_SUMMARY_STEPS = 10 def _should_export_checkpoint(strategy): return (not strategy) or strategy.extended.should_checkpoint def _should_export_summary(strategy): return (not strategy) or strategy.extended.should_save_summary def _save_checkpoint(strategy, checkpoint, model_dir, checkpoint_prefix): """Saves model to with provided checkpoint prefix.""" if _should_export_checkpoint(strategy): checkpoint_path = os.path.join(model_dir, checkpoint_prefix) saved_path = checkpoint.save(checkpoint_path) logging.info('Saving model as TF checkpoint: %s', saved_path) else: # In multi worker training we need every worker to save checkpoint, because # variables can trigger synchronization on read and synchronization needs # all workers to participate. To avoid workers overriding each other we save # to a temporary directory on non-chief workers. tmp_dir = tempfile.mkdtemp() checkpoint.save(os.path.join(tmp_dir, 'ckpt')) tf.io.gfile.rmtree(tmp_dir) return def _get_input_iterator(input_fn, strategy): """Returns distributed dataset iterator.""" # When training with TPU pods, datasets needs to be cloned across # workers. Since Dataset instance cannot be cloned in eager mode, we instead # pass callable that returns a dataset. if not callable(input_fn): raise ValueError('`input_fn` should be a closure that returns a dataset.') iterator = iter(strategy.distribute_datasets_from_function(input_fn)) return iterator def _float_metric_value(metric): """Gets the value of a float-value keras metric.""" return metric.result().numpy().astype(float) def clip_by_global_norm_callback(grads_and_vars): """Performs gradient clipping.""" grads, variables = zip(*grads_and_vars) (clipped_grads, _) = tf.clip_by_global_norm(grads, clip_norm=1.0) return zip(clipped_grads, variables) def steps_to_run(current_step, steps_per_epoch, steps_per_loop): """Calculates steps to run on device.""" if steps_per_loop <= 0: raise ValueError('steps_per_loop should be positive integer.') if steps_per_loop == 1: return steps_per_loop remainder_in_epoch = current_step % steps_per_epoch if remainder_in_epoch != 0: return min(steps_per_epoch - remainder_in_epoch, steps_per_loop) else: return steps_per_loop def write_txt_summary(training_summary, summary_dir): """Writes a summary text file to record stats.""" if not tf.io.gfile.exists(summary_dir): tf.io.gfile.mkdir(summary_dir) summary_path = os.path.join(summary_dir, _SUMMARY_TXT) with tf.io.gfile.GFile(summary_path, 'wb') as f: logging.info('Training Summary: \n%s', str(training_summary)) f.write(json.dumps(training_summary, indent=4)) @deprecation.deprecated( None, 'This function is deprecated and we do not expect adding new ' 'functionalities. Please do not have your code depending ' 'on this library.') def run_customized_training_loop( # pylint: disable=invalid-name _sentinel=None, # pylint: enable=invalid-name strategy=None, model_fn=None, loss_fn=None, scale_loss=True, model_dir=None, train_input_fn=None, steps_per_epoch=None, num_eval_per_epoch=1, steps_per_loop=None, epochs=1, eval_input_fn=None, eval_steps=None, metric_fn=None, init_checkpoint=None, custom_callbacks=None, run_eagerly=False, sub_model_export_name=None, explicit_allreduce=False, pre_allreduce_callbacks=None, post_allreduce_callbacks=None, train_summary_interval=0, allreduce_bytes_per_pack=0): """Run BERT pretrain model training using low-level API. Args: _sentinel: Used to prevent positional parameters. Internal, do not use. strategy: Distribution strategy on which to run low level training loop. model_fn: Function that returns a tuple (model, sub_model). Caller of this function should add optimizer to the `model` via calling `model.compile()` API or manually setting `model.optimizer` attribute. Second element of the returned tuple(sub_model) is an optional sub model to be used for initial checkpoint -- if provided. loss_fn: Function with signature func(labels, logits) and returns a loss tensor. scale_loss: Whether to divide the raw loss by number of replicas before gradients calculation. model_dir: Model directory used during training for restoring/saving model weights. train_input_fn: Function that returns a tf.data.Dataset used for training. steps_per_epoch: Number of steps to run per epoch. At the end of each epoch, model checkpoint will be saved and evaluation will be conducted if evaluation dataset is provided. num_eval_per_epoch: Number of evaluations per epoch. steps_per_loop: Number of steps per graph-mode loop. In order to reduce communication in eager context, training logs are printed every steps_per_loop. epochs: Number of epochs to train. eval_input_fn: Function that returns evaluation dataset. If none, evaluation is skipped. eval_steps: Number of steps to run evaluation. Required if `eval_input_fn` is not none. metric_fn: A metrics function that returns either a Keras Metric object or a list of Keras Metric objects to record evaluation result using evaluation dataset or with training dataset after every epoch. init_checkpoint: Optional checkpoint to load to `sub_model` returned by `model_fn`. custom_callbacks: A list of Keras Callbacks objects to run during training. More specifically, `on_train_begin(), on_train_end(), on_batch_begin()`, `on_batch_end()`, `on_epoch_begin()`, `on_epoch_end()` methods are invoked during training. Note that some metrics may be missing from `logs`. run_eagerly: Whether to run model training in pure eager execution. This should be disable for TPUStrategy. sub_model_export_name: If not None, will export `sub_model` returned by `model_fn` into checkpoint files. The name of intermediate checkpoint file is {sub_model_export_name}_step_{step}.ckpt and the last checkpint's name is {sub_model_export_name}.ckpt; if None, `sub_model` will not be exported as checkpoint. explicit_allreduce: Whether to explicitly perform gradient allreduce, instead of relying on implicit allreduce in optimizer.apply_gradients(). default is False. For now, if training using FP16 mixed precision, explicit allreduce will aggregate gradients in FP16 format. For TPU and GPU training using FP32, explicit allreduce will aggregate gradients in FP32 format. pre_allreduce_callbacks: A list of callback functions that takes gradients and model variables pairs as input, manipulate them, and returns a new gradients and model variables paris. The callback functions will be invoked in the list order and before gradients are allreduced. With mixed precision training, the pre_allreduce_allbacks will be applied on scaled_gradients. Default is no callbacks. Only used when explicit_allreduce=True. post_allreduce_callbacks: A list of callback functions that takes gradients and model variables pairs as input, manipulate them, and returns a new gradients and model variables paris. The callback functions will be invoked in the list order and right before gradients are applied to variables for updates. Default is no callbacks. Only used when explicit_allreduce=True. train_summary_interval: Step interval for training summaries. If the value is a negative number, then training summaries are not enabled. allreduce_bytes_per_pack: A non-negative integer. Breaks collective operations into packs of certain size. If it's zero, all gradients are in one pack. Breaking gradient into packs could enable overlap between allreduce and backprop computation. This flag only takes effect when explicit_allreduce is set to True.' Returns: Trained model. Raises: ValueError: (1) When model returned by `model_fn` does not have optimizer attribute or when required parameters are set to none. (2) eval args are not specified correctly. (3) metric_fn must be a callable if specified. (4) sub_model_checkpoint_name is specified, but `sub_model` returned by `model_fn` is None. """ if _sentinel is not None: raise ValueError('only call `run_customized_training_loop()` ' 'with named arguments.') required_arguments = [ strategy, model_fn, loss_fn, model_dir, steps_per_epoch, train_input_fn ] steps_between_evals = int(steps_per_epoch / num_eval_per_epoch) if [arg for arg in required_arguments if arg is None]: raise ValueError('`strategy`, `model_fn`, `loss_fn`, `model_dir`, ' '`steps_per_epoch` and `train_input_fn` are required ' 'parameters.') if not steps_per_loop: if tf.config.list_logical_devices('TPU'): # One can't fully utilize a TPU with steps_per_loop=1, so in this case # default users to a more useful value. steps_per_loop = min(1000, steps_between_evals) else: steps_per_loop = 1 logging.info('steps_per_loop not specified. Using steps_per_loop=%d', steps_per_loop) if steps_per_loop > steps_between_evals: logging.warning( 'steps_per_loop: %d is specified to be greater than ' ' steps_between_evals: %d, we will use steps_between_evals as' ' steps_per_loop.', steps_per_loop, steps_between_evals) steps_per_loop = steps_between_evals assert tf.executing_eagerly() if run_eagerly: if isinstance( strategy, (tf.distribute.TPUStrategy, tf.distribute.experimental.TPUStrategy)): raise ValueError( 'TPUStrategy should not run eagerly as it heavily relies on graph' ' optimization for the distributed system.') if eval_input_fn and eval_steps is None: raise ValueError( '`eval_step` is required when `eval_input_fn ` is not none.') if metric_fn and not callable(metric_fn): raise ValueError( 'if `metric_fn` is specified, metric_fn must be a callable.') total_training_steps = steps_per_epoch * epochs train_iterator = _get_input_iterator(train_input_fn, strategy) eval_loss_metric = tf.keras.metrics.Mean('training_loss', dtype=tf.float32) with distribute_utils.get_strategy_scope(strategy): # To correctly place the model weights on accelerators, # model and optimizer should be created in scope. model, sub_model = model_fn() if not hasattr(model, 'optimizer'): raise ValueError('User should set optimizer attribute to model ' 'inside `model_fn`.') if sub_model_export_name and sub_model is None: raise ValueError('sub_model_export_name is specified as %s, but ' 'sub_model is None.' % sub_model_export_name) callback_list = tf.keras.callbacks.CallbackList( callbacks=custom_callbacks, model=model) optimizer = model.optimizer if init_checkpoint: logging.info( 'Checkpoint file %s found and restoring from ' 'initial checkpoint for core model.', init_checkpoint) checkpoint = tf.train.Checkpoint(model=sub_model, encoder=sub_model) checkpoint.read(init_checkpoint).assert_existing_objects_matched() logging.info('Loading from checkpoint file completed') train_loss_metric = tf.keras.metrics.Mean('training_loss', dtype=tf.float32) eval_metrics = metric_fn() if metric_fn else [] if not isinstance(eval_metrics, list): eval_metrics = [eval_metrics] # If evaluation is required, make a copy of metric as it will be used by # both train and evaluation. train_metrics = [ metric.__class__.from_config(metric.get_config()) for metric in eval_metrics ] # Create summary writers if _should_export_summary(strategy): summary_dir = os.path.join(model_dir, 'summaries') else: # In multi worker training we need every worker to write summary, because # variables can trigger synchronization on read and synchronization needs # all workers to participate. summary_dir = tempfile.mkdtemp() eval_summary_writer = tf.summary.create_file_writer( os.path.join(summary_dir, 'eval')) last_summary_step = 0 if steps_per_loop >= _MIN_SUMMARY_STEPS and train_summary_interval >= 0: # Only writes summary when the stats are collected sufficiently over # enough steps. train_summary_writer = tf.summary.create_file_writer( os.path.join(summary_dir, 'train')) else: train_summary_writer = tf.summary.create_noop_writer() # Collects training variables. training_vars = model.trainable_variables def _replicated_step(inputs): """Replicated training step.""" inputs, labels = inputs with tf.GradientTape() as tape: model_outputs = model(inputs, training=True) loss = loss_fn(labels, model_outputs) # Raw loss is used for reporting in metrics/logs. raw_loss = loss if scale_loss: # Scales down the loss for gradients to be invariant from replicas. loss = loss / strategy.num_replicas_in_sync if explicit_allreduce: grad_utils.minimize_using_explicit_allreduce(tape, optimizer, loss, training_vars, pre_allreduce_callbacks, post_allreduce_callbacks, allreduce_bytes_per_pack) else: if isinstance(optimizer, tf.keras.mixed_precision.LossScaleOptimizer): with tape: scaled_loss = optimizer.get_scaled_loss(loss) scaled_grads = tape.gradient(scaled_loss, training_vars) grads = optimizer.get_unscaled_gradients(scaled_grads) else: grads = tape.gradient(loss, training_vars) optimizer.apply_gradients(zip(grads, training_vars)) # For reporting, the metric takes the mean of losses. train_loss_metric.update_state(raw_loss) for metric in train_metrics: metric.update_state(labels, model_outputs) @tf.function def train_steps(iterator, steps): """Performs distributed training steps in a loop. Args: iterator: the distributed iterator of training datasets. steps: an tf.int32 integer tensor to specify number of steps to run inside host training loop. Raises: ValueError: Any of the arguments or tensor shapes are invalid. """ if not isinstance(steps, tf.Tensor): raise ValueError('steps should be an Tensor. Python object may cause ' 'retracing.') for _ in tf.range(steps): strategy.run(_replicated_step, args=(next(iterator),)) def train_single_step(iterator): """Performs a distributed training step. Args: iterator: the distributed iterator of training datasets. Raises: ValueError: Any of the arguments or tensor shapes are invalid. """ strategy.run(_replicated_step, args=(next(iterator),)) def test_step(iterator): """Calculates evaluation metrics on distributed devices.""" def _test_step_fn(inputs): """Replicated accuracy calculation.""" inputs, labels = inputs model_outputs = model(inputs, training=False) for metric in eval_metrics: metric.update_state(labels, model_outputs) return model_outputs, labels outputs, labels = strategy.run(_test_step_fn, args=(next(iterator),)) outputs = tf.nest.map_structure(strategy.experimental_local_results, outputs) labels = tf.nest.map_structure(strategy.experimental_local_results, labels) return outputs, labels if not run_eagerly: train_single_step = tf.function(train_single_step) test_step = tf.function(test_step) def _run_evaluation(current_training_step, test_iterator): """Runs validation steps and aggregate metrics. Args: current_training_step: tf.int32 tensor containing the current step. test_iterator: distributed iterator of test datasets. Returns: A dict of metic names and values. """ # The last batch of the evaluation is often smaller than previous ones. # Moreover, in some distributed pieces it might even be empty. Therefore, # different from the way training_loss is calculated, it is needed to # gather all the logits and labels here to calculate the evaluation loss # outside. loss_list, loss_weights = list(), list() for _ in range(eval_steps): outputs, labels = test_step(test_iterator) for cur_logits, cur_labels in zip(outputs, labels): # This is to handle cases when cur_labels is not a single tensor, # but a dict of tensors. cur_weight = tf.shape(tf.nest.flatten(cur_labels)[0])[0] if cur_weight != 0: loss_list.append(loss_fn(cur_labels, cur_logits).numpy()) loss_weights.append(cur_weight) # The sample_weights are the actual number of examples in each batch, # a summation of numbers of examples in each replica if using # distributed training. eval_loss_metric.update_state(loss_list, sample_weight=loss_weights) logs = {} with eval_summary_writer.as_default(): for metric in [eval_loss_metric] + eval_metrics + model.metrics: metric_value = _float_metric_value(metric) logs[metric.name] = metric_value logging.info('Step: [%d] Validation %s = %f', current_training_step, metric.name, metric_value) tf.summary.scalar( metric.name, metric_value, step=current_training_step) eval_summary_writer.flush() return logs # Training loop starts here. checkpoint = tf.train.Checkpoint( model=model, optimizer=optimizer, global_step=optimizer.iterations) sub_model_checkpoint = tf.train.Checkpoint( model=sub_model, global_step=optimizer.iterations) if sub_model_export_name else None latest_checkpoint_file = tf.train.latest_checkpoint(model_dir) if latest_checkpoint_file: logging.info('Checkpoint file %s found and restoring from ' 'checkpoint', latest_checkpoint_file) checkpoint.restore(latest_checkpoint_file) logging.info('Loading from checkpoint file completed') current_step = optimizer.iterations.numpy() checkpoint_name = 'ctl_step_{step}.ckpt' logs = {} callback_list.on_train_begin() while current_step < total_training_steps and not model.stop_training: if current_step % steps_per_epoch == 0: callback_list.on_epoch_begin(int(current_step / steps_per_epoch) + 1) # Training loss/metric are taking average over steps inside micro # training loop. We reset the their values before each round. train_loss_metric.reset_states() for metric in train_metrics + model.metrics: metric.reset_states() callback_list.on_batch_begin(current_step) # Runs several steps in the host while loop. steps = steps_to_run(current_step, steps_between_evals, steps_per_loop) if tf.config.list_physical_devices('GPU'): # TODO(zongweiz): merge with train_steps once tf.while_loop # GPU performance bugs are fixed. for _ in range(steps): train_single_step(train_iterator) else: # Converts steps to a Tensor to avoid tf.function retracing. train_steps(train_iterator, tf.convert_to_tensor(steps, dtype=tf.int32)) train_loss = _float_metric_value(train_loss_metric) current_step += steps # Updates training logging. training_status = 'Train Step: %d/%d / loss = %s' % ( current_step, total_training_steps, train_loss) if current_step >= last_summary_step + train_summary_interval: summary_writer = train_summary_writer last_summary_step = current_step else: summary_writer = tf.summary.create_noop_writer() with summary_writer.as_default(): if callable(optimizer.learning_rate): tf.summary.scalar( 'learning_rate', optimizer.learning_rate(current_step), step=current_step) tf.summary.scalar(train_loss_metric.name, train_loss, step=current_step) for metric in train_metrics + model.metrics: metric_value = _float_metric_value(metric) training_status += ' %s = %f' % (metric.name, metric_value) tf.summary.scalar(metric.name, metric_value, step=current_step) summary_writer.flush() logging.info(training_status) # If no need for evaluation, we only call on_batch_end with train_loss, # this is to ensure we get granular global_step/sec on Tensorboard. if current_step % steps_between_evals: callback_list.on_batch_end(current_step - 1, {'loss': train_loss}) else: # Save a submodel with the step in the file name after each epoch. if sub_model_export_name: _save_checkpoint( strategy, sub_model_checkpoint, model_dir, '%s_step_%d.ckpt' % (sub_model_export_name, current_step)) # Save model checkpoints and run validation steps after each epoch # (with the exception of the final epoch which is handled after the # training loop). if current_step < total_training_steps: _save_checkpoint(strategy, checkpoint, model_dir, checkpoint_name.format(step=current_step)) if eval_input_fn: # Re-initialize evaluation metric. eval_loss_metric.reset_states() for metric in eval_metrics + model.metrics: metric.reset_states() logging.info('Running evaluation after step: %s.', current_step) logs = _run_evaluation(current_step, _get_input_iterator(eval_input_fn, strategy)) # We add train_loss here rather than call on_batch_end twice to make # sure that no duplicated values are generated. logs['loss'] = train_loss callback_list.on_batch_end(current_step - 1, logs) # Calls on_epoch_end after each real epoch ends to prevent mis-calculation # of training steps. if current_step % steps_per_epoch == 0: callback_list.on_epoch_end(int(current_step / steps_per_epoch), logs) if sub_model_export_name: _save_checkpoint(strategy, sub_model_checkpoint, model_dir, '%s.ckpt' % sub_model_export_name) _save_checkpoint(strategy, checkpoint, model_dir, checkpoint_name.format(step=current_step)) if eval_input_fn: # Re-initialize evaluation metric. eval_loss_metric.reset_states() for metric in eval_metrics + model.metrics: metric.reset_states() logging.info('Running final evaluation after training is complete.') logs = _run_evaluation(current_step, _get_input_iterator(eval_input_fn, strategy)) callback_list.on_epoch_end(int(current_step / steps_per_epoch), logs) training_summary = { 'total_training_steps': total_training_steps, 'train_loss': _float_metric_value(train_loss_metric), } for metric in model.metrics: training_summary[metric.name] = _float_metric_value(metric) if eval_metrics: training_summary['last_train_metrics'] = _float_metric_value( train_metrics[0]) training_summary['eval_metrics'] = _float_metric_value(eval_metrics[0]) write_txt_summary(training_summary, summary_dir) if not _should_export_summary(strategy): tf.io.gfile.rmtree(summary_dir) callback_list.on_train_end() return model
25,317
41.839255
80
py
models
models-master/official/legacy/bert/bert_models.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """BERT models that are compatible with TF 2.0.""" import gin import tensorflow as tf import tensorflow_hub as hub from official.legacy.albert import configs as albert_configs from official.legacy.bert import configs from official.modeling import tf_utils from official.nlp.modeling import models from official.nlp.modeling import networks class BertPretrainLossAndMetricLayer(tf.keras.layers.Layer): """Returns layer that computes custom loss and metrics for pretraining.""" def __init__(self, vocab_size, **kwargs): super(BertPretrainLossAndMetricLayer, self).__init__(**kwargs) self._vocab_size = vocab_size self.config = { 'vocab_size': vocab_size, } def _add_metrics(self, lm_output, lm_labels, lm_label_weights, lm_example_loss, sentence_output, sentence_labels, next_sentence_loss): """Adds metrics.""" masked_lm_accuracy = tf.keras.metrics.sparse_categorical_accuracy( lm_labels, lm_output) numerator = tf.reduce_sum(masked_lm_accuracy * lm_label_weights) denominator = tf.reduce_sum(lm_label_weights) + 1e-5 masked_lm_accuracy = numerator / denominator self.add_metric( masked_lm_accuracy, name='masked_lm_accuracy', aggregation='mean') self.add_metric(lm_example_loss, name='lm_example_loss', aggregation='mean') if sentence_labels is not None: next_sentence_accuracy = tf.keras.metrics.sparse_categorical_accuracy( sentence_labels, sentence_output) self.add_metric( next_sentence_accuracy, name='next_sentence_accuracy', aggregation='mean') if next_sentence_loss is not None: self.add_metric( next_sentence_loss, name='next_sentence_loss', aggregation='mean') def call(self, lm_output_logits, sentence_output_logits, lm_label_ids, lm_label_weights, sentence_labels=None): """Implements call() for the layer.""" lm_label_weights = tf.cast(lm_label_weights, tf.float32) lm_output_logits = tf.cast(lm_output_logits, tf.float32) lm_prediction_losses = tf.keras.losses.sparse_categorical_crossentropy( lm_label_ids, lm_output_logits, from_logits=True) lm_numerator_loss = tf.reduce_sum(lm_prediction_losses * lm_label_weights) lm_denominator_loss = tf.reduce_sum(lm_label_weights) mask_label_loss = tf.math.divide_no_nan(lm_numerator_loss, lm_denominator_loss) if sentence_labels is not None: sentence_output_logits = tf.cast(sentence_output_logits, tf.float32) sentence_loss = tf.keras.losses.sparse_categorical_crossentropy( sentence_labels, sentence_output_logits, from_logits=True) sentence_loss = tf.reduce_mean(sentence_loss) loss = mask_label_loss + sentence_loss else: sentence_loss = None loss = mask_label_loss batch_shape = tf.slice(tf.shape(lm_label_ids), [0], [1]) # TODO(hongkuny): Avoids the hack and switches add_loss. final_loss = tf.fill(batch_shape, loss) self._add_metrics(lm_output_logits, lm_label_ids, lm_label_weights, mask_label_loss, sentence_output_logits, sentence_labels, sentence_loss) return final_loss @gin.configurable def get_transformer_encoder(bert_config, sequence_length=None, transformer_encoder_cls=None, output_range=None): """Gets a 'TransformerEncoder' object. Args: bert_config: A 'modeling.BertConfig' or 'modeling.AlbertConfig' object. sequence_length: [Deprecated]. transformer_encoder_cls: A EncoderScaffold class. If it is None, uses the default BERT encoder implementation. output_range: the sequence output range, [0, output_range). Default setting is to return the entire sequence output. Returns: A encoder object. """ del sequence_length if transformer_encoder_cls is not None: # TODO(hongkuny): evaluate if it is better to put cfg definition in gin. embedding_cfg = dict( vocab_size=bert_config.vocab_size, type_vocab_size=bert_config.type_vocab_size, hidden_size=bert_config.hidden_size, max_seq_length=bert_config.max_position_embeddings, initializer=tf.keras.initializers.TruncatedNormal( stddev=bert_config.initializer_range), dropout_rate=bert_config.hidden_dropout_prob, ) hidden_cfg = dict( num_attention_heads=bert_config.num_attention_heads, intermediate_size=bert_config.intermediate_size, intermediate_activation=tf_utils.get_activation(bert_config.hidden_act), dropout_rate=bert_config.hidden_dropout_prob, attention_dropout_rate=bert_config.attention_probs_dropout_prob, kernel_initializer=tf.keras.initializers.TruncatedNormal( stddev=bert_config.initializer_range), ) kwargs = dict( embedding_cfg=embedding_cfg, hidden_cfg=hidden_cfg, num_hidden_instances=bert_config.num_hidden_layers, pooled_output_dim=bert_config.hidden_size, pooler_layer_initializer=tf.keras.initializers.TruncatedNormal( stddev=bert_config.initializer_range)) # Relies on gin configuration to define the Transformer encoder arguments. return transformer_encoder_cls(**kwargs) kwargs = dict( vocab_size=bert_config.vocab_size, hidden_size=bert_config.hidden_size, num_layers=bert_config.num_hidden_layers, num_attention_heads=bert_config.num_attention_heads, intermediate_size=bert_config.intermediate_size, activation=tf_utils.get_activation(bert_config.hidden_act), dropout_rate=bert_config.hidden_dropout_prob, attention_dropout_rate=bert_config.attention_probs_dropout_prob, max_sequence_length=bert_config.max_position_embeddings, type_vocab_size=bert_config.type_vocab_size, embedding_width=bert_config.embedding_size, initializer=tf.keras.initializers.TruncatedNormal( stddev=bert_config.initializer_range)) if isinstance(bert_config, albert_configs.AlbertConfig): return networks.AlbertEncoder(**kwargs) else: assert isinstance(bert_config, configs.BertConfig) kwargs['output_range'] = output_range return networks.BertEncoder(**kwargs) def pretrain_model(bert_config, seq_length, max_predictions_per_seq, initializer=None, use_next_sentence_label=True, return_core_pretrainer_model=False): """Returns model to be used for pre-training. Args: bert_config: Configuration that defines the core BERT model. seq_length: Maximum sequence length of the training data. max_predictions_per_seq: Maximum number of tokens in sequence to mask out and use for pretraining. initializer: Initializer for weights in BertPretrainer. use_next_sentence_label: Whether to use the next sentence label. return_core_pretrainer_model: Whether to also return the `BertPretrainer` object. Returns: A Tuple of (1) Pretraining model, (2) core BERT submodel from which to save weights after pretraining, and (3) optional core `BertPretrainer` object if argument `return_core_pretrainer_model` is True. """ input_word_ids = tf.keras.layers.Input( shape=(seq_length,), name='input_word_ids', dtype=tf.int32) input_mask = tf.keras.layers.Input( shape=(seq_length,), name='input_mask', dtype=tf.int32) input_type_ids = tf.keras.layers.Input( shape=(seq_length,), name='input_type_ids', dtype=tf.int32) masked_lm_positions = tf.keras.layers.Input( shape=(max_predictions_per_seq,), name='masked_lm_positions', dtype=tf.int32) masked_lm_ids = tf.keras.layers.Input( shape=(max_predictions_per_seq,), name='masked_lm_ids', dtype=tf.int32) masked_lm_weights = tf.keras.layers.Input( shape=(max_predictions_per_seq,), name='masked_lm_weights', dtype=tf.int32) if use_next_sentence_label: next_sentence_labels = tf.keras.layers.Input( shape=(1,), name='next_sentence_labels', dtype=tf.int32) else: next_sentence_labels = None transformer_encoder = get_transformer_encoder(bert_config, seq_length) if initializer is None: initializer = tf.keras.initializers.TruncatedNormal( stddev=bert_config.initializer_range) pretrainer_model = models.BertPretrainer( network=transformer_encoder, embedding_table=transformer_encoder.get_embedding_table(), num_classes=2, # The next sentence prediction label has two classes. activation=tf_utils.get_activation(bert_config.hidden_act), num_token_predictions=max_predictions_per_seq, initializer=initializer, output='logits') outputs = pretrainer_model( [input_word_ids, input_mask, input_type_ids, masked_lm_positions]) lm_output = outputs['masked_lm'] sentence_output = outputs['classification'] pretrain_loss_layer = BertPretrainLossAndMetricLayer( vocab_size=bert_config.vocab_size) output_loss = pretrain_loss_layer(lm_output, sentence_output, masked_lm_ids, masked_lm_weights, next_sentence_labels) inputs = { 'input_word_ids': input_word_ids, 'input_mask': input_mask, 'input_type_ids': input_type_ids, 'masked_lm_positions': masked_lm_positions, 'masked_lm_ids': masked_lm_ids, 'masked_lm_weights': masked_lm_weights, } if use_next_sentence_label: inputs['next_sentence_labels'] = next_sentence_labels keras_model = tf.keras.Model(inputs=inputs, outputs=output_loss) if return_core_pretrainer_model: return keras_model, transformer_encoder, pretrainer_model else: return keras_model, transformer_encoder def squad_model(bert_config, max_seq_length, initializer=None, hub_module_url=None, hub_module_trainable=True): """Returns BERT Squad model along with core BERT model to import weights. Args: bert_config: BertConfig, the config defines the core Bert model. max_seq_length: integer, the maximum input sequence length. initializer: Initializer for the final dense layer in the span labeler. Defaulted to TruncatedNormal initializer. hub_module_url: TF-Hub path/url to Bert module. hub_module_trainable: True to finetune layers in the hub module. Returns: A tuple of (1) keras model that outputs start logits and end logits and (2) the core BERT transformer encoder. """ if initializer is None: initializer = tf.keras.initializers.TruncatedNormal( stddev=bert_config.initializer_range) if not hub_module_url: bert_encoder = get_transformer_encoder(bert_config, max_seq_length) return models.BertSpanLabeler( network=bert_encoder, initializer=initializer), bert_encoder input_word_ids = tf.keras.layers.Input( shape=(max_seq_length,), dtype=tf.int32, name='input_word_ids') input_mask = tf.keras.layers.Input( shape=(max_seq_length,), dtype=tf.int32, name='input_mask') input_type_ids = tf.keras.layers.Input( shape=(max_seq_length,), dtype=tf.int32, name='input_type_ids') core_model = hub.KerasLayer(hub_module_url, trainable=hub_module_trainable) pooled_output, sequence_output = core_model( [input_word_ids, input_mask, input_type_ids]) bert_encoder = tf.keras.Model( inputs={ 'input_word_ids': input_word_ids, 'input_mask': input_mask, 'input_type_ids': input_type_ids, }, outputs=[sequence_output, pooled_output], name='core_model') return models.BertSpanLabeler( network=bert_encoder, initializer=initializer), bert_encoder def classifier_model(bert_config, num_labels, max_seq_length=None, final_layer_initializer=None, hub_module_url=None, hub_module_trainable=True): """BERT classifier model in functional API style. Construct a Keras model for predicting `num_labels` outputs from an input with maximum sequence length `max_seq_length`. Args: bert_config: BertConfig or AlbertConfig, the config defines the core BERT or ALBERT model. num_labels: integer, the number of classes. max_seq_length: integer, the maximum input sequence length. final_layer_initializer: Initializer for final dense layer. Defaulted TruncatedNormal initializer. hub_module_url: TF-Hub path/url to Bert module. hub_module_trainable: True to finetune layers in the hub module. Returns: Combined prediction model (words, mask, type) -> (one-hot labels) BERT sub-model (words, mask, type) -> (bert_outputs) """ if final_layer_initializer is not None: initializer = final_layer_initializer else: initializer = tf.keras.initializers.TruncatedNormal( stddev=bert_config.initializer_range) if not hub_module_url: bert_encoder = get_transformer_encoder( bert_config, max_seq_length, output_range=1) return models.BertClassifier( bert_encoder, num_classes=num_labels, dropout_rate=bert_config.hidden_dropout_prob, initializer=initializer), bert_encoder input_word_ids = tf.keras.layers.Input( shape=(max_seq_length,), dtype=tf.int32, name='input_word_ids') input_mask = tf.keras.layers.Input( shape=(max_seq_length,), dtype=tf.int32, name='input_mask') input_type_ids = tf.keras.layers.Input( shape=(max_seq_length,), dtype=tf.int32, name='input_type_ids') bert_model = hub.KerasLayer(hub_module_url, trainable=hub_module_trainable) pooled_output, _ = bert_model([input_word_ids, input_mask, input_type_ids]) output = tf.keras.layers.Dropout(rate=bert_config.hidden_dropout_prob)( pooled_output) output = tf.keras.layers.Dense( num_labels, kernel_initializer=initializer, name='output')( output) return tf.keras.Model( inputs={ 'input_word_ids': input_word_ids, 'input_mask': input_mask, 'input_type_ids': input_type_ids }, outputs=output), bert_model
14,931
39.797814
80
py
models
models-master/official/legacy/bert/bert_models_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import tensorflow as tf from official.legacy.bert import bert_models from official.legacy.bert import configs as bert_configs from official.nlp.modeling import networks class BertModelsTest(tf.test.TestCase): def setUp(self): super(BertModelsTest, self).setUp() self._bert_test_config = bert_configs.BertConfig( attention_probs_dropout_prob=0.0, hidden_act='gelu', hidden_dropout_prob=0.0, hidden_size=16, initializer_range=0.02, intermediate_size=32, max_position_embeddings=128, num_attention_heads=2, num_hidden_layers=2, type_vocab_size=2, vocab_size=30522) def test_pretrain_model(self): model, encoder = bert_models.pretrain_model( self._bert_test_config, seq_length=5, max_predictions_per_seq=2, initializer=None, use_next_sentence_label=True) self.assertIsInstance(model, tf.keras.Model) self.assertIsInstance(encoder, networks.BertEncoder) # model has one scalar output: loss value. self.assertEqual(model.output.shape.as_list(), [ None, ]) # Expect two output from encoder: sequence and classification output. self.assertIsInstance(encoder.output, list) self.assertLen(encoder.output, 2) # shape should be [batch size, hidden_size] self.assertEqual(encoder.output[1].shape.as_list(), [None, 16]) def test_squad_model(self): model, core_model = bert_models.squad_model( self._bert_test_config, max_seq_length=5, initializer=None, hub_module_url=None, hub_module_trainable=None) self.assertIsInstance(model, tf.keras.Model) self.assertIsInstance(core_model, tf.keras.Model) # Expect two output from model: start positions and end positions self.assertIsInstance(model.output, list) self.assertLen(model.output, 2) # Expect two output from core_model: sequence and classification output. self.assertIsInstance(core_model.output, list) self.assertLen(core_model.output, 2) # shape should be [batch size, None, hidden_size] self.assertEqual(core_model.output[0].shape.as_list(), [None, None, 16]) # shape should be [batch size, hidden_size] self.assertEqual(core_model.output[1].shape.as_list(), [None, 16]) def test_classifier_model(self): model, core_model = bert_models.classifier_model( self._bert_test_config, num_labels=3, max_seq_length=5, final_layer_initializer=None, hub_module_url=None, hub_module_trainable=None) self.assertIsInstance(model, tf.keras.Model) self.assertIsInstance(core_model, tf.keras.Model) # model has one classification output with num_labels=3. self.assertEqual(model.output.shape.as_list(), [None, 3]) # Expect two output from core_model: sequence and classification output. self.assertIsInstance(core_model.output, list) self.assertLen(core_model.output, 2) # shape should be [batch size, None, hidden_size] self.assertEqual(core_model.output[0].shape.as_list(), [None, None, 16]) # shape should be [batch size, hidden_size] self.assertEqual(core_model.output[1].shape.as_list(), [None, 16]) if __name__ == '__main__': tf.test.main()
3,883
35.299065
76
py
models
models-master/official/legacy/bert/common_flags.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Defining common flags used across all BERT models/applications.""" from absl import flags import tensorflow as tf from official.utils import hyperparams_flags from official.utils.flags import core as flags_core def define_common_bert_flags(): """Define common flags for BERT tasks.""" flags_core.define_base( data_dir=False, model_dir=True, clean=False, train_epochs=False, epochs_between_evals=False, stop_threshold=False, batch_size=False, num_gpu=True, export_dir=False, distribution_strategy=True, run_eagerly=True) flags_core.define_distribution() flags.DEFINE_string('bert_config_file', None, 'Bert configuration file to define core bert layers.') flags.DEFINE_string( 'model_export_path', None, 'Path to the directory, where trainined model will be ' 'exported.') flags.DEFINE_string('tpu', '', 'TPU address to connect to.') flags.DEFINE_string( 'init_checkpoint', None, 'Initial checkpoint (usually from a pre-trained BERT model).') flags.DEFINE_integer('num_train_epochs', 3, 'Total number of training epochs to perform.') flags.DEFINE_integer( 'steps_per_loop', None, 'Number of steps per graph-mode loop. Only training step ' 'happens inside the loop. Callbacks will not be called ' 'inside. If not set the value will be configured depending on the ' 'devices available.') flags.DEFINE_float('learning_rate', 5e-5, 'The initial learning rate for Adam.') flags.DEFINE_float('end_lr', 0.0, 'The end learning rate for learning rate decay.') flags.DEFINE_string('optimizer_type', 'adamw', 'The type of optimizer to use for training (adamw|lamb)') flags.DEFINE_boolean( 'scale_loss', False, 'Whether to divide the loss by number of replica inside the per-replica ' 'loss function.') flags.DEFINE_boolean( 'use_keras_compile_fit', False, 'If True, uses Keras compile/fit() API for training logic. Otherwise ' 'use custom training loop.') flags.DEFINE_string( 'hub_module_url', None, 'TF-Hub path/url to Bert module. ' 'If specified, init_checkpoint flag should not be used.') flags.DEFINE_bool('hub_module_trainable', True, 'True to make keras layers in the hub module trainable.') flags.DEFINE_string( 'sub_model_export_name', None, 'If set, `sub_model` checkpoints are exported into ' 'FLAGS.model_dir/FLAGS.sub_model_export_name.') flags.DEFINE_bool('explicit_allreduce', False, 'True to use explicit allreduce instead of the implicit ' 'allreduce in optimizer.apply_gradients(). If fp16 mixed ' 'precision training is used, this also enables allreduce ' 'gradients in fp16.') flags.DEFINE_integer('allreduce_bytes_per_pack', 0, 'Number of bytes of a gradient pack for allreduce. ' 'Should be positive integer, if set to 0, all ' 'gradients are in one pack. Breaking gradient into ' 'packs could enable overlap between allreduce and ' 'backprop computation. This flag only takes effect ' 'when explicit_allreduce is set to True.') flags_core.define_log_steps() # Adds flags for mixed precision and multi-worker training. flags_core.define_performance( num_parallel_calls=False, inter_op=False, intra_op=False, synthetic_data=False, max_train_steps=False, dtype=True, loss_scale=True, all_reduce_alg=True, num_packs=False, tf_gpu_thread_mode=True, datasets_num_private_threads=True, enable_xla=True, fp16_implementation=True, ) # Adds gin configuration flags. hyperparams_flags.define_gin_flags() def dtype(): return flags_core.get_tf_dtype(flags.FLAGS) def use_float16(): return flags_core.get_tf_dtype(flags.FLAGS) == tf.float16 def get_loss_scale(): return flags_core.get_loss_scale(flags.FLAGS, default_for_fp16='dynamic')
4,832
37.357143
79
py
models
models-master/official/legacy/bert/run_pretraining.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Run masked LM/next sentence pre-training for BERT in TF 2.x.""" # Import libraries from absl import app from absl import flags from absl import logging import gin import tensorflow as tf from official.common import distribute_utils from official.legacy.bert import bert_models from official.legacy.bert import common_flags from official.legacy.bert import configs from official.legacy.bert import input_pipeline from official.legacy.bert import model_training_utils from official.modeling import performance from official.nlp import optimization flags.DEFINE_string('input_files', None, 'File path to retrieve training data for pre-training.') # Model training specific flags. flags.DEFINE_integer( 'max_seq_length', 128, 'The maximum total input sequence length after WordPiece tokenization. ' 'Sequences longer than this will be truncated, and sequences shorter ' 'than this will be padded.') flags.DEFINE_integer('max_predictions_per_seq', 20, 'Maximum predictions per sequence_output.') flags.DEFINE_integer('train_batch_size', 32, 'Total batch size for training.') flags.DEFINE_integer('num_steps_per_epoch', 1000, 'Total number of training steps to run per epoch.') flags.DEFINE_float('warmup_steps', 10000, 'Warmup steps for Adam weight decay optimizer.') flags.DEFINE_bool('use_next_sentence_label', True, 'Whether to use next sentence label to compute final loss.') flags.DEFINE_bool('train_summary_interval', 0, 'Step interval for training ' 'summaries. If the value is a negative number, ' 'then training summaries are not enabled.') common_flags.define_common_bert_flags() FLAGS = flags.FLAGS def get_pretrain_dataset_fn(input_file_pattern, seq_length, max_predictions_per_seq, global_batch_size, use_next_sentence_label=True): """Returns input dataset from input file string.""" def _dataset_fn(ctx=None): """Returns tf.data.Dataset for distributed BERT pretraining.""" input_patterns = input_file_pattern.split(',') batch_size = ctx.get_per_replica_batch_size(global_batch_size) train_dataset = input_pipeline.create_pretrain_dataset( input_patterns, seq_length, max_predictions_per_seq, batch_size, is_training=True, input_pipeline_context=ctx, use_next_sentence_label=use_next_sentence_label) return train_dataset return _dataset_fn def get_loss_fn(): """Returns loss function for BERT pretraining.""" def _bert_pretrain_loss_fn(unused_labels, losses, **unused_args): return tf.reduce_mean(losses) return _bert_pretrain_loss_fn def run_customized_training(strategy, bert_config, init_checkpoint, max_seq_length, max_predictions_per_seq, model_dir, steps_per_epoch, steps_per_loop, epochs, initial_lr, warmup_steps, end_lr, optimizer_type, input_files, train_batch_size, use_next_sentence_label=True, train_summary_interval=0, custom_callbacks=None, explicit_allreduce=False, pre_allreduce_callbacks=None, post_allreduce_callbacks=None, allreduce_bytes_per_pack=0): """Run BERT pretrain model training using low-level API.""" train_input_fn = get_pretrain_dataset_fn(input_files, max_seq_length, max_predictions_per_seq, train_batch_size, use_next_sentence_label) def _get_pretrain_model(): """Gets a pretraining model.""" pretrain_model, core_model = bert_models.pretrain_model( bert_config, max_seq_length, max_predictions_per_seq, use_next_sentence_label=use_next_sentence_label) optimizer = optimization.create_optimizer( initial_lr, steps_per_epoch * epochs, warmup_steps, end_lr, optimizer_type) pretrain_model.optimizer = performance.configure_optimizer( optimizer, use_float16=common_flags.use_float16()) return pretrain_model, core_model trained_model = model_training_utils.run_customized_training_loop( strategy=strategy, model_fn=_get_pretrain_model, loss_fn=get_loss_fn(), scale_loss=FLAGS.scale_loss, model_dir=model_dir, init_checkpoint=init_checkpoint, train_input_fn=train_input_fn, steps_per_epoch=steps_per_epoch, steps_per_loop=steps_per_loop, epochs=epochs, sub_model_export_name='pretrained/bert_model', explicit_allreduce=explicit_allreduce, pre_allreduce_callbacks=pre_allreduce_callbacks, post_allreduce_callbacks=post_allreduce_callbacks, allreduce_bytes_per_pack=allreduce_bytes_per_pack, train_summary_interval=train_summary_interval, custom_callbacks=custom_callbacks) return trained_model def run_bert_pretrain(strategy, custom_callbacks=None): """Runs BERT pre-training.""" bert_config = configs.BertConfig.from_json_file(FLAGS.bert_config_file) if not strategy: raise ValueError('Distribution strategy is not specified.') # Runs customized training loop. logging.info('Training using customized training loop TF 2.0 with distributed' 'strategy.') performance.set_mixed_precision_policy(common_flags.dtype()) # Only when explicit_allreduce = True, post_allreduce_callbacks and # allreduce_bytes_per_pack will take effect. optimizer.apply_gradients() no # longer implicitly allreduce gradients, users manually allreduce gradient and # pass the allreduced grads_and_vars to apply_gradients(). # With explicit_allreduce = True, clip_by_global_norm is moved to after # allreduce. return run_customized_training( strategy, bert_config, FLAGS.init_checkpoint, # Used to initialize only the BERT submodel. FLAGS.max_seq_length, FLAGS.max_predictions_per_seq, FLAGS.model_dir, FLAGS.num_steps_per_epoch, FLAGS.steps_per_loop, FLAGS.num_train_epochs, FLAGS.learning_rate, FLAGS.warmup_steps, FLAGS.end_lr, FLAGS.optimizer_type, FLAGS.input_files, FLAGS.train_batch_size, FLAGS.use_next_sentence_label, FLAGS.train_summary_interval, custom_callbacks=custom_callbacks, explicit_allreduce=FLAGS.explicit_allreduce, pre_allreduce_callbacks=[ model_training_utils.clip_by_global_norm_callback ], allreduce_bytes_per_pack=FLAGS.allreduce_bytes_per_pack) def main(_): gin.parse_config_files_and_bindings(FLAGS.gin_file, FLAGS.gin_param) if not FLAGS.model_dir: FLAGS.model_dir = '/tmp/bert20/' # Configures cluster spec for multi-worker distribution strategy. if FLAGS.num_gpus > 0: _ = distribute_utils.configure_cluster(FLAGS.worker_hosts, FLAGS.task_index) strategy = distribute_utils.get_distribution_strategy( distribution_strategy=FLAGS.distribution_strategy, num_gpus=FLAGS.num_gpus, all_reduce_alg=FLAGS.all_reduce_alg, tpu_address=FLAGS.tpu) if strategy: print('***** Number of cores used : ', strategy.num_replicas_in_sync) run_bert_pretrain(strategy) if __name__ == '__main__': app.run(main)
8,410
37.582569
80
py
models
models-master/official/legacy/bert/model_training_utils_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for official.modeling.training.model_training_utils.""" import os from absl import logging from absl.testing import flagsaver from absl.testing import parameterized from absl.testing.absltest import mock import numpy as np import tensorflow as tf from tensorflow.python.distribute import combinations from tensorflow.python.distribute import strategy_combinations from official.legacy.bert import common_flags from official.legacy.bert import model_training_utils common_flags.define_common_bert_flags() def eager_strategy_combinations(): return combinations.combine( distribution=[ strategy_combinations.default_strategy, strategy_combinations.cloud_tpu_strategy, strategy_combinations.one_device_strategy_gpu, strategy_combinations.mirrored_strategy_with_gpu_and_cpu, strategy_combinations.mirrored_strategy_with_two_gpus, ],) def eager_gpu_strategy_combinations(): return combinations.combine( distribution=[ strategy_combinations.default_strategy, strategy_combinations.one_device_strategy_gpu, strategy_combinations.mirrored_strategy_with_gpu_and_cpu, strategy_combinations.mirrored_strategy_with_two_gpus, ],) def create_fake_data_input_fn(batch_size, features_shape, num_classes): """Creates a dummy input function with the given feature and label shapes. Args: batch_size: integer. features_shape: list[int]. Feature shape for an individual example. num_classes: integer. Number of labels. Returns: An input function that is usable in the executor. """ def _dataset_fn(input_context=None): """An input function for generating fake data.""" local_batch_size = input_context.get_per_replica_batch_size(batch_size) features = np.random.rand(64, *features_shape) labels = np.random.randint(2, size=[64, num_classes]) # Convert the inputs to a Dataset. dataset = tf.data.Dataset.from_tensor_slices((features, labels)) dataset = dataset.shard(input_context.num_input_pipelines, input_context.input_pipeline_id) def _assign_dtype(features, labels): features = tf.cast(features, tf.float32) labels = tf.cast(labels, tf.float32) return features, labels # Shuffle, repeat, and batch the examples. dataset = dataset.map(_assign_dtype) dataset = dataset.shuffle(64).repeat() dataset = dataset.batch(local_batch_size, drop_remainder=True) dataset = dataset.prefetch(buffer_size=64) return dataset return _dataset_fn def create_model_fn(input_shape, num_classes, use_float16=False): def _model_fn(): """A one-layer softmax model suitable for testing.""" input_layer = tf.keras.layers.Input(shape=input_shape) x = tf.keras.layers.Dense(num_classes, activation='relu')(input_layer) output_layer = tf.keras.layers.Dense(num_classes, activation='softmax')(x) sub_model = tf.keras.models.Model(input_layer, x, name='sub_model') model = tf.keras.models.Model(input_layer, output_layer, name='model') model.add_metric( tf.reduce_mean(input_layer), name='mean_input', aggregation='mean') model.optimizer = tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9) if use_float16: model.optimizer = tf.keras.mixed_precision.LossScaleOptimizer( model.optimizer) return model, sub_model return _model_fn def metric_fn(): """Gets a tf.keras metric object.""" return tf.keras.metrics.CategoricalAccuracy(name='accuracy', dtype=tf.float32) def summaries_with_matching_keyword(keyword, summary_dir): """Yields summary protos matching given keyword from event file.""" event_paths = tf.io.gfile.glob(os.path.join(summary_dir, 'events*')) for event in tf.compat.v1.train.summary_iterator(event_paths[-1]): if event.summary is not None: for value in event.summary.value: if keyword in value.tag: logging.error(event) yield event.summary def check_eventfile_for_keyword(keyword, summary_dir): """Checks event files for the keyword.""" return any(summaries_with_matching_keyword(keyword, summary_dir)) class RecordingCallback(tf.keras.callbacks.Callback): def __init__(self): self.batch_begin = [] # (batch, logs) self.batch_end = [] # (batch, logs) self.epoch_begin = [] # (epoch, logs) self.epoch_end = [] # (epoch, logs) def on_batch_begin(self, batch, logs=None): self.batch_begin.append((batch, logs)) def on_batch_end(self, batch, logs=None): self.batch_end.append((batch, logs)) def on_epoch_begin(self, epoch, logs=None): self.epoch_begin.append((epoch, logs)) def on_epoch_end(self, epoch, logs=None): self.epoch_end.append((epoch, logs)) class ModelTrainingUtilsTest(tf.test.TestCase, parameterized.TestCase): def setUp(self): super(ModelTrainingUtilsTest, self).setUp() self._model_fn = create_model_fn(input_shape=[128], num_classes=3) @flagsaver.flagsaver def run_training(self, strategy, model_dir, steps_per_loop, run_eagerly): input_fn = create_fake_data_input_fn( batch_size=8, features_shape=[128], num_classes=3) model_training_utils.run_customized_training_loop( strategy=strategy, model_fn=self._model_fn, loss_fn=tf.keras.losses.categorical_crossentropy, model_dir=model_dir, steps_per_epoch=20, steps_per_loop=steps_per_loop, epochs=2, train_input_fn=input_fn, eval_input_fn=input_fn, eval_steps=10, init_checkpoint=None, sub_model_export_name='my_submodel_name', metric_fn=metric_fn, custom_callbacks=None, run_eagerly=run_eagerly) @combinations.generate(eager_strategy_combinations()) def test_train_eager_single_step(self, distribution): model_dir = self.create_tempdir().full_path if isinstance( distribution, (tf.distribute.TPUStrategy, tf.distribute.experimental.TPUStrategy)): with self.assertRaises(ValueError): self.run_training( distribution, model_dir, steps_per_loop=1, run_eagerly=True) else: self.run_training( distribution, model_dir, steps_per_loop=1, run_eagerly=True) @combinations.generate(eager_gpu_strategy_combinations()) def test_train_eager_mixed_precision(self, distribution): model_dir = self.create_tempdir().full_path tf.keras.mixed_precision.set_global_policy('mixed_float16') self._model_fn = create_model_fn( input_shape=[128], num_classes=3, use_float16=True) self.run_training( distribution, model_dir, steps_per_loop=1, run_eagerly=True) @combinations.generate(eager_strategy_combinations()) def test_train_check_artifacts(self, distribution): model_dir = self.create_tempdir().full_path self.run_training( distribution, model_dir, steps_per_loop=10, run_eagerly=False) # Two checkpoints should be saved after two epochs. files = map(os.path.basename, tf.io.gfile.glob(os.path.join(model_dir, 'ctl_step_*index'))) self.assertCountEqual( ['ctl_step_20.ckpt-1.index', 'ctl_step_40.ckpt-2.index'], files) # Three submodel checkpoints should be saved after two epochs (one after # each epoch plus one final). files = map( os.path.basename, tf.io.gfile.glob(os.path.join(model_dir, 'my_submodel_name*index'))) self.assertCountEqual([ 'my_submodel_name.ckpt-3.index', 'my_submodel_name_step_20.ckpt-1.index', 'my_submodel_name_step_40.ckpt-2.index' ], files) self.assertNotEmpty( tf.io.gfile.glob( os.path.join(model_dir, 'summaries/training_summary*'))) # Loss and accuracy values should be written into summaries. self.assertTrue( check_eventfile_for_keyword('loss', os.path.join(model_dir, 'summaries/train'))) self.assertTrue( check_eventfile_for_keyword('accuracy', os.path.join(model_dir, 'summaries/train'))) self.assertTrue( check_eventfile_for_keyword('mean_input', os.path.join(model_dir, 'summaries/train'))) self.assertTrue( check_eventfile_for_keyword('accuracy', os.path.join(model_dir, 'summaries/eval'))) self.assertTrue( check_eventfile_for_keyword('mean_input', os.path.join(model_dir, 'summaries/eval'))) @combinations.generate(eager_strategy_combinations()) def test_train_check_callbacks(self, distribution): model_dir = self.create_tempdir().full_path callback = RecordingCallback() callbacks = [callback] input_fn = create_fake_data_input_fn( batch_size=8, features_shape=[128], num_classes=3) model_training_utils.run_customized_training_loop( strategy=distribution, model_fn=self._model_fn, loss_fn=tf.keras.losses.categorical_crossentropy, model_dir=model_dir, steps_per_epoch=20, num_eval_per_epoch=4, steps_per_loop=10, epochs=2, train_input_fn=input_fn, eval_input_fn=input_fn, eval_steps=10, init_checkpoint=None, metric_fn=metric_fn, custom_callbacks=callbacks, run_eagerly=False) self.assertEqual(callback.epoch_begin, [(1, {}), (2, {})]) epoch_ends, epoch_end_infos = zip(*callback.epoch_end) self.assertEqual(list(epoch_ends), [1, 2, 2]) for info in epoch_end_infos: self.assertIn('accuracy', info) self.assertEqual(callback.batch_begin, [(0, {}), (5, {}), (10, {}), (15, {}), (20, {}), (25, {}), (30, {}), (35, {})]) batch_ends, batch_end_infos = zip(*callback.batch_end) self.assertEqual(list(batch_ends), [4, 9, 14, 19, 24, 29, 34, 39]) for info in batch_end_infos: self.assertIn('loss', info) @combinations.generate( combinations.combine( distribution=[ strategy_combinations.one_device_strategy_gpu, ],)) def test_train_check_artifacts_non_chief(self, distribution): # We shouldn't export artifacts on non-chief workers. Since there's no easy # way to test with real MultiWorkerMirroredStrategy, we patch the strategy # to make it as if it's MultiWorkerMirroredStrategy on non-chief workers. extended = distribution.extended with mock.patch.object(extended.__class__, 'should_checkpoint', new_callable=mock.PropertyMock, return_value=False), \ mock.patch.object(extended.__class__, 'should_save_summary', new_callable=mock.PropertyMock, return_value=False): model_dir = self.create_tempdir().full_path self.run_training( distribution, model_dir, steps_per_loop=10, run_eagerly=False) self.assertEmpty(tf.io.gfile.listdir(model_dir)) if __name__ == '__main__': tf.test.main()
11,705
37.130293
81
py
models
models-master/official/legacy/bert/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
610
37.1875
74
py
models
models-master/official/legacy/bert/run_squad_helper.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Library for running BERT family models on SQuAD 1.1/2.0 in TF 2.x.""" import collections import json import os from absl import flags from absl import logging import tensorflow as tf from official.legacy.bert import bert_models from official.legacy.bert import common_flags from official.legacy.bert import input_pipeline from official.legacy.bert import model_saving_utils from official.legacy.bert import model_training_utils from official.modeling import performance from official.nlp import optimization from official.nlp.data import squad_lib_sp from official.nlp.tools import squad_evaluate_v1_1 from official.nlp.tools import squad_evaluate_v2_0 from official.utils.misc import keras_utils def define_common_squad_flags(): """Defines common flags used by SQuAD tasks.""" flags.DEFINE_enum( 'mode', 'train_and_eval', [ 'train_and_eval', 'train_and_predict', 'train', 'eval', 'predict', 'export_only' ], 'One of {"train_and_eval", "train_and_predict", ' '"train", "eval", "predict", "export_only"}. ' '`train_and_eval`: train & predict to json files & compute eval metrics. ' '`train_and_predict`: train & predict to json files. ' '`train`: only trains the model. ' '`eval`: predict answers from squad json file & compute eval metrics. ' '`predict`: predict answers from the squad json file. ' '`export_only`: will take the latest checkpoint inside ' 'model_dir and export a `SavedModel`.') flags.DEFINE_string('train_data_path', '', 'Training data path with train tfrecords.') flags.DEFINE_string( 'input_meta_data_path', None, 'Path to file that contains meta data about input ' 'to be used for training and evaluation.') # Model training specific flags. flags.DEFINE_integer('train_batch_size', 32, 'Total batch size for training.') # Predict processing related. flags.DEFINE_string( 'predict_file', None, 'SQuAD prediction json file path. ' '`predict` mode supports multiple files: one can use ' 'wildcard to specify multiple files and it can also be ' 'multiple file patterns separated by comma. Note that ' '`eval` mode only supports a single predict file.') flags.DEFINE_bool( 'do_lower_case', True, 'Whether to lower case the input text. Should be True for uncased ' 'models and False for cased models.') flags.DEFINE_float( 'null_score_diff_threshold', 0.0, 'If null_score - best_non_null is greater than the threshold, ' 'predict null. This is only used for SQuAD v2.') flags.DEFINE_bool( 'verbose_logging', False, 'If true, all of the warnings related to data processing will be ' 'printed. A number of warnings are expected for a normal SQuAD ' 'evaluation.') flags.DEFINE_integer('predict_batch_size', 8, 'Total batch size for prediction.') flags.DEFINE_integer( 'n_best_size', 20, 'The total number of n-best predictions to generate in the ' 'nbest_predictions.json output file.') flags.DEFINE_integer( 'max_answer_length', 30, 'The maximum length of an answer that can be generated. This is needed ' 'because the start and end predictions are not conditioned on one ' 'another.') common_flags.define_common_bert_flags() FLAGS = flags.FLAGS def squad_loss_fn(start_positions, end_positions, start_logits, end_logits): """Returns sparse categorical crossentropy for start/end logits.""" start_loss = tf.keras.losses.sparse_categorical_crossentropy( start_positions, start_logits, from_logits=True) end_loss = tf.keras.losses.sparse_categorical_crossentropy( end_positions, end_logits, from_logits=True) total_loss = (tf.reduce_mean(start_loss) + tf.reduce_mean(end_loss)) / 2 return total_loss def get_loss_fn(): """Gets a loss function for squad task.""" def _loss_fn(labels, model_outputs): start_positions = labels['start_positions'] end_positions = labels['end_positions'] start_logits, end_logits = model_outputs return squad_loss_fn(start_positions, end_positions, start_logits, end_logits) return _loss_fn RawResult = collections.namedtuple('RawResult', ['unique_id', 'start_logits', 'end_logits']) def get_raw_results(predictions): """Converts multi-replica predictions to RawResult.""" for unique_ids, start_logits, end_logits in zip(predictions['unique_ids'], predictions['start_logits'], predictions['end_logits']): for values in zip(unique_ids.numpy(), start_logits.numpy(), end_logits.numpy()): yield RawResult( unique_id=values[0], start_logits=values[1].tolist(), end_logits=values[2].tolist()) def get_dataset_fn(input_file_pattern, max_seq_length, global_batch_size, is_training): """Gets a closure to create a dataset..""" def _dataset_fn(ctx=None): """Returns tf.data.Dataset for distributed BERT pretraining.""" batch_size = ctx.get_per_replica_batch_size( global_batch_size) if ctx else global_batch_size dataset = input_pipeline.create_squad_dataset( input_file_pattern, max_seq_length, batch_size, is_training=is_training, input_pipeline_context=ctx) return dataset return _dataset_fn def get_squad_model_to_predict(strategy, bert_config, checkpoint_path, input_meta_data): """Gets a squad model to make predictions.""" with strategy.scope(): # Prediction always uses float32, even if training uses mixed precision. tf.keras.mixed_precision.set_global_policy('float32') squad_model, _ = bert_models.squad_model( bert_config, input_meta_data['max_seq_length'], hub_module_url=FLAGS.hub_module_url) if checkpoint_path is None: checkpoint_path = tf.train.latest_checkpoint(FLAGS.model_dir) logging.info('Restoring checkpoints from %s', checkpoint_path) checkpoint = tf.train.Checkpoint(model=squad_model) checkpoint.restore(checkpoint_path).expect_partial() return squad_model def predict_squad_customized(strategy, input_meta_data, predict_tfrecord_path, num_steps, squad_model): """Make predictions using a Bert-based squad model.""" predict_dataset_fn = get_dataset_fn( predict_tfrecord_path, input_meta_data['max_seq_length'], FLAGS.predict_batch_size, is_training=False) predict_iterator = iter( strategy.distribute_datasets_from_function(predict_dataset_fn)) @tf.function def predict_step(iterator): """Predicts on distributed devices.""" def _replicated_step(inputs): """Replicated prediction calculation.""" x, _ = inputs unique_ids = x.pop('unique_ids') start_logits, end_logits = squad_model(x, training=False) return dict( unique_ids=unique_ids, start_logits=start_logits, end_logits=end_logits) outputs = strategy.run(_replicated_step, args=(next(iterator),)) return tf.nest.map_structure(strategy.experimental_local_results, outputs) all_results = [] for _ in range(num_steps): predictions = predict_step(predict_iterator) for result in get_raw_results(predictions): all_results.append(result) if len(all_results) % 100 == 0: logging.info('Made predictions for %d records.', len(all_results)) return all_results def train_squad(strategy, input_meta_data, bert_config, custom_callbacks=None, run_eagerly=False, init_checkpoint=None, sub_model_export_name=None): """Run bert squad training.""" if strategy: logging.info('Training using customized training loop with distribution' ' strategy.') # Enables XLA in Session Config. Should not be set for TPU. keras_utils.set_session_config(FLAGS.enable_xla) performance.set_mixed_precision_policy(common_flags.dtype()) epochs = FLAGS.num_train_epochs num_train_examples = input_meta_data['train_data_size'] max_seq_length = input_meta_data['max_seq_length'] steps_per_epoch = int(num_train_examples / FLAGS.train_batch_size) warmup_steps = int(epochs * num_train_examples * 0.1 / FLAGS.train_batch_size) train_input_fn = get_dataset_fn( FLAGS.train_data_path, max_seq_length, FLAGS.train_batch_size, is_training=True) def _get_squad_model(): """Get Squad model and optimizer.""" squad_model, core_model = bert_models.squad_model( bert_config, max_seq_length, hub_module_url=FLAGS.hub_module_url, hub_module_trainable=FLAGS.hub_module_trainable) optimizer = optimization.create_optimizer(FLAGS.learning_rate, steps_per_epoch * epochs, warmup_steps, FLAGS.end_lr, FLAGS.optimizer_type) squad_model.optimizer = performance.configure_optimizer( optimizer, use_float16=common_flags.use_float16()) return squad_model, core_model # Only when explicit_allreduce = True, post_allreduce_callbacks and # allreduce_bytes_per_pack will take effect. optimizer.apply_gradients() no # longer implicitly allreduce gradients, users manually allreduce gradient and # pass the allreduced grads_and_vars to apply_gradients(). # With explicit_allreduce = True, clip_by_global_norm is moved to after # allreduce. model_training_utils.run_customized_training_loop( strategy=strategy, model_fn=_get_squad_model, loss_fn=get_loss_fn(), model_dir=FLAGS.model_dir, steps_per_epoch=steps_per_epoch, steps_per_loop=FLAGS.steps_per_loop, epochs=epochs, train_input_fn=train_input_fn, init_checkpoint=init_checkpoint or FLAGS.init_checkpoint, sub_model_export_name=sub_model_export_name, run_eagerly=run_eagerly, custom_callbacks=custom_callbacks, explicit_allreduce=FLAGS.explicit_allreduce, pre_allreduce_callbacks=[ model_training_utils.clip_by_global_norm_callback ], allreduce_bytes_per_pack=FLAGS.allreduce_bytes_per_pack) def prediction_output_squad(strategy, input_meta_data, tokenizer, squad_lib, predict_file, squad_model): """Makes predictions for a squad dataset.""" doc_stride = input_meta_data['doc_stride'] max_query_length = input_meta_data['max_query_length'] # Whether data should be in Ver 2.0 format. version_2_with_negative = input_meta_data.get('version_2_with_negative', False) eval_examples = squad_lib.read_squad_examples( input_file=predict_file, is_training=False, version_2_with_negative=version_2_with_negative) eval_writer = squad_lib.FeatureWriter( filename=os.path.join(FLAGS.model_dir, 'eval.tf_record'), is_training=False) eval_features = [] def _append_feature(feature, is_padding): if not is_padding: eval_features.append(feature) eval_writer.process_feature(feature) # TPU requires a fixed batch size for all batches, therefore the number # of examples must be a multiple of the batch size, or else examples # will get dropped. So we pad with fake examples which are ignored # later on. kwargs = dict( examples=eval_examples, tokenizer=tokenizer, max_seq_length=input_meta_data['max_seq_length'], doc_stride=doc_stride, max_query_length=max_query_length, is_training=False, output_fn=_append_feature, batch_size=FLAGS.predict_batch_size) # squad_lib_sp requires one more argument 'do_lower_case'. if squad_lib == squad_lib_sp: kwargs['do_lower_case'] = FLAGS.do_lower_case dataset_size = squad_lib.convert_examples_to_features(**kwargs) eval_writer.close() logging.info('***** Running predictions *****') logging.info(' Num orig examples = %d', len(eval_examples)) logging.info(' Num split examples = %d', len(eval_features)) logging.info(' Batch size = %d', FLAGS.predict_batch_size) num_steps = int(dataset_size / FLAGS.predict_batch_size) all_results = predict_squad_customized(strategy, input_meta_data, eval_writer.filename, num_steps, squad_model) all_predictions, all_nbest_json, scores_diff_json = ( squad_lib.postprocess_output( eval_examples, eval_features, all_results, FLAGS.n_best_size, FLAGS.max_answer_length, FLAGS.do_lower_case, version_2_with_negative=version_2_with_negative, null_score_diff_threshold=FLAGS.null_score_diff_threshold, verbose=FLAGS.verbose_logging)) return all_predictions, all_nbest_json, scores_diff_json def dump_to_files(all_predictions, all_nbest_json, scores_diff_json, squad_lib, version_2_with_negative, file_prefix=''): """Save output to json files.""" output_prediction_file = os.path.join(FLAGS.model_dir, '%spredictions.json' % file_prefix) output_nbest_file = os.path.join(FLAGS.model_dir, '%snbest_predictions.json' % file_prefix) output_null_log_odds_file = os.path.join(FLAGS.model_dir, file_prefix, '%snull_odds.json' % file_prefix) logging.info('Writing predictions to: %s', (output_prediction_file)) logging.info('Writing nbest to: %s', (output_nbest_file)) squad_lib.write_to_json_files(all_predictions, output_prediction_file) squad_lib.write_to_json_files(all_nbest_json, output_nbest_file) if version_2_with_negative: squad_lib.write_to_json_files(scores_diff_json, output_null_log_odds_file) def _get_matched_files(input_path): """Returns all files that matches the input_path.""" input_patterns = input_path.strip().split(',') all_matched_files = [] for input_pattern in input_patterns: input_pattern = input_pattern.strip() if not input_pattern: continue matched_files = tf.io.gfile.glob(input_pattern) if not matched_files: raise ValueError('%s does not match any files.' % input_pattern) else: all_matched_files.extend(matched_files) return sorted(all_matched_files) def predict_squad(strategy, input_meta_data, tokenizer, bert_config, squad_lib, init_checkpoint=None): """Get prediction results and evaluate them to hard drive.""" if init_checkpoint is None: init_checkpoint = tf.train.latest_checkpoint(FLAGS.model_dir) all_predict_files = _get_matched_files(FLAGS.predict_file) squad_model = get_squad_model_to_predict(strategy, bert_config, init_checkpoint, input_meta_data) for idx, predict_file in enumerate(all_predict_files): all_predictions, all_nbest_json, scores_diff_json = prediction_output_squad( strategy, input_meta_data, tokenizer, squad_lib, predict_file, squad_model) if len(all_predict_files) == 1: file_prefix = '' else: # if predict_file is /path/xquad.ar.json, the `file_prefix` may be # "xquad.ar-0-" file_prefix = '%s-' % os.path.splitext( os.path.basename(all_predict_files[idx]))[0] dump_to_files(all_predictions, all_nbest_json, scores_diff_json, squad_lib, input_meta_data.get('version_2_with_negative', False), file_prefix) def eval_squad(strategy, input_meta_data, tokenizer, bert_config, squad_lib, init_checkpoint=None): """Get prediction results and evaluate them against ground truth.""" if init_checkpoint is None: init_checkpoint = tf.train.latest_checkpoint(FLAGS.model_dir) all_predict_files = _get_matched_files(FLAGS.predict_file) if len(all_predict_files) != 1: raise ValueError('`eval_squad` only supports one predict file, ' 'but got %s' % all_predict_files) squad_model = get_squad_model_to_predict(strategy, bert_config, init_checkpoint, input_meta_data) all_predictions, all_nbest_json, scores_diff_json = prediction_output_squad( strategy, input_meta_data, tokenizer, squad_lib, all_predict_files[0], squad_model) dump_to_files(all_predictions, all_nbest_json, scores_diff_json, squad_lib, input_meta_data.get('version_2_with_negative', False)) with tf.io.gfile.GFile(FLAGS.predict_file, 'r') as reader: dataset_json = json.load(reader) pred_dataset = dataset_json['data'] if input_meta_data.get('version_2_with_negative', False): eval_metrics = squad_evaluate_v2_0.evaluate(pred_dataset, all_predictions, scores_diff_json) else: eval_metrics = squad_evaluate_v1_1.evaluate(pred_dataset, all_predictions) return eval_metrics def export_squad(model_export_path, input_meta_data, bert_config): """Exports a trained model as a `SavedModel` for inference. Args: model_export_path: a string specifying the path to the SavedModel directory. input_meta_data: dictionary containing meta data about input and model. bert_config: Bert configuration file to define core bert layers. Raises: Export path is not specified, got an empty string or None. """ if not model_export_path: raise ValueError('Export path is not specified: %s' % model_export_path) # Export uses float32 for now, even if training uses mixed precision. tf.keras.mixed_precision.set_global_policy('float32') squad_model, _ = bert_models.squad_model(bert_config, input_meta_data['max_seq_length']) model_saving_utils.export_bert_model( model_export_path, model=squad_model, checkpoint_dir=FLAGS.model_dir)
18,945
39.139831
80
py
models
models-master/official/legacy/bert/configs.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """The main BERT model and related functions.""" import copy import json import six import tensorflow as tf class BertConfig(object): """Configuration for `BertModel`.""" def __init__(self, vocab_size, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, hidden_act="gelu", hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=512, type_vocab_size=16, initializer_range=0.02, embedding_size=None, backward_compatible=True): """Constructs BertConfig. Args: vocab_size: Vocabulary size of `inputs_ids` in `BertModel`. hidden_size: Size of the encoder layers and the pooler layer. num_hidden_layers: Number of hidden layers in the Transformer encoder. num_attention_heads: Number of attention heads for each attention layer in the Transformer encoder. intermediate_size: The size of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder. hidden_act: The non-linear activation function (function or string) in the encoder and pooler. hidden_dropout_prob: The dropout probability for all fully connected layers in the embeddings, encoder, and pooler. attention_probs_dropout_prob: The dropout ratio for the attention probabilities. max_position_embeddings: The maximum sequence length that this model might ever be used with. Typically set this to something large just in case (e.g., 512 or 1024 or 2048). type_vocab_size: The vocabulary size of the `token_type_ids` passed into `BertModel`. initializer_range: The stdev of the truncated_normal_initializer for initializing all weight matrices. embedding_size: (Optional) width of the factorized word embeddings. backward_compatible: Boolean, whether the variables shape are compatible with checkpoints converted from TF 1.x BERT. """ self.vocab_size = vocab_size self.hidden_size = hidden_size self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads self.hidden_act = hidden_act self.intermediate_size = intermediate_size self.hidden_dropout_prob = hidden_dropout_prob self.attention_probs_dropout_prob = attention_probs_dropout_prob self.max_position_embeddings = max_position_embeddings self.type_vocab_size = type_vocab_size self.initializer_range = initializer_range self.embedding_size = embedding_size self.backward_compatible = backward_compatible @classmethod def from_dict(cls, json_object): """Constructs a `BertConfig` from a Python dictionary of parameters.""" config = BertConfig(vocab_size=None) for (key, value) in six.iteritems(json_object): config.__dict__[key] = value return config @classmethod def from_json_file(cls, json_file): """Constructs a `BertConfig` from a json file of parameters.""" with tf.io.gfile.GFile(json_file, "r") as reader: text = reader.read() return cls.from_dict(json.loads(text)) def to_dict(self): """Serializes this instance to a Python dictionary.""" output = copy.deepcopy(self.__dict__) return output def to_json_string(self): """Serializes this instance to a JSON string.""" return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"
4,167
38.695238
80
py
models
models-master/official/legacy/bert/model_saving_utils.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Utilities to save models.""" import os import typing from absl import logging import tensorflow as tf def export_bert_model(model_export_path: typing.Text, model: tf.keras.Model, checkpoint_dir: typing.Optional[typing.Text] = None, restore_model_using_load_weights: bool = False) -> None: """Export BERT model for serving which does not include the optimizer. Args: model_export_path: Path to which exported model will be saved. model: Keras model object to export. checkpoint_dir: Path from which model weights will be loaded, if specified. restore_model_using_load_weights: Whether to use checkpoint.restore() API for custom checkpoint or to use model.load_weights() API. There are 2 different ways to save checkpoints. One is using tf.train.Checkpoint and another is using Keras model.save_weights(). Custom training loop implementation uses tf.train.Checkpoint API and Keras ModelCheckpoint callback internally uses model.save_weights() API. Since these two API's cannot be used toghether, model loading logic must be take into account how model checkpoint was saved. Raises: ValueError when either model_export_path or model is not specified. """ if not model_export_path: raise ValueError('model_export_path must be specified.') if not isinstance(model, tf.keras.Model): raise ValueError('model must be a tf.keras.Model object.') if checkpoint_dir: if restore_model_using_load_weights: model_weight_path = os.path.join(checkpoint_dir, 'checkpoint') assert tf.io.gfile.exists(model_weight_path) model.load_weights(model_weight_path) else: checkpoint = tf.train.Checkpoint(model=model) # Restores the model from latest checkpoint. latest_checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir) assert latest_checkpoint_file logging.info('Checkpoint file %s found and restoring from ' 'checkpoint', latest_checkpoint_file) checkpoint.restore( latest_checkpoint_file).assert_existing_objects_matched() model.save(model_export_path, include_optimizer=False, save_format='tf')
2,875
41.294118
80
py
models
models-master/official/legacy/bert/input_pipeline.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """BERT model input pipelines.""" import tensorflow as tf def decode_record(record, name_to_features): """Decodes a record to a TensorFlow example.""" example = tf.io.parse_single_example(record, name_to_features) # tf.Example only supports tf.int64, but the TPU only supports tf.int32. # So cast all int64 to int32. for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.cast(t, tf.int32) example[name] = t return example def single_file_dataset(input_file, name_to_features, num_samples=None): """Creates a single-file dataset to be passed for BERT custom training.""" # For training, we want a lot of parallel reading and shuffling. # For eval, we want no shuffling and parallel reading doesn't matter. d = tf.data.TFRecordDataset(input_file) if num_samples: d = d.take(num_samples) d = d.map( lambda record: decode_record(record, name_to_features), num_parallel_calls=tf.data.experimental.AUTOTUNE) # When `input_file` is a path to a single file or a list # containing a single path, disable auto sharding so that # same input file is sent to all workers. if isinstance(input_file, str) or len(input_file) == 1: options = tf.data.Options() options.experimental_distribute.auto_shard_policy = ( tf.data.experimental.AutoShardPolicy.OFF) d = d.with_options(options) return d def create_pretrain_dataset(input_patterns, seq_length, max_predictions_per_seq, batch_size, is_training=True, input_pipeline_context=None, use_next_sentence_label=True, use_position_id=False, output_fake_labels=True): """Creates input dataset from (tf)records files for pretraining.""" name_to_features = { 'input_ids': tf.io.FixedLenFeature([seq_length], tf.int64), 'input_mask': tf.io.FixedLenFeature([seq_length], tf.int64), 'segment_ids': tf.io.FixedLenFeature([seq_length], tf.int64), 'masked_lm_positions': tf.io.FixedLenFeature([max_predictions_per_seq], tf.int64), 'masked_lm_ids': tf.io.FixedLenFeature([max_predictions_per_seq], tf.int64), 'masked_lm_weights': tf.io.FixedLenFeature([max_predictions_per_seq], tf.float32), } if use_next_sentence_label: name_to_features['next_sentence_labels'] = tf.io.FixedLenFeature([1], tf.int64) if use_position_id: name_to_features['position_ids'] = tf.io.FixedLenFeature([seq_length], tf.int64) for input_pattern in input_patterns: if not tf.io.gfile.glob(input_pattern): raise ValueError('%s does not match any files.' % input_pattern) dataset = tf.data.Dataset.list_files(input_patterns, shuffle=is_training) if input_pipeline_context and input_pipeline_context.num_input_pipelines > 1: dataset = dataset.shard(input_pipeline_context.num_input_pipelines, input_pipeline_context.input_pipeline_id) if is_training: dataset = dataset.repeat() # We set shuffle buffer to exactly match total number of # training files to ensure that training data is well shuffled. input_files = [] for input_pattern in input_patterns: input_files.extend(tf.io.gfile.glob(input_pattern)) dataset = dataset.shuffle(len(input_files)) # In parallel, create tf record dataset for each train files. # cycle_length = 8 means that up to 8 files will be read and deserialized in # parallel. You may want to increase this number if you have a large number of # CPU cores. dataset = dataset.interleave( tf.data.TFRecordDataset, cycle_length=8, num_parallel_calls=tf.data.experimental.AUTOTUNE) if is_training: dataset = dataset.shuffle(100) decode_fn = lambda record: decode_record(record, name_to_features) dataset = dataset.map( decode_fn, num_parallel_calls=tf.data.experimental.AUTOTUNE) def _select_data_from_record(record): """Filter out features to use for pretraining.""" x = { 'input_word_ids': record['input_ids'], 'input_mask': record['input_mask'], 'input_type_ids': record['segment_ids'], 'masked_lm_positions': record['masked_lm_positions'], 'masked_lm_ids': record['masked_lm_ids'], 'masked_lm_weights': record['masked_lm_weights'], } if use_next_sentence_label: x['next_sentence_labels'] = record['next_sentence_labels'] if use_position_id: x['position_ids'] = record['position_ids'] # TODO(hongkuny): Remove the fake labels after migrating bert pretraining. if output_fake_labels: return (x, record['masked_lm_weights']) else: return x dataset = dataset.map( _select_data_from_record, num_parallel_calls=tf.data.experimental.AUTOTUNE) dataset = dataset.batch(batch_size, drop_remainder=is_training) dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE) return dataset def create_classifier_dataset(file_path, seq_length, batch_size, is_training=True, input_pipeline_context=None, label_type=tf.int64, include_sample_weights=False, num_samples=None): """Creates input dataset from (tf)records files for train/eval.""" name_to_features = { 'input_ids': tf.io.FixedLenFeature([seq_length], tf.int64), 'input_mask': tf.io.FixedLenFeature([seq_length], tf.int64), 'segment_ids': tf.io.FixedLenFeature([seq_length], tf.int64), 'label_ids': tf.io.FixedLenFeature([], label_type), } if include_sample_weights: name_to_features['weight'] = tf.io.FixedLenFeature([], tf.float32) dataset = single_file_dataset(file_path, name_to_features, num_samples=num_samples) # The dataset is always sharded by number of hosts. # num_input_pipelines is the number of hosts rather than number of cores. if input_pipeline_context and input_pipeline_context.num_input_pipelines > 1: dataset = dataset.shard(input_pipeline_context.num_input_pipelines, input_pipeline_context.input_pipeline_id) def _select_data_from_record(record): x = { 'input_word_ids': record['input_ids'], 'input_mask': record['input_mask'], 'input_type_ids': record['segment_ids'] } y = record['label_ids'] if include_sample_weights: w = record['weight'] return (x, y, w) return (x, y) if is_training: dataset = dataset.shuffle(100) dataset = dataset.repeat() dataset = dataset.map( _select_data_from_record, num_parallel_calls=tf.data.experimental.AUTOTUNE) dataset = dataset.batch(batch_size, drop_remainder=is_training) dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE) return dataset def create_squad_dataset(file_path, seq_length, batch_size, is_training=True, input_pipeline_context=None): """Creates input dataset from (tf)records files for train/eval.""" name_to_features = { 'input_ids': tf.io.FixedLenFeature([seq_length], tf.int64), 'input_mask': tf.io.FixedLenFeature([seq_length], tf.int64), 'segment_ids': tf.io.FixedLenFeature([seq_length], tf.int64), } if is_training: name_to_features['start_positions'] = tf.io.FixedLenFeature([], tf.int64) name_to_features['end_positions'] = tf.io.FixedLenFeature([], tf.int64) else: name_to_features['unique_ids'] = tf.io.FixedLenFeature([], tf.int64) dataset = single_file_dataset(file_path, name_to_features) # The dataset is always sharded by number of hosts. # num_input_pipelines is the number of hosts rather than number of cores. if input_pipeline_context and input_pipeline_context.num_input_pipelines > 1: dataset = dataset.shard(input_pipeline_context.num_input_pipelines, input_pipeline_context.input_pipeline_id) def _select_data_from_record(record): """Dispatches record to features and labels.""" x, y = {}, {} for name, tensor in record.items(): if name in ('start_positions', 'end_positions'): y[name] = tensor elif name == 'input_ids': x['input_word_ids'] = tensor elif name == 'segment_ids': x['input_type_ids'] = tensor else: x[name] = tensor return (x, y) if is_training: dataset = dataset.shuffle(100) dataset = dataset.repeat() dataset = dataset.map( _select_data_from_record, num_parallel_calls=tf.data.experimental.AUTOTUNE) dataset = dataset.batch(batch_size, drop_remainder=True) dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE) return dataset def create_retrieval_dataset(file_path, seq_length, batch_size, input_pipeline_context=None): """Creates input dataset from (tf)records files for scoring.""" name_to_features = { 'input_ids': tf.io.FixedLenFeature([seq_length], tf.int64), 'input_mask': tf.io.FixedLenFeature([seq_length], tf.int64), 'segment_ids': tf.io.FixedLenFeature([seq_length], tf.int64), 'example_id': tf.io.FixedLenFeature([1], tf.int64), } dataset = single_file_dataset(file_path, name_to_features) # The dataset is always sharded by number of hosts. # num_input_pipelines is the number of hosts rather than number of cores. if input_pipeline_context and input_pipeline_context.num_input_pipelines > 1: dataset = dataset.shard(input_pipeline_context.num_input_pipelines, input_pipeline_context.input_pipeline_id) def _select_data_from_record(record): x = { 'input_word_ids': record['input_ids'], 'input_mask': record['input_mask'], 'input_type_ids': record['segment_ids'] } y = record['example_id'] return (x, y) dataset = dataset.map( _select_data_from_record, num_parallel_calls=tf.data.experimental.AUTOTUNE) dataset = dataset.batch(batch_size, drop_remainder=False) def _pad_to_batch(x, y): cur_size = tf.shape(y)[0] pad_size = batch_size - cur_size pad_ids = tf.zeros(shape=[pad_size, seq_length], dtype=tf.int32) for key in ('input_word_ids', 'input_mask', 'input_type_ids'): x[key] = tf.concat([x[key], pad_ids], axis=0) pad_labels = -tf.ones(shape=[pad_size, 1], dtype=tf.int32) y = tf.concat([y, pad_labels], axis=0) return x, y dataset = dataset.map( _pad_to_batch, num_parallel_calls=tf.data.experimental.AUTOTUNE) dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE) return dataset
11,724
37.69637
80
py
models
models-master/official/legacy/image_classification/callbacks.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Common modules for callbacks.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import os from typing import Any, List, MutableMapping, Optional, Text from absl import logging import tensorflow as tf from official.modeling import optimization from official.utils.misc import keras_utils def get_callbacks( model_checkpoint: bool = True, include_tensorboard: bool = True, time_history: bool = True, track_lr: bool = True, write_model_weights: bool = True, apply_moving_average: bool = False, initial_step: int = 0, batch_size: int = 0, log_steps: int = 0, model_dir: Optional[str] = None, backup_and_restore: bool = False) -> List[tf.keras.callbacks.Callback]: """Get all callbacks.""" model_dir = model_dir or '' callbacks = [] if model_checkpoint: ckpt_full_path = os.path.join(model_dir, 'model.ckpt-{epoch:04d}') callbacks.append( tf.keras.callbacks.ModelCheckpoint( ckpt_full_path, save_weights_only=True, verbose=1)) if backup_and_restore: backup_dir = os.path.join(model_dir, 'tmp') callbacks.append( tf.keras.callbacks.experimental.BackupAndRestore(backup_dir)) if include_tensorboard: callbacks.append( CustomTensorBoard( log_dir=model_dir, track_lr=track_lr, initial_step=initial_step, write_images=write_model_weights, profile_batch=0)) if time_history: callbacks.append( keras_utils.TimeHistory( batch_size, log_steps, logdir=model_dir if include_tensorboard else None)) if apply_moving_average: # Save moving average model to a different file so that # we can resume training from a checkpoint ckpt_full_path = os.path.join(model_dir, 'average', 'model.ckpt-{epoch:04d}') callbacks.append( AverageModelCheckpoint( update_weights=False, filepath=ckpt_full_path, save_weights_only=True, verbose=1)) callbacks.append(MovingAverageCallback()) return callbacks def get_scalar_from_tensor(t: tf.Tensor) -> int: """Utility function to convert a Tensor to a scalar.""" t = tf.keras.backend.get_value(t) if callable(t): return t() else: return t class CustomTensorBoard(tf.keras.callbacks.TensorBoard): """A customized TensorBoard callback that tracks additional datapoints. Metrics tracked: - Global learning rate Attributes: log_dir: the path of the directory where to save the log files to be parsed by TensorBoard. track_lr: `bool`, whether or not to track the global learning rate. initial_step: the initial step, used for preemption recovery. **kwargs: Additional arguments for backwards compatibility. Possible key is `period`. """ # TODO(b/146499062): track params, flops, log lr, l2 loss, # classification loss def __init__(self, log_dir: str, track_lr: bool = False, initial_step: int = 0, **kwargs): super(CustomTensorBoard, self).__init__(log_dir=log_dir, **kwargs) self.step = initial_step self._track_lr = track_lr def on_batch_begin(self, epoch: int, logs: Optional[MutableMapping[str, Any]] = None) -> None: self.step += 1 if logs is None: logs = {} logs.update(self._calculate_metrics()) super(CustomTensorBoard, self).on_batch_begin(epoch, logs) def on_epoch_begin(self, epoch: int, logs: Optional[MutableMapping[str, Any]] = None) -> None: if logs is None: logs = {} metrics = self._calculate_metrics() logs.update(metrics) for k, v in metrics.items(): logging.info('Current %s: %f', k, v) super(CustomTensorBoard, self).on_epoch_begin(epoch, logs) def on_epoch_end(self, epoch: int, logs: Optional[MutableMapping[str, Any]] = None) -> None: if logs is None: logs = {} metrics = self._calculate_metrics() logs.update(metrics) super(CustomTensorBoard, self).on_epoch_end(epoch, logs) def _calculate_metrics(self) -> MutableMapping[str, Any]: logs = {} # TODO(b/149030439): disable LR reporting. # if self._track_lr: # logs['learning_rate'] = self._calculate_lr() return logs def _calculate_lr(self) -> int: """Calculates the learning rate given the current step.""" return get_scalar_from_tensor( self._get_base_optimizer()._decayed_lr(var_dtype=tf.float32)) # pylint:disable=protected-access def _get_base_optimizer(self) -> tf.keras.optimizers.Optimizer: """Get the base optimizer used by the current model.""" optimizer = self.model.optimizer # The optimizer might be wrapped by another class, so unwrap it while hasattr(optimizer, '_optimizer'): optimizer = optimizer._optimizer # pylint:disable=protected-access return optimizer class MovingAverageCallback(tf.keras.callbacks.Callback): """A Callback to be used with a `ExponentialMovingAverage` optimizer. Applies moving average weights to the model during validation time to test and predict on the averaged weights rather than the current model weights. Once training is complete, the model weights will be overwritten with the averaged weights (by default). Attributes: overwrite_weights_on_train_end: Whether to overwrite the current model weights with the averaged weights from the moving average optimizer. **kwargs: Any additional callback arguments. """ def __init__(self, overwrite_weights_on_train_end: bool = False, **kwargs): super(MovingAverageCallback, self).__init__(**kwargs) self.overwrite_weights_on_train_end = overwrite_weights_on_train_end def set_model(self, model: tf.keras.Model): super(MovingAverageCallback, self).set_model(model) assert isinstance(self.model.optimizer, optimization.ExponentialMovingAverage) self.model.optimizer.shadow_copy(self.model) def on_test_begin(self, logs: Optional[MutableMapping[Text, Any]] = None): self.model.optimizer.swap_weights() def on_test_end(self, logs: Optional[MutableMapping[Text, Any]] = None): self.model.optimizer.swap_weights() def on_train_end(self, logs: Optional[MutableMapping[Text, Any]] = None): if self.overwrite_weights_on_train_end: self.model.optimizer.assign_average_vars(self.model.variables) class AverageModelCheckpoint(tf.keras.callbacks.ModelCheckpoint): """Saves and, optionally, assigns the averaged weights. Taken from tfa.callbacks.AverageModelCheckpoint. Attributes: update_weights: If True, assign the moving average weights to the model, and save them. If False, keep the old non-averaged weights, but the saved model uses the average weights. See `tf.keras.callbacks.ModelCheckpoint` for the other args. """ def __init__(self, update_weights: bool, filepath: str, monitor: str = 'val_loss', verbose: int = 0, save_best_only: bool = False, save_weights_only: bool = False, mode: str = 'auto', save_freq: str = 'epoch', **kwargs): self.update_weights = update_weights super().__init__(filepath, monitor, verbose, save_best_only, save_weights_only, mode, save_freq, **kwargs) def set_model(self, model): if not isinstance(model.optimizer, optimization.ExponentialMovingAverage): raise TypeError('AverageModelCheckpoint is only used when training' 'with MovingAverage') return super().set_model(model) def _save_model(self, epoch, logs): assert isinstance(self.model.optimizer, optimization.ExponentialMovingAverage) if self.update_weights: self.model.optimizer.assign_average_vars(self.model.variables) return super()._save_model(epoch, logs) # pytype: disable=attribute-error # typed-keras else: # Note: `model.get_weights()` gives us the weights (non-ref) # whereas `model.variables` returns references to the variables. non_avg_weights = self.model.get_weights() self.model.optimizer.assign_average_vars(self.model.variables) # result is currently None, since `super._save_model` doesn't # return anything, but this may change in the future. result = super()._save_model(epoch, logs) # pytype: disable=attribute-error # typed-keras self.model.set_weights(non_avg_weights) return result
9,355
35.546875
104
py
models
models-master/official/legacy/image_classification/optimizer_factory.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Optimizer factory for vision tasks.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from typing import Any, Dict, Optional, Text, Union from absl import logging import numpy as np import tensorflow as tf from official.legacy.image_classification import learning_rate from official.legacy.image_classification.configs import base_configs from official.modeling import optimization from official.modeling.optimization import legacy_adamw # pylint: disable=protected-access FloatTensorLike = Union[tf.Tensor, float, np.float16, np.float32, np.float64] class Lookahead(tf.keras.optimizers.legacy.Optimizer): """This class allows to extend optimizers with the lookahead mechanism. The mechanism is proposed by Michael R. Zhang et.al in the paper [Lookahead Optimizer: k steps forward, 1 step back] (https://arxiv.org/abs/1907.08610v1). The optimizer iteratively updates two sets of weights: the search directions for weights are chosen by the inner optimizer, while the "slow weights" are updated each `k` steps based on the directions of the "fast weights" and the two sets of weights are synchronized. This method improves the learning stability and lowers the variance of its inner optimizer. Example of usage: ```python opt = tf.keras.optimizers.SGD(learning_rate) opt = tfa.optimizers.Lookahead(opt) ``` """ def __init__( self, optimizer: tf.keras.optimizers.Optimizer, sync_period: int = 6, slow_step_size: FloatTensorLike = 0.5, name: str = 'Lookahead', **kwargs, ): """Wrap optimizer with the lookahead mechanism. Args: optimizer: The original optimizer that will be used to compute and apply the gradients. sync_period: An integer. The synchronization period of lookahead. Enable lookahead mechanism by setting it with a positive value. slow_step_size: A floating point value. The ratio for updating the slow weights. name: Optional name for the operations created when applying gradients. Defaults to "Lookahead". **kwargs: keyword arguments. Allowed to be {`clipnorm`, `clipvalue`, `lr`, `decay`}. `clipnorm` is clip gradients by norm; `clipvalue` is clip gradients by value, `decay` is included for backward compatibility to allow time inverse decay of learning rate. `lr` is included for backward compatibility, recommended to use `learning_rate` instead. """ super().__init__(name, **kwargs) if isinstance(optimizer, str): optimizer = tf.keras.optimizers.get(optimizer) if not isinstance( optimizer, (tf.keras.optimizers.Optimizer, tf.keras.optimizers.legacy.Optimizer), ): raise TypeError( 'optimizer is not an object of tf.keras.optimizers.Optimizer' ) self._optimizer = optimizer self._set_hyper('sync_period', sync_period) self._set_hyper('slow_step_size', slow_step_size) self._initialized = False self._track_trackable(self._optimizer, 'lh_base_optimizer') def _create_slots(self, var_list): self._optimizer._create_slots(var_list=var_list) # pylint: disable=protected-access for var in var_list: self.add_slot(var, 'slow', initializer=var) def _create_hypers(self): self._optimizer._create_hypers() # pylint: disable=protected-access def _prepare(self, var_list): return self._optimizer._prepare(var_list=var_list) # pylint: disable=protected-access def apply_gradients( self, grads_and_vars, name=None, skip_gradients_aggregation=None, **kwargs ): self._optimizer._iterations = self.iterations # pylint: disable=protected-access return super().apply_gradients(grads_and_vars, name, **kwargs) def _look_ahead_op(self, var): var_dtype = var.dtype.base_dtype slow_var = self.get_slot(var, 'slow') local_step = tf.cast(self.iterations + 1, tf.dtypes.int64) sync_period = self._get_hyper('sync_period', tf.dtypes.int64) slow_step_size = self._get_hyper('slow_step_size', var_dtype) step_back = slow_var + slow_step_size * (var - slow_var) sync_cond = tf.equal( tf.math.floordiv(local_step, sync_period) * sync_period, local_step ) with tf.control_dependencies([step_back]): slow_update = slow_var.assign( tf.where(sync_cond, step_back, slow_var), use_locking=self._use_locking, ) var_update = var.assign( tf.where(sync_cond, step_back, var), use_locking=self._use_locking ) return tf.group(slow_update, var_update) @property def weights(self): return self._weights + self._optimizer.weights def _resource_apply_dense(self, grad, var): train_op = self._optimizer._resource_apply_dense(grad, var) # pylint: disable=protected-access with tf.control_dependencies([train_op]): look_ahead_op = self._look_ahead_op(var) return tf.group(train_op, look_ahead_op) def _resource_apply_sparse(self, grad, var, indices): train_op = self._optimizer._resource_apply_sparse( # pylint: disable=protected-access grad, var, indices ) with tf.control_dependencies([train_op]): look_ahead_op = self._look_ahead_op(var) return tf.group(train_op, look_ahead_op) def get_config(self): config = { 'optimizer': tf.keras.optimizers.serialize(self._optimizer), 'sync_period': self._serialize_hyperparameter('sync_period'), 'slow_step_size': self._serialize_hyperparameter('slow_step_size'), } base_config = super().get_config() return {**base_config, **config} @property def learning_rate(self): return self._optimizer._get_hyper('learning_rate') @learning_rate.setter def learning_rate(self, value): self._optimizer._set_hyper('learning_rate', value) @property def lr(self): return self.learning_rate @lr.setter def lr(self, lr): self.learning_rate = lr @classmethod def from_config(cls, config, custom_objects=None): optimizer = tf.keras.optimizers.deserialize( config.pop('optimizer'), custom_objects=custom_objects ) return cls(optimizer, **config) def build_optimizer( optimizer_name: Text, base_learning_rate: tf.keras.optimizers.schedules.LearningRateSchedule, params: Dict[Text, Any], model: Optional[tf.keras.Model] = None): """Build the optimizer based on name. Args: optimizer_name: String representation of the optimizer name. Examples: sgd, momentum, rmsprop. base_learning_rate: `tf.keras.optimizers.schedules.LearningRateSchedule` base learning rate. params: String -> Any dictionary representing the optimizer params. This should contain optimizer specific parameters such as `base_learning_rate`, `decay`, etc. model: The `tf.keras.Model`. This is used for the shadow copy if using `ExponentialMovingAverage`. Returns: A tf.keras.optimizers.legacy.Optimizer. Raises: ValueError if the provided optimizer_name is not supported. """ optimizer_name = optimizer_name.lower() logging.info('Building %s optimizer with params %s', optimizer_name, params) if optimizer_name == 'sgd': logging.info('Using SGD optimizer') nesterov = params.get('nesterov', False) optimizer = tf.keras.optimizers.legacy.SGD( learning_rate=base_learning_rate, nesterov=nesterov) elif optimizer_name == 'momentum': logging.info('Using momentum optimizer') nesterov = params.get('nesterov', False) optimizer = tf.keras.optimizers.legacy.SGD( learning_rate=base_learning_rate, momentum=params['momentum'], nesterov=nesterov) elif optimizer_name == 'rmsprop': logging.info('Using RMSProp') rho = params.get('decay', None) or params.get('rho', 0.9) momentum = params.get('momentum', 0.9) epsilon = params.get('epsilon', 1e-07) optimizer = tf.keras.optimizers.legacy.RMSprop( learning_rate=base_learning_rate, rho=rho, momentum=momentum, epsilon=epsilon) elif optimizer_name == 'adam': logging.info('Using Adam') beta_1 = params.get('beta_1', 0.9) beta_2 = params.get('beta_2', 0.999) epsilon = params.get('epsilon', 1e-07) optimizer = tf.keras.optimizers.legacy.Adam( learning_rate=base_learning_rate, beta_1=beta_1, beta_2=beta_2, epsilon=epsilon) elif optimizer_name == 'adamw': logging.info('Using AdamW') weight_decay = params.get('weight_decay', 0.01) beta_1 = params.get('beta_1', 0.9) beta_2 = params.get('beta_2', 0.999) epsilon = params.get('epsilon', 1e-07) optimizer = legacy_adamw.AdamWeightDecay( learning_rate=base_learning_rate, weight_decay_rate=weight_decay, beta_1=beta_1, beta_2=beta_2, epsilon=epsilon, ) else: raise ValueError('Unknown optimizer %s' % optimizer_name) if params.get('lookahead', None): logging.info('Using lookahead optimizer.') optimizer = Lookahead(optimizer) # Moving average should be applied last, as it's applied at test time moving_average_decay = params.get('moving_average_decay', 0.) if moving_average_decay is not None and moving_average_decay > 0.: if model is None: raise ValueError( '`model` must be provided if using `ExponentialMovingAverage`.') logging.info('Including moving average decay.') optimizer = optimization.ExponentialMovingAverage( optimizer=optimizer, average_decay=moving_average_decay) optimizer.shadow_copy(model) return optimizer def build_learning_rate(params: base_configs.LearningRateConfig, batch_size: Optional[int] = None, train_epochs: Optional[int] = None, train_steps: Optional[int] = None): """Build the learning rate given the provided configuration.""" decay_type = params.name base_lr = params.initial_lr decay_rate = params.decay_rate if params.decay_epochs is not None: decay_steps = params.decay_epochs * train_steps else: decay_steps = 0 if params.warmup_epochs is not None: warmup_steps = params.warmup_epochs * train_steps else: warmup_steps = 0 lr_multiplier = params.scale_by_batch_size if lr_multiplier and lr_multiplier > 0: # Scale the learning rate based on the batch size and a multiplier base_lr *= lr_multiplier * batch_size logging.info( 'Scaling the learning rate based on the batch size ' 'multiplier. New base_lr: %f', base_lr) if decay_type == 'exponential': logging.info( 'Using exponential learning rate with: ' 'initial_learning_rate: %f, decay_steps: %d, ' 'decay_rate: %f', base_lr, decay_steps, decay_rate) lr = tf.keras.optimizers.schedules.ExponentialDecay( initial_learning_rate=base_lr, decay_steps=decay_steps, decay_rate=decay_rate, staircase=params.staircase) elif decay_type == 'stepwise': steps_per_epoch = params.examples_per_epoch // batch_size boundaries = [boundary * steps_per_epoch for boundary in params.boundaries] multipliers = [batch_size * multiplier for multiplier in params.multipliers] logging.info( 'Using stepwise learning rate. Parameters: ' 'boundaries: %s, values: %s', boundaries, multipliers) lr = tf.keras.optimizers.schedules.PiecewiseConstantDecay( boundaries=boundaries, values=multipliers) elif decay_type == 'cosine_with_warmup': lr = learning_rate.CosineDecayWithWarmup( batch_size=batch_size, total_steps=train_epochs * train_steps, warmup_steps=warmup_steps) if warmup_steps > 0: if decay_type not in ['cosine_with_warmup']: logging.info('Applying %d warmup steps to the learning rate', warmup_steps) lr = learning_rate.WarmupDecaySchedule( lr, warmup_steps, warmup_lr=base_lr) return lr
12,603
36.511905
99
py
models
models-master/official/legacy/image_classification/classifier_trainer_util_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Unit tests for the classifier trainer models.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import copy import os from absl.testing import parameterized import tensorflow as tf from official.legacy.image_classification import classifier_trainer from official.legacy.image_classification import dataset_factory from official.legacy.image_classification import test_utils from official.legacy.image_classification.configs import base_configs def get_trivial_model(num_classes: int) -> tf.keras.Model: """Creates and compiles trivial model for ImageNet dataset.""" model = test_utils.trivial_model(num_classes=num_classes) lr = 0.01 optimizer = tf.keras.optimizers.SGD(learning_rate=lr) loss_obj = tf.keras.losses.SparseCategoricalCrossentropy() model.compile(optimizer=optimizer, loss=loss_obj, run_eagerly=True) return model def get_trivial_data() -> tf.data.Dataset: """Gets trivial data in the ImageNet size.""" def generate_data(_) -> tf.data.Dataset: image = tf.zeros(shape=(224, 224, 3), dtype=tf.float32) label = tf.zeros([1], dtype=tf.int32) return image, label dataset = tf.data.Dataset.range(1) dataset = dataset.repeat() dataset = dataset.map( generate_data, num_parallel_calls=tf.data.experimental.AUTOTUNE) dataset = dataset.prefetch(buffer_size=1).batch(1) return dataset class UtilTests(parameterized.TestCase, tf.test.TestCase): """Tests for individual utility functions within classifier_trainer.py.""" @parameterized.named_parameters( ('efficientnet-b0', 'efficientnet', 'efficientnet-b0', 224), ('efficientnet-b1', 'efficientnet', 'efficientnet-b1', 240), ('efficientnet-b2', 'efficientnet', 'efficientnet-b2', 260), ('efficientnet-b3', 'efficientnet', 'efficientnet-b3', 300), ('efficientnet-b4', 'efficientnet', 'efficientnet-b4', 380), ('efficientnet-b5', 'efficientnet', 'efficientnet-b5', 456), ('efficientnet-b6', 'efficientnet', 'efficientnet-b6', 528), ('efficientnet-b7', 'efficientnet', 'efficientnet-b7', 600), ('resnet', 'resnet', '', None), ) def test_get_model_size(self, model, model_name, expected): config = base_configs.ExperimentConfig( model_name=model, model=base_configs.ModelConfig( model_params={ 'model_name': model_name, },)) size = classifier_trainer.get_image_size_from_model(config) self.assertEqual(size, expected) @parameterized.named_parameters( ('dynamic', 'dynamic', None, 'dynamic'), ('scalar', 128., None, 128.), ('float32', None, 'float32', 1), ('float16', None, 'float16', 128), ) def test_get_loss_scale(self, loss_scale, dtype, expected): config = base_configs.ExperimentConfig( runtime=base_configs.RuntimeConfig(loss_scale=loss_scale), train_dataset=dataset_factory.DatasetConfig(dtype=dtype)) ls = classifier_trainer.get_loss_scale(config, fp16_default=128) self.assertEqual(ls, expected) @parameterized.named_parameters(('float16', 'float16'), ('bfloat16', 'bfloat16')) def test_initialize(self, dtype): config = base_configs.ExperimentConfig( runtime=base_configs.RuntimeConfig( run_eagerly=False, enable_xla=False, per_gpu_thread_count=1, gpu_thread_mode='gpu_private', num_gpus=1, dataset_num_private_threads=1, ), train_dataset=dataset_factory.DatasetConfig(dtype=dtype), model=base_configs.ModelConfig(), ) class EmptyClass: pass fake_ds_builder = EmptyClass() fake_ds_builder.dtype = dtype fake_ds_builder.config = EmptyClass() classifier_trainer.initialize(config, fake_ds_builder) def test_resume_from_checkpoint(self): """Tests functionality for resuming from checkpoint.""" # Set the keras policy tf.keras.mixed_precision.set_global_policy('mixed_bfloat16') # Get the model, datasets, and compile it. model = get_trivial_model(10) # Create the checkpoint model_dir = self.create_tempdir().full_path train_epochs = 1 train_steps = 10 ds = get_trivial_data() callbacks = [ tf.keras.callbacks.ModelCheckpoint( os.path.join(model_dir, 'model.ckpt-{epoch:04d}'), save_weights_only=True) ] model.fit( ds, callbacks=callbacks, epochs=train_epochs, steps_per_epoch=train_steps) # Test load from checkpoint clean_model = get_trivial_model(10) weights_before_load = copy.deepcopy(clean_model.get_weights()) initial_epoch = classifier_trainer.resume_from_checkpoint( model=clean_model, model_dir=model_dir, train_steps=train_steps) self.assertEqual(initial_epoch, 1) self.assertNotAllClose(weights_before_load, clean_model.get_weights()) tf.io.gfile.rmtree(model_dir) def test_serialize_config(self): """Tests functionality for serializing data.""" config = base_configs.ExperimentConfig() model_dir = self.create_tempdir().full_path classifier_trainer.serialize_config(params=config, model_dir=model_dir) saved_params_path = os.path.join(model_dir, 'params.yaml') self.assertTrue(os.path.exists(saved_params_path)) tf.io.gfile.rmtree(model_dir) if __name__ == '__main__': tf.test.main()
6,047
35.433735
76
py
models
models-master/official/legacy/image_classification/dataset_factory.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Dataset utilities for vision tasks using TFDS and tf.data.Dataset.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import dataclasses import os from typing import Any, List, Mapping, Optional, Tuple, Union from absl import logging import tensorflow as tf import tensorflow_datasets as tfds from official.legacy.image_classification import augment from official.legacy.image_classification import preprocessing from official.modeling.hyperparams import base_config AUGMENTERS = { 'autoaugment': augment.AutoAugment, 'randaugment': augment.RandAugment, } @dataclasses.dataclass class AugmentConfig(base_config.Config): """Configuration for image augmenters. Attributes: name: The name of the image augmentation to use. Possible options are None (default), 'autoaugment', or 'randaugment'. params: Any parameters used to initialize the augmenter. """ name: Optional[str] = None params: Optional[Mapping[str, Any]] = None def build(self) -> augment.ImageAugment: """Build the augmenter using this config.""" params = self.params or {} augmenter = AUGMENTERS.get(self.name, None) return augmenter(**params) if augmenter is not None else None @dataclasses.dataclass class DatasetConfig(base_config.Config): """The base configuration for building datasets. Attributes: name: The name of the Dataset. Usually should correspond to a TFDS dataset. data_dir: The path where the dataset files are stored, if available. filenames: Optional list of strings representing the TFRecord names. builder: The builder type used to load the dataset. Value should be one of 'tfds' (load using TFDS), 'records' (load from TFRecords), or 'synthetic' (generate dummy synthetic data without reading from files). split: The split of the dataset. Usually 'train', 'validation', or 'test'. image_size: The size of the image in the dataset. This assumes that `width` == `height`. Set to 'infer' to infer the image size from TFDS info. This requires `name` to be a registered dataset in TFDS. num_classes: The number of classes given by the dataset. Set to 'infer' to infer the image size from TFDS info. This requires `name` to be a registered dataset in TFDS. num_channels: The number of channels given by the dataset. Set to 'infer' to infer the image size from TFDS info. This requires `name` to be a registered dataset in TFDS. num_examples: The number of examples given by the dataset. Set to 'infer' to infer the image size from TFDS info. This requires `name` to be a registered dataset in TFDS. batch_size: The base batch size for the dataset. use_per_replica_batch_size: Whether to scale the batch size based on available resources. If set to `True`, the dataset builder will return batch_size multiplied by `num_devices`, the number of device replicas (e.g., the number of GPUs or TPU cores). This setting should be `True` if the strategy argument is passed to `build()` and `num_devices > 1`. num_devices: The number of replica devices to use. This should be set by `strategy.num_replicas_in_sync` when using a distribution strategy. dtype: The desired dtype of the dataset. This will be set during preprocessing. one_hot: Whether to apply one hot encoding. Set to `True` to be able to use label smoothing. augmenter: The augmenter config to use. No augmentation is used by default. download: Whether to download data using TFDS. shuffle_buffer_size: The buffer size used for shuffling training data. file_shuffle_buffer_size: The buffer size used for shuffling raw training files. skip_decoding: Whether to skip image decoding when loading from TFDS. cache: whether to cache to dataset examples. Can be used to avoid re-reading from disk on the second epoch. Requires significant memory overhead. tf_data_service: The URI of a tf.data service to offload preprocessing onto during training. The URI should be in the format "protocol://address", e.g. "grpc://tf-data-service:5050". mean_subtract: whether or not to apply mean subtraction to the dataset. standardize: whether or not to apply standardization to the dataset. """ name: Optional[str] = None data_dir: Optional[str] = None filenames: Optional[List[str]] = None builder: str = 'tfds' split: str = 'train' image_size: Union[int, str] = 'infer' num_classes: Union[int, str] = 'infer' num_channels: Union[int, str] = 'infer' num_examples: Union[int, str] = 'infer' batch_size: int = 128 use_per_replica_batch_size: bool = True num_devices: int = 1 dtype: str = 'float32' one_hot: bool = True augmenter: AugmentConfig = dataclasses.field(default_factory=AugmentConfig) download: bool = False shuffle_buffer_size: int = 10000 file_shuffle_buffer_size: int = 1024 skip_decoding: bool = True cache: bool = False tf_data_service: Optional[str] = None mean_subtract: bool = False standardize: bool = False @property def has_data(self): """Whether this dataset is has any data associated with it.""" return self.name or self.data_dir or self.filenames @dataclasses.dataclass class ImageNetConfig(DatasetConfig): """The base ImageNet dataset config.""" name: str = 'imagenet2012' # Note: for large datasets like ImageNet, using records is faster than tfds builder: str = 'records' image_size: int = 224 num_channels: int = 3 num_examples: int = 1281167 num_classes: int = 1000 batch_size: int = 128 @dataclasses.dataclass class Cifar10Config(DatasetConfig): """The base CIFAR-10 dataset config.""" name: str = 'cifar10' image_size: int = 224 batch_size: int = 128 download: bool = True cache: bool = True class DatasetBuilder: """An object for building datasets. Allows building various pipelines fetching examples, preprocessing, etc. Maintains additional state information calculated from the dataset, i.e., training set split, batch size, and number of steps (batches). """ def __init__(self, config: DatasetConfig, **overrides: Any): """Initialize the builder from the config.""" self.config = config.replace(**overrides) self.builder_info = None if self.config.augmenter is not None: logging.info('Using augmentation: %s', self.config.augmenter.name) self.augmenter = self.config.augmenter.build() else: self.augmenter = None @property def is_training(self) -> bool: """Whether this is the training set.""" return self.config.split == 'train' @property def batch_size(self) -> int: """The batch size, multiplied by the number of replicas (if configured).""" if self.config.use_per_replica_batch_size: return self.config.batch_size * self.config.num_devices else: return self.config.batch_size @property def global_batch_size(self): """The global batch size across all replicas.""" return self.batch_size @property def local_batch_size(self): """The base unscaled batch size.""" if self.config.use_per_replica_batch_size: return self.config.batch_size else: return self.config.batch_size // self.config.num_devices @property def num_steps(self) -> int: """The number of steps (batches) to exhaust this dataset.""" # Always divide by the global batch size to get the correct # of steps return self.num_examples // self.global_batch_size @property def dtype(self) -> tf.dtypes.DType: """Converts the config's dtype string to a tf dtype. Returns: A mapping from string representation of a dtype to the `tf.dtypes.DType`. Raises: ValueError if the config's dtype is not supported. """ dtype_map = { 'float32': tf.float32, 'bfloat16': tf.bfloat16, 'float16': tf.float16, 'fp32': tf.float32, 'bf16': tf.bfloat16, } try: return dtype_map[self.config.dtype] except: raise ValueError('Invalid DType provided. Supported types: {}'.format( dtype_map.keys())) @property def image_size(self) -> int: """The size of each image (can be inferred from the dataset).""" if self.config.image_size == 'infer': return self.info.features['image'].shape[0] else: return int(self.config.image_size) @property def num_channels(self) -> int: """The number of image channels (can be inferred from the dataset).""" if self.config.num_channels == 'infer': return self.info.features['image'].shape[-1] else: return int(self.config.num_channels) @property def num_examples(self) -> int: """The number of examples (can be inferred from the dataset).""" if self.config.num_examples == 'infer': return self.info.splits[self.config.split].num_examples else: return int(self.config.num_examples) @property def num_classes(self) -> int: """The number of classes (can be inferred from the dataset).""" if self.config.num_classes == 'infer': return self.info.features['label'].num_classes else: return int(self.config.num_classes) @property def info(self) -> tfds.core.DatasetInfo: """The TFDS dataset info, if available.""" try: if self.builder_info is None: self.builder_info = tfds.builder(self.config.name).info except ConnectionError as e: logging.error('Failed to use TFDS to load info. Please set dataset info ' '(image_size, num_channels, num_examples, num_classes) in ' 'the dataset config.') raise e return self.builder_info def build( self, strategy: Optional[tf.distribute.Strategy] = None) -> tf.data.Dataset: """Construct a dataset end-to-end and return it using an optional strategy. Args: strategy: a strategy that, if passed, will distribute the dataset according to that strategy. If passed and `num_devices > 1`, `use_per_replica_batch_size` must be set to `True`. Returns: A TensorFlow dataset outputting batched images and labels. """ if strategy: if strategy.num_replicas_in_sync != self.config.num_devices: logging.warn( 'Passed a strategy with %d devices, but expected' '%d devices.', strategy.num_replicas_in_sync, self.config.num_devices) dataset = strategy.distribute_datasets_from_function(self._build) else: dataset = self._build() return dataset def _build( self, input_context: Optional[tf.distribute.InputContext] = None ) -> tf.data.Dataset: """Construct a dataset end-to-end and return it. Args: input_context: An optional context provided by `tf.distribute` for cross-replica training. Returns: A TensorFlow dataset outputting batched images and labels. """ builders = { 'tfds': self.load_tfds, 'records': self.load_records, 'synthetic': self.load_synthetic, } builder = builders.get(self.config.builder, None) if builder is None: raise ValueError('Unknown builder type {}'.format(self.config.builder)) self.input_context = input_context dataset = builder() dataset = self.pipeline(dataset) return dataset def load_tfds(self) -> tf.data.Dataset: """Return a dataset loading files from TFDS.""" logging.info('Using TFDS to load data.') builder = tfds.builder(self.config.name, data_dir=self.config.data_dir) if self.config.download: builder.download_and_prepare() decoders = {} if self.config.skip_decoding: decoders['image'] = tfds.decode.SkipDecoding() read_config = tfds.ReadConfig( interleave_cycle_length=10, interleave_block_length=1, input_context=self.input_context) dataset = builder.as_dataset( split=self.config.split, as_supervised=True, shuffle_files=True, decoders=decoders, read_config=read_config) return dataset def load_records(self) -> tf.data.Dataset: """Return a dataset loading files with TFRecords.""" logging.info('Using TFRecords to load data.') if self.config.filenames is None: if self.config.data_dir is None: raise ValueError('Dataset must specify a path for the data files.') file_pattern = os.path.join(self.config.data_dir, '{}*'.format(self.config.split)) dataset = tf.data.Dataset.list_files(file_pattern, shuffle=False) else: dataset = tf.data.Dataset.from_tensor_slices(self.config.filenames) return dataset def load_synthetic(self) -> tf.data.Dataset: """Return a dataset generating dummy synthetic data.""" logging.info('Generating a synthetic dataset.') def generate_data(_): image = tf.zeros([self.image_size, self.image_size, self.num_channels], dtype=self.dtype) label = tf.zeros([1], dtype=tf.int32) return image, label dataset = tf.data.Dataset.range(1) dataset = dataset.repeat() dataset = dataset.map( generate_data, num_parallel_calls=tf.data.experimental.AUTOTUNE) return dataset def pipeline(self, dataset: tf.data.Dataset) -> tf.data.Dataset: """Build a pipeline fetching, shuffling, and preprocessing the dataset. Args: dataset: A `tf.data.Dataset` that loads raw files. Returns: A TensorFlow dataset outputting batched images and labels. """ if (self.config.builder != 'tfds' and self.input_context and self.input_context.num_input_pipelines > 1): dataset = dataset.shard(self.input_context.num_input_pipelines, self.input_context.input_pipeline_id) logging.info( 'Sharding the dataset: input_pipeline_id=%d ' 'num_input_pipelines=%d', self.input_context.num_input_pipelines, self.input_context.input_pipeline_id) if self.is_training and self.config.builder == 'records': # Shuffle the input files. dataset.shuffle(buffer_size=self.config.file_shuffle_buffer_size) if self.is_training and not self.config.cache: dataset = dataset.repeat() if self.config.builder == 'records': # Read the data from disk in parallel dataset = dataset.interleave( tf.data.TFRecordDataset, cycle_length=10, block_length=1, num_parallel_calls=tf.data.experimental.AUTOTUNE) if self.config.cache: dataset = dataset.cache() if self.is_training: dataset = dataset.shuffle(self.config.shuffle_buffer_size) dataset = dataset.repeat() # Parse, pre-process, and batch the data in parallel if self.config.builder == 'records': preprocess = self.parse_record else: preprocess = self.preprocess dataset = dataset.map( preprocess, num_parallel_calls=tf.data.experimental.AUTOTUNE) if self.input_context and self.config.num_devices > 1: if not self.config.use_per_replica_batch_size: raise ValueError( 'The builder does not support a global batch size with more than ' 'one replica. Got {} replicas. Please set a ' '`per_replica_batch_size` and enable ' '`use_per_replica_batch_size=True`.'.format( self.config.num_devices)) # The batch size of the dataset will be multiplied by the number of # replicas automatically when strategy.distribute_datasets_from_function # is called, so we use local batch size here. dataset = dataset.batch( self.local_batch_size, drop_remainder=self.is_training) else: dataset = dataset.batch( self.global_batch_size, drop_remainder=self.is_training) # Prefetch overlaps in-feed with training dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE) if self.config.tf_data_service: if not hasattr(tf.data.experimental, 'service'): raise ValueError('The tf_data_service flag requires Tensorflow version ' '>= 2.3.0, but the version is {}'.format( tf.__version__)) dataset = dataset.apply( tf.data.experimental.service.distribute( processing_mode='parallel_epochs', service=self.config.tf_data_service, job_name='resnet_train')) dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) return dataset def parse_record(self, record: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]: """Parse an ImageNet record from a serialized string Tensor.""" keys_to_features = { 'image/encoded': tf.io.FixedLenFeature((), tf.string, ''), 'image/format': tf.io.FixedLenFeature((), tf.string, 'jpeg'), 'image/class/label': tf.io.FixedLenFeature([], tf.int64, -1), 'image/class/text': tf.io.FixedLenFeature([], tf.string, ''), 'image/object/bbox/xmin': tf.io.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymin': tf.io.VarLenFeature(dtype=tf.float32), 'image/object/bbox/xmax': tf.io.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymax': tf.io.VarLenFeature(dtype=tf.float32), 'image/object/class/label': tf.io.VarLenFeature(dtype=tf.int64), } parsed = tf.io.parse_single_example(record, keys_to_features) label = tf.reshape(parsed['image/class/label'], shape=[1]) # Subtract one so that labels are in [0, 1000) label -= 1 image_bytes = tf.reshape(parsed['image/encoded'], shape=[]) image, label = self.preprocess(image_bytes, label) return image, label def preprocess(self, image: tf.Tensor, label: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]: """Apply image preprocessing and augmentation to the image and label.""" if self.is_training: image = preprocessing.preprocess_for_train( image, image_size=self.image_size, mean_subtract=self.config.mean_subtract, standardize=self.config.standardize, dtype=self.dtype, augmenter=self.augmenter) else: image = preprocessing.preprocess_for_eval( image, image_size=self.image_size, num_channels=self.num_channels, mean_subtract=self.config.mean_subtract, standardize=self.config.standardize, dtype=self.dtype) label = tf.cast(label, tf.int32) if self.config.one_hot: label = tf.one_hot(label, self.num_classes) label = tf.reshape(label, [self.num_classes]) return image, label @classmethod def from_params(cls, *args, **kwargs): """Construct a dataset builder from a default config and any overrides.""" config = DatasetConfig.from_args(*args, **kwargs) return cls(config)
19,550
35.61236
80
py
models
models-master/official/legacy/image_classification/preprocessing.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Preprocessing functions for images.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from typing import List, Optional, Text, Tuple import tensorflow as tf from official.legacy.image_classification import augment # Calculated from the ImageNet training set MEAN_RGB = (0.485 * 255, 0.456 * 255, 0.406 * 255) STDDEV_RGB = (0.229 * 255, 0.224 * 255, 0.225 * 255) IMAGE_SIZE = 224 CROP_PADDING = 32 def mean_image_subtraction( image_bytes: tf.Tensor, means: Tuple[float, ...], num_channels: int = 3, dtype: tf.dtypes.DType = tf.float32, ) -> tf.Tensor: """Subtracts the given means from each image channel. For example: means = [123.68, 116.779, 103.939] image_bytes = mean_image_subtraction(image_bytes, means) Note that the rank of `image` must be known. Args: image_bytes: a tensor of size [height, width, C]. means: a C-vector of values to subtract from each channel. num_channels: number of color channels in the image that will be distorted. dtype: the dtype to convert the images to. Set to `None` to skip conversion. Returns: the centered image. Raises: ValueError: If the rank of `image` is unknown, if `image` has a rank other than three or if the number of channels in `image` doesn't match the number of values in `means`. """ if image_bytes.get_shape().ndims != 3: raise ValueError('Input must be of size [height, width, C>0]') if len(means) != num_channels: raise ValueError('len(means) must match the number of channels') # We have a 1-D tensor of means; convert to 3-D. # Note(b/130245863): we explicitly call `broadcast` instead of simply # expanding dimensions for better performance. means = tf.broadcast_to(means, tf.shape(image_bytes)) if dtype is not None: means = tf.cast(means, dtype=dtype) return image_bytes - means def standardize_image( image_bytes: tf.Tensor, stddev: Tuple[float, ...], num_channels: int = 3, dtype: tf.dtypes.DType = tf.float32, ) -> tf.Tensor: """Divides the given stddev from each image channel. For example: stddev = [123.68, 116.779, 103.939] image_bytes = standardize_image(image_bytes, stddev) Note that the rank of `image` must be known. Args: image_bytes: a tensor of size [height, width, C]. stddev: a C-vector of values to divide from each channel. num_channels: number of color channels in the image that will be distorted. dtype: the dtype to convert the images to. Set to `None` to skip conversion. Returns: the centered image. Raises: ValueError: If the rank of `image` is unknown, if `image` has a rank other than three or if the number of channels in `image` doesn't match the number of values in `stddev`. """ if image_bytes.get_shape().ndims != 3: raise ValueError('Input must be of size [height, width, C>0]') if len(stddev) != num_channels: raise ValueError('len(stddev) must match the number of channels') # We have a 1-D tensor of stddev; convert to 3-D. # Note(b/130245863): we explicitly call `broadcast` instead of simply # expanding dimensions for better performance. stddev = tf.broadcast_to(stddev, tf.shape(image_bytes)) if dtype is not None: stddev = tf.cast(stddev, dtype=dtype) return image_bytes / stddev def normalize_images(features: tf.Tensor, mean_rgb: Tuple[float, ...] = MEAN_RGB, stddev_rgb: Tuple[float, ...] = STDDEV_RGB, num_channels: int = 3, dtype: tf.dtypes.DType = tf.float32, data_format: Text = 'channels_last') -> tf.Tensor: """Normalizes the input image channels with the given mean and stddev. Args: features: `Tensor` representing decoded images in float format. mean_rgb: the mean of the channels to subtract. stddev_rgb: the stddev of the channels to divide. num_channels: the number of channels in the input image tensor. dtype: the dtype to convert the images to. Set to `None` to skip conversion. data_format: the format of the input image tensor ['channels_first', 'channels_last']. Returns: A normalized image `Tensor`. """ # TODO(allencwang) - figure out how to use mean_image_subtraction and # standardize_image on batches of images and replace the following. if data_format == 'channels_first': stats_shape = [num_channels, 1, 1] else: stats_shape = [1, 1, num_channels] if dtype is not None: features = tf.image.convert_image_dtype(features, dtype=dtype) if mean_rgb is not None: mean_rgb = tf.constant(mean_rgb, shape=stats_shape, dtype=features.dtype) mean_rgb = tf.broadcast_to(mean_rgb, tf.shape(features)) features = features - mean_rgb if stddev_rgb is not None: stddev_rgb = tf.constant(stddev_rgb, shape=stats_shape, dtype=features.dtype) stddev_rgb = tf.broadcast_to(stddev_rgb, tf.shape(features)) features = features / stddev_rgb return features def decode_and_center_crop(image_bytes: tf.Tensor, image_size: int = IMAGE_SIZE, crop_padding: int = CROP_PADDING) -> tf.Tensor: """Crops to center of image with padding then scales image_size. Args: image_bytes: `Tensor` representing an image binary of arbitrary size. image_size: image height/width dimension. crop_padding: the padding size to use when centering the crop. Returns: A decoded and cropped image `Tensor`. """ decoded = image_bytes.dtype != tf.string shape = (tf.shape(image_bytes) if decoded else tf.image.extract_jpeg_shape(image_bytes)) image_height = shape[0] image_width = shape[1] padded_center_crop_size = tf.cast( ((image_size / (image_size + crop_padding)) * tf.cast(tf.minimum(image_height, image_width), tf.float32)), tf.int32) offset_height = ((image_height - padded_center_crop_size) + 1) // 2 offset_width = ((image_width - padded_center_crop_size) + 1) // 2 crop_window = tf.stack([offset_height, offset_width, padded_center_crop_size, padded_center_crop_size]) if decoded: image = tf.image.crop_to_bounding_box( image_bytes, offset_height=offset_height, offset_width=offset_width, target_height=padded_center_crop_size, target_width=padded_center_crop_size) else: image = tf.image.decode_and_crop_jpeg(image_bytes, crop_window, channels=3) image = resize_image(image_bytes=image, height=image_size, width=image_size) return image def decode_crop_and_flip(image_bytes: tf.Tensor) -> tf.Tensor: """Crops an image to a random part of the image, then randomly flips. Args: image_bytes: `Tensor` representing an image binary of arbitrary size. Returns: A decoded and cropped image `Tensor`. """ decoded = image_bytes.dtype != tf.string bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4]) shape = (tf.shape(image_bytes) if decoded else tf.image.extract_jpeg_shape(image_bytes)) sample_distorted_bounding_box = tf.image.sample_distorted_bounding_box( shape, bounding_boxes=bbox, min_object_covered=0.1, aspect_ratio_range=[0.75, 1.33], area_range=[0.05, 1.0], max_attempts=100, use_image_if_no_bounding_boxes=True) bbox_begin, bbox_size, _ = sample_distorted_bounding_box # Reassemble the bounding box in the format the crop op requires. offset_height, offset_width, _ = tf.unstack(bbox_begin) target_height, target_width, _ = tf.unstack(bbox_size) crop_window = tf.stack([offset_height, offset_width, target_height, target_width]) if decoded: cropped = tf.image.crop_to_bounding_box( image_bytes, offset_height=offset_height, offset_width=offset_width, target_height=target_height, target_width=target_width) else: cropped = tf.image.decode_and_crop_jpeg(image_bytes, crop_window, channels=3) # Flip to add a little more random distortion in. cropped = tf.image.random_flip_left_right(cropped) return cropped def resize_image(image_bytes: tf.Tensor, height: int = IMAGE_SIZE, width: int = IMAGE_SIZE) -> tf.Tensor: """Resizes an image to a given height and width. Args: image_bytes: `Tensor` representing an image binary of arbitrary size. height: image height dimension. width: image width dimension. Returns: A tensor containing the resized image. """ print(height, width) return tf.compat.v1.image.resize( image_bytes, tf.convert_to_tensor([height, width]), method=tf.image.ResizeMethod.BILINEAR, align_corners=False) def preprocess_for_eval( image_bytes: tf.Tensor, image_size: int = IMAGE_SIZE, num_channels: int = 3, mean_subtract: bool = False, standardize: bool = False, dtype: tf.dtypes.DType = tf.float32 ) -> tf.Tensor: """Preprocesses the given image for evaluation. Args: image_bytes: `Tensor` representing an image binary of arbitrary size. image_size: image height/width dimension. num_channels: number of image input channels. mean_subtract: whether or not to apply mean subtraction. standardize: whether or not to apply standardization. dtype: the dtype to convert the images to. Set to `None` to skip conversion. Returns: A preprocessed and normalized image `Tensor`. """ images = decode_and_center_crop(image_bytes, image_size) images = tf.reshape(images, [image_size, image_size, num_channels]) if mean_subtract: images = mean_image_subtraction(image_bytes=images, means=MEAN_RGB) if standardize: images = standardize_image(image_bytes=images, stddev=STDDEV_RGB) if dtype is not None: images = tf.image.convert_image_dtype(images, dtype=dtype) return images def load_eval_image(filename: Text, image_size: int = IMAGE_SIZE) -> tf.Tensor: """Reads an image from the filesystem and applies image preprocessing. Args: filename: a filename path of an image. image_size: image height/width dimension. Returns: A preprocessed and normalized image `Tensor`. """ image_bytes = tf.io.read_file(filename) image = preprocess_for_eval(image_bytes, image_size) return image def build_eval_dataset(filenames: List[Text], labels: Optional[List[int]] = None, image_size: int = IMAGE_SIZE, batch_size: int = 1) -> tf.Tensor: """Builds a tf.data.Dataset from a list of filenames and labels. Args: filenames: a list of filename paths of images. labels: a list of labels corresponding to each image. image_size: image height/width dimension. batch_size: the batch size used by the dataset Returns: A preprocessed and normalized image `Tensor`. """ if labels is None: labels = [0] * len(filenames) filenames = tf.constant(filenames) labels = tf.constant(labels) dataset = tf.data.Dataset.from_tensor_slices((filenames, labels)) dataset = dataset.map( lambda filename, label: (load_eval_image(filename, image_size), label)) dataset = dataset.batch(batch_size) return dataset def preprocess_for_train(image_bytes: tf.Tensor, image_size: int = IMAGE_SIZE, augmenter: Optional[augment.ImageAugment] = None, mean_subtract: bool = False, standardize: bool = False, dtype: tf.dtypes.DType = tf.float32) -> tf.Tensor: """Preprocesses the given image for training. Args: image_bytes: `Tensor` representing an image binary of arbitrary size of dtype tf.uint8. image_size: image height/width dimension. augmenter: the image augmenter to apply. mean_subtract: whether or not to apply mean subtraction. standardize: whether or not to apply standardization. dtype: the dtype to convert the images to. Set to `None` to skip conversion. Returns: A preprocessed and normalized image `Tensor`. """ images = decode_crop_and_flip(image_bytes=image_bytes) images = resize_image(images, height=image_size, width=image_size) if augmenter is not None: images = augmenter.distort(images) if mean_subtract: images = mean_image_subtraction(image_bytes=images, means=MEAN_RGB) if standardize: images = standardize_image(image_bytes=images, stddev=STDDEV_RGB) if dtype is not None: images = tf.image.convert_image_dtype(images, dtype) return images
13,537
33.535714
80
py
models
models-master/official/legacy/image_classification/optimizer_factory_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for optimizer_factory.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from absl.testing import parameterized import tensorflow as tf from official.legacy.image_classification import optimizer_factory from official.legacy.image_classification.configs import base_configs class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase): def build_toy_model(self) -> tf.keras.Model: """Creates a toy `tf.Keras.Model`.""" model = tf.keras.Sequential() model.add(tf.keras.layers.Dense(1, input_shape=(1,))) return model @parameterized.named_parameters( ('sgd', 'sgd', 0., False), ('momentum', 'momentum', 0., False), ('rmsprop', 'rmsprop', 0., False), ('adam', 'adam', 0., False), ('adamw', 'adamw', 0., False), ('momentum_lookahead', 'momentum', 0., True), ('sgd_ema', 'sgd', 0.999, False), ('momentum_ema', 'momentum', 0.999, False), ('rmsprop_ema', 'rmsprop', 0.999, False)) def test_optimizer(self, optimizer_name, moving_average_decay, lookahead): """Smoke test to be sure no syntax errors.""" model = self.build_toy_model() params = { 'learning_rate': 0.001, 'rho': 0.09, 'momentum': 0., 'epsilon': 1e-07, 'moving_average_decay': moving_average_decay, 'lookahead': lookahead, } optimizer = optimizer_factory.build_optimizer( optimizer_name=optimizer_name, base_learning_rate=params['learning_rate'], params=params, model=model) self.assertTrue( issubclass(type(optimizer), tf.keras.optimizers.legacy.Optimizer) ) def test_unknown_optimizer(self): with self.assertRaises(ValueError): optimizer_factory.build_optimizer( optimizer_name='this_optimizer_does_not_exist', base_learning_rate=None, params=None) def test_learning_rate_without_decay_or_warmups(self): params = base_configs.LearningRateConfig( name='exponential', initial_lr=0.01, decay_rate=0.01, decay_epochs=None, warmup_epochs=None, scale_by_batch_size=0.01, examples_per_epoch=1, boundaries=[0], multipliers=[0, 1]) batch_size = 1 train_steps = 1 lr = optimizer_factory.build_learning_rate( params=params, batch_size=batch_size, train_steps=train_steps) self.assertTrue( issubclass( type(lr), tf.keras.optimizers.schedules.LearningRateSchedule)) @parameterized.named_parameters(('exponential', 'exponential'), ('cosine_with_warmup', 'cosine_with_warmup')) def test_learning_rate_with_decay_and_warmup(self, lr_decay_type): """Basic smoke test for syntax.""" params = base_configs.LearningRateConfig( name=lr_decay_type, initial_lr=0.01, decay_rate=0.01, decay_epochs=1, warmup_epochs=1, scale_by_batch_size=0.01, examples_per_epoch=1, boundaries=[0], multipliers=[0, 1]) batch_size = 1 train_epochs = 1 train_steps = 1 lr = optimizer_factory.build_learning_rate( params=params, batch_size=batch_size, train_epochs=train_epochs, train_steps=train_steps) self.assertTrue( issubclass( type(lr), tf.keras.optimizers.schedules.LearningRateSchedule)) if __name__ == '__main__': tf.test.main()
4,088
32.793388
79
py
models
models-master/official/legacy/image_classification/augment.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """AutoAugment and RandAugment policies for enhanced image preprocessing. AutoAugment Reference: https://arxiv.org/abs/1805.09501 RandAugment Reference: https://arxiv.org/abs/1909.13719 """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import math from typing import Any, Dict, List, Optional, Text, Tuple import tensorflow as tf # This signifies the max integer that the controller RNN could predict for the # augmentation scheme. _MAX_LEVEL = 10. def to_4d(image: tf.Tensor) -> tf.Tensor: """Converts an input Tensor to 4 dimensions. 4D image => [N, H, W, C] or [N, C, H, W] 3D image => [1, H, W, C] or [1, C, H, W] 2D image => [1, H, W, 1] Args: image: The 2/3/4D input tensor. Returns: A 4D image tensor. Raises: `TypeError` if `image` is not a 2/3/4D tensor. """ shape = tf.shape(image) original_rank = tf.rank(image) left_pad = tf.cast(tf.less_equal(original_rank, 3), dtype=tf.int32) right_pad = tf.cast(tf.equal(original_rank, 2), dtype=tf.int32) new_shape = tf.concat( [ tf.ones(shape=left_pad, dtype=tf.int32), shape, tf.ones(shape=right_pad, dtype=tf.int32), ], axis=0, ) return tf.reshape(image, new_shape) def from_4d(image: tf.Tensor, ndims: tf.Tensor) -> tf.Tensor: """Converts a 4D image back to `ndims` rank.""" shape = tf.shape(image) begin = tf.cast(tf.less_equal(ndims, 3), dtype=tf.int32) end = 4 - tf.cast(tf.equal(ndims, 2), dtype=tf.int32) new_shape = shape[begin:end] return tf.reshape(image, new_shape) def _convert_translation_to_transform(translations: tf.Tensor) -> tf.Tensor: """Converts translations to a projective transform. The translation matrix looks like this: [[1 0 -dx] [0 1 -dy] [0 0 1]] Args: translations: The 2-element list representing [dx, dy], or a matrix of 2-element lists representing [dx dy] to translate for each image. The shape must be static. Returns: The transformation matrix of shape (num_images, 8). Raises: `TypeError` if - the shape of `translations` is not known or - the shape of `translations` is not rank 1 or 2. """ translations = tf.convert_to_tensor(translations, dtype=tf.float32) if translations.get_shape().ndims is None: raise TypeError('translations rank must be statically known') elif len(translations.get_shape()) == 1: translations = translations[None] elif len(translations.get_shape()) != 2: raise TypeError('translations should have rank 1 or 2.') num_translations = tf.shape(translations)[0] return tf.concat( values=[ tf.ones((num_translations, 1), tf.dtypes.float32), tf.zeros((num_translations, 1), tf.dtypes.float32), -translations[:, 0, None], tf.zeros((num_translations, 1), tf.dtypes.float32), tf.ones((num_translations, 1), tf.dtypes.float32), -translations[:, 1, None], tf.zeros((num_translations, 2), tf.dtypes.float32), ], axis=1, ) def _convert_angles_to_transform(angles: tf.Tensor, image_width: tf.Tensor, image_height: tf.Tensor) -> tf.Tensor: """Converts an angle or angles to a projective transform. Args: angles: A scalar to rotate all images, or a vector to rotate a batch of images. This must be a scalar. image_width: The width of the image(s) to be transformed. image_height: The height of the image(s) to be transformed. Returns: A tensor of shape (num_images, 8). Raises: `TypeError` if `angles` is not rank 0 or 1. """ angles = tf.convert_to_tensor(angles, dtype=tf.float32) if len(angles.get_shape()) == 0: # pylint:disable=g-explicit-length-test angles = angles[None] elif len(angles.get_shape()) != 1: raise TypeError('Angles should have a rank 0 or 1.') x_offset = ((image_width - 1) - (tf.math.cos(angles) * (image_width - 1) - tf.math.sin(angles) * (image_height - 1))) / 2.0 y_offset = ((image_height - 1) - (tf.math.sin(angles) * (image_width - 1) + tf.math.cos(angles) * (image_height - 1))) / 2.0 num_angles = tf.shape(angles)[0] return tf.concat( values=[ tf.math.cos(angles)[:, None], -tf.math.sin(angles)[:, None], x_offset[:, None], tf.math.sin(angles)[:, None], tf.math.cos(angles)[:, None], y_offset[:, None], tf.zeros((num_angles, 2), tf.dtypes.float32), ], axis=1, ) def apply_transform_to_images( images, transforms, fill_mode='reflect', fill_value=0.0, interpolation='bilinear', output_shape=None, name=None, ): """Applies the given transform(s) to the image(s). Args: images: A tensor of shape `(num_images, num_rows, num_columns, num_channels)` (NHWC). The rank must be statically known (the shape is not `TensorShape(None)`). transforms: Projective transform matrix/matrices. A vector of length 8 or tensor of size N x 8. If one row of transforms is [a0, a1, a2, b0, b1, b2, c0, c1], then it maps the *output* point `(x, y)` to a transformed *input* point `(x', y') = ((a0 x + a1 y + a2) / k, (b0 x + b1 y + b2) / k)`, where `k = c0 x + c1 y + 1`. The transforms are *inverted* compared to the transform mapping input points to output points. Note that gradients are not backpropagated into transformation parameters. fill_mode: Points outside the boundaries of the input are filled according to the given mode (one of `{"constant", "reflect", "wrap", "nearest"}`). fill_value: a float represents the value to be filled outside the boundaries when `fill_mode="constant"`. interpolation: Interpolation mode. Supported values: `"nearest"`, `"bilinear"`. output_shape: Output dimension after the transform, `[height, width]`. If `None`, output is the same size as input image. name: The name of the op. Fill mode behavior for each valid value is as follows - `"reflect"`: `(d c b a | a b c d | d c b a)` The input is extended by reflecting about the edge of the last pixel. - `"constant"`: `(k k k k | a b c d | k k k k)` The input is extended by filling all values beyond the edge with the same constant value k = 0. - `"wrap"`: `(a b c d | a b c d | a b c d)` The input is extended by wrapping around to the opposite edge. - `"nearest"`: `(a a a a | a b c d | d d d d)` The input is extended by the nearest pixel. Input shape: 4D tensor with shape: `(samples, height, width, channels)`, in `"channels_last"` format. Output shape: 4D tensor with shape: `(samples, height, width, channels)`, in `"channels_last"` format. Returns: Image(s) with the same type and shape as `images`, with the given transform(s) applied. Transformed coordinates outside of the input image will be filled with zeros. """ with tf.name_scope(name or 'transform'): if output_shape is None: output_shape = tf.shape(images)[1:3] if not tf.executing_eagerly(): output_shape_value = tf.get_static_value(output_shape) if output_shape_value is not None: output_shape = output_shape_value output_shape = tf.convert_to_tensor( output_shape, tf.int32, name='output_shape' ) if not output_shape.get_shape().is_compatible_with([2]): raise ValueError( 'output_shape must be a 1-D Tensor of 2 elements: ' 'new_height, new_width, instead got ' f'output_shape={output_shape}' ) fill_value = tf.convert_to_tensor(fill_value, tf.float32, name='fill_value') return tf.raw_ops.ImageProjectiveTransformV3( images=images, output_shape=output_shape, fill_value=fill_value, transforms=transforms, fill_mode=fill_mode.upper(), interpolation=interpolation.upper(), ) def transform(image: tf.Tensor, transforms) -> tf.Tensor: """Prepares input data for `image_ops.transform`.""" original_ndims = tf.rank(image) transforms = tf.convert_to_tensor(transforms, dtype=tf.float32) if transforms.shape.rank == 1: transforms = transforms[None] image = to_4d(image) image = apply_transform_to_images( images=image, transforms=transforms, interpolation='nearest' ) return from_4d(image, original_ndims) def translate(image: tf.Tensor, translations) -> tf.Tensor: """Translates image(s) by provided vectors. Args: image: An image Tensor of type uint8. translations: A vector or matrix representing [dx dy]. Returns: The translated version of the image. """ transforms = _convert_translation_to_transform(translations) # pytype: disable=wrong-arg-types # always-use-return-annotations return transform(image, transforms=transforms) def rotate(image: tf.Tensor, degrees: float) -> tf.Tensor: """Rotates the image by degrees either clockwise or counterclockwise. Args: image: An image Tensor of type uint8. degrees: Float, a scalar angle in degrees to rotate all images by. If degrees is positive the image will be rotated clockwise otherwise it will be rotated counterclockwise. Returns: The rotated version of image. """ # Convert from degrees to radians. degrees_to_radians = math.pi / 180.0 radians = tf.cast(degrees * degrees_to_radians, tf.float32) original_ndims = tf.rank(image) image = to_4d(image) image_height = tf.cast(tf.shape(image)[1], tf.float32) image_width = tf.cast(tf.shape(image)[2], tf.float32) transforms = _convert_angles_to_transform( angles=radians, image_width=image_width, image_height=image_height) # In practice, we should randomize the rotation degrees by flipping # it negatively half the time, but that's done on 'degrees' outside # of the function. image = transform(image, transforms=transforms) return from_4d(image, original_ndims) def blend(image1: tf.Tensor, image2: tf.Tensor, factor: float) -> tf.Tensor: """Blend image1 and image2 using 'factor'. Factor can be above 0.0. A value of 0.0 means only image1 is used. A value of 1.0 means only image2 is used. A value between 0.0 and 1.0 means we linearly interpolate the pixel values between the two images. A value greater than 1.0 "extrapolates" the difference between the two pixel values, and we clip the results to values between 0 and 255. Args: image1: An image Tensor of type uint8. image2: An image Tensor of type uint8. factor: A floating point value above 0.0. Returns: A blended image Tensor of type uint8. """ if factor == 0.0: return tf.convert_to_tensor(image1) if factor == 1.0: return tf.convert_to_tensor(image2) image1 = tf.cast(image1, tf.float32) image2 = tf.cast(image2, tf.float32) difference = image2 - image1 scaled = factor * difference # Do addition in float. temp = tf.cast(image1, tf.float32) + scaled # Interpolate if factor > 0.0 and factor < 1.0: # Interpolation means we always stay within 0 and 255. return tf.cast(temp, tf.uint8) # Extrapolate: # # We need to clip and then cast. return tf.cast(tf.clip_by_value(temp, 0.0, 255.0), tf.uint8) def cutout(image: tf.Tensor, pad_size: int, replace: int = 0) -> tf.Tensor: """Apply cutout (https://arxiv.org/abs/1708.04552) to image. This operation applies a (2*pad_size x 2*pad_size) mask of zeros to a random location within `img`. The pixel values filled in will be of the value `replace`. The located where the mask will be applied is randomly chosen uniformly over the whole image. Args: image: An image Tensor of type uint8. pad_size: Specifies how big the zero mask that will be generated is that is applied to the image. The mask will be of size (2*pad_size x 2*pad_size). replace: What pixel value to fill in the image in the area that has the cutout mask applied to it. Returns: An image Tensor that is of type uint8. """ image_height = tf.shape(image)[0] image_width = tf.shape(image)[1] # Sample the center location in the image where the zero mask will be applied. cutout_center_height = tf.random.uniform( shape=[], minval=0, maxval=image_height, dtype=tf.int32) cutout_center_width = tf.random.uniform( shape=[], minval=0, maxval=image_width, dtype=tf.int32) lower_pad = tf.maximum(0, cutout_center_height - pad_size) upper_pad = tf.maximum(0, image_height - cutout_center_height - pad_size) left_pad = tf.maximum(0, cutout_center_width - pad_size) right_pad = tf.maximum(0, image_width - cutout_center_width - pad_size) cutout_shape = [ image_height - (lower_pad + upper_pad), image_width - (left_pad + right_pad) ] padding_dims = [[lower_pad, upper_pad], [left_pad, right_pad]] mask = tf.pad( tf.zeros(cutout_shape, dtype=image.dtype), padding_dims, constant_values=1) mask = tf.expand_dims(mask, -1) mask = tf.tile(mask, [1, 1, 3]) image = tf.where( tf.equal(mask, 0), tf.ones_like(image, dtype=image.dtype) * replace, image) return image def solarize(image: tf.Tensor, threshold: int = 128) -> tf.Tensor: # For each pixel in the image, select the pixel # if the value is less than the threshold. # Otherwise, subtract 255 from the pixel. return tf.where(image < threshold, image, 255 - image) def solarize_add(image: tf.Tensor, addition: int = 0, threshold: int = 128) -> tf.Tensor: # For each pixel in the image less than threshold # we add 'addition' amount to it and then clip the # pixel value to be between 0 and 255. The value # of 'addition' is between -128 and 128. added_image = tf.cast(image, tf.int64) + addition added_image = tf.cast(tf.clip_by_value(added_image, 0, 255), tf.uint8) return tf.where(image < threshold, added_image, image) def color(image: tf.Tensor, factor: float) -> tf.Tensor: """Equivalent of PIL Color.""" degenerate = tf.image.grayscale_to_rgb(tf.image.rgb_to_grayscale(image)) return blend(degenerate, image, factor) def contrast(image: tf.Tensor, factor: float) -> tf.Tensor: """Equivalent of PIL Contrast.""" degenerate = tf.image.rgb_to_grayscale(image) # Cast before calling tf.histogram. degenerate = tf.cast(degenerate, tf.int32) # Compute the grayscale histogram, then compute the mean pixel value, # and create a constant image size of that value. Use that as the # blending degenerate target of the original image. hist = tf.histogram_fixed_width(degenerate, [0, 255], nbins=256) mean = tf.reduce_sum(tf.cast(hist, tf.float32)) / 256.0 degenerate = tf.ones_like(degenerate, dtype=tf.float32) * mean degenerate = tf.clip_by_value(degenerate, 0.0, 255.0) degenerate = tf.image.grayscale_to_rgb(tf.cast(degenerate, tf.uint8)) return blend(degenerate, image, factor) def brightness(image: tf.Tensor, factor: float) -> tf.Tensor: """Equivalent of PIL Brightness.""" degenerate = tf.zeros_like(image) return blend(degenerate, image, factor) def posterize(image: tf.Tensor, bits: int) -> tf.Tensor: """Equivalent of PIL Posterize.""" shift = 8 - bits return tf.bitwise.left_shift(tf.bitwise.right_shift(image, shift), shift) def wrapped_rotate(image: tf.Tensor, degrees: float, replace: int) -> tf.Tensor: """Applies rotation with wrap/unwrap.""" image = rotate(wrap(image), degrees=degrees) return unwrap(image, replace) def translate_x(image: tf.Tensor, pixels: int, replace: int) -> tf.Tensor: """Equivalent of PIL Translate in X dimension.""" image = translate(wrap(image), [-pixels, 0]) return unwrap(image, replace) def translate_y(image: tf.Tensor, pixels: int, replace: int) -> tf.Tensor: """Equivalent of PIL Translate in Y dimension.""" image = translate(wrap(image), [0, -pixels]) return unwrap(image, replace) def shear_x(image: tf.Tensor, level: float, replace: int) -> tf.Tensor: """Equivalent of PIL Shearing in X dimension.""" # Shear parallel to x axis is a projective transform # with a matrix form of: # [1 level # 0 1]. image = transform( image=wrap(image), transforms=[1., level, 0., 0., 1., 0., 0., 0.]) return unwrap(image, replace) def shear_y(image: tf.Tensor, level: float, replace: int) -> tf.Tensor: """Equivalent of PIL Shearing in Y dimension.""" # Shear parallel to y axis is a projective transform # with a matrix form of: # [1 0 # level 1]. image = transform( image=wrap(image), transforms=[1., 0., 0., level, 1., 0., 0., 0.]) return unwrap(image, replace) def autocontrast(image: tf.Tensor) -> tf.Tensor: """Implements Autocontrast function from PIL using TF ops. Args: image: A 3D uint8 tensor. Returns: The image after it has had autocontrast applied to it and will be of type uint8. """ def scale_channel(image: tf.Tensor) -> tf.Tensor: """Scale the 2D image using the autocontrast rule.""" # A possibly cheaper version can be done using cumsum/unique_with_counts # over the histogram values, rather than iterating over the entire image. # to compute mins and maxes. lo = tf.cast(tf.reduce_min(image), tf.float32) hi = tf.cast(tf.reduce_max(image), tf.float32) # Scale the image, making the lowest value 0 and the highest value 255. def scale_values(im): scale = 255.0 / (hi - lo) offset = -lo * scale im = tf.cast(im, tf.float32) * scale + offset im = tf.clip_by_value(im, 0.0, 255.0) return tf.cast(im, tf.uint8) result = tf.cond(hi > lo, lambda: scale_values(image), lambda: image) return result # Assumes RGB for now. Scales each channel independently # and then stacks the result. s1 = scale_channel(image[:, :, 0]) s2 = scale_channel(image[:, :, 1]) s3 = scale_channel(image[:, :, 2]) image = tf.stack([s1, s2, s3], 2) return image def sharpness(image: tf.Tensor, factor: float) -> tf.Tensor: """Implements Sharpness function from PIL using TF ops.""" orig_image = image image = tf.cast(image, tf.float32) # Make image 4D for conv operation. image = tf.expand_dims(image, 0) # SMOOTH PIL Kernel. kernel = tf.constant([[1, 1, 1], [1, 5, 1], [1, 1, 1]], dtype=tf.float32, shape=[3, 3, 1, 1]) / 13. # Tile across channel dimension. kernel = tf.tile(kernel, [1, 1, 3, 1]) strides = [1, 1, 1, 1] degenerate = tf.nn.depthwise_conv2d( image, kernel, strides, padding='VALID', dilations=[1, 1]) degenerate = tf.clip_by_value(degenerate, 0.0, 255.0) degenerate = tf.squeeze(tf.cast(degenerate, tf.uint8), [0]) # For the borders of the resulting image, fill in the values of the # original image. mask = tf.ones_like(degenerate) padded_mask = tf.pad(mask, [[1, 1], [1, 1], [0, 0]]) padded_degenerate = tf.pad(degenerate, [[1, 1], [1, 1], [0, 0]]) result = tf.where(tf.equal(padded_mask, 1), padded_degenerate, orig_image) # Blend the final result. return blend(result, orig_image, factor) def equalize(image: tf.Tensor) -> tf.Tensor: """Implements Equalize function from PIL using TF ops.""" def scale_channel(im, c): """Scale the data in the channel to implement equalize.""" im = tf.cast(im[:, :, c], tf.int32) # Compute the histogram of the image channel. histo = tf.histogram_fixed_width(im, [0, 255], nbins=256) # For the purposes of computing the step, filter out the nonzeros. nonzero = tf.where(tf.not_equal(histo, 0)) nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1]) step = (tf.reduce_sum(nonzero_histo) - nonzero_histo[-1]) // 255 def build_lut(histo, step): # Compute the cumulative sum, shifting by step // 2 # and then normalization by step. lut = (tf.cumsum(histo) + (step // 2)) // step # Shift lut, prepending with 0. lut = tf.concat([[0], lut[:-1]], 0) # Clip the counts to be in range. This is done # in the C code for image.point. return tf.clip_by_value(lut, 0, 255) # If step is zero, return the original image. Otherwise, build # lut from the full histogram and step and then index from it. result = tf.cond( tf.equal(step, 0), lambda: im, lambda: tf.gather(build_lut(histo, step), im)) return tf.cast(result, tf.uint8) # Assumes RGB for now. Scales each channel independently # and then stacks the result. s1 = scale_channel(image, 0) s2 = scale_channel(image, 1) s3 = scale_channel(image, 2) image = tf.stack([s1, s2, s3], 2) return image def invert(image: tf.Tensor) -> tf.Tensor: """Inverts the image pixels.""" image = tf.convert_to_tensor(image) return 255 - image def wrap(image: tf.Tensor) -> tf.Tensor: """Returns 'image' with an extra channel set to all 1s.""" shape = tf.shape(image) extended_channel = tf.ones([shape[0], shape[1], 1], image.dtype) extended = tf.concat([image, extended_channel], axis=2) return extended def unwrap(image: tf.Tensor, replace: int) -> tf.Tensor: """Unwraps an image produced by wrap. Where there is a 0 in the last channel for every spatial position, the rest of the three channels in that spatial dimension are grayed (set to 128). Operations like translate and shear on a wrapped Tensor will leave 0s in empty locations. Some transformations look at the intensity of values to do preprocessing, and we want these empty pixels to assume the 'average' value, rather than pure black. Args: image: A 3D Image Tensor with 4 channels. replace: A one or three value 1D tensor to fill empty pixels. Returns: image: A 3D image Tensor with 3 channels. """ image_shape = tf.shape(image) # Flatten the spatial dimensions. flattened_image = tf.reshape(image, [-1, image_shape[2]]) # Find all pixels where the last channel is zero. alpha_channel = tf.expand_dims(flattened_image[:, 3], axis=-1) replace = tf.concat([replace, tf.ones([1], image.dtype)], 0) # Where they are zero, fill them in with 'replace'. flattened_image = tf.where( tf.equal(alpha_channel, 0), tf.ones_like(flattened_image, dtype=image.dtype) * replace, flattened_image) image = tf.reshape(flattened_image, image_shape) image = tf.slice(image, [0, 0, 0], [image_shape[0], image_shape[1], 3]) return image def _randomly_negate_tensor(tensor): """With 50% prob turn the tensor negative.""" should_flip = tf.cast(tf.floor(tf.random.uniform([]) + 0.5), tf.bool) final_tensor = tf.cond(should_flip, lambda: tensor, lambda: -tensor) return final_tensor def _rotate_level_to_arg(level: float): level = (level / _MAX_LEVEL) * 30. level = _randomly_negate_tensor(level) return (level,) def _shrink_level_to_arg(level: float): """Converts level to ratio by which we shrink the image content.""" if level == 0: return (1.0,) # if level is zero, do not shrink the image # Maximum shrinking ratio is 2.9. level = 2. / (_MAX_LEVEL / level) + 0.9 return (level,) def _enhance_level_to_arg(level: float): return ((level / _MAX_LEVEL) * 1.8 + 0.1,) def _shear_level_to_arg(level: float): level = (level / _MAX_LEVEL) * 0.3 # Flip level to negative with 50% chance. level = _randomly_negate_tensor(level) return (level,) def _translate_level_to_arg(level: float, translate_const: float): level = (level / _MAX_LEVEL) * float(translate_const) # Flip level to negative with 50% chance. level = _randomly_negate_tensor(level) return (level,) def _mult_to_arg(level: float, multiplier: float = 1.): return (int((level / _MAX_LEVEL) * multiplier),) def _apply_func_with_prob(func: Any, image: tf.Tensor, args: Any, prob: float): """Apply `func` to image w/ `args` as input with probability `prob`.""" assert isinstance(args, tuple) # Apply the function with probability `prob`. should_apply_op = tf.cast( tf.floor(tf.random.uniform([], dtype=tf.float32) + prob), tf.bool) augmented_image = tf.cond(should_apply_op, lambda: func(image, *args), lambda: image) return augmented_image def select_and_apply_random_policy(policies: Any, image: tf.Tensor): """Select a random policy from `policies` and apply it to `image`.""" policy_to_select = tf.random.uniform([], maxval=len(policies), dtype=tf.int32) # Note that using tf.case instead of tf.conds would result in significantly # larger graphs and would even break export for some larger policies. for (i, policy) in enumerate(policies): image = tf.cond( tf.equal(i, policy_to_select), lambda selected_policy=policy: selected_policy(image), lambda: image) return image NAME_TO_FUNC = { 'AutoContrast': autocontrast, 'Equalize': equalize, 'Invert': invert, 'Rotate': wrapped_rotate, 'Posterize': posterize, 'Solarize': solarize, 'SolarizeAdd': solarize_add, 'Color': color, 'Contrast': contrast, 'Brightness': brightness, 'Sharpness': sharpness, 'ShearX': shear_x, 'ShearY': shear_y, 'TranslateX': translate_x, 'TranslateY': translate_y, 'Cutout': cutout, } # Functions that have a 'replace' parameter REPLACE_FUNCS = frozenset({ 'Rotate', 'TranslateX', 'ShearX', 'ShearY', 'TranslateY', 'Cutout', }) def level_to_arg(cutout_const: float, translate_const: float): """Creates a dict mapping image operation names to their arguments.""" no_arg = lambda level: () posterize_arg = lambda level: _mult_to_arg(level, 4) solarize_arg = lambda level: _mult_to_arg(level, 256) solarize_add_arg = lambda level: _mult_to_arg(level, 110) cutout_arg = lambda level: _mult_to_arg(level, cutout_const) translate_arg = lambda level: _translate_level_to_arg(level, translate_const) args = { 'AutoContrast': no_arg, 'Equalize': no_arg, 'Invert': no_arg, 'Rotate': _rotate_level_to_arg, 'Posterize': posterize_arg, 'Solarize': solarize_arg, 'SolarizeAdd': solarize_add_arg, 'Color': _enhance_level_to_arg, 'Contrast': _enhance_level_to_arg, 'Brightness': _enhance_level_to_arg, 'Sharpness': _enhance_level_to_arg, 'ShearX': _shear_level_to_arg, 'ShearY': _shear_level_to_arg, 'Cutout': cutout_arg, 'TranslateX': translate_arg, 'TranslateY': translate_arg, } return args def _parse_policy_info(name: Text, prob: float, level: float, replace_value: List[int], cutout_const: float, translate_const: float) -> Tuple[Any, float, Any]: """Return the function that corresponds to `name` and update `level` param.""" func = NAME_TO_FUNC[name] args = level_to_arg(cutout_const, translate_const)[name](level) if name in REPLACE_FUNCS: # Add in replace arg if it is required for the function that is called. args = tuple(list(args) + [replace_value]) return func, prob, args class ImageAugment(object): """Image augmentation class for applying image distortions.""" def distort(self, image: tf.Tensor) -> tf.Tensor: """Given an image tensor, returns a distorted image with the same shape. Args: image: `Tensor` of shape [height, width, 3] representing an image. Returns: The augmented version of `image`. """ raise NotImplementedError() class AutoAugment(ImageAugment): """Applies the AutoAugment policy to images. AutoAugment is from the paper: https://arxiv.org/abs/1805.09501. """ def __init__(self, augmentation_name: Text = 'v0', policies: Optional[Dict[Text, Any]] = None, cutout_const: float = 100, translate_const: float = 250): """Applies the AutoAugment policy to images. Args: augmentation_name: The name of the AutoAugment policy to use. The available options are `v0` and `test`. `v0` is the policy used for all of the results in the paper and was found to achieve the best results on the COCO dataset. `v1`, `v2` and `v3` are additional good policies found on the COCO dataset that have slight variation in what operations were used during the search procedure along with how many operations are applied in parallel to a single image (2 vs 3). policies: list of lists of tuples in the form `(func, prob, level)`, `func` is a string name of the augmentation function, `prob` is the probability of applying the `func` operation, `level` is the input argument for `func`. cutout_const: multiplier for applying cutout. translate_const: multiplier for applying translation. """ super(AutoAugment, self).__init__() if policies is None: self.available_policies = { 'v0': self.policy_v0(), 'test': self.policy_test(), 'simple': self.policy_simple(), } if augmentation_name not in self.available_policies: raise ValueError( 'Invalid augmentation_name: {}'.format(augmentation_name)) self.augmentation_name = augmentation_name self.policies = self.available_policies[augmentation_name] self.cutout_const = float(cutout_const) self.translate_const = float(translate_const) def distort(self, image: tf.Tensor) -> tf.Tensor: """Applies the AutoAugment policy to `image`. AutoAugment is from the paper: https://arxiv.org/abs/1805.09501. Args: image: `Tensor` of shape [height, width, 3] representing an image. Returns: A version of image that now has data augmentation applied to it based on the `policies` pass into the function. """ input_image_type = image.dtype if input_image_type != tf.uint8: image = tf.clip_by_value(image, 0.0, 255.0) image = tf.cast(image, dtype=tf.uint8) replace_value = [128] * 3 # func is the string name of the augmentation function, prob is the # probability of applying the operation and level is the parameter # associated with the tf op. # tf_policies are functions that take in an image and return an augmented # image. tf_policies = [] for policy in self.policies: tf_policy = [] # Link string name to the correct python function and make sure the # correct argument is passed into that function. for policy_info in policy: policy_info = list(policy_info) + [ replace_value, self.cutout_const, self.translate_const ] tf_policy.append(_parse_policy_info(*policy_info)) # Now build the tf policy that will apply the augmentation procedue # on image. def make_final_policy(tf_policy_): def final_policy(image_): for func, prob, args in tf_policy_: image_ = _apply_func_with_prob(func, image_, args, prob) return image_ return final_policy tf_policies.append(make_final_policy(tf_policy)) image = select_and_apply_random_policy(tf_policies, image) image = tf.cast(image, dtype=input_image_type) return image @staticmethod def policy_v0(): """Autoaugment policy that was used in AutoAugment Paper. Each tuple is an augmentation operation of the form (operation, probability, magnitude). Each element in policy is a sub-policy that will be applied sequentially on the image. Returns: the policy. """ policy = [ [('Equalize', 0.8, 1), ('ShearY', 0.8, 4)], [('Color', 0.4, 9), ('Equalize', 0.6, 3)], [('Color', 0.4, 1), ('Rotate', 0.6, 8)], [('Solarize', 0.8, 3), ('Equalize', 0.4, 7)], [('Solarize', 0.4, 2), ('Solarize', 0.6, 2)], [('Color', 0.2, 0), ('Equalize', 0.8, 8)], [('Equalize', 0.4, 8), ('SolarizeAdd', 0.8, 3)], [('ShearX', 0.2, 9), ('Rotate', 0.6, 8)], [('Color', 0.6, 1), ('Equalize', 1.0, 2)], [('Invert', 0.4, 9), ('Rotate', 0.6, 0)], [('Equalize', 1.0, 9), ('ShearY', 0.6, 3)], [('Color', 0.4, 7), ('Equalize', 0.6, 0)], [('Posterize', 0.4, 6), ('AutoContrast', 0.4, 7)], [('Solarize', 0.6, 8), ('Color', 0.6, 9)], [('Solarize', 0.2, 4), ('Rotate', 0.8, 9)], [('Rotate', 1.0, 7), ('TranslateY', 0.8, 9)], [('ShearX', 0.0, 0), ('Solarize', 0.8, 4)], [('ShearY', 0.8, 0), ('Color', 0.6, 4)], [('Color', 1.0, 0), ('Rotate', 0.6, 2)], [('Equalize', 0.8, 4), ('Equalize', 0.0, 8)], [('Equalize', 1.0, 4), ('AutoContrast', 0.6, 2)], [('ShearY', 0.4, 7), ('SolarizeAdd', 0.6, 7)], [('Posterize', 0.8, 2), ('Solarize', 0.6, 10)], [('Solarize', 0.6, 8), ('Equalize', 0.6, 1)], [('Color', 0.8, 6), ('Rotate', 0.4, 5)], ] return policy @staticmethod def policy_simple(): """Same as `policy_v0`, except with custom ops removed.""" policy = [ [('Color', 0.4, 9), ('Equalize', 0.6, 3)], [('Solarize', 0.8, 3), ('Equalize', 0.4, 7)], [('Solarize', 0.4, 2), ('Solarize', 0.6, 2)], [('Color', 0.2, 0), ('Equalize', 0.8, 8)], [('Equalize', 0.4, 8), ('SolarizeAdd', 0.8, 3)], [('Color', 0.6, 1), ('Equalize', 1.0, 2)], [('Color', 0.4, 7), ('Equalize', 0.6, 0)], [('Posterize', 0.4, 6), ('AutoContrast', 0.4, 7)], [('Solarize', 0.6, 8), ('Color', 0.6, 9)], [('Equalize', 0.8, 4), ('Equalize', 0.0, 8)], [('Equalize', 1.0, 4), ('AutoContrast', 0.6, 2)], [('Posterize', 0.8, 2), ('Solarize', 0.6, 10)], [('Solarize', 0.6, 8), ('Equalize', 0.6, 1)], ] return policy @staticmethod def policy_test(): """Autoaugment test policy for debugging.""" policy = [ [('TranslateX', 1.0, 4), ('Equalize', 1.0, 10)], ] return policy class RandAugment(ImageAugment): """Applies the RandAugment policy to images. RandAugment is from the paper https://arxiv.org/abs/1909.13719, """ def __init__(self, num_layers: int = 2, magnitude: float = 10., cutout_const: float = 40., translate_const: float = 100.): """Applies the RandAugment policy to images. Args: num_layers: Integer, the number of augmentation transformations to apply sequentially to an image. Represented as (N) in the paper. Usually best values will be in the range [1, 3]. magnitude: Integer, shared magnitude across all augmentation operations. Represented as (M) in the paper. Usually best values are in the range [5, 10]. cutout_const: multiplier for applying cutout. translate_const: multiplier for applying translation. """ super(RandAugment, self).__init__() self.num_layers = num_layers self.magnitude = float(magnitude) self.cutout_const = float(cutout_const) self.translate_const = float(translate_const) self.available_ops = [ 'AutoContrast', 'Equalize', 'Invert', 'Rotate', 'Posterize', 'Solarize', 'Color', 'Contrast', 'Brightness', 'Sharpness', 'ShearX', 'ShearY', 'TranslateX', 'TranslateY', 'Cutout', 'SolarizeAdd' ] def distort(self, image: tf.Tensor) -> tf.Tensor: """Applies the RandAugment policy to `image`. Args: image: `Tensor` of shape [height, width, 3] representing an image. Returns: The augmented version of `image`. """ input_image_type = image.dtype if input_image_type != tf.uint8: image = tf.clip_by_value(image, 0.0, 255.0) image = tf.cast(image, dtype=tf.uint8) replace_value = [128] * 3 min_prob, max_prob = 0.2, 0.8 for _ in range(self.num_layers): op_to_select = tf.random.uniform([], maxval=len(self.available_ops) + 1, dtype=tf.int32) branch_fns = [] for (i, op_name) in enumerate(self.available_ops): prob = tf.random.uniform([], minval=min_prob, maxval=max_prob, dtype=tf.float32) func, _, args = _parse_policy_info(op_name, prob, self.magnitude, replace_value, self.cutout_const, self.translate_const) branch_fns.append(( i, # pylint:disable=g-long-lambda lambda selected_func=func, selected_args=args: selected_func( image, *selected_args))) # pylint:enable=g-long-lambda image = tf.switch_case( branch_index=op_to_select, branch_fns=branch_fns, default=lambda: tf.identity(image)) image = tf.cast(image, dtype=input_image_type) return image
37,501
34.312618
130
py
models
models-master/official/legacy/image_classification/mnist_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Test the Keras MNIST model on GPU.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import functools from absl.testing import parameterized import tensorflow as tf from tensorflow.python.distribute import combinations from tensorflow.python.distribute import strategy_combinations from official.legacy.image_classification import mnist_main from official.utils.testing import integration mnist_main.define_mnist_flags() def eager_strategy_combinations(): return combinations.combine( distribution=[ strategy_combinations.default_strategy, strategy_combinations.cloud_tpu_strategy, strategy_combinations.one_device_strategy_gpu, ],) class KerasMnistTest(tf.test.TestCase, parameterized.TestCase): """Unit tests for sample Keras MNIST model.""" _tempdir = None @classmethod def setUpClass(cls): # pylint: disable=invalid-name super(KerasMnistTest, cls).setUpClass() def tearDown(self): super(KerasMnistTest, self).tearDown() tf.io.gfile.rmtree(self.get_temp_dir()) @combinations.generate(eager_strategy_combinations()) def test_end_to_end(self, distribution): """Test Keras MNIST model with `strategy`.""" extra_flags = [ "-train_epochs", "1", # Let TFDS find the metadata folder automatically "--data_dir=" ] dummy_data = ( tf.ones(shape=(10, 28, 28, 1), dtype=tf.int32), tf.range(10), ) datasets = ( tf.data.Dataset.from_tensor_slices(dummy_data), tf.data.Dataset.from_tensor_slices(dummy_data), ) run = functools.partial( mnist_main.run, datasets_override=datasets, strategy_override=distribution) integration.run_synthetic( main=run, synth=False, tmp_root=self.create_tempdir().full_path, extra_flags=extra_flags) if __name__ == "__main__": tf.test.main()
2,581
27.688889
74
py
models
models-master/official/legacy/image_classification/classifier_trainer_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Unit tests for the classifier trainer models.""" import functools import json import os import sys from typing import Any, Callable, Iterable, Mapping, MutableMapping, Optional, Tuple from absl import flags from absl.testing import flagsaver from absl.testing import parameterized import tensorflow as tf from tensorflow.python.distribute import combinations from tensorflow.python.distribute import strategy_combinations from official.legacy.image_classification import classifier_trainer from official.utils.flags import core as flags_core classifier_trainer.define_classifier_flags() def distribution_strategy_combinations() -> Iterable[Tuple[Any, ...]]: """Returns the combinations of end-to-end tests to run.""" return combinations.combine( distribution=[ strategy_combinations.default_strategy, strategy_combinations.cloud_tpu_strategy, strategy_combinations.one_device_strategy_gpu, strategy_combinations.mirrored_strategy_with_two_gpus, ], model=[ 'efficientnet', 'resnet', 'vgg', ], dataset=[ 'imagenet', ], ) def get_params_override(params_override: Mapping[str, Any]) -> str: """Converts params_override dict to string command.""" return '--params_override=' + json.dumps(params_override) def basic_params_override(dtype: str = 'float32') -> MutableMapping[str, Any]: """Returns a basic parameter configuration for testing.""" return { 'train_dataset': { 'builder': 'synthetic', 'use_per_replica_batch_size': True, 'batch_size': 1, 'image_size': 224, 'dtype': dtype, }, 'validation_dataset': { 'builder': 'synthetic', 'batch_size': 1, 'use_per_replica_batch_size': True, 'image_size': 224, 'dtype': dtype, }, 'train': { 'steps': 1, 'epochs': 1, 'callbacks': { 'enable_checkpoint_and_export': True, 'enable_tensorboard': False, }, }, 'evaluation': { 'steps': 1, }, } @flagsaver.flagsaver def run_end_to_end(main: Callable[[Any], None], extra_flags: Optional[Iterable[str]] = None, model_dir: Optional[str] = None): """Runs the classifier trainer end-to-end.""" extra_flags = [] if extra_flags is None else extra_flags args = [sys.argv[0], '--model_dir', model_dir] + extra_flags flags_core.parse_flags(argv=args) main(flags.FLAGS) class ClassifierTest(tf.test.TestCase, parameterized.TestCase): """Unit tests for Keras models.""" _tempdir = None @classmethod def setUpClass(cls): # pylint: disable=invalid-name super(ClassifierTest, cls).setUpClass() def tearDown(self): super(ClassifierTest, self).tearDown() tf.io.gfile.rmtree(self.get_temp_dir()) @combinations.generate(distribution_strategy_combinations()) def test_end_to_end_train_and_eval(self, distribution, model, dataset): """Test train_and_eval and export for Keras classifier models.""" # Some parameters are not defined as flags (e.g. cannot run # classifier_train.py --batch_size=...) by design, so use # "--params_override=..." instead model_dir = self.create_tempdir().full_path base_flags = [ '--data_dir=not_used', '--model_type=' + model, '--dataset=' + dataset, ] train_and_eval_flags = base_flags + [ get_params_override(basic_params_override()), '--mode=train_and_eval', ] run = functools.partial( classifier_trainer.run, strategy_override=distribution) run_end_to_end( main=run, extra_flags=train_and_eval_flags, model_dir=model_dir) @combinations.generate( combinations.combine( distribution=[ strategy_combinations.one_device_strategy_gpu, ], model=[ 'efficientnet', 'resnet', 'vgg', ], dataset='imagenet', dtype='float16', )) def test_gpu_train(self, distribution, model, dataset, dtype): """Test train_and_eval and export for Keras classifier models.""" # Some parameters are not defined as flags (e.g. cannot run # classifier_train.py --batch_size=...) by design, so use # "--params_override=..." instead model_dir = self.create_tempdir().full_path base_flags = [ '--data_dir=not_used', '--model_type=' + model, '--dataset=' + dataset, ] train_and_eval_flags = base_flags + [ get_params_override(basic_params_override(dtype)), '--mode=train_and_eval', ] export_params = basic_params_override() export_path = os.path.join(model_dir, 'export') export_params['export'] = {} export_params['export']['destination'] = export_path export_flags = base_flags + [ '--mode=export_only', get_params_override(export_params) ] run = functools.partial( classifier_trainer.run, strategy_override=distribution) run_end_to_end( main=run, extra_flags=train_and_eval_flags, model_dir=model_dir) run_end_to_end(main=run, extra_flags=export_flags, model_dir=model_dir) self.assertTrue(os.path.exists(export_path)) @combinations.generate( combinations.combine( distribution=[ strategy_combinations.cloud_tpu_strategy, ], model=[ 'efficientnet', 'resnet', 'vgg', ], dataset='imagenet', dtype='bfloat16', )) def test_tpu_train(self, distribution, model, dataset, dtype): """Test train_and_eval and export for Keras classifier models.""" # Some parameters are not defined as flags (e.g. cannot run # classifier_train.py --batch_size=...) by design, so use # "--params_override=..." instead model_dir = self.create_tempdir().full_path base_flags = [ '--data_dir=not_used', '--model_type=' + model, '--dataset=' + dataset, ] train_and_eval_flags = base_flags + [ get_params_override(basic_params_override(dtype)), '--mode=train_and_eval', ] run = functools.partial( classifier_trainer.run, strategy_override=distribution) run_end_to_end( main=run, extra_flags=train_and_eval_flags, model_dir=model_dir) @combinations.generate(distribution_strategy_combinations()) def test_end_to_end_invalid_mode(self, distribution, model, dataset): """Test the Keras EfficientNet model with `strategy`.""" model_dir = self.create_tempdir().full_path extra_flags = [ '--data_dir=not_used', '--mode=invalid_mode', '--model_type=' + model, '--dataset=' + dataset, get_params_override(basic_params_override()), ] run = functools.partial( classifier_trainer.run, strategy_override=distribution) with self.assertRaises(ValueError): run_end_to_end(main=run, extra_flags=extra_flags, model_dir=model_dir) if __name__ == '__main__': tf.test.main()
7,755
31.451883
84
py
models
models-master/official/legacy/image_classification/learning_rate_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for learning_rate.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf from official.legacy.image_classification import learning_rate class LearningRateTests(tf.test.TestCase): def test_warmup_decay(self): """Basic computational test for warmup decay.""" initial_lr = 0.01 decay_steps = 100 decay_rate = 0.01 warmup_steps = 10 base_lr = tf.keras.optimizers.schedules.ExponentialDecay( initial_learning_rate=initial_lr, decay_steps=decay_steps, decay_rate=decay_rate) lr = learning_rate.WarmupDecaySchedule( lr_schedule=base_lr, warmup_steps=warmup_steps) for step in range(warmup_steps - 1): config = lr.get_config() self.assertEqual(config['warmup_steps'], warmup_steps) self.assertAllClose( self.evaluate(lr(step)), step / warmup_steps * initial_lr) def test_cosine_decay_with_warmup(self): """Basic computational test for cosine decay with warmup.""" expected_lrs = [0.0, 0.1, 0.05, 0.0] lr = learning_rate.CosineDecayWithWarmup( batch_size=256, total_steps=3, warmup_steps=1) for step in [0, 1, 2, 3]: self.assertAllClose(lr(step), expected_lrs[step]) if __name__ == '__main__': tf.test.main()
1,941
30.836066
74
py
models
models-master/official/legacy/image_classification/augment_test.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for autoaugment.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from absl.testing import parameterized import tensorflow as tf from official.legacy.image_classification import augment def get_dtype_test_cases(): return [ ('uint8', tf.uint8), ('int32', tf.int32), ('float16', tf.float16), ('float32', tf.float32), ] @parameterized.named_parameters(get_dtype_test_cases()) class TransformsTest(parameterized.TestCase, tf.test.TestCase): """Basic tests for fundamental transformations.""" def test_to_from_4d(self, dtype): for shape in [(10, 10), (10, 10, 10), (10, 10, 10, 10)]: original_ndims = len(shape) image = tf.zeros(shape, dtype=dtype) image_4d = augment.to_4d(image) self.assertEqual(4, tf.rank(image_4d)) self.assertAllEqual(image, augment.from_4d(image_4d, original_ndims)) def test_transform(self, dtype): image = tf.constant([[1, 2], [3, 4]], dtype=dtype) self.assertAllEqual( augment.transform(image, transforms=[1] * 8), [[4, 4], [4, 4]]) def test_translate(self, dtype): image = tf.constant( [[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1]], dtype=dtype) translations = [-1, -1] translated = augment.translate(image=image, translations=translations) expected = [[1, 0, 1, 1], [0, 1, 0, 0], [1, 0, 1, 1], [1, 0, 1, 1]] self.assertAllEqual(translated, expected) def test_translate_shapes(self, dtype): translation = [0, 0] for shape in [(3, 3), (5, 5), (224, 224, 3)]: image = tf.zeros(shape, dtype=dtype) self.assertAllEqual(image, augment.translate(image, translation)) def test_translate_invalid_translation(self, dtype): image = tf.zeros((1, 1), dtype=dtype) invalid_translation = [[[1, 1]]] with self.assertRaisesRegex(TypeError, 'rank 1 or 2'): _ = augment.translate(image, invalid_translation) def test_rotate(self, dtype): image = tf.reshape(tf.cast(tf.range(9), dtype), (3, 3)) rotation = 90. transformed = augment.rotate(image=image, degrees=rotation) expected = [[2, 5, 8], [1, 4, 7], [0, 3, 6]] self.assertAllEqual(transformed, expected) def test_rotate_shapes(self, dtype): degrees = 0. for shape in [(3, 3), (5, 5), (224, 224, 3)]: image = tf.zeros(shape, dtype=dtype) self.assertAllEqual(image, augment.rotate(image, degrees)) class AutoaugmentTest(tf.test.TestCase): def test_autoaugment(self): """Smoke test to be sure there are no syntax errors.""" image = tf.zeros((224, 224, 3), dtype=tf.uint8) augmenter = augment.AutoAugment() aug_image = augmenter.distort(image) self.assertEqual((224, 224, 3), aug_image.shape) def test_randaug(self): """Smoke test to be sure there are no syntax errors.""" image = tf.zeros((224, 224, 3), dtype=tf.uint8) augmenter = augment.RandAugment() aug_image = augmenter.distort(image) self.assertEqual((224, 224, 3), aug_image.shape) def test_all_policy_ops(self): """Smoke test to be sure all augmentation functions can execute.""" prob = 1 magnitude = 10 replace_value = [128] * 3 cutout_const = 100 translate_const = 250 image = tf.ones((224, 224, 3), dtype=tf.uint8) for op_name in augment.NAME_TO_FUNC: func, _, args = augment._parse_policy_info(op_name, prob, magnitude, replace_value, cutout_const, translate_const) image = func(image, *args) self.assertEqual((224, 224, 3), image.shape) if __name__ == '__main__': tf.test.main()
4,313
32.184615
78
py
models
models-master/official/legacy/image_classification/classifier_trainer.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Runs an Image Classification model.""" import os import pprint from typing import Any, Mapping, Optional, Text, Tuple from absl import app from absl import flags from absl import logging import tensorflow as tf from official.common import distribute_utils from official.legacy.image_classification import callbacks as custom_callbacks from official.legacy.image_classification import dataset_factory from official.legacy.image_classification import optimizer_factory from official.legacy.image_classification.configs import base_configs from official.legacy.image_classification.configs import configs from official.legacy.image_classification.efficientnet import efficientnet_model from official.legacy.image_classification.resnet import common from official.legacy.image_classification.resnet import resnet_model from official.legacy.image_classification.vgg import vgg_model from official.modeling import hyperparams from official.modeling import performance from official.utils import hyperparams_flags from official.utils.misc import keras_utils def get_models() -> Mapping[str, tf.keras.Model]: """Returns the mapping from model type name to Keras model.""" return { 'efficientnet': efficientnet_model.EfficientNet.from_name, 'resnet': resnet_model.resnet50, 'vgg': vgg_model.vgg16, } def get_dtype_map() -> Mapping[str, tf.dtypes.DType]: """Returns the mapping from dtype string representations to TF dtypes.""" return { 'float32': tf.float32, 'bfloat16': tf.bfloat16, 'float16': tf.float16, 'fp32': tf.float32, 'bf16': tf.bfloat16, } def _get_metrics(one_hot: bool) -> Mapping[Text, Any]: """Get a dict of available metrics to track.""" if one_hot: return { # (name, metric_fn) 'acc': tf.keras.metrics.CategoricalAccuracy(name='accuracy'), 'accuracy': tf.keras.metrics.CategoricalAccuracy(name='accuracy'), 'top_1': tf.keras.metrics.CategoricalAccuracy(name='accuracy'), 'top_5': tf.keras.metrics.TopKCategoricalAccuracy( k=5, name='top_5_accuracy'), } else: return { # (name, metric_fn) 'acc': tf.keras.metrics.SparseCategoricalAccuracy(name='accuracy'), 'accuracy': tf.keras.metrics.SparseCategoricalAccuracy(name='accuracy'), 'top_1': tf.keras.metrics.SparseCategoricalAccuracy(name='accuracy'), 'top_5': tf.keras.metrics.SparseTopKCategoricalAccuracy( k=5, name='top_5_accuracy'), } def get_image_size_from_model( params: base_configs.ExperimentConfig) -> Optional[int]: """If the given model has a preferred image size, return it.""" if params.model_name == 'efficientnet': efficientnet_name = params.model.model_params.model_name if efficientnet_name in efficientnet_model.MODEL_CONFIGS: return efficientnet_model.MODEL_CONFIGS[efficientnet_name].resolution return None def _get_dataset_builders(params: base_configs.ExperimentConfig, strategy: tf.distribute.Strategy, one_hot: bool) -> Tuple[Any, Any]: """Create and return train and validation dataset builders.""" if one_hot: logging.warning('label_smoothing > 0, so datasets will be one hot encoded.') else: logging.warning('label_smoothing not applied, so datasets will not be one ' 'hot encoded.') num_devices = strategy.num_replicas_in_sync if strategy else 1 image_size = get_image_size_from_model(params) dataset_configs = [params.train_dataset, params.validation_dataset] builders = [] for config in dataset_configs: if config is not None and config.has_data: builder = dataset_factory.DatasetBuilder( config, image_size=image_size or config.image_size, num_devices=num_devices, one_hot=one_hot) else: builder = None builders.append(builder) return builders def get_loss_scale(params: base_configs.ExperimentConfig, fp16_default: float = 128.) -> float: """Returns the loss scale for initializations.""" loss_scale = params.runtime.loss_scale if loss_scale == 'dynamic': return loss_scale elif loss_scale is not None: return float(loss_scale) elif (params.train_dataset.dtype == 'float32' or params.train_dataset.dtype == 'bfloat16'): return 1. else: assert params.train_dataset.dtype == 'float16' return fp16_default def _get_params_from_flags(flags_obj: flags.FlagValues): """Get ParamsDict from flags.""" model = flags_obj.model_type.lower() dataset = flags_obj.dataset.lower() params = configs.get_config(model=model, dataset=dataset) flags_overrides = { 'model_dir': flags_obj.model_dir, 'mode': flags_obj.mode, 'model': { 'name': model, }, 'runtime': { 'run_eagerly': flags_obj.run_eagerly, 'tpu': flags_obj.tpu, }, 'train_dataset': { 'data_dir': flags_obj.data_dir, }, 'validation_dataset': { 'data_dir': flags_obj.data_dir, }, 'train': { 'time_history': { 'log_steps': flags_obj.log_steps, }, }, } overriding_configs = (flags_obj.config_file, flags_obj.params_override, flags_overrides) pp = pprint.PrettyPrinter() logging.info('Base params: %s', pp.pformat(params.as_dict())) for param in overriding_configs: logging.info('Overriding params: %s', param) params = hyperparams.override_params_dict(params, param, is_strict=True) params.validate() params.lock() logging.info('Final model parameters: %s', pp.pformat(params.as_dict())) return params def resume_from_checkpoint(model: tf.keras.Model, model_dir: str, train_steps: int) -> int: """Resumes from the latest checkpoint, if possible. Loads the model weights and optimizer settings from a checkpoint. This function should be used in case of preemption recovery. Args: model: The model whose weights should be restored. model_dir: The directory where model weights were saved. train_steps: The number of steps to train. Returns: The epoch of the latest checkpoint, or 0 if not restoring. """ logging.info('Load from checkpoint is enabled.') latest_checkpoint = tf.train.latest_checkpoint(model_dir) logging.info('latest_checkpoint: %s', latest_checkpoint) if not latest_checkpoint: logging.info('No checkpoint detected.') return 0 logging.info('Checkpoint file %s found and restoring from ' 'checkpoint', latest_checkpoint) model.load_weights(latest_checkpoint) initial_epoch = model.optimizer.iterations // train_steps logging.info('Completed loading from checkpoint.') logging.info('Resuming from epoch %d', initial_epoch) return int(initial_epoch) def initialize(params: base_configs.ExperimentConfig, dataset_builder: dataset_factory.DatasetBuilder): """Initializes backend related initializations.""" keras_utils.set_session_config(enable_xla=params.runtime.enable_xla) performance.set_mixed_precision_policy(dataset_builder.dtype) if tf.config.list_physical_devices('GPU'): data_format = 'channels_first' else: data_format = 'channels_last' tf.keras.backend.set_image_data_format(data_format) if params.runtime.run_eagerly: # Enable eager execution to allow step-by-step debugging tf.config.experimental_run_functions_eagerly(True) if tf.config.list_physical_devices('GPU'): if params.runtime.gpu_thread_mode: keras_utils.set_gpu_thread_mode_and_count( per_gpu_thread_count=params.runtime.per_gpu_thread_count, gpu_thread_mode=params.runtime.gpu_thread_mode, num_gpus=params.runtime.num_gpus, datasets_num_private_threads=params.runtime .dataset_num_private_threads) # pylint:disable=line-too-long if params.runtime.batchnorm_spatial_persistent: os.environ['TF_USE_CUDNN_BATCHNORM_SPATIAL_PERSISTENT'] = '1' def define_classifier_flags(): """Defines common flags for image classification.""" hyperparams_flags.initialize_common_flags() flags.DEFINE_string( 'data_dir', default=None, help='The location of the input data.') flags.DEFINE_string( 'mode', default=None, help='Mode to run: `train`, `eval`, `train_and_eval` or `export`.') flags.DEFINE_bool( 'run_eagerly', default=None, help='Use eager execution and disable autograph for debugging.') flags.DEFINE_string( 'model_type', default=None, help='The type of the model, e.g. EfficientNet, etc.') flags.DEFINE_string( 'dataset', default=None, help='The name of the dataset, e.g. ImageNet, etc.') flags.DEFINE_integer( 'log_steps', default=100, help='The interval of steps between logging of batch level stats.') def serialize_config(params: base_configs.ExperimentConfig, model_dir: str): """Serializes and saves the experiment config.""" params_save_path = os.path.join(model_dir, 'params.yaml') logging.info('Saving experiment configuration to %s', params_save_path) tf.io.gfile.makedirs(model_dir) hyperparams.save_params_dict_to_yaml(params, params_save_path) def train_and_eval( params: base_configs.ExperimentConfig, strategy_override: tf.distribute.Strategy) -> Mapping[str, Any]: """Runs the train and eval path using compile/fit.""" logging.info('Running train and eval.') distribute_utils.configure_cluster(params.runtime.worker_hosts, params.runtime.task_index) # Note: for TPUs, strategy and scope should be created before the dataset strategy = strategy_override or distribute_utils.get_distribution_strategy( distribution_strategy=params.runtime.distribution_strategy, all_reduce_alg=params.runtime.all_reduce_alg, num_gpus=params.runtime.num_gpus, tpu_address=params.runtime.tpu) strategy_scope = distribute_utils.get_strategy_scope(strategy) logging.info('Detected %d devices.', strategy.num_replicas_in_sync if strategy else 1) label_smoothing = params.model.loss.label_smoothing one_hot = label_smoothing and label_smoothing > 0 builders = _get_dataset_builders(params, strategy, one_hot) datasets = [ builder.build(strategy) if builder else None for builder in builders ] # Unpack datasets and builders based on train/val/test splits train_builder, validation_builder = builders # pylint: disable=unbalanced-tuple-unpacking train_dataset, validation_dataset = datasets train_epochs = params.train.epochs train_steps = params.train.steps or train_builder.num_steps validation_steps = params.evaluation.steps or validation_builder.num_steps initialize(params, train_builder) logging.info('Global batch size: %d', train_builder.global_batch_size) with strategy_scope: model_params = params.model.model_params.as_dict() model = get_models()[params.model.name](**model_params) learning_rate = optimizer_factory.build_learning_rate( params=params.model.learning_rate, batch_size=train_builder.global_batch_size, train_epochs=train_epochs, train_steps=train_steps) optimizer = optimizer_factory.build_optimizer( optimizer_name=params.model.optimizer.name, base_learning_rate=learning_rate, params=params.model.optimizer.as_dict(), model=model) optimizer = performance.configure_optimizer( optimizer, use_float16=train_builder.dtype == 'float16', loss_scale=get_loss_scale(params)) metrics_map = _get_metrics(one_hot) metrics = [metrics_map[metric] for metric in params.train.metrics] steps_per_loop = train_steps if params.train.set_epoch_loop else 1 if one_hot: loss_obj = tf.keras.losses.CategoricalCrossentropy( label_smoothing=params.model.loss.label_smoothing) else: loss_obj = tf.keras.losses.SparseCategoricalCrossentropy() model.compile( optimizer=optimizer, loss=loss_obj, metrics=metrics, steps_per_execution=steps_per_loop) initial_epoch = 0 if params.train.resume_checkpoint: initial_epoch = resume_from_checkpoint( model=model, model_dir=params.model_dir, train_steps=train_steps) callbacks = custom_callbacks.get_callbacks( model_checkpoint=params.train.callbacks.enable_checkpoint_and_export, include_tensorboard=params.train.callbacks.enable_tensorboard, time_history=params.train.callbacks.enable_time_history, track_lr=params.train.tensorboard.track_lr, write_model_weights=params.train.tensorboard.write_model_weights, initial_step=initial_epoch * train_steps, batch_size=train_builder.global_batch_size, log_steps=params.train.time_history.log_steps, model_dir=params.model_dir, backup_and_restore=params.train.callbacks.enable_backup_and_restore) serialize_config(params=params, model_dir=params.model_dir) if params.evaluation.skip_eval: validation_kwargs = {} else: validation_kwargs = { 'validation_data': validation_dataset, 'validation_steps': validation_steps, 'validation_freq': params.evaluation.epochs_between_evals, } history = model.fit( train_dataset, epochs=train_epochs, steps_per_epoch=train_steps, initial_epoch=initial_epoch, callbacks=callbacks, verbose=2, **validation_kwargs) validation_output = None if not params.evaluation.skip_eval: validation_output = model.evaluate( validation_dataset, steps=validation_steps, verbose=2) # TODO(dankondratyuk): eval and save final test accuracy stats = common.build_stats(history, validation_output, callbacks) return stats def export(params: base_configs.ExperimentConfig): """Runs the model export functionality.""" logging.info('Exporting model.') model_params = params.model.model_params.as_dict() model = get_models()[params.model.name](**model_params) checkpoint = params.export.checkpoint if checkpoint is None: logging.info('No export checkpoint was provided. Using the latest ' 'checkpoint from model_dir.') checkpoint = tf.train.latest_checkpoint(params.model_dir) model.load_weights(checkpoint) model.save(params.export.destination) def run(flags_obj: flags.FlagValues, strategy_override: tf.distribute.Strategy = None) -> Mapping[str, Any]: """Runs Image Classification model using native Keras APIs. Args: flags_obj: An object containing parsed flag values. strategy_override: A `tf.distribute.Strategy` object to use for model. Returns: Dictionary of training/eval stats """ params = _get_params_from_flags(flags_obj) if params.mode == 'train_and_eval': return train_and_eval(params, strategy_override) elif params.mode == 'export_only': export(params) else: raise ValueError('{} is not a valid mode.'.format(params.mode)) def main(_): stats = run(flags.FLAGS) if stats: logging.info('Run stats:\n%s', stats) if __name__ == '__main__': logging.set_verbosity(logging.INFO) define_classifier_flags() flags.mark_flag_as_required('data_dir') flags.mark_flag_as_required('mode') flags.mark_flag_as_required('model_type') flags.mark_flag_as_required('dataset') app.run(main)
16,195
34.362445
92
py
models
models-master/official/legacy/image_classification/mnist_main.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Runs a simple model on the MNIST dataset.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import os # Import libraries from absl import app from absl import flags from absl import logging import tensorflow as tf import tensorflow_datasets as tfds from official.common import distribute_utils from official.legacy.image_classification.resnet import common from official.utils.flags import core as flags_core from official.utils.misc import model_helpers FLAGS = flags.FLAGS def build_model(): """Constructs the ML model used to predict handwritten digits.""" image = tf.keras.layers.Input(shape=(28, 28, 1)) y = tf.keras.layers.Conv2D(filters=32, kernel_size=5, padding='same', activation='relu')(image) y = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(y) y = tf.keras.layers.Conv2D(filters=32, kernel_size=5, padding='same', activation='relu')(y) y = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(y) y = tf.keras.layers.Flatten()(y) y = tf.keras.layers.Dense(1024, activation='relu')(y) y = tf.keras.layers.Dropout(0.4)(y) probs = tf.keras.layers.Dense(10, activation='softmax')(y) model = tf.keras.models.Model(image, probs, name='mnist') return model @tfds.decode.make_decoder(output_dtype=tf.float32) def decode_image(example, feature): """Convert image to float32 and normalize from [0, 255] to [0.0, 1.0].""" return tf.cast(feature.decode_example(example), dtype=tf.float32) / 255 def run(flags_obj, datasets_override=None, strategy_override=None): """Run MNIST model training and eval loop using native Keras APIs. Args: flags_obj: An object containing parsed flag values. datasets_override: A pair of `tf.data.Dataset` objects to train the model, representing the train and test sets. strategy_override: A `tf.distribute.Strategy` object to use for model. Returns: Dictionary of training and eval stats. """ # Start TF profiler server. tf.profiler.experimental.server.start(flags_obj.profiler_port) strategy = strategy_override or distribute_utils.get_distribution_strategy( distribution_strategy=flags_obj.distribution_strategy, num_gpus=flags_obj.num_gpus, tpu_address=flags_obj.tpu) strategy_scope = distribute_utils.get_strategy_scope(strategy) mnist = tfds.builder('mnist', data_dir=flags_obj.data_dir) if flags_obj.download: mnist.download_and_prepare() mnist_train, mnist_test = datasets_override or mnist.as_dataset( split=['train', 'test'], decoders={'image': decode_image()}, # pylint: disable=no-value-for-parameter as_supervised=True) train_input_dataset = mnist_train.cache().repeat().shuffle( buffer_size=50000).batch(flags_obj.batch_size) eval_input_dataset = mnist_test.cache().repeat().batch(flags_obj.batch_size) with strategy_scope: lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay( 0.05, decay_steps=100000, decay_rate=0.96) optimizer = tf.keras.optimizers.SGD(learning_rate=lr_schedule) model = build_model() model.compile( optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy']) num_train_examples = mnist.info.splits['train'].num_examples train_steps = num_train_examples // flags_obj.batch_size train_epochs = flags_obj.train_epochs ckpt_full_path = os.path.join(flags_obj.model_dir, 'model.ckpt-{epoch:04d}') callbacks = [ tf.keras.callbacks.ModelCheckpoint( ckpt_full_path, save_weights_only=True), tf.keras.callbacks.TensorBoard(log_dir=flags_obj.model_dir), ] num_eval_examples = mnist.info.splits['test'].num_examples num_eval_steps = num_eval_examples // flags_obj.batch_size history = model.fit( train_input_dataset, epochs=train_epochs, steps_per_epoch=train_steps, callbacks=callbacks, validation_steps=num_eval_steps, validation_data=eval_input_dataset, validation_freq=flags_obj.epochs_between_evals) export_path = os.path.join(flags_obj.model_dir, 'saved_model') model.save(export_path, include_optimizer=False) eval_output = model.evaluate( eval_input_dataset, steps=num_eval_steps, verbose=2) stats = common.build_stats(history, eval_output, callbacks) return stats def define_mnist_flags(): """Define command line flags for MNIST model.""" flags_core.define_base( clean=True, num_gpu=True, train_epochs=True, epochs_between_evals=True, distribution_strategy=True) flags_core.define_device() flags_core.define_distribution() flags.DEFINE_bool('download', True, 'Whether to download data to `--data_dir`.') flags.DEFINE_integer('profiler_port', 9012, 'Port to start profiler server on.') FLAGS.set_default('batch_size', 1024) def main(_): model_helpers.apply_clean(FLAGS) stats = run(flags.FLAGS) logging.info('Run stats:\n%s', stats) if __name__ == '__main__': logging.set_verbosity(logging.INFO) define_mnist_flags() app.run(main)
6,114
33.548023
83
py
models
models-master/official/legacy/image_classification/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
609
39.666667
74
py
models
models-master/official/legacy/image_classification/learning_rate.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Learning rate utilities for vision tasks.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from typing import Any, Mapping, Optional import numpy as np import tensorflow as tf BASE_LEARNING_RATE = 0.1 class WarmupDecaySchedule(tf.keras.optimizers.schedules.LearningRateSchedule): """A wrapper for LearningRateSchedule that includes warmup steps.""" def __init__(self, lr_schedule: tf.keras.optimizers.schedules.LearningRateSchedule, warmup_steps: int, warmup_lr: Optional[float] = None): """Add warmup decay to a learning rate schedule. Args: lr_schedule: base learning rate scheduler warmup_steps: number of warmup steps warmup_lr: an optional field for the final warmup learning rate. This should be provided if the base `lr_schedule` does not contain this field. """ super(WarmupDecaySchedule, self).__init__() self._lr_schedule = lr_schedule self._warmup_steps = warmup_steps self._warmup_lr = warmup_lr def __call__(self, step: int): lr = self._lr_schedule(step) if self._warmup_steps: if self._warmup_lr is not None: initial_learning_rate = tf.convert_to_tensor( self._warmup_lr, name="initial_learning_rate") else: initial_learning_rate = tf.convert_to_tensor( self._lr_schedule.initial_learning_rate, name="initial_learning_rate") dtype = initial_learning_rate.dtype global_step_recomp = tf.cast(step, dtype) warmup_steps = tf.cast(self._warmup_steps, dtype) warmup_lr = initial_learning_rate * global_step_recomp / warmup_steps lr = tf.cond(global_step_recomp < warmup_steps, lambda: warmup_lr, lambda: lr) return lr def get_config(self) -> Mapping[str, Any]: config = self._lr_schedule.get_config() config.update({ "warmup_steps": self._warmup_steps, "warmup_lr": self._warmup_lr, }) return config class CosineDecayWithWarmup(tf.keras.optimizers.schedules.LearningRateSchedule): """Class to generate learning rate tensor.""" def __init__(self, batch_size: int, total_steps: int, warmup_steps: int): """Creates the cosine learning rate tensor with linear warmup. Args: batch_size: The training batch size used in the experiment. total_steps: Total training steps. warmup_steps: Steps for the warm up period. """ super(CosineDecayWithWarmup, self).__init__() base_lr_batch_size = 256 self._total_steps = total_steps self._init_learning_rate = BASE_LEARNING_RATE * batch_size / base_lr_batch_size self._warmup_steps = warmup_steps def __call__(self, global_step: int): global_step = tf.cast(global_step, dtype=tf.float32) warmup_steps = self._warmup_steps init_lr = self._init_learning_rate total_steps = self._total_steps linear_warmup = global_step / warmup_steps * init_lr cosine_learning_rate = init_lr * (tf.cos(np.pi * (global_step - warmup_steps) / (total_steps - warmup_steps)) + 1.0) / 2.0 learning_rate = tf.where(global_step < warmup_steps, linear_warmup, cosine_learning_rate) return learning_rate def get_config(self): return { "total_steps": self._total_steps, "warmup_learning_rate": self._warmup_learning_rate, "warmup_steps": self._warmup_steps, "init_learning_rate": self._init_learning_rate, }
4,272
35.521368
83
py
models
models-master/official/legacy/image_classification/test_utils.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Test utilities for image classification tasks.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf def trivial_model(num_classes): """Trivial model for ImageNet dataset.""" input_shape = (224, 224, 3) img_input = tf.keras.layers.Input(shape=input_shape) x = tf.keras.layers.Lambda( lambda x: tf.keras.backend.reshape(x, [-1, 224 * 224 * 3]), name='reshape')(img_input) x = tf.keras.layers.Dense(1, name='fc1')(x) x = tf.keras.layers.Dense(num_classes, name='fc1000')(x) x = tf.keras.layers.Activation('softmax', dtype='float32')(x) return tf.keras.models.Model(img_input, x, name='trivial')
1,322
33.815789
74
py
models
models-master/official/legacy/image_classification/vgg/vgg_model.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """VGG16 model for Keras. Adapted from tf.keras.applications.vgg16.VGG16(). Related papers/blogs: - https://arxiv.org/abs/1409.1556 """ import tensorflow as tf layers = tf.keras.layers def _gen_l2_regularizer(use_l2_regularizer=True, l2_weight_decay=1e-4): return tf.keras.regularizers.L2( l2_weight_decay) if use_l2_regularizer else None def vgg16(num_classes, batch_size=None, use_l2_regularizer=True, batch_norm_decay=0.9, batch_norm_epsilon=1e-5): """Instantiates the VGG16 architecture. Args: num_classes: `int` number of classes for image classification. batch_size: Size of the batches for each step. use_l2_regularizer: whether to use L2 regularizer on Conv/Dense layer. batch_norm_decay: Moment of batch norm layers. batch_norm_epsilon: Epsilon of batch borm layers. Returns: A Keras model instance. """ input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape, batch_size=batch_size) x = img_input if tf.keras.backend.image_data_format() == 'channels_first': x = layers.Permute((3, 1, 2))(x) bn_axis = 1 else: # channels_last bn_axis = 3 # Block 1 x = layers.Conv2D( 64, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block1_conv1')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv1')( x) x = layers.Activation('relu')(x) x = layers.Conv2D( 64, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block1_conv2')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv2')( x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = layers.Conv2D( 128, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block2_conv1')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv3')( x) x = layers.Activation('relu')(x) x = layers.Conv2D( 128, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block2_conv2')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv4')( x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = layers.Conv2D( 256, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block3_conv1')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv5')( x) x = layers.Activation('relu')(x) x = layers.Conv2D( 256, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block3_conv2')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv6')( x) x = layers.Activation('relu')(x) x = layers.Conv2D( 256, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block3_conv3')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv7')( x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = layers.Conv2D( 512, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block4_conv1')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv8')( x) x = layers.Activation('relu')(x) x = layers.Conv2D( 512, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block4_conv2')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv9')( x) x = layers.Activation('relu')(x) x = layers.Conv2D( 512, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block4_conv3')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv10')( x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = layers.Conv2D( 512, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block5_conv1')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv11')( x) x = layers.Activation('relu')(x) x = layers.Conv2D( 512, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block5_conv2')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv12')( x) x = layers.Activation('relu')(x) x = layers.Conv2D( 512, (3, 3), padding='same', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='block5_conv3')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv13')( x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) x = layers.Flatten(name='flatten')(x) x = layers.Dense( 4096, kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='fc1')( x) x = layers.Activation('relu')(x) x = layers.Dropout(0.5)(x) x = layers.Dense( 4096, kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='fc2')( x) x = layers.Activation('relu')(x) x = layers.Dropout(0.5)(x) x = layers.Dense( num_classes, kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='fc1000')( x) x = layers.Activation('softmax', dtype='float32')(x) # Create model. return tf.keras.Model(img_input, x, name='vgg16')
7,607
27.177778
74
py
models
models-master/official/legacy/image_classification/vgg/vgg_config.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Configuration definitions for VGG losses, learning rates, and optimizers.""" import dataclasses from official.legacy.image_classification.configs import base_configs from official.modeling.hyperparams import base_config @dataclasses.dataclass class VGGModelConfig(base_configs.ModelConfig): """Configuration for the VGG model.""" name: str = 'VGG' num_classes: int = 1000 model_params: base_config.Config = dataclasses.field(default_factory=lambda: { # pylint:disable=g-long-lambda 'num_classes': 1000, 'batch_size': None, 'use_l2_regularizer': True }) loss: base_configs.LossConfig = dataclasses.field( default_factory=lambda: base_configs.LossConfig( # pylint: disable=g-long-lambda name='sparse_categorical_crossentropy' ) ) optimizer: base_configs.OptimizerConfig = dataclasses.field( default_factory=lambda: base_configs.OptimizerConfig( # pylint: disable=g-long-lambda name='momentum', epsilon=0.001, momentum=0.9, moving_average_decay=None, ) ) learning_rate: base_configs.LearningRateConfig = dataclasses.field( default_factory=lambda: base_configs.LearningRateConfig( # pylint: disable=g-long-lambda name='stepwise', initial_lr=0.01, examples_per_epoch=1281167, boundaries=[30, 60], warmup_epochs=0, scale_by_batch_size=1.0 / 256.0, multipliers=[0.01 / 256, 0.001 / 256, 0.0001 / 256], ) )
2,115
36.785714
113
py
models
models-master/official/legacy/image_classification/vgg/__init__.py
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
610
37.1875
74
py