anirudh2290 commented on a change in pull request #14173: [WIP] MXNet AMP (automatic mixed precision) URL: https://github.com/apache/incubator-mxnet/pull/14173#discussion_r284920488
########## File path: python/mxnet/contrib/amp/amp.py ########## @@ -0,0 +1,342 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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 +"""Functions for enabling AMP (automatic mixed precision).""" +__all__ = ['init', 'init_trainer', 'scale_loss', 'unscale'] + +from types import MethodType +import logging +import contextlib +import numpy as np + +from ... import symbol +from ...symbol import Symbol +from ...symbol import contrib as symbol_contrib +from ... import ndarray +from ...ndarray import NDArray +from . import lists +from ...gluon import trainer +from ... import base +from ... import optimizer as opt +from .loss_scaler import LossScaler + +def _cast_symbol_NDArray(s, dtype): + float_types = (np.float16, np.float32) + if isinstance(s, Symbol): + return symbol.amp_cast(s, dtype=dtype) + elif isinstance(s, NDArray): + if (s.dtype != dtype and + s.dtype in float_types and + s.context.device_type != 'cpu'): + return ndarray.amp_cast(s, dtype=dtype) + else: + return s + else: + return s + +def _get_fun_to_wrap(name, module, submodule_dict): + module_internal = getattr(module, "_internal") + prefix = base._get_op_name_prefix(name) + if len(prefix) > 0: + if prefix != '_random_' or name.endswith('_like'): + func_name = name[len(prefix):] + cur_module = submodule_dict[prefix] + else: + func_name = name + cur_module = module_internal + elif name.startswith('_'): + func_name = name + cur_module = module_internal + else: + func_name = name + cur_module = module + return func_name, cur_module + +def _wrap_symbol_functions(module, target_dtype, target_precision_ops=None, + conditional_fp32_ops=None, fp32_ops=None): + def _ndarray_wrapper(f, target_dtype, cond_arg=None): + def _new_fun(*args, **kwargs): + if cond_arg is not None: + if (cond_arg[0] not in kwargs or + kwargs[cond_arg[0]] not in cond_arg[1]): + return f(*args, **kwargs) + new_args = list(map(lambda x: _cast_symbol_NDArray(x, target_dtype), args)) + args = tuple(new_args) + kwargs = {k: _cast_symbol_NDArray(v, target_dtype) for k, v in kwargs.items()} + return f(*args, **kwargs) + _new_fun.__name__ = f.__name__ + _new_fun.__module__ = f.__module__ + _new_fun.__doc__ = f.__doc__ + return _new_fun + + def _symbol_wrapper(f, target_dtype, cond_arg=None): + def _new_fun(*args, **kwargs): + if cond_arg is not None: + if (cond_arg[0] not in kwargs or + kwargs[cond_arg[0]] not in cond_arg[1]): + return f(*args, **kwargs) + sym = f(*args, **kwargs) + inputs = sym.get_children() + aux = sym.list_auxiliary_states() + inputs = list(map(lambda x: _cast_symbol_NDArray(x, target_dtype) + if x.name not in aux else x, inputs)) + atomic_sym = sym._gen_atomic_symbol() Review comment: when pretrained models are loaded under amp.init will it silently fail ? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
