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
17 changes: 0 additions & 17 deletions .agents/skills/devtools/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
<!--- 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. -->

---
name: devtools
description: Developer reference for Apache TVM-FFI.
Expand Down
17 changes: 17 additions & 0 deletions include/tvm/ffi/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,23 @@ struct AnyEqual {
}
}
};

// Placed near the end because this specialization depends on error handling.
template <>
struct TypeTraits<uint64_t> : public TypeTraitsIntBase<uint64_t> {
TVM_FFI_INLINE static void CopyToAnyView(const uint64_t& src, TVMFFIAny* result) {
if (src > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
TVM_FFI_THROW(OverflowError)
<< "Integer value " << src << " is too large to fit in int64_t. "
<< "Consider explicitly casting to int64_t first if this is intentional.";
}
TypeTraitsIntBase<uint64_t>::CopyInt64ToAnyView(static_cast<int64_t>(src), result);
}

TVM_FFI_INLINE static void MoveToAny(uint64_t src, TVMFFIAny* result) {
CopyToAnyView(src, result);
}
};
} // namespace ffi

// Expose to the tvm namespace for usability
Expand Down
14 changes: 0 additions & 14 deletions include/tvm/ffi/container/container_details.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,6 @@ inline constexpr bool all_storage_enabled_v = (storage_enabled_v<T> && ...);
*/
template <typename... T>
inline constexpr bool all_object_ref_v = (std::is_base_of_v<ObjectRef, T> && ...);
/**
* \brief Check if Any storage of Derived can always be directly used as Base.
*
* \tparam Base The base type.
* \tparam Derived The derived type.
* \return True if Derived's storage can be used as Base's storage, false otherwise.
*/
template <typename Base, typename Derived>
inline constexpr bool type_contains_v =
std::is_base_of_v<Base, Derived> || std::is_same_v<Base, Derived>;
// special case for Any
template <typename Derived>
inline constexpr bool type_contains_v<Any, Derived> = true;

/*!
* \brief Create a string of the container type.
* \tparam V The types of the elements in the container.
Expand Down
44 changes: 44 additions & 0 deletions include/tvm/ffi/container/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,50 @@ inline size_t GetDataSize(const TensorView& tensor) {
return GetDataSize(tensor.numel(), tensor.dtype());
}

// DLTensor* support lives with Tensor/TensorView because the conversion path
// is tensor-specific and can reuse the FFI error layer.
template <>
struct TypeTraits<DLTensor*> : public TypeTraitsBase {
static constexpr bool storage_enabled = false;
static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIDLTensorPtr;

TVM_FFI_INLINE static void CopyToAnyView(DLTensor* src, TVMFFIAny* result) {
TVM_FFI_ICHECK_NOTNULL(src);
result->type_index = TypeIndex::kTVMFFIDLTensorPtr;
result->zero_padding = 0;
TVM_FFI_CLEAR_PTR_PADDING_IN_FFI_ANY(result);
result->v_ptr = src;
}

TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
return src->type_index == TypeIndex::kTVMFFIDLTensorPtr;
}

TVM_FFI_INLINE static DLTensor* CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
TVM_FFI_UNSAFE_ASSUME(src->type_index == TypeIndex::kTVMFFIDLTensorPtr);
return static_cast<DLTensor*>(src->v_ptr);
}

TVM_FFI_INLINE static void MoveToAny(DLTensor*, TVMFFIAny*) {
TVM_FFI_THROW(RuntimeError)
<< "DLTensor* cannot be held in Any as it does not retain ownership, use Tensor instead";
}

TVM_FFI_INLINE static std::optional<DLTensor*> TryCastFromAnyView(const TVMFFIAny* src) {
if (src->type_index == TypeIndex::kTVMFFIDLTensorPtr) {
return static_cast<DLTensor*>(src->v_ptr);
} else if (src->type_index == TypeIndex::kTVMFFITensor) {
return TVMFFITensorGetDLTensorPtr(src->v_obj);
}
return std::nullopt;
}

TVM_FFI_INLINE static std::string TypeStr() { return StaticTypeKey::kTVMFFIDLTensorPtr; }
TVM_FFI_INLINE static std::string TypeSchema() {
return R"({"type":")" + std::string(StaticTypeKey::kTVMFFIDLTensorPtr) + R"("})";
}
};

// TensorView type, allow implicit casting from DLTensor*
// NOTE: we deliberately do not support MoveToAny and MoveFromAny since it does not retain ownership
template <>
Expand Down
Loading
Loading