Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/benchmark/gpu_imagenet_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def benchmark(network, target):
else:
networks = [args.network]

target = tvm.target.create('%s -device=%s -model=%s' % (args.target, args.device, args.model))
target = tvm.target.Target('%s -device=%s -model=%s' % (args.target, args.device, args.model))

print("--------------------------------------------------")
print("%-20s %-20s" % ("Network Name", "Mean Inference Time (std dev)"))
Expand Down
2 changes: 1 addition & 1 deletion apps/topi_recipe/conv/test_conv_int8_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def run_inference(data_dtype, kernel_dtype, out_dtype, im_height, im_width, in_f
c_sch = tvm.nd.array(np.zeros(o_shape, dtype=out_dtype), CTX)


with tvm.target.create(TARGET_NAME):
with tvm.target.Target(TARGET_NAME):
if out_dtype == "float32":
conv = topi.nn.conv2d_NCHWc(data, kernel, stride=hstride,
padding=hpad, dilation=(1, 1),
Expand Down
2 changes: 1 addition & 1 deletion apps/topi_recipe/conv/test_conv_int8_intel.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def run_inference(data_dtype, kernel_dtype, out_dtype, im_height, im_width, in_f
c_sch = tvm.nd.array(np.zeros(o_shape, dtype=out_dtype), CTX)


with tvm.target.create(TARGET_NAME):
with tvm.target.Target(TARGET_NAME):
conv = topi.nn.conv2d_NCHWc(data, kernel, stride=hstride,
padding=hpad, dilation=(1, 1),
layout='NCHWc', out_layout='NCHWc', out_dtype=out_dtype)
Expand Down
2 changes: 1 addition & 1 deletion apps/topi_recipe/gemm/gemm_int8.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def block_size_filter(entity):
print(config)

with dispatch_context:
with tvm.target.create('cuda'):
with tvm.target.Target('cuda'):
s, arg_bufs = gemm_int8(n, m, l)
f = tvm.build(s, arg_bufs, 'cuda', name='gemm_int8')

Expand Down
155 changes: 155 additions & 0 deletions include/tvm/target/tag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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/tag.h
* \brief Target tag registry
*/
#ifndef TVM_TARGET_TAG_H_
#define TVM_TARGET_TAG_H_

#include <tvm/node/attr_registry_map.h>
#include <tvm/node/node.h>
#include <tvm/target/target.h>

#include <utility>

namespace tvm {

/*! \brief A target tag */
class TargetTagNode : public Object {
public:
/*! \brief Name of the target */
String name;
/*! \brief Config map to generate the target */
Map<String, ObjectRef> config;

void VisitAttrs(AttrVisitor* v) {
v->Visit("name", &name);
v->Visit("config", &config);
}

static constexpr const char* _type_key = "TargetTag";
TVM_DECLARE_FINAL_OBJECT_INFO(TargetTagNode, Object);

private:
/*! \brief Return the index stored in attr registry */
uint32_t AttrRegistryIndex() const { return index_; }
/*! \brief Return the name stored in attr registry */
String AttrRegistryName() const { return name; }
/*! \brief Index used for internal lookup of attribute registry */
uint32_t index_;

template <typename, typename>
friend class AttrRegistry;
template <typename>
friend class AttrRegistryMapContainerMap;
friend class TargetTagRegEntry;
};

/*!
* \brief Managed reference class to TargetTagNode
* \sa TargetTagNode
*/
class TargetTag : public ObjectRef {
public:
/*!
* \brief Retrieve the Target given it the name of target tag
* \param target_tag_name Name of the target tag
* \return The Target requested
*/
TVM_DLL static Optional<Target> Get(const String& target_tag_name);
/*!
* \brief List all names of the existing target tags
* \return A dictionary that maps tag name to the concrete target it corresponds to
*/
TVM_DLL static Map<String, Target> ListTags();
/*!
* \brief Add a tag into the registry
* \param name Name of the tag
* \param config The target config corresponding to the tag
* \param override Allow overriding existing tags
* \return Target created with the tag
*/
TVM_DLL static Target AddTag(String name, Map<String, ObjectRef> config, bool override);

TVM_DEFINE_OBJECT_REF_METHODS(TargetTag, ObjectRef, TargetTagNode);

private:
/*! \brief Mutable access to the container class */
TargetTagNode* operator->() { return static_cast<TargetTagNode*>(data_.get()); }
friend class TargetTagRegEntry;
};

class TargetTagRegEntry {
public:
/*!
* \brief Set the config dict corresponding to the target tag
* \param config The config dict for target creation
*/
inline TargetTagRegEntry& set_config(Map<String, ObjectRef> config);
/*! \brief Set name of the TargetTag to be the same as registry if it is empty */
inline TargetTagRegEntry& set_name();
/*!
* \brief Register or get a new entry.
* \param target_tag_name The name of the TargetTag.
* \return the corresponding entry.
*/
TVM_DLL static TargetTagRegEntry& RegisterOrGet(const String& target_tag_name);

private:
TargetTag tag_;
String name;

/*! \brief private constructor */
explicit TargetTagRegEntry(uint32_t reg_index) : tag_(make_object<TargetTagNode>()) {
tag_->index_ = reg_index;
}
template <typename, typename>
friend class AttrRegistry;
friend class TargetTag;
};

inline TargetTagRegEntry& TargetTagRegEntry::set_config(Map<String, ObjectRef> config) {
tag_->config = std::move(config);
return *this;
}

inline TargetTagRegEntry& TargetTagRegEntry::set_name() {
if (tag_->name.empty()) {
tag_->name = name;
}
return *this;
}

#define TVM_TARGET_TAG_REGISTER_VAR_DEF \
static DMLC_ATTRIBUTE_UNUSED ::tvm::TargetTagRegEntry& __make_##TargetTag

/*!
* \def TVM_REGISTER_TARGET_TAG
* \brief Register a new target tag, or set attribute of the corresponding target tag.
* \param TargetTagName The name of target tag
*/
#define TVM_REGISTER_TARGET_TAG(TargetTagName) \
TVM_STR_CONCAT(TVM_TARGET_TAG_REGISTER_VAR_DEF, __COUNTER__) = \
::tvm::TargetTagRegEntry::RegisterOrGet(TargetTagName).set_name()

} // namespace tvm

#endif // TVM_TARGET_TAG_H_
110 changes: 20 additions & 90 deletions include/tvm/target/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@
#ifndef TVM_TARGET_TARGET_H_
#define TVM_TARGET_TARGET_H_

#include <tvm/ir/expr.h>
#include <tvm/ir/transform.h>
#include <tvm/node/container.h>
#include <tvm/node/node.h>
#include <tvm/support/with.h>
#include <tvm/target/target_kind.h>

#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

namespace tvm {

class TargetInternal;

/*!
* \brief Compilation target.
* \note Use target::llvm, target::cuda etc functions.
* \sa Target
*/
class TargetNode : public Object {
Expand All @@ -52,7 +50,11 @@ class TargetNode : public Object {
Array<String> keys;
/*! \brief Collection of attributes */
Map<String, ObjectRef> attrs;
/*! \return the full device string to pass to codegen::Build */
/*!
* \brief The raw string representation of the target
* \return the full device string to pass to codegen::Build
* \note It will be deprecated after the Target RFC is fully landed.
*/
TVM_DLL const std::string& str() const;
/*! \return Export target to JSON-like configuration */
TVM_DLL Map<String, ObjectRef> Export() const;
Expand Down Expand Up @@ -106,27 +108,8 @@ class TargetNode : public Object {
private:
/*! \brief Internal string repr. */
mutable std::string str_repr_;
/*!
* \brief Parsing TargetNode::attrs from a list of raw strings
* \param obj The attribute to be parsed
* \param info The runtime type information for parsing
* \return The attribute parsed
*/
ObjectRef ParseAttr(const ObjectRef& obj, const TargetKindNode::ValueTypeInfo& info) const;
/*!
* \brief Parsing TargetNode::attrs from a list of raw strings
* \param options The raw string of fields to be parsed
* \return The attributes parsed
*/
Map<String, ObjectRef> ParseAttrsFromRaw(const std::vector<std::string>& options) const;
/*!
* \brief Serialize the attributes of a target to raw string
* \param attrs The attributes to be converted to string
* \return The string converted, NullOpt if attrs is empty
*/
Optional<String> StringifyAttrsToRaw(const Map<String, ObjectRef>& attrs) const;

friend class Target;
friend class TargetInternal;
};

/*!
Expand All @@ -135,31 +118,18 @@ class TargetNode : public Object {
*/
class Target : public ObjectRef {
public:
Target() {}
/*! \brief Constructor from ObjectPtr */
explicit Target(ObjectPtr<Object> n) : ObjectRef(n) {}
/*! \brief Construct a null Target */
TVM_DLL explicit Target(std::nullptr_t) { data_ = nullptr; }
/*!
* \brief Create a Target using a JSON-like configuration
* \param config The JSON-like configuration
* \return The target created
* \brief Construct a Target given a string
* \param tag_or_config_or_target_str the string to parse
*/
TVM_DLL static Target FromConfig(const Map<String, ObjectRef>& config);
TVM_DLL explicit Target(const String& tag_or_config_or_target_str);
/*!
* \brief Create a Target given a string
* \param target_str the string to parse
* \return The target created
*/
TVM_DLL static Target Create(const String& target_str);
/*!
* \brief Construct a Target node from the given name and options.
* \param name The major target name. Should be one of
* {"aocl", "aocl_sw_emu", "c", "cuda", "ext_dev", "hexagon", "hybrid", "llvm",
* "metal", "nvptx", "opencl", "rocm", "sdaccel", "stackvm", "vulkan"}
* \param options Additional options appended to the target
* \return The constructed Target
* \brief Construct a Target using a JSON-like configuration
* \param config The JSON-like configuration
*/
TVM_DLL static Target CreateTarget(const std::string& name,
const std::vector<std::string>& options);
TVM_DLL explicit Target(const Map<String, ObjectRef>& config);
/*!
* \brief Get the current target context from thread local storage.
* \param allow_not_defined If the context stack is empty and this is set to true, an
Expand All @@ -170,14 +140,11 @@ class Target : public ObjectRef {
*/
TVM_DLL static tvm::Target Current(bool allow_not_defined = true);

const TargetNode* operator->() const { return static_cast<const TargetNode*>(get()); }

using ContainerType = TargetNode;
class Internal;
TVM_DEFINE_OBJECT_REF_METHODS(Target, ObjectRef, TargetNode);

private:
// enable with syntax.
friend class Internal;
friend class TargetInternal;
friend class With<Target>;
/*!
* \brief Push a new target context onto the thread local stack.
Expand All @@ -192,42 +159,5 @@ class Target : public ObjectRef {
TVM_DLL void ExitWithScope();
};

/*! \brief This namespace provides functions to construct Target instances */
namespace target {

/*! \return A target for LLVM */
TVM_DLL Target llvm(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for CUDA */
TVM_DLL Target cuda(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for ROCm */
TVM_DLL Target rocm(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for OpenCL */
TVM_DLL Target opencl(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for Metal */
TVM_DLL Target metal(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for rasp */
TVM_DLL Target rasp(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for Mali */
TVM_DLL Target mali(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for Intel Graphics */
TVM_DLL Target intel_graphics(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for stackvm */
TVM_DLL Target stackvm(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for external device */
TVM_DLL Target ext_dev(const std::vector<std::string>& options = std::vector<std::string>());

/*! \return A target for hexagon */
TVM_DLL Target hexagon(const std::vector<std::string>& options = std::vector<std::string>());
} // namespace target

} // namespace tvm
#endif // TVM_TARGET_TARGET_H_
Loading