csullivan commented on a change in pull request #9313: URL: https://github.com/apache/tvm/pull/9313#discussion_r735858799
########## File path: include/tvm/target/compilation_config.h ########## @@ -0,0 +1,150 @@ +/* + * 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 tvm/target/compilation_config.h + * \brief A helper class to collect all the targets in canonical form necessary for compilation. + * CAUTION: Preliminary, currently only used to support device planning, very likely to change. + */ + +#ifndef TVM_TARGET_COMPILATION_CONFIG_H_ +#define TVM_TARGET_COMPILATION_CONFIG_H_ + +#include <tvm/target/se_scope.h> + +namespace tvm { + +/*! + * \brief Gathers the \p Targets and \p SEScopes in canonical form needed to compile a Relay module. + * + * CAUTION: This is a temporary class to help us bridge legacy and new target/device handling Review comment: >This is a temporary class Would be useful to add a sentence on what what we expect this to be replaced by if it is temporary. I didn't get it from the rest of the description. ########## File path: include/tvm/target/compilation_config.h ########## @@ -0,0 +1,150 @@ +/* + * 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 tvm/target/compilation_config.h + * \brief A helper class to collect all the targets in canonical form necessary for compilation. + * CAUTION: Preliminary, currently only used to support device planning, very likely to change. + */ + +#ifndef TVM_TARGET_COMPILATION_CONFIG_H_ +#define TVM_TARGET_COMPILATION_CONFIG_H_ + +#include <tvm/target/se_scope.h> + +namespace tvm { + +/*! + * \brief Gathers the \p Targets and \p SEScopes in canonical form needed to compile a Relay module. + * + * CAUTION: This is a temporary class to help us bridge legacy and new target/device handling + * and reduce code duplication and inconsistencies between the VM (relay/backend/vm/compile.cc), + * Graph/AOT (relay/backend/build_module.cc) and Interpreter (relay/backend/interpreter.cc) + * compilation flows. Preliminary and subject to change. + */ +class CompilationConfigNode : public Object { + public: + /*! + * \brief The legacy targets map, mapping device type to \p Targets. Does not include any + * entry for the host target. Intended to give a unique \p Target for every \p DLDeviceType, + * though we want to get rid of that limitation. + * + * CAUTION: Since keys are \p Integers they are compared by object equality not integer + * value. + * + * TODO(mbs): Remove once codegen updated for new target conventions. + */ + TargetMap legacy_target_map; + + /*! + * \brief The host target. Used for 'scalar' data and code (such as shapes and shape + * functions) and residual Relay expressions and data (such as conditionals and ADTs). + */ + Target host_target; + + /*! + * \brief Vector of all available targets for primitive operators. May contain a \p Target + * for the same device type as for the \p host_target, however the \p host_target should + * be preferred for all host computations and data. + */ + Array<Target> primitive_targets; + + /*! + * \brief \p SEScope for primitive operators which are not otherwise constrained to a particular + * device. + */ + SEScope default_primitive_se_scope = SEScope::FullyUnconstrained(); + + /*! \brief SEScope for the host. */ + SEScope host_se_scope = SEScope::FullyUnconstrained(); + + /*! + * \brief If defined then in 'homogenous execution mode' and all primitives will be compiled Review comment: >* \brief If defined then <run/compile> in 'homogenous execution mode' and all primitives will be compiled ########## File path: include/tvm/target/se_scope.h ########## @@ -0,0 +1,317 @@ +/* + * 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 tvm/target/se_scope.h + * \brief A compile time representation for a Storage or Execution Scope. + */ + +#ifndef TVM_TARGET_SE_SCOPE_H_ +#define TVM_TARGET_SE_SCOPE_H_ + +#include <tvm/ir/transform.h> +#include <tvm/target/target.h> + +#include <string> +#include <unordered_map> +#include <utility> + +namespace tvm { + +/*! + * \brief Describes at compile time where data is to be stored down to the device and memory + * scope level, or where execution is to take place, down to the device level. It is a quadruple of: + * - A \p device_type (\p DLDeviceType). + * - An uninterpreted \p virtual_device_id (\p int) distinguishing the intended device from all + * other devices (either of the same \p device_type, or across all availabel devices in the + * system). The \p virtual_device_id may be left as 0 if not significant. It is up to downstream + * compilation passes and/or the runtime to map a \p virtual_device_id to an actual physical + * device if required. In particular the \p virtual_device_id need not correspond exactly to + * any runtime \p Device's \p device_id. Review comment: >In particular the \p virtual_device_id need not correspond exactly to any runtime \p Device's \p device_id. In the case in which the virtual_dev_id doesn't correspond exactly to any runtime device's dev_id, then what does it correspond it? ########## File path: include/tvm/target/compilation_config.h ########## @@ -0,0 +1,150 @@ +/* + * 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 tvm/target/compilation_config.h + * \brief A helper class to collect all the targets in canonical form necessary for compilation. + * CAUTION: Preliminary, currently only used to support device planning, very likely to change. + */ + +#ifndef TVM_TARGET_COMPILATION_CONFIG_H_ +#define TVM_TARGET_COMPILATION_CONFIG_H_ + +#include <tvm/target/se_scope.h> + +namespace tvm { + +/*! + * \brief Gathers the \p Targets and \p SEScopes in canonical form needed to compile a Relay module. + * + * CAUTION: This is a temporary class to help us bridge legacy and new target/device handling + * and reduce code duplication and inconsistencies between the VM (relay/backend/vm/compile.cc), + * Graph/AOT (relay/backend/build_module.cc) and Interpreter (relay/backend/interpreter.cc) + * compilation flows. Preliminary and subject to change. + */ +class CompilationConfigNode : public Object { + public: + /*! + * \brief The legacy targets map, mapping device type to \p Targets. Does not include any + * entry for the host target. Intended to give a unique \p Target for every \p DLDeviceType, + * though we want to get rid of that limitation. + * + * CAUTION: Since keys are \p Integers they are compared by object equality not integer + * value. + * + * TODO(mbs): Remove once codegen updated for new target conventions. + */ + TargetMap legacy_target_map; + + /*! + * \brief The host target. Used for 'scalar' data and code (such as shapes and shape + * functions) and residual Relay expressions and data (such as conditionals and ADTs). + */ + Target host_target; + + /*! + * \brief Vector of all available targets for primitive operators. May contain a \p Target + * for the same device type as for the \p host_target, however the \p host_target should + * be preferred for all host computations and data. + */ + Array<Target> primitive_targets; + + /*! + * \brief \p SEScope for primitive operators which are not otherwise constrained to a particular + * device. + */ + SEScope default_primitive_se_scope = SEScope::FullyUnconstrained(); + + /*! \brief SEScope for the host. */ + SEScope host_se_scope = SEScope::FullyUnconstrained(); + + /*! + * \brief If defined then in 'homogenous execution mode' and all primitives will be compiled + * for this target. This is to support legacy passes which have not been adapted to hetrogeneous + * execution. + * + * TODO(mbs): Remove once all passes are 'hetrogeneous aware'. + */ + Target optional_homogeneous_target; + + void VisitAttrs(AttrVisitor* v); + + /*! + * \brief Returns a \p SEScope agreeing with \p se_scope on all it's constrained fields, however: Review comment: ```suggestion * \brief Returns a \p SEScope agreeing with \p se_scope on all its constrained fields, however: ``` ########## File path: include/tvm/target/compilation_config.h ########## @@ -0,0 +1,150 @@ +/* + * 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 tvm/target/compilation_config.h + * \brief A helper class to collect all the targets in canonical form necessary for compilation. + * CAUTION: Preliminary, currently only used to support device planning, very likely to change. + */ + +#ifndef TVM_TARGET_COMPILATION_CONFIG_H_ +#define TVM_TARGET_COMPILATION_CONFIG_H_ + +#include <tvm/target/se_scope.h> + +namespace tvm { + +/*! + * \brief Gathers the \p Targets and \p SEScopes in canonical form needed to compile a Relay module. + * + * CAUTION: This is a temporary class to help us bridge legacy and new target/device handling + * and reduce code duplication and inconsistencies between the VM (relay/backend/vm/compile.cc), + * Graph/AOT (relay/backend/build_module.cc) and Interpreter (relay/backend/interpreter.cc) + * compilation flows. Preliminary and subject to change. + */ +class CompilationConfigNode : public Object { + public: + /*! + * \brief The legacy targets map, mapping device type to \p Targets. Does not include any + * entry for the host target. Intended to give a unique \p Target for every \p DLDeviceType, + * though we want to get rid of that limitation. + * + * CAUTION: Since keys are \p Integers they are compared by object equality not integer + * value. + * + * TODO(mbs): Remove once codegen updated for new target conventions. + */ + TargetMap legacy_target_map; + + /*! + * \brief The host target. Used for 'scalar' data and code (such as shapes and shape + * functions) and residual Relay expressions and data (such as conditionals and ADTs). + */ + Target host_target; + + /*! + * \brief Vector of all available targets for primitive operators. May contain a \p Target + * for the same device type as for the \p host_target, however the \p host_target should + * be preferred for all host computations and data. + */ + Array<Target> primitive_targets; + + /*! + * \brief \p SEScope for primitive operators which are not otherwise constrained to a particular + * device. + */ + SEScope default_primitive_se_scope = SEScope::FullyUnconstrained(); + + /*! \brief SEScope for the host. */ + SEScope host_se_scope = SEScope::FullyUnconstrained(); + + /*! + * \brief If defined then in 'homogenous execution mode' and all primitives will be compiled + * for this target. This is to support legacy passes which have not been adapted to hetrogeneous + * execution. + * + * TODO(mbs): Remove once all passes are 'hetrogeneous aware'. + */ + Target optional_homogeneous_target; + + void VisitAttrs(AttrVisitor* v); + + /*! + * \brief Returns a \p SEScope agreeing with \p se_scope on all it's constrained fields, however: + * - If the target is null then it is filled in from the known available primitive targets by + * matching on device type. Fails if no such target is known. + * - The returned object is unique for the field values w.r.t. all other \p SEScopes returned + * by this method. + * + * We call the result the 'canonical' \p SEScope. Two canonical \p SEScopes are structurally + * equal if and only if they are pointer equal. + */ + SEScope CanonicalSEScope(const SEScope& se_scope) const; + + static constexpr const char* _type_key = "CompilationConfig"; + TVM_DECLARE_FINAL_OBJECT_INFO(CompilationConfigNode, Object) + + private: + /*! + * \brief Establishes the default \p SEScope for primitives and the \p SEScope for the host + * given vector of available \p Targets. If necessary, add new \p Targets to match the Review comment: ```suggestion * given vector of available primitive \p Targets. If necessary, add new \p Targets to match the ``` If this is correct, I suggest this change. If not, clarity as to which targets are referred to here is needed. ########## File path: include/tvm/target/se_scope.h ########## @@ -0,0 +1,317 @@ +/* Review comment: cc @Lunderberg please take a look at this header if you have the cycles. Many relevant target<>device concepts, as well as memory scopes. ########## File path: include/tvm/target/se_scope.h ########## @@ -0,0 +1,317 @@ +/* + * 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 tvm/target/se_scope.h + * \brief A compile time representation for a Storage or Execution Scope. + */ + +#ifndef TVM_TARGET_SE_SCOPE_H_ +#define TVM_TARGET_SE_SCOPE_H_ + +#include <tvm/ir/transform.h> +#include <tvm/target/target.h> + +#include <string> +#include <unordered_map> +#include <utility> + +namespace tvm { + +/*! + * \brief Describes at compile time where data is to be stored down to the device and memory + * scope level, or where execution is to take place, down to the device level. It is a quadruple of: + * - A \p device_type (\p DLDeviceType). + * - An uninterpreted \p virtual_device_id (\p int) distinguishing the intended device from all + * other devices (either of the same \p device_type, or across all availabel devices in the + * system). The \p virtual_device_id may be left as 0 if not significant. It is up to downstream + * compilation passes and/or the runtime to map a \p virtual_device_id to an actual physical + * device if required. In particular the \p virtual_device_id need not correspond exactly to + * any runtime \p Device's \p device_id. + * - A \p target (\p Target) describing how to compile code for the intended device. The + * \p target->kind->device_type must match the above \p device_type. + * - A \p memory_scope (currently just \p String) describing which memory area is to be used to + * hold data. The area should be reachable from the device but need not be 'on' the device, + * see below. (We're using a \p String for now but would prefer a more structured representation.) + * + * All of these fields may be 'unconstrained', signaling that device planning is free to choose + * a value consistent with the whole program. However if a \p target is given then the + * \p device_type must equal \p target->kind->device_type. + * + * Note that currently we assume if a function returns its result on a particular device + * then the function body is also executed on that device. See the overview comment in + * src/relay/transforms/device_planner.cc for more details. + * + * By 'data' we include both tensors and additional supporting datastructures such as shapes, + * Relay AST items, Relay tuples, and Relay references. Typically non-tensor data must reside + * on a 'CPU'-like device with good support for scalars. + * + * By 'execution' we include both (fused) primitive operators, and all the Relay expressions + * surrounding them which coordinates data and control flow. Again, typically non-primitive + * operators must be executed on a 'CPU'-like device with good support for control flow. + * + * Targets vs Devices + * ------------------ + * Generally \p Targets (a compile-time only datastructue) describe compiler options for a specific + * microarchitecture and toolchain, while \p Devices (a runtime datastructure alsa available at Review comment: ```suggestion * microarchitecture and toolchain, while \p Devices (a runtime datastructure also available at ``` ########## File path: src/target/compilation_config.cc ########## @@ -0,0 +1,219 @@ +/* + * 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 tvm/target/compilation_config.cc + * \brief Implementation of \p CompilationConfig for collecting \p Targets. + */ + +#include <tvm/runtime/device_api.h> +#include <tvm/target/compilation_config.h> + +namespace tvm { + +TVM_REGISTER_NODE_TYPE(CompilationConfigNode); + +void CompilationConfigNode::VisitAttrs(AttrVisitor* v) { + v->Visit("legacy_target_map", &legacy_target_map); + v->Visit("host_target", &host_target); + v->Visit("primitive_targets", &primitive_targets); + v->Visit("default_primitive_se_scope", &default_primitive_se_scope); + v->Visit("host_se_scope", &host_se_scope); + v->Visit("optional_homogenous_target", &optional_homogeneous_target); + // NOTE: The se_scope_cache_ is not accessible via FFI. +} + +SEScope CompilationConfigNode::CanonicalSEScope(const SEScope& se_scope) const { + if (se_scope->target().defined()) { + return se_scope_cache_.Unique(se_scope); + } + DLDeviceType device_type = se_scope->device_type(); + // TODO(mbs): Proper diagnostics. + CHECK(device_type != kInvalidDeviceType) + << "SEScope annotations must include at least a device_type"; + Target target = FindPrimitiveTargetOrFail(se_scope->device_type()); + return se_scope_cache_.Unique( + SEScope(device_type, se_scope->virtual_device_id(), target, se_scope->memory_scope())); +} + +/*! + * \brief Returns the default \p SEScope for primitives and the \p SEScope for the host + * given vector of available \p targets. If necessary, add new \p Targets to \p targets + * to match the required devices. + */ Review comment: nit: this is a duplicated description from the header. Prefer only one doc string to avoid them getting out of sync. ########## File path: include/tvm/target/compilation_config.h ########## @@ -0,0 +1,150 @@ +/* + * 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 tvm/target/compilation_config.h + * \brief A helper class to collect all the targets in canonical form necessary for compilation. + * CAUTION: Preliminary, currently only used to support device planning, very likely to change. + */ + +#ifndef TVM_TARGET_COMPILATION_CONFIG_H_ +#define TVM_TARGET_COMPILATION_CONFIG_H_ + +#include <tvm/target/se_scope.h> + +namespace tvm { + +/*! + * \brief Gathers the \p Targets and \p SEScopes in canonical form needed to compile a Relay module. + * + * CAUTION: This is a temporary class to help us bridge legacy and new target/device handling Review comment: Coming back to this comment after I read through the rest of the class definition it makes more sense. It could still be nice to prep the reader that each of these public members will slowly be removed, at which point `CompilationConfigNode` will be defunct. ########## File path: include/tvm/target/se_scope.h ########## @@ -0,0 +1,317 @@ +/* + * 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 tvm/target/se_scope.h + * \brief A compile time representation for a Storage or Execution Scope. + */ + +#ifndef TVM_TARGET_SE_SCOPE_H_ +#define TVM_TARGET_SE_SCOPE_H_ + +#include <tvm/ir/transform.h> +#include <tvm/target/target.h> + +#include <string> +#include <unordered_map> +#include <utility> + +namespace tvm { + +/*! + * \brief Describes at compile time where data is to be stored down to the device and memory + * scope level, or where execution is to take place, down to the device level. It is a quadruple of: + * - A \p device_type (\p DLDeviceType). + * - An uninterpreted \p virtual_device_id (\p int) distinguishing the intended device from all + * other devices (either of the same \p device_type, or across all availabel devices in the Review comment: ```suggestion * other devices (either of the same \p device_type, or across all available devices in the ``` -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
