@@ -24,6 +24,7 @@ namespace executorch::backends::cuda {
2424
2525using executorch::aten::SizesType;
2626using executorch::aten::StridesType;
27+ using executorch::backends::aoti::aoti_torch_dtype_bool;
2728using executorch::backends::aoti::aoti_torch_get_device_index;
2829using executorch::backends::aoti::aoti_torch_get_dtype;
2930using executorch::backends::aoti::aoti_torch_get_sizes;
@@ -800,6 +801,126 @@ AOTITorchError aoti_torch_new_tensor_handle(
800801
801802 return Error::Ok;
802803}
804+
805+ AOTITorchError aoti_torch_item_bool (Tensor* tensor, bool * ret_value) {
806+ // Validate input parameters
807+ ET_CHECK_OR_RETURN_ERROR (
808+ tensor != nullptr ,
809+ InvalidArgument,
810+ " aoti_torch_item_bool failed: tensor is null" );
811+
812+ ET_CHECK_OR_RETURN_ERROR (
813+ ret_value != nullptr ,
814+ InvalidArgument,
815+ " aoti_torch_item_bool failed: ret_value is null" );
816+
817+ // Validate that tensor dtype is bool
818+ int32_t dtype;
819+ ET_CHECK_OK_OR_RETURN_ERROR (aoti_torch_get_dtype (tensor, &dtype));
820+
821+ ET_CHECK_OR_RETURN_ERROR (
822+ dtype == aoti_torch_dtype_bool (),
823+ InvalidArgument,
824+ " aoti_torch_item_bool failed: tensor dtype is not bool (got %d)" ,
825+ dtype);
826+
827+ // Get the data pointer
828+ const void * data_ptr = tensor->const_data_ptr ();
829+ ET_CHECK_OR_RETURN_ERROR (
830+ data_ptr != nullptr ,
831+ InvalidArgument,
832+ " aoti_torch_item_bool failed: tensor data pointer is null" );
833+
834+ // Check if tensor is on CUDA or CPU
835+ cudaPointerAttributes attributes{};
836+ ET_CUDA_CHECK_OR_RETURN_ERROR (
837+ cudaPointerGetAttributes (&attributes, data_ptr));
838+
839+ if (attributes.type == cudaMemoryTypeDevice) {
840+ // CUDA memory case: copy from device to host
841+ bool device_value;
842+ ET_CUDA_CHECK_OR_RETURN_ERROR (cudaMemcpy (
843+ &device_value, data_ptr, sizeof (bool ), cudaMemcpyDeviceToHost));
844+ *ret_value = device_value;
845+ } else {
846+ // CPU memory case: direct access
847+ const bool * bool_ptr = static_cast <const bool *>(data_ptr);
848+ *ret_value = *bool_ptr;
849+ }
850+
851+ return Error::Ok;
852+ }
853+
854+ AOTITorchError aoti_torch_assign_tensors_out (Tensor* src, Tensor** ret_dst) {
855+ // Validate input parameters
856+ ET_CHECK_OR_RETURN_ERROR (
857+ src != nullptr ,
858+ InvalidArgument,
859+ " aoti_torch_assign_tensors_out failed: src is null" );
860+
861+ ET_CHECK_OR_RETURN_ERROR (
862+ ret_dst != nullptr ,
863+ InvalidArgument,
864+ " aoti_torch_assign_tensors_out failed: ret_dst is null" );
865+
866+ // Get the data pointer from the source tensor
867+ void * data_ptr = src->mutable_data_ptr ();
868+ ET_CHECK_OR_RETURN_ERROR (
869+ data_ptr != nullptr ,
870+ InvalidArgument,
871+ " Source tensor has null data pointer" );
872+
873+ // Check if the given memory is in the map, if not return error
874+ auto memory_it = memory_to_n_tensor.find (data_ptr);
875+ ET_CHECK_OR_RETURN_ERROR (
876+ memory_it != memory_to_n_tensor.end (),
877+ InvalidArgument,
878+ " Memory address %p is not being tracked by reference counting system" ,
879+ data_ptr);
880+
881+ // Get dtype from source tensor
882+ int32_t dtype = 0 ;
883+ ET_CHECK_OK_OR_RETURN_ERROR (aoti_torch_get_dtype (src, &dtype));
884+
885+ // Get sizes and strides from source tensor
886+ int64_t * sizes_ptr;
887+ int64_t * strides_ptr;
888+ ET_CHECK_OK_OR_RETURN_ERROR (aoti_torch_get_sizes (src, &sizes_ptr));
889+ ET_CHECK_OK_OR_RETURN_ERROR (aoti_torch_get_strides (src, &strides_ptr));
890+
891+ int64_t ndim = src->dim ();
892+
893+ // Convert to vectors
894+ std::vector<SizesType> sizes = convert_sizes_to_vector (ndim, sizes_ptr);
895+ std::vector<StridesType> strides =
896+ convert_strides_to_vector (ndim, sizes_ptr, strides_ptr);
897+
898+ // Create new tensor view that shares the same memory as source tensor
899+ std::shared_ptr<Tensor> tensor = make_tensor (
900+ sizes,
901+ data_ptr, // Share the same memory from source tensor
902+ {}, // dim_order (empty, will be auto-generated)
903+ strides,
904+ dtype_to_scalar_type (dtype));
905+
906+ ET_CHECK_OR_RETURN_ERROR (
907+ tensor != nullptr ,
908+ InvalidArgument,
909+ " Failed to create tensor view in aoti_torch_assign_tensors_out" );
910+
911+ // Store the tensor so it doesn't get destroyed
912+ tensors.insert (tensor);
913+
914+ *ret_dst = tensor.get ();
915+
916+ // Increment the reference count for this memory address only if it is owned
917+ // by tensor
918+ memory_to_n_tensor[data_ptr] = memory_to_n_tensor[data_ptr] == NOT_OWN
919+ ? NOT_OWN
920+ : memory_to_n_tensor[data_ptr] + 1 ;
921+
922+ return Error::Ok;
923+ }
803924} // extern "C"
804925
805926} // namespace executorch::backends::cuda
0 commit comments