M
Modular•10mo ago
Jake Brooks

Determining Tensor datatype at runtime

Hi folks! I'm working on a project where I need to extract a bunch of numeric data from a file, and the datatype for the file data is contained within the metadata for the file. I want the data within to always be read into a Tensor, but I'm not sure how to handle the datatyping at runtime, since you have to define the Tensor with a datatype at compile time. I have a feeling the answer lies in pointers, but I'm used to Python so I'm still learning about them 🙂 Grateful for any advice!
2 Replies
Michael K
Michael K•9mo ago
fn do_stuff[T: DType]():
# you have your DType as a parameter at compile time to do whatever you like
var data = Tensor[T](1, 2)

fn main():
# get your actual type name as a string from a file
var type = 'f32'
if type == 'f32':
do_stuff[DType.float32]()
else:
do_stuff[DType.float64]()
fn do_stuff[T: DType]():
# you have your DType as a parameter at compile time to do whatever you like
var data = Tensor[T](1, 2)

fn main():
# get your actual type name as a string from a file
var type = 'f32'
if type == 'f32':
do_stuff[DType.float32]()
else:
do_stuff[DType.float64]()
Jake Brooks
Jake BrooksOP•9mo ago
ah of course, thank you!
Want results from more Discord servers?
Add your server