ptrendx commented on a change in pull request #14173: [WIP] MXNet AMP (automatic mixed precision) URL: https://github.com/apache/incubator-mxnet/pull/14173#discussion_r282240986
########## File path: src/operator/tensor/amp_cast.h ########## @@ -0,0 +1,165 @@ +/* + * 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. + */ + +/*! + * \file amp_cast.h + * \brief Function definition of casts used by AMP + */ + +#ifndef MXNET_OPERATOR_TENSOR_AMP_CAST_H_ +#define MXNET_OPERATOR_TENSOR_AMP_CAST_H_ + +#include <vector> +#include <utility> +#include <algorithm> +#include "../mshadow_op.h" +#include "../mxnet_op.h" +#include "../elemwise_op_common.h" +#include "../operator_common.h" + +namespace mxnet { +namespace op { + +struct AMPCastParam : public dmlc::Parameter<AMPCastParam> { + // use int for enumeration + int dtype; + DMLC_DECLARE_PARAMETER(AMPCastParam) { + DMLC_DECLARE_FIELD(dtype) + MXNET_ADD_ALL_TYPES + .describe("Output data type."); + } +}; + +struct AMPMultiCastParam : public dmlc::Parameter<AMPMultiCastParam> { + int num_outputs; + + DMLC_DECLARE_PARAMETER(AMPMultiCastParam) { + DMLC_DECLARE_FIELD(num_outputs) + .describe("Number of input/output pairs to be casted to the widest type."); + } +}; + +inline bool AMPCastType(const nnvm::NodeAttrs& attrs, + std::vector<int> *in_attrs, + std::vector<int> *out_attrs) { + using mshadow::kFloat32; + using mshadow::kFloat16; + const AMPCastParam& param = nnvm::get<AMPCastParam>(attrs.parsed); + CHECK_EQ(in_attrs->size(), 1U); + CHECK_EQ(out_attrs->size(), 1U); + if ((*in_attrs)[0] == kFloat32 || (*in_attrs)[0] == kFloat16) { + TYPE_ASSIGN_CHECK(*out_attrs, 0, param.dtype); + } else { + TYPE_ASSIGN_CHECK(*out_attrs, 0, (*in_attrs)[0]); + } + return (*in_attrs)[0] != -1; +} + +inline bool AMPMultiCastType(const nnvm::NodeAttrs& attrs, + std::vector<int> *in_attrs, + std::vector<int> *out_attrs) { + using mshadow::kFloat32; + using mshadow::kFloat16; Review comment: That is not really possible though since you need to know what is the relationship between those types (which one is wider). ---------------------------------------------------------------- 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
