Skip to content

Commit

Permalink
Tensor : Fix construction from BoolData
Browse files Browse the repository at this point in the history
I was creating a tensor that referenced temporary data directly, so when that data went out of scope it was just referencing junk. Now we're creating a tensor which owns its own memory.
  • Loading branch information
johnhaddon committed Nov 7, 2024
1 parent aaa0ba0 commit aa1319e
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/GafferML/Tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,19 @@ Tensor::Tensor( const IECore::ConstDataPtr &data, std::vector<int64_t> shape )
}
}

Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu( OrtArenaAllocator, OrtMemTypeDefault );

if constexpr( std::is_same_v<DataType, BoolVectorData> )
{
// Special case for the vector of bool fiasco.
auto array = std::make_unique<bool[]>( typedData->readable().size() );
std::copy( typedData->readable().begin(), typedData->readable().end(), array.get() );
m_state = new State{
Ort::Value::CreateTensor(
memoryInfo.GetConst(),
array.get(), typedData->readable().size(),
shape.data(), shape.size()
),
nullptr
};
Ort::AllocatorWithDefaultOptions allocator;
Ort::Value value = Ort::Value::CreateTensor(
allocator, shape.data(), shape.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL
);
std::copy( typedData->readable().begin(), typedData->readable().end(), value.GetTensorMutableData<bool>() );
m_state = new State{ std::move( value ), nullptr };
}
else if constexpr( HasTensorType<BaseType>::value )
{
Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu( OrtArenaAllocator, OrtMemTypeDefault );
m_state = new State{
Ort::Value::CreateTensor(
memoryInfo.GetConst(),
Expand Down

0 comments on commit aa1319e

Please sign in to comment.