You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by tq...@apache.org on 2025/03/12 21:37:04 UTC
(tvm) 01/01: Remove legacy rust map/array support
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch refactor-s0
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 40afdde0ab51910b8d41cefb7af342867d4aae08
Author: tqchen <ti...@gmail.com>
AuthorDate: Wed Mar 12 17:36:53 2025 -0400
Remove legacy rust map/array support
---
rust/tvm-rt/src/array.rs | 219 -------------------------------------
rust/tvm-rt/src/lib.rs | 2 -
rust/tvm-rt/src/map.rs | 275 -----------------------------------------------
3 files changed, 496 deletions(-)
diff --git a/rust/tvm-rt/src/array.rs b/rust/tvm-rt/src/array.rs
deleted file mode 100644
index 02c34a1d13..0000000000
--- a/rust/tvm-rt/src/array.rs
+++ /dev/null
@@ -1,219 +0,0 @@
-/*
- * 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.
- */
-
-use std::convert::{TryFrom, TryInto};
-use std::iter::{FromIterator, IntoIterator, Iterator};
-use std::marker::PhantomData;
-
-use crate::errors::Error;
-use crate::object::{IsObjectRef, Object, ObjectPtr, ObjectRef};
-use crate::{
- external,
- function::{Function, Result},
- ArgValue, RetValue,
-};
-
-#[repr(C)]
-#[derive(Clone)]
-pub struct Array<T: IsObjectRef> {
- object: ObjectRef,
- _data: PhantomData<T>,
-}
-
-// TODO(@jroesch): convert to use generics instead of casting inside
-// the implementation.
-external! {
- #[name("runtime.ArrayGetItem")]
- fn array_get_item(array: ObjectRef, index: isize) -> ObjectRef;
- #[name("runtime.ArraySize")]
- fn array_size(array: ObjectRef) -> i64;
-}
-
-impl<T: IsObjectRef + 'static> IsObjectRef for Array<T> {
- type Object = Object;
- fn as_ptr(&self) -> Option<&ObjectPtr<Self::Object>> {
- self.object.as_ptr()
- }
-
- fn into_ptr(self) -> Option<ObjectPtr<Self::Object>> {
- self.object.into_ptr()
- }
-
- fn from_ptr(object_ptr: Option<ObjectPtr<Self::Object>>) -> Self {
- let object_ref = match object_ptr {
- Some(o) => o.into(),
- _ => panic!(),
- };
-
- Array {
- object: object_ref,
- _data: PhantomData,
- }
- }
-}
-
-impl<T: IsObjectRef> Array<T> {
- pub fn from_vec(data: Vec<T>) -> Result<Array<T>> {
- let iter = data.iter().map(T::into_arg_value).collect();
-
- let func = Function::get("runtime.Array").expect(
- "runtime.Array function is not registered, this is most likely a build or linking error",
- );
-
- // let array_data = func.invoke(iter)?;
- // let array_data: ObjectRef = func.invoke(iter)?.try_into()?;
- let array_data: ObjectPtr<Object> = func.invoke(iter)?.try_into()?;
-
- debug_assert!(
- array_data.count() >= 1,
- "array reference count is {}",
- array_data.count()
- );
-
- Ok(Array {
- object: array_data.into(),
- _data: PhantomData,
- })
- }
-
- pub fn get(&self, index: isize) -> Result<T>
- where
- T: TryFrom<RetValue, Error = Error>,
- {
- let oref: ObjectRef = array_get_item(self.object.clone(), index)?;
- oref.downcast()
- }
-
- pub fn len(&self) -> i64 {
- array_size(self.object.clone()).expect("size should never fail")
- }
-}
-
-impl<T: IsObjectRef> std::fmt::Debug for Array<T> {
- fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
- let as_vec: Vec<T> = self.clone().into_iter().collect();
- write!(formatter, "{:?}", as_vec)
- }
-}
-
-pub struct IntoIter<T: IsObjectRef> {
- array: Array<T>,
- pos: isize,
- size: isize,
-}
-
-impl<T: IsObjectRef> Iterator for IntoIter<T> {
- type Item = T;
-
- fn next(&mut self) -> Option<Self::Item> {
- if self.pos < self.size {
- let item =
- self.array.get(self.pos)
- .expect("Can not index as in-bounds position after bounds checking.\nNote: this error can only be do to an uncaught issue with API bindings.");
- self.pos += 1;
- Some(item)
- } else {
- None
- }
- }
-}
-
-impl<T: IsObjectRef> IntoIterator for Array<T> {
- type Item = T;
- type IntoIter = IntoIter<T>;
-
- fn into_iter(self) -> Self::IntoIter {
- let size = self.len() as isize;
- IntoIter {
- array: self,
- pos: 0,
- size: size,
- }
- }
-}
-
-impl<T: IsObjectRef> FromIterator<T> for Array<T> {
- fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
- Array::from_vec(iter.into_iter().collect()).unwrap()
- }
-}
-
-impl<'a, T: IsObjectRef> From<&'a Array<T>> for ArgValue<'a> {
- fn from(array: &'a Array<T>) -> ArgValue<'a> {
- (&array.object).into()
- }
-}
-
-impl<T: IsObjectRef> From<Array<T>> for RetValue {
- fn from(array: Array<T>) -> RetValue {
- array.object.into()
- }
-}
-
-impl<'a, T: IsObjectRef> TryFrom<ArgValue<'a>> for Array<T> {
- type Error = Error;
-
- fn try_from(array: ArgValue<'a>) -> Result<Array<T>> {
- let object_ref: ObjectRef = array.try_into()?;
- // TODO: type check
- Ok(Array {
- object: object_ref,
- _data: PhantomData,
- })
- }
-}
-
-impl<'a, T: IsObjectRef> TryFrom<RetValue> for Array<T> {
- type Error = Error;
-
- fn try_from(array: RetValue) -> Result<Array<T>> {
- let object_ref = array.try_into()?;
- Ok(Array {
- object: object_ref,
- _data: PhantomData,
- })
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::Array;
- use crate::function::Result;
- use crate::object::{IsObjectRef, ObjectRef};
- use crate::string::String;
-
- #[test]
- fn create_array_and_get() -> Result<()> {
- let vec: Vec<String> = vec!["foo".into(), "bar".into(), "baz".into()];
- let array = Array::from_vec(vec)?;
- assert_eq!(array.get(0)?.to_string(), "foo");
- assert_eq!(array.get(1)?.to_string(), "bar");
- assert_eq!(array.get(2)?.to_string(), "baz");
- Ok(())
- }
-
- #[test]
- fn downcast() -> Result<()> {
- let vec: Vec<String> = vec!["foo".into(), "bar".into(), "baz".into()];
- let array: ObjectRef = ObjectRef::from_ptr(Array::from_vec(vec)?.into_ptr());
- let array: Array<ObjectRef> = array.downcast::<Array<ObjectRef>>().unwrap();
- assert_eq!(array.get(1)?.downcast::<String>().unwrap(), "bar");
- Ok(())
- }
-}
diff --git a/rust/tvm-rt/src/lib.rs b/rust/tvm-rt/src/lib.rs
index 0e9936318d..921117abae 100644
--- a/rust/tvm-rt/src/lib.rs
+++ b/rust/tvm-rt/src/lib.rs
@@ -49,11 +49,9 @@ macro_rules! check_call {
}
// Define all sumodules.
-pub mod array;
pub mod device;
pub mod errors;
pub mod function;
-pub mod map;
pub mod module;
pub mod ndarray;
pub mod object;
diff --git a/rust/tvm-rt/src/map.rs b/rust/tvm-rt/src/map.rs
deleted file mode 100644
index 5594a91dc0..0000000000
--- a/rust/tvm-rt/src/map.rs
+++ /dev/null
@@ -1,275 +0,0 @@
-/*
- * 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.
- */
-
-use std::collections::HashMap;
-use std::convert::{TryFrom, TryInto};
-use std::iter::FromIterator;
-use std::marker::PhantomData;
-
-use crate::object::debug_print;
-
-use crate::array::Array;
-use crate::errors::Error;
-use crate::object::{IsObjectRef, Object, ObjectPtr, ObjectRef};
-use crate::ArgValue;
-use crate::{
- external,
- function::{Function, Result},
- RetValue,
-};
-
-#[repr(C)]
-#[derive(Clone)]
-pub struct Map<K, V>
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- object: ObjectRef,
- _data: PhantomData<(K, V)>,
-}
-
-// TODO(@jroesch): convert to use generics instead of casting inside
-// the implementation.
-external! {
- #[name("runtime.MapSize")]
- fn map_size(map: ObjectRef) -> i64;
- #[name("runtime.MapGetItem")]
- fn map_get_item(map_object: ObjectRef, key: ObjectRef) -> ObjectRef;
- #[name("runtime.MapCount")]
- fn map_count(map: ObjectRef, key: ObjectRef) -> ObjectRef;
- #[name("runtime.MapItems")]
- fn map_items(map: ObjectRef) -> Array<ObjectRef>;
-}
-
-impl<'a, K: 'a, V: 'a> FromIterator<(&'a K, &'a V)> for Map<K, V>
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- fn from_iter<T: IntoIterator<Item = (&'a K, &'a V)>>(iter: T) -> Self {
- let iter = iter.into_iter();
- let (lower_bound, upper_bound) = iter.size_hint();
- let mut buffer: Vec<ArgValue> = Vec::with_capacity(upper_bound.unwrap_or(lower_bound) * 2);
- for (k, v) in iter {
- buffer.push(k.into_arg_value());
- buffer.push(v.into_arg_value());
- }
- Self::from_data(buffer).expect("failed to convert from data")
- }
-}
-
-impl<K, V> Map<K, V>
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- pub fn from_data(data: Vec<ArgValue>) -> Result<Map<K, V>> {
- let func = Function::get("runtime.Map").expect(
- "runtime.Map function is not registered, this is most likely a build or linking error",
- );
-
- let map_data: ObjectPtr<Object> = func.invoke(data)?.try_into()?;
-
- debug_assert!(
- map_data.count() >= 1,
- "map_data count is {}",
- map_data.count()
- );
-
- Ok(Map {
- object: map_data.into(),
- _data: PhantomData,
- })
- }
-
- pub fn get(&self, key: &K) -> Result<V>
- where
- V: TryFrom<RetValue, Error = Error>,
- {
- let key = key.clone();
- let oref: ObjectRef = map_get_item(self.object.clone(), key.upcast())?;
- oref.downcast()
- }
-
- pub fn empty() -> Self {
- Self::from_iter(vec![].into_iter())
- }
-
- //(@jroesch): I don't think this is a correct implementation.
- pub fn null() -> Self {
- Map {
- object: ObjectRef::null(),
- _data: PhantomData,
- }
- }
-}
-
-pub struct IntoIter<K, V> {
- // NB: due to FFI this isn't as lazy as one might like
- key_and_values: Array<ObjectRef>,
- next_key: i64,
- _data: PhantomData<(K, V)>,
-}
-
-impl<K, V> Iterator for IntoIter<K, V>
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- type Item = (K, V);
-
- #[inline]
- fn next(&mut self) -> Option<(K, V)> {
- if self.next_key < self.key_and_values.len() {
- let key = self
- .key_and_values
- .get(self.next_key as isize)
- .expect("this should always succeed");
- let value = self
- .key_and_values
- .get((self.next_key as isize) + 1)
- .expect("this should always succeed");
- self.next_key += 2;
- Some((key.downcast().unwrap(), value.downcast().unwrap()))
- } else {
- None
- }
- }
-
- #[inline]
- fn size_hint(&self) -> (usize, Option<usize>) {
- ((self.key_and_values.len() / 2) as usize, None)
- }
-}
-
-impl<K, V> IntoIterator for Map<K, V>
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- type Item = (K, V);
- type IntoIter = IntoIter<K, V>;
-
- fn into_iter(self) -> IntoIter<K, V> {
- let items = map_items(self.object).expect("unable to get map items");
- IntoIter {
- key_and_values: items,
- next_key: 0,
- _data: PhantomData,
- }
- }
-}
-
-use std::fmt;
-
-impl<K, V> fmt::Debug for Map<K, V>
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
- let ctr = debug_print(self.object.clone()).unwrap();
- fmt.write_fmt(format_args!("{:?}", ctr))
- }
-}
-
-impl<K, V, S> From<Map<K, V>> for HashMap<K, V, S>
-where
- K: Eq + std::hash::Hash,
- K: IsObjectRef,
- V: IsObjectRef,
- S: std::hash::BuildHasher + std::default::Default,
-{
- fn from(map: Map<K, V>) -> HashMap<K, V, S> {
- HashMap::from_iter(map.into_iter())
- }
-}
-
-impl<'a, K, V> From<&'a Map<K, V>> for ArgValue<'a>
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- fn from(map: &'a Map<K, V>) -> ArgValue<'a> {
- (&map.object).into()
- }
-}
-
-impl<K, V> From<Map<K, V>> for RetValue
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- fn from(map: Map<K, V>) -> RetValue {
- map.object.into()
- }
-}
-
-impl<'a, K, V> TryFrom<ArgValue<'a>> for Map<K, V>
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- type Error = Error;
-
- fn try_from(array: ArgValue<'a>) -> Result<Map<K, V>> {
- let object_ref: ObjectRef = array.try_into()?;
- // TODO: type check
- Ok(Map {
- object: object_ref,
- _data: PhantomData,
- })
- }
-}
-
-impl<K, V> TryFrom<RetValue> for Map<K, V>
-where
- K: IsObjectRef,
- V: IsObjectRef,
-{
- type Error = Error;
-
- fn try_from(array: RetValue) -> Result<Map<K, V>> {
- let object_ref = array.try_into()?;
- // TODO: type check
- Ok(Map {
- object: object_ref,
- _data: PhantomData,
- })
- }
-}
-
-#[cfg(test)]
-mod test {
- use std::collections::HashMap;
-
- use super::*;
- use crate::string::String as TString;
-
- #[test]
- fn test_from_into_hash_map() {
- let mut std_map: HashMap<TString, TString> = HashMap::new();
- std_map.insert("key1".into(), "value1".into());
- std_map.insert("key2".into(), "value2".into());
- let tvm_map = Map::from_iter(std_map.iter());
- let back_map = tvm_map.into();
- assert_eq!(std_map, back_map);
- }
-}