jroesch commented on a change in pull request #5039: [Relay] GradientCell Relay Pass URL: https://github.com/apache/incubator-tvm/pull/5039#discussion_r396822885
########## File path: src/relay/transforms/gradient_cell.cc ########## @@ -0,0 +1,304 @@ +/* + * 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 gradient_cell.cc + * + * \brief Convert all tensors to a Gradient Cell + * + * This pass delays or removes memory allocation by converting tensors into + * GradCell, an algebraic data type defined in gradient.rly + * + * This will delay or decrease memory usage. All calls to + * ones, ones_like, zeros, zeros_like will call the One or Zero constructor + * of GradCell, which will not instantiate in memory until needed. All other cases result + * in using the Raw constructor which means the tensor is instantiated in memory. + * + * It also overloads + and * operation which can increase performance when doing + * operations involving tensors with values of only 0 or 1. + * + * Note: this pass can only be used with functions where the input/output types are + * a combination of TupleTypes and TensorTypes + * + * This pass optimizes 6 ops: + * - add + * - multiply + * - ones + * - ones_like + * - zeros + * - zeros_like + * + * This pass makes use of three visitor. The most important one visits the entire function, + * one is used for wrap inputs and one to unwrap outputs. + * + * For example: + * fn: TensorType[(10,10), float32] -> TensorType[(10,10), float32] + * + * After this pass + * fn: GradCell[TensorType[(10,10), float32]] -> GradCell[TensorType[(10,10), float32]] + * + * Thus, it is necessary to wrap this outer function so that the input/output types remain the same + */ + +#include <tvm/relay/analysis.h> +#include <tvm/relay/expr_functor.h> +#include <tvm/ir/type_functor.h> +#include <tvm/relay/transform.h> +#include "let_list.h" + +namespace tvm { +namespace relay { + +/*! +* \brief Visitor to wrap inputs Review comment: Add a better comment here. ---------------------------------------------------------------- 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
