AndrewZhaoLuo commented on a change in pull request #8069: URL: https://github.com/apache/tvm/pull/8069#discussion_r652184550
########## File path: src/relay/transforms/to_mixed_precision.cc ########## @@ -0,0 +1,409 @@ +/* + * 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 to_mixed_precision.cc + * \brief Automatic mixed floating point precision for relay graphs. i.e. turn a graph into fp16. + * + */ + +#include <tvm/ir/attrs.h> +#include <tvm/relay/expr_functor.h> +#include <tvm/relay/transform.h> +#include <tvm/runtime/object.h> + +#include <utility> + +#include "pattern_utils.h" + +namespace tvm { +namespace relay { + +// A callable which hashes std::pair +struct pair_hash { + template <class T1, class T2> + std::size_t operator()(const std::pair<T1, T2>& pair) const { + auto h1 = std::hash<T1>()(pair.first); + auto h2 = std::hash<T2>()(pair.second); + + // Use boost's combine_hash strategy + return h1 ^ (h1 + 0x9e3779b9 + (h2 << 6) + (h2 >> 2)); + } +}; + +// MIXED_PRECISION_ALWAYS ops should always be done in lower precision due to the speed and memory +// savings. MIXED_PRECISION_FOLLOW ops can be done in lower precision but don't have speedups to +// justify a cast. MIXED_PRECISION_NEVER colored ops should not be done in lower precision due to +// numerical reasons. +enum MixedTypeConversionCategory : int { + MIXED_PRECISION_ALWAYS = 0, + MIXED_PRECISION_FOLLOW = 1, + MIXED_PRECISION_NEVER = 2 +}; + +// A map of a parent node and a wanted dtype to existing nodes casted to the wanted dtype +using CachedCastNodes = std::unordered_map<std::pair<const ExprNode*, DataType>, Expr, pair_hash>; + +// Return array is of type : [MixedTypeConversionCategory (int), String, String] +// The fields are : [ConversionCategory, accumulation_datatype, output_datatype] +// Call is a call node, DataType is the mixed precision type +using FTVMMixedPrecisionConversionType = runtime::TypedPackedFunc<Array<ObjectRef>( + const Call& call_node, const std::string& target_dtype_str)>; + +class MixedPrecisionPass : public MixedModeMutator { + private: + CachedCastNodes cast_nodes_cache; Review comment: done -- 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]
